1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, 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 In_Object_Declaration
(N
: Node_Id
) return Boolean;
94 -- Return True if N is part of an object declaration, False otherwise
96 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean;
97 -- Returns true if N is an aggregate used to initialize the components
98 -- of a statically allocated dispatch table.
100 function Late_Expansion
103 Target
: Node_Id
) return List_Id
;
104 -- This routine implements top-down expansion of nested aggregates. In
105 -- doing so, it avoids the generation of temporaries at each level. N is
106 -- a nested record or array aggregate with the Expansion_Delayed flag.
107 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
108 -- expression that will hold the result of the aggregate expansion.
110 function Make_OK_Assignment_Statement
113 Expression
: Node_Id
) return Node_Id
;
114 -- This is like Make_Assignment_Statement, except that Assignment_OK
115 -- is set in the left operand. All assignments built by this unit use
116 -- this routine. This is needed to deal with assignments to initialized
117 -- constants that are done in place.
120 (Obj_Type
: Entity_Id
;
121 Typ
: Entity_Id
) return Boolean;
122 -- A static array aggregate in an object declaration can in most cases be
123 -- expanded in place. The one exception is when the aggregate is given
124 -- with component associations that specify different bounds from those of
125 -- the type definition in the object declaration. In this pathological
126 -- case the aggregate must slide, and we must introduce an intermediate
127 -- temporary to hold it.
129 -- The same holds in an assignment to one-dimensional array of arrays,
130 -- when a component may be given with bounds that differ from those of the
133 function Number_Of_Choices
(N
: Node_Id
) return Nat
;
134 -- Returns the number of discrete choices (not including the others choice
135 -- if present) contained in (sub-)aggregate N.
137 procedure Process_Transient_Component
139 Comp_Typ
: Entity_Id
;
141 Fin_Call
: out Node_Id
;
142 Hook_Clear
: out Node_Id
;
143 Aggr
: Node_Id
:= Empty
;
144 Stmts
: List_Id
:= No_List
);
145 -- Subsidiary to the expansion of array and record aggregates. Generate
146 -- part of the necessary code to finalize a transient component. Comp_Typ
147 -- is the component type. Init_Expr is the initialization expression of the
148 -- component which is always a function call. Fin_Call is the finalization
149 -- call used to clean up the transient function result. Hook_Clear is the
150 -- hook reset statement. Aggr and Stmts both control the placement of the
151 -- generated code. Aggr is the related aggregate. If present, all code is
152 -- inserted prior to Aggr using Insert_Action. Stmts is the initialization
153 -- statements of the component. If present, all code is added to Stmts.
155 procedure Process_Transient_Component_Completion
159 Hook_Clear
: Node_Id
;
161 -- Subsidiary to the expansion of array and record aggregates. Generate
162 -- part of the necessary code to finalize a transient component. Aggr is
163 -- the related aggregate. Fin_Clear is the finalization call used to clean
164 -- up the transient component. Hook_Clear is the hook reset statment. Stmts
165 -- is the initialization statement list for the component. All generated
166 -- code is added to Stmts.
168 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
);
169 -- Sort the Case Table using the Lower Bound of each Choice as the key.
170 -- A simple insertion sort is used since the number of choices in a case
171 -- statement of variant part will usually be small and probably in near
174 ------------------------------------------------------
175 -- Local subprograms for Record Aggregate Expansion --
176 ------------------------------------------------------
178 function Is_Build_In_Place_Aggregate_Return
(N
: Node_Id
) return Boolean;
179 -- True if N is an aggregate (possibly qualified or converted) that is
180 -- being returned from a build-in-place function.
182 function Build_Record_Aggr_Code
185 Lhs
: Node_Id
) return List_Id
;
186 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
187 -- aggregate. Target is an expression containing the location on which the
188 -- component by component assignments will take place. Returns the list of
189 -- assignments plus all other adjustments needed for tagged and controlled
192 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
);
193 -- Transform a record aggregate into a sequence of assignments performed
194 -- component by component. N is an N_Aggregate or N_Extension_Aggregate.
195 -- Typ is the type of the record aggregate.
197 procedure Expand_Record_Aggregate
199 Orig_Tag
: Node_Id
:= Empty
;
200 Parent_Expr
: Node_Id
:= Empty
);
201 -- This is the top level procedure for record aggregate expansion.
202 -- Expansion for record aggregates needs expand aggregates for tagged
203 -- record types. Specifically Expand_Record_Aggregate adds the Tag
204 -- field in front of the Component_Association list that was created
205 -- during resolution by Resolve_Record_Aggregate.
207 -- N is the record aggregate node.
208 -- Orig_Tag is the value of the Tag that has to be provided for this
209 -- specific aggregate. It carries the tag corresponding to the type
210 -- of the outermost aggregate during the recursive expansion
211 -- Parent_Expr is the ancestor part of the original extension
214 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean;
215 -- Return true if one of the components is of a discriminated type with
216 -- defaults. An aggregate for a type with mutable components must be
217 -- expanded into individual assignments.
219 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
);
220 -- If the type of the aggregate is a type extension with renamed discrimi-
221 -- nants, we must initialize the hidden discriminants of the parent.
222 -- Otherwise, the target object must not be initialized. The discriminants
223 -- are initialized by calling the initialization procedure for the type.
224 -- This is incorrect if the initialization of other components has any
225 -- side effects. We restrict this call to the case where the parent type
226 -- has a variant part, because this is the only case where the hidden
227 -- discriminants are accessed, namely when calling discriminant checking
228 -- functions of the parent type, and when applying a stream attribute to
229 -- an object of the derived type.
231 -----------------------------------------------------
232 -- Local Subprograms for Array Aggregate Expansion --
233 -----------------------------------------------------
235 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean;
236 -- Very large static aggregates present problems to the back-end, and are
237 -- transformed into assignments and loops. This function verifies that the
238 -- total number of components of an aggregate is acceptable for rewriting
239 -- into a purely positional static form. Aggr_Size_OK must be called before
242 -- This function also detects and warns about one-component aggregates that
243 -- appear in a nonstatic context. Even if the component value is static,
244 -- such an aggregate must be expanded into an assignment.
246 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean;
247 -- This function checks if array aggregate N can be processed directly
248 -- by the backend. If this is the case, True is returned.
250 function Build_Array_Aggr_Code
255 Scalar_Comp
: Boolean;
256 Indexes
: List_Id
:= No_List
) return List_Id
;
257 -- This recursive routine returns a list of statements containing the
258 -- loops and assignments that are needed for the expansion of the array
261 -- N is the (sub-)aggregate node to be expanded into code. This node has
262 -- been fully analyzed, and its Etype is properly set.
264 -- Index is the index node corresponding to the array subaggregate N
266 -- Into is the target expression into which we are copying the aggregate.
267 -- Note that this node may not have been analyzed yet, and so the Etype
268 -- field may not be set.
270 -- Scalar_Comp is True if the component type of the aggregate is scalar
272 -- Indexes is the current list of expressions used to index the object we
275 procedure Convert_Array_Aggr_In_Allocator
279 -- If the aggregate appears within an allocator and can be expanded in
280 -- place, this routine generates the individual assignments to components
281 -- of the designated object. This is an optimization over the general
282 -- case, where a temporary is first created on the stack and then used to
283 -- construct the allocated object on the heap.
285 procedure Convert_To_Positional
287 Max_Others_Replicate
: Nat
:= 5;
288 Handle_Bit_Packed
: Boolean := False);
289 -- If possible, convert named notation to positional notation. This
290 -- conversion is possible only in some static cases. If the conversion is
291 -- possible, then N is rewritten with the analyzed converted aggregate.
292 -- The parameter Max_Others_Replicate controls the maximum number of
293 -- values corresponding to an others choice that will be converted to
294 -- positional notation (the default of 5 is the normal limit, and reflects
295 -- the fact that normally the loop is better than a lot of separate
296 -- assignments). Note that this limit gets overridden in any case if
297 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
298 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
299 -- not expect the back end to handle bit packed arrays, so the normal case
300 -- of conversion is pointless), but in the special case of a call from
301 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
302 -- these are cases we handle in there.
304 -- It would seem useful to have a higher default for Max_Others_Replicate,
305 -- but aggregates in the compiler make this impossible: the compiler
306 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
309 procedure Expand_Array_Aggregate
(N
: Node_Id
);
310 -- This is the top-level routine to perform array aggregate expansion.
311 -- N is the N_Aggregate node to be expanded.
313 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean;
314 -- For two-dimensional packed aggregates with constant bounds and constant
315 -- components, it is preferable to pack the inner aggregates because the
316 -- whole matrix can then be presented to the back-end as a one-dimensional
317 -- list of literals. This is much more efficient than expanding into single
318 -- component assignments. This function determines if the type Typ is for
319 -- an array that is suitable for this optimization: it returns True if Typ
320 -- is a two dimensional bit packed array with component size 1, 2, or 4.
322 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean;
323 -- Given an array aggregate, this function handles the case of a packed
324 -- array aggregate with all constant values, where the aggregate can be
325 -- evaluated at compile time. If this is possible, then N is rewritten
326 -- to be its proper compile time value with all the components properly
327 -- assembled. The expression is analyzed and resolved and True is returned.
328 -- If this transformation is not possible, N is unchanged and False is
331 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean;
332 -- If the type of the aggregate is a two-dimensional bit_packed array
333 -- it may be transformed into an array of bytes with constant values,
334 -- and presented to the back-end as a static value. The function returns
335 -- false if this transformation cannot be performed. THis is similar to,
336 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
342 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean is
351 -- Determines the maximum size of an array aggregate produced by
352 -- converting named to positional notation (e.g. from others clauses).
353 -- This avoids running away with attempts to convert huge aggregates,
354 -- which hit memory limits in the backend.
356 function Component_Count
(T
: Entity_Id
) return Nat
;
357 -- The limit is applied to the total number of subcomponents that the
358 -- aggregate will have, which is the number of static expressions
359 -- that will appear in the flattened array. This requires a recursive
360 -- computation of the number of scalar components of the structure.
362 ---------------------
363 -- Component_Count --
364 ---------------------
366 function Component_Count
(T
: Entity_Id
) return Nat
is
371 if Is_Scalar_Type
(T
) then
374 elsif Is_Record_Type
(T
) then
375 Comp
:= First_Component
(T
);
376 while Present
(Comp
) loop
377 Res
:= Res
+ Component_Count
(Etype
(Comp
));
378 Next_Component
(Comp
);
383 elsif Is_Array_Type
(T
) then
385 Lo
: constant Node_Id
:=
386 Type_Low_Bound
(Etype
(First_Index
(T
)));
387 Hi
: constant Node_Id
:=
388 Type_High_Bound
(Etype
(First_Index
(T
)));
390 Siz
: constant Nat
:= Component_Count
(Component_Type
(T
));
393 -- Check for superflat arrays, i.e. arrays with such bounds
394 -- as 4 .. 2, to insure that this function never returns a
395 -- meaningless negative value.
397 if not Compile_Time_Known_Value
(Lo
)
398 or else not Compile_Time_Known_Value
(Hi
)
399 or else Expr_Value
(Hi
) < Expr_Value
(Lo
)
404 -- If the number of components is greater than Int'Last,
405 -- then return Int'Last, so caller will return False (Aggr
406 -- size is not OK). Otherwise, UI_To_Int will crash.
409 UI
: constant Uint
:=
410 Expr_Value
(Hi
) - Expr_Value
(Lo
) + 1;
412 if UI_Is_In_Int_Range
(UI
) then
413 return Siz
* UI_To_Int
(UI
);
422 -- Can only be a null for an access type
428 -- Start of processing for Aggr_Size_OK
431 -- The normal aggregate limit is 500000, but we increase this limit to
432 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
433 -- Restrictions (No_Implicit_Loops) is specified, since in either case
434 -- we are at risk of declaring the program illegal because of this
435 -- limit. We also increase the limit when Static_Elaboration_Desired,
436 -- given that this means that objects are intended to be placed in data
439 -- We also increase the limit if the aggregate is for a packed two-
440 -- dimensional array, because if components are static it is much more
441 -- efficient to construct a one-dimensional equivalent array with static
444 -- Conversely, we decrease the maximum size if none of the above
445 -- requirements apply, and if the aggregate has a single component
446 -- association, which will be more efficient if implemented with a loop.
448 -- Finally, we use a small limit in CodePeer mode where we favor loops
449 -- instead of thousands of single assignments (from large aggregates).
451 Max_Aggr_Size
:= 500000;
453 if CodePeer_Mode
then
454 Max_Aggr_Size
:= 100;
456 elsif Restriction_Active
(No_Elaboration_Code
)
457 or else Restriction_Active
(No_Implicit_Loops
)
458 or else Is_Two_Dim_Packed_Array
(Typ
)
459 or else (Ekind
(Current_Scope
) = E_Package
460 and then Static_Elaboration_Desired
(Current_Scope
))
462 Max_Aggr_Size
:= 2 ** 24;
464 elsif No
(Expressions
(N
))
465 and then No
(Next
(First
(Component_Associations
(N
))))
467 Max_Aggr_Size
:= 5000;
470 Siz
:= Component_Count
(Component_Type
(Typ
));
472 Indx
:= First_Index
(Typ
);
473 while Present
(Indx
) loop
474 Lo
:= Type_Low_Bound
(Etype
(Indx
));
475 Hi
:= Type_High_Bound
(Etype
(Indx
));
477 -- Bounds need to be known at compile time
479 if not Compile_Time_Known_Value
(Lo
)
480 or else not Compile_Time_Known_Value
(Hi
)
485 Lov
:= Expr_Value
(Lo
);
486 Hiv
:= Expr_Value
(Hi
);
488 -- A flat array is always safe
494 -- One-component aggregates are suspicious, and if the context type
495 -- is an object declaration with nonstatic bounds it will trip gcc;
496 -- such an aggregate must be expanded into a single assignment.
498 if Hiv
= Lov
and then Nkind
(Parent
(N
)) = N_Object_Declaration
then
500 Index_Type
: constant Entity_Id
:=
502 (First_Index
(Etype
(Defining_Identifier
(Parent
(N
)))));
506 if not Compile_Time_Known_Value
(Type_Low_Bound
(Index_Type
))
507 or else not Compile_Time_Known_Value
508 (Type_High_Bound
(Index_Type
))
510 if Present
(Component_Associations
(N
)) then
513 (Choice_List
(First
(Component_Associations
(N
))));
515 if Is_Entity_Name
(Indx
)
516 and then not Is_Type
(Entity
(Indx
))
519 ("single component aggregate in "
520 & "non-static context??", Indx
);
521 Error_Msg_N
("\maybe subtype name was meant??", Indx
);
531 Rng
: constant Uint
:= Hiv
- Lov
+ 1;
534 -- Check if size is too large
536 if not UI_Is_In_Int_Range
(Rng
) then
540 Siz
:= Siz
* UI_To_Int
(Rng
);
544 or else Siz
> Max_Aggr_Size
549 -- Bounds must be in integer range, for later array construction
551 if not UI_Is_In_Int_Range
(Lov
)
553 not UI_Is_In_Int_Range
(Hiv
)
564 ---------------------------------
565 -- Backend_Processing_Possible --
566 ---------------------------------
568 -- Backend processing by Gigi/gcc is possible only if all the following
569 -- conditions are met:
571 -- 1. N is fully positional
573 -- 2. N is not a bit-packed array aggregate;
575 -- 3. The size of N's array type must be known at compile time. Note
576 -- that this implies that the component size is also known
578 -- 4. The array type of N does not follow the Fortran layout convention
579 -- or if it does it must be 1 dimensional.
581 -- 5. The array component type may not be tagged (which could necessitate
582 -- reassignment of proper tags).
584 -- 6. The array component type must not have unaligned bit components
586 -- 7. None of the components of the aggregate may be bit unaligned
589 -- 8. There cannot be delayed components, since we do not know enough
590 -- at this stage to know if back end processing is possible.
592 -- 9. There cannot be any discriminated record components, since the
593 -- back end cannot handle this complex case.
595 -- 10. No controlled actions need to be generated for components
597 -- 11. When generating C code, N must be part of a N_Object_Declaration
599 -- 12. When generating C code, N must not include function calls
601 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean is
602 Typ
: constant Entity_Id
:= Etype
(N
);
603 -- Typ is the correct constrained array subtype of the aggregate
605 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean;
606 -- This routine checks components of aggregate N, enforcing checks
607 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
608 -- are performed on subaggregates. The Index value is the current index
609 -- being checked in the multidimensional case.
611 ---------------------
612 -- Component_Check --
613 ---------------------
615 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean is
616 function Ultimate_Original_Expression
(N
: Node_Id
) return Node_Id
;
617 -- Given a type conversion or an unchecked type conversion N, return
618 -- its innermost original expression.
620 ----------------------------------
621 -- Ultimate_Original_Expression --
622 ----------------------------------
624 function Ultimate_Original_Expression
(N
: Node_Id
) return Node_Id
is
625 Expr
: Node_Id
:= Original_Node
(N
);
628 while Nkind_In
(Expr
, N_Type_Conversion
,
629 N_Unchecked_Type_Conversion
)
631 Expr
:= Original_Node
(Expression
(Expr
));
635 end Ultimate_Original_Expression
;
641 -- Start of processing for Component_Check
644 -- Checks 1: (no component associations)
646 if Present
(Component_Associations
(N
)) then
650 -- Checks 11: The C code generator cannot handle aggregates that are
651 -- not part of an object declaration.
653 if Modify_Tree_For_C
then
655 Par
: Node_Id
:= Parent
(N
);
658 -- Skip enclosing nested aggregates and their qualified
661 while Nkind
(Par
) = N_Aggregate
662 or else Nkind
(Par
) = N_Qualified_Expression
667 if Nkind
(Par
) /= N_Object_Declaration
then
673 -- Checks on components
675 -- Recurse to check subaggregates, which may appear in qualified
676 -- expressions. If delayed, the front-end will have to expand.
677 -- If the component is a discriminated record, treat as nonstatic,
678 -- as the back-end cannot handle this properly.
680 Expr
:= First
(Expressions
(N
));
681 while Present
(Expr
) loop
683 -- Checks 8: (no delayed components)
685 if Is_Delayed_Aggregate
(Expr
) then
689 -- Checks 9: (no discriminated records)
691 if Present
(Etype
(Expr
))
692 and then Is_Record_Type
(Etype
(Expr
))
693 and then Has_Discriminants
(Etype
(Expr
))
698 -- Checks 7. Component must not be bit aligned component
700 if Possible_Bit_Aligned_Component
(Expr
) then
704 -- Checks 12: (no function call)
708 Nkind
(Ultimate_Original_Expression
(Expr
)) = N_Function_Call
713 -- Recursion to following indexes for multiple dimension case
715 if Present
(Next_Index
(Index
))
716 and then not Component_Check
(Expr
, Next_Index
(Index
))
721 -- All checks for that component finished, on to next
729 -- Start of processing for Backend_Processing_Possible
732 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
734 if Is_Bit_Packed_Array
(Typ
) or else Needs_Finalization
(Typ
) then
738 -- If component is limited, aggregate must be expanded because each
739 -- component assignment must be built in place.
741 if Is_Limited_View
(Component_Type
(Typ
)) then
745 -- Checks 4 (array must not be multidimensional Fortran case)
747 if Convention
(Typ
) = Convention_Fortran
748 and then Number_Dimensions
(Typ
) > 1
753 -- Checks 3 (size of array must be known at compile time)
755 if not Size_Known_At_Compile_Time
(Typ
) then
759 -- Checks on components
761 if not Component_Check
(N
, First_Index
(Typ
)) then
765 -- Checks 5 (if the component type is tagged, then we may need to do
766 -- tag adjustments. Perhaps this should be refined to check for any
767 -- component associations that actually need tag adjustment, similar
768 -- to the test in Component_OK_For_Backend for record aggregates with
769 -- tagged components, but not clear whether it's worthwhile ???; in the
770 -- case of virtual machines (no Tagged_Type_Expansion), object tags are
771 -- handled implicitly).
773 if Is_Tagged_Type
(Component_Type
(Typ
))
774 and then Tagged_Type_Expansion
779 -- Checks 6 (component type must not have bit aligned components)
781 if Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
)) then
785 -- Backend processing is possible
787 Set_Size_Known_At_Compile_Time
(Etype
(N
), True);
789 end Backend_Processing_Possible
;
791 ---------------------------
792 -- Build_Array_Aggr_Code --
793 ---------------------------
795 -- The code that we generate from a one dimensional aggregate is
797 -- 1. If the subaggregate contains discrete choices we
799 -- (a) Sort the discrete choices
801 -- (b) Otherwise for each discrete choice that specifies a range we
802 -- emit a loop. If a range specifies a maximum of three values, or
803 -- we are dealing with an expression we emit a sequence of
804 -- assignments instead of a loop.
806 -- (c) Generate the remaining loops to cover the others choice if any
808 -- 2. If the aggregate contains positional elements we
810 -- (a) translate the positional elements in a series of assignments
812 -- (b) Generate a final loop to cover the others choice if any.
813 -- Note that this final loop has to be a while loop since the case
815 -- L : Integer := Integer'Last;
816 -- H : Integer := Integer'Last;
817 -- A : array (L .. H) := (1, others =>0);
819 -- cannot be handled by a for loop. Thus for the following
821 -- array (L .. H) := (.. positional elements.., others =>E);
823 -- we always generate something like:
825 -- J : Index_Type := Index_Of_Last_Positional_Element;
827 -- J := Index_Base'Succ (J)
831 function Build_Array_Aggr_Code
836 Scalar_Comp
: Boolean;
837 Indexes
: List_Id
:= No_List
) return List_Id
839 Loc
: constant Source_Ptr
:= Sloc
(N
);
840 Index_Base
: constant Entity_Id
:= Base_Type
(Etype
(Index
));
841 Index_Base_L
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
842 Index_Base_H
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
844 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
;
845 -- Returns an expression where Val is added to expression To, unless
846 -- To+Val is provably out of To's base type range. To must be an
847 -- already analyzed expression.
849 function Empty_Range
(L
, H
: Node_Id
) return Boolean;
850 -- Returns True if the range defined by L .. H is certainly empty
852 function Equal
(L
, H
: Node_Id
) return Boolean;
853 -- Returns True if L = H for sure
855 function Index_Base_Name
return Node_Id
;
856 -- Returns a new reference to the index type name
861 In_Loop
: Boolean := False) return List_Id
;
862 -- Ind must be a side-effect-free expression. If the input aggregate N
863 -- to Build_Loop contains no subaggregates, then this function returns
864 -- the assignment statement:
866 -- Into (Indexes, Ind) := Expr;
868 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
869 -- when the assignment appears within a generated loop.
871 -- Ada 2005 (AI-287): In case of default initialized component, Expr
872 -- is empty and we generate a call to the corresponding IP subprogram.
874 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
875 -- Nodes L and H must be side-effect-free expressions. If the input
876 -- aggregate N to Build_Loop contains no subaggregates, this routine
877 -- returns the for loop statement:
879 -- for J in Index_Base'(L) .. Index_Base'(H) loop
880 -- Into (Indexes, J) := Expr;
883 -- Otherwise we call Build_Code recursively. As an optimization if the
884 -- loop covers 3 or fewer scalar elements we generate a sequence of
886 -- If the component association that generates the loop comes from an
887 -- Iterated_Component_Association, the loop parameter has the name of
888 -- the corresponding parameter in the original construct.
890 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
891 -- Nodes L and H must be side-effect-free expressions. If the input
892 -- aggregate N to Build_Loop contains no subaggregates, this routine
893 -- returns the while loop statement:
895 -- J : Index_Base := L;
897 -- J := Index_Base'Succ (J);
898 -- Into (Indexes, J) := Expr;
901 -- Otherwise we call Build_Code recursively
903 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
;
904 -- For an association with a box, use value given by aspect
905 -- Default_Component_Value of array type if specified, else use
906 -- value given by aspect Default_Value for component type itself
907 -- if specified, else return Empty.
909 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean;
910 function Local_Expr_Value
(E
: Node_Id
) return Uint
;
911 -- These two Local routines are used to replace the corresponding ones
912 -- in sem_eval because while processing the bounds of an aggregate with
913 -- discrete choices whose index type is an enumeration, we build static
914 -- expressions not recognized by Compile_Time_Known_Value as such since
915 -- they have not yet been analyzed and resolved. All the expressions in
916 -- question are things like Index_Base_Name'Val (Const) which we can
917 -- easily recognize as being constant.
923 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
is
928 U_Val
: constant Uint
:= UI_From_Int
(Val
);
931 -- Note: do not try to optimize the case of Val = 0, because
932 -- we need to build a new node with the proper Sloc value anyway.
934 -- First test if we can do constant folding
936 if Local_Compile_Time_Known_Value
(To
) then
937 U_To
:= Local_Expr_Value
(To
) + Val
;
939 -- Determine if our constant is outside the range of the index.
940 -- If so return an Empty node. This empty node will be caught
941 -- by Empty_Range below.
943 if Compile_Time_Known_Value
(Index_Base_L
)
944 and then U_To
< Expr_Value
(Index_Base_L
)
948 elsif Compile_Time_Known_Value
(Index_Base_H
)
949 and then U_To
> Expr_Value
(Index_Base_H
)
954 Expr_Pos
:= Make_Integer_Literal
(Loc
, U_To
);
955 Set_Is_Static_Expression
(Expr_Pos
);
957 if not Is_Enumeration_Type
(Index_Base
) then
960 -- If we are dealing with enumeration return
961 -- Index_Base'Val (Expr_Pos)
965 Make_Attribute_Reference
967 Prefix
=> Index_Base_Name
,
968 Attribute_Name
=> Name_Val
,
969 Expressions
=> New_List
(Expr_Pos
));
975 -- If we are here no constant folding possible
977 if not Is_Enumeration_Type
(Index_Base
) then
980 Left_Opnd
=> Duplicate_Subexpr
(To
),
981 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
983 -- If we are dealing with enumeration return
984 -- Index_Base'Val (Index_Base'Pos (To) + Val)
988 Make_Attribute_Reference
990 Prefix
=> Index_Base_Name
,
991 Attribute_Name
=> Name_Pos
,
992 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
997 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
1000 Make_Attribute_Reference
1002 Prefix
=> Index_Base_Name
,
1003 Attribute_Name
=> Name_Val
,
1004 Expressions
=> New_List
(Expr_Pos
));
1014 function Empty_Range
(L
, H
: Node_Id
) return Boolean is
1015 Is_Empty
: Boolean := False;
1020 -- First check if L or H were already detected as overflowing the
1021 -- index base range type by function Add above. If this is so Add
1022 -- returns the empty node.
1024 if No
(L
) or else No
(H
) then
1028 for J
in 1 .. 3 loop
1031 -- L > H range is empty
1037 -- B_L > H range must be empty
1040 Low
:= Index_Base_L
;
1043 -- L > B_H range must be empty
1047 High
:= Index_Base_H
;
1050 if Local_Compile_Time_Known_Value
(Low
)
1052 Local_Compile_Time_Known_Value
(High
)
1055 UI_Gt
(Local_Expr_Value
(Low
), Local_Expr_Value
(High
));
1068 function Equal
(L
, H
: Node_Id
) return Boolean is
1073 elsif Local_Compile_Time_Known_Value
(L
)
1075 Local_Compile_Time_Known_Value
(H
)
1077 return UI_Eq
(Local_Expr_Value
(L
), Local_Expr_Value
(H
));
1090 In_Loop
: Boolean := False) return List_Id
1092 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
;
1093 -- Collect insert_actions generated in the construction of a loop,
1094 -- and prepend them to the sequence of assignments to complete the
1095 -- eventual body of the loop.
1097 procedure Initialize_Array_Component
1098 (Arr_Comp
: Node_Id
;
1100 Init_Expr
: Node_Id
;
1102 -- Perform the initialization of array component Arr_Comp with
1103 -- expected type Comp_Typ. Init_Expr denotes the initialization
1104 -- expression of the array component. All generated code is added
1107 procedure Initialize_Ctrl_Array_Component
1108 (Arr_Comp
: Node_Id
;
1109 Comp_Typ
: Entity_Id
;
1110 Init_Expr
: Node_Id
;
1112 -- Perform the initialization of array component Arr_Comp when its
1113 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1114 -- the initialization expression of the array component. All hook-
1115 -- related declarations are inserted prior to aggregate N. Remaining
1116 -- code is added to list Stmts.
1118 ----------------------
1119 -- Add_Loop_Actions --
1120 ----------------------
1122 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
is
1126 -- Ada 2005 (AI-287): Do nothing else in case of default
1127 -- initialized component.
1132 elsif Nkind
(Parent
(Expr
)) = N_Component_Association
1133 and then Present
(Loop_Actions
(Parent
(Expr
)))
1135 Append_List
(Lis
, Loop_Actions
(Parent
(Expr
)));
1136 Res
:= Loop_Actions
(Parent
(Expr
));
1137 Set_Loop_Actions
(Parent
(Expr
), No_List
);
1143 end Add_Loop_Actions
;
1145 --------------------------------
1146 -- Initialize_Array_Component --
1147 --------------------------------
1149 procedure Initialize_Array_Component
1150 (Arr_Comp
: Node_Id
;
1152 Init_Expr
: Node_Id
;
1155 Exceptions_OK
: constant Boolean :=
1156 not Restriction_Active
1157 (No_Exception_Propagation
);
1159 Finalization_OK
: constant Boolean :=
1161 and then Needs_Finalization
(Comp_Typ
);
1163 Full_Typ
: constant Entity_Id
:= Underlying_Type
(Comp_Typ
);
1165 Blk_Stmts
: List_Id
;
1166 Init_Stmt
: Node_Id
;
1169 -- Protect the initialization statements from aborts. Generate:
1173 if Finalization_OK
and Abort_Allowed
then
1174 if Exceptions_OK
then
1175 Blk_Stmts
:= New_List
;
1180 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
1182 -- Otherwise aborts are not allowed. All generated code is added
1183 -- directly to the input list.
1189 -- Initialize the array element. Generate:
1191 -- Arr_Comp := Init_Expr;
1193 -- Note that the initialization expression is replicated because
1194 -- it has to be reevaluated within a generated loop.
1197 Make_OK_Assignment_Statement
(Loc
,
1198 Name
=> New_Copy_Tree
(Arr_Comp
),
1199 Expression
=> New_Copy_Tree
(Init_Expr
));
1200 Set_No_Ctrl_Actions
(Init_Stmt
);
1202 -- If this is an aggregate for an array of arrays, each
1203 -- subaggregate will be expanded as well, and even with
1204 -- No_Ctrl_Actions the assignments of inner components will
1205 -- require attachment in their assignments to temporaries. These
1206 -- temporaries must be finalized for each subaggregate. Generate:
1209 -- Arr_Comp := Init_Expr;
1212 if Finalization_OK
and then Is_Array_Type
(Comp_Typ
) then
1214 Make_Block_Statement
(Loc
,
1215 Handled_Statement_Sequence
=>
1216 Make_Handled_Sequence_Of_Statements
(Loc
,
1217 Statements
=> New_List
(Init_Stmt
)));
1220 Append_To
(Blk_Stmts
, Init_Stmt
);
1222 -- Adjust the tag due to a possible view conversion. Generate:
1224 -- Arr_Comp._tag := Full_TypP;
1226 if Tagged_Type_Expansion
1227 and then Present
(Comp_Typ
)
1228 and then Is_Tagged_Type
(Comp_Typ
)
1230 Append_To
(Blk_Stmts
,
1231 Make_OK_Assignment_Statement
(Loc
,
1233 Make_Selected_Component
(Loc
,
1234 Prefix
=> New_Copy_Tree
(Arr_Comp
),
1237 (First_Tag_Component
(Full_Typ
), Loc
)),
1240 Unchecked_Convert_To
(RTE
(RE_Tag
),
1242 (Node
(First_Elmt
(Access_Disp_Table
(Full_Typ
))),
1246 -- Adjust the array component. Controlled subaggregates are not
1247 -- considered because each of their individual elements will
1248 -- receive an adjustment of its own. Generate:
1250 -- [Deep_]Adjust (Arr_Comp);
1253 and then not Is_Limited_Type
(Comp_Typ
)
1254 and then not Is_Build_In_Place_Function_Call
(Init_Expr
)
1256 (Is_Array_Type
(Comp_Typ
)
1257 and then Is_Controlled
(Component_Type
(Comp_Typ
))
1258 and then Nkind
(Expr
) = N_Aggregate
)
1262 (Obj_Ref
=> New_Copy_Tree
(Arr_Comp
),
1265 -- Guard against a missing [Deep_]Adjust when the component
1266 -- type was not frozen properly.
1268 if Present
(Adj_Call
) then
1269 Append_To
(Blk_Stmts
, Adj_Call
);
1273 -- Complete the protection of the initialization statements
1275 if Finalization_OK
and Abort_Allowed
then
1277 -- Wrap the initialization statements in a block to catch a
1278 -- potential exception. Generate:
1282 -- Arr_Comp := Init_Expr;
1283 -- Arr_Comp._tag := Full_TypP;
1284 -- [Deep_]Adjust (Arr_Comp);
1286 -- Abort_Undefer_Direct;
1289 if Exceptions_OK
then
1291 Build_Abort_Undefer_Block
(Loc
,
1295 -- Otherwise exceptions are not propagated. Generate:
1298 -- Arr_Comp := Init_Expr;
1299 -- Arr_Comp._tag := Full_TypP;
1300 -- [Deep_]Adjust (Arr_Comp);
1304 Append_To
(Blk_Stmts
,
1305 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
1308 end Initialize_Array_Component
;
1310 -------------------------------------
1311 -- Initialize_Ctrl_Array_Component --
1312 -------------------------------------
1314 procedure Initialize_Ctrl_Array_Component
1315 (Arr_Comp
: Node_Id
;
1316 Comp_Typ
: Entity_Id
;
1317 Init_Expr
: Node_Id
;
1321 Act_Stmts
: List_Id
;
1324 Hook_Clear
: Node_Id
;
1326 In_Place_Expansion
: Boolean;
1327 -- Flag set when a nonlimited controlled function call requires
1328 -- in-place expansion.
1331 -- Duplicate the initialization expression in case the context is
1332 -- a multi choice list or an "others" choice which plugs various
1333 -- holes in the aggregate. As a result the expression is no longer
1334 -- shared between the various components and is reevaluated for
1335 -- each such component.
1337 Expr
:= New_Copy_Tree
(Init_Expr
);
1338 Set_Parent
(Expr
, Parent
(Init_Expr
));
1340 -- Perform a preliminary analysis and resolution to determine what
1341 -- the initialization expression denotes. An unanalyzed function
1342 -- call may appear as an identifier or an indexed component.
1344 if Nkind_In
(Expr
, N_Function_Call
,
1346 N_Indexed_Component
)
1347 and then not Analyzed
(Expr
)
1349 Preanalyze_And_Resolve
(Expr
, Comp_Typ
);
1352 In_Place_Expansion
:=
1353 Nkind
(Expr
) = N_Function_Call
1354 and then not Is_Build_In_Place_Result_Type
(Comp_Typ
);
1356 -- The initialization expression is a controlled function call.
1357 -- Perform in-place removal of side effects to avoid creating a
1358 -- transient scope, which leads to premature finalization.
1360 -- This in-place expansion is not performed for limited transient
1361 -- objects because the initialization is already done in-place.
1363 if In_Place_Expansion
then
1365 -- Suppress the removal of side effects by general analysis
1366 -- because this behavior is emulated here. This avoids the
1367 -- generation of a transient scope, which leads to out-of-order
1368 -- adjustment and finalization.
1370 Set_No_Side_Effect_Removal
(Expr
);
1372 -- When the transient component initialization is related to a
1373 -- range or an "others", keep all generated statements within
1374 -- the enclosing loop. This way the controlled function call
1375 -- will be evaluated at each iteration, and its result will be
1376 -- finalized at the end of each iteration.
1382 -- Otherwise this is a single component initialization. Hook-
1383 -- related statements are inserted prior to the aggregate.
1387 Act_Stmts
:= No_List
;
1390 -- Install all hook-related declarations and prepare the clean
1393 Process_Transient_Component
1395 Comp_Typ
=> Comp_Typ
,
1397 Fin_Call
=> Fin_Call
,
1398 Hook_Clear
=> Hook_Clear
,
1400 Stmts
=> Act_Stmts
);
1403 -- Use the noncontrolled component initialization circuitry to
1404 -- assign the result of the function call to the array element.
1405 -- This also performs subaggregate wrapping, tag adjustment, and
1406 -- [deep] adjustment of the array element.
1408 Initialize_Array_Component
1409 (Arr_Comp
=> Arr_Comp
,
1410 Comp_Typ
=> Comp_Typ
,
1414 -- At this point the array element is fully initialized. Complete
1415 -- the processing of the controlled array component by finalizing
1416 -- the transient function result.
1418 if In_Place_Expansion
then
1419 Process_Transient_Component_Completion
1422 Fin_Call
=> Fin_Call
,
1423 Hook_Clear
=> Hook_Clear
,
1426 end Initialize_Ctrl_Array_Component
;
1430 Stmts
: constant List_Id
:= New_List
;
1432 Comp_Typ
: Entity_Id
:= Empty
;
1434 Indexed_Comp
: Node_Id
;
1435 Init_Call
: Node_Id
;
1436 New_Indexes
: List_Id
;
1438 -- Start of processing for Gen_Assign
1441 if No
(Indexes
) then
1442 New_Indexes
:= New_List
;
1444 New_Indexes
:= New_Copy_List_Tree
(Indexes
);
1447 Append_To
(New_Indexes
, Ind
);
1449 if Present
(Next_Index
(Index
)) then
1452 Build_Array_Aggr_Code
1455 Index
=> Next_Index
(Index
),
1457 Scalar_Comp
=> Scalar_Comp
,
1458 Indexes
=> New_Indexes
));
1461 -- If we get here then we are at a bottom-level (sub-)aggregate
1465 (Make_Indexed_Component
(Loc
,
1466 Prefix
=> New_Copy_Tree
(Into
),
1467 Expressions
=> New_Indexes
));
1469 Set_Assignment_OK
(Indexed_Comp
);
1471 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1472 -- is not present (and therefore we also initialize Expr_Q to empty).
1476 elsif Nkind
(Expr
) = N_Qualified_Expression
then
1477 Expr_Q
:= Expression
(Expr
);
1482 if Present
(Etype
(N
)) and then Etype
(N
) /= Any_Composite
then
1483 Comp_Typ
:= Component_Type
(Etype
(N
));
1484 pragma Assert
(Comp_Typ
= Ctype
); -- AI-287
1486 elsif Present
(Next
(First
(New_Indexes
))) then
1488 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1489 -- component because we have received the component type in
1490 -- the formal parameter Ctype.
1492 -- ??? Some assert pragmas have been added to check if this new
1493 -- formal can be used to replace this code in all cases.
1495 if Present
(Expr
) then
1497 -- This is a multidimensional array. Recover the component type
1498 -- from the outermost aggregate, because subaggregates do not
1499 -- have an assigned type.
1506 while Present
(P
) loop
1507 if Nkind
(P
) = N_Aggregate
1508 and then Present
(Etype
(P
))
1510 Comp_Typ
:= Component_Type
(Etype
(P
));
1518 pragma Assert
(Comp_Typ
= Ctype
); -- AI-287
1523 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1524 -- default initialized components (otherwise Expr_Q is not present).
1527 and then Nkind_In
(Expr_Q
, N_Aggregate
, N_Extension_Aggregate
)
1529 -- At this stage the Expression may not have been analyzed yet
1530 -- because the array aggregate code has not been updated to use
1531 -- the Expansion_Delayed flag and avoid analysis altogether to
1532 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1533 -- the analysis of non-array aggregates now in order to get the
1534 -- value of Expansion_Delayed flag for the inner aggregate ???
1536 -- In the case of an iterated component association, the analysis
1537 -- of the generated loop will analyze the expression in the
1538 -- proper context, in which the loop parameter is visible.
1540 if Present
(Comp_Typ
) and then not Is_Array_Type
(Comp_Typ
) then
1541 if Nkind
(Parent
(Expr_Q
)) = N_Iterated_Component_Association
1542 or else Nkind
(Parent
(Parent
((Expr_Q
)))) =
1543 N_Iterated_Component_Association
1547 Analyze_And_Resolve
(Expr_Q
, Comp_Typ
);
1551 if Is_Delayed_Aggregate
(Expr_Q
) then
1553 -- This is either a subaggregate of a multidimensional array,
1554 -- or a component of an array type whose component type is
1555 -- also an array. In the latter case, the expression may have
1556 -- component associations that provide different bounds from
1557 -- those of the component type, and sliding must occur. Instead
1558 -- of decomposing the current aggregate assignment, force the
1559 -- reanalysis of the assignment, so that a temporary will be
1560 -- generated in the usual fashion, and sliding will take place.
1562 if Nkind
(Parent
(N
)) = N_Assignment_Statement
1563 and then Is_Array_Type
(Comp_Typ
)
1564 and then Present
(Component_Associations
(Expr_Q
))
1565 and then Must_Slide
(Comp_Typ
, Etype
(Expr_Q
))
1567 Set_Expansion_Delayed
(Expr_Q
, False);
1568 Set_Analyzed
(Expr_Q
, False);
1573 Late_Expansion
(Expr_Q
, Etype
(Expr_Q
), Indexed_Comp
));
1578 if Present
(Expr
) then
1580 -- Handle an initialization expression of a controlled type in
1581 -- case it denotes a function call. In general such a scenario
1582 -- will produce a transient scope, but this will lead to wrong
1583 -- order of initialization, adjustment, and finalization in the
1584 -- context of aggregates.
1586 -- Target (1) := Ctrl_Func_Call;
1589 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1590 -- Target (1) := Trans_Obj;
1591 -- Finalize (Trans_Obj);
1593 -- Target (1)._tag := ...;
1594 -- Adjust (Target (1));
1596 -- In the example above, the call to Finalize occurs too early
1597 -- and as a result it may leave the array component in a bad
1598 -- state. Finalization of the transient object should really
1599 -- happen after adjustment.
1601 -- To avoid this scenario, perform in-place side-effect removal
1602 -- of the function call. This eliminates the transient property
1603 -- of the function result and ensures correct order of actions.
1605 -- Res : ... := Ctrl_Func_Call;
1606 -- Target (1) := Res;
1607 -- Target (1)._tag := ...;
1608 -- Adjust (Target (1));
1611 if Present
(Comp_Typ
)
1612 and then Needs_Finalization
(Comp_Typ
)
1613 and then Nkind
(Expr
) /= N_Aggregate
1615 Initialize_Ctrl_Array_Component
1616 (Arr_Comp
=> Indexed_Comp
,
1617 Comp_Typ
=> Comp_Typ
,
1621 -- Otherwise perform simple component initialization
1624 Initialize_Array_Component
1625 (Arr_Comp
=> Indexed_Comp
,
1626 Comp_Typ
=> Comp_Typ
,
1631 -- Ada 2005 (AI-287): In case of default initialized component, call
1632 -- the initialization subprogram associated with the component type.
1633 -- If the component type is an access type, add an explicit null
1634 -- assignment, because for the back-end there is an initialization
1635 -- present for the whole aggregate, and no default initialization
1638 -- In addition, if the component type is controlled, we must call
1639 -- its Initialize procedure explicitly, because there is no explicit
1640 -- object creation that will invoke it otherwise.
1643 if Present
(Base_Init_Proc
(Base_Type
(Ctype
)))
1644 or else Has_Task
(Base_Type
(Ctype
))
1646 Append_List_To
(Stmts
,
1647 Build_Initialization_Call
(Loc
,
1648 Id_Ref
=> Indexed_Comp
,
1650 With_Default_Init
=> True));
1652 -- If the component type has invariants, add an invariant
1653 -- check after the component is default-initialized. It will
1654 -- be analyzed and resolved before the code for initialization
1655 -- of other components.
1657 if Has_Invariants
(Ctype
) then
1658 Set_Etype
(Indexed_Comp
, Ctype
);
1659 Append_To
(Stmts
, Make_Invariant_Call
(Indexed_Comp
));
1662 elsif Is_Access_Type
(Ctype
) then
1664 Make_Assignment_Statement
(Loc
,
1665 Name
=> New_Copy_Tree
(Indexed_Comp
),
1666 Expression
=> Make_Null
(Loc
)));
1669 if Needs_Finalization
(Ctype
) then
1672 (Obj_Ref
=> New_Copy_Tree
(Indexed_Comp
),
1675 -- Guard against a missing [Deep_]Initialize when the component
1676 -- type was not properly frozen.
1678 if Present
(Init_Call
) then
1679 Append_To
(Stmts
, Init_Call
);
1684 return Add_Loop_Actions
(Stmts
);
1691 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1692 Is_Iterated_Component
: constant Boolean :=
1693 Nkind
(Parent
(Expr
)) = N_Iterated_Component_Association
;
1704 -- Index_Base'(L) .. Index_Base'(H)
1706 L_Iteration_Scheme
: Node_Id
;
1707 -- L_J in Index_Base'(L) .. Index_Base'(H)
1710 -- The statements to execute in the loop
1712 S
: constant List_Id
:= New_List
;
1713 -- List of statements
1716 -- Copy of expression tree, used for checking purposes
1719 -- If loop bounds define an empty range return the null statement
1721 if Empty_Range
(L
, H
) then
1722 Append_To
(S
, Make_Null_Statement
(Loc
));
1724 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1725 -- default initialized component.
1731 -- The expression must be type-checked even though no component
1732 -- of the aggregate will have this value. This is done only for
1733 -- actual components of the array, not for subaggregates. Do
1734 -- the check on a copy, because the expression may be shared
1735 -- among several choices, some of which might be non-null.
1737 if Present
(Etype
(N
))
1738 and then Is_Array_Type
(Etype
(N
))
1739 and then No
(Next_Index
(Index
))
1741 Expander_Mode_Save_And_Set
(False);
1742 Tcopy
:= New_Copy_Tree
(Expr
);
1743 Set_Parent
(Tcopy
, N
);
1744 Analyze_And_Resolve
(Tcopy
, Component_Type
(Etype
(N
)));
1745 Expander_Mode_Restore
;
1751 -- If loop bounds are the same then generate an assignment, unless
1752 -- the parent construct is an Iterated_Component_Association.
1754 elsif Equal
(L
, H
) and then not Is_Iterated_Component
then
1755 return Gen_Assign
(New_Copy_Tree
(L
), Expr
);
1757 -- If H - L <= 2 then generate a sequence of assignments when we are
1758 -- processing the bottom most aggregate and it contains scalar
1761 elsif No
(Next_Index
(Index
))
1762 and then Scalar_Comp
1763 and then Local_Compile_Time_Known_Value
(L
)
1764 and then Local_Compile_Time_Known_Value
(H
)
1765 and then Local_Expr_Value
(H
) - Local_Expr_Value
(L
) <= 2
1766 and then not Is_Iterated_Component
1768 Append_List_To
(S
, Gen_Assign
(New_Copy_Tree
(L
), Expr
));
1769 Append_List_To
(S
, Gen_Assign
(Add
(1, To
=> L
), Expr
));
1771 if Local_Expr_Value
(H
) - Local_Expr_Value
(L
) = 2 then
1772 Append_List_To
(S
, Gen_Assign
(Add
(2, To
=> L
), Expr
));
1778 -- Otherwise construct the loop, starting with the loop index L_J
1780 if Is_Iterated_Component
then
1782 Make_Defining_Identifier
(Loc
,
1783 Chars
=> (Chars
(Defining_Identifier
(Parent
(Expr
)))));
1786 L_J
:= Make_Temporary
(Loc
, 'J', L
);
1789 -- Construct "L .. H" in Index_Base. We use a qualified expression
1790 -- for the bound to convert to the index base, but we don't need
1791 -- to do that if we already have the base type at hand.
1793 if Etype
(L
) = Index_Base
then
1797 Make_Qualified_Expression
(Loc
,
1798 Subtype_Mark
=> Index_Base_Name
,
1799 Expression
=> New_Copy_Tree
(L
));
1802 if Etype
(H
) = Index_Base
then
1806 Make_Qualified_Expression
(Loc
,
1807 Subtype_Mark
=> Index_Base_Name
,
1808 Expression
=> New_Copy_Tree
(H
));
1816 -- Construct "for L_J in Index_Base range L .. H"
1818 L_Iteration_Scheme
:=
1819 Make_Iteration_Scheme
1821 Loop_Parameter_Specification
=>
1822 Make_Loop_Parameter_Specification
1824 Defining_Identifier
=> L_J
,
1825 Discrete_Subtype_Definition
=> L_Range
));
1827 -- Construct the statements to execute in the loop body
1830 Gen_Assign
(New_Occurrence_Of
(L_J
, Loc
), Expr
, In_Loop
=> True);
1832 -- Construct the final loop
1835 Make_Implicit_Loop_Statement
1837 Identifier
=> Empty
,
1838 Iteration_Scheme
=> L_Iteration_Scheme
,
1839 Statements
=> L_Body
));
1841 -- A small optimization: if the aggregate is initialized with a box
1842 -- and the component type has no initialization procedure, remove the
1843 -- useless empty loop.
1845 if Nkind
(First
(S
)) = N_Loop_Statement
1846 and then Is_Empty_List
(Statements
(First
(S
)))
1848 return New_List
(Make_Null_Statement
(Loc
));
1858 -- The code built is
1860 -- W_J : Index_Base := L;
1861 -- while W_J < H loop
1862 -- W_J := Index_Base'Succ (W);
1866 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1870 -- W_J : Base_Type := L;
1872 W_Iteration_Scheme
: Node_Id
;
1875 W_Index_Succ
: Node_Id
;
1876 -- Index_Base'Succ (J)
1878 W_Increment
: Node_Id
;
1879 -- W_J := Index_Base'Succ (W)
1881 W_Body
: constant List_Id
:= New_List
;
1882 -- The statements to execute in the loop
1884 S
: constant List_Id
:= New_List
;
1885 -- list of statement
1888 -- If loop bounds define an empty range or are equal return null
1890 if Empty_Range
(L
, H
) or else Equal
(L
, H
) then
1891 Append_To
(S
, Make_Null_Statement
(Loc
));
1895 -- Build the decl of W_J
1897 W_J
:= Make_Temporary
(Loc
, 'J', L
);
1899 Make_Object_Declaration
1901 Defining_Identifier
=> W_J
,
1902 Object_Definition
=> Index_Base_Name
,
1905 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1906 -- that in this particular case L is a fresh Expr generated by
1907 -- Add which we are the only ones to use.
1909 Append_To
(S
, W_Decl
);
1911 -- Construct " while W_J < H"
1913 W_Iteration_Scheme
:=
1914 Make_Iteration_Scheme
1916 Condition
=> Make_Op_Lt
1918 Left_Opnd
=> New_Occurrence_Of
(W_J
, Loc
),
1919 Right_Opnd
=> New_Copy_Tree
(H
)));
1921 -- Construct the statements to execute in the loop body
1924 Make_Attribute_Reference
1926 Prefix
=> Index_Base_Name
,
1927 Attribute_Name
=> Name_Succ
,
1928 Expressions
=> New_List
(New_Occurrence_Of
(W_J
, Loc
)));
1931 Make_OK_Assignment_Statement
1933 Name
=> New_Occurrence_Of
(W_J
, Loc
),
1934 Expression
=> W_Index_Succ
);
1936 Append_To
(W_Body
, W_Increment
);
1938 Append_List_To
(W_Body
,
1939 Gen_Assign
(New_Occurrence_Of
(W_J
, Loc
), Expr
, In_Loop
=> True));
1941 -- Construct the final loop
1944 Make_Implicit_Loop_Statement
1946 Identifier
=> Empty
,
1947 Iteration_Scheme
=> W_Iteration_Scheme
,
1948 Statements
=> W_Body
));
1953 --------------------
1954 -- Get_Assoc_Expr --
1955 --------------------
1957 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
is
1958 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
1961 if Box_Present
(Assoc
) then
1962 if Is_Scalar_Type
(Ctype
) then
1963 if Present
(Default_Aspect_Component_Value
(Typ
)) then
1964 return Default_Aspect_Component_Value
(Typ
);
1965 elsif Present
(Default_Aspect_Value
(Ctype
)) then
1966 return Default_Aspect_Value
(Ctype
);
1976 return Expression
(Assoc
);
1980 ---------------------
1981 -- Index_Base_Name --
1982 ---------------------
1984 function Index_Base_Name
return Node_Id
is
1986 return New_Occurrence_Of
(Index_Base
, Sloc
(N
));
1987 end Index_Base_Name
;
1989 ------------------------------------
1990 -- Local_Compile_Time_Known_Value --
1991 ------------------------------------
1993 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean is
1995 return Compile_Time_Known_Value
(E
)
1997 (Nkind
(E
) = N_Attribute_Reference
1998 and then Attribute_Name
(E
) = Name_Val
1999 and then Compile_Time_Known_Value
(First
(Expressions
(E
))));
2000 end Local_Compile_Time_Known_Value
;
2002 ----------------------
2003 -- Local_Expr_Value --
2004 ----------------------
2006 function Local_Expr_Value
(E
: Node_Id
) return Uint
is
2008 if Compile_Time_Known_Value
(E
) then
2009 return Expr_Value
(E
);
2011 return Expr_Value
(First
(Expressions
(E
)));
2013 end Local_Expr_Value
;
2017 New_Code
: constant List_Id
:= New_List
;
2019 Aggr_L
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(N
));
2020 Aggr_H
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(N
));
2021 -- The aggregate bounds of this specific subaggregate. Note that if the
2022 -- code generated by Build_Array_Aggr_Code is executed then these bounds
2023 -- are OK. Otherwise a Constraint_Error would have been raised.
2025 Aggr_Low
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_L
);
2026 Aggr_High
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_H
);
2027 -- After Duplicate_Subexpr these are side-effect free
2036 Nb_Choices
: Nat
:= 0;
2037 Table
: Case_Table_Type
(1 .. Number_Of_Choices
(N
));
2038 -- Used to sort all the different choice values
2041 -- Number of elements in the positional aggregate
2043 Others_Assoc
: Node_Id
:= Empty
;
2045 -- Start of processing for Build_Array_Aggr_Code
2048 -- First before we start, a special case. if we have a bit packed
2049 -- array represented as a modular type, then clear the value to
2050 -- zero first, to ensure that unused bits are properly cleared.
2055 and then Is_Bit_Packed_Array
(Typ
)
2056 and then Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
))
2058 Append_To
(New_Code
,
2059 Make_Assignment_Statement
(Loc
,
2060 Name
=> New_Copy_Tree
(Into
),
2062 Unchecked_Convert_To
(Typ
,
2063 Make_Integer_Literal
(Loc
, Uint_0
))));
2066 -- If the component type contains tasks, we need to build a Master
2067 -- entity in the current scope, because it will be needed if build-
2068 -- in-place functions are called in the expanded code.
2070 if Nkind
(Parent
(N
)) = N_Object_Declaration
and then Has_Task
(Typ
) then
2071 Build_Master_Entity
(Defining_Identifier
(Parent
(N
)));
2074 -- STEP 1: Process component associations
2076 -- For those associations that may generate a loop, initialize
2077 -- Loop_Actions to collect inserted actions that may be crated.
2079 -- Skip this if no component associations
2081 if No
(Expressions
(N
)) then
2083 -- STEP 1 (a): Sort the discrete choices
2085 Assoc
:= First
(Component_Associations
(N
));
2086 while Present
(Assoc
) loop
2087 Choice
:= First
(Choice_List
(Assoc
));
2088 while Present
(Choice
) loop
2089 if Nkind
(Choice
) = N_Others_Choice
then
2090 Set_Loop_Actions
(Assoc
, New_List
);
2091 Others_Assoc
:= Assoc
;
2095 Get_Index_Bounds
(Choice
, Low
, High
);
2098 Set_Loop_Actions
(Assoc
, New_List
);
2101 Nb_Choices
:= Nb_Choices
+ 1;
2103 Table
(Nb_Choices
) :=
2106 Choice_Node
=> Get_Assoc_Expr
(Assoc
));
2114 -- If there is more than one set of choices these must be static
2115 -- and we can therefore sort them. Remember that Nb_Choices does not
2116 -- account for an others choice.
2118 if Nb_Choices
> 1 then
2119 Sort_Case_Table
(Table
);
2122 -- STEP 1 (b): take care of the whole set of discrete choices
2124 for J
in 1 .. Nb_Choices
loop
2125 Low
:= Table
(J
).Choice_Lo
;
2126 High
:= Table
(J
).Choice_Hi
;
2127 Expr
:= Table
(J
).Choice_Node
;
2128 Append_List
(Gen_Loop
(Low
, High
, Expr
), To
=> New_Code
);
2131 -- STEP 1 (c): generate the remaining loops to cover others choice
2132 -- We don't need to generate loops over empty gaps, but if there is
2133 -- a single empty range we must analyze the expression for semantics
2135 if Present
(Others_Assoc
) then
2137 First
: Boolean := True;
2140 for J
in 0 .. Nb_Choices
loop
2144 Low
:= Add
(1, To
=> Table
(J
).Choice_Hi
);
2147 if J
= Nb_Choices
then
2150 High
:= Add
(-1, To
=> Table
(J
+ 1).Choice_Lo
);
2153 -- If this is an expansion within an init proc, make
2154 -- sure that discriminant references are replaced by
2155 -- the corresponding discriminal.
2157 if Inside_Init_Proc
then
2158 if Is_Entity_Name
(Low
)
2159 and then Ekind
(Entity
(Low
)) = E_Discriminant
2161 Set_Entity
(Low
, Discriminal
(Entity
(Low
)));
2164 if Is_Entity_Name
(High
)
2165 and then Ekind
(Entity
(High
)) = E_Discriminant
2167 Set_Entity
(High
, Discriminal
(Entity
(High
)));
2172 or else not Empty_Range
(Low
, High
)
2176 (Gen_Loop
(Low
, High
,
2177 Get_Assoc_Expr
(Others_Assoc
)), To
=> New_Code
);
2183 -- STEP 2: Process positional components
2186 -- STEP 2 (a): Generate the assignments for each positional element
2187 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2188 -- Aggr_L is analyzed and Add wants an analyzed expression.
2190 Expr
:= First
(Expressions
(N
));
2192 while Present
(Expr
) loop
2193 Nb_Elements
:= Nb_Elements
+ 1;
2194 Append_List
(Gen_Assign
(Add
(Nb_Elements
, To
=> Aggr_L
), Expr
),
2199 -- STEP 2 (b): Generate final loop if an others choice is present
2200 -- Here Nb_Elements gives the offset of the last positional element.
2202 if Present
(Component_Associations
(N
)) then
2203 Assoc
:= Last
(Component_Associations
(N
));
2205 -- Ada 2005 (AI-287)
2207 Append_List
(Gen_While
(Add
(Nb_Elements
, To
=> Aggr_L
),
2209 Get_Assoc_Expr
(Assoc
)), -- AI-287
2215 end Build_Array_Aggr_Code
;
2217 ----------------------------
2218 -- Build_Record_Aggr_Code --
2219 ----------------------------
2221 function Build_Record_Aggr_Code
2224 Lhs
: Node_Id
) return List_Id
2226 Loc
: constant Source_Ptr
:= Sloc
(N
);
2227 L
: constant List_Id
:= New_List
;
2228 N_Typ
: constant Entity_Id
:= Etype
(N
);
2234 Comp_Type
: Entity_Id
;
2235 Selector
: Entity_Id
;
2236 Comp_Expr
: Node_Id
;
2239 -- If this is an internal aggregate, the External_Final_List is an
2240 -- expression for the controller record of the enclosing type.
2242 -- If the current aggregate has several controlled components, this
2243 -- expression will appear in several calls to attach to the finali-
2244 -- zation list, and it must not be shared.
2246 Ancestor_Is_Expression
: Boolean := False;
2247 Ancestor_Is_Subtype_Mark
: Boolean := False;
2249 Init_Typ
: Entity_Id
:= Empty
;
2251 Finalization_Done
: Boolean := False;
2252 -- True if Generate_Finalization_Actions has already been called; calls
2253 -- after the first do nothing.
2255 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
;
2256 -- Returns the value that the given discriminant of an ancestor type
2257 -- should receive (in the absence of a conflict with the value provided
2258 -- by an ancestor part of an extension aggregate).
2260 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
);
2261 -- Check that each of the discriminant values defined by the ancestor
2262 -- part of an extension aggregate match the corresponding values
2263 -- provided by either an association of the aggregate or by the
2264 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2266 function Compatible_Int_Bounds
2267 (Agg_Bounds
: Node_Id
;
2268 Typ_Bounds
: Node_Id
) return Boolean;
2269 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2270 -- assumed that both bounds are integer ranges.
2272 procedure Generate_Finalization_Actions
;
2273 -- Deal with the various controlled type data structure initializations
2274 -- (but only if it hasn't been done already).
2276 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
;
2277 -- Returns the first discriminant association in the constraint
2278 -- associated with T, if any, otherwise returns Empty.
2280 function Get_Explicit_Discriminant_Value
(D
: Entity_Id
) return Node_Id
;
2281 -- If the ancestor part is an unconstrained type and further ancestors
2282 -- do not provide discriminants for it, check aggregate components for
2283 -- values of the discriminants.
2285 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
);
2286 -- If Typ is derived, and constrains discriminants of the parent type,
2287 -- these discriminants are not components of the aggregate, and must be
2288 -- initialized. The assignments are appended to List. The same is done
2289 -- if Typ derives fron an already constrained subtype of a discriminated
2292 procedure Init_Stored_Discriminants
;
2293 -- If the type is derived and has inherited discriminants, generate
2294 -- explicit assignments for each, using the store constraint of the
2295 -- type. Note that both visible and stored discriminants must be
2296 -- initialized in case the derived type has some renamed and some
2297 -- constrained discriminants.
2299 procedure Init_Visible_Discriminants
;
2300 -- If type has discriminants, retrieve their values from aggregate,
2301 -- and generate explicit assignments for each. This does not include
2302 -- discriminants inherited from ancestor, which are handled above.
2303 -- The type of the aggregate is a subtype created ealier using the
2304 -- given values of the discriminant components of the aggregate.
2306 procedure Initialize_Ctrl_Record_Component
2307 (Rec_Comp
: Node_Id
;
2308 Comp_Typ
: Entity_Id
;
2309 Init_Expr
: Node_Id
;
2311 -- Perform the initialization of controlled record component Rec_Comp.
2312 -- Comp_Typ is the component type. Init_Expr is the initialization
2313 -- expression for the record component. Hook-related declarations are
2314 -- inserted prior to aggregate N using Insert_Action. All remaining
2315 -- generated code is added to list Stmts.
2317 procedure Initialize_Record_Component
2318 (Rec_Comp
: Node_Id
;
2319 Comp_Typ
: Entity_Id
;
2320 Init_Expr
: Node_Id
;
2322 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2323 -- is the component type. Init_Expr is the initialization expression
2324 -- of the record component. All generated code is added to list Stmts.
2326 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean;
2327 -- Check whether Bounds is a range node and its lower and higher bounds
2328 -- are integers literals.
2330 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
;
2331 -- If the aggregate contains a self-reference, traverse each expression
2332 -- to replace a possible self-reference with a reference to the proper
2333 -- component of the target of the assignment.
2335 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
;
2336 -- If default expression of a component mentions a discriminant of the
2337 -- type, it must be rewritten as the discriminant of the target object.
2339 ---------------------------------
2340 -- Ancestor_Discriminant_Value --
2341 ---------------------------------
2343 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
is
2345 Assoc_Elmt
: Elmt_Id
;
2346 Aggr_Comp
: Entity_Id
;
2347 Corresp_Disc
: Entity_Id
;
2348 Current_Typ
: Entity_Id
:= Base_Type
(Typ
);
2349 Parent_Typ
: Entity_Id
;
2350 Parent_Disc
: Entity_Id
;
2351 Save_Assoc
: Node_Id
:= Empty
;
2354 -- First check any discriminant associations to see if any of them
2355 -- provide a value for the discriminant.
2357 if Present
(Discriminant_Specifications
(Parent
(Current_Typ
))) then
2358 Assoc
:= First
(Component_Associations
(N
));
2359 while Present
(Assoc
) loop
2360 Aggr_Comp
:= Entity
(First
(Choices
(Assoc
)));
2362 if Ekind
(Aggr_Comp
) = E_Discriminant
then
2363 Save_Assoc
:= Expression
(Assoc
);
2365 Corresp_Disc
:= Corresponding_Discriminant
(Aggr_Comp
);
2366 while Present
(Corresp_Disc
) loop
2368 -- If found a corresponding discriminant then return the
2369 -- value given in the aggregate. (Note: this is not
2370 -- correct in the presence of side effects. ???)
2372 if Disc
= Corresp_Disc
then
2373 return Duplicate_Subexpr
(Expression
(Assoc
));
2376 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
2384 -- No match found in aggregate, so chain up parent types to find
2385 -- a constraint that defines the value of the discriminant.
2387 Parent_Typ
:= Etype
(Current_Typ
);
2388 while Current_Typ
/= Parent_Typ
loop
2389 if Has_Discriminants
(Parent_Typ
)
2390 and then not Has_Unknown_Discriminants
(Parent_Typ
)
2392 Parent_Disc
:= First_Discriminant
(Parent_Typ
);
2394 -- We either get the association from the subtype indication
2395 -- of the type definition itself, or from the discriminant
2396 -- constraint associated with the type entity (which is
2397 -- preferable, but it's not always present ???)
2399 if Is_Empty_Elmt_List
(Discriminant_Constraint
(Current_Typ
))
2401 Assoc
:= Get_Constraint_Association
(Current_Typ
);
2402 Assoc_Elmt
:= No_Elmt
;
2405 First_Elmt
(Discriminant_Constraint
(Current_Typ
));
2406 Assoc
:= Node
(Assoc_Elmt
);
2409 -- Traverse the discriminants of the parent type looking
2410 -- for one that corresponds.
2412 while Present
(Parent_Disc
) and then Present
(Assoc
) loop
2413 Corresp_Disc
:= Parent_Disc
;
2414 while Present
(Corresp_Disc
)
2415 and then Disc
/= Corresp_Disc
2417 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
2420 if Disc
= Corresp_Disc
then
2421 if Nkind
(Assoc
) = N_Discriminant_Association
then
2422 Assoc
:= Expression
(Assoc
);
2425 -- If the located association directly denotes
2426 -- a discriminant, then use the value of a saved
2427 -- association of the aggregate. This is an approach
2428 -- used to handle certain cases involving multiple
2429 -- discriminants mapped to a single discriminant of
2430 -- a descendant. It's not clear how to locate the
2431 -- appropriate discriminant value for such cases. ???
2433 if Is_Entity_Name
(Assoc
)
2434 and then Ekind
(Entity
(Assoc
)) = E_Discriminant
2436 Assoc
:= Save_Assoc
;
2439 return Duplicate_Subexpr
(Assoc
);
2442 Next_Discriminant
(Parent_Disc
);
2444 if No
(Assoc_Elmt
) then
2448 Next_Elmt
(Assoc_Elmt
);
2450 if Present
(Assoc_Elmt
) then
2451 Assoc
:= Node
(Assoc_Elmt
);
2459 Current_Typ
:= Parent_Typ
;
2460 Parent_Typ
:= Etype
(Current_Typ
);
2463 -- In some cases there's no ancestor value to locate (such as
2464 -- when an ancestor part given by an expression defines the
2465 -- discriminant value).
2468 end Ancestor_Discriminant_Value
;
2470 ----------------------------------
2471 -- Check_Ancestor_Discriminants --
2472 ----------------------------------
2474 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
) is
2476 Disc_Value
: Node_Id
;
2480 Discr
:= First_Discriminant
(Base_Type
(Anc_Typ
));
2481 while Present
(Discr
) loop
2482 Disc_Value
:= Ancestor_Discriminant_Value
(Discr
);
2484 if Present
(Disc_Value
) then
2485 Cond
:= Make_Op_Ne
(Loc
,
2487 Make_Selected_Component
(Loc
,
2488 Prefix
=> New_Copy_Tree
(Target
),
2489 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2490 Right_Opnd
=> Disc_Value
);
2493 Make_Raise_Constraint_Error
(Loc
,
2495 Reason
=> CE_Discriminant_Check_Failed
));
2498 Next_Discriminant
(Discr
);
2500 end Check_Ancestor_Discriminants
;
2502 ---------------------------
2503 -- Compatible_Int_Bounds --
2504 ---------------------------
2506 function Compatible_Int_Bounds
2507 (Agg_Bounds
: Node_Id
;
2508 Typ_Bounds
: Node_Id
) return Boolean
2510 Agg_Lo
: constant Uint
:= Intval
(Low_Bound
(Agg_Bounds
));
2511 Agg_Hi
: constant Uint
:= Intval
(High_Bound
(Agg_Bounds
));
2512 Typ_Lo
: constant Uint
:= Intval
(Low_Bound
(Typ_Bounds
));
2513 Typ_Hi
: constant Uint
:= Intval
(High_Bound
(Typ_Bounds
));
2515 return Typ_Lo
<= Agg_Lo
and then Agg_Hi
<= Typ_Hi
;
2516 end Compatible_Int_Bounds
;
2518 -----------------------------------
2519 -- Generate_Finalization_Actions --
2520 -----------------------------------
2522 procedure Generate_Finalization_Actions
is
2524 -- Do the work only the first time this is called
2526 if Finalization_Done
then
2530 Finalization_Done
:= True;
2532 -- Determine the external finalization list. It is either the
2533 -- finalization list of the outer scope or the one coming from an
2534 -- outer aggregate. When the target is not a temporary, the proper
2535 -- scope is the scope of the target rather than the potentially
2536 -- transient current scope.
2538 if Is_Controlled
(Typ
) and then Ancestor_Is_Subtype_Mark
then
2539 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2540 Set_Assignment_OK
(Ref
);
2543 Make_Procedure_Call_Statement
(Loc
,
2546 (Find_Prim_Op
(Init_Typ
, Name_Initialize
), Loc
),
2547 Parameter_Associations
=> New_List
(New_Copy_Tree
(Ref
))));
2549 end Generate_Finalization_Actions
;
2551 --------------------------------
2552 -- Get_Constraint_Association --
2553 --------------------------------
2555 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
is
2562 -- If type is private, get constraint from full view. This was
2563 -- previously done in an instance context, but is needed whenever
2564 -- the ancestor part has a discriminant, possibly inherited through
2565 -- multiple derivations.
2567 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
2568 Typ
:= Full_View
(Typ
);
2571 Indic
:= Subtype_Indication
(Type_Definition
(Parent
(Typ
)));
2573 -- Verify that the subtype indication carries a constraint
2575 if Nkind
(Indic
) = N_Subtype_Indication
2576 and then Present
(Constraint
(Indic
))
2578 return First
(Constraints
(Constraint
(Indic
)));
2582 end Get_Constraint_Association
;
2584 -------------------------------------
2585 -- Get_Explicit_Discriminant_Value --
2586 -------------------------------------
2588 function Get_Explicit_Discriminant_Value
2589 (D
: Entity_Id
) return Node_Id
2596 -- The aggregate has been normalized and all associations have a
2599 Assoc
:= First
(Component_Associations
(N
));
2600 while Present
(Assoc
) loop
2601 Choice
:= First
(Choices
(Assoc
));
2603 if Chars
(Choice
) = Chars
(D
) then
2604 Val
:= Expression
(Assoc
);
2613 end Get_Explicit_Discriminant_Value
;
2615 -------------------------------
2616 -- Init_Hidden_Discriminants --
2617 -------------------------------
2619 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
) is
2620 function Is_Completely_Hidden_Discriminant
2621 (Discr
: Entity_Id
) return Boolean;
2622 -- Determine whether Discr is a completely hidden discriminant of
2625 ---------------------------------------
2626 -- Is_Completely_Hidden_Discriminant --
2627 ---------------------------------------
2629 function Is_Completely_Hidden_Discriminant
2630 (Discr
: Entity_Id
) return Boolean
2635 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2636 -- completely hidden discriminants.
2638 Item
:= First_Entity
(Typ
);
2639 while Present
(Item
) loop
2640 if Ekind
(Item
) = E_Discriminant
2641 and then Is_Completely_Hidden
(Item
)
2642 and then Chars
(Original_Record_Component
(Item
)) =
2652 end Is_Completely_Hidden_Discriminant
;
2656 Base_Typ
: Entity_Id
;
2658 Discr_Constr
: Elmt_Id
;
2659 Discr_Init
: Node_Id
;
2660 Discr_Val
: Node_Id
;
2661 In_Aggr_Type
: Boolean;
2662 Par_Typ
: Entity_Id
;
2664 -- Start of processing for Init_Hidden_Discriminants
2667 -- The constraints on the hidden discriminants, if present, are kept
2668 -- in the Stored_Constraint list of the type itself, or in that of
2669 -- the base type. If not in the constraints of the aggregate itself,
2670 -- we examine ancestors to find discriminants that are not renamed
2671 -- by other discriminants but constrained explicitly.
2673 In_Aggr_Type
:= True;
2675 Base_Typ
:= Base_Type
(Typ
);
2676 while Is_Derived_Type
(Base_Typ
)
2678 (Present
(Stored_Constraint
(Base_Typ
))
2680 (In_Aggr_Type
and then Present
(Stored_Constraint
(Typ
))))
2682 Par_Typ
:= Etype
(Base_Typ
);
2684 if not Has_Discriminants
(Par_Typ
) then
2688 Discr
:= First_Discriminant
(Par_Typ
);
2690 -- We know that one of the stored-constraint lists is present
2692 if Present
(Stored_Constraint
(Base_Typ
)) then
2693 Discr_Constr
:= First_Elmt
(Stored_Constraint
(Base_Typ
));
2695 -- For private extension, stored constraint may be on full view
2697 elsif Is_Private_Type
(Base_Typ
)
2698 and then Present
(Full_View
(Base_Typ
))
2699 and then Present
(Stored_Constraint
(Full_View
(Base_Typ
)))
2702 First_Elmt
(Stored_Constraint
(Full_View
(Base_Typ
)));
2705 Discr_Constr
:= First_Elmt
(Stored_Constraint
(Typ
));
2708 while Present
(Discr
) and then Present
(Discr_Constr
) loop
2709 Discr_Val
:= Node
(Discr_Constr
);
2711 -- The parent discriminant is renamed in the derived type,
2712 -- nothing to initialize.
2714 -- type Deriv_Typ (Discr : ...)
2715 -- is new Parent_Typ (Discr => Discr);
2717 if Is_Entity_Name
(Discr_Val
)
2718 and then Ekind
(Entity
(Discr_Val
)) = E_Discriminant
2722 -- When the parent discriminant is constrained at the type
2723 -- extension level, it does not appear in the derived type.
2725 -- type Deriv_Typ (Discr : ...)
2726 -- is new Parent_Typ (Discr => Discr,
2727 -- Hidden_Discr => Expression);
2729 elsif Is_Completely_Hidden_Discriminant
(Discr
) then
2732 -- Otherwise initialize the discriminant
2736 Make_OK_Assignment_Statement
(Loc
,
2738 Make_Selected_Component
(Loc
,
2739 Prefix
=> New_Copy_Tree
(Target
),
2740 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2741 Expression
=> New_Copy_Tree
(Discr_Val
));
2743 Append_To
(List
, Discr_Init
);
2746 Next_Elmt
(Discr_Constr
);
2747 Next_Discriminant
(Discr
);
2750 In_Aggr_Type
:= False;
2751 Base_Typ
:= Base_Type
(Par_Typ
);
2753 end Init_Hidden_Discriminants
;
2755 --------------------------------
2756 -- Init_Visible_Discriminants --
2757 --------------------------------
2759 procedure Init_Visible_Discriminants
is
2760 Discriminant
: Entity_Id
;
2761 Discriminant_Value
: Node_Id
;
2764 Discriminant
:= First_Discriminant
(Typ
);
2765 while Present
(Discriminant
) loop
2767 Make_Selected_Component
(Loc
,
2768 Prefix
=> New_Copy_Tree
(Target
),
2769 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2771 Discriminant_Value
:=
2772 Get_Discriminant_Value
2773 (Discriminant
, Typ
, Discriminant_Constraint
(N_Typ
));
2776 Make_OK_Assignment_Statement
(Loc
,
2778 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2780 Append_To
(L
, Instr
);
2782 Next_Discriminant
(Discriminant
);
2784 end Init_Visible_Discriminants
;
2786 -------------------------------
2787 -- Init_Stored_Discriminants --
2788 -------------------------------
2790 procedure Init_Stored_Discriminants
is
2791 Discriminant
: Entity_Id
;
2792 Discriminant_Value
: Node_Id
;
2795 Discriminant
:= First_Stored_Discriminant
(Typ
);
2796 while Present
(Discriminant
) loop
2798 Make_Selected_Component
(Loc
,
2799 Prefix
=> New_Copy_Tree
(Target
),
2800 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2802 Discriminant_Value
:=
2803 Get_Discriminant_Value
2804 (Discriminant
, N_Typ
, Discriminant_Constraint
(N_Typ
));
2807 Make_OK_Assignment_Statement
(Loc
,
2809 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2811 Append_To
(L
, Instr
);
2813 Next_Stored_Discriminant
(Discriminant
);
2815 end Init_Stored_Discriminants
;
2817 --------------------------------------
2818 -- Initialize_Ctrl_Record_Component --
2819 --------------------------------------
2821 procedure Initialize_Ctrl_Record_Component
2822 (Rec_Comp
: Node_Id
;
2823 Comp_Typ
: Entity_Id
;
2824 Init_Expr
: Node_Id
;
2828 Hook_Clear
: Node_Id
;
2830 In_Place_Expansion
: Boolean;
2831 -- Flag set when a nonlimited controlled function call requires
2832 -- in-place expansion.
2835 -- Perform a preliminary analysis and resolution to determine what
2836 -- the initialization expression denotes. Unanalyzed function calls
2837 -- may appear as identifiers or indexed components.
2839 if Nkind_In
(Init_Expr
, N_Function_Call
,
2841 N_Indexed_Component
)
2842 and then not Analyzed
(Init_Expr
)
2844 Preanalyze_And_Resolve
(Init_Expr
, Comp_Typ
);
2847 In_Place_Expansion
:=
2848 Nkind
(Init_Expr
) = N_Function_Call
2849 and then not Is_Build_In_Place_Result_Type
(Comp_Typ
);
2851 -- The initialization expression is a controlled function call.
2852 -- Perform in-place removal of side effects to avoid creating a
2855 -- This in-place expansion is not performed for limited transient
2856 -- objects because the initialization is already done in place.
2858 if In_Place_Expansion
then
2860 -- Suppress the removal of side effects by general analysis
2861 -- because this behavior is emulated here. This avoids the
2862 -- generation of a transient scope, which leads to out-of-order
2863 -- adjustment and finalization.
2865 Set_No_Side_Effect_Removal
(Init_Expr
);
2867 -- Install all hook-related declarations and prepare the clean up
2868 -- statements. The generated code follows the initialization order
2869 -- of individual components and discriminants, rather than being
2870 -- inserted prior to the aggregate. This ensures that a transient
2871 -- component which mentions a discriminant has proper visibility
2872 -- of the discriminant.
2874 Process_Transient_Component
2876 Comp_Typ
=> Comp_Typ
,
2877 Init_Expr
=> Init_Expr
,
2878 Fin_Call
=> Fin_Call
,
2879 Hook_Clear
=> Hook_Clear
,
2883 -- Use the noncontrolled component initialization circuitry to
2884 -- assign the result of the function call to the record component.
2885 -- This also performs tag adjustment and [deep] adjustment of the
2886 -- record component.
2888 Initialize_Record_Component
2889 (Rec_Comp
=> Rec_Comp
,
2890 Comp_Typ
=> Comp_Typ
,
2891 Init_Expr
=> Init_Expr
,
2894 -- At this point the record component is fully initialized. Complete
2895 -- the processing of the controlled record component by finalizing
2896 -- the transient function result.
2898 if In_Place_Expansion
then
2899 Process_Transient_Component_Completion
2902 Fin_Call
=> Fin_Call
,
2903 Hook_Clear
=> Hook_Clear
,
2906 end Initialize_Ctrl_Record_Component
;
2908 ---------------------------------
2909 -- Initialize_Record_Component --
2910 ---------------------------------
2912 procedure Initialize_Record_Component
2913 (Rec_Comp
: Node_Id
;
2914 Comp_Typ
: Entity_Id
;
2915 Init_Expr
: Node_Id
;
2918 Exceptions_OK
: constant Boolean :=
2919 not Restriction_Active
(No_Exception_Propagation
);
2921 Finalization_OK
: constant Boolean := Needs_Finalization
(Comp_Typ
);
2923 Full_Typ
: constant Entity_Id
:= Underlying_Type
(Comp_Typ
);
2925 Blk_Stmts
: List_Id
;
2926 Init_Stmt
: Node_Id
;
2929 -- Protect the initialization statements from aborts. Generate:
2933 if Finalization_OK
and Abort_Allowed
then
2934 if Exceptions_OK
then
2935 Blk_Stmts
:= New_List
;
2940 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
2942 -- Otherwise aborts are not allowed. All generated code is added
2943 -- directly to the input list.
2949 -- Initialize the record component. Generate:
2951 -- Rec_Comp := Init_Expr;
2953 -- Note that the initialization expression is NOT replicated because
2954 -- only a single component may be initialized by it.
2957 Make_OK_Assignment_Statement
(Loc
,
2958 Name
=> New_Copy_Tree
(Rec_Comp
),
2959 Expression
=> Init_Expr
);
2960 Set_No_Ctrl_Actions
(Init_Stmt
);
2962 Append_To
(Blk_Stmts
, Init_Stmt
);
2964 -- Adjust the tag due to a possible view conversion. Generate:
2966 -- Rec_Comp._tag := Full_TypeP;
2968 if Tagged_Type_Expansion
and then Is_Tagged_Type
(Comp_Typ
) then
2969 Append_To
(Blk_Stmts
,
2970 Make_OK_Assignment_Statement
(Loc
,
2972 Make_Selected_Component
(Loc
,
2973 Prefix
=> New_Copy_Tree
(Rec_Comp
),
2976 (First_Tag_Component
(Full_Typ
), Loc
)),
2979 Unchecked_Convert_To
(RTE
(RE_Tag
),
2981 (Node
(First_Elmt
(Access_Disp_Table
(Full_Typ
))),
2985 -- Adjust the component. Generate:
2987 -- [Deep_]Adjust (Rec_Comp);
2990 and then not Is_Limited_Type
(Comp_Typ
)
2991 and then not Is_Build_In_Place_Function_Call
(Init_Expr
)
2995 (Obj_Ref
=> New_Copy_Tree
(Rec_Comp
),
2998 -- Guard against a missing [Deep_]Adjust when the component type
2999 -- was not properly frozen.
3001 if Present
(Adj_Call
) then
3002 Append_To
(Blk_Stmts
, Adj_Call
);
3006 -- Complete the protection of the initialization statements
3008 if Finalization_OK
and Abort_Allowed
then
3010 -- Wrap the initialization statements in a block to catch a
3011 -- potential exception. Generate:
3015 -- Rec_Comp := Init_Expr;
3016 -- Rec_Comp._tag := Full_TypP;
3017 -- [Deep_]Adjust (Rec_Comp);
3019 -- Abort_Undefer_Direct;
3022 if Exceptions_OK
then
3024 Build_Abort_Undefer_Block
(Loc
,
3028 -- Otherwise exceptions are not propagated. Generate:
3031 -- Rec_Comp := Init_Expr;
3032 -- Rec_Comp._tag := Full_TypP;
3033 -- [Deep_]Adjust (Rec_Comp);
3037 Append_To
(Blk_Stmts
,
3038 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
3041 end Initialize_Record_Component
;
3043 -------------------------
3044 -- Is_Int_Range_Bounds --
3045 -------------------------
3047 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean is
3049 return Nkind
(Bounds
) = N_Range
3050 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
3051 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
;
3052 end Is_Int_Range_Bounds
;
3058 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
is
3060 -- Note regarding the Root_Type test below: Aggregate components for
3061 -- self-referential types include attribute references to the current
3062 -- instance, of the form: Typ'access, etc.. These references are
3063 -- rewritten as references to the target of the aggregate: the
3064 -- left-hand side of an assignment, the entity in a declaration,
3065 -- or a temporary. Without this test, we would improperly extended
3066 -- this rewriting to attribute references whose prefix was not the
3067 -- type of the aggregate.
3069 if Nkind
(Expr
) = N_Attribute_Reference
3070 and then Is_Entity_Name
(Prefix
(Expr
))
3071 and then Is_Type
(Entity
(Prefix
(Expr
)))
3072 and then Root_Type
(Etype
(N
)) = Root_Type
(Entity
(Prefix
(Expr
)))
3074 if Is_Entity_Name
(Lhs
) then
3075 Rewrite
(Prefix
(Expr
), New_Occurrence_Of
(Entity
(Lhs
), Loc
));
3079 Make_Attribute_Reference
(Loc
,
3080 Attribute_Name
=> Name_Unrestricted_Access
,
3081 Prefix
=> New_Copy_Tree
(Lhs
)));
3082 Set_Analyzed
(Parent
(Expr
), False);
3089 --------------------------
3090 -- Rewrite_Discriminant --
3091 --------------------------
3093 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
is
3095 if Is_Entity_Name
(Expr
)
3096 and then Present
(Entity
(Expr
))
3097 and then Ekind
(Entity
(Expr
)) = E_In_Parameter
3098 and then Present
(Discriminal_Link
(Entity
(Expr
)))
3099 and then Scope
(Discriminal_Link
(Entity
(Expr
))) =
3100 Base_Type
(Etype
(N
))
3103 Make_Selected_Component
(Loc
,
3104 Prefix
=> New_Copy_Tree
(Lhs
),
3105 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Expr
))));
3109 end Rewrite_Discriminant
;
3111 procedure Replace_Discriminants
is
3112 new Traverse_Proc
(Rewrite_Discriminant
);
3114 procedure Replace_Self_Reference
is
3115 new Traverse_Proc
(Replace_Type
);
3117 -- Start of processing for Build_Record_Aggr_Code
3120 if Has_Self_Reference
(N
) then
3121 Replace_Self_Reference
(N
);
3124 -- If the target of the aggregate is class-wide, we must convert it
3125 -- to the actual type of the aggregate, so that the proper components
3126 -- are visible. We know already that the types are compatible.
3128 if Present
(Etype
(Lhs
))
3129 and then Is_Class_Wide_Type
(Etype
(Lhs
))
3131 Target
:= Unchecked_Convert_To
(Typ
, Lhs
);
3136 -- Deal with the ancestor part of extension aggregates or with the
3137 -- discriminants of the root type.
3139 if Nkind
(N
) = N_Extension_Aggregate
then
3141 Ancestor
: constant Node_Id
:= Ancestor_Part
(N
);
3146 -- If the ancestor part is a subtype mark "T", we generate
3148 -- init-proc (T (tmp)); if T is constrained and
3149 -- init-proc (S (tmp)); where S applies an appropriate
3150 -- constraint if T is unconstrained
3152 if Is_Entity_Name
(Ancestor
)
3153 and then Is_Type
(Entity
(Ancestor
))
3155 Ancestor_Is_Subtype_Mark
:= True;
3157 if Is_Constrained
(Entity
(Ancestor
)) then
3158 Init_Typ
:= Entity
(Ancestor
);
3160 -- For an ancestor part given by an unconstrained type mark,
3161 -- create a subtype constrained by appropriate corresponding
3162 -- discriminant values coming from either associations of the
3163 -- aggregate or a constraint on a parent type. The subtype will
3164 -- be used to generate the correct default value for the
3167 elsif Has_Discriminants
(Entity
(Ancestor
)) then
3169 Anc_Typ
: constant Entity_Id
:= Entity
(Ancestor
);
3170 Anc_Constr
: constant List_Id
:= New_List
;
3171 Discrim
: Entity_Id
;
3172 Disc_Value
: Node_Id
;
3173 New_Indic
: Node_Id
;
3174 Subt_Decl
: Node_Id
;
3177 Discrim
:= First_Discriminant
(Anc_Typ
);
3178 while Present
(Discrim
) loop
3179 Disc_Value
:= Ancestor_Discriminant_Value
(Discrim
);
3181 -- If no usable discriminant in ancestors, check
3182 -- whether aggregate has an explicit value for it.
3184 if No
(Disc_Value
) then
3186 Get_Explicit_Discriminant_Value
(Discrim
);
3189 Append_To
(Anc_Constr
, Disc_Value
);
3190 Next_Discriminant
(Discrim
);
3194 Make_Subtype_Indication
(Loc
,
3195 Subtype_Mark
=> New_Occurrence_Of
(Anc_Typ
, Loc
),
3197 Make_Index_Or_Discriminant_Constraint
(Loc
,
3198 Constraints
=> Anc_Constr
));
3200 Init_Typ
:= Create_Itype
(Ekind
(Anc_Typ
), N
);
3203 Make_Subtype_Declaration
(Loc
,
3204 Defining_Identifier
=> Init_Typ
,
3205 Subtype_Indication
=> New_Indic
);
3207 -- Itypes must be analyzed with checks off Declaration
3208 -- must have a parent for proper handling of subsidiary
3211 Set_Parent
(Subt_Decl
, N
);
3212 Analyze
(Subt_Decl
, Suppress
=> All_Checks
);
3216 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3217 Set_Assignment_OK
(Ref
);
3219 if not Is_Interface
(Init_Typ
) then
3221 Build_Initialization_Call
(Loc
,
3224 In_Init_Proc
=> Within_Init_Proc
,
3225 With_Default_Init
=> Has_Default_Init_Comps
(N
)
3227 Has_Task
(Base_Type
(Init_Typ
))));
3229 if Is_Constrained
(Entity
(Ancestor
))
3230 and then Has_Discriminants
(Entity
(Ancestor
))
3232 Check_Ancestor_Discriminants
(Entity
(Ancestor
));
3236 -- Handle calls to C++ constructors
3238 elsif Is_CPP_Constructor_Call
(Ancestor
) then
3239 Init_Typ
:= Etype
(Ancestor
);
3240 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3241 Set_Assignment_OK
(Ref
);
3244 Build_Initialization_Call
(Loc
,
3247 In_Init_Proc
=> Within_Init_Proc
,
3248 With_Default_Init
=> Has_Default_Init_Comps
(N
),
3249 Constructor_Ref
=> Ancestor
));
3251 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3252 -- limited type, a recursive call expands the ancestor. Note that
3253 -- in the limited case, the ancestor part must be either a
3254 -- function call (possibly qualified) or aggregate (definitely
3257 elsif Is_Limited_Type
(Etype
(Ancestor
))
3258 and then Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
3259 N_Extension_Aggregate
)
3261 Ancestor_Is_Expression
:= True;
3263 -- Set up finalization data for enclosing record, because
3264 -- controlled subcomponents of the ancestor part will be
3267 Generate_Finalization_Actions
;
3270 Build_Record_Aggr_Code
3271 (N
=> Unqualify
(Ancestor
),
3272 Typ
=> Etype
(Unqualify
(Ancestor
)),
3275 -- If the ancestor part is an expression "E", we generate
3279 -- In Ada 2005, this includes the case of a (possibly qualified)
3280 -- limited function call. The assignment will turn into a
3281 -- build-in-place function call (for further details, see
3282 -- Make_Build_In_Place_Call_In_Assignment).
3285 Ancestor_Is_Expression
:= True;
3286 Init_Typ
:= Etype
(Ancestor
);
3288 -- If the ancestor part is an aggregate, force its full
3289 -- expansion, which was delayed.
3291 if Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
3292 N_Extension_Aggregate
)
3294 Set_Analyzed
(Ancestor
, False);
3295 Set_Analyzed
(Expression
(Ancestor
), False);
3298 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3299 Set_Assignment_OK
(Ref
);
3301 -- Make the assignment without usual controlled actions, since
3302 -- we only want to Adjust afterwards, but not to Finalize
3303 -- beforehand. Add manual Adjust when necessary.
3305 Assign
:= New_List
(
3306 Make_OK_Assignment_Statement
(Loc
,
3308 Expression
=> Ancestor
));
3309 Set_No_Ctrl_Actions
(First
(Assign
));
3311 -- Assign the tag now to make sure that the dispatching call in
3312 -- the subsequent deep_adjust works properly (unless
3313 -- Tagged_Type_Expansion where tags are implicit).
3315 if Tagged_Type_Expansion
then
3317 Make_OK_Assignment_Statement
(Loc
,
3319 Make_Selected_Component
(Loc
,
3320 Prefix
=> New_Copy_Tree
(Target
),
3323 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
3326 Unchecked_Convert_To
(RTE
(RE_Tag
),
3329 (Access_Disp_Table
(Base_Type
(Typ
)))),
3332 Set_Assignment_OK
(Name
(Instr
));
3333 Append_To
(Assign
, Instr
);
3335 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3336 -- also initialize tags of the secondary dispatch tables.
3338 if Has_Interfaces
(Base_Type
(Typ
)) then
3340 (Typ
=> Base_Type
(Typ
),
3342 Stmts_List
=> Assign
,
3343 Init_Tags_List
=> Assign
);
3347 -- Call Adjust manually
3349 if Needs_Finalization
(Etype
(Ancestor
))
3350 and then not Is_Limited_Type
(Etype
(Ancestor
))
3351 and then not Is_Build_In_Place_Function_Call
(Ancestor
)
3355 (Obj_Ref
=> New_Copy_Tree
(Ref
),
3356 Typ
=> Etype
(Ancestor
));
3358 -- Guard against a missing [Deep_]Adjust when the ancestor
3359 -- type was not properly frozen.
3361 if Present
(Adj_Call
) then
3362 Append_To
(Assign
, Adj_Call
);
3367 Make_Unsuppress_Block
(Loc
, Name_Discriminant_Check
, Assign
));
3369 if Has_Discriminants
(Init_Typ
) then
3370 Check_Ancestor_Discriminants
(Init_Typ
);
3374 pragma Assert
(Nkind
(N
) = N_Extension_Aggregate
);
3376 (not (Ancestor_Is_Expression
and Ancestor_Is_Subtype_Mark
));
3379 -- Generate assignments of hidden discriminants. If the base type is
3380 -- an unchecked union, the discriminants are unknown to the back-end
3381 -- and absent from a value of the type, so assignments for them are
3384 if Has_Discriminants
(Typ
)
3385 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
3387 Init_Hidden_Discriminants
(Typ
, L
);
3390 -- Normal case (not an extension aggregate)
3393 -- Generate the discriminant expressions, component by component.
3394 -- If the base type is an unchecked union, the discriminants are
3395 -- unknown to the back-end and absent from a value of the type, so
3396 -- assignments for them are not emitted.
3398 if Has_Discriminants
(Typ
)
3399 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
3401 Init_Hidden_Discriminants
(Typ
, L
);
3403 -- Generate discriminant init values for the visible discriminants
3405 Init_Visible_Discriminants
;
3407 if Is_Derived_Type
(N_Typ
) then
3408 Init_Stored_Discriminants
;
3413 -- For CPP types we generate an implicit call to the C++ default
3414 -- constructor to ensure the proper initialization of the _Tag
3417 if Is_CPP_Class
(Root_Type
(Typ
)) and then CPP_Num_Prims
(Typ
) > 0 then
3418 Invoke_Constructor
: declare
3419 CPP_Parent
: constant Entity_Id
:= Enclosing_CPP_Parent
(Typ
);
3421 procedure Invoke_IC_Proc
(T
: Entity_Id
);
3422 -- Recursive routine used to climb to parents. Required because
3423 -- parents must be initialized before descendants to ensure
3424 -- propagation of inherited C++ slots.
3426 --------------------
3427 -- Invoke_IC_Proc --
3428 --------------------
3430 procedure Invoke_IC_Proc
(T
: Entity_Id
) is
3432 -- Avoid generating extra calls. Initialization required
3433 -- only for types defined from the level of derivation of
3434 -- type of the constructor and the type of the aggregate.
3436 if T
= CPP_Parent
then
3440 Invoke_IC_Proc
(Etype
(T
));
3442 -- Generate call to the IC routine
3444 if Present
(CPP_Init_Proc
(T
)) then
3446 Make_Procedure_Call_Statement
(Loc
,
3447 Name
=> New_Occurrence_Of
(CPP_Init_Proc
(T
), Loc
)));
3451 -- Start of processing for Invoke_Constructor
3454 -- Implicit invocation of the C++ constructor
3456 if Nkind
(N
) = N_Aggregate
then
3458 Make_Procedure_Call_Statement
(Loc
,
3460 New_Occurrence_Of
(Base_Init_Proc
(CPP_Parent
), Loc
),
3461 Parameter_Associations
=> New_List
(
3462 Unchecked_Convert_To
(CPP_Parent
,
3463 New_Copy_Tree
(Lhs
)))));
3466 Invoke_IC_Proc
(Typ
);
3467 end Invoke_Constructor
;
3470 -- Generate the assignments, component by component
3472 -- tmp.comp1 := Expr1_From_Aggr;
3473 -- tmp.comp2 := Expr2_From_Aggr;
3476 Comp
:= First
(Component_Associations
(N
));
3477 while Present
(Comp
) loop
3478 Selector
:= Entity
(First
(Choices
(Comp
)));
3482 if Is_CPP_Constructor_Call
(Expression
(Comp
)) then
3484 Build_Initialization_Call
(Loc
,
3486 Make_Selected_Component
(Loc
,
3487 Prefix
=> New_Copy_Tree
(Target
),
3488 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
)),
3489 Typ
=> Etype
(Selector
),
3491 With_Default_Init
=> True,
3492 Constructor_Ref
=> Expression
(Comp
)));
3494 -- Ada 2005 (AI-287): For each default-initialized component generate
3495 -- a call to the corresponding IP subprogram if available.
3497 elsif Box_Present
(Comp
)
3498 and then Has_Non_Null_Base_Init_Proc
(Etype
(Selector
))
3500 if Ekind
(Selector
) /= E_Discriminant
then
3501 Generate_Finalization_Actions
;
3504 -- Ada 2005 (AI-287): If the component type has tasks then
3505 -- generate the activation chain and master entities (except
3506 -- in case of an allocator because in that case these entities
3507 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3510 Ctype
: constant Entity_Id
:= Etype
(Selector
);
3511 Inside_Allocator
: Boolean := False;
3512 P
: Node_Id
:= Parent
(N
);
3515 if Is_Task_Type
(Ctype
) or else Has_Task
(Ctype
) then
3516 while Present
(P
) loop
3517 if Nkind
(P
) = N_Allocator
then
3518 Inside_Allocator
:= True;
3525 if not Inside_Init_Proc
and not Inside_Allocator
then
3526 Build_Activation_Chain_Entity
(N
);
3532 Build_Initialization_Call
(Loc
,
3533 Id_Ref
=> Make_Selected_Component
(Loc
,
3534 Prefix
=> New_Copy_Tree
(Target
),
3536 New_Occurrence_Of
(Selector
, Loc
)),
3537 Typ
=> Etype
(Selector
),
3539 With_Default_Init
=> True));
3541 -- Prepare for component assignment
3543 elsif Ekind
(Selector
) /= E_Discriminant
3544 or else Nkind
(N
) = N_Extension_Aggregate
3546 -- All the discriminants have now been assigned
3548 -- This is now a good moment to initialize and attach all the
3549 -- controllers. Their position may depend on the discriminants.
3551 if Ekind
(Selector
) /= E_Discriminant
then
3552 Generate_Finalization_Actions
;
3555 Comp_Type
:= Underlying_Type
(Etype
(Selector
));
3557 Make_Selected_Component
(Loc
,
3558 Prefix
=> New_Copy_Tree
(Target
),
3559 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
));
3561 if Nkind
(Expression
(Comp
)) = N_Qualified_Expression
then
3562 Expr_Q
:= Expression
(Expression
(Comp
));
3564 Expr_Q
:= Expression
(Comp
);
3567 -- Now either create the assignment or generate the code for the
3568 -- inner aggregate top-down.
3570 if Is_Delayed_Aggregate
(Expr_Q
) then
3572 -- We have the following case of aggregate nesting inside
3573 -- an object declaration:
3575 -- type Arr_Typ is array (Integer range <>) of ...;
3577 -- type Rec_Typ (...) is record
3578 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3581 -- Obj_Rec_Typ : Rec_Typ := (...,
3582 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3584 -- The length of the ranges of the aggregate and Obj_Add_Typ
3585 -- are equal (B - A = Y - X), but they do not coincide (X /=
3586 -- A and B /= Y). This case requires array sliding which is
3587 -- performed in the following manner:
3589 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3591 -- Temp (X) := (...);
3593 -- Temp (Y) := (...);
3594 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3596 if Ekind
(Comp_Type
) = E_Array_Subtype
3597 and then Is_Int_Range_Bounds
(Aggregate_Bounds
(Expr_Q
))
3598 and then Is_Int_Range_Bounds
(First_Index
(Comp_Type
))
3600 Compatible_Int_Bounds
3601 (Agg_Bounds
=> Aggregate_Bounds
(Expr_Q
),
3602 Typ_Bounds
=> First_Index
(Comp_Type
))
3604 -- Create the array subtype with bounds equal to those of
3605 -- the corresponding aggregate.
3608 SubE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
3610 SubD
: constant Node_Id
:=
3611 Make_Subtype_Declaration
(Loc
,
3612 Defining_Identifier
=> SubE
,
3613 Subtype_Indication
=>
3614 Make_Subtype_Indication
(Loc
,
3616 New_Occurrence_Of
(Etype
(Comp_Type
), Loc
),
3618 Make_Index_Or_Discriminant_Constraint
3620 Constraints
=> New_List
(
3622 (Aggregate_Bounds
(Expr_Q
))))));
3624 -- Create a temporary array of the above subtype which
3625 -- will be used to capture the aggregate assignments.
3627 TmpE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A', N
);
3629 TmpD
: constant Node_Id
:=
3630 Make_Object_Declaration
(Loc
,
3631 Defining_Identifier
=> TmpE
,
3632 Object_Definition
=> New_Occurrence_Of
(SubE
, Loc
));
3635 Set_No_Initialization
(TmpD
);
3636 Append_To
(L
, SubD
);
3637 Append_To
(L
, TmpD
);
3639 -- Expand aggregate into assignments to the temp array
3642 Late_Expansion
(Expr_Q
, Comp_Type
,
3643 New_Occurrence_Of
(TmpE
, Loc
)));
3648 Make_Assignment_Statement
(Loc
,
3649 Name
=> New_Copy_Tree
(Comp_Expr
),
3650 Expression
=> New_Occurrence_Of
(TmpE
, Loc
)));
3653 -- Normal case (sliding not required)
3657 Late_Expansion
(Expr_Q
, Comp_Type
, Comp_Expr
));
3660 -- Expr_Q is not delayed aggregate
3663 if Has_Discriminants
(Typ
) then
3664 Replace_Discriminants
(Expr_Q
);
3666 -- If the component is an array type that depends on
3667 -- discriminants, and the expression is a single Others
3668 -- clause, create an explicit subtype for it because the
3669 -- backend has troubles recovering the actual bounds.
3671 if Nkind
(Expr_Q
) = N_Aggregate
3672 and then Is_Array_Type
(Comp_Type
)
3673 and then Present
(Component_Associations
(Expr_Q
))
3676 Assoc
: constant Node_Id
:=
3677 First
(Component_Associations
(Expr_Q
));
3681 if Nkind
(First
(Choices
(Assoc
))) = N_Others_Choice
3684 Build_Actual_Subtype_Of_Component
3685 (Comp_Type
, Comp_Expr
);
3687 -- If the component type does not in fact depend on
3688 -- discriminants, the subtype declaration is empty.
3690 if Present
(Decl
) then
3691 Append_To
(L
, Decl
);
3692 Set_Etype
(Comp_Expr
, Defining_Entity
(Decl
));
3699 if Modify_Tree_For_C
3700 and then Nkind
(Expr_Q
) = N_Aggregate
3701 and then Is_Array_Type
(Etype
(Expr_Q
))
3702 and then Present
(First_Index
(Etype
(Expr_Q
)))
3705 Expr_Q_Type
: constant Node_Id
:= Etype
(Expr_Q
);
3708 Build_Array_Aggr_Code
3710 Ctype
=> Component_Type
(Expr_Q_Type
),
3711 Index
=> First_Index
(Expr_Q_Type
),
3714 Is_Scalar_Type
(Component_Type
(Expr_Q_Type
))));
3718 -- Handle an initialization expression of a controlled type
3719 -- in case it denotes a function call. In general such a
3720 -- scenario will produce a transient scope, but this will
3721 -- lead to wrong order of initialization, adjustment, and
3722 -- finalization in the context of aggregates.
3724 -- Target.Comp := Ctrl_Func_Call;
3727 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3728 -- Target.Comp := Trans_Obj;
3729 -- Finalize (Trans_Obj);
3731 -- Target.Comp._tag := ...;
3732 -- Adjust (Target.Comp);
3734 -- In the example above, the call to Finalize occurs too
3735 -- early and as a result it may leave the record component
3736 -- in a bad state. Finalization of the transient object
3737 -- should really happen after adjustment.
3739 -- To avoid this scenario, perform in-place side-effect
3740 -- removal of the function call. This eliminates the
3741 -- transient property of the function result and ensures
3742 -- correct order of actions.
3744 -- Res : ... := Ctrl_Func_Call;
3745 -- Target.Comp := Res;
3746 -- Target.Comp._tag := ...;
3747 -- Adjust (Target.Comp);
3750 if Needs_Finalization
(Comp_Type
)
3751 and then Nkind
(Expr_Q
) /= N_Aggregate
3753 Initialize_Ctrl_Record_Component
3754 (Rec_Comp
=> Comp_Expr
,
3755 Comp_Typ
=> Etype
(Selector
),
3756 Init_Expr
=> Expr_Q
,
3759 -- Otherwise perform single component initialization
3762 Initialize_Record_Component
3763 (Rec_Comp
=> Comp_Expr
,
3764 Comp_Typ
=> Etype
(Selector
),
3765 Init_Expr
=> Expr_Q
,
3771 -- comment would be good here ???
3773 elsif Ekind
(Selector
) = E_Discriminant
3774 and then Nkind
(N
) /= N_Extension_Aggregate
3775 and then Nkind
(Parent
(N
)) = N_Component_Association
3776 and then Is_Constrained
(Typ
)
3778 -- We must check that the discriminant value imposed by the
3779 -- context is the same as the value given in the subaggregate,
3780 -- because after the expansion into assignments there is no
3781 -- record on which to perform a regular discriminant check.
3788 D_Val
:= First_Elmt
(Discriminant_Constraint
(Typ
));
3789 Disc
:= First_Discriminant
(Typ
);
3790 while Chars
(Disc
) /= Chars
(Selector
) loop
3791 Next_Discriminant
(Disc
);
3795 pragma Assert
(Present
(D_Val
));
3797 -- This check cannot performed for components that are
3798 -- constrained by a current instance, because this is not a
3799 -- value that can be compared with the actual constraint.
3801 if Nkind
(Node
(D_Val
)) /= N_Attribute_Reference
3802 or else not Is_Entity_Name
(Prefix
(Node
(D_Val
)))
3803 or else not Is_Type
(Entity
(Prefix
(Node
(D_Val
))))
3806 Make_Raise_Constraint_Error
(Loc
,
3809 Left_Opnd
=> New_Copy_Tree
(Node
(D_Val
)),
3810 Right_Opnd
=> Expression
(Comp
)),
3811 Reason
=> CE_Discriminant_Check_Failed
));
3814 -- Find self-reference in previous discriminant assignment,
3815 -- and replace with proper expression.
3822 while Present
(Ass
) loop
3823 if Nkind
(Ass
) = N_Assignment_Statement
3824 and then Nkind
(Name
(Ass
)) = N_Selected_Component
3825 and then Chars
(Selector_Name
(Name
(Ass
))) =
3829 (Ass
, New_Copy_Tree
(Expression
(Comp
)));
3842 -- If the type is tagged, the tag needs to be initialized (unless we
3843 -- are in VM-mode where tags are implicit). It is done late in the
3844 -- initialization process because in some cases, we call the init
3845 -- proc of an ancestor which will not leave out the right tag.
3847 if Ancestor_Is_Expression
then
3850 -- For CPP types we generated a call to the C++ default constructor
3851 -- before the components have been initialized to ensure the proper
3852 -- initialization of the _Tag component (see above).
3854 elsif Is_CPP_Class
(Typ
) then
3857 elsif Is_Tagged_Type
(Typ
) and then Tagged_Type_Expansion
then
3859 Make_OK_Assignment_Statement
(Loc
,
3861 Make_Selected_Component
(Loc
,
3862 Prefix
=> New_Copy_Tree
(Target
),
3865 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
3868 Unchecked_Convert_To
(RTE
(RE_Tag
),
3870 (Node
(First_Elmt
(Access_Disp_Table
(Base_Type
(Typ
)))),
3873 Append_To
(L
, Instr
);
3875 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3876 -- abstract interfaces we must also initialize the tags of the
3877 -- secondary dispatch tables.
3879 if Has_Interfaces
(Base_Type
(Typ
)) then
3881 (Typ
=> Base_Type
(Typ
),
3884 Init_Tags_List
=> L
);
3888 -- If the controllers have not been initialized yet (by lack of non-
3889 -- discriminant components), let's do it now.
3891 Generate_Finalization_Actions
;
3894 end Build_Record_Aggr_Code
;
3896 ---------------------------------------
3897 -- Collect_Initialization_Statements --
3898 ---------------------------------------
3900 procedure Collect_Initialization_Statements
3903 Node_After
: Node_Id
)
3905 Loc
: constant Source_Ptr
:= Sloc
(N
);
3906 Init_Actions
: constant List_Id
:= New_List
;
3907 Init_Node
: Node_Id
;
3908 Comp_Stmt
: Node_Id
;
3911 -- Nothing to do if Obj is already frozen, as in this case we known we
3912 -- won't need to move the initialization statements about later on.
3914 if Is_Frozen
(Obj
) then
3919 while Next
(Init_Node
) /= Node_After
loop
3920 Append_To
(Init_Actions
, Remove_Next
(Init_Node
));
3923 if not Is_Empty_List
(Init_Actions
) then
3924 Comp_Stmt
:= Make_Compound_Statement
(Loc
, Actions
=> Init_Actions
);
3925 Insert_Action_After
(Init_Node
, Comp_Stmt
);
3926 Set_Initialization_Statements
(Obj
, Comp_Stmt
);
3928 end Collect_Initialization_Statements
;
3930 -------------------------------
3931 -- Convert_Aggr_In_Allocator --
3932 -------------------------------
3934 procedure Convert_Aggr_In_Allocator
3939 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3940 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3941 Temp
: constant Entity_Id
:= Defining_Identifier
(Decl
);
3943 Occ
: constant Node_Id
:=
3944 Unchecked_Convert_To
(Typ
,
3945 Make_Explicit_Dereference
(Loc
, New_Occurrence_Of
(Temp
, Loc
)));
3948 if Is_Array_Type
(Typ
) then
3949 Convert_Array_Aggr_In_Allocator
(Decl
, Aggr
, Occ
);
3951 elsif Has_Default_Init_Comps
(Aggr
) then
3953 L
: constant List_Id
:= New_List
;
3954 Init_Stmts
: List_Id
;
3957 Init_Stmts
:= Late_Expansion
(Aggr
, Typ
, Occ
);
3959 if Has_Task
(Typ
) then
3960 Build_Task_Allocate_Block_With_Init_Stmts
(L
, Aggr
, Init_Stmts
);
3961 Insert_Actions
(Alloc
, L
);
3963 Insert_Actions
(Alloc
, Init_Stmts
);
3968 Insert_Actions
(Alloc
, Late_Expansion
(Aggr
, Typ
, Occ
));
3970 end Convert_Aggr_In_Allocator
;
3972 --------------------------------
3973 -- Convert_Aggr_In_Assignment --
3974 --------------------------------
3976 procedure Convert_Aggr_In_Assignment
(N
: Node_Id
) is
3977 Aggr
: Node_Id
:= Expression
(N
);
3978 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3979 Occ
: constant Node_Id
:= New_Copy_Tree
(Name
(N
));
3982 if Nkind
(Aggr
) = N_Qualified_Expression
then
3983 Aggr
:= Expression
(Aggr
);
3986 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
3987 end Convert_Aggr_In_Assignment
;
3989 ---------------------------------
3990 -- Convert_Aggr_In_Object_Decl --
3991 ---------------------------------
3993 procedure Convert_Aggr_In_Object_Decl
(N
: Node_Id
) is
3994 Obj
: constant Entity_Id
:= Defining_Identifier
(N
);
3995 Aggr
: Node_Id
:= Expression
(N
);
3996 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3997 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3998 Occ
: constant Node_Id
:= New_Occurrence_Of
(Obj
, Loc
);
4000 function Discriminants_Ok
return Boolean;
4001 -- If the object type is constrained, the discriminants in the
4002 -- aggregate must be checked against the discriminants of the subtype.
4003 -- This cannot be done using Apply_Discriminant_Checks because after
4004 -- expansion there is no aggregate left to check.
4006 ----------------------
4007 -- Discriminants_Ok --
4008 ----------------------
4010 function Discriminants_Ok
return Boolean is
4011 Cond
: Node_Id
:= Empty
;
4020 D
:= First_Discriminant
(Typ
);
4021 Disc1
:= First_Elmt
(Discriminant_Constraint
(Typ
));
4022 Disc2
:= First_Elmt
(Discriminant_Constraint
(Etype
(Obj
)));
4023 while Present
(Disc1
) and then Present
(Disc2
) loop
4024 Val1
:= Node
(Disc1
);
4025 Val2
:= Node
(Disc2
);
4027 if not Is_OK_Static_Expression
(Val1
)
4028 or else not Is_OK_Static_Expression
(Val2
)
4030 Check
:= Make_Op_Ne
(Loc
,
4031 Left_Opnd
=> Duplicate_Subexpr
(Val1
),
4032 Right_Opnd
=> Duplicate_Subexpr
(Val2
));
4038 Cond
:= Make_Or_Else
(Loc
,
4040 Right_Opnd
=> Check
);
4043 elsif Expr_Value
(Val1
) /= Expr_Value
(Val2
) then
4044 Apply_Compile_Time_Constraint_Error
(Aggr
,
4045 Msg
=> "incorrect value for discriminant&??",
4046 Reason
=> CE_Discriminant_Check_Failed
,
4051 Next_Discriminant
(D
);
4056 -- If any discriminant constraint is nonstatic, emit a check
4058 if Present
(Cond
) then
4060 Make_Raise_Constraint_Error
(Loc
,
4062 Reason
=> CE_Discriminant_Check_Failed
));
4066 end Discriminants_Ok
;
4068 -- Start of processing for Convert_Aggr_In_Object_Decl
4071 Set_Assignment_OK
(Occ
);
4073 if Nkind
(Aggr
) = N_Qualified_Expression
then
4074 Aggr
:= Expression
(Aggr
);
4077 if Has_Discriminants
(Typ
)
4078 and then Typ
/= Etype
(Obj
)
4079 and then Is_Constrained
(Etype
(Obj
))
4080 and then not Discriminants_Ok
4085 -- If the context is an extended return statement, it has its own
4086 -- finalization machinery (i.e. works like a transient scope) and
4087 -- we do not want to create an additional one, because objects on
4088 -- the finalization list of the return must be moved to the caller's
4089 -- finalization list to complete the return.
4091 -- However, if the aggregate is limited, it is built in place, and the
4092 -- controlled components are not assigned to intermediate temporaries
4093 -- so there is no need for a transient scope in this case either.
4095 if Requires_Transient_Scope
(Typ
)
4096 and then Ekind
(Current_Scope
) /= E_Return_Statement
4097 and then not Is_Limited_Type
(Typ
)
4099 Establish_Transient_Scope
(Aggr
, Manage_Sec_Stack
=> False);
4103 Node_After
: constant Node_Id
:= Next
(N
);
4105 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
4106 Collect_Initialization_Statements
(Obj
, N
, Node_After
);
4109 Set_No_Initialization
(N
);
4110 Initialize_Discriminants
(N
, Typ
);
4111 end Convert_Aggr_In_Object_Decl
;
4113 -------------------------------------
4114 -- Convert_Array_Aggr_In_Allocator --
4115 -------------------------------------
4117 procedure Convert_Array_Aggr_In_Allocator
4122 Aggr_Code
: List_Id
;
4123 Typ
: constant Entity_Id
:= Etype
(Aggr
);
4124 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
4127 -- The target is an explicit dereference of the allocated object.
4128 -- Generate component assignments to it, as for an aggregate that
4129 -- appears on the right-hand side of an assignment statement.
4132 Build_Array_Aggr_Code
(Aggr
,
4134 Index
=> First_Index
(Typ
),
4136 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
4138 Insert_Actions_After
(Decl
, Aggr_Code
);
4139 end Convert_Array_Aggr_In_Allocator
;
4141 ----------------------------
4142 -- Convert_To_Assignments --
4143 ----------------------------
4145 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
) is
4146 Loc
: constant Source_Ptr
:= Sloc
(N
);
4150 Aggr_Code
: List_Id
;
4152 Target_Expr
: Node_Id
;
4153 Parent_Kind
: Node_Kind
;
4154 Unc_Decl
: Boolean := False;
4155 Parent_Node
: Node_Id
;
4158 pragma Assert
(Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
));
4159 pragma Assert
(not Is_Static_Dispatch_Table_Aggregate
(N
));
4160 pragma Assert
(Is_Record_Type
(Typ
));
4162 Parent_Node
:= Parent
(N
);
4163 Parent_Kind
:= Nkind
(Parent_Node
);
4165 if Parent_Kind
= N_Qualified_Expression
then
4166 -- Check if we are in an unconstrained declaration because in this
4167 -- case the current delayed expansion mechanism doesn't work when
4168 -- the declared object size depends on the initializing expr.
4170 Parent_Node
:= Parent
(Parent_Node
);
4171 Parent_Kind
:= Nkind
(Parent_Node
);
4173 if Parent_Kind
= N_Object_Declaration
then
4175 not Is_Entity_Name
(Object_Definition
(Parent_Node
))
4176 or else (Nkind
(N
) = N_Aggregate
4179 (Entity
(Object_Definition
(Parent_Node
))))
4180 or else Is_Class_Wide_Type
4181 (Entity
(Object_Definition
(Parent_Node
)));
4185 -- Just set the Delay flag in the cases where the transformation will be
4186 -- done top down from above.
4190 -- Internal aggregate (transformed when expanding the parent)
4192 or else Parent_Kind
= N_Aggregate
4193 or else Parent_Kind
= N_Extension_Aggregate
4194 or else Parent_Kind
= N_Component_Association
4196 -- Allocator (see Convert_Aggr_In_Allocator)
4198 or else Parent_Kind
= N_Allocator
4200 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4202 or else (Parent_Kind
= N_Object_Declaration
and then not Unc_Decl
)
4204 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4205 -- assignments in init procs are taken into account.
4207 or else (Parent_Kind
= N_Assignment_Statement
4208 and then Inside_Init_Proc
)
4210 -- (Ada 2005) An inherently limited type in a return statement, which
4211 -- will be handled in a build-in-place fashion, and may be rewritten
4212 -- as an extended return and have its own finalization machinery.
4213 -- In the case of a simple return, the aggregate needs to be delayed
4214 -- until the scope for the return statement has been created, so
4215 -- that any finalization chain will be associated with that scope.
4216 -- For extended returns, we delay expansion to avoid the creation
4217 -- of an unwanted transient scope that could result in premature
4218 -- finalization of the return object (which is built in place
4219 -- within the caller's scope).
4221 or else Is_Build_In_Place_Aggregate_Return
(N
)
4223 Set_Expansion_Delayed
(N
);
4227 -- Otherwise, if a transient scope is required, create it now. If we
4228 -- are within an initialization procedure do not create such, because
4229 -- the target of the assignment must not be declared within a local
4230 -- block, and because cleanup will take place on return from the
4231 -- initialization procedure.
4233 -- Should the condition be more restrictive ???
4235 if Requires_Transient_Scope
(Typ
) and then not Inside_Init_Proc
then
4236 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
4239 -- If the aggregate is nonlimited, create a temporary. If it is limited
4240 -- and context is an assignment, this is a subaggregate for an enclosing
4241 -- aggregate being expanded. It must be built in place, so use target of
4242 -- the current assignment.
4244 if Is_Limited_Type
(Typ
)
4245 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
4247 Target_Expr
:= New_Copy_Tree
(Name
(Parent
(N
)));
4248 Insert_Actions
(Parent
(N
),
4249 Build_Record_Aggr_Code
(N
, Typ
, Target_Expr
));
4250 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
4253 Temp
:= Make_Temporary
(Loc
, 'A', N
);
4255 -- If the type inherits unknown discriminants, use the view with
4256 -- known discriminants if available.
4258 if Has_Unknown_Discriminants
(Typ
)
4259 and then Present
(Underlying_Record_View
(Typ
))
4261 T
:= Underlying_Record_View
(Typ
);
4267 Make_Object_Declaration
(Loc
,
4268 Defining_Identifier
=> Temp
,
4269 Object_Definition
=> New_Occurrence_Of
(T
, Loc
));
4271 Set_No_Initialization
(Instr
);
4272 Insert_Action
(N
, Instr
);
4273 Initialize_Discriminants
(Instr
, T
);
4275 Target_Expr
:= New_Occurrence_Of
(Temp
, Loc
);
4276 Aggr_Code
:= Build_Record_Aggr_Code
(N
, T
, Target_Expr
);
4278 -- Save the last assignment statement associated with the aggregate
4279 -- when building a controlled object. This reference is utilized by
4280 -- the finalization machinery when marking an object as successfully
4283 if Needs_Finalization
(T
) then
4284 Set_Last_Aggregate_Assignment
(Temp
, Last
(Aggr_Code
));
4287 Insert_Actions
(N
, Aggr_Code
);
4288 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4289 Analyze_And_Resolve
(N
, T
);
4291 end Convert_To_Assignments
;
4293 ---------------------------
4294 -- Convert_To_Positional --
4295 ---------------------------
4297 procedure Convert_To_Positional
4299 Max_Others_Replicate
: Nat
:= 5;
4300 Handle_Bit_Packed
: Boolean := False)
4302 Typ
: constant Entity_Id
:= Etype
(N
);
4304 Static_Components
: Boolean := True;
4306 procedure Check_Static_Components
;
4307 -- Check whether all components of the aggregate are compile-time known
4308 -- values, and can be passed as is to the back-end without further
4310 -- An Iterated_Component_Association is treated as nonstatic, but there
4311 -- are possibilities for optimization here.
4316 Ixb
: Node_Id
) return Boolean;
4317 -- Convert the aggregate into a purely positional form if possible. On
4318 -- entry the bounds of all dimensions are known to be static, and the
4319 -- total number of components is safe enough to expand.
4321 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean;
4322 -- Return True iff the array N is flat (which is not trivial in the case
4323 -- of multidimensional aggregates).
4325 -----------------------------
4326 -- Check_Static_Components --
4327 -----------------------------
4329 -- Could use some comments in this body ???
4331 procedure Check_Static_Components
is
4335 Static_Components
:= True;
4337 if Nkind
(N
) = N_String_Literal
then
4340 elsif Present
(Expressions
(N
)) then
4341 Expr
:= First
(Expressions
(N
));
4342 while Present
(Expr
) loop
4343 if Nkind
(Expr
) /= N_Aggregate
4344 or else not Compile_Time_Known_Aggregate
(Expr
)
4345 or else Expansion_Delayed
(Expr
)
4347 Static_Components
:= False;
4355 if Nkind
(N
) = N_Aggregate
4356 and then Present
(Component_Associations
(N
))
4358 Expr
:= First
(Component_Associations
(N
));
4359 while Present
(Expr
) loop
4360 if Nkind_In
(Expression
(Expr
), N_Integer_Literal
,
4365 elsif Is_Entity_Name
(Expression
(Expr
))
4366 and then Present
(Entity
(Expression
(Expr
)))
4367 and then Ekind
(Entity
(Expression
(Expr
))) =
4368 E_Enumeration_Literal
4372 elsif Nkind
(Expression
(Expr
)) /= N_Aggregate
4373 or else not Compile_Time_Known_Aggregate
(Expression
(Expr
))
4374 or else Expansion_Delayed
(Expression
(Expr
))
4375 or else Nkind
(Expr
) = N_Iterated_Component_Association
4377 Static_Components
:= False;
4384 end Check_Static_Components
;
4393 Ixb
: Node_Id
) return Boolean
4395 Loc
: constant Source_Ptr
:= Sloc
(N
);
4396 Blo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ixb
));
4397 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ix
));
4398 Hi
: constant Node_Id
:= Type_High_Bound
(Etype
(Ix
));
4402 Others_Present
: Boolean := False;
4405 if Nkind
(Original_Node
(N
)) = N_String_Literal
then
4409 if not Compile_Time_Known_Value
(Lo
)
4410 or else not Compile_Time_Known_Value
(Hi
)
4415 Lov
:= Expr_Value
(Lo
);
4416 Hiv
:= Expr_Value
(Hi
);
4418 -- Check if there is an others choice
4420 if Present
(Component_Associations
(N
)) then
4426 Assoc
:= First
(Component_Associations
(N
));
4427 while Present
(Assoc
) loop
4429 -- If this is a box association, flattening is in general
4430 -- not possible because at this point we cannot tell if the
4431 -- default is static or even exists.
4433 if Box_Present
(Assoc
) then
4436 elsif Nkind
(Assoc
) = N_Iterated_Component_Association
then
4440 Choice
:= First
(Choice_List
(Assoc
));
4442 while Present
(Choice
) loop
4443 if Nkind
(Choice
) = N_Others_Choice
then
4444 Others_Present
:= True;
4455 -- If the low bound is not known at compile time and others is not
4456 -- present we can proceed since the bounds can be obtained from the
4460 or else (not Compile_Time_Known_Value
(Blo
) and then Others_Present
)
4465 -- Determine if set of alternatives is suitable for conversion and
4466 -- build an array containing the values in sequence.
4469 Vals
: array (UI_To_Int
(Lov
) .. UI_To_Int
(Hiv
))
4470 of Node_Id
:= (others => Empty
);
4471 -- The values in the aggregate sorted appropriately
4474 -- Same data as Vals in list form
4477 -- Used to validate Max_Others_Replicate limit
4480 Num
: Int
:= UI_To_Int
(Lov
);
4486 if Present
(Expressions
(N
)) then
4487 Elmt
:= First
(Expressions
(N
));
4488 while Present
(Elmt
) loop
4489 if Nkind
(Elmt
) = N_Aggregate
4490 and then Present
(Next_Index
(Ix
))
4492 not Flatten
(Elmt
, Next_Index
(Ix
), Next_Index
(Ixb
))
4497 Vals
(Num
) := Relocate_Node
(Elmt
);
4504 if No
(Component_Associations
(N
)) then
4508 Elmt
:= First
(Component_Associations
(N
));
4510 if Nkind
(Expression
(Elmt
)) = N_Aggregate
then
4511 if Present
(Next_Index
(Ix
))
4514 (Expression
(Elmt
), Next_Index
(Ix
), Next_Index
(Ixb
))
4520 Component_Loop
: while Present
(Elmt
) loop
4521 Choice
:= First
(Choice_List
(Elmt
));
4522 Choice_Loop
: while Present
(Choice
) loop
4524 -- If we have an others choice, fill in the missing elements
4525 -- subject to the limit established by Max_Others_Replicate.
4527 if Nkind
(Choice
) = N_Others_Choice
then
4530 for J
in Vals
'Range loop
4531 if No
(Vals
(J
)) then
4532 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
4533 Rep_Count
:= Rep_Count
+ 1;
4535 -- Check for maximum others replication. Note that
4536 -- we skip this test if either of the restrictions
4537 -- No_Elaboration_Code or No_Implicit_Loops is
4538 -- active, if this is a preelaborable unit or
4539 -- a predefined unit, or if the unit must be
4540 -- placed in data memory. This also ensures that
4541 -- predefined units get the same level of constant
4542 -- folding in Ada 95 and Ada 2005, where their
4543 -- categorization has changed.
4546 P
: constant Entity_Id
:=
4547 Cunit_Entity
(Current_Sem_Unit
);
4550 -- Check if duplication OK and if so continue
4553 if Restriction_Active
(No_Elaboration_Code
)
4554 or else Restriction_Active
(No_Implicit_Loops
)
4556 (Ekind
(Current_Scope
) = E_Package
4557 and then Static_Elaboration_Desired
4559 or else Is_Preelaborated
(P
)
4560 or else (Ekind
(P
) = E_Package_Body
4562 Is_Preelaborated
(Spec_Entity
(P
)))
4564 Is_Predefined_Unit
(Get_Source_Unit
(P
))
4568 -- If duplication not OK, then we return False
4569 -- if the replication count is too high
4571 elsif Rep_Count
> Max_Others_Replicate
then
4574 -- Continue on if duplication not OK, but the
4575 -- replication count is not excessive.
4585 and then Warn_On_Redundant_Constructs
4587 Error_Msg_N
("there are no others?r?", Elmt
);
4590 exit Component_Loop
;
4592 -- Case of a subtype mark, identifier or expanded name
4594 elsif Is_Entity_Name
(Choice
)
4595 and then Is_Type
(Entity
(Choice
))
4597 Lo
:= Type_Low_Bound
(Etype
(Choice
));
4598 Hi
:= Type_High_Bound
(Etype
(Choice
));
4600 -- Case of subtype indication
4602 elsif Nkind
(Choice
) = N_Subtype_Indication
then
4603 Lo
:= Low_Bound
(Range_Expression
(Constraint
(Choice
)));
4604 Hi
:= High_Bound
(Range_Expression
(Constraint
(Choice
)));
4608 elsif Nkind
(Choice
) = N_Range
then
4609 Lo
:= Low_Bound
(Choice
);
4610 Hi
:= High_Bound
(Choice
);
4612 -- Normal subexpression case
4614 else pragma Assert
(Nkind
(Choice
) in N_Subexpr
);
4615 if not Compile_Time_Known_Value
(Choice
) then
4619 Choice_Index
:= UI_To_Int
(Expr_Value
(Choice
));
4621 if Choice_Index
in Vals
'Range then
4622 Vals
(Choice_Index
) :=
4623 New_Copy_Tree
(Expression
(Elmt
));
4626 -- Choice is statically out-of-range, will be
4627 -- rewritten to raise Constraint_Error.
4635 -- Range cases merge with Lo,Hi set
4637 if not Compile_Time_Known_Value
(Lo
)
4639 not Compile_Time_Known_Value
(Hi
)
4644 for J
in UI_To_Int
(Expr_Value
(Lo
)) ..
4645 UI_To_Int
(Expr_Value
(Hi
))
4647 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
4653 end loop Choice_Loop
;
4656 end loop Component_Loop
;
4658 -- If we get here the conversion is possible
4661 for J
in Vals
'Range loop
4662 Append
(Vals
(J
), Vlist
);
4665 Rewrite
(N
, Make_Aggregate
(Loc
, Expressions
=> Vlist
));
4666 Set_Aggregate_Bounds
(N
, Aggregate_Bounds
(Original_Node
(N
)));
4675 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean is
4682 elsif Nkind
(N
) = N_Aggregate
then
4683 if Present
(Component_Associations
(N
)) then
4687 Elmt
:= First
(Expressions
(N
));
4688 while Present
(Elmt
) loop
4689 if not Is_Flat
(Elmt
, Dims
- 1) then
4703 -- Start of processing for Convert_To_Positional
4706 -- Only convert to positional when generating C in case of an
4707 -- object declaration, this is the only case where aggregates are
4710 if Modify_Tree_For_C
and then not In_Object_Declaration
(N
) then
4714 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4715 -- components because in this case will need to call the corresponding
4718 if Has_Default_Init_Comps
(N
) then
4722 if Is_Flat
(N
, Number_Dimensions
(Typ
)) then
4726 if Is_Bit_Packed_Array
(Typ
) and then not Handle_Bit_Packed
then
4730 -- Do not convert to positional if controlled components are involved
4731 -- since these require special processing
4733 if Has_Controlled_Component
(Typ
) then
4737 Check_Static_Components
;
4739 -- If the size is known, or all the components are static, try to
4740 -- build a fully positional aggregate.
4742 -- The size of the type may not be known for an aggregate with
4743 -- discriminated array components, but if the components are static
4744 -- it is still possible to verify statically that the length is
4745 -- compatible with the upper bound of the type, and therefore it is
4746 -- worth flattening such aggregates as well.
4748 -- For now the back-end expands these aggregates into individual
4749 -- assignments to the target anyway, but it is conceivable that
4750 -- it will eventually be able to treat such aggregates statically???
4752 if Aggr_Size_OK
(N
, Typ
)
4753 and then Flatten
(N
, First_Index
(Typ
), First_Index
(Base_Type
(Typ
)))
4755 if Static_Components
then
4756 Set_Compile_Time_Known_Aggregate
(N
);
4757 Set_Expansion_Delayed
(N
, False);
4760 Analyze_And_Resolve
(N
, Typ
);
4763 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4764 -- that will still require initialization code.
4766 if (Ekind
(Current_Scope
) = E_Package
4767 and then Static_Elaboration_Desired
(Current_Scope
))
4768 and then Nkind
(Parent
(N
)) = N_Object_Declaration
4774 if Nkind
(N
) = N_Aggregate
and then Present
(Expressions
(N
)) then
4775 Expr
:= First
(Expressions
(N
));
4776 while Present
(Expr
) loop
4777 if Nkind_In
(Expr
, N_Integer_Literal
, N_Real_Literal
)
4779 (Is_Entity_Name
(Expr
)
4780 and then Ekind
(Entity
(Expr
)) = E_Enumeration_Literal
)
4786 ("non-static object requires elaboration code??", N
);
4793 if Present
(Component_Associations
(N
)) then
4794 Error_Msg_N
("object requires elaboration code??", N
);
4799 end Convert_To_Positional
;
4801 ----------------------------
4802 -- Expand_Array_Aggregate --
4803 ----------------------------
4805 -- Array aggregate expansion proceeds as follows:
4807 -- 1. If requested we generate code to perform all the array aggregate
4808 -- bound checks, specifically
4810 -- (a) Check that the index range defined by aggregate bounds is
4811 -- compatible with corresponding index subtype.
4813 -- (b) If an others choice is present check that no aggregate
4814 -- index is outside the bounds of the index constraint.
4816 -- (c) For multidimensional arrays make sure that all subaggregates
4817 -- corresponding to the same dimension have the same bounds.
4819 -- 2. Check for packed array aggregate which can be converted to a
4820 -- constant so that the aggregate disappears completely.
4822 -- 3. Check case of nested aggregate. Generally nested aggregates are
4823 -- handled during the processing of the parent aggregate.
4825 -- 4. Check if the aggregate can be statically processed. If this is the
4826 -- case pass it as is to Gigi. Note that a necessary condition for
4827 -- static processing is that the aggregate be fully positional.
4829 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4830 -- a temporary) then mark the aggregate as such and return. Otherwise
4831 -- create a new temporary and generate the appropriate initialization
4834 procedure Expand_Array_Aggregate
(N
: Node_Id
) is
4835 Loc
: constant Source_Ptr
:= Sloc
(N
);
4837 Typ
: constant Entity_Id
:= Etype
(N
);
4838 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
4839 -- Typ is the correct constrained array subtype of the aggregate
4840 -- Ctyp is the corresponding component type.
4842 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
4843 -- Number of aggregate index dimensions
4845 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
;
4846 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
;
4847 -- Low and High bounds of the constraint for each aggregate index
4849 Aggr_Index_Typ
: array (1 .. Aggr_Dimension
) of Entity_Id
;
4850 -- The type of each index
4852 In_Place_Assign_OK_For_Declaration
: Boolean := False;
4853 -- True if we are to generate an in place assignment for a declaration
4855 Maybe_In_Place_OK
: Boolean;
4856 -- If the type is neither controlled nor packed and the aggregate
4857 -- is the expression in an assignment, assignment in place may be
4858 -- possible, provided other conditions are met on the LHS.
4860 Others_Present
: array (1 .. Aggr_Dimension
) of Boolean :=
4862 -- If Others_Present (J) is True, then there is an others choice in one
4863 -- of the subaggregates of N at dimension J.
4865 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean;
4866 -- Returns true if an aggregate assignment can be done by the back end
4868 procedure Build_Constrained_Type
(Positional
: Boolean);
4869 -- If the subtype is not static or unconstrained, build a constrained
4870 -- type using the computable sizes of the aggregate and its sub-
4873 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
);
4874 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4877 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4878 -- Checks that in a multidimensional array aggregate all subaggregates
4879 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4880 -- an array subaggregate. Dim is the dimension corresponding to the
4883 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4884 -- Computes the values of array Others_Present. Sub_Aggr is the array
4885 -- subaggregate we start the computation from. Dim is the dimension
4886 -- corresponding to the subaggregate.
4888 function In_Place_Assign_OK
return Boolean;
4889 -- Simple predicate to determine whether an aggregate assignment can
4890 -- be done in place, because none of the new values can depend on the
4891 -- components of the target of the assignment.
4893 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4894 -- Checks that if an others choice is present in any subaggregate, no
4895 -- aggregate index is outside the bounds of the index constraint.
4896 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4897 -- to the subaggregate.
4899 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean;
4900 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4901 -- built directly into the target of the assignment it must be free
4904 ------------------------------------
4905 -- Aggr_Assignment_OK_For_Backend --
4906 ------------------------------------
4908 -- Backend processing by Gigi/gcc is possible only if all the following
4909 -- conditions are met:
4911 -- 1. N consists of a single OTHERS choice, possibly recursively
4913 -- 2. The array type has no null ranges (the purpose of this is to
4914 -- avoid a bogus warning for an out-of-range value).
4916 -- 3. The array type has no atomic components
4918 -- 4. The component type is elementary
4920 -- 5. The component size is a multiple of Storage_Unit
4922 -- 6. The component size is Storage_Unit or the value is of the form
4923 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4924 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4925 -- the 8-bit value M, concatenated together.
4927 -- The ultimate goal is to generate a call to a fast memset routine
4928 -- specifically optimized for the target.
4930 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean is
4942 -- Recurse as far as possible to find the innermost component type
4946 while Is_Array_Type
(Ctyp
) loop
4947 if Nkind
(Expr
) /= N_Aggregate
4948 or else not Is_Others_Aggregate
(Expr
)
4953 Index
:= First_Index
(Ctyp
);
4954 while Present
(Index
) loop
4955 Get_Index_Bounds
(Index
, Low
, High
);
4957 if Is_Null_Range
(Low
, High
) then
4964 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
4966 for J
in 1 .. Number_Dimensions
(Ctyp
) - 1 loop
4967 if Nkind
(Expr
) /= N_Aggregate
4968 or else not Is_Others_Aggregate
(Expr
)
4973 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
4976 if Has_Atomic_Components
(Ctyp
) then
4980 Csiz
:= Component_Size
(Ctyp
);
4981 Ctyp
:= Component_Type
(Ctyp
);
4983 if Is_Atomic_Or_VFA
(Ctyp
) then
4988 -- An Iterated_Component_Association involves a loop (in most cases)
4989 -- and is never static.
4991 if Nkind
(Parent
(Expr
)) = N_Iterated_Component_Association
then
4995 -- Access types need to be dealt with specially
4997 if Is_Access_Type
(Ctyp
) then
4999 -- Component_Size is not set by Layout_Type if the component
5000 -- type is an access type ???
5002 Csiz
:= Esize
(Ctyp
);
5004 -- Fat pointers are rejected as they are not really elementary
5007 if Csiz
/= System_Address_Size
then
5011 -- The supported expressions are NULL and constants, others are
5012 -- rejected upfront to avoid being analyzed below, which can be
5013 -- problematic for some of them, for example allocators.
5015 if Nkind
(Expr
) /= N_Null
and then not Is_Entity_Name
(Expr
) then
5019 -- Scalar types are OK if their size is a multiple of Storage_Unit
5021 elsif Is_Scalar_Type
(Ctyp
) then
5022 if Csiz
mod System_Storage_Unit
/= 0 then
5026 -- Composite types are rejected
5032 -- The expression needs to be analyzed if True is returned
5034 Analyze_And_Resolve
(Expr
, Ctyp
);
5036 -- Strip away any conversions from the expression as they simply
5037 -- qualify the real expression.
5039 while Nkind_In
(Expr
, N_Unchecked_Type_Conversion
,
5042 Expr
:= Expression
(Expr
);
5045 Nunits
:= UI_To_Int
(Csiz
) / System_Storage_Unit
;
5051 if not Compile_Time_Known_Value
(Expr
) then
5055 -- The only supported value for floating point is 0.0
5057 if Is_Floating_Point_Type
(Ctyp
) then
5058 return Expr_Value_R
(Expr
) = Ureal_0
;
5061 -- For other types, we can look into the value as an integer
5063 Value
:= Expr_Value
(Expr
);
5065 if Has_Biased_Representation
(Ctyp
) then
5066 Value
:= Value
- Expr_Value
(Type_Low_Bound
(Ctyp
));
5069 -- Values 0 and -1 immediately satisfy the last check
5071 if Value
= Uint_0
or else Value
= Uint_Minus_1
then
5075 -- We need to work with an unsigned value
5078 Value
:= Value
+ 2**(System_Storage_Unit
* Nunits
);
5081 Remainder
:= Value
rem 2**System_Storage_Unit
;
5083 for J
in 1 .. Nunits
- 1 loop
5084 Value
:= Value
/ 2**System_Storage_Unit
;
5086 if Value
rem 2**System_Storage_Unit
/= Remainder
then
5092 end Aggr_Assignment_OK_For_Backend
;
5094 ----------------------------
5095 -- Build_Constrained_Type --
5096 ----------------------------
5098 procedure Build_Constrained_Type
(Positional
: Boolean) is
5099 Loc
: constant Source_Ptr
:= Sloc
(N
);
5100 Agg_Type
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
5103 Typ
: constant Entity_Id
:= Etype
(N
);
5104 Indexes
: constant List_Id
:= New_List
;
5109 -- If the aggregate is purely positional, all its subaggregates
5110 -- have the same size. We collect the dimensions from the first
5111 -- subaggregate at each level.
5116 for D
in 1 .. Number_Dimensions
(Typ
) loop
5117 Sub_Agg
:= First
(Expressions
(Sub_Agg
));
5121 while Present
(Comp
) loop
5128 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
5129 High_Bound
=> Make_Integer_Literal
(Loc
, Num
)));
5133 -- We know the aggregate type is unconstrained and the aggregate
5134 -- is not processable by the back end, therefore not necessarily
5135 -- positional. Retrieve each dimension bounds (computed earlier).
5137 for D
in 1 .. Number_Dimensions
(Typ
) loop
5140 Low_Bound
=> Aggr_Low
(D
),
5141 High_Bound
=> Aggr_High
(D
)));
5146 Make_Full_Type_Declaration
(Loc
,
5147 Defining_Identifier
=> Agg_Type
,
5149 Make_Constrained_Array_Definition
(Loc
,
5150 Discrete_Subtype_Definitions
=> Indexes
,
5151 Component_Definition
=>
5152 Make_Component_Definition
(Loc
,
5153 Aliased_Present
=> False,
5154 Subtype_Indication
=>
5155 New_Occurrence_Of
(Component_Type
(Typ
), Loc
))));
5157 Insert_Action
(N
, Decl
);
5159 Set_Etype
(N
, Agg_Type
);
5160 Set_Is_Itype
(Agg_Type
);
5161 Freeze_Itype
(Agg_Type
, N
);
5162 end Build_Constrained_Type
;
5168 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
) is
5175 Cond
: Node_Id
:= Empty
;
5178 Get_Index_Bounds
(Aggr_Bounds
, Aggr_Lo
, Aggr_Hi
);
5179 Get_Index_Bounds
(Index_Bounds
, Ind_Lo
, Ind_Hi
);
5181 -- Generate the following test:
5183 -- [constraint_error when
5184 -- Aggr_Lo <= Aggr_Hi and then
5185 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5187 -- As an optimization try to see if some tests are trivially vacuous
5188 -- because we are comparing an expression against itself.
5190 if Aggr_Lo
= Ind_Lo
and then Aggr_Hi
= Ind_Hi
then
5193 elsif Aggr_Hi
= Ind_Hi
then
5196 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5197 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
));
5199 elsif Aggr_Lo
= Ind_Lo
then
5202 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
5203 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Hi
));
5210 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5211 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
)),
5215 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
5216 Right_Opnd
=> Duplicate_Subexpr
(Ind_Hi
)));
5219 if Present
(Cond
) then
5224 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5225 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
)),
5227 Right_Opnd
=> Cond
);
5229 Set_Analyzed
(Left_Opnd
(Left_Opnd
(Cond
)), False);
5230 Set_Analyzed
(Right_Opnd
(Left_Opnd
(Cond
)), False);
5232 Make_Raise_Constraint_Error
(Loc
,
5234 Reason
=> CE_Range_Check_Failed
));
5238 ----------------------------
5239 -- Check_Same_Aggr_Bounds --
5240 ----------------------------
5242 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5243 Sub_Lo
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(Sub_Aggr
));
5244 Sub_Hi
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(Sub_Aggr
));
5245 -- The bounds of this specific subaggregate
5247 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
5248 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
5249 -- The bounds of the aggregate for this dimension
5251 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
5252 -- The index type for this dimension.xxx
5254 Cond
: Node_Id
:= Empty
;
5259 -- If index checks are on generate the test
5261 -- [constraint_error when
5262 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5264 -- As an optimization try to see if some tests are trivially vacuos
5265 -- because we are comparing an expression against itself. Also for
5266 -- the first dimension the test is trivially vacuous because there
5267 -- is just one aggregate for dimension 1.
5269 if Index_Checks_Suppressed
(Ind_Typ
) then
5272 elsif Dim
= 1 or else (Aggr_Lo
= Sub_Lo
and then Aggr_Hi
= Sub_Hi
)
5276 elsif Aggr_Hi
= Sub_Hi
then
5279 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5280 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
));
5282 elsif Aggr_Lo
= Sub_Lo
then
5285 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
5286 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Hi
));
5293 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5294 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
)),
5298 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
5299 Right_Opnd
=> Duplicate_Subexpr
(Sub_Hi
)));
5302 if Present
(Cond
) then
5304 Make_Raise_Constraint_Error
(Loc
,
5306 Reason
=> CE_Length_Check_Failed
));
5309 -- Now look inside the subaggregate to see if there is more work
5311 if Dim
< Aggr_Dimension
then
5313 -- Process positional components
5315 if Present
(Expressions
(Sub_Aggr
)) then
5316 Expr
:= First
(Expressions
(Sub_Aggr
));
5317 while Present
(Expr
) loop
5318 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
5323 -- Process component associations
5325 if Present
(Component_Associations
(Sub_Aggr
)) then
5326 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5327 while Present
(Assoc
) loop
5328 Expr
:= Expression
(Assoc
);
5329 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
5334 end Check_Same_Aggr_Bounds
;
5336 ----------------------------
5337 -- Compute_Others_Present --
5338 ----------------------------
5340 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5345 if Present
(Component_Associations
(Sub_Aggr
)) then
5346 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
5348 if Nkind
(First
(Choice_List
(Assoc
))) = N_Others_Choice
then
5349 Others_Present
(Dim
) := True;
5353 -- Now look inside the subaggregate to see if there is more work
5355 if Dim
< Aggr_Dimension
then
5357 -- Process positional components
5359 if Present
(Expressions
(Sub_Aggr
)) then
5360 Expr
:= First
(Expressions
(Sub_Aggr
));
5361 while Present
(Expr
) loop
5362 Compute_Others_Present
(Expr
, Dim
+ 1);
5367 -- Process component associations
5369 if Present
(Component_Associations
(Sub_Aggr
)) then
5370 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5371 while Present
(Assoc
) loop
5372 Expr
:= Expression
(Assoc
);
5373 Compute_Others_Present
(Expr
, Dim
+ 1);
5378 end Compute_Others_Present
;
5380 ------------------------
5381 -- In_Place_Assign_OK --
5382 ------------------------
5384 function In_Place_Assign_OK
return Boolean is
5392 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean;
5393 -- Check recursively that each component of a (sub)aggregate does not
5394 -- depend on the variable being assigned to.
5396 function Safe_Component
(Expr
: Node_Id
) return Boolean;
5397 -- Verify that an expression cannot depend on the variable being
5398 -- assigned to. Room for improvement here (but less than before).
5400 --------------------
5401 -- Safe_Aggregate --
5402 --------------------
5404 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean is
5408 if Nkind
(Parent
(Aggr
)) = N_Iterated_Component_Association
then
5412 if Present
(Expressions
(Aggr
)) then
5413 Expr
:= First
(Expressions
(Aggr
));
5414 while Present
(Expr
) loop
5415 if Nkind
(Expr
) = N_Aggregate
then
5416 if not Safe_Aggregate
(Expr
) then
5420 elsif not Safe_Component
(Expr
) then
5428 if Present
(Component_Associations
(Aggr
)) then
5429 Expr
:= First
(Component_Associations
(Aggr
));
5430 while Present
(Expr
) loop
5431 if Nkind
(Expression
(Expr
)) = N_Aggregate
then
5432 if not Safe_Aggregate
(Expression
(Expr
)) then
5436 -- If association has a box, no way to determine yet
5437 -- whether default can be assigned in place.
5439 elsif Box_Present
(Expr
) then
5442 elsif not Safe_Component
(Expression
(Expr
)) then
5453 --------------------
5454 -- Safe_Component --
5455 --------------------
5457 function Safe_Component
(Expr
: Node_Id
) return Boolean is
5458 Comp
: Node_Id
:= Expr
;
5460 function Check_Component
(Comp
: Node_Id
) return Boolean;
5461 -- Do the recursive traversal, after copy
5463 ---------------------
5464 -- Check_Component --
5465 ---------------------
5467 function Check_Component
(Comp
: Node_Id
) return Boolean is
5469 if Is_Overloaded
(Comp
) then
5473 return Compile_Time_Known_Value
(Comp
)
5475 or else (Is_Entity_Name
(Comp
)
5476 and then Present
(Entity
(Comp
))
5477 and then No
(Renamed_Object
(Entity
(Comp
))))
5479 or else (Nkind
(Comp
) = N_Attribute_Reference
5480 and then Check_Component
(Prefix
(Comp
)))
5482 or else (Nkind
(Comp
) in N_Binary_Op
5483 and then Check_Component
(Left_Opnd
(Comp
))
5484 and then Check_Component
(Right_Opnd
(Comp
)))
5486 or else (Nkind
(Comp
) in N_Unary_Op
5487 and then Check_Component
(Right_Opnd
(Comp
)))
5489 or else (Nkind
(Comp
) = N_Selected_Component
5490 and then Check_Component
(Prefix
(Comp
)))
5492 or else (Nkind
(Comp
) = N_Unchecked_Type_Conversion
5493 and then Check_Component
(Expression
(Comp
)));
5494 end Check_Component
;
5496 -- Start of processing for Safe_Component
5499 -- If the component appears in an association that may correspond
5500 -- to more than one element, it is not analyzed before expansion
5501 -- into assignments, to avoid side effects. We analyze, but do not
5502 -- resolve the copy, to obtain sufficient entity information for
5503 -- the checks that follow. If component is overloaded we assume
5504 -- an unsafe function call.
5506 if not Analyzed
(Comp
) then
5507 if Is_Overloaded
(Expr
) then
5510 elsif Nkind
(Expr
) = N_Aggregate
5511 and then not Is_Others_Aggregate
(Expr
)
5515 elsif Nkind
(Expr
) = N_Allocator
then
5517 -- For now, too complex to analyze
5521 elsif Nkind
(Parent
(Expr
)) =
5522 N_Iterated_Component_Association
5524 -- Ditto for iterated component associations, which in
5525 -- general require an enclosing loop and involve nonstatic
5531 Comp
:= New_Copy_Tree
(Expr
);
5532 Set_Parent
(Comp
, Parent
(Expr
));
5536 if Nkind
(Comp
) = N_Aggregate
then
5537 return Safe_Aggregate
(Comp
);
5539 return Check_Component
(Comp
);
5543 -- Start of processing for In_Place_Assign_OK
5546 if Present
(Component_Associations
(N
)) then
5548 -- On assignment, sliding can take place, so we cannot do the
5549 -- assignment in place unless the bounds of the aggregate are
5550 -- statically equal to those of the target.
5552 -- If the aggregate is given by an others choice, the bounds are
5553 -- derived from the left-hand side, and the assignment is safe if
5554 -- the expression is.
5556 if Is_Others_Aggregate
(N
) then
5559 (Expression
(First
(Component_Associations
(N
))));
5562 Aggr_In
:= First_Index
(Etype
(N
));
5564 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
5565 Obj_In
:= First_Index
(Etype
(Name
(Parent
(N
))));
5568 -- Context is an allocator. Check bounds of aggregate against
5569 -- given type in qualified expression.
5571 pragma Assert
(Nkind
(Parent
(Parent
(N
))) = N_Allocator
);
5573 First_Index
(Etype
(Entity
(Subtype_Mark
(Parent
(N
)))));
5576 while Present
(Aggr_In
) loop
5577 Get_Index_Bounds
(Aggr_In
, Aggr_Lo
, Aggr_Hi
);
5578 Get_Index_Bounds
(Obj_In
, Obj_Lo
, Obj_Hi
);
5580 if not Compile_Time_Known_Value
(Aggr_Lo
)
5581 or else not Compile_Time_Known_Value
(Obj_Lo
)
5582 or else not Compile_Time_Known_Value
(Obj_Hi
)
5583 or else Expr_Value
(Aggr_Lo
) /= Expr_Value
(Obj_Lo
)
5587 -- For an assignment statement we require static matching of
5588 -- bounds. Ditto for an allocator whose qualified expression
5589 -- is a constrained type. If the expression in the allocator
5590 -- is an unconstrained array, we accept an upper bound that
5591 -- is not static, to allow for nonstatic expressions of the
5592 -- base type. Clearly there are further possibilities (with
5593 -- diminishing returns) for safely building arrays in place
5596 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
5597 or else Is_Constrained
(Etype
(Parent
(N
)))
5599 if not Compile_Time_Known_Value
(Aggr_Hi
)
5600 or else Expr_Value
(Aggr_Hi
) /= Expr_Value
(Obj_Hi
)
5606 Next_Index
(Aggr_In
);
5607 Next_Index
(Obj_In
);
5611 -- Now check the component values themselves
5613 return Safe_Aggregate
(N
);
5614 end In_Place_Assign_OK
;
5620 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5621 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
5622 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
5623 -- The bounds of the aggregate for this dimension
5625 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
5626 -- The index type for this dimension
5628 Need_To_Check
: Boolean := False;
5630 Choices_Lo
: Node_Id
:= Empty
;
5631 Choices_Hi
: Node_Id
:= Empty
;
5632 -- The lowest and highest discrete choices for a named subaggregate
5634 Nb_Choices
: Int
:= -1;
5635 -- The number of discrete non-others choices in this subaggregate
5637 Nb_Elements
: Uint
:= Uint_0
;
5638 -- The number of elements in a positional aggregate
5640 Cond
: Node_Id
:= Empty
;
5647 -- Check if we have an others choice. If we do make sure that this
5648 -- subaggregate contains at least one element in addition to the
5651 if Range_Checks_Suppressed
(Ind_Typ
) then
5652 Need_To_Check
:= False;
5654 elsif Present
(Expressions
(Sub_Aggr
))
5655 and then Present
(Component_Associations
(Sub_Aggr
))
5657 Need_To_Check
:= True;
5659 elsif Present
(Component_Associations
(Sub_Aggr
)) then
5660 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
5662 if Nkind
(First
(Choice_List
(Assoc
))) /= N_Others_Choice
then
5663 Need_To_Check
:= False;
5666 -- Count the number of discrete choices. Start with -1 because
5667 -- the others choice does not count.
5669 -- Is there some reason we do not use List_Length here ???
5672 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5673 while Present
(Assoc
) loop
5674 Choice
:= First
(Choice_List
(Assoc
));
5675 while Present
(Choice
) loop
5676 Nb_Choices
:= Nb_Choices
+ 1;
5683 -- If there is only an others choice nothing to do
5685 Need_To_Check
:= (Nb_Choices
> 0);
5689 Need_To_Check
:= False;
5692 -- If we are dealing with a positional subaggregate with an others
5693 -- choice then compute the number or positional elements.
5695 if Need_To_Check
and then Present
(Expressions
(Sub_Aggr
)) then
5696 Expr
:= First
(Expressions
(Sub_Aggr
));
5697 Nb_Elements
:= Uint_0
;
5698 while Present
(Expr
) loop
5699 Nb_Elements
:= Nb_Elements
+ 1;
5703 -- If the aggregate contains discrete choices and an others choice
5704 -- compute the smallest and largest discrete choice values.
5706 elsif Need_To_Check
then
5707 Compute_Choices_Lo_And_Choices_Hi
: declare
5709 Table
: Case_Table_Type
(1 .. Nb_Choices
);
5710 -- Used to sort all the different choice values
5717 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5718 while Present
(Assoc
) loop
5719 Choice
:= First
(Choice_List
(Assoc
));
5720 while Present
(Choice
) loop
5721 if Nkind
(Choice
) = N_Others_Choice
then
5725 Get_Index_Bounds
(Choice
, Low
, High
);
5726 Table
(J
).Choice_Lo
:= Low
;
5727 Table
(J
).Choice_Hi
:= High
;
5736 -- Sort the discrete choices
5738 Sort_Case_Table
(Table
);
5740 Choices_Lo
:= Table
(1).Choice_Lo
;
5741 Choices_Hi
:= Table
(Nb_Choices
).Choice_Hi
;
5742 end Compute_Choices_Lo_And_Choices_Hi
;
5745 -- If no others choice in this subaggregate, or the aggregate
5746 -- comprises only an others choice, nothing to do.
5748 if not Need_To_Check
then
5751 -- If we are dealing with an aggregate containing an others choice
5752 -- and positional components, we generate the following test:
5754 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5755 -- Ind_Typ'Pos (Aggr_Hi)
5757 -- raise Constraint_Error;
5760 elsif Nb_Elements
> Uint_0
then
5766 Make_Attribute_Reference
(Loc
,
5767 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
5768 Attribute_Name
=> Name_Pos
,
5771 (Duplicate_Subexpr_Move_Checks
(Aggr_Lo
))),
5772 Right_Opnd
=> Make_Integer_Literal
(Loc
, Nb_Elements
- 1)),
5775 Make_Attribute_Reference
(Loc
,
5776 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
5777 Attribute_Name
=> Name_Pos
,
5778 Expressions
=> New_List
(
5779 Duplicate_Subexpr_Move_Checks
(Aggr_Hi
))));
5781 -- If we are dealing with an aggregate containing an others choice
5782 -- and discrete choices we generate the following test:
5784 -- [constraint_error when
5785 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5792 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Choices_Lo
),
5793 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
)),
5797 Left_Opnd
=> Duplicate_Subexpr
(Choices_Hi
),
5798 Right_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
)));
5801 if Present
(Cond
) then
5803 Make_Raise_Constraint_Error
(Loc
,
5805 Reason
=> CE_Length_Check_Failed
));
5806 -- Questionable reason code, shouldn't that be a
5807 -- CE_Range_Check_Failed ???
5810 -- Now look inside the subaggregate to see if there is more work
5812 if Dim
< Aggr_Dimension
then
5814 -- Process positional components
5816 if Present
(Expressions
(Sub_Aggr
)) then
5817 Expr
:= First
(Expressions
(Sub_Aggr
));
5818 while Present
(Expr
) loop
5819 Others_Check
(Expr
, Dim
+ 1);
5824 -- Process component associations
5826 if Present
(Component_Associations
(Sub_Aggr
)) then
5827 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5828 while Present
(Assoc
) loop
5829 Expr
:= Expression
(Assoc
);
5830 Others_Check
(Expr
, Dim
+ 1);
5837 -------------------------
5838 -- Safe_Left_Hand_Side --
5839 -------------------------
5841 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean is
5842 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean;
5843 -- If the left-hand side includes an indexed component, check that
5844 -- the indexes are free of side effects.
5850 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean is
5852 if Is_Entity_Name
(Indx
) then
5855 elsif Nkind
(Indx
) = N_Integer_Literal
then
5858 elsif Nkind
(Indx
) = N_Function_Call
5859 and then Is_Entity_Name
(Name
(Indx
))
5860 and then Has_Pragma_Pure_Function
(Entity
(Name
(Indx
)))
5864 elsif Nkind
(Indx
) = N_Type_Conversion
5865 and then Is_Safe_Index
(Expression
(Indx
))
5874 -- Start of processing for Safe_Left_Hand_Side
5877 if Is_Entity_Name
(N
) then
5880 elsif Nkind_In
(N
, N_Explicit_Dereference
, N_Selected_Component
)
5881 and then Safe_Left_Hand_Side
(Prefix
(N
))
5885 elsif Nkind
(N
) = N_Indexed_Component
5886 and then Safe_Left_Hand_Side
(Prefix
(N
))
5887 and then Is_Safe_Index
(First
(Expressions
(N
)))
5891 elsif Nkind
(N
) = N_Unchecked_Type_Conversion
then
5892 return Safe_Left_Hand_Side
(Expression
(N
));
5897 end Safe_Left_Hand_Side
;
5902 -- Holds the temporary aggregate value
5905 -- Holds the declaration of Tmp
5907 Aggr_Code
: List_Id
;
5908 Parent_Node
: Node_Id
;
5909 Parent_Kind
: Node_Kind
;
5911 -- Start of processing for Expand_Array_Aggregate
5914 -- Do not touch the special aggregates of attributes used for Asm calls
5916 if Is_RTE
(Ctyp
, RE_Asm_Input_Operand
)
5917 or else Is_RTE
(Ctyp
, RE_Asm_Output_Operand
)
5921 -- Do not expand an aggregate for an array type which contains tasks if
5922 -- the aggregate is associated with an unexpanded return statement of a
5923 -- build-in-place function. The aggregate is expanded when the related
5924 -- return statement (rewritten into an extended return) is processed.
5925 -- This delay ensures that any temporaries and initialization code
5926 -- generated for the aggregate appear in the proper return block and
5927 -- use the correct _chain and _master.
5929 elsif Has_Task
(Base_Type
(Etype
(N
)))
5930 and then Nkind
(Parent
(N
)) = N_Simple_Return_Statement
5931 and then Is_Build_In_Place_Function
5932 (Return_Applies_To
(Return_Statement_Entity
(Parent
(N
))))
5936 -- Do not attempt expansion if error already detected. We may reach this
5937 -- point in spite of previous errors when compiling with -gnatq, to
5938 -- force all possible errors (this is the usual ACATS mode).
5940 elsif Error_Posted
(N
) then
5944 -- If the semantic analyzer has determined that aggregate N will raise
5945 -- Constraint_Error at run time, then the aggregate node has been
5946 -- replaced with an N_Raise_Constraint_Error node and we should
5949 pragma Assert
(not Raises_Constraint_Error
(N
));
5953 -- Check that the index range defined by aggregate bounds is
5954 -- compatible with corresponding index subtype.
5956 Index_Compatibility_Check
: declare
5957 Aggr_Index_Range
: Node_Id
:= First_Index
(Typ
);
5958 -- The current aggregate index range
5960 Index_Constraint
: Node_Id
:= First_Index
(Etype
(Typ
));
5961 -- The corresponding index constraint against which we have to
5962 -- check the above aggregate index range.
5965 Compute_Others_Present
(N
, 1);
5967 for J
in 1 .. Aggr_Dimension
loop
5968 -- There is no need to emit a check if an others choice is present
5969 -- for this array aggregate dimension since in this case one of
5970 -- N's subaggregates has taken its bounds from the context and
5971 -- these bounds must have been checked already. In addition all
5972 -- subaggregates corresponding to the same dimension must all have
5973 -- the same bounds (checked in (c) below).
5975 if not Range_Checks_Suppressed
(Etype
(Index_Constraint
))
5976 and then not Others_Present
(J
)
5978 -- We don't use Checks.Apply_Range_Check here because it emits
5979 -- a spurious check. Namely it checks that the range defined by
5980 -- the aggregate bounds is nonempty. But we know this already
5983 Check_Bounds
(Aggr_Index_Range
, Index_Constraint
);
5986 -- Save the low and high bounds of the aggregate index as well as
5987 -- the index type for later use in checks (b) and (c) below.
5989 Aggr_Low
(J
) := Low_Bound
(Aggr_Index_Range
);
5990 Aggr_High
(J
) := High_Bound
(Aggr_Index_Range
);
5992 Aggr_Index_Typ
(J
) := Etype
(Index_Constraint
);
5994 Next_Index
(Aggr_Index_Range
);
5995 Next_Index
(Index_Constraint
);
5997 end Index_Compatibility_Check
;
6001 -- If an others choice is present check that no aggregate index is
6002 -- outside the bounds of the index constraint.
6004 Others_Check
(N
, 1);
6008 -- For multidimensional arrays make sure that all subaggregates
6009 -- corresponding to the same dimension have the same bounds.
6011 if Aggr_Dimension
> 1 then
6012 Check_Same_Aggr_Bounds
(N
, 1);
6017 -- If we have a default component value, or simple initialization is
6018 -- required for the component type, then we replace <> in component
6019 -- associations by the required default value.
6022 Default_Val
: Node_Id
;
6026 if (Present
(Default_Aspect_Component_Value
(Typ
))
6027 or else Needs_Simple_Initialization
(Ctyp
))
6028 and then Present
(Component_Associations
(N
))
6030 Assoc
:= First
(Component_Associations
(N
));
6031 while Present
(Assoc
) loop
6032 if Nkind
(Assoc
) = N_Component_Association
6033 and then Box_Present
(Assoc
)
6035 Set_Box_Present
(Assoc
, False);
6037 if Present
(Default_Aspect_Component_Value
(Typ
)) then
6038 Default_Val
:= Default_Aspect_Component_Value
(Typ
);
6040 Default_Val
:= Get_Simple_Init_Val
(Ctyp
, N
);
6043 Set_Expression
(Assoc
, New_Copy_Tree
(Default_Val
));
6044 Analyze_And_Resolve
(Expression
(Assoc
), Ctyp
);
6054 -- Here we test for is packed array aggregate that we can handle at
6055 -- compile time. If so, return with transformation done. Note that we do
6056 -- this even if the aggregate is nested, because once we have done this
6057 -- processing, there is no more nested aggregate.
6059 if Packed_Array_Aggregate_Handled
(N
) then
6063 -- At this point we try to convert to positional form
6065 if Ekind
(Current_Scope
) = E_Package
6066 and then Static_Elaboration_Desired
(Current_Scope
)
6068 Convert_To_Positional
(N
, Max_Others_Replicate
=> 100);
6070 Convert_To_Positional
(N
);
6073 -- if the result is no longer an aggregate (e.g. it may be a string
6074 -- literal, or a temporary which has the needed value), then we are
6075 -- done, since there is no longer a nested aggregate.
6077 if Nkind
(N
) /= N_Aggregate
then
6080 -- We are also done if the result is an analyzed aggregate, indicating
6081 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6084 elsif Analyzed
(N
) and then N
/= Original_Node
(N
) then
6088 -- If all aggregate components are compile-time known and the aggregate
6089 -- has been flattened, nothing left to do. The same occurs if the
6090 -- aggregate is used to initialize the components of a statically
6091 -- allocated dispatch table.
6093 if Compile_Time_Known_Aggregate
(N
)
6094 or else Is_Static_Dispatch_Table_Aggregate
(N
)
6096 Set_Expansion_Delayed
(N
, False);
6100 -- Now see if back end processing is possible
6102 if Backend_Processing_Possible
(N
) then
6104 -- If the aggregate is static but the constraints are not, build
6105 -- a static subtype for the aggregate, so that Gigi can place it
6106 -- in static memory. Perform an unchecked_conversion to the non-
6107 -- static type imposed by the context.
6110 Itype
: constant Entity_Id
:= Etype
(N
);
6112 Needs_Type
: Boolean := False;
6115 Index
:= First_Index
(Itype
);
6116 while Present
(Index
) loop
6117 if not Is_OK_Static_Subtype
(Etype
(Index
)) then
6126 Build_Constrained_Type
(Positional
=> True);
6127 Rewrite
(N
, Unchecked_Convert_To
(Itype
, N
));
6137 -- Delay expansion for nested aggregates: it will be taken care of when
6138 -- the parent aggregate is expanded.
6140 Parent_Node
:= Parent
(N
);
6141 Parent_Kind
:= Nkind
(Parent_Node
);
6143 if Parent_Kind
= N_Qualified_Expression
then
6144 Parent_Node
:= Parent
(Parent_Node
);
6145 Parent_Kind
:= Nkind
(Parent_Node
);
6148 if Parent_Kind
= N_Aggregate
6149 or else Parent_Kind
= N_Extension_Aggregate
6150 or else Parent_Kind
= N_Component_Association
6151 or else (Parent_Kind
= N_Object_Declaration
6152 and then Needs_Finalization
(Typ
))
6153 or else (Parent_Kind
= N_Assignment_Statement
6154 and then Inside_Init_Proc
)
6156 if Static_Array_Aggregate
(N
)
6157 or else Compile_Time_Known_Aggregate
(N
)
6159 Set_Expansion_Delayed
(N
, False);
6162 Set_Expansion_Delayed
(N
);
6169 -- Look if in place aggregate expansion is possible
6171 -- For object declarations we build the aggregate in place, unless
6172 -- the array is bit-packed or the component is controlled.
6174 -- For assignments we do the assignment in place if all the component
6175 -- associations have compile-time known values. For other cases we
6176 -- create a temporary. The analysis for safety of on-line assignment
6177 -- is delicate, i.e. we don't know how to do it fully yet ???
6179 -- For allocators we assign to the designated object in place if the
6180 -- aggregate meets the same conditions as other in-place assignments.
6181 -- In this case the aggregate may not come from source but was created
6182 -- for default initialization, e.g. with Initialize_Scalars.
6184 if Requires_Transient_Scope
(Typ
) then
6185 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
6188 if Has_Default_Init_Comps
(N
) then
6189 Maybe_In_Place_OK
:= False;
6191 elsif Is_Bit_Packed_Array
(Typ
)
6192 or else Has_Controlled_Component
(Typ
)
6194 Maybe_In_Place_OK
:= False;
6197 Maybe_In_Place_OK
:=
6198 (Nkind
(Parent
(N
)) = N_Assignment_Statement
6199 and then In_Place_Assign_OK
)
6202 (Nkind
(Parent
(Parent
(N
))) = N_Allocator
6203 and then In_Place_Assign_OK
);
6206 -- If this is an array of tasks, it will be expanded into build-in-place
6207 -- assignments. Build an activation chain for the tasks now.
6209 if Has_Task
(Etype
(N
)) then
6210 Build_Activation_Chain_Entity
(N
);
6213 -- Perform in-place expansion of aggregate in an object declaration.
6214 -- Note: actions generated for the aggregate will be captured in an
6215 -- expression-with-actions statement so that they can be transferred
6216 -- to freeze actions later if there is an address clause for the
6217 -- object. (Note: we don't use a block statement because this would
6218 -- cause generated freeze nodes to be elaborated in the wrong scope).
6220 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6221 -- expected to appear in qualified form. In-place expansion eliminates
6222 -- the qualification and eventually violates this SPARK 05 restiction.
6224 -- Should document the rest of the guards ???
6226 if not Has_Default_Init_Comps
(N
)
6227 and then Comes_From_Source
(Parent_Node
)
6228 and then Parent_Kind
= N_Object_Declaration
6229 and then Present
(Expression
(Parent_Node
))
6231 Must_Slide
(Etype
(Defining_Identifier
(Parent_Node
)), Typ
)
6232 and then not Has_Controlled_Component
(Typ
)
6233 and then not Is_Bit_Packed_Array
(Typ
)
6234 and then not Restriction_Check_Required
(SPARK_05
)
6236 In_Place_Assign_OK_For_Declaration
:= True;
6237 Tmp
:= Defining_Identifier
(Parent_Node
);
6238 Set_No_Initialization
(Parent_Node
);
6239 Set_Expression
(Parent_Node
, Empty
);
6241 -- Set kind and type of the entity, for use in the analysis
6242 -- of the subsequent assignments. If the nominal type is not
6243 -- constrained, build a subtype from the known bounds of the
6244 -- aggregate. If the declaration has a subtype mark, use it,
6245 -- otherwise use the itype of the aggregate.
6247 Set_Ekind
(Tmp
, E_Variable
);
6249 if not Is_Constrained
(Typ
) then
6250 Build_Constrained_Type
(Positional
=> False);
6252 elsif Is_Entity_Name
(Object_Definition
(Parent_Node
))
6253 and then Is_Constrained
(Entity
(Object_Definition
(Parent_Node
)))
6255 Set_Etype
(Tmp
, Entity
(Object_Definition
(Parent_Node
)));
6258 Set_Size_Known_At_Compile_Time
(Typ
, False);
6259 Set_Etype
(Tmp
, Typ
);
6262 elsif Maybe_In_Place_OK
6263 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
6264 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
6266 Set_Expansion_Delayed
(N
);
6269 -- In the remaining cases the aggregate is the RHS of an assignment
6271 elsif Maybe_In_Place_OK
6272 and then Safe_Left_Hand_Side
(Name
(Parent
(N
)))
6274 Tmp
:= Name
(Parent
(N
));
6276 if Etype
(Tmp
) /= Etype
(N
) then
6277 Apply_Length_Check
(N
, Etype
(Tmp
));
6279 if Nkind
(N
) = N_Raise_Constraint_Error
then
6281 -- Static error, nothing further to expand
6287 -- If a slice assignment has an aggregate with a single others_choice,
6288 -- the assignment can be done in place even if bounds are not static,
6289 -- by converting it into a loop over the discrete range of the slice.
6291 elsif Maybe_In_Place_OK
6292 and then Nkind
(Name
(Parent
(N
))) = N_Slice
6293 and then Is_Others_Aggregate
(N
)
6295 Tmp
:= Name
(Parent
(N
));
6297 -- Set type of aggregate to be type of lhs in assignment, in order
6298 -- to suppress redundant length checks.
6300 Set_Etype
(N
, Etype
(Tmp
));
6304 -- In place aggregate expansion is not possible
6307 Maybe_In_Place_OK
:= False;
6308 Tmp
:= Make_Temporary
(Loc
, 'A', N
);
6310 Make_Object_Declaration
(Loc
,
6311 Defining_Identifier
=> Tmp
,
6312 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
6313 Set_No_Initialization
(Tmp_Decl
, True);
6315 -- If we are within a loop, the temporary will be pushed on the
6316 -- stack at each iteration. If the aggregate is the expression
6317 -- for an allocator, it will be immediately copied to the heap
6318 -- and can be reclaimed at once. We create a transient scope
6319 -- around the aggregate for this purpose.
6321 if Ekind
(Current_Scope
) = E_Loop
6322 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
6324 Establish_Transient_Scope
(N
, Manage_Sec_Stack
=> False);
6327 Insert_Action
(N
, Tmp_Decl
);
6330 -- Construct and insert the aggregate code. We can safely suppress index
6331 -- checks because this code is guaranteed not to raise CE on index
6332 -- checks. However we should *not* suppress all checks.
6338 if Nkind
(Tmp
) = N_Defining_Identifier
then
6339 Target
:= New_Occurrence_Of
(Tmp
, Loc
);
6342 if Has_Default_Init_Comps
(N
) then
6344 -- Ada 2005 (AI-287): This case has not been analyzed???
6346 raise Program_Error
;
6349 -- Name in assignment is explicit dereference
6351 Target
:= New_Copy
(Tmp
);
6354 -- If we are to generate an in place assignment for a declaration or
6355 -- an assignment statement, and the assignment can be done directly
6356 -- by the back end, then do not expand further.
6358 -- ??? We can also do that if in place expansion is not possible but
6359 -- then we could go into an infinite recursion.
6361 if (In_Place_Assign_OK_For_Declaration
or else Maybe_In_Place_OK
)
6362 and then not CodePeer_Mode
6363 and then not Modify_Tree_For_C
6364 and then not Possible_Bit_Aligned_Component
(Target
)
6365 and then not Is_Possibly_Unaligned_Slice
(Target
)
6366 and then Aggr_Assignment_OK_For_Backend
(N
)
6368 if Maybe_In_Place_OK
then
6374 Make_Assignment_Statement
(Loc
,
6376 Expression
=> New_Copy_Tree
(N
)));
6380 Build_Array_Aggr_Code
(N
,
6382 Index
=> First_Index
(Typ
),
6384 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
6387 -- Save the last assignment statement associated with the aggregate
6388 -- when building a controlled object. This reference is utilized by
6389 -- the finalization machinery when marking an object as successfully
6392 if Needs_Finalization
(Typ
)
6393 and then Is_Entity_Name
(Target
)
6394 and then Present
(Entity
(Target
))
6395 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
6397 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
6401 -- If the aggregate is the expression in a declaration, the expanded
6402 -- code must be inserted after it. The defining entity might not come
6403 -- from source if this is part of an inlined body, but the declaration
6406 if Comes_From_Source
(Tmp
)
6408 (Nkind
(Parent
(N
)) = N_Object_Declaration
6409 and then Comes_From_Source
(Parent
(N
))
6410 and then Tmp
= Defining_Entity
(Parent
(N
)))
6413 Node_After
: constant Node_Id
:= Next
(Parent_Node
);
6416 Insert_Actions_After
(Parent_Node
, Aggr_Code
);
6418 if Parent_Kind
= N_Object_Declaration
then
6419 Collect_Initialization_Statements
6420 (Obj
=> Tmp
, N
=> Parent_Node
, Node_After
=> Node_After
);
6425 Insert_Actions
(N
, Aggr_Code
);
6428 -- If the aggregate has been assigned in place, remove the original
6431 if Nkind
(Parent
(N
)) = N_Assignment_Statement
6432 and then Maybe_In_Place_OK
6434 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
6436 elsif Nkind
(Parent
(N
)) /= N_Object_Declaration
6437 or else Tmp
/= Defining_Identifier
(Parent
(N
))
6439 Rewrite
(N
, New_Occurrence_Of
(Tmp
, Loc
));
6440 Analyze_And_Resolve
(N
, Typ
);
6442 end Expand_Array_Aggregate
;
6444 ------------------------
6445 -- Expand_N_Aggregate --
6446 ------------------------
6448 procedure Expand_N_Aggregate
(N
: Node_Id
) is
6450 -- Record aggregate case
6452 if Is_Record_Type
(Etype
(N
)) then
6453 Expand_Record_Aggregate
(N
);
6455 -- Array aggregate case
6458 -- A special case, if we have a string subtype with bounds 1 .. N,
6459 -- where N is known at compile time, and the aggregate is of the
6460 -- form (others => 'x'), with a single choice and no expressions,
6461 -- and N is less than 80 (an arbitrary limit for now), then replace
6462 -- the aggregate by the equivalent string literal (but do not mark
6463 -- it as static since it is not).
6465 -- Note: this entire circuit is redundant with respect to code in
6466 -- Expand_Array_Aggregate that collapses others choices to positional
6467 -- form, but there are two problems with that circuit:
6469 -- a) It is limited to very small cases due to ill-understood
6470 -- interactions with bootstrapping. That limit is removed by
6471 -- use of the No_Implicit_Loops restriction.
6473 -- b) It incorrectly ends up with the resulting expressions being
6474 -- considered static when they are not. For example, the
6475 -- following test should fail:
6477 -- pragma Restrictions (No_Implicit_Loops);
6478 -- package NonSOthers4 is
6479 -- B : constant String (1 .. 6) := (others => 'A');
6480 -- DH : constant String (1 .. 8) := B & "BB";
6482 -- pragma Export (C, X, Link_Name => DH);
6485 -- But it succeeds (DH looks static to pragma Export)
6487 -- To be sorted out ???
6489 if Present
(Component_Associations
(N
)) then
6491 CA
: constant Node_Id
:= First
(Component_Associations
(N
));
6492 MX
: constant := 80;
6495 if Nkind
(First
(Choice_List
(CA
))) = N_Others_Choice
6496 and then Nkind
(Expression
(CA
)) = N_Character_Literal
6497 and then No
(Expressions
(N
))
6500 T
: constant Entity_Id
:= Etype
(N
);
6501 X
: constant Node_Id
:= First_Index
(T
);
6502 EC
: constant Node_Id
:= Expression
(CA
);
6503 CV
: constant Uint
:= Char_Literal_Value
(EC
);
6504 CC
: constant Int
:= UI_To_Int
(CV
);
6507 if Nkind
(X
) = N_Range
6508 and then Compile_Time_Known_Value
(Low_Bound
(X
))
6509 and then Expr_Value
(Low_Bound
(X
)) = 1
6510 and then Compile_Time_Known_Value
(High_Bound
(X
))
6513 Hi
: constant Uint
:= Expr_Value
(High_Bound
(X
));
6519 for J
in 1 .. UI_To_Int
(Hi
) loop
6520 Store_String_Char
(Char_Code
(CC
));
6524 Make_String_Literal
(Sloc
(N
),
6525 Strval
=> End_String
));
6527 if CC
>= Int
(2 ** 16) then
6528 Set_Has_Wide_Wide_Character
(N
);
6529 elsif CC
>= Int
(2 ** 8) then
6530 Set_Has_Wide_Character
(N
);
6533 Analyze_And_Resolve
(N
, T
);
6534 Set_Is_Static_Expression
(N
, False);
6544 -- Not that special case, so normal expansion of array aggregate
6546 Expand_Array_Aggregate
(N
);
6550 when RE_Not_Available
=>
6552 end Expand_N_Aggregate
;
6554 ------------------------------
6555 -- Expand_N_Delta_Aggregate --
6556 ------------------------------
6558 procedure Expand_N_Delta_Aggregate
(N
: Node_Id
) is
6559 Loc
: constant Source_Ptr
:= Sloc
(N
);
6560 Typ
: constant Entity_Id
:= Etype
(N
);
6565 Make_Object_Declaration
(Loc
,
6566 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
6567 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
6568 Expression
=> New_Copy_Tree
(Expression
(N
)));
6570 if Is_Array_Type
(Etype
(N
)) then
6571 Expand_Delta_Array_Aggregate
(N
, New_List
(Decl
));
6573 Expand_Delta_Record_Aggregate
(N
, New_List
(Decl
));
6575 end Expand_N_Delta_Aggregate
;
6577 ----------------------------------
6578 -- Expand_Delta_Array_Aggregate --
6579 ----------------------------------
6581 procedure Expand_Delta_Array_Aggregate
(N
: Node_Id
; Deltas
: List_Id
) is
6582 Loc
: constant Source_Ptr
:= Sloc
(N
);
6583 Temp
: constant Entity_Id
:= Defining_Identifier
(First
(Deltas
));
6586 function Generate_Loop
(C
: Node_Id
) return Node_Id
;
6587 -- Generate a loop containing individual component assignments for
6588 -- choices that are ranges, subtype indications, subtype names, and
6589 -- iterated component associations.
6595 function Generate_Loop
(C
: Node_Id
) return Node_Id
is
6596 Sl
: constant Source_Ptr
:= Sloc
(C
);
6600 if Nkind
(Parent
(C
)) = N_Iterated_Component_Association
then
6602 Make_Defining_Identifier
(Loc
,
6603 Chars
=> (Chars
(Defining_Identifier
(Parent
(C
)))));
6605 Ix
:= Make_Temporary
(Sl
, 'I');
6609 Make_Loop_Statement
(Loc
,
6611 Make_Iteration_Scheme
(Sl
,
6612 Loop_Parameter_Specification
=>
6613 Make_Loop_Parameter_Specification
(Sl
,
6614 Defining_Identifier
=> Ix
,
6615 Discrete_Subtype_Definition
=> New_Copy_Tree
(C
))),
6617 Statements
=> New_List
(
6618 Make_Assignment_Statement
(Sl
,
6620 Make_Indexed_Component
(Sl
,
6621 Prefix
=> New_Occurrence_Of
(Temp
, Sl
),
6622 Expressions
=> New_List
(New_Occurrence_Of
(Ix
, Sl
))),
6623 Expression
=> New_Copy_Tree
(Expression
(Assoc
)))),
6624 End_Label
=> Empty
);
6631 -- Start of processing for Expand_Delta_Array_Aggregate
6634 Assoc
:= First
(Component_Associations
(N
));
6635 while Present
(Assoc
) loop
6636 Choice
:= First
(Choice_List
(Assoc
));
6637 if Nkind
(Assoc
) = N_Iterated_Component_Association
then
6638 while Present
(Choice
) loop
6639 Append_To
(Deltas
, Generate_Loop
(Choice
));
6644 while Present
(Choice
) loop
6646 -- Choice can be given by a range, a subtype indication, a
6647 -- subtype name, a scalar value, or an entity.
6649 if Nkind
(Choice
) = N_Range
6650 or else (Is_Entity_Name
(Choice
)
6651 and then Is_Type
(Entity
(Choice
)))
6653 Append_To
(Deltas
, Generate_Loop
(Choice
));
6655 elsif Nkind
(Choice
) = N_Subtype_Indication
then
6657 Generate_Loop
(Range_Expression
(Constraint
(Choice
))));
6661 Make_Assignment_Statement
(Sloc
(Choice
),
6663 Make_Indexed_Component
(Sloc
(Choice
),
6664 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
6665 Expressions
=> New_List
(New_Copy_Tree
(Choice
))),
6666 Expression
=> New_Copy_Tree
(Expression
(Assoc
))));
6676 Insert_Actions
(N
, Deltas
);
6677 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
6678 end Expand_Delta_Array_Aggregate
;
6680 -----------------------------------
6681 -- Expand_Delta_Record_Aggregate --
6682 -----------------------------------
6684 procedure Expand_Delta_Record_Aggregate
(N
: Node_Id
; Deltas
: List_Id
) is
6685 Loc
: constant Source_Ptr
:= Sloc
(N
);
6686 Temp
: constant Entity_Id
:= Defining_Identifier
(First
(Deltas
));
6691 Assoc
:= First
(Component_Associations
(N
));
6693 while Present
(Assoc
) loop
6694 Choice
:= First
(Choice_List
(Assoc
));
6695 while Present
(Choice
) loop
6697 Make_Assignment_Statement
(Sloc
(Choice
),
6699 Make_Selected_Component
(Sloc
(Choice
),
6700 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
6701 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Choice
))),
6702 Expression
=> New_Copy_Tree
(Expression
(Assoc
))));
6709 Insert_Actions
(N
, Deltas
);
6710 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
6711 end Expand_Delta_Record_Aggregate
;
6713 ----------------------------------
6714 -- Expand_N_Extension_Aggregate --
6715 ----------------------------------
6717 -- If the ancestor part is an expression, add a component association for
6718 -- the parent field. If the type of the ancestor part is not the direct
6719 -- parent of the expected type, build recursively the needed ancestors.
6720 -- If the ancestor part is a subtype_mark, replace aggregate with a
6721 -- declaration for a temporary of the expected type, followed by
6722 -- individual assignments to the given components.
6724 procedure Expand_N_Extension_Aggregate
(N
: Node_Id
) is
6725 A
: constant Node_Id
:= Ancestor_Part
(N
);
6726 Loc
: constant Source_Ptr
:= Sloc
(N
);
6727 Typ
: constant Entity_Id
:= Etype
(N
);
6730 -- If the ancestor is a subtype mark, an init proc must be called
6731 -- on the resulting object which thus has to be materialized in
6734 if Is_Entity_Name
(A
) and then Is_Type
(Entity
(A
)) then
6735 Convert_To_Assignments
(N
, Typ
);
6737 -- The extension aggregate is transformed into a record aggregate
6738 -- of the following form (c1 and c2 are inherited components)
6740 -- (Exp with c3 => a, c4 => b)
6741 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6746 if Tagged_Type_Expansion
then
6747 Expand_Record_Aggregate
(N
,
6750 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
),
6753 -- No tag is needed in the case of a VM
6756 Expand_Record_Aggregate
(N
, Parent_Expr
=> A
);
6761 when RE_Not_Available
=>
6763 end Expand_N_Extension_Aggregate
;
6765 -----------------------------
6766 -- Expand_Record_Aggregate --
6767 -----------------------------
6769 procedure Expand_Record_Aggregate
6771 Orig_Tag
: Node_Id
:= Empty
;
6772 Parent_Expr
: Node_Id
:= Empty
)
6774 Loc
: constant Source_Ptr
:= Sloc
(N
);
6775 Comps
: constant List_Id
:= Component_Associations
(N
);
6776 Typ
: constant Entity_Id
:= Etype
(N
);
6777 Base_Typ
: constant Entity_Id
:= Base_Type
(Typ
);
6779 Static_Components
: Boolean := True;
6780 -- Flag to indicate whether all components are compile-time known,
6781 -- and the aggregate can be constructed statically and handled by
6782 -- the back-end. Set to False by Component_OK_For_Backend.
6784 procedure Build_Back_End_Aggregate
;
6785 -- Build a proper aggregate to be handled by the back-end
6787 function Compile_Time_Known_Composite_Value
(N
: Node_Id
) return Boolean;
6788 -- Returns true if N is an expression of composite type which can be
6789 -- fully evaluated at compile time without raising constraint error.
6790 -- Such expressions can be passed as is to Gigi without any expansion.
6792 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6793 -- set and constants whose expression is such an aggregate, recursively.
6795 function Component_OK_For_Backend
return Boolean;
6796 -- Check for presence of a component which makes it impossible for the
6797 -- backend to process the aggregate, thus requiring the use of a series
6798 -- of assignment statements. Cases checked for are a nested aggregate
6799 -- needing Late_Expansion, the presence of a tagged component which may
6800 -- need tag adjustment, and a bit unaligned component reference.
6802 -- We also force expansion into assignments if a component is of a
6803 -- mutable type (including a private type with discriminants) because
6804 -- in that case the size of the component to be copied may be smaller
6805 -- than the side of the target, and there is no simple way for gigi
6806 -- to compute the size of the object to be copied.
6808 -- NOTE: This is part of the ongoing work to define precisely the
6809 -- interface between front-end and back-end handling of aggregates.
6810 -- In general it is desirable to pass aggregates as they are to gigi,
6811 -- in order to minimize elaboration code. This is one case where the
6812 -- semantics of Ada complicate the analysis and lead to anomalies in
6813 -- the gcc back-end if the aggregate is not expanded into assignments.
6815 -- NOTE: This sets the global Static_Components to False in most, but
6816 -- not all, cases when it returns False.
6818 function Has_Per_Object_Constraint
(L
: List_Id
) return Boolean;
6819 -- Return True if any element of L has Has_Per_Object_Constraint set.
6820 -- L should be the Choices component of an N_Component_Association.
6822 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean;
6823 -- If any ancestor of the current type is private, the aggregate
6824 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6825 -- because it will not be set when type and its parent are in the
6826 -- same scope, and the parent component needs expansion.
6828 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
;
6829 -- For nested aggregates return the ultimate enclosing aggregate; for
6830 -- non-nested aggregates return N.
6832 ------------------------------
6833 -- Build_Back_End_Aggregate --
6834 ------------------------------
6836 procedure Build_Back_End_Aggregate
is
6839 Tag_Value
: Node_Id
;
6842 if Nkind
(N
) = N_Aggregate
then
6844 -- If the aggregate is static and can be handled by the back-end,
6845 -- nothing left to do.
6847 if Static_Components
then
6848 Set_Compile_Time_Known_Aggregate
(N
);
6849 Set_Expansion_Delayed
(N
, False);
6853 -- If no discriminants, nothing special to do
6855 if not Has_Discriminants
(Typ
) then
6858 -- Case of discriminants present
6860 elsif Is_Derived_Type
(Typ
) then
6862 -- For untagged types, non-stored discriminants are replaced with
6863 -- stored discriminants, which are the ones that gigi uses to
6864 -- describe the type and its components.
6866 Generate_Aggregate_For_Derived_Type
: declare
6867 procedure Prepend_Stored_Values
(T
: Entity_Id
);
6868 -- Scan the list of stored discriminants of the type, and add
6869 -- their values to the aggregate being built.
6871 ---------------------------
6872 -- Prepend_Stored_Values --
6873 ---------------------------
6875 procedure Prepend_Stored_Values
(T
: Entity_Id
) is
6877 First_Comp
: Node_Id
:= Empty
;
6880 Discr
:= First_Stored_Discriminant
(T
);
6881 while Present
(Discr
) loop
6883 Make_Component_Association
(Loc
,
6884 Choices
=> New_List
(
6885 New_Occurrence_Of
(Discr
, Loc
)),
6888 (Get_Discriminant_Value
6891 Discriminant_Constraint
(Typ
))));
6893 if No
(First_Comp
) then
6894 Prepend_To
(Component_Associations
(N
), New_Comp
);
6896 Insert_After
(First_Comp
, New_Comp
);
6899 First_Comp
:= New_Comp
;
6900 Next_Stored_Discriminant
(Discr
);
6902 end Prepend_Stored_Values
;
6906 Constraints
: constant List_Id
:= New_List
;
6910 Num_Disc
: Nat
:= 0;
6911 Num_Gird
: Nat
:= 0;
6913 -- Start of processing for Generate_Aggregate_For_Derived_Type
6916 -- Remove the associations for the discriminant of derived type
6919 First_Comp
: Node_Id
;
6922 First_Comp
:= First
(Component_Associations
(N
));
6923 while Present
(First_Comp
) loop
6927 if Ekind
(Entity
(First
(Choices
(Comp
)))) =
6931 Num_Disc
:= Num_Disc
+ 1;
6936 -- Insert stored discriminant associations in the correct
6937 -- order. If there are more stored discriminants than new
6938 -- discriminants, there is at least one new discriminant that
6939 -- constrains more than one of the stored discriminants. In
6940 -- this case we need to construct a proper subtype of the
6941 -- parent type, in order to supply values to all the
6942 -- components. Otherwise there is one-one correspondence
6943 -- between the constraints and the stored discriminants.
6945 Discr
:= First_Stored_Discriminant
(Base_Type
(Typ
));
6946 while Present
(Discr
) loop
6947 Num_Gird
:= Num_Gird
+ 1;
6948 Next_Stored_Discriminant
(Discr
);
6951 -- Case of more stored discriminants than new discriminants
6953 if Num_Gird
> Num_Disc
then
6955 -- Create a proper subtype of the parent type, which is the
6956 -- proper implementation type for the aggregate, and convert
6957 -- it to the intended target type.
6959 Discr
:= First_Stored_Discriminant
(Base_Type
(Typ
));
6960 while Present
(Discr
) loop
6963 (Get_Discriminant_Value
6966 Discriminant_Constraint
(Typ
)));
6968 Append
(New_Comp
, Constraints
);
6969 Next_Stored_Discriminant
(Discr
);
6973 Make_Subtype_Declaration
(Loc
,
6974 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
6975 Subtype_Indication
=>
6976 Make_Subtype_Indication
(Loc
,
6978 New_Occurrence_Of
(Etype
(Base_Type
(Typ
)), Loc
),
6980 Make_Index_Or_Discriminant_Constraint
6981 (Loc
, Constraints
)));
6983 Insert_Action
(N
, Decl
);
6984 Prepend_Stored_Values
(Base_Type
(Typ
));
6986 Set_Etype
(N
, Defining_Identifier
(Decl
));
6989 Rewrite
(N
, Unchecked_Convert_To
(Typ
, N
));
6992 -- Case where we do not have fewer new discriminants than
6993 -- stored discriminants, so in this case we can simply use the
6994 -- stored discriminants of the subtype.
6997 Prepend_Stored_Values
(Typ
);
6999 end Generate_Aggregate_For_Derived_Type
;
7002 if Is_Tagged_Type
(Typ
) then
7004 -- In the tagged case, _parent and _tag component must be created
7006 -- Reset Null_Present unconditionally. Tagged records always have
7007 -- at least one field (the tag or the parent).
7009 Set_Null_Record_Present
(N
, False);
7011 -- When the current aggregate comes from the expansion of an
7012 -- extension aggregate, the parent expr is replaced by an
7013 -- aggregate formed by selected components of this expr.
7015 if Present
(Parent_Expr
) and then Is_Empty_List
(Comps
) then
7016 Comp
:= First_Component_Or_Discriminant
(Typ
);
7017 while Present
(Comp
) loop
7019 -- Skip all expander-generated components
7021 if not Comes_From_Source
(Original_Record_Component
(Comp
))
7027 Make_Selected_Component
(Loc
,
7029 Unchecked_Convert_To
(Typ
,
7030 Duplicate_Subexpr
(Parent_Expr
, True)),
7031 Selector_Name
=> New_Occurrence_Of
(Comp
, Loc
));
7034 Make_Component_Association
(Loc
,
7035 Choices
=> New_List
(
7036 New_Occurrence_Of
(Comp
, Loc
)),
7037 Expression
=> New_Comp
));
7039 Analyze_And_Resolve
(New_Comp
, Etype
(Comp
));
7042 Next_Component_Or_Discriminant
(Comp
);
7046 -- Compute the value for the Tag now, if the type is a root it
7047 -- will be included in the aggregate right away, otherwise it will
7048 -- be propagated to the parent aggregate.
7050 if Present
(Orig_Tag
) then
7051 Tag_Value
:= Orig_Tag
;
7053 elsif not Tagged_Type_Expansion
then
7059 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
);
7062 -- For a derived type, an aggregate for the parent is formed with
7063 -- all the inherited components.
7065 if Is_Derived_Type
(Typ
) then
7067 First_Comp
: Node_Id
;
7068 Parent_Comps
: List_Id
;
7069 Parent_Aggr
: Node_Id
;
7070 Parent_Name
: Node_Id
;
7073 -- Remove the inherited component association from the
7074 -- aggregate and store them in the parent aggregate
7076 First_Comp
:= First
(Component_Associations
(N
));
7077 Parent_Comps
:= New_List
;
7078 while Present
(First_Comp
)
7080 Scope
(Original_Record_Component
7081 (Entity
(First
(Choices
(First_Comp
))))) /=
7087 Append
(Comp
, Parent_Comps
);
7091 Make_Aggregate
(Loc
,
7092 Component_Associations
=> Parent_Comps
);
7093 Set_Etype
(Parent_Aggr
, Etype
(Base_Type
(Typ
)));
7095 -- Find the _parent component
7097 Comp
:= First_Component
(Typ
);
7098 while Chars
(Comp
) /= Name_uParent
loop
7099 Comp
:= Next_Component
(Comp
);
7102 Parent_Name
:= New_Occurrence_Of
(Comp
, Loc
);
7104 -- Insert the parent aggregate
7106 Prepend_To
(Component_Associations
(N
),
7107 Make_Component_Association
(Loc
,
7108 Choices
=> New_List
(Parent_Name
),
7109 Expression
=> Parent_Aggr
));
7111 -- Expand recursively the parent propagating the right Tag
7113 Expand_Record_Aggregate
7114 (Parent_Aggr
, Tag_Value
, Parent_Expr
);
7116 -- The ancestor part may be a nested aggregate that has
7117 -- delayed expansion: recheck now.
7119 if not Component_OK_For_Backend
then
7120 Convert_To_Assignments
(N
, Typ
);
7124 -- For a root type, the tag component is added (unless compiling
7125 -- for the VMs, where tags are implicit).
7127 elsif Tagged_Type_Expansion
then
7129 Tag_Name
: constant Node_Id
:=
7131 (First_Tag_Component
(Typ
), Loc
);
7132 Typ_Tag
: constant Entity_Id
:= RTE
(RE_Tag
);
7133 Conv_Node
: constant Node_Id
:=
7134 Unchecked_Convert_To
(Typ_Tag
, Tag_Value
);
7137 Set_Etype
(Conv_Node
, Typ_Tag
);
7138 Prepend_To
(Component_Associations
(N
),
7139 Make_Component_Association
(Loc
,
7140 Choices
=> New_List
(Tag_Name
),
7141 Expression
=> Conv_Node
));
7145 end Build_Back_End_Aggregate
;
7147 ----------------------------------------
7148 -- Compile_Time_Known_Composite_Value --
7149 ----------------------------------------
7151 function Compile_Time_Known_Composite_Value
7152 (N
: Node_Id
) return Boolean
7155 -- If we have an entity name, then see if it is the name of a
7156 -- constant and if so, test the corresponding constant value.
7158 if Is_Entity_Name
(N
) then
7160 E
: constant Entity_Id
:= Entity
(N
);
7163 if Ekind
(E
) /= E_Constant
then
7166 V
:= Constant_Value
(E
);
7168 and then Compile_Time_Known_Composite_Value
(V
);
7172 -- We have a value, see if it is compile time known
7175 if Nkind
(N
) = N_Aggregate
then
7176 return Compile_Time_Known_Aggregate
(N
);
7179 -- All other types of values are not known at compile time
7184 end Compile_Time_Known_Composite_Value
;
7186 ------------------------------
7187 -- Component_OK_For_Backend --
7188 ------------------------------
7190 function Component_OK_For_Backend
return Boolean is
7200 while Present
(C
) loop
7202 -- If the component has box initialization, expansion is needed
7203 -- and component is not ready for backend.
7205 if Box_Present
(C
) then
7209 if Nkind
(Expression
(C
)) = N_Qualified_Expression
then
7210 Expr_Q
:= Expression
(Expression
(C
));
7212 Expr_Q
:= Expression
(C
);
7215 -- Return False if the aggregate has any associations for tagged
7216 -- components that may require tag adjustment.
7218 -- These are cases where the source expression may have a tag that
7219 -- could differ from the component tag (e.g., can occur for type
7220 -- conversions and formal parameters). (Tag adjustment not needed
7221 -- if Tagged_Type_Expansion because object tags are implicit in
7224 if Is_Tagged_Type
(Etype
(Expr_Q
))
7225 and then (Nkind
(Expr_Q
) = N_Type_Conversion
7226 or else (Is_Entity_Name
(Expr_Q
)
7228 Ekind
(Entity
(Expr_Q
)) in Formal_Kind
))
7229 and then Tagged_Type_Expansion
7231 Static_Components
:= False;
7234 elsif Is_Delayed_Aggregate
(Expr_Q
) then
7235 Static_Components
:= False;
7238 elsif Possible_Bit_Aligned_Component
(Expr_Q
) then
7239 Static_Components
:= False;
7242 elsif Modify_Tree_For_C
7243 and then Nkind
(C
) = N_Component_Association
7244 and then Has_Per_Object_Constraint
(Choices
(C
))
7246 Static_Components
:= False;
7249 elsif Modify_Tree_For_C
7250 and then Nkind
(Expr_Q
) = N_Identifier
7251 and then Is_Array_Type
(Etype
(Expr_Q
))
7253 Static_Components
:= False;
7256 elsif Modify_Tree_For_C
7257 and then Nkind
(Expr_Q
) = N_Type_Conversion
7258 and then Is_Array_Type
(Etype
(Expr_Q
))
7260 Static_Components
:= False;
7264 if Is_Elementary_Type
(Etype
(Expr_Q
)) then
7265 if not Compile_Time_Known_Value
(Expr_Q
) then
7266 Static_Components
:= False;
7269 elsif not Compile_Time_Known_Composite_Value
(Expr_Q
) then
7270 Static_Components
:= False;
7272 if Is_Private_Type
(Etype
(Expr_Q
))
7273 and then Has_Discriminants
(Etype
(Expr_Q
))
7283 end Component_OK_For_Backend
;
7285 -------------------------------
7286 -- Has_Per_Object_Constraint --
7287 -------------------------------
7289 function Has_Per_Object_Constraint
(L
: List_Id
) return Boolean is
7290 N
: Node_Id
:= First
(L
);
7292 while Present
(N
) loop
7293 if Is_Entity_Name
(N
)
7294 and then Present
(Entity
(N
))
7295 and then Has_Per_Object_Constraint
(Entity
(N
))
7304 end Has_Per_Object_Constraint
;
7306 -----------------------------------
7307 -- Has_Visible_Private_Ancestor --
7308 -----------------------------------
7310 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean is
7311 R
: constant Entity_Id
:= Root_Type
(Id
);
7312 T1
: Entity_Id
:= Id
;
7316 if Is_Private_Type
(T1
) then
7326 end Has_Visible_Private_Ancestor
;
7328 -------------------------
7329 -- Top_Level_Aggregate --
7330 -------------------------
7332 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
is
7337 while Present
(Parent
(Aggr
))
7338 and then Nkind_In
(Parent
(Aggr
), N_Aggregate
,
7339 N_Component_Association
)
7341 Aggr
:= Parent
(Aggr
);
7345 end Top_Level_Aggregate
;
7349 Top_Level_Aggr
: constant Node_Id
:= Top_Level_Aggregate
(N
);
7351 -- Start of processing for Expand_Record_Aggregate
7354 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7355 -- to prevent a piecemeal assignment even if the aggregate is to be
7356 -- expanded. We create a temporary for the aggregate, and assign the
7357 -- temporary instead, so that the back end can generate an atomic move
7360 if Is_Atomic_VFA_Aggregate
(N
) then
7363 -- No special management required for aggregates used to initialize
7364 -- statically allocated dispatch tables
7366 elsif Is_Static_Dispatch_Table_Aggregate
(N
) then
7370 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7371 -- are build-in-place function calls. The assignments will each turn
7372 -- into a build-in-place function call. If components are all static,
7373 -- we can pass the aggregate to the back end regardless of limitedness.
7375 -- Extension aggregates, aggregates in extended return statements, and
7376 -- aggregates for C++ imported types must be expanded.
7378 if Ada_Version
>= Ada_2005
and then Is_Limited_View
(Typ
) then
7379 if not Nkind_In
(Parent
(N
), N_Component_Association
,
7380 N_Object_Declaration
)
7382 Convert_To_Assignments
(N
, Typ
);
7384 elsif Nkind
(N
) = N_Extension_Aggregate
7385 or else Convention
(Typ
) = Convention_CPP
7387 Convert_To_Assignments
(N
, Typ
);
7389 elsif not Size_Known_At_Compile_Time
(Typ
)
7390 or else not Component_OK_For_Backend
7391 or else not Static_Components
7393 Convert_To_Assignments
(N
, Typ
);
7395 -- In all other cases, build a proper aggregate to be handled by
7399 Build_Back_End_Aggregate
;
7402 -- Gigi doesn't properly handle temporaries of variable size so we
7403 -- generate it in the front-end
7405 elsif not Size_Known_At_Compile_Time
(Typ
)
7406 and then Tagged_Type_Expansion
7408 Convert_To_Assignments
(N
, Typ
);
7410 -- An aggregate used to initialize a controlled object must be turned
7411 -- into component assignments as the components themselves may require
7412 -- finalization actions such as adjustment.
7414 elsif Needs_Finalization
(Typ
) then
7415 Convert_To_Assignments
(N
, Typ
);
7417 -- Ada 2005 (AI-287): In case of default initialized components we
7418 -- convert the aggregate into assignments.
7420 elsif Has_Default_Init_Comps
(N
) then
7421 Convert_To_Assignments
(N
, Typ
);
7425 elsif not Component_OK_For_Backend
then
7426 Convert_To_Assignments
(N
, Typ
);
7428 -- If an ancestor is private, some components are not inherited and we
7429 -- cannot expand into a record aggregate.
7431 elsif Has_Visible_Private_Ancestor
(Typ
) then
7432 Convert_To_Assignments
(N
, Typ
);
7434 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7435 -- is not able to handle the aggregate for Late_Request.
7437 elsif Is_Tagged_Type
(Typ
) and then Has_Discriminants
(Typ
) then
7438 Convert_To_Assignments
(N
, Typ
);
7440 -- If the tagged types covers interface types we need to initialize all
7441 -- hidden components containing pointers to secondary dispatch tables.
7443 elsif Is_Tagged_Type
(Typ
) and then Has_Interfaces
(Typ
) then
7444 Convert_To_Assignments
(N
, Typ
);
7446 -- If some components are mutable, the size of the aggregate component
7447 -- may be distinct from the default size of the type component, so
7448 -- we need to expand to insure that the back-end copies the proper
7449 -- size of the data. However, if the aggregate is the initial value of
7450 -- a constant, the target is immutable and might be built statically
7451 -- if components are appropriate.
7453 elsif Has_Mutable_Components
(Typ
)
7455 (Nkind
(Parent
(Top_Level_Aggr
)) /= N_Object_Declaration
7456 or else not Constant_Present
(Parent
(Top_Level_Aggr
))
7457 or else not Static_Components
)
7459 Convert_To_Assignments
(N
, Typ
);
7461 -- If the type involved has bit aligned components, then we are not sure
7462 -- that the back end can handle this case correctly.
7464 elsif Type_May_Have_Bit_Aligned_Components
(Typ
) then
7465 Convert_To_Assignments
(N
, Typ
);
7467 -- When generating C, only generate an aggregate when declaring objects
7468 -- since C does not support aggregates in e.g. assignment statements.
7470 elsif Modify_Tree_For_C
and then not In_Object_Declaration
(N
) then
7471 Convert_To_Assignments
(N
, Typ
);
7473 -- In all other cases, build a proper aggregate to be handled by gigi
7476 Build_Back_End_Aggregate
;
7478 end Expand_Record_Aggregate
;
7480 ----------------------------
7481 -- Has_Default_Init_Comps --
7482 ----------------------------
7484 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean is
7485 Comps
: constant List_Id
:= Component_Associations
(N
);
7490 pragma Assert
(Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
));
7496 if Has_Self_Reference
(N
) then
7500 -- Check if any direct component has default initialized components
7503 while Present
(C
) loop
7504 if Box_Present
(C
) then
7511 -- Recursive call in case of aggregate expression
7514 while Present
(C
) loop
7515 Expr
:= Expression
(C
);
7518 and then Nkind_In
(Expr
, N_Aggregate
, N_Extension_Aggregate
)
7519 and then Has_Default_Init_Comps
(Expr
)
7528 end Has_Default_Init_Comps
;
7530 ----------------------------------------
7531 -- Is_Build_In_Place_Aggregate_Return --
7532 ----------------------------------------
7534 function Is_Build_In_Place_Aggregate_Return
(N
: Node_Id
) return Boolean is
7535 P
: Node_Id
:= Parent
(N
);
7538 while Nkind
(P
) = N_Qualified_Expression
loop
7542 if Nkind
(P
) = N_Simple_Return_Statement
then
7545 elsif Nkind
(Parent
(P
)) = N_Extended_Return_Statement
then
7553 Is_Build_In_Place_Function
7554 (Return_Applies_To
(Return_Statement_Entity
(P
)));
7555 end Is_Build_In_Place_Aggregate_Return
;
7557 --------------------------
7558 -- Is_Delayed_Aggregate --
7559 --------------------------
7561 function Is_Delayed_Aggregate
(N
: Node_Id
) return Boolean is
7562 Node
: Node_Id
:= N
;
7563 Kind
: Node_Kind
:= Nkind
(Node
);
7566 if Kind
= N_Qualified_Expression
then
7567 Node
:= Expression
(Node
);
7568 Kind
:= Nkind
(Node
);
7571 if not Nkind_In
(Kind
, N_Aggregate
, N_Extension_Aggregate
) then
7574 return Expansion_Delayed
(Node
);
7576 end Is_Delayed_Aggregate
;
7578 ---------------------------
7579 -- In_Object_Declaration --
7580 ---------------------------
7582 function In_Object_Declaration
(N
: Node_Id
) return Boolean is
7583 P
: Node_Id
:= Parent
(N
);
7585 while Present
(P
) loop
7586 if Nkind
(P
) = N_Object_Declaration
then
7594 end In_Object_Declaration
;
7596 ----------------------------------------
7597 -- Is_Static_Dispatch_Table_Aggregate --
7598 ----------------------------------------
7600 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean is
7601 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
7604 return Building_Static_Dispatch_Tables
7605 and then Tagged_Type_Expansion
7606 and then RTU_Loaded
(Ada_Tags
)
7608 -- Avoid circularity when rebuilding the compiler
7610 and then Cunit_Entity
(Get_Source_Unit
(N
)) /= RTU_Entity
(Ada_Tags
)
7611 and then (Typ
= RTE
(RE_Dispatch_Table_Wrapper
)
7613 Typ
= RTE
(RE_Address_Array
)
7615 Typ
= RTE
(RE_Type_Specific_Data
)
7617 Typ
= RTE
(RE_Tag_Table
)
7619 (RTE_Available
(RE_Interface_Data
)
7620 and then Typ
= RTE
(RE_Interface_Data
))
7622 (RTE_Available
(RE_Interfaces_Array
)
7623 and then Typ
= RTE
(RE_Interfaces_Array
))
7625 (RTE_Available
(RE_Interface_Data_Element
)
7626 and then Typ
= RTE
(RE_Interface_Data_Element
)));
7627 end Is_Static_Dispatch_Table_Aggregate
;
7629 -----------------------------
7630 -- Is_Two_Dim_Packed_Array --
7631 -----------------------------
7633 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean is
7634 C
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
7636 return Number_Dimensions
(Typ
) = 2
7637 and then Is_Bit_Packed_Array
(Typ
)
7638 and then (C
= 1 or else C
= 2 or else C
= 4);
7639 end Is_Two_Dim_Packed_Array
;
7641 --------------------
7642 -- Late_Expansion --
7643 --------------------
7645 function Late_Expansion
7648 Target
: Node_Id
) return List_Id
7650 Aggr_Code
: List_Id
;
7653 if Is_Array_Type
(Etype
(N
)) then
7655 Build_Array_Aggr_Code
7657 Ctype
=> Component_Type
(Etype
(N
)),
7658 Index
=> First_Index
(Typ
),
7660 Scalar_Comp
=> Is_Scalar_Type
(Component_Type
(Typ
)),
7661 Indexes
=> No_List
);
7663 -- Directly or indirectly (e.g. access protected procedure) a record
7666 Aggr_Code
:= Build_Record_Aggr_Code
(N
, Typ
, Target
);
7669 -- Save the last assignment statement associated with the aggregate
7670 -- when building a controlled object. This reference is utilized by
7671 -- the finalization machinery when marking an object as successfully
7674 if Needs_Finalization
(Typ
)
7675 and then Is_Entity_Name
(Target
)
7676 and then Present
(Entity
(Target
))
7677 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
7679 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
7685 ----------------------------------
7686 -- Make_OK_Assignment_Statement --
7687 ----------------------------------
7689 function Make_OK_Assignment_Statement
7692 Expression
: Node_Id
) return Node_Id
7695 Set_Assignment_OK
(Name
);
7696 return Make_Assignment_Statement
(Sloc
, Name
, Expression
);
7697 end Make_OK_Assignment_Statement
;
7699 -----------------------
7700 -- Number_Of_Choices --
7701 -----------------------
7703 function Number_Of_Choices
(N
: Node_Id
) return Nat
is
7707 Nb_Choices
: Nat
:= 0;
7710 if Present
(Expressions
(N
)) then
7714 Assoc
:= First
(Component_Associations
(N
));
7715 while Present
(Assoc
) loop
7716 Choice
:= First
(Choice_List
(Assoc
));
7717 while Present
(Choice
) loop
7718 if Nkind
(Choice
) /= N_Others_Choice
then
7719 Nb_Choices
:= Nb_Choices
+ 1;
7729 end Number_Of_Choices
;
7731 ------------------------------------
7732 -- Packed_Array_Aggregate_Handled --
7733 ------------------------------------
7735 -- The current version of this procedure will handle at compile time
7736 -- any array aggregate that meets these conditions:
7738 -- One and two dimensional, bit packed
7739 -- Underlying packed type is modular type
7740 -- Bounds are within 32-bit Int range
7741 -- All bounds and values are static
7743 -- Note: for now, in the 2-D case, we only handle component sizes of
7744 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7746 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean is
7747 Loc
: constant Source_Ptr
:= Sloc
(N
);
7748 Typ
: constant Entity_Id
:= Etype
(N
);
7749 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
7751 Not_Handled
: exception;
7752 -- Exception raised if this aggregate cannot be handled
7755 -- Handle one- or two dimensional bit packed array
7757 if not Is_Bit_Packed_Array
(Typ
)
7758 or else Number_Dimensions
(Typ
) > 2
7763 -- If two-dimensional, check whether it can be folded, and transformed
7764 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7765 -- the original type.
7767 if Number_Dimensions
(Typ
) = 2 then
7768 return Two_Dim_Packed_Array_Handled
(N
);
7771 if not Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
)) then
7775 if not Is_Scalar_Type
(Component_Type
(Typ
))
7776 and then Has_Non_Standard_Rep
(Component_Type
(Typ
))
7782 Csiz
: constant Nat
:= UI_To_Int
(Component_Size
(Typ
));
7786 -- Bounds of index type
7790 -- Values of bounds if compile time known
7792 function Get_Component_Val
(N
: Node_Id
) return Uint
;
7793 -- Given a expression value N of the component type Ctyp, returns a
7794 -- value of Csiz (component size) bits representing this value. If
7795 -- the value is nonstatic or any other reason exists why the value
7796 -- cannot be returned, then Not_Handled is raised.
7798 -----------------------
7799 -- Get_Component_Val --
7800 -----------------------
7802 function Get_Component_Val
(N
: Node_Id
) return Uint
is
7806 -- We have to analyze the expression here before doing any further
7807 -- processing here. The analysis of such expressions is deferred
7808 -- till expansion to prevent some problems of premature analysis.
7810 Analyze_And_Resolve
(N
, Ctyp
);
7812 -- Must have a compile time value. String literals have to be
7813 -- converted into temporaries as well, because they cannot easily
7814 -- be converted into their bit representation.
7816 if not Compile_Time_Known_Value
(N
)
7817 or else Nkind
(N
) = N_String_Literal
7822 Val
:= Expr_Rep_Value
(N
);
7824 -- Adjust for bias, and strip proper number of bits
7826 if Has_Biased_Representation
(Ctyp
) then
7827 Val
:= Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
7830 return Val
mod Uint_2
** Csiz
;
7831 end Get_Component_Val
;
7833 -- Here we know we have a one dimensional bit packed array
7836 Get_Index_Bounds
(First_Index
(Typ
), Lo
, Hi
);
7838 -- Cannot do anything if bounds are dynamic
7840 if not Compile_Time_Known_Value
(Lo
)
7842 not Compile_Time_Known_Value
(Hi
)
7847 -- Or are silly out of range of int bounds
7849 Lob
:= Expr_Value
(Lo
);
7850 Hib
:= Expr_Value
(Hi
);
7852 if not UI_Is_In_Int_Range
(Lob
)
7854 not UI_Is_In_Int_Range
(Hib
)
7859 -- At this stage we have a suitable aggregate for handling at compile
7860 -- time. The only remaining checks are that the values of expressions
7861 -- in the aggregate are compile-time known (checks are performed by
7862 -- Get_Component_Val), and that any subtypes or ranges are statically
7865 -- If the aggregate is not fully positional at this stage, then
7866 -- convert it to positional form. Either this will fail, in which
7867 -- case we can do nothing, or it will succeed, in which case we have
7868 -- succeeded in handling the aggregate and transforming it into a
7869 -- modular value, or it will stay an aggregate, in which case we
7870 -- have failed to create a packed value for it.
7872 if Present
(Component_Associations
(N
)) then
7873 Convert_To_Positional
7874 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
7875 return Nkind
(N
) /= N_Aggregate
;
7878 -- Otherwise we are all positional, so convert to proper value
7881 Lov
: constant Int
:= UI_To_Int
(Lob
);
7882 Hiv
: constant Int
:= UI_To_Int
(Hib
);
7884 Len
: constant Nat
:= Int
'Max (0, Hiv
- Lov
+ 1);
7885 -- The length of the array (number of elements)
7887 Aggregate_Val
: Uint
;
7888 -- Value of aggregate. The value is set in the low order bits of
7889 -- this value. For the little-endian case, the values are stored
7890 -- from low-order to high-order and for the big-endian case the
7891 -- values are stored from high-order to low-order. Note that gigi
7892 -- will take care of the conversions to left justify the value in
7893 -- the big endian case (because of left justified modular type
7894 -- processing), so we do not have to worry about that here.
7897 -- Integer literal for resulting constructed value
7900 -- Shift count from low order for next value
7903 -- Shift increment for loop
7906 -- Next expression from positional parameters of aggregate
7908 Left_Justified
: Boolean;
7909 -- Set True if we are filling the high order bits of the target
7910 -- value (i.e. the value is left justified).
7913 -- For little endian, we fill up the low order bits of the target
7914 -- value. For big endian we fill up the high order bits of the
7915 -- target value (which is a left justified modular value).
7917 Left_Justified
:= Bytes_Big_Endian
;
7919 -- Switch justification if using -gnatd8
7921 if Debug_Flag_8
then
7922 Left_Justified
:= not Left_Justified
;
7925 -- Switch justfification if reverse storage order
7927 if Reverse_Storage_Order
(Base_Type
(Typ
)) then
7928 Left_Justified
:= not Left_Justified
;
7931 if Left_Justified
then
7932 Shift
:= Csiz
* (Len
- 1);
7939 -- Loop to set the values
7942 Aggregate_Val
:= Uint_0
;
7944 Expr
:= First
(Expressions
(N
));
7945 Aggregate_Val
:= Get_Component_Val
(Expr
) * Uint_2
** Shift
;
7947 for J
in 2 .. Len
loop
7948 Shift
:= Shift
+ Incr
;
7951 Aggregate_Val
+ Get_Component_Val
(Expr
) * Uint_2
** Shift
;
7955 -- Now we can rewrite with the proper value
7957 Lit
:= Make_Integer_Literal
(Loc
, Intval
=> Aggregate_Val
);
7958 Set_Print_In_Hex
(Lit
);
7960 -- Construct the expression using this literal. Note that it is
7961 -- important to qualify the literal with its proper modular type
7962 -- since universal integer does not have the required range and
7963 -- also this is a left justified modular type, which is important
7964 -- in the big-endian case.
7967 Unchecked_Convert_To
(Typ
,
7968 Make_Qualified_Expression
(Loc
,
7970 New_Occurrence_Of
(Packed_Array_Impl_Type
(Typ
), Loc
),
7971 Expression
=> Lit
)));
7973 Analyze_And_Resolve
(N
, Typ
);
7981 end Packed_Array_Aggregate_Handled
;
7983 ----------------------------
7984 -- Has_Mutable_Components --
7985 ----------------------------
7987 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean is
7991 Comp
:= First_Component
(Typ
);
7992 while Present
(Comp
) loop
7993 if Is_Record_Type
(Etype
(Comp
))
7994 and then Has_Discriminants
(Etype
(Comp
))
7995 and then not Is_Constrained
(Etype
(Comp
))
8000 Next_Component
(Comp
);
8004 end Has_Mutable_Components
;
8006 ------------------------------
8007 -- Initialize_Discriminants --
8008 ------------------------------
8010 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
) is
8011 Loc
: constant Source_Ptr
:= Sloc
(N
);
8012 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
8013 Par
: constant Entity_Id
:= Etype
(Bas
);
8014 Decl
: constant Node_Id
:= Parent
(Par
);
8018 if Is_Tagged_Type
(Bas
)
8019 and then Is_Derived_Type
(Bas
)
8020 and then Has_Discriminants
(Par
)
8021 and then Has_Discriminants
(Bas
)
8022 and then Number_Discriminants
(Bas
) /= Number_Discriminants
(Par
)
8023 and then Nkind
(Decl
) = N_Full_Type_Declaration
8024 and then Nkind
(Type_Definition
(Decl
)) = N_Record_Definition
8026 Present
(Variant_Part
(Component_List
(Type_Definition
(Decl
))))
8027 and then Nkind
(N
) /= N_Extension_Aggregate
8030 -- Call init proc to set discriminants.
8031 -- There should eventually be a special procedure for this ???
8033 Ref
:= New_Occurrence_Of
(Defining_Identifier
(N
), Loc
);
8034 Insert_Actions_After
(N
,
8035 Build_Initialization_Call
(Sloc
(N
), Ref
, Typ
));
8037 end Initialize_Discriminants
;
8044 (Obj_Type
: Entity_Id
;
8045 Typ
: Entity_Id
) return Boolean
8047 L1
, L2
, H1
, H2
: Node_Id
;
8050 -- No sliding if the type of the object is not established yet, if it is
8051 -- an unconstrained type whose actual subtype comes from the aggregate,
8052 -- or if the two types are identical.
8054 if not Is_Array_Type
(Obj_Type
) then
8057 elsif not Is_Constrained
(Obj_Type
) then
8060 elsif Typ
= Obj_Type
then
8064 -- Sliding can only occur along the first dimension
8066 Get_Index_Bounds
(First_Index
(Typ
), L1
, H1
);
8067 Get_Index_Bounds
(First_Index
(Obj_Type
), L2
, H2
);
8069 if not Is_OK_Static_Expression
(L1
) or else
8070 not Is_OK_Static_Expression
(L2
) or else
8071 not Is_OK_Static_Expression
(H1
) or else
8072 not Is_OK_Static_Expression
(H2
)
8076 return Expr_Value
(L1
) /= Expr_Value
(L2
)
8078 Expr_Value
(H1
) /= Expr_Value
(H2
);
8083 ---------------------------------
8084 -- Process_Transient_Component --
8085 ---------------------------------
8087 procedure Process_Transient_Component
8089 Comp_Typ
: Entity_Id
;
8090 Init_Expr
: Node_Id
;
8091 Fin_Call
: out Node_Id
;
8092 Hook_Clear
: out Node_Id
;
8093 Aggr
: Node_Id
:= Empty
;
8094 Stmts
: List_Id
:= No_List
)
8096 procedure Add_Item
(Item
: Node_Id
);
8097 -- Insert arbitrary node Item into the tree depending on the values of
8104 procedure Add_Item
(Item
: Node_Id
) is
8106 if Present
(Aggr
) then
8107 Insert_Action
(Aggr
, Item
);
8109 pragma Assert
(Present
(Stmts
));
8110 Append_To
(Stmts
, Item
);
8116 Hook_Assign
: Node_Id
;
8117 Hook_Decl
: Node_Id
;
8121 Res_Typ
: Entity_Id
;
8123 -- Start of processing for Process_Transient_Component
8126 -- Add the access type, which provides a reference to the function
8127 -- result. Generate:
8129 -- type Res_Typ is access all Comp_Typ;
8131 Res_Typ
:= Make_Temporary
(Loc
, 'A');
8132 Set_Ekind
(Res_Typ
, E_General_Access_Type
);
8133 Set_Directly_Designated_Type
(Res_Typ
, Comp_Typ
);
8136 (Make_Full_Type_Declaration
(Loc
,
8137 Defining_Identifier
=> Res_Typ
,
8139 Make_Access_To_Object_Definition
(Loc
,
8140 All_Present
=> True,
8141 Subtype_Indication
=> New_Occurrence_Of
(Comp_Typ
, Loc
))));
8143 -- Add the temporary which captures the result of the function call.
8146 -- Res : constant Res_Typ := Init_Expr'Reference;
8148 -- Note that this temporary is effectively a transient object because
8149 -- its lifetime is bounded by the current array or record component.
8151 Res_Id
:= Make_Temporary
(Loc
, 'R');
8152 Set_Ekind
(Res_Id
, E_Constant
);
8153 Set_Etype
(Res_Id
, Res_Typ
);
8155 -- Mark the transient object as successfully processed to avoid double
8158 Set_Is_Finalized_Transient
(Res_Id
);
8160 -- Signal the general finalization machinery that this transient object
8161 -- should not be considered for finalization actions because its cleanup
8162 -- will be performed by Process_Transient_Component_Completion.
8164 Set_Is_Ignored_Transient
(Res_Id
);
8167 Make_Object_Declaration
(Loc
,
8168 Defining_Identifier
=> Res_Id
,
8169 Constant_Present
=> True,
8170 Object_Definition
=> New_Occurrence_Of
(Res_Typ
, Loc
),
8172 Make_Reference
(Loc
, New_Copy_Tree
(Init_Expr
)));
8174 Add_Item
(Res_Decl
);
8176 -- Construct all pieces necessary to hook and finalize the transient
8179 Build_Transient_Object_Statements
8180 (Obj_Decl
=> Res_Decl
,
8181 Fin_Call
=> Fin_Call
,
8182 Hook_Assign
=> Hook_Assign
,
8183 Hook_Clear
=> Hook_Clear
,
8184 Hook_Decl
=> Hook_Decl
,
8185 Ptr_Decl
=> Ptr_Decl
);
8187 -- Add the access type which provides a reference to the transient
8188 -- result. Generate:
8190 -- type Ptr_Typ is access all Comp_Typ;
8192 Add_Item
(Ptr_Decl
);
8194 -- Add the temporary which acts as a hook to the transient result.
8197 -- Hook : Ptr_Typ := null;
8199 Add_Item
(Hook_Decl
);
8201 -- Attach the transient result to the hook. Generate:
8203 -- Hook := Ptr_Typ (Res);
8205 Add_Item
(Hook_Assign
);
8207 -- The original initialization expression now references the value of
8208 -- the temporary function result. Generate:
8213 Make_Explicit_Dereference
(Loc
,
8214 Prefix
=> New_Occurrence_Of
(Res_Id
, Loc
)));
8215 end Process_Transient_Component
;
8217 --------------------------------------------
8218 -- Process_Transient_Component_Completion --
8219 --------------------------------------------
8221 procedure Process_Transient_Component_Completion
8225 Hook_Clear
: Node_Id
;
8228 Exceptions_OK
: constant Boolean :=
8229 not Restriction_Active
(No_Exception_Propagation
);
8232 pragma Assert
(Present
(Hook_Clear
));
8234 -- Generate the following code if exception propagation is allowed:
8237 -- Abort : constant Boolean := Triggered_By_Abort;
8239 -- Abort : constant Boolean := False; -- no abort
8241 -- E : Exception_Occurrence;
8242 -- Raised : Boolean := False;
8249 -- [Deep_]Finalize (Res.all);
8253 -- if not Raised then
8255 -- Save_Occurrence (E,
8256 -- Get_Curent_Excep.all.all);
8262 -- if Raised and then not Abort then
8263 -- Raise_From_Controlled_Operation (E);
8267 if Exceptions_OK
then
8268 Abort_And_Exception
: declare
8269 Blk_Decls
: constant List_Id
:= New_List
;
8270 Blk_Stmts
: constant List_Id
:= New_List
;
8271 Fin_Stmts
: constant List_Id
:= New_List
;
8273 Fin_Data
: Finalization_Exception_Data
;
8276 -- Create the declarations of the two flags and the exception
8279 Build_Object_Declarations
(Fin_Data
, Blk_Decls
, Loc
);
8284 if Abort_Allowed
then
8285 Append_To
(Blk_Stmts
,
8286 Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
8289 -- Wrap the hook clear and the finalization call in order to trap
8290 -- a potential exception.
8292 Append_To
(Fin_Stmts
, Hook_Clear
);
8294 if Present
(Fin_Call
) then
8295 Append_To
(Fin_Stmts
, Fin_Call
);
8298 Append_To
(Blk_Stmts
,
8299 Make_Block_Statement
(Loc
,
8300 Handled_Statement_Sequence
=>
8301 Make_Handled_Sequence_Of_Statements
(Loc
,
8302 Statements
=> Fin_Stmts
,
8303 Exception_Handlers
=> New_List
(
8304 Build_Exception_Handler
(Fin_Data
)))));
8309 if Abort_Allowed
then
8310 Append_To
(Blk_Stmts
,
8311 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
8314 -- Reraise the potential exception with a proper "upgrade" to
8315 -- Program_Error if needed.
8317 Append_To
(Blk_Stmts
, Build_Raise_Statement
(Fin_Data
));
8319 -- Wrap everything in a block
8322 Make_Block_Statement
(Loc
,
8323 Declarations
=> Blk_Decls
,
8324 Handled_Statement_Sequence
=>
8325 Make_Handled_Sequence_Of_Statements
(Loc
,
8326 Statements
=> Blk_Stmts
)));
8327 end Abort_And_Exception
;
8329 -- Generate the following code if exception propagation is not allowed
8330 -- and aborts are allowed:
8335 -- [Deep_]Finalize (Res.all);
8337 -- Abort_Undefer_Direct;
8340 elsif Abort_Allowed
then
8341 Abort_Only
: declare
8342 Blk_Stmts
: constant List_Id
:= New_List
;
8345 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
8346 Append_To
(Blk_Stmts
, Hook_Clear
);
8348 if Present
(Fin_Call
) then
8349 Append_To
(Blk_Stmts
, Fin_Call
);
8353 Build_Abort_Undefer_Block
(Loc
,
8358 -- Otherwise generate:
8361 -- [Deep_]Finalize (Res.all);
8364 Append_To
(Stmts
, Hook_Clear
);
8366 if Present
(Fin_Call
) then
8367 Append_To
(Stmts
, Fin_Call
);
8370 end Process_Transient_Component_Completion
;
8372 ---------------------
8373 -- Sort_Case_Table --
8374 ---------------------
8376 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
8377 L
: constant Int
:= Case_Table
'First;
8378 U
: constant Int
:= Case_Table
'Last;
8386 T
:= Case_Table
(K
+ 1);
8390 and then Expr_Value
(Case_Table
(J
- 1).Choice_Lo
) >
8391 Expr_Value
(T
.Choice_Lo
)
8393 Case_Table
(J
) := Case_Table
(J
- 1);
8397 Case_Table
(J
) := T
;
8400 end Sort_Case_Table
;
8402 ----------------------------
8403 -- Static_Array_Aggregate --
8404 ----------------------------
8406 function Static_Array_Aggregate
(N
: Node_Id
) return Boolean is
8407 Bounds
: constant Node_Id
:= Aggregate_Bounds
(N
);
8409 Typ
: constant Entity_Id
:= Etype
(N
);
8410 Comp_Type
: constant Entity_Id
:= Component_Type
(Typ
);
8417 if Is_Tagged_Type
(Typ
)
8418 or else Is_Controlled
(Typ
)
8419 or else Is_Packed
(Typ
)
8425 and then Nkind
(Bounds
) = N_Range
8426 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
8427 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
8429 Lo
:= Low_Bound
(Bounds
);
8430 Hi
:= High_Bound
(Bounds
);
8432 if No
(Component_Associations
(N
)) then
8434 -- Verify that all components are static integers
8436 Expr
:= First
(Expressions
(N
));
8437 while Present
(Expr
) loop
8438 if Nkind
(Expr
) /= N_Integer_Literal
then
8448 -- We allow only a single named association, either a static
8449 -- range or an others_clause, with a static expression.
8451 Expr
:= First
(Component_Associations
(N
));
8453 if Present
(Expressions
(N
)) then
8456 elsif Present
(Next
(Expr
)) then
8459 elsif Present
(Next
(First
(Choice_List
(Expr
)))) then
8463 -- The aggregate is static if all components are literals,
8464 -- or else all its components are static aggregates for the
8465 -- component type. We also limit the size of a static aggregate
8466 -- to prevent runaway static expressions.
8468 if Is_Array_Type
(Comp_Type
)
8469 or else Is_Record_Type
(Comp_Type
)
8471 if Nkind
(Expression
(Expr
)) /= N_Aggregate
8473 not Compile_Time_Known_Aggregate
(Expression
(Expr
))
8478 elsif Nkind
(Expression
(Expr
)) /= N_Integer_Literal
then
8482 if not Aggr_Size_OK
(N
, Typ
) then
8486 -- Create a positional aggregate with the right number of
8487 -- copies of the expression.
8489 Agg
:= Make_Aggregate
(Sloc
(N
), New_List
, No_List
);
8491 for I
in UI_To_Int
(Intval
(Lo
)) .. UI_To_Int
(Intval
(Hi
))
8493 Append_To
(Expressions
(Agg
), New_Copy
(Expression
(Expr
)));
8495 -- The copied expression must be analyzed and resolved.
8496 -- Besides setting the type, this ensures that static
8497 -- expressions are appropriately marked as such.
8500 (Last
(Expressions
(Agg
)), Component_Type
(Typ
));
8503 Set_Aggregate_Bounds
(Agg
, Bounds
);
8504 Set_Etype
(Agg
, Typ
);
8507 Set_Compile_Time_Known_Aggregate
(N
);
8516 end Static_Array_Aggregate
;
8518 ----------------------------------
8519 -- Two_Dim_Packed_Array_Handled --
8520 ----------------------------------
8522 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean is
8523 Loc
: constant Source_Ptr
:= Sloc
(N
);
8524 Typ
: constant Entity_Id
:= Etype
(N
);
8525 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
8526 Comp_Size
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
8527 Packed_Array
: constant Entity_Id
:=
8528 Packed_Array_Impl_Type
(Base_Type
(Typ
));
8531 -- Expression in original aggregate
8534 -- One-dimensional subaggregate
8538 -- For now, only deal with cases where an integral number of elements
8539 -- fit in a single byte. This includes the most common boolean case.
8541 if not (Comp_Size
= 1 or else
8542 Comp_Size
= 2 or else
8548 Convert_To_Positional
8549 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
8551 -- Verify that all components are static
8553 if Nkind
(N
) = N_Aggregate
8554 and then Compile_Time_Known_Aggregate
(N
)
8558 -- The aggregate may have been reanalyzed and converted already
8560 elsif Nkind
(N
) /= N_Aggregate
then
8563 -- If component associations remain, the aggregate is not static
8565 elsif Present
(Component_Associations
(N
)) then
8569 One_Dim
:= First
(Expressions
(N
));
8570 while Present
(One_Dim
) loop
8571 if Present
(Component_Associations
(One_Dim
)) then
8575 One_Comp
:= First
(Expressions
(One_Dim
));
8576 while Present
(One_Comp
) loop
8577 if not Is_OK_Static_Expression
(One_Comp
) then
8588 -- Two-dimensional aggregate is now fully positional so pack one
8589 -- dimension to create a static one-dimensional array, and rewrite
8590 -- as an unchecked conversion to the original type.
8593 Byte_Size
: constant Int
:= UI_To_Int
(Component_Size
(Packed_Array
));
8594 -- The packed array type is a byte array
8597 -- Number of components accumulated in current byte
8600 -- Assembled list of packed values for equivalent aggregate
8603 -- Integer value of component
8606 -- Step size for packing
8609 -- Endian-dependent start position for packing
8612 -- Current insertion position
8615 -- Component of packed array being assembled
8622 -- Account for endianness. See corresponding comment in
8623 -- Packed_Array_Aggregate_Handled concerning the following.
8627 xor Reverse_Storage_Order
(Base_Type
(Typ
))
8629 Init_Shift
:= Byte_Size
- Comp_Size
;
8636 -- Iterate over each subaggregate
8638 Shift
:= Init_Shift
;
8639 One_Dim
:= First
(Expressions
(N
));
8640 while Present
(One_Dim
) loop
8641 One_Comp
:= First
(Expressions
(One_Dim
));
8642 while Present
(One_Comp
) loop
8643 if Packed_Num
= Byte_Size
/ Comp_Size
then
8645 -- Byte is complete, add to list of expressions
8647 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
8649 Shift
:= Init_Shift
;
8653 Comp_Val
:= Expr_Rep_Value
(One_Comp
);
8655 -- Adjust for bias, and strip proper number of bits
8657 if Has_Biased_Representation
(Ctyp
) then
8658 Comp_Val
:= Comp_Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
8661 Comp_Val
:= Comp_Val
mod Uint_2
** Comp_Size
;
8662 Val
:= UI_To_Int
(Val
+ Comp_Val
* Uint_2
** Shift
);
8663 Shift
:= Shift
+ Incr
;
8664 One_Comp
:= Next
(One_Comp
);
8665 Packed_Num
:= Packed_Num
+ 1;
8669 One_Dim
:= Next
(One_Dim
);
8672 if Packed_Num
> 0 then
8674 -- Add final incomplete byte if present
8676 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
8680 Unchecked_Convert_To
(Typ
,
8681 Make_Qualified_Expression
(Loc
,
8682 Subtype_Mark
=> New_Occurrence_Of
(Packed_Array
, Loc
),
8683 Expression
=> Make_Aggregate
(Loc
, Expressions
=> Comps
))));
8684 Analyze_And_Resolve
(N
);
8687 end Two_Dim_Packed_Array_Handled
;