1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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 Fname
; use Fname
;
41 with Freeze
; use Freeze
;
42 with Itypes
; use Itypes
;
44 with Namet
; use Namet
;
45 with Nmake
; use Nmake
;
46 with Nlists
; use Nlists
;
48 with Restrict
; use Restrict
;
49 with Rident
; use Rident
;
50 with Rtsfind
; use Rtsfind
;
51 with Ttypes
; use Ttypes
;
53 with Sem_Aggr
; use Sem_Aggr
;
54 with Sem_Aux
; use Sem_Aux
;
55 with Sem_Ch3
; use Sem_Ch3
;
56 with Sem_Eval
; use Sem_Eval
;
57 with Sem_Res
; use Sem_Res
;
58 with Sem_Util
; use Sem_Util
;
59 with Sinfo
; use Sinfo
;
60 with Snames
; use Snames
;
61 with Stand
; use Stand
;
62 with Stringt
; use Stringt
;
63 with Targparm
; use Targparm
;
64 with Tbuild
; use Tbuild
;
65 with Uintp
; use Uintp
;
67 package body Exp_Aggr
is
69 type Case_Bounds
is record
72 Choice_Node
: Node_Id
;
75 type Case_Table_Type
is array (Nat
range <>) of Case_Bounds
;
76 -- Table type used by Check_Case_Choices procedure
78 procedure Collect_Initialization_Statements
81 Node_After
: Node_Id
);
82 -- If Obj is not frozen, collect actions inserted after N until, but not
83 -- including, Node_After, for initialization of Obj, and move them to an
84 -- expression with actions, which becomes the Initialization_Statements for
87 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean;
88 -- N is an aggregate (record or array). Checks the presence of default
89 -- initialization (<>) in any component (Ada 2005: AI-287).
91 function In_Object_Declaration
(N
: Node_Id
) return Boolean;
92 -- Return True if N is part of an object declaration, False otherwise
94 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean;
95 -- Returns true if N is an aggregate used to initialize the components
96 -- of a statically allocated dispatch table.
99 (Obj_Type
: Entity_Id
;
100 Typ
: Entity_Id
) return Boolean;
101 -- A static array aggregate in an object declaration can in most cases be
102 -- expanded in place. The one exception is when the aggregate is given
103 -- with component associations that specify different bounds from those of
104 -- the type definition in the object declaration. In this pathological
105 -- case the aggregate must slide, and we must introduce an intermediate
106 -- temporary to hold it.
108 -- The same holds in an assignment to one-dimensional array of arrays,
109 -- when a component may be given with bounds that differ from those of the
112 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
);
113 -- Sort the Case Table using the Lower Bound of each Choice as the key.
114 -- A simple insertion sort is used since the number of choices in a case
115 -- statement of variant part will usually be small and probably in near
118 ------------------------------------------------------
119 -- Local subprograms for Record Aggregate Expansion --
120 ------------------------------------------------------
122 function Build_Record_Aggr_Code
125 Lhs
: Node_Id
) return List_Id
;
126 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
127 -- aggregate. Target is an expression containing the location on which the
128 -- component by component assignments will take place. Returns the list of
129 -- assignments plus all other adjustments needed for tagged and controlled
132 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
);
133 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
134 -- aggregate (which can only be a record type, this procedure is only used
135 -- for record types). Transform the given aggregate into a sequence of
136 -- assignments performed component by component.
138 procedure Expand_Record_Aggregate
140 Orig_Tag
: Node_Id
:= Empty
;
141 Parent_Expr
: Node_Id
:= Empty
);
142 -- This is the top level procedure for record aggregate expansion.
143 -- Expansion for record aggregates needs expand aggregates for tagged
144 -- record types. Specifically Expand_Record_Aggregate adds the Tag
145 -- field in front of the Component_Association list that was created
146 -- during resolution by Resolve_Record_Aggregate.
148 -- N is the record aggregate node.
149 -- Orig_Tag is the value of the Tag that has to be provided for this
150 -- specific aggregate. It carries the tag corresponding to the type
151 -- of the outermost aggregate during the recursive expansion
152 -- Parent_Expr is the ancestor part of the original extension
155 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean;
156 -- Return true if one of the components is of a discriminated type with
157 -- defaults. An aggregate for a type with mutable components must be
158 -- expanded into individual assignments.
160 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
);
161 -- If the type of the aggregate is a type extension with renamed discrimi-
162 -- nants, we must initialize the hidden discriminants of the parent.
163 -- Otherwise, the target object must not be initialized. The discriminants
164 -- are initialized by calling the initialization procedure for the type.
165 -- This is incorrect if the initialization of other components has any
166 -- side effects. We restrict this call to the case where the parent type
167 -- has a variant part, because this is the only case where the hidden
168 -- discriminants are accessed, namely when calling discriminant checking
169 -- functions of the parent type, and when applying a stream attribute to
170 -- an object of the derived type.
172 -----------------------------------------------------
173 -- Local Subprograms for Array Aggregate Expansion --
174 -----------------------------------------------------
176 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean;
177 -- Very large static aggregates present problems to the back-end, and are
178 -- transformed into assignments and loops. This function verifies that the
179 -- total number of components of an aggregate is acceptable for rewriting
180 -- into a purely positional static form. Aggr_Size_OK must be called before
183 -- This function also detects and warns about one-component aggregates that
184 -- appear in a non-static context. Even if the component value is static,
185 -- such an aggregate must be expanded into an assignment.
187 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean;
188 -- This function checks if array aggregate N can be processed directly
189 -- by the backend. If this is the case, True is returned.
191 function Build_Array_Aggr_Code
196 Scalar_Comp
: Boolean;
197 Indexes
: List_Id
:= No_List
) return List_Id
;
198 -- This recursive routine returns a list of statements containing the
199 -- loops and assignments that are needed for the expansion of the array
202 -- N is the (sub-)aggregate node to be expanded into code. This node has
203 -- been fully analyzed, and its Etype is properly set.
205 -- Index is the index node corresponding to the array sub-aggregate N
207 -- Into is the target expression into which we are copying the aggregate.
208 -- Note that this node may not have been analyzed yet, and so the Etype
209 -- field may not be set.
211 -- Scalar_Comp is True if the component type of the aggregate is scalar
213 -- Indexes is the current list of expressions used to index the object we
216 procedure Convert_Array_Aggr_In_Allocator
220 -- If the aggregate appears within an allocator and can be expanded in
221 -- place, this routine generates the individual assignments to components
222 -- of the designated object. This is an optimization over the general
223 -- case, where a temporary is first created on the stack and then used to
224 -- construct the allocated object on the heap.
226 procedure Convert_To_Positional
228 Max_Others_Replicate
: Nat
:= 5;
229 Handle_Bit_Packed
: Boolean := False);
230 -- If possible, convert named notation to positional notation. This
231 -- conversion is possible only in some static cases. If the conversion is
232 -- possible, then N is rewritten with the analyzed converted aggregate.
233 -- The parameter Max_Others_Replicate controls the maximum number of
234 -- values corresponding to an others choice that will be converted to
235 -- positional notation (the default of 5 is the normal limit, and reflects
236 -- the fact that normally the loop is better than a lot of separate
237 -- assignments). Note that this limit gets overridden in any case if
238 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
239 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
240 -- not expect the back end to handle bit packed arrays, so the normal case
241 -- of conversion is pointless), but in the special case of a call from
242 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
243 -- these are cases we handle in there.
245 -- It would seem useful to have a higher default for Max_Others_Replicate,
246 -- but aggregates in the compiler make this impossible: the compiler
247 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
250 procedure Expand_Array_Aggregate
(N
: Node_Id
);
251 -- This is the top-level routine to perform array aggregate expansion.
252 -- N is the N_Aggregate node to be expanded.
254 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean;
255 -- For two-dimensional packed aggregates with constant bounds and constant
256 -- components, it is preferable to pack the inner aggregates because the
257 -- whole matrix can then be presented to the back-end as a one-dimensional
258 -- list of literals. This is much more efficient than expanding into single
259 -- component assignments. This function determines if the type Typ is for
260 -- an array that is suitable for this optimization: it returns True if Typ
261 -- is a two dimensional bit packed array with component size 1, 2, or 4.
263 function Late_Expansion
266 Target
: Node_Id
) return List_Id
;
267 -- This routine implements top-down expansion of nested aggregates. In
268 -- doing so, it avoids the generation of temporaries at each level. N is
269 -- a nested record or array aggregate with the Expansion_Delayed flag.
270 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
271 -- expression that will hold the result of the aggregate expansion.
273 function Make_OK_Assignment_Statement
276 Expression
: Node_Id
) return Node_Id
;
277 -- This is like Make_Assignment_Statement, except that Assignment_OK
278 -- is set in the left operand. All assignments built by this unit use
279 -- this routine. This is needed to deal with assignments to initialized
280 -- constants that are done in place.
282 function Number_Of_Choices
(N
: Node_Id
) return Nat
;
283 -- Returns the number of discrete choices (not including the others choice
284 -- if present) contained in (sub-)aggregate N.
286 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean;
287 -- Given an array aggregate, this function handles the case of a packed
288 -- array aggregate with all constant values, where the aggregate can be
289 -- evaluated at compile time. If this is possible, then N is rewritten
290 -- to be its proper compile time value with all the components properly
291 -- assembled. The expression is analyzed and resolved and True is returned.
292 -- If this transformation is not possible, N is unchanged and False is
295 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean;
296 -- If the type of the aggregate is a two-dimensional bit_packed array
297 -- it may be transformed into an array of bytes with constant values,
298 -- and presented to the back-end as a static value. The function returns
299 -- false if this transformation cannot be performed. THis is similar to,
300 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
306 function Aggr_Size_OK
(N
: Node_Id
; Typ
: Entity_Id
) return Boolean is
315 -- Determines the maximum size of an array aggregate produced by
316 -- converting named to positional notation (e.g. from others clauses).
317 -- This avoids running away with attempts to convert huge aggregates,
318 -- which hit memory limits in the backend.
320 function Component_Count
(T
: Entity_Id
) return Int
;
321 -- The limit is applied to the total number of components that the
322 -- aggregate will have, which is the number of static expressions
323 -- that will appear in the flattened array. This requires a recursive
324 -- computation of the number of scalar components of the structure.
326 ---------------------
327 -- Component_Count --
328 ---------------------
330 function Component_Count
(T
: Entity_Id
) return Int
is
335 if Is_Scalar_Type
(T
) then
338 elsif Is_Record_Type
(T
) then
339 Comp
:= First_Component
(T
);
340 while Present
(Comp
) loop
341 Res
:= Res
+ Component_Count
(Etype
(Comp
));
342 Next_Component
(Comp
);
347 elsif Is_Array_Type
(T
) then
349 Lo
: constant Node_Id
:=
350 Type_Low_Bound
(Etype
(First_Index
(T
)));
351 Hi
: constant Node_Id
:=
352 Type_High_Bound
(Etype
(First_Index
(T
)));
354 Siz
: constant Int
:= Component_Count
(Component_Type
(T
));
357 if not Compile_Time_Known_Value
(Lo
)
358 or else not Compile_Time_Known_Value
(Hi
)
363 Siz
* UI_To_Int
(Expr_Value
(Hi
) - Expr_Value
(Lo
) + 1);
368 -- Can only be a null for an access type
374 -- Start of processing for Aggr_Size_OK
377 -- The normal aggregate limit is 50000, but we increase this limit to
378 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
379 -- Restrictions (No_Implicit_Loops) is specified, since in either case
380 -- we are at risk of declaring the program illegal because of this
381 -- limit. We also increase the limit when Static_Elaboration_Desired,
382 -- given that this means that objects are intended to be placed in data
385 -- We also increase the limit if the aggregate is for a packed two-
386 -- dimensional array, because if components are static it is much more
387 -- efficient to construct a one-dimensional equivalent array with static
390 -- Conversely, we decrease the maximum size if none of the above
391 -- requirements apply, and if the aggregate has a single component
392 -- association, which will be more efficient if implemented with a loop.
394 -- Finally, we use a small limit in CodePeer mode where we favor loops
395 -- instead of thousands of single assignments (from large aggregates).
397 Max_Aggr_Size
:= 50000;
399 if CodePeer_Mode
then
400 Max_Aggr_Size
:= 100;
402 elsif Restriction_Active
(No_Elaboration_Code
)
403 or else Restriction_Active
(No_Implicit_Loops
)
404 or else Is_Two_Dim_Packed_Array
(Typ
)
405 or else (Ekind
(Current_Scope
) = E_Package
406 and then Static_Elaboration_Desired
(Current_Scope
))
408 Max_Aggr_Size
:= 2 ** 24;
410 elsif No
(Expressions
(N
))
411 and then No
(Next
(First
(Component_Associations
(N
))))
413 Max_Aggr_Size
:= 5000;
416 Siz
:= Component_Count
(Component_Type
(Typ
));
418 Indx
:= First_Index
(Typ
);
419 while Present
(Indx
) loop
420 Lo
:= Type_Low_Bound
(Etype
(Indx
));
421 Hi
:= Type_High_Bound
(Etype
(Indx
));
423 -- Bounds need to be known at compile time
425 if not Compile_Time_Known_Value
(Lo
)
426 or else not Compile_Time_Known_Value
(Hi
)
431 Lov
:= Expr_Value
(Lo
);
432 Hiv
:= Expr_Value
(Hi
);
434 -- A flat array is always safe
440 -- One-component aggregates are suspicious, and if the context type
441 -- is an object declaration with non-static bounds it will trip gcc;
442 -- such an aggregate must be expanded into a single assignment.
444 if Hiv
= Lov
and then Nkind
(Parent
(N
)) = N_Object_Declaration
then
446 Index_Type
: constant Entity_Id
:=
448 (First_Index
(Etype
(Defining_Identifier
(Parent
(N
)))));
452 if not Compile_Time_Known_Value
(Type_Low_Bound
(Index_Type
))
453 or else not Compile_Time_Known_Value
454 (Type_High_Bound
(Index_Type
))
456 if Present
(Component_Associations
(N
)) then
458 First
(Choices
(First
(Component_Associations
(N
))));
460 if Is_Entity_Name
(Indx
)
461 and then not Is_Type
(Entity
(Indx
))
464 ("single component aggregate in "
465 & "non-static context??", Indx
);
466 Error_Msg_N
("\maybe subtype name was meant??", Indx
);
476 Rng
: constant Uint
:= Hiv
- Lov
+ 1;
479 -- Check if size is too large
481 if not UI_Is_In_Int_Range
(Rng
) then
485 Siz
:= Siz
* UI_To_Int
(Rng
);
489 or else Siz
> Max_Aggr_Size
494 -- Bounds must be in integer range, for later array construction
496 if not UI_Is_In_Int_Range
(Lov
)
498 not UI_Is_In_Int_Range
(Hiv
)
509 ---------------------------------
510 -- Backend_Processing_Possible --
511 ---------------------------------
513 -- Backend processing by Gigi/gcc is possible only if all the following
514 -- conditions are met:
516 -- 1. N is fully positional
518 -- 2. N is not a bit-packed array aggregate;
520 -- 3. The size of N's array type must be known at compile time. Note
521 -- that this implies that the component size is also known
523 -- 4. The array type of N does not follow the Fortran layout convention
524 -- or if it does it must be 1 dimensional.
526 -- 5. The array component type may not be tagged (which could necessitate
527 -- reassignment of proper tags).
529 -- 6. The array component type must not have unaligned bit components
531 -- 7. None of the components of the aggregate may be bit unaligned
534 -- 8. There cannot be delayed components, since we do not know enough
535 -- at this stage to know if back end processing is possible.
537 -- 9. There cannot be any discriminated record components, since the
538 -- back end cannot handle this complex case.
540 -- 10. No controlled actions need to be generated for components
542 function Backend_Processing_Possible
(N
: Node_Id
) return Boolean is
543 Typ
: constant Entity_Id
:= Etype
(N
);
544 -- Typ is the correct constrained array subtype of the aggregate
546 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean;
547 -- This routine checks components of aggregate N, enforcing checks
548 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
549 -- performed on subaggregates. The Index value is the current index
550 -- being checked in the multi-dimensional case.
552 ---------------------
553 -- Component_Check --
554 ---------------------
556 function Component_Check
(N
: Node_Id
; Index
: Node_Id
) return Boolean is
560 -- Checks 1: (no component associations)
562 if Present
(Component_Associations
(N
)) then
566 -- Checks on components
568 -- Recurse to check subaggregates, which may appear in qualified
569 -- expressions. If delayed, the front-end will have to expand.
570 -- If the component is a discriminated record, treat as non-static,
571 -- as the back-end cannot handle this properly.
573 Expr
:= First
(Expressions
(N
));
574 while Present
(Expr
) loop
576 -- Checks 8: (no delayed components)
578 if Is_Delayed_Aggregate
(Expr
) then
582 -- Checks 9: (no discriminated records)
584 if Present
(Etype
(Expr
))
585 and then Is_Record_Type
(Etype
(Expr
))
586 and then Has_Discriminants
(Etype
(Expr
))
591 -- Checks 7. Component must not be bit aligned component
593 if Possible_Bit_Aligned_Component
(Expr
) then
597 -- Recursion to following indexes for multiple dimension case
599 if Present
(Next_Index
(Index
))
600 and then not Component_Check
(Expr
, Next_Index
(Index
))
605 -- All checks for that component finished, on to next
613 -- Start of processing for Backend_Processing_Possible
616 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
618 if Is_Bit_Packed_Array
(Typ
) or else Needs_Finalization
(Typ
) then
622 -- If component is limited, aggregate must be expanded because each
623 -- component assignment must be built in place.
625 if Is_Limited_View
(Component_Type
(Typ
)) then
629 -- Checks 4 (array must not be multi-dimensional Fortran case)
631 if Convention
(Typ
) = Convention_Fortran
632 and then Number_Dimensions
(Typ
) > 1
637 -- Checks 3 (size of array must be known at compile time)
639 if not Size_Known_At_Compile_Time
(Typ
) then
643 -- Checks on components
645 if not Component_Check
(N
, First_Index
(Typ
)) then
649 -- Checks 5 (if the component type is tagged, then we may need to do
650 -- tag adjustments. Perhaps this should be refined to check for any
651 -- component associations that actually need tag adjustment, similar
652 -- to the test in Component_Not_OK_For_Backend for record aggregates
653 -- with tagged components, but not clear whether it's worthwhile ???;
654 -- in the case of virtual machines (no Tagged_Type_Expansion), object
655 -- tags are handled implicitly).
657 if Is_Tagged_Type
(Component_Type
(Typ
))
658 and then Tagged_Type_Expansion
663 -- Checks 6 (component type must not have bit aligned components)
665 if Type_May_Have_Bit_Aligned_Components
(Component_Type
(Typ
)) then
669 -- Backend processing is possible
671 Set_Size_Known_At_Compile_Time
(Etype
(N
), True);
673 end Backend_Processing_Possible
;
675 ---------------------------
676 -- Build_Array_Aggr_Code --
677 ---------------------------
679 -- The code that we generate from a one dimensional aggregate is
681 -- 1. If the sub-aggregate contains discrete choices we
683 -- (a) Sort the discrete choices
685 -- (b) Otherwise for each discrete choice that specifies a range we
686 -- emit a loop. If a range specifies a maximum of three values, or
687 -- we are dealing with an expression we emit a sequence of
688 -- assignments instead of a loop.
690 -- (c) Generate the remaining loops to cover the others choice if any
692 -- 2. If the aggregate contains positional elements we
694 -- (a) translate the positional elements in a series of assignments
696 -- (b) Generate a final loop to cover the others choice if any.
697 -- Note that this final loop has to be a while loop since the case
699 -- L : Integer := Integer'Last;
700 -- H : Integer := Integer'Last;
701 -- A : array (L .. H) := (1, others =>0);
703 -- cannot be handled by a for loop. Thus for the following
705 -- array (L .. H) := (.. positional elements.., others =>E);
707 -- we always generate something like:
709 -- J : Index_Type := Index_Of_Last_Positional_Element;
711 -- J := Index_Base'Succ (J)
715 function Build_Array_Aggr_Code
720 Scalar_Comp
: Boolean;
721 Indexes
: List_Id
:= No_List
) return List_Id
723 Loc
: constant Source_Ptr
:= Sloc
(N
);
724 Index_Base
: constant Entity_Id
:= Base_Type
(Etype
(Index
));
725 Index_Base_L
: constant Node_Id
:= Type_Low_Bound
(Index_Base
);
726 Index_Base_H
: constant Node_Id
:= Type_High_Bound
(Index_Base
);
728 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
;
729 -- Returns an expression where Val is added to expression To, unless
730 -- To+Val is provably out of To's base type range. To must be an
731 -- already analyzed expression.
733 function Empty_Range
(L
, H
: Node_Id
) return Boolean;
734 -- Returns True if the range defined by L .. H is certainly empty
736 function Equal
(L
, H
: Node_Id
) return Boolean;
737 -- Returns True if L = H for sure
739 function Index_Base_Name
return Node_Id
;
740 -- Returns a new reference to the index type name
742 function Gen_Assign
(Ind
: Node_Id
; Expr
: Node_Id
) return List_Id
;
743 -- Ind must be a side-effect free expression. If the input aggregate
744 -- N to Build_Loop contains no sub-aggregates, then this function
745 -- returns the assignment statement:
747 -- Into (Indexes, Ind) := Expr;
749 -- Otherwise we call Build_Code recursively
751 -- Ada 2005 (AI-287): In case of default initialized component, Expr
752 -- is empty and we generate a call to the corresponding IP subprogram.
754 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
755 -- Nodes L and H must be side-effect free expressions.
756 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
757 -- This routine returns the for loop statement
759 -- for J in Index_Base'(L) .. Index_Base'(H) loop
760 -- Into (Indexes, J) := Expr;
763 -- Otherwise we call Build_Code recursively.
764 -- As an optimization if the loop covers 3 or less scalar elements we
765 -- generate a sequence of assignments.
767 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
;
768 -- Nodes L and H must be side-effect free expressions.
769 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
770 -- This routine returns the while loop statement
772 -- J : Index_Base := L;
774 -- J := Index_Base'Succ (J);
775 -- Into (Indexes, J) := Expr;
778 -- Otherwise we call Build_Code recursively
780 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
;
781 -- For an association with a box, use value given by aspect
782 -- Default_Component_Value of array type if specified, else use
783 -- value given by aspect Default_Value for component type itself
784 -- if specified, else return Empty.
786 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean;
787 function Local_Expr_Value
(E
: Node_Id
) return Uint
;
788 -- These two Local routines are used to replace the corresponding ones
789 -- in sem_eval because while processing the bounds of an aggregate with
790 -- discrete choices whose index type is an enumeration, we build static
791 -- expressions not recognized by Compile_Time_Known_Value as such since
792 -- they have not yet been analyzed and resolved. All the expressions in
793 -- question are things like Index_Base_Name'Val (Const) which we can
794 -- easily recognize as being constant.
800 function Add
(Val
: Int
; To
: Node_Id
) return Node_Id
is
805 U_Val
: constant Uint
:= UI_From_Int
(Val
);
808 -- Note: do not try to optimize the case of Val = 0, because
809 -- we need to build a new node with the proper Sloc value anyway.
811 -- First test if we can do constant folding
813 if Local_Compile_Time_Known_Value
(To
) then
814 U_To
:= Local_Expr_Value
(To
) + Val
;
816 -- Determine if our constant is outside the range of the index.
817 -- If so return an Empty node. This empty node will be caught
818 -- by Empty_Range below.
820 if Compile_Time_Known_Value
(Index_Base_L
)
821 and then U_To
< Expr_Value
(Index_Base_L
)
825 elsif Compile_Time_Known_Value
(Index_Base_H
)
826 and then U_To
> Expr_Value
(Index_Base_H
)
831 Expr_Pos
:= Make_Integer_Literal
(Loc
, U_To
);
832 Set_Is_Static_Expression
(Expr_Pos
);
834 if not Is_Enumeration_Type
(Index_Base
) then
837 -- If we are dealing with enumeration return
838 -- Index_Base'Val (Expr_Pos)
842 Make_Attribute_Reference
844 Prefix
=> Index_Base_Name
,
845 Attribute_Name
=> Name_Val
,
846 Expressions
=> New_List
(Expr_Pos
));
852 -- If we are here no constant folding possible
854 if not Is_Enumeration_Type
(Index_Base
) then
857 Left_Opnd
=> Duplicate_Subexpr
(To
),
858 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
860 -- If we are dealing with enumeration return
861 -- Index_Base'Val (Index_Base'Pos (To) + Val)
865 Make_Attribute_Reference
867 Prefix
=> Index_Base_Name
,
868 Attribute_Name
=> Name_Pos
,
869 Expressions
=> New_List
(Duplicate_Subexpr
(To
)));
874 Right_Opnd
=> Make_Integer_Literal
(Loc
, U_Val
));
877 Make_Attribute_Reference
879 Prefix
=> Index_Base_Name
,
880 Attribute_Name
=> Name_Val
,
881 Expressions
=> New_List
(Expr_Pos
));
891 function Empty_Range
(L
, H
: Node_Id
) return Boolean is
892 Is_Empty
: Boolean := False;
897 -- First check if L or H were already detected as overflowing the
898 -- index base range type by function Add above. If this is so Add
899 -- returns the empty node.
901 if No
(L
) or else No
(H
) then
908 -- L > H range is empty
914 -- B_L > H range must be empty
920 -- L > B_H range must be empty
924 High
:= Index_Base_H
;
927 if Local_Compile_Time_Known_Value
(Low
)
929 Local_Compile_Time_Known_Value
(High
)
932 UI_Gt
(Local_Expr_Value
(Low
), Local_Expr_Value
(High
));
945 function Equal
(L
, H
: Node_Id
) return Boolean is
950 elsif Local_Compile_Time_Known_Value
(L
)
952 Local_Compile_Time_Known_Value
(H
)
954 return UI_Eq
(Local_Expr_Value
(L
), Local_Expr_Value
(H
));
964 function Gen_Assign
(Ind
: Node_Id
; Expr
: Node_Id
) return List_Id
is
965 L
: constant List_Id
:= New_List
;
968 New_Indexes
: List_Id
;
969 Indexed_Comp
: Node_Id
;
971 Comp_Type
: Entity_Id
:= Empty
;
973 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
;
974 -- Collect insert_actions generated in the construction of a
975 -- loop, and prepend them to the sequence of assignments to
976 -- complete the eventual body of the loop.
978 ----------------------
979 -- Add_Loop_Actions --
980 ----------------------
982 function Add_Loop_Actions
(Lis
: List_Id
) return List_Id
is
986 -- Ada 2005 (AI-287): Do nothing else in case of default
987 -- initialized component.
992 elsif Nkind
(Parent
(Expr
)) = N_Component_Association
993 and then Present
(Loop_Actions
(Parent
(Expr
)))
995 Append_List
(Lis
, Loop_Actions
(Parent
(Expr
)));
996 Res
:= Loop_Actions
(Parent
(Expr
));
997 Set_Loop_Actions
(Parent
(Expr
), No_List
);
1003 end Add_Loop_Actions
;
1005 -- Start of processing for Gen_Assign
1008 if No
(Indexes
) then
1009 New_Indexes
:= New_List
;
1011 New_Indexes
:= New_Copy_List_Tree
(Indexes
);
1014 Append_To
(New_Indexes
, Ind
);
1016 if Present
(Next_Index
(Index
)) then
1019 Build_Array_Aggr_Code
1022 Index
=> Next_Index
(Index
),
1024 Scalar_Comp
=> Scalar_Comp
,
1025 Indexes
=> New_Indexes
));
1028 -- If we get here then we are at a bottom-level (sub-)aggregate
1032 (Make_Indexed_Component
(Loc
,
1033 Prefix
=> New_Copy_Tree
(Into
),
1034 Expressions
=> New_Indexes
));
1036 Set_Assignment_OK
(Indexed_Comp
);
1038 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1039 -- is not present (and therefore we also initialize Expr_Q to empty).
1043 elsif Nkind
(Expr
) = N_Qualified_Expression
then
1044 Expr_Q
:= Expression
(Expr
);
1049 if Present
(Etype
(N
)) and then Etype
(N
) /= Any_Composite
then
1050 Comp_Type
:= Component_Type
(Etype
(N
));
1051 pragma Assert
(Comp_Type
= Ctype
); -- AI-287
1053 elsif Present
(Next
(First
(New_Indexes
))) then
1055 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1056 -- component because we have received the component type in
1057 -- the formal parameter Ctype.
1059 -- ??? Some assert pragmas have been added to check if this new
1060 -- formal can be used to replace this code in all cases.
1062 if Present
(Expr
) then
1064 -- This is a multidimensional array. Recover the component type
1065 -- from the outermost aggregate, because subaggregates do not
1066 -- have an assigned type.
1073 while Present
(P
) loop
1074 if Nkind
(P
) = N_Aggregate
1075 and then Present
(Etype
(P
))
1077 Comp_Type
:= Component_Type
(Etype
(P
));
1085 pragma Assert
(Comp_Type
= Ctype
); -- AI-287
1090 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1091 -- default initialized components (otherwise Expr_Q is not present).
1094 and then Nkind_In
(Expr_Q
, N_Aggregate
, N_Extension_Aggregate
)
1096 -- At this stage the Expression may not have been analyzed yet
1097 -- because the array aggregate code has not been updated to use
1098 -- the Expansion_Delayed flag and avoid analysis altogether to
1099 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1100 -- the analysis of non-array aggregates now in order to get the
1101 -- value of Expansion_Delayed flag for the inner aggregate ???
1103 if Present
(Comp_Type
) and then not Is_Array_Type
(Comp_Type
) then
1104 Analyze_And_Resolve
(Expr_Q
, Comp_Type
);
1107 if Is_Delayed_Aggregate
(Expr_Q
) then
1109 -- This is either a subaggregate of a multidimensional array,
1110 -- or a component of an array type whose component type is
1111 -- also an array. In the latter case, the expression may have
1112 -- component associations that provide different bounds from
1113 -- those of the component type, and sliding must occur. Instead
1114 -- of decomposing the current aggregate assignment, force the
1115 -- re-analysis of the assignment, so that a temporary will be
1116 -- generated in the usual fashion, and sliding will take place.
1118 if Nkind
(Parent
(N
)) = N_Assignment_Statement
1119 and then Is_Array_Type
(Comp_Type
)
1120 and then Present
(Component_Associations
(Expr_Q
))
1121 and then Must_Slide
(Comp_Type
, Etype
(Expr_Q
))
1123 Set_Expansion_Delayed
(Expr_Q
, False);
1124 Set_Analyzed
(Expr_Q
, False);
1129 Late_Expansion
(Expr_Q
, Etype
(Expr_Q
), Indexed_Comp
));
1134 -- Ada 2005 (AI-287): In case of default initialized component, call
1135 -- the initialization subprogram associated with the component type.
1136 -- If the component type is an access type, add an explicit null
1137 -- assignment, because for the back-end there is an initialization
1138 -- present for the whole aggregate, and no default initialization
1141 -- In addition, if the component type is controlled, we must call
1142 -- its Initialize procedure explicitly, because there is no explicit
1143 -- object creation that will invoke it otherwise.
1146 if Present
(Base_Init_Proc
(Base_Type
(Ctype
)))
1147 or else Has_Task
(Base_Type
(Ctype
))
1150 Build_Initialization_Call
(Loc
,
1151 Id_Ref
=> Indexed_Comp
,
1153 With_Default_Init
=> True));
1155 -- If the component type has invariants, add an invariant
1156 -- check after the component is default-initialized. It will
1157 -- be analyzed and resolved before the code for initialization
1158 -- of other components.
1160 if Has_Invariants
(Ctype
) then
1161 Set_Etype
(Indexed_Comp
, Ctype
);
1162 Append_To
(L
, Make_Invariant_Call
(Indexed_Comp
));
1165 elsif Is_Access_Type
(Ctype
) then
1167 Make_Assignment_Statement
(Loc
,
1168 Name
=> Indexed_Comp
,
1169 Expression
=> Make_Null
(Loc
)));
1172 if Needs_Finalization
(Ctype
) then
1175 (Obj_Ref
=> New_Copy_Tree
(Indexed_Comp
),
1181 Make_OK_Assignment_Statement
(Loc
,
1182 Name
=> Indexed_Comp
,
1183 Expression
=> New_Copy_Tree
(Expr
));
1185 -- The target of the assignment may not have been initialized,
1186 -- so it is not possible to call Finalize as expected in normal
1187 -- controlled assignments. We must also avoid using the primitive
1188 -- _assign (which depends on a valid target, and may for example
1189 -- perform discriminant checks on it).
1191 -- Both Finalize and usage of _assign are disabled by setting
1192 -- No_Ctrl_Actions on the assignment. The rest of the controlled
1193 -- actions are done manually with the proper finalization list
1194 -- coming from the context.
1196 Set_No_Ctrl_Actions
(A
);
1198 -- If this is an aggregate for an array of arrays, each
1199 -- sub-aggregate will be expanded as well, and even with
1200 -- No_Ctrl_Actions the assignments of inner components will
1201 -- require attachment in their assignments to temporaries. These
1202 -- temporaries must be finalized for each subaggregate, to prevent
1203 -- multiple attachments of the same temporary location to same
1204 -- finalization chain (and consequently circular lists). To ensure
1205 -- that finalization takes place for each subaggregate we wrap the
1206 -- assignment in a block.
1208 if Present
(Comp_Type
)
1209 and then Needs_Finalization
(Comp_Type
)
1210 and then Is_Array_Type
(Comp_Type
)
1211 and then Present
(Expr
)
1214 Make_Block_Statement
(Loc
,
1215 Handled_Statement_Sequence
=>
1216 Make_Handled_Sequence_Of_Statements
(Loc
,
1217 Statements
=> New_List
(A
)));
1222 -- Adjust the tag if tagged (because of possible view
1223 -- conversions), unless compiling for a VM where tags
1226 if Present
(Comp_Type
)
1227 and then Is_Tagged_Type
(Comp_Type
)
1228 and then Tagged_Type_Expansion
1231 Full_Typ
: constant Entity_Id
:= Underlying_Type
(Comp_Type
);
1235 Make_OK_Assignment_Statement
(Loc
,
1237 Make_Selected_Component
(Loc
,
1238 Prefix
=> New_Copy_Tree
(Indexed_Comp
),
1241 (First_Tag_Component
(Full_Typ
), Loc
)),
1244 Unchecked_Convert_To
(RTE
(RE_Tag
),
1246 (Node
(First_Elmt
(Access_Disp_Table
(Full_Typ
))),
1253 -- Adjust and attach the component to the proper final list, which
1254 -- can be the controller of the outer record object or the final
1255 -- list associated with the scope.
1257 -- If the component is itself an array of controlled types, whose
1258 -- value is given by a sub-aggregate, then the attach calls have
1259 -- been generated when individual subcomponent are assigned, and
1260 -- must not be done again to prevent malformed finalization chains
1261 -- (see comments above, concerning the creation of a block to hold
1262 -- inner finalization actions).
1264 if Present
(Comp_Type
)
1265 and then Needs_Finalization
(Comp_Type
)
1266 and then not Is_Limited_Type
(Comp_Type
)
1268 (Is_Array_Type
(Comp_Type
)
1269 and then Is_Controlled
(Component_Type
(Comp_Type
))
1270 and then Nkind
(Expr
) = N_Aggregate
)
1274 (Obj_Ref
=> New_Copy_Tree
(Indexed_Comp
),
1279 return Add_Loop_Actions
(L
);
1286 function Gen_Loop
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1296 -- Index_Base'(L) .. Index_Base'(H)
1298 L_Iteration_Scheme
: Node_Id
;
1299 -- L_J in Index_Base'(L) .. Index_Base'(H)
1302 -- The statements to execute in the loop
1304 S
: constant List_Id
:= New_List
;
1305 -- List of statements
1308 -- Copy of expression tree, used for checking purposes
1311 -- If loop bounds define an empty range return the null statement
1313 if Empty_Range
(L
, H
) then
1314 Append_To
(S
, Make_Null_Statement
(Loc
));
1316 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1317 -- default initialized component.
1323 -- The expression must be type-checked even though no component
1324 -- of the aggregate will have this value. This is done only for
1325 -- actual components of the array, not for subaggregates. Do
1326 -- the check on a copy, because the expression may be shared
1327 -- among several choices, some of which might be non-null.
1329 if Present
(Etype
(N
))
1330 and then Is_Array_Type
(Etype
(N
))
1331 and then No
(Next_Index
(Index
))
1333 Expander_Mode_Save_And_Set
(False);
1334 Tcopy
:= New_Copy_Tree
(Expr
);
1335 Set_Parent
(Tcopy
, N
);
1336 Analyze_And_Resolve
(Tcopy
, Component_Type
(Etype
(N
)));
1337 Expander_Mode_Restore
;
1343 -- If loop bounds are the same then generate an assignment
1345 elsif Equal
(L
, H
) then
1346 return Gen_Assign
(New_Copy_Tree
(L
), Expr
);
1348 -- If H - L <= 2 then generate a sequence of assignments when we are
1349 -- processing the bottom most aggregate and it contains scalar
1352 elsif No
(Next_Index
(Index
))
1353 and then Scalar_Comp
1354 and then Local_Compile_Time_Known_Value
(L
)
1355 and then Local_Compile_Time_Known_Value
(H
)
1356 and then Local_Expr_Value
(H
) - Local_Expr_Value
(L
) <= 2
1359 Append_List_To
(S
, Gen_Assign
(New_Copy_Tree
(L
), Expr
));
1360 Append_List_To
(S
, Gen_Assign
(Add
(1, To
=> L
), Expr
));
1362 if Local_Expr_Value
(H
) - Local_Expr_Value
(L
) = 2 then
1363 Append_List_To
(S
, Gen_Assign
(Add
(2, To
=> L
), Expr
));
1369 -- Otherwise construct the loop, starting with the loop index L_J
1371 L_J
:= Make_Temporary
(Loc
, 'J', L
);
1373 -- Construct "L .. H" in Index_Base. We use a qualified expression
1374 -- for the bound to convert to the index base, but we don't need
1375 -- to do that if we already have the base type at hand.
1377 if Etype
(L
) = Index_Base
then
1381 Make_Qualified_Expression
(Loc
,
1382 Subtype_Mark
=> Index_Base_Name
,
1386 if Etype
(H
) = Index_Base
then
1390 Make_Qualified_Expression
(Loc
,
1391 Subtype_Mark
=> Index_Base_Name
,
1400 -- Construct "for L_J in Index_Base range L .. H"
1402 L_Iteration_Scheme
:=
1403 Make_Iteration_Scheme
1405 Loop_Parameter_Specification
=>
1406 Make_Loop_Parameter_Specification
1408 Defining_Identifier
=> L_J
,
1409 Discrete_Subtype_Definition
=> L_Range
));
1411 -- Construct the statements to execute in the loop body
1413 L_Body
:= Gen_Assign
(New_Occurrence_Of
(L_J
, Loc
), Expr
);
1415 -- Construct the final loop
1418 Make_Implicit_Loop_Statement
1420 Identifier
=> Empty
,
1421 Iteration_Scheme
=> L_Iteration_Scheme
,
1422 Statements
=> L_Body
));
1424 -- A small optimization: if the aggregate is initialized with a box
1425 -- and the component type has no initialization procedure, remove the
1426 -- useless empty loop.
1428 if Nkind
(First
(S
)) = N_Loop_Statement
1429 and then Is_Empty_List
(Statements
(First
(S
)))
1431 return New_List
(Make_Null_Statement
(Loc
));
1441 -- The code built is
1443 -- W_J : Index_Base := L;
1444 -- while W_J < H loop
1445 -- W_J := Index_Base'Succ (W);
1449 function Gen_While
(L
, H
: Node_Id
; Expr
: Node_Id
) return List_Id
is
1453 -- W_J : Base_Type := L;
1455 W_Iteration_Scheme
: Node_Id
;
1458 W_Index_Succ
: Node_Id
;
1459 -- Index_Base'Succ (J)
1461 W_Increment
: Node_Id
;
1462 -- W_J := Index_Base'Succ (W)
1464 W_Body
: constant List_Id
:= New_List
;
1465 -- The statements to execute in the loop
1467 S
: constant List_Id
:= New_List
;
1468 -- list of statement
1471 -- If loop bounds define an empty range or are equal return null
1473 if Empty_Range
(L
, H
) or else Equal
(L
, H
) then
1474 Append_To
(S
, Make_Null_Statement
(Loc
));
1478 -- Build the decl of W_J
1480 W_J
:= Make_Temporary
(Loc
, 'J', L
);
1482 Make_Object_Declaration
1484 Defining_Identifier
=> W_J
,
1485 Object_Definition
=> Index_Base_Name
,
1488 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1489 -- that in this particular case L is a fresh Expr generated by
1490 -- Add which we are the only ones to use.
1492 Append_To
(S
, W_Decl
);
1494 -- Construct " while W_J < H"
1496 W_Iteration_Scheme
:=
1497 Make_Iteration_Scheme
1499 Condition
=> Make_Op_Lt
1501 Left_Opnd
=> New_Occurrence_Of
(W_J
, Loc
),
1502 Right_Opnd
=> New_Copy_Tree
(H
)));
1504 -- Construct the statements to execute in the loop body
1507 Make_Attribute_Reference
1509 Prefix
=> Index_Base_Name
,
1510 Attribute_Name
=> Name_Succ
,
1511 Expressions
=> New_List
(New_Occurrence_Of
(W_J
, Loc
)));
1514 Make_OK_Assignment_Statement
1516 Name
=> New_Occurrence_Of
(W_J
, Loc
),
1517 Expression
=> W_Index_Succ
);
1519 Append_To
(W_Body
, W_Increment
);
1520 Append_List_To
(W_Body
,
1521 Gen_Assign
(New_Occurrence_Of
(W_J
, Loc
), Expr
));
1523 -- Construct the final loop
1526 Make_Implicit_Loop_Statement
1528 Identifier
=> Empty
,
1529 Iteration_Scheme
=> W_Iteration_Scheme
,
1530 Statements
=> W_Body
));
1535 --------------------
1536 -- Get_Assoc_Expr --
1537 --------------------
1539 function Get_Assoc_Expr
(Assoc
: Node_Id
) return Node_Id
is
1540 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
1543 if Box_Present
(Assoc
) then
1544 if Is_Scalar_Type
(Ctype
) then
1545 if Present
(Default_Aspect_Component_Value
(Typ
)) then
1546 return Default_Aspect_Component_Value
(Typ
);
1547 elsif Present
(Default_Aspect_Value
(Ctype
)) then
1548 return Default_Aspect_Value
(Ctype
);
1558 return Expression
(Assoc
);
1562 ---------------------
1563 -- Index_Base_Name --
1564 ---------------------
1566 function Index_Base_Name
return Node_Id
is
1568 return New_Occurrence_Of
(Index_Base
, Sloc
(N
));
1569 end Index_Base_Name
;
1571 ------------------------------------
1572 -- Local_Compile_Time_Known_Value --
1573 ------------------------------------
1575 function Local_Compile_Time_Known_Value
(E
: Node_Id
) return Boolean is
1577 return Compile_Time_Known_Value
(E
)
1579 (Nkind
(E
) = N_Attribute_Reference
1580 and then Attribute_Name
(E
) = Name_Val
1581 and then Compile_Time_Known_Value
(First
(Expressions
(E
))));
1582 end Local_Compile_Time_Known_Value
;
1584 ----------------------
1585 -- Local_Expr_Value --
1586 ----------------------
1588 function Local_Expr_Value
(E
: Node_Id
) return Uint
is
1590 if Compile_Time_Known_Value
(E
) then
1591 return Expr_Value
(E
);
1593 return Expr_Value
(First
(Expressions
(E
)));
1595 end Local_Expr_Value
;
1597 -- Build_Array_Aggr_Code Variables
1604 Others_Assoc
: Node_Id
:= Empty
;
1606 Aggr_L
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(N
));
1607 Aggr_H
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(N
));
1608 -- The aggregate bounds of this specific sub-aggregate. Note that if
1609 -- the code generated by Build_Array_Aggr_Code is executed then these
1610 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1612 Aggr_Low
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_L
);
1613 Aggr_High
: constant Node_Id
:= Duplicate_Subexpr_No_Checks
(Aggr_H
);
1614 -- After Duplicate_Subexpr these are side-effect free
1619 Nb_Choices
: Nat
:= 0;
1620 Table
: Case_Table_Type
(1 .. Number_Of_Choices
(N
));
1621 -- Used to sort all the different choice values
1624 -- Number of elements in the positional aggregate
1626 New_Code
: constant List_Id
:= New_List
;
1628 -- Start of processing for Build_Array_Aggr_Code
1631 -- First before we start, a special case. if we have a bit packed
1632 -- array represented as a modular type, then clear the value to
1633 -- zero first, to ensure that unused bits are properly cleared.
1638 and then Is_Bit_Packed_Array
(Typ
)
1639 and then Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
))
1641 Append_To
(New_Code
,
1642 Make_Assignment_Statement
(Loc
,
1643 Name
=> New_Copy_Tree
(Into
),
1645 Unchecked_Convert_To
(Typ
,
1646 Make_Integer_Literal
(Loc
, Uint_0
))));
1649 -- If the component type contains tasks, we need to build a Master
1650 -- entity in the current scope, because it will be needed if build-
1651 -- in-place functions are called in the expanded code.
1653 if Nkind
(Parent
(N
)) = N_Object_Declaration
and then Has_Task
(Typ
) then
1654 Build_Master_Entity
(Defining_Identifier
(Parent
(N
)));
1657 -- STEP 1: Process component associations
1659 -- For those associations that may generate a loop, initialize
1660 -- Loop_Actions to collect inserted actions that may be crated.
1662 -- Skip this if no component associations
1664 if No
(Expressions
(N
)) then
1666 -- STEP 1 (a): Sort the discrete choices
1668 Assoc
:= First
(Component_Associations
(N
));
1669 while Present
(Assoc
) loop
1670 Choice
:= First
(Choices
(Assoc
));
1671 while Present
(Choice
) loop
1672 if Nkind
(Choice
) = N_Others_Choice
then
1673 Set_Loop_Actions
(Assoc
, New_List
);
1674 Others_Assoc
:= Assoc
;
1678 Get_Index_Bounds
(Choice
, Low
, High
);
1681 Set_Loop_Actions
(Assoc
, New_List
);
1684 Nb_Choices
:= Nb_Choices
+ 1;
1686 Table
(Nb_Choices
) :=
1689 Choice_Node
=> Get_Assoc_Expr
(Assoc
));
1697 -- If there is more than one set of choices these must be static
1698 -- and we can therefore sort them. Remember that Nb_Choices does not
1699 -- account for an others choice.
1701 if Nb_Choices
> 1 then
1702 Sort_Case_Table
(Table
);
1705 -- STEP 1 (b): take care of the whole set of discrete choices
1707 for J
in 1 .. Nb_Choices
loop
1708 Low
:= Table
(J
).Choice_Lo
;
1709 High
:= Table
(J
).Choice_Hi
;
1710 Expr
:= Table
(J
).Choice_Node
;
1711 Append_List
(Gen_Loop
(Low
, High
, Expr
), To
=> New_Code
);
1714 -- STEP 1 (c): generate the remaining loops to cover others choice
1715 -- We don't need to generate loops over empty gaps, but if there is
1716 -- a single empty range we must analyze the expression for semantics
1718 if Present
(Others_Assoc
) then
1720 First
: Boolean := True;
1723 for J
in 0 .. Nb_Choices
loop
1727 Low
:= Add
(1, To
=> Table
(J
).Choice_Hi
);
1730 if J
= Nb_Choices
then
1733 High
:= Add
(-1, To
=> Table
(J
+ 1).Choice_Lo
);
1736 -- If this is an expansion within an init proc, make
1737 -- sure that discriminant references are replaced by
1738 -- the corresponding discriminal.
1740 if Inside_Init_Proc
then
1741 if Is_Entity_Name
(Low
)
1742 and then Ekind
(Entity
(Low
)) = E_Discriminant
1744 Set_Entity
(Low
, Discriminal
(Entity
(Low
)));
1747 if Is_Entity_Name
(High
)
1748 and then Ekind
(Entity
(High
)) = E_Discriminant
1750 Set_Entity
(High
, Discriminal
(Entity
(High
)));
1755 or else not Empty_Range
(Low
, High
)
1759 (Gen_Loop
(Low
, High
,
1760 Get_Assoc_Expr
(Others_Assoc
)), To
=> New_Code
);
1766 -- STEP 2: Process positional components
1769 -- STEP 2 (a): Generate the assignments for each positional element
1770 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1771 -- Aggr_L is analyzed and Add wants an analyzed expression.
1773 Expr
:= First
(Expressions
(N
));
1775 while Present
(Expr
) loop
1776 Nb_Elements
:= Nb_Elements
+ 1;
1777 Append_List
(Gen_Assign
(Add
(Nb_Elements
, To
=> Aggr_L
), Expr
),
1782 -- STEP 2 (b): Generate final loop if an others choice is present
1783 -- Here Nb_Elements gives the offset of the last positional element.
1785 if Present
(Component_Associations
(N
)) then
1786 Assoc
:= Last
(Component_Associations
(N
));
1788 -- Ada 2005 (AI-287)
1790 Append_List
(Gen_While
(Add
(Nb_Elements
, To
=> Aggr_L
),
1792 Get_Assoc_Expr
(Assoc
)), -- AI-287
1798 end Build_Array_Aggr_Code
;
1800 ----------------------------
1801 -- Build_Record_Aggr_Code --
1802 ----------------------------
1804 function Build_Record_Aggr_Code
1807 Lhs
: Node_Id
) return List_Id
1809 Loc
: constant Source_Ptr
:= Sloc
(N
);
1810 L
: constant List_Id
:= New_List
;
1811 N_Typ
: constant Entity_Id
:= Etype
(N
);
1817 Comp_Type
: Entity_Id
;
1818 Selector
: Entity_Id
;
1819 Comp_Expr
: Node_Id
;
1822 -- If this is an internal aggregate, the External_Final_List is an
1823 -- expression for the controller record of the enclosing type.
1825 -- If the current aggregate has several controlled components, this
1826 -- expression will appear in several calls to attach to the finali-
1827 -- zation list, and it must not be shared.
1829 Ancestor_Is_Expression
: Boolean := False;
1830 Ancestor_Is_Subtype_Mark
: Boolean := False;
1832 Init_Typ
: Entity_Id
:= Empty
;
1834 Finalization_Done
: Boolean := False;
1835 -- True if Generate_Finalization_Actions has already been called; calls
1836 -- after the first do nothing.
1838 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
;
1839 -- Returns the value that the given discriminant of an ancestor type
1840 -- should receive (in the absence of a conflict with the value provided
1841 -- by an ancestor part of an extension aggregate).
1843 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
);
1844 -- Check that each of the discriminant values defined by the ancestor
1845 -- part of an extension aggregate match the corresponding values
1846 -- provided by either an association of the aggregate or by the
1847 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1849 function Compatible_Int_Bounds
1850 (Agg_Bounds
: Node_Id
;
1851 Typ_Bounds
: Node_Id
) return Boolean;
1852 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1853 -- assumed that both bounds are integer ranges.
1855 procedure Generate_Finalization_Actions
;
1856 -- Deal with the various controlled type data structure initializations
1857 -- (but only if it hasn't been done already).
1859 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
;
1860 -- Returns the first discriminant association in the constraint
1861 -- associated with T, if any, otherwise returns Empty.
1863 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
);
1864 -- If Typ is derived, and constrains discriminants of the parent type,
1865 -- these discriminants are not components of the aggregate, and must be
1866 -- initialized. The assignments are appended to List. The same is done
1867 -- if Typ derives fron an already constrained subtype of a discriminated
1870 function Get_Explicit_Discriminant_Value
(D
: Entity_Id
) return Node_Id
;
1871 -- If the ancestor part is an unconstrained type and further ancestors
1872 -- do not provide discriminants for it, check aggregate components for
1873 -- values of the discriminants.
1875 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean;
1876 -- Check whether Bounds is a range node and its lower and higher bounds
1877 -- are integers literals.
1879 ---------------------------------
1880 -- Ancestor_Discriminant_Value --
1881 ---------------------------------
1883 function Ancestor_Discriminant_Value
(Disc
: Entity_Id
) return Node_Id
is
1885 Assoc_Elmt
: Elmt_Id
;
1886 Aggr_Comp
: Entity_Id
;
1887 Corresp_Disc
: Entity_Id
;
1888 Current_Typ
: Entity_Id
:= Base_Type
(Typ
);
1889 Parent_Typ
: Entity_Id
;
1890 Parent_Disc
: Entity_Id
;
1891 Save_Assoc
: Node_Id
:= Empty
;
1894 -- First check any discriminant associations to see if any of them
1895 -- provide a value for the discriminant.
1897 if Present
(Discriminant_Specifications
(Parent
(Current_Typ
))) then
1898 Assoc
:= First
(Component_Associations
(N
));
1899 while Present
(Assoc
) loop
1900 Aggr_Comp
:= Entity
(First
(Choices
(Assoc
)));
1902 if Ekind
(Aggr_Comp
) = E_Discriminant
then
1903 Save_Assoc
:= Expression
(Assoc
);
1905 Corresp_Disc
:= Corresponding_Discriminant
(Aggr_Comp
);
1906 while Present
(Corresp_Disc
) loop
1908 -- If found a corresponding discriminant then return the
1909 -- value given in the aggregate. (Note: this is not
1910 -- correct in the presence of side effects. ???)
1912 if Disc
= Corresp_Disc
then
1913 return Duplicate_Subexpr
(Expression
(Assoc
));
1916 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
1924 -- No match found in aggregate, so chain up parent types to find
1925 -- a constraint that defines the value of the discriminant.
1927 Parent_Typ
:= Etype
(Current_Typ
);
1928 while Current_Typ
/= Parent_Typ
loop
1929 if Has_Discriminants
(Parent_Typ
)
1930 and then not Has_Unknown_Discriminants
(Parent_Typ
)
1932 Parent_Disc
:= First_Discriminant
(Parent_Typ
);
1934 -- We either get the association from the subtype indication
1935 -- of the type definition itself, or from the discriminant
1936 -- constraint associated with the type entity (which is
1937 -- preferable, but it's not always present ???)
1939 if Is_Empty_Elmt_List
(Discriminant_Constraint
(Current_Typ
))
1941 Assoc
:= Get_Constraint_Association
(Current_Typ
);
1942 Assoc_Elmt
:= No_Elmt
;
1945 First_Elmt
(Discriminant_Constraint
(Current_Typ
));
1946 Assoc
:= Node
(Assoc_Elmt
);
1949 -- Traverse the discriminants of the parent type looking
1950 -- for one that corresponds.
1952 while Present
(Parent_Disc
) and then Present
(Assoc
) loop
1953 Corresp_Disc
:= Parent_Disc
;
1954 while Present
(Corresp_Disc
)
1955 and then Disc
/= Corresp_Disc
1957 Corresp_Disc
:= Corresponding_Discriminant
(Corresp_Disc
);
1960 if Disc
= Corresp_Disc
then
1961 if Nkind
(Assoc
) = N_Discriminant_Association
then
1962 Assoc
:= Expression
(Assoc
);
1965 -- If the located association directly denotes
1966 -- a discriminant, then use the value of a saved
1967 -- association of the aggregate. This is an approach
1968 -- used to handle certain cases involving multiple
1969 -- discriminants mapped to a single discriminant of
1970 -- a descendant. It's not clear how to locate the
1971 -- appropriate discriminant value for such cases. ???
1973 if Is_Entity_Name
(Assoc
)
1974 and then Ekind
(Entity
(Assoc
)) = E_Discriminant
1976 Assoc
:= Save_Assoc
;
1979 return Duplicate_Subexpr
(Assoc
);
1982 Next_Discriminant
(Parent_Disc
);
1984 if No
(Assoc_Elmt
) then
1988 Next_Elmt
(Assoc_Elmt
);
1990 if Present
(Assoc_Elmt
) then
1991 Assoc
:= Node
(Assoc_Elmt
);
1999 Current_Typ
:= Parent_Typ
;
2000 Parent_Typ
:= Etype
(Current_Typ
);
2003 -- In some cases there's no ancestor value to locate (such as
2004 -- when an ancestor part given by an expression defines the
2005 -- discriminant value).
2008 end Ancestor_Discriminant_Value
;
2010 ----------------------------------
2011 -- Check_Ancestor_Discriminants --
2012 ----------------------------------
2014 procedure Check_Ancestor_Discriminants
(Anc_Typ
: Entity_Id
) is
2016 Disc_Value
: Node_Id
;
2020 Discr
:= First_Discriminant
(Base_Type
(Anc_Typ
));
2021 while Present
(Discr
) loop
2022 Disc_Value
:= Ancestor_Discriminant_Value
(Discr
);
2024 if Present
(Disc_Value
) then
2025 Cond
:= Make_Op_Ne
(Loc
,
2027 Make_Selected_Component
(Loc
,
2028 Prefix
=> New_Copy_Tree
(Target
),
2029 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2030 Right_Opnd
=> Disc_Value
);
2033 Make_Raise_Constraint_Error
(Loc
,
2035 Reason
=> CE_Discriminant_Check_Failed
));
2038 Next_Discriminant
(Discr
);
2040 end Check_Ancestor_Discriminants
;
2042 ---------------------------
2043 -- Compatible_Int_Bounds --
2044 ---------------------------
2046 function Compatible_Int_Bounds
2047 (Agg_Bounds
: Node_Id
;
2048 Typ_Bounds
: Node_Id
) return Boolean
2050 Agg_Lo
: constant Uint
:= Intval
(Low_Bound
(Agg_Bounds
));
2051 Agg_Hi
: constant Uint
:= Intval
(High_Bound
(Agg_Bounds
));
2052 Typ_Lo
: constant Uint
:= Intval
(Low_Bound
(Typ_Bounds
));
2053 Typ_Hi
: constant Uint
:= Intval
(High_Bound
(Typ_Bounds
));
2055 return Typ_Lo
<= Agg_Lo
and then Agg_Hi
<= Typ_Hi
;
2056 end Compatible_Int_Bounds
;
2058 --------------------------------
2059 -- Get_Constraint_Association --
2060 --------------------------------
2062 function Get_Constraint_Association
(T
: Entity_Id
) return Node_Id
is
2069 -- If type is private, get constraint from full view. This was
2070 -- previously done in an instance context, but is needed whenever
2071 -- the ancestor part has a discriminant, possibly inherited through
2072 -- multiple derivations.
2074 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
2075 Typ
:= Full_View
(Typ
);
2078 Indic
:= Subtype_Indication
(Type_Definition
(Parent
(Typ
)));
2080 -- Verify that the subtype indication carries a constraint
2082 if Nkind
(Indic
) = N_Subtype_Indication
2083 and then Present
(Constraint
(Indic
))
2085 return First
(Constraints
(Constraint
(Indic
)));
2089 end Get_Constraint_Association
;
2091 -------------------------------------
2092 -- Get_Explicit_Discriminant_Value --
2093 -------------------------------------
2095 function Get_Explicit_Discriminant_Value
2096 (D
: Entity_Id
) return Node_Id
2103 -- The aggregate has been normalized and all associations have a
2106 Assoc
:= First
(Component_Associations
(N
));
2107 while Present
(Assoc
) loop
2108 Choice
:= First
(Choices
(Assoc
));
2110 if Chars
(Choice
) = Chars
(D
) then
2111 Val
:= Expression
(Assoc
);
2120 end Get_Explicit_Discriminant_Value
;
2122 -------------------------------
2123 -- Init_Hidden_Discriminants --
2124 -------------------------------
2126 procedure Init_Hidden_Discriminants
(Typ
: Entity_Id
; List
: List_Id
) is
2127 function Is_Completely_Hidden_Discriminant
2128 (Discr
: Entity_Id
) return Boolean;
2129 -- Determine whether Discr is a completely hidden discriminant of
2132 ---------------------------------------
2133 -- Is_Completely_Hidden_Discriminant --
2134 ---------------------------------------
2136 function Is_Completely_Hidden_Discriminant
2137 (Discr
: Entity_Id
) return Boolean
2142 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2143 -- completely hidden discriminants.
2145 Item
:= First_Entity
(Typ
);
2146 while Present
(Item
) loop
2147 if Ekind
(Item
) = E_Discriminant
2148 and then Is_Completely_Hidden
(Item
)
2149 and then Chars
(Original_Record_Component
(Item
)) =
2159 end Is_Completely_Hidden_Discriminant
;
2163 Base_Typ
: Entity_Id
;
2165 Discr_Constr
: Elmt_Id
;
2166 Discr_Init
: Node_Id
;
2167 Discr_Val
: Node_Id
;
2168 In_Aggr_Type
: Boolean;
2169 Par_Typ
: Entity_Id
;
2171 -- Start of processing for Init_Hidden_Discriminants
2174 -- The constraints on the hidden discriminants, if present, are kept
2175 -- in the Stored_Constraint list of the type itself, or in that of
2176 -- the base type. If not in the constraints of the aggregate itself,
2177 -- we examine ancestors to find discriminants that are not renamed
2178 -- by other discriminants but constrained explicitly.
2180 In_Aggr_Type
:= True;
2182 Base_Typ
:= Base_Type
(Typ
);
2183 while Is_Derived_Type
(Base_Typ
)
2185 (Present
(Stored_Constraint
(Base_Typ
))
2187 (In_Aggr_Type
and then Present
(Stored_Constraint
(Typ
))))
2189 Par_Typ
:= Etype
(Base_Typ
);
2191 if not Has_Discriminants
(Par_Typ
) then
2195 Discr
:= First_Discriminant
(Par_Typ
);
2197 -- We know that one of the stored-constraint lists is present
2199 if Present
(Stored_Constraint
(Base_Typ
)) then
2200 Discr_Constr
:= First_Elmt
(Stored_Constraint
(Base_Typ
));
2202 -- For private extension, stored constraint may be on full view
2204 elsif Is_Private_Type
(Base_Typ
)
2205 and then Present
(Full_View
(Base_Typ
))
2206 and then Present
(Stored_Constraint
(Full_View
(Base_Typ
)))
2209 First_Elmt
(Stored_Constraint
(Full_View
(Base_Typ
)));
2212 Discr_Constr
:= First_Elmt
(Stored_Constraint
(Typ
));
2215 while Present
(Discr
) and then Present
(Discr_Constr
) loop
2216 Discr_Val
:= Node
(Discr_Constr
);
2218 -- The parent discriminant is renamed in the derived type,
2219 -- nothing to initialize.
2221 -- type Deriv_Typ (Discr : ...)
2222 -- is new Parent_Typ (Discr => Discr);
2224 if Is_Entity_Name
(Discr_Val
)
2225 and then Ekind
(Entity
(Discr_Val
)) = E_Discriminant
2229 -- When the parent discriminant is constrained at the type
2230 -- extension level, it does not appear in the derived type.
2232 -- type Deriv_Typ (Discr : ...)
2233 -- is new Parent_Typ (Discr => Discr,
2234 -- Hidden_Discr => Expression);
2236 elsif Is_Completely_Hidden_Discriminant
(Discr
) then
2239 -- Otherwise initialize the discriminant
2243 Make_OK_Assignment_Statement
(Loc
,
2245 Make_Selected_Component
(Loc
,
2246 Prefix
=> New_Copy_Tree
(Target
),
2247 Selector_Name
=> New_Occurrence_Of
(Discr
, Loc
)),
2248 Expression
=> New_Copy_Tree
(Discr_Val
));
2250 Set_No_Ctrl_Actions
(Discr_Init
);
2251 Append_To
(List
, Discr_Init
);
2254 Next_Elmt
(Discr_Constr
);
2255 Next_Discriminant
(Discr
);
2258 In_Aggr_Type
:= False;
2259 Base_Typ
:= Base_Type
(Par_Typ
);
2261 end Init_Hidden_Discriminants
;
2263 -------------------------
2264 -- Is_Int_Range_Bounds --
2265 -------------------------
2267 function Is_Int_Range_Bounds
(Bounds
: Node_Id
) return Boolean is
2269 return Nkind
(Bounds
) = N_Range
2270 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
2271 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
;
2272 end Is_Int_Range_Bounds
;
2274 -----------------------------------
2275 -- Generate_Finalization_Actions --
2276 -----------------------------------
2278 procedure Generate_Finalization_Actions
is
2280 -- Do the work only the first time this is called
2282 if Finalization_Done
then
2286 Finalization_Done
:= True;
2288 -- Determine the external finalization list. It is either the
2289 -- finalization list of the outer-scope or the one coming from an
2290 -- outer aggregate. When the target is not a temporary, the proper
2291 -- scope is the scope of the target rather than the potentially
2292 -- transient current scope.
2294 if Is_Controlled
(Typ
) and then Ancestor_Is_Subtype_Mark
then
2295 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2296 Set_Assignment_OK
(Ref
);
2299 Make_Procedure_Call_Statement
(Loc
,
2302 (Find_Prim_Op
(Init_Typ
, Name_Initialize
), Loc
),
2303 Parameter_Associations
=> New_List
(New_Copy_Tree
(Ref
))));
2305 end Generate_Finalization_Actions
;
2307 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
;
2308 -- If default expression of a component mentions a discriminant of the
2309 -- type, it must be rewritten as the discriminant of the target object.
2311 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
;
2312 -- If the aggregate contains a self-reference, traverse each expression
2313 -- to replace a possible self-reference with a reference to the proper
2314 -- component of the target of the assignment.
2316 --------------------------
2317 -- Rewrite_Discriminant --
2318 --------------------------
2320 function Rewrite_Discriminant
(Expr
: Node_Id
) return Traverse_Result
is
2322 if Is_Entity_Name
(Expr
)
2323 and then Present
(Entity
(Expr
))
2324 and then Ekind
(Entity
(Expr
)) = E_In_Parameter
2325 and then Present
(Discriminal_Link
(Entity
(Expr
)))
2326 and then Scope
(Discriminal_Link
(Entity
(Expr
))) =
2327 Base_Type
(Etype
(N
))
2330 Make_Selected_Component
(Loc
,
2331 Prefix
=> New_Copy_Tree
(Lhs
),
2332 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Expr
))));
2336 end Rewrite_Discriminant
;
2342 function Replace_Type
(Expr
: Node_Id
) return Traverse_Result
is
2344 -- Note regarding the Root_Type test below: Aggregate components for
2345 -- self-referential types include attribute references to the current
2346 -- instance, of the form: Typ'access, etc.. These references are
2347 -- rewritten as references to the target of the aggregate: the
2348 -- left-hand side of an assignment, the entity in a declaration,
2349 -- or a temporary. Without this test, we would improperly extended
2350 -- this rewriting to attribute references whose prefix was not the
2351 -- type of the aggregate.
2353 if Nkind
(Expr
) = N_Attribute_Reference
2354 and then Is_Entity_Name
(Prefix
(Expr
))
2355 and then Is_Type
(Entity
(Prefix
(Expr
)))
2356 and then Root_Type
(Etype
(N
)) = Root_Type
(Entity
(Prefix
(Expr
)))
2358 if Is_Entity_Name
(Lhs
) then
2359 Rewrite
(Prefix
(Expr
),
2360 New_Occurrence_Of
(Entity
(Lhs
), Loc
));
2362 elsif Nkind
(Lhs
) = N_Selected_Component
then
2364 Make_Attribute_Reference
(Loc
,
2365 Attribute_Name
=> Name_Unrestricted_Access
,
2366 Prefix
=> New_Copy_Tree
(Lhs
)));
2367 Set_Analyzed
(Parent
(Expr
), False);
2371 Make_Attribute_Reference
(Loc
,
2372 Attribute_Name
=> Name_Unrestricted_Access
,
2373 Prefix
=> New_Copy_Tree
(Lhs
)));
2374 Set_Analyzed
(Parent
(Expr
), False);
2381 procedure Replace_Self_Reference
is
2382 new Traverse_Proc
(Replace_Type
);
2384 procedure Replace_Discriminants
is
2385 new Traverse_Proc
(Rewrite_Discriminant
);
2387 -- Start of processing for Build_Record_Aggr_Code
2390 if Has_Self_Reference
(N
) then
2391 Replace_Self_Reference
(N
);
2394 -- If the target of the aggregate is class-wide, we must convert it
2395 -- to the actual type of the aggregate, so that the proper components
2396 -- are visible. We know already that the types are compatible.
2398 if Present
(Etype
(Lhs
))
2399 and then Is_Class_Wide_Type
(Etype
(Lhs
))
2401 Target
:= Unchecked_Convert_To
(Typ
, Lhs
);
2406 -- Deal with the ancestor part of extension aggregates or with the
2407 -- discriminants of the root type.
2409 if Nkind
(N
) = N_Extension_Aggregate
then
2411 Ancestor
: constant Node_Id
:= Ancestor_Part
(N
);
2415 -- If the ancestor part is a subtype mark "T", we generate
2417 -- init-proc (T (tmp)); if T is constrained and
2418 -- init-proc (S (tmp)); where S applies an appropriate
2419 -- constraint if T is unconstrained
2421 if Is_Entity_Name
(Ancestor
)
2422 and then Is_Type
(Entity
(Ancestor
))
2424 Ancestor_Is_Subtype_Mark
:= True;
2426 if Is_Constrained
(Entity
(Ancestor
)) then
2427 Init_Typ
:= Entity
(Ancestor
);
2429 -- For an ancestor part given by an unconstrained type mark,
2430 -- create a subtype constrained by appropriate corresponding
2431 -- discriminant values coming from either associations of the
2432 -- aggregate or a constraint on a parent type. The subtype will
2433 -- be used to generate the correct default value for the
2436 elsif Has_Discriminants
(Entity
(Ancestor
)) then
2438 Anc_Typ
: constant Entity_Id
:= Entity
(Ancestor
);
2439 Anc_Constr
: constant List_Id
:= New_List
;
2440 Discrim
: Entity_Id
;
2441 Disc_Value
: Node_Id
;
2442 New_Indic
: Node_Id
;
2443 Subt_Decl
: Node_Id
;
2446 Discrim
:= First_Discriminant
(Anc_Typ
);
2447 while Present
(Discrim
) loop
2448 Disc_Value
:= Ancestor_Discriminant_Value
(Discrim
);
2450 -- If no usable discriminant in ancestors, check
2451 -- whether aggregate has an explicit value for it.
2453 if No
(Disc_Value
) then
2455 Get_Explicit_Discriminant_Value
(Discrim
);
2458 Append_To
(Anc_Constr
, Disc_Value
);
2459 Next_Discriminant
(Discrim
);
2463 Make_Subtype_Indication
(Loc
,
2464 Subtype_Mark
=> New_Occurrence_Of
(Anc_Typ
, Loc
),
2466 Make_Index_Or_Discriminant_Constraint
(Loc
,
2467 Constraints
=> Anc_Constr
));
2469 Init_Typ
:= Create_Itype
(Ekind
(Anc_Typ
), N
);
2472 Make_Subtype_Declaration
(Loc
,
2473 Defining_Identifier
=> Init_Typ
,
2474 Subtype_Indication
=> New_Indic
);
2476 -- Itypes must be analyzed with checks off Declaration
2477 -- must have a parent for proper handling of subsidiary
2480 Set_Parent
(Subt_Decl
, N
);
2481 Analyze
(Subt_Decl
, Suppress
=> All_Checks
);
2485 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2486 Set_Assignment_OK
(Ref
);
2488 if not Is_Interface
(Init_Typ
) then
2490 Build_Initialization_Call
(Loc
,
2493 In_Init_Proc
=> Within_Init_Proc
,
2494 With_Default_Init
=> Has_Default_Init_Comps
(N
)
2496 Has_Task
(Base_Type
(Init_Typ
))));
2498 if Is_Constrained
(Entity
(Ancestor
))
2499 and then Has_Discriminants
(Entity
(Ancestor
))
2501 Check_Ancestor_Discriminants
(Entity
(Ancestor
));
2505 -- Handle calls to C++ constructors
2507 elsif Is_CPP_Constructor_Call
(Ancestor
) then
2508 Init_Typ
:= Etype
(Ancestor
);
2509 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2510 Set_Assignment_OK
(Ref
);
2513 Build_Initialization_Call
(Loc
,
2516 In_Init_Proc
=> Within_Init_Proc
,
2517 With_Default_Init
=> Has_Default_Init_Comps
(N
),
2518 Constructor_Ref
=> Ancestor
));
2520 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2521 -- limited type, a recursive call expands the ancestor. Note that
2522 -- in the limited case, the ancestor part must be either a
2523 -- function call (possibly qualified, or wrapped in an unchecked
2524 -- conversion) or aggregate (definitely qualified).
2526 -- The ancestor part can also be a function call (that may be
2527 -- transformed into an explicit dereference) or a qualification
2530 elsif Is_Limited_Type
(Etype
(Ancestor
))
2531 and then Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
2532 N_Extension_Aggregate
)
2534 Ancestor_Is_Expression
:= True;
2536 -- Set up finalization data for enclosing record, because
2537 -- controlled subcomponents of the ancestor part will be
2540 Generate_Finalization_Actions
;
2543 Build_Record_Aggr_Code
2544 (N
=> Unqualify
(Ancestor
),
2545 Typ
=> Etype
(Unqualify
(Ancestor
)),
2548 -- If the ancestor part is an expression "E", we generate
2552 -- In Ada 2005, this includes the case of a (possibly qualified)
2553 -- limited function call. The assignment will turn into a
2554 -- build-in-place function call (for further details, see
2555 -- Make_Build_In_Place_Call_In_Assignment).
2558 Ancestor_Is_Expression
:= True;
2559 Init_Typ
:= Etype
(Ancestor
);
2561 -- If the ancestor part is an aggregate, force its full
2562 -- expansion, which was delayed.
2564 if Nkind_In
(Unqualify
(Ancestor
), N_Aggregate
,
2565 N_Extension_Aggregate
)
2567 Set_Analyzed
(Ancestor
, False);
2568 Set_Analyzed
(Expression
(Ancestor
), False);
2571 Ref
:= Convert_To
(Init_Typ
, New_Copy_Tree
(Target
));
2572 Set_Assignment_OK
(Ref
);
2574 -- Make the assignment without usual controlled actions, since
2575 -- we only want to Adjust afterwards, but not to Finalize
2576 -- beforehand. Add manual Adjust when necessary.
2578 Assign
:= New_List
(
2579 Make_OK_Assignment_Statement
(Loc
,
2581 Expression
=> Ancestor
));
2582 Set_No_Ctrl_Actions
(First
(Assign
));
2584 -- Assign the tag now to make sure that the dispatching call in
2585 -- the subsequent deep_adjust works properly (unless
2586 -- Tagged_Type_Expansion where tags are implicit).
2588 if Tagged_Type_Expansion
then
2590 Make_OK_Assignment_Statement
(Loc
,
2592 Make_Selected_Component
(Loc
,
2593 Prefix
=> New_Copy_Tree
(Target
),
2596 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
2599 Unchecked_Convert_To
(RTE
(RE_Tag
),
2602 (Access_Disp_Table
(Base_Type
(Typ
)))),
2605 Set_Assignment_OK
(Name
(Instr
));
2606 Append_To
(Assign
, Instr
);
2608 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2609 -- also initialize tags of the secondary dispatch tables.
2611 if Has_Interfaces
(Base_Type
(Typ
)) then
2613 (Typ
=> Base_Type
(Typ
),
2615 Stmts_List
=> Assign
);
2619 -- Call Adjust manually
2621 if Needs_Finalization
(Etype
(Ancestor
))
2622 and then not Is_Limited_Type
(Etype
(Ancestor
))
2626 (Obj_Ref
=> New_Copy_Tree
(Ref
),
2627 Typ
=> Etype
(Ancestor
)));
2631 Make_Unsuppress_Block
(Loc
, Name_Discriminant_Check
, Assign
));
2633 if Has_Discriminants
(Init_Typ
) then
2634 Check_Ancestor_Discriminants
(Init_Typ
);
2639 -- Generate assignments of hidden discriminants. If the base type is
2640 -- an unchecked union, the discriminants are unknown to the back-end
2641 -- and absent from a value of the type, so assignments for them are
2644 if Has_Discriminants
(Typ
)
2645 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
2647 Init_Hidden_Discriminants
(Typ
, L
);
2650 -- Normal case (not an extension aggregate)
2653 -- Generate the discriminant expressions, component by component.
2654 -- If the base type is an unchecked union, the discriminants are
2655 -- unknown to the back-end and absent from a value of the type, so
2656 -- assignments for them are not emitted.
2658 if Has_Discriminants
(Typ
)
2659 and then not Is_Unchecked_Union
(Base_Type
(Typ
))
2661 Init_Hidden_Discriminants
(Typ
, L
);
2663 -- Generate discriminant init values for the visible discriminants
2666 Discriminant
: Entity_Id
;
2667 Discriminant_Value
: Node_Id
;
2670 Discriminant
:= First_Stored_Discriminant
(Typ
);
2671 while Present
(Discriminant
) loop
2673 Make_Selected_Component
(Loc
,
2674 Prefix
=> New_Copy_Tree
(Target
),
2675 Selector_Name
=> New_Occurrence_Of
(Discriminant
, Loc
));
2677 Discriminant_Value
:=
2678 Get_Discriminant_Value
2681 Discriminant_Constraint
(N_Typ
));
2684 Make_OK_Assignment_Statement
(Loc
,
2686 Expression
=> New_Copy_Tree
(Discriminant_Value
));
2688 Set_No_Ctrl_Actions
(Instr
);
2689 Append_To
(L
, Instr
);
2691 Next_Stored_Discriminant
(Discriminant
);
2697 -- For CPP types we generate an implicit call to the C++ default
2698 -- constructor to ensure the proper initialization of the _Tag
2701 if Is_CPP_Class
(Root_Type
(Typ
)) and then CPP_Num_Prims
(Typ
) > 0 then
2702 Invoke_Constructor
: declare
2703 CPP_Parent
: constant Entity_Id
:= Enclosing_CPP_Parent
(Typ
);
2705 procedure Invoke_IC_Proc
(T
: Entity_Id
);
2706 -- Recursive routine used to climb to parents. Required because
2707 -- parents must be initialized before descendants to ensure
2708 -- propagation of inherited C++ slots.
2710 --------------------
2711 -- Invoke_IC_Proc --
2712 --------------------
2714 procedure Invoke_IC_Proc
(T
: Entity_Id
) is
2716 -- Avoid generating extra calls. Initialization required
2717 -- only for types defined from the level of derivation of
2718 -- type of the constructor and the type of the aggregate.
2720 if T
= CPP_Parent
then
2724 Invoke_IC_Proc
(Etype
(T
));
2726 -- Generate call to the IC routine
2728 if Present
(CPP_Init_Proc
(T
)) then
2730 Make_Procedure_Call_Statement
(Loc
,
2731 Name
=> New_Occurrence_Of
(CPP_Init_Proc
(T
), Loc
)));
2735 -- Start of processing for Invoke_Constructor
2738 -- Implicit invocation of the C++ constructor
2740 if Nkind
(N
) = N_Aggregate
then
2742 Make_Procedure_Call_Statement
(Loc
,
2744 New_Occurrence_Of
(Base_Init_Proc
(CPP_Parent
), Loc
),
2745 Parameter_Associations
=> New_List
(
2746 Unchecked_Convert_To
(CPP_Parent
,
2747 New_Copy_Tree
(Lhs
)))));
2750 Invoke_IC_Proc
(Typ
);
2751 end Invoke_Constructor
;
2754 -- Generate the assignments, component by component
2756 -- tmp.comp1 := Expr1_From_Aggr;
2757 -- tmp.comp2 := Expr2_From_Aggr;
2760 Comp
:= First
(Component_Associations
(N
));
2761 while Present
(Comp
) loop
2762 Selector
:= Entity
(First
(Choices
(Comp
)));
2766 if Is_CPP_Constructor_Call
(Expression
(Comp
)) then
2768 Build_Initialization_Call
(Loc
,
2770 Make_Selected_Component
(Loc
,
2771 Prefix
=> New_Copy_Tree
(Target
),
2772 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
)),
2773 Typ
=> Etype
(Selector
),
2775 With_Default_Init
=> True,
2776 Constructor_Ref
=> Expression
(Comp
)));
2778 -- Ada 2005 (AI-287): For each default-initialized component generate
2779 -- a call to the corresponding IP subprogram if available.
2781 elsif Box_Present
(Comp
)
2782 and then Has_Non_Null_Base_Init_Proc
(Etype
(Selector
))
2784 if Ekind
(Selector
) /= E_Discriminant
then
2785 Generate_Finalization_Actions
;
2788 -- Ada 2005 (AI-287): If the component type has tasks then
2789 -- generate the activation chain and master entities (except
2790 -- in case of an allocator because in that case these entities
2791 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2794 Ctype
: constant Entity_Id
:= Etype
(Selector
);
2795 Inside_Allocator
: Boolean := False;
2796 P
: Node_Id
:= Parent
(N
);
2799 if Is_Task_Type
(Ctype
) or else Has_Task
(Ctype
) then
2800 while Present
(P
) loop
2801 if Nkind
(P
) = N_Allocator
then
2802 Inside_Allocator
:= True;
2809 if not Inside_Init_Proc
and not Inside_Allocator
then
2810 Build_Activation_Chain_Entity
(N
);
2816 Build_Initialization_Call
(Loc
,
2817 Id_Ref
=> Make_Selected_Component
(Loc
,
2818 Prefix
=> New_Copy_Tree
(Target
),
2820 New_Occurrence_Of
(Selector
, Loc
)),
2821 Typ
=> Etype
(Selector
),
2823 With_Default_Init
=> True));
2825 -- Prepare for component assignment
2827 elsif Ekind
(Selector
) /= E_Discriminant
2828 or else Nkind
(N
) = N_Extension_Aggregate
2830 -- All the discriminants have now been assigned
2832 -- This is now a good moment to initialize and attach all the
2833 -- controllers. Their position may depend on the discriminants.
2835 if Ekind
(Selector
) /= E_Discriminant
then
2836 Generate_Finalization_Actions
;
2839 Comp_Type
:= Underlying_Type
(Etype
(Selector
));
2841 Make_Selected_Component
(Loc
,
2842 Prefix
=> New_Copy_Tree
(Target
),
2843 Selector_Name
=> New_Occurrence_Of
(Selector
, Loc
));
2845 if Nkind
(Expression
(Comp
)) = N_Qualified_Expression
then
2846 Expr_Q
:= Expression
(Expression
(Comp
));
2848 Expr_Q
:= Expression
(Comp
);
2851 -- Now either create the assignment or generate the code for the
2852 -- inner aggregate top-down.
2854 if Is_Delayed_Aggregate
(Expr_Q
) then
2856 -- We have the following case of aggregate nesting inside
2857 -- an object declaration:
2859 -- type Arr_Typ is array (Integer range <>) of ...;
2861 -- type Rec_Typ (...) is record
2862 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2865 -- Obj_Rec_Typ : Rec_Typ := (...,
2866 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2868 -- The length of the ranges of the aggregate and Obj_Add_Typ
2869 -- are equal (B - A = Y - X), but they do not coincide (X /=
2870 -- A and B /= Y). This case requires array sliding which is
2871 -- performed in the following manner:
2873 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2875 -- Temp (X) := (...);
2877 -- Temp (Y) := (...);
2878 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2880 if Ekind
(Comp_Type
) = E_Array_Subtype
2881 and then Is_Int_Range_Bounds
(Aggregate_Bounds
(Expr_Q
))
2882 and then Is_Int_Range_Bounds
(First_Index
(Comp_Type
))
2884 Compatible_Int_Bounds
2885 (Agg_Bounds
=> Aggregate_Bounds
(Expr_Q
),
2886 Typ_Bounds
=> First_Index
(Comp_Type
))
2888 -- Create the array subtype with bounds equal to those of
2889 -- the corresponding aggregate.
2892 SubE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
2894 SubD
: constant Node_Id
:=
2895 Make_Subtype_Declaration
(Loc
,
2896 Defining_Identifier
=> SubE
,
2897 Subtype_Indication
=>
2898 Make_Subtype_Indication
(Loc
,
2900 New_Occurrence_Of
(Etype
(Comp_Type
), Loc
),
2902 Make_Index_Or_Discriminant_Constraint
2904 Constraints
=> New_List
(
2906 (Aggregate_Bounds
(Expr_Q
))))));
2908 -- Create a temporary array of the above subtype which
2909 -- will be used to capture the aggregate assignments.
2911 TmpE
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A', N
);
2913 TmpD
: constant Node_Id
:=
2914 Make_Object_Declaration
(Loc
,
2915 Defining_Identifier
=> TmpE
,
2916 Object_Definition
=> New_Occurrence_Of
(SubE
, Loc
));
2919 Set_No_Initialization
(TmpD
);
2920 Append_To
(L
, SubD
);
2921 Append_To
(L
, TmpD
);
2923 -- Expand aggregate into assignments to the temp array
2926 Late_Expansion
(Expr_Q
, Comp_Type
,
2927 New_Occurrence_Of
(TmpE
, Loc
)));
2932 Make_Assignment_Statement
(Loc
,
2933 Name
=> New_Copy_Tree
(Comp_Expr
),
2934 Expression
=> New_Occurrence_Of
(TmpE
, Loc
)));
2937 -- Normal case (sliding not required)
2941 Late_Expansion
(Expr_Q
, Comp_Type
, Comp_Expr
));
2944 -- Expr_Q is not delayed aggregate
2947 if Has_Discriminants
(Typ
) then
2948 Replace_Discriminants
(Expr_Q
);
2950 -- If the component is an array type that depends on
2951 -- discriminants, and the expression is a single Others
2952 -- clause, create an explicit subtype for it because the
2953 -- backend has troubles recovering the actual bounds.
2955 if Nkind
(Expr_Q
) = N_Aggregate
2956 and then Is_Array_Type
(Comp_Type
)
2957 and then Present
(Component_Associations
(Expr_Q
))
2960 Assoc
: constant Node_Id
:=
2961 First
(Component_Associations
(Expr_Q
));
2965 if Nkind
(First
(Choices
(Assoc
))) = N_Others_Choice
2968 Build_Actual_Subtype_Of_Component
2969 (Comp_Type
, Comp_Expr
);
2971 -- If the component type does not in fact depend on
2972 -- discriminants, the subtype declaration is empty.
2974 if Present
(Decl
) then
2975 Append_To
(L
, Decl
);
2976 Set_Etype
(Comp_Expr
, Defining_Entity
(Decl
));
2984 and then Nkind
(Expr_Q
) = N_Aggregate
2985 and then Is_Array_Type
(Etype
(Expr_Q
))
2986 and then Present
(First_Index
(Etype
(Expr_Q
)))
2989 Expr_Q_Type
: constant Node_Id
:= Etype
(Expr_Q
);
2992 Build_Array_Aggr_Code
2994 Ctype
=> Component_Type
(Expr_Q_Type
),
2995 Index
=> First_Index
(Expr_Q_Type
),
2997 Scalar_Comp
=> Is_Scalar_Type
2998 (Component_Type
(Expr_Q_Type
))));
3003 Make_OK_Assignment_Statement
(Loc
,
3005 Expression
=> Expr_Q
);
3007 Set_No_Ctrl_Actions
(Instr
);
3008 Append_To
(L
, Instr
);
3011 -- Adjust the tag if tagged (because of possible view
3012 -- conversions), unless compiling for a VM where tags are
3015 -- tmp.comp._tag := comp_typ'tag;
3017 if Is_Tagged_Type
(Comp_Type
)
3018 and then Tagged_Type_Expansion
3021 Make_OK_Assignment_Statement
(Loc
,
3023 Make_Selected_Component
(Loc
,
3024 Prefix
=> New_Copy_Tree
(Comp_Expr
),
3027 (First_Tag_Component
(Comp_Type
), Loc
)),
3030 Unchecked_Convert_To
(RTE
(RE_Tag
),
3032 (Node
(First_Elmt
(Access_Disp_Table
(Comp_Type
))),
3035 Append_To
(L
, Instr
);
3039 -- Adjust (tmp.comp);
3041 if Needs_Finalization
(Comp_Type
)
3042 and then not Is_Limited_Type
(Comp_Type
)
3046 (Obj_Ref
=> New_Copy_Tree
(Comp_Expr
),
3051 -- comment would be good here ???
3053 elsif Ekind
(Selector
) = E_Discriminant
3054 and then Nkind
(N
) /= N_Extension_Aggregate
3055 and then Nkind
(Parent
(N
)) = N_Component_Association
3056 and then Is_Constrained
(Typ
)
3058 -- We must check that the discriminant value imposed by the
3059 -- context is the same as the value given in the subaggregate,
3060 -- because after the expansion into assignments there is no
3061 -- record on which to perform a regular discriminant check.
3068 D_Val
:= First_Elmt
(Discriminant_Constraint
(Typ
));
3069 Disc
:= First_Discriminant
(Typ
);
3070 while Chars
(Disc
) /= Chars
(Selector
) loop
3071 Next_Discriminant
(Disc
);
3075 pragma Assert
(Present
(D_Val
));
3077 -- This check cannot performed for components that are
3078 -- constrained by a current instance, because this is not a
3079 -- value that can be compared with the actual constraint.
3081 if Nkind
(Node
(D_Val
)) /= N_Attribute_Reference
3082 or else not Is_Entity_Name
(Prefix
(Node
(D_Val
)))
3083 or else not Is_Type
(Entity
(Prefix
(Node
(D_Val
))))
3086 Make_Raise_Constraint_Error
(Loc
,
3089 Left_Opnd
=> New_Copy_Tree
(Node
(D_Val
)),
3090 Right_Opnd
=> Expression
(Comp
)),
3091 Reason
=> CE_Discriminant_Check_Failed
));
3094 -- Find self-reference in previous discriminant assignment,
3095 -- and replace with proper expression.
3102 while Present
(Ass
) loop
3103 if Nkind
(Ass
) = N_Assignment_Statement
3104 and then Nkind
(Name
(Ass
)) = N_Selected_Component
3105 and then Chars
(Selector_Name
(Name
(Ass
))) =
3109 (Ass
, New_Copy_Tree
(Expression
(Comp
)));
3122 -- If the type is tagged, the tag needs to be initialized (unless we
3123 -- are in VM-mode where tags are implicit). It is done late in the
3124 -- initialization process because in some cases, we call the init
3125 -- proc of an ancestor which will not leave out the right tag.
3127 if Ancestor_Is_Expression
then
3130 -- For CPP types we generated a call to the C++ default constructor
3131 -- before the components have been initialized to ensure the proper
3132 -- initialization of the _Tag component (see above).
3134 elsif Is_CPP_Class
(Typ
) then
3137 elsif Is_Tagged_Type
(Typ
) and then Tagged_Type_Expansion
then
3139 Make_OK_Assignment_Statement
(Loc
,
3141 Make_Selected_Component
(Loc
,
3142 Prefix
=> New_Copy_Tree
(Target
),
3145 (First_Tag_Component
(Base_Type
(Typ
)), Loc
)),
3148 Unchecked_Convert_To
(RTE
(RE_Tag
),
3150 (Node
(First_Elmt
(Access_Disp_Table
(Base_Type
(Typ
)))),
3153 Append_To
(L
, Instr
);
3155 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3156 -- abstract interfaces we must also initialize the tags of the
3157 -- secondary dispatch tables.
3159 if Has_Interfaces
(Base_Type
(Typ
)) then
3161 (Typ
=> Base_Type
(Typ
),
3167 -- If the controllers have not been initialized yet (by lack of non-
3168 -- discriminant components), let's do it now.
3170 Generate_Finalization_Actions
;
3173 end Build_Record_Aggr_Code
;
3175 ---------------------------------------
3176 -- Collect_Initialization_Statements --
3177 ---------------------------------------
3179 procedure Collect_Initialization_Statements
3182 Node_After
: Node_Id
)
3184 Loc
: constant Source_Ptr
:= Sloc
(N
);
3185 Init_Actions
: constant List_Id
:= New_List
;
3186 Init_Node
: Node_Id
;
3187 Comp_Stmt
: Node_Id
;
3190 -- Nothing to do if Obj is already frozen, as in this case we known we
3191 -- won't need to move the initialization statements about later on.
3193 if Is_Frozen
(Obj
) then
3198 while Next
(Init_Node
) /= Node_After
loop
3199 Append_To
(Init_Actions
, Remove_Next
(Init_Node
));
3202 if not Is_Empty_List
(Init_Actions
) then
3203 Comp_Stmt
:= Make_Compound_Statement
(Loc
, Actions
=> Init_Actions
);
3204 Insert_Action_After
(Init_Node
, Comp_Stmt
);
3205 Set_Initialization_Statements
(Obj
, Comp_Stmt
);
3207 end Collect_Initialization_Statements
;
3209 -------------------------------
3210 -- Convert_Aggr_In_Allocator --
3211 -------------------------------
3213 procedure Convert_Aggr_In_Allocator
3218 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3219 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3220 Temp
: constant Entity_Id
:= Defining_Identifier
(Decl
);
3222 Occ
: constant Node_Id
:=
3223 Unchecked_Convert_To
(Typ
,
3224 Make_Explicit_Dereference
(Loc
, New_Occurrence_Of
(Temp
, Loc
)));
3227 if Is_Array_Type
(Typ
) then
3228 Convert_Array_Aggr_In_Allocator
(Decl
, Aggr
, Occ
);
3230 elsif Has_Default_Init_Comps
(Aggr
) then
3232 L
: constant List_Id
:= New_List
;
3233 Init_Stmts
: List_Id
;
3236 Init_Stmts
:= Late_Expansion
(Aggr
, Typ
, Occ
);
3238 if Has_Task
(Typ
) then
3239 Build_Task_Allocate_Block_With_Init_Stmts
(L
, Aggr
, Init_Stmts
);
3240 Insert_Actions
(Alloc
, L
);
3242 Insert_Actions
(Alloc
, Init_Stmts
);
3247 Insert_Actions
(Alloc
, Late_Expansion
(Aggr
, Typ
, Occ
));
3249 end Convert_Aggr_In_Allocator
;
3251 --------------------------------
3252 -- Convert_Aggr_In_Assignment --
3253 --------------------------------
3255 procedure Convert_Aggr_In_Assignment
(N
: Node_Id
) is
3256 Aggr
: Node_Id
:= Expression
(N
);
3257 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3258 Occ
: constant Node_Id
:= New_Copy_Tree
(Name
(N
));
3261 if Nkind
(Aggr
) = N_Qualified_Expression
then
3262 Aggr
:= Expression
(Aggr
);
3265 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
3266 end Convert_Aggr_In_Assignment
;
3268 ---------------------------------
3269 -- Convert_Aggr_In_Object_Decl --
3270 ---------------------------------
3272 procedure Convert_Aggr_In_Object_Decl
(N
: Node_Id
) is
3273 Obj
: constant Entity_Id
:= Defining_Identifier
(N
);
3274 Aggr
: Node_Id
:= Expression
(N
);
3275 Loc
: constant Source_Ptr
:= Sloc
(Aggr
);
3276 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3277 Occ
: constant Node_Id
:= New_Occurrence_Of
(Obj
, Loc
);
3279 function Discriminants_Ok
return Boolean;
3280 -- If the object type is constrained, the discriminants in the
3281 -- aggregate must be checked against the discriminants of the subtype.
3282 -- This cannot be done using Apply_Discriminant_Checks because after
3283 -- expansion there is no aggregate left to check.
3285 ----------------------
3286 -- Discriminants_Ok --
3287 ----------------------
3289 function Discriminants_Ok
return Boolean is
3290 Cond
: Node_Id
:= Empty
;
3299 D
:= First_Discriminant
(Typ
);
3300 Disc1
:= First_Elmt
(Discriminant_Constraint
(Typ
));
3301 Disc2
:= First_Elmt
(Discriminant_Constraint
(Etype
(Obj
)));
3302 while Present
(Disc1
) and then Present
(Disc2
) loop
3303 Val1
:= Node
(Disc1
);
3304 Val2
:= Node
(Disc2
);
3306 if not Is_OK_Static_Expression
(Val1
)
3307 or else not Is_OK_Static_Expression
(Val2
)
3309 Check
:= Make_Op_Ne
(Loc
,
3310 Left_Opnd
=> Duplicate_Subexpr
(Val1
),
3311 Right_Opnd
=> Duplicate_Subexpr
(Val2
));
3317 Cond
:= Make_Or_Else
(Loc
,
3319 Right_Opnd
=> Check
);
3322 elsif Expr_Value
(Val1
) /= Expr_Value
(Val2
) then
3323 Apply_Compile_Time_Constraint_Error
(Aggr
,
3324 Msg
=> "incorrect value for discriminant&??",
3325 Reason
=> CE_Discriminant_Check_Failed
,
3330 Next_Discriminant
(D
);
3335 -- If any discriminant constraint is non-static, emit a check
3337 if Present
(Cond
) then
3339 Make_Raise_Constraint_Error
(Loc
,
3341 Reason
=> CE_Discriminant_Check_Failed
));
3345 end Discriminants_Ok
;
3347 -- Start of processing for Convert_Aggr_In_Object_Decl
3350 Set_Assignment_OK
(Occ
);
3352 if Nkind
(Aggr
) = N_Qualified_Expression
then
3353 Aggr
:= Expression
(Aggr
);
3356 if Has_Discriminants
(Typ
)
3357 and then Typ
/= Etype
(Obj
)
3358 and then Is_Constrained
(Etype
(Obj
))
3359 and then not Discriminants_Ok
3364 -- If the context is an extended return statement, it has its own
3365 -- finalization machinery (i.e. works like a transient scope) and
3366 -- we do not want to create an additional one, because objects on
3367 -- the finalization list of the return must be moved to the caller's
3368 -- finalization list to complete the return.
3370 -- However, if the aggregate is limited, it is built in place, and the
3371 -- controlled components are not assigned to intermediate temporaries
3372 -- so there is no need for a transient scope in this case either.
3374 if Requires_Transient_Scope
(Typ
)
3375 and then Ekind
(Current_Scope
) /= E_Return_Statement
3376 and then not Is_Limited_Type
(Typ
)
3378 Establish_Transient_Scope
3381 Is_Controlled
(Typ
) or else Has_Controlled_Component
(Typ
));
3385 Node_After
: constant Node_Id
:= Next
(N
);
3387 Insert_Actions_After
(N
, Late_Expansion
(Aggr
, Typ
, Occ
));
3388 Collect_Initialization_Statements
(Obj
, N
, Node_After
);
3390 Set_No_Initialization
(N
);
3391 Initialize_Discriminants
(N
, Typ
);
3392 end Convert_Aggr_In_Object_Decl
;
3394 -------------------------------------
3395 -- Convert_Array_Aggr_In_Allocator --
3396 -------------------------------------
3398 procedure Convert_Array_Aggr_In_Allocator
3403 Aggr_Code
: List_Id
;
3404 Typ
: constant Entity_Id
:= Etype
(Aggr
);
3405 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
3408 -- The target is an explicit dereference of the allocated object.
3409 -- Generate component assignments to it, as for an aggregate that
3410 -- appears on the right-hand side of an assignment statement.
3413 Build_Array_Aggr_Code
(Aggr
,
3415 Index
=> First_Index
(Typ
),
3417 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
3419 Insert_Actions_After
(Decl
, Aggr_Code
);
3420 end Convert_Array_Aggr_In_Allocator
;
3422 ----------------------------
3423 -- Convert_To_Assignments --
3424 ----------------------------
3426 procedure Convert_To_Assignments
(N
: Node_Id
; Typ
: Entity_Id
) is
3427 Loc
: constant Source_Ptr
:= Sloc
(N
);
3431 Aggr_Code
: List_Id
;
3433 Target_Expr
: Node_Id
;
3434 Parent_Kind
: Node_Kind
;
3435 Unc_Decl
: Boolean := False;
3436 Parent_Node
: Node_Id
;
3439 pragma Assert
(not Is_Static_Dispatch_Table_Aggregate
(N
));
3440 pragma Assert
(Is_Record_Type
(Typ
));
3442 Parent_Node
:= Parent
(N
);
3443 Parent_Kind
:= Nkind
(Parent_Node
);
3445 if Parent_Kind
= N_Qualified_Expression
then
3447 -- Check if we are in a unconstrained declaration because in this
3448 -- case the current delayed expansion mechanism doesn't work when
3449 -- the declared object size depend on the initializing expr.
3452 Parent_Node
:= Parent
(Parent_Node
);
3453 Parent_Kind
:= Nkind
(Parent_Node
);
3455 if Parent_Kind
= N_Object_Declaration
then
3457 not Is_Entity_Name
(Object_Definition
(Parent_Node
))
3458 or else Has_Discriminants
3459 (Entity
(Object_Definition
(Parent_Node
)))
3460 or else Is_Class_Wide_Type
3461 (Entity
(Object_Definition
(Parent_Node
)));
3466 -- Just set the Delay flag in the cases where the transformation will be
3467 -- done top down from above.
3471 -- Internal aggregate (transformed when expanding the parent)
3473 or else Parent_Kind
= N_Aggregate
3474 or else Parent_Kind
= N_Extension_Aggregate
3475 or else Parent_Kind
= N_Component_Association
3477 -- Allocator (see Convert_Aggr_In_Allocator)
3479 or else Parent_Kind
= N_Allocator
3481 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3483 or else (Parent_Kind
= N_Object_Declaration
and then not Unc_Decl
)
3485 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3486 -- assignments in init procs are taken into account.
3488 or else (Parent_Kind
= N_Assignment_Statement
3489 and then Inside_Init_Proc
)
3491 -- (Ada 2005) An inherently limited type in a return statement, which
3492 -- will be handled in a build-in-place fashion, and may be rewritten
3493 -- as an extended return and have its own finalization machinery.
3494 -- In the case of a simple return, the aggregate needs to be delayed
3495 -- until the scope for the return statement has been created, so
3496 -- that any finalization chain will be associated with that scope.
3497 -- For extended returns, we delay expansion to avoid the creation
3498 -- of an unwanted transient scope that could result in premature
3499 -- finalization of the return object (which is built in place
3500 -- within the caller's scope).
3503 (Is_Limited_View
(Typ
)
3505 (Nkind
(Parent
(Parent_Node
)) = N_Extended_Return_Statement
3506 or else Nkind
(Parent_Node
) = N_Simple_Return_Statement
))
3508 Set_Expansion_Delayed
(N
);
3512 -- Otherwise, if a transient scope is required, create it now. If we
3513 -- are within an initialization procedure do not create such, because
3514 -- the target of the assignment must not be declared within a local
3515 -- block, and because cleanup will take place on return from the
3516 -- initialization procedure.
3517 -- Should the condition be more restrictive ???
3519 if Requires_Transient_Scope
(Typ
) and then not Inside_Init_Proc
then
3520 Establish_Transient_Scope
(N
, Sec_Stack
=> Needs_Finalization
(Typ
));
3523 -- If the aggregate is non-limited, create a temporary. If it is limited
3524 -- and context is an assignment, this is a subaggregate for an enclosing
3525 -- aggregate being expanded. It must be built in place, so use target of
3526 -- the current assignment.
3528 if Is_Limited_Type
(Typ
)
3529 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
3531 Target_Expr
:= New_Copy_Tree
(Name
(Parent
(N
)));
3532 Insert_Actions
(Parent
(N
),
3533 Build_Record_Aggr_Code
(N
, Typ
, Target_Expr
));
3534 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
3537 Temp
:= Make_Temporary
(Loc
, 'A', N
);
3539 -- If the type inherits unknown discriminants, use the view with
3540 -- known discriminants if available.
3542 if Has_Unknown_Discriminants
(Typ
)
3543 and then Present
(Underlying_Record_View
(Typ
))
3545 T
:= Underlying_Record_View
(Typ
);
3551 Make_Object_Declaration
(Loc
,
3552 Defining_Identifier
=> Temp
,
3553 Object_Definition
=> New_Occurrence_Of
(T
, Loc
));
3555 Set_No_Initialization
(Instr
);
3556 Insert_Action
(N
, Instr
);
3557 Initialize_Discriminants
(Instr
, T
);
3559 Target_Expr
:= New_Occurrence_Of
(Temp
, Loc
);
3560 Aggr_Code
:= Build_Record_Aggr_Code
(N
, T
, Target_Expr
);
3562 -- Save the last assignment statement associated with the aggregate
3563 -- when building a controlled object. This reference is utilized by
3564 -- the finalization machinery when marking an object as successfully
3567 if Needs_Finalization
(T
) then
3568 Set_Last_Aggregate_Assignment
(Temp
, Last
(Aggr_Code
));
3571 Insert_Actions
(N
, Aggr_Code
);
3572 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
3573 Analyze_And_Resolve
(N
, T
);
3575 end Convert_To_Assignments
;
3577 ---------------------------
3578 -- Convert_To_Positional --
3579 ---------------------------
3581 procedure Convert_To_Positional
3583 Max_Others_Replicate
: Nat
:= 5;
3584 Handle_Bit_Packed
: Boolean := False)
3586 Typ
: constant Entity_Id
:= Etype
(N
);
3588 Static_Components
: Boolean := True;
3590 procedure Check_Static_Components
;
3591 -- Check whether all components of the aggregate are compile-time known
3592 -- values, and can be passed as is to the back-end without further
3598 Ixb
: Node_Id
) return Boolean;
3599 -- Convert the aggregate into a purely positional form if possible. On
3600 -- entry the bounds of all dimensions are known to be static, and the
3601 -- total number of components is safe enough to expand.
3603 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean;
3604 -- Return True iff the array N is flat (which is not trivial in the case
3605 -- of multidimensional aggregates).
3607 -----------------------------
3608 -- Check_Static_Components --
3609 -----------------------------
3611 -- Could use some comments in this body ???
3613 procedure Check_Static_Components
is
3617 Static_Components
:= True;
3619 if Nkind
(N
) = N_String_Literal
then
3622 elsif Present
(Expressions
(N
)) then
3623 Expr
:= First
(Expressions
(N
));
3624 while Present
(Expr
) loop
3625 if Nkind
(Expr
) /= N_Aggregate
3626 or else not Compile_Time_Known_Aggregate
(Expr
)
3627 or else Expansion_Delayed
(Expr
)
3629 Static_Components
:= False;
3637 if Nkind
(N
) = N_Aggregate
3638 and then Present
(Component_Associations
(N
))
3640 Expr
:= First
(Component_Associations
(N
));
3641 while Present
(Expr
) loop
3642 if Nkind_In
(Expression
(Expr
), N_Integer_Literal
,
3647 elsif Is_Entity_Name
(Expression
(Expr
))
3648 and then Present
(Entity
(Expression
(Expr
)))
3649 and then Ekind
(Entity
(Expression
(Expr
))) =
3650 E_Enumeration_Literal
3654 elsif Nkind
(Expression
(Expr
)) /= N_Aggregate
3655 or else not Compile_Time_Known_Aggregate
(Expression
(Expr
))
3656 or else Expansion_Delayed
(Expression
(Expr
))
3658 Static_Components
:= False;
3665 end Check_Static_Components
;
3674 Ixb
: Node_Id
) return Boolean
3676 Loc
: constant Source_Ptr
:= Sloc
(N
);
3677 Blo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ixb
));
3678 Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Ix
));
3679 Hi
: constant Node_Id
:= Type_High_Bound
(Etype
(Ix
));
3683 Others_Present
: Boolean := False;
3686 if Nkind
(Original_Node
(N
)) = N_String_Literal
then
3690 if not Compile_Time_Known_Value
(Lo
)
3691 or else not Compile_Time_Known_Value
(Hi
)
3696 Lov
:= Expr_Value
(Lo
);
3697 Hiv
:= Expr_Value
(Hi
);
3699 -- Check if there is an others choice
3701 if Present
(Component_Associations
(N
)) then
3707 Assoc
:= First
(Component_Associations
(N
));
3708 while Present
(Assoc
) loop
3710 -- If this is a box association, flattening is in general
3711 -- not possible because at this point we cannot tell if the
3712 -- default is static or even exists.
3714 if Box_Present
(Assoc
) then
3718 Choice
:= First
(Choices
(Assoc
));
3720 while Present
(Choice
) loop
3721 if Nkind
(Choice
) = N_Others_Choice
then
3722 Others_Present
:= True;
3733 -- If the low bound is not known at compile time and others is not
3734 -- present we can proceed since the bounds can be obtained from the
3738 or else (not Compile_Time_Known_Value
(Blo
) and then Others_Present
)
3743 -- Determine if set of alternatives is suitable for conversion and
3744 -- build an array containing the values in sequence.
3747 Vals
: array (UI_To_Int
(Lov
) .. UI_To_Int
(Hiv
))
3748 of Node_Id
:= (others => Empty
);
3749 -- The values in the aggregate sorted appropriately
3752 -- Same data as Vals in list form
3755 -- Used to validate Max_Others_Replicate limit
3758 Num
: Int
:= UI_To_Int
(Lov
);
3764 if Present
(Expressions
(N
)) then
3765 Elmt
:= First
(Expressions
(N
));
3766 while Present
(Elmt
) loop
3767 if Nkind
(Elmt
) = N_Aggregate
3768 and then Present
(Next_Index
(Ix
))
3770 not Flatten
(Elmt
, Next_Index
(Ix
), Next_Index
(Ixb
))
3775 Vals
(Num
) := Relocate_Node
(Elmt
);
3782 if No
(Component_Associations
(N
)) then
3786 Elmt
:= First
(Component_Associations
(N
));
3788 if Nkind
(Expression
(Elmt
)) = N_Aggregate
then
3789 if Present
(Next_Index
(Ix
))
3792 (Expression
(Elmt
), Next_Index
(Ix
), Next_Index
(Ixb
))
3798 Component_Loop
: while Present
(Elmt
) loop
3799 Choice
:= First
(Choices
(Elmt
));
3800 Choice_Loop
: while Present
(Choice
) loop
3802 -- If we have an others choice, fill in the missing elements
3803 -- subject to the limit established by Max_Others_Replicate.
3805 if Nkind
(Choice
) = N_Others_Choice
then
3808 for J
in Vals
'Range loop
3809 if No
(Vals
(J
)) then
3810 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
3811 Rep_Count
:= Rep_Count
+ 1;
3813 -- Check for maximum others replication. Note that
3814 -- we skip this test if either of the restrictions
3815 -- No_Elaboration_Code or No_Implicit_Loops is
3816 -- active, if this is a preelaborable unit or
3817 -- a predefined unit, or if the unit must be
3818 -- placed in data memory. This also ensures that
3819 -- predefined units get the same level of constant
3820 -- folding in Ada 95 and Ada 2005, where their
3821 -- categorization has changed.
3824 P
: constant Entity_Id
:=
3825 Cunit_Entity
(Current_Sem_Unit
);
3828 -- Check if duplication OK and if so continue
3831 if Restriction_Active
(No_Elaboration_Code
)
3832 or else Restriction_Active
(No_Implicit_Loops
)
3834 (Ekind
(Current_Scope
) = E_Package
3835 and then Static_Elaboration_Desired
3837 or else Is_Preelaborated
(P
)
3838 or else (Ekind
(P
) = E_Package_Body
3840 Is_Preelaborated
(Spec_Entity
(P
)))
3842 Is_Predefined_File_Name
3843 (Unit_File_Name
(Get_Source_Unit
(P
)))
3847 -- If duplication not OK, then we return False
3848 -- if the replication count is too high
3850 elsif Rep_Count
> Max_Others_Replicate
then
3853 -- Continue on if duplication not OK, but the
3854 -- replication count is not excessive.
3863 exit Component_Loop
;
3865 -- Case of a subtype mark, identifier or expanded name
3867 elsif Is_Entity_Name
(Choice
)
3868 and then Is_Type
(Entity
(Choice
))
3870 Lo
:= Type_Low_Bound
(Etype
(Choice
));
3871 Hi
:= Type_High_Bound
(Etype
(Choice
));
3873 -- Case of subtype indication
3875 elsif Nkind
(Choice
) = N_Subtype_Indication
then
3876 Lo
:= Low_Bound
(Range_Expression
(Constraint
(Choice
)));
3877 Hi
:= High_Bound
(Range_Expression
(Constraint
(Choice
)));
3881 elsif Nkind
(Choice
) = N_Range
then
3882 Lo
:= Low_Bound
(Choice
);
3883 Hi
:= High_Bound
(Choice
);
3885 -- Normal subexpression case
3887 else pragma Assert
(Nkind
(Choice
) in N_Subexpr
);
3888 if not Compile_Time_Known_Value
(Choice
) then
3892 Choice_Index
:= UI_To_Int
(Expr_Value
(Choice
));
3894 if Choice_Index
in Vals
'Range then
3895 Vals
(Choice_Index
) :=
3896 New_Copy_Tree
(Expression
(Elmt
));
3899 -- Choice is statically out-of-range, will be
3900 -- rewritten to raise Constraint_Error.
3908 -- Range cases merge with Lo,Hi set
3910 if not Compile_Time_Known_Value
(Lo
)
3912 not Compile_Time_Known_Value
(Hi
)
3917 for J
in UI_To_Int
(Expr_Value
(Lo
)) ..
3918 UI_To_Int
(Expr_Value
(Hi
))
3920 Vals
(J
) := New_Copy_Tree
(Expression
(Elmt
));
3926 end loop Choice_Loop
;
3929 end loop Component_Loop
;
3931 -- If we get here the conversion is possible
3934 for J
in Vals
'Range loop
3935 Append
(Vals
(J
), Vlist
);
3938 Rewrite
(N
, Make_Aggregate
(Loc
, Expressions
=> Vlist
));
3939 Set_Aggregate_Bounds
(N
, Aggregate_Bounds
(Original_Node
(N
)));
3948 function Is_Flat
(N
: Node_Id
; Dims
: Int
) return Boolean is
3955 elsif Nkind
(N
) = N_Aggregate
then
3956 if Present
(Component_Associations
(N
)) then
3960 Elmt
:= First
(Expressions
(N
));
3961 while Present
(Elmt
) loop
3962 if not Is_Flat
(Elmt
, Dims
- 1) then
3976 -- Start of processing for Convert_To_Positional
3979 -- Only convert to positional when generating C in case of an
3980 -- object declaration, this is the only case where aggregates are
3983 if Modify_Tree_For_C
and then not In_Object_Declaration
(N
) then
3987 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3988 -- components because in this case will need to call the corresponding
3991 if Has_Default_Init_Comps
(N
) then
3995 if Is_Flat
(N
, Number_Dimensions
(Typ
)) then
3999 if Is_Bit_Packed_Array
(Typ
) and then not Handle_Bit_Packed
then
4003 -- Do not convert to positional if controlled components are involved
4004 -- since these require special processing
4006 if Has_Controlled_Component
(Typ
) then
4010 Check_Static_Components
;
4012 -- If the size is known, or all the components are static, try to
4013 -- build a fully positional aggregate.
4015 -- The size of the type may not be known for an aggregate with
4016 -- discriminated array components, but if the components are static
4017 -- it is still possible to verify statically that the length is
4018 -- compatible with the upper bound of the type, and therefore it is
4019 -- worth flattening such aggregates as well.
4021 -- For now the back-end expands these aggregates into individual
4022 -- assignments to the target anyway, but it is conceivable that
4023 -- it will eventually be able to treat such aggregates statically???
4025 if Aggr_Size_OK
(N
, Typ
)
4026 and then Flatten
(N
, First_Index
(Typ
), First_Index
(Base_Type
(Typ
)))
4028 if Static_Components
then
4029 Set_Compile_Time_Known_Aggregate
(N
);
4030 Set_Expansion_Delayed
(N
, False);
4033 Analyze_And_Resolve
(N
, Typ
);
4036 -- Is Static_Eaboration_Desired has been specified, diagnose aggregates
4037 -- that will still require initialization code.
4039 if (Ekind
(Current_Scope
) = E_Package
4040 and then Static_Elaboration_Desired
(Current_Scope
))
4041 and then Nkind
(Parent
(N
)) = N_Object_Declaration
4047 if Nkind
(N
) = N_Aggregate
and then Present
(Expressions
(N
)) then
4048 Expr
:= First
(Expressions
(N
));
4049 while Present
(Expr
) loop
4050 if Nkind_In
(Expr
, N_Integer_Literal
, N_Real_Literal
)
4052 (Is_Entity_Name
(Expr
)
4053 and then Ekind
(Entity
(Expr
)) = E_Enumeration_Literal
)
4059 ("non-static object requires elaboration code??", N
);
4066 if Present
(Component_Associations
(N
)) then
4067 Error_Msg_N
("object requires elaboration code??", N
);
4072 end Convert_To_Positional
;
4074 ----------------------------
4075 -- Expand_Array_Aggregate --
4076 ----------------------------
4078 -- Array aggregate expansion proceeds as follows:
4080 -- 1. If requested we generate code to perform all the array aggregate
4081 -- bound checks, specifically
4083 -- (a) Check that the index range defined by aggregate bounds is
4084 -- compatible with corresponding index subtype.
4086 -- (b) If an others choice is present check that no aggregate
4087 -- index is outside the bounds of the index constraint.
4089 -- (c) For multidimensional arrays make sure that all subaggregates
4090 -- corresponding to the same dimension have the same bounds.
4092 -- 2. Check for packed array aggregate which can be converted to a
4093 -- constant so that the aggregate disappears completely.
4095 -- 3. Check case of nested aggregate. Generally nested aggregates are
4096 -- handled during the processing of the parent aggregate.
4098 -- 4. Check if the aggregate can be statically processed. If this is the
4099 -- case pass it as is to Gigi. Note that a necessary condition for
4100 -- static processing is that the aggregate be fully positional.
4102 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4103 -- a temporary) then mark the aggregate as such and return. Otherwise
4104 -- create a new temporary and generate the appropriate initialization
4107 procedure Expand_Array_Aggregate
(N
: Node_Id
) is
4108 Loc
: constant Source_Ptr
:= Sloc
(N
);
4110 Typ
: constant Entity_Id
:= Etype
(N
);
4111 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
4112 -- Typ is the correct constrained array subtype of the aggregate
4113 -- Ctyp is the corresponding component type.
4115 Aggr_Dimension
: constant Pos
:= Number_Dimensions
(Typ
);
4116 -- Number of aggregate index dimensions
4118 Aggr_Low
: array (1 .. Aggr_Dimension
) of Node_Id
;
4119 Aggr_High
: array (1 .. Aggr_Dimension
) of Node_Id
;
4120 -- Low and High bounds of the constraint for each aggregate index
4122 Aggr_Index_Typ
: array (1 .. Aggr_Dimension
) of Entity_Id
;
4123 -- The type of each index
4125 In_Place_Assign_OK_For_Declaration
: Boolean := False;
4126 -- True if we are to generate an in place assignment for a declaration
4128 Maybe_In_Place_OK
: Boolean;
4129 -- If the type is neither controlled nor packed and the aggregate
4130 -- is the expression in an assignment, assignment in place may be
4131 -- possible, provided other conditions are met on the LHS.
4133 Others_Present
: array (1 .. Aggr_Dimension
) of Boolean :=
4135 -- If Others_Present (J) is True, then there is an others choice
4136 -- in one of the sub-aggregates of N at dimension J.
4138 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean;
4139 -- Returns true if an aggregate assignment can be done by the back end
4141 procedure Build_Constrained_Type
(Positional
: Boolean);
4142 -- If the subtype is not static or unconstrained, build a constrained
4143 -- type using the computable sizes of the aggregate and its sub-
4146 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
);
4147 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4150 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4151 -- Checks that in a multi-dimensional array aggregate all subaggregates
4152 -- corresponding to the same dimension have the same bounds.
4153 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4154 -- corresponding to the sub-aggregate.
4156 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4157 -- Computes the values of array Others_Present. Sub_Aggr is the
4158 -- array sub-aggregate we start the computation from. Dim is the
4159 -- dimension corresponding to the sub-aggregate.
4161 function In_Place_Assign_OK
return Boolean;
4162 -- Simple predicate to determine whether an aggregate assignment can
4163 -- be done in place, because none of the new values can depend on the
4164 -- components of the target of the assignment.
4166 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
);
4167 -- Checks that if an others choice is present in any sub-aggregate no
4168 -- aggregate index is outside the bounds of the index constraint.
4169 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4170 -- corresponding to the sub-aggregate.
4172 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean;
4173 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4174 -- built directly into the target of the assignment it must be free
4177 ------------------------------------
4178 -- Aggr_Assignment_OK_For_Backend --
4179 ------------------------------------
4181 -- Backend processing by Gigi/gcc is possible only if all the following
4182 -- conditions are met:
4184 -- 1. N consists of a single OTHERS choice, possibly recursively
4186 -- 2. The array type is not packed
4188 -- 3. The array type has no atomic components
4190 -- 4. The array type has no null ranges (the purpose of this is to
4191 -- avoid a bogus warning for an out-of-range value).
4193 -- 5. The component type is discrete
4195 -- 6. The component size is Storage_Unit or the value is of the form
4196 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4197 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4198 -- the 8-bit value M, concatenated together.
4200 -- The ultimate goal is to generate a call to a fast memset routine
4201 -- specifically optimized for the target.
4203 function Aggr_Assignment_OK_For_Backend
(N
: Node_Id
) return Boolean is
4206 Expr
: Node_Id
:= N
;
4214 -- Recurse as far as possible to find the innermost component type
4217 while Is_Array_Type
(Ctyp
) loop
4218 if Nkind
(Expr
) /= N_Aggregate
4219 or else not Is_Others_Aggregate
(Expr
)
4224 if Present
(Packed_Array_Impl_Type
(Ctyp
)) then
4228 if Has_Atomic_Components
(Ctyp
) then
4232 Index
:= First_Index
(Ctyp
);
4233 while Present
(Index
) loop
4234 Get_Index_Bounds
(Index
, Low
, High
);
4236 if Is_Null_Range
(Low
, High
) then
4243 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
4245 for J
in 1 .. Number_Dimensions
(Ctyp
) - 1 loop
4246 if Nkind
(Expr
) /= N_Aggregate
4247 or else not Is_Others_Aggregate
(Expr
)
4252 Expr
:= Expression
(First
(Component_Associations
(Expr
)));
4255 Ctyp
:= Component_Type
(Ctyp
);
4257 if Is_Atomic_Or_VFA
(Ctyp
) then
4262 if not Is_Discrete_Type
(Ctyp
) then
4266 -- The expression needs to be analyzed if True is returned
4268 Analyze_And_Resolve
(Expr
, Ctyp
);
4270 -- The back end uses the Esize as the precision of the type
4272 Nunits
:= UI_To_Int
(Esize
(Ctyp
)) / System_Storage_Unit
;
4278 if not Compile_Time_Known_Value
(Expr
) then
4282 Value
:= Expr_Value
(Expr
);
4284 if Has_Biased_Representation
(Ctyp
) then
4285 Value
:= Value
- Expr_Value
(Type_Low_Bound
(Ctyp
));
4288 -- Values 0 and -1 immediately satisfy the last check
4290 if Value
= Uint_0
or else Value
= Uint_Minus_1
then
4294 -- We need to work with an unsigned value
4297 Value
:= Value
+ 2**(System_Storage_Unit
* Nunits
);
4300 Remainder
:= Value
rem 2**System_Storage_Unit
;
4302 for J
in 1 .. Nunits
- 1 loop
4303 Value
:= Value
/ 2**System_Storage_Unit
;
4305 if Value
rem 2**System_Storage_Unit
/= Remainder
then
4311 end Aggr_Assignment_OK_For_Backend
;
4313 ----------------------------
4314 -- Build_Constrained_Type --
4315 ----------------------------
4317 procedure Build_Constrained_Type
(Positional
: Boolean) is
4318 Loc
: constant Source_Ptr
:= Sloc
(N
);
4319 Agg_Type
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
4322 Typ
: constant Entity_Id
:= Etype
(N
);
4323 Indexes
: constant List_Id
:= New_List
;
4328 -- If the aggregate is purely positional, all its subaggregates
4329 -- have the same size. We collect the dimensions from the first
4330 -- subaggregate at each level.
4335 for D
in 1 .. Number_Dimensions
(Typ
) loop
4336 Sub_Agg
:= First
(Expressions
(Sub_Agg
));
4340 while Present
(Comp
) loop
4347 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
4348 High_Bound
=> Make_Integer_Literal
(Loc
, Num
)));
4352 -- We know the aggregate type is unconstrained and the aggregate
4353 -- is not processable by the back end, therefore not necessarily
4354 -- positional. Retrieve each dimension bounds (computed earlier).
4356 for D
in 1 .. Number_Dimensions
(Typ
) loop
4359 Low_Bound
=> Aggr_Low
(D
),
4360 High_Bound
=> Aggr_High
(D
)));
4365 Make_Full_Type_Declaration
(Loc
,
4366 Defining_Identifier
=> Agg_Type
,
4368 Make_Constrained_Array_Definition
(Loc
,
4369 Discrete_Subtype_Definitions
=> Indexes
,
4370 Component_Definition
=>
4371 Make_Component_Definition
(Loc
,
4372 Aliased_Present
=> False,
4373 Subtype_Indication
=>
4374 New_Occurrence_Of
(Component_Type
(Typ
), Loc
))));
4376 Insert_Action
(N
, Decl
);
4378 Set_Etype
(N
, Agg_Type
);
4379 Set_Is_Itype
(Agg_Type
);
4380 Freeze_Itype
(Agg_Type
, N
);
4381 end Build_Constrained_Type
;
4387 procedure Check_Bounds
(Aggr_Bounds
: Node_Id
; Index_Bounds
: Node_Id
) is
4394 Cond
: Node_Id
:= Empty
;
4397 Get_Index_Bounds
(Aggr_Bounds
, Aggr_Lo
, Aggr_Hi
);
4398 Get_Index_Bounds
(Index_Bounds
, Ind_Lo
, Ind_Hi
);
4400 -- Generate the following test:
4402 -- [constraint_error when
4403 -- Aggr_Lo <= Aggr_Hi and then
4404 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4406 -- As an optimization try to see if some tests are trivially vacuous
4407 -- because we are comparing an expression against itself.
4409 if Aggr_Lo
= Ind_Lo
and then Aggr_Hi
= Ind_Hi
then
4412 elsif Aggr_Hi
= Ind_Hi
then
4415 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
4416 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
));
4418 elsif Aggr_Lo
= Ind_Lo
then
4421 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
4422 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Hi
));
4429 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
4430 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ind_Lo
)),
4434 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
4435 Right_Opnd
=> Duplicate_Subexpr
(Ind_Hi
)));
4438 if Present
(Cond
) then
4443 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
4444 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
)),
4446 Right_Opnd
=> Cond
);
4448 Set_Analyzed
(Left_Opnd
(Left_Opnd
(Cond
)), False);
4449 Set_Analyzed
(Right_Opnd
(Left_Opnd
(Cond
)), False);
4451 Make_Raise_Constraint_Error
(Loc
,
4453 Reason
=> CE_Range_Check_Failed
));
4457 ----------------------------
4458 -- Check_Same_Aggr_Bounds --
4459 ----------------------------
4461 procedure Check_Same_Aggr_Bounds
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
4462 Sub_Lo
: constant Node_Id
:= Low_Bound
(Aggregate_Bounds
(Sub_Aggr
));
4463 Sub_Hi
: constant Node_Id
:= High_Bound
(Aggregate_Bounds
(Sub_Aggr
));
4464 -- The bounds of this specific sub-aggregate
4466 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
4467 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
4468 -- The bounds of the aggregate for this dimension
4470 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
4471 -- The index type for this dimension.xxx
4473 Cond
: Node_Id
:= Empty
;
4478 -- If index checks are on generate the test
4480 -- [constraint_error when
4481 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4483 -- As an optimization try to see if some tests are trivially vacuos
4484 -- because we are comparing an expression against itself. Also for
4485 -- the first dimension the test is trivially vacuous because there
4486 -- is just one aggregate for dimension 1.
4488 if Index_Checks_Suppressed
(Ind_Typ
) then
4491 elsif Dim
= 1 or else (Aggr_Lo
= Sub_Lo
and then Aggr_Hi
= Sub_Hi
)
4495 elsif Aggr_Hi
= Sub_Hi
then
4498 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
4499 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
));
4501 elsif Aggr_Lo
= Sub_Lo
then
4504 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Hi
),
4505 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Hi
));
4512 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
),
4513 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Sub_Lo
)),
4517 Left_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
),
4518 Right_Opnd
=> Duplicate_Subexpr
(Sub_Hi
)));
4521 if Present
(Cond
) then
4523 Make_Raise_Constraint_Error
(Loc
,
4525 Reason
=> CE_Length_Check_Failed
));
4528 -- Now look inside the sub-aggregate to see if there is more work
4530 if Dim
< Aggr_Dimension
then
4532 -- Process positional components
4534 if Present
(Expressions
(Sub_Aggr
)) then
4535 Expr
:= First
(Expressions
(Sub_Aggr
));
4536 while Present
(Expr
) loop
4537 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
4542 -- Process component associations
4544 if Present
(Component_Associations
(Sub_Aggr
)) then
4545 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
4546 while Present
(Assoc
) loop
4547 Expr
:= Expression
(Assoc
);
4548 Check_Same_Aggr_Bounds
(Expr
, Dim
+ 1);
4553 end Check_Same_Aggr_Bounds
;
4555 ----------------------------
4556 -- Compute_Others_Present --
4557 ----------------------------
4559 procedure Compute_Others_Present
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
4564 if Present
(Component_Associations
(Sub_Aggr
)) then
4565 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
4567 if Nkind
(First
(Choices
(Assoc
))) = N_Others_Choice
then
4568 Others_Present
(Dim
) := True;
4572 -- Now look inside the sub-aggregate to see if there is more work
4574 if Dim
< Aggr_Dimension
then
4576 -- Process positional components
4578 if Present
(Expressions
(Sub_Aggr
)) then
4579 Expr
:= First
(Expressions
(Sub_Aggr
));
4580 while Present
(Expr
) loop
4581 Compute_Others_Present
(Expr
, Dim
+ 1);
4586 -- Process component associations
4588 if Present
(Component_Associations
(Sub_Aggr
)) then
4589 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
4590 while Present
(Assoc
) loop
4591 Expr
:= Expression
(Assoc
);
4592 Compute_Others_Present
(Expr
, Dim
+ 1);
4597 end Compute_Others_Present
;
4599 ------------------------
4600 -- In_Place_Assign_OK --
4601 ------------------------
4603 function In_Place_Assign_OK
return Boolean is
4611 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean;
4612 -- Check recursively that each component of a (sub)aggregate does
4613 -- not depend on the variable being assigned to.
4615 function Safe_Component
(Expr
: Node_Id
) return Boolean;
4616 -- Verify that an expression cannot depend on the variable being
4617 -- assigned to. Room for improvement here (but less than before).
4619 --------------------
4620 -- Safe_Aggregate --
4621 --------------------
4623 function Safe_Aggregate
(Aggr
: Node_Id
) return Boolean is
4627 if Present
(Expressions
(Aggr
)) then
4628 Expr
:= First
(Expressions
(Aggr
));
4629 while Present
(Expr
) loop
4630 if Nkind
(Expr
) = N_Aggregate
then
4631 if not Safe_Aggregate
(Expr
) then
4635 elsif not Safe_Component
(Expr
) then
4643 if Present
(Component_Associations
(Aggr
)) then
4644 Expr
:= First
(Component_Associations
(Aggr
));
4645 while Present
(Expr
) loop
4646 if Nkind
(Expression
(Expr
)) = N_Aggregate
then
4647 if not Safe_Aggregate
(Expression
(Expr
)) then
4651 -- If association has a box, no way to determine yet
4652 -- whether default can be assigned in place.
4654 elsif Box_Present
(Expr
) then
4657 elsif not Safe_Component
(Expression
(Expr
)) then
4668 --------------------
4669 -- Safe_Component --
4670 --------------------
4672 function Safe_Component
(Expr
: Node_Id
) return Boolean is
4673 Comp
: Node_Id
:= Expr
;
4675 function Check_Component
(Comp
: Node_Id
) return Boolean;
4676 -- Do the recursive traversal, after copy
4678 ---------------------
4679 -- Check_Component --
4680 ---------------------
4682 function Check_Component
(Comp
: Node_Id
) return Boolean is
4684 if Is_Overloaded
(Comp
) then
4688 return Compile_Time_Known_Value
(Comp
)
4690 or else (Is_Entity_Name
(Comp
)
4691 and then Present
(Entity
(Comp
))
4692 and then No
(Renamed_Object
(Entity
(Comp
))))
4694 or else (Nkind
(Comp
) = N_Attribute_Reference
4695 and then Check_Component
(Prefix
(Comp
)))
4697 or else (Nkind
(Comp
) in N_Binary_Op
4698 and then Check_Component
(Left_Opnd
(Comp
))
4699 and then Check_Component
(Right_Opnd
(Comp
)))
4701 or else (Nkind
(Comp
) in N_Unary_Op
4702 and then Check_Component
(Right_Opnd
(Comp
)))
4704 or else (Nkind
(Comp
) = N_Selected_Component
4705 and then Check_Component
(Prefix
(Comp
)))
4707 or else (Nkind
(Comp
) = N_Unchecked_Type_Conversion
4708 and then Check_Component
(Expression
(Comp
)));
4709 end Check_Component
;
4711 -- Start of processing for Safe_Component
4714 -- If the component appears in an association that may correspond
4715 -- to more than one element, it is not analyzed before expansion
4716 -- into assignments, to avoid side effects. We analyze, but do not
4717 -- resolve the copy, to obtain sufficient entity information for
4718 -- the checks that follow. If component is overloaded we assume
4719 -- an unsafe function call.
4721 if not Analyzed
(Comp
) then
4722 if Is_Overloaded
(Expr
) then
4725 elsif Nkind
(Expr
) = N_Aggregate
4726 and then not Is_Others_Aggregate
(Expr
)
4730 elsif Nkind
(Expr
) = N_Allocator
then
4732 -- For now, too complex to analyze
4737 Comp
:= New_Copy_Tree
(Expr
);
4738 Set_Parent
(Comp
, Parent
(Expr
));
4742 if Nkind
(Comp
) = N_Aggregate
then
4743 return Safe_Aggregate
(Comp
);
4745 return Check_Component
(Comp
);
4749 -- Start of processing for In_Place_Assign_OK
4752 if Present
(Component_Associations
(N
)) then
4754 -- On assignment, sliding can take place, so we cannot do the
4755 -- assignment in place unless the bounds of the aggregate are
4756 -- statically equal to those of the target.
4758 -- If the aggregate is given by an others choice, the bounds are
4759 -- derived from the left-hand side, and the assignment is safe if
4760 -- the expression is.
4762 if Is_Others_Aggregate
(N
) then
4765 (Expression
(First
(Component_Associations
(N
))));
4768 Aggr_In
:= First_Index
(Etype
(N
));
4770 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
4771 Obj_In
:= First_Index
(Etype
(Name
(Parent
(N
))));
4774 -- Context is an allocator. Check bounds of aggregate against
4775 -- given type in qualified expression.
4777 pragma Assert
(Nkind
(Parent
(Parent
(N
))) = N_Allocator
);
4779 First_Index
(Etype
(Entity
(Subtype_Mark
(Parent
(N
)))));
4782 while Present
(Aggr_In
) loop
4783 Get_Index_Bounds
(Aggr_In
, Aggr_Lo
, Aggr_Hi
);
4784 Get_Index_Bounds
(Obj_In
, Obj_Lo
, Obj_Hi
);
4786 if not Compile_Time_Known_Value
(Aggr_Lo
)
4787 or else not Compile_Time_Known_Value
(Aggr_Hi
)
4788 or else not Compile_Time_Known_Value
(Obj_Lo
)
4789 or else not Compile_Time_Known_Value
(Obj_Hi
)
4790 or else Expr_Value
(Aggr_Lo
) /= Expr_Value
(Obj_Lo
)
4791 or else Expr_Value
(Aggr_Hi
) /= Expr_Value
(Obj_Hi
)
4796 Next_Index
(Aggr_In
);
4797 Next_Index
(Obj_In
);
4801 -- Now check the component values themselves
4803 return Safe_Aggregate
(N
);
4804 end In_Place_Assign_OK
;
4810 procedure Others_Check
(Sub_Aggr
: Node_Id
; Dim
: Pos
) is
4811 Aggr_Lo
: constant Node_Id
:= Aggr_Low
(Dim
);
4812 Aggr_Hi
: constant Node_Id
:= Aggr_High
(Dim
);
4813 -- The bounds of the aggregate for this dimension
4815 Ind_Typ
: constant Entity_Id
:= Aggr_Index_Typ
(Dim
);
4816 -- The index type for this dimension
4818 Need_To_Check
: Boolean := False;
4820 Choices_Lo
: Node_Id
:= Empty
;
4821 Choices_Hi
: Node_Id
:= Empty
;
4822 -- The lowest and highest discrete choices for a named sub-aggregate
4824 Nb_Choices
: Int
:= -1;
4825 -- The number of discrete non-others choices in this sub-aggregate
4827 Nb_Elements
: Uint
:= Uint_0
;
4828 -- The number of elements in a positional aggregate
4830 Cond
: Node_Id
:= Empty
;
4837 -- Check if we have an others choice. If we do make sure that this
4838 -- sub-aggregate contains at least one element in addition to the
4841 if Range_Checks_Suppressed
(Ind_Typ
) then
4842 Need_To_Check
:= False;
4844 elsif Present
(Expressions
(Sub_Aggr
))
4845 and then Present
(Component_Associations
(Sub_Aggr
))
4847 Need_To_Check
:= True;
4849 elsif Present
(Component_Associations
(Sub_Aggr
)) then
4850 Assoc
:= Last
(Component_Associations
(Sub_Aggr
));
4852 if Nkind
(First
(Choices
(Assoc
))) /= N_Others_Choice
then
4853 Need_To_Check
:= False;
4856 -- Count the number of discrete choices. Start with -1 because
4857 -- the others choice does not count.
4859 -- Is there some reason we do not use List_Length here ???
4862 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
4863 while Present
(Assoc
) loop
4864 Choice
:= First
(Choices
(Assoc
));
4865 while Present
(Choice
) loop
4866 Nb_Choices
:= Nb_Choices
+ 1;
4873 -- If there is only an others choice nothing to do
4875 Need_To_Check
:= (Nb_Choices
> 0);
4879 Need_To_Check
:= False;
4882 -- If we are dealing with a positional sub-aggregate with an others
4883 -- choice then compute the number or positional elements.
4885 if Need_To_Check
and then Present
(Expressions
(Sub_Aggr
)) then
4886 Expr
:= First
(Expressions
(Sub_Aggr
));
4887 Nb_Elements
:= Uint_0
;
4888 while Present
(Expr
) loop
4889 Nb_Elements
:= Nb_Elements
+ 1;
4893 -- If the aggregate contains discrete choices and an others choice
4894 -- compute the smallest and largest discrete choice values.
4896 elsif Need_To_Check
then
4897 Compute_Choices_Lo_And_Choices_Hi
: declare
4899 Table
: Case_Table_Type
(1 .. Nb_Choices
);
4900 -- Used to sort all the different choice values
4907 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
4908 while Present
(Assoc
) loop
4909 Choice
:= First
(Choices
(Assoc
));
4910 while Present
(Choice
) loop
4911 if Nkind
(Choice
) = N_Others_Choice
then
4915 Get_Index_Bounds
(Choice
, Low
, High
);
4916 Table
(J
).Choice_Lo
:= Low
;
4917 Table
(J
).Choice_Hi
:= High
;
4926 -- Sort the discrete choices
4928 Sort_Case_Table
(Table
);
4930 Choices_Lo
:= Table
(1).Choice_Lo
;
4931 Choices_Hi
:= Table
(Nb_Choices
).Choice_Hi
;
4932 end Compute_Choices_Lo_And_Choices_Hi
;
4935 -- If no others choice in this sub-aggregate, or the aggregate
4936 -- comprises only an others choice, nothing to do.
4938 if not Need_To_Check
then
4941 -- If we are dealing with an aggregate containing an others choice
4942 -- and positional components, we generate the following test:
4944 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4945 -- Ind_Typ'Pos (Aggr_Hi)
4947 -- raise Constraint_Error;
4950 elsif Nb_Elements
> Uint_0
then
4956 Make_Attribute_Reference
(Loc
,
4957 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
4958 Attribute_Name
=> Name_Pos
,
4961 (Duplicate_Subexpr_Move_Checks
(Aggr_Lo
))),
4962 Right_Opnd
=> Make_Integer_Literal
(Loc
, Nb_Elements
- 1)),
4965 Make_Attribute_Reference
(Loc
,
4966 Prefix
=> New_Occurrence_Of
(Ind_Typ
, Loc
),
4967 Attribute_Name
=> Name_Pos
,
4968 Expressions
=> New_List
(
4969 Duplicate_Subexpr_Move_Checks
(Aggr_Hi
))));
4971 -- If we are dealing with an aggregate containing an others choice
4972 -- and discrete choices we generate the following test:
4974 -- [constraint_error when
4975 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4982 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Choices_Lo
),
4983 Right_Opnd
=> Duplicate_Subexpr_Move_Checks
(Aggr_Lo
)),
4987 Left_Opnd
=> Duplicate_Subexpr
(Choices_Hi
),
4988 Right_Opnd
=> Duplicate_Subexpr
(Aggr_Hi
)));
4991 if Present
(Cond
) then
4993 Make_Raise_Constraint_Error
(Loc
,
4995 Reason
=> CE_Length_Check_Failed
));
4996 -- Questionable reason code, shouldn't that be a
4997 -- CE_Range_Check_Failed ???
5000 -- Now look inside the sub-aggregate to see if there is more work
5002 if Dim
< Aggr_Dimension
then
5004 -- Process positional components
5006 if Present
(Expressions
(Sub_Aggr
)) then
5007 Expr
:= First
(Expressions
(Sub_Aggr
));
5008 while Present
(Expr
) loop
5009 Others_Check
(Expr
, Dim
+ 1);
5014 -- Process component associations
5016 if Present
(Component_Associations
(Sub_Aggr
)) then
5017 Assoc
:= First
(Component_Associations
(Sub_Aggr
));
5018 while Present
(Assoc
) loop
5019 Expr
:= Expression
(Assoc
);
5020 Others_Check
(Expr
, Dim
+ 1);
5027 -------------------------
5028 -- Safe_Left_Hand_Side --
5029 -------------------------
5031 function Safe_Left_Hand_Side
(N
: Node_Id
) return Boolean is
5032 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean;
5033 -- If the left-hand side includes an indexed component, check that
5034 -- the indexes are free of side-effect.
5040 function Is_Safe_Index
(Indx
: Node_Id
) return Boolean is
5042 if Is_Entity_Name
(Indx
) then
5045 elsif Nkind
(Indx
) = N_Integer_Literal
then
5048 elsif Nkind
(Indx
) = N_Function_Call
5049 and then Is_Entity_Name
(Name
(Indx
))
5050 and then Has_Pragma_Pure_Function
(Entity
(Name
(Indx
)))
5054 elsif Nkind
(Indx
) = N_Type_Conversion
5055 and then Is_Safe_Index
(Expression
(Indx
))
5064 -- Start of processing for Safe_Left_Hand_Side
5067 if Is_Entity_Name
(N
) then
5070 elsif Nkind_In
(N
, N_Explicit_Dereference
, N_Selected_Component
)
5071 and then Safe_Left_Hand_Side
(Prefix
(N
))
5075 elsif Nkind
(N
) = N_Indexed_Component
5076 and then Safe_Left_Hand_Side
(Prefix
(N
))
5077 and then Is_Safe_Index
(First
(Expressions
(N
)))
5081 elsif Nkind
(N
) = N_Unchecked_Type_Conversion
then
5082 return Safe_Left_Hand_Side
(Expression
(N
));
5087 end Safe_Left_Hand_Side
;
5092 -- Holds the temporary aggregate value
5095 -- Holds the declaration of Tmp
5097 Aggr_Code
: List_Id
;
5098 Parent_Node
: Node_Id
;
5099 Parent_Kind
: Node_Kind
;
5101 -- Start of processing for Expand_Array_Aggregate
5104 -- Do not touch the special aggregates of attributes used for Asm calls
5106 if Is_RTE
(Ctyp
, RE_Asm_Input_Operand
)
5107 or else Is_RTE
(Ctyp
, RE_Asm_Output_Operand
)
5111 -- Do not expand an aggregate for an array type which contains tasks if
5112 -- the aggregate is associated with an unexpanded return statement of a
5113 -- build-in-place function. The aggregate is expanded when the related
5114 -- return statement (rewritten into an extended return) is processed.
5115 -- This delay ensures that any temporaries and initialization code
5116 -- generated for the aggregate appear in the proper return block and
5117 -- use the correct _chain and _master.
5119 elsif Has_Task
(Base_Type
(Etype
(N
)))
5120 and then Nkind
(Parent
(N
)) = N_Simple_Return_Statement
5121 and then Is_Build_In_Place_Function
5122 (Return_Applies_To
(Return_Statement_Entity
(Parent
(N
))))
5126 -- Do not attempt expansion if error already detected. We may reach this
5127 -- point in spite of previous errors when compiling with -gnatq, to
5128 -- force all possible errors (this is the usual ACATS mode).
5130 elsif Error_Posted
(N
) then
5134 -- If the semantic analyzer has determined that aggregate N will raise
5135 -- Constraint_Error at run time, then the aggregate node has been
5136 -- replaced with an N_Raise_Constraint_Error node and we should
5139 pragma Assert
(not Raises_Constraint_Error
(N
));
5143 -- Check that the index range defined by aggregate bounds is
5144 -- compatible with corresponding index subtype.
5146 Index_Compatibility_Check
: declare
5147 Aggr_Index_Range
: Node_Id
:= First_Index
(Typ
);
5148 -- The current aggregate index range
5150 Index_Constraint
: Node_Id
:= First_Index
(Etype
(Typ
));
5151 -- The corresponding index constraint against which we have to
5152 -- check the above aggregate index range.
5155 Compute_Others_Present
(N
, 1);
5157 for J
in 1 .. Aggr_Dimension
loop
5158 -- There is no need to emit a check if an others choice is present
5159 -- for this array aggregate dimension since in this case one of
5160 -- N's sub-aggregates has taken its bounds from the context and
5161 -- these bounds must have been checked already. In addition all
5162 -- sub-aggregates corresponding to the same dimension must all
5163 -- have the same bounds (checked in (c) below).
5165 if not Range_Checks_Suppressed
(Etype
(Index_Constraint
))
5166 and then not Others_Present
(J
)
5168 -- We don't use Checks.Apply_Range_Check here because it emits
5169 -- a spurious check. Namely it checks that the range defined by
5170 -- the aggregate bounds is non empty. But we know this already
5173 Check_Bounds
(Aggr_Index_Range
, Index_Constraint
);
5176 -- Save the low and high bounds of the aggregate index as well as
5177 -- the index type for later use in checks (b) and (c) below.
5179 Aggr_Low
(J
) := Low_Bound
(Aggr_Index_Range
);
5180 Aggr_High
(J
) := High_Bound
(Aggr_Index_Range
);
5182 Aggr_Index_Typ
(J
) := Etype
(Index_Constraint
);
5184 Next_Index
(Aggr_Index_Range
);
5185 Next_Index
(Index_Constraint
);
5187 end Index_Compatibility_Check
;
5191 -- If an others choice is present check that no aggregate index is
5192 -- outside the bounds of the index constraint.
5194 Others_Check
(N
, 1);
5198 -- For multidimensional arrays make sure that all subaggregates
5199 -- corresponding to the same dimension have the same bounds.
5201 if Aggr_Dimension
> 1 then
5202 Check_Same_Aggr_Bounds
(N
, 1);
5207 -- If we have a default component value, or simple initialization is
5208 -- required for the component type, then we replace <> in component
5209 -- associations by the required default value.
5212 Default_Val
: Node_Id
;
5216 if (Present
(Default_Aspect_Component_Value
(Typ
))
5217 or else Needs_Simple_Initialization
(Ctyp
))
5218 and then Present
(Component_Associations
(N
))
5220 Assoc
:= First
(Component_Associations
(N
));
5221 while Present
(Assoc
) loop
5222 if Nkind
(Assoc
) = N_Component_Association
5223 and then Box_Present
(Assoc
)
5225 Set_Box_Present
(Assoc
, False);
5227 if Present
(Default_Aspect_Component_Value
(Typ
)) then
5228 Default_Val
:= Default_Aspect_Component_Value
(Typ
);
5230 Default_Val
:= Get_Simple_Init_Val
(Ctyp
, N
);
5233 Set_Expression
(Assoc
, New_Copy_Tree
(Default_Val
));
5234 Analyze_And_Resolve
(Expression
(Assoc
), Ctyp
);
5244 -- Here we test for is packed array aggregate that we can handle at
5245 -- compile time. If so, return with transformation done. Note that we do
5246 -- this even if the aggregate is nested, because once we have done this
5247 -- processing, there is no more nested aggregate.
5249 if Packed_Array_Aggregate_Handled
(N
) then
5253 -- At this point we try to convert to positional form
5255 if Ekind
(Current_Scope
) = E_Package
5256 and then Static_Elaboration_Desired
(Current_Scope
)
5258 Convert_To_Positional
(N
, Max_Others_Replicate
=> 100);
5260 Convert_To_Positional
(N
);
5263 -- if the result is no longer an aggregate (e.g. it may be a string
5264 -- literal, or a temporary which has the needed value), then we are
5265 -- done, since there is no longer a nested aggregate.
5267 if Nkind
(N
) /= N_Aggregate
then
5270 -- We are also done if the result is an analyzed aggregate, indicating
5271 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5274 elsif Analyzed
(N
) and then N
/= Original_Node
(N
) then
5278 -- If all aggregate components are compile-time known and the aggregate
5279 -- has been flattened, nothing left to do. The same occurs if the
5280 -- aggregate is used to initialize the components of a statically
5281 -- allocated dispatch table.
5283 if Compile_Time_Known_Aggregate
(N
)
5284 or else Is_Static_Dispatch_Table_Aggregate
(N
)
5286 Set_Expansion_Delayed
(N
, False);
5290 -- Now see if back end processing is possible
5292 if Backend_Processing_Possible
(N
) then
5294 -- If the aggregate is static but the constraints are not, build
5295 -- a static subtype for the aggregate, so that Gigi can place it
5296 -- in static memory. Perform an unchecked_conversion to the non-
5297 -- static type imposed by the context.
5300 Itype
: constant Entity_Id
:= Etype
(N
);
5302 Needs_Type
: Boolean := False;
5305 Index
:= First_Index
(Itype
);
5306 while Present
(Index
) loop
5307 if not Is_OK_Static_Subtype
(Etype
(Index
)) then
5316 Build_Constrained_Type
(Positional
=> True);
5317 Rewrite
(N
, Unchecked_Convert_To
(Itype
, N
));
5327 -- Delay expansion for nested aggregates: it will be taken care of
5328 -- when the parent aggregate is expanded.
5330 Parent_Node
:= Parent
(N
);
5331 Parent_Kind
:= Nkind
(Parent_Node
);
5333 if Parent_Kind
= N_Qualified_Expression
then
5334 Parent_Node
:= Parent
(Parent_Node
);
5335 Parent_Kind
:= Nkind
(Parent_Node
);
5338 if Parent_Kind
= N_Aggregate
5339 or else Parent_Kind
= N_Extension_Aggregate
5340 or else Parent_Kind
= N_Component_Association
5341 or else (Parent_Kind
= N_Object_Declaration
5342 and then Needs_Finalization
(Typ
))
5343 or else (Parent_Kind
= N_Assignment_Statement
5344 and then Inside_Init_Proc
)
5346 if Static_Array_Aggregate
(N
)
5347 or else Compile_Time_Known_Aggregate
(N
)
5349 Set_Expansion_Delayed
(N
, False);
5352 Set_Expansion_Delayed
(N
);
5359 -- Look if in place aggregate expansion is possible
5361 -- For object declarations we build the aggregate in place, unless
5362 -- the array is bit-packed or the component is controlled.
5364 -- For assignments we do the assignment in place if all the component
5365 -- associations have compile-time known values. For other cases we
5366 -- create a temporary. The analysis for safety of on-line assignment
5367 -- is delicate, i.e. we don't know how to do it fully yet ???
5369 -- For allocators we assign to the designated object in place if the
5370 -- aggregate meets the same conditions as other in-place assignments.
5371 -- In this case the aggregate may not come from source but was created
5372 -- for default initialization, e.g. with Initialize_Scalars.
5374 if Requires_Transient_Scope
(Typ
) then
5375 Establish_Transient_Scope
5376 (N
, Sec_Stack
=> Has_Controlled_Component
(Typ
));
5379 if Has_Default_Init_Comps
(N
) then
5380 Maybe_In_Place_OK
:= False;
5382 elsif Is_Bit_Packed_Array
(Typ
)
5383 or else Has_Controlled_Component
(Typ
)
5385 Maybe_In_Place_OK
:= False;
5388 Maybe_In_Place_OK
:=
5389 (Nkind
(Parent
(N
)) = N_Assignment_Statement
5390 and then In_Place_Assign_OK
)
5393 (Nkind
(Parent
(Parent
(N
))) = N_Allocator
5394 and then In_Place_Assign_OK
);
5397 -- If this is an array of tasks, it will be expanded into build-in-place
5398 -- assignments. Build an activation chain for the tasks now.
5400 if Has_Task
(Etype
(N
)) then
5401 Build_Activation_Chain_Entity
(N
);
5404 -- Perform in-place expansion of aggregate in an object declaration.
5405 -- Note: actions generated for the aggregate will be captured in an
5406 -- expression-with-actions statement so that they can be transferred
5407 -- to freeze actions later if there is an address clause for the
5408 -- object. (Note: we don't use a block statement because this would
5409 -- cause generated freeze nodes to be elaborated in the wrong scope).
5411 -- Should document these individual tests ???
5413 if not Has_Default_Init_Comps
(N
)
5414 and then Comes_From_Source
(Parent_Node
)
5415 and then Parent_Kind
= N_Object_Declaration
5417 Must_Slide
(Etype
(Defining_Identifier
(Parent_Node
)), Typ
)
5418 and then N
= Expression
(Parent_Node
)
5419 and then not Is_Bit_Packed_Array
(Typ
)
5420 and then not Has_Controlled_Component
(Typ
)
5422 In_Place_Assign_OK_For_Declaration
:= True;
5423 Tmp
:= Defining_Identifier
(Parent
(N
));
5424 Set_No_Initialization
(Parent
(N
));
5425 Set_Expression
(Parent
(N
), Empty
);
5427 -- Set kind and type of the entity, for use in the analysis
5428 -- of the subsequent assignments. If the nominal type is not
5429 -- constrained, build a subtype from the known bounds of the
5430 -- aggregate. If the declaration has a subtype mark, use it,
5431 -- otherwise use the itype of the aggregate.
5433 Set_Ekind
(Tmp
, E_Variable
);
5435 if not Is_Constrained
(Typ
) then
5436 Build_Constrained_Type
(Positional
=> False);
5438 elsif Is_Entity_Name
(Object_Definition
(Parent
(N
)))
5439 and then Is_Constrained
(Entity
(Object_Definition
(Parent
(N
))))
5441 Set_Etype
(Tmp
, Entity
(Object_Definition
(Parent
(N
))));
5444 Set_Size_Known_At_Compile_Time
(Typ
, False);
5445 Set_Etype
(Tmp
, Typ
);
5448 elsif Maybe_In_Place_OK
5449 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
5450 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
5452 Set_Expansion_Delayed
(N
);
5455 -- In the remaining cases the aggregate is the RHS of an assignment
5457 elsif Maybe_In_Place_OK
5458 and then Safe_Left_Hand_Side
(Name
(Parent
(N
)))
5460 Tmp
:= Name
(Parent
(N
));
5462 if Etype
(Tmp
) /= Etype
(N
) then
5463 Apply_Length_Check
(N
, Etype
(Tmp
));
5465 if Nkind
(N
) = N_Raise_Constraint_Error
then
5467 -- Static error, nothing further to expand
5473 -- If a slice assignment has an aggregate with a single others_choice,
5474 -- the assignment can be done in place even if bounds are not static,
5475 -- by converting it into a loop over the discrete range of the slice.
5477 elsif Maybe_In_Place_OK
5478 and then Nkind
(Name
(Parent
(N
))) = N_Slice
5479 and then Is_Others_Aggregate
(N
)
5481 Tmp
:= Name
(Parent
(N
));
5483 -- Set type of aggregate to be type of lhs in assignment, in order
5484 -- to suppress redundant length checks.
5486 Set_Etype
(N
, Etype
(Tmp
));
5490 -- In place aggregate expansion is not possible
5493 Maybe_In_Place_OK
:= False;
5494 Tmp
:= Make_Temporary
(Loc
, 'A', N
);
5496 Make_Object_Declaration
(Loc
,
5497 Defining_Identifier
=> Tmp
,
5498 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
5499 Set_No_Initialization
(Tmp_Decl
, True);
5501 -- If we are within a loop, the temporary will be pushed on the
5502 -- stack at each iteration. If the aggregate is the expression for an
5503 -- allocator, it will be immediately copied to the heap and can
5504 -- be reclaimed at once. We create a transient scope around the
5505 -- aggregate for this purpose.
5507 if Ekind
(Current_Scope
) = E_Loop
5508 and then Nkind
(Parent
(Parent
(N
))) = N_Allocator
5510 Establish_Transient_Scope
(N
, False);
5513 Insert_Action
(N
, Tmp_Decl
);
5516 -- Construct and insert the aggregate code. We can safely suppress index
5517 -- checks because this code is guaranteed not to raise CE on index
5518 -- checks. However we should *not* suppress all checks.
5524 if Nkind
(Tmp
) = N_Defining_Identifier
then
5525 Target
:= New_Occurrence_Of
(Tmp
, Loc
);
5528 if Has_Default_Init_Comps
(N
) then
5530 -- Ada 2005 (AI-287): This case has not been analyzed???
5532 raise Program_Error
;
5535 -- Name in assignment is explicit dereference
5537 Target
:= New_Copy
(Tmp
);
5540 -- If we are to generate an in place assignment for a declaration or
5541 -- an assignment statement, and the assignment can be done directly
5542 -- by the back end, then do not expand further.
5544 -- ??? We can also do that if in place expansion is not possible but
5545 -- then we could go into an infinite recursion.
5547 if (In_Place_Assign_OK_For_Declaration
or else Maybe_In_Place_OK
)
5548 and then not AAMP_On_Target
5549 and then not CodePeer_Mode
5550 and then not Generate_C_Code
5551 and then not Possible_Bit_Aligned_Component
(Target
)
5552 and then not Is_Possibly_Unaligned_Slice
(Target
)
5553 and then Aggr_Assignment_OK_For_Backend
(N
)
5555 if Maybe_In_Place_OK
then
5561 Make_Assignment_Statement
(Loc
,
5563 Expression
=> New_Copy
(N
)));
5567 Build_Array_Aggr_Code
(N
,
5569 Index
=> First_Index
(Typ
),
5571 Scalar_Comp
=> Is_Scalar_Type
(Ctyp
));
5574 -- Save the last assignment statement associated with the aggregate
5575 -- when building a controlled object. This reference is utilized by
5576 -- the finalization machinery when marking an object as successfully
5579 if Needs_Finalization
(Typ
)
5580 and then Is_Entity_Name
(Target
)
5581 and then Present
(Entity
(Target
))
5582 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
5584 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
5588 -- If the aggregate is the expression in a declaration, the expanded
5589 -- code must be inserted after it. The defining entity might not come
5590 -- from source if this is part of an inlined body, but the declaration
5593 if Comes_From_Source
(Tmp
)
5595 (Nkind
(Parent
(N
)) = N_Object_Declaration
5596 and then Comes_From_Source
(Parent
(N
))
5597 and then Tmp
= Defining_Entity
(Parent
(N
)))
5600 Node_After
: constant Node_Id
:= Next
(Parent_Node
);
5603 Insert_Actions_After
(Parent_Node
, Aggr_Code
);
5605 if Parent_Kind
= N_Object_Declaration
then
5606 Collect_Initialization_Statements
5607 (Obj
=> Tmp
, N
=> Parent_Node
, Node_After
=> Node_After
);
5612 Insert_Actions
(N
, Aggr_Code
);
5615 -- If the aggregate has been assigned in place, remove the original
5618 if Nkind
(Parent
(N
)) = N_Assignment_Statement
5619 and then Maybe_In_Place_OK
5621 Rewrite
(Parent
(N
), Make_Null_Statement
(Loc
));
5623 elsif Nkind
(Parent
(N
)) /= N_Object_Declaration
5624 or else Tmp
/= Defining_Identifier
(Parent
(N
))
5626 Rewrite
(N
, New_Occurrence_Of
(Tmp
, Loc
));
5627 Analyze_And_Resolve
(N
, Typ
);
5629 end Expand_Array_Aggregate
;
5631 ------------------------
5632 -- Expand_N_Aggregate --
5633 ------------------------
5635 procedure Expand_N_Aggregate
(N
: Node_Id
) is
5637 -- Record aggregate case
5639 if Is_Record_Type
(Etype
(N
)) then
5640 Expand_Record_Aggregate
(N
);
5642 -- Array aggregate case
5645 -- A special case, if we have a string subtype with bounds 1 .. N,
5646 -- where N is known at compile time, and the aggregate is of the
5647 -- form (others => 'x'), with a single choice and no expressions,
5648 -- and N is less than 80 (an arbitrary limit for now), then replace
5649 -- the aggregate by the equivalent string literal (but do not mark
5650 -- it as static since it is not).
5652 -- Note: this entire circuit is redundant with respect to code in
5653 -- Expand_Array_Aggregate that collapses others choices to positional
5654 -- form, but there are two problems with that circuit:
5656 -- a) It is limited to very small cases due to ill-understood
5657 -- interactions with bootstrapping. That limit is removed by
5658 -- use of the No_Implicit_Loops restriction.
5660 -- b) It incorrectly ends up with the resulting expressions being
5661 -- considered static when they are not. For example, the
5662 -- following test should fail:
5664 -- pragma Restrictions (No_Implicit_Loops);
5665 -- package NonSOthers4 is
5666 -- B : constant String (1 .. 6) := (others => 'A');
5667 -- DH : constant String (1 .. 8) := B & "BB";
5669 -- pragma Export (C, X, Link_Name => DH);
5672 -- But it succeeds (DH looks static to pragma Export)
5674 -- To be sorted out ???
5676 if Present
(Component_Associations
(N
)) then
5678 CA
: constant Node_Id
:= First
(Component_Associations
(N
));
5679 MX
: constant := 80;
5682 if Nkind
(First
(Choices
(CA
))) = N_Others_Choice
5683 and then Nkind
(Expression
(CA
)) = N_Character_Literal
5684 and then No
(Expressions
(N
))
5687 T
: constant Entity_Id
:= Etype
(N
);
5688 X
: constant Node_Id
:= First_Index
(T
);
5689 EC
: constant Node_Id
:= Expression
(CA
);
5690 CV
: constant Uint
:= Char_Literal_Value
(EC
);
5691 CC
: constant Int
:= UI_To_Int
(CV
);
5694 if Nkind
(X
) = N_Range
5695 and then Compile_Time_Known_Value
(Low_Bound
(X
))
5696 and then Expr_Value
(Low_Bound
(X
)) = 1
5697 and then Compile_Time_Known_Value
(High_Bound
(X
))
5700 Hi
: constant Uint
:= Expr_Value
(High_Bound
(X
));
5706 for J
in 1 .. UI_To_Int
(Hi
) loop
5707 Store_String_Char
(Char_Code
(CC
));
5711 Make_String_Literal
(Sloc
(N
),
5712 Strval
=> End_String
));
5714 if CC
>= Int
(2 ** 16) then
5715 Set_Has_Wide_Wide_Character
(N
);
5716 elsif CC
>= Int
(2 ** 8) then
5717 Set_Has_Wide_Character
(N
);
5720 Analyze_And_Resolve
(N
, T
);
5721 Set_Is_Static_Expression
(N
, False);
5731 -- Not that special case, so normal expansion of array aggregate
5733 Expand_Array_Aggregate
(N
);
5737 when RE_Not_Available
=>
5739 end Expand_N_Aggregate
;
5741 ----------------------------------
5742 -- Expand_N_Extension_Aggregate --
5743 ----------------------------------
5745 -- If the ancestor part is an expression, add a component association for
5746 -- the parent field. If the type of the ancestor part is not the direct
5747 -- parent of the expected type, build recursively the needed ancestors.
5748 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5749 -- ration for a temporary of the expected type, followed by individual
5750 -- assignments to the given components.
5752 procedure Expand_N_Extension_Aggregate
(N
: Node_Id
) is
5753 Loc
: constant Source_Ptr
:= Sloc
(N
);
5754 A
: constant Node_Id
:= Ancestor_Part
(N
);
5755 Typ
: constant Entity_Id
:= Etype
(N
);
5758 -- If the ancestor is a subtype mark, an init proc must be called
5759 -- on the resulting object which thus has to be materialized in
5762 if Is_Entity_Name
(A
) and then Is_Type
(Entity
(A
)) then
5763 Convert_To_Assignments
(N
, Typ
);
5765 -- The extension aggregate is transformed into a record aggregate
5766 -- of the following form (c1 and c2 are inherited components)
5768 -- (Exp with c3 => a, c4 => b)
5769 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5774 if Tagged_Type_Expansion
then
5775 Expand_Record_Aggregate
(N
,
5778 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
),
5781 -- No tag is needed in the case of a VM
5784 Expand_Record_Aggregate
(N
, Parent_Expr
=> A
);
5789 when RE_Not_Available
=>
5791 end Expand_N_Extension_Aggregate
;
5793 -----------------------------
5794 -- Expand_Record_Aggregate --
5795 -----------------------------
5797 procedure Expand_Record_Aggregate
5799 Orig_Tag
: Node_Id
:= Empty
;
5800 Parent_Expr
: Node_Id
:= Empty
)
5802 Loc
: constant Source_Ptr
:= Sloc
(N
);
5803 Comps
: constant List_Id
:= Component_Associations
(N
);
5804 Typ
: constant Entity_Id
:= Etype
(N
);
5805 Base_Typ
: constant Entity_Id
:= Base_Type
(Typ
);
5807 Static_Components
: Boolean := True;
5808 -- Flag to indicate whether all components are compile-time known,
5809 -- and the aggregate can be constructed statically and handled by
5812 function Compile_Time_Known_Composite_Value
(N
: Node_Id
) return Boolean;
5813 -- Returns true if N is an expression of composite type which can be
5814 -- fully evaluated at compile time without raising constraint error.
5815 -- Such expressions can be passed as is to Gigi without any expansion.
5817 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
5818 -- set and constants whose expression is such an aggregate, recursively.
5820 function Component_Not_OK_For_Backend
return Boolean;
5821 -- Check for presence of a component which makes it impossible for the
5822 -- backend to process the aggregate, thus requiring the use of a series
5823 -- of assignment statements. Cases checked for are a nested aggregate
5824 -- needing Late_Expansion, the presence of a tagged component which may
5825 -- need tag adjustment, and a bit unaligned component reference.
5827 -- We also force expansion into assignments if a component is of a
5828 -- mutable type (including a private type with discriminants) because
5829 -- in that case the size of the component to be copied may be smaller
5830 -- than the side of the target, and there is no simple way for gigi
5831 -- to compute the size of the object to be copied.
5833 -- NOTE: This is part of the ongoing work to define precisely the
5834 -- interface between front-end and back-end handling of aggregates.
5835 -- In general it is desirable to pass aggregates as they are to gigi,
5836 -- in order to minimize elaboration code. This is one case where the
5837 -- semantics of Ada complicate the analysis and lead to anomalies in
5838 -- the gcc back-end if the aggregate is not expanded into assignments.
5840 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean;
5841 -- If any ancestor of the current type is private, the aggregate
5842 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
5843 -- because it will not be set when type and its parent are in the
5844 -- same scope, and the parent component needs expansion.
5846 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
;
5847 -- For nested aggregates return the ultimate enclosing aggregate; for
5848 -- non-nested aggregates return N.
5850 ----------------------------------------
5851 -- Compile_Time_Known_Composite_Value --
5852 ----------------------------------------
5854 function Compile_Time_Known_Composite_Value
5855 (N
: Node_Id
) return Boolean
5858 -- If we have an entity name, then see if it is the name of a
5859 -- constant and if so, test the corresponding constant value.
5861 if Is_Entity_Name
(N
) then
5863 E
: constant Entity_Id
:= Entity
(N
);
5866 if Ekind
(E
) /= E_Constant
then
5869 V
:= Constant_Value
(E
);
5871 and then Compile_Time_Known_Composite_Value
(V
);
5875 -- We have a value, see if it is compile time known
5878 if Nkind
(N
) = N_Aggregate
then
5879 return Compile_Time_Known_Aggregate
(N
);
5882 -- All other types of values are not known at compile time
5887 end Compile_Time_Known_Composite_Value
;
5889 ----------------------------------
5890 -- Component_Not_OK_For_Backend --
5891 ----------------------------------
5893 function Component_Not_OK_For_Backend
return Boolean is
5903 while Present
(C
) loop
5905 -- If the component has box initialization, expansion is needed
5906 -- and component is not ready for backend.
5908 if Box_Present
(C
) then
5912 if Nkind
(Expression
(C
)) = N_Qualified_Expression
then
5913 Expr_Q
:= Expression
(Expression
(C
));
5915 Expr_Q
:= Expression
(C
);
5918 -- Return true if the aggregate has any associations for tagged
5919 -- components that may require tag adjustment.
5921 -- These are cases where the source expression may have a tag that
5922 -- could differ from the component tag (e.g., can occur for type
5923 -- conversions and formal parameters). (Tag adjustment not needed
5924 -- if Tagged_Type_Expansion because object tags are implicit in
5927 if Is_Tagged_Type
(Etype
(Expr_Q
))
5928 and then (Nkind
(Expr_Q
) = N_Type_Conversion
5929 or else (Is_Entity_Name
(Expr_Q
)
5931 Ekind
(Entity
(Expr_Q
)) in Formal_Kind
))
5932 and then Tagged_Type_Expansion
5934 Static_Components
:= False;
5937 elsif Is_Delayed_Aggregate
(Expr_Q
) then
5938 Static_Components
:= False;
5941 elsif Possible_Bit_Aligned_Component
(Expr_Q
) then
5942 Static_Components
:= False;
5946 if Is_Elementary_Type
(Etype
(Expr_Q
)) then
5947 if not Compile_Time_Known_Value
(Expr_Q
) then
5948 Static_Components
:= False;
5951 elsif not Compile_Time_Known_Composite_Value
(Expr_Q
) then
5952 Static_Components
:= False;
5954 if Is_Private_Type
(Etype
(Expr_Q
))
5955 and then Has_Discriminants
(Etype
(Expr_Q
))
5965 end Component_Not_OK_For_Backend
;
5967 -----------------------------------
5968 -- Has_Visible_Private_Ancestor --
5969 -----------------------------------
5971 function Has_Visible_Private_Ancestor
(Id
: E
) return Boolean is
5972 R
: constant Entity_Id
:= Root_Type
(Id
);
5973 T1
: Entity_Id
:= Id
;
5977 if Is_Private_Type
(T1
) then
5987 end Has_Visible_Private_Ancestor
;
5989 -------------------------
5990 -- Top_Level_Aggregate --
5991 -------------------------
5993 function Top_Level_Aggregate
(N
: Node_Id
) return Node_Id
is
5998 while Present
(Parent
(Aggr
))
5999 and then Nkind_In
(Parent
(Aggr
), N_Component_Association
,
6002 Aggr
:= Parent
(Aggr
);
6006 end Top_Level_Aggregate
;
6010 Top_Level_Aggr
: constant Node_Id
:= Top_Level_Aggregate
(N
);
6011 Tag_Value
: Node_Id
;
6015 -- Start of processing for Expand_Record_Aggregate
6018 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
6019 -- to prevent a piecemeal assignment even if the aggregate is to be
6020 -- expanded. We create a temporary for the aggregate, and assign the
6021 -- temporary instead, so that the back end can generate an atomic move
6024 if Is_Atomic_VFA_Aggregate
(N
) then
6027 -- No special management required for aggregates used to initialize
6028 -- statically allocated dispatch tables
6030 elsif Is_Static_Dispatch_Table_Aggregate
(N
) then
6034 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
6035 -- are build-in-place function calls. The assignments will each turn
6036 -- into a build-in-place function call. If components are all static,
6037 -- we can pass the aggregate to the backend regardless of limitedness.
6039 -- Extension aggregates, aggregates in extended return statements, and
6040 -- aggregates for C++ imported types must be expanded.
6042 if Ada_Version
>= Ada_2005
and then Is_Limited_View
(Typ
) then
6043 if not Nkind_In
(Parent
(N
), N_Object_Declaration
,
6044 N_Component_Association
)
6046 Convert_To_Assignments
(N
, Typ
);
6048 elsif Nkind
(N
) = N_Extension_Aggregate
6049 or else Convention
(Typ
) = Convention_CPP
6051 Convert_To_Assignments
(N
, Typ
);
6053 elsif not Size_Known_At_Compile_Time
(Typ
)
6054 or else Component_Not_OK_For_Backend
6055 or else not Static_Components
6057 Convert_To_Assignments
(N
, Typ
);
6060 Set_Compile_Time_Known_Aggregate
(N
);
6061 Set_Expansion_Delayed
(N
, False);
6064 -- Gigi doesn't properly handle temporaries of variable size so we
6065 -- generate it in the front-end
6067 elsif not Size_Known_At_Compile_Time
(Typ
)
6068 and then Tagged_Type_Expansion
6070 Convert_To_Assignments
(N
, Typ
);
6072 -- An aggregate used to initialize a controlled object must be turned
6073 -- into component assignments as the components themselves may require
6074 -- finalization actions such as adjustment.
6076 elsif Needs_Finalization
(Typ
) then
6077 Convert_To_Assignments
(N
, Typ
);
6079 -- Ada 2005 (AI-287): In case of default initialized components we
6080 -- convert the aggregate into assignments.
6082 elsif Has_Default_Init_Comps
(N
) then
6083 Convert_To_Assignments
(N
, Typ
);
6087 elsif Component_Not_OK_For_Backend
then
6088 Convert_To_Assignments
(N
, Typ
);
6090 -- If an ancestor is private, some components are not inherited and we
6091 -- cannot expand into a record aggregate.
6093 elsif Has_Visible_Private_Ancestor
(Typ
) then
6094 Convert_To_Assignments
(N
, Typ
);
6096 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
6097 -- is not able to handle the aggregate for Late_Request.
6099 elsif Is_Tagged_Type
(Typ
) and then Has_Discriminants
(Typ
) then
6100 Convert_To_Assignments
(N
, Typ
);
6102 -- If the tagged types covers interface types we need to initialize all
6103 -- hidden components containing pointers to secondary dispatch tables.
6105 elsif Is_Tagged_Type
(Typ
) and then Has_Interfaces
(Typ
) then
6106 Convert_To_Assignments
(N
, Typ
);
6108 -- If some components are mutable, the size of the aggregate component
6109 -- may be distinct from the default size of the type component, so
6110 -- we need to expand to insure that the back-end copies the proper
6111 -- size of the data. However, if the aggregate is the initial value of
6112 -- a constant, the target is immutable and might be built statically
6113 -- if components are appropriate.
6115 elsif Has_Mutable_Components
(Typ
)
6117 (Nkind
(Parent
(Top_Level_Aggr
)) /= N_Object_Declaration
6118 or else not Constant_Present
(Parent
(Top_Level_Aggr
))
6119 or else not Static_Components
)
6121 Convert_To_Assignments
(N
, Typ
);
6123 -- If the type involved has bit aligned components, then we are not sure
6124 -- that the back end can handle this case correctly.
6126 elsif Type_May_Have_Bit_Aligned_Components
(Typ
) then
6127 Convert_To_Assignments
(N
, Typ
);
6129 -- When generating C, only generate an aggregate when declaring objects
6130 -- since C does not support aggregates in e.g. assignment statements.
6132 elsif Modify_Tree_For_C
and then not In_Object_Declaration
(N
) then
6133 Convert_To_Assignments
(N
, Typ
);
6135 -- In all other cases, build a proper aggregate to be handled by gigi
6138 if Nkind
(N
) = N_Aggregate
then
6140 -- If the aggregate is static and can be handled by the back-end,
6141 -- nothing left to do.
6143 if Static_Components
then
6144 Set_Compile_Time_Known_Aggregate
(N
);
6145 Set_Expansion_Delayed
(N
, False);
6149 -- If no discriminants, nothing special to do
6151 if not Has_Discriminants
(Typ
) then
6154 -- Case of discriminants present
6156 elsif Is_Derived_Type
(Typ
) then
6158 -- For untagged types, non-stored discriminants are replaced
6159 -- with stored discriminants, which are the ones that gigi uses
6160 -- to describe the type and its components.
6162 Generate_Aggregate_For_Derived_Type
: declare
6163 Constraints
: constant List_Id
:= New_List
;
6164 First_Comp
: Node_Id
;
6165 Discriminant
: Entity_Id
;
6167 Num_Disc
: Int
:= 0;
6168 Num_Gird
: Int
:= 0;
6170 procedure Prepend_Stored_Values
(T
: Entity_Id
);
6171 -- Scan the list of stored discriminants of the type, and add
6172 -- their values to the aggregate being built.
6174 ---------------------------
6175 -- Prepend_Stored_Values --
6176 ---------------------------
6178 procedure Prepend_Stored_Values
(T
: Entity_Id
) is
6180 Discriminant
:= First_Stored_Discriminant
(T
);
6181 while Present
(Discriminant
) loop
6183 Make_Component_Association
(Loc
,
6185 New_List
(New_Occurrence_Of
(Discriminant
, Loc
)),
6189 (Get_Discriminant_Value
6192 Discriminant_Constraint
(Typ
))));
6194 if No
(First_Comp
) then
6195 Prepend_To
(Component_Associations
(N
), New_Comp
);
6197 Insert_After
(First_Comp
, New_Comp
);
6200 First_Comp
:= New_Comp
;
6201 Next_Stored_Discriminant
(Discriminant
);
6203 end Prepend_Stored_Values
;
6205 -- Start of processing for Generate_Aggregate_For_Derived_Type
6208 -- Remove the associations for the discriminant of derived type
6210 First_Comp
:= First
(Component_Associations
(N
));
6211 while Present
(First_Comp
) loop
6215 if Ekind
(Entity
(First
(Choices
(Comp
)))) = E_Discriminant
6218 Num_Disc
:= Num_Disc
+ 1;
6222 -- Insert stored discriminant associations in the correct
6223 -- order. If there are more stored discriminants than new
6224 -- discriminants, there is at least one new discriminant that
6225 -- constrains more than one of the stored discriminants. In
6226 -- this case we need to construct a proper subtype of the
6227 -- parent type, in order to supply values to all the
6228 -- components. Otherwise there is one-one correspondence
6229 -- between the constraints and the stored discriminants.
6231 First_Comp
:= Empty
;
6233 Discriminant
:= First_Stored_Discriminant
(Base_Type
(Typ
));
6234 while Present
(Discriminant
) loop
6235 Num_Gird
:= Num_Gird
+ 1;
6236 Next_Stored_Discriminant
(Discriminant
);
6239 -- Case of more stored discriminants than new discriminants
6241 if Num_Gird
> Num_Disc
then
6243 -- Create a proper subtype of the parent type, which is the
6244 -- proper implementation type for the aggregate, and convert
6245 -- it to the intended target type.
6247 Discriminant
:= First_Stored_Discriminant
(Base_Type
(Typ
));
6248 while Present
(Discriminant
) loop
6251 (Get_Discriminant_Value
6254 Discriminant_Constraint
(Typ
)));
6255 Append
(New_Comp
, Constraints
);
6256 Next_Stored_Discriminant
(Discriminant
);
6260 Make_Subtype_Declaration
(Loc
,
6261 Defining_Identifier
=> Make_Temporary
(Loc
, 'T'),
6262 Subtype_Indication
=>
6263 Make_Subtype_Indication
(Loc
,
6265 New_Occurrence_Of
(Etype
(Base_Type
(Typ
)), Loc
),
6267 Make_Index_Or_Discriminant_Constraint
6268 (Loc
, Constraints
)));
6270 Insert_Action
(N
, Decl
);
6271 Prepend_Stored_Values
(Base_Type
(Typ
));
6273 Set_Etype
(N
, Defining_Identifier
(Decl
));
6276 Rewrite
(N
, Unchecked_Convert_To
(Typ
, N
));
6279 -- Case where we do not have fewer new discriminants than
6280 -- stored discriminants, so in this case we can simply use the
6281 -- stored discriminants of the subtype.
6284 Prepend_Stored_Values
(Typ
);
6286 end Generate_Aggregate_For_Derived_Type
;
6289 if Is_Tagged_Type
(Typ
) then
6291 -- In the tagged case, _parent and _tag component must be created
6293 -- Reset Null_Present unconditionally. Tagged records always have
6294 -- at least one field (the tag or the parent).
6296 Set_Null_Record_Present
(N
, False);
6298 -- When the current aggregate comes from the expansion of an
6299 -- extension aggregate, the parent expr is replaced by an
6300 -- aggregate formed by selected components of this expr.
6302 if Present
(Parent_Expr
) and then Is_Empty_List
(Comps
) then
6303 Comp
:= First_Component_Or_Discriminant
(Typ
);
6304 while Present
(Comp
) loop
6306 -- Skip all expander-generated components
6308 if not Comes_From_Source
(Original_Record_Component
(Comp
))
6314 Make_Selected_Component
(Loc
,
6316 Unchecked_Convert_To
(Typ
,
6317 Duplicate_Subexpr
(Parent_Expr
, True)),
6318 Selector_Name
=> New_Occurrence_Of
(Comp
, Loc
));
6321 Make_Component_Association
(Loc
,
6323 New_List
(New_Occurrence_Of
(Comp
, Loc
)),
6324 Expression
=> New_Comp
));
6326 Analyze_And_Resolve
(New_Comp
, Etype
(Comp
));
6329 Next_Component_Or_Discriminant
(Comp
);
6333 -- Compute the value for the Tag now, if the type is a root it
6334 -- will be included in the aggregate right away, otherwise it will
6335 -- be propagated to the parent aggregate.
6337 if Present
(Orig_Tag
) then
6338 Tag_Value
:= Orig_Tag
;
6339 elsif not Tagged_Type_Expansion
then
6344 (Node
(First_Elmt
(Access_Disp_Table
(Typ
))), Loc
);
6347 -- For a derived type, an aggregate for the parent is formed with
6348 -- all the inherited components.
6350 if Is_Derived_Type
(Typ
) then
6353 First_Comp
: Node_Id
;
6354 Parent_Comps
: List_Id
;
6355 Parent_Aggr
: Node_Id
;
6356 Parent_Name
: Node_Id
;
6359 -- Remove the inherited component association from the
6360 -- aggregate and store them in the parent aggregate
6362 First_Comp
:= First
(Component_Associations
(N
));
6363 Parent_Comps
:= New_List
;
6364 while Present
(First_Comp
)
6366 Scope
(Original_Record_Component
6367 (Entity
(First
(Choices
(First_Comp
))))) /=
6373 Append
(Comp
, Parent_Comps
);
6377 Make_Aggregate
(Loc
,
6378 Component_Associations
=> Parent_Comps
);
6379 Set_Etype
(Parent_Aggr
, Etype
(Base_Type
(Typ
)));
6381 -- Find the _parent component
6383 Comp
:= First_Component
(Typ
);
6384 while Chars
(Comp
) /= Name_uParent
loop
6385 Comp
:= Next_Component
(Comp
);
6388 Parent_Name
:= New_Occurrence_Of
(Comp
, Loc
);
6390 -- Insert the parent aggregate
6392 Prepend_To
(Component_Associations
(N
),
6393 Make_Component_Association
(Loc
,
6394 Choices
=> New_List
(Parent_Name
),
6395 Expression
=> Parent_Aggr
));
6397 -- Expand recursively the parent propagating the right Tag
6399 Expand_Record_Aggregate
6400 (Parent_Aggr
, Tag_Value
, Parent_Expr
);
6402 -- The ancestor part may be a nested aggregate that has
6403 -- delayed expansion: recheck now.
6405 if Component_Not_OK_For_Backend
then
6406 Convert_To_Assignments
(N
, Typ
);
6410 -- For a root type, the tag component is added (unless compiling
6411 -- for the VMs, where tags are implicit).
6413 elsif Tagged_Type_Expansion
then
6415 Tag_Name
: constant Node_Id
:=
6416 New_Occurrence_Of
(First_Tag_Component
(Typ
), Loc
);
6417 Typ_Tag
: constant Entity_Id
:= RTE
(RE_Tag
);
6418 Conv_Node
: constant Node_Id
:=
6419 Unchecked_Convert_To
(Typ_Tag
, Tag_Value
);
6422 Set_Etype
(Conv_Node
, Typ_Tag
);
6423 Prepend_To
(Component_Associations
(N
),
6424 Make_Component_Association
(Loc
,
6425 Choices
=> New_List
(Tag_Name
),
6426 Expression
=> Conv_Node
));
6432 end Expand_Record_Aggregate
;
6434 ----------------------------
6435 -- Has_Default_Init_Comps --
6436 ----------------------------
6438 function Has_Default_Init_Comps
(N
: Node_Id
) return Boolean is
6439 Comps
: constant List_Id
:= Component_Associations
(N
);
6444 pragma Assert
(Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
));
6450 if Has_Self_Reference
(N
) then
6454 -- Check if any direct component has default initialized components
6457 while Present
(C
) loop
6458 if Box_Present
(C
) then
6465 -- Recursive call in case of aggregate expression
6468 while Present
(C
) loop
6469 Expr
:= Expression
(C
);
6472 and then Nkind_In
(Expr
, N_Aggregate
, N_Extension_Aggregate
)
6473 and then Has_Default_Init_Comps
(Expr
)
6482 end Has_Default_Init_Comps
;
6484 --------------------------
6485 -- Is_Delayed_Aggregate --
6486 --------------------------
6488 function Is_Delayed_Aggregate
(N
: Node_Id
) return Boolean is
6489 Node
: Node_Id
:= N
;
6490 Kind
: Node_Kind
:= Nkind
(Node
);
6493 if Kind
= N_Qualified_Expression
then
6494 Node
:= Expression
(Node
);
6495 Kind
:= Nkind
(Node
);
6498 if not Nkind_In
(Kind
, N_Aggregate
, N_Extension_Aggregate
) then
6501 return Expansion_Delayed
(Node
);
6503 end Is_Delayed_Aggregate
;
6505 ---------------------------
6506 -- In_Object_Declaration --
6507 ---------------------------
6509 function In_Object_Declaration
(N
: Node_Id
) return Boolean is
6510 P
: Node_Id
:= Parent
(N
);
6512 while Present
(P
) loop
6513 if Nkind
(P
) = N_Object_Declaration
then
6521 end In_Object_Declaration
;
6523 ----------------------------------------
6524 -- Is_Static_Dispatch_Table_Aggregate --
6525 ----------------------------------------
6527 function Is_Static_Dispatch_Table_Aggregate
(N
: Node_Id
) return Boolean is
6528 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
6531 return Static_Dispatch_Tables
6532 and then Tagged_Type_Expansion
6533 and then RTU_Loaded
(Ada_Tags
)
6535 -- Avoid circularity when rebuilding the compiler
6537 and then Cunit_Entity
(Get_Source_Unit
(N
)) /= RTU_Entity
(Ada_Tags
)
6538 and then (Typ
= RTE
(RE_Dispatch_Table_Wrapper
)
6540 Typ
= RTE
(RE_Address_Array
)
6542 Typ
= RTE
(RE_Type_Specific_Data
)
6544 Typ
= RTE
(RE_Tag_Table
)
6546 (RTE_Available
(RE_Interface_Data
)
6547 and then Typ
= RTE
(RE_Interface_Data
))
6549 (RTE_Available
(RE_Interfaces_Array
)
6550 and then Typ
= RTE
(RE_Interfaces_Array
))
6552 (RTE_Available
(RE_Interface_Data_Element
)
6553 and then Typ
= RTE
(RE_Interface_Data_Element
)));
6554 end Is_Static_Dispatch_Table_Aggregate
;
6556 -----------------------------
6557 -- Is_Two_Dim_Packed_Array --
6558 -----------------------------
6560 function Is_Two_Dim_Packed_Array
(Typ
: Entity_Id
) return Boolean is
6561 C
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
6563 return Number_Dimensions
(Typ
) = 2
6564 and then Is_Bit_Packed_Array
(Typ
)
6565 and then (C
= 1 or else C
= 2 or else C
= 4);
6566 end Is_Two_Dim_Packed_Array
;
6568 --------------------
6569 -- Late_Expansion --
6570 --------------------
6572 function Late_Expansion
6575 Target
: Node_Id
) return List_Id
6577 Aggr_Code
: List_Id
;
6580 if Is_Array_Type
(Etype
(N
)) then
6582 Build_Array_Aggr_Code
6584 Ctype
=> Component_Type
(Etype
(N
)),
6585 Index
=> First_Index
(Typ
),
6587 Scalar_Comp
=> Is_Scalar_Type
(Component_Type
(Typ
)),
6588 Indexes
=> No_List
);
6590 -- Directly or indirectly (e.g. access protected procedure) a record
6593 Aggr_Code
:= Build_Record_Aggr_Code
(N
, Typ
, Target
);
6596 -- Save the last assignment statement associated with the aggregate
6597 -- when building a controlled object. This reference is utilized by
6598 -- the finalization machinery when marking an object as successfully
6601 if Needs_Finalization
(Typ
)
6602 and then Is_Entity_Name
(Target
)
6603 and then Present
(Entity
(Target
))
6604 and then Ekind_In
(Entity
(Target
), E_Constant
, E_Variable
)
6606 Set_Last_Aggregate_Assignment
(Entity
(Target
), Last
(Aggr_Code
));
6612 ----------------------------------
6613 -- Make_OK_Assignment_Statement --
6614 ----------------------------------
6616 function Make_OK_Assignment_Statement
6619 Expression
: Node_Id
) return Node_Id
6622 Set_Assignment_OK
(Name
);
6623 return Make_Assignment_Statement
(Sloc
, Name
, Expression
);
6624 end Make_OK_Assignment_Statement
;
6626 -----------------------
6627 -- Number_Of_Choices --
6628 -----------------------
6630 function Number_Of_Choices
(N
: Node_Id
) return Nat
is
6634 Nb_Choices
: Nat
:= 0;
6637 if Present
(Expressions
(N
)) then
6641 Assoc
:= First
(Component_Associations
(N
));
6642 while Present
(Assoc
) loop
6643 Choice
:= First
(Choices
(Assoc
));
6644 while Present
(Choice
) loop
6645 if Nkind
(Choice
) /= N_Others_Choice
then
6646 Nb_Choices
:= Nb_Choices
+ 1;
6656 end Number_Of_Choices
;
6658 ------------------------------------
6659 -- Packed_Array_Aggregate_Handled --
6660 ------------------------------------
6662 -- The current version of this procedure will handle at compile time
6663 -- any array aggregate that meets these conditions:
6665 -- One and two dimensional, bit packed
6666 -- Underlying packed type is modular type
6667 -- Bounds are within 32-bit Int range
6668 -- All bounds and values are static
6670 -- Note: for now, in the 2-D case, we only handle component sizes of
6671 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
6673 function Packed_Array_Aggregate_Handled
(N
: Node_Id
) return Boolean is
6674 Loc
: constant Source_Ptr
:= Sloc
(N
);
6675 Typ
: constant Entity_Id
:= Etype
(N
);
6676 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
6678 Not_Handled
: exception;
6679 -- Exception raised if this aggregate cannot be handled
6682 -- Handle one- or two dimensional bit packed array
6684 if not Is_Bit_Packed_Array
(Typ
)
6685 or else Number_Dimensions
(Typ
) > 2
6690 -- If two-dimensional, check whether it can be folded, and transformed
6691 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
6692 -- the original type.
6694 if Number_Dimensions
(Typ
) = 2 then
6695 return Two_Dim_Packed_Array_Handled
(N
);
6698 if not Is_Modular_Integer_Type
(Packed_Array_Impl_Type
(Typ
)) then
6702 if not Is_Scalar_Type
(Component_Type
(Typ
))
6703 and then Has_Non_Standard_Rep
(Component_Type
(Typ
))
6709 Csiz
: constant Nat
:= UI_To_Int
(Component_Size
(Typ
));
6713 -- Bounds of index type
6717 -- Values of bounds if compile time known
6719 function Get_Component_Val
(N
: Node_Id
) return Uint
;
6720 -- Given a expression value N of the component type Ctyp, returns a
6721 -- value of Csiz (component size) bits representing this value. If
6722 -- the value is non-static or any other reason exists why the value
6723 -- cannot be returned, then Not_Handled is raised.
6725 -----------------------
6726 -- Get_Component_Val --
6727 -----------------------
6729 function Get_Component_Val
(N
: Node_Id
) return Uint
is
6733 -- We have to analyze the expression here before doing any further
6734 -- processing here. The analysis of such expressions is deferred
6735 -- till expansion to prevent some problems of premature analysis.
6737 Analyze_And_Resolve
(N
, Ctyp
);
6739 -- Must have a compile time value. String literals have to be
6740 -- converted into temporaries as well, because they cannot easily
6741 -- be converted into their bit representation.
6743 if not Compile_Time_Known_Value
(N
)
6744 or else Nkind
(N
) = N_String_Literal
6749 Val
:= Expr_Rep_Value
(N
);
6751 -- Adjust for bias, and strip proper number of bits
6753 if Has_Biased_Representation
(Ctyp
) then
6754 Val
:= Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
6757 return Val
mod Uint_2
** Csiz
;
6758 end Get_Component_Val
;
6760 -- Here we know we have a one dimensional bit packed array
6763 Get_Index_Bounds
(First_Index
(Typ
), Lo
, Hi
);
6765 -- Cannot do anything if bounds are dynamic
6767 if not Compile_Time_Known_Value
(Lo
)
6769 not Compile_Time_Known_Value
(Hi
)
6774 -- Or are silly out of range of int bounds
6776 Lob
:= Expr_Value
(Lo
);
6777 Hib
:= Expr_Value
(Hi
);
6779 if not UI_Is_In_Int_Range
(Lob
)
6781 not UI_Is_In_Int_Range
(Hib
)
6786 -- At this stage we have a suitable aggregate for handling at compile
6787 -- time. The only remaining checks are that the values of expressions
6788 -- in the aggregate are compile-time known (checks are performed by
6789 -- Get_Component_Val), and that any subtypes or ranges are statically
6792 -- If the aggregate is not fully positional at this stage, then
6793 -- convert it to positional form. Either this will fail, in which
6794 -- case we can do nothing, or it will succeed, in which case we have
6795 -- succeeded in handling the aggregate and transforming it into a
6796 -- modular value, or it will stay an aggregate, in which case we
6797 -- have failed to create a packed value for it.
6799 if Present
(Component_Associations
(N
)) then
6800 Convert_To_Positional
6801 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
6802 return Nkind
(N
) /= N_Aggregate
;
6805 -- Otherwise we are all positional, so convert to proper value
6808 Lov
: constant Int
:= UI_To_Int
(Lob
);
6809 Hiv
: constant Int
:= UI_To_Int
(Hib
);
6811 Len
: constant Nat
:= Int
'Max (0, Hiv
- Lov
+ 1);
6812 -- The length of the array (number of elements)
6814 Aggregate_Val
: Uint
;
6815 -- Value of aggregate. The value is set in the low order bits of
6816 -- this value. For the little-endian case, the values are stored
6817 -- from low-order to high-order and for the big-endian case the
6818 -- values are stored from high-order to low-order. Note that gigi
6819 -- will take care of the conversions to left justify the value in
6820 -- the big endian case (because of left justified modular type
6821 -- processing), so we do not have to worry about that here.
6824 -- Integer literal for resulting constructed value
6827 -- Shift count from low order for next value
6830 -- Shift increment for loop
6833 -- Next expression from positional parameters of aggregate
6835 Left_Justified
: Boolean;
6836 -- Set True if we are filling the high order bits of the target
6837 -- value (i.e. the value is left justified).
6840 -- For little endian, we fill up the low order bits of the target
6841 -- value. For big endian we fill up the high order bits of the
6842 -- target value (which is a left justified modular value).
6844 Left_Justified
:= Bytes_Big_Endian
;
6846 -- Switch justification if using -gnatd8
6848 if Debug_Flag_8
then
6849 Left_Justified
:= not Left_Justified
;
6852 -- Switch justfification if reverse storage order
6854 if Reverse_Storage_Order
(Base_Type
(Typ
)) then
6855 Left_Justified
:= not Left_Justified
;
6858 if Left_Justified
then
6859 Shift
:= Csiz
* (Len
- 1);
6866 -- Loop to set the values
6869 Aggregate_Val
:= Uint_0
;
6871 Expr
:= First
(Expressions
(N
));
6872 Aggregate_Val
:= Get_Component_Val
(Expr
) * Uint_2
** Shift
;
6874 for J
in 2 .. Len
loop
6875 Shift
:= Shift
+ Incr
;
6878 Aggregate_Val
+ Get_Component_Val
(Expr
) * Uint_2
** Shift
;
6882 -- Now we can rewrite with the proper value
6884 Lit
:= Make_Integer_Literal
(Loc
, Intval
=> Aggregate_Val
);
6885 Set_Print_In_Hex
(Lit
);
6887 -- Construct the expression using this literal. Note that it is
6888 -- important to qualify the literal with its proper modular type
6889 -- since universal integer does not have the required range and
6890 -- also this is a left justified modular type, which is important
6891 -- in the big-endian case.
6894 Unchecked_Convert_To
(Typ
,
6895 Make_Qualified_Expression
(Loc
,
6897 New_Occurrence_Of
(Packed_Array_Impl_Type
(Typ
), Loc
),
6898 Expression
=> Lit
)));
6900 Analyze_And_Resolve
(N
, Typ
);
6908 end Packed_Array_Aggregate_Handled
;
6910 ----------------------------
6911 -- Has_Mutable_Components --
6912 ----------------------------
6914 function Has_Mutable_Components
(Typ
: Entity_Id
) return Boolean is
6918 Comp
:= First_Component
(Typ
);
6919 while Present
(Comp
) loop
6920 if Is_Record_Type
(Etype
(Comp
))
6921 and then Has_Discriminants
(Etype
(Comp
))
6922 and then not Is_Constrained
(Etype
(Comp
))
6927 Next_Component
(Comp
);
6931 end Has_Mutable_Components
;
6933 ------------------------------
6934 -- Initialize_Discriminants --
6935 ------------------------------
6937 procedure Initialize_Discriminants
(N
: Node_Id
; Typ
: Entity_Id
) is
6938 Loc
: constant Source_Ptr
:= Sloc
(N
);
6939 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
6940 Par
: constant Entity_Id
:= Etype
(Bas
);
6941 Decl
: constant Node_Id
:= Parent
(Par
);
6945 if Is_Tagged_Type
(Bas
)
6946 and then Is_Derived_Type
(Bas
)
6947 and then Has_Discriminants
(Par
)
6948 and then Has_Discriminants
(Bas
)
6949 and then Number_Discriminants
(Bas
) /= Number_Discriminants
(Par
)
6950 and then Nkind
(Decl
) = N_Full_Type_Declaration
6951 and then Nkind
(Type_Definition
(Decl
)) = N_Record_Definition
6953 Present
(Variant_Part
(Component_List
(Type_Definition
(Decl
))))
6954 and then Nkind
(N
) /= N_Extension_Aggregate
6957 -- Call init proc to set discriminants.
6958 -- There should eventually be a special procedure for this ???
6960 Ref
:= New_Occurrence_Of
(Defining_Identifier
(N
), Loc
);
6961 Insert_Actions_After
(N
,
6962 Build_Initialization_Call
(Sloc
(N
), Ref
, Typ
));
6964 end Initialize_Discriminants
;
6971 (Obj_Type
: Entity_Id
;
6972 Typ
: Entity_Id
) return Boolean
6974 L1
, L2
, H1
, H2
: Node_Id
;
6977 -- No sliding if the type of the object is not established yet, if it is
6978 -- an unconstrained type whose actual subtype comes from the aggregate,
6979 -- or if the two types are identical.
6981 if not Is_Array_Type
(Obj_Type
) then
6984 elsif not Is_Constrained
(Obj_Type
) then
6987 elsif Typ
= Obj_Type
then
6991 -- Sliding can only occur along the first dimension
6993 Get_Index_Bounds
(First_Index
(Typ
), L1
, H1
);
6994 Get_Index_Bounds
(First_Index
(Obj_Type
), L2
, H2
);
6996 if not Is_OK_Static_Expression
(L1
) or else
6997 not Is_OK_Static_Expression
(L2
) or else
6998 not Is_OK_Static_Expression
(H1
) or else
6999 not Is_OK_Static_Expression
(H2
)
7003 return Expr_Value
(L1
) /= Expr_Value
(L2
)
7005 Expr_Value
(H1
) /= Expr_Value
(H2
);
7010 ----------------------------------
7011 -- Two_Dim_Packed_Array_Handled --
7012 ----------------------------------
7014 function Two_Dim_Packed_Array_Handled
(N
: Node_Id
) return Boolean is
7015 Loc
: constant Source_Ptr
:= Sloc
(N
);
7016 Typ
: constant Entity_Id
:= Etype
(N
);
7017 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
7018 Comp_Size
: constant Int
:= UI_To_Int
(Component_Size
(Typ
));
7019 Packed_Array
: constant Entity_Id
:=
7020 Packed_Array_Impl_Type
(Base_Type
(Typ
));
7023 -- Expression in original aggregate
7026 -- One-dimensional subaggregate
7030 -- For now, only deal with cases where an integral number of elements
7031 -- fit in a single byte. This includes the most common boolean case.
7033 if not (Comp_Size
= 1 or else
7034 Comp_Size
= 2 or else
7040 Convert_To_Positional
7041 (N
, Max_Others_Replicate
=> 64, Handle_Bit_Packed
=> True);
7043 -- Verify that all components are static
7045 if Nkind
(N
) = N_Aggregate
7046 and then Compile_Time_Known_Aggregate
(N
)
7050 -- The aggregate may have been re-analyzed and converted already
7052 elsif Nkind
(N
) /= N_Aggregate
then
7055 -- If component associations remain, the aggregate is not static
7057 elsif Present
(Component_Associations
(N
)) then
7061 One_Dim
:= First
(Expressions
(N
));
7062 while Present
(One_Dim
) loop
7063 if Present
(Component_Associations
(One_Dim
)) then
7067 One_Comp
:= First
(Expressions
(One_Dim
));
7068 while Present
(One_Comp
) loop
7069 if not Is_OK_Static_Expression
(One_Comp
) then
7080 -- Two-dimensional aggregate is now fully positional so pack one
7081 -- dimension to create a static one-dimensional array, and rewrite
7082 -- as an unchecked conversion to the original type.
7085 Byte_Size
: constant Int
:= UI_To_Int
(Component_Size
(Packed_Array
));
7086 -- The packed array type is a byte array
7089 -- Number of components accumulated in current byte
7092 -- Assembled list of packed values for equivalent aggregate
7095 -- integer value of component
7098 -- Step size for packing
7101 -- Endian-dependent start position for packing
7104 -- Current insertion position
7107 -- Component of packed array being assembled.
7114 -- Account for endianness. See corresponding comment in
7115 -- Packed_Array_Aggregate_Handled concerning the following.
7119 xor Reverse_Storage_Order
(Base_Type
(Typ
))
7121 Init_Shift
:= Byte_Size
- Comp_Size
;
7128 -- Iterate over each subaggregate
7130 Shift
:= Init_Shift
;
7131 One_Dim
:= First
(Expressions
(N
));
7132 while Present
(One_Dim
) loop
7133 One_Comp
:= First
(Expressions
(One_Dim
));
7134 while Present
(One_Comp
) loop
7135 if Packed_Num
= Byte_Size
/ Comp_Size
then
7137 -- Byte is complete, add to list of expressions
7139 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
7141 Shift
:= Init_Shift
;
7145 Comp_Val
:= Expr_Rep_Value
(One_Comp
);
7147 -- Adjust for bias, and strip proper number of bits
7149 if Has_Biased_Representation
(Ctyp
) then
7150 Comp_Val
:= Comp_Val
- Expr_Value
(Type_Low_Bound
(Ctyp
));
7153 Comp_Val
:= Comp_Val
mod Uint_2
** Comp_Size
;
7154 Val
:= UI_To_Int
(Val
+ Comp_Val
* Uint_2
** Shift
);
7155 Shift
:= Shift
+ Incr
;
7156 One_Comp
:= Next
(One_Comp
);
7157 Packed_Num
:= Packed_Num
+ 1;
7161 One_Dim
:= Next
(One_Dim
);
7164 if Packed_Num
> 0 then
7166 -- Add final incomplete byte if present
7168 Append
(Make_Integer_Literal
(Sloc
(One_Dim
), Val
), Comps
);
7172 Unchecked_Convert_To
(Typ
,
7173 Make_Qualified_Expression
(Loc
,
7174 Subtype_Mark
=> New_Occurrence_Of
(Packed_Array
, Loc
),
7175 Expression
=> Make_Aggregate
(Loc
, Expressions
=> Comps
))));
7176 Analyze_And_Resolve
(N
);
7179 end Two_Dim_Packed_Array_Handled
;
7181 ---------------------
7182 -- Sort_Case_Table --
7183 ---------------------
7185 procedure Sort_Case_Table
(Case_Table
: in out Case_Table_Type
) is
7186 L
: constant Int
:= Case_Table
'First;
7187 U
: constant Int
:= Case_Table
'Last;
7195 T
:= Case_Table
(K
+ 1);
7199 and then Expr_Value
(Case_Table
(J
- 1).Choice_Lo
) >
7200 Expr_Value
(T
.Choice_Lo
)
7202 Case_Table
(J
) := Case_Table
(J
- 1);
7206 Case_Table
(J
) := T
;
7209 end Sort_Case_Table
;
7211 ----------------------------
7212 -- Static_Array_Aggregate --
7213 ----------------------------
7215 function Static_Array_Aggregate
(N
: Node_Id
) return Boolean is
7216 Bounds
: constant Node_Id
:= Aggregate_Bounds
(N
);
7218 Typ
: constant Entity_Id
:= Etype
(N
);
7219 Comp_Type
: constant Entity_Id
:= Component_Type
(Typ
);
7226 if Is_Tagged_Type
(Typ
)
7227 or else Is_Controlled
(Typ
)
7228 or else Is_Packed
(Typ
)
7234 and then Nkind
(Bounds
) = N_Range
7235 and then Nkind
(Low_Bound
(Bounds
)) = N_Integer_Literal
7236 and then Nkind
(High_Bound
(Bounds
)) = N_Integer_Literal
7238 Lo
:= Low_Bound
(Bounds
);
7239 Hi
:= High_Bound
(Bounds
);
7241 if No
(Component_Associations
(N
)) then
7243 -- Verify that all components are static integers
7245 Expr
:= First
(Expressions
(N
));
7246 while Present
(Expr
) loop
7247 if Nkind
(Expr
) /= N_Integer_Literal
then
7257 -- We allow only a single named association, either a static
7258 -- range or an others_clause, with a static expression.
7260 Expr
:= First
(Component_Associations
(N
));
7262 if Present
(Expressions
(N
)) then
7265 elsif Present
(Next
(Expr
)) then
7268 elsif Present
(Next
(First
(Choices
(Expr
)))) then
7272 -- The aggregate is static if all components are literals,
7273 -- or else all its components are static aggregates for the
7274 -- component type. We also limit the size of a static aggregate
7275 -- to prevent runaway static expressions.
7277 if Is_Array_Type
(Comp_Type
)
7278 or else Is_Record_Type
(Comp_Type
)
7280 if Nkind
(Expression
(Expr
)) /= N_Aggregate
7282 not Compile_Time_Known_Aggregate
(Expression
(Expr
))
7287 elsif Nkind
(Expression
(Expr
)) /= N_Integer_Literal
then
7291 if not Aggr_Size_OK
(N
, Typ
) then
7295 -- Create a positional aggregate with the right number of
7296 -- copies of the expression.
7298 Agg
:= Make_Aggregate
(Sloc
(N
), New_List
, No_List
);
7300 for I
in UI_To_Int
(Intval
(Lo
)) .. UI_To_Int
(Intval
(Hi
))
7302 Append_To
(Expressions
(Agg
), New_Copy
(Expression
(Expr
)));
7304 -- The copied expression must be analyzed and resolved.
7305 -- Besides setting the type, this ensures that static
7306 -- expressions are appropriately marked as such.
7309 (Last
(Expressions
(Agg
)), Component_Type
(Typ
));
7312 Set_Aggregate_Bounds
(Agg
, Bounds
);
7313 Set_Etype
(Agg
, Typ
);
7316 Set_Compile_Time_Known_Aggregate
(N
);
7325 end Static_Array_Aggregate
;