1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, 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 Targparm
; use Targparm
;
63 with Tbuild
; use Tbuild
;
64 with Uintp
; use Uintp
;
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 Build_Record_Aggr_Code
181 Lhs
: Node_Id
) return List_Id
;
182 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
183 -- aggregate. Target is an expression containing the location on which the
184 -- component by component assignments will take place. Returns the list of
185 -- assignments plus all other adjustments needed for tagged and controlled
188 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
);
189 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
190 -- aggregate (which can only be a record type, this procedure is only used
191 -- for record types). Transform the given aggregate into a sequence of
192 -- assignments performed component by component.
194 procedure Expand_Record_Aggregate
196 Orig_Tag
: Node_Id
:= Empty
;
197 Parent_Expr
: Node_Id
:= Empty
);
198 -- This is the top level procedure for record aggregate expansion.
199 -- Expansion for record aggregates needs expand aggregates for tagged
200 -- record types. Specifically Expand_Record_Aggregate adds the Tag
201 -- field in front of the Component_Association list that was created
202 -- during resolution by Resolve_Record_Aggregate.
204 -- N is the record aggregate node.
205 -- Orig_Tag is the value of the Tag that has to be provided for this
206 -- specific aggregate. It carries the tag corresponding to the type
207 -- of the outermost aggregate during the recursive expansion
208 -- Parent_Expr is the ancestor part of the original extension
211 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean;
212 -- Return true if one of the components is of a discriminated type with
213 -- defaults. An aggregate for a type with mutable components must be
214 -- expanded into individual assignments.
216 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
);
217 -- If the type of the aggregate is a type extension with renamed discrimi-
218 -- nants, we must initialize the hidden discriminants of the parent.
219 -- Otherwise, the target object must not be initialized. The discriminants
220 -- are initialized by calling the initialization procedure for the type.
221 -- This is incorrect if the initialization of other components has any
222 -- side effects. We restrict this call to the case where the parent type
223 -- has a variant part, because this is the only case where the hidden
224 -- discriminants are accessed, namely when calling discriminant checking
225 -- functions of the parent type, and when applying a stream attribute to
226 -- an object of the derived type.
228 -----------------------------------------------------
229 -- Local Subprograms for Array Aggregate Expansion --
230 -----------------------------------------------------
232 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean;
233 -- Very large static aggregates present problems to the back-end, and are
234 -- transformed into assignments and loops. This function verifies that the
235 -- total number of components of an aggregate is acceptable for rewriting
236 -- into a purely positional static form. Aggr_Size_OK must be called before
239 -- This function also detects and warns about one-component aggregates that
240 -- appear in a non-static context. Even if the component value is static,
241 -- such an aggregate must be expanded into an assignment.
243 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean;
244 -- This function checks if array aggregate N can be processed directly
245 -- by the backend. If this is the case, True is returned.
247 function Build_Array_Aggr_Code
252 Scalar_Comp
: Boolean;
253 Indexes
: List_Id
:= No_List
) return List_Id
;
254 -- This recursive routine returns a list of statements containing the
255 -- loops and assignments that are needed for the expansion of the array
258 -- N is the (sub-)aggregate node to be expanded into code. This node has
259 -- been fully analyzed, and its Etype is properly set.
261 -- Index is the index node corresponding to the array subaggregate N
263 -- Into is the target expression into which we are copying the aggregate.
264 -- Note that this node may not have been analyzed yet, and so the Etype
265 -- field may not be set.
267 -- Scalar_Comp is True if the component type of the aggregate is scalar
269 -- Indexes is the current list of expressions used to index the object we
272 procedure Convert_Array_Aggr_In_Allocator
276 -- If the aggregate appears within an allocator and can be expanded in
277 -- place, this routine generates the individual assignments to components
278 -- of the designated object. This is an optimization over the general
279 -- case, where a temporary is first created on the stack and then used to
280 -- construct the allocated object on the heap.
282 procedure Convert_To_Positional
284 Max_Others_Replicate
: Nat
:= 5;
285 Handle_Bit_Packed
: Boolean := False);
286 -- If possible, convert named notation to positional notation. This
287 -- conversion is possible only in some static cases. If the conversion is
288 -- possible, then N is rewritten with the analyzed converted aggregate.
289 -- The parameter Max_Others_Replicate controls the maximum number of
290 -- values corresponding to an others choice that will be converted to
291 -- positional notation (the default of 5 is the normal limit, and reflects
292 -- the fact that normally the loop is better than a lot of separate
293 -- assignments). Note that this limit gets overridden in any case if
294 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
295 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
296 -- not expect the back end to handle bit packed arrays, so the normal case
297 -- of conversion is pointless), but in the special case of a call from
298 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
299 -- these are cases we handle in there.
301 -- It would seem useful to have a higher default for Max_Others_Replicate,
302 -- but aggregates in the compiler make this impossible: the compiler
303 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
306 procedure Expand_Array_Aggregate
(N
: Node_Id
);
307 -- This is the top-level routine to perform array aggregate expansion.
308 -- N is the N_Aggregate node to be expanded.
310 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean;
311 -- For two-dimensional packed aggregates with constant bounds and constant
312 -- components, it is preferable to pack the inner aggregates because the
313 -- whole matrix can then be presented to the back-end as a one-dimensional
314 -- list of literals. This is much more efficient than expanding into single
315 -- component assignments. This function determines if the type Typ is for
316 -- an array that is suitable for this optimization: it returns True if Typ
317 -- is a two dimensional bit packed array with component size 1, 2, or 4.
319 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean;
320 -- Given an array aggregate, this function handles the case of a packed
321 -- array aggregate with all constant values, where the aggregate can be
322 -- evaluated at compile time. If this is possible, then N is rewritten
323 -- to be its proper compile time value with all the components properly
324 -- assembled. The expression is analyzed and resolved and True is returned.
325 -- If this transformation is not possible, N is unchanged and False is
328 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean;
329 -- If the type of the aggregate is a two-dimensional bit_packed array
330 -- it may be transformed into an array of bytes with constant values,
331 -- and presented to the back-end as a static value. The function returns
332 -- false if this transformation cannot be performed. THis is similar to,
333 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
339 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean is
348 -- Determines the maximum size of an array aggregate produced by
349 -- converting named to positional notation (e.g. from others clauses).
350 -- This avoids running away with attempts to convert huge aggregates,
351 -- which hit memory limits in the backend.
353 function Component_Count
(T
: Entity_Id
) return Nat
;
354 -- The limit is applied to the total number of subcomponents that the
355 -- aggregate will have, which is the number of static expressions
356 -- that will appear in the flattened array. This requires a recursive
357 -- computation of the number of scalar components of the structure.
359 ---------------------
360 -- Component_Count --
361 ---------------------
363 function Component_Count
(T
: Entity_Id
) return Nat
is
368 if Is_Scalar_Type
(T
) then
371 elsif Is_Record_Type
(T
) then
372 Comp
:= First_Component
(T
);
373 while Present
(Comp
) loop
374 Res
:= Res
+ Component_Count
(Etype
(Comp
));
375 Next_Component
(Comp
);
380 elsif Is_Array_Type
(T
) then
382 Lo
: constant Node_Id
:=
383 Type_Low_Bound
(Etype
(First_Index
(T
)));
384 Hi
: constant Node_Id
:=
385 Type_High_Bound
(Etype
(First_Index
(T
)));
387 Siz
: constant Nat
:= Component_Count
(Component_Type
(T
));
390 -- Check for superflat arrays, i.e. arrays with such bounds
391 -- as 4 .. 2, to insure that this function never returns a
392 -- meaningless negative value.
394 if not Compile_Time_Known_Value
(Lo
)
395 or else not Compile_Time_Known_Value
(Hi
)
396 or else Expr_Value
(Hi
) < Expr_Value
(Lo
)
401 -- If the number of components is greater than Int'Last,
402 -- then return Int'Last, so caller will return False (Aggr
403 -- size is not OK). Otherwise, UI_To_Int will crash.
406 UI
: constant Uint
:=
407 Expr_Value
(Hi
) - Expr_Value
(Lo
) + 1;
409 if UI_Is_In_Int_Range
(UI
) then
410 return Siz
* UI_To_Int
(UI
);
419 -- Can only be a null for an access type
425 -- Start of processing for Aggr_Size_OK
428 -- The normal aggregate limit is 50000, but we increase this limit to
429 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
430 -- Restrictions (No_Implicit_Loops) is specified, since in either case
431 -- we are at risk of declaring the program illegal because of this
432 -- limit. We also increase the limit when Static_Elaboration_Desired,
433 -- given that this means that objects are intended to be placed in data
436 -- We also increase the limit if the aggregate is for a packed two-
437 -- dimensional array, because if components are static it is much more
438 -- efficient to construct a one-dimensional equivalent array with static
441 -- Conversely, we decrease the maximum size if none of the above
442 -- requirements apply, and if the aggregate has a single component
443 -- association, which will be more efficient if implemented with a loop.
445 -- Finally, we use a small limit in CodePeer mode where we favor loops
446 -- instead of thousands of single assignments (from large aggregates).
448 Max_Aggr_Size
:= 50000;
450 if CodePeer_Mode
then
451 Max_Aggr_Size
:= 100;
453 elsif Restriction_Active
(No_Elaboration_Code
)
454 or else Restriction_Active
(No_Implicit_Loops
)
455 or else Is_Two_Dim_Packed_Array
(Typ
)
456 or else (Ekind
(Current_Scope
) = E_Package
457 and then Static_Elaboration_Desired
(Current_Scope
))
459 Max_Aggr_Size
:= 2 ** 24;
461 elsif No
(Expressions
(N
))
462 and then No
(Next
(First
(Component_Associations
(N
))))
464 Max_Aggr_Size
:= 5000;
467 Siz
:= Component_Count
(Component_Type
(Typ
));
469 Indx
:= First_Index
(Typ
);
470 while Present
(Indx
) loop
471 Lo
:= Type_Low_Bound
(Etype
(Indx
));
472 Hi
:= Type_High_Bound
(Etype
(Indx
));
474 -- Bounds need to be known at compile time
476 if not Compile_Time_Known_Value
(Lo
)
477 or else not Compile_Time_Known_Value
(Hi
)
482 Lov
:= Expr_Value
(Lo
);
483 Hiv
:= Expr_Value
(Hi
);
485 -- A flat array is always safe
491 -- One-component aggregates are suspicious, and if the context type
492 -- is an object declaration with non-static bounds it will trip gcc;
493 -- such an aggregate must be expanded into a single assignment.
495 if Hiv
= Lov
and then Nkind
(Parent
(N
)) = N_Object_Declaration
then
497 Index_Type
: constant Entity_Id
:=
499 (First_Index
(Etype
(Defining_Identifier
(Parent
(N
)))));
503 if not Compile_Time_Known_Value
(Type_Low_Bound
(Index_Type
))
504 or else not Compile_Time_Known_Value
505 (Type_High_Bound
(Index_Type
))
507 if Present
(Component_Associations
(N
)) then
510 (Choice_List
(First
(Component_Associations
(N
))));
512 if Is_Entity_Name
(Indx
)
513 and then not Is_Type
(Entity
(Indx
))
516 ("single component aggregate in "
517 & "non-static context??", Indx
);
518 Error_Msg_N
("\maybe subtype name was meant??", Indx
);
528 Rng
: constant Uint
:= Hiv
- Lov
+ 1;
531 -- Check if size is too large
533 if not UI_Is_In_Int_Range
(Rng
) then
537 Siz
:= Siz
* UI_To_Int
(Rng
);
541 or else Siz
> Max_Aggr_Size
546 -- Bounds must be in integer range, for later array construction
548 if not UI_Is_In_Int_Range
(Lov
)
550 not UI_Is_In_Int_Range
(Hiv
)
561 ---------------------------------
562 -- Backend_Processing_Possible --
563 ---------------------------------
565 -- Backend processing by Gigi/gcc is possible only if all the following
566 -- conditions are met:
568 -- 1. N is fully positional
570 -- 2. N is not a bit-packed array aggregate;
572 -- 3. The size of N's array type must be known at compile time. Note
573 -- that this implies that the component size is also known
575 -- 4. The array type of N does not follow the Fortran layout convention
576 -- or if it does it must be 1 dimensional.
578 -- 5. The array component type may not be tagged (which could necessitate
579 -- reassignment of proper tags).
581 -- 6. The array component type must not have unaligned bit components
583 -- 7. None of the components of the aggregate may be bit unaligned
586 -- 8. There cannot be delayed components, since we do not know enough
587 -- at this stage to know if back end processing is possible.
589 -- 9. There cannot be any discriminated record components, since the
590 -- back end cannot handle this complex case.
592 -- 10. No controlled actions need to be generated for components
594 -- 11. When generating C code, N must be part of a N_Object_Declaration
596 -- 12. When generating C code, N must not include function calls
598 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean is
599 Typ
: constant Entity_Id
:= Etype
(N
);
600 -- Typ is the correct constrained array subtype of the aggregate
602 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean;
603 -- This routine checks components of aggregate N, enforcing checks
604 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
605 -- are performed on subaggregates. The Index value is the current index
606 -- being checked in the multidimensional case.
608 ---------------------
609 -- Component_Check --
610 ---------------------
612 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean is
613 function Ultimate_Original_Expression
(N
: Node_Id
) return Node_Id
;
614 -- Given a type conversion or an unchecked type conversion N, return
615 -- its innermost original expression.
617 ----------------------------------
618 -- Ultimate_Original_Expression --
619 ----------------------------------
621 function Ultimate_Original_Expression
(N
: Node_Id
) return Node_Id
is
622 Expr
: Node_Id
:= Original_Node
(N
);
625 while Nkind_In
(Expr
, N_Type_Conversion
,
626 N_Unchecked_Type_Conversion
)
628 Expr
:= Original_Node
(Expression
(Expr
));
632 end Ultimate_Original_Expression
;
638 -- Start of processing for Component_Check
641 -- Checks 1: (no component associations)
643 if Present
(Component_Associations
(N
)) then
647 -- Checks 11: (part of an object declaration)
650 and then Nkind
(Parent
(N
)) /= N_Object_Declaration
652 (Nkind
(Parent
(N
)) /= N_Qualified_Expression
653 or else Nkind
(Parent
(Parent
(N
))) /= N_Object_Declaration
)
658 -- Checks on components
660 -- Recurse to check subaggregates, which may appear in qualified
661 -- expressions. If delayed, the front-end will have to expand.
662 -- If the component is a discriminated record, treat as non-static,
663 -- as the back-end cannot handle this properly.
665 Expr
:= First
(Expressions
(N
));
666 while Present
(Expr
) loop
668 -- Checks 8: (no delayed components)
670 if Is_Delayed_Aggregate
(Expr
) then
674 -- Checks 9: (no discriminated records)
676 if Present
(Etype
(Expr
))
677 and then Is_Record_Type
(Etype
(Expr
))
678 and then Has_Discriminants
(Etype
(Expr
))
683 -- Checks 7. Component must not be bit aligned component
685 if Possible_Bit_Aligned_Component
(Expr
) then
689 -- Checks 12: (no function call)
693 Nkind
(Ultimate_Original_Expression
(Expr
)) = N_Function_Call
698 -- Recursion to following indexes for multiple dimension case
700 if Present
(Next_Index
(Index
))
701 and then not Component_Check
(Expr
, Next_Index
(Index
))
706 -- All checks for that component finished, on to next
714 -- Start of processing for Backend_Processing_Possible
717 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
719 if Is_Bit_Packed_Array
(Typ
) or else Needs_Finalization
(Typ
) then
723 -- If component is limited, aggregate must be expanded because each
724 -- component assignment must be built in place.
726 if Is_Limited_View
(Component_Type
(Typ
)) then
730 -- Checks 4 (array must not be multidimensional Fortran case)
732 if Convention
(Typ
) = Convention_Fortran
733 and then Number_Dimensions
(Typ
) > 1
738 -- Checks 3 (size of array must be known at compile time)
740 if not Size_Known_At_Compile_Time
(Typ
) then
744 -- Checks on components
746 if not Component_Check
(N
, First_Index
(Typ
)) then
750 -- Checks 5 (if the component type is tagged, then we may need to do
751 -- tag adjustments. Perhaps this should be refined to check for any
752 -- component associations that actually need tag adjustment, similar
753 -- to the test in Component_Not_OK_For_Backend for record aggregates
754 -- with tagged components, but not clear whether it's worthwhile ???;
755 -- in the case of virtual machines (no Tagged_Type_Expansion), object
756 -- tags are handled implicitly).
758 if Is_Tagged_Type
(Component_Type
(Typ
))
759 and then Tagged_Type_Expansion
764 -- Checks 6 (component type must not have bit aligned components)
766 if Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
)) then
770 -- Backend processing is possible
772 Set_Size_Known_At_Compile_Time
(Etype
(N
), True);
774 end Backend_Processing_Possible
;
776 ---------------------------
777 -- Build_Array_Aggr_Code --
778 ---------------------------
780 -- The code that we generate from a one dimensional aggregate is
782 -- 1. If the subaggregate contains discrete choices we
784 -- (a) Sort the discrete choices
786 -- (b) Otherwise for each discrete choice that specifies a range we
787 -- emit a loop. If a range specifies a maximum of three values, or
788 -- we are dealing with an expression we emit a sequence of
789 -- assignments instead of a loop.
791 -- (c) Generate the remaining loops to cover the others choice if any
793 -- 2. If the aggregate contains positional elements we
795 -- (a) translate the positional elements in a series of assignments
797 -- (b) Generate a final loop to cover the others choice if any.
798 -- Note that this final loop has to be a while loop since the case
800 -- L : Integer := Integer'Last;
801 -- H : Integer := Integer'Last;
802 -- A : array (L .. H) := (1, others =>0);
804 -- cannot be handled by a for loop. Thus for the following
806 -- array (L .. H) := (.. positional elements.., others =>E);
808 -- we always generate something like:
810 -- J : Index_Type := Index_Of_Last_Positional_Element;
812 -- J := Index_Base'Succ (J)
816 function Build_Array_Aggr_Code
821 Scalar_Comp
: Boolean;
822 Indexes
: List_Id
:= No_List
) return List_Id
824 Loc
: constant Source_Ptr
:= Sloc
(N
);
825 Index_Base
: constant Entity_Id
:= Base_Type
(Etype
(Index
));
826 Index_Base_L
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
827 Index_Base_H
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
829 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
;
830 -- Returns an expression where Val is added to expression To, unless
831 -- To+Val is provably out of To's base type range. To must be an
832 -- already analyzed expression.
834 function Empty_Range
(L
, H
: Node_Id
) return Boolean;
835 -- Returns True if the range defined by L .. H is certainly empty
837 function Equal
(L
, H
: Node_Id
) return Boolean;
838 -- Returns True if L = H for sure
840 function Index_Base_Name
return Node_Id
;
841 -- Returns a new reference to the index type name
846 In_Loop
: Boolean := False) return List_Id
;
847 -- Ind must be a side-effect-free expression. If the input aggregate N
848 -- to Build_Loop contains no subaggregates, then this function returns
849 -- the assignment statement:
851 -- Into (Indexes, Ind) := Expr;
853 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
854 -- when the assignment appears within a generated loop.
856 -- Ada 2005 (AI-287): In case of default initialized component, Expr
857 -- is empty and we generate a call to the corresponding IP subprogram.
859 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
860 -- Nodes L and H must be side-effect-free expressions. If the input
861 -- aggregate N to Build_Loop contains no subaggregates, this routine
862 -- returns the for loop statement:
864 -- for J in Index_Base'(L) .. Index_Base'(H) loop
865 -- Into (Indexes, J) := Expr;
868 -- Otherwise we call Build_Code recursively. As an optimization if the
869 -- loop covers 3 or fewer scalar elements we generate a sequence of
871 -- If the component association that generates the loop comes from an
872 -- Iterated_Component_Association, the loop parameter has the name of
873 -- the corresponding parameter in the original construct.
875 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
876 -- Nodes L and H must be side-effect-free expressions. If the input
877 -- aggregate N to Build_Loop contains no subaggregates, this routine
878 -- returns the while loop statement:
880 -- J : Index_Base := L;
882 -- J := Index_Base'Succ (J);
883 -- Into (Indexes, J) := Expr;
886 -- Otherwise we call Build_Code recursively
888 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
;
889 -- For an association with a box, use value given by aspect
890 -- Default_Component_Value of array type if specified, else use
891 -- value given by aspect Default_Value for component type itself
892 -- if specified, else return Empty.
894 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean;
895 function Local_Expr_Value
(E
: Node_Id
) return Uint
;
896 -- These two Local routines are used to replace the corresponding ones
897 -- in sem_eval because while processing the bounds of an aggregate with
898 -- discrete choices whose index type is an enumeration, we build static
899 -- expressions not recognized by Compile_Time_Known_Value as such since
900 -- they have not yet been analyzed and resolved. All the expressions in
901 -- question are things like Index_Base_Name'Val (Const) which we can
902 -- easily recognize as being constant.
908 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
is
913 U_Val
: constant Uint
:= UI_From_Int
(Val
);
916 -- Note: do not try to optimize the case of Val = 0, because
917 -- we need to build a new node with the proper Sloc value anyway.
919 -- First test if we can do constant folding
921 if Local_Compile_Time_Known_Value
(To
) then
922 U_To
:= Local_Expr_Value
(To
) + Val
;
924 -- Determine if our constant is outside the range of the index.
925 -- If so return an Empty node. This empty node will be caught
926 -- by Empty_Range below.
928 if Compile_Time_Known_Value
(Index_Base_L
)
929 and then U_To
< Expr_Value
(Index_Base_L
)
933 elsif Compile_Time_Known_Value
(Index_Base_H
)
934 and then U_To
> Expr_Value
(Index_Base_H
)
939 Expr_Pos
:= Make_Integer_Literal
(Loc
, U_To
);
940 Set_Is_Static_Expression
(Expr_Pos
);
942 if not Is_Enumeration_Type
(Index_Base
) then
945 -- If we are dealing with enumeration return
946 -- Index_Base'Val (Expr_Pos)
950 Make_Attribute_Reference
952 Prefix
=> Index_Base_Name
,
953 Attribute_Name
=> Name_Val
,
954 Expressions
=> New_List
(Expr_Pos
));
960 -- If we are here no constant folding possible
962 if not Is_Enumeration_Type
(Index_Base
) then
965 Left_Opnd
=> Duplicate_Subexpr
(To
),
966 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
968 -- If we are dealing with enumeration return
969 -- Index_Base'Val (Index_Base'Pos (To) + Val)
973 Make_Attribute_Reference
975 Prefix
=> Index_Base_Name
,
976 Attribute_Name
=> Name_Pos
,
977 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
982 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
985 Make_Attribute_Reference
987 Prefix
=> Index_Base_Name
,
988 Attribute_Name
=> Name_Val
,
989 Expressions
=> New_List
(Expr_Pos
));
999 function Empty_Range
(L
, H
: Node_Id
) return Boolean is
1000 Is_Empty
: Boolean := False;
1005 -- First check if L or H were already detected as overflowing the
1006 -- index base range type by function Add above. If this is so Add
1007 -- returns the empty node.
1009 if No
(L
) or else No
(H
) then
1013 for J
in 1 .. 3 loop
1016 -- L > H range is empty
1022 -- B_L > H range must be empty
1025 Low
:= Index_Base_L
;
1028 -- L > B_H range must be empty
1032 High
:= Index_Base_H
;
1035 if Local_Compile_Time_Known_Value
(Low
)
1037 Local_Compile_Time_Known_Value
(High
)
1040 UI_Gt
(Local_Expr_Value
(Low
), Local_Expr_Value
(High
));
1053 function Equal
(L
, H
: Node_Id
) return Boolean is
1058 elsif Local_Compile_Time_Known_Value
(L
)
1060 Local_Compile_Time_Known_Value
(H
)
1062 return UI_Eq
(Local_Expr_Value
(L
), Local_Expr_Value
(H
));
1075 In_Loop
: Boolean := False) return List_Id
1077 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
;
1078 -- Collect insert_actions generated in the construction of a loop,
1079 -- and prepend them to the sequence of assignments to complete the
1080 -- eventual body of the loop.
1082 procedure Initialize_Array_Component
1083 (Arr_Comp
: Node_Id
;
1085 Init_Expr
: Node_Id
;
1087 -- Perform the initialization of array component Arr_Comp with
1088 -- expected type Comp_Typ. Init_Expr denotes the initialization
1089 -- expression of the array component. All generated code is added
1092 procedure Initialize_Ctrl_Array_Component
1093 (Arr_Comp
: Node_Id
;
1094 Comp_Typ
: Entity_Id
;
1095 Init_Expr
: Node_Id
;
1097 -- Perform the initialization of array component Arr_Comp when its
1098 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1099 -- the initialization expression of the array component. All hook-
1100 -- related declarations are inserted prior to aggregate N. Remaining
1101 -- code is added to list Stmts.
1103 ----------------------
1104 -- Add_Loop_Actions --
1105 ----------------------
1107 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
is
1111 -- Ada 2005 (AI-287): Do nothing else in case of default
1112 -- initialized component.
1117 elsif Nkind
(Parent
(Expr
)) = N_Component_Association
1118 and then Present
(Loop_Actions
(Parent
(Expr
)))
1120 Append_List
(Lis
, Loop_Actions
(Parent
(Expr
)));
1121 Res
:= Loop_Actions
(Parent
(Expr
));
1122 Set_Loop_Actions
(Parent
(Expr
), No_List
);
1128 end Add_Loop_Actions
;
1130 --------------------------------
1131 -- Initialize_Array_Component --
1132 --------------------------------
1134 procedure Initialize_Array_Component
1135 (Arr_Comp
: Node_Id
;
1137 Init_Expr
: Node_Id
;
1140 Exceptions_OK
: constant Boolean :=
1141 not Restriction_Active
1142 (No_Exception_Propagation
);
1144 Finalization_OK
: constant Boolean :=
1146 and then Needs_Finalization
(Comp_Typ
);
1148 Full_Typ
: constant Entity_Id
:= Underlying_Type
(Comp_Typ
);
1150 Blk_Stmts
: List_Id
;
1151 Init_Stmt
: Node_Id
;
1154 -- Protect the initialization statements from aborts. Generate:
1158 if Finalization_OK
and Abort_Allowed
then
1159 if Exceptions_OK
then
1160 Blk_Stmts
:= New_List
;
1165 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
1167 -- Otherwise aborts are not allowed. All generated code is added
1168 -- directly to the input list.
1174 -- Initialize the array element. Generate:
1176 -- Arr_Comp := Init_Expr;
1178 -- Note that the initialization expression is replicated because
1179 -- it has to be reevaluated within a generated loop.
1182 Make_OK_Assignment_Statement
(Loc
,
1183 Name
=> New_Copy_Tree
(Arr_Comp
),
1184 Expression
=> New_Copy_Tree
(Init_Expr
));
1185 Set_No_Ctrl_Actions
(Init_Stmt
);
1187 -- If this is an aggregate for an array of arrays, each
1188 -- subaggregate will be expanded as well, and even with
1189 -- No_Ctrl_Actions the assignments of inner components will
1190 -- require attachment in their assignments to temporaries. These
1191 -- temporaries must be finalized for each subaggregate. Generate:
1194 -- Arr_Comp := Init_Expr;
1197 if Finalization_OK
and then Is_Array_Type
(Comp_Typ
) then
1199 Make_Block_Statement
(Loc
,
1200 Handled_Statement_Sequence
=>
1201 Make_Handled_Sequence_Of_Statements
(Loc
,
1202 Statements
=> New_List
(Init_Stmt
)));
1205 Append_To
(Blk_Stmts
, Init_Stmt
);
1207 -- Adjust the tag due to a possible view conversion. Generate:
1209 -- Arr_Comp._tag := Full_TypP;
1211 if Tagged_Type_Expansion
1212 and then Present
(Comp_Typ
)
1213 and then Is_Tagged_Type
(Comp_Typ
)
1215 Append_To
(Blk_Stmts
,
1216 Make_OK_Assignment_Statement
(Loc
,
1218 Make_Selected_Component
(Loc
,
1219 Prefix
=> New_Copy_Tree
(Arr_Comp
),
1222 (First_Tag_Component
(Full_Typ
), Loc
)),
1225 Unchecked_Convert_To
(RTE
(RE_Tag
),
1227 (Node
(First_Elmt
(Access_Disp_Table
(Full_Typ
))),
1231 -- Adjust the array component. Controlled subaggregates are not
1232 -- considered because each of their individual elements will
1233 -- receive an adjustment of its own. Generate:
1235 -- [Deep_]Adjust (Arr_Comp);
1238 and then not Is_Limited_Type
(Comp_Typ
)
1240 (Is_Array_Type
(Comp_Typ
)
1241 and then Is_Controlled
(Component_Type
(Comp_Typ
))
1242 and then Nkind
(Expr
) = N_Aggregate
)
1246 (Obj_Ref
=> New_Copy_Tree
(Arr_Comp
),
1249 -- Guard against a missing [Deep_]Adjust when the component
1250 -- type was not frozen properly.
1252 if Present
(Adj_Call
) then
1253 Append_To
(Blk_Stmts
, Adj_Call
);
1257 -- Complete the protection of the initialization statements
1259 if Finalization_OK
and Abort_Allowed
then
1261 -- Wrap the initialization statements in a block to catch a
1262 -- potential exception. Generate:
1266 -- Arr_Comp := Init_Expr;
1267 -- Arr_Comp._tag := Full_TypP;
1268 -- [Deep_]Adjust (Arr_Comp);
1270 -- Abort_Undefer_Direct;
1273 if Exceptions_OK
then
1275 Build_Abort_Undefer_Block
(Loc
,
1279 -- Otherwise exceptions are not propagated. Generate:
1282 -- Arr_Comp := Init_Expr;
1283 -- Arr_Comp._tag := Full_TypP;
1284 -- [Deep_]Adjust (Arr_Comp);
1288 Append_To
(Blk_Stmts
,
1289 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
1292 end Initialize_Array_Component
;
1294 -------------------------------------
1295 -- Initialize_Ctrl_Array_Component --
1296 -------------------------------------
1298 procedure Initialize_Ctrl_Array_Component
1299 (Arr_Comp
: Node_Id
;
1300 Comp_Typ
: Entity_Id
;
1301 Init_Expr
: Node_Id
;
1305 Act_Stmts
: List_Id
;
1308 Hook_Clear
: Node_Id
;
1310 In_Place_Expansion
: Boolean;
1311 -- Flag set when a nonlimited controlled function call requires
1312 -- in-place expansion.
1315 -- Duplicate the initialization expression in case the context is
1316 -- a multi choice list or an "others" choice which plugs various
1317 -- holes in the aggregate. As a result the expression is no longer
1318 -- shared between the various components and is reevaluated for
1319 -- each such component.
1321 Expr
:= New_Copy_Tree
(Init_Expr
);
1322 Set_Parent
(Expr
, Parent
(Init_Expr
));
1324 -- Perform a preliminary analysis and resolution to determine what
1325 -- the initialization expression denotes. An unanalyzed function
1326 -- call may appear as an identifier or an indexed component.
1328 if Nkind_In
(Expr
, N_Function_Call
,
1330 N_Indexed_Component
)
1331 and then not Analyzed
(Expr
)
1333 Preanalyze_And_Resolve
(Expr
, Comp_Typ
);
1336 In_Place_Expansion
:=
1337 Nkind
(Expr
) = N_Function_Call
1338 and then not Is_Limited_Type
(Comp_Typ
);
1340 -- The initialization expression is a controlled function call.
1341 -- Perform in-place removal of side effects to avoid creating a
1342 -- transient scope, which leads to premature finalization.
1344 -- This in-place expansion is not performed for limited transient
1345 -- objects because the initialization is already done in-place.
1347 if In_Place_Expansion
then
1349 -- Suppress the removal of side effects by general analysis
1350 -- because this behavior is emulated here. This avoids the
1351 -- generation of a transient scope, which leads to out-of-order
1352 -- adjustment and finalization.
1354 Set_No_Side_Effect_Removal
(Expr
);
1356 -- When the transient component initialization is related to a
1357 -- range or an "others", keep all generated statements within
1358 -- the enclosing loop. This way the controlled function call
1359 -- will be evaluated at each iteration, and its result will be
1360 -- finalized at the end of each iteration.
1366 -- Otherwise this is a single component initialization. Hook-
1367 -- related statements are inserted prior to the aggregate.
1371 Act_Stmts
:= No_List
;
1374 -- Install all hook-related declarations and prepare the clean
1377 Process_Transient_Component
1379 Comp_Typ
=> Comp_Typ
,
1381 Fin_Call
=> Fin_Call
,
1382 Hook_Clear
=> Hook_Clear
,
1384 Stmts
=> Act_Stmts
);
1387 -- Use the noncontrolled component initialization circuitry to
1388 -- assign the result of the function call to the array element.
1389 -- This also performs subaggregate wrapping, tag adjustment, and
1390 -- [deep] adjustment of the array element.
1392 Initialize_Array_Component
1393 (Arr_Comp
=> Arr_Comp
,
1394 Comp_Typ
=> Comp_Typ
,
1398 -- At this point the array element is fully initialized. Complete
1399 -- the processing of the controlled array component by finalizing
1400 -- the transient function result.
1402 if In_Place_Expansion
then
1403 Process_Transient_Component_Completion
1406 Fin_Call
=> Fin_Call
,
1407 Hook_Clear
=> Hook_Clear
,
1410 end Initialize_Ctrl_Array_Component
;
1414 Stmts
: constant List_Id
:= New_List
;
1416 Comp_Typ
: Entity_Id
:= Empty
;
1418 Indexed_Comp
: Node_Id
;
1419 Init_Call
: Node_Id
;
1420 New_Indexes
: List_Id
;
1422 -- Start of processing for Gen_Assign
1425 if No
(Indexes
) then
1426 New_Indexes
:= New_List
;
1428 New_Indexes
:= New_Copy_List_Tree
(Indexes
);
1431 Append_To
(New_Indexes
, Ind
);
1433 if Present
(Next_Index
(Index
)) then
1436 Build_Array_Aggr_Code
1439 Index
=> Next_Index
(Index
),
1441 Scalar_Comp
=> Scalar_Comp
,
1442 Indexes
=> New_Indexes
));
1445 -- If we get here then we are at a bottom-level (sub-)aggregate
1449 (Make_Indexed_Component
(Loc
,
1450 Prefix
=> New_Copy_Tree
(Into
),
1451 Expressions
=> New_Indexes
));
1453 Set_Assignment_OK
(Indexed_Comp
);
1455 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1456 -- is not present (and therefore we also initialize Expr_Q to empty).
1460 elsif Nkind
(Expr
) = N_Qualified_Expression
then
1461 Expr_Q
:= Expression
(Expr
);
1466 if Present
(Etype
(N
)) and then Etype
(N
) /= Any_Composite
then
1467 Comp_Typ
:= Component_Type
(Etype
(N
));
1468 pragma Assert
(Comp_Typ
= Ctype
); -- AI-287
1470 elsif Present
(Next
(First
(New_Indexes
))) then
1472 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1473 -- component because we have received the component type in
1474 -- the formal parameter Ctype.
1476 -- ??? Some assert pragmas have been added to check if this new
1477 -- formal can be used to replace this code in all cases.
1479 if Present
(Expr
) then
1481 -- This is a multidimensional array. Recover the component type
1482 -- from the outermost aggregate, because subaggregates do not
1483 -- have an assigned type.
1490 while Present
(P
) loop
1491 if Nkind
(P
) = N_Aggregate
1492 and then Present
(Etype
(P
))
1494 Comp_Typ
:= Component_Type
(Etype
(P
));
1502 pragma Assert
(Comp_Typ
= Ctype
); -- AI-287
1507 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1508 -- default initialized components (otherwise Expr_Q is not present).
1511 and then Nkind_In
(Expr_Q
, N_Aggregate
, N_Extension_Aggregate
)
1513 -- At this stage the Expression may not have been analyzed yet
1514 -- because the array aggregate code has not been updated to use
1515 -- the Expansion_Delayed flag and avoid analysis altogether to
1516 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1517 -- the analysis of non-array aggregates now in order to get the
1518 -- value of Expansion_Delayed flag for the inner aggregate ???
1520 if Present
(Comp_Typ
) and then not Is_Array_Type
(Comp_Typ
) then
1521 Analyze_And_Resolve
(Expr_Q
, Comp_Typ
);
1524 if Is_Delayed_Aggregate
(Expr_Q
) then
1526 -- This is either a subaggregate of a multidimensional array,
1527 -- or a component of an array type whose component type is
1528 -- also an array. In the latter case, the expression may have
1529 -- component associations that provide different bounds from
1530 -- those of the component type, and sliding must occur. Instead
1531 -- of decomposing the current aggregate assignment, force the
1532 -- reanalysis of the assignment, so that a temporary will be
1533 -- generated in the usual fashion, and sliding will take place.
1535 if Nkind
(Parent
(N
)) = N_Assignment_Statement
1536 and then Is_Array_Type
(Comp_Typ
)
1537 and then Present
(Component_Associations
(Expr_Q
))
1538 and then Must_Slide
(Comp_Typ
, Etype
(Expr_Q
))
1540 Set_Expansion_Delayed
(Expr_Q
, False);
1541 Set_Analyzed
(Expr_Q
, False);
1546 Late_Expansion
(Expr_Q
, Etype
(Expr_Q
), Indexed_Comp
));
1551 if Present
(Expr
) then
1553 -- Handle an initialization expression of a controlled type in
1554 -- case it denotes a function call. In general such a scenario
1555 -- will produce a transient scope, but this will lead to wrong
1556 -- order of initialization, adjustment, and finalization in the
1557 -- context of aggregates.
1559 -- Target (1) := Ctrl_Func_Call;
1562 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1563 -- Target (1) := Trans_Obj;
1564 -- Finalize (Trans_Obj);
1566 -- Target (1)._tag := ...;
1567 -- Adjust (Target (1));
1569 -- In the example above, the call to Finalize occurs too early
1570 -- and as a result it may leave the array component in a bad
1571 -- state. Finalization of the transient object should really
1572 -- happen after adjustment.
1574 -- To avoid this scenario, perform in-place side-effect removal
1575 -- of the function call. This eliminates the transient property
1576 -- of the function result and ensures correct order of actions.
1578 -- Res : ... := Ctrl_Func_Call;
1579 -- Target (1) := Res;
1580 -- Target (1)._tag := ...;
1581 -- Adjust (Target (1));
1584 if Present
(Comp_Typ
)
1585 and then Needs_Finalization
(Comp_Typ
)
1586 and then Nkind
(Expr
) /= N_Aggregate
1588 Initialize_Ctrl_Array_Component
1589 (Arr_Comp
=> Indexed_Comp
,
1590 Comp_Typ
=> Comp_Typ
,
1594 -- Otherwise perform simple component initialization
1597 Initialize_Array_Component
1598 (Arr_Comp
=> Indexed_Comp
,
1599 Comp_Typ
=> Comp_Typ
,
1604 -- Ada 2005 (AI-287): In case of default initialized component, call
1605 -- the initialization subprogram associated with the component type.
1606 -- If the component type is an access type, add an explicit null
1607 -- assignment, because for the back-end there is an initialization
1608 -- present for the whole aggregate, and no default initialization
1611 -- In addition, if the component type is controlled, we must call
1612 -- its Initialize procedure explicitly, because there is no explicit
1613 -- object creation that will invoke it otherwise.
1616 if Present
(Base_Init_Proc
(Base_Type
(Ctype
)))
1617 or else Has_Task
(Base_Type
(Ctype
))
1619 Append_List_To
(Stmts
,
1620 Build_Initialization_Call
(Loc
,
1621 Id_Ref
=> Indexed_Comp
,
1623 With_Default_Init
=> True));
1625 -- If the component type has invariants, add an invariant
1626 -- check after the component is default-initialized. It will
1627 -- be analyzed and resolved before the code for initialization
1628 -- of other components.
1630 if Has_Invariants
(Ctype
) then
1631 Set_Etype
(Indexed_Comp
, Ctype
);
1632 Append_To
(Stmts
, Make_Invariant_Call
(Indexed_Comp
));
1635 elsif Is_Access_Type
(Ctype
) then
1637 Make_Assignment_Statement
(Loc
,
1638 Name
=> New_Copy_Tree
(Indexed_Comp
),
1639 Expression
=> Make_Null
(Loc
)));
1642 if Needs_Finalization
(Ctype
) then
1645 (Obj_Ref
=> New_Copy_Tree
(Indexed_Comp
),
1648 -- Guard against a missing [Deep_]Initialize when the component
1649 -- type was not properly frozen.
1651 if Present
(Init_Call
) then
1652 Append_To
(Stmts
, Init_Call
);
1657 return Add_Loop_Actions
(Stmts
);
1664 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1665 Is_Iterated_Component
: constant Boolean :=
1666 Nkind
(Parent
(Expr
)) = N_Iterated_Component_Association
;
1677 -- Index_Base'(L) .. Index_Base'(H)
1679 L_Iteration_Scheme
: Node_Id
;
1680 -- L_J in Index_Base'(L) .. Index_Base'(H)
1683 -- The statements to execute in the loop
1685 S
: constant List_Id
:= New_List
;
1686 -- List of statements
1689 -- Copy of expression tree, used for checking purposes
1692 -- If loop bounds define an empty range return the null statement
1694 if Empty_Range
(L
, H
) then
1695 Append_To
(S
, Make_Null_Statement
(Loc
));
1697 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1698 -- default initialized component.
1704 -- The expression must be type-checked even though no component
1705 -- of the aggregate will have this value. This is done only for
1706 -- actual components of the array, not for subaggregates. Do
1707 -- the check on a copy, because the expression may be shared
1708 -- among several choices, some of which might be non-null.
1710 if Present
(Etype
(N
))
1711 and then Is_Array_Type
(Etype
(N
))
1712 and then No
(Next_Index
(Index
))
1714 Expander_Mode_Save_And_Set
(False);
1715 Tcopy
:= New_Copy_Tree
(Expr
);
1716 Set_Parent
(Tcopy
, N
);
1717 Analyze_And_Resolve
(Tcopy
, Component_Type
(Etype
(N
)));
1718 Expander_Mode_Restore
;
1724 -- If loop bounds are the same then generate an assignment, unless
1725 -- the parent construct is an Iterated_Component_Association.
1727 elsif Equal
(L
, H
) and then not Is_Iterated_Component
then
1728 return Gen_Assign
(New_Copy_Tree
(L
), Expr
);
1730 -- If H - L <= 2 then generate a sequence of assignments when we are
1731 -- processing the bottom most aggregate and it contains scalar
1734 elsif No
(Next_Index
(Index
))
1735 and then Scalar_Comp
1736 and then Local_Compile_Time_Known_Value
(L
)
1737 and then Local_Compile_Time_Known_Value
(H
)
1738 and then Local_Expr_Value
(H
) - Local_Expr_Value
(L
) <= 2
1739 and then not Is_Iterated_Component
1741 Append_List_To
(S
, Gen_Assign
(New_Copy_Tree
(L
), Expr
));
1742 Append_List_To
(S
, Gen_Assign
(Add
(1, To
=> L
), Expr
));
1744 if Local_Expr_Value
(H
) - Local_Expr_Value
(L
) = 2 then
1745 Append_List_To
(S
, Gen_Assign
(Add
(2, To
=> L
), Expr
));
1751 -- Otherwise construct the loop, starting with the loop index L_J
1753 if Is_Iterated_Component
then
1755 Make_Defining_Identifier
(Loc
,
1756 Chars
=> (Chars
(Defining_Identifier
(Parent
(Expr
)))));
1759 L_J
:= Make_Temporary
(Loc
, 'J', L
);
1762 -- Construct "L .. H" in Index_Base. We use a qualified expression
1763 -- for the bound to convert to the index base, but we don't need
1764 -- to do that if we already have the base type at hand.
1766 if Etype
(L
) = Index_Base
then
1770 Make_Qualified_Expression
(Loc
,
1771 Subtype_Mark
=> Index_Base_Name
,
1772 Expression
=> New_Copy_Tree
(L
));
1775 if Etype
(H
) = Index_Base
then
1779 Make_Qualified_Expression
(Loc
,
1780 Subtype_Mark
=> Index_Base_Name
,
1781 Expression
=> New_Copy_Tree
(H
));
1789 -- Construct "for L_J in Index_Base range L .. H"
1791 L_Iteration_Scheme
:=
1792 Make_Iteration_Scheme
1794 Loop_Parameter_Specification
=>
1795 Make_Loop_Parameter_Specification
1797 Defining_Identifier
=> L_J
,
1798 Discrete_Subtype_Definition
=> L_Range
));
1800 -- Construct the statements to execute in the loop body
1803 Gen_Assign
(New_Occurrence_Of
(L_J
, Loc
), Expr
, In_Loop
=> True);
1805 -- Construct the final loop
1808 Make_Implicit_Loop_Statement
1810 Identifier
=> Empty
,
1811 Iteration_Scheme
=> L_Iteration_Scheme
,
1812 Statements
=> L_Body
));
1814 -- A small optimization: if the aggregate is initialized with a box
1815 -- and the component type has no initialization procedure, remove the
1816 -- useless empty loop.
1818 if Nkind
(First
(S
)) = N_Loop_Statement
1819 and then Is_Empty_List
(Statements
(First
(S
)))
1821 return New_List
(Make_Null_Statement
(Loc
));
1831 -- The code built is
1833 -- W_J : Index_Base := L;
1834 -- while W_J < H loop
1835 -- W_J := Index_Base'Succ (W);
1839 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1843 -- W_J : Base_Type := L;
1845 W_Iteration_Scheme
: Node_Id
;
1848 W_Index_Succ
: Node_Id
;
1849 -- Index_Base'Succ (J)
1851 W_Increment
: Node_Id
;
1852 -- W_J := Index_Base'Succ (W)
1854 W_Body
: constant List_Id
:= New_List
;
1855 -- The statements to execute in the loop
1857 S
: constant List_Id
:= New_List
;
1858 -- list of statement
1861 -- If loop bounds define an empty range or are equal return null
1863 if Empty_Range
(L
, H
) or else Equal
(L
, H
) then
1864 Append_To
(S
, Make_Null_Statement
(Loc
));
1868 -- Build the decl of W_J
1870 W_J
:= Make_Temporary
(Loc
, 'J', L
);
1872 Make_Object_Declaration
1874 Defining_Identifier
=> W_J
,
1875 Object_Definition
=> Index_Base_Name
,
1878 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1879 -- that in this particular case L is a fresh Expr generated by
1880 -- Add which we are the only ones to use.
1882 Append_To
(S
, W_Decl
);
1884 -- Construct " while W_J < H"
1886 W_Iteration_Scheme
:=
1887 Make_Iteration_Scheme
1889 Condition
=> Make_Op_Lt
1891 Left_Opnd
=> New_Occurrence_Of
(W_J
, Loc
),
1892 Right_Opnd
=> New_Copy_Tree
(H
)));
1894 -- Construct the statements to execute in the loop body
1897 Make_Attribute_Reference
1899 Prefix
=> Index_Base_Name
,
1900 Attribute_Name
=> Name_Succ
,
1901 Expressions
=> New_List
(New_Occurrence_Of
(W_J
, Loc
)));
1904 Make_OK_Assignment_Statement
1906 Name
=> New_Occurrence_Of
(W_J
, Loc
),
1907 Expression
=> W_Index_Succ
);
1909 Append_To
(W_Body
, W_Increment
);
1911 Append_List_To
(W_Body
,
1912 Gen_Assign
(New_Occurrence_Of
(W_J
, Loc
), Expr
, In_Loop
=> True));
1914 -- Construct the final loop
1917 Make_Implicit_Loop_Statement
1919 Identifier
=> Empty
,
1920 Iteration_Scheme
=> W_Iteration_Scheme
,
1921 Statements
=> W_Body
));
1926 --------------------
1927 -- Get_Assoc_Expr --
1928 --------------------
1930 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
is
1931 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
1934 if Box_Present
(Assoc
) then
1935 if Is_Scalar_Type
(Ctype
) then
1936 if Present
(Default_Aspect_Component_Value
(Typ
)) then
1937 return Default_Aspect_Component_Value
(Typ
);
1938 elsif Present
(Default_Aspect_Value
(Ctype
)) then
1939 return Default_Aspect_Value
(Ctype
);
1949 return Expression
(Assoc
);
1953 ---------------------
1954 -- Index_Base_Name --
1955 ---------------------
1957 function Index_Base_Name
return Node_Id
is
1959 return New_Occurrence_Of
(Index_Base
, Sloc
(N
));
1960 end Index_Base_Name
;
1962 ------------------------------------
1963 -- Local_Compile_Time_Known_Value --
1964 ------------------------------------
1966 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean is
1968 return Compile_Time_Known_Value
(E
)
1970 (Nkind
(E
) = N_Attribute_Reference
1971 and then Attribute_Name
(E
) = Name_Val
1972 and then Compile_Time_Known_Value
(First
(Expressions
(E
))));
1973 end Local_Compile_Time_Known_Value
;
1975 ----------------------
1976 -- Local_Expr_Value --
1977 ----------------------
1979 function Local_Expr_Value
(E
: Node_Id
) return Uint
is
1981 if Compile_Time_Known_Value
(E
) then
1982 return Expr_Value
(E
);
1984 return Expr_Value
(First
(Expressions
(E
)));
1986 end Local_Expr_Value
;
1990 New_Code
: constant List_Id
:= New_List
;
1992 Aggr_L
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(N
));
1993 Aggr_H
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(N
));
1994 -- The aggregate bounds of this specific subaggregate. Note that if the
1995 -- code generated by Build_Array_Aggr_Code is executed then these bounds
1996 -- are OK. Otherwise a Constraint_Error would have been raised.
1998 Aggr_Low
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_L
);
1999 Aggr_High
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_H
);
2000 -- After Duplicate_Subexpr these are side-effect free
2009 Nb_Choices
: Nat
:= 0;
2010 Table
: Case_Table_Type
(1 .. Number_Of_Choices
(N
));
2011 -- Used to sort all the different choice values
2014 -- Number of elements in the positional aggregate
2016 Others_Assoc
: Node_Id
:= Empty
;
2018 -- Start of processing for Build_Array_Aggr_Code
2021 -- First before we start, a special case. if we have a bit packed
2022 -- array represented as a modular type, then clear the value to
2023 -- zero first, to ensure that unused bits are properly cleared.
2028 and then Is_Bit_Packed_Array
(Typ
)
2029 and then Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
))
2031 Append_To
(New_Code
,
2032 Make_Assignment_Statement
(Loc
,
2033 Name
=> New_Copy_Tree
(Into
),
2035 Unchecked_Convert_To
(Typ
,
2036 Make_Integer_Literal
(Loc
, Uint_0
))));
2039 -- If the component type contains tasks, we need to build a Master
2040 -- entity in the current scope, because it will be needed if build-
2041 -- in-place functions are called in the expanded code.
2043 if Nkind
(Parent
(N
)) = N_Object_Declaration
and then Has_Task
(Typ
) then
2044 Build_Master_Entity
(Defining_Identifier
(Parent
(N
)));
2047 -- STEP 1: Process component associations
2049 -- For those associations that may generate a loop, initialize
2050 -- Loop_Actions to collect inserted actions that may be crated.
2052 -- Skip this if no component associations
2054 if No
(Expressions
(N
)) then
2056 -- STEP 1 (a): Sort the discrete choices
2058 Assoc
:= First
(Component_Associations
(N
));
2059 while Present
(Assoc
) loop
2060 Choice
:= First
(Choice_List
(Assoc
));
2061 while Present
(Choice
) loop
2062 if Nkind
(Choice
) = N_Others_Choice
then
2063 Set_Loop_Actions
(Assoc
, New_List
);
2064 Others_Assoc
:= Assoc
;
2068 Get_Index_Bounds
(Choice
, Low
, High
);
2071 Set_Loop_Actions
(Assoc
, New_List
);
2074 Nb_Choices
:= Nb_Choices
+ 1;
2076 Table
(Nb_Choices
) :=
2079 Choice_Node
=> Get_Assoc_Expr
(Assoc
));
2087 -- If there is more than one set of choices these must be static
2088 -- and we can therefore sort them. Remember that Nb_Choices does not
2089 -- account for an others choice.
2091 if Nb_Choices
> 1 then
2092 Sort_Case_Table
(Table
);
2095 -- STEP 1 (b): take care of the whole set of discrete choices
2097 for J
in 1 .. Nb_Choices
loop
2098 Low
:= Table
(J
).Choice_Lo
;
2099 High
:= Table
(J
).Choice_Hi
;
2100 Expr
:= Table
(J
).Choice_Node
;
2101 Append_List
(Gen_Loop
(Low
, High
, Expr
), To
=> New_Code
);
2104 -- STEP 1 (c): generate the remaining loops to cover others choice
2105 -- We don't need to generate loops over empty gaps, but if there is
2106 -- a single empty range we must analyze the expression for semantics
2108 if Present
(Others_Assoc
) then
2110 First
: Boolean := True;
2113 for J
in 0 .. Nb_Choices
loop
2117 Low
:= Add
(1, To
=> Table
(J
).Choice_Hi
);
2120 if J
= Nb_Choices
then
2123 High
:= Add
(-1, To
=> Table
(J
+ 1).Choice_Lo
);
2126 -- If this is an expansion within an init proc, make
2127 -- sure that discriminant references are replaced by
2128 -- the corresponding discriminal.
2130 if Inside_Init_Proc
then
2131 if Is_Entity_Name
(Low
)
2132 and then Ekind
(Entity
(Low
)) = E_Discriminant
2134 Set_Entity
(Low
, Discriminal
(Entity
(Low
)));
2137 if Is_Entity_Name
(High
)
2138 and then Ekind
(Entity
(High
)) = E_Discriminant
2140 Set_Entity
(High
, Discriminal
(Entity
(High
)));
2145 or else not Empty_Range
(Low
, High
)
2149 (Gen_Loop
(Low
, High
,
2150 Get_Assoc_Expr
(Others_Assoc
)), To
=> New_Code
);
2156 -- STEP 2: Process positional components
2159 -- STEP 2 (a): Generate the assignments for each positional element
2160 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2161 -- Aggr_L is analyzed and Add wants an analyzed expression.
2163 Expr
:= First
(Expressions
(N
));
2165 while Present
(Expr
) loop
2166 Nb_Elements
:= Nb_Elements
+ 1;
2167 Append_List
(Gen_Assign
(Add
(Nb_Elements
, To
=> Aggr_L
), Expr
),
2172 -- STEP 2 (b): Generate final loop if an others choice is present
2173 -- Here Nb_Elements gives the offset of the last positional element.
2175 if Present
(Component_Associations
(N
)) then
2176 Assoc
:= Last
(Component_Associations
(N
));
2178 -- Ada 2005 (AI-287)
2180 Append_List
(Gen_While
(Add
(Nb_Elements
, To
=> Aggr_L
),
2182 Get_Assoc_Expr
(Assoc
)), -- AI-287
2188 end Build_Array_Aggr_Code
;
2190 ----------------------------
2191 -- Build_Record_Aggr_Code --
2192 ----------------------------
2194 function Build_Record_Aggr_Code
2197 Lhs
: Node_Id
) return List_Id
2199 Loc
: constant Source_Ptr
:= Sloc
(N
);
2200 L
: constant List_Id
:= New_List
;
2201 N_Typ
: constant Entity_Id
:= Etype
(N
);
2207 Comp_Type
: Entity_Id
;
2208 Selector
: Entity_Id
;
2209 Comp_Expr
: Node_Id
;
2212 -- If this is an internal aggregate, the External_Final_List is an
2213 -- expression for the controller record of the enclosing type.
2215 -- If the current aggregate has several controlled components, this
2216 -- expression will appear in several calls to attach to the finali-
2217 -- zation list, and it must not be shared.
2219 Ancestor_Is_Expression
: Boolean := False;
2220 Ancestor_Is_Subtype_Mark
: Boolean := False;
2222 Init_Typ
: Entity_Id
:= Empty
;
2224 Finalization_Done
: Boolean := False;
2225 -- True if Generate_Finalization_Actions has already been called; calls
2226 -- after the first do nothing.
2228 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
;
2229 -- Returns the value that the given discriminant of an ancestor type
2230 -- should receive (in the absence of a conflict with the value provided
2231 -- by an ancestor part of an extension aggregate).
2233 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
);
2234 -- Check that each of the discriminant values defined by the ancestor
2235 -- part of an extension aggregate match the corresponding values
2236 -- provided by either an association of the aggregate or by the
2237 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2239 function Compatible_Int_Bounds
2240 (Agg_Bounds
: Node_Id
;
2241 Typ_Bounds
: Node_Id
) return Boolean;
2242 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2243 -- assumed that both bounds are integer ranges.
2245 procedure Generate_Finalization_Actions
;
2246 -- Deal with the various controlled type data structure initializations
2247 -- (but only if it hasn't been done already).
2249 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
;
2250 -- Returns the first discriminant association in the constraint
2251 -- associated with T, if any, otherwise returns Empty.
2253 function Get_Explicit_Discriminant_Value
(D
: Entity_Id
) return Node_Id
;
2254 -- If the ancestor part is an unconstrained type and further ancestors
2255 -- do not provide discriminants for it, check aggregate components for
2256 -- values of the discriminants.
2258 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
);
2259 -- If Typ is derived, and constrains discriminants of the parent type,
2260 -- these discriminants are not components of the aggregate, and must be
2261 -- initialized. The assignments are appended to List. The same is done
2262 -- if Typ derives fron an already constrained subtype of a discriminated
2265 procedure Init_Stored_Discriminants
;
2266 -- If the type is derived and has inherited discriminants, generate
2267 -- explicit assignments for each, using the store constraint of the
2268 -- type. Note that both visible and stored discriminants must be
2269 -- initialized in case the derived type has some renamed and some
2270 -- constrained discriminants.
2272 procedure Init_Visible_Discriminants
;
2273 -- If type has discriminants, retrieve their values from aggregate,
2274 -- and generate explicit assignments for each. This does not include
2275 -- discriminants inherited from ancestor, which are handled above.
2276 -- The type of the aggregate is a subtype created ealier using the
2277 -- given values of the discriminant components of the aggregate.
2279 procedure Initialize_Ctrl_Record_Component
2280 (Rec_Comp
: Node_Id
;
2281 Comp_Typ
: Entity_Id
;
2282 Init_Expr
: Node_Id
;
2284 -- Perform the initialization of controlled record component Rec_Comp.
2285 -- Comp_Typ is the component type. Init_Expr is the initialization
2286 -- expression for the record component. Hook-related declarations are
2287 -- inserted prior to aggregate N using Insert_Action. All remaining
2288 -- generated code is added to list Stmts.
2290 procedure Initialize_Record_Component
2291 (Rec_Comp
: Node_Id
;
2292 Comp_Typ
: Entity_Id
;
2293 Init_Expr
: Node_Id
;
2295 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2296 -- is the component type. Init_Expr is the initialization expression
2297 -- of the record component. All generated code is added to list Stmts.
2299 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean;
2300 -- Check whether Bounds is a range node and its lower and higher bounds
2301 -- are integers literals.
2303 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
;
2304 -- If the aggregate contains a self-reference, traverse each expression
2305 -- to replace a possible self-reference with a reference to the proper
2306 -- component of the target of the assignment.
2308 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
;
2309 -- If default expression of a component mentions a discriminant of the
2310 -- type, it must be rewritten as the discriminant of the target object.
2312 ---------------------------------
2313 -- Ancestor_Discriminant_Value --
2314 ---------------------------------
2316 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
is
2318 Assoc_Elmt
: Elmt_Id
;
2319 Aggr_Comp
: Entity_Id
;
2320 Corresp_Disc
: Entity_Id
;
2321 Current_Typ
: Entity_Id
:= Base_Type
(Typ
);
2322 Parent_Typ
: Entity_Id
;
2323 Parent_Disc
: Entity_Id
;
2324 Save_Assoc
: Node_Id
:= Empty
;
2327 -- First check any discriminant associations to see if any of them
2328 -- provide a value for the discriminant.
2330 if Present
(Discriminant_Specifications
(Parent
(Current_Typ
))) then
2331 Assoc
:= First
(Component_Associations
(N
));
2332 while Present
(Assoc
) loop
2333 Aggr_Comp
:= Entity
(First
(Choices
(Assoc
)));
2335 if Ekind
(Aggr_Comp
) = E_Discriminant
then
2336 Save_Assoc
:= Expression
(Assoc
);
2338 Corresp_Disc
:= Corresponding_Discriminant
(Aggr_Comp
);
2339 while Present
(Corresp_Disc
) loop
2341 -- If found a corresponding discriminant then return the
2342 -- value given in the aggregate. (Note: this is not
2343 -- correct in the presence of side effects. ???)
2345 if Disc
= Corresp_Disc
then
2346 return Duplicate_Subexpr
(Expression
(Assoc
));
2349 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
2357 -- No match found in aggregate, so chain up parent types to find
2358 -- a constraint that defines the value of the discriminant.
2360 Parent_Typ
:= Etype
(Current_Typ
);
2361 while Current_Typ
/= Parent_Typ
loop
2362 if Has_Discriminants
(Parent_Typ
)
2363 and then not Has_Unknown_Discriminants
(Parent_Typ
)
2365 Parent_Disc
:= First_Discriminant
(Parent_Typ
);
2367 -- We either get the association from the subtype indication
2368 -- of the type definition itself, or from the discriminant
2369 -- constraint associated with the type entity (which is
2370 -- preferable, but it's not always present ???)
2372 if Is_Empty_Elmt_List
(Discriminant_Constraint
(Current_Typ
))
2374 Assoc
:= Get_Constraint_Association
(Current_Typ
);
2375 Assoc_Elmt
:= No_Elmt
;
2378 First_Elmt
(Discriminant_Constraint
(Current_Typ
));
2379 Assoc
:= Node
(Assoc_Elmt
);
2382 -- Traverse the discriminants of the parent type looking
2383 -- for one that corresponds.
2385 while Present
(Parent_Disc
) and then Present
(Assoc
) loop
2386 Corresp_Disc
:= Parent_Disc
;
2387 while Present
(Corresp_Disc
)
2388 and then Disc
/= Corresp_Disc
2390 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
2393 if Disc
= Corresp_Disc
then
2394 if Nkind
(Assoc
) = N_Discriminant_Association
then
2395 Assoc
:= Expression
(Assoc
);
2398 -- If the located association directly denotes
2399 -- a discriminant, then use the value of a saved
2400 -- association of the aggregate. This is an approach
2401 -- used to handle certain cases involving multiple
2402 -- discriminants mapped to a single discriminant of
2403 -- a descendant. It's not clear how to locate the
2404 -- appropriate discriminant value for such cases. ???
2406 if Is_Entity_Name
(Assoc
)
2407 and then Ekind
(Entity
(Assoc
)) = E_Discriminant
2409 Assoc
:= Save_Assoc
;
2412 return Duplicate_Subexpr
(Assoc
);
2415 Next_Discriminant
(Parent_Disc
);
2417 if No
(Assoc_Elmt
) then
2421 Next_Elmt
(Assoc_Elmt
);
2423 if Present
(Assoc_Elmt
) then
2424 Assoc
:= Node
(Assoc_Elmt
);
2432 Current_Typ
:= Parent_Typ
;
2433 Parent_Typ
:= Etype
(Current_Typ
);
2436 -- In some cases there's no ancestor value to locate (such as
2437 -- when an ancestor part given by an expression defines the
2438 -- discriminant value).
2441 end Ancestor_Discriminant_Value
;
2443 ----------------------------------
2444 -- Check_Ancestor_Discriminants --
2445 ----------------------------------
2447 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
) is
2449 Disc_Value
: Node_Id
;
2453 Discr
:= First_Discriminant
(Base_Type
(Anc_Typ
));
2454 while Present
(Discr
) loop
2455 Disc_Value
:= Ancestor_Discriminant_Value
(Discr
);
2457 if Present
(Disc_Value
) then
2458 Cond
:= Make_Op_Ne
(Loc
,
2460 Make_Selected_Component
(Loc
,
2461 Prefix
=> New_Copy_Tree
(Target
),
2462 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2463 Right_Opnd
=> Disc_Value
);
2466 Make_Raise_Constraint_Error
(Loc
,
2468 Reason
=> CE_Discriminant_Check_Failed
));
2471 Next_Discriminant
(Discr
);
2473 end Check_Ancestor_Discriminants
;
2475 ---------------------------
2476 -- Compatible_Int_Bounds --
2477 ---------------------------
2479 function Compatible_Int_Bounds
2480 (Agg_Bounds
: Node_Id
;
2481 Typ_Bounds
: Node_Id
) return Boolean
2483 Agg_Lo
: constant Uint
:= Intval
(Low_Bound
(Agg_Bounds
));
2484 Agg_Hi
: constant Uint
:= Intval
(High_Bound
(Agg_Bounds
));
2485 Typ_Lo
: constant Uint
:= Intval
(Low_Bound
(Typ_Bounds
));
2486 Typ_Hi
: constant Uint
:= Intval
(High_Bound
(Typ_Bounds
));
2488 return Typ_Lo
<= Agg_Lo
and then Agg_Hi
<= Typ_Hi
;
2489 end Compatible_Int_Bounds
;
2491 -----------------------------------
2492 -- Generate_Finalization_Actions --
2493 -----------------------------------
2495 procedure Generate_Finalization_Actions
is
2497 -- Do the work only the first time this is called
2499 if Finalization_Done
then
2503 Finalization_Done
:= True;
2505 -- Determine the external finalization list. It is either the
2506 -- finalization list of the outer scope or the one coming from an
2507 -- outer aggregate. When the target is not a temporary, the proper
2508 -- scope is the scope of the target rather than the potentially
2509 -- transient current scope.
2511 if Is_Controlled
(Typ
) and then Ancestor_Is_Subtype_Mark
then
2512 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2513 Set_Assignment_OK
(Ref
);
2516 Make_Procedure_Call_Statement
(Loc
,
2519 (Find_Prim_Op
(Init_Typ
, Name_Initialize
), Loc
),
2520 Parameter_Associations
=> New_List
(New_Copy_Tree
(Ref
))));
2522 end Generate_Finalization_Actions
;
2524 --------------------------------
2525 -- Get_Constraint_Association --
2526 --------------------------------
2528 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
is
2535 -- If type is private, get constraint from full view. This was
2536 -- previously done in an instance context, but is needed whenever
2537 -- the ancestor part has a discriminant, possibly inherited through
2538 -- multiple derivations.
2540 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
2541 Typ
:= Full_View
(Typ
);
2544 Indic
:= Subtype_Indication
(Type_Definition
(Parent
(Typ
)));
2546 -- Verify that the subtype indication carries a constraint
2548 if Nkind
(Indic
) = N_Subtype_Indication
2549 and then Present
(Constraint
(Indic
))
2551 return First
(Constraints
(Constraint
(Indic
)));
2555 end Get_Constraint_Association
;
2557 -------------------------------------
2558 -- Get_Explicit_Discriminant_Value --
2559 -------------------------------------
2561 function Get_Explicit_Discriminant_Value
2562 (D
: Entity_Id
) return Node_Id
2569 -- The aggregate has been normalized and all associations have a
2572 Assoc
:= First
(Component_Associations
(N
));
2573 while Present
(Assoc
) loop
2574 Choice
:= First
(Choices
(Assoc
));
2576 if Chars
(Choice
) = Chars
(D
) then
2577 Val
:= Expression
(Assoc
);
2586 end Get_Explicit_Discriminant_Value
;
2588 -------------------------------
2589 -- Init_Hidden_Discriminants --
2590 -------------------------------
2592 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
) is
2593 function Is_Completely_Hidden_Discriminant
2594 (Discr
: Entity_Id
) return Boolean;
2595 -- Determine whether Discr is a completely hidden discriminant of
2598 ---------------------------------------
2599 -- Is_Completely_Hidden_Discriminant --
2600 ---------------------------------------
2602 function Is_Completely_Hidden_Discriminant
2603 (Discr
: Entity_Id
) return Boolean
2608 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2609 -- completely hidden discriminants.
2611 Item
:= First_Entity
(Typ
);
2612 while Present
(Item
) loop
2613 if Ekind
(Item
) = E_Discriminant
2614 and then Is_Completely_Hidden
(Item
)
2615 and then Chars
(Original_Record_Component
(Item
)) =
2625 end Is_Completely_Hidden_Discriminant
;
2629 Base_Typ
: Entity_Id
;
2631 Discr_Constr
: Elmt_Id
;
2632 Discr_Init
: Node_Id
;
2633 Discr_Val
: Node_Id
;
2634 In_Aggr_Type
: Boolean;
2635 Par_Typ
: Entity_Id
;
2637 -- Start of processing for Init_Hidden_Discriminants
2640 -- The constraints on the hidden discriminants, if present, are kept
2641 -- in the Stored_Constraint list of the type itself, or in that of
2642 -- the base type. If not in the constraints of the aggregate itself,
2643 -- we examine ancestors to find discriminants that are not renamed
2644 -- by other discriminants but constrained explicitly.
2646 In_Aggr_Type
:= True;
2648 Base_Typ
:= Base_Type
(Typ
);
2649 while Is_Derived_Type
(Base_Typ
)
2651 (Present
(Stored_Constraint
(Base_Typ
))
2653 (In_Aggr_Type
and then Present
(Stored_Constraint
(Typ
))))
2655 Par_Typ
:= Etype
(Base_Typ
);
2657 if not Has_Discriminants
(Par_Typ
) then
2661 Discr
:= First_Discriminant
(Par_Typ
);
2663 -- We know that one of the stored-constraint lists is present
2665 if Present
(Stored_Constraint
(Base_Typ
)) then
2666 Discr_Constr
:= First_Elmt
(Stored_Constraint
(Base_Typ
));
2668 -- For private extension, stored constraint may be on full view
2670 elsif Is_Private_Type
(Base_Typ
)
2671 and then Present
(Full_View
(Base_Typ
))
2672 and then Present
(Stored_Constraint
(Full_View
(Base_Typ
)))
2675 First_Elmt
(Stored_Constraint
(Full_View
(Base_Typ
)));
2678 Discr_Constr
:= First_Elmt
(Stored_Constraint
(Typ
));
2681 while Present
(Discr
) and then Present
(Discr_Constr
) loop
2682 Discr_Val
:= Node
(Discr_Constr
);
2684 -- The parent discriminant is renamed in the derived type,
2685 -- nothing to initialize.
2687 -- type Deriv_Typ (Discr : ...)
2688 -- is new Parent_Typ (Discr => Discr);
2690 if Is_Entity_Name
(Discr_Val
)
2691 and then Ekind
(Entity
(Discr_Val
)) = E_Discriminant
2695 -- When the parent discriminant is constrained at the type
2696 -- extension level, it does not appear in the derived type.
2698 -- type Deriv_Typ (Discr : ...)
2699 -- is new Parent_Typ (Discr => Discr,
2700 -- Hidden_Discr => Expression);
2702 elsif Is_Completely_Hidden_Discriminant
(Discr
) then
2705 -- Otherwise initialize the discriminant
2709 Make_OK_Assignment_Statement
(Loc
,
2711 Make_Selected_Component
(Loc
,
2712 Prefix
=> New_Copy_Tree
(Target
),
2713 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2714 Expression
=> New_Copy_Tree
(Discr_Val
));
2716 Set_No_Ctrl_Actions
(Discr_Init
);
2717 Append_To
(List
, Discr_Init
);
2720 Next_Elmt
(Discr_Constr
);
2721 Next_Discriminant
(Discr
);
2724 In_Aggr_Type
:= False;
2725 Base_Typ
:= Base_Type
(Par_Typ
);
2727 end Init_Hidden_Discriminants
;
2729 --------------------------------
2730 -- Init_Visible_Discriminants --
2731 --------------------------------
2733 procedure Init_Visible_Discriminants
is
2734 Discriminant
: Entity_Id
;
2735 Discriminant_Value
: Node_Id
;
2738 Discriminant
:= First_Discriminant
(Typ
);
2739 while Present
(Discriminant
) loop
2741 Make_Selected_Component
(Loc
,
2742 Prefix
=> New_Copy_Tree
(Target
),
2743 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2745 Discriminant_Value
:=
2746 Get_Discriminant_Value
2747 (Discriminant
, Typ
, Discriminant_Constraint
(N_Typ
));
2750 Make_OK_Assignment_Statement
(Loc
,
2752 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2754 Set_No_Ctrl_Actions
(Instr
);
2755 Append_To
(L
, Instr
);
2757 Next_Discriminant
(Discriminant
);
2759 end Init_Visible_Discriminants
;
2761 -------------------------------
2762 -- Init_Stored_Discriminants --
2763 -------------------------------
2765 procedure Init_Stored_Discriminants
is
2766 Discriminant
: Entity_Id
;
2767 Discriminant_Value
: Node_Id
;
2770 Discriminant
:= First_Stored_Discriminant
(Typ
);
2771 while Present
(Discriminant
) loop
2773 Make_Selected_Component
(Loc
,
2774 Prefix
=> New_Copy_Tree
(Target
),
2775 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2777 Discriminant_Value
:=
2778 Get_Discriminant_Value
2779 (Discriminant
, N_Typ
, Discriminant_Constraint
(N_Typ
));
2782 Make_OK_Assignment_Statement
(Loc
,
2784 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2786 Set_No_Ctrl_Actions
(Instr
);
2787 Append_To
(L
, Instr
);
2789 Next_Stored_Discriminant
(Discriminant
);
2791 end Init_Stored_Discriminants
;
2793 --------------------------------------
2794 -- Initialize_Ctrl_Record_Component --
2795 --------------------------------------
2797 procedure Initialize_Ctrl_Record_Component
2798 (Rec_Comp
: Node_Id
;
2799 Comp_Typ
: Entity_Id
;
2800 Init_Expr
: Node_Id
;
2804 Hook_Clear
: Node_Id
;
2806 In_Place_Expansion
: Boolean;
2807 -- Flag set when a nonlimited controlled function call requires
2808 -- in-place expansion.
2811 -- Perform a preliminary analysis and resolution to determine what
2812 -- the initialization expression denotes. Unanalyzed function calls
2813 -- may appear as identifiers or indexed components.
2815 if Nkind_In
(Init_Expr
, N_Function_Call
,
2817 N_Indexed_Component
)
2818 and then not Analyzed
(Init_Expr
)
2820 Preanalyze_And_Resolve
(Init_Expr
, Comp_Typ
);
2823 In_Place_Expansion
:=
2824 Nkind
(Init_Expr
) = N_Function_Call
2825 and then not Is_Limited_Type
(Comp_Typ
);
2827 -- The initialization expression is a controlled function call.
2828 -- Perform in-place removal of side effects to avoid creating a
2831 -- This in-place expansion is not performed for limited transient
2832 -- objects because the initialization is already done in place.
2834 if In_Place_Expansion
then
2836 -- Suppress the removal of side effects by general analysis
2837 -- because this behavior is emulated here. This avoids the
2838 -- generation of a transient scope, which leads to out-of-order
2839 -- adjustment and finalization.
2841 Set_No_Side_Effect_Removal
(Init_Expr
);
2843 -- Install all hook-related declarations and prepare the clean up
2846 Process_Transient_Component
2848 Comp_Typ
=> Comp_Typ
,
2849 Init_Expr
=> Init_Expr
,
2850 Fin_Call
=> Fin_Call
,
2851 Hook_Clear
=> Hook_Clear
,
2855 -- Use the noncontrolled component initialization circuitry to
2856 -- assign the result of the function call to the record component.
2857 -- This also performs tag adjustment and [deep] adjustment of the
2858 -- record component.
2860 Initialize_Record_Component
2861 (Rec_Comp
=> Rec_Comp
,
2862 Comp_Typ
=> Comp_Typ
,
2863 Init_Expr
=> Init_Expr
,
2866 -- At this point the record component is fully initialized. Complete
2867 -- the processing of the controlled record component by finalizing
2868 -- the transient function result.
2870 if In_Place_Expansion
then
2871 Process_Transient_Component_Completion
2874 Fin_Call
=> Fin_Call
,
2875 Hook_Clear
=> Hook_Clear
,
2878 end Initialize_Ctrl_Record_Component
;
2880 ---------------------------------
2881 -- Initialize_Record_Component --
2882 ---------------------------------
2884 procedure Initialize_Record_Component
2885 (Rec_Comp
: Node_Id
;
2886 Comp_Typ
: Entity_Id
;
2887 Init_Expr
: Node_Id
;
2890 Exceptions_OK
: constant Boolean :=
2891 not Restriction_Active
(No_Exception_Propagation
);
2893 Finalization_OK
: constant Boolean := Needs_Finalization
(Comp_Typ
);
2895 Full_Typ
: constant Entity_Id
:= Underlying_Type
(Comp_Typ
);
2897 Blk_Stmts
: List_Id
;
2898 Init_Stmt
: Node_Id
;
2901 -- Protect the initialization statements from aborts. Generate:
2905 if Finalization_OK
and Abort_Allowed
then
2906 if Exceptions_OK
then
2907 Blk_Stmts
:= New_List
;
2912 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
2914 -- Otherwise aborts are not allowed. All generated code is added
2915 -- directly to the input list.
2921 -- Initialize the record component. Generate:
2923 -- Rec_Comp := Init_Expr;
2925 -- Note that the initialization expression is NOT replicated because
2926 -- only a single component may be initialized by it.
2929 Make_OK_Assignment_Statement
(Loc
,
2930 Name
=> New_Copy_Tree
(Rec_Comp
),
2931 Expression
=> Init_Expr
);
2932 Set_No_Ctrl_Actions
(Init_Stmt
);
2934 Append_To
(Blk_Stmts
, Init_Stmt
);
2936 -- Adjust the tag due to a possible view conversion. Generate:
2938 -- Rec_Comp._tag := Full_TypeP;
2940 if Tagged_Type_Expansion
and then Is_Tagged_Type
(Comp_Typ
) then
2941 Append_To
(Blk_Stmts
,
2942 Make_OK_Assignment_Statement
(Loc
,
2944 Make_Selected_Component
(Loc
,
2945 Prefix
=> New_Copy_Tree
(Rec_Comp
),
2948 (First_Tag_Component
(Full_Typ
), Loc
)),
2951 Unchecked_Convert_To
(RTE
(RE_Tag
),
2953 (Node
(First_Elmt
(Access_Disp_Table
(Full_Typ
))),
2957 -- Adjust the component. Generate:
2959 -- [Deep_]Adjust (Rec_Comp);
2961 if Finalization_OK
and then not Is_Limited_Type
(Comp_Typ
) then
2964 (Obj_Ref
=> New_Copy_Tree
(Rec_Comp
),
2967 -- Guard against a missing [Deep_]Adjust when the component type
2968 -- was not properly frozen.
2970 if Present
(Adj_Call
) then
2971 Append_To
(Blk_Stmts
, Adj_Call
);
2975 -- Complete the protection of the initialization statements
2977 if Finalization_OK
and Abort_Allowed
then
2979 -- Wrap the initialization statements in a block to catch a
2980 -- potential exception. Generate:
2984 -- Rec_Comp := Init_Expr;
2985 -- Rec_Comp._tag := Full_TypP;
2986 -- [Deep_]Adjust (Rec_Comp);
2988 -- Abort_Undefer_Direct;
2991 if Exceptions_OK
then
2993 Build_Abort_Undefer_Block
(Loc
,
2997 -- Otherwise exceptions are not propagated. Generate:
3000 -- Rec_Comp := Init_Expr;
3001 -- Rec_Comp._tag := Full_TypP;
3002 -- [Deep_]Adjust (Rec_Comp);
3006 Append_To
(Blk_Stmts
,
3007 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
3010 end Initialize_Record_Component
;
3012 -------------------------
3013 -- Is_Int_Range_Bounds --
3014 -------------------------
3016 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean is
3018 return Nkind
(Bounds
) = N_Range
3019 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
3020 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
;
3021 end Is_Int_Range_Bounds
;
3027 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
is
3029 -- Note regarding the Root_Type test below: Aggregate components for
3030 -- self-referential types include attribute references to the current
3031 -- instance, of the form: Typ'access, etc.. These references are
3032 -- rewritten as references to the target of the aggregate: the
3033 -- left-hand side of an assignment, the entity in a declaration,
3034 -- or a temporary. Without this test, we would improperly extended
3035 -- this rewriting to attribute references whose prefix was not the
3036 -- type of the aggregate.
3038 if Nkind
(Expr
) = N_Attribute_Reference
3039 and then Is_Entity_Name
(Prefix
(Expr
))
3040 and then Is_Type
(Entity
(Prefix
(Expr
)))
3041 and then Root_Type
(Etype
(N
)) = Root_Type
(Entity
(Prefix
(Expr
)))
3043 if Is_Entity_Name
(Lhs
) then
3044 Rewrite
(Prefix
(Expr
), New_Occurrence_Of
(Entity
(Lhs
), Loc
));
3048 Make_Attribute_Reference
(Loc
,
3049 Attribute_Name
=> Name_Unrestricted_Access
,
3050 Prefix
=> New_Copy_Tree
(Lhs
)));
3051 Set_Analyzed
(Parent
(Expr
), False);
3058 --------------------------
3059 -- Rewrite_Discriminant --
3060 --------------------------
3062 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
is
3064 if Is_Entity_Name
(Expr
)
3065 and then Present
(Entity
(Expr
))
3066 and then Ekind
(Entity
(Expr
)) = E_In_Parameter
3067 and then Present
(Discriminal_Link
(Entity
(Expr
)))
3068 and then Scope
(Discriminal_Link
(Entity
(Expr
))) =
3069 Base_Type
(Etype
(N
))
3072 Make_Selected_Component
(Loc
,
3073 Prefix
=> New_Copy_Tree
(Lhs
),
3074 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Expr
))));
3078 end Rewrite_Discriminant
;
3080 procedure Replace_Discriminants
is
3081 new Traverse_Proc
(Rewrite_Discriminant
);
3083 procedure Replace_Self_Reference
is
3084 new Traverse_Proc
(Replace_Type
);
3086 -- Start of processing for Build_Record_Aggr_Code
3089 if Has_Self_Reference
(N
) then
3090 Replace_Self_Reference
(N
);
3093 -- If the target of the aggregate is class-wide, we must convert it
3094 -- to the actual type of the aggregate, so that the proper components
3095 -- are visible. We know already that the types are compatible.
3097 if Present
(Etype
(Lhs
))
3098 and then Is_Class_Wide_Type
(Etype
(Lhs
))
3100 Target
:= Unchecked_Convert_To
(Typ
, Lhs
);
3105 -- Deal with the ancestor part of extension aggregates or with the
3106 -- discriminants of the root type.
3108 if Nkind
(N
) = N_Extension_Aggregate
then
3110 Ancestor
: constant Node_Id
:= Ancestor_Part
(N
);
3115 -- If the ancestor part is a subtype mark "T", we generate
3117 -- init-proc (T (tmp)); if T is constrained and
3118 -- init-proc (S (tmp)); where S applies an appropriate
3119 -- constraint if T is unconstrained
3121 if Is_Entity_Name
(Ancestor
)
3122 and then Is_Type
(Entity
(Ancestor
))
3124 Ancestor_Is_Subtype_Mark
:= True;
3126 if Is_Constrained
(Entity
(Ancestor
)) then
3127 Init_Typ
:= Entity
(Ancestor
);
3129 -- For an ancestor part given by an unconstrained type mark,
3130 -- create a subtype constrained by appropriate corresponding
3131 -- discriminant values coming from either associations of the
3132 -- aggregate or a constraint on a parent type. The subtype will
3133 -- be used to generate the correct default value for the
3136 elsif Has_Discriminants
(Entity
(Ancestor
)) then
3138 Anc_Typ
: constant Entity_Id
:= Entity
(Ancestor
);
3139 Anc_Constr
: constant List_Id
:= New_List
;
3140 Discrim
: Entity_Id
;
3141 Disc_Value
: Node_Id
;
3142 New_Indic
: Node_Id
;
3143 Subt_Decl
: Node_Id
;
3146 Discrim
:= First_Discriminant
(Anc_Typ
);
3147 while Present
(Discrim
) loop
3148 Disc_Value
:= Ancestor_Discriminant_Value
(Discrim
);
3150 -- If no usable discriminant in ancestors, check
3151 -- whether aggregate has an explicit value for it.
3153 if No
(Disc_Value
) then
3155 Get_Explicit_Discriminant_Value
(Discrim
);
3158 Append_To
(Anc_Constr
, Disc_Value
);
3159 Next_Discriminant
(Discrim
);
3163 Make_Subtype_Indication
(Loc
,
3164 Subtype_Mark
=> New_Occurrence_Of
(Anc_Typ
, Loc
),
3166 Make_Index_Or_Discriminant_Constraint
(Loc
,
3167 Constraints
=> Anc_Constr
));
3169 Init_Typ
:= Create_Itype
(Ekind
(Anc_Typ
), N
);
3172 Make_Subtype_Declaration
(Loc
,
3173 Defining_Identifier
=> Init_Typ
,
3174 Subtype_Indication
=> New_Indic
);
3176 -- Itypes must be analyzed with checks off Declaration
3177 -- must have a parent for proper handling of subsidiary
3180 Set_Parent
(Subt_Decl
, N
);
3181 Analyze
(Subt_Decl
, Suppress
=> All_Checks
);
3185 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3186 Set_Assignment_OK
(Ref
);
3188 if not Is_Interface
(Init_Typ
) then
3190 Build_Initialization_Call
(Loc
,
3193 In_Init_Proc
=> Within_Init_Proc
,
3194 With_Default_Init
=> Has_Default_Init_Comps
(N
)
3196 Has_Task
(Base_Type
(Init_Typ
))));
3198 if Is_Constrained
(Entity
(Ancestor
))
3199 and then Has_Discriminants
(Entity
(Ancestor
))
3201 Check_Ancestor_Discriminants
(Entity
(Ancestor
));
3205 -- Handle calls to C++ constructors
3207 elsif Is_CPP_Constructor_Call
(Ancestor
) then
3208 Init_Typ
:= Etype
(Ancestor
);
3209 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3210 Set_Assignment_OK
(Ref
);
3213 Build_Initialization_Call
(Loc
,
3216 In_Init_Proc
=> Within_Init_Proc
,
3217 With_Default_Init
=> Has_Default_Init_Comps
(N
),
3218 Constructor_Ref
=> Ancestor
));
3220 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3221 -- limited type, a recursive call expands the ancestor. Note that
3222 -- in the limited case, the ancestor part must be either a
3223 -- function call (possibly qualified, or wrapped in an unchecked
3224 -- conversion) or aggregate (definitely qualified).
3226 -- The ancestor part can also be a function call (that may be
3227 -- transformed into an explicit dereference) or a qualification
3230 elsif Is_Limited_Type
(Etype
(Ancestor
))
3231 and then Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
3232 N_Extension_Aggregate
)
3234 Ancestor_Is_Expression
:= True;
3236 -- Set up finalization data for enclosing record, because
3237 -- controlled subcomponents of the ancestor part will be
3240 Generate_Finalization_Actions
;
3243 Build_Record_Aggr_Code
3244 (N
=> Unqualify
(Ancestor
),
3245 Typ
=> Etype
(Unqualify
(Ancestor
)),
3248 -- If the ancestor part is an expression "E", we generate
3252 -- In Ada 2005, this includes the case of a (possibly qualified)
3253 -- limited function call. The assignment will turn into a
3254 -- build-in-place function call (for further details, see
3255 -- Make_Build_In_Place_Call_In_Assignment).
3258 Ancestor_Is_Expression
:= True;
3259 Init_Typ
:= Etype
(Ancestor
);
3261 -- If the ancestor part is an aggregate, force its full
3262 -- expansion, which was delayed.
3264 if Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
3265 N_Extension_Aggregate
)
3267 Set_Analyzed
(Ancestor
, False);
3268 Set_Analyzed
(Expression
(Ancestor
), False);
3271 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
3272 Set_Assignment_OK
(Ref
);
3274 -- Make the assignment without usual controlled actions, since
3275 -- we only want to Adjust afterwards, but not to Finalize
3276 -- beforehand. Add manual Adjust when necessary.
3278 Assign
:= New_List
(
3279 Make_OK_Assignment_Statement
(Loc
,
3281 Expression
=> Ancestor
));
3282 Set_No_Ctrl_Actions
(First
(Assign
));
3284 -- Assign the tag now to make sure that the dispatching call in
3285 -- the subsequent deep_adjust works properly (unless
3286 -- Tagged_Type_Expansion where tags are implicit).
3288 if Tagged_Type_Expansion
then
3290 Make_OK_Assignment_Statement
(Loc
,
3292 Make_Selected_Component
(Loc
,
3293 Prefix
=> New_Copy_Tree
(Target
),
3296 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
3299 Unchecked_Convert_To
(RTE
(RE_Tag
),
3302 (Access_Disp_Table
(Base_Type
(Typ
)))),
3305 Set_Assignment_OK
(Name
(Instr
));
3306 Append_To
(Assign
, Instr
);
3308 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3309 -- also initialize tags of the secondary dispatch tables.
3311 if Has_Interfaces
(Base_Type
(Typ
)) then
3313 (Typ
=> Base_Type
(Typ
),
3315 Stmts_List
=> Assign
);
3319 -- Call Adjust manually
3321 if Needs_Finalization
(Etype
(Ancestor
))
3322 and then not Is_Limited_Type
(Etype
(Ancestor
))
3326 (Obj_Ref
=> New_Copy_Tree
(Ref
),
3327 Typ
=> Etype
(Ancestor
));
3329 -- Guard against a missing [Deep_]Adjust when the ancestor
3330 -- type was not properly frozen.
3332 if Present
(Adj_Call
) then
3333 Append_To
(Assign
, Adj_Call
);
3338 Make_Unsuppress_Block
(Loc
, Name_Discriminant_Check
, Assign
));
3340 if Has_Discriminants
(Init_Typ
) then
3341 Check_Ancestor_Discriminants
(Init_Typ
);
3346 -- Generate assignments of hidden discriminants. If the base type is
3347 -- an unchecked union, the discriminants are unknown to the back-end
3348 -- and absent from a value of the type, so assignments for them are
3351 if Has_Discriminants
(Typ
)
3352 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
3354 Init_Hidden_Discriminants
(Typ
, L
);
3357 -- Normal case (not an extension aggregate)
3360 -- Generate the discriminant expressions, component by component.
3361 -- If the base type is an unchecked union, the discriminants are
3362 -- unknown to the back-end and absent from a value of the type, so
3363 -- assignments for them are not emitted.
3365 if Has_Discriminants
(Typ
)
3366 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
3368 Init_Hidden_Discriminants
(Typ
, L
);
3370 -- Generate discriminant init values for the visible discriminants
3372 Init_Visible_Discriminants
;
3374 if Is_Derived_Type
(N_Typ
) then
3375 Init_Stored_Discriminants
;
3380 -- For CPP types we generate an implicit call to the C++ default
3381 -- constructor to ensure the proper initialization of the _Tag
3384 if Is_CPP_Class
(Root_Type
(Typ
)) and then CPP_Num_Prims
(Typ
) > 0 then
3385 Invoke_Constructor
: declare
3386 CPP_Parent
: constant Entity_Id
:= Enclosing_CPP_Parent
(Typ
);
3388 procedure Invoke_IC_Proc
(T
: Entity_Id
);
3389 -- Recursive routine used to climb to parents. Required because
3390 -- parents must be initialized before descendants to ensure
3391 -- propagation of inherited C++ slots.
3393 --------------------
3394 -- Invoke_IC_Proc --
3395 --------------------
3397 procedure Invoke_IC_Proc
(T
: Entity_Id
) is
3399 -- Avoid generating extra calls. Initialization required
3400 -- only for types defined from the level of derivation of
3401 -- type of the constructor and the type of the aggregate.
3403 if T
= CPP_Parent
then
3407 Invoke_IC_Proc
(Etype
(T
));
3409 -- Generate call to the IC routine
3411 if Present
(CPP_Init_Proc
(T
)) then
3413 Make_Procedure_Call_Statement
(Loc
,
3414 Name
=> New_Occurrence_Of
(CPP_Init_Proc
(T
), Loc
)));
3418 -- Start of processing for Invoke_Constructor
3421 -- Implicit invocation of the C++ constructor
3423 if Nkind
(N
) = N_Aggregate
then
3425 Make_Procedure_Call_Statement
(Loc
,
3427 New_Occurrence_Of
(Base_Init_Proc
(CPP_Parent
), Loc
),
3428 Parameter_Associations
=> New_List
(
3429 Unchecked_Convert_To
(CPP_Parent
,
3430 New_Copy_Tree
(Lhs
)))));
3433 Invoke_IC_Proc
(Typ
);
3434 end Invoke_Constructor
;
3437 -- Generate the assignments, component by component
3439 -- tmp.comp1 := Expr1_From_Aggr;
3440 -- tmp.comp2 := Expr2_From_Aggr;
3443 Comp
:= First
(Component_Associations
(N
));
3444 while Present
(Comp
) loop
3445 Selector
:= Entity
(First
(Choices
(Comp
)));
3449 if Is_CPP_Constructor_Call
(Expression
(Comp
)) then
3451 Build_Initialization_Call
(Loc
,
3453 Make_Selected_Component
(Loc
,
3454 Prefix
=> New_Copy_Tree
(Target
),
3455 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
)),
3456 Typ
=> Etype
(Selector
),
3458 With_Default_Init
=> True,
3459 Constructor_Ref
=> Expression
(Comp
)));
3461 -- Ada 2005 (AI-287): For each default-initialized component generate
3462 -- a call to the corresponding IP subprogram if available.
3464 elsif Box_Present
(Comp
)
3465 and then Has_Non_Null_Base_Init_Proc
(Etype
(Selector
))
3467 if Ekind
(Selector
) /= E_Discriminant
then
3468 Generate_Finalization_Actions
;
3471 -- Ada 2005 (AI-287): If the component type has tasks then
3472 -- generate the activation chain and master entities (except
3473 -- in case of an allocator because in that case these entities
3474 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3477 Ctype
: constant Entity_Id
:= Etype
(Selector
);
3478 Inside_Allocator
: Boolean := False;
3479 P
: Node_Id
:= Parent
(N
);
3482 if Is_Task_Type
(Ctype
) or else Has_Task
(Ctype
) then
3483 while Present
(P
) loop
3484 if Nkind
(P
) = N_Allocator
then
3485 Inside_Allocator
:= True;
3492 if not Inside_Init_Proc
and not Inside_Allocator
then
3493 Build_Activation_Chain_Entity
(N
);
3499 Build_Initialization_Call
(Loc
,
3500 Id_Ref
=> Make_Selected_Component
(Loc
,
3501 Prefix
=> New_Copy_Tree
(Target
),
3503 New_Occurrence_Of
(Selector
, Loc
)),
3504 Typ
=> Etype
(Selector
),
3506 With_Default_Init
=> True));
3508 -- Prepare for component assignment
3510 elsif Ekind
(Selector
) /= E_Discriminant
3511 or else Nkind
(N
) = N_Extension_Aggregate
3513 -- All the discriminants have now been assigned
3515 -- This is now a good moment to initialize and attach all the
3516 -- controllers. Their position may depend on the discriminants.
3518 if Ekind
(Selector
) /= E_Discriminant
then
3519 Generate_Finalization_Actions
;
3522 Comp_Type
:= Underlying_Type
(Etype
(Selector
));
3524 Make_Selected_Component
(Loc
,
3525 Prefix
=> New_Copy_Tree
(Target
),
3526 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
));
3528 if Nkind
(Expression
(Comp
)) = N_Qualified_Expression
then
3529 Expr_Q
:= Expression
(Expression
(Comp
));
3531 Expr_Q
:= Expression
(Comp
);
3534 -- Now either create the assignment or generate the code for the
3535 -- inner aggregate top-down.
3537 if Is_Delayed_Aggregate
(Expr_Q
) then
3539 -- We have the following case of aggregate nesting inside
3540 -- an object declaration:
3542 -- type Arr_Typ is array (Integer range <>) of ...;
3544 -- type Rec_Typ (...) is record
3545 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3548 -- Obj_Rec_Typ : Rec_Typ := (...,
3549 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3551 -- The length of the ranges of the aggregate and Obj_Add_Typ
3552 -- are equal (B - A = Y - X), but they do not coincide (X /=
3553 -- A and B /= Y). This case requires array sliding which is
3554 -- performed in the following manner:
3556 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3558 -- Temp (X) := (...);
3560 -- Temp (Y) := (...);
3561 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3563 if Ekind
(Comp_Type
) = E_Array_Subtype
3564 and then Is_Int_Range_Bounds
(Aggregate_Bounds
(Expr_Q
))
3565 and then Is_Int_Range_Bounds
(First_Index
(Comp_Type
))
3567 Compatible_Int_Bounds
3568 (Agg_Bounds
=> Aggregate_Bounds
(Expr_Q
),
3569 Typ_Bounds
=> First_Index
(Comp_Type
))
3571 -- Create the array subtype with bounds equal to those of
3572 -- the corresponding aggregate.
3575 SubE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
3577 SubD
: constant Node_Id
:=
3578 Make_Subtype_Declaration
(Loc
,
3579 Defining_Identifier
=> SubE
,
3580 Subtype_Indication
=>
3581 Make_Subtype_Indication
(Loc
,
3583 New_Occurrence_Of
(Etype
(Comp_Type
), Loc
),
3585 Make_Index_Or_Discriminant_Constraint
3587 Constraints
=> New_List
(
3589 (Aggregate_Bounds
(Expr_Q
))))));
3591 -- Create a temporary array of the above subtype which
3592 -- will be used to capture the aggregate assignments.
3594 TmpE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A', N
);
3596 TmpD
: constant Node_Id
:=
3597 Make_Object_Declaration
(Loc
,
3598 Defining_Identifier
=> TmpE
,
3599 Object_Definition
=> New_Occurrence_Of
(SubE
, Loc
));
3602 Set_No_Initialization
(TmpD
);
3603 Append_To
(L
, SubD
);
3604 Append_To
(L
, TmpD
);
3606 -- Expand aggregate into assignments to the temp array
3609 Late_Expansion
(Expr_Q
, Comp_Type
,
3610 New_Occurrence_Of
(TmpE
, Loc
)));
3615 Make_Assignment_Statement
(Loc
,
3616 Name
=> New_Copy_Tree
(Comp_Expr
),
3617 Expression
=> New_Occurrence_Of
(TmpE
, Loc
)));
3620 -- Normal case (sliding not required)
3624 Late_Expansion
(Expr_Q
, Comp_Type
, Comp_Expr
));
3627 -- Expr_Q is not delayed aggregate
3630 if Has_Discriminants
(Typ
) then
3631 Replace_Discriminants
(Expr_Q
);
3633 -- If the component is an array type that depends on
3634 -- discriminants, and the expression is a single Others
3635 -- clause, create an explicit subtype for it because the
3636 -- backend has troubles recovering the actual bounds.
3638 if Nkind
(Expr_Q
) = N_Aggregate
3639 and then Is_Array_Type
(Comp_Type
)
3640 and then Present
(Component_Associations
(Expr_Q
))
3643 Assoc
: constant Node_Id
:=
3644 First
(Component_Associations
(Expr_Q
));
3648 if Nkind
(First
(Choices
(Assoc
))) = N_Others_Choice
3651 Build_Actual_Subtype_Of_Component
3652 (Comp_Type
, Comp_Expr
);
3654 -- If the component type does not in fact depend on
3655 -- discriminants, the subtype declaration is empty.
3657 if Present
(Decl
) then
3658 Append_To
(L
, Decl
);
3659 Set_Etype
(Comp_Expr
, Defining_Entity
(Decl
));
3666 if Modify_Tree_For_C
3667 and then Nkind
(Expr_Q
) = N_Aggregate
3668 and then Is_Array_Type
(Etype
(Expr_Q
))
3669 and then Present
(First_Index
(Etype
(Expr_Q
)))
3672 Expr_Q_Type
: constant Node_Id
:= Etype
(Expr_Q
);
3675 Build_Array_Aggr_Code
3677 Ctype
=> Component_Type
(Expr_Q_Type
),
3678 Index
=> First_Index
(Expr_Q_Type
),
3681 Is_Scalar_Type
(Component_Type
(Expr_Q_Type
))));
3685 -- Handle an initialization expression of a controlled type
3686 -- in case it denotes a function call. In general such a
3687 -- scenario will produce a transient scope, but this will
3688 -- lead to wrong order of initialization, adjustment, and
3689 -- finalization in the context of aggregates.
3691 -- Target.Comp := Ctrl_Func_Call;
3694 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3695 -- Target.Comp := Trans_Obj;
3696 -- Finalize (Trans_Obj);
3698 -- Target.Comp._tag := ...;
3699 -- Adjust (Target.Comp);
3701 -- In the example above, the call to Finalize occurs too
3702 -- early and as a result it may leave the record component
3703 -- in a bad state. Finalization of the transient object
3704 -- should really happen after adjustment.
3706 -- To avoid this scenario, perform in-place side-effect
3707 -- removal of the function call. This eliminates the
3708 -- transient property of the function result and ensures
3709 -- correct order of actions.
3711 -- Res : ... := Ctrl_Func_Call;
3712 -- Target.Comp := Res;
3713 -- Target.Comp._tag := ...;
3714 -- Adjust (Target.Comp);
3717 if Needs_Finalization
(Comp_Type
)
3718 and then Nkind
(Expr_Q
) /= N_Aggregate
3720 Initialize_Ctrl_Record_Component
3721 (Rec_Comp
=> Comp_Expr
,
3722 Comp_Typ
=> Etype
(Selector
),
3723 Init_Expr
=> Expr_Q
,
3726 -- Otherwise perform single component initialization
3729 Initialize_Record_Component
3730 (Rec_Comp
=> Comp_Expr
,
3731 Comp_Typ
=> Etype
(Selector
),
3732 Init_Expr
=> Expr_Q
,
3738 -- comment would be good here ???
3740 elsif Ekind
(Selector
) = E_Discriminant
3741 and then Nkind
(N
) /= N_Extension_Aggregate
3742 and then Nkind
(Parent
(N
)) = N_Component_Association
3743 and then Is_Constrained
(Typ
)
3745 -- We must check that the discriminant value imposed by the
3746 -- context is the same as the value given in the subaggregate,
3747 -- because after the expansion into assignments there is no
3748 -- record on which to perform a regular discriminant check.
3755 D_Val
:= First_Elmt
(Discriminant_Constraint
(Typ
));
3756 Disc
:= First_Discriminant
(Typ
);
3757 while Chars
(Disc
) /= Chars
(Selector
) loop
3758 Next_Discriminant
(Disc
);
3762 pragma Assert
(Present
(D_Val
));
3764 -- This check cannot performed for components that are
3765 -- constrained by a current instance, because this is not a
3766 -- value that can be compared with the actual constraint.
3768 if Nkind
(Node
(D_Val
)) /= N_Attribute_Reference
3769 or else not Is_Entity_Name
(Prefix
(Node
(D_Val
)))
3770 or else not Is_Type
(Entity
(Prefix
(Node
(D_Val
))))
3773 Make_Raise_Constraint_Error
(Loc
,
3776 Left_Opnd
=> New_Copy_Tree
(Node
(D_Val
)),
3777 Right_Opnd
=> Expression
(Comp
)),
3778 Reason
=> CE_Discriminant_Check_Failed
));
3781 -- Find self-reference in previous discriminant assignment,
3782 -- and replace with proper expression.
3789 while Present
(Ass
) loop
3790 if Nkind
(Ass
) = N_Assignment_Statement
3791 and then Nkind
(Name
(Ass
)) = N_Selected_Component
3792 and then Chars
(Selector_Name
(Name
(Ass
))) =
3796 (Ass
, New_Copy_Tree
(Expression
(Comp
)));
3809 -- If the type is tagged, the tag needs to be initialized (unless we
3810 -- are in VM-mode where tags are implicit). It is done late in the
3811 -- initialization process because in some cases, we call the init
3812 -- proc of an ancestor which will not leave out the right tag.
3814 if Ancestor_Is_Expression
then
3817 -- For CPP types we generated a call to the C++ default constructor
3818 -- before the components have been initialized to ensure the proper
3819 -- initialization of the _Tag component (see above).
3821 elsif Is_CPP_Class
(Typ
) then
3824 elsif Is_Tagged_Type
(Typ
) and then Tagged_Type_Expansion
then
3826 Make_OK_Assignment_Statement
(Loc
,
3828 Make_Selected_Component
(Loc
,
3829 Prefix
=> New_Copy_Tree
(Target
),
3832 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
3835 Unchecked_Convert_To
(RTE
(RE_Tag
),
3837 (Node
(First_Elmt
(Access_Disp_Table
(Base_Type
(Typ
)))),
3840 Append_To
(L
, Instr
);
3842 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3843 -- abstract interfaces we must also initialize the tags of the
3844 -- secondary dispatch tables.
3846 if Has_Interfaces
(Base_Type
(Typ
)) then
3848 (Typ
=> Base_Type
(Typ
),
3854 -- If the controllers have not been initialized yet (by lack of non-
3855 -- discriminant components), let's do it now.
3857 Generate_Finalization_Actions
;
3860 end Build_Record_Aggr_Code
;
3862 ---------------------------------------
3863 -- Collect_Initialization_Statements --
3864 ---------------------------------------
3866 procedure Collect_Initialization_Statements
3869 Node_After
: Node_Id
)
3871 Loc
: constant Source_Ptr
:= Sloc
(N
);
3872 Init_Actions
: constant List_Id
:= New_List
;
3873 Init_Node
: Node_Id
;
3874 Comp_Stmt
: Node_Id
;
3877 -- Nothing to do if Obj is already frozen, as in this case we known we
3878 -- won't need to move the initialization statements about later on.
3880 if Is_Frozen
(Obj
) then
3885 while Next
(Init_Node
) /= Node_After
loop
3886 Append_To
(Init_Actions
, Remove_Next
(Init_Node
));
3889 if not Is_Empty_List
(Init_Actions
) then
3890 Comp_Stmt
:= Make_Compound_Statement
(Loc
, Actions
=> Init_Actions
);
3891 Insert_Action_After
(Init_Node
, Comp_Stmt
);
3892 Set_Initialization_Statements
(Obj
, Comp_Stmt
);
3894 end Collect_Initialization_Statements
;
3896 -------------------------------
3897 -- Convert_Aggr_In_Allocator --
3898 -------------------------------
3900 procedure Convert_Aggr_In_Allocator
3905 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3906 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3907 Temp
: constant Entity_Id
:= Defining_Identifier
(Decl
);
3909 Occ
: constant Node_Id
:=
3910 Unchecked_Convert_To
(Typ
,
3911 Make_Explicit_Dereference
(Loc
, New_Occurrence_Of
(Temp
, Loc
)));
3914 if Is_Array_Type
(Typ
) then
3915 Convert_Array_Aggr_In_Allocator
(Decl
, Aggr
, Occ
);
3917 elsif Has_Default_Init_Comps
(Aggr
) then
3919 L
: constant List_Id
:= New_List
;
3920 Init_Stmts
: List_Id
;
3923 Init_Stmts
:= Late_Expansion
(Aggr
, Typ
, Occ
);
3925 if Has_Task
(Typ
) then
3926 Build_Task_Allocate_Block_With_Init_Stmts
(L
, Aggr
, Init_Stmts
);
3927 Insert_Actions
(Alloc
, L
);
3929 Insert_Actions
(Alloc
, Init_Stmts
);
3934 Insert_Actions
(Alloc
, Late_Expansion
(Aggr
, Typ
, Occ
));
3936 end Convert_Aggr_In_Allocator
;
3938 --------------------------------
3939 -- Convert_Aggr_In_Assignment --
3940 --------------------------------
3942 procedure Convert_Aggr_In_Assignment
(N
: Node_Id
) is
3943 Aggr
: Node_Id
:= Expression
(N
);
3944 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3945 Occ
: constant Node_Id
:= New_Copy_Tree
(Name
(N
));
3948 if Nkind
(Aggr
) = N_Qualified_Expression
then
3949 Aggr
:= Expression
(Aggr
);
3952 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
3953 end Convert_Aggr_In_Assignment
;
3955 ---------------------------------
3956 -- Convert_Aggr_In_Object_Decl --
3957 ---------------------------------
3959 procedure Convert_Aggr_In_Object_Decl
(N
: Node_Id
) is
3960 Obj
: constant Entity_Id
:= Defining_Identifier
(N
);
3961 Aggr
: Node_Id
:= Expression
(N
);
3962 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3963 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3964 Occ
: constant Node_Id
:= New_Occurrence_Of
(Obj
, Loc
);
3966 function Discriminants_Ok
return Boolean;
3967 -- If the object type is constrained, the discriminants in the
3968 -- aggregate must be checked against the discriminants of the subtype.
3969 -- This cannot be done using Apply_Discriminant_Checks because after
3970 -- expansion there is no aggregate left to check.
3972 ----------------------
3973 -- Discriminants_Ok --
3974 ----------------------
3976 function Discriminants_Ok
return Boolean is
3977 Cond
: Node_Id
:= Empty
;
3986 D
:= First_Discriminant
(Typ
);
3987 Disc1
:= First_Elmt
(Discriminant_Constraint
(Typ
));
3988 Disc2
:= First_Elmt
(Discriminant_Constraint
(Etype
(Obj
)));
3989 while Present
(Disc1
) and then Present
(Disc2
) loop
3990 Val1
:= Node
(Disc1
);
3991 Val2
:= Node
(Disc2
);
3993 if not Is_OK_Static_Expression
(Val1
)
3994 or else not Is_OK_Static_Expression
(Val2
)
3996 Check
:= Make_Op_Ne
(Loc
,
3997 Left_Opnd
=> Duplicate_Subexpr
(Val1
),
3998 Right_Opnd
=> Duplicate_Subexpr
(Val2
));
4004 Cond
:= Make_Or_Else
(Loc
,
4006 Right_Opnd
=> Check
);
4009 elsif Expr_Value
(Val1
) /= Expr_Value
(Val2
) then
4010 Apply_Compile_Time_Constraint_Error
(Aggr
,
4011 Msg
=> "incorrect value for discriminant&??",
4012 Reason
=> CE_Discriminant_Check_Failed
,
4017 Next_Discriminant
(D
);
4022 -- If any discriminant constraint is non-static, emit a check
4024 if Present
(Cond
) then
4026 Make_Raise_Constraint_Error
(Loc
,
4028 Reason
=> CE_Discriminant_Check_Failed
));
4032 end Discriminants_Ok
;
4034 -- Start of processing for Convert_Aggr_In_Object_Decl
4037 Set_Assignment_OK
(Occ
);
4039 if Nkind
(Aggr
) = N_Qualified_Expression
then
4040 Aggr
:= Expression
(Aggr
);
4043 if Has_Discriminants
(Typ
)
4044 and then Typ
/= Etype
(Obj
)
4045 and then Is_Constrained
(Etype
(Obj
))
4046 and then not Discriminants_Ok
4051 -- If the context is an extended return statement, it has its own
4052 -- finalization machinery (i.e. works like a transient scope) and
4053 -- we do not want to create an additional one, because objects on
4054 -- the finalization list of the return must be moved to the caller's
4055 -- finalization list to complete the return.
4057 -- However, if the aggregate is limited, it is built in place, and the
4058 -- controlled components are not assigned to intermediate temporaries
4059 -- so there is no need for a transient scope in this case either.
4061 if Requires_Transient_Scope
(Typ
)
4062 and then Ekind
(Current_Scope
) /= E_Return_Statement
4063 and then not Is_Limited_Type
(Typ
)
4065 Establish_Transient_Scope
4068 Is_Controlled
(Typ
) or else Has_Controlled_Component
(Typ
));
4072 Node_After
: constant Node_Id
:= Next
(N
);
4074 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
4075 Collect_Initialization_Statements
(Obj
, N
, Node_After
);
4077 Set_No_Initialization
(N
);
4078 Initialize_Discriminants
(N
, Typ
);
4079 end Convert_Aggr_In_Object_Decl
;
4081 -------------------------------------
4082 -- Convert_Array_Aggr_In_Allocator --
4083 -------------------------------------
4085 procedure Convert_Array_Aggr_In_Allocator
4090 Aggr_Code
: List_Id
;
4091 Typ
: constant Entity_Id
:= Etype
(Aggr
);
4092 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
4095 -- The target is an explicit dereference of the allocated object.
4096 -- Generate component assignments to it, as for an aggregate that
4097 -- appears on the right-hand side of an assignment statement.
4100 Build_Array_Aggr_Code
(Aggr
,
4102 Index
=> First_Index
(Typ
),
4104 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
4106 Insert_Actions_After
(Decl
, Aggr_Code
);
4107 end Convert_Array_Aggr_In_Allocator
;
4109 ----------------------------
4110 -- Convert_To_Assignments --
4111 ----------------------------
4113 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
) is
4114 Loc
: constant Source_Ptr
:= Sloc
(N
);
4118 Aggr_Code
: List_Id
;
4120 Target_Expr
: Node_Id
;
4121 Parent_Kind
: Node_Kind
;
4122 Unc_Decl
: Boolean := False;
4123 Parent_Node
: Node_Id
;
4126 pragma Assert
(not Is_Static_Dispatch_Table_Aggregate
(N
));
4127 pragma Assert
(Is_Record_Type
(Typ
));
4129 Parent_Node
:= Parent
(N
);
4130 Parent_Kind
:= Nkind
(Parent_Node
);
4132 if Parent_Kind
= N_Qualified_Expression
then
4134 -- Check if we are in a unconstrained declaration because in this
4135 -- case the current delayed expansion mechanism doesn't work when
4136 -- the declared object size depend on the initializing expr.
4138 Parent_Node
:= Parent
(Parent_Node
);
4139 Parent_Kind
:= Nkind
(Parent_Node
);
4141 if Parent_Kind
= N_Object_Declaration
then
4143 not Is_Entity_Name
(Object_Definition
(Parent_Node
))
4144 or else Has_Discriminants
4145 (Entity
(Object_Definition
(Parent_Node
)))
4146 or else Is_Class_Wide_Type
4147 (Entity
(Object_Definition
(Parent_Node
)));
4151 -- Just set the Delay flag in the cases where the transformation will be
4152 -- done top down from above.
4156 -- Internal aggregate (transformed when expanding the parent)
4158 or else Parent_Kind
= N_Aggregate
4159 or else Parent_Kind
= N_Extension_Aggregate
4160 or else Parent_Kind
= N_Component_Association
4162 -- Allocator (see Convert_Aggr_In_Allocator)
4164 or else Parent_Kind
= N_Allocator
4166 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4168 or else (Parent_Kind
= N_Object_Declaration
and then not Unc_Decl
)
4170 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4171 -- assignments in init procs are taken into account.
4173 or else (Parent_Kind
= N_Assignment_Statement
4174 and then Inside_Init_Proc
)
4176 -- (Ada 2005) An inherently limited type in a return statement, which
4177 -- will be handled in a build-in-place fashion, and may be rewritten
4178 -- as an extended return and have its own finalization machinery.
4179 -- In the case of a simple return, the aggregate needs to be delayed
4180 -- until the scope for the return statement has been created, so
4181 -- that any finalization chain will be associated with that scope.
4182 -- For extended returns, we delay expansion to avoid the creation
4183 -- of an unwanted transient scope that could result in premature
4184 -- finalization of the return object (which is built in place
4185 -- within the caller's scope).
4188 (Is_Limited_View
(Typ
)
4190 (Nkind
(Parent
(Parent_Node
)) = N_Extended_Return_Statement
4191 or else Nkind
(Parent_Node
) = N_Simple_Return_Statement
))
4193 Set_Expansion_Delayed
(N
);
4197 -- Otherwise, if a transient scope is required, create it now. If we
4198 -- are within an initialization procedure do not create such, because
4199 -- the target of the assignment must not be declared within a local
4200 -- block, and because cleanup will take place on return from the
4201 -- initialization procedure.
4203 -- Should the condition be more restrictive ???
4205 if Requires_Transient_Scope
(Typ
) and then not Inside_Init_Proc
then
4206 Establish_Transient_Scope
(N
, Sec_Stack
=> Needs_Finalization
(Typ
));
4209 -- If the aggregate is nonlimited, create a temporary. If it is limited
4210 -- and context is an assignment, this is a subaggregate for an enclosing
4211 -- aggregate being expanded. It must be built in place, so use target of
4212 -- the current assignment.
4214 if Is_Limited_Type
(Typ
)
4215 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
4217 Target_Expr
:= New_Copy_Tree
(Name
(Parent
(N
)));
4218 Insert_Actions
(Parent
(N
),
4219 Build_Record_Aggr_Code
(N
, Typ
, Target_Expr
));
4220 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
4223 Temp
:= Make_Temporary
(Loc
, 'A', N
);
4225 -- If the type inherits unknown discriminants, use the view with
4226 -- known discriminants if available.
4228 if Has_Unknown_Discriminants
(Typ
)
4229 and then Present
(Underlying_Record_View
(Typ
))
4231 T
:= Underlying_Record_View
(Typ
);
4237 Make_Object_Declaration
(Loc
,
4238 Defining_Identifier
=> Temp
,
4239 Object_Definition
=> New_Occurrence_Of
(T
, Loc
));
4241 Set_No_Initialization
(Instr
);
4242 Insert_Action
(N
, Instr
);
4243 Initialize_Discriminants
(Instr
, T
);
4245 Target_Expr
:= New_Occurrence_Of
(Temp
, Loc
);
4246 Aggr_Code
:= Build_Record_Aggr_Code
(N
, T
, Target_Expr
);
4248 -- Save the last assignment statement associated with the aggregate
4249 -- when building a controlled object. This reference is utilized by
4250 -- the finalization machinery when marking an object as successfully
4253 if Needs_Finalization
(T
) then
4254 Set_Last_Aggregate_Assignment
(Temp
, Last
(Aggr_Code
));
4257 Insert_Actions
(N
, Aggr_Code
);
4258 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4259 Analyze_And_Resolve
(N
, T
);
4261 end Convert_To_Assignments
;
4263 ---------------------------
4264 -- Convert_To_Positional --
4265 ---------------------------
4267 procedure Convert_To_Positional
4269 Max_Others_Replicate
: Nat
:= 5;
4270 Handle_Bit_Packed
: Boolean := False)
4272 Typ
: constant Entity_Id
:= Etype
(N
);
4274 Static_Components
: Boolean := True;
4276 procedure Check_Static_Components
;
4277 -- Check whether all components of the aggregate are compile-time known
4278 -- values, and can be passed as is to the back-end without further
4280 -- An Iterated_Component_Association is treated as non-static, but there
4281 -- are possibilities for optimization here.
4286 Ixb
: Node_Id
) return Boolean;
4287 -- Convert the aggregate into a purely positional form if possible. On
4288 -- entry the bounds of all dimensions are known to be static, and the
4289 -- total number of components is safe enough to expand.
4291 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean;
4292 -- Return True iff the array N is flat (which is not trivial in the case
4293 -- of multidimensional aggregates).
4295 -----------------------------
4296 -- Check_Static_Components --
4297 -----------------------------
4299 -- Could use some comments in this body ???
4301 procedure Check_Static_Components
is
4305 Static_Components
:= True;
4307 if Nkind
(N
) = N_String_Literal
then
4310 elsif Present
(Expressions
(N
)) then
4311 Expr
:= First
(Expressions
(N
));
4312 while Present
(Expr
) loop
4313 if Nkind
(Expr
) /= N_Aggregate
4314 or else not Compile_Time_Known_Aggregate
(Expr
)
4315 or else Expansion_Delayed
(Expr
)
4317 Static_Components
:= False;
4325 if Nkind
(N
) = N_Aggregate
4326 and then Present
(Component_Associations
(N
))
4328 Expr
:= First
(Component_Associations
(N
));
4329 while Present
(Expr
) loop
4330 if Nkind_In
(Expression
(Expr
), N_Integer_Literal
,
4335 elsif Is_Entity_Name
(Expression
(Expr
))
4336 and then Present
(Entity
(Expression
(Expr
)))
4337 and then Ekind
(Entity
(Expression
(Expr
))) =
4338 E_Enumeration_Literal
4342 elsif Nkind
(Expression
(Expr
)) /= N_Aggregate
4343 or else not Compile_Time_Known_Aggregate
(Expression
(Expr
))
4344 or else Expansion_Delayed
(Expression
(Expr
))
4345 or else Nkind
(Expr
) = N_Iterated_Component_Association
4347 Static_Components
:= False;
4354 end Check_Static_Components
;
4363 Ixb
: Node_Id
) return Boolean
4365 Loc
: constant Source_Ptr
:= Sloc
(N
);
4366 Blo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ixb
));
4367 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ix
));
4368 Hi
: constant Node_Id
:= Type_High_Bound
(Etype
(Ix
));
4372 Others_Present
: Boolean := False;
4375 if Nkind
(Original_Node
(N
)) = N_String_Literal
then
4379 if not Compile_Time_Known_Value
(Lo
)
4380 or else not Compile_Time_Known_Value
(Hi
)
4385 Lov
:= Expr_Value
(Lo
);
4386 Hiv
:= Expr_Value
(Hi
);
4388 -- Check if there is an others choice
4390 if Present
(Component_Associations
(N
)) then
4396 Assoc
:= First
(Component_Associations
(N
));
4397 while Present
(Assoc
) loop
4399 -- If this is a box association, flattening is in general
4400 -- not possible because at this point we cannot tell if the
4401 -- default is static or even exists.
4403 if Box_Present
(Assoc
) then
4406 elsif Nkind
(Assoc
) = N_Iterated_Component_Association
then
4410 Choice
:= First
(Choice_List
(Assoc
));
4412 while Present
(Choice
) loop
4413 if Nkind
(Choice
) = N_Others_Choice
then
4414 Others_Present
:= True;
4425 -- If the low bound is not known at compile time and others is not
4426 -- present we can proceed since the bounds can be obtained from the
4430 or else (not Compile_Time_Known_Value
(Blo
) and then Others_Present
)
4435 -- Determine if set of alternatives is suitable for conversion and
4436 -- build an array containing the values in sequence.
4439 Vals
: array (UI_To_Int
(Lov
) .. UI_To_Int
(Hiv
))
4440 of Node_Id
:= (others => Empty
);
4441 -- The values in the aggregate sorted appropriately
4444 -- Same data as Vals in list form
4447 -- Used to validate Max_Others_Replicate limit
4450 Num
: Int
:= UI_To_Int
(Lov
);
4456 if Present
(Expressions
(N
)) then
4457 Elmt
:= First
(Expressions
(N
));
4458 while Present
(Elmt
) loop
4459 if Nkind
(Elmt
) = N_Aggregate
4460 and then Present
(Next_Index
(Ix
))
4462 not Flatten
(Elmt
, Next_Index
(Ix
), Next_Index
(Ixb
))
4467 Vals
(Num
) := Relocate_Node
(Elmt
);
4474 if No
(Component_Associations
(N
)) then
4478 Elmt
:= First
(Component_Associations
(N
));
4480 if Nkind
(Expression
(Elmt
)) = N_Aggregate
then
4481 if Present
(Next_Index
(Ix
))
4484 (Expression
(Elmt
), Next_Index
(Ix
), Next_Index
(Ixb
))
4490 Component_Loop
: while Present
(Elmt
) loop
4491 Choice
:= First
(Choice_List
(Elmt
));
4492 Choice_Loop
: while Present
(Choice
) loop
4494 -- If we have an others choice, fill in the missing elements
4495 -- subject to the limit established by Max_Others_Replicate.
4497 if Nkind
(Choice
) = N_Others_Choice
then
4500 for J
in Vals
'Range loop
4501 if No
(Vals
(J
)) then
4502 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
4503 Rep_Count
:= Rep_Count
+ 1;
4505 -- Check for maximum others replication. Note that
4506 -- we skip this test if either of the restrictions
4507 -- No_Elaboration_Code or No_Implicit_Loops is
4508 -- active, if this is a preelaborable unit or
4509 -- a predefined unit, or if the unit must be
4510 -- placed in data memory. This also ensures that
4511 -- predefined units get the same level of constant
4512 -- folding in Ada 95 and Ada 2005, where their
4513 -- categorization has changed.
4516 P
: constant Entity_Id
:=
4517 Cunit_Entity
(Current_Sem_Unit
);
4520 -- Check if duplication OK and if so continue
4523 if Restriction_Active
(No_Elaboration_Code
)
4524 or else Restriction_Active
(No_Implicit_Loops
)
4526 (Ekind
(Current_Scope
) = E_Package
4527 and then Static_Elaboration_Desired
4529 or else Is_Preelaborated
(P
)
4530 or else (Ekind
(P
) = E_Package_Body
4532 Is_Preelaborated
(Spec_Entity
(P
)))
4534 Is_Predefined_Unit
(Get_Source_Unit
(P
))
4538 -- If duplication not OK, then we return False
4539 -- if the replication count is too high
4541 elsif Rep_Count
> Max_Others_Replicate
then
4544 -- Continue on if duplication not OK, but the
4545 -- replication count is not excessive.
4554 exit Component_Loop
;
4556 -- Case of a subtype mark, identifier or expanded name
4558 elsif Is_Entity_Name
(Choice
)
4559 and then Is_Type
(Entity
(Choice
))
4561 Lo
:= Type_Low_Bound
(Etype
(Choice
));
4562 Hi
:= Type_High_Bound
(Etype
(Choice
));
4564 -- Case of subtype indication
4566 elsif Nkind
(Choice
) = N_Subtype_Indication
then
4567 Lo
:= Low_Bound
(Range_Expression
(Constraint
(Choice
)));
4568 Hi
:= High_Bound
(Range_Expression
(Constraint
(Choice
)));
4572 elsif Nkind
(Choice
) = N_Range
then
4573 Lo
:= Low_Bound
(Choice
);
4574 Hi
:= High_Bound
(Choice
);
4576 -- Normal subexpression case
4578 else pragma Assert
(Nkind
(Choice
) in N_Subexpr
);
4579 if not Compile_Time_Known_Value
(Choice
) then
4583 Choice_Index
:= UI_To_Int
(Expr_Value
(Choice
));
4585 if Choice_Index
in Vals
'Range then
4586 Vals
(Choice_Index
) :=
4587 New_Copy_Tree
(Expression
(Elmt
));
4590 -- Choice is statically out-of-range, will be
4591 -- rewritten to raise Constraint_Error.
4599 -- Range cases merge with Lo,Hi set
4601 if not Compile_Time_Known_Value
(Lo
)
4603 not Compile_Time_Known_Value
(Hi
)
4608 for J
in UI_To_Int
(Expr_Value
(Lo
)) ..
4609 UI_To_Int
(Expr_Value
(Hi
))
4611 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
4617 end loop Choice_Loop
;
4620 end loop Component_Loop
;
4622 -- If we get here the conversion is possible
4625 for J
in Vals
'Range loop
4626 Append
(Vals
(J
), Vlist
);
4629 Rewrite
(N
, Make_Aggregate
(Loc
, Expressions
=> Vlist
));
4630 Set_Aggregate_Bounds
(N
, Aggregate_Bounds
(Original_Node
(N
)));
4639 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean is
4646 elsif Nkind
(N
) = N_Aggregate
then
4647 if Present
(Component_Associations
(N
)) then
4651 Elmt
:= First
(Expressions
(N
));
4652 while Present
(Elmt
) loop
4653 if not Is_Flat
(Elmt
, Dims
- 1) then
4667 -- Start of processing for Convert_To_Positional
4670 -- Only convert to positional when generating C in case of an
4671 -- object declaration, this is the only case where aggregates are
4674 if Modify_Tree_For_C
and then not In_Object_Declaration
(N
) then
4678 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4679 -- components because in this case will need to call the corresponding
4682 if Has_Default_Init_Comps
(N
) then
4686 if Is_Flat
(N
, Number_Dimensions
(Typ
)) then
4690 if Is_Bit_Packed_Array
(Typ
) and then not Handle_Bit_Packed
then
4694 -- Do not convert to positional if controlled components are involved
4695 -- since these require special processing
4697 if Has_Controlled_Component
(Typ
) then
4701 Check_Static_Components
;
4703 -- If the size is known, or all the components are static, try to
4704 -- build a fully positional aggregate.
4706 -- The size of the type may not be known for an aggregate with
4707 -- discriminated array components, but if the components are static
4708 -- it is still possible to verify statically that the length is
4709 -- compatible with the upper bound of the type, and therefore it is
4710 -- worth flattening such aggregates as well.
4712 -- For now the back-end expands these aggregates into individual
4713 -- assignments to the target anyway, but it is conceivable that
4714 -- it will eventually be able to treat such aggregates statically???
4716 if Aggr_Size_OK
(N
, Typ
)
4717 and then Flatten
(N
, First_Index
(Typ
), First_Index
(Base_Type
(Typ
)))
4719 if Static_Components
then
4720 Set_Compile_Time_Known_Aggregate
(N
);
4721 Set_Expansion_Delayed
(N
, False);
4724 Analyze_And_Resolve
(N
, Typ
);
4727 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4728 -- that will still require initialization code.
4730 if (Ekind
(Current_Scope
) = E_Package
4731 and then Static_Elaboration_Desired
(Current_Scope
))
4732 and then Nkind
(Parent
(N
)) = N_Object_Declaration
4738 if Nkind
(N
) = N_Aggregate
and then Present
(Expressions
(N
)) then
4739 Expr
:= First
(Expressions
(N
));
4740 while Present
(Expr
) loop
4741 if Nkind_In
(Expr
, N_Integer_Literal
, N_Real_Literal
)
4743 (Is_Entity_Name
(Expr
)
4744 and then Ekind
(Entity
(Expr
)) = E_Enumeration_Literal
)
4750 ("non-static object requires elaboration code??", N
);
4757 if Present
(Component_Associations
(N
)) then
4758 Error_Msg_N
("object requires elaboration code??", N
);
4763 end Convert_To_Positional
;
4765 ----------------------------
4766 -- Expand_Array_Aggregate --
4767 ----------------------------
4769 -- Array aggregate expansion proceeds as follows:
4771 -- 1. If requested we generate code to perform all the array aggregate
4772 -- bound checks, specifically
4774 -- (a) Check that the index range defined by aggregate bounds is
4775 -- compatible with corresponding index subtype.
4777 -- (b) If an others choice is present check that no aggregate
4778 -- index is outside the bounds of the index constraint.
4780 -- (c) For multidimensional arrays make sure that all subaggregates
4781 -- corresponding to the same dimension have the same bounds.
4783 -- 2. Check for packed array aggregate which can be converted to a
4784 -- constant so that the aggregate disappears completely.
4786 -- 3. Check case of nested aggregate. Generally nested aggregates are
4787 -- handled during the processing of the parent aggregate.
4789 -- 4. Check if the aggregate can be statically processed. If this is the
4790 -- case pass it as is to Gigi. Note that a necessary condition for
4791 -- static processing is that the aggregate be fully positional.
4793 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4794 -- a temporary) then mark the aggregate as such and return. Otherwise
4795 -- create a new temporary and generate the appropriate initialization
4798 procedure Expand_Array_Aggregate
(N
: Node_Id
) is
4799 Loc
: constant Source_Ptr
:= Sloc
(N
);
4801 Typ
: constant Entity_Id
:= Etype
(N
);
4802 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
4803 -- Typ is the correct constrained array subtype of the aggregate
4804 -- Ctyp is the corresponding component type.
4806 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
4807 -- Number of aggregate index dimensions
4809 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
;
4810 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
;
4811 -- Low and High bounds of the constraint for each aggregate index
4813 Aggr_Index_Typ
: array (1 .. Aggr_Dimension
) of Entity_Id
;
4814 -- The type of each index
4816 In_Place_Assign_OK_For_Declaration
: Boolean := False;
4817 -- True if we are to generate an in place assignment for a declaration
4819 Maybe_In_Place_OK
: Boolean;
4820 -- If the type is neither controlled nor packed and the aggregate
4821 -- is the expression in an assignment, assignment in place may be
4822 -- possible, provided other conditions are met on the LHS.
4824 Others_Present
: array (1 .. Aggr_Dimension
) of Boolean :=
4826 -- If Others_Present (J) is True, then there is an others choice in one
4827 -- of the subaggregates of N at dimension J.
4829 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean;
4830 -- Returns true if an aggregate assignment can be done by the back end
4832 procedure Build_Constrained_Type
(Positional
: Boolean);
4833 -- If the subtype is not static or unconstrained, build a constrained
4834 -- type using the computable sizes of the aggregate and its sub-
4837 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
);
4838 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4841 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4842 -- Checks that in a multidimensional array aggregate all subaggregates
4843 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4844 -- an array subaggregate. Dim is the dimension corresponding to the
4847 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4848 -- Computes the values of array Others_Present. Sub_Aggr is the array
4849 -- subaggregate we start the computation from. Dim is the dimension
4850 -- corresponding to the subaggregate.
4852 function In_Place_Assign_OK
return Boolean;
4853 -- Simple predicate to determine whether an aggregate assignment can
4854 -- be done in place, because none of the new values can depend on the
4855 -- components of the target of the assignment.
4857 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4858 -- Checks that if an others choice is present in any subaggregate, no
4859 -- aggregate index is outside the bounds of the index constraint.
4860 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4861 -- to the subaggregate.
4863 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean;
4864 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4865 -- built directly into the target of the assignment it must be free
4868 ------------------------------------
4869 -- Aggr_Assignment_OK_For_Backend --
4870 ------------------------------------
4872 -- Backend processing by Gigi/gcc is possible only if all the following
4873 -- conditions are met:
4875 -- 1. N consists of a single OTHERS choice, possibly recursively
4877 -- 2. The array type is not packed
4879 -- 3. The array type has no atomic components
4881 -- 4. The array type has no null ranges (the purpose of this is to
4882 -- avoid a bogus warning for an out-of-range value).
4884 -- 5. The component type is discrete
4886 -- 6. The component size is Storage_Unit or the value is of the form
4887 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4888 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4889 -- the 8-bit value M, concatenated together.
4891 -- The ultimate goal is to generate a call to a fast memset routine
4892 -- specifically optimized for the target.
4894 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean is
4897 Expr
: Node_Id
:= N
;
4905 -- Recurse as far as possible to find the innermost component type
4908 while Is_Array_Type
(Ctyp
) loop
4909 if Nkind
(Expr
) /= N_Aggregate
4910 or else not Is_Others_Aggregate
(Expr
)
4915 if Present
(Packed_Array_Impl_Type
(Ctyp
)) then
4919 if Has_Atomic_Components
(Ctyp
) then
4923 Index
:= First_Index
(Ctyp
);
4924 while Present
(Index
) loop
4925 Get_Index_Bounds
(Index
, Low
, High
);
4927 if Is_Null_Range
(Low
, High
) then
4934 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
4936 for J
in 1 .. Number_Dimensions
(Ctyp
) - 1 loop
4937 if Nkind
(Expr
) /= N_Aggregate
4938 or else not Is_Others_Aggregate
(Expr
)
4943 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
4946 Ctyp
:= Component_Type
(Ctyp
);
4948 if Is_Atomic_Or_VFA
(Ctyp
) then
4953 -- An Iterated_Component_Association involves a loop (in most cases)
4954 -- and is never static.
4956 if Nkind
(Parent
(Expr
)) = N_Iterated_Component_Association
then
4960 if not Is_Discrete_Type
(Ctyp
) then
4964 -- The expression needs to be analyzed if True is returned
4966 Analyze_And_Resolve
(Expr
, Ctyp
);
4968 -- The back end uses the Esize as the precision of the type
4970 Nunits
:= UI_To_Int
(Esize
(Ctyp
)) / System_Storage_Unit
;
4976 if not Compile_Time_Known_Value
(Expr
) then
4980 Value
:= Expr_Value
(Expr
);
4982 if Has_Biased_Representation
(Ctyp
) then
4983 Value
:= Value
- Expr_Value
(Type_Low_Bound
(Ctyp
));
4986 -- Values 0 and -1 immediately satisfy the last check
4988 if Value
= Uint_0
or else Value
= Uint_Minus_1
then
4992 -- We need to work with an unsigned value
4995 Value
:= Value
+ 2**(System_Storage_Unit
* Nunits
);
4998 Remainder
:= Value
rem 2**System_Storage_Unit
;
5000 for J
in 1 .. Nunits
- 1 loop
5001 Value
:= Value
/ 2**System_Storage_Unit
;
5003 if Value
rem 2**System_Storage_Unit
/= Remainder
then
5009 end Aggr_Assignment_OK_For_Backend
;
5011 ----------------------------
5012 -- Build_Constrained_Type --
5013 ----------------------------
5015 procedure Build_Constrained_Type
(Positional
: Boolean) is
5016 Loc
: constant Source_Ptr
:= Sloc
(N
);
5017 Agg_Type
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
5020 Typ
: constant Entity_Id
:= Etype
(N
);
5021 Indexes
: constant List_Id
:= New_List
;
5026 -- If the aggregate is purely positional, all its subaggregates
5027 -- have the same size. We collect the dimensions from the first
5028 -- subaggregate at each level.
5033 for D
in 1 .. Number_Dimensions
(Typ
) loop
5034 Sub_Agg
:= First
(Expressions
(Sub_Agg
));
5038 while Present
(Comp
) loop
5045 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
5046 High_Bound
=> Make_Integer_Literal
(Loc
, Num
)));
5050 -- We know the aggregate type is unconstrained and the aggregate
5051 -- is not processable by the back end, therefore not necessarily
5052 -- positional. Retrieve each dimension bounds (computed earlier).
5054 for D
in 1 .. Number_Dimensions
(Typ
) loop
5057 Low_Bound
=> Aggr_Low
(D
),
5058 High_Bound
=> Aggr_High
(D
)));
5063 Make_Full_Type_Declaration
(Loc
,
5064 Defining_Identifier
=> Agg_Type
,
5066 Make_Constrained_Array_Definition
(Loc
,
5067 Discrete_Subtype_Definitions
=> Indexes
,
5068 Component_Definition
=>
5069 Make_Component_Definition
(Loc
,
5070 Aliased_Present
=> False,
5071 Subtype_Indication
=>
5072 New_Occurrence_Of
(Component_Type
(Typ
), Loc
))));
5074 Insert_Action
(N
, Decl
);
5076 Set_Etype
(N
, Agg_Type
);
5077 Set_Is_Itype
(Agg_Type
);
5078 Freeze_Itype
(Agg_Type
, N
);
5079 end Build_Constrained_Type
;
5085 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
) is
5092 Cond
: Node_Id
:= Empty
;
5095 Get_Index_Bounds
(Aggr_Bounds
, Aggr_Lo
, Aggr_Hi
);
5096 Get_Index_Bounds
(Index_Bounds
, Ind_Lo
, Ind_Hi
);
5098 -- Generate the following test:
5100 -- [constraint_error when
5101 -- Aggr_Lo <= Aggr_Hi and then
5102 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5104 -- As an optimization try to see if some tests are trivially vacuous
5105 -- because we are comparing an expression against itself.
5107 if Aggr_Lo
= Ind_Lo
and then Aggr_Hi
= Ind_Hi
then
5110 elsif Aggr_Hi
= Ind_Hi
then
5113 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5114 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
));
5116 elsif Aggr_Lo
= Ind_Lo
then
5119 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
5120 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Hi
));
5127 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5128 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
)),
5132 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
5133 Right_Opnd
=> Duplicate_Subexpr
(Ind_Hi
)));
5136 if Present
(Cond
) then
5141 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5142 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
)),
5144 Right_Opnd
=> Cond
);
5146 Set_Analyzed
(Left_Opnd
(Left_Opnd
(Cond
)), False);
5147 Set_Analyzed
(Right_Opnd
(Left_Opnd
(Cond
)), False);
5149 Make_Raise_Constraint_Error
(Loc
,
5151 Reason
=> CE_Range_Check_Failed
));
5155 ----------------------------
5156 -- Check_Same_Aggr_Bounds --
5157 ----------------------------
5159 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5160 Sub_Lo
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(Sub_Aggr
));
5161 Sub_Hi
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(Sub_Aggr
));
5162 -- The bounds of this specific subaggregate
5164 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
5165 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
5166 -- The bounds of the aggregate for this dimension
5168 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
5169 -- The index type for this dimension.xxx
5171 Cond
: Node_Id
:= Empty
;
5176 -- If index checks are on generate the test
5178 -- [constraint_error when
5179 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5181 -- As an optimization try to see if some tests are trivially vacuos
5182 -- because we are comparing an expression against itself. Also for
5183 -- the first dimension the test is trivially vacuous because there
5184 -- is just one aggregate for dimension 1.
5186 if Index_Checks_Suppressed
(Ind_Typ
) then
5189 elsif Dim
= 1 or else (Aggr_Lo
= Sub_Lo
and then Aggr_Hi
= Sub_Hi
)
5193 elsif Aggr_Hi
= Sub_Hi
then
5196 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5197 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
));
5199 elsif Aggr_Lo
= Sub_Lo
then
5202 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
5203 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Hi
));
5210 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
5211 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
)),
5215 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
5216 Right_Opnd
=> Duplicate_Subexpr
(Sub_Hi
)));
5219 if Present
(Cond
) then
5221 Make_Raise_Constraint_Error
(Loc
,
5223 Reason
=> CE_Length_Check_Failed
));
5226 -- Now look inside the subaggregate to see if there is more work
5228 if Dim
< Aggr_Dimension
then
5230 -- Process positional components
5232 if Present
(Expressions
(Sub_Aggr
)) then
5233 Expr
:= First
(Expressions
(Sub_Aggr
));
5234 while Present
(Expr
) loop
5235 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
5240 -- Process component associations
5242 if Present
(Component_Associations
(Sub_Aggr
)) then
5243 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5244 while Present
(Assoc
) loop
5245 Expr
:= Expression
(Assoc
);
5246 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
5251 end Check_Same_Aggr_Bounds
;
5253 ----------------------------
5254 -- Compute_Others_Present --
5255 ----------------------------
5257 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5262 if Present
(Component_Associations
(Sub_Aggr
)) then
5263 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
5265 if Nkind
(First
(Choice_List
(Assoc
))) = N_Others_Choice
then
5266 Others_Present
(Dim
) := True;
5270 -- Now look inside the subaggregate to see if there is more work
5272 if Dim
< Aggr_Dimension
then
5274 -- Process positional components
5276 if Present
(Expressions
(Sub_Aggr
)) then
5277 Expr
:= First
(Expressions
(Sub_Aggr
));
5278 while Present
(Expr
) loop
5279 Compute_Others_Present
(Expr
, Dim
+ 1);
5284 -- Process component associations
5286 if Present
(Component_Associations
(Sub_Aggr
)) then
5287 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5288 while Present
(Assoc
) loop
5289 Expr
:= Expression
(Assoc
);
5290 Compute_Others_Present
(Expr
, Dim
+ 1);
5295 end Compute_Others_Present
;
5297 ------------------------
5298 -- In_Place_Assign_OK --
5299 ------------------------
5301 function In_Place_Assign_OK
return Boolean is
5309 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean;
5310 -- Check recursively that each component of a (sub)aggregate does not
5311 -- depend on the variable being assigned to.
5313 function Safe_Component
(Expr
: Node_Id
) return Boolean;
5314 -- Verify that an expression cannot depend on the variable being
5315 -- assigned to. Room for improvement here (but less than before).
5317 --------------------
5318 -- Safe_Aggregate --
5319 --------------------
5321 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean is
5325 if Present
(Expressions
(Aggr
)) then
5326 Expr
:= First
(Expressions
(Aggr
));
5327 while Present
(Expr
) loop
5328 if Nkind
(Expr
) = N_Aggregate
then
5329 if not Safe_Aggregate
(Expr
) then
5333 elsif not Safe_Component
(Expr
) then
5341 if Present
(Component_Associations
(Aggr
)) then
5342 Expr
:= First
(Component_Associations
(Aggr
));
5343 while Present
(Expr
) loop
5344 if Nkind
(Expression
(Expr
)) = N_Aggregate
then
5345 if not Safe_Aggregate
(Expression
(Expr
)) then
5349 -- If association has a box, no way to determine yet
5350 -- whether default can be assigned in place.
5352 elsif Box_Present
(Expr
) then
5355 elsif not Safe_Component
(Expression
(Expr
)) then
5366 --------------------
5367 -- Safe_Component --
5368 --------------------
5370 function Safe_Component
(Expr
: Node_Id
) return Boolean is
5371 Comp
: Node_Id
:= Expr
;
5373 function Check_Component
(Comp
: Node_Id
) return Boolean;
5374 -- Do the recursive traversal, after copy
5376 ---------------------
5377 -- Check_Component --
5378 ---------------------
5380 function Check_Component
(Comp
: Node_Id
) return Boolean is
5382 if Is_Overloaded
(Comp
) then
5386 return Compile_Time_Known_Value
(Comp
)
5388 or else (Is_Entity_Name
(Comp
)
5389 and then Present
(Entity
(Comp
))
5390 and then No
(Renamed_Object
(Entity
(Comp
))))
5392 or else (Nkind
(Comp
) = N_Attribute_Reference
5393 and then Check_Component
(Prefix
(Comp
)))
5395 or else (Nkind
(Comp
) in N_Binary_Op
5396 and then Check_Component
(Left_Opnd
(Comp
))
5397 and then Check_Component
(Right_Opnd
(Comp
)))
5399 or else (Nkind
(Comp
) in N_Unary_Op
5400 and then Check_Component
(Right_Opnd
(Comp
)))
5402 or else (Nkind
(Comp
) = N_Selected_Component
5403 and then Check_Component
(Prefix
(Comp
)))
5405 or else (Nkind
(Comp
) = N_Unchecked_Type_Conversion
5406 and then Check_Component
(Expression
(Comp
)));
5407 end Check_Component
;
5409 -- Start of processing for Safe_Component
5412 -- If the component appears in an association that may correspond
5413 -- to more than one element, it is not analyzed before expansion
5414 -- into assignments, to avoid side effects. We analyze, but do not
5415 -- resolve the copy, to obtain sufficient entity information for
5416 -- the checks that follow. If component is overloaded we assume
5417 -- an unsafe function call.
5419 if not Analyzed
(Comp
) then
5420 if Is_Overloaded
(Expr
) then
5423 elsif Nkind
(Expr
) = N_Aggregate
5424 and then not Is_Others_Aggregate
(Expr
)
5428 elsif Nkind
(Expr
) = N_Allocator
then
5430 -- For now, too complex to analyze
5435 Comp
:= New_Copy_Tree
(Expr
);
5436 Set_Parent
(Comp
, Parent
(Expr
));
5440 if Nkind
(Comp
) = N_Aggregate
then
5441 return Safe_Aggregate
(Comp
);
5443 return Check_Component
(Comp
);
5447 -- Start of processing for In_Place_Assign_OK
5450 if Present
(Component_Associations
(N
)) then
5452 -- On assignment, sliding can take place, so we cannot do the
5453 -- assignment in place unless the bounds of the aggregate are
5454 -- statically equal to those of the target.
5456 -- If the aggregate is given by an others choice, the bounds are
5457 -- derived from the left-hand side, and the assignment is safe if
5458 -- the expression is.
5460 if Is_Others_Aggregate
(N
) then
5463 (Expression
(First
(Component_Associations
(N
))));
5466 Aggr_In
:= First_Index
(Etype
(N
));
5468 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
5469 Obj_In
:= First_Index
(Etype
(Name
(Parent
(N
))));
5472 -- Context is an allocator. Check bounds of aggregate against
5473 -- given type in qualified expression.
5475 pragma Assert
(Nkind
(Parent
(Parent
(N
))) = N_Allocator
);
5477 First_Index
(Etype
(Entity
(Subtype_Mark
(Parent
(N
)))));
5480 while Present
(Aggr_In
) loop
5481 Get_Index_Bounds
(Aggr_In
, Aggr_Lo
, Aggr_Hi
);
5482 Get_Index_Bounds
(Obj_In
, Obj_Lo
, Obj_Hi
);
5484 if not Compile_Time_Known_Value
(Aggr_Lo
)
5485 or else not Compile_Time_Known_Value
(Aggr_Hi
)
5486 or else not Compile_Time_Known_Value
(Obj_Lo
)
5487 or else not Compile_Time_Known_Value
(Obj_Hi
)
5488 or else Expr_Value
(Aggr_Lo
) /= Expr_Value
(Obj_Lo
)
5489 or else Expr_Value
(Aggr_Hi
) /= Expr_Value
(Obj_Hi
)
5494 Next_Index
(Aggr_In
);
5495 Next_Index
(Obj_In
);
5499 -- Now check the component values themselves
5501 return Safe_Aggregate
(N
);
5502 end In_Place_Assign_OK
;
5508 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
5509 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
5510 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
5511 -- The bounds of the aggregate for this dimension
5513 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
5514 -- The index type for this dimension
5516 Need_To_Check
: Boolean := False;
5518 Choices_Lo
: Node_Id
:= Empty
;
5519 Choices_Hi
: Node_Id
:= Empty
;
5520 -- The lowest and highest discrete choices for a named subaggregate
5522 Nb_Choices
: Int
:= -1;
5523 -- The number of discrete non-others choices in this subaggregate
5525 Nb_Elements
: Uint
:= Uint_0
;
5526 -- The number of elements in a positional aggregate
5528 Cond
: Node_Id
:= Empty
;
5535 -- Check if we have an others choice. If we do make sure that this
5536 -- subaggregate contains at least one element in addition to the
5539 if Range_Checks_Suppressed
(Ind_Typ
) then
5540 Need_To_Check
:= False;
5542 elsif Present
(Expressions
(Sub_Aggr
))
5543 and then Present
(Component_Associations
(Sub_Aggr
))
5545 Need_To_Check
:= True;
5547 elsif Present
(Component_Associations
(Sub_Aggr
)) then
5548 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
5550 if Nkind
(First
(Choice_List
(Assoc
))) /= N_Others_Choice
then
5551 Need_To_Check
:= False;
5554 -- Count the number of discrete choices. Start with -1 because
5555 -- the others choice does not count.
5557 -- Is there some reason we do not use List_Length here ???
5560 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5561 while Present
(Assoc
) loop
5562 Choice
:= First
(Choice_List
(Assoc
));
5563 while Present
(Choice
) loop
5564 Nb_Choices
:= Nb_Choices
+ 1;
5571 -- If there is only an others choice nothing to do
5573 Need_To_Check
:= (Nb_Choices
> 0);
5577 Need_To_Check
:= False;
5580 -- If we are dealing with a positional subaggregate with an others
5581 -- choice then compute the number or positional elements.
5583 if Need_To_Check
and then Present
(Expressions
(Sub_Aggr
)) then
5584 Expr
:= First
(Expressions
(Sub_Aggr
));
5585 Nb_Elements
:= Uint_0
;
5586 while Present
(Expr
) loop
5587 Nb_Elements
:= Nb_Elements
+ 1;
5591 -- If the aggregate contains discrete choices and an others choice
5592 -- compute the smallest and largest discrete choice values.
5594 elsif Need_To_Check
then
5595 Compute_Choices_Lo_And_Choices_Hi
: declare
5597 Table
: Case_Table_Type
(1 .. Nb_Choices
);
5598 -- Used to sort all the different choice values
5605 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5606 while Present
(Assoc
) loop
5607 Choice
:= First
(Choice_List
(Assoc
));
5608 while Present
(Choice
) loop
5609 if Nkind
(Choice
) = N_Others_Choice
then
5613 Get_Index_Bounds
(Choice
, Low
, High
);
5614 Table
(J
).Choice_Lo
:= Low
;
5615 Table
(J
).Choice_Hi
:= High
;
5624 -- Sort the discrete choices
5626 Sort_Case_Table
(Table
);
5628 Choices_Lo
:= Table
(1).Choice_Lo
;
5629 Choices_Hi
:= Table
(Nb_Choices
).Choice_Hi
;
5630 end Compute_Choices_Lo_And_Choices_Hi
;
5633 -- If no others choice in this subaggregate, or the aggregate
5634 -- comprises only an others choice, nothing to do.
5636 if not Need_To_Check
then
5639 -- If we are dealing with an aggregate containing an others choice
5640 -- and positional components, we generate the following test:
5642 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5643 -- Ind_Typ'Pos (Aggr_Hi)
5645 -- raise Constraint_Error;
5648 elsif Nb_Elements
> Uint_0
then
5654 Make_Attribute_Reference
(Loc
,
5655 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
5656 Attribute_Name
=> Name_Pos
,
5659 (Duplicate_Subexpr_Move_Checks
(Aggr_Lo
))),
5660 Right_Opnd
=> Make_Integer_Literal
(Loc
, Nb_Elements
- 1)),
5663 Make_Attribute_Reference
(Loc
,
5664 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
5665 Attribute_Name
=> Name_Pos
,
5666 Expressions
=> New_List
(
5667 Duplicate_Subexpr_Move_Checks
(Aggr_Hi
))));
5669 -- If we are dealing with an aggregate containing an others choice
5670 -- and discrete choices we generate the following test:
5672 -- [constraint_error when
5673 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5680 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Choices_Lo
),
5681 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
)),
5685 Left_Opnd
=> Duplicate_Subexpr
(Choices_Hi
),
5686 Right_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
)));
5689 if Present
(Cond
) then
5691 Make_Raise_Constraint_Error
(Loc
,
5693 Reason
=> CE_Length_Check_Failed
));
5694 -- Questionable reason code, shouldn't that be a
5695 -- CE_Range_Check_Failed ???
5698 -- Now look inside the subaggregate to see if there is more work
5700 if Dim
< Aggr_Dimension
then
5702 -- Process positional components
5704 if Present
(Expressions
(Sub_Aggr
)) then
5705 Expr
:= First
(Expressions
(Sub_Aggr
));
5706 while Present
(Expr
) loop
5707 Others_Check
(Expr
, Dim
+ 1);
5712 -- Process component associations
5714 if Present
(Component_Associations
(Sub_Aggr
)) then
5715 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5716 while Present
(Assoc
) loop
5717 Expr
:= Expression
(Assoc
);
5718 Others_Check
(Expr
, Dim
+ 1);
5725 -------------------------
5726 -- Safe_Left_Hand_Side --
5727 -------------------------
5729 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean is
5730 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean;
5731 -- If the left-hand side includes an indexed component, check that
5732 -- the indexes are free of side effects.
5738 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean is
5740 if Is_Entity_Name
(Indx
) then
5743 elsif Nkind
(Indx
) = N_Integer_Literal
then
5746 elsif Nkind
(Indx
) = N_Function_Call
5747 and then Is_Entity_Name
(Name
(Indx
))
5748 and then Has_Pragma_Pure_Function
(Entity
(Name
(Indx
)))
5752 elsif Nkind
(Indx
) = N_Type_Conversion
5753 and then Is_Safe_Index
(Expression
(Indx
))
5762 -- Start of processing for Safe_Left_Hand_Side
5765 if Is_Entity_Name
(N
) then
5768 elsif Nkind_In
(N
, N_Explicit_Dereference
, N_Selected_Component
)
5769 and then Safe_Left_Hand_Side
(Prefix
(N
))
5773 elsif Nkind
(N
) = N_Indexed_Component
5774 and then Safe_Left_Hand_Side
(Prefix
(N
))
5775 and then Is_Safe_Index
(First
(Expressions
(N
)))
5779 elsif Nkind
(N
) = N_Unchecked_Type_Conversion
then
5780 return Safe_Left_Hand_Side
(Expression
(N
));
5785 end Safe_Left_Hand_Side
;
5790 -- Holds the temporary aggregate value
5793 -- Holds the declaration of Tmp
5795 Aggr_Code
: List_Id
;
5796 Parent_Node
: Node_Id
;
5797 Parent_Kind
: Node_Kind
;
5799 -- Start of processing for Expand_Array_Aggregate
5802 -- Do not touch the special aggregates of attributes used for Asm calls
5804 if Is_RTE
(Ctyp
, RE_Asm_Input_Operand
)
5805 or else Is_RTE
(Ctyp
, RE_Asm_Output_Operand
)
5809 -- Do not expand an aggregate for an array type which contains tasks if
5810 -- the aggregate is associated with an unexpanded return statement of a
5811 -- build-in-place function. The aggregate is expanded when the related
5812 -- return statement (rewritten into an extended return) is processed.
5813 -- This delay ensures that any temporaries and initialization code
5814 -- generated for the aggregate appear in the proper return block and
5815 -- use the correct _chain and _master.
5817 elsif Has_Task
(Base_Type
(Etype
(N
)))
5818 and then Nkind
(Parent
(N
)) = N_Simple_Return_Statement
5819 and then Is_Build_In_Place_Function
5820 (Return_Applies_To
(Return_Statement_Entity
(Parent
(N
))))
5824 -- Do not attempt expansion if error already detected. We may reach this
5825 -- point in spite of previous errors when compiling with -gnatq, to
5826 -- force all possible errors (this is the usual ACATS mode).
5828 elsif Error_Posted
(N
) then
5832 -- If the semantic analyzer has determined that aggregate N will raise
5833 -- Constraint_Error at run time, then the aggregate node has been
5834 -- replaced with an N_Raise_Constraint_Error node and we should
5837 pragma Assert
(not Raises_Constraint_Error
(N
));
5841 -- Check that the index range defined by aggregate bounds is
5842 -- compatible with corresponding index subtype.
5844 Index_Compatibility_Check
: declare
5845 Aggr_Index_Range
: Node_Id
:= First_Index
(Typ
);
5846 -- The current aggregate index range
5848 Index_Constraint
: Node_Id
:= First_Index
(Etype
(Typ
));
5849 -- The corresponding index constraint against which we have to
5850 -- check the above aggregate index range.
5853 Compute_Others_Present
(N
, 1);
5855 for J
in 1 .. Aggr_Dimension
loop
5856 -- There is no need to emit a check if an others choice is present
5857 -- for this array aggregate dimension since in this case one of
5858 -- N's subaggregates has taken its bounds from the context and
5859 -- these bounds must have been checked already. In addition all
5860 -- subaggregates corresponding to the same dimension must all have
5861 -- the same bounds (checked in (c) below).
5863 if not Range_Checks_Suppressed
(Etype
(Index_Constraint
))
5864 and then not Others_Present
(J
)
5866 -- We don't use Checks.Apply_Range_Check here because it emits
5867 -- a spurious check. Namely it checks that the range defined by
5868 -- the aggregate bounds is nonempty. But we know this already
5871 Check_Bounds
(Aggr_Index_Range
, Index_Constraint
);
5874 -- Save the low and high bounds of the aggregate index as well as
5875 -- the index type for later use in checks (b) and (c) below.
5877 Aggr_Low
(J
) := Low_Bound
(Aggr_Index_Range
);
5878 Aggr_High
(J
) := High_Bound
(Aggr_Index_Range
);
5880 Aggr_Index_Typ
(J
) := Etype
(Index_Constraint
);
5882 Next_Index
(Aggr_Index_Range
);
5883 Next_Index
(Index_Constraint
);
5885 end Index_Compatibility_Check
;
5889 -- If an others choice is present check that no aggregate index is
5890 -- outside the bounds of the index constraint.
5892 Others_Check
(N
, 1);
5896 -- For multidimensional arrays make sure that all subaggregates
5897 -- corresponding to the same dimension have the same bounds.
5899 if Aggr_Dimension
> 1 then
5900 Check_Same_Aggr_Bounds
(N
, 1);
5905 -- If we have a default component value, or simple initialization is
5906 -- required for the component type, then we replace <> in component
5907 -- associations by the required default value.
5910 Default_Val
: Node_Id
;
5914 if (Present
(Default_Aspect_Component_Value
(Typ
))
5915 or else Needs_Simple_Initialization
(Ctyp
))
5916 and then Present
(Component_Associations
(N
))
5918 Assoc
:= First
(Component_Associations
(N
));
5919 while Present
(Assoc
) loop
5920 if Nkind
(Assoc
) = N_Component_Association
5921 and then Box_Present
(Assoc
)
5923 Set_Box_Present
(Assoc
, False);
5925 if Present
(Default_Aspect_Component_Value
(Typ
)) then
5926 Default_Val
:= Default_Aspect_Component_Value
(Typ
);
5928 Default_Val
:= Get_Simple_Init_Val
(Ctyp
, N
);
5931 Set_Expression
(Assoc
, New_Copy_Tree
(Default_Val
));
5932 Analyze_And_Resolve
(Expression
(Assoc
), Ctyp
);
5942 -- Here we test for is packed array aggregate that we can handle at
5943 -- compile time. If so, return with transformation done. Note that we do
5944 -- this even if the aggregate is nested, because once we have done this
5945 -- processing, there is no more nested aggregate.
5947 if Packed_Array_Aggregate_Handled
(N
) then
5951 -- At this point we try to convert to positional form
5953 if Ekind
(Current_Scope
) = E_Package
5954 and then Static_Elaboration_Desired
(Current_Scope
)
5956 Convert_To_Positional
(N
, Max_Others_Replicate
=> 100);
5958 Convert_To_Positional
(N
);
5961 -- if the result is no longer an aggregate (e.g. it may be a string
5962 -- literal, or a temporary which has the needed value), then we are
5963 -- done, since there is no longer a nested aggregate.
5965 if Nkind
(N
) /= N_Aggregate
then
5968 -- We are also done if the result is an analyzed aggregate, indicating
5969 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5972 elsif Analyzed
(N
) and then N
/= Original_Node
(N
) then
5976 -- If all aggregate components are compile-time known and the aggregate
5977 -- has been flattened, nothing left to do. The same occurs if the
5978 -- aggregate is used to initialize the components of a statically
5979 -- allocated dispatch table.
5981 if Compile_Time_Known_Aggregate
(N
)
5982 or else Is_Static_Dispatch_Table_Aggregate
(N
)
5984 Set_Expansion_Delayed
(N
, False);
5988 -- Now see if back end processing is possible
5990 if Backend_Processing_Possible
(N
) then
5992 -- If the aggregate is static but the constraints are not, build
5993 -- a static subtype for the aggregate, so that Gigi can place it
5994 -- in static memory. Perform an unchecked_conversion to the non-
5995 -- static type imposed by the context.
5998 Itype
: constant Entity_Id
:= Etype
(N
);
6000 Needs_Type
: Boolean := False;
6003 Index
:= First_Index
(Itype
);
6004 while Present
(Index
) loop
6005 if not Is_OK_Static_Subtype
(Etype
(Index
)) then
6014 Build_Constrained_Type
(Positional
=> True);
6015 Rewrite
(N
, Unchecked_Convert_To
(Itype
, N
));
6025 -- Delay expansion for nested aggregates: it will be taken care of when
6026 -- the parent aggregate is expanded.
6028 Parent_Node
:= Parent
(N
);
6029 Parent_Kind
:= Nkind
(Parent_Node
);
6031 if Parent_Kind
= N_Qualified_Expression
then
6032 Parent_Node
:= Parent
(Parent_Node
);
6033 Parent_Kind
:= Nkind
(Parent_Node
);
6036 if Parent_Kind
= N_Aggregate
6037 or else Parent_Kind
= N_Extension_Aggregate
6038 or else Parent_Kind
= N_Component_Association
6039 or else (Parent_Kind
= N_Object_Declaration
6040 and then Needs_Finalization
(Typ
))
6041 or else (Parent_Kind
= N_Assignment_Statement
6042 and then Inside_Init_Proc
)
6044 if Static_Array_Aggregate
(N
)
6045 or else Compile_Time_Known_Aggregate
(N
)
6047 Set_Expansion_Delayed
(N
, False);
6050 Set_Expansion_Delayed
(N
);
6057 -- Look if in place aggregate expansion is possible
6059 -- For object declarations we build the aggregate in place, unless
6060 -- the array is bit-packed or the component is controlled.
6062 -- For assignments we do the assignment in place if all the component
6063 -- associations have compile-time known values. For other cases we
6064 -- create a temporary. The analysis for safety of on-line assignment
6065 -- is delicate, i.e. we don't know how to do it fully yet ???
6067 -- For allocators we assign to the designated object in place if the
6068 -- aggregate meets the same conditions as other in-place assignments.
6069 -- In this case the aggregate may not come from source but was created
6070 -- for default initialization, e.g. with Initialize_Scalars.
6072 if Requires_Transient_Scope
(Typ
) then
6073 Establish_Transient_Scope
6074 (N
, Sec_Stack
=> Has_Controlled_Component
(Typ
));
6077 if Has_Default_Init_Comps
(N
) then
6078 Maybe_In_Place_OK
:= False;
6080 elsif Is_Bit_Packed_Array
(Typ
)
6081 or else Has_Controlled_Component
(Typ
)
6083 Maybe_In_Place_OK
:= False;
6086 Maybe_In_Place_OK
:=
6087 (Nkind
(Parent
(N
)) = N_Assignment_Statement
6088 and then In_Place_Assign_OK
)
6091 (Nkind
(Parent
(Parent
(N
))) = N_Allocator
6092 and then In_Place_Assign_OK
);
6095 -- If this is an array of tasks, it will be expanded into build-in-place
6096 -- assignments. Build an activation chain for the tasks now.
6098 if Has_Task
(Etype
(N
)) then
6099 Build_Activation_Chain_Entity
(N
);
6102 -- Perform in-place expansion of aggregate in an object declaration.
6103 -- Note: actions generated for the aggregate will be captured in an
6104 -- expression-with-actions statement so that they can be transferred
6105 -- to freeze actions later if there is an address clause for the
6106 -- object. (Note: we don't use a block statement because this would
6107 -- cause generated freeze nodes to be elaborated in the wrong scope).
6109 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6110 -- expected to appear in qualified form. In-place expansion eliminates
6111 -- the qualification and eventually violates this SPARK 05 restiction.
6113 -- Should document the rest of the guards ???
6115 if not Has_Default_Init_Comps
(N
)
6116 and then Comes_From_Source
(Parent_Node
)
6117 and then Parent_Kind
= N_Object_Declaration
6118 and then Present
(Expression
(Parent_Node
))
6120 Must_Slide
(Etype
(Defining_Identifier
(Parent_Node
)), Typ
)
6121 and then not Has_Controlled_Component
(Typ
)
6122 and then not Is_Bit_Packed_Array
(Typ
)
6123 and then not Restriction_Check_Required
(SPARK_05
)
6125 In_Place_Assign_OK_For_Declaration
:= True;
6126 Tmp
:= Defining_Identifier
(Parent_Node
);
6127 Set_No_Initialization
(Parent_Node
);
6128 Set_Expression
(Parent_Node
, Empty
);
6130 -- Set kind and type of the entity, for use in the analysis
6131 -- of the subsequent assignments. If the nominal type is not
6132 -- constrained, build a subtype from the known bounds of the
6133 -- aggregate. If the declaration has a subtype mark, use it,
6134 -- otherwise use the itype of the aggregate.
6136 Set_Ekind
(Tmp
, E_Variable
);
6138 if not Is_Constrained
(Typ
) then
6139 Build_Constrained_Type
(Positional
=> False);
6141 elsif Is_Entity_Name
(Object_Definition
(Parent_Node
))
6142 and then Is_Constrained
(Entity
(Object_Definition
(Parent_Node
)))
6144 Set_Etype
(Tmp
, Entity
(Object_Definition
(Parent_Node
)));
6147 Set_Size_Known_At_Compile_Time
(Typ
, False);
6148 Set_Etype
(Tmp
, Typ
);
6151 elsif Maybe_In_Place_OK
6152 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
6153 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
6155 Set_Expansion_Delayed
(N
);
6158 -- In the remaining cases the aggregate is the RHS of an assignment
6160 elsif Maybe_In_Place_OK
6161 and then Safe_Left_Hand_Side
(Name
(Parent
(N
)))
6163 Tmp
:= Name
(Parent
(N
));
6165 if Etype
(Tmp
) /= Etype
(N
) then
6166 Apply_Length_Check
(N
, Etype
(Tmp
));
6168 if Nkind
(N
) = N_Raise_Constraint_Error
then
6170 -- Static error, nothing further to expand
6176 -- If a slice assignment has an aggregate with a single others_choice,
6177 -- the assignment can be done in place even if bounds are not static,
6178 -- by converting it into a loop over the discrete range of the slice.
6180 elsif Maybe_In_Place_OK
6181 and then Nkind
(Name
(Parent
(N
))) = N_Slice
6182 and then Is_Others_Aggregate
(N
)
6184 Tmp
:= Name
(Parent
(N
));
6186 -- Set type of aggregate to be type of lhs in assignment, in order
6187 -- to suppress redundant length checks.
6189 Set_Etype
(N
, Etype
(Tmp
));
6193 -- In place aggregate expansion is not possible
6196 Maybe_In_Place_OK
:= False;
6197 Tmp
:= Make_Temporary
(Loc
, 'A', N
);
6199 Make_Object_Declaration
(Loc
,
6200 Defining_Identifier
=> Tmp
,
6201 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
6202 Set_No_Initialization
(Tmp_Decl
, True);
6204 -- If we are within a loop, the temporary will be pushed on the
6205 -- stack at each iteration. If the aggregate is the expression for an
6206 -- allocator, it will be immediately copied to the heap and can
6207 -- be reclaimed at once. We create a transient scope around the
6208 -- aggregate for this purpose.
6210 if Ekind
(Current_Scope
) = E_Loop
6211 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
6213 Establish_Transient_Scope
(N
, False);
6216 Insert_Action
(N
, Tmp_Decl
);
6219 -- Construct and insert the aggregate code. We can safely suppress index
6220 -- checks because this code is guaranteed not to raise CE on index
6221 -- checks. However we should *not* suppress all checks.
6227 if Nkind
(Tmp
) = N_Defining_Identifier
then
6228 Target
:= New_Occurrence_Of
(Tmp
, Loc
);
6231 if Has_Default_Init_Comps
(N
) then
6233 -- Ada 2005 (AI-287): This case has not been analyzed???
6235 raise Program_Error
;
6238 -- Name in assignment is explicit dereference
6240 Target
:= New_Copy
(Tmp
);
6243 -- If we are to generate an in place assignment for a declaration or
6244 -- an assignment statement, and the assignment can be done directly
6245 -- by the back end, then do not expand further.
6247 -- ??? We can also do that if in place expansion is not possible but
6248 -- then we could go into an infinite recursion.
6250 if (In_Place_Assign_OK_For_Declaration
or else Maybe_In_Place_OK
)
6251 and then not AAMP_On_Target
6252 and then not CodePeer_Mode
6253 and then not Modify_Tree_For_C
6254 and then not Possible_Bit_Aligned_Component
(Target
)
6255 and then not Is_Possibly_Unaligned_Slice
(Target
)
6256 and then Aggr_Assignment_OK_For_Backend
(N
)
6258 if Maybe_In_Place_OK
then
6264 Make_Assignment_Statement
(Loc
,
6266 Expression
=> New_Copy
(N
)));
6270 Build_Array_Aggr_Code
(N
,
6272 Index
=> First_Index
(Typ
),
6274 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
6277 -- Save the last assignment statement associated with the aggregate
6278 -- when building a controlled object. This reference is utilized by
6279 -- the finalization machinery when marking an object as successfully
6282 if Needs_Finalization
(Typ
)
6283 and then Is_Entity_Name
(Target
)
6284 and then Present
(Entity
(Target
))
6285 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
6287 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
6291 -- If the aggregate is the expression in a declaration, the expanded
6292 -- code must be inserted after it. The defining entity might not come
6293 -- from source if this is part of an inlined body, but the declaration
6296 if Comes_From_Source
(Tmp
)
6298 (Nkind
(Parent
(N
)) = N_Object_Declaration
6299 and then Comes_From_Source
(Parent
(N
))
6300 and then Tmp
= Defining_Entity
(Parent
(N
)))
6303 Node_After
: constant Node_Id
:= Next
(Parent_Node
);
6306 Insert_Actions_After
(Parent_Node
, Aggr_Code
);
6308 if Parent_Kind
= N_Object_Declaration
then
6309 Collect_Initialization_Statements
6310 (Obj
=> Tmp
, N
=> Parent_Node
, Node_After
=> Node_After
);
6315 Insert_Actions
(N
, Aggr_Code
);
6318 -- If the aggregate has been assigned in place, remove the original
6321 if Nkind
(Parent
(N
)) = N_Assignment_Statement
6322 and then Maybe_In_Place_OK
6324 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
6326 elsif Nkind
(Parent
(N
)) /= N_Object_Declaration
6327 or else Tmp
/= Defining_Identifier
(Parent
(N
))
6329 Rewrite
(N
, New_Occurrence_Of
(Tmp
, Loc
));
6330 Analyze_And_Resolve
(N
, Typ
);
6332 end Expand_Array_Aggregate
;
6334 ------------------------
6335 -- Expand_N_Aggregate --
6336 ------------------------
6338 procedure Expand_N_Aggregate
(N
: Node_Id
) is
6340 -- Record aggregate case
6342 if Is_Record_Type
(Etype
(N
)) then
6343 Expand_Record_Aggregate
(N
);
6345 -- Array aggregate case
6348 -- A special case, if we have a string subtype with bounds 1 .. N,
6349 -- where N is known at compile time, and the aggregate is of the
6350 -- form (others => 'x'), with a single choice and no expressions,
6351 -- and N is less than 80 (an arbitrary limit for now), then replace
6352 -- the aggregate by the equivalent string literal (but do not mark
6353 -- it as static since it is not).
6355 -- Note: this entire circuit is redundant with respect to code in
6356 -- Expand_Array_Aggregate that collapses others choices to positional
6357 -- form, but there are two problems with that circuit:
6359 -- a) It is limited to very small cases due to ill-understood
6360 -- interactions with bootstrapping. That limit is removed by
6361 -- use of the No_Implicit_Loops restriction.
6363 -- b) It incorrectly ends up with the resulting expressions being
6364 -- considered static when they are not. For example, the
6365 -- following test should fail:
6367 -- pragma Restrictions (No_Implicit_Loops);
6368 -- package NonSOthers4 is
6369 -- B : constant String (1 .. 6) := (others => 'A');
6370 -- DH : constant String (1 .. 8) := B & "BB";
6372 -- pragma Export (C, X, Link_Name => DH);
6375 -- But it succeeds (DH looks static to pragma Export)
6377 -- To be sorted out ???
6379 if Present
(Component_Associations
(N
)) then
6381 CA
: constant Node_Id
:= First
(Component_Associations
(N
));
6382 MX
: constant := 80;
6385 if Nkind
(First
(Choice_List
(CA
))) = N_Others_Choice
6386 and then Nkind
(Expression
(CA
)) = N_Character_Literal
6387 and then No
(Expressions
(N
))
6390 T
: constant Entity_Id
:= Etype
(N
);
6391 X
: constant Node_Id
:= First_Index
(T
);
6392 EC
: constant Node_Id
:= Expression
(CA
);
6393 CV
: constant Uint
:= Char_Literal_Value
(EC
);
6394 CC
: constant Int
:= UI_To_Int
(CV
);
6397 if Nkind
(X
) = N_Range
6398 and then Compile_Time_Known_Value
(Low_Bound
(X
))
6399 and then Expr_Value
(Low_Bound
(X
)) = 1
6400 and then Compile_Time_Known_Value
(High_Bound
(X
))
6403 Hi
: constant Uint
:= Expr_Value
(High_Bound
(X
));
6409 for J
in 1 .. UI_To_Int
(Hi
) loop
6410 Store_String_Char
(Char_Code
(CC
));
6414 Make_String_Literal
(Sloc
(N
),
6415 Strval
=> End_String
));
6417 if CC
>= Int
(2 ** 16) then
6418 Set_Has_Wide_Wide_Character
(N
);
6419 elsif CC
>= Int
(2 ** 8) then
6420 Set_Has_Wide_Character
(N
);
6423 Analyze_And_Resolve
(N
, T
);
6424 Set_Is_Static_Expression
(N
, False);
6434 -- Not that special case, so normal expansion of array aggregate
6436 Expand_Array_Aggregate
(N
);
6440 when RE_Not_Available
=>
6442 end Expand_N_Aggregate
;
6444 ------------------------------
6445 -- Expand_N_Delta_Aggregate --
6446 ------------------------------
6448 procedure Expand_N_Delta_Aggregate
(N
: Node_Id
) is
6449 Loc
: constant Source_Ptr
:= Sloc
(N
);
6450 Typ
: constant Entity_Id
:= Etype
(N
);
6455 Make_Object_Declaration
(Loc
,
6456 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
6457 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
6458 Expression
=> New_Copy_Tree
(Expression
(N
)));
6460 if Is_Array_Type
(Etype
(N
)) then
6461 Expand_Delta_Array_Aggregate
(N
, New_List
(Decl
));
6463 Expand_Delta_Record_Aggregate
(N
, New_List
(Decl
));
6465 end Expand_N_Delta_Aggregate
;
6467 ----------------------------------
6468 -- Expand_Delta_Array_Aggregate --
6469 ----------------------------------
6471 procedure Expand_Delta_Array_Aggregate
(N
: Node_Id
; Deltas
: List_Id
) is
6472 Loc
: constant Source_Ptr
:= Sloc
(N
);
6473 Temp
: constant Entity_Id
:= Defining_Identifier
(First
(Deltas
));
6476 function Generate_Loop
(C
: Node_Id
) return Node_Id
;
6477 -- Generate a loop containing individual component assignments for
6478 -- choices that are ranges, subtype indications, subtype names, and
6479 -- iterated component associations.
6485 function Generate_Loop
(C
: Node_Id
) return Node_Id
is
6486 Sl
: constant Source_Ptr
:= Sloc
(C
);
6490 if Nkind
(Parent
(C
)) = N_Iterated_Component_Association
then
6492 Make_Defining_Identifier
(Loc
,
6493 Chars
=> (Chars
(Defining_Identifier
(Parent
(C
)))));
6495 Ix
:= Make_Temporary
(Sl
, 'I');
6499 Make_Loop_Statement
(Loc
,
6501 Make_Iteration_Scheme
(Sl
,
6502 Loop_Parameter_Specification
=>
6503 Make_Loop_Parameter_Specification
(Sl
,
6504 Defining_Identifier
=> Ix
,
6505 Discrete_Subtype_Definition
=> New_Copy_Tree
(C
))),
6507 Statements
=> New_List
(
6508 Make_Assignment_Statement
(Sl
,
6510 Make_Indexed_Component
(Sl
,
6511 Prefix
=> New_Occurrence_Of
(Temp
, Sl
),
6512 Expressions
=> New_List
(New_Occurrence_Of
(Ix
, Sl
))),
6513 Expression
=> New_Copy_Tree
(Expression
(Assoc
)))),
6514 End_Label
=> Empty
);
6521 -- Start of processing for Expand_Delta_Array_Aggregate
6524 Assoc
:= First
(Component_Associations
(N
));
6525 while Present
(Assoc
) loop
6526 Choice
:= First
(Choice_List
(Assoc
));
6527 if Nkind
(Assoc
) = N_Iterated_Component_Association
then
6528 while Present
(Choice
) loop
6529 Append_To
(Deltas
, Generate_Loop
(Choice
));
6534 while Present
(Choice
) loop
6536 -- Choice can be given by a range, a subtype indication, a
6537 -- subtype name, a scalar value, or an entity.
6539 if Nkind
(Choice
) = N_Range
6540 or else (Is_Entity_Name
(Choice
)
6541 and then Is_Type
(Entity
(Choice
)))
6543 Append_To
(Deltas
, Generate_Loop
(Choice
));
6545 elsif Nkind
(Choice
) = N_Subtype_Indication
then
6547 Generate_Loop
(Range_Expression
(Constraint
(Choice
))));
6551 Make_Assignment_Statement
(Sloc
(Choice
),
6553 Make_Indexed_Component
(Sloc
(Choice
),
6554 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
6555 Expressions
=> New_List
(New_Copy_Tree
(Choice
))),
6556 Expression
=> New_Copy_Tree
(Expression
(Assoc
))));
6566 Insert_Actions
(N
, Deltas
);
6567 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
6568 end Expand_Delta_Array_Aggregate
;
6570 -----------------------------------
6571 -- Expand_Delta_Record_Aggregate --
6572 -----------------------------------
6574 procedure Expand_Delta_Record_Aggregate
(N
: Node_Id
; Deltas
: List_Id
) is
6575 Loc
: constant Source_Ptr
:= Sloc
(N
);
6576 Temp
: constant Entity_Id
:= Defining_Identifier
(First
(Deltas
));
6581 Assoc
:= First
(Component_Associations
(N
));
6583 while Present
(Assoc
) loop
6584 Choice
:= First
(Choice_List
(Assoc
));
6585 while Present
(Choice
) loop
6587 Make_Assignment_Statement
(Sloc
(Choice
),
6589 Make_Selected_Component
(Sloc
(Choice
),
6590 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
6591 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Choice
))),
6592 Expression
=> New_Copy_Tree
(Expression
(Assoc
))));
6599 Insert_Actions
(N
, Deltas
);
6600 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
6601 end Expand_Delta_Record_Aggregate
;
6603 ----------------------------------
6604 -- Expand_N_Extension_Aggregate --
6605 ----------------------------------
6607 -- If the ancestor part is an expression, add a component association for
6608 -- the parent field. If the type of the ancestor part is not the direct
6609 -- parent of the expected type, build recursively the needed ancestors.
6610 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
6611 -- ration for a temporary of the expected type, followed by individual
6612 -- assignments to the given components.
6614 procedure Expand_N_Extension_Aggregate
(N
: Node_Id
) is
6615 Loc
: constant Source_Ptr
:= Sloc
(N
);
6616 A
: constant Node_Id
:= Ancestor_Part
(N
);
6617 Typ
: constant Entity_Id
:= Etype
(N
);
6620 -- If the ancestor is a subtype mark, an init proc must be called
6621 -- on the resulting object which thus has to be materialized in
6624 if Is_Entity_Name
(A
) and then Is_Type
(Entity
(A
)) then
6625 Convert_To_Assignments
(N
, Typ
);
6627 -- The extension aggregate is transformed into a record aggregate
6628 -- of the following form (c1 and c2 are inherited components)
6630 -- (Exp with c3 => a, c4 => b)
6631 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6636 if Tagged_Type_Expansion
then
6637 Expand_Record_Aggregate
(N
,
6640 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
),
6643 -- No tag is needed in the case of a VM
6646 Expand_Record_Aggregate
(N
, Parent_Expr
=> A
);
6651 when RE_Not_Available
=>
6653 end Expand_N_Extension_Aggregate
;
6655 -----------------------------
6656 -- Expand_Record_Aggregate --
6657 -----------------------------
6659 procedure Expand_Record_Aggregate
6661 Orig_Tag
: Node_Id
:= Empty
;
6662 Parent_Expr
: Node_Id
:= Empty
)
6664 Loc
: constant Source_Ptr
:= Sloc
(N
);
6665 Comps
: constant List_Id
:= Component_Associations
(N
);
6666 Typ
: constant Entity_Id
:= Etype
(N
);
6667 Base_Typ
: constant Entity_Id
:= Base_Type
(Typ
);
6669 Static_Components
: Boolean := True;
6670 -- Flag to indicate whether all components are compile-time known,
6671 -- and the aggregate can be constructed statically and handled by
6674 procedure Build_Back_End_Aggregate
;
6675 -- Build a proper aggregate to be handled by the back-end
6677 function Compile_Time_Known_Composite_Value
(N
: Node_Id
) return Boolean;
6678 -- Returns true if N is an expression of composite type which can be
6679 -- fully evaluated at compile time without raising constraint error.
6680 -- Such expressions can be passed as is to Gigi without any expansion.
6682 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6683 -- set and constants whose expression is such an aggregate, recursively.
6685 function Component_Not_OK_For_Backend
return Boolean;
6686 -- Check for presence of a component which makes it impossible for the
6687 -- backend to process the aggregate, thus requiring the use of a series
6688 -- of assignment statements. Cases checked for are a nested aggregate
6689 -- needing Late_Expansion, the presence of a tagged component which may
6690 -- need tag adjustment, and a bit unaligned component reference.
6692 -- We also force expansion into assignments if a component is of a
6693 -- mutable type (including a private type with discriminants) because
6694 -- in that case the size of the component to be copied may be smaller
6695 -- than the side of the target, and there is no simple way for gigi
6696 -- to compute the size of the object to be copied.
6698 -- NOTE: This is part of the ongoing work to define precisely the
6699 -- interface between front-end and back-end handling of aggregates.
6700 -- In general it is desirable to pass aggregates as they are to gigi,
6701 -- in order to minimize elaboration code. This is one case where the
6702 -- semantics of Ada complicate the analysis and lead to anomalies in
6703 -- the gcc back-end if the aggregate is not expanded into assignments.
6705 function Has_Per_Object_Constraint
(L
: List_Id
) return Boolean;
6706 -- Return True if any element of L has Has_Per_Object_Constraint set.
6707 -- L should be the Choices component of an N_Component_Association.
6709 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean;
6710 -- If any ancestor of the current type is private, the aggregate
6711 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6712 -- because it will not be set when type and its parent are in the
6713 -- same scope, and the parent component needs expansion.
6715 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
;
6716 -- For nested aggregates return the ultimate enclosing aggregate; for
6717 -- non-nested aggregates return N.
6719 ------------------------------
6720 -- Build_Back_End_Aggregate --
6721 ------------------------------
6723 procedure Build_Back_End_Aggregate
is
6726 Tag_Value
: Node_Id
;
6729 if Nkind
(N
) = N_Aggregate
then
6731 -- If the aggregate is static and can be handled by the back-end,
6732 -- nothing left to do.
6734 if Static_Components
then
6735 Set_Compile_Time_Known_Aggregate
(N
);
6736 Set_Expansion_Delayed
(N
, False);
6740 -- If no discriminants, nothing special to do
6742 if not Has_Discriminants
(Typ
) then
6745 -- Case of discriminants present
6747 elsif Is_Derived_Type
(Typ
) then
6749 -- For untagged types, non-stored discriminants are replaced with
6750 -- stored discriminants, which are the ones that gigi uses to
6751 -- describe the type and its components.
6753 Generate_Aggregate_For_Derived_Type
: declare
6754 procedure Prepend_Stored_Values
(T
: Entity_Id
);
6755 -- Scan the list of stored discriminants of the type, and add
6756 -- their values to the aggregate being built.
6758 ---------------------------
6759 -- Prepend_Stored_Values --
6760 ---------------------------
6762 procedure Prepend_Stored_Values
(T
: Entity_Id
) is
6764 First_Comp
: Node_Id
:= Empty
;
6767 Discr
:= First_Stored_Discriminant
(T
);
6768 while Present
(Discr
) loop
6770 Make_Component_Association
(Loc
,
6771 Choices
=> New_List
(
6772 New_Occurrence_Of
(Discr
, Loc
)),
6775 (Get_Discriminant_Value
6778 Discriminant_Constraint
(Typ
))));
6780 if No
(First_Comp
) then
6781 Prepend_To
(Component_Associations
(N
), New_Comp
);
6783 Insert_After
(First_Comp
, New_Comp
);
6786 First_Comp
:= New_Comp
;
6787 Next_Stored_Discriminant
(Discr
);
6789 end Prepend_Stored_Values
;
6793 Constraints
: constant List_Id
:= New_List
;
6797 Num_Disc
: Nat
:= 0;
6798 Num_Gird
: Nat
:= 0;
6800 -- Start of processing for Generate_Aggregate_For_Derived_Type
6803 -- Remove the associations for the discriminant of derived type
6806 First_Comp
: Node_Id
;
6809 First_Comp
:= First
(Component_Associations
(N
));
6810 while Present
(First_Comp
) loop
6814 if Ekind
(Entity
(First
(Choices
(Comp
)))) =
6818 Num_Disc
:= Num_Disc
+ 1;
6823 -- Insert stored discriminant associations in the correct
6824 -- order. If there are more stored discriminants than new
6825 -- discriminants, there is at least one new discriminant that
6826 -- constrains more than one of the stored discriminants. In
6827 -- this case we need to construct a proper subtype of the
6828 -- parent type, in order to supply values to all the
6829 -- components. Otherwise there is one-one correspondence
6830 -- between the constraints and the stored discriminants.
6832 Discr
:= First_Stored_Discriminant
(Base_Type
(Typ
));
6833 while Present
(Discr
) loop
6834 Num_Gird
:= Num_Gird
+ 1;
6835 Next_Stored_Discriminant
(Discr
);
6838 -- Case of more stored discriminants than new discriminants
6840 if Num_Gird
> Num_Disc
then
6842 -- Create a proper subtype of the parent type, which is the
6843 -- proper implementation type for the aggregate, and convert
6844 -- it to the intended target type.
6846 Discr
:= First_Stored_Discriminant
(Base_Type
(Typ
));
6847 while Present
(Discr
) loop
6850 (Get_Discriminant_Value
6853 Discriminant_Constraint
(Typ
)));
6855 Append
(New_Comp
, Constraints
);
6856 Next_Stored_Discriminant
(Discr
);
6860 Make_Subtype_Declaration
(Loc
,
6861 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
6862 Subtype_Indication
=>
6863 Make_Subtype_Indication
(Loc
,
6865 New_Occurrence_Of
(Etype
(Base_Type
(Typ
)), Loc
),
6867 Make_Index_Or_Discriminant_Constraint
6868 (Loc
, Constraints
)));
6870 Insert_Action
(N
, Decl
);
6871 Prepend_Stored_Values
(Base_Type
(Typ
));
6873 Set_Etype
(N
, Defining_Identifier
(Decl
));
6876 Rewrite
(N
, Unchecked_Convert_To
(Typ
, N
));
6879 -- Case where we do not have fewer new discriminants than
6880 -- stored discriminants, so in this case we can simply use the
6881 -- stored discriminants of the subtype.
6884 Prepend_Stored_Values
(Typ
);
6886 end Generate_Aggregate_For_Derived_Type
;
6889 if Is_Tagged_Type
(Typ
) then
6891 -- In the tagged case, _parent and _tag component must be created
6893 -- Reset Null_Present unconditionally. Tagged records always have
6894 -- at least one field (the tag or the parent).
6896 Set_Null_Record_Present
(N
, False);
6898 -- When the current aggregate comes from the expansion of an
6899 -- extension aggregate, the parent expr is replaced by an
6900 -- aggregate formed by selected components of this expr.
6902 if Present
(Parent_Expr
) and then Is_Empty_List
(Comps
) then
6903 Comp
:= First_Component_Or_Discriminant
(Typ
);
6904 while Present
(Comp
) loop
6906 -- Skip all expander-generated components
6908 if not Comes_From_Source
(Original_Record_Component
(Comp
))
6914 Make_Selected_Component
(Loc
,
6916 Unchecked_Convert_To
(Typ
,
6917 Duplicate_Subexpr
(Parent_Expr
, True)),
6918 Selector_Name
=> New_Occurrence_Of
(Comp
, Loc
));
6921 Make_Component_Association
(Loc
,
6922 Choices
=> New_List
(
6923 New_Occurrence_Of
(Comp
, Loc
)),
6924 Expression
=> New_Comp
));
6926 Analyze_And_Resolve
(New_Comp
, Etype
(Comp
));
6929 Next_Component_Or_Discriminant
(Comp
);
6933 -- Compute the value for the Tag now, if the type is a root it
6934 -- will be included in the aggregate right away, otherwise it will
6935 -- be propagated to the parent aggregate.
6937 if Present
(Orig_Tag
) then
6938 Tag_Value
:= Orig_Tag
;
6940 elsif not Tagged_Type_Expansion
then
6946 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
);
6949 -- For a derived type, an aggregate for the parent is formed with
6950 -- all the inherited components.
6952 if Is_Derived_Type
(Typ
) then
6954 First_Comp
: Node_Id
;
6955 Parent_Comps
: List_Id
;
6956 Parent_Aggr
: Node_Id
;
6957 Parent_Name
: Node_Id
;
6960 -- Remove the inherited component association from the
6961 -- aggregate and store them in the parent aggregate
6963 First_Comp
:= First
(Component_Associations
(N
));
6964 Parent_Comps
:= New_List
;
6965 while Present
(First_Comp
)
6967 Scope
(Original_Record_Component
6968 (Entity
(First
(Choices
(First_Comp
))))) /=
6974 Append
(Comp
, Parent_Comps
);
6978 Make_Aggregate
(Loc
,
6979 Component_Associations
=> Parent_Comps
);
6980 Set_Etype
(Parent_Aggr
, Etype
(Base_Type
(Typ
)));
6982 -- Find the _parent component
6984 Comp
:= First_Component
(Typ
);
6985 while Chars
(Comp
) /= Name_uParent
loop
6986 Comp
:= Next_Component
(Comp
);
6989 Parent_Name
:= New_Occurrence_Of
(Comp
, Loc
);
6991 -- Insert the parent aggregate
6993 Prepend_To
(Component_Associations
(N
),
6994 Make_Component_Association
(Loc
,
6995 Choices
=> New_List
(Parent_Name
),
6996 Expression
=> Parent_Aggr
));
6998 -- Expand recursively the parent propagating the right Tag
7000 Expand_Record_Aggregate
7001 (Parent_Aggr
, Tag_Value
, Parent_Expr
);
7003 -- The ancestor part may be a nested aggregate that has
7004 -- delayed expansion: recheck now.
7006 if Component_Not_OK_For_Backend
then
7007 Convert_To_Assignments
(N
, Typ
);
7011 -- For a root type, the tag component is added (unless compiling
7012 -- for the VMs, where tags are implicit).
7014 elsif Tagged_Type_Expansion
then
7016 Tag_Name
: constant Node_Id
:=
7018 (First_Tag_Component
(Typ
), Loc
);
7019 Typ_Tag
: constant Entity_Id
:= RTE
(RE_Tag
);
7020 Conv_Node
: constant Node_Id
:=
7021 Unchecked_Convert_To
(Typ_Tag
, Tag_Value
);
7024 Set_Etype
(Conv_Node
, Typ_Tag
);
7025 Prepend_To
(Component_Associations
(N
),
7026 Make_Component_Association
(Loc
,
7027 Choices
=> New_List
(Tag_Name
),
7028 Expression
=> Conv_Node
));
7032 end Build_Back_End_Aggregate
;
7034 ----------------------------------------
7035 -- Compile_Time_Known_Composite_Value --
7036 ----------------------------------------
7038 function Compile_Time_Known_Composite_Value
7039 (N
: Node_Id
) return Boolean
7042 -- If we have an entity name, then see if it is the name of a
7043 -- constant and if so, test the corresponding constant value.
7045 if Is_Entity_Name
(N
) then
7047 E
: constant Entity_Id
:= Entity
(N
);
7050 if Ekind
(E
) /= E_Constant
then
7053 V
:= Constant_Value
(E
);
7055 and then Compile_Time_Known_Composite_Value
(V
);
7059 -- We have a value, see if it is compile time known
7062 if Nkind
(N
) = N_Aggregate
then
7063 return Compile_Time_Known_Aggregate
(N
);
7066 -- All other types of values are not known at compile time
7071 end Compile_Time_Known_Composite_Value
;
7073 ----------------------------------
7074 -- Component_Not_OK_For_Backend --
7075 ----------------------------------
7077 function Component_Not_OK_For_Backend
return Boolean is
7087 while Present
(C
) loop
7089 -- If the component has box initialization, expansion is needed
7090 -- and component is not ready for backend.
7092 if Box_Present
(C
) then
7096 if Nkind
(Expression
(C
)) = N_Qualified_Expression
then
7097 Expr_Q
:= Expression
(Expression
(C
));
7099 Expr_Q
:= Expression
(C
);
7102 -- Return true if the aggregate has any associations for tagged
7103 -- components that may require tag adjustment.
7105 -- These are cases where the source expression may have a tag that
7106 -- could differ from the component tag (e.g., can occur for type
7107 -- conversions and formal parameters). (Tag adjustment not needed
7108 -- if Tagged_Type_Expansion because object tags are implicit in
7111 if Is_Tagged_Type
(Etype
(Expr_Q
))
7112 and then (Nkind
(Expr_Q
) = N_Type_Conversion
7113 or else (Is_Entity_Name
(Expr_Q
)
7115 Ekind
(Entity
(Expr_Q
)) in Formal_Kind
))
7116 and then Tagged_Type_Expansion
7118 Static_Components
:= False;
7121 elsif Is_Delayed_Aggregate
(Expr_Q
) then
7122 Static_Components
:= False;
7125 elsif Possible_Bit_Aligned_Component
(Expr_Q
) then
7126 Static_Components
:= False;
7129 elsif Modify_Tree_For_C
7130 and then Nkind
(C
) = N_Component_Association
7131 and then Has_Per_Object_Constraint
(Choices
(C
))
7133 Static_Components
:= False;
7136 elsif Modify_Tree_For_C
7137 and then Nkind
(Expr_Q
) = N_Identifier
7138 and then Is_Array_Type
(Etype
(Expr_Q
))
7140 Static_Components
:= False;
7144 if Is_Elementary_Type
(Etype
(Expr_Q
)) then
7145 if not Compile_Time_Known_Value
(Expr_Q
) then
7146 Static_Components
:= False;
7149 elsif not Compile_Time_Known_Composite_Value
(Expr_Q
) then
7150 Static_Components
:= False;
7152 if Is_Private_Type
(Etype
(Expr_Q
))
7153 and then Has_Discriminants
(Etype
(Expr_Q
))
7163 end Component_Not_OK_For_Backend
;
7165 -------------------------------
7166 -- Has_Per_Object_Constraint --
7167 -------------------------------
7169 function Has_Per_Object_Constraint
(L
: List_Id
) return Boolean is
7170 N
: Node_Id
:= First
(L
);
7172 while Present
(N
) loop
7173 if Is_Entity_Name
(N
)
7174 and then Present
(Entity
(N
))
7175 and then Has_Per_Object_Constraint
(Entity
(N
))
7184 end Has_Per_Object_Constraint
;
7186 -----------------------------------
7187 -- Has_Visible_Private_Ancestor --
7188 -----------------------------------
7190 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean is
7191 R
: constant Entity_Id
:= Root_Type
(Id
);
7192 T1
: Entity_Id
:= Id
;
7196 if Is_Private_Type
(T1
) then
7206 end Has_Visible_Private_Ancestor
;
7208 -------------------------
7209 -- Top_Level_Aggregate --
7210 -------------------------
7212 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
is
7217 while Present
(Parent
(Aggr
))
7218 and then Nkind_In
(Parent
(Aggr
), N_Aggregate
,
7219 N_Component_Association
)
7221 Aggr
:= Parent
(Aggr
);
7225 end Top_Level_Aggregate
;
7229 Top_Level_Aggr
: constant Node_Id
:= Top_Level_Aggregate
(N
);
7231 -- Start of processing for Expand_Record_Aggregate
7234 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7235 -- to prevent a piecemeal assignment even if the aggregate is to be
7236 -- expanded. We create a temporary for the aggregate, and assign the
7237 -- temporary instead, so that the back end can generate an atomic move
7240 if Is_Atomic_VFA_Aggregate
(N
) then
7243 -- No special management required for aggregates used to initialize
7244 -- statically allocated dispatch tables
7246 elsif Is_Static_Dispatch_Table_Aggregate
(N
) then
7250 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7251 -- are build-in-place function calls. The assignments will each turn
7252 -- into a build-in-place function call. If components are all static,
7253 -- we can pass the aggregate to the backend regardless of limitedness.
7255 -- Extension aggregates, aggregates in extended return statements, and
7256 -- aggregates for C++ imported types must be expanded.
7258 if Ada_Version
>= Ada_2005
and then Is_Limited_View
(Typ
) then
7259 if not Nkind_In
(Parent
(N
), N_Component_Association
,
7260 N_Object_Declaration
)
7262 Convert_To_Assignments
(N
, Typ
);
7264 elsif Nkind
(N
) = N_Extension_Aggregate
7265 or else Convention
(Typ
) = Convention_CPP
7267 Convert_To_Assignments
(N
, Typ
);
7269 elsif not Size_Known_At_Compile_Time
(Typ
)
7270 or else Component_Not_OK_For_Backend
7271 or else not Static_Components
7273 Convert_To_Assignments
(N
, Typ
);
7275 -- In all other cases, build a proper aggregate to be handled by
7279 Build_Back_End_Aggregate
;
7282 -- Gigi doesn't properly handle temporaries of variable size so we
7283 -- generate it in the front-end
7285 elsif not Size_Known_At_Compile_Time
(Typ
)
7286 and then Tagged_Type_Expansion
7288 Convert_To_Assignments
(N
, Typ
);
7290 -- An aggregate used to initialize a controlled object must be turned
7291 -- into component assignments as the components themselves may require
7292 -- finalization actions such as adjustment.
7294 elsif Needs_Finalization
(Typ
) then
7295 Convert_To_Assignments
(N
, Typ
);
7297 -- Ada 2005 (AI-287): In case of default initialized components we
7298 -- convert the aggregate into assignments.
7300 elsif Has_Default_Init_Comps
(N
) then
7301 Convert_To_Assignments
(N
, Typ
);
7305 elsif Component_Not_OK_For_Backend
then
7306 Convert_To_Assignments
(N
, Typ
);
7308 -- If an ancestor is private, some components are not inherited and we
7309 -- cannot expand into a record aggregate.
7311 elsif Has_Visible_Private_Ancestor
(Typ
) then
7312 Convert_To_Assignments
(N
, Typ
);
7314 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7315 -- is not able to handle the aggregate for Late_Request.
7317 elsif Is_Tagged_Type
(Typ
) and then Has_Discriminants
(Typ
) then
7318 Convert_To_Assignments
(N
, Typ
);
7320 -- If the tagged types covers interface types we need to initialize all
7321 -- hidden components containing pointers to secondary dispatch tables.
7323 elsif Is_Tagged_Type
(Typ
) and then Has_Interfaces
(Typ
) then
7324 Convert_To_Assignments
(N
, Typ
);
7326 -- If some components are mutable, the size of the aggregate component
7327 -- may be distinct from the default size of the type component, so
7328 -- we need to expand to insure that the back-end copies the proper
7329 -- size of the data. However, if the aggregate is the initial value of
7330 -- a constant, the target is immutable and might be built statically
7331 -- if components are appropriate.
7333 elsif Has_Mutable_Components
(Typ
)
7335 (Nkind
(Parent
(Top_Level_Aggr
)) /= N_Object_Declaration
7336 or else not Constant_Present
(Parent
(Top_Level_Aggr
))
7337 or else not Static_Components
)
7339 Convert_To_Assignments
(N
, Typ
);
7341 -- If the type involved has bit aligned components, then we are not sure
7342 -- that the back end can handle this case correctly.
7344 elsif Type_May_Have_Bit_Aligned_Components
(Typ
) then
7345 Convert_To_Assignments
(N
, Typ
);
7347 -- When generating C, only generate an aggregate when declaring objects
7348 -- since C does not support aggregates in e.g. assignment statements.
7350 elsif Modify_Tree_For_C
and then not In_Object_Declaration
(N
) then
7351 Convert_To_Assignments
(N
, Typ
);
7353 -- In all other cases, build a proper aggregate to be handled by gigi
7356 Build_Back_End_Aggregate
;
7358 end Expand_Record_Aggregate
;
7360 ----------------------------
7361 -- Has_Default_Init_Comps --
7362 ----------------------------
7364 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean is
7365 Comps
: constant List_Id
:= Component_Associations
(N
);
7370 pragma Assert
(Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
));
7376 if Has_Self_Reference
(N
) then
7380 -- Check if any direct component has default initialized components
7383 while Present
(C
) loop
7384 if Box_Present
(C
) then
7391 -- Recursive call in case of aggregate expression
7394 while Present
(C
) loop
7395 Expr
:= Expression
(C
);
7398 and then Nkind_In
(Expr
, N_Aggregate
, N_Extension_Aggregate
)
7399 and then Has_Default_Init_Comps
(Expr
)
7408 end Has_Default_Init_Comps
;
7410 --------------------------
7411 -- Is_Delayed_Aggregate --
7412 --------------------------
7414 function Is_Delayed_Aggregate
(N
: Node_Id
) return Boolean is
7415 Node
: Node_Id
:= N
;
7416 Kind
: Node_Kind
:= Nkind
(Node
);
7419 if Kind
= N_Qualified_Expression
then
7420 Node
:= Expression
(Node
);
7421 Kind
:= Nkind
(Node
);
7424 if not Nkind_In
(Kind
, N_Aggregate
, N_Extension_Aggregate
) then
7427 return Expansion_Delayed
(Node
);
7429 end Is_Delayed_Aggregate
;
7431 ---------------------------
7432 -- In_Object_Declaration --
7433 ---------------------------
7435 function In_Object_Declaration
(N
: Node_Id
) return Boolean is
7436 P
: Node_Id
:= Parent
(N
);
7438 while Present
(P
) loop
7439 if Nkind
(P
) = N_Object_Declaration
then
7447 end In_Object_Declaration
;
7449 ----------------------------------------
7450 -- Is_Static_Dispatch_Table_Aggregate --
7451 ----------------------------------------
7453 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean is
7454 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
7457 return Static_Dispatch_Tables
7458 and then Tagged_Type_Expansion
7459 and then RTU_Loaded
(Ada_Tags
)
7461 -- Avoid circularity when rebuilding the compiler
7463 and then Cunit_Entity
(Get_Source_Unit
(N
)) /= RTU_Entity
(Ada_Tags
)
7464 and then (Typ
= RTE
(RE_Dispatch_Table_Wrapper
)
7466 Typ
= RTE
(RE_Address_Array
)
7468 Typ
= RTE
(RE_Type_Specific_Data
)
7470 Typ
= RTE
(RE_Tag_Table
)
7472 (RTE_Available
(RE_Interface_Data
)
7473 and then Typ
= RTE
(RE_Interface_Data
))
7475 (RTE_Available
(RE_Interfaces_Array
)
7476 and then Typ
= RTE
(RE_Interfaces_Array
))
7478 (RTE_Available
(RE_Interface_Data_Element
)
7479 and then Typ
= RTE
(RE_Interface_Data_Element
)));
7480 end Is_Static_Dispatch_Table_Aggregate
;
7482 -----------------------------
7483 -- Is_Two_Dim_Packed_Array --
7484 -----------------------------
7486 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean is
7487 C
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
7489 return Number_Dimensions
(Typ
) = 2
7490 and then Is_Bit_Packed_Array
(Typ
)
7491 and then (C
= 1 or else C
= 2 or else C
= 4);
7492 end Is_Two_Dim_Packed_Array
;
7494 --------------------
7495 -- Late_Expansion --
7496 --------------------
7498 function Late_Expansion
7501 Target
: Node_Id
) return List_Id
7503 Aggr_Code
: List_Id
;
7506 if Is_Array_Type
(Etype
(N
)) then
7508 Build_Array_Aggr_Code
7510 Ctype
=> Component_Type
(Etype
(N
)),
7511 Index
=> First_Index
(Typ
),
7513 Scalar_Comp
=> Is_Scalar_Type
(Component_Type
(Typ
)),
7514 Indexes
=> No_List
);
7516 -- Directly or indirectly (e.g. access protected procedure) a record
7519 Aggr_Code
:= Build_Record_Aggr_Code
(N
, Typ
, Target
);
7522 -- Save the last assignment statement associated with the aggregate
7523 -- when building a controlled object. This reference is utilized by
7524 -- the finalization machinery when marking an object as successfully
7527 if Needs_Finalization
(Typ
)
7528 and then Is_Entity_Name
(Target
)
7529 and then Present
(Entity
(Target
))
7530 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
7532 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
7538 ----------------------------------
7539 -- Make_OK_Assignment_Statement --
7540 ----------------------------------
7542 function Make_OK_Assignment_Statement
7545 Expression
: Node_Id
) return Node_Id
7548 Set_Assignment_OK
(Name
);
7549 return Make_Assignment_Statement
(Sloc
, Name
, Expression
);
7550 end Make_OK_Assignment_Statement
;
7552 -----------------------
7553 -- Number_Of_Choices --
7554 -----------------------
7556 function Number_Of_Choices
(N
: Node_Id
) return Nat
is
7560 Nb_Choices
: Nat
:= 0;
7563 if Present
(Expressions
(N
)) then
7567 Assoc
:= First
(Component_Associations
(N
));
7568 while Present
(Assoc
) loop
7569 Choice
:= First
(Choice_List
(Assoc
));
7570 while Present
(Choice
) loop
7571 if Nkind
(Choice
) /= N_Others_Choice
then
7572 Nb_Choices
:= Nb_Choices
+ 1;
7582 end Number_Of_Choices
;
7584 ------------------------------------
7585 -- Packed_Array_Aggregate_Handled --
7586 ------------------------------------
7588 -- The current version of this procedure will handle at compile time
7589 -- any array aggregate that meets these conditions:
7591 -- One and two dimensional, bit packed
7592 -- Underlying packed type is modular type
7593 -- Bounds are within 32-bit Int range
7594 -- All bounds and values are static
7596 -- Note: for now, in the 2-D case, we only handle component sizes of
7597 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7599 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean is
7600 Loc
: constant Source_Ptr
:= Sloc
(N
);
7601 Typ
: constant Entity_Id
:= Etype
(N
);
7602 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
7604 Not_Handled
: exception;
7605 -- Exception raised if this aggregate cannot be handled
7608 -- Handle one- or two dimensional bit packed array
7610 if not Is_Bit_Packed_Array
(Typ
)
7611 or else Number_Dimensions
(Typ
) > 2
7616 -- If two-dimensional, check whether it can be folded, and transformed
7617 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7618 -- the original type.
7620 if Number_Dimensions
(Typ
) = 2 then
7621 return Two_Dim_Packed_Array_Handled
(N
);
7624 if not Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
)) then
7628 if not Is_Scalar_Type
(Component_Type
(Typ
))
7629 and then Has_Non_Standard_Rep
(Component_Type
(Typ
))
7635 Csiz
: constant Nat
:= UI_To_Int
(Component_Size
(Typ
));
7639 -- Bounds of index type
7643 -- Values of bounds if compile time known
7645 function Get_Component_Val
(N
: Node_Id
) return Uint
;
7646 -- Given a expression value N of the component type Ctyp, returns a
7647 -- value of Csiz (component size) bits representing this value. If
7648 -- the value is non-static or any other reason exists why the value
7649 -- cannot be returned, then Not_Handled is raised.
7651 -----------------------
7652 -- Get_Component_Val --
7653 -----------------------
7655 function Get_Component_Val
(N
: Node_Id
) return Uint
is
7659 -- We have to analyze the expression here before doing any further
7660 -- processing here. The analysis of such expressions is deferred
7661 -- till expansion to prevent some problems of premature analysis.
7663 Analyze_And_Resolve
(N
, Ctyp
);
7665 -- Must have a compile time value. String literals have to be
7666 -- converted into temporaries as well, because they cannot easily
7667 -- be converted into their bit representation.
7669 if not Compile_Time_Known_Value
(N
)
7670 or else Nkind
(N
) = N_String_Literal
7675 Val
:= Expr_Rep_Value
(N
);
7677 -- Adjust for bias, and strip proper number of bits
7679 if Has_Biased_Representation
(Ctyp
) then
7680 Val
:= Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
7683 return Val
mod Uint_2
** Csiz
;
7684 end Get_Component_Val
;
7686 -- Here we know we have a one dimensional bit packed array
7689 Get_Index_Bounds
(First_Index
(Typ
), Lo
, Hi
);
7691 -- Cannot do anything if bounds are dynamic
7693 if not Compile_Time_Known_Value
(Lo
)
7695 not Compile_Time_Known_Value
(Hi
)
7700 -- Or are silly out of range of int bounds
7702 Lob
:= Expr_Value
(Lo
);
7703 Hib
:= Expr_Value
(Hi
);
7705 if not UI_Is_In_Int_Range
(Lob
)
7707 not UI_Is_In_Int_Range
(Hib
)
7712 -- At this stage we have a suitable aggregate for handling at compile
7713 -- time. The only remaining checks are that the values of expressions
7714 -- in the aggregate are compile-time known (checks are performed by
7715 -- Get_Component_Val), and that any subtypes or ranges are statically
7718 -- If the aggregate is not fully positional at this stage, then
7719 -- convert it to positional form. Either this will fail, in which
7720 -- case we can do nothing, or it will succeed, in which case we have
7721 -- succeeded in handling the aggregate and transforming it into a
7722 -- modular value, or it will stay an aggregate, in which case we
7723 -- have failed to create a packed value for it.
7725 if Present
(Component_Associations
(N
)) then
7726 Convert_To_Positional
7727 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
7728 return Nkind
(N
) /= N_Aggregate
;
7731 -- Otherwise we are all positional, so convert to proper value
7734 Lov
: constant Int
:= UI_To_Int
(Lob
);
7735 Hiv
: constant Int
:= UI_To_Int
(Hib
);
7737 Len
: constant Nat
:= Int
'Max (0, Hiv
- Lov
+ 1);
7738 -- The length of the array (number of elements)
7740 Aggregate_Val
: Uint
;
7741 -- Value of aggregate. The value is set in the low order bits of
7742 -- this value. For the little-endian case, the values are stored
7743 -- from low-order to high-order and for the big-endian case the
7744 -- values are stored from high-order to low-order. Note that gigi
7745 -- will take care of the conversions to left justify the value in
7746 -- the big endian case (because of left justified modular type
7747 -- processing), so we do not have to worry about that here.
7750 -- Integer literal for resulting constructed value
7753 -- Shift count from low order for next value
7756 -- Shift increment for loop
7759 -- Next expression from positional parameters of aggregate
7761 Left_Justified
: Boolean;
7762 -- Set True if we are filling the high order bits of the target
7763 -- value (i.e. the value is left justified).
7766 -- For little endian, we fill up the low order bits of the target
7767 -- value. For big endian we fill up the high order bits of the
7768 -- target value (which is a left justified modular value).
7770 Left_Justified
:= Bytes_Big_Endian
;
7772 -- Switch justification if using -gnatd8
7774 if Debug_Flag_8
then
7775 Left_Justified
:= not Left_Justified
;
7778 -- Switch justfification if reverse storage order
7780 if Reverse_Storage_Order
(Base_Type
(Typ
)) then
7781 Left_Justified
:= not Left_Justified
;
7784 if Left_Justified
then
7785 Shift
:= Csiz
* (Len
- 1);
7792 -- Loop to set the values
7795 Aggregate_Val
:= Uint_0
;
7797 Expr
:= First
(Expressions
(N
));
7798 Aggregate_Val
:= Get_Component_Val
(Expr
) * Uint_2
** Shift
;
7800 for J
in 2 .. Len
loop
7801 Shift
:= Shift
+ Incr
;
7804 Aggregate_Val
+ Get_Component_Val
(Expr
) * Uint_2
** Shift
;
7808 -- Now we can rewrite with the proper value
7810 Lit
:= Make_Integer_Literal
(Loc
, Intval
=> Aggregate_Val
);
7811 Set_Print_In_Hex
(Lit
);
7813 -- Construct the expression using this literal. Note that it is
7814 -- important to qualify the literal with its proper modular type
7815 -- since universal integer does not have the required range and
7816 -- also this is a left justified modular type, which is important
7817 -- in the big-endian case.
7820 Unchecked_Convert_To
(Typ
,
7821 Make_Qualified_Expression
(Loc
,
7823 New_Occurrence_Of
(Packed_Array_Impl_Type
(Typ
), Loc
),
7824 Expression
=> Lit
)));
7826 Analyze_And_Resolve
(N
, Typ
);
7834 end Packed_Array_Aggregate_Handled
;
7836 ----------------------------
7837 -- Has_Mutable_Components --
7838 ----------------------------
7840 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean is
7844 Comp
:= First_Component
(Typ
);
7845 while Present
(Comp
) loop
7846 if Is_Record_Type
(Etype
(Comp
))
7847 and then Has_Discriminants
(Etype
(Comp
))
7848 and then not Is_Constrained
(Etype
(Comp
))
7853 Next_Component
(Comp
);
7857 end Has_Mutable_Components
;
7859 ------------------------------
7860 -- Initialize_Discriminants --
7861 ------------------------------
7863 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
) is
7864 Loc
: constant Source_Ptr
:= Sloc
(N
);
7865 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
7866 Par
: constant Entity_Id
:= Etype
(Bas
);
7867 Decl
: constant Node_Id
:= Parent
(Par
);
7871 if Is_Tagged_Type
(Bas
)
7872 and then Is_Derived_Type
(Bas
)
7873 and then Has_Discriminants
(Par
)
7874 and then Has_Discriminants
(Bas
)
7875 and then Number_Discriminants
(Bas
) /= Number_Discriminants
(Par
)
7876 and then Nkind
(Decl
) = N_Full_Type_Declaration
7877 and then Nkind
(Type_Definition
(Decl
)) = N_Record_Definition
7879 Present
(Variant_Part
(Component_List
(Type_Definition
(Decl
))))
7880 and then Nkind
(N
) /= N_Extension_Aggregate
7883 -- Call init proc to set discriminants.
7884 -- There should eventually be a special procedure for this ???
7886 Ref
:= New_Occurrence_Of
(Defining_Identifier
(N
), Loc
);
7887 Insert_Actions_After
(N
,
7888 Build_Initialization_Call
(Sloc
(N
), Ref
, Typ
));
7890 end Initialize_Discriminants
;
7897 (Obj_Type
: Entity_Id
;
7898 Typ
: Entity_Id
) return Boolean
7900 L1
, L2
, H1
, H2
: Node_Id
;
7903 -- No sliding if the type of the object is not established yet, if it is
7904 -- an unconstrained type whose actual subtype comes from the aggregate,
7905 -- or if the two types are identical.
7907 if not Is_Array_Type
(Obj_Type
) then
7910 elsif not Is_Constrained
(Obj_Type
) then
7913 elsif Typ
= Obj_Type
then
7917 -- Sliding can only occur along the first dimension
7919 Get_Index_Bounds
(First_Index
(Typ
), L1
, H1
);
7920 Get_Index_Bounds
(First_Index
(Obj_Type
), L2
, H2
);
7922 if not Is_OK_Static_Expression
(L1
) or else
7923 not Is_OK_Static_Expression
(L2
) or else
7924 not Is_OK_Static_Expression
(H1
) or else
7925 not Is_OK_Static_Expression
(H2
)
7929 return Expr_Value
(L1
) /= Expr_Value
(L2
)
7931 Expr_Value
(H1
) /= Expr_Value
(H2
);
7936 ---------------------------------
7937 -- Process_Transient_Component --
7938 ---------------------------------
7940 procedure Process_Transient_Component
7942 Comp_Typ
: Entity_Id
;
7943 Init_Expr
: Node_Id
;
7944 Fin_Call
: out Node_Id
;
7945 Hook_Clear
: out Node_Id
;
7946 Aggr
: Node_Id
:= Empty
;
7947 Stmts
: List_Id
:= No_List
)
7949 procedure Add_Item
(Item
: Node_Id
);
7950 -- Insert arbitrary node Item into the tree depending on the values of
7957 procedure Add_Item
(Item
: Node_Id
) is
7959 if Present
(Aggr
) then
7960 Insert_Action
(Aggr
, Item
);
7962 pragma Assert
(Present
(Stmts
));
7963 Append_To
(Stmts
, Item
);
7969 Hook_Assign
: Node_Id
;
7970 Hook_Decl
: Node_Id
;
7974 Res_Typ
: Entity_Id
;
7976 -- Start of processing for Process_Transient_Component
7979 -- Add the access type, which provides a reference to the function
7980 -- result. Generate:
7982 -- type Res_Typ is access all Comp_Typ;
7984 Res_Typ
:= Make_Temporary
(Loc
, 'A');
7985 Set_Ekind
(Res_Typ
, E_General_Access_Type
);
7986 Set_Directly_Designated_Type
(Res_Typ
, Comp_Typ
);
7989 (Make_Full_Type_Declaration
(Loc
,
7990 Defining_Identifier
=> Res_Typ
,
7992 Make_Access_To_Object_Definition
(Loc
,
7993 All_Present
=> True,
7994 Subtype_Indication
=> New_Occurrence_Of
(Comp_Typ
, Loc
))));
7996 -- Add the temporary which captures the result of the function call.
7999 -- Res : constant Res_Typ := Init_Expr'Reference;
8001 -- Note that this temporary is effectively a transient object because
8002 -- its lifetime is bounded by the current array or record component.
8004 Res_Id
:= Make_Temporary
(Loc
, 'R');
8005 Set_Ekind
(Res_Id
, E_Constant
);
8006 Set_Etype
(Res_Id
, Res_Typ
);
8008 -- Mark the transient object as successfully processed to avoid double
8011 Set_Is_Finalized_Transient
(Res_Id
);
8013 -- Signal the general finalization machinery that this transient object
8014 -- should not be considered for finalization actions because its cleanup
8015 -- will be performed by Process_Transient_Component_Completion.
8017 Set_Is_Ignored_Transient
(Res_Id
);
8020 Make_Object_Declaration
(Loc
,
8021 Defining_Identifier
=> Res_Id
,
8022 Constant_Present
=> True,
8023 Object_Definition
=> New_Occurrence_Of
(Res_Typ
, Loc
),
8025 Make_Reference
(Loc
, New_Copy_Tree
(Init_Expr
)));
8027 Add_Item
(Res_Decl
);
8029 -- Construct all pieces necessary to hook and finalize the transient
8032 Build_Transient_Object_Statements
8033 (Obj_Decl
=> Res_Decl
,
8034 Fin_Call
=> Fin_Call
,
8035 Hook_Assign
=> Hook_Assign
,
8036 Hook_Clear
=> Hook_Clear
,
8037 Hook_Decl
=> Hook_Decl
,
8038 Ptr_Decl
=> Ptr_Decl
);
8040 -- Add the access type which provides a reference to the transient
8041 -- result. Generate:
8043 -- type Ptr_Typ is access all Comp_Typ;
8045 Add_Item
(Ptr_Decl
);
8047 -- Add the temporary which acts as a hook to the transient result.
8050 -- Hook : Ptr_Typ := null;
8052 Add_Item
(Hook_Decl
);
8054 -- Attach the transient result to the hook. Generate:
8056 -- Hook := Ptr_Typ (Res);
8058 Add_Item
(Hook_Assign
);
8060 -- The original initialization expression now references the value of
8061 -- the temporary function result. Generate:
8066 Make_Explicit_Dereference
(Loc
,
8067 Prefix
=> New_Occurrence_Of
(Res_Id
, Loc
)));
8068 end Process_Transient_Component
;
8070 --------------------------------------------
8071 -- Process_Transient_Component_Completion --
8072 --------------------------------------------
8074 procedure Process_Transient_Component_Completion
8078 Hook_Clear
: Node_Id
;
8081 Exceptions_OK
: constant Boolean :=
8082 not Restriction_Active
(No_Exception_Propagation
);
8085 pragma Assert
(Present
(Hook_Clear
));
8087 -- Generate the following code if exception propagation is allowed:
8090 -- Abort : constant Boolean := Triggered_By_Abort;
8092 -- Abort : constant Boolean := False; -- no abort
8094 -- E : Exception_Occurrence;
8095 -- Raised : Boolean := False;
8102 -- [Deep_]Finalize (Res.all);
8106 -- if not Raised then
8108 -- Save_Occurrence (E,
8109 -- Get_Curent_Excep.all.all);
8115 -- if Raised and then not Abort then
8116 -- Raise_From_Controlled_Operation (E);
8120 if Exceptions_OK
then
8121 Abort_And_Exception
: declare
8122 Blk_Decls
: constant List_Id
:= New_List
;
8123 Blk_Stmts
: constant List_Id
:= New_List
;
8124 Fin_Stmts
: constant List_Id
:= New_List
;
8126 Fin_Data
: Finalization_Exception_Data
;
8129 -- Create the declarations of the two flags and the exception
8132 Build_Object_Declarations
(Fin_Data
, Blk_Decls
, Loc
);
8137 if Abort_Allowed
then
8138 Append_To
(Blk_Stmts
,
8139 Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
8142 -- Wrap the hook clear and the finalization call in order to trap
8143 -- a potential exception.
8145 Append_To
(Fin_Stmts
, Hook_Clear
);
8147 if Present
(Fin_Call
) then
8148 Append_To
(Fin_Stmts
, Fin_Call
);
8151 Append_To
(Blk_Stmts
,
8152 Make_Block_Statement
(Loc
,
8153 Handled_Statement_Sequence
=>
8154 Make_Handled_Sequence_Of_Statements
(Loc
,
8155 Statements
=> Fin_Stmts
,
8156 Exception_Handlers
=> New_List
(
8157 Build_Exception_Handler
(Fin_Data
)))));
8162 if Abort_Allowed
then
8163 Append_To
(Blk_Stmts
,
8164 Build_Runtime_Call
(Loc
, RE_Abort_Undefer
));
8167 -- Reraise the potential exception with a proper "upgrade" to
8168 -- Program_Error if needed.
8170 Append_To
(Blk_Stmts
, Build_Raise_Statement
(Fin_Data
));
8172 -- Wrap everything in a block
8175 Make_Block_Statement
(Loc
,
8176 Declarations
=> Blk_Decls
,
8177 Handled_Statement_Sequence
=>
8178 Make_Handled_Sequence_Of_Statements
(Loc
,
8179 Statements
=> Blk_Stmts
)));
8180 end Abort_And_Exception
;
8182 -- Generate the following code if exception propagation is not allowed
8183 -- and aborts are allowed:
8188 -- [Deep_]Finalize (Res.all);
8190 -- Abort_Undefer_Direct;
8193 elsif Abort_Allowed
then
8194 Abort_Only
: declare
8195 Blk_Stmts
: constant List_Id
:= New_List
;
8198 Append_To
(Blk_Stmts
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
8199 Append_To
(Blk_Stmts
, Hook_Clear
);
8201 if Present
(Fin_Call
) then
8202 Append_To
(Blk_Stmts
, Fin_Call
);
8206 Build_Abort_Undefer_Block
(Loc
,
8211 -- Otherwise generate:
8214 -- [Deep_]Finalize (Res.all);
8217 Append_To
(Stmts
, Hook_Clear
);
8219 if Present
(Fin_Call
) then
8220 Append_To
(Stmts
, Fin_Call
);
8223 end Process_Transient_Component_Completion
;
8225 ---------------------
8226 -- Sort_Case_Table --
8227 ---------------------
8229 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
8230 L
: constant Int
:= Case_Table
'First;
8231 U
: constant Int
:= Case_Table
'Last;
8239 T
:= Case_Table
(K
+ 1);
8243 and then Expr_Value
(Case_Table
(J
- 1).Choice_Lo
) >
8244 Expr_Value
(T
.Choice_Lo
)
8246 Case_Table
(J
) := Case_Table
(J
- 1);
8250 Case_Table
(J
) := T
;
8253 end Sort_Case_Table
;
8255 ----------------------------
8256 -- Static_Array_Aggregate --
8257 ----------------------------
8259 function Static_Array_Aggregate
(N
: Node_Id
) return Boolean is
8260 Bounds
: constant Node_Id
:= Aggregate_Bounds
(N
);
8262 Typ
: constant Entity_Id
:= Etype
(N
);
8263 Comp_Type
: constant Entity_Id
:= Component_Type
(Typ
);
8270 if Is_Tagged_Type
(Typ
)
8271 or else Is_Controlled
(Typ
)
8272 or else Is_Packed
(Typ
)
8278 and then Nkind
(Bounds
) = N_Range
8279 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
8280 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
8282 Lo
:= Low_Bound
(Bounds
);
8283 Hi
:= High_Bound
(Bounds
);
8285 if No
(Component_Associations
(N
)) then
8287 -- Verify that all components are static integers
8289 Expr
:= First
(Expressions
(N
));
8290 while Present
(Expr
) loop
8291 if Nkind
(Expr
) /= N_Integer_Literal
then
8301 -- We allow only a single named association, either a static
8302 -- range or an others_clause, with a static expression.
8304 Expr
:= First
(Component_Associations
(N
));
8306 if Present
(Expressions
(N
)) then
8309 elsif Present
(Next
(Expr
)) then
8312 elsif Present
(Next
(First
(Choice_List
(Expr
)))) then
8316 -- The aggregate is static if all components are literals,
8317 -- or else all its components are static aggregates for the
8318 -- component type. We also limit the size of a static aggregate
8319 -- to prevent runaway static expressions.
8321 if Is_Array_Type
(Comp_Type
)
8322 or else Is_Record_Type
(Comp_Type
)
8324 if Nkind
(Expression
(Expr
)) /= N_Aggregate
8326 not Compile_Time_Known_Aggregate
(Expression
(Expr
))
8331 elsif Nkind
(Expression
(Expr
)) /= N_Integer_Literal
then
8335 if not Aggr_Size_OK
(N
, Typ
) then
8339 -- Create a positional aggregate with the right number of
8340 -- copies of the expression.
8342 Agg
:= Make_Aggregate
(Sloc
(N
), New_List
, No_List
);
8344 for I
in UI_To_Int
(Intval
(Lo
)) .. UI_To_Int
(Intval
(Hi
))
8346 Append_To
(Expressions
(Agg
), New_Copy
(Expression
(Expr
)));
8348 -- The copied expression must be analyzed and resolved.
8349 -- Besides setting the type, this ensures that static
8350 -- expressions are appropriately marked as such.
8353 (Last
(Expressions
(Agg
)), Component_Type
(Typ
));
8356 Set_Aggregate_Bounds
(Agg
, Bounds
);
8357 Set_Etype
(Agg
, Typ
);
8360 Set_Compile_Time_Known_Aggregate
(N
);
8369 end Static_Array_Aggregate
;
8371 ----------------------------------
8372 -- Two_Dim_Packed_Array_Handled --
8373 ----------------------------------
8375 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean is
8376 Loc
: constant Source_Ptr
:= Sloc
(N
);
8377 Typ
: constant Entity_Id
:= Etype
(N
);
8378 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
8379 Comp_Size
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
8380 Packed_Array
: constant Entity_Id
:=
8381 Packed_Array_Impl_Type
(Base_Type
(Typ
));
8384 -- Expression in original aggregate
8387 -- One-dimensional subaggregate
8391 -- For now, only deal with cases where an integral number of elements
8392 -- fit in a single byte. This includes the most common boolean case.
8394 if not (Comp_Size
= 1 or else
8395 Comp_Size
= 2 or else
8401 Convert_To_Positional
8402 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
8404 -- Verify that all components are static
8406 if Nkind
(N
) = N_Aggregate
8407 and then Compile_Time_Known_Aggregate
(N
)
8411 -- The aggregate may have been reanalyzed and converted already
8413 elsif Nkind
(N
) /= N_Aggregate
then
8416 -- If component associations remain, the aggregate is not static
8418 elsif Present
(Component_Associations
(N
)) then
8422 One_Dim
:= First
(Expressions
(N
));
8423 while Present
(One_Dim
) loop
8424 if Present
(Component_Associations
(One_Dim
)) then
8428 One_Comp
:= First
(Expressions
(One_Dim
));
8429 while Present
(One_Comp
) loop
8430 if not Is_OK_Static_Expression
(One_Comp
) then
8441 -- Two-dimensional aggregate is now fully positional so pack one
8442 -- dimension to create a static one-dimensional array, and rewrite
8443 -- as an unchecked conversion to the original type.
8446 Byte_Size
: constant Int
:= UI_To_Int
(Component_Size
(Packed_Array
));
8447 -- The packed array type is a byte array
8450 -- Number of components accumulated in current byte
8453 -- Assembled list of packed values for equivalent aggregate
8456 -- Integer value of component
8459 -- Step size for packing
8462 -- Endian-dependent start position for packing
8465 -- Current insertion position
8468 -- Component of packed array being assembled
8475 -- Account for endianness. See corresponding comment in
8476 -- Packed_Array_Aggregate_Handled concerning the following.
8480 xor Reverse_Storage_Order
(Base_Type
(Typ
))
8482 Init_Shift
:= Byte_Size
- Comp_Size
;
8489 -- Iterate over each subaggregate
8491 Shift
:= Init_Shift
;
8492 One_Dim
:= First
(Expressions
(N
));
8493 while Present
(One_Dim
) loop
8494 One_Comp
:= First
(Expressions
(One_Dim
));
8495 while Present
(One_Comp
) loop
8496 if Packed_Num
= Byte_Size
/ Comp_Size
then
8498 -- Byte is complete, add to list of expressions
8500 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
8502 Shift
:= Init_Shift
;
8506 Comp_Val
:= Expr_Rep_Value
(One_Comp
);
8508 -- Adjust for bias, and strip proper number of bits
8510 if Has_Biased_Representation
(Ctyp
) then
8511 Comp_Val
:= Comp_Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
8514 Comp_Val
:= Comp_Val
mod Uint_2
** Comp_Size
;
8515 Val
:= UI_To_Int
(Val
+ Comp_Val
* Uint_2
** Shift
);
8516 Shift
:= Shift
+ Incr
;
8517 One_Comp
:= Next
(One_Comp
);
8518 Packed_Num
:= Packed_Num
+ 1;
8522 One_Dim
:= Next
(One_Dim
);
8525 if Packed_Num
> 0 then
8527 -- Add final incomplete byte if present
8529 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
8533 Unchecked_Convert_To
(Typ
,
8534 Make_Qualified_Expression
(Loc
,
8535 Subtype_Mark
=> New_Occurrence_Of
(Packed_Array
, Loc
),
8536 Expression
=> Make_Aggregate
(Loc
, Expressions
=> Comps
))));
8537 Analyze_And_Resolve
(N
);
8540 end Two_Dim_Packed_Array_Handled
;