PR27116, Spelling errors found by Debian style checker
[official-gcc.git] / gcc / ada / exp_aggr.adb
blobcd5cc0b76698aeaaa07f82a280a1c992489d2525
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Exp_Util; use Exp_Util;
37 with Exp_Ch3; use Exp_Ch3;
38 with Exp_Ch6; use Exp_Ch6;
39 with Exp_Ch7; use Exp_Ch7;
40 with Exp_Ch9; use Exp_Ch9;
41 with Exp_Disp; use Exp_Disp;
42 with Exp_Tss; use Exp_Tss;
43 with Freeze; use Freeze;
44 with Itypes; use Itypes;
45 with Lib; use Lib;
46 with Namet; use Namet;
47 with Nmake; use Nmake;
48 with Nlists; use Nlists;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Ttypes; use Ttypes;
54 with Sem; use Sem;
55 with Sem_Aggr; use Sem_Aggr;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Case; use Sem_Case;
58 with Sem_Ch3; use Sem_Ch3;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Mech; use Sem_Mech;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 use Sem_Util.Storage_Model_Support;
67 with Sinfo; use Sinfo;
68 with Sinfo.Nodes; use Sinfo.Nodes;
69 with Sinfo.Utils; use Sinfo.Utils;
70 with Snames; use Snames;
71 with Stand; use Stand;
72 with Stringt; use Stringt;
73 with Tbuild; use Tbuild;
74 with Uintp; use Uintp;
75 with Urealp; use Urealp;
76 with Warnsw; use Warnsw;
78 package body Exp_Aggr is
80 function Build_Assignment_With_Temporary
81 (Target : Node_Id;
82 Typ : Entity_Id;
83 Source : Node_Id) return List_Id;
84 -- Returns a list of actions to assign Source to Target of type Typ using
85 -- an extra temporary, which can potentially be large.
87 type Case_Bounds is record
88 Choice_Lo : Node_Id;
89 Choice_Hi : Node_Id;
90 Choice_Node : Node_Id;
91 end record;
93 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
94 -- Table type used by Check_Case_Choices procedure
96 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id);
97 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id);
98 procedure Expand_Container_Aggregate (N : Node_Id);
100 function Get_Base_Object (N : Node_Id) return Entity_Id;
101 -- Return the base object, i.e. the outermost prefix object, that N refers
102 -- to statically, or Empty if it cannot be determined. The assumption is
103 -- that all dereferences are explicit in the tree rooted at N.
105 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
106 -- N is an aggregate (record or array). Checks the presence of default
107 -- initialization (<>) in any component (Ada 2005: AI-287).
109 procedure Initialize_Component
110 (N : Node_Id;
111 Comp : Node_Id;
112 Comp_Typ : Node_Id;
113 Init_Expr : Node_Id;
114 Stmts : List_Id);
115 -- Perform the initialization of component Comp with expected type
116 -- Comp_Typ of aggregate N. Init_Expr denotes the initialization
117 -- expression of the component. All generated code is added to Stmts.
119 function Is_CCG_Supported_Aggregate (N : Node_Id) return Boolean;
120 -- Return True if aggregate N is located in a context supported by the
121 -- CCG backend; False otherwise.
123 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
124 -- Returns true if N is an aggregate used to initialize the components
125 -- of a statically allocated dispatch table.
127 function Late_Expansion
128 (N : Node_Id;
129 Typ : Entity_Id;
130 Target : Node_Id) return List_Id;
131 -- This routine implements top-down expansion of nested aggregates. In
132 -- doing so, it avoids the generation of temporaries at each level. N is
133 -- a nested record or array aggregate with the Expansion_Delayed flag.
134 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
135 -- expression that will hold the result of the aggregate expansion.
137 function Make_OK_Assignment_Statement
138 (Sloc : Source_Ptr;
139 Name : Node_Id;
140 Expression : Node_Id) return Node_Id;
141 -- This is like Make_Assignment_Statement, except that Assignment_OK
142 -- is set in the left operand. All assignments built by this unit use
143 -- this routine. This is needed to deal with assignments to initialized
144 -- constants that are done in place.
146 function Must_Slide
147 (Aggr : Node_Id;
148 Obj_Type : Entity_Id;
149 Typ : Entity_Id) return Boolean;
150 -- A static array aggregate in an object declaration can in most cases be
151 -- expanded in place. The one exception is when the aggregate is given
152 -- with component associations that specify different bounds from those of
153 -- the type definition in the object declaration. In this pathological
154 -- case the aggregate must slide, and we must introduce an intermediate
155 -- temporary to hold it.
157 -- The same holds in an assignment to one-dimensional array of arrays,
158 -- when a component may be given with bounds that differ from those of the
159 -- component type.
161 function Number_Of_Choices (N : Node_Id) return Nat;
162 -- Returns the number of discrete choices (not including the others choice
163 -- if present) contained in (sub-)aggregate N.
165 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
166 -- Sort the Case Table using the Lower Bound of each Choice as the key.
167 -- A simple insertion sort is used since the number of choices in a case
168 -- statement of variant part will usually be small and probably in near
169 -- sorted order.
171 ------------------------------------------------------
172 -- Local subprograms for Record Aggregate Expansion --
173 ------------------------------------------------------
175 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean;
176 -- True if N is an aggregate (possibly qualified or converted) that is
177 -- being returned from a build-in-place function.
179 function Build_Record_Aggr_Code
180 (N : Node_Id;
181 Typ : Entity_Id;
182 Lhs : Node_Id) return List_Id;
183 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
184 -- aggregate. Target is an expression containing the location on which the
185 -- component by component assignments will take place. Returns the list of
186 -- assignments plus all other adjustments needed for tagged and controlled
187 -- types.
189 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
190 -- Transform a record aggregate into a sequence of assignments performed
191 -- component by component. N is an N_Aggregate or N_Extension_Aggregate.
192 -- Typ is the type of the record aggregate.
194 procedure Expand_Record_Aggregate
195 (N : Node_Id;
196 Orig_Tag : Node_Id := Empty;
197 Parent_Expr : Node_Id := Empty);
198 -- This is the top level procedure for record aggregate expansion.
199 -- Expansion for record aggregates needs expand aggregates for tagged
200 -- record types. Specifically Expand_Record_Aggregate adds the Tag
201 -- field in front of the Component_Association list that was created
202 -- during resolution by Resolve_Record_Aggregate.
204 -- N is the record aggregate node.
205 -- Orig_Tag is the value of the Tag that has to be provided for this
206 -- specific aggregate. It carries the tag corresponding to the type
207 -- of the outermost aggregate during the recursive expansion
208 -- Parent_Expr is the ancestor part of the original extension
209 -- aggregate
211 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
212 -- Return true if one of the components is of a discriminated type with
213 -- defaults. An aggregate for a type with mutable components must be
214 -- expanded into individual assignments.
216 function In_Place_Assign_OK
217 (N : Node_Id;
218 Target_Object : Entity_Id := Empty) return Boolean;
219 -- Predicate to determine whether an aggregate assignment can be done in
220 -- place, because none of the new values can depend on the components of
221 -- the target of the assignment.
223 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
224 -- If the type of the aggregate is a type extension with renamed discrimi-
225 -- nants, we must initialize the hidden discriminants of the parent.
226 -- Otherwise, the target object must not be initialized. The discriminants
227 -- are initialized by calling the initialization procedure for the type.
228 -- This is incorrect if the initialization of other components has any
229 -- side effects. We restrict this call to the case where the parent type
230 -- has a variant part, because this is the only case where the hidden
231 -- discriminants are accessed, namely when calling discriminant checking
232 -- functions of the parent type, and when applying a stream attribute to
233 -- an object of the derived type.
235 -----------------------------------------------------
236 -- Local Subprograms for Array Aggregate Expansion --
237 -----------------------------------------------------
239 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
240 -- Returns true if an aggregate assignment can be done by the back end
242 function Aggr_Size_OK (N : Node_Id) return Boolean;
243 -- Very large static aggregates present problems to the back-end, and are
244 -- transformed into assignments and loops. This function verifies that the
245 -- total number of components of an aggregate is acceptable for rewriting
246 -- into a purely positional static form. Aggr_Size_OK must be called before
247 -- calling Flatten.
249 -- This function also detects and warns about one-component aggregates that
250 -- appear in a nonstatic context. Even if the component value is static,
251 -- such an aggregate must be expanded into an assignment.
253 function Backend_Processing_Possible (N : Node_Id) return Boolean;
254 -- This function checks if array aggregate N can be processed directly
255 -- by the backend. If this is the case, True is returned.
257 function Build_Array_Aggr_Code
258 (N : Node_Id;
259 Ctype : Entity_Id;
260 Index : Node_Id;
261 Into : Node_Id;
262 Scalar_Comp : Boolean;
263 Indexes : List_Id := No_List) return List_Id;
264 -- This recursive routine returns a list of statements containing the
265 -- loops and assignments that are needed for the expansion of the array
266 -- aggregate N.
268 -- N is the (sub-)aggregate node to be expanded into code. This node has
269 -- been fully analyzed, and its Etype is properly set.
271 -- Index is the index node corresponding to the array subaggregate N
273 -- Into is the target expression into which we are copying the aggregate.
274 -- Note that this node may not have been analyzed yet, and so the Etype
275 -- field may not be set.
277 -- Scalar_Comp is True if the component type of the aggregate is scalar
279 -- Indexes is the current list of expressions used to index the object we
280 -- are writing into.
282 procedure Convert_Array_Aggr_In_Allocator
283 (Decl : Node_Id;
284 Aggr : Node_Id;
285 Target : Node_Id);
286 -- If the aggregate appears within an allocator and can be expanded in
287 -- place, this routine generates the individual assignments to components
288 -- of the designated object. This is an optimization over the general
289 -- case, where a temporary is first created on the stack and then used to
290 -- construct the allocated object on the heap.
292 procedure Convert_To_Positional
293 (N : Node_Id;
294 Handle_Bit_Packed : Boolean := False);
295 -- If possible, convert named notation to positional notation. This
296 -- conversion is possible only in some static cases. If the conversion is
297 -- possible, then N is rewritten with the analyzed converted aggregate.
298 -- The parameter Handle_Bit_Packed is usually set False (since we do
299 -- not expect the back end to handle bit packed arrays, so the normal case
300 -- of conversion is pointless), but in the special case of a call from
301 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
302 -- these are cases we handle in there.
304 procedure Expand_Array_Aggregate (N : Node_Id);
305 -- This is the top-level routine to perform array aggregate expansion.
306 -- N is the N_Aggregate node to be expanded.
308 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
309 -- For 2D packed array aggregates with constant bounds and constant scalar
310 -- components, it is preferable to pack the inner aggregates because the
311 -- whole matrix can then be presented to the back-end as a one-dimensional
312 -- list of literals. This is much more efficient than expanding into single
313 -- component assignments. This function determines if the type Typ is for
314 -- an array that is suitable for this optimization: it returns True if Typ
315 -- is a two dimensional bit packed array with component size 1, 2, or 4.
317 function Max_Aggregate_Size
318 (N : Node_Id;
319 Default_Size : Nat := 5000) return Nat;
320 -- Return the max size for a static aggregate N. Return Default_Size if no
321 -- other special criteria trigger.
323 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
324 -- Given an array aggregate, this function handles the case of a packed
325 -- array aggregate with all constant values, where the aggregate can be
326 -- evaluated at compile time. If this is possible, then N is rewritten
327 -- to be its proper compile time value with all the components properly
328 -- assembled. The expression is analyzed and resolved and True is returned.
329 -- If this transformation is not possible, N is unchanged and False is
330 -- returned.
332 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
333 -- If the type of the aggregate is a two-dimensional bit_packed array
334 -- it may be transformed into an array of bytes with constant values,
335 -- and presented to the back-end as a static value. The function returns
336 -- false if this transformation cannot be performed. THis is similar to,
337 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
339 ------------------------------------
340 -- Aggr_Assignment_OK_For_Backend --
341 ------------------------------------
343 -- Back-end processing by Gigi/gcc is possible only if all the following
344 -- conditions are met:
346 -- 1. N consists of a single OTHERS choice, possibly recursively, or
347 -- of a single choice, possibly recursively, if it is surrounded by
348 -- a qualified expression whose subtype mark is unconstrained.
350 -- 2. The array type has no null ranges (the purpose of this is to
351 -- avoid a bogus warning for an out-of-range value).
353 -- 3. The array type has no atomic components
355 -- 4. The component type is elementary
357 -- 5. The component size is a multiple of Storage_Unit
359 -- 6. The component size is Storage_Unit or the value is of the form
360 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
361 -- and M in 0 .. A-1. This can also be viewed as K occurrences of
362 -- the Storage_Unit value M, concatenated together.
364 -- The ultimate goal is to generate a call to a fast memset routine
365 -- specifically optimized for the target.
367 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
369 function Is_OK_Aggregate (Aggr : Node_Id) return Boolean;
370 -- Return true if Aggr is suitable for back-end assignment
372 ---------------------
373 -- Is_OK_Aggregate --
374 ---------------------
376 function Is_OK_Aggregate (Aggr : Node_Id) return Boolean is
377 Assoc : constant List_Id := Component_Associations (Aggr);
379 begin
380 -- An "others" aggregate is most likely OK, but see below
382 if Is_Others_Aggregate (Aggr) then
383 null;
385 -- An aggregate with a single choice requires a qualified expression
386 -- whose subtype mark is an unconstrained type because we need it to
387 -- have the semantics of an "others" aggregate.
389 elsif Nkind (Parent (N)) = N_Qualified_Expression
390 and then not Is_Constrained (Entity (Subtype_Mark (Parent (N))))
391 and then Is_Single_Aggregate (Aggr)
392 then
393 null;
395 -- The other cases are not OK
397 else
398 return False;
399 end if;
401 -- In any case we do not support an iterated association
403 return Nkind (First (Assoc)) /= N_Iterated_Component_Association;
404 end Is_OK_Aggregate;
406 Bounds : Range_Nodes;
407 Csiz : Uint := No_Uint;
408 Ctyp : Entity_Id;
409 Expr : Node_Id;
410 Index : Entity_Id;
411 Nunits : Int;
412 Remainder : Uint;
413 Value : Uint;
415 -- Start of processing for Aggr_Assignment_OK_For_Backend
417 begin
418 -- Back end doesn't know about <>
420 if Has_Default_Init_Comps (N) then
421 return False;
422 end if;
424 -- Recurse as far as possible to find the innermost component type
426 Ctyp := Etype (N);
427 Expr := N;
428 while Is_Array_Type (Ctyp) loop
429 if Nkind (Expr) /= N_Aggregate
430 or else not Is_OK_Aggregate (Expr)
431 then
432 return False;
433 end if;
435 Index := First_Index (Ctyp);
436 while Present (Index) loop
437 Bounds := Get_Index_Bounds (Index);
439 if Is_Null_Range (Bounds.First, Bounds.Last) then
440 return False;
441 end if;
443 Next_Index (Index);
444 end loop;
446 Expr := Expression (First (Component_Associations (Expr)));
448 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
449 if Nkind (Expr) /= N_Aggregate
450 or else not Is_OK_Aggregate (Expr)
451 then
452 return False;
453 end if;
455 Expr := Expression (First (Component_Associations (Expr)));
456 end loop;
458 if Has_Atomic_Components (Ctyp) then
459 return False;
460 end if;
462 Csiz := Component_Size (Ctyp);
463 Ctyp := Component_Type (Ctyp);
465 if Is_Full_Access (Ctyp) then
466 return False;
467 end if;
468 end loop;
470 -- Access types need to be dealt with specially
472 if Is_Access_Type (Ctyp) then
474 -- Component_Size is not set by Layout_Type if the component
475 -- type is an access type ???
477 Csiz := Esize (Ctyp);
479 -- Fat pointers are rejected as they are not really elementary
480 -- for the backend.
482 if No (Csiz) or else Csiz /= System_Address_Size then
483 return False;
484 end if;
486 -- The supported expressions are NULL and constants, others are
487 -- rejected upfront to avoid being analyzed below, which can be
488 -- problematic for some of them, for example allocators.
490 if Nkind (Expr) /= N_Null and then not Is_Entity_Name (Expr) then
491 return False;
492 end if;
494 -- Scalar types are OK if their size is a multiple of Storage_Unit
496 elsif Is_Scalar_Type (Ctyp) and then Present (Csiz) then
498 if Csiz mod System_Storage_Unit /= 0 then
499 return False;
500 end if;
502 -- Composite types are rejected
504 else
505 return False;
506 end if;
508 -- If the expression has side effects (e.g. contains calls with
509 -- potential side effects) reject as well. We only preanalyze the
510 -- expression to prevent the removal of intended side effects.
512 Preanalyze_And_Resolve (Expr, Ctyp);
514 if not Side_Effect_Free (Expr) then
515 return False;
516 end if;
518 -- The expression needs to be analyzed if True is returned
520 Analyze_And_Resolve (Expr, Ctyp);
522 -- Strip away any conversions from the expression as they simply
523 -- qualify the real expression.
525 while Nkind (Expr) in N_Unchecked_Type_Conversion | N_Type_Conversion
526 loop
527 Expr := Expression (Expr);
528 end loop;
530 Nunits := UI_To_Int (Csiz) / System_Storage_Unit;
532 if Nunits = 1 then
533 return True;
534 end if;
536 if not Compile_Time_Known_Value (Expr) then
537 return False;
538 end if;
540 -- The only supported value for floating point is 0.0
542 if Is_Floating_Point_Type (Ctyp) then
543 return Expr_Value_R (Expr) = Ureal_0;
544 end if;
546 -- For other types, we can look into the value as an integer, which
547 -- means the representation value for enumeration literals.
549 Value := Expr_Rep_Value (Expr);
551 if Has_Biased_Representation (Ctyp) then
552 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
553 end if;
555 -- Values 0 and -1 immediately satisfy the last check
557 if Value = Uint_0 or else Value = Uint_Minus_1 then
558 return True;
559 end if;
561 -- We need to work with an unsigned value
563 if Value < 0 then
564 Value := Value + 2**(System_Storage_Unit * Nunits);
565 end if;
567 Remainder := Value rem 2**System_Storage_Unit;
569 for J in 1 .. Nunits - 1 loop
570 Value := Value / 2**System_Storage_Unit;
572 if Value rem 2**System_Storage_Unit /= Remainder then
573 return False;
574 end if;
575 end loop;
577 return True;
578 end Aggr_Assignment_OK_For_Backend;
580 ------------------
581 -- Aggr_Size_OK --
582 ------------------
584 function Aggr_Size_OK (N : Node_Id) return Boolean is
585 Typ : constant Entity_Id := Etype (N);
586 Lo : Node_Id;
587 Hi : Node_Id;
588 Indx : Node_Id;
589 Size : Uint;
590 Lov : Uint;
591 Hiv : Uint;
593 Max_Aggr_Size : Nat;
594 -- Determines the maximum size of an array aggregate produced by
595 -- converting named to positional notation (e.g. from others clauses).
596 -- This avoids running away with attempts to convert huge aggregates,
597 -- which hit memory limits in the backend.
599 function Component_Count (T : Entity_Id) return Nat;
600 -- The limit is applied to the total number of subcomponents that the
601 -- aggregate will have, which is the number of static expressions
602 -- that will appear in the flattened array. This requires a recursive
603 -- computation of the number of scalar components of the structure.
605 ---------------------
606 -- Component_Count --
607 ---------------------
609 function Component_Count (T : Entity_Id) return Nat is
610 Res : Nat := 0;
611 Comp : Entity_Id;
613 begin
614 if Is_Scalar_Type (T) then
615 return 1;
617 elsif Is_Record_Type (T) then
618 Comp := First_Component (T);
619 while Present (Comp) loop
620 Res := Res + Component_Count (Etype (Comp));
621 Next_Component (Comp);
622 end loop;
624 return Res;
626 elsif Is_Array_Type (T) then
627 declare
628 Lo : constant Node_Id :=
629 Type_Low_Bound (Etype (First_Index (T)));
630 Hi : constant Node_Id :=
631 Type_High_Bound (Etype (First_Index (T)));
633 Siz : constant Nat := Component_Count (Component_Type (T));
635 begin
636 -- Check for superflat arrays, i.e. arrays with such bounds
637 -- as 4 .. 2, to insure that this function never returns a
638 -- meaningless negative value.
640 if not Compile_Time_Known_Value (Lo)
641 or else not Compile_Time_Known_Value (Hi)
642 or else Expr_Value (Hi) < Expr_Value (Lo)
643 then
644 return 0;
646 else
647 -- If the number of components is greater than Int'Last,
648 -- then return Int'Last, so caller will return False (Aggr
649 -- size is not OK). Otherwise, UI_To_Int will crash.
651 declare
652 UI : constant Uint :=
653 (Expr_Value (Hi) - Expr_Value (Lo) + 1) * Siz;
654 begin
655 if UI_Is_In_Int_Range (UI) then
656 return UI_To_Int (UI);
657 else
658 return Int'Last;
659 end if;
660 end;
661 end if;
662 end;
664 else
665 -- Can only be a null for an access type
667 return 1;
668 end if;
669 end Component_Count;
671 -- Start of processing for Aggr_Size_OK
673 begin
674 -- We bump the maximum size unless the aggregate has a single component
675 -- association, which will be more efficient if implemented with a loop.
676 -- The -gnatd_g switch disables this bumping.
678 if (No (Expressions (N))
679 and then No (Next (First (Component_Associations (N)))))
680 or else Debug_Flag_Underscore_G
681 then
682 Max_Aggr_Size := Max_Aggregate_Size (N);
683 else
684 Max_Aggr_Size := Max_Aggregate_Size (N, 500_000);
685 end if;
687 Size := UI_From_Int (Component_Count (Component_Type (Typ)));
689 Indx := First_Index (Typ);
690 while Present (Indx) loop
691 Lo := Type_Low_Bound (Etype (Indx));
692 Hi := Type_High_Bound (Etype (Indx));
694 -- Bounds need to be known at compile time
696 if not Compile_Time_Known_Value (Lo)
697 or else not Compile_Time_Known_Value (Hi)
698 then
699 return False;
700 end if;
702 Lov := Expr_Value (Lo);
703 Hiv := Expr_Value (Hi);
705 -- A flat array is always safe
707 if Hiv < Lov then
708 return True;
709 end if;
711 -- One-component aggregates are suspicious, and if the context type
712 -- is an object declaration with nonstatic bounds it will trip gcc;
713 -- such an aggregate must be expanded into a single assignment.
715 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
716 declare
717 Index_Type : constant Entity_Id :=
718 Etype
719 (First_Index (Etype (Defining_Identifier (Parent (N)))));
720 Indx : Node_Id;
722 begin
723 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
724 or else not Compile_Time_Known_Value
725 (Type_High_Bound (Index_Type))
726 then
727 if Present (Component_Associations (N)) then
728 Indx :=
729 First
730 (Choice_List (First (Component_Associations (N))));
732 if Is_Entity_Name (Indx)
733 and then not Is_Type (Entity (Indx))
734 then
735 Error_Msg_N
736 ("single component aggregate in "
737 & "non-static context??", Indx);
738 Error_Msg_N ("\maybe subtype name was meant??", Indx);
739 end if;
740 end if;
742 return False;
743 end if;
744 end;
745 end if;
747 declare
748 Rng : constant Uint := Hiv - Lov + 1;
750 begin
751 -- Check if size is too large
753 if not UI_Is_In_Int_Range (Rng) then
754 return False;
755 end if;
757 -- Compute the size using universal arithmetic to avoid the
758 -- possibility of overflow on very large aggregates.
760 Size := Size * Rng;
762 if Size <= 0
763 or else Size > Max_Aggr_Size
764 then
765 return False;
766 end if;
767 end;
769 -- Bounds must be in integer range, for later array construction
771 if not UI_Is_In_Int_Range (Lov)
772 or else
773 not UI_Is_In_Int_Range (Hiv)
774 then
775 return False;
776 end if;
778 Next_Index (Indx);
779 end loop;
781 return True;
782 end Aggr_Size_OK;
784 ---------------------------------
785 -- Backend_Processing_Possible --
786 ---------------------------------
788 -- Backend processing by Gigi/gcc is possible only if all the following
789 -- conditions are met:
791 -- 1. N is fully positional
793 -- 2. N is not a bit-packed array aggregate;
795 -- 3. The size of N's array type must be known at compile time. Note
796 -- that this implies that the component size is also known
798 -- 4. The array type of N does not follow the Fortran layout convention
799 -- or if it does it must be 1 dimensional.
801 -- 5. The array component type may not be tagged (which could necessitate
802 -- reassignment of proper tags).
804 -- 6. The array component type must not have unaligned bit components
806 -- 7. None of the components of the aggregate may be bit unaligned
807 -- components.
809 -- 8. There cannot be delayed components, since we do not know enough
810 -- at this stage to know if back end processing is possible.
812 -- 9. There cannot be any discriminated record components, since the
813 -- back end cannot handle this complex case.
815 -- 10. No controlled actions need to be generated for components
817 -- 11. When generating C code, N must be part of a N_Object_Declaration
819 -- 12. When generating C code, N must not include function calls
821 function Backend_Processing_Possible (N : Node_Id) return Boolean is
822 Typ : constant Entity_Id := Etype (N);
823 -- Typ is the correct constrained array subtype of the aggregate
825 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
826 -- This routine checks components of aggregate N, enforcing checks
827 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
828 -- are performed on subaggregates. The Index value is the current index
829 -- being checked in the multidimensional case.
831 ---------------------
832 -- Component_Check --
833 ---------------------
835 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
836 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
837 -- Given a type conversion or an unchecked type conversion N, return
838 -- its innermost original expression.
840 ----------------------------------
841 -- Ultimate_Original_Expression --
842 ----------------------------------
844 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
845 Expr : Node_Id := Original_Node (N);
847 begin
848 while Nkind (Expr) in
849 N_Type_Conversion | N_Unchecked_Type_Conversion
850 loop
851 Expr := Original_Node (Expression (Expr));
852 end loop;
854 return Expr;
855 end Ultimate_Original_Expression;
857 -- Local variables
859 Expr : Node_Id;
861 -- Start of processing for Component_Check
863 begin
864 -- Checks 1: (no component associations)
866 if Present (Component_Associations (N)) then
867 return False;
868 end if;
870 -- Checks 11: The C code generator cannot handle aggregates that are
871 -- not part of an object declaration.
873 if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
874 return False;
875 end if;
877 -- Checks on components
879 -- Recurse to check subaggregates, which may appear in qualified
880 -- expressions. If delayed, the front-end will have to expand.
881 -- If the component is a discriminated record, treat as nonstatic,
882 -- as the back-end cannot handle this properly.
884 Expr := First (Expressions (N));
885 while Present (Expr) loop
887 -- Checks 8: (no delayed components)
889 if Is_Delayed_Aggregate (Expr) then
890 return False;
891 end if;
893 -- Checks 9: (no discriminated records)
895 if Present (Etype (Expr))
896 and then Is_Record_Type (Etype (Expr))
897 and then Has_Discriminants (Etype (Expr))
898 then
899 return False;
900 end if;
902 -- Checks 7. Component must not be bit aligned component
904 if Possible_Bit_Aligned_Component (Expr) then
905 return False;
906 end if;
908 -- Checks 12: (no function call)
910 if Modify_Tree_For_C
911 and then
912 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
913 then
914 return False;
915 end if;
917 -- Recursion to following indexes for multiple dimension case
919 if Present (Next_Index (Index))
920 and then not Component_Check (Expr, Next_Index (Index))
921 then
922 return False;
923 end if;
925 -- All checks for that component finished, on to next
927 Next (Expr);
928 end loop;
930 return True;
931 end Component_Check;
933 -- Start of processing for Backend_Processing_Possible
935 begin
936 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
938 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
939 return False;
940 end if;
942 -- If component is limited, aggregate must be expanded because each
943 -- component assignment must be built in place.
945 if Is_Limited_View (Component_Type (Typ)) then
946 return False;
947 end if;
949 -- Checks 4 (array must not be multidimensional Fortran case)
951 if Convention (Typ) = Convention_Fortran
952 and then Number_Dimensions (Typ) > 1
953 then
954 return False;
955 end if;
957 -- Checks 3 (size of array must be known at compile time)
959 if not Size_Known_At_Compile_Time (Typ) then
960 return False;
961 end if;
963 -- Checks on components
965 if not Component_Check (N, First_Index (Typ)) then
966 return False;
967 end if;
969 -- Checks 5 (if the component type is tagged, then we may need to do
970 -- tag adjustments. Perhaps this should be refined to check for any
971 -- component associations that actually need tag adjustment, similar
972 -- to the test in Component_OK_For_Backend for record aggregates with
973 -- tagged components, but not clear whether it's worthwhile ???; in the
974 -- case of virtual machines (no Tagged_Type_Expansion), object tags are
975 -- handled implicitly).
977 if Is_Tagged_Type (Component_Type (Typ))
978 and then Tagged_Type_Expansion
979 then
980 return False;
981 end if;
983 -- Checks 6 (component type must not have bit aligned components)
985 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
986 return False;
987 end if;
989 -- Backend processing is possible
991 return True;
992 end Backend_Processing_Possible;
994 ---------------------------
995 -- Build_Array_Aggr_Code --
996 ---------------------------
998 -- The code that we generate from a one dimensional aggregate is
1000 -- 1. If the subaggregate contains discrete choices we
1002 -- (a) Sort the discrete choices
1004 -- (b) Otherwise for each discrete choice that specifies a range we
1005 -- emit a loop. If a range specifies a maximum of three values, or
1006 -- we are dealing with an expression we emit a sequence of
1007 -- assignments instead of a loop.
1009 -- (c) Generate the remaining loops to cover the others choice if any
1011 -- 2. If the aggregate contains positional elements we
1013 -- (a) Translate the positional elements in a series of assignments
1015 -- (b) Generate a final loop to cover the others choice if any.
1016 -- Note that this final loop has to be a while loop since the case
1018 -- L : Integer := Integer'Last;
1019 -- H : Integer := Integer'Last;
1020 -- A : array (L .. H) := (1, others =>0);
1022 -- cannot be handled by a for loop. Thus for the following
1024 -- array (L .. H) := (.. positional elements.., others => E);
1026 -- we always generate something like:
1028 -- J : Index_Type := Index_Of_Last_Positional_Element;
1029 -- while J < H loop
1030 -- J := Index_Base'Succ (J)
1031 -- Tmp (J) := E;
1032 -- end loop;
1034 function Build_Array_Aggr_Code
1035 (N : Node_Id;
1036 Ctype : Entity_Id;
1037 Index : Node_Id;
1038 Into : Node_Id;
1039 Scalar_Comp : Boolean;
1040 Indexes : List_Id := No_List) return List_Id
1042 Loc : constant Source_Ptr := Sloc (N);
1043 Typ : constant Entity_Id := Etype (N);
1044 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
1045 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
1046 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
1048 function Add (Val : Int; To : Node_Id) return Node_Id;
1049 -- Returns an expression where Val is added to expression To, unless
1050 -- To+Val is provably out of To's base type range. To must be an
1051 -- already analyzed expression.
1053 function Empty_Range (L, H : Node_Id) return Boolean;
1054 -- Returns True if the range defined by L .. H is certainly empty
1056 function Equal (L, H : Node_Id) return Boolean;
1057 -- Returns True if L = H for sure
1059 function Index_Base_Name return Node_Id;
1060 -- Returns a new reference to the index type name
1062 function Gen_Assign
1063 (Ind : Node_Id;
1064 Expr : Node_Id) return List_Id;
1065 -- Ind must be a side-effect-free expression. If the input aggregate N
1066 -- to Build_Loop contains no subaggregates, then this function returns
1067 -- the assignment statement:
1069 -- Into (Indexes, Ind) := Expr;
1071 -- Otherwise we call Build_Code recursively.
1073 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1074 -- is empty and we generate a call to the corresponding IP subprogram.
1076 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
1077 -- Nodes L and H must be side-effect-free expressions. If the input
1078 -- aggregate N to Build_Loop contains no subaggregates, this routine
1079 -- returns the for loop statement:
1081 -- for J in Index_Base'(L) .. Index_Base'(H) loop
1082 -- Into (Indexes, J) := Expr;
1083 -- end loop;
1085 -- Otherwise we call Build_Code recursively. As an optimization if the
1086 -- loop covers 3 or fewer scalar elements we generate a sequence of
1087 -- assignments.
1088 -- If the component association that generates the loop comes from an
1089 -- Iterated_Component_Association, the loop parameter has the name of
1090 -- the corresponding parameter in the original construct.
1092 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
1093 -- Nodes L and H must be side-effect-free expressions. If the input
1094 -- aggregate N to Build_Loop contains no subaggregates, this routine
1095 -- returns the while loop statement:
1097 -- J : Index_Base := L;
1098 -- while J < H loop
1099 -- J := Index_Base'Succ (J);
1100 -- Into (Indexes, J) := Expr;
1101 -- end loop;
1103 -- Otherwise we call Build_Code recursively
1105 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
1106 -- For an association with a box, use value given by aspect
1107 -- Default_Component_Value of array type if specified, else use
1108 -- value given by aspect Default_Value for component type itself
1109 -- if specified, else return Empty.
1111 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
1112 function Local_Expr_Value (E : Node_Id) return Uint;
1113 -- These two Local routines are used to replace the corresponding ones
1114 -- in sem_eval because while processing the bounds of an aggregate with
1115 -- discrete choices whose index type is an enumeration, we build static
1116 -- expressions not recognized by Compile_Time_Known_Value as such since
1117 -- they have not yet been analyzed and resolved. All the expressions in
1118 -- question are things like Index_Base_Name'Val (Const) which we can
1119 -- easily recognize as being constant.
1121 ---------
1122 -- Add --
1123 ---------
1125 function Add (Val : Int; To : Node_Id) return Node_Id is
1126 Expr_Pos : Node_Id;
1127 Expr : Node_Id;
1128 To_Pos : Node_Id;
1129 U_To : Uint;
1130 U_Val : constant Uint := UI_From_Int (Val);
1132 begin
1133 -- Note: do not try to optimize the case of Val = 0, because
1134 -- we need to build a new node with the proper Sloc value anyway.
1136 -- First test if we can do constant folding
1138 if Local_Compile_Time_Known_Value (To) then
1139 U_To := Local_Expr_Value (To) + Val;
1141 -- Determine if our constant is outside the range of the index.
1142 -- If so return an Empty node. This empty node will be caught
1143 -- by Empty_Range below.
1145 if Compile_Time_Known_Value (Index_Base_L)
1146 and then U_To < Expr_Value (Index_Base_L)
1147 then
1148 return Empty;
1150 elsif Compile_Time_Known_Value (Index_Base_H)
1151 and then U_To > Expr_Value (Index_Base_H)
1152 then
1153 return Empty;
1154 end if;
1156 Expr_Pos := Make_Integer_Literal (Loc, U_To);
1157 Set_Is_Static_Expression (Expr_Pos);
1159 if not Is_Enumeration_Type (Index_Base) then
1160 Expr := Expr_Pos;
1162 -- If we are dealing with enumeration return
1163 -- Index_Base'Val (Expr_Pos)
1165 else
1166 Expr :=
1167 Make_Attribute_Reference
1168 (Loc,
1169 Prefix => Index_Base_Name,
1170 Attribute_Name => Name_Val,
1171 Expressions => New_List (Expr_Pos));
1172 end if;
1174 return Expr;
1175 end if;
1177 -- If we are here no constant folding possible
1179 if not Is_Enumeration_Type (Index_Base) then
1180 Expr :=
1181 Make_Op_Add (Loc,
1182 Left_Opnd => Duplicate_Subexpr (To),
1183 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
1185 -- If we are dealing with enumeration return
1186 -- Index_Base'Val (Index_Base'Pos (To) + Val)
1188 else
1189 To_Pos :=
1190 Make_Attribute_Reference
1191 (Loc,
1192 Prefix => Index_Base_Name,
1193 Attribute_Name => Name_Pos,
1194 Expressions => New_List (Duplicate_Subexpr (To)));
1196 Expr_Pos :=
1197 Make_Op_Add (Loc,
1198 Left_Opnd => To_Pos,
1199 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
1201 Expr :=
1202 Make_Attribute_Reference
1203 (Loc,
1204 Prefix => Index_Base_Name,
1205 Attribute_Name => Name_Val,
1206 Expressions => New_List (Expr_Pos));
1207 end if;
1209 return Expr;
1210 end Add;
1212 -----------------
1213 -- Empty_Range --
1214 -----------------
1216 function Empty_Range (L, H : Node_Id) return Boolean is
1217 Is_Empty : Boolean := False;
1218 Low : Node_Id;
1219 High : Node_Id;
1221 begin
1222 -- First check if L or H were already detected as overflowing the
1223 -- index base range type by function Add above. If this is so Add
1224 -- returns the empty node.
1226 if No (L) or else No (H) then
1227 return True;
1228 end if;
1230 for J in 1 .. 3 loop
1231 case J is
1233 -- L > H range is empty
1235 when 1 =>
1236 Low := L;
1237 High := H;
1239 -- B_L > H range must be empty
1241 when 2 =>
1242 Low := Index_Base_L;
1243 High := H;
1245 -- L > B_H range must be empty
1247 when 3 =>
1248 Low := L;
1249 High := Index_Base_H;
1250 end case;
1252 if Local_Compile_Time_Known_Value (Low)
1253 and then
1254 Local_Compile_Time_Known_Value (High)
1255 then
1256 Is_Empty :=
1257 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
1258 end if;
1260 exit when Is_Empty;
1261 end loop;
1263 return Is_Empty;
1264 end Empty_Range;
1266 -----------
1267 -- Equal --
1268 -----------
1270 function Equal (L, H : Node_Id) return Boolean is
1271 begin
1272 if L = H then
1273 return True;
1275 elsif Local_Compile_Time_Known_Value (L)
1276 and then
1277 Local_Compile_Time_Known_Value (H)
1278 then
1279 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1280 end if;
1282 return False;
1283 end Equal;
1285 ----------------
1286 -- Gen_Assign --
1287 ----------------
1289 function Gen_Assign
1290 (Ind : Node_Id;
1291 Expr : Node_Id) return List_Id
1293 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1294 -- Collect insert_actions generated in the construction of a loop,
1295 -- and prepend them to the sequence of assignments to complete the
1296 -- eventual body of the loop.
1298 ----------------------
1299 -- Add_Loop_Actions --
1300 ----------------------
1302 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1303 Res : List_Id;
1305 begin
1306 -- Ada 2005 (AI-287): Do nothing else in case of default
1307 -- initialized component.
1309 if No (Expr) then
1310 return Lis;
1312 elsif Nkind (Parent (Expr)) = N_Component_Association
1313 and then Present (Loop_Actions (Parent (Expr)))
1314 then
1315 Append_List (Lis, Loop_Actions (Parent (Expr)));
1316 Res := Loop_Actions (Parent (Expr));
1317 Set_Loop_Actions (Parent (Expr), No_List);
1318 return Res;
1320 else
1321 return Lis;
1322 end if;
1323 end Add_Loop_Actions;
1325 -- Local variables
1327 Stmts : constant List_Id := New_List;
1329 Comp_Typ : Entity_Id := Empty;
1330 Expr_Q : Node_Id;
1331 Indexed_Comp : Node_Id;
1332 Init_Call : Node_Id;
1333 New_Indexes : List_Id;
1335 -- Start of processing for Gen_Assign
1337 begin
1338 if No (Indexes) then
1339 New_Indexes := New_List;
1340 else
1341 New_Indexes := New_Copy_List_Tree (Indexes);
1342 end if;
1344 Append_To (New_Indexes, Ind);
1346 if Present (Next_Index (Index)) then
1347 return
1348 Add_Loop_Actions (
1349 Build_Array_Aggr_Code
1350 (N => Expr,
1351 Ctype => Ctype,
1352 Index => Next_Index (Index),
1353 Into => Into,
1354 Scalar_Comp => Scalar_Comp,
1355 Indexes => New_Indexes));
1356 end if;
1358 -- If we get here then we are at a bottom-level (sub-)aggregate
1360 Indexed_Comp :=
1361 Checks_Off
1362 (Make_Indexed_Component (Loc,
1363 Prefix => New_Copy_Tree (Into),
1364 Expressions => New_Indexes));
1366 Set_Assignment_OK (Indexed_Comp);
1368 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1369 -- is not present (and therefore we also initialize Expr_Q to empty).
1371 Expr_Q := Unqualify (Expr);
1373 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1374 Comp_Typ := Component_Type (Etype (N));
1375 pragma Assert (Comp_Typ = Ctype); -- AI-287
1377 elsif Present (Next (First (New_Indexes))) then
1379 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1380 -- component because we have received the component type in
1381 -- the formal parameter Ctype.
1383 -- ??? Some assert pragmas have been added to check if this new
1384 -- formal can be used to replace this code in all cases.
1386 if Present (Expr) then
1388 -- This is a multidimensional array. Recover the component type
1389 -- from the outermost aggregate, because subaggregates do not
1390 -- have an assigned type.
1392 declare
1393 P : Node_Id;
1395 begin
1396 P := Parent (Expr);
1397 while Present (P) loop
1398 if Nkind (P) = N_Aggregate
1399 and then Present (Etype (P))
1400 then
1401 Comp_Typ := Component_Type (Etype (P));
1402 exit;
1404 else
1405 P := Parent (P);
1406 end if;
1407 end loop;
1409 pragma Assert (Comp_Typ = Ctype); -- AI-287
1410 end;
1411 end if;
1412 end if;
1414 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1415 -- default initialized components (otherwise Expr_Q is not present).
1417 if Present (Expr_Q)
1418 and then Nkind (Expr_Q) in N_Aggregate | N_Extension_Aggregate
1419 then
1420 -- At this stage the Expression may not have been analyzed yet
1421 -- because the array aggregate code has not been updated to use
1422 -- the Expansion_Delayed flag and avoid analysis altogether to
1423 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1424 -- the analysis of non-array aggregates now in order to get the
1425 -- value of Expansion_Delayed flag for the inner aggregate ???
1427 -- In the case of an iterated component association, the analysis
1428 -- of the generated loop will analyze the expression in the
1429 -- proper context, in which the loop parameter is visible.
1431 if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then
1432 if Nkind (Parent (Expr_Q)) = N_Iterated_Component_Association
1433 or else Nkind (Parent (Parent ((Expr_Q)))) =
1434 N_Iterated_Component_Association
1435 then
1436 null;
1437 else
1438 Analyze_And_Resolve (Expr_Q, Comp_Typ);
1439 end if;
1440 end if;
1442 if Is_Delayed_Aggregate (Expr_Q) then
1444 -- This is either a subaggregate of a multidimensional array,
1445 -- or a component of an array type whose component type is
1446 -- also an array. In the latter case, the expression may have
1447 -- component associations that provide different bounds from
1448 -- those of the component type, and sliding must occur. Instead
1449 -- of decomposing the current aggregate assignment, force the
1450 -- reanalysis of the assignment, so that a temporary will be
1451 -- generated in the usual fashion, and sliding will take place.
1453 if Nkind (Parent (N)) = N_Assignment_Statement
1454 and then Is_Array_Type (Comp_Typ)
1455 and then Present (Component_Associations (Expr_Q))
1456 and then Must_Slide (N, Comp_Typ, Etype (Expr_Q))
1457 then
1458 Set_Expansion_Delayed (Expr_Q, False);
1459 Set_Analyzed (Expr_Q, False);
1461 else
1462 return
1463 Add_Loop_Actions (
1464 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1465 end if;
1466 end if;
1467 end if;
1469 if Present (Expr) then
1470 Initialize_Component
1471 (N => N,
1472 Comp => Indexed_Comp,
1473 Comp_Typ => Comp_Typ,
1474 Init_Expr => Expr,
1475 Stmts => Stmts);
1477 -- Ada 2005 (AI-287): In case of default initialized component, call
1478 -- the initialization subprogram associated with the component type.
1479 -- If the component type is an access type, add an explicit null
1480 -- assignment, because for the back-end there is an initialization
1481 -- present for the whole aggregate, and no default initialization
1482 -- will take place.
1484 -- In addition, if the component type is controlled, we must call
1485 -- its Initialize procedure explicitly, because there is no explicit
1486 -- object creation that will invoke it otherwise.
1488 else
1489 if Present (Base_Init_Proc (Base_Type (Ctype)))
1490 or else Has_Task (Base_Type (Ctype))
1491 then
1492 Append_List_To (Stmts,
1493 Build_Initialization_Call (Loc,
1494 Id_Ref => Indexed_Comp,
1495 Typ => Ctype,
1496 With_Default_Init => True));
1498 -- If the component type has invariants, add an invariant
1499 -- check after the component is default-initialized. It will
1500 -- be analyzed and resolved before the code for initialization
1501 -- of other components.
1503 if Has_Invariants (Ctype) then
1504 Set_Etype (Indexed_Comp, Ctype);
1505 Append_To (Stmts, Make_Invariant_Call (Indexed_Comp));
1506 end if;
1507 end if;
1509 if Needs_Finalization (Ctype) then
1510 Init_Call :=
1511 Make_Init_Call
1512 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1513 Typ => Ctype);
1515 -- Guard against a missing [Deep_]Initialize when the component
1516 -- type was not properly frozen.
1518 if Present (Init_Call) then
1519 Append_To (Stmts, Init_Call);
1520 end if;
1521 end if;
1523 -- If Default_Initial_Condition applies to the component type,
1524 -- add a DIC check after the component is default-initialized,
1525 -- as well as after an Initialize procedure is called, in the
1526 -- case of components of a controlled type. It will be analyzed
1527 -- and resolved before the code for initialization of other
1528 -- components.
1530 -- Theoretically this might also be needed for cases where Expr
1531 -- is not empty, but a default init still applies, such as for
1532 -- Default_Value cases, in which case we won't get here. ???
1534 if Has_DIC (Ctype) and then Present (DIC_Procedure (Ctype)) then
1535 Append_To (Stmts,
1536 Build_DIC_Call (Loc, New_Copy_Tree (Indexed_Comp), Ctype));
1537 end if;
1538 end if;
1540 return Add_Loop_Actions (Stmts);
1541 end Gen_Assign;
1543 --------------
1544 -- Gen_Loop --
1545 --------------
1547 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1548 Is_Iterated_Component : constant Boolean :=
1549 Parent_Kind (Expr) = N_Iterated_Component_Association;
1551 Ent : Entity_Id;
1553 L_J : Node_Id;
1555 L_L : Node_Id;
1556 -- Index_Base'(L)
1558 L_H : Node_Id;
1559 -- Index_Base'(H)
1561 L_Range : Node_Id;
1562 -- Index_Base'(L) .. Index_Base'(H)
1564 L_Iteration_Scheme : Node_Id;
1565 -- L_J in Index_Base'(L) .. Index_Base'(H)
1567 L_Body : List_Id;
1568 -- The statements to execute in the loop
1570 S : constant List_Id := New_List;
1571 -- List of statements
1573 Tcopy : Node_Id;
1574 -- Copy of expression tree, used for checking purposes
1576 begin
1577 -- If loop bounds define an empty range return the null statement
1579 if Empty_Range (L, H) then
1580 Append_To (S, Make_Null_Statement (Loc));
1582 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1583 -- default initialized component.
1585 if No (Expr) then
1586 null;
1588 else
1589 -- The expression must be type-checked even though no component
1590 -- of the aggregate will have this value. This is done only for
1591 -- actual components of the array, not for subaggregates. Do
1592 -- the check on a copy, because the expression may be shared
1593 -- among several choices, some of which might be non-null.
1595 if Present (Etype (N))
1596 and then Is_Array_Type (Etype (N))
1597 and then No (Next_Index (Index))
1598 then
1599 Expander_Mode_Save_And_Set (False);
1600 Tcopy := New_Copy_Tree (Expr);
1601 Set_Parent (Tcopy, N);
1603 -- For iterated_component_association analyze and resolve
1604 -- the expression with name of the index parameter visible.
1605 -- To manipulate scopes, we use entity of the implicit loop.
1607 if Is_Iterated_Component then
1608 declare
1609 Index_Parameter : constant Entity_Id :=
1610 Defining_Identifier (Parent (Expr));
1611 begin
1612 Push_Scope (Scope (Index_Parameter));
1613 Enter_Name (Index_Parameter);
1614 Analyze_And_Resolve
1615 (Tcopy, Component_Type (Etype (N)));
1616 End_Scope;
1617 end;
1619 -- For ordinary component association, just analyze and
1620 -- resolve the expression.
1622 else
1623 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1624 end if;
1626 Expander_Mode_Restore;
1627 end if;
1628 end if;
1630 return S;
1632 -- If loop bounds are the same then generate an assignment, unless
1633 -- the parent construct is an Iterated_Component_Association.
1635 elsif Equal (L, H) and then not Is_Iterated_Component then
1636 return Gen_Assign (New_Copy_Tree (L), Expr);
1638 -- If H - L <= 2 then generate a sequence of assignments when we are
1639 -- processing the bottom most aggregate and it contains scalar
1640 -- components.
1642 elsif No (Next_Index (Index))
1643 and then Scalar_Comp
1644 and then Local_Compile_Time_Known_Value (L)
1645 and then Local_Compile_Time_Known_Value (H)
1646 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1647 and then not Is_Iterated_Component
1648 then
1649 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1650 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1652 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1653 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1654 end if;
1656 return S;
1657 end if;
1659 -- Otherwise construct the loop, starting with the loop index L_J
1661 if Is_Iterated_Component then
1663 -- Create a new scope for the loop variable so that the
1664 -- following Gen_Assign (that ends up calling
1665 -- Preanalyze_And_Resolve) can correctly find it.
1667 Ent := New_Internal_Entity (E_Loop,
1668 Current_Scope, Loc, 'L');
1669 Set_Etype (Ent, Standard_Void_Type);
1670 Set_Parent (Ent, Parent (Parent (Expr)));
1671 Push_Scope (Ent);
1673 L_J :=
1674 Make_Defining_Identifier (Loc,
1675 Chars => (Chars (Defining_Identifier (Parent (Expr)))));
1677 Enter_Name (L_J);
1679 -- The Etype will be set by a later Analyze call.
1680 Set_Etype (L_J, Any_Type);
1682 Mutate_Ekind (L_J, E_Variable);
1683 Set_Is_Not_Self_Hidden (L_J);
1684 Set_Scope (L_J, Ent);
1685 else
1686 L_J := Make_Temporary (Loc, 'J', L);
1687 end if;
1689 -- Construct "L .. H" in Index_Base. We use a qualified expression
1690 -- for the bound to convert to the index base, but we don't need
1691 -- to do that if we already have the base type at hand.
1693 if Etype (L) = Index_Base then
1694 L_L := New_Copy_Tree (L);
1695 else
1696 L_L :=
1697 Make_Qualified_Expression (Loc,
1698 Subtype_Mark => Index_Base_Name,
1699 Expression => New_Copy_Tree (L));
1700 end if;
1702 if Etype (H) = Index_Base then
1703 L_H := New_Copy_Tree (H);
1704 else
1705 L_H :=
1706 Make_Qualified_Expression (Loc,
1707 Subtype_Mark => Index_Base_Name,
1708 Expression => New_Copy_Tree (H));
1709 end if;
1711 L_Range :=
1712 Make_Range (Loc,
1713 Low_Bound => L_L,
1714 High_Bound => L_H);
1716 -- Construct "for L_J in Index_Base range L .. H"
1718 L_Iteration_Scheme :=
1719 Make_Iteration_Scheme (Loc,
1720 Loop_Parameter_Specification =>
1721 Make_Loop_Parameter_Specification (Loc,
1722 Defining_Identifier => L_J,
1723 Discrete_Subtype_Definition => L_Range));
1725 -- Construct the statements to execute in the loop body
1727 L_Body := Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr);
1729 -- Construct the final loop
1731 Append_To (S,
1732 Make_Implicit_Loop_Statement
1733 (Node => N,
1734 Identifier => Empty,
1735 Iteration_Scheme => L_Iteration_Scheme,
1736 Statements => L_Body));
1738 if Is_Iterated_Component then
1739 End_Scope;
1740 end if;
1742 -- A small optimization: if the aggregate is initialized with a box
1743 -- and the component type has no initialization procedure, remove the
1744 -- useless empty loop.
1746 if Nkind (First (S)) = N_Loop_Statement
1747 and then Is_Empty_List (Statements (First (S)))
1748 then
1749 return New_List (Make_Null_Statement (Loc));
1750 else
1751 return S;
1752 end if;
1753 end Gen_Loop;
1755 ---------------
1756 -- Gen_While --
1757 ---------------
1759 -- The code built is
1761 -- W_J : Index_Base := L;
1762 -- while W_J < H loop
1763 -- W_J := Index_Base'Succ (W);
1764 -- L_Body;
1765 -- end loop;
1767 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1768 W_J : Node_Id;
1770 W_Decl : Node_Id;
1771 -- W_J : Base_Type := L;
1773 W_Iteration_Scheme : Node_Id;
1774 -- while W_J < H
1776 W_Index_Succ : Node_Id;
1777 -- Index_Base'Succ (J)
1779 W_Increment : Node_Id;
1780 -- W_J := Index_Base'Succ (W)
1782 W_Body : constant List_Id := New_List;
1783 -- The statements to execute in the loop
1785 S : constant List_Id := New_List;
1786 -- list of statement
1788 begin
1789 -- If loop bounds define an empty range or are equal return null
1791 if Empty_Range (L, H) or else Equal (L, H) then
1792 Append_To (S, Make_Null_Statement (Loc));
1793 return S;
1794 end if;
1796 -- Build the decl of W_J
1798 W_J := Make_Temporary (Loc, 'J', L);
1799 W_Decl :=
1800 Make_Object_Declaration
1801 (Loc,
1802 Defining_Identifier => W_J,
1803 Object_Definition => Index_Base_Name,
1804 Expression => L);
1806 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1807 -- that in this particular case L is a fresh Expr generated by
1808 -- Add which we are the only ones to use.
1810 Append_To (S, W_Decl);
1812 -- Construct " while W_J < H"
1814 W_Iteration_Scheme :=
1815 Make_Iteration_Scheme
1816 (Loc,
1817 Condition => Make_Op_Lt
1818 (Loc,
1819 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1820 Right_Opnd => New_Copy_Tree (H)));
1822 -- Construct the statements to execute in the loop body
1824 W_Index_Succ :=
1825 Make_Attribute_Reference
1826 (Loc,
1827 Prefix => Index_Base_Name,
1828 Attribute_Name => Name_Succ,
1829 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1831 W_Increment :=
1832 Make_OK_Assignment_Statement
1833 (Loc,
1834 Name => New_Occurrence_Of (W_J, Loc),
1835 Expression => W_Index_Succ);
1837 Append_To (W_Body, W_Increment);
1839 Append_List_To (W_Body,
1840 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr));
1842 -- Construct the final loop
1844 Append_To (S,
1845 Make_Implicit_Loop_Statement
1846 (Node => N,
1847 Identifier => Empty,
1848 Iteration_Scheme => W_Iteration_Scheme,
1849 Statements => W_Body));
1851 return S;
1852 end Gen_While;
1854 --------------------
1855 -- Get_Assoc_Expr --
1856 --------------------
1858 -- Duplicate the expression in case we will be generating several loops.
1859 -- As a result the expression is no longer shared between the loops and
1860 -- is reevaluated for each such loop.
1862 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1863 Typ : constant Entity_Id := Base_Type (Etype (N));
1865 begin
1866 if Box_Present (Assoc) then
1867 if Present (Default_Aspect_Component_Value (Typ)) then
1868 return New_Copy_Tree (Default_Aspect_Component_Value (Typ));
1869 elsif Needs_Simple_Initialization (Ctype) then
1870 return New_Copy_Tree (Get_Simple_Init_Val (Ctype, N));
1871 else
1872 return Empty;
1873 end if;
1875 else
1876 -- The expression will be passed to Gen_Loop, which immediately
1877 -- calls Parent_Kind on it, so we set Parent when it matters.
1879 return
1880 Expr : constant Node_Id := New_Copy_Tree (Expression (Assoc))
1882 Copy_Parent (To => Expr, From => Expression (Assoc));
1883 end return;
1884 end if;
1885 end Get_Assoc_Expr;
1887 ---------------------
1888 -- Index_Base_Name --
1889 ---------------------
1891 function Index_Base_Name return Node_Id is
1892 begin
1893 return New_Occurrence_Of (Index_Base, Sloc (N));
1894 end Index_Base_Name;
1896 ------------------------------------
1897 -- Local_Compile_Time_Known_Value --
1898 ------------------------------------
1900 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1901 begin
1902 return Compile_Time_Known_Value (E)
1903 or else
1904 (Nkind (E) = N_Attribute_Reference
1905 and then Attribute_Name (E) = Name_Val
1906 and then Compile_Time_Known_Value (First (Expressions (E))));
1907 end Local_Compile_Time_Known_Value;
1909 ----------------------
1910 -- Local_Expr_Value --
1911 ----------------------
1913 function Local_Expr_Value (E : Node_Id) return Uint is
1914 begin
1915 if Compile_Time_Known_Value (E) then
1916 return Expr_Value (E);
1917 else
1918 return Expr_Value (First (Expressions (E)));
1919 end if;
1920 end Local_Expr_Value;
1922 -- Local variables
1924 New_Code : constant List_Id := New_List;
1926 Aggr_Bounds : constant Range_Nodes :=
1927 Get_Index_Bounds (Aggregate_Bounds (N));
1928 Aggr_L : Node_Id renames Aggr_Bounds.First;
1929 Aggr_H : Node_Id renames Aggr_Bounds.Last;
1930 -- The aggregate bounds of this specific subaggregate. Note that if the
1931 -- code generated by Build_Array_Aggr_Code is executed then these bounds
1932 -- are OK. Otherwise a Constraint_Error would have been raised.
1934 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1935 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1936 -- After Duplicate_Subexpr these are side-effect free
1938 Assoc : Node_Id;
1939 Choice : Node_Id;
1940 Expr : Node_Id;
1942 Bounds : Range_Nodes;
1943 Low : Node_Id renames Bounds.First;
1944 High : Node_Id renames Bounds.Last;
1946 Nb_Choices : Nat := 0;
1947 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1948 -- Used to sort all the different choice values
1950 Nb_Elements : Int;
1951 -- Number of elements in the positional aggregate
1953 Others_Assoc : Node_Id := Empty;
1955 -- Start of processing for Build_Array_Aggr_Code
1957 begin
1958 -- First before we start, a special case. If we have a bit packed
1959 -- array represented as a modular type, then clear the value to
1960 -- zero first, to ensure that unused bits are properly cleared.
1962 if Present (Typ)
1963 and then Is_Bit_Packed_Array (Typ)
1964 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
1965 then
1966 declare
1967 Zero : constant Node_Id := Make_Integer_Literal (Loc, Uint_0);
1968 begin
1969 Analyze_And_Resolve (Zero, Packed_Array_Impl_Type (Typ));
1970 Append_To (New_Code,
1971 Make_Assignment_Statement (Loc,
1972 Name => New_Copy_Tree (Into),
1973 Expression => Unchecked_Convert_To (Typ, Zero)));
1974 end;
1975 end if;
1977 -- If the component type contains tasks, we need to build a Master
1978 -- entity in the current scope, because it will be needed if build-
1979 -- in-place functions are called in the expanded code.
1981 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
1982 Build_Master_Entity (Defining_Identifier (Parent (N)));
1983 end if;
1985 -- STEP 1: Process component associations
1987 -- For those associations that may generate a loop, initialize
1988 -- Loop_Actions to collect inserted actions that may be crated.
1990 -- Skip this if no component associations
1992 if No (Expressions (N)) then
1994 -- STEP 1 (a): Sort the discrete choices
1996 Assoc := First (Component_Associations (N));
1997 while Present (Assoc) loop
1998 Choice := First (Choice_List (Assoc));
1999 while Present (Choice) loop
2000 if Nkind (Choice) = N_Others_Choice then
2001 Others_Assoc := Assoc;
2002 exit;
2003 end if;
2005 Bounds := Get_Index_Bounds (Choice);
2007 if Low /= High then
2008 Set_Loop_Actions (Assoc, New_List);
2009 end if;
2011 Nb_Choices := Nb_Choices + 1;
2013 Table (Nb_Choices) :=
2014 (Choice_Lo => Low,
2015 Choice_Hi => High,
2016 Choice_Node => Get_Assoc_Expr (Assoc));
2018 Next (Choice);
2019 end loop;
2021 Next (Assoc);
2022 end loop;
2024 -- If there is more than one set of choices these must be static
2025 -- and we can therefore sort them. Remember that Nb_Choices does not
2026 -- account for an others choice.
2028 if Nb_Choices > 1 then
2029 Sort_Case_Table (Table);
2030 end if;
2032 -- STEP 1 (b): take care of the whole set of discrete choices
2034 for J in 1 .. Nb_Choices loop
2035 Low := Table (J).Choice_Lo;
2036 High := Table (J).Choice_Hi;
2037 Expr := Table (J).Choice_Node;
2038 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2039 end loop;
2041 -- STEP 1 (c): generate the remaining loops to cover others choice
2042 -- We don't need to generate loops over empty gaps, but if there is
2043 -- a single empty range we must analyze the expression for semantics
2045 if Present (Others_Assoc) then
2046 declare
2047 First : Boolean := True;
2049 begin
2050 for J in 0 .. Nb_Choices loop
2051 if J = 0 then
2052 Low := Aggr_Low;
2053 else
2054 Low := Add (1, To => Table (J).Choice_Hi);
2055 end if;
2057 if J = Nb_Choices then
2058 High := Aggr_High;
2059 else
2060 High := Add (-1, To => Table (J + 1).Choice_Lo);
2061 end if;
2063 -- If this is an expansion within an init proc, make
2064 -- sure that discriminant references are replaced by
2065 -- the corresponding discriminal.
2067 if Inside_Init_Proc then
2068 if Is_Entity_Name (Low)
2069 and then Ekind (Entity (Low)) = E_Discriminant
2070 then
2071 Set_Entity (Low, Discriminal (Entity (Low)));
2072 end if;
2074 if Is_Entity_Name (High)
2075 and then Ekind (Entity (High)) = E_Discriminant
2076 then
2077 Set_Entity (High, Discriminal (Entity (High)));
2078 end if;
2079 end if;
2081 if First or else not Empty_Range (Low, High) then
2082 First := False;
2083 Set_Loop_Actions (Others_Assoc, New_List);
2084 Expr := Get_Assoc_Expr (Others_Assoc);
2085 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2086 end if;
2087 end loop;
2088 end;
2089 end if;
2091 -- STEP 2: Process positional components
2093 else
2094 -- STEP 2 (a): Generate the assignments for each positional element
2095 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2096 -- Aggr_L is analyzed and Add wants an analyzed expression.
2098 Expr := First (Expressions (N));
2099 Nb_Elements := -1;
2100 while Present (Expr) loop
2101 Nb_Elements := Nb_Elements + 1;
2102 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2103 To => New_Code);
2104 Next (Expr);
2105 end loop;
2107 -- STEP 2 (b): Generate final loop if an others choice is present.
2108 -- Here Nb_Elements gives the offset of the last positional element.
2110 if Present (Component_Associations (N)) then
2111 Assoc := Last (Component_Associations (N));
2113 if Nkind (Assoc) = N_Iterated_Component_Association then
2114 -- Ada 2022: generate a loop to have a proper scope for
2115 -- the identifier that typically appears in the expression.
2116 -- The lower bound of the loop is the position after all
2117 -- previous positional components.
2119 Append_List (Gen_Loop (Add (Nb_Elements + 1, To => Aggr_L),
2120 Aggr_High,
2121 Expression (Assoc)),
2122 To => New_Code);
2123 else
2124 -- Ada 2005 (AI-287)
2126 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2127 Aggr_High,
2128 Get_Assoc_Expr (Assoc)),
2129 To => New_Code);
2130 end if;
2131 end if;
2132 end if;
2134 return New_Code;
2135 end Build_Array_Aggr_Code;
2137 -------------------------------------
2138 -- Build_Assignment_With_Temporary --
2139 -------------------------------------
2141 function Build_Assignment_With_Temporary
2142 (Target : Node_Id;
2143 Typ : Entity_Id;
2144 Source : Node_Id) return List_Id
2146 Loc : constant Source_Ptr := Sloc (Source);
2148 Aggr_Code : List_Id;
2149 Tmp : Entity_Id;
2151 begin
2152 Aggr_Code := New_List;
2154 Tmp := Build_Temporary_On_Secondary_Stack (Loc, Typ, Aggr_Code);
2156 Append_To (Aggr_Code,
2157 Make_OK_Assignment_Statement (Loc,
2158 Name =>
2159 Make_Explicit_Dereference (Loc,
2160 Prefix => New_Occurrence_Of (Tmp, Loc)),
2161 Expression => Source));
2163 Append_To (Aggr_Code,
2164 Make_OK_Assignment_Statement (Loc,
2165 Name => Target,
2166 Expression =>
2167 Make_Explicit_Dereference (Loc,
2168 Prefix => New_Occurrence_Of (Tmp, Loc))));
2170 return Aggr_Code;
2171 end Build_Assignment_With_Temporary;
2173 ----------------------------
2174 -- Build_Record_Aggr_Code --
2175 ----------------------------
2177 function Build_Record_Aggr_Code
2178 (N : Node_Id;
2179 Typ : Entity_Id;
2180 Lhs : Node_Id) return List_Id
2182 Loc : constant Source_Ptr := Sloc (N);
2183 L : constant List_Id := New_List;
2184 N_Typ : constant Entity_Id := Etype (N);
2186 Comp : Node_Id;
2187 Instr : Node_Id;
2188 Ref : Node_Id;
2189 Target : Entity_Id;
2190 Comp_Type : Entity_Id;
2191 Selector : Entity_Id;
2192 Comp_Expr : Node_Id;
2193 Expr_Q : Node_Id;
2195 Ancestor_Is_Subtype_Mark : Boolean := False;
2197 Init_Typ : Entity_Id := Empty;
2199 Finalization_Done : Boolean := False;
2200 -- True if Generate_Finalization_Actions has already been called; calls
2201 -- after the first do nothing.
2203 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2204 -- Returns the value that the given discriminant of an ancestor type
2205 -- should receive (in the absence of a conflict with the value provided
2206 -- by an ancestor part of an extension aggregate).
2208 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2209 -- Check that each of the discriminant values defined by the ancestor
2210 -- part of an extension aggregate match the corresponding values
2211 -- provided by either an association of the aggregate or by the
2212 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2214 function Compatible_Int_Bounds
2215 (Agg_Bounds : Node_Id;
2216 Typ_Bounds : Node_Id) return Boolean;
2217 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2218 -- assumed that both bounds are integer ranges.
2220 procedure Generate_Finalization_Actions;
2221 -- Deal with the various controlled type data structure initializations
2222 -- (but only if it hasn't been done already).
2224 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2225 -- Returns the first discriminant association in the constraint
2226 -- associated with T, if any, otherwise returns Empty.
2228 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2229 -- If the ancestor part is an unconstrained type and further ancestors
2230 -- do not provide discriminants for it, check aggregate components for
2231 -- values of the discriminants.
2233 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2234 -- If Typ is derived, and constrains discriminants of the parent type,
2235 -- these discriminants are not components of the aggregate, and must be
2236 -- initialized. The assignments are appended to List. The same is done
2237 -- if Typ derives from an already constrained subtype of a discriminated
2238 -- parent type.
2240 procedure Init_Stored_Discriminants;
2241 -- If the type is derived and has inherited discriminants, generate
2242 -- explicit assignments for each, using the store constraint of the
2243 -- type. Note that both visible and stored discriminants must be
2244 -- initialized in case the derived type has some renamed and some
2245 -- constrained discriminants.
2247 procedure Init_Visible_Discriminants;
2248 -- If type has discriminants, retrieve their values from aggregate,
2249 -- and generate explicit assignments for each. This does not include
2250 -- discriminants inherited from ancestor, which are handled above.
2251 -- The type of the aggregate is a subtype created ealier using the
2252 -- given values of the discriminant components of the aggregate.
2254 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2255 -- Check whether Bounds is a range node and its lower and higher bounds
2256 -- are integers literals.
2258 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2259 -- If the aggregate contains a self-reference, traverse each expression
2260 -- to replace a possible self-reference with a reference to the proper
2261 -- component of the target of the assignment.
2263 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2264 -- If default expression of a component mentions a discriminant of the
2265 -- type, it must be rewritten as the discriminant of the target object.
2267 ---------------------------------
2268 -- Ancestor_Discriminant_Value --
2269 ---------------------------------
2271 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2272 Assoc : Node_Id;
2273 Assoc_Elmt : Elmt_Id;
2274 Aggr_Comp : Entity_Id;
2275 Corresp_Disc : Entity_Id;
2276 Current_Typ : Entity_Id := Base_Type (Typ);
2277 Parent_Typ : Entity_Id;
2278 Parent_Disc : Entity_Id;
2279 Save_Assoc : Node_Id := Empty;
2281 begin
2282 -- First check any discriminant associations to see if any of them
2283 -- provide a value for the discriminant.
2285 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2286 Assoc := First (Component_Associations (N));
2287 while Present (Assoc) loop
2288 Aggr_Comp := Entity (First (Choices (Assoc)));
2290 if Ekind (Aggr_Comp) = E_Discriminant then
2291 Save_Assoc := Expression (Assoc);
2293 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2294 while Present (Corresp_Disc) loop
2296 -- If found a corresponding discriminant then return the
2297 -- value given in the aggregate. (Note: this is not
2298 -- correct in the presence of side effects. ???)
2300 if Disc = Corresp_Disc then
2301 return Duplicate_Subexpr (Expression (Assoc));
2302 end if;
2304 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2305 end loop;
2306 end if;
2308 Next (Assoc);
2309 end loop;
2310 end if;
2312 -- No match found in aggregate, so chain up parent types to find
2313 -- a constraint that defines the value of the discriminant.
2315 Parent_Typ := Etype (Current_Typ);
2316 while Current_Typ /= Parent_Typ loop
2317 if Has_Discriminants (Parent_Typ)
2318 and then not Has_Unknown_Discriminants (Parent_Typ)
2319 then
2320 Parent_Disc := First_Discriminant (Parent_Typ);
2322 -- We either get the association from the subtype indication
2323 -- of the type definition itself, or from the discriminant
2324 -- constraint associated with the type entity (which is
2325 -- preferable, but it's not always present ???)
2327 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2328 then
2329 Assoc := Get_Constraint_Association (Current_Typ);
2330 Assoc_Elmt := No_Elmt;
2331 else
2332 Assoc_Elmt :=
2333 First_Elmt (Discriminant_Constraint (Current_Typ));
2334 Assoc := Node (Assoc_Elmt);
2335 end if;
2337 -- Traverse the discriminants of the parent type looking
2338 -- for one that corresponds.
2340 while Present (Parent_Disc) and then Present (Assoc) loop
2341 Corresp_Disc := Parent_Disc;
2342 while Present (Corresp_Disc)
2343 and then Disc /= Corresp_Disc
2344 loop
2345 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2346 end loop;
2348 if Disc = Corresp_Disc then
2349 if Nkind (Assoc) = N_Discriminant_Association then
2350 Assoc := Expression (Assoc);
2351 end if;
2353 -- If the located association directly denotes
2354 -- a discriminant, then use the value of a saved
2355 -- association of the aggregate. This is an approach
2356 -- used to handle certain cases involving multiple
2357 -- discriminants mapped to a single discriminant of
2358 -- a descendant. It's not clear how to locate the
2359 -- appropriate discriminant value for such cases. ???
2361 if Is_Entity_Name (Assoc)
2362 and then Ekind (Entity (Assoc)) = E_Discriminant
2363 then
2364 Assoc := Save_Assoc;
2365 end if;
2367 return Duplicate_Subexpr (Assoc);
2368 end if;
2370 Next_Discriminant (Parent_Disc);
2372 if No (Assoc_Elmt) then
2373 Next (Assoc);
2375 else
2376 Next_Elmt (Assoc_Elmt);
2378 if Present (Assoc_Elmt) then
2379 Assoc := Node (Assoc_Elmt);
2380 else
2381 Assoc := Empty;
2382 end if;
2383 end if;
2384 end loop;
2385 end if;
2387 Current_Typ := Parent_Typ;
2388 Parent_Typ := Etype (Current_Typ);
2389 end loop;
2391 -- In some cases there's no ancestor value to locate (such as
2392 -- when an ancestor part given by an expression defines the
2393 -- discriminant value).
2395 return Empty;
2396 end Ancestor_Discriminant_Value;
2398 ----------------------------------
2399 -- Check_Ancestor_Discriminants --
2400 ----------------------------------
2402 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2403 Discr : Entity_Id;
2404 Disc_Value : Node_Id;
2405 Cond : Node_Id;
2407 begin
2408 Discr := First_Discriminant (Base_Type (Anc_Typ));
2409 while Present (Discr) loop
2410 Disc_Value := Ancestor_Discriminant_Value (Discr);
2412 if Present (Disc_Value) then
2413 Cond := Make_Op_Ne (Loc,
2414 Left_Opnd =>
2415 Make_Selected_Component (Loc,
2416 Prefix => New_Copy_Tree (Target),
2417 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2418 Right_Opnd => Disc_Value);
2420 Append_To (L,
2421 Make_Raise_Constraint_Error (Loc,
2422 Condition => Cond,
2423 Reason => CE_Discriminant_Check_Failed));
2424 end if;
2426 Next_Discriminant (Discr);
2427 end loop;
2428 end Check_Ancestor_Discriminants;
2430 ---------------------------
2431 -- Compatible_Int_Bounds --
2432 ---------------------------
2434 function Compatible_Int_Bounds
2435 (Agg_Bounds : Node_Id;
2436 Typ_Bounds : Node_Id) return Boolean
2438 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2439 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2440 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2441 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2442 begin
2443 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2444 end Compatible_Int_Bounds;
2446 -----------------------------------
2447 -- Generate_Finalization_Actions --
2448 -----------------------------------
2450 procedure Generate_Finalization_Actions is
2451 begin
2452 -- Do the work only the first time this is called
2454 if Finalization_Done then
2455 return;
2456 end if;
2458 Finalization_Done := True;
2460 -- Determine the external finalization list. It is either the
2461 -- finalization list of the outer scope or the one coming from an
2462 -- outer aggregate. When the target is not a temporary, the proper
2463 -- scope is the scope of the target rather than the potentially
2464 -- transient current scope.
2466 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2467 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2468 Set_Assignment_OK (Ref);
2470 Append_To (L,
2471 Make_Procedure_Call_Statement (Loc,
2472 Name =>
2473 New_Occurrence_Of
2474 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2475 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2476 end if;
2477 end Generate_Finalization_Actions;
2479 --------------------------------
2480 -- Get_Constraint_Association --
2481 --------------------------------
2483 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2484 Indic : Node_Id;
2485 Typ : Entity_Id;
2487 begin
2488 Typ := T;
2490 -- If type is private, get constraint from full view. This was
2491 -- previously done in an instance context, but is needed whenever
2492 -- the ancestor part has a discriminant, possibly inherited through
2493 -- multiple derivations.
2495 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2496 Typ := Full_View (Typ);
2497 end if;
2499 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2501 -- Verify that the subtype indication carries a constraint
2503 if Nkind (Indic) = N_Subtype_Indication
2504 and then Present (Constraint (Indic))
2505 then
2506 return First (Constraints (Constraint (Indic)));
2507 end if;
2509 return Empty;
2510 end Get_Constraint_Association;
2512 -------------------------------------
2513 -- Get_Explicit_Discriminant_Value --
2514 -------------------------------------
2516 function Get_Explicit_Discriminant_Value
2517 (D : Entity_Id) return Node_Id
2519 Assoc : Node_Id;
2520 Choice : Node_Id;
2521 Val : Node_Id;
2523 begin
2524 -- The aggregate has been normalized and all associations have a
2525 -- single choice.
2527 Assoc := First (Component_Associations (N));
2528 while Present (Assoc) loop
2529 Choice := First (Choices (Assoc));
2531 if Chars (Choice) = Chars (D) then
2532 Val := Expression (Assoc);
2533 Remove (Assoc);
2534 return Val;
2535 end if;
2537 Next (Assoc);
2538 end loop;
2540 return Empty;
2541 end Get_Explicit_Discriminant_Value;
2543 -------------------------------
2544 -- Init_Hidden_Discriminants --
2545 -------------------------------
2547 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2548 function Is_Completely_Hidden_Discriminant
2549 (Discr : Entity_Id) return Boolean;
2550 -- Determine whether Discr is a completely hidden discriminant of
2551 -- type Typ.
2553 ---------------------------------------
2554 -- Is_Completely_Hidden_Discriminant --
2555 ---------------------------------------
2557 function Is_Completely_Hidden_Discriminant
2558 (Discr : Entity_Id) return Boolean
2560 Item : Entity_Id;
2562 begin
2563 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2564 -- completely hidden discriminants.
2566 Item := First_Entity (Typ);
2567 while Present (Item) loop
2568 if Ekind (Item) = E_Discriminant
2569 and then Is_Completely_Hidden (Item)
2570 and then Chars (Original_Record_Component (Item)) =
2571 Chars (Discr)
2572 then
2573 return True;
2574 end if;
2576 Next_Entity (Item);
2577 end loop;
2579 return False;
2580 end Is_Completely_Hidden_Discriminant;
2582 -- Local variables
2584 Base_Typ : Entity_Id;
2585 Discr : Entity_Id;
2586 Discr_Constr : Elmt_Id;
2587 Discr_Init : Node_Id;
2588 Discr_Val : Node_Id;
2589 In_Aggr_Type : Boolean;
2590 Par_Typ : Entity_Id;
2592 -- Start of processing for Init_Hidden_Discriminants
2594 begin
2595 -- The constraints on the hidden discriminants, if present, are kept
2596 -- in the Stored_Constraint list of the type itself, or in that of
2597 -- the base type. If not in the constraints of the aggregate itself,
2598 -- we examine ancestors to find discriminants that are not renamed
2599 -- by other discriminants but constrained explicitly.
2601 In_Aggr_Type := True;
2603 Base_Typ := Base_Type (Typ);
2604 while Is_Derived_Type (Base_Typ)
2605 and then
2606 (Present (Stored_Constraint (Base_Typ))
2607 or else
2608 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2609 loop
2610 Par_Typ := Etype (Base_Typ);
2612 if not Has_Discriminants (Par_Typ) then
2613 return;
2614 end if;
2616 Discr := First_Discriminant (Par_Typ);
2618 -- We know that one of the stored-constraint lists is present
2620 if Present (Stored_Constraint (Base_Typ)) then
2621 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2623 -- For private extension, stored constraint may be on full view
2625 elsif Is_Private_Type (Base_Typ)
2626 and then Present (Full_View (Base_Typ))
2627 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2628 then
2629 Discr_Constr :=
2630 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2632 -- Otherwise, no discriminant to process
2634 else
2635 Discr_Constr := No_Elmt;
2636 end if;
2638 while Present (Discr) and then Present (Discr_Constr) loop
2639 Discr_Val := Node (Discr_Constr);
2641 -- The parent discriminant is renamed in the derived type,
2642 -- nothing to initialize.
2644 -- type Deriv_Typ (Discr : ...)
2645 -- is new Parent_Typ (Discr => Discr);
2647 if Is_Entity_Name (Discr_Val)
2648 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2649 then
2650 null;
2652 -- When the parent discriminant is constrained at the type
2653 -- extension level, it does not appear in the derived type.
2655 -- type Deriv_Typ (Discr : ...)
2656 -- is new Parent_Typ (Discr => Discr,
2657 -- Hidden_Discr => Expression);
2659 elsif Is_Completely_Hidden_Discriminant (Discr) then
2660 null;
2662 -- Otherwise initialize the discriminant
2664 else
2665 Discr_Init :=
2666 Make_OK_Assignment_Statement (Loc,
2667 Name =>
2668 Make_Selected_Component (Loc,
2669 Prefix => New_Copy_Tree (Target),
2670 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2671 Expression => New_Copy_Tree (Discr_Val));
2673 Append_To (List, Discr_Init);
2674 end if;
2676 Next_Elmt (Discr_Constr);
2677 Next_Discriminant (Discr);
2678 end loop;
2680 In_Aggr_Type := False;
2681 Base_Typ := Base_Type (Par_Typ);
2682 end loop;
2683 end Init_Hidden_Discriminants;
2685 --------------------------------
2686 -- Init_Visible_Discriminants --
2687 --------------------------------
2689 procedure Init_Visible_Discriminants is
2690 Discriminant : Entity_Id;
2691 Discriminant_Value : Node_Id;
2693 begin
2694 Discriminant := First_Discriminant (Typ);
2695 while Present (Discriminant) loop
2696 Comp_Expr :=
2697 Make_Selected_Component (Loc,
2698 Prefix => New_Copy_Tree (Target),
2699 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2701 Discriminant_Value :=
2702 Get_Discriminant_Value
2703 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2705 Instr :=
2706 Make_OK_Assignment_Statement (Loc,
2707 Name => Comp_Expr,
2708 Expression => New_Copy_Tree (Discriminant_Value));
2710 Append_To (L, Instr);
2712 Next_Discriminant (Discriminant);
2713 end loop;
2714 end Init_Visible_Discriminants;
2716 -------------------------------
2717 -- Init_Stored_Discriminants --
2718 -------------------------------
2720 procedure Init_Stored_Discriminants is
2721 Discriminant : Entity_Id;
2722 Discriminant_Value : Node_Id;
2724 begin
2725 Discriminant := First_Stored_Discriminant (Typ);
2726 while Present (Discriminant) loop
2727 Comp_Expr :=
2728 Make_Selected_Component (Loc,
2729 Prefix => New_Copy_Tree (Target),
2730 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2732 Discriminant_Value :=
2733 Get_Discriminant_Value
2734 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2736 Instr :=
2737 Make_OK_Assignment_Statement (Loc,
2738 Name => Comp_Expr,
2739 Expression => New_Copy_Tree (Discriminant_Value));
2741 Append_To (L, Instr);
2743 Next_Stored_Discriminant (Discriminant);
2744 end loop;
2745 end Init_Stored_Discriminants;
2747 -------------------------
2748 -- Is_Int_Range_Bounds --
2749 -------------------------
2751 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2752 begin
2753 return Nkind (Bounds) = N_Range
2754 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2755 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2756 end Is_Int_Range_Bounds;
2758 ------------------
2759 -- Replace_Type --
2760 ------------------
2762 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2763 begin
2764 -- Note about the Is_Ancestor test below: aggregate components for
2765 -- self-referential types include attribute references to the current
2766 -- instance, of the form: Typ'access, etc. These references are
2767 -- rewritten as references to the target of the aggregate: the
2768 -- left-hand side of an assignment, the entity in a declaration,
2769 -- or a temporary. Without this test, we would improperly extend
2770 -- this rewriting to attribute references whose prefix is not the
2771 -- type of the aggregate.
2773 if Nkind (Expr) = N_Attribute_Reference
2774 and then Is_Entity_Name (Prefix (Expr))
2775 and then Is_Type (Entity (Prefix (Expr)))
2776 and then
2777 Is_Ancestor
2778 (Entity (Prefix (Expr)), Etype (N), Use_Full_View => True)
2779 then
2780 if Is_Entity_Name (Lhs) then
2781 Rewrite (Prefix (Expr), New_Occurrence_Of (Entity (Lhs), Loc));
2783 else
2784 Rewrite (Expr,
2785 Make_Attribute_Reference (Loc,
2786 Attribute_Name => Name_Unrestricted_Access,
2787 Prefix => New_Copy_Tree (Lhs)));
2788 Set_Analyzed (Parent (Expr), False);
2789 end if;
2790 end if;
2792 return OK;
2793 end Replace_Type;
2795 --------------------------
2796 -- Rewrite_Discriminant --
2797 --------------------------
2799 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2800 begin
2801 if Is_Entity_Name (Expr)
2802 and then Present (Entity (Expr))
2803 and then Ekind (Entity (Expr)) = E_In_Parameter
2804 and then Present (Discriminal_Link (Entity (Expr)))
2805 and then Scope (Discriminal_Link (Entity (Expr))) =
2806 Base_Type (Etype (N))
2807 then
2808 Rewrite (Expr,
2809 Make_Selected_Component (Loc,
2810 Prefix => New_Copy_Tree (Lhs),
2811 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2813 -- The generated code will be reanalyzed, but if the reference
2814 -- to the discriminant appears within an already analyzed
2815 -- expression (e.g. a conditional) we must set its proper entity
2816 -- now. Context is an initialization procedure.
2818 Analyze (Expr);
2819 end if;
2821 return OK;
2822 end Rewrite_Discriminant;
2824 procedure Replace_Discriminants is
2825 new Traverse_Proc (Rewrite_Discriminant);
2827 procedure Replace_Self_Reference is
2828 new Traverse_Proc (Replace_Type);
2830 -- Start of processing for Build_Record_Aggr_Code
2832 begin
2833 if Has_Self_Reference (N) then
2834 Replace_Self_Reference (N);
2835 end if;
2837 -- If the target of the aggregate is class-wide, we must convert it
2838 -- to the actual type of the aggregate, so that the proper components
2839 -- are visible. We know already that the types are compatible.
2841 if Present (Etype (Lhs)) and then Is_Class_Wide_Type (Etype (Lhs)) then
2842 Target := Unchecked_Convert_To (Typ, Lhs);
2843 else
2844 Target := Lhs;
2845 end if;
2847 -- Deal with the ancestor part of extension aggregates or with the
2848 -- discriminants of the root type.
2850 if Nkind (N) = N_Extension_Aggregate then
2851 declare
2852 Ancestor : constant Node_Id := Ancestor_Part (N);
2853 Ancestor_Q : constant Node_Id := Unqualify (Ancestor);
2855 Assign : List_Id;
2857 begin
2858 -- If the ancestor part is a subtype mark T, we generate
2860 -- init-proc (T (tmp)); if T is constrained and
2861 -- init-proc (S (tmp)); where S applies an appropriate
2862 -- constraint if T is unconstrained
2864 if Is_Entity_Name (Ancestor)
2865 and then Is_Type (Entity (Ancestor))
2866 then
2867 Ancestor_Is_Subtype_Mark := True;
2869 if Is_Constrained (Entity (Ancestor)) then
2870 Init_Typ := Entity (Ancestor);
2872 -- For an ancestor part given by an unconstrained type mark,
2873 -- create a subtype constrained by appropriate corresponding
2874 -- discriminant values coming from either associations of the
2875 -- aggregate or a constraint on a parent type. The subtype will
2876 -- be used to generate the correct default value for the
2877 -- ancestor part.
2879 elsif Has_Discriminants (Entity (Ancestor)) then
2880 declare
2881 Anc_Typ : constant Entity_Id := Entity (Ancestor);
2882 Anc_Constr : constant List_Id := New_List;
2883 Discrim : Entity_Id;
2884 Disc_Value : Node_Id;
2885 New_Indic : Node_Id;
2886 Subt_Decl : Node_Id;
2888 begin
2889 Discrim := First_Discriminant (Anc_Typ);
2890 while Present (Discrim) loop
2891 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2893 -- If no usable discriminant in ancestors, check
2894 -- whether aggregate has an explicit value for it.
2896 if No (Disc_Value) then
2897 Disc_Value :=
2898 Get_Explicit_Discriminant_Value (Discrim);
2899 end if;
2901 Append_To (Anc_Constr, Disc_Value);
2902 Next_Discriminant (Discrim);
2903 end loop;
2905 New_Indic :=
2906 Make_Subtype_Indication (Loc,
2907 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2908 Constraint =>
2909 Make_Index_Or_Discriminant_Constraint (Loc,
2910 Constraints => Anc_Constr));
2912 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2914 Subt_Decl :=
2915 Make_Subtype_Declaration (Loc,
2916 Defining_Identifier => Init_Typ,
2917 Subtype_Indication => New_Indic);
2919 -- Itypes must be analyzed with checks off Declaration
2920 -- must have a parent for proper handling of subsidiary
2921 -- actions.
2923 Set_Parent (Subt_Decl, N);
2924 Analyze (Subt_Decl, Suppress => All_Checks);
2925 end;
2926 end if;
2928 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2929 Set_Assignment_OK (Ref);
2931 if not Is_Interface (Init_Typ) then
2932 Append_List_To (L,
2933 Build_Initialization_Call (Loc,
2934 Id_Ref => Ref,
2935 Typ => Init_Typ,
2936 In_Init_Proc => Within_Init_Proc,
2937 With_Default_Init => Has_Default_Init_Comps (N)
2938 or else
2939 Has_Task (Base_Type (Init_Typ))));
2941 if Is_Constrained (Entity (Ancestor))
2942 and then Has_Discriminants (Entity (Ancestor))
2943 then
2944 Check_Ancestor_Discriminants (Entity (Ancestor));
2945 end if;
2947 -- If ancestor type has Default_Initialization_Condition,
2948 -- add a DIC check after the ancestor object is initialized
2949 -- by default.
2951 if Has_DIC (Entity (Ancestor))
2952 and then Present (DIC_Procedure (Entity (Ancestor)))
2953 then
2954 Append_To (L,
2955 Build_DIC_Call
2956 (Loc, New_Copy_Tree (Ref), Entity (Ancestor)));
2957 end if;
2958 end if;
2960 -- Handle calls to C++ constructors
2962 elsif Is_CPP_Constructor_Call (Ancestor) then
2963 Init_Typ := Etype (Ancestor);
2964 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2965 Set_Assignment_OK (Ref);
2967 Append_List_To (L,
2968 Build_Initialization_Call (Loc,
2969 Id_Ref => Ref,
2970 Typ => Init_Typ,
2971 In_Init_Proc => Within_Init_Proc,
2972 With_Default_Init => Has_Default_Init_Comps (N),
2973 Constructor_Ref => Ancestor));
2975 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2976 -- limited type, a recursive call expands the ancestor. Note that
2977 -- in the limited case, the ancestor part must be either a
2978 -- function call (possibly qualified) or aggregate (definitely
2979 -- qualified).
2981 elsif Is_Limited_Type (Etype (Ancestor))
2982 and then Nkind (Ancestor_Q) in N_Aggregate
2983 | N_Extension_Aggregate
2984 then
2985 Append_List_To (L,
2986 Build_Record_Aggr_Code
2987 (N => Ancestor_Q,
2988 Typ => Etype (Ancestor_Q),
2989 Lhs => Target));
2991 -- If the ancestor part is an expression E of type T, we generate
2993 -- T (tmp) := E;
2995 -- In Ada 2005, this includes the case of a (possibly qualified)
2996 -- limited function call. The assignment will later be turned into
2997 -- a build-in-place function call (for further details, see
2998 -- Make_Build_In_Place_Call_In_Assignment).
3000 else
3001 Init_Typ := Etype (Ancestor);
3003 -- If the ancestor part is an aggregate, force its full
3004 -- expansion, which was delayed.
3006 if Nkind (Ancestor_Q) in N_Aggregate | N_Extension_Aggregate
3007 then
3008 Set_Analyzed (Ancestor, False);
3009 Set_Analyzed (Expression (Ancestor), False);
3010 end if;
3012 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3014 Assign := New_List (
3015 Make_OK_Assignment_Statement (Loc,
3016 Name => Ref,
3017 Expression => Ancestor));
3019 -- Arrange for the component to be adjusted if need be (the
3020 -- call will be generated by Make_Tag_Ctrl_Assignment).
3022 if Needs_Finalization (Init_Typ)
3023 and then not Is_Limited_View (Init_Typ)
3024 then
3025 Set_No_Finalize_Actions (First (Assign));
3026 else
3027 Set_No_Ctrl_Actions (First (Assign));
3028 end if;
3030 Append_To (L,
3031 Make_Suppress_Block (Loc, Name_Discriminant_Check, Assign));
3033 if Has_Discriminants (Init_Typ) then
3034 Check_Ancestor_Discriminants (Init_Typ);
3035 end if;
3036 end if;
3037 end;
3039 -- Generate assignments of hidden discriminants. If the base type is
3040 -- an unchecked union, the discriminants are unknown to the back-end
3041 -- and absent from a value of the type, so assignments for them are
3042 -- not emitted.
3044 if Has_Discriminants (Typ)
3045 and then not Is_Unchecked_Union (Base_Type (Typ))
3046 then
3047 Init_Hidden_Discriminants (Typ, L);
3048 end if;
3050 -- Normal case (not an extension aggregate)
3052 else
3053 -- Generate the discriminant expressions, component by component.
3054 -- If the base type is an unchecked union, the discriminants are
3055 -- unknown to the back-end and absent from a value of the type, so
3056 -- assignments for them are not emitted.
3058 if Has_Discriminants (Typ)
3059 and then not Is_Unchecked_Union (Base_Type (Typ))
3060 then
3061 Init_Hidden_Discriminants (Typ, L);
3063 -- Generate discriminant init values for the visible discriminants
3065 Init_Visible_Discriminants;
3067 if Is_Derived_Type (N_Typ) then
3068 Init_Stored_Discriminants;
3069 end if;
3070 end if;
3071 end if;
3073 -- For CPP types we generate an implicit call to the C++ default
3074 -- constructor to ensure the proper initialization of the _Tag
3075 -- component.
3077 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3078 Invoke_Constructor : declare
3079 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3081 procedure Invoke_IC_Proc (T : Entity_Id);
3082 -- Recursive routine used to climb to parents. Required because
3083 -- parents must be initialized before descendants to ensure
3084 -- propagation of inherited C++ slots.
3086 --------------------
3087 -- Invoke_IC_Proc --
3088 --------------------
3090 procedure Invoke_IC_Proc (T : Entity_Id) is
3091 begin
3092 -- Avoid generating extra calls. Initialization required
3093 -- only for types defined from the level of derivation of
3094 -- type of the constructor and the type of the aggregate.
3096 if T = CPP_Parent then
3097 return;
3098 end if;
3100 Invoke_IC_Proc (Etype (T));
3102 -- Generate call to the IC routine
3104 if Present (CPP_Init_Proc (T)) then
3105 Append_To (L,
3106 Make_Procedure_Call_Statement (Loc,
3107 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3108 end if;
3109 end Invoke_IC_Proc;
3111 -- Start of processing for Invoke_Constructor
3113 begin
3114 -- Implicit invocation of the C++ constructor
3116 if Nkind (N) = N_Aggregate then
3117 Append_To (L,
3118 Make_Procedure_Call_Statement (Loc,
3119 Name =>
3120 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3121 Parameter_Associations => New_List (
3122 Unchecked_Convert_To (CPP_Parent,
3123 New_Copy_Tree (Lhs)))));
3124 end if;
3126 Invoke_IC_Proc (Typ);
3127 end Invoke_Constructor;
3128 end if;
3130 -- Generate the assignments, component by component
3132 -- tmp.comp1 := Expr1_From_Aggr;
3133 -- tmp.comp2 := Expr2_From_Aggr;
3134 -- ....
3136 Comp := First (Component_Associations (N));
3137 while Present (Comp) loop
3138 Selector := Entity (First (Choices (Comp)));
3139 pragma Assert (Present (Selector));
3141 -- C++ constructors
3143 if Is_CPP_Constructor_Call (Expression (Comp)) then
3144 Append_List_To (L,
3145 Build_Initialization_Call (Loc,
3146 Id_Ref =>
3147 Make_Selected_Component (Loc,
3148 Prefix => New_Copy_Tree (Target),
3149 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3150 Typ => Etype (Selector),
3151 Enclos_Type => Typ,
3152 With_Default_Init => True,
3153 Constructor_Ref => Expression (Comp)));
3155 elsif Box_Present (Comp)
3156 and then Needs_Simple_Initialization (Etype (Selector))
3157 then
3158 Comp_Expr :=
3159 Make_Selected_Component (Loc,
3160 Prefix => New_Copy_Tree (Target),
3161 Selector_Name => New_Occurrence_Of (Selector, Loc));
3163 Initialize_Component
3164 (N => N,
3165 Comp => Comp_Expr,
3166 Comp_Typ => Etype (Selector),
3167 Init_Expr => Get_Simple_Init_Val
3168 (Typ => Etype (Selector),
3169 N => Comp,
3170 Size =>
3171 (if Known_Esize (Selector)
3172 then Esize (Selector)
3173 else Uint_0)),
3174 Stmts => L);
3176 -- Ada 2005 (AI-287): For each default-initialized component generate
3177 -- a call to the corresponding IP subprogram if available.
3179 elsif Box_Present (Comp)
3180 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3181 then
3182 if Ekind (Selector) /= E_Discriminant then
3183 Generate_Finalization_Actions;
3184 end if;
3186 -- Ada 2005 (AI-287): If the component type has tasks then
3187 -- generate the activation chain and master entities (except
3188 -- in case of an allocator because in that case these entities
3189 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3191 declare
3192 Ctype : constant Entity_Id := Etype (Selector);
3193 Inside_Allocator : Boolean := False;
3194 P : Node_Id := Parent (N);
3196 begin
3197 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3198 while Present (P) loop
3199 if Nkind (P) = N_Allocator then
3200 Inside_Allocator := True;
3201 exit;
3202 end if;
3204 P := Parent (P);
3205 end loop;
3207 if not Inside_Init_Proc and not Inside_Allocator then
3208 Build_Activation_Chain_Entity (N);
3209 end if;
3210 end if;
3211 end;
3213 Append_List_To (L,
3214 Build_Initialization_Call (Loc,
3215 Id_Ref => Make_Selected_Component (Loc,
3216 Prefix => New_Copy_Tree (Target),
3217 Selector_Name =>
3218 New_Occurrence_Of (Selector, Loc)),
3219 Typ => Etype (Selector),
3220 Enclos_Type => Typ,
3221 With_Default_Init => True));
3223 -- Prepare for component assignment
3225 elsif Ekind (Selector) /= E_Discriminant
3226 or else Nkind (N) = N_Extension_Aggregate
3227 then
3228 -- All the discriminants have now been assigned
3230 -- This is now a good moment to initialize and attach all the
3231 -- controllers. Their position may depend on the discriminants.
3233 if Ekind (Selector) /= E_Discriminant then
3234 Generate_Finalization_Actions;
3235 end if;
3237 Comp_Type := Underlying_Type (Etype (Selector));
3238 Comp_Expr :=
3239 Make_Selected_Component (Loc,
3240 Prefix => New_Copy_Tree (Target),
3241 Selector_Name => New_Occurrence_Of (Selector, Loc));
3243 Expr_Q := Unqualify (Expression (Comp));
3245 -- Now either create the assignment or generate the code for the
3246 -- inner aggregate top-down.
3248 if Is_Delayed_Aggregate (Expr_Q) then
3250 -- We have the following case of aggregate nesting inside
3251 -- an object declaration:
3253 -- type Arr_Typ is array (Integer range <>) of ...;
3255 -- type Rec_Typ (...) is record
3256 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3257 -- end record;
3259 -- Obj_Rec_Typ : Rec_Typ := (...,
3260 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3262 -- The length of the ranges of the aggregate and Obj_Add_Typ
3263 -- are equal (B - A = Y - X), but they do not coincide (X /=
3264 -- A and B /= Y). This case requires array sliding which is
3265 -- performed in the following manner:
3267 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3268 -- Temp : Arr_Sub;
3269 -- Temp (X) := (...);
3270 -- ...
3271 -- Temp (Y) := (...);
3272 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3274 if Ekind (Comp_Type) = E_Array_Subtype
3275 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3276 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3277 and then not
3278 Compatible_Int_Bounds
3279 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3280 Typ_Bounds => First_Index (Comp_Type))
3281 then
3282 -- Create the array subtype with bounds equal to those of
3283 -- the corresponding aggregate.
3285 declare
3286 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3288 SubD : constant Node_Id :=
3289 Make_Subtype_Declaration (Loc,
3290 Defining_Identifier => SubE,
3291 Subtype_Indication =>
3292 Make_Subtype_Indication (Loc,
3293 Subtype_Mark =>
3294 New_Occurrence_Of (Etype (Comp_Type), Loc),
3295 Constraint =>
3296 Make_Index_Or_Discriminant_Constraint
3297 (Loc,
3298 Constraints => New_List (
3299 New_Copy_Tree
3300 (Aggregate_Bounds (Expr_Q))))));
3302 -- Create a temporary array of the above subtype which
3303 -- will be used to capture the aggregate assignments.
3305 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3307 TmpD : constant Node_Id :=
3308 Make_Object_Declaration (Loc,
3309 Defining_Identifier => TmpE,
3310 Object_Definition => New_Occurrence_Of (SubE, Loc));
3312 begin
3313 Set_No_Initialization (TmpD);
3314 Append_To (L, SubD);
3315 Append_To (L, TmpD);
3317 -- Expand aggregate into assignments to the temp array
3319 Append_List_To (L,
3320 Late_Expansion (Expr_Q, Comp_Type,
3321 New_Occurrence_Of (TmpE, Loc)));
3323 -- Slide
3325 Append_To (L,
3326 Make_Assignment_Statement (Loc,
3327 Name => New_Copy_Tree (Comp_Expr),
3328 Expression => New_Occurrence_Of (TmpE, Loc)));
3329 end;
3331 -- Normal case (sliding not required)
3333 else
3334 Append_List_To (L,
3335 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3336 end if;
3338 -- Expr_Q is not delayed aggregate
3340 else
3341 if Has_Discriminants (Typ) then
3342 Replace_Discriminants (Expr_Q);
3344 -- If the component is an array type that depends on
3345 -- discriminants, and the expression is a single Others
3346 -- clause, create an explicit subtype for it because the
3347 -- backend has troubles recovering the actual bounds.
3349 if Nkind (Expr_Q) = N_Aggregate
3350 and then Is_Array_Type (Comp_Type)
3351 and then Present (Component_Associations (Expr_Q))
3352 then
3353 declare
3354 Assoc : constant Node_Id :=
3355 First (Component_Associations (Expr_Q));
3356 Decl : Node_Id;
3358 begin
3359 if Present (Assoc)
3360 and then
3361 Nkind (First (Choices (Assoc))) = N_Others_Choice
3362 then
3363 Decl :=
3364 Build_Actual_Subtype_Of_Component
3365 (Comp_Type, Comp_Expr);
3367 -- If the component type does not in fact depend on
3368 -- discriminants, the subtype declaration is empty.
3370 if Present (Decl) then
3371 Append_To (L, Decl);
3372 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3373 end if;
3374 end if;
3375 end;
3376 end if;
3377 end if;
3379 if Modify_Tree_For_C
3380 and then Nkind (Expr_Q) = N_Aggregate
3381 and then Is_Array_Type (Etype (Expr_Q))
3382 and then Present (First_Index (Etype (Expr_Q)))
3383 then
3384 declare
3385 Expr_Q_Type : constant Entity_Id := Etype (Expr_Q);
3386 begin
3387 Append_List_To (L,
3388 Build_Array_Aggr_Code
3389 (N => Expr_Q,
3390 Ctype => Component_Type (Expr_Q_Type),
3391 Index => First_Index (Expr_Q_Type),
3392 Into => Comp_Expr,
3393 Scalar_Comp =>
3394 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
3395 end;
3397 else
3398 Initialize_Component
3399 (N => N,
3400 Comp => Comp_Expr,
3401 Comp_Typ => Etype (Selector),
3402 Init_Expr => Expr_Q,
3403 Stmts => L);
3404 end if;
3405 end if;
3407 -- comment would be good here ???
3409 elsif Ekind (Selector) = E_Discriminant
3410 and then Nkind (N) /= N_Extension_Aggregate
3411 and then Nkind (Parent (N)) = N_Component_Association
3412 and then Is_Constrained (Typ)
3413 then
3414 -- We must check that the discriminant value imposed by the
3415 -- context is the same as the value given in the subaggregate,
3416 -- because after the expansion into assignments there is no
3417 -- record on which to perform a regular discriminant check.
3419 declare
3420 D_Val : Elmt_Id;
3421 Disc : Entity_Id;
3423 begin
3424 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3425 Disc := First_Discriminant (Typ);
3426 while Chars (Disc) /= Chars (Selector) loop
3427 Next_Discriminant (Disc);
3428 Next_Elmt (D_Val);
3429 end loop;
3431 pragma Assert (Present (D_Val));
3433 -- This check cannot performed for components that are
3434 -- constrained by a current instance, because this is not a
3435 -- value that can be compared with the actual constraint.
3437 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3438 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3439 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3440 then
3441 Append_To (L,
3442 Make_Raise_Constraint_Error (Loc,
3443 Condition =>
3444 Make_Op_Ne (Loc,
3445 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3446 Right_Opnd => Expression (Comp)),
3447 Reason => CE_Discriminant_Check_Failed));
3449 else
3450 -- Find self-reference in previous discriminant assignment,
3451 -- and replace with proper expression.
3453 declare
3454 Ass : Node_Id;
3456 begin
3457 Ass := First (L);
3458 while Present (Ass) loop
3459 if Nkind (Ass) = N_Assignment_Statement
3460 and then Nkind (Name (Ass)) = N_Selected_Component
3461 and then Chars (Selector_Name (Name (Ass))) =
3462 Chars (Disc)
3463 then
3464 Set_Expression
3465 (Ass, New_Copy_Tree (Expression (Comp)));
3466 exit;
3467 end if;
3468 Next (Ass);
3469 end loop;
3470 end;
3471 end if;
3472 end;
3473 end if;
3475 -- If the component association was specified with a box and the
3476 -- component type has a Default_Initial_Condition, then generate
3477 -- a call to the DIC procedure.
3479 if Has_DIC (Etype (Selector))
3480 and then Was_Default_Init_Box_Association (Comp)
3481 and then Present (DIC_Procedure (Etype (Selector)))
3482 then
3483 Append_To (L,
3484 Build_DIC_Call (Loc,
3485 Make_Selected_Component (Loc,
3486 Prefix => New_Copy_Tree (Target),
3487 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3488 Etype (Selector)));
3489 end if;
3491 Next (Comp);
3492 end loop;
3494 -- For CPP types we generated a call to the C++ default constructor
3495 -- before the components have been initialized to ensure the proper
3496 -- initialization of the _Tag component (see above).
3498 if Is_CPP_Class (Typ) then
3499 null;
3501 -- If the type is tagged, the tag needs to be initialized (unless we
3502 -- are in VM-mode where tags are implicit). It is done late in the
3503 -- initialization process because in some cases, we call the init
3504 -- proc of an ancestor which will not leave out the right tag.
3506 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3507 Instr :=
3508 Make_Tag_Assignment_From_Type
3509 (Loc, New_Copy_Tree (Target), Base_Type (Typ));
3511 Append_To (L, Instr);
3513 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3514 -- abstract interfaces we must also initialize the tags of the
3515 -- secondary dispatch tables.
3517 if Has_Interfaces (Base_Type (Typ)) then
3518 Init_Secondary_Tags
3519 (Typ => Base_Type (Typ),
3520 Target => Target,
3521 Stmts_List => L,
3522 Init_Tags_List => L);
3523 end if;
3524 end if;
3526 -- If the controllers have not been initialized yet (by lack of non-
3527 -- discriminant components), let's do it now.
3529 Generate_Finalization_Actions;
3531 return L;
3532 end Build_Record_Aggr_Code;
3534 -------------------------------
3535 -- Convert_Aggr_In_Allocator --
3536 -------------------------------
3538 procedure Convert_Aggr_In_Allocator
3539 (Alloc : Node_Id;
3540 Decl : Node_Id;
3541 Aggr : Node_Id)
3543 Loc : constant Source_Ptr := Sloc (Aggr);
3544 Typ : constant Entity_Id := Etype (Aggr);
3545 Temp : constant Entity_Id := Defining_Identifier (Decl);
3547 Occ : constant Node_Id :=
3548 Unchecked_Convert_To (Typ,
3549 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3551 begin
3552 if Is_Array_Type (Typ) then
3553 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3555 elsif Has_Default_Init_Comps (Aggr) then
3556 declare
3557 L : constant List_Id := New_List;
3558 Init_Stmts : List_Id;
3560 begin
3561 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3563 if Has_Task (Typ) then
3564 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3565 Insert_Actions (Alloc, L);
3566 else
3567 Insert_Actions (Alloc, Init_Stmts);
3568 end if;
3569 end;
3571 else
3572 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3573 end if;
3574 end Convert_Aggr_In_Allocator;
3576 --------------------------------
3577 -- Convert_Aggr_In_Assignment --
3578 --------------------------------
3580 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3581 Aggr : constant Node_Id := Unqualify (Expression (N));
3582 Typ : constant Entity_Id := Etype (Aggr);
3583 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3585 begin
3586 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3587 end Convert_Aggr_In_Assignment;
3589 ---------------------------------
3590 -- Convert_Aggr_In_Object_Decl --
3591 ---------------------------------
3593 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3594 Obj : constant Entity_Id := Defining_Identifier (N);
3595 Aggr : constant Node_Id := Unqualify (Expression (N));
3596 Loc : constant Source_Ptr := Sloc (Aggr);
3597 Typ : constant Entity_Id := Etype (Aggr);
3598 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3600 Has_Transient_Scope : Boolean := False;
3602 function Discriminants_Ok return Boolean;
3603 -- If the object type is constrained, the discriminants in the
3604 -- aggregate must be checked against the discriminants of the subtype.
3605 -- This cannot be done using Apply_Discriminant_Checks because after
3606 -- expansion there is no aggregate left to check.
3608 ----------------------
3609 -- Discriminants_Ok --
3610 ----------------------
3612 function Discriminants_Ok return Boolean is
3613 Cond : Node_Id := Empty;
3614 Check : Node_Id;
3615 D : Entity_Id;
3616 Disc1 : Elmt_Id;
3617 Disc2 : Elmt_Id;
3618 Val1 : Node_Id;
3619 Val2 : Node_Id;
3621 begin
3622 D := First_Discriminant (Typ);
3623 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3624 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3625 while Present (Disc1) and then Present (Disc2) loop
3626 Val1 := Node (Disc1);
3627 Val2 := Node (Disc2);
3629 if not Is_OK_Static_Expression (Val1)
3630 or else not Is_OK_Static_Expression (Val2)
3631 then
3632 Check := Make_Op_Ne (Loc,
3633 Left_Opnd => Duplicate_Subexpr (Val1),
3634 Right_Opnd => Duplicate_Subexpr (Val2));
3636 if No (Cond) then
3637 Cond := Check;
3639 else
3640 Cond := Make_Or_Else (Loc,
3641 Left_Opnd => Cond,
3642 Right_Opnd => Check);
3643 end if;
3645 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3646 Apply_Compile_Time_Constraint_Error (Aggr,
3647 Msg => "incorrect value for discriminant&??",
3648 Reason => CE_Discriminant_Check_Failed,
3649 Ent => D);
3650 return False;
3651 end if;
3653 Next_Discriminant (D);
3654 Next_Elmt (Disc1);
3655 Next_Elmt (Disc2);
3656 end loop;
3658 -- If any discriminant constraint is nonstatic, emit a check
3660 if Present (Cond) then
3661 Insert_Action (N,
3662 Make_Raise_Constraint_Error (Loc,
3663 Condition => Cond,
3664 Reason => CE_Discriminant_Check_Failed));
3665 end if;
3667 return True;
3668 end Discriminants_Ok;
3670 -- Start of processing for Convert_Aggr_In_Object_Decl
3672 begin
3673 Set_Assignment_OK (Occ);
3675 if Has_Discriminants (Typ)
3676 and then Typ /= Etype (Obj)
3677 and then Is_Constrained (Etype (Obj))
3678 and then not Discriminants_Ok
3679 then
3680 return;
3681 end if;
3683 -- If the context is an extended return statement, it has its own
3684 -- finalization machinery (i.e. works like a transient scope) and
3685 -- we do not want to create an additional one, because objects on
3686 -- the finalization list of the return must be moved to the caller's
3687 -- finalization list to complete the return.
3689 -- Similarly if the aggregate is limited, it is built in place, and the
3690 -- controlled components are not assigned to intermediate temporaries
3691 -- so there is no need for a transient scope in this case either.
3693 if Requires_Transient_Scope (Typ)
3694 and then Ekind (Current_Scope) /= E_Return_Statement
3695 and then not Is_Limited_Type (Typ)
3696 then
3697 Establish_Transient_Scope (Aggr, Manage_Sec_Stack => False);
3698 Has_Transient_Scope := True;
3699 end if;
3701 declare
3702 Stmts : constant List_Id := Late_Expansion (Aggr, Typ, Occ);
3703 Stmt : Node_Id;
3704 Param : Node_Id;
3706 begin
3707 -- If Obj is already frozen or if N is wrapped in a transient scope,
3708 -- Stmts do not need to be saved in Initialization_Statements since
3709 -- there is no freezing issue.
3711 if Is_Frozen (Obj) or else Has_Transient_Scope then
3712 Insert_Actions_After (N, Stmts);
3713 else
3714 Stmt := Make_Compound_Statement (Sloc (N), Actions => Stmts);
3715 Insert_Action_After (N, Stmt);
3717 -- Insert_Action_After may freeze Obj in which case we should
3718 -- remove the compound statement just created and simply insert
3719 -- Stmts after N.
3721 if Is_Frozen (Obj) then
3722 Remove (Stmt);
3723 Insert_Actions_After (N, Stmts);
3724 else
3725 Set_Initialization_Statements (Obj, Stmt);
3726 end if;
3727 end if;
3729 -- If Typ has controlled components and a call to a Slice_Assign
3730 -- procedure is part of the initialization statements, then we
3731 -- need to initialize the array component since Slice_Assign will
3732 -- need to adjust it.
3734 if Has_Controlled_Component (Typ) then
3735 Stmt := First (Stmts);
3737 while Present (Stmt) loop
3738 if Nkind (Stmt) = N_Procedure_Call_Statement
3739 and then Is_TSS (Entity (Name (Stmt)), TSS_Slice_Assign)
3740 then
3741 Param := First (Parameter_Associations (Stmt));
3742 Insert_Actions
3743 (Stmt,
3744 Build_Initialization_Call
3745 (Sloc (N), New_Copy_Tree (Param), Etype (Param)));
3746 end if;
3748 Next (Stmt);
3749 end loop;
3750 end if;
3751 end;
3753 Set_No_Initialization (N);
3755 -- After expansion the expression can be removed from the declaration
3756 -- except if the object is class-wide, in which case the aggregate
3757 -- provides the actual type.
3759 if not Is_Class_Wide_Type (Etype (Obj)) then
3760 Set_Expression (N, Empty);
3761 end if;
3763 Initialize_Discriminants (N, Typ);
3764 end Convert_Aggr_In_Object_Decl;
3766 -------------------------------------
3767 -- Convert_Array_Aggr_In_Allocator --
3768 -------------------------------------
3770 procedure Convert_Array_Aggr_In_Allocator
3771 (Decl : Node_Id;
3772 Aggr : Node_Id;
3773 Target : Node_Id)
3775 Typ : constant Entity_Id := Etype (Aggr);
3776 Ctyp : constant Entity_Id := Component_Type (Typ);
3777 Aggr_Code : List_Id;
3778 New_Aggr : Node_Id;
3780 begin
3781 -- The target is an explicit dereference of the allocated object
3783 -- If the assignment can be done directly by the back end, then
3784 -- reset Set_Expansion_Delayed and do not expand further.
3786 if not CodePeer_Mode
3787 and then not Modify_Tree_For_C
3788 and then Aggr_Assignment_OK_For_Backend (Aggr)
3789 then
3790 New_Aggr := New_Copy_Tree (Aggr);
3791 Set_Expansion_Delayed (New_Aggr, False);
3793 -- In the case of Target's type using the Designated_Storage_Model
3794 -- aspect with a Copy_To procedure, insert a temporary and have the
3795 -- back end handle the assignment to it. Copy the result to the
3796 -- original target.
3798 if Has_Designated_Storage_Model_Aspect
3799 (Etype (Prefix (Expression (Target))))
3800 and then Present (Storage_Model_Copy_To
3801 (Storage_Model_Object
3802 (Etype (Prefix (Expression (Target))))))
3803 then
3804 Aggr_Code :=
3805 Build_Assignment_With_Temporary (Target, Typ, New_Aggr);
3807 else
3808 Aggr_Code :=
3809 New_List (
3810 Make_OK_Assignment_Statement (Sloc (New_Aggr),
3811 Name => Target,
3812 Expression => New_Aggr));
3813 end if;
3815 -- Or else, generate component assignments to it, as for an aggregate
3816 -- that appears on the right-hand side of an assignment statement.
3817 else
3818 Aggr_Code :=
3819 Build_Array_Aggr_Code (Aggr,
3820 Ctype => Ctyp,
3821 Index => First_Index (Typ),
3822 Into => Target,
3823 Scalar_Comp => Is_Scalar_Type (Ctyp));
3824 end if;
3826 Insert_Actions_After (Decl, Aggr_Code);
3827 end Convert_Array_Aggr_In_Allocator;
3829 ------------------------
3830 -- In_Place_Assign_OK --
3831 ------------------------
3833 function In_Place_Assign_OK
3834 (N : Node_Id;
3835 Target_Object : Entity_Id := Empty) return Boolean
3837 Is_Array : constant Boolean := Is_Array_Type (Etype (N));
3839 Aggr_In : Node_Id;
3840 Aggr_Bounds : Range_Nodes;
3841 Obj_In : Node_Id;
3842 Obj_Bounds : Range_Nodes;
3843 Parent_Kind : Node_Kind;
3844 Parent_Node : Node_Id;
3846 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
3847 -- Check recursively that each component of a (sub)aggregate does not
3848 -- depend on the variable being assigned to.
3850 function Safe_Component (Expr : Node_Id) return Boolean;
3851 -- Verify that an expression cannot depend on the target being assigned
3852 -- to. Return true for compile-time known values, stand-alone objects,
3853 -- parameters passed by copy, calls to functions that return by copy,
3854 -- selected components thereof only if the aggregate's type is an array,
3855 -- indexed components and slices thereof only if the aggregate's type is
3856 -- a record, and simple expressions involving only these as operands.
3857 -- This is OK whatever the target because, for a component to overlap
3858 -- with the target, it must be either a direct reference to a component
3859 -- of the target, in which case there must be a matching selection or
3860 -- indexation or slicing, or an indirect reference to such a component,
3861 -- which is excluded by the above condition. Additionally, if the target
3862 -- is statically known, return true for arbitrarily nested selections,
3863 -- indexations or slicings, provided that their ultimate prefix is not
3864 -- the target itself.
3866 --------------------
3867 -- Safe_Aggregate --
3868 --------------------
3870 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
3871 Expr : Node_Id;
3873 begin
3874 if Nkind (Parent (Aggr)) = N_Iterated_Component_Association then
3875 return False;
3876 end if;
3878 if Present (Expressions (Aggr)) then
3879 Expr := First (Expressions (Aggr));
3880 while Present (Expr) loop
3881 if Nkind (Expr) = N_Aggregate then
3882 if not Safe_Aggregate (Expr) then
3883 return False;
3884 end if;
3886 elsif not Safe_Component (Expr) then
3887 return False;
3888 end if;
3890 Next (Expr);
3891 end loop;
3892 end if;
3894 if Present (Component_Associations (Aggr)) then
3895 Expr := First (Component_Associations (Aggr));
3896 while Present (Expr) loop
3897 if Nkind (Expression (Expr)) = N_Aggregate then
3898 if not Safe_Aggregate (Expression (Expr)) then
3899 return False;
3900 end if;
3902 -- If association has a box, no way to determine yet whether
3903 -- default can be assigned in place.
3905 elsif Box_Present (Expr) then
3906 return False;
3908 elsif not Safe_Component (Expression (Expr)) then
3909 return False;
3910 end if;
3912 Next (Expr);
3913 end loop;
3914 end if;
3916 return True;
3917 end Safe_Aggregate;
3919 --------------------
3920 -- Safe_Component --
3921 --------------------
3923 function Safe_Component (Expr : Node_Id) return Boolean is
3924 Comp : Node_Id := Expr;
3926 function Check_Component (C : Node_Id; T_OK : Boolean) return Boolean;
3927 -- Do the recursive traversal, after copy. If T_OK is True, return
3928 -- True for a stand-alone object only if the target is statically
3929 -- known and distinct from the object. At the top level, we start
3930 -- with T_OK set to False and set it to True at a deeper level only
3931 -- if we cannot disambiguate the component here without statically
3932 -- knowing the target. Note that this is not optimal, we should do
3933 -- something along the lines of Denotes_Same_Prefix for that.
3935 ---------------------
3936 -- Check_Component --
3937 ---------------------
3939 function Check_Component (C : Node_Id; T_OK : Boolean) return Boolean
3942 function SDO (E : Entity_Id) return Uint;
3943 -- Return the Scope Depth Of the enclosing dynamic scope of E
3945 ---------
3946 -- SDO --
3947 ---------
3949 function SDO (E : Entity_Id) return Uint is
3950 begin
3951 return Scope_Depth (Enclosing_Dynamic_Scope (E));
3952 end SDO;
3954 -- Start of processing for Check_Component
3956 begin
3957 if Is_Overloaded (C) then
3958 return False;
3960 elsif Compile_Time_Known_Value (C) then
3961 return True;
3962 end if;
3964 case Nkind (C) is
3965 when N_Attribute_Reference =>
3966 return Check_Component (Prefix (C), T_OK);
3968 when N_Function_Call =>
3969 if Nkind (Name (C)) = N_Explicit_Dereference then
3970 return not Returns_By_Ref (Etype (Name (C)));
3971 else
3972 return not Returns_By_Ref (Entity (Name (C)));
3973 end if;
3975 when N_Indexed_Component | N_Slice =>
3976 -- In a target record, these operations cannot determine
3977 -- alone a component so we can recurse whatever the target.
3978 return Check_Component (Prefix (C), T_OK or else Is_Array);
3980 when N_Selected_Component =>
3981 -- In a target array, this operation cannot determine alone
3982 -- a component so we can recurse whatever the target.
3983 return
3984 Check_Component (Prefix (C), T_OK or else not Is_Array);
3986 when N_Type_Conversion | N_Unchecked_Type_Conversion =>
3987 return Check_Component (Expression (C), T_OK);
3989 when N_Binary_Op =>
3990 return Check_Component (Left_Opnd (C), T_OK)
3991 and then Check_Component (Right_Opnd (C), T_OK);
3993 when N_Unary_Op =>
3994 return Check_Component (Right_Opnd (C), T_OK);
3996 when others =>
3997 if Is_Entity_Name (C) and then Is_Object (Entity (C)) then
3998 -- Case of a formal parameter component. It's either
3999 -- trivial if passed by copy or very annoying if not,
4000 -- because in the latter case it's almost equivalent
4001 -- to a dereference, so the path-based disambiguation
4002 -- logic is totally off and we always need the target.
4004 if Is_Formal (Entity (C)) then
4006 -- If it is passed by copy, then this is safe
4008 if Mechanism (Entity (C)) = By_Copy then
4009 return True;
4011 -- Otherwise, this is safe if the target is present
4012 -- and is at least as deeply nested as the component.
4014 else
4015 return Present (Target_Object)
4016 and then not Is_Formal (Target_Object)
4017 and then SDO (Target_Object) >= SDO (Entity (C));
4018 end if;
4020 -- For a renamed object, recurse
4022 elsif Present (Renamed_Object (Entity (C))) then
4023 return
4024 Check_Component (Renamed_Object (Entity (C)), T_OK);
4026 -- If this is safe whatever the target, we are done
4028 elsif not T_OK then
4029 return True;
4031 -- If there is no target or the component is the target,
4032 -- this is not safe.
4034 elsif No (Target_Object)
4035 or else Entity (C) = Target_Object
4036 then
4037 return False;
4039 -- Case of a formal parameter target. This is safe if it
4040 -- is at most as deeply nested as the component.
4042 elsif Is_Formal (Target_Object) then
4043 return SDO (Target_Object) <= SDO (Entity (C));
4045 -- For distinct stand-alone objects, this is safe
4047 else
4048 return True;
4049 end if;
4051 -- For anything else than an object, this is not safe
4053 else
4054 return False;
4055 end if;
4056 end case;
4057 end Check_Component;
4059 -- Start of processing for Safe_Component
4061 begin
4062 -- If the component appears in an association that may correspond
4063 -- to more than one element, it is not analyzed before expansion
4064 -- into assignments, to avoid side effects. We analyze, but do not
4065 -- resolve the copy, to obtain sufficient entity information for
4066 -- the checks that follow. If component is overloaded we assume
4067 -- an unsafe function call.
4069 if not Analyzed (Comp) then
4070 if Is_Overloaded (Expr) then
4071 return False;
4073 elsif Nkind (Expr) = N_Allocator then
4075 -- For now, too complex to analyze
4077 return False;
4079 elsif Nkind (Parent (Expr)) = N_Iterated_Component_Association then
4081 -- Ditto for iterated component associations, which in general
4082 -- require an enclosing loop and involve nonstatic expressions.
4084 return False;
4085 end if;
4087 Comp := New_Copy_Tree (Expr);
4088 Set_Parent (Comp, Parent (Expr));
4089 Analyze (Comp);
4090 end if;
4092 if Nkind (Comp) = N_Aggregate then
4093 return Safe_Aggregate (Comp);
4094 else
4095 return Check_Component (Comp, False);
4096 end if;
4097 end Safe_Component;
4099 -- Start of processing for In_Place_Assign_OK
4101 begin
4102 -- By-copy semantic cannot be guaranteed for controlled objects
4104 if Needs_Finalization (Etype (N)) then
4105 return False;
4106 end if;
4108 Parent_Node := Parent (N);
4109 Parent_Kind := Nkind (Parent_Node);
4111 if Parent_Kind = N_Qualified_Expression then
4112 Parent_Node := Parent (Parent_Node);
4113 Parent_Kind := Nkind (Parent_Node);
4114 end if;
4116 -- On assignment, sliding can take place, so we cannot do the
4117 -- assignment in place unless the bounds of the aggregate are
4118 -- statically equal to those of the target.
4120 -- If the aggregate is given by an others choice, the bounds are
4121 -- derived from the left-hand side, and the assignment is safe if
4122 -- the expression is.
4124 if Is_Array
4125 and then Present (Component_Associations (N))
4126 and then not Is_Others_Aggregate (N)
4127 then
4128 Aggr_In := First_Index (Etype (N));
4130 -- Context is an assignment
4132 if Parent_Kind = N_Assignment_Statement then
4133 Obj_In := First_Index (Etype (Name (Parent_Node)));
4135 -- Context is an allocator. Check the bounds of the aggregate against
4136 -- those of the designated type, except in the case where the type is
4137 -- unconstrained (and then we can directly return true, see below).
4139 else pragma Assert (Parent_Kind = N_Allocator);
4140 declare
4141 Desig_Typ : constant Entity_Id :=
4142 Designated_Type (Etype (Parent_Node));
4143 begin
4144 if not Is_Constrained (Desig_Typ) then
4145 return True;
4146 end if;
4148 Obj_In := First_Index (Desig_Typ);
4149 end;
4150 end if;
4152 while Present (Aggr_In) loop
4153 Aggr_Bounds := Get_Index_Bounds (Aggr_In);
4154 Obj_Bounds := Get_Index_Bounds (Obj_In);
4156 -- We require static bounds for the target and a static matching
4157 -- of low bound for the aggregate.
4159 if not Compile_Time_Known_Value (Obj_Bounds.First)
4160 or else not Compile_Time_Known_Value (Obj_Bounds.Last)
4161 or else not Compile_Time_Known_Value (Aggr_Bounds.First)
4162 or else Expr_Value (Aggr_Bounds.First) /=
4163 Expr_Value (Obj_Bounds.First)
4164 then
4165 return False;
4167 -- For an assignment statement we require static matching of
4168 -- bounds. Ditto for an allocator whose qualified expression
4169 -- is a constrained type. If the expression in the allocator
4170 -- is an unconstrained array, we accept an upper bound that
4171 -- is not static, to allow for nonstatic expressions of the
4172 -- base type. Clearly there are further possibilities (with
4173 -- diminishing returns) for safely building arrays in place
4174 -- here.
4176 elsif Parent_Kind = N_Assignment_Statement
4177 or else Is_Constrained (Etype (Parent_Node))
4178 then
4179 if not Compile_Time_Known_Value (Aggr_Bounds.Last)
4180 or else Expr_Value (Aggr_Bounds.Last) /=
4181 Expr_Value (Obj_Bounds.Last)
4182 then
4183 return False;
4184 end if;
4185 end if;
4187 Next_Index (Aggr_In);
4188 Next_Index (Obj_In);
4189 end loop;
4190 end if;
4192 -- Now check the component values themselves, except for an allocator
4193 -- for which the target is newly allocated memory.
4195 if Parent_Kind = N_Allocator then
4196 return True;
4197 else
4198 return Safe_Aggregate (N);
4199 end if;
4200 end In_Place_Assign_OK;
4202 ----------------------------
4203 -- Convert_To_Assignments --
4204 ----------------------------
4206 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4207 Loc : constant Source_Ptr := Sloc (N);
4208 T : Entity_Id;
4209 Temp : Entity_Id;
4211 Aggr_Code : List_Id;
4212 Instr : Node_Id;
4213 Target_Expr : Node_Id;
4214 Parent_Kind : Node_Kind;
4215 Unc_Decl : Boolean := False;
4216 Parent_Node : Node_Id;
4218 begin
4219 pragma Assert (Nkind (N) in N_Aggregate | N_Extension_Aggregate);
4220 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4221 pragma Assert (Is_Record_Type (Typ));
4223 Parent_Node := Parent (N);
4224 Parent_Kind := Nkind (Parent_Node);
4226 if Parent_Kind = N_Qualified_Expression then
4227 -- Check if we are in an unconstrained declaration because in this
4228 -- case the current delayed expansion mechanism doesn't work when
4229 -- the declared object size depends on the initializing expr.
4231 Parent_Node := Parent (Parent_Node);
4232 Parent_Kind := Nkind (Parent_Node);
4234 if Parent_Kind = N_Object_Declaration then
4235 Unc_Decl :=
4236 not Is_Entity_Name (Object_Definition (Parent_Node))
4237 or else (Nkind (N) = N_Aggregate
4238 and then
4239 Has_Discriminants
4240 (Entity (Object_Definition (Parent_Node))))
4241 or else Is_Class_Wide_Type
4242 (Entity (Object_Definition (Parent_Node)));
4243 end if;
4244 end if;
4246 -- Just set the Delay flag in the cases where the transformation will be
4247 -- done top down from above.
4250 -- Internal aggregates (transformed when expanding the parent),
4251 -- excluding container aggregates as these are transformed into
4252 -- subprogram calls later.
4254 (Parent_Kind in
4255 N_Component_Association | N_Aggregate | N_Extension_Aggregate
4256 and then not Is_Container_Aggregate (Parent_Node))
4258 -- Allocator (see Convert_Aggr_In_Allocator)
4260 or else Parent_Kind = N_Allocator
4262 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4264 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4266 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4267 -- assignments in init procs are taken into account.
4269 or else (Parent_Kind = N_Assignment_Statement
4270 and then Inside_Init_Proc)
4272 -- (Ada 2005) An inherently limited type in a return statement, which
4273 -- will be handled in a build-in-place fashion, and may be rewritten
4274 -- as an extended return and have its own finalization machinery.
4275 -- In the case of a simple return, the aggregate needs to be delayed
4276 -- until the scope for the return statement has been created, so
4277 -- that any finalization chain will be associated with that scope.
4278 -- For extended returns, we delay expansion to avoid the creation
4279 -- of an unwanted transient scope that could result in premature
4280 -- finalization of the return object (which is built in place
4281 -- within the caller's scope).
4283 or else Is_Build_In_Place_Aggregate_Return (N)
4284 then
4285 Set_Expansion_Delayed (N);
4286 return;
4287 end if;
4289 -- Otherwise, if a transient scope is required, create it now. If we
4290 -- are within an initialization procedure do not create such, because
4291 -- the target of the assignment must not be declared within a local
4292 -- block, and because cleanup will take place on return from the
4293 -- initialization procedure.
4295 -- Should the condition be more restrictive ???
4297 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4298 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
4299 end if;
4301 -- If the aggregate is nonlimited, create a temporary, since aggregates
4302 -- have "by copy" semantics. If it is limited and context is an
4303 -- assignment, this is a subaggregate for an enclosing aggregate being
4304 -- expanded. It must be built in place, so use target of the current
4305 -- assignment.
4307 if Is_Limited_Type (Typ)
4308 and then Parent_Kind = N_Assignment_Statement
4309 then
4310 Target_Expr := New_Copy_Tree (Name (Parent_Node));
4311 Insert_Actions (Parent_Node,
4312 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4313 Rewrite (Parent_Node, Make_Null_Statement (Loc));
4315 -- Do not declare a temporary to initialize an aggregate assigned to
4316 -- a target when in-place assignment is possible, i.e. preserving the
4317 -- by-copy semantic of aggregates. This avoids large stack usage and
4318 -- generates more efficient code.
4320 elsif Parent_Kind = N_Assignment_Statement
4321 and then In_Place_Assign_OK (N, Get_Base_Object (Name (Parent_Node)))
4322 then
4323 declare
4324 Lhs : constant Node_Id := Name (Parent_Node);
4325 begin
4326 -- Apply discriminant check if required
4328 if Has_Discriminants (Etype (N)) then
4329 Apply_Discriminant_Check (N, Etype (Lhs), Lhs);
4330 end if;
4332 -- The check just above may have replaced the aggregate with a CE
4334 if Nkind (N) in N_Aggregate | N_Extension_Aggregate then
4335 Target_Expr := New_Copy_Tree (Lhs);
4336 Insert_Actions (Parent_Node,
4337 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4338 Rewrite (Parent_Node, Make_Null_Statement (Loc));
4339 end if;
4340 end;
4342 else
4343 Temp := Make_Temporary (Loc, 'A', N);
4345 -- If the type inherits unknown discriminants, use the view with
4346 -- known discriminants if available.
4348 if Has_Unknown_Discriminants (Typ)
4349 and then Present (Underlying_Record_View (Typ))
4350 then
4351 T := Underlying_Record_View (Typ);
4352 else
4353 T := Typ;
4354 end if;
4356 Instr :=
4357 Make_Object_Declaration (Loc,
4358 Defining_Identifier => Temp,
4359 Object_Definition => New_Occurrence_Of (T, Loc));
4361 Set_No_Initialization (Instr);
4362 Insert_Action (N, Instr);
4363 Initialize_Discriminants (Instr, T);
4365 Target_Expr := New_Occurrence_Of (Temp, Loc);
4366 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
4368 -- Save the last assignment statement associated with the aggregate
4369 -- when building a controlled object. This reference is utilized by
4370 -- the finalization machinery when marking an object as successfully
4371 -- initialized.
4373 if Needs_Finalization (T) then
4374 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
4375 end if;
4377 Insert_Actions (N, Aggr_Code);
4378 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4379 Analyze_And_Resolve (N, T);
4380 end if;
4381 end Convert_To_Assignments;
4383 ---------------------------
4384 -- Convert_To_Positional --
4385 ---------------------------
4387 procedure Convert_To_Positional
4388 (N : Node_Id;
4389 Handle_Bit_Packed : Boolean := False)
4391 Typ : constant Entity_Id := Etype (N);
4392 Dims : constant Nat := Number_Dimensions (Typ);
4393 Max_Others_Replicate : constant Nat := Max_Aggregate_Size (N);
4395 Static_Components : Boolean := True;
4397 procedure Check_Static_Components;
4398 -- Check whether all components of the aggregate are compile-time known
4399 -- values, and can be passed as is to the back-end without further
4400 -- expansion.
4402 function Flatten
4403 (N : Node_Id;
4404 Dims : Nat;
4405 Ix : Node_Id;
4406 Ixb : Node_Id) return Boolean;
4407 -- Convert the aggregate into a purely positional form if possible after
4408 -- checking that the bounds of all dimensions are known to be static.
4410 function Is_Flat (N : Node_Id; Dims : Nat) return Boolean;
4411 -- Return True if the aggregate N is flat (which is not trivial in the
4412 -- case of multidimensional aggregates).
4414 function Is_Static_Element (N : Node_Id; Dims : Nat) return Boolean;
4415 -- Return True if N, an element of a component association list, i.e.
4416 -- N_Component_Association or N_Iterated_Component_Association, has a
4417 -- compile-time known value and can be passed as is to the back-end
4418 -- without further expansion.
4419 -- An Iterated_Component_Association is treated as nonstatic in most
4420 -- cases for now, so there are possibilities for optimization.
4422 -----------------------------
4423 -- Check_Static_Components --
4424 -----------------------------
4426 -- Could use some comments in this body ???
4428 procedure Check_Static_Components is
4429 Assoc : Node_Id;
4430 Expr : Node_Id;
4432 begin
4433 Static_Components := True;
4435 if Nkind (N) = N_String_Literal then
4436 null;
4438 elsif Present (Expressions (N)) then
4439 Expr := First (Expressions (N));
4440 while Present (Expr) loop
4441 if Nkind (Expr) /= N_Aggregate
4442 or else not Compile_Time_Known_Aggregate (Expr)
4443 or else Expansion_Delayed (Expr)
4444 then
4445 Static_Components := False;
4446 exit;
4447 end if;
4449 Next (Expr);
4450 end loop;
4451 end if;
4453 if Nkind (N) = N_Aggregate
4454 and then Present (Component_Associations (N))
4455 then
4456 Assoc := First (Component_Associations (N));
4457 while Present (Assoc) loop
4458 if not Is_Static_Element (Assoc, Dims) then
4459 Static_Components := False;
4460 exit;
4461 end if;
4463 Next (Assoc);
4464 end loop;
4465 end if;
4466 end Check_Static_Components;
4468 -------------
4469 -- Flatten --
4470 -------------
4472 function Flatten
4473 (N : Node_Id;
4474 Dims : Nat;
4475 Ix : Node_Id;
4476 Ixb : Node_Id) return Boolean
4478 Loc : constant Source_Ptr := Sloc (N);
4479 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
4480 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
4481 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
4483 function Cannot_Flatten_Next_Aggr (Expr : Node_Id) return Boolean;
4484 -- Return true if Expr is an aggregate for the next dimension that
4485 -- cannot be recursively flattened.
4487 ------------------------------
4488 -- Cannot_Flatten_Next_Aggr --
4489 ------------------------------
4491 function Cannot_Flatten_Next_Aggr (Expr : Node_Id) return Boolean is
4492 begin
4493 return Nkind (Expr) = N_Aggregate
4494 and then Present (Next_Index (Ix))
4495 and then not
4496 Flatten (Expr, Dims - 1, Next_Index (Ix), Next_Index (Ixb));
4497 end Cannot_Flatten_Next_Aggr;
4499 -- Local variables
4501 Lov : Uint;
4502 Hiv : Uint;
4503 Others_Present : Boolean;
4505 -- Start of processing for Flatten
4507 begin
4508 if Nkind (Original_Node (N)) = N_String_Literal then
4509 return True;
4510 end if;
4512 if not Compile_Time_Known_Value (Lo)
4513 or else not Compile_Time_Known_Value (Hi)
4514 then
4515 return False;
4516 end if;
4518 Lov := Expr_Value (Lo);
4519 Hiv := Expr_Value (Hi);
4521 -- Check if there is an others choice
4523 Others_Present := False;
4525 if Present (Component_Associations (N)) then
4526 if Is_Empty_List (Component_Associations (N)) then
4527 -- an expanded null array aggregate
4528 return False;
4529 end if;
4531 declare
4532 Assoc : Node_Id;
4533 Choice : Node_Id;
4535 begin
4536 Assoc := First (Component_Associations (N));
4537 while Present (Assoc) loop
4539 -- If this is a box association, flattening is in general
4540 -- not possible because at this point we cannot tell if the
4541 -- default is static or even exists.
4543 if Box_Present (Assoc) then
4544 return False;
4546 elsif Nkind (Assoc) = N_Iterated_Component_Association then
4547 return False;
4548 end if;
4550 Choice := First (Choice_List (Assoc));
4552 while Present (Choice) loop
4553 if Nkind (Choice) = N_Others_Choice then
4554 Others_Present := True;
4555 end if;
4557 Next (Choice);
4558 end loop;
4560 Next (Assoc);
4561 end loop;
4562 end;
4563 end if;
4565 -- If the low bound is not known at compile time and others is not
4566 -- present we can proceed since the bounds can be obtained from the
4567 -- aggregate.
4569 if Hiv < Lov
4570 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
4571 then
4572 return False;
4573 end if;
4575 -- Determine if set of alternatives is suitable for conversion and
4576 -- build an array containing the values in sequence.
4578 declare
4579 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
4580 of Node_Id := (others => Empty);
4581 -- The values in the aggregate sorted appropriately
4583 Vlist : List_Id;
4584 -- Same data as Vals in list form
4586 Rep_Count : Nat;
4587 -- Used to validate Max_Others_Replicate limit
4589 Elmt : Node_Id;
4590 Expr : Node_Id;
4591 Num : Int := UI_To_Int (Lov);
4592 Choice_Index : Int;
4593 Choice : Node_Id;
4594 Lo, Hi : Node_Id;
4596 begin
4597 if Present (Expressions (N)) then
4598 Elmt := First (Expressions (N));
4599 while Present (Elmt) loop
4600 -- In the case of a multidimensional array, check that the
4601 -- aggregate can be recursively flattened.
4603 if Cannot_Flatten_Next_Aggr (Elmt) then
4604 return False;
4605 end if;
4607 -- Duplicate expression for each index it covers
4609 Vals (Num) := New_Copy_Tree (Elmt);
4610 Num := Num + 1;
4612 Next (Elmt);
4613 end loop;
4614 end if;
4616 if No (Component_Associations (N)) then
4617 return True;
4618 end if;
4620 Elmt := First (Component_Associations (N));
4622 Component_Loop : while Present (Elmt) loop
4623 Expr := Expression (Elmt);
4625 -- In the case of a multidimensional array, check that the
4626 -- aggregate can be recursively flattened.
4628 if Cannot_Flatten_Next_Aggr (Expr) then
4629 return False;
4630 end if;
4632 Choice := First (Choice_List (Elmt));
4633 Choice_Loop : while Present (Choice) loop
4635 -- If we have an others choice, fill in the missing elements
4636 -- subject to the limit established by Max_Others_Replicate.
4638 if Nkind (Choice) = N_Others_Choice then
4639 Rep_Count := 0;
4641 -- If the expression involves a construct that generates
4642 -- a loop, we must generate individual assignments and
4643 -- no flattening is possible.
4645 if Nkind (Expr) = N_Quantified_Expression then
4646 return False;
4647 end if;
4649 for J in Vals'Range loop
4650 if No (Vals (J)) then
4651 Vals (J) := New_Copy_Tree (Expr);
4652 Rep_Count := Rep_Count + 1;
4654 -- Check for maximum others replication. Note that
4655 -- we skip this test if either of the restrictions
4656 -- No_Implicit_Loops or No_Elaboration_Code is
4657 -- active, if this is a preelaborable unit or
4658 -- a predefined unit, or if the unit must be
4659 -- placed in data memory. This also ensures that
4660 -- predefined units get the same level of constant
4661 -- folding in Ada 95 and Ada 2005, where their
4662 -- categorization has changed.
4664 declare
4665 P : constant Entity_Id :=
4666 Cunit_Entity (Current_Sem_Unit);
4668 begin
4669 -- Check if duplication is always OK and, if so,
4670 -- continue processing.
4672 if Restriction_Active (No_Implicit_Loops) then
4673 null;
4675 -- If duplication is not always OK, continue
4676 -- only if either the element is static or is
4677 -- an aggregate (we already know it is OK).
4679 elsif not Is_Static_Element (Elmt, Dims)
4680 and then Nkind (Expr) /= N_Aggregate
4681 then
4682 return False;
4684 -- Check if duplication is OK for elaboration
4685 -- purposes and, if so, continue processing.
4687 elsif Restriction_Active (No_Elaboration_Code)
4688 or else
4689 (Ekind (Current_Scope) = E_Package
4690 and then
4691 Static_Elaboration_Desired (Current_Scope))
4692 or else Is_Preelaborated (P)
4693 or else (Ekind (P) = E_Package_Body
4694 and then
4695 Is_Preelaborated (Spec_Entity (P)))
4696 or else
4697 Is_Predefined_Unit (Get_Source_Unit (P))
4698 then
4699 null;
4701 -- Otherwise, check that the replication count
4702 -- is not too high.
4704 elsif Rep_Count > Max_Others_Replicate then
4705 return False;
4706 end if;
4707 end;
4708 end if;
4709 end loop;
4711 if Rep_Count = 0
4712 and then Warn_On_Redundant_Constructs
4713 then
4714 Error_Msg_N ("there are no others?r?", Elmt);
4715 end if;
4717 exit Component_Loop;
4719 -- Case of a subtype mark, identifier or expanded name
4721 elsif Is_Entity_Name (Choice)
4722 and then Is_Type (Entity (Choice))
4723 then
4724 Lo := Type_Low_Bound (Etype (Choice));
4725 Hi := Type_High_Bound (Etype (Choice));
4727 -- Case of subtype indication
4729 elsif Nkind (Choice) = N_Subtype_Indication then
4730 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
4731 Hi := High_Bound (Range_Expression (Constraint (Choice)));
4733 -- Case of a range
4735 elsif Nkind (Choice) = N_Range then
4736 Lo := Low_Bound (Choice);
4737 Hi := High_Bound (Choice);
4739 -- Normal subexpression case
4741 else pragma Assert (Nkind (Choice) in N_Subexpr);
4742 if not Compile_Time_Known_Value (Choice) then
4743 return False;
4745 else
4746 Choice_Index := UI_To_Int (Expr_Value (Choice));
4748 if Choice_Index in Vals'Range then
4749 Vals (Choice_Index) := New_Copy_Tree (Expr);
4750 goto Continue;
4752 -- Choice is statically out-of-range, will be
4753 -- rewritten to raise Constraint_Error.
4755 else
4756 return False;
4757 end if;
4758 end if;
4759 end if;
4761 -- Range cases merge with Lo,Hi set
4763 if not Compile_Time_Known_Value (Lo)
4764 or else
4765 not Compile_Time_Known_Value (Hi)
4766 then
4767 return False;
4769 else
4770 for J in UI_To_Int (Expr_Value (Lo)) ..
4771 UI_To_Int (Expr_Value (Hi))
4772 loop
4773 Vals (J) := New_Copy_Tree (Expr);
4774 end loop;
4775 end if;
4777 <<Continue>>
4778 Next (Choice);
4779 end loop Choice_Loop;
4781 Next (Elmt);
4782 end loop Component_Loop;
4784 -- If we get here the conversion is possible
4786 Vlist := New_List;
4787 for J in Vals'Range loop
4788 Append (Vals (J), Vlist);
4789 end loop;
4791 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4792 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4793 return True;
4794 end;
4795 end Flatten;
4797 -------------
4798 -- Is_Flat --
4799 -------------
4801 function Is_Flat (N : Node_Id; Dims : Nat) return Boolean is
4802 Elmt : Node_Id;
4804 begin
4805 if Dims = 0 then
4806 return True;
4808 elsif Nkind (N) = N_Aggregate then
4809 if Present (Component_Associations (N)) then
4810 return False;
4812 else
4813 Elmt := First (Expressions (N));
4814 while Present (Elmt) loop
4815 if not Is_Flat (Elmt, Dims - 1) then
4816 return False;
4817 end if;
4819 Next (Elmt);
4820 end loop;
4822 return True;
4823 end if;
4824 else
4825 return True;
4826 end if;
4827 end Is_Flat;
4829 -------------------------
4830 -- Is_Static_Element --
4831 -------------------------
4833 function Is_Static_Element (N : Node_Id; Dims : Nat) return Boolean is
4834 Expr : constant Node_Id := Expression (N);
4836 begin
4837 -- In most cases the interesting expressions are unambiguously static
4839 if Compile_Time_Known_Value (Expr) then
4840 return True;
4842 elsif Nkind (N) = N_Iterated_Component_Association then
4843 return False;
4845 elsif Nkind (Expr) = N_Aggregate
4846 and then Compile_Time_Known_Aggregate (Expr)
4847 and then not Expansion_Delayed (Expr)
4848 then
4849 return True;
4851 -- However, one may write static expressions that are syntactically
4852 -- ambiguous, so preanalyze the expression before checking it again,
4853 -- but only at the innermost level for a multidimensional array.
4855 elsif Dims = 1 then
4856 Preanalyze_And_Resolve (Expr, Component_Type (Typ));
4857 return Compile_Time_Known_Value (Expr);
4859 else
4860 return False;
4861 end if;
4862 end Is_Static_Element;
4864 -- Start of processing for Convert_To_Positional
4866 begin
4867 -- Only convert to positional when generating C in case of an
4868 -- object declaration, this is the only case where aggregates are
4869 -- supported in C.
4871 if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
4872 return;
4873 end if;
4875 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4876 -- components because in this case will need to call the corresponding
4877 -- IP procedure.
4879 if Has_Default_Init_Comps (N) then
4880 return;
4881 end if;
4883 -- A subaggregate may have been flattened but is not known to be
4884 -- Compile_Time_Known. Set that flag in cases that cannot require
4885 -- elaboration code, so that the aggregate can be used as the
4886 -- initial value of a thread-local variable.
4888 if Is_Flat (N, Dims) then
4889 if Static_Array_Aggregate (N) then
4890 Set_Compile_Time_Known_Aggregate (N);
4891 end if;
4893 return;
4894 end if;
4896 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4897 return;
4898 end if;
4900 -- Do not convert to positional if controlled components are involved
4901 -- since these require special processing
4903 if Has_Controlled_Component (Typ) then
4904 return;
4905 end if;
4907 Check_Static_Components;
4909 -- If the size is known, or all the components are static, try to
4910 -- build a fully positional aggregate.
4912 -- The size of the type may not be known for an aggregate with
4913 -- discriminated array components, but if the components are static
4914 -- it is still possible to verify statically that the length is
4915 -- compatible with the upper bound of the type, and therefore it is
4916 -- worth flattening such aggregates as well.
4918 if Aggr_Size_OK (N)
4919 and then
4920 Flatten (N, Dims, First_Index (Typ), First_Index (Base_Type (Typ)))
4921 then
4922 if Static_Components then
4923 Set_Compile_Time_Known_Aggregate (N);
4924 Set_Expansion_Delayed (N, False);
4925 end if;
4927 Analyze_And_Resolve (N, Typ);
4928 end if;
4930 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4931 -- that will still require initialization code.
4933 if (Ekind (Current_Scope) = E_Package
4934 and then Static_Elaboration_Desired (Current_Scope))
4935 and then Nkind (Parent (N)) = N_Object_Declaration
4936 then
4937 declare
4938 Expr : Node_Id;
4940 begin
4941 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4942 Expr := First (Expressions (N));
4943 while Present (Expr) loop
4944 if not Compile_Time_Known_Value (Expr) then
4945 Error_Msg_N
4946 ("non-static object requires elaboration code??", N);
4947 exit;
4948 end if;
4950 Next (Expr);
4951 end loop;
4953 if Present (Component_Associations (N)) then
4954 Error_Msg_N ("object requires elaboration code??", N);
4955 end if;
4956 end if;
4957 end;
4958 end if;
4959 end Convert_To_Positional;
4961 ----------------------------
4962 -- Expand_Array_Aggregate --
4963 ----------------------------
4965 -- Array aggregate expansion proceeds as follows:
4967 -- 1. If requested we generate code to perform all the array aggregate
4968 -- bound checks, specifically
4970 -- (a) Check that the index range defined by aggregate bounds is
4971 -- compatible with corresponding index subtype.
4973 -- (b) If an others choice is present check that no aggregate
4974 -- index is outside the bounds of the index constraint.
4976 -- (c) For multidimensional arrays make sure that all subaggregates
4977 -- corresponding to the same dimension have the same bounds.
4979 -- 2. Check for packed array aggregate which can be converted to a
4980 -- constant so that the aggregate disappears completely.
4982 -- 3. Check case of nested aggregate. Generally nested aggregates are
4983 -- handled during the processing of the parent aggregate.
4985 -- 4. Check if the aggregate can be statically processed. If this is the
4986 -- case pass it as is to Gigi. Note that a necessary condition for
4987 -- static processing is that the aggregate be fully positional.
4989 -- 5. If in-place aggregate expansion is possible (i.e. no need to create
4990 -- a temporary) then mark the aggregate as such and return. Otherwise
4991 -- create a new temporary and generate the appropriate initialization
4992 -- code.
4994 procedure Expand_Array_Aggregate (N : Node_Id) is
4995 Loc : constant Source_Ptr := Sloc (N);
4997 Typ : constant Entity_Id := Etype (N);
4998 Ctyp : constant Entity_Id := Component_Type (Typ);
4999 -- Typ is the correct constrained array subtype of the aggregate
5000 -- Ctyp is the corresponding component type.
5002 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
5003 -- Number of aggregate index dimensions
5005 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
5006 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
5007 -- Low and High bounds of the constraint for each aggregate index
5009 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
5010 -- The type of each index
5012 In_Place_Assign_OK_For_Declaration : Boolean := False;
5013 -- True if we are to generate an in-place assignment for a declaration
5015 Maybe_In_Place_OK : Boolean;
5016 -- If the type is neither controlled nor packed and the aggregate
5017 -- is the expression in an assignment, assignment in place may be
5018 -- possible, provided other conditions are met on the LHS.
5020 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
5021 (others => False);
5022 -- If Others_Present (J) is True, then there is an others choice in one
5023 -- of the subaggregates of N at dimension J.
5025 procedure Build_Constrained_Type (Positional : Boolean);
5026 -- If the subtype is not static or unconstrained, build a constrained
5027 -- type using the computable sizes of the aggregate and its sub-
5028 -- aggregates.
5030 procedure Check_Bounds (Aggr_Bounds_Node, Index_Bounds_Node : Node_Id);
5031 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
5032 -- by Index_Bounds. For null array aggregate (Ada 2022) check that the
5033 -- aggregate bounds define a null range.
5035 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
5036 -- Checks that in a multidimensional array aggregate all subaggregates
5037 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
5038 -- an array subaggregate. Dim is the dimension corresponding to the
5039 -- subaggregate.
5041 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
5042 -- Computes the values of array Others_Present. Sub_Aggr is the array
5043 -- subaggregate we start the computation from. Dim is the dimension
5044 -- corresponding to the subaggregate.
5046 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
5047 -- Checks that if an others choice is present in any subaggregate, no
5048 -- aggregate index is outside the bounds of the index constraint.
5049 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
5050 -- to the subaggregate.
5052 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
5053 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
5054 -- built directly into the target of the assignment it must be free
5055 -- of side effects. N is the LHS of an assignment.
5057 procedure Two_Pass_Aggregate_Expansion (N : Node_Id);
5058 -- If the aggregate consists only of iterated associations then the
5059 -- aggregate is constructed in two steps:
5060 -- a) Build an expression to compute the number of elements
5061 -- generated by each iterator, and use the expression to allocate
5062 -- the destination aggregate.
5063 -- b) Generate the loops corresponding to each iterator to insert
5064 -- the elements in their proper positions.
5066 ----------------------------
5067 -- Build_Constrained_Type --
5068 ----------------------------
5070 procedure Build_Constrained_Type (Positional : Boolean) is
5071 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
5072 Decl : Node_Id;
5073 Indexes : constant List_Id := New_List;
5074 Num : Nat;
5075 Sub_Agg : Node_Id;
5077 begin
5078 -- If the aggregate is purely positional, all its subaggregates
5079 -- have the same size. We collect the dimensions from the first
5080 -- subaggregate at each level.
5082 if Positional then
5083 Sub_Agg := N;
5085 for D in 1 .. Aggr_Dimension loop
5086 Num := List_Length (Expressions (Sub_Agg));
5088 Append_To (Indexes,
5089 Make_Range (Loc,
5090 Low_Bound => Make_Integer_Literal (Loc, Uint_1),
5091 High_Bound => Make_Integer_Literal (Loc, Num)));
5093 Sub_Agg := First (Expressions (Sub_Agg));
5094 end loop;
5096 else
5097 -- We know the aggregate type is unconstrained and the aggregate
5098 -- is not processable by the back end, therefore not necessarily
5099 -- positional. Retrieve each dimension bounds (computed earlier).
5101 for D in 1 .. Aggr_Dimension loop
5102 Append_To (Indexes,
5103 Make_Range (Loc,
5104 Low_Bound => Aggr_Low (D),
5105 High_Bound => Aggr_High (D)));
5106 end loop;
5107 end if;
5109 Decl :=
5110 Make_Full_Type_Declaration (Loc,
5111 Defining_Identifier => Agg_Type,
5112 Type_Definition =>
5113 Make_Constrained_Array_Definition (Loc,
5114 Discrete_Subtype_Definitions => Indexes,
5115 Component_Definition =>
5116 Make_Component_Definition (Loc,
5117 Subtype_Indication =>
5118 New_Occurrence_Of (Component_Type (Typ), Loc))));
5120 Insert_Action (N, Decl);
5121 Analyze (Decl);
5122 Set_Etype (N, Agg_Type);
5123 Set_Is_Itype (Agg_Type);
5124 Freeze_Itype (Agg_Type, N);
5125 end Build_Constrained_Type;
5127 ------------------
5128 -- Check_Bounds --
5129 ------------------
5131 procedure Check_Bounds (Aggr_Bounds_Node, Index_Bounds_Node : Node_Id) is
5132 Aggr_Bounds : constant Range_Nodes :=
5133 Get_Index_Bounds (Aggr_Bounds_Node);
5134 Ind_Bounds : constant Range_Nodes :=
5135 Get_Index_Bounds (Index_Bounds_Node);
5137 Cond : Node_Id;
5139 begin
5140 -- For a null array aggregate check that high bound (i.e., low
5141 -- bound predecessor) exists. Fail if low bound is low bound of
5142 -- base subtype (in all cases, including modular).
5144 if Is_Null_Aggregate (N) then
5145 Insert_Action (N,
5146 Make_Raise_Constraint_Error (Loc,
5147 Condition =>
5148 Make_Op_Eq (Loc,
5149 New_Copy_Tree (Aggr_Bounds.First),
5150 New_Copy_Tree
5151 (Type_Low_Bound (Base_Type (Etype (Ind_Bounds.First))))),
5152 Reason => CE_Range_Check_Failed));
5153 return;
5154 end if;
5156 -- Generate the following test:
5158 -- [constraint_error when
5159 -- Aggr_Bounds.First <= Aggr_Bounds.Last and then
5160 -- (Aggr_Bounds.First < Ind_Bounds.First
5161 -- or else Aggr_Bounds.Last > Ind_Bounds.Last)]
5163 -- As an optimization try to see if some tests are trivially vacuous
5164 -- because we are comparing an expression against itself.
5166 if Aggr_Bounds.First = Ind_Bounds.First
5167 and then Aggr_Bounds.Last = Ind_Bounds.Last
5168 then
5169 Cond := Empty;
5171 elsif Aggr_Bounds.Last = Ind_Bounds.Last then
5172 Cond :=
5173 Make_Op_Lt (Loc,
5174 Left_Opnd =>
5175 Duplicate_Subexpr_Move_Checks (Aggr_Bounds.First),
5176 Right_Opnd =>
5177 Duplicate_Subexpr_Move_Checks (Ind_Bounds.First));
5179 elsif Aggr_Bounds.First = Ind_Bounds.First then
5180 Cond :=
5181 Make_Op_Gt (Loc,
5182 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Bounds.Last),
5183 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Bounds.Last));
5185 else
5186 Cond :=
5187 Make_Or_Else (Loc,
5188 Left_Opnd =>
5189 Make_Op_Lt (Loc,
5190 Left_Opnd =>
5191 Duplicate_Subexpr_Move_Checks (Aggr_Bounds.First),
5192 Right_Opnd =>
5193 Duplicate_Subexpr_Move_Checks (Ind_Bounds.First)),
5195 Right_Opnd =>
5196 Make_Op_Gt (Loc,
5197 Left_Opnd => Duplicate_Subexpr (Aggr_Bounds.Last),
5198 Right_Opnd => Duplicate_Subexpr (Ind_Bounds.Last)));
5199 end if;
5201 if Present (Cond) then
5202 Cond :=
5203 Make_And_Then (Loc,
5204 Left_Opnd =>
5205 Make_Op_Le (Loc,
5206 Left_Opnd =>
5207 Duplicate_Subexpr_Move_Checks (Aggr_Bounds.First),
5208 Right_Opnd =>
5209 Duplicate_Subexpr_Move_Checks (Aggr_Bounds.Last)),
5211 Right_Opnd => Cond);
5213 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5214 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5215 Insert_Action (N,
5216 Make_Raise_Constraint_Error (Loc,
5217 Condition => Cond,
5218 Reason => CE_Range_Check_Failed));
5219 end if;
5220 end Check_Bounds;
5222 ----------------------------
5223 -- Check_Same_Aggr_Bounds --
5224 ----------------------------
5226 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5227 Sub_Bounds : constant Range_Nodes :=
5228 Get_Index_Bounds (Aggregate_Bounds (Sub_Aggr));
5229 Sub_Lo : Node_Id renames Sub_Bounds.First;
5230 Sub_Hi : Node_Id renames Sub_Bounds.Last;
5231 -- The bounds of this specific subaggregate
5233 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5234 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5235 -- The bounds of the aggregate for this dimension
5237 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5238 -- The index type for this dimension.xxx
5240 Cond : Node_Id;
5241 Assoc : Node_Id;
5242 Expr : Node_Id;
5244 begin
5245 -- If index checks are on generate the test
5247 -- [constraint_error when
5248 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5250 -- As an optimization try to see if some tests are trivially vacuos
5251 -- because we are comparing an expression against itself. Also for
5252 -- the first dimension the test is trivially vacuous because there
5253 -- is just one aggregate for dimension 1.
5255 if Index_Checks_Suppressed (Ind_Typ) then
5256 Cond := Empty;
5258 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5259 then
5260 Cond := Empty;
5262 elsif Aggr_Hi = Sub_Hi then
5263 Cond :=
5264 Make_Op_Ne (Loc,
5265 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5266 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5268 elsif Aggr_Lo = Sub_Lo then
5269 Cond :=
5270 Make_Op_Ne (Loc,
5271 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5272 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5274 else
5275 Cond :=
5276 Make_Or_Else (Loc,
5277 Left_Opnd =>
5278 Make_Op_Ne (Loc,
5279 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5280 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5282 Right_Opnd =>
5283 Make_Op_Ne (Loc,
5284 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5285 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5286 end if;
5288 if Present (Cond) then
5289 Insert_Action (N,
5290 Make_Raise_Constraint_Error (Loc,
5291 Condition => Cond,
5292 Reason => CE_Length_Check_Failed));
5293 end if;
5295 -- Now look inside the subaggregate to see if there is more work
5297 if Dim < Aggr_Dimension then
5299 -- Process positional components
5301 if Present (Expressions (Sub_Aggr)) then
5302 Expr := First (Expressions (Sub_Aggr));
5303 while Present (Expr) loop
5304 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5305 Next (Expr);
5306 end loop;
5307 end if;
5309 -- Process component associations
5311 if Present (Component_Associations (Sub_Aggr)) then
5312 Assoc := First (Component_Associations (Sub_Aggr));
5313 while Present (Assoc) loop
5314 Expr := Expression (Assoc);
5315 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5316 Next (Assoc);
5317 end loop;
5318 end if;
5319 end if;
5320 end Check_Same_Aggr_Bounds;
5322 ----------------------------
5323 -- Compute_Others_Present --
5324 ----------------------------
5326 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5327 Assoc : Node_Id;
5328 Expr : Node_Id;
5330 begin
5331 if Present (Component_Associations (Sub_Aggr)) then
5332 Assoc := Last (Component_Associations (Sub_Aggr));
5334 if Present (Assoc)
5335 and then Nkind (First (Choice_List (Assoc))) = N_Others_Choice
5336 then
5337 Others_Present (Dim) := True;
5339 -- An others_clause may be superfluous if previous components
5340 -- cover the full given range of a constrained array. In such
5341 -- a case an others_clause does not contribute any additional
5342 -- components and has not been analyzed. We analyze it now to
5343 -- detect type errors in the expression, even though no code
5344 -- will be generated for it.
5346 if Dim = Aggr_Dimension
5347 and then Nkind (Assoc) /= N_Iterated_Component_Association
5348 and then not Analyzed (Expression (Assoc))
5349 and then not Box_Present (Assoc)
5350 then
5351 Preanalyze_And_Resolve (Expression (Assoc), Ctyp);
5352 end if;
5353 end if;
5354 end if;
5356 -- Now look inside the subaggregate to see if there is more work
5358 if Dim < Aggr_Dimension then
5360 -- Process positional components
5362 if Present (Expressions (Sub_Aggr)) then
5363 Expr := First (Expressions (Sub_Aggr));
5364 while Present (Expr) loop
5365 Compute_Others_Present (Expr, Dim + 1);
5366 Next (Expr);
5367 end loop;
5368 end if;
5370 -- Process component associations
5372 if Present (Component_Associations (Sub_Aggr)) then
5373 Assoc := First (Component_Associations (Sub_Aggr));
5374 while Present (Assoc) loop
5375 Expr := Expression (Assoc);
5376 Compute_Others_Present (Expr, Dim + 1);
5377 Next (Assoc);
5378 end loop;
5379 end if;
5380 end if;
5381 end Compute_Others_Present;
5383 ------------------
5384 -- Others_Check --
5385 ------------------
5387 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
5388 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5389 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5390 -- The bounds of the aggregate for this dimension
5392 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5393 -- The index type for this dimension
5395 Need_To_Check : Boolean := False;
5397 Choices_Lo : Node_Id := Empty;
5398 Choices_Hi : Node_Id := Empty;
5399 -- The lowest and highest discrete choices for a named subaggregate
5401 Nb_Choices : Int := -1;
5402 -- The number of discrete non-others choices in this subaggregate
5404 Nb_Elements : Uint := Uint_0;
5405 -- The number of elements in a positional aggregate
5407 Cond : Node_Id := Empty;
5409 Assoc : Node_Id;
5410 Choice : Node_Id;
5411 Expr : Node_Id;
5413 begin
5414 -- Check if we have an others choice. If we do make sure that this
5415 -- subaggregate contains at least one element in addition to the
5416 -- others choice.
5418 if Range_Checks_Suppressed (Ind_Typ) then
5419 Need_To_Check := False;
5421 elsif Present (Expressions (Sub_Aggr))
5422 and then Present (Component_Associations (Sub_Aggr))
5423 then
5424 Need_To_Check :=
5425 not (Is_Empty_List (Expressions (Sub_Aggr))
5426 and then Is_Empty_List
5427 (Component_Associations (Sub_Aggr)));
5429 elsif Present (Component_Associations (Sub_Aggr)) then
5430 Assoc := Last (Component_Associations (Sub_Aggr));
5432 if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then
5433 Need_To_Check := False;
5435 else
5436 -- Count the number of discrete choices. Start with -1 because
5437 -- the others choice does not count.
5439 -- Is there some reason we do not use List_Length here ???
5441 Nb_Choices := -1;
5442 Assoc := First (Component_Associations (Sub_Aggr));
5443 while Present (Assoc) loop
5444 Choice := First (Choice_List (Assoc));
5445 while Present (Choice) loop
5446 Nb_Choices := Nb_Choices + 1;
5447 Next (Choice);
5448 end loop;
5450 Next (Assoc);
5451 end loop;
5453 -- If there is only an others choice nothing to do
5455 Need_To_Check := (Nb_Choices > 0);
5456 end if;
5458 else
5459 Need_To_Check := False;
5460 end if;
5462 -- If we are dealing with a positional subaggregate with an others
5463 -- choice then compute the number or positional elements.
5465 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
5466 Expr := First (Expressions (Sub_Aggr));
5467 Nb_Elements := Uint_0;
5468 while Present (Expr) loop
5469 Nb_Elements := Nb_Elements + 1;
5470 Next (Expr);
5471 end loop;
5473 -- If the aggregate contains discrete choices and an others choice
5474 -- compute the smallest and largest discrete choice values.
5476 elsif Need_To_Check then
5477 Compute_Choices_Lo_And_Choices_Hi : declare
5479 Table : Case_Table_Type (1 .. Nb_Choices);
5480 -- Used to sort all the different choice values
5482 J : Pos := 1;
5484 begin
5485 Assoc := First (Component_Associations (Sub_Aggr));
5486 while Present (Assoc) loop
5487 Choice := First (Choice_List (Assoc));
5488 while Present (Choice) loop
5489 if Nkind (Choice) = N_Others_Choice then
5490 exit;
5491 end if;
5493 declare
5494 Bounds : constant Range_Nodes :=
5495 Get_Index_Bounds (Choice);
5496 begin
5497 Table (J).Choice_Lo := Bounds.First;
5498 Table (J).Choice_Hi := Bounds.Last;
5499 end;
5501 J := J + 1;
5502 Next (Choice);
5503 end loop;
5505 Next (Assoc);
5506 end loop;
5508 -- Sort the discrete choices
5510 Sort_Case_Table (Table);
5512 Choices_Lo := Table (1).Choice_Lo;
5513 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5514 end Compute_Choices_Lo_And_Choices_Hi;
5515 end if;
5517 -- If no others choice in this subaggregate, or the aggregate
5518 -- comprises only an others choice, nothing to do.
5520 if not Need_To_Check then
5521 Cond := Empty;
5523 -- If we are dealing with an aggregate containing an others choice
5524 -- and positional components, we generate the following test:
5526 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5527 -- Ind_Typ'Pos (Aggr_Hi)
5528 -- then
5529 -- raise Constraint_Error;
5530 -- end if;
5532 -- in the general case, but the following simpler test:
5534 -- [constraint_error when
5535 -- Aggr_Lo + (Nb_Elements - 1) > Aggr_Hi];
5537 -- instead if the index type is a signed integer.
5539 elsif Nb_Elements > Uint_0 then
5540 if Nb_Elements = Uint_1 then
5541 Cond :=
5542 Make_Op_Gt (Loc,
5543 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5544 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi));
5546 elsif Is_Signed_Integer_Type (Ind_Typ) then
5547 Cond :=
5548 Make_Op_Gt (Loc,
5549 Left_Opnd =>
5550 Make_Op_Add (Loc,
5551 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5552 Right_Opnd =>
5553 Make_Integer_Literal (Loc, Nb_Elements - 1)),
5554 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi));
5556 else
5557 Cond :=
5558 Make_Op_Gt (Loc,
5559 Left_Opnd =>
5560 Make_Op_Add (Loc,
5561 Left_Opnd =>
5562 Make_Attribute_Reference (Loc,
5563 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5564 Attribute_Name => Name_Pos,
5565 Expressions =>
5566 New_List
5567 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5568 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5570 Right_Opnd =>
5571 Make_Attribute_Reference (Loc,
5572 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5573 Attribute_Name => Name_Pos,
5574 Expressions => New_List (
5575 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5576 end if;
5578 -- If we are dealing with an aggregate containing an others choice
5579 -- and discrete choices we generate the following test:
5581 -- [constraint_error when
5582 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5584 else
5585 Cond :=
5586 Make_Or_Else (Loc,
5587 Left_Opnd =>
5588 Make_Op_Lt (Loc,
5589 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5590 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5592 Right_Opnd =>
5593 Make_Op_Gt (Loc,
5594 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5595 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5596 end if;
5598 if Present (Cond) then
5599 Insert_Action (N,
5600 Make_Raise_Constraint_Error (Loc,
5601 Condition => Cond,
5602 Reason => CE_Length_Check_Failed));
5603 -- Questionable reason code, shouldn't that be a
5604 -- CE_Range_Check_Failed ???
5605 end if;
5607 -- Now look inside the subaggregate to see if there is more work
5609 if Dim < Aggr_Dimension then
5611 -- Process positional components
5613 if Present (Expressions (Sub_Aggr)) then
5614 Expr := First (Expressions (Sub_Aggr));
5615 while Present (Expr) loop
5616 Others_Check (Expr, Dim + 1);
5617 Next (Expr);
5618 end loop;
5619 end if;
5621 -- Process component associations
5623 if Present (Component_Associations (Sub_Aggr)) then
5624 Assoc := First (Component_Associations (Sub_Aggr));
5625 while Present (Assoc) loop
5626 Expr := Expression (Assoc);
5627 Others_Check (Expr, Dim + 1);
5628 Next (Assoc);
5629 end loop;
5630 end if;
5631 end if;
5632 end Others_Check;
5634 -------------------------
5635 -- Safe_Left_Hand_Side --
5636 -------------------------
5638 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5639 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5640 -- If the left-hand side includes an indexed component, check that
5641 -- the indexes are free of side effects.
5643 -------------------
5644 -- Is_Safe_Index --
5645 -------------------
5647 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5648 begin
5649 if Is_Entity_Name (Indx) then
5650 return True;
5652 elsif Nkind (Indx) = N_Integer_Literal then
5653 return True;
5655 elsif Nkind (Indx) = N_Function_Call
5656 and then Is_Entity_Name (Name (Indx))
5657 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5658 then
5659 return True;
5661 elsif Nkind (Indx) = N_Type_Conversion
5662 and then Is_Safe_Index (Expression (Indx))
5663 then
5664 return True;
5666 else
5667 return False;
5668 end if;
5669 end Is_Safe_Index;
5671 -- Start of processing for Safe_Left_Hand_Side
5673 begin
5674 if Is_Entity_Name (N) then
5675 return True;
5677 elsif Nkind (N) in N_Explicit_Dereference | N_Selected_Component
5678 and then Safe_Left_Hand_Side (Prefix (N))
5679 then
5680 return True;
5682 elsif Nkind (N) = N_Indexed_Component
5683 and then Safe_Left_Hand_Side (Prefix (N))
5684 and then Is_Safe_Index (First (Expressions (N)))
5685 then
5686 return True;
5688 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5689 return Safe_Left_Hand_Side (Expression (N));
5691 else
5692 return False;
5693 end if;
5694 end Safe_Left_Hand_Side;
5696 ----------------------------------
5697 -- Two_Pass_Aggregate_Expansion --
5698 ----------------------------------
5700 procedure Two_Pass_Aggregate_Expansion (N : Node_Id) is
5701 Loc : constant Source_Ptr := Sloc (N);
5702 Comp_Type : constant Entity_Id := Etype (N);
5703 Index_Id : constant Entity_Id := Make_Temporary (Loc, 'I', N);
5704 Index_Type : constant Entity_Id := Etype (First_Index (Etype (N)));
5705 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'I', N);
5706 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
5708 Assoc : Node_Id := First (Component_Associations (N));
5709 Incr : Node_Id;
5710 Iter : Node_Id;
5711 New_Comp : Node_Id;
5712 One_Loop : Node_Id;
5714 Size_Expr_Code : List_Id;
5715 Insertion_Code : List_Id := New_List;
5717 begin
5718 Size_Expr_Code := New_List (
5719 Make_Object_Declaration (Loc,
5720 Defining_Identifier => Size_Id,
5721 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
5722 Expression => Make_Integer_Literal (Loc, 0)));
5724 -- First pass: execute the iterators to count the number of elements
5725 -- that will be generated.
5727 while Present (Assoc) loop
5728 Iter := Iterator_Specification (Assoc);
5729 Incr := Make_Assignment_Statement (Loc,
5730 Name => New_Occurrence_Of (Size_Id, Loc),
5731 Expression =>
5732 Make_Op_Add (Loc,
5733 Left_Opnd => New_Occurrence_Of (Size_Id, Loc),
5734 Right_Opnd => Make_Integer_Literal (Loc, 1)));
5736 One_Loop := Make_Implicit_Loop_Statement (N,
5737 Iteration_Scheme =>
5738 Make_Iteration_Scheme (Loc,
5739 Iterator_Specification => New_Copy_Tree (Iter)),
5740 Statements => New_List (Incr));
5742 Append (One_Loop, Size_Expr_Code);
5743 Next (Assoc);
5744 end loop;
5746 Insert_Actions (N, Size_Expr_Code);
5748 -- Build a constrained subtype with the calculated length
5749 -- and declare the proper bounded aggregate object.
5750 -- The index type is some discrete type, so the bounds of the
5751 -- constructed array are computed as T'Val (T'Pos (ineger bound));
5753 declare
5754 Pos_Lo : constant Node_Id :=
5755 Make_Attribute_Reference (Loc,
5756 Prefix => New_Occurrence_Of (Index_Type, Loc),
5757 Attribute_Name => Name_Pos,
5758 Expressions => New_List (
5759 Make_Attribute_Reference (Loc,
5760 Prefix => New_Occurrence_Of (Index_Type, Loc),
5761 Attribute_Name => Name_First)));
5763 Aggr_Lo : constant Node_Id :=
5764 Make_Attribute_Reference (Loc,
5765 Prefix => New_Occurrence_Of (Index_Type, Loc),
5766 Attribute_Name => Name_Val,
5767 Expressions => New_List (New_Copy_Tree (Pos_Lo)));
5769 -- Hi = Index_type'Pos (Lo + Size -1).
5771 Pos_Hi : constant Node_Id :=
5772 Make_Op_Add (Loc,
5773 Left_Opnd => New_Copy_Tree (Pos_Lo),
5774 Right_Opnd =>
5775 Make_Op_Subtract (Loc,
5776 Left_Opnd => New_Occurrence_Of (Size_Id, Loc),
5777 Right_Opnd => Make_Integer_Literal (Loc, 1)));
5779 -- Corresponding index value
5781 Aggr_Hi : constant Node_Id :=
5782 Make_Attribute_Reference (Loc,
5783 Prefix => New_Occurrence_Of (Index_Type, Loc),
5784 Attribute_Name => Name_Val,
5785 Expressions => New_List (New_Copy_Tree (Pos_Hi)));
5787 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
5788 SubD : constant Node_Id :=
5789 Make_Subtype_Declaration (Loc,
5790 Defining_Identifier => SubE,
5791 Subtype_Indication =>
5792 Make_Subtype_Indication (Loc,
5793 Subtype_Mark =>
5794 New_Occurrence_Of (Etype (Comp_Type), Loc),
5795 Constraint =>
5796 Make_Index_Or_Discriminant_Constraint
5797 (Loc,
5798 Constraints =>
5799 New_List (Make_Range (Loc, Aggr_Lo, Aggr_Hi)))));
5801 -- Create a temporary array of the above subtype which
5802 -- will be used to capture the aggregate assignments.
5804 TmpD : constant Node_Id :=
5805 Make_Object_Declaration (Loc,
5806 Defining_Identifier => TmpE,
5807 Object_Definition => New_Occurrence_Of (SubE, Loc));
5808 begin
5809 Insert_Actions (N, New_List (SubD, TmpD));
5810 end;
5812 -- Second pass: use the iterators to generate the elements of the
5813 -- aggregate. Insertion index starts at Index_Type'First. We
5814 -- assume that the second evaluation of each iterator generates
5815 -- the same number of elements as the first pass, and consider
5816 -- that the execution is erroneous (even if the RM does not state
5817 -- this explicitly) if the number of elements generated differs
5818 -- between first and second pass.
5820 Assoc := First (Component_Associations (N));
5822 -- Initialize insertion position to first array component.
5824 Insertion_Code := New_List (
5825 Make_Object_Declaration (Loc,
5826 Defining_Identifier => Index_Id,
5827 Object_Definition =>
5828 New_Occurrence_Of (Index_Type, Loc),
5829 Expression =>
5830 Make_Attribute_Reference (Loc,
5831 Prefix => New_Occurrence_Of (Index_Type, Loc),
5832 Attribute_Name => Name_First)));
5834 while Present (Assoc) loop
5835 Iter := Iterator_Specification (Assoc);
5836 New_Comp := Make_Assignment_Statement (Loc,
5837 Name =>
5838 Make_Indexed_Component (Loc,
5839 Prefix => New_Occurrence_Of (TmpE, Loc),
5840 Expressions =>
5841 New_List (New_Occurrence_Of (Index_Id, Loc))),
5842 Expression => Copy_Separate_Tree (Expression (Assoc)));
5844 -- Advance index position for insertion.
5846 Incr := Make_Assignment_Statement (Loc,
5847 Name => New_Occurrence_Of (Index_Id, Loc),
5848 Expression =>
5849 Make_Attribute_Reference (Loc,
5850 Prefix =>
5851 New_Occurrence_Of (Index_Type, Loc),
5852 Attribute_Name => Name_Succ,
5853 Expressions =>
5854 New_List (New_Occurrence_Of (Index_Id, Loc))));
5856 -- Add guard to skip last increment when upper bound is reached.
5858 Incr := Make_If_Statement (Loc,
5859 Condition =>
5860 Make_Op_Ne (Loc,
5861 Left_Opnd => New_Occurrence_Of (Index_Id, Loc),
5862 Right_Opnd =>
5863 Make_Attribute_Reference (Loc,
5864 Prefix => New_Occurrence_Of (Index_Type, Loc),
5865 Attribute_Name => Name_Last)),
5866 Then_Statements => New_List (Incr));
5868 One_Loop := Make_Implicit_Loop_Statement (N,
5869 Iteration_Scheme =>
5870 Make_Iteration_Scheme (Loc,
5871 Iterator_Specification => Copy_Separate_Tree (Iter)),
5872 Statements => New_List (New_Comp, Incr));
5874 Append (One_Loop, Insertion_Code);
5875 Next (Assoc);
5876 end loop;
5878 Insert_Actions (N, Insertion_Code);
5880 -- Depending on context this may not work for build-in-place
5881 -- arrays ???
5883 Rewrite (N, New_Occurrence_Of (TmpE, Loc));
5885 end Two_Pass_Aggregate_Expansion;
5887 -- Local variables
5889 Tmp : Entity_Id;
5890 -- Holds the temporary aggregate value
5892 Tmp_Decl : Node_Id;
5893 -- Holds the declaration of Tmp
5895 Aggr_Code : List_Id;
5896 Parent_Node : Node_Id;
5897 Parent_Kind : Node_Kind;
5899 -- Start of processing for Expand_Array_Aggregate
5901 begin
5902 -- Do not touch the special aggregates of attributes used for Asm calls
5904 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5905 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5906 then
5907 return;
5909 elsif Present (Component_Associations (N))
5910 and then Nkind (First (Component_Associations (N))) =
5911 N_Iterated_Component_Association
5912 and then
5913 Present (Iterator_Specification (First (Component_Associations (N))))
5914 then
5915 Two_Pass_Aggregate_Expansion (N);
5916 return;
5918 -- Do not attempt expansion if error already detected. We may reach this
5919 -- point in spite of previous errors when compiling with -gnatq, to
5920 -- force all possible errors (this is the usual ACATS mode).
5922 elsif Error_Posted (N) then
5923 return;
5924 end if;
5926 -- If the semantic analyzer has determined that aggregate N will raise
5927 -- Constraint_Error at run time, then the aggregate node has been
5928 -- replaced with an N_Raise_Constraint_Error node and we should
5929 -- never get here.
5931 pragma Assert (not Raises_Constraint_Error (N));
5933 -- STEP 1a
5935 -- Check that the index range defined by aggregate bounds is
5936 -- compatible with corresponding index subtype.
5938 Index_Compatibility_Check : declare
5939 Aggr_Index_Range : Node_Id := First_Index (Typ);
5940 -- The current aggregate index range
5942 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5943 -- The corresponding index constraint against which we have to
5944 -- check the above aggregate index range.
5946 begin
5947 Compute_Others_Present (N, 1);
5949 for J in 1 .. Aggr_Dimension loop
5950 -- There is no need to emit a check if an others choice is present
5951 -- for this array aggregate dimension since in this case one of
5952 -- N's subaggregates has taken its bounds from the context and
5953 -- these bounds must have been checked already. In addition all
5954 -- subaggregates corresponding to the same dimension must all have
5955 -- the same bounds (checked in (c) below).
5957 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5958 and then not Others_Present (J)
5959 then
5960 -- We don't use Checks.Apply_Range_Check here because it emits
5961 -- a spurious check. Namely it checks that the range defined by
5962 -- the aggregate bounds is nonempty. But we know this already
5963 -- if we get here.
5965 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5966 end if;
5968 -- Save the low and high bounds of the aggregate index as well as
5969 -- the index type for later use in checks (b) and (c) below.
5971 Get_Index_Bounds
5972 (Aggr_Index_Range, L => Aggr_Low (J), H => Aggr_High (J));
5974 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5976 Next_Index (Aggr_Index_Range);
5977 Next_Index (Index_Constraint);
5978 end loop;
5979 end Index_Compatibility_Check;
5981 -- STEP 1b
5983 -- If an others choice is present check that no aggregate index is
5984 -- outside the bounds of the index constraint.
5986 Others_Check (N, 1);
5988 -- STEP 1c
5990 -- For multidimensional arrays make sure that all subaggregates
5991 -- corresponding to the same dimension have the same bounds.
5993 if Aggr_Dimension > 1 then
5994 Check_Same_Aggr_Bounds (N, 1);
5995 end if;
5997 -- STEP 1d
5999 -- If we have a default component value, or simple initialization is
6000 -- required for the component type, then we replace <> in component
6001 -- associations by the required default value.
6003 declare
6004 Default_Val : Node_Id;
6005 Assoc : Node_Id;
6007 begin
6008 if (Present (Default_Aspect_Component_Value (Typ))
6009 or else Needs_Simple_Initialization (Ctyp))
6010 and then Present (Component_Associations (N))
6011 then
6012 Assoc := First (Component_Associations (N));
6013 while Present (Assoc) loop
6014 if Nkind (Assoc) = N_Component_Association
6015 and then Box_Present (Assoc)
6016 then
6017 Set_Box_Present (Assoc, False);
6019 if Present (Default_Aspect_Component_Value (Typ)) then
6020 Default_Val := Default_Aspect_Component_Value (Typ);
6021 else
6022 Default_Val := Get_Simple_Init_Val (Ctyp, N);
6023 end if;
6025 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
6026 Analyze_And_Resolve (Expression (Assoc), Ctyp);
6027 end if;
6029 Next (Assoc);
6030 end loop;
6031 end if;
6032 end;
6034 -- STEP 2
6036 -- Here we test for is packed array aggregate that we can handle at
6037 -- compile time. If so, return with transformation done. Note that we do
6038 -- this even if the aggregate is nested, because once we have done this
6039 -- processing, there is no more nested aggregate.
6041 if Packed_Array_Aggregate_Handled (N) then
6042 return;
6043 end if;
6045 -- At this point we try to convert to positional form
6047 Convert_To_Positional (N);
6049 -- If the result is no longer an aggregate (e.g. it may be a string
6050 -- literal, or a temporary which has the needed value), then we are
6051 -- done, since there is no longer a nested aggregate.
6053 if Nkind (N) /= N_Aggregate then
6054 return;
6056 -- We are also done if the result is an analyzed aggregate, indicating
6057 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6058 -- aggregate.
6060 elsif Analyzed (N) and then Is_Rewrite_Substitution (N) then
6061 return;
6062 end if;
6064 -- If all aggregate components are compile-time known and the aggregate
6065 -- has been flattened, nothing left to do. The same occurs if the
6066 -- aggregate is used to initialize the components of a statically
6067 -- allocated dispatch table.
6069 if Compile_Time_Known_Aggregate (N)
6070 or else Is_Static_Dispatch_Table_Aggregate (N)
6071 then
6072 Set_Expansion_Delayed (N, False);
6073 return;
6074 end if;
6076 -- Now see if back end processing is possible
6078 if Backend_Processing_Possible (N) then
6080 -- If the aggregate is static but the constraints are not, build
6081 -- a static subtype for the aggregate, so that Gigi can place it
6082 -- in static memory. Perform an unchecked_conversion to the non-
6083 -- static type imposed by the context.
6085 declare
6086 Itype : constant Entity_Id := Etype (N);
6087 Index : Node_Id;
6088 Needs_Type : Boolean := False;
6090 begin
6091 Index := First_Index (Itype);
6092 while Present (Index) loop
6093 if not Is_OK_Static_Subtype (Etype (Index)) then
6094 Needs_Type := True;
6095 exit;
6096 else
6097 Next_Index (Index);
6098 end if;
6099 end loop;
6101 if Needs_Type then
6102 Build_Constrained_Type (Positional => True);
6103 Rewrite (N, Unchecked_Convert_To (Itype, N));
6104 Analyze (N);
6105 end if;
6106 end;
6108 return;
6109 end if;
6111 -- STEP 3
6113 -- Delay expansion for nested aggregates: it will be taken care of when
6114 -- the parent aggregate is expanded, excluding container aggregates as
6115 -- these are transformed into subprogram calls later.
6117 Parent_Node := Parent (N);
6118 Parent_Kind := Nkind (Parent_Node);
6120 if Parent_Kind = N_Qualified_Expression then
6121 Parent_Node := Parent (Parent_Node);
6122 Parent_Kind := Nkind (Parent_Node);
6123 end if;
6125 if ((Parent_Kind = N_Component_Association
6126 or else Parent_Kind = N_Aggregate
6127 or else Parent_Kind = N_Extension_Aggregate)
6128 and then not Is_Container_Aggregate (Parent_Node))
6129 or else (Parent_Kind = N_Object_Declaration
6130 and then (Needs_Finalization (Typ)
6131 or else Is_Special_Return_Object
6132 (Defining_Identifier (Parent_Node))))
6133 or else (Parent_Kind = N_Assignment_Statement
6134 and then Inside_Init_Proc)
6135 then
6136 Set_Expansion_Delayed (N, not Static_Array_Aggregate (N));
6137 return;
6138 end if;
6140 -- STEP 4
6142 -- Check whether in-place aggregate expansion is possible
6144 -- For object declarations we build the aggregate in place, unless
6145 -- the array is bit-packed.
6147 -- For assignments we do the assignment in place if all the component
6148 -- associations have compile-time known values, or are default-
6149 -- initialized limited components, e.g. tasks. For other cases we
6150 -- create a temporary. A full analysis for safety of in-place assignment
6151 -- is delicate.
6153 -- For allocators we assign to the designated object in place if the
6154 -- aggregate meets the same conditions as other in-place assignments.
6155 -- In this case the aggregate may not come from source but was created
6156 -- for default initialization, e.g. with Initialize_Scalars.
6158 if Requires_Transient_Scope (Typ) then
6159 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6160 end if;
6162 -- An array of limited components is built in place
6164 if Is_Limited_Type (Typ) then
6165 Maybe_In_Place_OK := True;
6167 elsif Has_Default_Init_Comps (N) then
6168 Maybe_In_Place_OK := False;
6170 elsif Is_Bit_Packed_Array (Typ)
6171 or else Has_Controlled_Component (Typ)
6172 then
6173 Maybe_In_Place_OK := False;
6175 elsif Parent_Kind = N_Assignment_Statement then
6176 Maybe_In_Place_OK :=
6177 In_Place_Assign_OK (N, Get_Base_Object (Name (Parent_Node)));
6179 elsif Parent_Kind = N_Allocator then
6180 Maybe_In_Place_OK := In_Place_Assign_OK (N);
6182 else
6183 Maybe_In_Place_OK := False;
6184 end if;
6186 -- If this is an array of tasks, it will be expanded into build-in-place
6187 -- assignments. Build an activation chain for the tasks now.
6189 if Has_Task (Typ) then
6190 Build_Activation_Chain_Entity (N);
6191 end if;
6193 -- Perform in-place expansion of aggregate in an object declaration.
6194 -- Note: actions generated for the aggregate will be captured in an
6195 -- expression-with-actions statement so that they can be transferred
6196 -- to freeze actions later if there is an address clause for the
6197 -- object. (Note: we don't use a block statement because this would
6198 -- cause generated freeze nodes to be elaborated in the wrong scope).
6200 -- Arrays of limited components must be built in place. The code
6201 -- previously excluded controlled components but this is an old
6202 -- oversight: the rules in 7.6 (17) are clear.
6204 if Comes_From_Source (Parent_Node)
6205 and then Parent_Kind = N_Object_Declaration
6206 and then Present (Expression (Parent_Node))
6207 and then not
6208 Must_Slide (N, Etype (Defining_Identifier (Parent_Node)), Typ)
6209 and then not Is_Bit_Packed_Array (Typ)
6210 then
6211 In_Place_Assign_OK_For_Declaration := True;
6212 Tmp := Defining_Identifier (Parent_Node);
6213 Set_No_Initialization (Parent_Node);
6214 Set_Expression (Parent_Node, Empty);
6216 -- Set kind and type of the entity, for use in the analysis
6217 -- of the subsequent assignments. If the nominal type is not
6218 -- constrained, build a subtype from the known bounds of the
6219 -- aggregate. If the declaration has a subtype mark, use it,
6220 -- otherwise use the itype of the aggregate.
6222 Mutate_Ekind (Tmp, E_Variable);
6224 if not Is_Constrained (Typ) then
6225 Build_Constrained_Type (Positional => False);
6227 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6228 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6229 then
6230 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6232 else
6233 Set_Size_Known_At_Compile_Time (Typ, False);
6234 Set_Etype (Tmp, Typ);
6235 end if;
6237 elsif Maybe_In_Place_OK and then Parent_Kind = N_Allocator then
6238 Set_Expansion_Delayed (N);
6239 return;
6241 -- Limited arrays in return statements are expanded when
6242 -- enclosing construct is expanded.
6244 elsif Maybe_In_Place_OK
6245 and then Parent_Kind = N_Simple_Return_Statement
6246 then
6247 Set_Expansion_Delayed (N);
6248 return;
6250 -- In the remaining cases the aggregate appears in the RHS of an
6251 -- assignment, which may be part of the expansion of an object
6252 -- declaration. If the aggregate is an actual in a call, itself
6253 -- possibly in a RHS, building it in the target is not possible.
6255 elsif Maybe_In_Place_OK
6256 and then Nkind (Parent_Node) not in N_Subprogram_Call
6257 and then Safe_Left_Hand_Side (Name (Parent_Node))
6258 then
6259 Tmp := Name (Parent_Node);
6261 if Etype (Tmp) /= Etype (N) then
6262 Apply_Length_Check (N, Etype (Tmp));
6264 if Nkind (N) = N_Raise_Constraint_Error then
6266 -- Static error, nothing further to expand
6268 return;
6269 end if;
6270 end if;
6272 -- If a slice assignment has an aggregate with a single others_choice,
6273 -- the assignment can be done in place even if bounds are not static,
6274 -- by converting it into a loop over the discrete range of the slice.
6276 elsif Maybe_In_Place_OK
6277 and then Nkind (Name (Parent_Node)) = N_Slice
6278 and then Is_Others_Aggregate (N)
6279 then
6280 Tmp := Name (Parent_Node);
6282 -- Set type of aggregate to be type of lhs in assignment, in order
6283 -- to suppress redundant length checks.
6285 Set_Etype (N, Etype (Tmp));
6287 -- Step 5
6289 -- In-place aggregate expansion is not possible
6291 else
6292 Maybe_In_Place_OK := False;
6293 Tmp := Make_Temporary (Loc, 'A', N);
6294 Tmp_Decl :=
6295 Make_Object_Declaration (Loc,
6296 Defining_Identifier => Tmp,
6297 Object_Definition => New_Occurrence_Of (Typ, Loc));
6298 Set_No_Initialization (Tmp_Decl, True);
6300 -- If we are within a loop, the temporary will be pushed on the
6301 -- stack at each iteration. If the aggregate is the expression
6302 -- for an allocator, it will be immediately copied to the heap
6303 -- and can be reclaimed at once. We create a transient scope
6304 -- around the aggregate for this purpose.
6306 if Ekind (Current_Scope) = E_Loop
6307 and then Parent_Kind = N_Allocator
6308 then
6309 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6311 -- If the parent is an assignment for which no controlled actions
6312 -- should take place, prevent the temporary from being finalized.
6314 elsif Parent_Kind = N_Assignment_Statement
6315 and then No_Ctrl_Actions (Parent_Node)
6316 then
6317 Mutate_Ekind (Tmp, E_Variable);
6318 Set_Is_Ignored_Transient (Tmp);
6319 end if;
6321 Insert_Action (N, Tmp_Decl);
6322 end if;
6324 -- Construct and insert the aggregate code. We can safely suppress index
6325 -- checks because this code is guaranteed not to raise CE on index
6326 -- checks. However we should *not* suppress all checks.
6328 declare
6329 Target : Node_Id;
6331 begin
6332 if Nkind (Tmp) = N_Defining_Identifier then
6333 Target := New_Occurrence_Of (Tmp, Loc);
6335 else
6336 if Has_Default_Init_Comps (N)
6337 and then not Maybe_In_Place_OK
6338 then
6339 -- Ada 2005 (AI-287): This case has not been analyzed???
6341 raise Program_Error;
6342 end if;
6344 -- Name in assignment is explicit dereference
6346 Target := New_Copy (Tmp);
6347 end if;
6349 -- If we are to generate an in-place assignment for a declaration or
6350 -- an assignment statement, and the assignment can be done directly
6351 -- by the back end, then do not expand further.
6353 -- ??? We can also do that if in-place expansion is not possible but
6354 -- then we could go into an infinite recursion.
6356 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6357 and then not CodePeer_Mode
6358 and then not Modify_Tree_For_C
6359 and then not Possible_Bit_Aligned_Component (Target)
6360 and then not Is_Possibly_Unaligned_Slice (Target)
6361 and then Aggr_Assignment_OK_For_Backend (N)
6362 then
6364 -- In the case of an assignment using an access with the
6365 -- Designated_Storage_Model aspect with a Copy_To procedure,
6366 -- insert a temporary and have the back end handle the assignment
6367 -- to it. Copy the result to the original target.
6369 if Parent_Kind = N_Assignment_Statement
6370 and then Nkind (Name (Parent_Node)) = N_Explicit_Dereference
6371 and then Has_Designated_Storage_Model_Aspect
6372 (Etype (Prefix (Name (Parent_Node))))
6373 and then Present (Storage_Model_Copy_To
6374 (Storage_Model_Object
6375 (Etype (Prefix (Name (Parent_Node))))))
6376 then
6377 Aggr_Code := Build_Assignment_With_Temporary
6378 (Target, Typ, New_Copy_Tree (N));
6380 else
6381 if Maybe_In_Place_OK then
6382 return;
6383 end if;
6385 Aggr_Code := New_List (
6386 Make_Assignment_Statement (Loc,
6387 Name => Target,
6388 Expression => New_Copy_Tree (N)));
6389 end if;
6391 else
6392 Aggr_Code :=
6393 Build_Array_Aggr_Code (N,
6394 Ctype => Ctyp,
6395 Index => First_Index (Typ),
6396 Into => Target,
6397 Scalar_Comp => Is_Scalar_Type (Ctyp));
6398 end if;
6400 -- Save the last assignment statement associated with the aggregate
6401 -- when building a controlled object. This reference is utilized by
6402 -- the finalization machinery when marking an object as successfully
6403 -- initialized.
6405 if Needs_Finalization (Typ)
6406 and then Is_Entity_Name (Target)
6407 and then Present (Entity (Target))
6408 and then Ekind (Entity (Target)) in E_Constant | E_Variable
6409 then
6410 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6411 end if;
6412 end;
6414 -- If the aggregate is the expression in a declaration, the expanded
6415 -- code must be inserted after it. The defining entity might not come
6416 -- from source if this is part of an inlined body, but the declaration
6417 -- itself will.
6418 -- The test below looks very specialized and kludgy???
6420 if Comes_From_Source (Tmp)
6421 or else
6422 (Nkind (Parent (N)) = N_Object_Declaration
6423 and then Comes_From_Source (Parent (N))
6424 and then Tmp = Defining_Entity (Parent (N)))
6425 then
6426 if Parent_Kind /= N_Object_Declaration or else Is_Frozen (Tmp) then
6427 Insert_Actions_After (Parent_Node, Aggr_Code);
6428 else
6429 declare
6430 Comp_Stmt : constant Node_Id :=
6431 Make_Compound_Statement
6432 (Sloc (Parent_Node), Actions => Aggr_Code);
6433 begin
6434 Insert_Action_After (Parent_Node, Comp_Stmt);
6435 Set_Initialization_Statements (Tmp, Comp_Stmt);
6436 end;
6437 end if;
6438 else
6439 Insert_Actions (N, Aggr_Code);
6440 end if;
6442 -- If the aggregate has been assigned in place, remove the original
6443 -- assignment.
6445 if Parent_Kind = N_Assignment_Statement and then Maybe_In_Place_OK then
6446 Rewrite (Parent_Node, Make_Null_Statement (Loc));
6448 -- Or else, if a temporary was created, replace the aggregate with it
6450 elsif Parent_Kind /= N_Object_Declaration
6451 or else Tmp /= Defining_Identifier (Parent_Node)
6452 then
6453 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6454 Analyze_And_Resolve (N, Typ);
6455 end if;
6456 end Expand_Array_Aggregate;
6458 ------------------------
6459 -- Expand_N_Aggregate --
6460 ------------------------
6462 procedure Expand_N_Aggregate (N : Node_Id) is
6463 T : constant Entity_Id := Etype (N);
6464 begin
6465 -- Record aggregate case
6467 if Is_Record_Type (T)
6468 and then not Is_Private_Type (T)
6469 and then not Is_Homogeneous_Aggregate (N)
6470 then
6471 Expand_Record_Aggregate (N);
6473 elsif Has_Aspect (T, Aspect_Aggregate) then
6474 Expand_Container_Aggregate (N);
6476 -- Array aggregate case
6478 else
6479 -- A special case, if we have a string subtype with bounds 1 .. N,
6480 -- where N is known at compile time, and the aggregate is of the
6481 -- form (others => 'x'), with a single choice and no expressions,
6482 -- and N is less than 80 (an arbitrary limit for now), then replace
6483 -- the aggregate by the equivalent string literal (but do not mark
6484 -- it as static since it is not).
6486 -- Note: this entire circuit is redundant with respect to code in
6487 -- Expand_Array_Aggregate that collapses others choices to positional
6488 -- form, but there are two problems with that circuit:
6490 -- a) It is limited to very small cases due to ill-understood
6491 -- interactions with bootstrapping. That limit is removed by
6492 -- use of the No_Implicit_Loops restriction.
6494 -- b) It incorrectly ends up with the resulting expressions being
6495 -- considered static when they are not. For example, the
6496 -- following test should fail:
6498 -- pragma Restrictions (No_Implicit_Loops);
6499 -- package NonSOthers4 is
6500 -- B : constant String (1 .. 6) := (others => 'A');
6501 -- DH : constant String (1 .. 8) := B & "BB";
6502 -- X : Integer;
6503 -- pragma Export (C, X, Link_Name => DH);
6504 -- end;
6506 -- But it succeeds (DH looks static to pragma Export)
6508 -- To be sorted out ???
6510 if Present (Component_Associations (N)) then
6511 declare
6512 CA : constant Node_Id := First (Component_Associations (N));
6513 MX : constant := 80;
6515 begin
6516 if Present (CA)
6517 and then Nkind (First (Choice_List (CA))) = N_Others_Choice
6518 and then Nkind (Expression (CA)) = N_Character_Literal
6519 and then No (Expressions (N))
6520 then
6521 declare
6522 X : constant Node_Id := First_Index (T);
6523 EC : constant Node_Id := Expression (CA);
6524 CV : constant Uint := Char_Literal_Value (EC);
6525 CC : constant Char_Code := UI_To_CC (CV);
6527 begin
6528 if Nkind (X) = N_Range
6529 and then Compile_Time_Known_Value (Low_Bound (X))
6530 and then Expr_Value (Low_Bound (X)) = 1
6531 and then Compile_Time_Known_Value (High_Bound (X))
6532 then
6533 declare
6534 Hi : constant Uint := Expr_Value (High_Bound (X));
6536 begin
6537 if Hi <= MX then
6538 Start_String;
6540 for J in 1 .. UI_To_Int (Hi) loop
6541 Store_String_Char (CC);
6542 end loop;
6544 Rewrite (N,
6545 Make_String_Literal (Sloc (N),
6546 Strval => End_String));
6548 if In_Character_Range (CC) then
6549 null;
6550 elsif In_Wide_Character_Range (CC) then
6551 Set_Has_Wide_Character (N);
6552 else
6553 Set_Has_Wide_Wide_Character (N);
6554 end if;
6556 Analyze_And_Resolve (N, T);
6557 Set_Is_Static_Expression (N, False);
6558 return;
6559 end if;
6560 end;
6561 end if;
6562 end;
6563 end if;
6564 end;
6565 end if;
6567 -- Not that special case, so normal expansion of array aggregate
6569 Expand_Array_Aggregate (N);
6570 end if;
6572 exception
6573 when RE_Not_Available =>
6574 return;
6575 end Expand_N_Aggregate;
6577 --------------------------------
6578 -- Expand_Container_Aggregate --
6579 --------------------------------
6581 procedure Expand_Container_Aggregate (N : Node_Id) is
6582 Loc : constant Source_Ptr := Sloc (N);
6583 Typ : constant Entity_Id := Etype (N);
6584 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
6586 Empty_Subp : Node_Id := Empty;
6587 Add_Named_Subp : Node_Id := Empty;
6588 Add_Unnamed_Subp : Node_Id := Empty;
6589 New_Indexed_Subp : Node_Id := Empty;
6590 Assign_Indexed_Subp : Node_Id := Empty;
6592 Aggr_Code : constant List_Id := New_List;
6593 Temp : constant Entity_Id := Make_Temporary (Loc, 'C', N);
6595 Comp : Node_Id;
6596 Decl : Node_Id;
6597 Default : Node_Id;
6598 Init_Stat : Node_Id;
6599 Siz : Int;
6601 -- The following are used when the size of the aggregate is not
6602 -- static and requires a dynamic evaluation.
6603 Siz_Decl : Node_Id;
6604 Siz_Exp : Node_Id := Empty;
6605 Count_Type : Entity_Id;
6607 function Aggregate_Size return Int;
6608 -- Compute number of entries in aggregate, including choices
6609 -- that cover a range or subtype, as well as iterated constructs.
6610 -- Return -1 if the size is not known statically, in which case
6611 -- allocate a default size for the aggregate, or build an expression
6612 -- to estimate the size dynamically.
6614 function Build_Siz_Exp (Comp : Node_Id) return Int;
6615 -- When the aggregate contains a single Iterated_Component_Association
6616 -- or Element_Association with non-static bounds, build an expression
6617 -- to be used as the allocated size of the container. This may be an
6618 -- overestimate if a filter is present, but is a safe approximation.
6619 -- If bounds are dynamic the aggregate is created in two passes, and
6620 -- the first generates a loop for the sole purpose of computing the
6621 -- number of elements that will be generated on the second pass.
6623 procedure Expand_Iterated_Component (Comp : Node_Id);
6624 -- Handle iterated_component_association and iterated_Element
6625 -- association by generating a loop over the specified range,
6626 -- given either by a loop parameter specification or an iterator
6627 -- specification.
6629 --------------------
6630 -- Aggregate_Size --
6631 --------------------
6633 function Aggregate_Size return Int is
6634 Comp : Node_Id;
6635 Choice : Node_Id;
6636 Lo, Hi : Node_Id;
6637 Siz : Int;
6639 procedure Add_Range_Size;
6640 -- Compute number of components specified by a component association
6641 -- given by a range or subtype name.
6643 --------------------
6644 -- Add_Range_Size --
6645 --------------------
6647 procedure Add_Range_Size is
6648 begin
6649 -- The bounds of the discrete range are integers or enumeration
6650 -- literals
6652 if Nkind (Lo) = N_Integer_Literal then
6653 Siz := Siz + UI_To_Int (Intval (Hi))
6654 - UI_To_Int (Intval (Lo)) + 1;
6655 else
6656 Siz := Siz + UI_To_Int (Enumeration_Pos (Hi))
6657 - UI_To_Int (Enumeration_Pos (Lo)) + 1;
6658 end if;
6659 end Add_Range_Size;
6661 begin
6662 -- Aggregate is either all positional or all named
6664 Siz := List_Length (Expressions (N));
6666 if Present (Component_Associations (N)) then
6667 Comp := First (Component_Associations (N));
6668 -- If there is a single component association it can be
6669 -- an iterated component with dynamic bounds or an element
6670 -- iterator over an iterable object. If it is an array
6671 -- we can use the attribute Length to get its size;
6672 -- for a predefined container the function Length plays
6673 -- the same role. There is no available mechanism for
6674 -- user-defined containers. For now we treat all of these
6675 -- as dynamic.
6677 if List_Length (Component_Associations (N)) = 1
6678 and then Nkind (Comp) in N_Iterated_Component_Association |
6679 N_Iterated_Element_Association
6680 then
6681 return Build_Siz_Exp (Comp);
6682 end if;
6684 -- Otherwise all associations must specify static sizes.
6686 while Present (Comp) loop
6687 Choice := First (Choice_List (Comp));
6689 while Present (Choice) loop
6690 Analyze (Choice);
6692 if Nkind (Choice) = N_Range then
6693 Lo := Low_Bound (Choice);
6694 Hi := High_Bound (Choice);
6695 Add_Range_Size;
6697 elsif Is_Entity_Name (Choice)
6698 and then Is_Type (Entity (Choice))
6699 then
6700 Lo := Type_Low_Bound (Entity (Choice));
6701 Hi := Type_High_Bound (Entity (Choice));
6702 Add_Range_Size;
6704 Rewrite (Choice,
6705 Make_Range (Loc,
6706 New_Copy_Tree (Lo),
6707 New_Copy_Tree (Hi)));
6709 else
6710 -- Single choice (syntax excludes a subtype
6711 -- indication).
6713 Siz := Siz + 1;
6714 end if;
6716 Next (Choice);
6717 end loop;
6718 Next (Comp);
6719 end loop;
6720 end if;
6722 return Siz;
6723 end Aggregate_Size;
6725 -------------------
6726 -- Build_Siz_Exp --
6727 -------------------
6729 function Build_Siz_Exp (Comp : Node_Id) return Int is
6730 Lo, Hi : Node_Id;
6731 begin
6732 if Nkind (Comp) = N_Range then
6733 Lo := Low_Bound (Comp);
6734 Hi := High_Bound (Comp);
6735 Analyze (Lo);
6736 Analyze (Hi);
6738 -- Compute static size when possible.
6740 if Is_Static_Expression (Lo)
6741 and then Is_Static_Expression (Hi)
6742 then
6743 if Nkind (Lo) = N_Integer_Literal then
6744 Siz := UI_To_Int (Intval (Hi)) - UI_To_Int (Intval (Lo)) + 1;
6745 else
6746 Siz := UI_To_Int (Enumeration_Pos (Hi))
6747 - UI_To_Int (Enumeration_Pos (Lo)) + 1;
6748 end if;
6749 return Siz;
6751 else
6752 Siz_Exp :=
6753 Make_Op_Add (Sloc (Comp),
6754 Left_Opnd =>
6755 Make_Op_Subtract (Sloc (Comp),
6756 Left_Opnd => New_Copy_Tree (Hi),
6757 Right_Opnd => New_Copy_Tree (Lo)),
6758 Right_Opnd =>
6759 Make_Integer_Literal (Loc, 1));
6760 return -1;
6761 end if;
6763 elsif Nkind (Comp) = N_Iterated_Component_Association then
6764 return Build_Siz_Exp (First (Discrete_Choices (Comp)));
6766 elsif Nkind (Comp) = N_Iterated_Element_Association then
6767 return -1;
6769 -- ??? Need to create code for a loop and add to generated code,
6770 -- as is done for array aggregates with iterated element
6771 -- associations, instead of using Append operations.
6773 else
6774 return -1;
6775 end if;
6776 end Build_Siz_Exp;
6778 -------------------------------
6779 -- Expand_Iterated_Component --
6780 -------------------------------
6782 procedure Expand_Iterated_Component (Comp : Node_Id) is
6783 Expr : constant Node_Id := Expression (Comp);
6785 Key_Expr : Node_Id := Empty;
6786 Loop_Id : Entity_Id;
6787 L_Range : Node_Id;
6788 L_Iteration_Scheme : Node_Id;
6789 Loop_Stat : Node_Id;
6790 Params : List_Id;
6791 Stats : List_Id;
6793 begin
6794 if Nkind (Comp) = N_Iterated_Element_Association then
6795 Key_Expr := Key_Expression (Comp);
6797 -- We create a new entity as loop identifier in all cases,
6798 -- as is done for generated loops elsewhere, as the loop
6799 -- structure has been previously analyzed.
6801 if Present (Iterator_Specification (Comp)) then
6803 -- Either an Iterator_Specification or a Loop_Parameter_
6804 -- Specification is present.
6806 L_Iteration_Scheme :=
6807 Make_Iteration_Scheme (Loc,
6808 Iterator_Specification => Iterator_Specification (Comp));
6809 Loop_Id :=
6810 Make_Defining_Identifier (Loc,
6811 Chars => Chars (Defining_Identifier
6812 (Iterator_Specification (Comp))));
6813 Set_Defining_Identifier
6814 (Iterator_Specification (L_Iteration_Scheme), Loop_Id);
6816 else
6817 L_Iteration_Scheme :=
6818 Make_Iteration_Scheme (Loc,
6819 Loop_Parameter_Specification =>
6820 Loop_Parameter_Specification (Comp));
6821 Loop_Id :=
6822 Make_Defining_Identifier (Loc,
6823 Chars => Chars (Defining_Identifier
6824 (Loop_Parameter_Specification (Comp))));
6825 Set_Defining_Identifier
6826 (Loop_Parameter_Specification
6827 (L_Iteration_Scheme), Loop_Id);
6828 end if;
6829 else
6831 -- Iterated_Component_Association.
6833 if Present (Iterator_Specification (Comp)) then
6834 Loop_Id :=
6835 Make_Defining_Identifier (Loc,
6836 Chars => Chars (Defining_Identifier
6837 (Iterator_Specification (Comp))));
6838 L_Iteration_Scheme :=
6839 Make_Iteration_Scheme (Loc,
6840 Iterator_Specification => Iterator_Specification (Comp));
6842 else
6843 -- Loop_Parameter_Specification is parsed with a choice list.
6844 -- where the range is the first (and only) choice.
6846 Loop_Id :=
6847 Make_Defining_Identifier (Loc,
6848 Chars => Chars (Defining_Identifier (Comp)));
6849 L_Range := Relocate_Node (First (Discrete_Choices (Comp)));
6851 L_Iteration_Scheme :=
6852 Make_Iteration_Scheme (Loc,
6853 Loop_Parameter_Specification =>
6854 Make_Loop_Parameter_Specification (Loc,
6855 Defining_Identifier => Loop_Id,
6856 Discrete_Subtype_Definition => L_Range));
6857 end if;
6858 end if;
6860 -- Build insertion statement. For a positional aggregate, only the
6861 -- expression is needed. For a named aggregate, the loop variable,
6862 -- whose type is that of the key, is an additional parameter for
6863 -- the insertion operation.
6864 -- If a Key_Expression is present, it serves as the additional
6865 -- parameter. Otherwise the key is given by the loop parameter
6866 -- itself.
6868 if Present (Add_Unnamed_Subp)
6869 and then No (Add_Named_Subp)
6870 then
6871 Stats := New_List
6872 (Make_Procedure_Call_Statement (Loc,
6873 Name => New_Occurrence_Of (Entity (Add_Unnamed_Subp), Loc),
6874 Parameter_Associations =>
6875 New_List (New_Occurrence_Of (Temp, Loc),
6876 New_Copy_Tree (Expr))));
6877 else
6878 -- Named or indexed aggregate, for which a key is present,
6879 -- possibly with a specified key_expression.
6881 if Present (Key_Expr) then
6882 Params := New_List (New_Occurrence_Of (Temp, Loc),
6883 New_Copy_Tree (Key_Expr),
6884 New_Copy_Tree (Expr));
6885 else
6886 Params := New_List (New_Occurrence_Of (Temp, Loc),
6887 New_Occurrence_Of (Loop_Id, Loc),
6888 New_Copy_Tree (Expr));
6889 end if;
6891 Stats := New_List
6892 (Make_Procedure_Call_Statement (Loc,
6893 Name => New_Occurrence_Of (Entity (Add_Named_Subp), Loc),
6894 Parameter_Associations => Params));
6895 end if;
6897 Loop_Stat := Make_Implicit_Loop_Statement
6898 (Node => N,
6899 Identifier => Empty,
6900 Iteration_Scheme => L_Iteration_Scheme,
6901 Statements => Stats);
6902 Append (Loop_Stat, Aggr_Code);
6904 end Expand_Iterated_Component;
6906 -- Start of processing for Expand_Container_Aggregate
6908 begin
6909 Parse_Aspect_Aggregate (Asp,
6910 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
6911 New_Indexed_Subp, Assign_Indexed_Subp);
6913 -- The constructor for bounded containers is a function with
6914 -- a parameter that sets the size of the container. If the
6915 -- size cannot be determined statically we use a default value
6916 -- or a dynamic expression.
6918 Siz := Aggregate_Size;
6920 if Ekind (Entity (Empty_Subp)) = E_Function
6921 and then Present (First_Formal (Entity (Empty_Subp)))
6922 then
6923 Default := Default_Value (First_Formal (Entity (Empty_Subp)));
6925 -- If aggregate size is not static, we can use default value
6926 -- of formal parameter for allocation. We assume that this
6927 -- (implementation-dependent) value is static, even though
6928 -- the AI does not require it.
6930 -- Create declaration for size: a constant literal in the simple
6931 -- case, an expression if iterated component associations may be
6932 -- involved, the default otherwise.
6934 Count_Type := Etype (First_Formal (Entity (Empty_Subp)));
6935 if Siz = -1 then
6936 if No (Siz_Exp) then
6937 Siz := UI_To_Int (Intval (Default));
6938 Siz_Exp := Make_Integer_Literal (Loc, Siz);
6940 else
6941 Siz_Exp := Make_Type_Conversion (Loc,
6942 Subtype_Mark =>
6943 New_Occurrence_Of (Count_Type, Loc),
6944 Expression => Siz_Exp);
6945 end if;
6947 else
6948 Siz_Exp := Make_Integer_Literal (Loc, Siz);
6949 end if;
6951 Siz_Decl := Make_Object_Declaration (Loc,
6952 Defining_Identifier => Make_Temporary (Loc, 'S', N),
6953 Object_Definition =>
6954 New_Occurrence_Of (Count_Type, Loc),
6955 Expression => Siz_Exp);
6956 Append (Siz_Decl, Aggr_Code);
6958 if Nkind (Siz_Exp) = N_Integer_Literal then
6959 Init_Stat :=
6960 Make_Object_Declaration (Loc,
6961 Defining_Identifier => Temp,
6962 Object_Definition => New_Occurrence_Of (Typ, Loc),
6963 Expression => Make_Function_Call (Loc,
6964 Name => New_Occurrence_Of (Entity (Empty_Subp), Loc),
6965 Parameter_Associations =>
6966 New_List
6967 (New_Occurrence_Of
6968 (Defining_Identifier (Siz_Decl), Loc))));
6970 else
6971 Init_Stat :=
6972 Make_Object_Declaration (Loc,
6973 Defining_Identifier => Temp,
6974 Object_Definition => New_Occurrence_Of (Typ, Loc),
6975 Expression => Make_Function_Call (Loc,
6976 Name =>
6977 New_Occurrence_Of (Entity (New_Indexed_Subp), Loc),
6978 Parameter_Associations =>
6979 New_List (
6980 Make_Integer_Literal (Loc, 1),
6981 New_Occurrence_Of
6982 (Defining_Identifier (Siz_Decl), Loc))));
6983 end if;
6985 Append (Init_Stat, Aggr_Code);
6987 -- Size is dynamic: Create declaration for object, and intitialize
6988 -- with a call to the null container, or an assignment to it.
6990 else
6991 Decl :=
6992 Make_Object_Declaration (Loc,
6993 Defining_Identifier => Temp,
6994 Object_Definition => New_Occurrence_Of (Typ, Loc));
6996 Insert_Action (N, Decl);
6998 -- The Empty entity is either a parameterless function, or
6999 -- a constant.
7001 if Ekind (Entity (Empty_Subp)) = E_Function then
7002 Init_Stat := Make_Assignment_Statement (Loc,
7003 Name => New_Occurrence_Of (Temp, Loc),
7004 Expression => Make_Function_Call (Loc,
7005 Name => New_Occurrence_Of (Entity (Empty_Subp), Loc)));
7007 else
7008 Init_Stat := Make_Assignment_Statement (Loc,
7009 Name => New_Occurrence_Of (Temp, Loc),
7010 Expression => New_Occurrence_Of (Entity (Empty_Subp), Loc));
7011 end if;
7013 Append (Init_Stat, Aggr_Code);
7014 end if;
7016 ---------------------------
7017 -- Positional aggregate --
7018 ---------------------------
7020 -- If the aggregate is positional the aspect must include
7021 -- an Add_Unnamed subprogram.
7023 if Present (Add_Unnamed_Subp) then
7024 if Present (Expressions (N)) then
7025 declare
7026 Insert : constant Entity_Id := Entity (Add_Unnamed_Subp);
7027 Comp : Node_Id;
7028 Stat : Node_Id;
7030 begin
7031 Comp := First (Expressions (N));
7032 while Present (Comp) loop
7033 Stat := Make_Procedure_Call_Statement (Loc,
7034 Name => New_Occurrence_Of (Insert, Loc),
7035 Parameter_Associations =>
7036 New_List (New_Occurrence_Of (Temp, Loc),
7037 New_Copy_Tree (Comp)));
7038 Append (Stat, Aggr_Code);
7039 Next (Comp);
7040 end loop;
7041 end;
7042 end if;
7044 -- Indexed aggregates are handled below. Unnamed aggregates
7045 -- such as sets may include iterated component associations.
7047 if No (New_Indexed_Subp) then
7048 Comp := First (Component_Associations (N));
7049 while Present (Comp) loop
7050 if Nkind (Comp) = N_Iterated_Component_Association then
7051 Expand_Iterated_Component (Comp);
7052 end if;
7053 Next (Comp);
7054 end loop;
7055 end if;
7057 ---------------------
7058 -- Named_Aggregate --
7059 ---------------------
7061 elsif Present (Add_Named_Subp) then
7062 declare
7063 Insert : constant Entity_Id := Entity (Add_Named_Subp);
7064 Stat : Node_Id;
7065 Key : Node_Id;
7066 begin
7067 Comp := First (Component_Associations (N));
7069 -- Each component association may contain several choices;
7070 -- generate an insertion statement for each.
7072 while Present (Comp) loop
7073 if Nkind (Comp) in N_Iterated_Component_Association
7074 | N_Iterated_Element_Association
7075 then
7076 Expand_Iterated_Component (Comp);
7077 else
7078 Key := First (Choices (Comp));
7080 while Present (Key) loop
7081 Stat := Make_Procedure_Call_Statement (Loc,
7082 Name => New_Occurrence_Of (Insert, Loc),
7083 Parameter_Associations =>
7084 New_List (New_Occurrence_Of (Temp, Loc),
7085 New_Copy_Tree (Key),
7086 New_Copy_Tree (Expression (Comp))));
7087 Append (Stat, Aggr_Code);
7089 Next (Key);
7090 end loop;
7091 end if;
7093 Next (Comp);
7094 end loop;
7095 end;
7096 end if;
7098 -----------------------
7099 -- Indexed_Aggregate --
7100 -----------------------
7102 -- For an indexed aggregate there must be an Assigned_Indexeed
7103 -- subprogram. Note that unlike array aggregates, a container
7104 -- aggregate must be fully positional or fully indexed. In the
7105 -- first case the expansion has already taken place.
7106 -- TBA: the keys for an indexed aggregate must provide a dense
7107 -- range with no repetitions.
7109 if Present (Assign_Indexed_Subp)
7110 and then Present (Component_Associations (N))
7111 then
7112 declare
7113 Insert : constant Entity_Id := Entity (Assign_Indexed_Subp);
7114 Index_Type : constant Entity_Id :=
7115 Etype (Next_Formal (First_Formal (Insert)));
7117 function Expand_Range_Component
7118 (Rng : Node_Id;
7119 Expr : Node_Id) return Node_Id;
7120 -- Transform a component assoication with a range into an
7121 -- explicit loop. If the choice is a subtype name, it is
7122 -- rewritten as a range with the corresponding bounds, which
7123 -- are known to be static.
7125 Comp : Node_Id;
7126 Index : Node_Id;
7127 Pos : Int := 0;
7128 Stat : Node_Id;
7129 Key : Node_Id;
7131 -----------------------------
7132 -- Expand_Raange_Component --
7133 -----------------------------
7135 function Expand_Range_Component
7136 (Rng : Node_Id;
7137 Expr : Node_Id) return Node_Id
7139 Loop_Id : constant Entity_Id :=
7140 Make_Temporary (Loc, 'T');
7142 L_Iteration_Scheme : Node_Id;
7143 Stats : List_Id;
7145 begin
7146 L_Iteration_Scheme :=
7147 Make_Iteration_Scheme (Loc,
7148 Loop_Parameter_Specification =>
7149 Make_Loop_Parameter_Specification (Loc,
7150 Defining_Identifier => Loop_Id,
7151 Discrete_Subtype_Definition => New_Copy_Tree (Rng)));
7153 Stats := New_List
7154 (Make_Procedure_Call_Statement (Loc,
7155 Name =>
7156 New_Occurrence_Of (Entity (Assign_Indexed_Subp), Loc),
7157 Parameter_Associations =>
7158 New_List (New_Occurrence_Of (Temp, Loc),
7159 New_Occurrence_Of (Loop_Id, Loc),
7160 New_Copy_Tree (Expr))));
7162 return Make_Implicit_Loop_Statement
7163 (Node => N,
7164 Identifier => Empty,
7165 Iteration_Scheme => L_Iteration_Scheme,
7166 Statements => Stats);
7167 end Expand_Range_Component;
7169 begin
7170 if Siz > 0 then
7172 -- Modify the call to the constructor to allocate the
7173 -- required size for the aggregwte : call the provided
7174 -- constructor rather than the Empty aggregate.
7176 Index := Make_Op_Add (Loc,
7177 Left_Opnd => New_Copy_Tree (Type_Low_Bound (Index_Type)),
7178 Right_Opnd => Make_Integer_Literal (Loc, Siz - 1));
7180 Set_Expression (Init_Stat,
7181 Make_Function_Call (Loc,
7182 Name =>
7183 New_Occurrence_Of (Entity (New_Indexed_Subp), Loc),
7184 Parameter_Associations =>
7185 New_List (
7186 New_Copy_Tree (Type_Low_Bound (Index_Type)),
7187 Index)));
7188 end if;
7190 if Present (Expressions (N)) then
7191 Comp := First (Expressions (N));
7193 while Present (Comp) loop
7195 -- Compute index position for successive components
7196 -- in the list of expressions, and use the indexed
7197 -- assignment procedure for each.
7199 Index := Make_Op_Add (Loc,
7200 Left_Opnd => Type_Low_Bound (Index_Type),
7201 Right_Opnd => Make_Integer_Literal (Loc, Pos));
7203 Stat := Make_Procedure_Call_Statement (Loc,
7204 Name => New_Occurrence_Of (Insert, Loc),
7205 Parameter_Associations =>
7206 New_List (New_Occurrence_Of (Temp, Loc),
7207 Index,
7208 New_Copy_Tree (Comp)));
7210 Pos := Pos + 1;
7212 Append (Stat, Aggr_Code);
7213 Next (Comp);
7214 end loop;
7215 end if;
7217 if Present (Component_Associations (N)) then
7218 Comp := First (Component_Associations (N));
7220 -- The choice may be a static value, or a range with
7221 -- static bounds.
7223 while Present (Comp) loop
7224 if Nkind (Comp) = N_Component_Association then
7225 Key := First (Choices (Comp));
7226 while Present (Key) loop
7228 -- If the expression is a box, the corresponding
7229 -- component (s) is left uninitialized.
7231 if Box_Present (Comp) then
7232 goto Next_Key;
7234 elsif Nkind (Key) = N_Range then
7236 -- Create loop for tne specified range,
7237 -- with copies of the expression.
7239 Stat :=
7240 Expand_Range_Component (Key, Expression (Comp));
7242 else
7243 Stat := Make_Procedure_Call_Statement (Loc,
7244 Name => New_Occurrence_Of
7245 (Entity (Assign_Indexed_Subp), Loc),
7246 Parameter_Associations =>
7247 New_List (New_Occurrence_Of (Temp, Loc),
7248 New_Copy_Tree (Key),
7249 New_Copy_Tree (Expression (Comp))));
7250 end if;
7252 Append (Stat, Aggr_Code);
7254 <<Next_Key>>
7255 Next (Key);
7256 end loop;
7258 else
7259 -- Iterated component association. Discard
7260 -- positional insertion procedure.
7262 if not Present (Iterator_Specification (Comp)) then
7263 Add_Named_Subp := Assign_Indexed_Subp;
7264 Add_Unnamed_Subp := Empty;
7265 end if;
7267 Expand_Iterated_Component (Comp);
7268 end if;
7270 Next (Comp);
7271 end loop;
7272 end if;
7273 end;
7274 end if;
7276 Insert_Actions (N, Aggr_Code);
7277 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7278 Analyze_And_Resolve (N, Typ);
7279 end Expand_Container_Aggregate;
7281 ------------------------------
7282 -- Expand_N_Delta_Aggregate --
7283 ------------------------------
7285 procedure Expand_N_Delta_Aggregate (N : Node_Id) is
7286 Loc : constant Source_Ptr := Sloc (N);
7287 Typ : constant Entity_Id := Etype (Expression (N));
7288 Decl : Node_Id;
7290 begin
7291 Decl :=
7292 Make_Object_Declaration (Loc,
7293 Defining_Identifier => Make_Temporary (Loc, 'T'),
7294 Object_Definition => New_Occurrence_Of (Typ, Loc),
7295 Expression => New_Copy_Tree (Expression (N)));
7297 if Is_Array_Type (Etype (N)) then
7298 Expand_Delta_Array_Aggregate (N, New_List (Decl));
7299 else
7300 Expand_Delta_Record_Aggregate (N, New_List (Decl));
7301 end if;
7302 end Expand_N_Delta_Aggregate;
7304 ----------------------------------
7305 -- Expand_Delta_Array_Aggregate --
7306 ----------------------------------
7308 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is
7309 Loc : constant Source_Ptr := Sloc (N);
7310 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
7311 Assoc : Node_Id;
7313 function Generate_Loop (C : Node_Id) return Node_Id;
7314 -- Generate a loop containing individual component assignments for
7315 -- choices that are ranges, subtype indications, subtype names, and
7316 -- iterated component associations.
7318 -------------------
7319 -- Generate_Loop --
7320 -------------------
7322 function Generate_Loop (C : Node_Id) return Node_Id is
7323 Sl : constant Source_Ptr := Sloc (C);
7324 Ix : Entity_Id;
7326 begin
7327 if Nkind (Parent (C)) = N_Iterated_Component_Association then
7328 Ix :=
7329 Make_Defining_Identifier (Loc,
7330 Chars => (Chars (Defining_Identifier (Parent (C)))));
7331 else
7332 Ix := Make_Temporary (Sl, 'I');
7333 end if;
7335 return
7336 Make_Implicit_Loop_Statement (C,
7337 Iteration_Scheme =>
7338 Make_Iteration_Scheme (Sl,
7339 Loop_Parameter_Specification =>
7340 Make_Loop_Parameter_Specification (Sl,
7341 Defining_Identifier => Ix,
7342 Discrete_Subtype_Definition => New_Copy_Tree (C))),
7344 Statements => New_List (
7345 Make_Assignment_Statement (Sl,
7346 Name =>
7347 Make_Indexed_Component (Sl,
7348 Prefix => New_Occurrence_Of (Temp, Sl),
7349 Expressions => New_List (New_Occurrence_Of (Ix, Sl))),
7350 Expression => New_Copy_Tree (Expression (Assoc)))),
7351 End_Label => Empty);
7352 end Generate_Loop;
7354 -- Local variables
7356 Choice : Node_Id;
7358 -- Start of processing for Expand_Delta_Array_Aggregate
7360 begin
7361 Assoc := First (Component_Associations (N));
7362 while Present (Assoc) loop
7363 Choice := First (Choice_List (Assoc));
7364 if Nkind (Assoc) = N_Iterated_Component_Association then
7365 while Present (Choice) loop
7366 Append_To (Deltas, Generate_Loop (Choice));
7367 Next (Choice);
7368 end loop;
7370 else
7371 while Present (Choice) loop
7373 -- Choice can be given by a range, a subtype indication, a
7374 -- subtype name, a scalar value, or an entity.
7376 if Nkind (Choice) = N_Range
7377 or else (Is_Entity_Name (Choice)
7378 and then Is_Type (Entity (Choice)))
7379 then
7380 Append_To (Deltas, Generate_Loop (Choice));
7382 elsif Nkind (Choice) = N_Subtype_Indication then
7383 Append_To (Deltas,
7384 Generate_Loop (Range_Expression (Constraint (Choice))));
7386 else
7387 Append_To (Deltas,
7388 Make_Assignment_Statement (Sloc (Choice),
7389 Name =>
7390 Make_Indexed_Component (Sloc (Choice),
7391 Prefix => New_Occurrence_Of (Temp, Loc),
7392 Expressions => New_List (New_Copy_Tree (Choice))),
7393 Expression => New_Copy_Tree (Expression (Assoc))));
7394 end if;
7396 Next (Choice);
7397 end loop;
7398 end if;
7400 Next (Assoc);
7401 end loop;
7403 Insert_Actions (N, Deltas);
7404 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7405 end Expand_Delta_Array_Aggregate;
7407 -----------------------------------
7408 -- Expand_Delta_Record_Aggregate --
7409 -----------------------------------
7411 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is
7412 Loc : constant Source_Ptr := Sloc (N);
7413 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
7414 Assoc : Node_Id;
7415 Choice : Node_Id;
7417 begin
7418 Assoc := First (Component_Associations (N));
7420 while Present (Assoc) loop
7421 Choice := First (Choice_List (Assoc));
7422 while Present (Choice) loop
7423 Append_To (Deltas,
7424 Make_Assignment_Statement (Sloc (Choice),
7425 Name =>
7426 Make_Selected_Component (Sloc (Choice),
7427 Prefix => New_Occurrence_Of (Temp, Loc),
7428 Selector_Name => Make_Identifier (Loc, Chars (Choice))),
7429 Expression => New_Copy_Tree (Expression (Assoc))));
7430 Next (Choice);
7431 end loop;
7433 Next (Assoc);
7434 end loop;
7436 Insert_Actions (N, Deltas);
7437 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7438 end Expand_Delta_Record_Aggregate;
7440 ----------------------------------
7441 -- Expand_N_Extension_Aggregate --
7442 ----------------------------------
7444 -- If the ancestor part is an expression, add a component association for
7445 -- the parent field. If the type of the ancestor part is not the direct
7446 -- parent of the expected type, build recursively the needed ancestors.
7447 -- If the ancestor part is a subtype_mark, replace aggregate with a
7448 -- declaration for a temporary of the expected type, followed by
7449 -- individual assignments to the given components.
7451 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
7452 A : constant Node_Id := Ancestor_Part (N);
7453 Loc : constant Source_Ptr := Sloc (N);
7454 Typ : constant Entity_Id := Etype (N);
7456 begin
7457 -- If the ancestor is a subtype mark, an init proc must be called
7458 -- on the resulting object which thus has to be materialized in
7459 -- the front-end
7461 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
7462 Convert_To_Assignments (N, Typ);
7464 -- The extension aggregate is transformed into a record aggregate
7465 -- of the following form (c1 and c2 are inherited components)
7467 -- (Exp with c3 => a, c4 => b)
7468 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
7470 else
7471 Set_Etype (N, Typ);
7473 if Tagged_Type_Expansion then
7474 Expand_Record_Aggregate (N,
7475 Orig_Tag =>
7476 New_Occurrence_Of
7477 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
7478 Parent_Expr => A);
7480 -- No tag is needed in the case of a VM
7482 else
7483 Expand_Record_Aggregate (N, Parent_Expr => A);
7484 end if;
7485 end if;
7487 exception
7488 when RE_Not_Available =>
7489 return;
7490 end Expand_N_Extension_Aggregate;
7492 -----------------------------
7493 -- Expand_Record_Aggregate --
7494 -----------------------------
7496 procedure Expand_Record_Aggregate
7497 (N : Node_Id;
7498 Orig_Tag : Node_Id := Empty;
7499 Parent_Expr : Node_Id := Empty)
7501 Loc : constant Source_Ptr := Sloc (N);
7502 Comps : constant List_Id := Component_Associations (N);
7503 Typ : constant Entity_Id := Etype (N);
7504 Base_Typ : constant Entity_Id := Base_Type (Typ);
7506 Static_Components : Boolean := True;
7507 -- Flag to indicate whether all components are compile-time known,
7508 -- and the aggregate can be constructed statically and handled by
7509 -- the back-end. Set to False by Component_OK_For_Backend.
7511 procedure Build_Back_End_Aggregate;
7512 -- Build a proper aggregate to be handled by the back-end
7514 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
7515 -- Returns true if N is an expression of composite type which can be
7516 -- fully evaluated at compile time without raising constraint error.
7517 -- Such expressions can be passed as is to Gigi without any expansion.
7519 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
7520 -- set and constants whose expression is such an aggregate, recursively.
7522 function Component_OK_For_Backend return Boolean;
7523 -- Check for presence of a component which makes it impossible for the
7524 -- backend to process the aggregate, thus requiring the use of a series
7525 -- of assignment statements. Cases checked for are a nested aggregate
7526 -- needing Late_Expansion, the presence of a tagged component which may
7527 -- need tag adjustment, and a bit unaligned component reference.
7529 -- We also force expansion into assignments if a component is of a
7530 -- mutable type (including a private type with discriminants) because
7531 -- in that case the size of the component to be copied may be smaller
7532 -- than the side of the target, and there is no simple way for gigi
7533 -- to compute the size of the object to be copied.
7535 -- NOTE: This is part of the ongoing work to define precisely the
7536 -- interface between front-end and back-end handling of aggregates.
7537 -- In general it is desirable to pass aggregates as they are to gigi,
7538 -- in order to minimize elaboration code. This is one case where the
7539 -- semantics of Ada complicate the analysis and lead to anomalies in
7540 -- the gcc back-end if the aggregate is not expanded into assignments.
7542 -- NOTE: This sets the global Static_Components to False in most, but
7543 -- not all, cases when it returns False.
7545 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
7546 -- Return True if any element of L has Has_Per_Object_Constraint set.
7547 -- L should be the Choices component of an N_Component_Association.
7549 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
7550 -- If any ancestor of the current type is private, the aggregate
7551 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
7552 -- because it will not be set when type and its parent are in the
7553 -- same scope, and the parent component needs expansion.
7555 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
7556 -- For nested aggregates return the ultimate enclosing aggregate; for
7557 -- non-nested aggregates return N.
7559 ------------------------------
7560 -- Build_Back_End_Aggregate --
7561 ------------------------------
7563 procedure Build_Back_End_Aggregate is
7564 Comp : Entity_Id;
7565 New_Comp : Node_Id;
7566 Tag_Value : Node_Id;
7568 begin
7569 if Nkind (N) = N_Aggregate then
7571 -- If the aggregate is static and can be handled by the back-end,
7572 -- nothing left to do.
7574 if Static_Components then
7575 Set_Compile_Time_Known_Aggregate (N);
7576 Set_Expansion_Delayed (N, False);
7577 end if;
7578 end if;
7580 -- If no discriminants, nothing special to do
7582 if not Has_Discriminants (Typ) then
7583 null;
7585 -- Case of discriminants present
7587 elsif Is_Derived_Type (Typ) then
7589 -- For untagged types, non-stored discriminants are replaced with
7590 -- stored discriminants, which are the ones that gigi uses to
7591 -- describe the type and its components.
7593 Generate_Aggregate_For_Derived_Type : declare
7594 procedure Prepend_Stored_Values (T : Entity_Id);
7595 -- Scan the list of stored discriminants of the type, and add
7596 -- their values to the aggregate being built.
7598 ---------------------------
7599 -- Prepend_Stored_Values --
7600 ---------------------------
7602 procedure Prepend_Stored_Values (T : Entity_Id) is
7603 Discr : Entity_Id;
7604 First_Comp : Node_Id := Empty;
7606 begin
7607 Discr := First_Stored_Discriminant (T);
7608 while Present (Discr) loop
7609 New_Comp :=
7610 Make_Component_Association (Loc,
7611 Choices => New_List (
7612 New_Occurrence_Of (Discr, Loc)),
7613 Expression =>
7614 New_Copy_Tree
7615 (Get_Discriminant_Value
7616 (Discr,
7617 Typ,
7618 Discriminant_Constraint (Typ))));
7620 if No (First_Comp) then
7621 Prepend_To (Component_Associations (N), New_Comp);
7622 else
7623 Insert_After (First_Comp, New_Comp);
7624 end if;
7626 First_Comp := New_Comp;
7627 Next_Stored_Discriminant (Discr);
7628 end loop;
7629 end Prepend_Stored_Values;
7631 -- Local variables
7633 Constraints : constant List_Id := New_List;
7635 Discr : Entity_Id;
7636 Decl : Node_Id;
7637 Num_Disc : Nat := 0;
7638 Num_Stor : Nat := 0;
7640 -- Start of processing for Generate_Aggregate_For_Derived_Type
7642 begin
7643 -- Remove the associations for the discriminant of derived type
7645 declare
7646 First_Comp : Node_Id;
7648 begin
7649 First_Comp := First (Component_Associations (N));
7650 while Present (First_Comp) loop
7651 Comp := First_Comp;
7652 Next (First_Comp);
7654 if Ekind (Entity (First (Choices (Comp)))) =
7655 E_Discriminant
7656 then
7657 Remove (Comp);
7658 Num_Disc := Num_Disc + 1;
7659 end if;
7660 end loop;
7661 end;
7663 -- Insert stored discriminant associations in the correct
7664 -- order. If there are more stored discriminants than new
7665 -- discriminants, there is at least one new discriminant that
7666 -- constrains more than one of the stored discriminants. In
7667 -- this case we need to construct a proper subtype of the
7668 -- parent type, in order to supply values to all the
7669 -- components. Otherwise there is one-one correspondence
7670 -- between the constraints and the stored discriminants.
7672 Discr := First_Stored_Discriminant (Base_Type (Typ));
7673 while Present (Discr) loop
7674 Num_Stor := Num_Stor + 1;
7675 Next_Stored_Discriminant (Discr);
7676 end loop;
7678 -- Case of more stored discriminants than new discriminants
7680 if Num_Stor > Num_Disc then
7682 -- Create a proper subtype of the parent type, which is the
7683 -- proper implementation type for the aggregate, and convert
7684 -- it to the intended target type.
7686 Discr := First_Stored_Discriminant (Base_Type (Typ));
7687 while Present (Discr) loop
7688 New_Comp :=
7689 New_Copy_Tree
7690 (Get_Discriminant_Value
7691 (Discr,
7692 Typ,
7693 Discriminant_Constraint (Typ)));
7695 Append (New_Comp, Constraints);
7696 Next_Stored_Discriminant (Discr);
7697 end loop;
7699 Decl :=
7700 Make_Subtype_Declaration (Loc,
7701 Defining_Identifier => Make_Temporary (Loc, 'T'),
7702 Subtype_Indication =>
7703 Make_Subtype_Indication (Loc,
7704 Subtype_Mark =>
7705 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
7706 Constraint =>
7707 Make_Index_Or_Discriminant_Constraint
7708 (Loc, Constraints)));
7710 Insert_Action (N, Decl);
7711 Prepend_Stored_Values (Base_Type (Typ));
7713 Set_Etype (N, Defining_Identifier (Decl));
7714 Set_Analyzed (N);
7716 Rewrite (N, Unchecked_Convert_To (Typ, N));
7717 Analyze (N);
7719 -- Case where we do not have fewer new discriminants than
7720 -- stored discriminants, so in this case we can simply use the
7721 -- stored discriminants of the subtype.
7723 else
7724 Prepend_Stored_Values (Typ);
7725 end if;
7726 end Generate_Aggregate_For_Derived_Type;
7727 end if;
7729 if Is_Tagged_Type (Typ) then
7731 -- In the tagged case, _parent and _tag component must be created
7733 -- Reset Null_Present unconditionally. Tagged records always have
7734 -- at least one field (the tag or the parent).
7736 Set_Null_Record_Present (N, False);
7738 -- When the current aggregate comes from the expansion of an
7739 -- extension aggregate, the parent expr is replaced by an
7740 -- aggregate formed by selected components of this expr.
7742 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
7743 Comp := First_Component_Or_Discriminant (Typ);
7744 while Present (Comp) loop
7746 -- Skip all expander-generated components
7748 if not Comes_From_Source (Original_Record_Component (Comp))
7749 then
7750 null;
7752 else
7753 New_Comp :=
7754 Make_Selected_Component (Loc,
7755 Prefix =>
7756 Unchecked_Convert_To (Typ,
7757 Duplicate_Subexpr (Parent_Expr, True)),
7758 Selector_Name => New_Occurrence_Of (Comp, Loc));
7760 Append_To (Comps,
7761 Make_Component_Association (Loc,
7762 Choices => New_List (
7763 New_Occurrence_Of (Comp, Loc)),
7764 Expression => New_Comp));
7766 Analyze_And_Resolve (New_Comp, Etype (Comp));
7767 end if;
7769 Next_Component_Or_Discriminant (Comp);
7770 end loop;
7771 end if;
7773 -- Compute the value for the Tag now, if the type is a root it
7774 -- will be included in the aggregate right away, otherwise it will
7775 -- be propagated to the parent aggregate.
7777 if Present (Orig_Tag) then
7778 Tag_Value := Orig_Tag;
7780 elsif not Tagged_Type_Expansion then
7781 Tag_Value := Empty;
7783 else
7784 Tag_Value :=
7785 New_Occurrence_Of
7786 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
7787 end if;
7789 -- For a derived type, an aggregate for the parent is formed with
7790 -- all the inherited components.
7792 if Is_Derived_Type (Typ) then
7793 declare
7794 First_Comp : Node_Id;
7795 Parent_Comps : List_Id;
7796 Parent_Aggr : Node_Id;
7797 Parent_Name : Node_Id;
7799 begin
7800 First_Comp := First (Component_Associations (N));
7801 Parent_Comps := New_List;
7803 -- First skip the discriminants
7805 while Present (First_Comp)
7806 and then Ekind (Entity (First (Choices (First_Comp))))
7807 = E_Discriminant
7808 loop
7809 Next (First_Comp);
7810 end loop;
7812 -- Then remove the inherited component association from the
7813 -- aggregate and store them in the parent aggregate
7815 while Present (First_Comp)
7816 and then
7817 Scope (Original_Record_Component
7818 (Entity (First (Choices (First_Comp))))) /=
7819 Base_Typ
7820 loop
7821 Comp := First_Comp;
7822 Next (First_Comp);
7823 Remove (Comp);
7824 Append (Comp, Parent_Comps);
7825 end loop;
7827 Parent_Aggr :=
7828 Make_Aggregate (Loc,
7829 Component_Associations => Parent_Comps);
7830 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
7832 -- Find the _parent component
7834 Comp := First_Component (Typ);
7835 while Chars (Comp) /= Name_uParent loop
7836 Next_Component (Comp);
7837 end loop;
7839 Parent_Name := New_Occurrence_Of (Comp, Loc);
7841 -- Insert the parent aggregate
7843 Prepend_To (Component_Associations (N),
7844 Make_Component_Association (Loc,
7845 Choices => New_List (Parent_Name),
7846 Expression => Parent_Aggr));
7848 -- Expand recursively the parent propagating the right Tag
7850 Expand_Record_Aggregate
7851 (Parent_Aggr, Tag_Value, Parent_Expr);
7853 -- The ancestor part may be a nested aggregate that has
7854 -- delayed expansion: recheck now.
7856 if not Component_OK_For_Backend then
7857 Convert_To_Assignments (N, Typ);
7858 end if;
7859 end;
7861 -- For a root type, the tag component is added (unless compiling
7862 -- for the VMs, where tags are implicit).
7864 elsif Tagged_Type_Expansion then
7865 declare
7866 Tag_Name : constant Node_Id :=
7867 New_Occurrence_Of
7868 (First_Tag_Component (Typ), Loc);
7869 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7870 Conv_Node : constant Node_Id :=
7871 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7873 begin
7874 Set_Etype (Conv_Node, Typ_Tag);
7875 Prepend_To (Component_Associations (N),
7876 Make_Component_Association (Loc,
7877 Choices => New_List (Tag_Name),
7878 Expression => Conv_Node));
7879 end;
7880 end if;
7881 end if;
7882 end Build_Back_End_Aggregate;
7884 ----------------------------------------
7885 -- Compile_Time_Known_Composite_Value --
7886 ----------------------------------------
7888 function Compile_Time_Known_Composite_Value
7889 (N : Node_Id) return Boolean
7891 begin
7892 -- If we have an entity name, then see if it is the name of a
7893 -- constant and if so, test the corresponding constant value.
7895 if Is_Entity_Name (N) then
7896 declare
7897 E : constant Entity_Id := Entity (N);
7898 V : Node_Id;
7899 begin
7900 if Ekind (E) /= E_Constant then
7901 return False;
7902 else
7903 V := Constant_Value (E);
7904 return Present (V)
7905 and then Compile_Time_Known_Composite_Value (V);
7906 end if;
7907 end;
7909 -- We have a value, see if it is compile time known
7911 else
7912 if Nkind (N) = N_Aggregate then
7913 return Compile_Time_Known_Aggregate (N);
7914 end if;
7916 -- All other types of values are not known at compile time
7918 return False;
7919 end if;
7921 end Compile_Time_Known_Composite_Value;
7923 ------------------------------
7924 -- Component_OK_For_Backend --
7925 ------------------------------
7927 function Component_OK_For_Backend return Boolean is
7928 C : Node_Id;
7929 Expr_Q : Node_Id;
7931 begin
7932 C := First (Comps);
7933 while Present (C) loop
7935 -- If the component has box initialization, expansion is needed
7936 -- and component is not ready for backend.
7938 if Box_Present (C) then
7939 return False;
7940 end if;
7942 Expr_Q := Unqualify (Expression (C));
7944 -- Return False for array components whose bounds raise
7945 -- constraint error.
7947 declare
7948 Comp : constant Entity_Id := First (Choices (C));
7949 Indx : Node_Id;
7951 begin
7952 if Present (Etype (Comp))
7953 and then Is_Array_Type (Etype (Comp))
7954 then
7955 Indx := First_Index (Etype (Comp));
7956 while Present (Indx) loop
7957 if Nkind (Type_Low_Bound (Etype (Indx))) =
7958 N_Raise_Constraint_Error
7959 or else Nkind (Type_High_Bound (Etype (Indx))) =
7960 N_Raise_Constraint_Error
7961 then
7962 return False;
7963 end if;
7965 Next_Index (Indx);
7966 end loop;
7967 end if;
7968 end;
7970 -- Return False if the aggregate has any associations for tagged
7971 -- components that may require tag adjustment.
7973 -- These are cases where the source expression may have a tag that
7974 -- could differ from the component tag (e.g., can occur for type
7975 -- conversions and formal parameters). (Tag adjustment not needed
7976 -- if Tagged_Type_Expansion because object tags are implicit in
7977 -- the machine.)
7979 if Is_Tagged_Type (Etype (Expr_Q))
7980 and then
7981 (Nkind (Expr_Q) = N_Type_Conversion
7982 or else
7983 (Is_Entity_Name (Expr_Q)
7984 and then Is_Formal (Entity (Expr_Q))))
7985 and then Tagged_Type_Expansion
7986 then
7987 Static_Components := False;
7988 return False;
7990 elsif Is_Delayed_Aggregate (Expr_Q) then
7991 Static_Components := False;
7992 return False;
7994 elsif Nkind (Expr_Q) = N_Quantified_Expression then
7995 Static_Components := False;
7996 return False;
7998 elsif Possible_Bit_Aligned_Component (Expr_Q) then
7999 Static_Components := False;
8000 return False;
8002 elsif Modify_Tree_For_C
8003 and then Nkind (C) = N_Component_Association
8004 and then Has_Per_Object_Constraint (Choices (C))
8005 then
8006 Static_Components := False;
8007 return False;
8009 elsif Modify_Tree_For_C
8010 and then Nkind (Expr_Q) = N_Identifier
8011 and then Is_Array_Type (Etype (Expr_Q))
8012 then
8013 Static_Components := False;
8014 return False;
8016 elsif Modify_Tree_For_C
8017 and then Nkind (Expr_Q) = N_Type_Conversion
8018 and then Is_Array_Type (Etype (Expr_Q))
8019 then
8020 Static_Components := False;
8021 return False;
8022 end if;
8024 if Is_Elementary_Type (Etype (Expr_Q)) then
8025 if not Compile_Time_Known_Value (Expr_Q) then
8026 Static_Components := False;
8027 end if;
8029 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
8030 Static_Components := False;
8032 if Is_Private_Type (Etype (Expr_Q))
8033 and then Has_Discriminants (Etype (Expr_Q))
8034 then
8035 return False;
8036 end if;
8037 end if;
8039 Next (C);
8040 end loop;
8042 return True;
8043 end Component_OK_For_Backend;
8045 -------------------------------
8046 -- Has_Per_Object_Constraint --
8047 -------------------------------
8049 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
8050 N : Node_Id := First (L);
8051 begin
8052 while Present (N) loop
8053 if Is_Entity_Name (N)
8054 and then Present (Entity (N))
8055 and then Has_Per_Object_Constraint (Entity (N))
8056 then
8057 return True;
8058 end if;
8060 Next (N);
8061 end loop;
8063 return False;
8064 end Has_Per_Object_Constraint;
8066 -----------------------------------
8067 -- Has_Visible_Private_Ancestor --
8068 -----------------------------------
8070 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
8071 R : constant Entity_Id := Root_Type (Id);
8072 T1 : Entity_Id := Id;
8074 begin
8075 loop
8076 if Is_Private_Type (T1) then
8077 return True;
8079 elsif T1 = R then
8080 return False;
8082 else
8083 T1 := Etype (T1);
8084 end if;
8085 end loop;
8086 end Has_Visible_Private_Ancestor;
8088 -------------------------
8089 -- Top_Level_Aggregate --
8090 -------------------------
8092 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
8093 Aggr : Node_Id;
8095 begin
8096 Aggr := N;
8097 while Present (Parent (Aggr))
8098 and then Nkind (Parent (Aggr)) in
8099 N_Aggregate | N_Component_Association
8100 loop
8101 Aggr := Parent (Aggr);
8102 end loop;
8104 return Aggr;
8105 end Top_Level_Aggregate;
8107 -- Local variables
8109 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
8111 -- Start of processing for Expand_Record_Aggregate
8113 begin
8114 -- No special management required for aggregates used to initialize
8115 -- statically allocated dispatch tables
8117 if Is_Static_Dispatch_Table_Aggregate (N) then
8118 return;
8120 -- Case pattern aggregates need to remain as aggregates
8122 elsif Is_Case_Choice_Pattern (N) then
8123 return;
8124 end if;
8126 -- If the pragma Aggregate_Individually_Assign is set, always convert to
8127 -- assignments.
8129 if Aggregate_Individually_Assign then
8130 Convert_To_Assignments (N, Typ);
8132 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
8133 -- are build-in-place function calls. The assignments will each turn
8134 -- into a build-in-place function call. If components are all static,
8135 -- we can pass the aggregate to the back end regardless of limitedness.
8137 -- Extension aggregates, aggregates in extended return statements, and
8138 -- aggregates for C++ imported types must be expanded.
8140 elsif Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
8141 if Nkind (Parent (N)) not in
8142 N_Component_Association | N_Object_Declaration
8143 then
8144 Convert_To_Assignments (N, Typ);
8146 elsif Nkind (N) = N_Extension_Aggregate
8147 or else Convention (Typ) = Convention_CPP
8148 then
8149 Convert_To_Assignments (N, Typ);
8151 elsif not Size_Known_At_Compile_Time (Typ)
8152 or else not Component_OK_For_Backend
8153 or else not Static_Components
8154 then
8155 Convert_To_Assignments (N, Typ);
8157 -- In all other cases, build a proper aggregate to be handled by
8158 -- the back-end.
8160 else
8161 Build_Back_End_Aggregate;
8162 end if;
8164 -- Gigi doesn't properly handle temporaries of variable size so we
8165 -- generate it in the front-end
8167 elsif not Size_Known_At_Compile_Time (Typ)
8168 and then Tagged_Type_Expansion
8169 then
8170 Convert_To_Assignments (N, Typ);
8172 -- An aggregate used to initialize a controlled object must be turned
8173 -- into component assignments as the components themselves may require
8174 -- finalization actions such as adjustment.
8176 elsif Needs_Finalization (Typ) then
8177 Convert_To_Assignments (N, Typ);
8179 -- Ada 2005 (AI-287): In case of default initialized components we
8180 -- convert the aggregate into assignments.
8182 elsif Has_Default_Init_Comps (N) then
8183 Convert_To_Assignments (N, Typ);
8185 -- Check components
8187 elsif not Component_OK_For_Backend then
8188 Convert_To_Assignments (N, Typ);
8190 -- If an ancestor is private, some components are not inherited and we
8191 -- cannot expand into a record aggregate.
8193 elsif Has_Visible_Private_Ancestor (Typ) then
8194 Convert_To_Assignments (N, Typ);
8196 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
8197 -- is not able to handle the aggregate for Late_Request.
8199 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
8200 Convert_To_Assignments (N, Typ);
8202 -- If the tagged types covers interface types we need to initialize all
8203 -- hidden components containing pointers to secondary dispatch tables.
8205 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
8206 Convert_To_Assignments (N, Typ);
8208 -- If some components are mutable, the size of the aggregate component
8209 -- may be distinct from the default size of the type component, so
8210 -- we need to expand to insure that the back-end copies the proper
8211 -- size of the data. However, if the aggregate is the initial value of
8212 -- a constant, the target is immutable and might be built statically
8213 -- if components are appropriate.
8215 elsif Has_Mutable_Components (Typ)
8216 and then
8217 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
8218 or else not Constant_Present (Parent (Top_Level_Aggr))
8219 or else not Static_Components)
8220 then
8221 Convert_To_Assignments (N, Typ);
8223 -- If the type involved has bit aligned components, then we are not sure
8224 -- that the back end can handle this case correctly.
8226 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
8227 Convert_To_Assignments (N, Typ);
8229 -- When generating C, only generate an aggregate when declaring objects
8230 -- since C does not support aggregates in e.g. assignment statements.
8232 elsif Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
8233 Convert_To_Assignments (N, Typ);
8235 -- In all other cases, build a proper aggregate to be handled by gigi
8237 else
8238 Build_Back_End_Aggregate;
8239 end if;
8240 end Expand_Record_Aggregate;
8242 ---------------------
8243 -- Get_Base_Object --
8244 ---------------------
8246 function Get_Base_Object (N : Node_Id) return Entity_Id is
8247 R : Node_Id;
8249 begin
8250 R := Get_Referenced_Object (N);
8252 while Nkind (R) in N_Indexed_Component | N_Selected_Component | N_Slice
8253 loop
8254 R := Get_Referenced_Object (Prefix (R));
8255 end loop;
8257 if Is_Entity_Name (R) and then Is_Object (Entity (R)) then
8258 return Entity (R);
8259 else
8260 return Empty;
8261 end if;
8262 end Get_Base_Object;
8264 ----------------------------
8265 -- Has_Default_Init_Comps --
8266 ----------------------------
8268 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
8269 Assoc : Node_Id;
8270 Expr : Node_Id;
8271 -- Component association and expression, respectively
8273 begin
8274 pragma Assert (Nkind (N) in N_Aggregate | N_Extension_Aggregate);
8276 if Has_Self_Reference (N) then
8277 return True;
8278 end if;
8280 Assoc := First (Component_Associations (N));
8281 while Present (Assoc) loop
8282 -- Each component association has either a box or an expression
8284 pragma Assert (Box_Present (Assoc) xor Present (Expression (Assoc)));
8286 -- Check if any direct component has default initialized components
8288 if Box_Present (Assoc) then
8289 return True;
8291 -- Recursive call in case of aggregate expression
8293 else
8294 Expr := Expression (Assoc);
8296 if Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
8297 and then Has_Default_Init_Comps (Expr)
8298 then
8299 return True;
8300 end if;
8301 end if;
8303 Next (Assoc);
8304 end loop;
8306 return False;
8307 end Has_Default_Init_Comps;
8309 --------------------------
8310 -- Initialize_Component --
8311 --------------------------
8313 procedure Initialize_Component
8314 (N : Node_Id;
8315 Comp : Node_Id;
8316 Comp_Typ : Node_Id;
8317 Init_Expr : Node_Id;
8318 Stmts : List_Id)
8320 Exceptions_OK : constant Boolean :=
8321 not Restriction_Active (No_Exception_Propagation);
8322 Finalization_OK : constant Boolean :=
8323 Present (Comp_Typ)
8324 and then Needs_Finalization (Comp_Typ);
8325 Loc : constant Source_Ptr := Sloc (N);
8327 Blk_Stmts : List_Id;
8328 Init_Stmt : Node_Id;
8330 begin
8331 pragma Assert (Nkind (Init_Expr) in N_Subexpr);
8333 -- Protect the initialization statements from aborts. Generate:
8335 -- Abort_Defer;
8337 if Finalization_OK and Abort_Allowed then
8338 if Exceptions_OK then
8339 Blk_Stmts := New_List;
8340 else
8341 Blk_Stmts := Stmts;
8342 end if;
8344 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8346 -- Otherwise aborts are not allowed. All generated code is added
8347 -- directly to the input list.
8349 else
8350 Blk_Stmts := Stmts;
8351 end if;
8353 -- Initialize the component. Generate:
8355 -- Comp := Init_Expr;
8357 -- Note that the initialization expression is not duplicated because
8358 -- either only a single component may be initialized by it (record)
8359 -- or it has already been duplicated if need be (array).
8361 Init_Stmt :=
8362 Make_OK_Assignment_Statement (Loc,
8363 Name => New_Copy_Tree (Comp),
8364 Expression => Relocate_Node (Init_Expr));
8366 Append_To (Blk_Stmts, Init_Stmt);
8368 -- Arrange for the component to be adjusted if need be (the call will be
8369 -- generated by Make_Tag_Ctrl_Assignment). But, in the case of an array
8370 -- aggregate, controlled subaggregates are not considered because each
8371 -- of their individual elements will receive an adjustment of its own.
8373 if Finalization_OK
8374 and then not Is_Limited_View (Comp_Typ)
8375 and then not
8376 (Is_Array_Type (Etype (N))
8377 and then Is_Array_Type (Comp_Typ)
8378 and then Needs_Finalization (Component_Type (Comp_Typ))
8379 and then Nkind (Unqualify (Init_Expr)) = N_Aggregate)
8380 then
8381 Set_No_Finalize_Actions (Init_Stmt);
8383 -- Or else, only adjust the tag due to a possible view conversion
8385 else
8386 Set_No_Ctrl_Actions (Init_Stmt);
8388 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
8389 Append_To (Blk_Stmts,
8390 Make_Tag_Assignment_From_Type
8391 (Loc, New_Copy_Tree (Comp), Underlying_Type (Comp_Typ)));
8392 end if;
8393 end if;
8395 -- Complete the protection of the initialization statements
8397 if Finalization_OK and Abort_Allowed then
8399 -- Wrap the initialization statements in a block to catch a
8400 -- potential exception. Generate:
8402 -- begin
8403 -- Abort_Defer;
8404 -- Comp := Init_Expr;
8405 -- Comp._tag := Full_TypP;
8406 -- [Deep_]Adjust (Comp);
8407 -- at end
8408 -- Abort_Undefer_Direct;
8409 -- end;
8411 if Exceptions_OK then
8412 Append_To (Stmts,
8413 Build_Abort_Undefer_Block (Loc,
8414 Stmts => Blk_Stmts,
8415 Context => N));
8417 -- Otherwise exceptions are not propagated. Generate:
8419 -- Abort_Defer;
8420 -- Comp := Init_Expr;
8421 -- Comp._tag := Full_TypP;
8422 -- [Deep_]Adjust (Comp);
8423 -- Abort_Undefer;
8425 else
8426 Append_To (Blk_Stmts,
8427 Build_Runtime_Call (Loc, RE_Abort_Undefer));
8428 end if;
8429 end if;
8430 end Initialize_Component;
8432 ----------------------------------------
8433 -- Is_Build_In_Place_Aggregate_Return --
8434 ----------------------------------------
8436 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean is
8437 P : Node_Id := Parent (N);
8439 begin
8440 while Nkind (P) = N_Qualified_Expression loop
8441 P := Parent (P);
8442 end loop;
8444 if Nkind (P) = N_Simple_Return_Statement then
8445 null;
8447 elsif Nkind (Parent (P)) = N_Extended_Return_Statement then
8448 P := Parent (P);
8450 else
8451 return False;
8452 end if;
8454 return
8455 Is_Build_In_Place_Function
8456 (Return_Applies_To (Return_Statement_Entity (P)));
8457 end Is_Build_In_Place_Aggregate_Return;
8459 --------------------------
8460 -- Is_Delayed_Aggregate --
8461 --------------------------
8463 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
8464 Unqual_N : constant Node_Id := Unqualify (N);
8466 begin
8467 return Nkind (Unqual_N) in N_Aggregate | N_Extension_Aggregate
8468 and then Expansion_Delayed (Unqual_N);
8469 end Is_Delayed_Aggregate;
8471 --------------------------------
8472 -- Is_CCG_Supported_Aggregate --
8473 --------------------------------
8475 function Is_CCG_Supported_Aggregate
8476 (N : Node_Id) return Boolean
8478 P : Node_Id := Parent (N);
8480 begin
8481 -- Aggregates are not supported for nonstandard rep clauses, since they
8482 -- may lead to extra padding fields in CCG.
8484 if Is_Record_Type (Etype (N))
8485 and then Has_Non_Standard_Rep (Etype (N))
8486 then
8487 return False;
8488 end if;
8490 while Present (P) and then Nkind (P) = N_Aggregate loop
8491 P := Parent (P);
8492 end loop;
8494 -- Check cases where aggregates are supported by the CCG backend
8496 if Nkind (P) = N_Object_Declaration then
8497 declare
8498 P_Typ : constant Entity_Id := Etype (Defining_Identifier (P));
8500 begin
8501 if Is_Record_Type (P_Typ) then
8502 return True;
8503 else
8504 return Compile_Time_Known_Bounds (P_Typ);
8505 end if;
8506 end;
8508 elsif Nkind (P) = N_Qualified_Expression then
8509 if Nkind (Parent (P)) = N_Object_Declaration then
8510 declare
8511 P_Typ : constant Entity_Id :=
8512 Etype (Defining_Identifier (Parent (P)));
8513 begin
8514 if Is_Record_Type (P_Typ) then
8515 return True;
8516 else
8517 return Compile_Time_Known_Bounds (P_Typ);
8518 end if;
8519 end;
8521 elsif Nkind (Parent (P)) = N_Allocator then
8522 return True;
8523 end if;
8524 end if;
8526 return False;
8527 end Is_CCG_Supported_Aggregate;
8529 ----------------------------------------
8530 -- Is_Static_Dispatch_Table_Aggregate --
8531 ----------------------------------------
8533 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
8534 Typ : constant Entity_Id := Base_Type (Etype (N));
8536 begin
8537 return Building_Static_Dispatch_Tables
8538 and then Tagged_Type_Expansion
8540 -- Avoid circularity when rebuilding the compiler
8542 and then not Is_RTU (Cunit_Entity (Get_Source_Unit (N)), Ada_Tags)
8543 and then (Is_RTE (Typ, RE_Dispatch_Table_Wrapper)
8544 or else
8545 Is_RTE (Typ, RE_Address_Array)
8546 or else
8547 Is_RTE (Typ, RE_Type_Specific_Data)
8548 or else
8549 Is_RTE (Typ, RE_Tag_Table)
8550 or else
8551 Is_RTE (Typ, RE_Object_Specific_Data)
8552 or else
8553 Is_RTE (Typ, RE_Interface_Data)
8554 or else
8555 Is_RTE (Typ, RE_Interfaces_Array)
8556 or else
8557 Is_RTE (Typ, RE_Interface_Data_Element));
8558 end Is_Static_Dispatch_Table_Aggregate;
8560 -----------------------------
8561 -- Is_Two_Dim_Packed_Array --
8562 -----------------------------
8564 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
8565 C : constant Uint := Component_Size (Typ);
8567 begin
8568 return Number_Dimensions (Typ) = 2
8569 and then Is_Bit_Packed_Array (Typ)
8570 and then Is_Scalar_Type (Component_Type (Typ))
8571 and then C in Uint_1 | Uint_2 | Uint_4; -- False if No_Uint
8572 end Is_Two_Dim_Packed_Array;
8574 --------------------
8575 -- Late_Expansion --
8576 --------------------
8578 function Late_Expansion
8579 (N : Node_Id;
8580 Typ : Entity_Id;
8581 Target : Node_Id) return List_Id
8583 Aggr_Code : List_Id;
8584 New_Aggr : Node_Id;
8586 begin
8587 if Is_Array_Type (Typ) then
8588 -- If the assignment can be done directly by the back end, then
8589 -- reset Set_Expansion_Delayed and do not expand further.
8591 if not CodePeer_Mode
8592 and then not Modify_Tree_For_C
8593 and then not Possible_Bit_Aligned_Component (Target)
8594 and then not Is_Possibly_Unaligned_Slice (Target)
8595 and then Aggr_Assignment_OK_For_Backend (N)
8596 then
8597 New_Aggr := New_Copy_Tree (N);
8598 Set_Expansion_Delayed (New_Aggr, False);
8600 Aggr_Code :=
8601 New_List (
8602 Make_OK_Assignment_Statement (Sloc (New_Aggr),
8603 Name => Target,
8604 Expression => New_Aggr));
8606 -- Or else, generate component assignments to it
8608 else
8609 Aggr_Code :=
8610 Build_Array_Aggr_Code
8611 (N => N,
8612 Ctype => Component_Type (Typ),
8613 Index => First_Index (Typ),
8614 Into => Target,
8615 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
8616 Indexes => No_List);
8617 end if;
8619 -- Directly or indirectly (e.g. access protected procedure) a record
8621 else
8622 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
8623 end if;
8625 -- Save the last assignment statement associated with the aggregate
8626 -- when building a controlled object. This reference is utilized by
8627 -- the finalization machinery when marking an object as successfully
8628 -- initialized.
8630 if Needs_Finalization (Typ)
8631 and then Is_Entity_Name (Target)
8632 and then Present (Entity (Target))
8633 and then Ekind (Entity (Target)) in E_Constant | E_Variable
8634 then
8635 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
8636 end if;
8638 return Aggr_Code;
8639 end Late_Expansion;
8641 ----------------------------------
8642 -- Make_OK_Assignment_Statement --
8643 ----------------------------------
8645 function Make_OK_Assignment_Statement
8646 (Sloc : Source_Ptr;
8647 Name : Node_Id;
8648 Expression : Node_Id) return Node_Id
8650 begin
8651 Set_Assignment_OK (Name);
8652 return Make_Assignment_Statement (Sloc, Name, Expression);
8653 end Make_OK_Assignment_Statement;
8655 ------------------------
8656 -- Max_Aggregate_Size --
8657 ------------------------
8659 function Max_Aggregate_Size
8660 (N : Node_Id;
8661 Default_Size : Nat := 5000) return Nat
8663 function Use_Small_Size (N : Node_Id) return Boolean;
8664 -- True if we should return a very small size, which means large
8665 -- aggregates will be implemented as a loop when possible (potentially
8666 -- transformed to memset calls).
8668 function Aggr_Context (N : Node_Id) return Node_Id;
8669 -- Return the context in which the aggregate appears, not counting
8670 -- qualified expressions and similar.
8672 ------------------
8673 -- Aggr_Context --
8674 ------------------
8676 function Aggr_Context (N : Node_Id) return Node_Id is
8677 Result : Node_Id := Parent (N);
8678 begin
8679 if Nkind (Result) in N_Qualified_Expression
8680 | N_Type_Conversion
8681 | N_Unchecked_Type_Conversion
8682 | N_If_Expression
8683 | N_Case_Expression
8684 | N_Component_Association
8685 | N_Aggregate
8686 then
8687 Result := Aggr_Context (Result);
8688 end if;
8690 return Result;
8691 end Aggr_Context;
8693 --------------------
8694 -- Use_Small_Size --
8695 --------------------
8697 function Use_Small_Size (N : Node_Id) return Boolean is
8698 C : constant Node_Id := Aggr_Context (N);
8699 -- The decision depends on the context in which the aggregate occurs,
8700 -- and for variable declarations, whether we are nested inside a
8701 -- subprogram.
8702 begin
8703 case Nkind (C) is
8704 -- True for assignment statements and similar
8706 when N_Assignment_Statement
8707 | N_Simple_Return_Statement
8708 | N_Allocator
8709 | N_Attribute_Reference
8711 return True;
8713 -- True for nested variable declarations. False for library level
8714 -- variables, and for constants (whether or not nested).
8716 when N_Object_Declaration =>
8717 return not Constant_Present (C)
8718 and then Is_Subprogram (Current_Scope);
8720 -- False for all other contexts
8722 when others =>
8723 return False;
8724 end case;
8725 end Use_Small_Size;
8727 -- Local variables
8729 Typ : constant Entity_Id := Etype (N);
8731 -- Start of processing for Max_Aggregate_Size
8733 begin
8734 -- We use a small limit in CodePeer mode where we favor loops instead of
8735 -- thousands of single assignments (from large aggregates).
8737 -- We also increase the limit to 2**24 (about 16 million) if
8738 -- Restrictions (No_Elaboration_Code) or Restrictions
8739 -- (No_Implicit_Loops) is specified, since in either case we are at risk
8740 -- of declaring the program illegal because of this limit. We also
8741 -- increase the limit when Static_Elaboration_Desired, given that this
8742 -- means that objects are intended to be placed in data memory.
8744 -- Same if the aggregate is for a packed two-dimensional array, because
8745 -- if components are static it is much more efficient to construct a
8746 -- one-dimensional equivalent array with static components.
8748 if CodePeer_Mode then
8749 return 100;
8750 elsif Restriction_Active (No_Elaboration_Code)
8751 or else Restriction_Active (No_Implicit_Loops)
8752 or else Is_Two_Dim_Packed_Array (Typ)
8753 or else (Ekind (Current_Scope) = E_Package
8754 and then Static_Elaboration_Desired (Current_Scope))
8755 then
8756 return 2 ** 24;
8757 elsif Use_Small_Size (N) then
8758 return 64;
8759 end if;
8761 return Default_Size;
8762 end Max_Aggregate_Size;
8764 -----------------------
8765 -- Number_Of_Choices --
8766 -----------------------
8768 function Number_Of_Choices (N : Node_Id) return Nat is
8769 Assoc : Node_Id;
8770 Choice : Node_Id;
8772 Nb_Choices : Nat := 0;
8774 begin
8775 if Present (Expressions (N)) then
8776 return 0;
8777 end if;
8779 Assoc := First (Component_Associations (N));
8780 while Present (Assoc) loop
8781 Choice := First (Choice_List (Assoc));
8782 while Present (Choice) loop
8783 if Nkind (Choice) /= N_Others_Choice then
8784 Nb_Choices := Nb_Choices + 1;
8785 end if;
8787 Next (Choice);
8788 end loop;
8790 Next (Assoc);
8791 end loop;
8793 return Nb_Choices;
8794 end Number_Of_Choices;
8796 ------------------------------------
8797 -- Packed_Array_Aggregate_Handled --
8798 ------------------------------------
8800 -- The current version of this procedure will handle at compile time
8801 -- any array aggregate that meets these conditions:
8803 -- One and two dimensional, bit packed
8804 -- Underlying packed type is modular type
8805 -- Bounds are within 32-bit Int range
8806 -- All bounds and values are static
8808 -- Note: for now, in the 2-D case, we only handle component sizes of
8809 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
8811 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
8812 Loc : constant Source_Ptr := Sloc (N);
8813 Typ : constant Entity_Id := Etype (N);
8814 Ctyp : constant Entity_Id := Component_Type (Typ);
8816 Not_Handled : exception;
8817 -- Exception raised if this aggregate cannot be handled
8819 begin
8820 -- Handle one- or two dimensional bit packed array
8822 if not Is_Bit_Packed_Array (Typ)
8823 or else Number_Dimensions (Typ) > 2
8824 then
8825 return False;
8826 end if;
8828 -- If two-dimensional, check whether it can be folded, and transformed
8829 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
8830 -- the original type.
8832 if Number_Dimensions (Typ) = 2 then
8833 return Two_Dim_Packed_Array_Handled (N);
8834 end if;
8836 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
8837 return False;
8838 end if;
8840 if not Is_Scalar_Type (Ctyp) then
8841 return False;
8842 end if;
8844 declare
8845 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
8847 function Get_Component_Val (N : Node_Id) return Uint;
8848 -- Given a expression value N of the component type Ctyp, returns a
8849 -- value of Csiz (component size) bits representing this value. If
8850 -- the value is nonstatic or any other reason exists why the value
8851 -- cannot be returned, then Not_Handled is raised.
8853 -----------------------
8854 -- Get_Component_Val --
8855 -----------------------
8857 function Get_Component_Val (N : Node_Id) return Uint is
8858 Val : Uint;
8860 begin
8861 -- We have to analyze the expression here before doing any further
8862 -- processing here. The analysis of such expressions is deferred
8863 -- till expansion to prevent some problems of premature analysis.
8865 Analyze_And_Resolve (N, Ctyp);
8867 -- Must have a compile time value. String literals have to be
8868 -- converted into temporaries as well, because they cannot easily
8869 -- be converted into their bit representation.
8871 if not Compile_Time_Known_Value (N)
8872 or else Nkind (N) = N_String_Literal
8873 then
8874 raise Not_Handled;
8875 end if;
8877 Val := Expr_Rep_Value (N);
8879 -- Adjust for bias, and strip proper number of bits
8881 if Has_Biased_Representation (Ctyp) then
8882 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
8883 end if;
8885 return Val mod Uint_2 ** Csiz;
8886 end Get_Component_Val;
8888 Bounds : constant Range_Nodes := Get_Index_Bounds (First_Index (Typ));
8890 -- Here we know we have a one dimensional bit packed array
8892 begin
8893 -- Cannot do anything if bounds are dynamic
8895 if not (Compile_Time_Known_Value (Bounds.First)
8896 and then
8897 Compile_Time_Known_Value (Bounds.Last))
8898 then
8899 return False;
8900 end if;
8902 declare
8903 Bounds_Vals : Range_Values;
8904 -- Compile-time known values of bounds
8905 begin
8906 -- Or are silly out of range of int bounds
8908 Bounds_Vals.First := Expr_Value (Bounds.First);
8909 Bounds_Vals.Last := Expr_Value (Bounds.Last);
8911 if not UI_Is_In_Int_Range (Bounds_Vals.First)
8912 or else
8913 not UI_Is_In_Int_Range (Bounds_Vals.Last)
8914 then
8915 return False;
8916 end if;
8918 -- At this stage we have a suitable aggregate for handling at
8919 -- compile time. The only remaining checks are that the values of
8920 -- expressions in the aggregate are compile-time known (checks are
8921 -- performed by Get_Component_Val), and that any subtypes or
8922 -- ranges are statically known.
8924 -- If the aggregate is not fully positional at this stage, then
8925 -- convert it to positional form. Either this will fail, in which
8926 -- case we can do nothing, or it will succeed, in which case we
8927 -- have succeeded in handling the aggregate and transforming it
8928 -- into a modular value, or it will stay an aggregate, in which
8929 -- case we have failed to create a packed value for it.
8931 if Present (Component_Associations (N)) then
8932 Convert_To_Positional (N, Handle_Bit_Packed => True);
8933 return Nkind (N) /= N_Aggregate;
8934 end if;
8936 -- Otherwise we are all positional, so convert to proper value
8938 declare
8939 Len : constant Nat :=
8940 Int'Max (0, UI_To_Int (Bounds_Vals.Last) -
8941 UI_To_Int (Bounds_Vals.First) + 1);
8942 -- The length of the array (number of elements)
8944 Aggregate_Val : Uint;
8945 -- Value of aggregate. The value is set in the low order bits
8946 -- of this value. For the little-endian case, the values are
8947 -- stored from low-order to high-order and for the big-endian
8948 -- case the values are stored from high order to low order.
8949 -- Note that gigi will take care of the conversions to left
8950 -- justify the value in the big endian case (because of left
8951 -- justified modular type processing), so we do not have to
8952 -- worry about that here.
8954 Lit : Node_Id;
8955 -- Integer literal for resulting constructed value
8957 Shift : Nat;
8958 -- Shift count from low order for next value
8960 Incr : Int;
8961 -- Shift increment for loop
8963 Expr : Node_Id;
8964 -- Next expression from positional parameters of aggregate
8966 Left_Justified : Boolean;
8967 -- Set True if we are filling the high order bits of the target
8968 -- value (i.e. the value is left justified).
8970 begin
8971 -- For little endian, we fill up the low order bits of the
8972 -- target value. For big endian we fill up the high order bits
8973 -- of the target value (which is a left justified modular
8974 -- value).
8976 Left_Justified := Bytes_Big_Endian;
8978 -- Switch justification if using -gnatd8
8980 if Debug_Flag_8 then
8981 Left_Justified := not Left_Justified;
8982 end if;
8984 -- Switch justfification if reverse storage order
8986 if Reverse_Storage_Order (Base_Type (Typ)) then
8987 Left_Justified := not Left_Justified;
8988 end if;
8990 if Left_Justified then
8991 Shift := Csiz * (Len - 1);
8992 Incr := -Csiz;
8993 else
8994 Shift := 0;
8995 Incr := +Csiz;
8996 end if;
8998 -- Loop to set the values
9000 if Len = 0 then
9001 Aggregate_Val := Uint_0;
9002 else
9003 Expr := First (Expressions (N));
9004 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
9006 for J in 2 .. Len loop
9007 Shift := Shift + Incr;
9008 Next (Expr);
9009 Aggregate_Val :=
9010 Aggregate_Val +
9011 Get_Component_Val (Expr) * Uint_2 ** Shift;
9012 end loop;
9013 end if;
9015 -- Now we can rewrite with the proper value
9017 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
9018 Set_Print_In_Hex (Lit);
9020 -- Construct the expression using this literal. Note that it
9021 -- is important to qualify the literal with its proper modular
9022 -- type since universal integer does not have the required
9023 -- range and also this is a left justified modular type,
9024 -- which is important in the big-endian case.
9026 Rewrite (N,
9027 Unchecked_Convert_To (Typ,
9028 Make_Qualified_Expression (Loc,
9029 Subtype_Mark =>
9030 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
9031 Expression => Lit)));
9033 Analyze_And_Resolve (N, Typ);
9034 return True;
9035 end;
9036 end;
9037 end;
9039 exception
9040 when Not_Handled =>
9041 return False;
9042 end Packed_Array_Aggregate_Handled;
9044 ----------------------------
9045 -- Has_Mutable_Components --
9046 ----------------------------
9048 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
9049 Comp : Entity_Id;
9050 Ctyp : Entity_Id;
9052 begin
9053 Comp := First_Component (Typ);
9054 while Present (Comp) loop
9055 Ctyp := Underlying_Type (Etype (Comp));
9056 if Is_Record_Type (Ctyp)
9057 and then Has_Discriminants (Ctyp)
9058 and then not Is_Constrained (Ctyp)
9059 then
9060 return True;
9061 end if;
9063 Next_Component (Comp);
9064 end loop;
9066 return False;
9067 end Has_Mutable_Components;
9069 ------------------------------
9070 -- Initialize_Discriminants --
9071 ------------------------------
9073 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
9074 Loc : constant Source_Ptr := Sloc (N);
9075 Bas : constant Entity_Id := Base_Type (Typ);
9076 Par : constant Entity_Id := Etype (Bas);
9077 Decl : constant Node_Id := Parent (Par);
9078 Ref : Node_Id;
9080 begin
9081 if Is_Tagged_Type (Bas)
9082 and then Is_Derived_Type (Bas)
9083 and then Has_Discriminants (Par)
9084 and then Has_Discriminants (Bas)
9085 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
9086 and then Nkind (Decl) = N_Full_Type_Declaration
9087 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
9088 and then
9089 Present (Variant_Part (Component_List (Type_Definition (Decl))))
9090 and then Nkind (N) /= N_Extension_Aggregate
9091 then
9093 -- Call init proc to set discriminants.
9094 -- There should eventually be a special procedure for this ???
9096 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
9097 Insert_Actions_After (N,
9098 Build_Initialization_Call (Sloc (N), Ref, Typ));
9099 end if;
9100 end Initialize_Discriminants;
9102 ----------------
9103 -- Must_Slide --
9104 ----------------
9106 function Must_Slide
9107 (Aggr : Node_Id;
9108 Obj_Type : Entity_Id;
9109 Typ : Entity_Id) return Boolean
9111 begin
9112 -- No sliding if the type of the object is not established yet, if it is
9113 -- an unconstrained type whose actual subtype comes from the aggregate,
9114 -- or if the two types are identical. If the aggregate contains only
9115 -- an Others_Clause it gets its type from the context and no sliding
9116 -- is involved either.
9118 if not Is_Array_Type (Obj_Type) then
9119 return False;
9121 elsif not Is_Constrained (Obj_Type) then
9122 return False;
9124 elsif Typ = Obj_Type then
9125 return False;
9127 elsif Is_Others_Aggregate (Aggr) then
9128 return False;
9130 else
9131 -- Sliding can only occur along the first dimension
9132 -- If any the bounds of non-static sliding is required
9133 -- to force potential range checks.
9135 declare
9136 Bounds1 : constant Range_Nodes :=
9137 Get_Index_Bounds (First_Index (Typ));
9138 Bounds2 : constant Range_Nodes :=
9139 Get_Index_Bounds (First_Index (Obj_Type));
9141 begin
9142 if not Is_OK_Static_Expression (Bounds1.First) or else
9143 not Is_OK_Static_Expression (Bounds2.First) or else
9144 not Is_OK_Static_Expression (Bounds1.Last) or else
9145 not Is_OK_Static_Expression (Bounds2.Last)
9146 then
9147 return True;
9149 else
9150 return Expr_Value (Bounds1.First) /= Expr_Value (Bounds2.First)
9151 or else
9152 Expr_Value (Bounds1.Last) /= Expr_Value (Bounds2.Last);
9153 end if;
9154 end;
9155 end if;
9156 end Must_Slide;
9158 ---------------------
9159 -- Sort_Case_Table --
9160 ---------------------
9162 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
9163 L : constant Int := Case_Table'First;
9164 U : constant Int := Case_Table'Last;
9165 K : Int;
9166 J : Int;
9167 T : Case_Bounds;
9169 begin
9170 K := L;
9171 while K /= U loop
9172 T := Case_Table (K + 1);
9174 J := K + 1;
9175 while J /= L
9176 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
9177 Expr_Value (T.Choice_Lo)
9178 loop
9179 Case_Table (J) := Case_Table (J - 1);
9180 J := J - 1;
9181 end loop;
9183 Case_Table (J) := T;
9184 K := K + 1;
9185 end loop;
9186 end Sort_Case_Table;
9188 ----------------------------
9189 -- Static_Array_Aggregate --
9190 ----------------------------
9192 function Static_Array_Aggregate (N : Node_Id) return Boolean is
9193 function Is_Static_Component (Nod : Node_Id) return Boolean;
9194 -- Return True if Nod has a compile-time known value and can be passed
9195 -- as is to the back-end without further expansion.
9197 ---------------------------
9198 -- Is_Static_Component --
9199 ---------------------------
9201 function Is_Static_Component (Nod : Node_Id) return Boolean is
9202 begin
9203 if Nkind (Nod) in N_Integer_Literal | N_Real_Literal then
9204 return True;
9206 elsif Is_Entity_Name (Nod)
9207 and then Present (Entity (Nod))
9208 and then Ekind (Entity (Nod)) = E_Enumeration_Literal
9209 then
9210 return True;
9212 elsif Nkind (Nod) = N_Aggregate
9213 and then Compile_Time_Known_Aggregate (Nod)
9214 then
9215 return True;
9217 else
9218 return False;
9219 end if;
9220 end Is_Static_Component;
9222 -- Local variables
9224 Bounds : constant Node_Id := Aggregate_Bounds (N);
9225 Typ : constant Entity_Id := Etype (N);
9227 Agg : Node_Id;
9228 Expr : Node_Id;
9229 Lo : Node_Id;
9230 Hi : Node_Id;
9232 -- Start of processing for Static_Array_Aggregate
9234 begin
9235 if Is_Packed (Typ) or else Has_Discriminants (Component_Type (Typ)) then
9236 return False;
9237 end if;
9239 if Present (Bounds)
9240 and then Nkind (Bounds) = N_Range
9241 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
9242 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
9243 then
9244 Lo := Low_Bound (Bounds);
9245 Hi := High_Bound (Bounds);
9247 if No (Component_Associations (N)) then
9249 -- Verify that all components are static
9251 Expr := First (Expressions (N));
9252 while Present (Expr) loop
9253 if not Is_Static_Component (Expr) then
9254 return False;
9255 end if;
9257 Next (Expr);
9258 end loop;
9260 return True;
9262 else
9263 -- We allow only a single named association, either a static
9264 -- range or an others_clause, with a static expression.
9266 Expr := First (Component_Associations (N));
9268 if Present (Expressions (N)) then
9269 return False;
9271 elsif Present (Next (Expr)) then
9272 return False;
9274 elsif Present (Next (First (Choice_List (Expr)))) then
9275 return False;
9277 else
9278 -- The aggregate is static if all components are literals,
9279 -- or else all its components are static aggregates for the
9280 -- component type. We also limit the size of a static aggregate
9281 -- to prevent runaway static expressions.
9283 if not Is_Static_Component (Expression (Expr)) then
9284 return False;
9285 end if;
9287 if not Aggr_Size_OK (N) then
9288 return False;
9289 end if;
9291 -- Create a positional aggregate with the right number of
9292 -- copies of the expression.
9294 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
9296 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
9297 loop
9298 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
9300 -- The copied expression must be analyzed and resolved.
9301 -- Besides setting the type, this ensures that static
9302 -- expressions are appropriately marked as such.
9304 Analyze_And_Resolve
9305 (Last (Expressions (Agg)), Component_Type (Typ));
9306 end loop;
9308 Set_Aggregate_Bounds (Agg, Bounds);
9309 Set_Etype (Agg, Typ);
9310 Set_Analyzed (Agg);
9311 Rewrite (N, Agg);
9312 Set_Compile_Time_Known_Aggregate (N);
9314 return True;
9315 end if;
9316 end if;
9318 else
9319 return False;
9320 end if;
9321 end Static_Array_Aggregate;
9323 ----------------------------------
9324 -- Two_Dim_Packed_Array_Handled --
9325 ----------------------------------
9327 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
9328 Loc : constant Source_Ptr := Sloc (N);
9329 Typ : constant Entity_Id := Etype (N);
9330 Ctyp : constant Entity_Id := Component_Type (Typ);
9331 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
9332 Packed_Array : constant Entity_Id :=
9333 Packed_Array_Impl_Type (Base_Type (Typ));
9335 One_Comp : Node_Id;
9336 -- Expression in original aggregate
9338 One_Dim : Node_Id;
9339 -- One-dimensional subaggregate
9341 begin
9343 -- For now, only deal with cases where an integral number of elements
9344 -- fit in a single byte. This includes the most common boolean case.
9346 if not (Comp_Size = 1 or else
9347 Comp_Size = 2 or else
9348 Comp_Size = 4)
9349 then
9350 return False;
9351 end if;
9353 Convert_To_Positional (N, Handle_Bit_Packed => True);
9355 -- Verify that all components are static
9357 if Nkind (N) = N_Aggregate
9358 and then Compile_Time_Known_Aggregate (N)
9359 then
9360 null;
9362 -- The aggregate may have been reanalyzed and converted already
9364 elsif Nkind (N) /= N_Aggregate then
9365 return True;
9367 -- If component associations remain, the aggregate is not static
9369 elsif Present (Component_Associations (N)) then
9370 return False;
9372 else
9373 One_Dim := First (Expressions (N));
9374 while Present (One_Dim) loop
9375 if Present (Component_Associations (One_Dim)) then
9376 return False;
9377 end if;
9379 One_Comp := First (Expressions (One_Dim));
9380 while Present (One_Comp) loop
9381 if not Is_OK_Static_Expression (One_Comp) then
9382 return False;
9383 end if;
9385 Next (One_Comp);
9386 end loop;
9388 Next (One_Dim);
9389 end loop;
9390 end if;
9392 -- Two-dimensional aggregate is now fully positional so pack one
9393 -- dimension to create a static one-dimensional array, and rewrite
9394 -- as an unchecked conversion to the original type.
9396 declare
9397 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
9398 -- The packed array type is a byte array
9400 Packed_Num : Nat;
9401 -- Number of components accumulated in current byte
9403 Comps : List_Id;
9404 -- Assembled list of packed values for equivalent aggregate
9406 Comp_Val : Uint;
9407 -- Integer value of component
9409 Incr : Int;
9410 -- Step size for packing
9412 Init_Shift : Int;
9413 -- Endian-dependent start position for packing
9415 Shift : Int;
9416 -- Current insertion position
9418 Val : Int;
9419 -- Component of packed array being assembled
9421 begin
9422 Comps := New_List;
9423 Val := 0;
9424 Packed_Num := 0;
9426 -- Account for endianness. See corresponding comment in
9427 -- Packed_Array_Aggregate_Handled concerning the following.
9429 if Bytes_Big_Endian
9430 xor Debug_Flag_8
9431 xor Reverse_Storage_Order (Base_Type (Typ))
9432 then
9433 Init_Shift := Byte_Size - Comp_Size;
9434 Incr := -Comp_Size;
9435 else
9436 Init_Shift := 0;
9437 Incr := +Comp_Size;
9438 end if;
9440 -- Iterate over each subaggregate
9442 Shift := Init_Shift;
9443 One_Dim := First (Expressions (N));
9444 while Present (One_Dim) loop
9445 One_Comp := First (Expressions (One_Dim));
9446 while Present (One_Comp) loop
9447 if Packed_Num = Byte_Size / Comp_Size then
9449 -- Byte is complete, add to list of expressions
9451 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
9452 Val := 0;
9453 Shift := Init_Shift;
9454 Packed_Num := 0;
9456 else
9457 Comp_Val := Expr_Rep_Value (One_Comp);
9459 -- Adjust for bias, and strip proper number of bits
9461 if Has_Biased_Representation (Ctyp) then
9462 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
9463 end if;
9465 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
9466 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
9467 Shift := Shift + Incr;
9468 Next (One_Comp);
9469 Packed_Num := Packed_Num + 1;
9470 end if;
9471 end loop;
9473 Next (One_Dim);
9474 end loop;
9476 if Packed_Num > 0 then
9478 -- Add final incomplete byte if present
9480 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
9481 end if;
9483 Rewrite (N,
9484 Unchecked_Convert_To (Typ,
9485 Make_Qualified_Expression (Loc,
9486 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
9487 Expression => Make_Aggregate (Loc, Expressions => Comps))));
9488 Analyze_And_Resolve (N);
9489 return True;
9490 end;
9491 end Two_Dim_Packed_Array_Handled;
9493 end Exp_Aggr;