]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/exp_aggr.adb
f8168fe77589d73bd137d16b5f7b873b2c26cbff
[thirdparty/gcc.git] / gcc / ada / exp_aggr.adb
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-2021, 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 ------------------------------------------------------------------------------
25
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 Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Util; use Exp_Util;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Tss; use Exp_Tss;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
43 with Lib; use Lib;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Ttypes; use Ttypes;
52 with Sem; use Sem;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Mech; use Sem_Mech;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sinfo; use Sinfo;
63 with Snames; use Snames;
64 with Stand; use Stand;
65 with Stringt; use Stringt;
66 with Tbuild; use Tbuild;
67 with Uintp; use Uintp;
68 with Urealp; use Urealp;
69
70 package body Exp_Aggr is
71
72 type Case_Bounds is record
73 Choice_Lo : Node_Id;
74 Choice_Hi : Node_Id;
75 Choice_Node : Node_Id;
76 end record;
77
78 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
79 -- Table type used by Check_Case_Choices procedure
80
81 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id);
82 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id);
83 procedure Expand_Container_Aggregate (N : Node_Id);
84
85 function Get_Base_Object (N : Node_Id) return Entity_Id;
86 -- Return the base object, i.e. the outermost prefix object, that N refers
87 -- to statically, or Empty if it cannot be determined. The assumption is
88 -- that all dereferences are explicit in the tree rooted at N.
89
90 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
91 -- N is an aggregate (record or array). Checks the presence of default
92 -- initialization (<>) in any component (Ada 2005: AI-287).
93
94 function Is_CCG_Supported_Aggregate (N : Node_Id) return Boolean;
95 -- Return True if aggregate N is located in a context supported by the
96 -- CCG backend; False otherwise.
97
98 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
99 -- Returns true if N is an aggregate used to initialize the components
100 -- of a statically allocated dispatch table.
101
102 function Late_Expansion
103 (N : Node_Id;
104 Typ : Entity_Id;
105 Target : Node_Id) return List_Id;
106 -- This routine implements top-down expansion of nested aggregates. In
107 -- doing so, it avoids the generation of temporaries at each level. N is
108 -- a nested record or array aggregate with the Expansion_Delayed flag.
109 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
110 -- expression that will hold the result of the aggregate expansion.
111
112 function Make_OK_Assignment_Statement
113 (Sloc : Source_Ptr;
114 Name : Node_Id;
115 Expression : Node_Id) return Node_Id;
116 -- This is like Make_Assignment_Statement, except that Assignment_OK
117 -- is set in the left operand. All assignments built by this unit use
118 -- this routine. This is needed to deal with assignments to initialized
119 -- constants that are done in place.
120
121 function Must_Slide
122 (Obj_Type : Entity_Id;
123 Typ : Entity_Id) return Boolean;
124 -- A static array aggregate in an object declaration can in most cases be
125 -- expanded in place. The one exception is when the aggregate is given
126 -- with component associations that specify different bounds from those of
127 -- the type definition in the object declaration. In this pathological
128 -- case the aggregate must slide, and we must introduce an intermediate
129 -- temporary to hold it.
130 --
131 -- The same holds in an assignment to one-dimensional array of arrays,
132 -- when a component may be given with bounds that differ from those of the
133 -- component type.
134
135 function Number_Of_Choices (N : Node_Id) return Nat;
136 -- Returns the number of discrete choices (not including the others choice
137 -- if present) contained in (sub-)aggregate N.
138
139 procedure Process_Transient_Component
140 (Loc : Source_Ptr;
141 Comp_Typ : Entity_Id;
142 Init_Expr : Node_Id;
143 Fin_Call : out Node_Id;
144 Hook_Clear : out Node_Id;
145 Aggr : Node_Id := Empty;
146 Stmts : List_Id := No_List);
147 -- Subsidiary to the expansion of array and record aggregates. Generate
148 -- part of the necessary code to finalize a transient component. Comp_Typ
149 -- is the component type. Init_Expr is the initialization expression of the
150 -- component which is always a function call. Fin_Call is the finalization
151 -- call used to clean up the transient function result. Hook_Clear is the
152 -- hook reset statement. Aggr and Stmts both control the placement of the
153 -- generated code. Aggr is the related aggregate. If present, all code is
154 -- inserted prior to Aggr using Insert_Action. Stmts is the initialization
155 -- statements of the component. If present, all code is added to Stmts.
156
157 procedure Process_Transient_Component_Completion
158 (Loc : Source_Ptr;
159 Aggr : Node_Id;
160 Fin_Call : Node_Id;
161 Hook_Clear : Node_Id;
162 Stmts : List_Id);
163 -- Subsidiary to the expansion of array and record aggregates. Generate
164 -- part of the necessary code to finalize a transient component. Aggr is
165 -- the related aggregate. Fin_Clear is the finalization call used to clean
166 -- up the transient component. Hook_Clear is the hook reset statment. Stmts
167 -- is the initialization statement list for the component. All generated
168 -- code is added to Stmts.
169
170 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
171 -- Sort the Case Table using the Lower Bound of each Choice as the key.
172 -- A simple insertion sort is used since the number of choices in a case
173 -- statement of variant part will usually be small and probably in near
174 -- sorted order.
175
176 ------------------------------------------------------
177 -- Local subprograms for Record Aggregate Expansion --
178 ------------------------------------------------------
179
180 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean;
181 -- True if N is an aggregate (possibly qualified or converted) that is
182 -- being returned from a build-in-place function.
183
184 function Build_Record_Aggr_Code
185 (N : Node_Id;
186 Typ : Entity_Id;
187 Lhs : Node_Id) return List_Id;
188 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
189 -- aggregate. Target is an expression containing the location on which the
190 -- component by component assignments will take place. Returns the list of
191 -- assignments plus all other adjustments needed for tagged and controlled
192 -- types.
193
194 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
195 -- Transform a record aggregate into a sequence of assignments performed
196 -- component by component. N is an N_Aggregate or N_Extension_Aggregate.
197 -- Typ is the type of the record aggregate.
198
199 procedure Expand_Record_Aggregate
200 (N : Node_Id;
201 Orig_Tag : Node_Id := Empty;
202 Parent_Expr : Node_Id := Empty);
203 -- This is the top level procedure for record aggregate expansion.
204 -- Expansion for record aggregates needs expand aggregates for tagged
205 -- record types. Specifically Expand_Record_Aggregate adds the Tag
206 -- field in front of the Component_Association list that was created
207 -- during resolution by Resolve_Record_Aggregate.
208 --
209 -- N is the record aggregate node.
210 -- Orig_Tag is the value of the Tag that has to be provided for this
211 -- specific aggregate. It carries the tag corresponding to the type
212 -- of the outermost aggregate during the recursive expansion
213 -- Parent_Expr is the ancestor part of the original extension
214 -- aggregate
215
216 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
217 -- Return true if one of the components is of a discriminated type with
218 -- defaults. An aggregate for a type with mutable components must be
219 -- expanded into individual assignments.
220
221 function In_Place_Assign_OK
222 (N : Node_Id;
223 Target_Object : Entity_Id := Empty) return Boolean;
224 -- Predicate to determine whether an aggregate assignment can be done in
225 -- place, because none of the new values can depend on the components of
226 -- the target of the assignment.
227
228 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
229 -- If the type of the aggregate is a type extension with renamed discrimi-
230 -- nants, we must initialize the hidden discriminants of the parent.
231 -- Otherwise, the target object must not be initialized. The discriminants
232 -- are initialized by calling the initialization procedure for the type.
233 -- This is incorrect if the initialization of other components has any
234 -- side effects. We restrict this call to the case where the parent type
235 -- has a variant part, because this is the only case where the hidden
236 -- discriminants are accessed, namely when calling discriminant checking
237 -- functions of the parent type, and when applying a stream attribute to
238 -- an object of the derived type.
239
240 -----------------------------------------------------
241 -- Local Subprograms for Array Aggregate Expansion --
242 -----------------------------------------------------
243
244 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
245 -- Returns true if an aggregate assignment can be done by the back end
246
247 function Aggr_Size_OK (N : Node_Id) return Boolean;
248 -- Very large static aggregates present problems to the back-end, and are
249 -- transformed into assignments and loops. This function verifies that the
250 -- total number of components of an aggregate is acceptable for rewriting
251 -- into a purely positional static form. Aggr_Size_OK must be called before
252 -- calling Flatten.
253 --
254 -- This function also detects and warns about one-component aggregates that
255 -- appear in a nonstatic context. Even if the component value is static,
256 -- such an aggregate must be expanded into an assignment.
257
258 function Backend_Processing_Possible (N : Node_Id) return Boolean;
259 -- This function checks if array aggregate N can be processed directly
260 -- by the backend. If this is the case, True is returned.
261
262 function Build_Array_Aggr_Code
263 (N : Node_Id;
264 Ctype : Entity_Id;
265 Index : Node_Id;
266 Into : Node_Id;
267 Scalar_Comp : Boolean;
268 Indexes : List_Id := No_List) return List_Id;
269 -- This recursive routine returns a list of statements containing the
270 -- loops and assignments that are needed for the expansion of the array
271 -- aggregate N.
272 --
273 -- N is the (sub-)aggregate node to be expanded into code. This node has
274 -- been fully analyzed, and its Etype is properly set.
275 --
276 -- Index is the index node corresponding to the array subaggregate N
277 --
278 -- Into is the target expression into which we are copying the aggregate.
279 -- Note that this node may not have been analyzed yet, and so the Etype
280 -- field may not be set.
281 --
282 -- Scalar_Comp is True if the component type of the aggregate is scalar
283 --
284 -- Indexes is the current list of expressions used to index the object we
285 -- are writing into.
286
287 procedure Convert_Array_Aggr_In_Allocator
288 (Decl : Node_Id;
289 Aggr : Node_Id;
290 Target : Node_Id);
291 -- If the aggregate appears within an allocator and can be expanded in
292 -- place, this routine generates the individual assignments to components
293 -- of the designated object. This is an optimization over the general
294 -- case, where a temporary is first created on the stack and then used to
295 -- construct the allocated object on the heap.
296
297 procedure Convert_To_Positional
298 (N : Node_Id;
299 Handle_Bit_Packed : Boolean := False);
300 -- If possible, convert named notation to positional notation. This
301 -- conversion is possible only in some static cases. If the conversion is
302 -- possible, then N is rewritten with the analyzed converted aggregate.
303 -- The parameter Handle_Bit_Packed is usually set False (since we do
304 -- not expect the back end to handle bit packed arrays, so the normal case
305 -- of conversion is pointless), but in the special case of a call from
306 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
307 -- these are cases we handle in there.
308
309 procedure Expand_Array_Aggregate (N : Node_Id);
310 -- This is the top-level routine to perform array aggregate expansion.
311 -- N is the N_Aggregate node to be expanded.
312
313 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
314 -- For two-dimensional packed aggregates with constant bounds and constant
315 -- components, it is preferable to pack the inner aggregates because the
316 -- whole matrix can then be presented to the back-end as a one-dimensional
317 -- list of literals. This is much more efficient than expanding into single
318 -- component assignments. This function determines if the type Typ is for
319 -- an array that is suitable for this optimization: it returns True if Typ
320 -- is a two dimensional bit packed array with component size 1, 2, or 4.
321
322 function Max_Aggregate_Size
323 (N : Node_Id;
324 Default_Size : Nat := 5000) return Nat;
325 -- Return the max size for a static aggregate N. Return Default_Size if no
326 -- other special criteria trigger.
327
328 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
329 -- Given an array aggregate, this function handles the case of a packed
330 -- array aggregate with all constant values, where the aggregate can be
331 -- evaluated at compile time. If this is possible, then N is rewritten
332 -- to be its proper compile time value with all the components properly
333 -- assembled. The expression is analyzed and resolved and True is returned.
334 -- If this transformation is not possible, N is unchanged and False is
335 -- returned.
336
337 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
338 -- If the type of the aggregate is a two-dimensional bit_packed array
339 -- it may be transformed into an array of bytes with constant values,
340 -- and presented to the back-end as a static value. The function returns
341 -- false if this transformation cannot be performed. THis is similar to,
342 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
343
344 ------------------------------------
345 -- Aggr_Assignment_OK_For_Backend --
346 ------------------------------------
347
348 -- Back-end processing by Gigi/gcc is possible only if all the following
349 -- conditions are met:
350
351 -- 1. N consists of a single OTHERS choice, possibly recursively, or
352 -- of a single choice, possibly recursively, if it is surrounded by
353 -- a qualified expression whose subtype mark is unconstrained.
354
355 -- 2. The array type has no null ranges (the purpose of this is to
356 -- avoid a bogus warning for an out-of-range value).
357
358 -- 3. The array type has no atomic components
359
360 -- 4. The component type is elementary
361
362 -- 5. The component size is a multiple of Storage_Unit
363
364 -- 6. The component size is Storage_Unit or the value is of the form
365 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
366 -- and M in 0 .. A-1. This can also be viewed as K occurrences of
367 -- the Storage_Unit value M, concatenated together.
368
369 -- The ultimate goal is to generate a call to a fast memset routine
370 -- specifically optimized for the target.
371
372 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
373 Csiz : Uint := No_Uint;
374 Ctyp : Entity_Id;
375 Expr : Node_Id;
376 High : Node_Id;
377 Index : Entity_Id;
378 Low : Node_Id;
379 Nunits : Int;
380 Remainder : Uint;
381 Value : Uint;
382
383 function Is_OK_Aggregate (Aggr : Node_Id) return Boolean;
384 -- Return true if Aggr is suitable for back-end assignment
385
386 ---------------------
387 -- Is_OK_Aggregate --
388 ---------------------
389
390 function Is_OK_Aggregate (Aggr : Node_Id) return Boolean is
391 Assoc : constant List_Id := Component_Associations (Aggr);
392
393 begin
394 -- An "others" aggregate is most likely OK, but see below
395
396 if Is_Others_Aggregate (Aggr) then
397 null;
398
399 -- An aggregate with a single choice requires a qualified expression
400 -- whose subtype mark is an unconstrained type because we need it to
401 -- have the semantics of an "others" aggregate.
402
403 elsif Nkind (Parent (N)) = N_Qualified_Expression
404 and then not Is_Constrained (Entity (Subtype_Mark (Parent (N))))
405 and then Is_Single_Aggregate (Aggr)
406 then
407 null;
408
409 -- The other cases are not OK
410
411 else
412 return False;
413 end if;
414
415 -- In any case we do not support an iterated association
416
417 return Nkind (First (Assoc)) /= N_Iterated_Component_Association;
418 end Is_OK_Aggregate;
419
420 -- Start of processing for Aggr_Assignment_OK_For_Backend
421
422 begin
423 -- Back end doesn't know about <>
424
425 if Has_Default_Init_Comps (N) then
426 return False;
427 end if;
428
429 -- Recurse as far as possible to find the innermost component type
430
431 Ctyp := Etype (N);
432 Expr := N;
433 while Is_Array_Type (Ctyp) loop
434 if Nkind (Expr) /= N_Aggregate
435 or else not Is_OK_Aggregate (Expr)
436 then
437 return False;
438 end if;
439
440 Index := First_Index (Ctyp);
441 while Present (Index) loop
442 Get_Index_Bounds (Index, Low, High);
443
444 if Is_Null_Range (Low, High) then
445 return False;
446 end if;
447
448 Next_Index (Index);
449 end loop;
450
451 Expr := Expression (First (Component_Associations (Expr)));
452
453 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
454 if Nkind (Expr) /= N_Aggregate
455 or else not Is_OK_Aggregate (Expr)
456 then
457 return False;
458 end if;
459
460 Expr := Expression (First (Component_Associations (Expr)));
461 end loop;
462
463 if Has_Atomic_Components (Ctyp) then
464 return False;
465 end if;
466
467 Csiz := Component_Size (Ctyp);
468 Ctyp := Component_Type (Ctyp);
469
470 if Is_Full_Access (Ctyp) then
471 return False;
472 end if;
473 end loop;
474
475 -- Access types need to be dealt with specially
476
477 if Is_Access_Type (Ctyp) then
478
479 -- Component_Size is not set by Layout_Type if the component
480 -- type is an access type ???
481
482 Csiz := Esize (Ctyp);
483
484 -- Fat pointers are rejected as they are not really elementary
485 -- for the backend.
486
487 if Csiz /= System_Address_Size then
488 return False;
489 end if;
490
491 -- The supported expressions are NULL and constants, others are
492 -- rejected upfront to avoid being analyzed below, which can be
493 -- problematic for some of them, for example allocators.
494
495 if Nkind (Expr) /= N_Null and then not Is_Entity_Name (Expr) then
496 return False;
497 end if;
498
499 -- Scalar types are OK if their size is a multiple of Storage_Unit
500
501 elsif Is_Scalar_Type (Ctyp) then
502 pragma Assert (Csiz /= No_Uint);
503
504 if Csiz mod System_Storage_Unit /= 0 then
505 return False;
506 end if;
507
508 -- Composite types are rejected
509
510 else
511 return False;
512 end if;
513
514 -- If the expression has side effects (e.g. contains calls with
515 -- potential side effects) reject as well. We only preanalyze the
516 -- expression to prevent the removal of intended side effects.
517
518 Preanalyze_And_Resolve (Expr, Ctyp);
519
520 if not Side_Effect_Free (Expr) then
521 return False;
522 end if;
523
524 -- The expression needs to be analyzed if True is returned
525
526 Analyze_And_Resolve (Expr, Ctyp);
527
528 -- Strip away any conversions from the expression as they simply
529 -- qualify the real expression.
530
531 while Nkind (Expr) in N_Unchecked_Type_Conversion | N_Type_Conversion
532 loop
533 Expr := Expression (Expr);
534 end loop;
535
536 Nunits := UI_To_Int (Csiz) / System_Storage_Unit;
537
538 if Nunits = 1 then
539 return True;
540 end if;
541
542 if not Compile_Time_Known_Value (Expr) then
543 return False;
544 end if;
545
546 -- The only supported value for floating point is 0.0
547
548 if Is_Floating_Point_Type (Ctyp) then
549 return Expr_Value_R (Expr) = Ureal_0;
550 end if;
551
552 -- For other types, we can look into the value as an integer, which
553 -- means the representation value for enumeration literals.
554
555 Value := Expr_Rep_Value (Expr);
556
557 if Has_Biased_Representation (Ctyp) then
558 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
559 end if;
560
561 -- Values 0 and -1 immediately satisfy the last check
562
563 if Value = Uint_0 or else Value = Uint_Minus_1 then
564 return True;
565 end if;
566
567 -- We need to work with an unsigned value
568
569 if Value < 0 then
570 Value := Value + 2**(System_Storage_Unit * Nunits);
571 end if;
572
573 Remainder := Value rem 2**System_Storage_Unit;
574
575 for J in 1 .. Nunits - 1 loop
576 Value := Value / 2**System_Storage_Unit;
577
578 if Value rem 2**System_Storage_Unit /= Remainder then
579 return False;
580 end if;
581 end loop;
582
583 return True;
584 end Aggr_Assignment_OK_For_Backend;
585
586 ------------------
587 -- Aggr_Size_OK --
588 ------------------
589
590 function Aggr_Size_OK (N : Node_Id) return Boolean is
591 Typ : constant Entity_Id := Etype (N);
592 Lo : Node_Id;
593 Hi : Node_Id;
594 Indx : Node_Id;
595 Size : Uint;
596 Lov : Uint;
597 Hiv : Uint;
598
599 Max_Aggr_Size : Nat;
600 -- Determines the maximum size of an array aggregate produced by
601 -- converting named to positional notation (e.g. from others clauses).
602 -- This avoids running away with attempts to convert huge aggregates,
603 -- which hit memory limits in the backend.
604
605 function Component_Count (T : Entity_Id) return Nat;
606 -- The limit is applied to the total number of subcomponents that the
607 -- aggregate will have, which is the number of static expressions
608 -- that will appear in the flattened array. This requires a recursive
609 -- computation of the number of scalar components of the structure.
610
611 ---------------------
612 -- Component_Count --
613 ---------------------
614
615 function Component_Count (T : Entity_Id) return Nat is
616 Res : Nat := 0;
617 Comp : Entity_Id;
618
619 begin
620 if Is_Scalar_Type (T) then
621 return 1;
622
623 elsif Is_Record_Type (T) then
624 Comp := First_Component (T);
625 while Present (Comp) loop
626 Res := Res + Component_Count (Etype (Comp));
627 Next_Component (Comp);
628 end loop;
629
630 return Res;
631
632 elsif Is_Array_Type (T) then
633 declare
634 Lo : constant Node_Id :=
635 Type_Low_Bound (Etype (First_Index (T)));
636 Hi : constant Node_Id :=
637 Type_High_Bound (Etype (First_Index (T)));
638
639 Siz : constant Nat := Component_Count (Component_Type (T));
640
641 begin
642 -- Check for superflat arrays, i.e. arrays with such bounds
643 -- as 4 .. 2, to insure that this function never returns a
644 -- meaningless negative value.
645
646 if not Compile_Time_Known_Value (Lo)
647 or else not Compile_Time_Known_Value (Hi)
648 or else Expr_Value (Hi) < Expr_Value (Lo)
649 then
650 return 0;
651
652 else
653 -- If the number of components is greater than Int'Last,
654 -- then return Int'Last, so caller will return False (Aggr
655 -- size is not OK). Otherwise, UI_To_Int will crash.
656
657 declare
658 UI : constant Uint :=
659 Expr_Value (Hi) - Expr_Value (Lo) + 1;
660 begin
661 if UI_Is_In_Int_Range (UI) then
662 return Siz * UI_To_Int (UI);
663 else
664 return Int'Last;
665 end if;
666 end;
667 end if;
668 end;
669
670 else
671 -- Can only be a null for an access type
672
673 return 1;
674 end if;
675 end Component_Count;
676
677 -- Start of processing for Aggr_Size_OK
678
679 begin
680 -- We bump the maximum size unless the aggregate has a single component
681 -- association, which will be more efficient if implemented with a loop.
682
683 if No (Expressions (N))
684 and then No (Next (First (Component_Associations (N))))
685 then
686 Max_Aggr_Size := Max_Aggregate_Size (N);
687 else
688 Max_Aggr_Size := Max_Aggregate_Size (N, 500_000);
689 end if;
690
691 Size := UI_From_Int (Component_Count (Component_Type (Typ)));
692
693 Indx := First_Index (Typ);
694 while Present (Indx) loop
695 Lo := Type_Low_Bound (Etype (Indx));
696 Hi := Type_High_Bound (Etype (Indx));
697
698 -- Bounds need to be known at compile time
699
700 if not Compile_Time_Known_Value (Lo)
701 or else not Compile_Time_Known_Value (Hi)
702 then
703 return False;
704 end if;
705
706 Lov := Expr_Value (Lo);
707 Hiv := Expr_Value (Hi);
708
709 -- A flat array is always safe
710
711 if Hiv < Lov then
712 return True;
713 end if;
714
715 -- One-component aggregates are suspicious, and if the context type
716 -- is an object declaration with nonstatic bounds it will trip gcc;
717 -- such an aggregate must be expanded into a single assignment.
718
719 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
720 declare
721 Index_Type : constant Entity_Id :=
722 Etype
723 (First_Index (Etype (Defining_Identifier (Parent (N)))));
724 Indx : Node_Id;
725
726 begin
727 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
728 or else not Compile_Time_Known_Value
729 (Type_High_Bound (Index_Type))
730 then
731 if Present (Component_Associations (N)) then
732 Indx :=
733 First
734 (Choice_List (First (Component_Associations (N))));
735
736 if Is_Entity_Name (Indx)
737 and then not Is_Type (Entity (Indx))
738 then
739 Error_Msg_N
740 ("single component aggregate in "
741 & "non-static context??", Indx);
742 Error_Msg_N ("\maybe subtype name was meant??", Indx);
743 end if;
744 end if;
745
746 return False;
747 end if;
748 end;
749 end if;
750
751 declare
752 Rng : constant Uint := Hiv - Lov + 1;
753
754 begin
755 -- Check if size is too large
756
757 if not UI_Is_In_Int_Range (Rng) then
758 return False;
759 end if;
760
761 -- Compute the size using universal arithmetic to avoid the
762 -- possibility of overflow on very large aggregates.
763
764 Size := Size * Rng;
765
766 if Size <= 0
767 or else Size > Max_Aggr_Size
768 then
769 return False;
770 end if;
771 end;
772
773 -- Bounds must be in integer range, for later array construction
774
775 if not UI_Is_In_Int_Range (Lov)
776 or else
777 not UI_Is_In_Int_Range (Hiv)
778 then
779 return False;
780 end if;
781
782 Next_Index (Indx);
783 end loop;
784
785 return True;
786 end Aggr_Size_OK;
787
788 ---------------------------------
789 -- Backend_Processing_Possible --
790 ---------------------------------
791
792 -- Backend processing by Gigi/gcc is possible only if all the following
793 -- conditions are met:
794
795 -- 1. N is fully positional
796
797 -- 2. N is not a bit-packed array aggregate;
798
799 -- 3. The size of N's array type must be known at compile time. Note
800 -- that this implies that the component size is also known
801
802 -- 4. The array type of N does not follow the Fortran layout convention
803 -- or if it does it must be 1 dimensional.
804
805 -- 5. The array component type may not be tagged (which could necessitate
806 -- reassignment of proper tags).
807
808 -- 6. The array component type must not have unaligned bit components
809
810 -- 7. None of the components of the aggregate may be bit unaligned
811 -- components.
812
813 -- 8. There cannot be delayed components, since we do not know enough
814 -- at this stage to know if back end processing is possible.
815
816 -- 9. There cannot be any discriminated record components, since the
817 -- back end cannot handle this complex case.
818
819 -- 10. No controlled actions need to be generated for components
820
821 -- 11. When generating C code, N must be part of a N_Object_Declaration
822
823 -- 12. When generating C code, N must not include function calls
824
825 function Backend_Processing_Possible (N : Node_Id) return Boolean is
826 Typ : constant Entity_Id := Etype (N);
827 -- Typ is the correct constrained array subtype of the aggregate
828
829 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
830 -- This routine checks components of aggregate N, enforcing checks
831 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
832 -- are performed on subaggregates. The Index value is the current index
833 -- being checked in the multidimensional case.
834
835 ---------------------
836 -- Component_Check --
837 ---------------------
838
839 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
840 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
841 -- Given a type conversion or an unchecked type conversion N, return
842 -- its innermost original expression.
843
844 ----------------------------------
845 -- Ultimate_Original_Expression --
846 ----------------------------------
847
848 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
849 Expr : Node_Id := Original_Node (N);
850
851 begin
852 while Nkind (Expr) in
853 N_Type_Conversion | N_Unchecked_Type_Conversion
854 loop
855 Expr := Original_Node (Expression (Expr));
856 end loop;
857
858 return Expr;
859 end Ultimate_Original_Expression;
860
861 -- Local variables
862
863 Expr : Node_Id;
864
865 -- Start of processing for Component_Check
866
867 begin
868 -- Checks 1: (no component associations)
869
870 if Present (Component_Associations (N)) then
871 return False;
872 end if;
873
874 -- Checks 11: The C code generator cannot handle aggregates that are
875 -- not part of an object declaration.
876
877 if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
878 return False;
879 end if;
880
881 -- Checks on components
882
883 -- Recurse to check subaggregates, which may appear in qualified
884 -- expressions. If delayed, the front-end will have to expand.
885 -- If the component is a discriminated record, treat as nonstatic,
886 -- as the back-end cannot handle this properly.
887
888 Expr := First (Expressions (N));
889 while Present (Expr) loop
890
891 -- Checks 8: (no delayed components)
892
893 if Is_Delayed_Aggregate (Expr) then
894 return False;
895 end if;
896
897 -- Checks 9: (no discriminated records)
898
899 if Present (Etype (Expr))
900 and then Is_Record_Type (Etype (Expr))
901 and then Has_Discriminants (Etype (Expr))
902 then
903 return False;
904 end if;
905
906 -- Checks 7. Component must not be bit aligned component
907
908 if Possible_Bit_Aligned_Component (Expr) then
909 return False;
910 end if;
911
912 -- Checks 12: (no function call)
913
914 if Modify_Tree_For_C
915 and then
916 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
917 then
918 return False;
919 end if;
920
921 -- Recursion to following indexes for multiple dimension case
922
923 if Present (Next_Index (Index))
924 and then not Component_Check (Expr, Next_Index (Index))
925 then
926 return False;
927 end if;
928
929 -- All checks for that component finished, on to next
930
931 Next (Expr);
932 end loop;
933
934 return True;
935 end Component_Check;
936
937 -- Start of processing for Backend_Processing_Possible
938
939 begin
940 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
941
942 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
943 return False;
944 end if;
945
946 -- If component is limited, aggregate must be expanded because each
947 -- component assignment must be built in place.
948
949 if Is_Limited_View (Component_Type (Typ)) then
950 return False;
951 end if;
952
953 -- Checks 4 (array must not be multidimensional Fortran case)
954
955 if Convention (Typ) = Convention_Fortran
956 and then Number_Dimensions (Typ) > 1
957 then
958 return False;
959 end if;
960
961 -- Checks 3 (size of array must be known at compile time)
962
963 if not Size_Known_At_Compile_Time (Typ) then
964 return False;
965 end if;
966
967 -- Checks on components
968
969 if not Component_Check (N, First_Index (Typ)) then
970 return False;
971 end if;
972
973 -- Checks 5 (if the component type is tagged, then we may need to do
974 -- tag adjustments. Perhaps this should be refined to check for any
975 -- component associations that actually need tag adjustment, similar
976 -- to the test in Component_OK_For_Backend for record aggregates with
977 -- tagged components, but not clear whether it's worthwhile ???; in the
978 -- case of virtual machines (no Tagged_Type_Expansion), object tags are
979 -- handled implicitly).
980
981 if Is_Tagged_Type (Component_Type (Typ))
982 and then Tagged_Type_Expansion
983 then
984 return False;
985 end if;
986
987 -- Checks 6 (component type must not have bit aligned components)
988
989 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
990 return False;
991 end if;
992
993 -- Backend processing is possible
994
995 return True;
996 end Backend_Processing_Possible;
997
998 ---------------------------
999 -- Build_Array_Aggr_Code --
1000 ---------------------------
1001
1002 -- The code that we generate from a one dimensional aggregate is
1003
1004 -- 1. If the subaggregate contains discrete choices we
1005
1006 -- (a) Sort the discrete choices
1007
1008 -- (b) Otherwise for each discrete choice that specifies a range we
1009 -- emit a loop. If a range specifies a maximum of three values, or
1010 -- we are dealing with an expression we emit a sequence of
1011 -- assignments instead of a loop.
1012
1013 -- (c) Generate the remaining loops to cover the others choice if any
1014
1015 -- 2. If the aggregate contains positional elements we
1016
1017 -- (a) translate the positional elements in a series of assignments
1018
1019 -- (b) Generate a final loop to cover the others choice if any.
1020 -- Note that this final loop has to be a while loop since the case
1021
1022 -- L : Integer := Integer'Last;
1023 -- H : Integer := Integer'Last;
1024 -- A : array (L .. H) := (1, others =>0);
1025
1026 -- cannot be handled by a for loop. Thus for the following
1027
1028 -- array (L .. H) := (.. positional elements.., others =>E);
1029
1030 -- we always generate something like:
1031
1032 -- J : Index_Type := Index_Of_Last_Positional_Element;
1033 -- while J < H loop
1034 -- J := Index_Base'Succ (J)
1035 -- Tmp (J) := E;
1036 -- end loop;
1037
1038 function Build_Array_Aggr_Code
1039 (N : Node_Id;
1040 Ctype : Entity_Id;
1041 Index : Node_Id;
1042 Into : Node_Id;
1043 Scalar_Comp : Boolean;
1044 Indexes : List_Id := No_List) return List_Id
1045 is
1046 Loc : constant Source_Ptr := Sloc (N);
1047 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
1048 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
1049 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
1050
1051 function Add (Val : Int; To : Node_Id) return Node_Id;
1052 -- Returns an expression where Val is added to expression To, unless
1053 -- To+Val is provably out of To's base type range. To must be an
1054 -- already analyzed expression.
1055
1056 function Empty_Range (L, H : Node_Id) return Boolean;
1057 -- Returns True if the range defined by L .. H is certainly empty
1058
1059 function Equal (L, H : Node_Id) return Boolean;
1060 -- Returns True if L = H for sure
1061
1062 function Index_Base_Name return Node_Id;
1063 -- Returns a new reference to the index type name
1064
1065 function Gen_Assign
1066 (Ind : Node_Id;
1067 Expr : Node_Id;
1068 In_Loop : Boolean := False) return List_Id;
1069 -- Ind must be a side-effect-free expression. If the input aggregate N
1070 -- to Build_Loop contains no subaggregates, then this function returns
1071 -- the assignment statement:
1072 --
1073 -- Into (Indexes, Ind) := Expr;
1074 --
1075 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
1076 -- when the assignment appears within a generated loop.
1077 --
1078 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1079 -- is empty and we generate a call to the corresponding IP subprogram.
1080
1081 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
1082 -- Nodes L and H must be side-effect-free expressions. If the input
1083 -- aggregate N to Build_Loop contains no subaggregates, this routine
1084 -- returns the for loop statement:
1085 --
1086 -- for J in Index_Base'(L) .. Index_Base'(H) loop
1087 -- Into (Indexes, J) := Expr;
1088 -- end loop;
1089 --
1090 -- Otherwise we call Build_Code recursively. As an optimization if the
1091 -- loop covers 3 or fewer scalar elements we generate a sequence of
1092 -- assignments.
1093 -- If the component association that generates the loop comes from an
1094 -- Iterated_Component_Association, the loop parameter has the name of
1095 -- the corresponding parameter in the original construct.
1096
1097 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
1098 -- Nodes L and H must be side-effect-free expressions. If the input
1099 -- aggregate N to Build_Loop contains no subaggregates, this routine
1100 -- returns the while loop statement:
1101 --
1102 -- J : Index_Base := L;
1103 -- while J < H loop
1104 -- J := Index_Base'Succ (J);
1105 -- Into (Indexes, J) := Expr;
1106 -- end loop;
1107 --
1108 -- Otherwise we call Build_Code recursively
1109
1110 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
1111 -- For an association with a box, use value given by aspect
1112 -- Default_Component_Value of array type if specified, else use
1113 -- value given by aspect Default_Value for component type itself
1114 -- if specified, else return Empty.
1115
1116 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
1117 function Local_Expr_Value (E : Node_Id) return Uint;
1118 -- These two Local routines are used to replace the corresponding ones
1119 -- in sem_eval because while processing the bounds of an aggregate with
1120 -- discrete choices whose index type is an enumeration, we build static
1121 -- expressions not recognized by Compile_Time_Known_Value as such since
1122 -- they have not yet been analyzed and resolved. All the expressions in
1123 -- question are things like Index_Base_Name'Val (Const) which we can
1124 -- easily recognize as being constant.
1125
1126 ---------
1127 -- Add --
1128 ---------
1129
1130 function Add (Val : Int; To : Node_Id) return Node_Id is
1131 Expr_Pos : Node_Id;
1132 Expr : Node_Id;
1133 To_Pos : Node_Id;
1134 U_To : Uint;
1135 U_Val : constant Uint := UI_From_Int (Val);
1136
1137 begin
1138 -- Note: do not try to optimize the case of Val = 0, because
1139 -- we need to build a new node with the proper Sloc value anyway.
1140
1141 -- First test if we can do constant folding
1142
1143 if Local_Compile_Time_Known_Value (To) then
1144 U_To := Local_Expr_Value (To) + Val;
1145
1146 -- Determine if our constant is outside the range of the index.
1147 -- If so return an Empty node. This empty node will be caught
1148 -- by Empty_Range below.
1149
1150 if Compile_Time_Known_Value (Index_Base_L)
1151 and then U_To < Expr_Value (Index_Base_L)
1152 then
1153 return Empty;
1154
1155 elsif Compile_Time_Known_Value (Index_Base_H)
1156 and then U_To > Expr_Value (Index_Base_H)
1157 then
1158 return Empty;
1159 end if;
1160
1161 Expr_Pos := Make_Integer_Literal (Loc, U_To);
1162 Set_Is_Static_Expression (Expr_Pos);
1163
1164 if not Is_Enumeration_Type (Index_Base) then
1165 Expr := Expr_Pos;
1166
1167 -- If we are dealing with enumeration return
1168 -- Index_Base'Val (Expr_Pos)
1169
1170 else
1171 Expr :=
1172 Make_Attribute_Reference
1173 (Loc,
1174 Prefix => Index_Base_Name,
1175 Attribute_Name => Name_Val,
1176 Expressions => New_List (Expr_Pos));
1177 end if;
1178
1179 return Expr;
1180 end if;
1181
1182 -- If we are here no constant folding possible
1183
1184 if not Is_Enumeration_Type (Index_Base) then
1185 Expr :=
1186 Make_Op_Add (Loc,
1187 Left_Opnd => Duplicate_Subexpr (To),
1188 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
1189
1190 -- If we are dealing with enumeration return
1191 -- Index_Base'Val (Index_Base'Pos (To) + Val)
1192
1193 else
1194 To_Pos :=
1195 Make_Attribute_Reference
1196 (Loc,
1197 Prefix => Index_Base_Name,
1198 Attribute_Name => Name_Pos,
1199 Expressions => New_List (Duplicate_Subexpr (To)));
1200
1201 Expr_Pos :=
1202 Make_Op_Add (Loc,
1203 Left_Opnd => To_Pos,
1204 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
1205
1206 Expr :=
1207 Make_Attribute_Reference
1208 (Loc,
1209 Prefix => Index_Base_Name,
1210 Attribute_Name => Name_Val,
1211 Expressions => New_List (Expr_Pos));
1212 end if;
1213
1214 return Expr;
1215 end Add;
1216
1217 -----------------
1218 -- Empty_Range --
1219 -----------------
1220
1221 function Empty_Range (L, H : Node_Id) return Boolean is
1222 Is_Empty : Boolean := False;
1223 Low : Node_Id;
1224 High : Node_Id;
1225
1226 begin
1227 -- First check if L or H were already detected as overflowing the
1228 -- index base range type by function Add above. If this is so Add
1229 -- returns the empty node.
1230
1231 if No (L) or else No (H) then
1232 return True;
1233 end if;
1234
1235 for J in 1 .. 3 loop
1236 case J is
1237
1238 -- L > H range is empty
1239
1240 when 1 =>
1241 Low := L;
1242 High := H;
1243
1244 -- B_L > H range must be empty
1245
1246 when 2 =>
1247 Low := Index_Base_L;
1248 High := H;
1249
1250 -- L > B_H range must be empty
1251
1252 when 3 =>
1253 Low := L;
1254 High := Index_Base_H;
1255 end case;
1256
1257 if Local_Compile_Time_Known_Value (Low)
1258 and then
1259 Local_Compile_Time_Known_Value (High)
1260 then
1261 Is_Empty :=
1262 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
1263 end if;
1264
1265 exit when Is_Empty;
1266 end loop;
1267
1268 return Is_Empty;
1269 end Empty_Range;
1270
1271 -----------
1272 -- Equal --
1273 -----------
1274
1275 function Equal (L, H : Node_Id) return Boolean is
1276 begin
1277 if L = H then
1278 return True;
1279
1280 elsif Local_Compile_Time_Known_Value (L)
1281 and then
1282 Local_Compile_Time_Known_Value (H)
1283 then
1284 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1285 end if;
1286
1287 return False;
1288 end Equal;
1289
1290 ----------------
1291 -- Gen_Assign --
1292 ----------------
1293
1294 function Gen_Assign
1295 (Ind : Node_Id;
1296 Expr : Node_Id;
1297 In_Loop : Boolean := False) return List_Id
1298 is
1299 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1300 -- Collect insert_actions generated in the construction of a loop,
1301 -- and prepend them to the sequence of assignments to complete the
1302 -- eventual body of the loop.
1303
1304 procedure Initialize_Array_Component
1305 (Arr_Comp : Node_Id;
1306 Comp_Typ : Node_Id;
1307 Init_Expr : Node_Id;
1308 Stmts : List_Id);
1309 -- Perform the initialization of array component Arr_Comp with
1310 -- expected type Comp_Typ. Init_Expr denotes the initialization
1311 -- expression of the array component. All generated code is added
1312 -- to list Stmts.
1313
1314 procedure Initialize_Ctrl_Array_Component
1315 (Arr_Comp : Node_Id;
1316 Comp_Typ : Entity_Id;
1317 Init_Expr : Node_Id;
1318 Stmts : List_Id);
1319 -- Perform the initialization of array component Arr_Comp when its
1320 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1321 -- the initialization expression of the array component. All hook-
1322 -- related declarations are inserted prior to aggregate N. Remaining
1323 -- code is added to list Stmts.
1324
1325 ----------------------
1326 -- Add_Loop_Actions --
1327 ----------------------
1328
1329 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1330 Res : List_Id;
1331
1332 begin
1333 -- Ada 2005 (AI-287): Do nothing else in case of default
1334 -- initialized component.
1335
1336 if No (Expr) then
1337 return Lis;
1338
1339 elsif Nkind (Parent (Expr)) = N_Component_Association
1340 and then Present (Loop_Actions (Parent (Expr)))
1341 then
1342 Append_List (Lis, Loop_Actions (Parent (Expr)));
1343 Res := Loop_Actions (Parent (Expr));
1344 Set_Loop_Actions (Parent (Expr), No_List);
1345 return Res;
1346
1347 else
1348 return Lis;
1349 end if;
1350 end Add_Loop_Actions;
1351
1352 --------------------------------
1353 -- Initialize_Array_Component --
1354 --------------------------------
1355
1356 procedure Initialize_Array_Component
1357 (Arr_Comp : Node_Id;
1358 Comp_Typ : Node_Id;
1359 Init_Expr : Node_Id;
1360 Stmts : List_Id)
1361 is
1362 Exceptions_OK : constant Boolean :=
1363 not Restriction_Active
1364 (No_Exception_Propagation);
1365
1366 Finalization_OK : constant Boolean :=
1367 Present (Comp_Typ)
1368 and then Needs_Finalization (Comp_Typ);
1369
1370 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
1371 Adj_Call : Node_Id;
1372 Blk_Stmts : List_Id;
1373 Init_Stmt : Node_Id;
1374
1375 begin
1376 -- Protect the initialization statements from aborts. Generate:
1377
1378 -- Abort_Defer;
1379
1380 if Finalization_OK and Abort_Allowed then
1381 if Exceptions_OK then
1382 Blk_Stmts := New_List;
1383 else
1384 Blk_Stmts := Stmts;
1385 end if;
1386
1387 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
1388
1389 -- Otherwise aborts are not allowed. All generated code is added
1390 -- directly to the input list.
1391
1392 else
1393 Blk_Stmts := Stmts;
1394 end if;
1395
1396 -- Initialize the array element. Generate:
1397
1398 -- Arr_Comp := Init_Expr;
1399
1400 -- Note that the initialization expression is replicated because
1401 -- it has to be reevaluated within a generated loop.
1402
1403 Init_Stmt :=
1404 Make_OK_Assignment_Statement (Loc,
1405 Name => New_Copy_Tree (Arr_Comp),
1406 Expression => New_Copy_Tree (Init_Expr));
1407 Set_No_Ctrl_Actions (Init_Stmt);
1408
1409 -- If this is an aggregate for an array of arrays, each
1410 -- subaggregate will be expanded as well, and even with
1411 -- No_Ctrl_Actions the assignments of inner components will
1412 -- require attachment in their assignments to temporaries. These
1413 -- temporaries must be finalized for each subaggregate. Generate:
1414
1415 -- begin
1416 -- Arr_Comp := Init_Expr;
1417 -- end;
1418
1419 if Finalization_OK and then Is_Array_Type (Comp_Typ) then
1420 Init_Stmt :=
1421 Make_Block_Statement (Loc,
1422 Handled_Statement_Sequence =>
1423 Make_Handled_Sequence_Of_Statements (Loc,
1424 Statements => New_List (Init_Stmt)));
1425 end if;
1426
1427 Append_To (Blk_Stmts, Init_Stmt);
1428
1429 -- Adjust the tag due to a possible view conversion. Generate:
1430
1431 -- Arr_Comp._tag := Full_TypP;
1432
1433 if Tagged_Type_Expansion
1434 and then Present (Comp_Typ)
1435 and then Is_Tagged_Type (Comp_Typ)
1436 then
1437 Append_To (Blk_Stmts,
1438 Make_OK_Assignment_Statement (Loc,
1439 Name =>
1440 Make_Selected_Component (Loc,
1441 Prefix => New_Copy_Tree (Arr_Comp),
1442 Selector_Name =>
1443 New_Occurrence_Of
1444 (First_Tag_Component (Full_Typ), Loc)),
1445
1446 Expression =>
1447 Unchecked_Convert_To (RTE (RE_Tag),
1448 New_Occurrence_Of
1449 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1450 Loc))));
1451 end if;
1452
1453 -- Adjust the array component. Controlled subaggregates are not
1454 -- considered because each of their individual elements will
1455 -- receive an adjustment of its own. Generate:
1456
1457 -- [Deep_]Adjust (Arr_Comp);
1458
1459 if Finalization_OK
1460 and then not Is_Limited_Type (Comp_Typ)
1461 and then not Is_Build_In_Place_Function_Call (Init_Expr)
1462 and then not
1463 (Is_Array_Type (Comp_Typ)
1464 and then Is_Controlled (Component_Type (Comp_Typ))
1465 and then Nkind (Expr) = N_Aggregate)
1466 then
1467 Adj_Call :=
1468 Make_Adjust_Call
1469 (Obj_Ref => New_Copy_Tree (Arr_Comp),
1470 Typ => Comp_Typ);
1471
1472 -- Guard against a missing [Deep_]Adjust when the component
1473 -- type was not frozen properly.
1474
1475 if Present (Adj_Call) then
1476 Append_To (Blk_Stmts, Adj_Call);
1477 end if;
1478 end if;
1479
1480 -- Complete the protection of the initialization statements
1481
1482 if Finalization_OK and Abort_Allowed then
1483
1484 -- Wrap the initialization statements in a block to catch a
1485 -- potential exception. Generate:
1486
1487 -- begin
1488 -- Abort_Defer;
1489 -- Arr_Comp := Init_Expr;
1490 -- Arr_Comp._tag := Full_TypP;
1491 -- [Deep_]Adjust (Arr_Comp);
1492 -- at end
1493 -- Abort_Undefer_Direct;
1494 -- end;
1495
1496 if Exceptions_OK then
1497 Append_To (Stmts,
1498 Build_Abort_Undefer_Block (Loc,
1499 Stmts => Blk_Stmts,
1500 Context => N));
1501
1502 -- Otherwise exceptions are not propagated. Generate:
1503
1504 -- Abort_Defer;
1505 -- Arr_Comp := Init_Expr;
1506 -- Arr_Comp._tag := Full_TypP;
1507 -- [Deep_]Adjust (Arr_Comp);
1508 -- Abort_Undefer;
1509
1510 else
1511 Append_To (Blk_Stmts,
1512 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1513 end if;
1514 end if;
1515 end Initialize_Array_Component;
1516
1517 -------------------------------------
1518 -- Initialize_Ctrl_Array_Component --
1519 -------------------------------------
1520
1521 procedure Initialize_Ctrl_Array_Component
1522 (Arr_Comp : Node_Id;
1523 Comp_Typ : Entity_Id;
1524 Init_Expr : Node_Id;
1525 Stmts : List_Id)
1526 is
1527 Act_Aggr : Node_Id;
1528 Act_Stmts : List_Id;
1529 Expr : Node_Id;
1530 Fin_Call : Node_Id;
1531 Hook_Clear : Node_Id;
1532
1533 In_Place_Expansion : Boolean;
1534 -- Flag set when a nonlimited controlled function call requires
1535 -- in-place expansion.
1536
1537 begin
1538 -- Duplicate the initialization expression in case the context is
1539 -- a multi choice list or an "others" choice which plugs various
1540 -- holes in the aggregate. As a result the expression is no longer
1541 -- shared between the various components and is reevaluated for
1542 -- each such component.
1543
1544 Expr := New_Copy_Tree (Init_Expr);
1545 Set_Parent (Expr, Parent (Init_Expr));
1546
1547 -- Perform a preliminary analysis and resolution to determine what
1548 -- the initialization expression denotes. An unanalyzed function
1549 -- call may appear as an identifier or an indexed component.
1550
1551 if Nkind (Expr) in N_Function_Call
1552 | N_Identifier
1553 | N_Indexed_Component
1554 and then not Analyzed (Expr)
1555 then
1556 Preanalyze_And_Resolve (Expr, Comp_Typ);
1557 end if;
1558
1559 In_Place_Expansion :=
1560 Nkind (Expr) = N_Function_Call
1561 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
1562
1563 -- The initialization expression is a controlled function call.
1564 -- Perform in-place removal of side effects to avoid creating a
1565 -- transient scope, which leads to premature finalization.
1566
1567 -- This in-place expansion is not performed for limited transient
1568 -- objects, because the initialization is already done in place.
1569
1570 if In_Place_Expansion then
1571
1572 -- Suppress the removal of side effects by general analysis,
1573 -- because this behavior is emulated here. This avoids the
1574 -- generation of a transient scope, which leads to out-of-order
1575 -- adjustment and finalization.
1576
1577 Set_No_Side_Effect_Removal (Expr);
1578
1579 -- When the transient component initialization is related to a
1580 -- range or an "others", keep all generated statements within
1581 -- the enclosing loop. This way the controlled function call
1582 -- will be evaluated at each iteration, and its result will be
1583 -- finalized at the end of each iteration.
1584
1585 if In_Loop then
1586 Act_Aggr := Empty;
1587 Act_Stmts := Stmts;
1588
1589 -- Otherwise this is a single component initialization. Hook-
1590 -- related statements are inserted prior to the aggregate.
1591
1592 else
1593 Act_Aggr := N;
1594 Act_Stmts := No_List;
1595 end if;
1596
1597 -- Install all hook-related declarations and prepare the clean
1598 -- up statements.
1599
1600 Process_Transient_Component
1601 (Loc => Loc,
1602 Comp_Typ => Comp_Typ,
1603 Init_Expr => Expr,
1604 Fin_Call => Fin_Call,
1605 Hook_Clear => Hook_Clear,
1606 Aggr => Act_Aggr,
1607 Stmts => Act_Stmts);
1608 end if;
1609
1610 -- Use the noncontrolled component initialization circuitry to
1611 -- assign the result of the function call to the array element.
1612 -- This also performs subaggregate wrapping, tag adjustment, and
1613 -- [deep] adjustment of the array element.
1614
1615 Initialize_Array_Component
1616 (Arr_Comp => Arr_Comp,
1617 Comp_Typ => Comp_Typ,
1618 Init_Expr => Expr,
1619 Stmts => Stmts);
1620
1621 -- At this point the array element is fully initialized. Complete
1622 -- the processing of the controlled array component by finalizing
1623 -- the transient function result.
1624
1625 if In_Place_Expansion then
1626 Process_Transient_Component_Completion
1627 (Loc => Loc,
1628 Aggr => N,
1629 Fin_Call => Fin_Call,
1630 Hook_Clear => Hook_Clear,
1631 Stmts => Stmts);
1632 end if;
1633 end Initialize_Ctrl_Array_Component;
1634
1635 -- Local variables
1636
1637 Stmts : constant List_Id := New_List;
1638
1639 Comp_Typ : Entity_Id := Empty;
1640 Expr_Q : Node_Id;
1641 Indexed_Comp : Node_Id;
1642 Init_Call : Node_Id;
1643 New_Indexes : List_Id;
1644
1645 -- Start of processing for Gen_Assign
1646
1647 begin
1648 if No (Indexes) then
1649 New_Indexes := New_List;
1650 else
1651 New_Indexes := New_Copy_List_Tree (Indexes);
1652 end if;
1653
1654 Append_To (New_Indexes, Ind);
1655
1656 if Present (Next_Index (Index)) then
1657 return
1658 Add_Loop_Actions (
1659 Build_Array_Aggr_Code
1660 (N => Expr,
1661 Ctype => Ctype,
1662 Index => Next_Index (Index),
1663 Into => Into,
1664 Scalar_Comp => Scalar_Comp,
1665 Indexes => New_Indexes));
1666 end if;
1667
1668 -- If we get here then we are at a bottom-level (sub-)aggregate
1669
1670 Indexed_Comp :=
1671 Checks_Off
1672 (Make_Indexed_Component (Loc,
1673 Prefix => New_Copy_Tree (Into),
1674 Expressions => New_Indexes));
1675
1676 Set_Assignment_OK (Indexed_Comp);
1677
1678 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1679 -- is not present (and therefore we also initialize Expr_Q to empty).
1680
1681 if No (Expr) then
1682 Expr_Q := Empty;
1683 elsif Nkind (Expr) = N_Qualified_Expression then
1684 Expr_Q := Expression (Expr);
1685 else
1686 Expr_Q := Expr;
1687 end if;
1688
1689 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1690 Comp_Typ := Component_Type (Etype (N));
1691 pragma Assert (Comp_Typ = Ctype); -- AI-287
1692
1693 elsif Present (Next (First (New_Indexes))) then
1694
1695 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1696 -- component because we have received the component type in
1697 -- the formal parameter Ctype.
1698
1699 -- ??? Some assert pragmas have been added to check if this new
1700 -- formal can be used to replace this code in all cases.
1701
1702 if Present (Expr) then
1703
1704 -- This is a multidimensional array. Recover the component type
1705 -- from the outermost aggregate, because subaggregates do not
1706 -- have an assigned type.
1707
1708 declare
1709 P : Node_Id;
1710
1711 begin
1712 P := Parent (Expr);
1713 while Present (P) loop
1714 if Nkind (P) = N_Aggregate
1715 and then Present (Etype (P))
1716 then
1717 Comp_Typ := Component_Type (Etype (P));
1718 exit;
1719
1720 else
1721 P := Parent (P);
1722 end if;
1723 end loop;
1724
1725 pragma Assert (Comp_Typ = Ctype); -- AI-287
1726 end;
1727 end if;
1728 end if;
1729
1730 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1731 -- default initialized components (otherwise Expr_Q is not present).
1732
1733 if Present (Expr_Q)
1734 and then Nkind (Expr_Q) in N_Aggregate | N_Extension_Aggregate
1735 then
1736 -- At this stage the Expression may not have been analyzed yet
1737 -- because the array aggregate code has not been updated to use
1738 -- the Expansion_Delayed flag and avoid analysis altogether to
1739 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1740 -- the analysis of non-array aggregates now in order to get the
1741 -- value of Expansion_Delayed flag for the inner aggregate ???
1742
1743 -- In the case of an iterated component association, the analysis
1744 -- of the generated loop will analyze the expression in the
1745 -- proper context, in which the loop parameter is visible.
1746
1747 if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then
1748 if Nkind (Parent (Expr_Q)) = N_Iterated_Component_Association
1749 or else Nkind (Parent (Parent ((Expr_Q)))) =
1750 N_Iterated_Component_Association
1751 then
1752 null;
1753 else
1754 Analyze_And_Resolve (Expr_Q, Comp_Typ);
1755 end if;
1756 end if;
1757
1758 if Is_Delayed_Aggregate (Expr_Q) then
1759
1760 -- This is either a subaggregate of a multidimensional array,
1761 -- or a component of an array type whose component type is
1762 -- also an array. In the latter case, the expression may have
1763 -- component associations that provide different bounds from
1764 -- those of the component type, and sliding must occur. Instead
1765 -- of decomposing the current aggregate assignment, force the
1766 -- reanalysis of the assignment, so that a temporary will be
1767 -- generated in the usual fashion, and sliding will take place.
1768
1769 if Nkind (Parent (N)) = N_Assignment_Statement
1770 and then Is_Array_Type (Comp_Typ)
1771 and then Present (Component_Associations (Expr_Q))
1772 and then Must_Slide (Comp_Typ, Etype (Expr_Q))
1773 then
1774 Set_Expansion_Delayed (Expr_Q, False);
1775 Set_Analyzed (Expr_Q, False);
1776
1777 else
1778 return
1779 Add_Loop_Actions (
1780 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1781 end if;
1782 end if;
1783 end if;
1784
1785 if Present (Expr) then
1786
1787 -- Handle an initialization expression of a controlled type in
1788 -- case it denotes a function call. In general such a scenario
1789 -- will produce a transient scope, but this will lead to wrong
1790 -- order of initialization, adjustment, and finalization in the
1791 -- context of aggregates.
1792
1793 -- Target (1) := Ctrl_Func_Call;
1794
1795 -- begin -- scope
1796 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1797 -- Target (1) := Trans_Obj;
1798 -- Finalize (Trans_Obj);
1799 -- end;
1800 -- Target (1)._tag := ...;
1801 -- Adjust (Target (1));
1802
1803 -- In the example above, the call to Finalize occurs too early
1804 -- and as a result it may leave the array component in a bad
1805 -- state. Finalization of the transient object should really
1806 -- happen after adjustment.
1807
1808 -- To avoid this scenario, perform in-place side-effect removal
1809 -- of the function call. This eliminates the transient property
1810 -- of the function result and ensures correct order of actions.
1811
1812 -- Res : ... := Ctrl_Func_Call;
1813 -- Target (1) := Res;
1814 -- Target (1)._tag := ...;
1815 -- Adjust (Target (1));
1816 -- Finalize (Res);
1817
1818 if Present (Comp_Typ)
1819 and then Needs_Finalization (Comp_Typ)
1820 and then Nkind (Expr) /= N_Aggregate
1821 then
1822 Initialize_Ctrl_Array_Component
1823 (Arr_Comp => Indexed_Comp,
1824 Comp_Typ => Comp_Typ,
1825 Init_Expr => Expr,
1826 Stmts => Stmts);
1827
1828 -- Otherwise perform simple component initialization
1829
1830 else
1831 Initialize_Array_Component
1832 (Arr_Comp => Indexed_Comp,
1833 Comp_Typ => Comp_Typ,
1834 Init_Expr => Expr,
1835 Stmts => Stmts);
1836 end if;
1837
1838 -- Ada 2005 (AI-287): In case of default initialized component, call
1839 -- the initialization subprogram associated with the component type.
1840 -- If the component type is an access type, add an explicit null
1841 -- assignment, because for the back-end there is an initialization
1842 -- present for the whole aggregate, and no default initialization
1843 -- will take place.
1844
1845 -- In addition, if the component type is controlled, we must call
1846 -- its Initialize procedure explicitly, because there is no explicit
1847 -- object creation that will invoke it otherwise.
1848
1849 else
1850 if Present (Base_Init_Proc (Base_Type (Ctype)))
1851 or else Has_Task (Base_Type (Ctype))
1852 then
1853 Append_List_To (Stmts,
1854 Build_Initialization_Call (Loc,
1855 Id_Ref => Indexed_Comp,
1856 Typ => Ctype,
1857 With_Default_Init => True));
1858
1859 -- If the component type has invariants, add an invariant
1860 -- check after the component is default-initialized. It will
1861 -- be analyzed and resolved before the code for initialization
1862 -- of other components.
1863
1864 if Has_Invariants (Ctype) then
1865 Set_Etype (Indexed_Comp, Ctype);
1866 Append_To (Stmts, Make_Invariant_Call (Indexed_Comp));
1867 end if;
1868
1869 elsif Is_Access_Type (Ctype) then
1870 Append_To (Stmts,
1871 Make_Assignment_Statement (Loc,
1872 Name => New_Copy_Tree (Indexed_Comp),
1873 Expression => Make_Null (Loc)));
1874 end if;
1875
1876 if Needs_Finalization (Ctype) then
1877 Init_Call :=
1878 Make_Init_Call
1879 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1880 Typ => Ctype);
1881
1882 -- Guard against a missing [Deep_]Initialize when the component
1883 -- type was not properly frozen.
1884
1885 if Present (Init_Call) then
1886 Append_To (Stmts, Init_Call);
1887 end if;
1888 end if;
1889
1890 -- If Default_Initial_Condition applies to the component type,
1891 -- add a DIC check after the component is default-initialized,
1892 -- as well as after an Initialize procedure is called, in the
1893 -- case of components of a controlled type. It will be analyzed
1894 -- and resolved before the code for initialization of other
1895 -- components.
1896
1897 -- Theoretically this might also be needed for cases where Expr
1898 -- is not empty, but a default init still applies, such as for
1899 -- Default_Value cases, in which case we won't get here. ???
1900
1901 if Has_DIC (Ctype) and then Present (DIC_Procedure (Ctype)) then
1902 Append_To (Stmts,
1903 Build_DIC_Call (Loc, New_Copy_Tree (Indexed_Comp), Ctype));
1904 end if;
1905 end if;
1906
1907 return Add_Loop_Actions (Stmts);
1908 end Gen_Assign;
1909
1910 --------------
1911 -- Gen_Loop --
1912 --------------
1913
1914 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1915 Is_Iterated_Component : constant Boolean :=
1916 Nkind (Parent (Expr)) = N_Iterated_Component_Association;
1917
1918 L_J : Node_Id;
1919
1920 L_L : Node_Id;
1921 -- Index_Base'(L)
1922
1923 L_H : Node_Id;
1924 -- Index_Base'(H)
1925
1926 L_Range : Node_Id;
1927 -- Index_Base'(L) .. Index_Base'(H)
1928
1929 L_Iteration_Scheme : Node_Id;
1930 -- L_J in Index_Base'(L) .. Index_Base'(H)
1931
1932 L_Body : List_Id;
1933 -- The statements to execute in the loop
1934
1935 S : constant List_Id := New_List;
1936 -- List of statements
1937
1938 Tcopy : Node_Id;
1939 -- Copy of expression tree, used for checking purposes
1940
1941 begin
1942 -- If loop bounds define an empty range return the null statement
1943
1944 if Empty_Range (L, H) then
1945 Append_To (S, Make_Null_Statement (Loc));
1946
1947 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1948 -- default initialized component.
1949
1950 if No (Expr) then
1951 null;
1952
1953 else
1954 -- The expression must be type-checked even though no component
1955 -- of the aggregate will have this value. This is done only for
1956 -- actual components of the array, not for subaggregates. Do
1957 -- the check on a copy, because the expression may be shared
1958 -- among several choices, some of which might be non-null.
1959
1960 if Present (Etype (N))
1961 and then Is_Array_Type (Etype (N))
1962 and then No (Next_Index (Index))
1963 then
1964 Expander_Mode_Save_And_Set (False);
1965 Tcopy := New_Copy_Tree (Expr);
1966 Set_Parent (Tcopy, N);
1967
1968 -- For iterated_component_association analyze and resolve
1969 -- the expression with name of the index parameter visible.
1970 -- To manipulate scopes, we use entity of the implicit loop.
1971
1972 if Is_Iterated_Component then
1973 declare
1974 Index_Parameter : constant Entity_Id :=
1975 Defining_Identifier (Parent (Expr));
1976 begin
1977 Push_Scope (Scope (Index_Parameter));
1978 Enter_Name (Index_Parameter);
1979 Analyze_And_Resolve
1980 (Tcopy, Component_Type (Etype (N)));
1981 End_Scope;
1982 end;
1983
1984 -- For ordinary component association, just analyze and
1985 -- resolve the expression.
1986
1987 else
1988 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1989 end if;
1990
1991 Expander_Mode_Restore;
1992 end if;
1993 end if;
1994
1995 return S;
1996
1997 -- If loop bounds are the same then generate an assignment, unless
1998 -- the parent construct is an Iterated_Component_Association.
1999
2000 elsif Equal (L, H) and then not Is_Iterated_Component then
2001 return Gen_Assign (New_Copy_Tree (L), Expr);
2002
2003 -- If H - L <= 2 then generate a sequence of assignments when we are
2004 -- processing the bottom most aggregate and it contains scalar
2005 -- components.
2006
2007 elsif No (Next_Index (Index))
2008 and then Scalar_Comp
2009 and then Local_Compile_Time_Known_Value (L)
2010 and then Local_Compile_Time_Known_Value (H)
2011 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
2012 and then not Is_Iterated_Component
2013 then
2014 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
2015 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
2016
2017 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
2018 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
2019 end if;
2020
2021 return S;
2022 end if;
2023
2024 -- Otherwise construct the loop, starting with the loop index L_J
2025
2026 if Is_Iterated_Component then
2027 L_J :=
2028 Make_Defining_Identifier (Loc,
2029 Chars => (Chars (Defining_Identifier (Parent (Expr)))));
2030
2031 else
2032 L_J := Make_Temporary (Loc, 'J', L);
2033 end if;
2034
2035 -- Construct "L .. H" in Index_Base. We use a qualified expression
2036 -- for the bound to convert to the index base, but we don't need
2037 -- to do that if we already have the base type at hand.
2038
2039 if Etype (L) = Index_Base then
2040 L_L := L;
2041 else
2042 L_L :=
2043 Make_Qualified_Expression (Loc,
2044 Subtype_Mark => Index_Base_Name,
2045 Expression => New_Copy_Tree (L));
2046 end if;
2047
2048 if Etype (H) = Index_Base then
2049 L_H := H;
2050 else
2051 L_H :=
2052 Make_Qualified_Expression (Loc,
2053 Subtype_Mark => Index_Base_Name,
2054 Expression => New_Copy_Tree (H));
2055 end if;
2056
2057 L_Range :=
2058 Make_Range (Loc,
2059 Low_Bound => L_L,
2060 High_Bound => L_H);
2061
2062 -- Construct "for L_J in Index_Base range L .. H"
2063
2064 L_Iteration_Scheme :=
2065 Make_Iteration_Scheme
2066 (Loc,
2067 Loop_Parameter_Specification =>
2068 Make_Loop_Parameter_Specification
2069 (Loc,
2070 Defining_Identifier => L_J,
2071 Discrete_Subtype_Definition => L_Range));
2072
2073 -- Construct the statements to execute in the loop body
2074
2075 L_Body :=
2076 Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr, In_Loop => True);
2077
2078 -- Construct the final loop
2079
2080 Append_To (S,
2081 Make_Implicit_Loop_Statement
2082 (Node => N,
2083 Identifier => Empty,
2084 Iteration_Scheme => L_Iteration_Scheme,
2085 Statements => L_Body));
2086
2087 -- A small optimization: if the aggregate is initialized with a box
2088 -- and the component type has no initialization procedure, remove the
2089 -- useless empty loop.
2090
2091 if Nkind (First (S)) = N_Loop_Statement
2092 and then Is_Empty_List (Statements (First (S)))
2093 then
2094 return New_List (Make_Null_Statement (Loc));
2095 else
2096 return S;
2097 end if;
2098 end Gen_Loop;
2099
2100 ---------------
2101 -- Gen_While --
2102 ---------------
2103
2104 -- The code built is
2105
2106 -- W_J : Index_Base := L;
2107 -- while W_J < H loop
2108 -- W_J := Index_Base'Succ (W);
2109 -- L_Body;
2110 -- end loop;
2111
2112 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
2113 W_J : Node_Id;
2114
2115 W_Decl : Node_Id;
2116 -- W_J : Base_Type := L;
2117
2118 W_Iteration_Scheme : Node_Id;
2119 -- while W_J < H
2120
2121 W_Index_Succ : Node_Id;
2122 -- Index_Base'Succ (J)
2123
2124 W_Increment : Node_Id;
2125 -- W_J := Index_Base'Succ (W)
2126
2127 W_Body : constant List_Id := New_List;
2128 -- The statements to execute in the loop
2129
2130 S : constant List_Id := New_List;
2131 -- list of statement
2132
2133 begin
2134 -- If loop bounds define an empty range or are equal return null
2135
2136 if Empty_Range (L, H) or else Equal (L, H) then
2137 Append_To (S, Make_Null_Statement (Loc));
2138 return S;
2139 end if;
2140
2141 -- Build the decl of W_J
2142
2143 W_J := Make_Temporary (Loc, 'J', L);
2144 W_Decl :=
2145 Make_Object_Declaration
2146 (Loc,
2147 Defining_Identifier => W_J,
2148 Object_Definition => Index_Base_Name,
2149 Expression => L);
2150
2151 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
2152 -- that in this particular case L is a fresh Expr generated by
2153 -- Add which we are the only ones to use.
2154
2155 Append_To (S, W_Decl);
2156
2157 -- Construct " while W_J < H"
2158
2159 W_Iteration_Scheme :=
2160 Make_Iteration_Scheme
2161 (Loc,
2162 Condition => Make_Op_Lt
2163 (Loc,
2164 Left_Opnd => New_Occurrence_Of (W_J, Loc),
2165 Right_Opnd => New_Copy_Tree (H)));
2166
2167 -- Construct the statements to execute in the loop body
2168
2169 W_Index_Succ :=
2170 Make_Attribute_Reference
2171 (Loc,
2172 Prefix => Index_Base_Name,
2173 Attribute_Name => Name_Succ,
2174 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
2175
2176 W_Increment :=
2177 Make_OK_Assignment_Statement
2178 (Loc,
2179 Name => New_Occurrence_Of (W_J, Loc),
2180 Expression => W_Index_Succ);
2181
2182 Append_To (W_Body, W_Increment);
2183
2184 Append_List_To (W_Body,
2185 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr, In_Loop => True));
2186
2187 -- Construct the final loop
2188
2189 Append_To (S,
2190 Make_Implicit_Loop_Statement
2191 (Node => N,
2192 Identifier => Empty,
2193 Iteration_Scheme => W_Iteration_Scheme,
2194 Statements => W_Body));
2195
2196 return S;
2197 end Gen_While;
2198
2199 --------------------
2200 -- Get_Assoc_Expr --
2201 --------------------
2202
2203 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
2204 Typ : constant Entity_Id := Base_Type (Etype (N));
2205
2206 begin
2207 if Box_Present (Assoc) then
2208 if Is_Scalar_Type (Ctype) then
2209 if Present (Default_Aspect_Component_Value (Typ)) then
2210 return Default_Aspect_Component_Value (Typ);
2211 elsif Present (Default_Aspect_Value (Ctype)) then
2212 return Default_Aspect_Value (Ctype);
2213 else
2214 return Empty;
2215 end if;
2216
2217 else
2218 return Empty;
2219 end if;
2220
2221 else
2222 return Expression (Assoc);
2223 end if;
2224 end Get_Assoc_Expr;
2225
2226 ---------------------
2227 -- Index_Base_Name --
2228 ---------------------
2229
2230 function Index_Base_Name return Node_Id is
2231 begin
2232 return New_Occurrence_Of (Index_Base, Sloc (N));
2233 end Index_Base_Name;
2234
2235 ------------------------------------
2236 -- Local_Compile_Time_Known_Value --
2237 ------------------------------------
2238
2239 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
2240 begin
2241 return Compile_Time_Known_Value (E)
2242 or else
2243 (Nkind (E) = N_Attribute_Reference
2244 and then Attribute_Name (E) = Name_Val
2245 and then Compile_Time_Known_Value (First (Expressions (E))));
2246 end Local_Compile_Time_Known_Value;
2247
2248 ----------------------
2249 -- Local_Expr_Value --
2250 ----------------------
2251
2252 function Local_Expr_Value (E : Node_Id) return Uint is
2253 begin
2254 if Compile_Time_Known_Value (E) then
2255 return Expr_Value (E);
2256 else
2257 return Expr_Value (First (Expressions (E)));
2258 end if;
2259 end Local_Expr_Value;
2260
2261 -- Local variables
2262
2263 New_Code : constant List_Id := New_List;
2264
2265 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
2266 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
2267 -- The aggregate bounds of this specific subaggregate. Note that if the
2268 -- code generated by Build_Array_Aggr_Code is executed then these bounds
2269 -- are OK. Otherwise a Constraint_Error would have been raised.
2270
2271 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
2272 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
2273 -- After Duplicate_Subexpr these are side-effect free
2274
2275 Assoc : Node_Id;
2276 Choice : Node_Id;
2277 Expr : Node_Id;
2278 High : Node_Id;
2279 Low : Node_Id;
2280 Typ : Entity_Id;
2281
2282 Nb_Choices : Nat := 0;
2283 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
2284 -- Used to sort all the different choice values
2285
2286 Nb_Elements : Int;
2287 -- Number of elements in the positional aggregate
2288
2289 Others_Assoc : Node_Id := Empty;
2290
2291 -- Start of processing for Build_Array_Aggr_Code
2292
2293 begin
2294 -- First before we start, a special case. if we have a bit packed
2295 -- array represented as a modular type, then clear the value to
2296 -- zero first, to ensure that unused bits are properly cleared.
2297
2298 Typ := Etype (N);
2299
2300 if Present (Typ)
2301 and then Is_Bit_Packed_Array (Typ)
2302 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
2303 then
2304 declare
2305 Zero : constant Node_Id := Make_Integer_Literal (Loc, Uint_0);
2306 begin
2307 Analyze_And_Resolve (Zero, Packed_Array_Impl_Type (Typ));
2308 Append_To (New_Code,
2309 Make_Assignment_Statement (Loc,
2310 Name => New_Copy_Tree (Into),
2311 Expression => Unchecked_Convert_To (Typ, Zero)));
2312 end;
2313 end if;
2314
2315 -- If the component type contains tasks, we need to build a Master
2316 -- entity in the current scope, because it will be needed if build-
2317 -- in-place functions are called in the expanded code.
2318
2319 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
2320 Build_Master_Entity (Defining_Identifier (Parent (N)));
2321 end if;
2322
2323 -- STEP 1: Process component associations
2324
2325 -- For those associations that may generate a loop, initialize
2326 -- Loop_Actions to collect inserted actions that may be crated.
2327
2328 -- Skip this if no component associations
2329
2330 if No (Expressions (N)) then
2331
2332 -- STEP 1 (a): Sort the discrete choices
2333
2334 Assoc := First (Component_Associations (N));
2335 while Present (Assoc) loop
2336 Choice := First (Choice_List (Assoc));
2337 while Present (Choice) loop
2338 if Nkind (Choice) = N_Others_Choice then
2339 Others_Assoc := Assoc;
2340 exit;
2341 end if;
2342
2343 Get_Index_Bounds (Choice, Low, High);
2344
2345 if Low /= High then
2346 Set_Loop_Actions (Assoc, New_List);
2347 end if;
2348
2349 Nb_Choices := Nb_Choices + 1;
2350
2351 Table (Nb_Choices) :=
2352 (Choice_Lo => Low,
2353 Choice_Hi => High,
2354 Choice_Node => Get_Assoc_Expr (Assoc));
2355
2356 Next (Choice);
2357 end loop;
2358
2359 Next (Assoc);
2360 end loop;
2361
2362 -- If there is more than one set of choices these must be static
2363 -- and we can therefore sort them. Remember that Nb_Choices does not
2364 -- account for an others choice.
2365
2366 if Nb_Choices > 1 then
2367 Sort_Case_Table (Table);
2368 end if;
2369
2370 -- STEP 1 (b): take care of the whole set of discrete choices
2371
2372 for J in 1 .. Nb_Choices loop
2373 Low := Table (J).Choice_Lo;
2374 High := Table (J).Choice_Hi;
2375 Expr := Table (J).Choice_Node;
2376 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2377 end loop;
2378
2379 -- STEP 1 (c): generate the remaining loops to cover others choice
2380 -- We don't need to generate loops over empty gaps, but if there is
2381 -- a single empty range we must analyze the expression for semantics
2382
2383 if Present (Others_Assoc) then
2384 declare
2385 First : Boolean := True;
2386 Dup_Expr : Node_Id;
2387
2388 begin
2389 for J in 0 .. Nb_Choices loop
2390 if J = 0 then
2391 Low := Aggr_Low;
2392 else
2393 Low := Add (1, To => Table (J).Choice_Hi);
2394 end if;
2395
2396 if J = Nb_Choices then
2397 High := Aggr_High;
2398 else
2399 High := Add (-1, To => Table (J + 1).Choice_Lo);
2400 end if;
2401
2402 -- If this is an expansion within an init proc, make
2403 -- sure that discriminant references are replaced by
2404 -- the corresponding discriminal.
2405
2406 if Inside_Init_Proc then
2407 if Is_Entity_Name (Low)
2408 and then Ekind (Entity (Low)) = E_Discriminant
2409 then
2410 Set_Entity (Low, Discriminal (Entity (Low)));
2411 end if;
2412
2413 if Is_Entity_Name (High)
2414 and then Ekind (Entity (High)) = E_Discriminant
2415 then
2416 Set_Entity (High, Discriminal (Entity (High)));
2417 end if;
2418 end if;
2419
2420 if First
2421 or else not Empty_Range (Low, High)
2422 then
2423 First := False;
2424
2425 -- Duplicate the expression in case we will be generating
2426 -- several loops. As a result the expression is no longer
2427 -- shared between the loops and is reevaluated for each
2428 -- such loop.
2429
2430 Expr := Get_Assoc_Expr (Others_Assoc);
2431 Dup_Expr := New_Copy_Tree (Expr);
2432 Set_Parent (Dup_Expr, Parent (Expr));
2433
2434 Set_Loop_Actions (Others_Assoc, New_List);
2435 Append_List
2436 (Gen_Loop (Low, High, Dup_Expr), To => New_Code);
2437 end if;
2438 end loop;
2439 end;
2440 end if;
2441
2442 -- STEP 2: Process positional components
2443
2444 else
2445 -- STEP 2 (a): Generate the assignments for each positional element
2446 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2447 -- Aggr_L is analyzed and Add wants an analyzed expression.
2448
2449 Expr := First (Expressions (N));
2450 Nb_Elements := -1;
2451 while Present (Expr) loop
2452 Nb_Elements := Nb_Elements + 1;
2453 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2454 To => New_Code);
2455 Next (Expr);
2456 end loop;
2457
2458 -- STEP 2 (b): Generate final loop if an others choice is present.
2459 -- Here Nb_Elements gives the offset of the last positional element.
2460
2461 if Present (Component_Associations (N)) then
2462 Assoc := Last (Component_Associations (N));
2463
2464 if Nkind (Assoc) = N_Iterated_Component_Association then
2465 -- Ada 2020: generate a loop to have a proper scope for
2466 -- the identifier that typically appears in the expression.
2467 -- The lower bound of the loop is the position after all
2468 -- previous positional components.
2469
2470 Append_List (Gen_Loop (Add (Nb_Elements + 1, To => Aggr_L),
2471 Aggr_High,
2472 Expression (Assoc)),
2473 To => New_Code);
2474 else
2475 -- Ada 2005 (AI-287)
2476
2477 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2478 Aggr_High,
2479 Get_Assoc_Expr (Assoc)),
2480 To => New_Code);
2481 end if;
2482 end if;
2483 end if;
2484
2485 return New_Code;
2486 end Build_Array_Aggr_Code;
2487
2488 ----------------------------
2489 -- Build_Record_Aggr_Code --
2490 ----------------------------
2491
2492 function Build_Record_Aggr_Code
2493 (N : Node_Id;
2494 Typ : Entity_Id;
2495 Lhs : Node_Id) return List_Id
2496 is
2497 Loc : constant Source_Ptr := Sloc (N);
2498 L : constant List_Id := New_List;
2499 N_Typ : constant Entity_Id := Etype (N);
2500
2501 Comp : Node_Id;
2502 Instr : Node_Id;
2503 Ref : Node_Id;
2504 Target : Entity_Id;
2505 Comp_Type : Entity_Id;
2506 Selector : Entity_Id;
2507 Comp_Expr : Node_Id;
2508 Expr_Q : Node_Id;
2509
2510 -- If this is an internal aggregate, the External_Final_List is an
2511 -- expression for the controller record of the enclosing type.
2512
2513 -- If the current aggregate has several controlled components, this
2514 -- expression will appear in several calls to attach to the finali-
2515 -- zation list, and it must not be shared.
2516
2517 Ancestor_Is_Expression : Boolean := False;
2518 Ancestor_Is_Subtype_Mark : Boolean := False;
2519
2520 Init_Typ : Entity_Id := Empty;
2521
2522 Finalization_Done : Boolean := False;
2523 -- True if Generate_Finalization_Actions has already been called; calls
2524 -- after the first do nothing.
2525
2526 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2527 -- Returns the value that the given discriminant of an ancestor type
2528 -- should receive (in the absence of a conflict with the value provided
2529 -- by an ancestor part of an extension aggregate).
2530
2531 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2532 -- Check that each of the discriminant values defined by the ancestor
2533 -- part of an extension aggregate match the corresponding values
2534 -- provided by either an association of the aggregate or by the
2535 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2536
2537 function Compatible_Int_Bounds
2538 (Agg_Bounds : Node_Id;
2539 Typ_Bounds : Node_Id) return Boolean;
2540 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2541 -- assumed that both bounds are integer ranges.
2542
2543 procedure Generate_Finalization_Actions;
2544 -- Deal with the various controlled type data structure initializations
2545 -- (but only if it hasn't been done already).
2546
2547 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2548 -- Returns the first discriminant association in the constraint
2549 -- associated with T, if any, otherwise returns Empty.
2550
2551 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2552 -- If the ancestor part is an unconstrained type and further ancestors
2553 -- do not provide discriminants for it, check aggregate components for
2554 -- values of the discriminants.
2555
2556 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2557 -- If Typ is derived, and constrains discriminants of the parent type,
2558 -- these discriminants are not components of the aggregate, and must be
2559 -- initialized. The assignments are appended to List. The same is done
2560 -- if Typ derives fron an already constrained subtype of a discriminated
2561 -- parent type.
2562
2563 procedure Init_Stored_Discriminants;
2564 -- If the type is derived and has inherited discriminants, generate
2565 -- explicit assignments for each, using the store constraint of the
2566 -- type. Note that both visible and stored discriminants must be
2567 -- initialized in case the derived type has some renamed and some
2568 -- constrained discriminants.
2569
2570 procedure Init_Visible_Discriminants;
2571 -- If type has discriminants, retrieve their values from aggregate,
2572 -- and generate explicit assignments for each. This does not include
2573 -- discriminants inherited from ancestor, which are handled above.
2574 -- The type of the aggregate is a subtype created ealier using the
2575 -- given values of the discriminant components of the aggregate.
2576
2577 procedure Initialize_Ctrl_Record_Component
2578 (Rec_Comp : Node_Id;
2579 Comp_Typ : Entity_Id;
2580 Init_Expr : Node_Id;
2581 Stmts : List_Id);
2582 -- Perform the initialization of controlled record component Rec_Comp.
2583 -- Comp_Typ is the component type. Init_Expr is the initialization
2584 -- expression for the record component. Hook-related declarations are
2585 -- inserted prior to aggregate N using Insert_Action. All remaining
2586 -- generated code is added to list Stmts.
2587
2588 procedure Initialize_Record_Component
2589 (Rec_Comp : Node_Id;
2590 Comp_Typ : Entity_Id;
2591 Init_Expr : Node_Id;
2592 Stmts : List_Id);
2593 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2594 -- is the component type. Init_Expr is the initialization expression
2595 -- of the record component. All generated code is added to list Stmts.
2596
2597 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2598 -- Check whether Bounds is a range node and its lower and higher bounds
2599 -- are integers literals.
2600
2601 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2602 -- If the aggregate contains a self-reference, traverse each expression
2603 -- to replace a possible self-reference with a reference to the proper
2604 -- component of the target of the assignment.
2605
2606 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2607 -- If default expression of a component mentions a discriminant of the
2608 -- type, it must be rewritten as the discriminant of the target object.
2609
2610 ---------------------------------
2611 -- Ancestor_Discriminant_Value --
2612 ---------------------------------
2613
2614 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2615 Assoc : Node_Id;
2616 Assoc_Elmt : Elmt_Id;
2617 Aggr_Comp : Entity_Id;
2618 Corresp_Disc : Entity_Id;
2619 Current_Typ : Entity_Id := Base_Type (Typ);
2620 Parent_Typ : Entity_Id;
2621 Parent_Disc : Entity_Id;
2622 Save_Assoc : Node_Id := Empty;
2623
2624 begin
2625 -- First check any discriminant associations to see if any of them
2626 -- provide a value for the discriminant.
2627
2628 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2629 Assoc := First (Component_Associations (N));
2630 while Present (Assoc) loop
2631 Aggr_Comp := Entity (First (Choices (Assoc)));
2632
2633 if Ekind (Aggr_Comp) = E_Discriminant then
2634 Save_Assoc := Expression (Assoc);
2635
2636 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2637 while Present (Corresp_Disc) loop
2638
2639 -- If found a corresponding discriminant then return the
2640 -- value given in the aggregate. (Note: this is not
2641 -- correct in the presence of side effects. ???)
2642
2643 if Disc = Corresp_Disc then
2644 return Duplicate_Subexpr (Expression (Assoc));
2645 end if;
2646
2647 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2648 end loop;
2649 end if;
2650
2651 Next (Assoc);
2652 end loop;
2653 end if;
2654
2655 -- No match found in aggregate, so chain up parent types to find
2656 -- a constraint that defines the value of the discriminant.
2657
2658 Parent_Typ := Etype (Current_Typ);
2659 while Current_Typ /= Parent_Typ loop
2660 if Has_Discriminants (Parent_Typ)
2661 and then not Has_Unknown_Discriminants (Parent_Typ)
2662 then
2663 Parent_Disc := First_Discriminant (Parent_Typ);
2664
2665 -- We either get the association from the subtype indication
2666 -- of the type definition itself, or from the discriminant
2667 -- constraint associated with the type entity (which is
2668 -- preferable, but it's not always present ???)
2669
2670 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2671 then
2672 Assoc := Get_Constraint_Association (Current_Typ);
2673 Assoc_Elmt := No_Elmt;
2674 else
2675 Assoc_Elmt :=
2676 First_Elmt (Discriminant_Constraint (Current_Typ));
2677 Assoc := Node (Assoc_Elmt);
2678 end if;
2679
2680 -- Traverse the discriminants of the parent type looking
2681 -- for one that corresponds.
2682
2683 while Present (Parent_Disc) and then Present (Assoc) loop
2684 Corresp_Disc := Parent_Disc;
2685 while Present (Corresp_Disc)
2686 and then Disc /= Corresp_Disc
2687 loop
2688 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2689 end loop;
2690
2691 if Disc = Corresp_Disc then
2692 if Nkind (Assoc) = N_Discriminant_Association then
2693 Assoc := Expression (Assoc);
2694 end if;
2695
2696 -- If the located association directly denotes
2697 -- a discriminant, then use the value of a saved
2698 -- association of the aggregate. This is an approach
2699 -- used to handle certain cases involving multiple
2700 -- discriminants mapped to a single discriminant of
2701 -- a descendant. It's not clear how to locate the
2702 -- appropriate discriminant value for such cases. ???
2703
2704 if Is_Entity_Name (Assoc)
2705 and then Ekind (Entity (Assoc)) = E_Discriminant
2706 then
2707 Assoc := Save_Assoc;
2708 end if;
2709
2710 return Duplicate_Subexpr (Assoc);
2711 end if;
2712
2713 Next_Discriminant (Parent_Disc);
2714
2715 if No (Assoc_Elmt) then
2716 Next (Assoc);
2717
2718 else
2719 Next_Elmt (Assoc_Elmt);
2720
2721 if Present (Assoc_Elmt) then
2722 Assoc := Node (Assoc_Elmt);
2723 else
2724 Assoc := Empty;
2725 end if;
2726 end if;
2727 end loop;
2728 end if;
2729
2730 Current_Typ := Parent_Typ;
2731 Parent_Typ := Etype (Current_Typ);
2732 end loop;
2733
2734 -- In some cases there's no ancestor value to locate (such as
2735 -- when an ancestor part given by an expression defines the
2736 -- discriminant value).
2737
2738 return Empty;
2739 end Ancestor_Discriminant_Value;
2740
2741 ----------------------------------
2742 -- Check_Ancestor_Discriminants --
2743 ----------------------------------
2744
2745 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2746 Discr : Entity_Id;
2747 Disc_Value : Node_Id;
2748 Cond : Node_Id;
2749
2750 begin
2751 Discr := First_Discriminant (Base_Type (Anc_Typ));
2752 while Present (Discr) loop
2753 Disc_Value := Ancestor_Discriminant_Value (Discr);
2754
2755 if Present (Disc_Value) then
2756 Cond := Make_Op_Ne (Loc,
2757 Left_Opnd =>
2758 Make_Selected_Component (Loc,
2759 Prefix => New_Copy_Tree (Target),
2760 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2761 Right_Opnd => Disc_Value);
2762
2763 Append_To (L,
2764 Make_Raise_Constraint_Error (Loc,
2765 Condition => Cond,
2766 Reason => CE_Discriminant_Check_Failed));
2767 end if;
2768
2769 Next_Discriminant (Discr);
2770 end loop;
2771 end Check_Ancestor_Discriminants;
2772
2773 ---------------------------
2774 -- Compatible_Int_Bounds --
2775 ---------------------------
2776
2777 function Compatible_Int_Bounds
2778 (Agg_Bounds : Node_Id;
2779 Typ_Bounds : Node_Id) return Boolean
2780 is
2781 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2782 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2783 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2784 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2785 begin
2786 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2787 end Compatible_Int_Bounds;
2788
2789 -----------------------------------
2790 -- Generate_Finalization_Actions --
2791 -----------------------------------
2792
2793 procedure Generate_Finalization_Actions is
2794 begin
2795 -- Do the work only the first time this is called
2796
2797 if Finalization_Done then
2798 return;
2799 end if;
2800
2801 Finalization_Done := True;
2802
2803 -- Determine the external finalization list. It is either the
2804 -- finalization list of the outer scope or the one coming from an
2805 -- outer aggregate. When the target is not a temporary, the proper
2806 -- scope is the scope of the target rather than the potentially
2807 -- transient current scope.
2808
2809 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2810 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2811 Set_Assignment_OK (Ref);
2812
2813 Append_To (L,
2814 Make_Procedure_Call_Statement (Loc,
2815 Name =>
2816 New_Occurrence_Of
2817 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2818 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2819 end if;
2820 end Generate_Finalization_Actions;
2821
2822 --------------------------------
2823 -- Get_Constraint_Association --
2824 --------------------------------
2825
2826 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2827 Indic : Node_Id;
2828 Typ : Entity_Id;
2829
2830 begin
2831 Typ := T;
2832
2833 -- If type is private, get constraint from full view. This was
2834 -- previously done in an instance context, but is needed whenever
2835 -- the ancestor part has a discriminant, possibly inherited through
2836 -- multiple derivations.
2837
2838 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2839 Typ := Full_View (Typ);
2840 end if;
2841
2842 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2843
2844 -- Verify that the subtype indication carries a constraint
2845
2846 if Nkind (Indic) = N_Subtype_Indication
2847 and then Present (Constraint (Indic))
2848 then
2849 return First (Constraints (Constraint (Indic)));
2850 end if;
2851
2852 return Empty;
2853 end Get_Constraint_Association;
2854
2855 -------------------------------------
2856 -- Get_Explicit_Discriminant_Value --
2857 -------------------------------------
2858
2859 function Get_Explicit_Discriminant_Value
2860 (D : Entity_Id) return Node_Id
2861 is
2862 Assoc : Node_Id;
2863 Choice : Node_Id;
2864 Val : Node_Id;
2865
2866 begin
2867 -- The aggregate has been normalized and all associations have a
2868 -- single choice.
2869
2870 Assoc := First (Component_Associations (N));
2871 while Present (Assoc) loop
2872 Choice := First (Choices (Assoc));
2873
2874 if Chars (Choice) = Chars (D) then
2875 Val := Expression (Assoc);
2876 Remove (Assoc);
2877 return Val;
2878 end if;
2879
2880 Next (Assoc);
2881 end loop;
2882
2883 return Empty;
2884 end Get_Explicit_Discriminant_Value;
2885
2886 -------------------------------
2887 -- Init_Hidden_Discriminants --
2888 -------------------------------
2889
2890 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2891 function Is_Completely_Hidden_Discriminant
2892 (Discr : Entity_Id) return Boolean;
2893 -- Determine whether Discr is a completely hidden discriminant of
2894 -- type Typ.
2895
2896 ---------------------------------------
2897 -- Is_Completely_Hidden_Discriminant --
2898 ---------------------------------------
2899
2900 function Is_Completely_Hidden_Discriminant
2901 (Discr : Entity_Id) return Boolean
2902 is
2903 Item : Entity_Id;
2904
2905 begin
2906 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2907 -- completely hidden discriminants.
2908
2909 Item := First_Entity (Typ);
2910 while Present (Item) loop
2911 if Ekind (Item) = E_Discriminant
2912 and then Is_Completely_Hidden (Item)
2913 and then Chars (Original_Record_Component (Item)) =
2914 Chars (Discr)
2915 then
2916 return True;
2917 end if;
2918
2919 Next_Entity (Item);
2920 end loop;
2921
2922 return False;
2923 end Is_Completely_Hidden_Discriminant;
2924
2925 -- Local variables
2926
2927 Base_Typ : Entity_Id;
2928 Discr : Entity_Id;
2929 Discr_Constr : Elmt_Id;
2930 Discr_Init : Node_Id;
2931 Discr_Val : Node_Id;
2932 In_Aggr_Type : Boolean;
2933 Par_Typ : Entity_Id;
2934
2935 -- Start of processing for Init_Hidden_Discriminants
2936
2937 begin
2938 -- The constraints on the hidden discriminants, if present, are kept
2939 -- in the Stored_Constraint list of the type itself, or in that of
2940 -- the base type. If not in the constraints of the aggregate itself,
2941 -- we examine ancestors to find discriminants that are not renamed
2942 -- by other discriminants but constrained explicitly.
2943
2944 In_Aggr_Type := True;
2945
2946 Base_Typ := Base_Type (Typ);
2947 while Is_Derived_Type (Base_Typ)
2948 and then
2949 (Present (Stored_Constraint (Base_Typ))
2950 or else
2951 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2952 loop
2953 Par_Typ := Etype (Base_Typ);
2954
2955 if not Has_Discriminants (Par_Typ) then
2956 return;
2957 end if;
2958
2959 Discr := First_Discriminant (Par_Typ);
2960
2961 -- We know that one of the stored-constraint lists is present
2962
2963 if Present (Stored_Constraint (Base_Typ)) then
2964 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2965
2966 -- For private extension, stored constraint may be on full view
2967
2968 elsif Is_Private_Type (Base_Typ)
2969 and then Present (Full_View (Base_Typ))
2970 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2971 then
2972 Discr_Constr :=
2973 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2974
2975 -- Otherwise, no discriminant to process
2976
2977 else
2978 Discr_Constr := No_Elmt;
2979 end if;
2980
2981 while Present (Discr) and then Present (Discr_Constr) loop
2982 Discr_Val := Node (Discr_Constr);
2983
2984 -- The parent discriminant is renamed in the derived type,
2985 -- nothing to initialize.
2986
2987 -- type Deriv_Typ (Discr : ...)
2988 -- is new Parent_Typ (Discr => Discr);
2989
2990 if Is_Entity_Name (Discr_Val)
2991 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2992 then
2993 null;
2994
2995 -- When the parent discriminant is constrained at the type
2996 -- extension level, it does not appear in the derived type.
2997
2998 -- type Deriv_Typ (Discr : ...)
2999 -- is new Parent_Typ (Discr => Discr,
3000 -- Hidden_Discr => Expression);
3001
3002 elsif Is_Completely_Hidden_Discriminant (Discr) then
3003 null;
3004
3005 -- Otherwise initialize the discriminant
3006
3007 else
3008 Discr_Init :=
3009 Make_OK_Assignment_Statement (Loc,
3010 Name =>
3011 Make_Selected_Component (Loc,
3012 Prefix => New_Copy_Tree (Target),
3013 Selector_Name => New_Occurrence_Of (Discr, Loc)),
3014 Expression => New_Copy_Tree (Discr_Val));
3015
3016 Append_To (List, Discr_Init);
3017 end if;
3018
3019 Next_Elmt (Discr_Constr);
3020 Next_Discriminant (Discr);
3021 end loop;
3022
3023 In_Aggr_Type := False;
3024 Base_Typ := Base_Type (Par_Typ);
3025 end loop;
3026 end Init_Hidden_Discriminants;
3027
3028 --------------------------------
3029 -- Init_Visible_Discriminants --
3030 --------------------------------
3031
3032 procedure Init_Visible_Discriminants is
3033 Discriminant : Entity_Id;
3034 Discriminant_Value : Node_Id;
3035
3036 begin
3037 Discriminant := First_Discriminant (Typ);
3038 while Present (Discriminant) loop
3039 Comp_Expr :=
3040 Make_Selected_Component (Loc,
3041 Prefix => New_Copy_Tree (Target),
3042 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
3043
3044 Discriminant_Value :=
3045 Get_Discriminant_Value
3046 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
3047
3048 Instr :=
3049 Make_OK_Assignment_Statement (Loc,
3050 Name => Comp_Expr,
3051 Expression => New_Copy_Tree (Discriminant_Value));
3052
3053 Append_To (L, Instr);
3054
3055 Next_Discriminant (Discriminant);
3056 end loop;
3057 end Init_Visible_Discriminants;
3058
3059 -------------------------------
3060 -- Init_Stored_Discriminants --
3061 -------------------------------
3062
3063 procedure Init_Stored_Discriminants is
3064 Discriminant : Entity_Id;
3065 Discriminant_Value : Node_Id;
3066
3067 begin
3068 Discriminant := First_Stored_Discriminant (Typ);
3069 while Present (Discriminant) loop
3070 Comp_Expr :=
3071 Make_Selected_Component (Loc,
3072 Prefix => New_Copy_Tree (Target),
3073 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
3074
3075 Discriminant_Value :=
3076 Get_Discriminant_Value
3077 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
3078
3079 Instr :=
3080 Make_OK_Assignment_Statement (Loc,
3081 Name => Comp_Expr,
3082 Expression => New_Copy_Tree (Discriminant_Value));
3083
3084 Append_To (L, Instr);
3085
3086 Next_Stored_Discriminant (Discriminant);
3087 end loop;
3088 end Init_Stored_Discriminants;
3089
3090 --------------------------------------
3091 -- Initialize_Ctrl_Record_Component --
3092 --------------------------------------
3093
3094 procedure Initialize_Ctrl_Record_Component
3095 (Rec_Comp : Node_Id;
3096 Comp_Typ : Entity_Id;
3097 Init_Expr : Node_Id;
3098 Stmts : List_Id)
3099 is
3100 Fin_Call : Node_Id;
3101 Hook_Clear : Node_Id;
3102
3103 In_Place_Expansion : Boolean;
3104 -- Flag set when a nonlimited controlled function call requires
3105 -- in-place expansion.
3106
3107 begin
3108 -- Perform a preliminary analysis and resolution to determine what
3109 -- the initialization expression denotes. Unanalyzed function calls
3110 -- may appear as identifiers or indexed components.
3111
3112 if Nkind (Init_Expr) in N_Function_Call
3113 | N_Identifier
3114 | N_Indexed_Component
3115 and then not Analyzed (Init_Expr)
3116 then
3117 Preanalyze_And_Resolve (Init_Expr, Comp_Typ);
3118 end if;
3119
3120 In_Place_Expansion :=
3121 Nkind (Init_Expr) = N_Function_Call
3122 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
3123
3124 -- The initialization expression is a controlled function call.
3125 -- Perform in-place removal of side effects to avoid creating a
3126 -- transient scope.
3127
3128 -- This in-place expansion is not performed for limited transient
3129 -- objects because the initialization is already done in place.
3130
3131 if In_Place_Expansion then
3132
3133 -- Suppress the removal of side effects by general analysis
3134 -- because this behavior is emulated here. This avoids the
3135 -- generation of a transient scope, which leads to out-of-order
3136 -- adjustment and finalization.
3137
3138 Set_No_Side_Effect_Removal (Init_Expr);
3139
3140 -- Install all hook-related declarations and prepare the clean up
3141 -- statements. The generated code follows the initialization order
3142 -- of individual components and discriminants, rather than being
3143 -- inserted prior to the aggregate. This ensures that a transient
3144 -- component which mentions a discriminant has proper visibility
3145 -- of the discriminant.
3146
3147 Process_Transient_Component
3148 (Loc => Loc,
3149 Comp_Typ => Comp_Typ,
3150 Init_Expr => Init_Expr,
3151 Fin_Call => Fin_Call,
3152 Hook_Clear => Hook_Clear,
3153 Stmts => Stmts);
3154 end if;
3155
3156 -- Use the noncontrolled component initialization circuitry to
3157 -- assign the result of the function call to the record component.
3158 -- This also performs tag adjustment and [deep] adjustment of the
3159 -- record component.
3160
3161 Initialize_Record_Component
3162 (Rec_Comp => Rec_Comp,
3163 Comp_Typ => Comp_Typ,
3164 Init_Expr => Init_Expr,
3165 Stmts => Stmts);
3166
3167 -- At this point the record component is fully initialized. Complete
3168 -- the processing of the controlled record component by finalizing
3169 -- the transient function result.
3170
3171 if In_Place_Expansion then
3172 Process_Transient_Component_Completion
3173 (Loc => Loc,
3174 Aggr => N,
3175 Fin_Call => Fin_Call,
3176 Hook_Clear => Hook_Clear,
3177 Stmts => Stmts);
3178 end if;
3179 end Initialize_Ctrl_Record_Component;
3180
3181 ---------------------------------
3182 -- Initialize_Record_Component --
3183 ---------------------------------
3184
3185 procedure Initialize_Record_Component
3186 (Rec_Comp : Node_Id;
3187 Comp_Typ : Entity_Id;
3188 Init_Expr : Node_Id;
3189 Stmts : List_Id)
3190 is
3191 Exceptions_OK : constant Boolean :=
3192 not Restriction_Active (No_Exception_Propagation);
3193
3194 Finalization_OK : constant Boolean := Needs_Finalization (Comp_Typ);
3195
3196 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
3197 Adj_Call : Node_Id;
3198 Blk_Stmts : List_Id;
3199 Init_Stmt : Node_Id;
3200
3201 begin
3202 -- Protect the initialization statements from aborts. Generate:
3203
3204 -- Abort_Defer;
3205
3206 if Finalization_OK and Abort_Allowed then
3207 if Exceptions_OK then
3208 Blk_Stmts := New_List;
3209 else
3210 Blk_Stmts := Stmts;
3211 end if;
3212
3213 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
3214
3215 -- Otherwise aborts are not allowed. All generated code is added
3216 -- directly to the input list.
3217
3218 else
3219 Blk_Stmts := Stmts;
3220 end if;
3221
3222 -- Initialize the record component. Generate:
3223
3224 -- Rec_Comp := Init_Expr;
3225
3226 -- Note that the initialization expression is NOT replicated because
3227 -- only a single component may be initialized by it.
3228
3229 Init_Stmt :=
3230 Make_OK_Assignment_Statement (Loc,
3231 Name => New_Copy_Tree (Rec_Comp),
3232 Expression => Init_Expr);
3233 Set_No_Ctrl_Actions (Init_Stmt);
3234
3235 Append_To (Blk_Stmts, Init_Stmt);
3236
3237 -- Adjust the tag due to a possible view conversion. Generate:
3238
3239 -- Rec_Comp._tag := Full_TypeP;
3240
3241 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
3242 Append_To (Blk_Stmts,
3243 Make_OK_Assignment_Statement (Loc,
3244 Name =>
3245 Make_Selected_Component (Loc,
3246 Prefix => New_Copy_Tree (Rec_Comp),
3247 Selector_Name =>
3248 New_Occurrence_Of
3249 (First_Tag_Component (Full_Typ), Loc)),
3250
3251 Expression =>
3252 Unchecked_Convert_To (RTE (RE_Tag),
3253 New_Occurrence_Of
3254 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
3255 Loc))));
3256 end if;
3257
3258 -- Adjust the component. Generate:
3259
3260 -- [Deep_]Adjust (Rec_Comp);
3261
3262 if Finalization_OK
3263 and then not Is_Limited_Type (Comp_Typ)
3264 and then not Is_Build_In_Place_Function_Call (Init_Expr)
3265 then
3266 Adj_Call :=
3267 Make_Adjust_Call
3268 (Obj_Ref => New_Copy_Tree (Rec_Comp),
3269 Typ => Comp_Typ);
3270
3271 -- Guard against a missing [Deep_]Adjust when the component type
3272 -- was not properly frozen.
3273
3274 if Present (Adj_Call) then
3275 Append_To (Blk_Stmts, Adj_Call);
3276 end if;
3277 end if;
3278
3279 -- Complete the protection of the initialization statements
3280
3281 if Finalization_OK and Abort_Allowed then
3282
3283 -- Wrap the initialization statements in a block to catch a
3284 -- potential exception. Generate:
3285
3286 -- begin
3287 -- Abort_Defer;
3288 -- Rec_Comp := Init_Expr;
3289 -- Rec_Comp._tag := Full_TypP;
3290 -- [Deep_]Adjust (Rec_Comp);
3291 -- at end
3292 -- Abort_Undefer_Direct;
3293 -- end;
3294
3295 if Exceptions_OK then
3296 Append_To (Stmts,
3297 Build_Abort_Undefer_Block (Loc,
3298 Stmts => Blk_Stmts,
3299 Context => N));
3300
3301 -- Otherwise exceptions are not propagated. Generate:
3302
3303 -- Abort_Defer;
3304 -- Rec_Comp := Init_Expr;
3305 -- Rec_Comp._tag := Full_TypP;
3306 -- [Deep_]Adjust (Rec_Comp);
3307 -- Abort_Undefer;
3308
3309 else
3310 Append_To (Blk_Stmts,
3311 Build_Runtime_Call (Loc, RE_Abort_Undefer));
3312 end if;
3313 end if;
3314 end Initialize_Record_Component;
3315
3316 -------------------------
3317 -- Is_Int_Range_Bounds --
3318 -------------------------
3319
3320 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
3321 begin
3322 return Nkind (Bounds) = N_Range
3323 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
3324 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
3325 end Is_Int_Range_Bounds;
3326
3327 ------------------
3328 -- Replace_Type --
3329 ------------------
3330
3331 function Replace_Type (Expr : Node_Id) return Traverse_Result is
3332 begin
3333 -- Note regarding the Root_Type test below: Aggregate components for
3334 -- self-referential types include attribute references to the current
3335 -- instance, of the form: Typ'access, etc.. These references are
3336 -- rewritten as references to the target of the aggregate: the
3337 -- left-hand side of an assignment, the entity in a declaration,
3338 -- or a temporary. Without this test, we would improperly extended
3339 -- this rewriting to attribute references whose prefix was not the
3340 -- type of the aggregate.
3341
3342 if Nkind (Expr) = N_Attribute_Reference
3343 and then Is_Entity_Name (Prefix (Expr))
3344 and then Is_Type (Entity (Prefix (Expr)))
3345 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
3346 then
3347 if Is_Entity_Name (Lhs) then
3348 Rewrite (Prefix (Expr), New_Occurrence_Of (Entity (Lhs), Loc));
3349
3350 else
3351 Rewrite (Expr,
3352 Make_Attribute_Reference (Loc,
3353 Attribute_Name => Name_Unrestricted_Access,
3354 Prefix => New_Copy_Tree (Lhs)));
3355 Set_Analyzed (Parent (Expr), False);
3356 end if;
3357 end if;
3358
3359 return OK;
3360 end Replace_Type;
3361
3362 --------------------------
3363 -- Rewrite_Discriminant --
3364 --------------------------
3365
3366 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
3367 begin
3368 if Is_Entity_Name (Expr)
3369 and then Present (Entity (Expr))
3370 and then Ekind (Entity (Expr)) = E_In_Parameter
3371 and then Present (Discriminal_Link (Entity (Expr)))
3372 and then Scope (Discriminal_Link (Entity (Expr))) =
3373 Base_Type (Etype (N))
3374 then
3375 Rewrite (Expr,
3376 Make_Selected_Component (Loc,
3377 Prefix => New_Copy_Tree (Lhs),
3378 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
3379
3380 -- The generated code will be reanalyzed, but if the reference
3381 -- to the discriminant appears within an already analyzed
3382 -- expression (e.g. a conditional) we must set its proper entity
3383 -- now. Context is an initialization procedure.
3384
3385 Analyze (Expr);
3386 end if;
3387
3388 return OK;
3389 end Rewrite_Discriminant;
3390
3391 procedure Replace_Discriminants is
3392 new Traverse_Proc (Rewrite_Discriminant);
3393
3394 procedure Replace_Self_Reference is
3395 new Traverse_Proc (Replace_Type);
3396
3397 -- Start of processing for Build_Record_Aggr_Code
3398
3399 begin
3400 if Has_Self_Reference (N) then
3401 Replace_Self_Reference (N);
3402 end if;
3403
3404 -- If the target of the aggregate is class-wide, we must convert it
3405 -- to the actual type of the aggregate, so that the proper components
3406 -- are visible. We know already that the types are compatible.
3407
3408 if Present (Etype (Lhs))
3409 and then Is_Class_Wide_Type (Etype (Lhs))
3410 then
3411 Target := Unchecked_Convert_To (Typ, Lhs);
3412 else
3413 Target := Lhs;
3414 end if;
3415
3416 -- Deal with the ancestor part of extension aggregates or with the
3417 -- discriminants of the root type.
3418
3419 if Nkind (N) = N_Extension_Aggregate then
3420 declare
3421 Ancestor : constant Node_Id := Ancestor_Part (N);
3422 Adj_Call : Node_Id;
3423 Assign : List_Id;
3424
3425 begin
3426 -- If the ancestor part is a subtype mark "T", we generate
3427
3428 -- init-proc (T (tmp)); if T is constrained and
3429 -- init-proc (S (tmp)); where S applies an appropriate
3430 -- constraint if T is unconstrained
3431
3432 if Is_Entity_Name (Ancestor)
3433 and then Is_Type (Entity (Ancestor))
3434 then
3435 Ancestor_Is_Subtype_Mark := True;
3436
3437 if Is_Constrained (Entity (Ancestor)) then
3438 Init_Typ := Entity (Ancestor);
3439
3440 -- For an ancestor part given by an unconstrained type mark,
3441 -- create a subtype constrained by appropriate corresponding
3442 -- discriminant values coming from either associations of the
3443 -- aggregate or a constraint on a parent type. The subtype will
3444 -- be used to generate the correct default value for the
3445 -- ancestor part.
3446
3447 elsif Has_Discriminants (Entity (Ancestor)) then
3448 declare
3449 Anc_Typ : constant Entity_Id := Entity (Ancestor);
3450 Anc_Constr : constant List_Id := New_List;
3451 Discrim : Entity_Id;
3452 Disc_Value : Node_Id;
3453 New_Indic : Node_Id;
3454 Subt_Decl : Node_Id;
3455
3456 begin
3457 Discrim := First_Discriminant (Anc_Typ);
3458 while Present (Discrim) loop
3459 Disc_Value := Ancestor_Discriminant_Value (Discrim);
3460
3461 -- If no usable discriminant in ancestors, check
3462 -- whether aggregate has an explicit value for it.
3463
3464 if No (Disc_Value) then
3465 Disc_Value :=
3466 Get_Explicit_Discriminant_Value (Discrim);
3467 end if;
3468
3469 Append_To (Anc_Constr, Disc_Value);
3470 Next_Discriminant (Discrim);
3471 end loop;
3472
3473 New_Indic :=
3474 Make_Subtype_Indication (Loc,
3475 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
3476 Constraint =>
3477 Make_Index_Or_Discriminant_Constraint (Loc,
3478 Constraints => Anc_Constr));
3479
3480 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
3481
3482 Subt_Decl :=
3483 Make_Subtype_Declaration (Loc,
3484 Defining_Identifier => Init_Typ,
3485 Subtype_Indication => New_Indic);
3486
3487 -- Itypes must be analyzed with checks off Declaration
3488 -- must have a parent for proper handling of subsidiary
3489 -- actions.
3490
3491 Set_Parent (Subt_Decl, N);
3492 Analyze (Subt_Decl, Suppress => All_Checks);
3493 end;
3494 end if;
3495
3496 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3497 Set_Assignment_OK (Ref);
3498
3499 if not Is_Interface (Init_Typ) then
3500 Append_List_To (L,
3501 Build_Initialization_Call (Loc,
3502 Id_Ref => Ref,
3503 Typ => Init_Typ,
3504 In_Init_Proc => Within_Init_Proc,
3505 With_Default_Init => Has_Default_Init_Comps (N)
3506 or else
3507 Has_Task (Base_Type (Init_Typ))));
3508
3509 if Is_Constrained (Entity (Ancestor))
3510 and then Has_Discriminants (Entity (Ancestor))
3511 then
3512 Check_Ancestor_Discriminants (Entity (Ancestor));
3513 end if;
3514
3515 -- If ancestor type has Default_Initialization_Condition,
3516 -- add a DIC check after the ancestor object is initialized
3517 -- by default.
3518
3519 if Has_DIC (Entity (Ancestor))
3520 and then Present (DIC_Procedure (Entity (Ancestor)))
3521 then
3522 Append_To (L,
3523 Build_DIC_Call
3524 (Loc, New_Copy_Tree (Ref), Entity (Ancestor)));
3525 end if;
3526 end if;
3527
3528 -- Handle calls to C++ constructors
3529
3530 elsif Is_CPP_Constructor_Call (Ancestor) then
3531 Init_Typ := Etype (Ancestor);
3532 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3533 Set_Assignment_OK (Ref);
3534
3535 Append_List_To (L,
3536 Build_Initialization_Call (Loc,
3537 Id_Ref => Ref,
3538 Typ => Init_Typ,
3539 In_Init_Proc => Within_Init_Proc,
3540 With_Default_Init => Has_Default_Init_Comps (N),
3541 Constructor_Ref => Ancestor));
3542
3543 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3544 -- limited type, a recursive call expands the ancestor. Note that
3545 -- in the limited case, the ancestor part must be either a
3546 -- function call (possibly qualified) or aggregate (definitely
3547 -- qualified).
3548
3549 elsif Is_Limited_Type (Etype (Ancestor))
3550 and then Nkind (Unqualify (Ancestor)) in
3551 N_Aggregate | N_Extension_Aggregate
3552 then
3553 Ancestor_Is_Expression := True;
3554
3555 -- Set up finalization data for enclosing record, because
3556 -- controlled subcomponents of the ancestor part will be
3557 -- attached to it.
3558
3559 Generate_Finalization_Actions;
3560
3561 Append_List_To (L,
3562 Build_Record_Aggr_Code
3563 (N => Unqualify (Ancestor),
3564 Typ => Etype (Unqualify (Ancestor)),
3565 Lhs => Target));
3566
3567 -- If the ancestor part is an expression "E", we generate
3568
3569 -- T (tmp) := E;
3570
3571 -- In Ada 2005, this includes the case of a (possibly qualified)
3572 -- limited function call. The assignment will turn into a
3573 -- build-in-place function call (for further details, see
3574 -- Make_Build_In_Place_Call_In_Assignment).
3575
3576 else
3577 Ancestor_Is_Expression := True;
3578 Init_Typ := Etype (Ancestor);
3579
3580 -- If the ancestor part is an aggregate, force its full
3581 -- expansion, which was delayed.
3582
3583 if Nkind (Unqualify (Ancestor)) in
3584 N_Aggregate | N_Extension_Aggregate
3585 then
3586 Set_Analyzed (Ancestor, False);
3587 Set_Analyzed (Expression (Ancestor), False);
3588 end if;
3589
3590 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3591 Set_Assignment_OK (Ref);
3592
3593 -- Make the assignment without usual controlled actions, since
3594 -- we only want to Adjust afterwards, but not to Finalize
3595 -- beforehand. Add manual Adjust when necessary.
3596
3597 Assign := New_List (
3598 Make_OK_Assignment_Statement (Loc,
3599 Name => Ref,
3600 Expression => Ancestor));
3601 Set_No_Ctrl_Actions (First (Assign));
3602
3603 -- Assign the tag now to make sure that the dispatching call in
3604 -- the subsequent deep_adjust works properly (unless
3605 -- Tagged_Type_Expansion where tags are implicit).
3606
3607 if Tagged_Type_Expansion then
3608 Instr :=
3609 Make_OK_Assignment_Statement (Loc,
3610 Name =>
3611 Make_Selected_Component (Loc,
3612 Prefix => New_Copy_Tree (Target),
3613 Selector_Name =>
3614 New_Occurrence_Of
3615 (First_Tag_Component (Base_Type (Typ)), Loc)),
3616
3617 Expression =>
3618 Unchecked_Convert_To (RTE (RE_Tag),
3619 New_Occurrence_Of
3620 (Node (First_Elmt
3621 (Access_Disp_Table (Base_Type (Typ)))),
3622 Loc)));
3623
3624 Set_Assignment_OK (Name (Instr));
3625 Append_To (Assign, Instr);
3626
3627 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3628 -- also initialize tags of the secondary dispatch tables.
3629
3630 if Has_Interfaces (Base_Type (Typ)) then
3631 Init_Secondary_Tags
3632 (Typ => Base_Type (Typ),
3633 Target => Target,
3634 Stmts_List => Assign,
3635 Init_Tags_List => Assign);
3636 end if;
3637 end if;
3638
3639 -- Call Adjust manually
3640
3641 if Needs_Finalization (Etype (Ancestor))
3642 and then not Is_Limited_Type (Etype (Ancestor))
3643 and then not Is_Build_In_Place_Function_Call (Ancestor)
3644 then
3645 Adj_Call :=
3646 Make_Adjust_Call
3647 (Obj_Ref => New_Copy_Tree (Ref),
3648 Typ => Etype (Ancestor));
3649
3650 -- Guard against a missing [Deep_]Adjust when the ancestor
3651 -- type was not properly frozen.
3652
3653 if Present (Adj_Call) then
3654 Append_To (Assign, Adj_Call);
3655 end if;
3656 end if;
3657
3658 Append_To (L,
3659 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
3660
3661 if Has_Discriminants (Init_Typ) then
3662 Check_Ancestor_Discriminants (Init_Typ);
3663 end if;
3664 end if;
3665
3666 pragma Assert (Nkind (N) = N_Extension_Aggregate);
3667 pragma Assert
3668 (not (Ancestor_Is_Expression and Ancestor_Is_Subtype_Mark));
3669 end;
3670
3671 -- Generate assignments of hidden discriminants. If the base type is
3672 -- an unchecked union, the discriminants are unknown to the back-end
3673 -- and absent from a value of the type, so assignments for them are
3674 -- not emitted.
3675
3676 if Has_Discriminants (Typ)
3677 and then not Is_Unchecked_Union (Base_Type (Typ))
3678 then
3679 Init_Hidden_Discriminants (Typ, L);
3680 end if;
3681
3682 -- Normal case (not an extension aggregate)
3683
3684 else
3685 -- Generate the discriminant expressions, component by component.
3686 -- If the base type is an unchecked union, the discriminants are
3687 -- unknown to the back-end and absent from a value of the type, so
3688 -- assignments for them are not emitted.
3689
3690 if Has_Discriminants (Typ)
3691 and then not Is_Unchecked_Union (Base_Type (Typ))
3692 then
3693 Init_Hidden_Discriminants (Typ, L);
3694
3695 -- Generate discriminant init values for the visible discriminants
3696
3697 Init_Visible_Discriminants;
3698
3699 if Is_Derived_Type (N_Typ) then
3700 Init_Stored_Discriminants;
3701 end if;
3702 end if;
3703 end if;
3704
3705 -- For CPP types we generate an implicit call to the C++ default
3706 -- constructor to ensure the proper initialization of the _Tag
3707 -- component.
3708
3709 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3710 Invoke_Constructor : declare
3711 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3712
3713 procedure Invoke_IC_Proc (T : Entity_Id);
3714 -- Recursive routine used to climb to parents. Required because
3715 -- parents must be initialized before descendants to ensure
3716 -- propagation of inherited C++ slots.
3717
3718 --------------------
3719 -- Invoke_IC_Proc --
3720 --------------------
3721
3722 procedure Invoke_IC_Proc (T : Entity_Id) is
3723 begin
3724 -- Avoid generating extra calls. Initialization required
3725 -- only for types defined from the level of derivation of
3726 -- type of the constructor and the type of the aggregate.
3727
3728 if T = CPP_Parent then
3729 return;
3730 end if;
3731
3732 Invoke_IC_Proc (Etype (T));
3733
3734 -- Generate call to the IC routine
3735
3736 if Present (CPP_Init_Proc (T)) then
3737 Append_To (L,
3738 Make_Procedure_Call_Statement (Loc,
3739 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3740 end if;
3741 end Invoke_IC_Proc;
3742
3743 -- Start of processing for Invoke_Constructor
3744
3745 begin
3746 -- Implicit invocation of the C++ constructor
3747
3748 if Nkind (N) = N_Aggregate then
3749 Append_To (L,
3750 Make_Procedure_Call_Statement (Loc,
3751 Name =>
3752 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3753 Parameter_Associations => New_List (
3754 Unchecked_Convert_To (CPP_Parent,
3755 New_Copy_Tree (Lhs)))));
3756 end if;
3757
3758 Invoke_IC_Proc (Typ);
3759 end Invoke_Constructor;
3760 end if;
3761
3762 -- Generate the assignments, component by component
3763
3764 -- tmp.comp1 := Expr1_From_Aggr;
3765 -- tmp.comp2 := Expr2_From_Aggr;
3766 -- ....
3767
3768 Comp := First (Component_Associations (N));
3769 while Present (Comp) loop
3770 Selector := Entity (First (Choices (Comp)));
3771
3772 -- C++ constructors
3773
3774 if Is_CPP_Constructor_Call (Expression (Comp)) then
3775 Append_List_To (L,
3776 Build_Initialization_Call (Loc,
3777 Id_Ref =>
3778 Make_Selected_Component (Loc,
3779 Prefix => New_Copy_Tree (Target),
3780 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3781 Typ => Etype (Selector),
3782 Enclos_Type => Typ,
3783 With_Default_Init => True,
3784 Constructor_Ref => Expression (Comp)));
3785
3786 -- Ada 2005 (AI-287): For each default-initialized component generate
3787 -- a call to the corresponding IP subprogram if available.
3788
3789 elsif Box_Present (Comp)
3790 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3791 then
3792 if Ekind (Selector) /= E_Discriminant then
3793 Generate_Finalization_Actions;
3794 end if;
3795
3796 -- Ada 2005 (AI-287): If the component type has tasks then
3797 -- generate the activation chain and master entities (except
3798 -- in case of an allocator because in that case these entities
3799 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3800
3801 declare
3802 Ctype : constant Entity_Id := Etype (Selector);
3803 Inside_Allocator : Boolean := False;
3804 P : Node_Id := Parent (N);
3805
3806 begin
3807 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3808 while Present (P) loop
3809 if Nkind (P) = N_Allocator then
3810 Inside_Allocator := True;
3811 exit;
3812 end if;
3813
3814 P := Parent (P);
3815 end loop;
3816
3817 if not Inside_Init_Proc and not Inside_Allocator then
3818 Build_Activation_Chain_Entity (N);
3819 end if;
3820 end if;
3821 end;
3822
3823 Append_List_To (L,
3824 Build_Initialization_Call (Loc,
3825 Id_Ref => Make_Selected_Component (Loc,
3826 Prefix => New_Copy_Tree (Target),
3827 Selector_Name =>
3828 New_Occurrence_Of (Selector, Loc)),
3829 Typ => Etype (Selector),
3830 Enclos_Type => Typ,
3831 With_Default_Init => True));
3832
3833 -- Prepare for component assignment
3834
3835 elsif Ekind (Selector) /= E_Discriminant
3836 or else Nkind (N) = N_Extension_Aggregate
3837 then
3838 -- All the discriminants have now been assigned
3839
3840 -- This is now a good moment to initialize and attach all the
3841 -- controllers. Their position may depend on the discriminants.
3842
3843 if Ekind (Selector) /= E_Discriminant then
3844 Generate_Finalization_Actions;
3845 end if;
3846
3847 Comp_Type := Underlying_Type (Etype (Selector));
3848 Comp_Expr :=
3849 Make_Selected_Component (Loc,
3850 Prefix => New_Copy_Tree (Target),
3851 Selector_Name => New_Occurrence_Of (Selector, Loc));
3852
3853 if Nkind (Expression (Comp)) = N_Qualified_Expression then
3854 Expr_Q := Expression (Expression (Comp));
3855 else
3856 Expr_Q := Expression (Comp);
3857 end if;
3858
3859 -- Now either create the assignment or generate the code for the
3860 -- inner aggregate top-down.
3861
3862 if Is_Delayed_Aggregate (Expr_Q) then
3863
3864 -- We have the following case of aggregate nesting inside
3865 -- an object declaration:
3866
3867 -- type Arr_Typ is array (Integer range <>) of ...;
3868
3869 -- type Rec_Typ (...) is record
3870 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3871 -- end record;
3872
3873 -- Obj_Rec_Typ : Rec_Typ := (...,
3874 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3875
3876 -- The length of the ranges of the aggregate and Obj_Add_Typ
3877 -- are equal (B - A = Y - X), but they do not coincide (X /=
3878 -- A and B /= Y). This case requires array sliding which is
3879 -- performed in the following manner:
3880
3881 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3882 -- Temp : Arr_Sub;
3883 -- Temp (X) := (...);
3884 -- ...
3885 -- Temp (Y) := (...);
3886 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3887
3888 if Ekind (Comp_Type) = E_Array_Subtype
3889 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3890 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3891 and then not
3892 Compatible_Int_Bounds
3893 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3894 Typ_Bounds => First_Index (Comp_Type))
3895 then
3896 -- Create the array subtype with bounds equal to those of
3897 -- the corresponding aggregate.
3898
3899 declare
3900 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3901
3902 SubD : constant Node_Id :=
3903 Make_Subtype_Declaration (Loc,
3904 Defining_Identifier => SubE,
3905 Subtype_Indication =>
3906 Make_Subtype_Indication (Loc,
3907 Subtype_Mark =>
3908 New_Occurrence_Of (Etype (Comp_Type), Loc),
3909 Constraint =>
3910 Make_Index_Or_Discriminant_Constraint
3911 (Loc,
3912 Constraints => New_List (
3913 New_Copy_Tree
3914 (Aggregate_Bounds (Expr_Q))))));
3915
3916 -- Create a temporary array of the above subtype which
3917 -- will be used to capture the aggregate assignments.
3918
3919 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3920
3921 TmpD : constant Node_Id :=
3922 Make_Object_Declaration (Loc,
3923 Defining_Identifier => TmpE,
3924 Object_Definition => New_Occurrence_Of (SubE, Loc));
3925
3926 begin
3927 Set_No_Initialization (TmpD);
3928 Append_To (L, SubD);
3929 Append_To (L, TmpD);
3930
3931 -- Expand aggregate into assignments to the temp array
3932
3933 Append_List_To (L,
3934 Late_Expansion (Expr_Q, Comp_Type,
3935 New_Occurrence_Of (TmpE, Loc)));
3936
3937 -- Slide
3938
3939 Append_To (L,
3940 Make_Assignment_Statement (Loc,
3941 Name => New_Copy_Tree (Comp_Expr),
3942 Expression => New_Occurrence_Of (TmpE, Loc)));
3943 end;
3944
3945 -- Normal case (sliding not required)
3946
3947 else
3948 Append_List_To (L,
3949 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3950 end if;
3951
3952 -- Expr_Q is not delayed aggregate
3953
3954 else
3955 if Has_Discriminants (Typ) then
3956 Replace_Discriminants (Expr_Q);
3957
3958 -- If the component is an array type that depends on
3959 -- discriminants, and the expression is a single Others
3960 -- clause, create an explicit subtype for it because the
3961 -- backend has troubles recovering the actual bounds.
3962
3963 if Nkind (Expr_Q) = N_Aggregate
3964 and then Is_Array_Type (Comp_Type)
3965 and then Present (Component_Associations (Expr_Q))
3966 then
3967 declare
3968 Assoc : constant Node_Id :=
3969 First (Component_Associations (Expr_Q));
3970 Decl : Node_Id;
3971
3972 begin
3973 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3974 then
3975 Decl :=
3976 Build_Actual_Subtype_Of_Component
3977 (Comp_Type, Comp_Expr);
3978
3979 -- If the component type does not in fact depend on
3980 -- discriminants, the subtype declaration is empty.
3981
3982 if Present (Decl) then
3983 Append_To (L, Decl);
3984 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3985 end if;
3986 end if;
3987 end;
3988 end if;
3989 end if;
3990
3991 if Modify_Tree_For_C
3992 and then Nkind (Expr_Q) = N_Aggregate
3993 and then Is_Array_Type (Etype (Expr_Q))
3994 and then Present (First_Index (Etype (Expr_Q)))
3995 then
3996 declare
3997 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3998 begin
3999 Append_List_To (L,
4000 Build_Array_Aggr_Code
4001 (N => Expr_Q,
4002 Ctype => Component_Type (Expr_Q_Type),
4003 Index => First_Index (Expr_Q_Type),
4004 Into => Comp_Expr,
4005 Scalar_Comp =>
4006 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
4007 end;
4008
4009 else
4010 -- Handle an initialization expression of a controlled type
4011 -- in case it denotes a function call. In general such a
4012 -- scenario will produce a transient scope, but this will
4013 -- lead to wrong order of initialization, adjustment, and
4014 -- finalization in the context of aggregates.
4015
4016 -- Target.Comp := Ctrl_Func_Call;
4017
4018 -- begin -- scope
4019 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
4020 -- Target.Comp := Trans_Obj;
4021 -- Finalize (Trans_Obj);
4022 -- end
4023 -- Target.Comp._tag := ...;
4024 -- Adjust (Target.Comp);
4025
4026 -- In the example above, the call to Finalize occurs too
4027 -- early and as a result it may leave the record component
4028 -- in a bad state. Finalization of the transient object
4029 -- should really happen after adjustment.
4030
4031 -- To avoid this scenario, perform in-place side-effect
4032 -- removal of the function call. This eliminates the
4033 -- transient property of the function result and ensures
4034 -- correct order of actions.
4035
4036 -- Res : ... := Ctrl_Func_Call;
4037 -- Target.Comp := Res;
4038 -- Target.Comp._tag := ...;
4039 -- Adjust (Target.Comp);
4040 -- Finalize (Res);
4041
4042 if Needs_Finalization (Comp_Type)
4043 and then Nkind (Expr_Q) /= N_Aggregate
4044 then
4045 Initialize_Ctrl_Record_Component
4046 (Rec_Comp => Comp_Expr,
4047 Comp_Typ => Etype (Selector),
4048 Init_Expr => Expr_Q,
4049 Stmts => L);
4050
4051 -- Otherwise perform single component initialization
4052
4053 else
4054 Initialize_Record_Component
4055 (Rec_Comp => Comp_Expr,
4056 Comp_Typ => Etype (Selector),
4057 Init_Expr => Expr_Q,
4058 Stmts => L);
4059 end if;
4060 end if;
4061 end if;
4062
4063 -- comment would be good here ???
4064
4065 elsif Ekind (Selector) = E_Discriminant
4066 and then Nkind (N) /= N_Extension_Aggregate
4067 and then Nkind (Parent (N)) = N_Component_Association
4068 and then Is_Constrained (Typ)
4069 then
4070 -- We must check that the discriminant value imposed by the
4071 -- context is the same as the value given in the subaggregate,
4072 -- because after the expansion into assignments there is no
4073 -- record on which to perform a regular discriminant check.
4074
4075 declare
4076 D_Val : Elmt_Id;
4077 Disc : Entity_Id;
4078
4079 begin
4080 D_Val := First_Elmt (Discriminant_Constraint (Typ));
4081 Disc := First_Discriminant (Typ);
4082 while Chars (Disc) /= Chars (Selector) loop
4083 Next_Discriminant (Disc);
4084 Next_Elmt (D_Val);
4085 end loop;
4086
4087 pragma Assert (Present (D_Val));
4088
4089 -- This check cannot performed for components that are
4090 -- constrained by a current instance, because this is not a
4091 -- value that can be compared with the actual constraint.
4092
4093 if Nkind (Node (D_Val)) /= N_Attribute_Reference
4094 or else not Is_Entity_Name (Prefix (Node (D_Val)))
4095 or else not Is_Type (Entity (Prefix (Node (D_Val))))
4096 then
4097 Append_To (L,
4098 Make_Raise_Constraint_Error (Loc,
4099 Condition =>
4100 Make_Op_Ne (Loc,
4101 Left_Opnd => New_Copy_Tree (Node (D_Val)),
4102 Right_Opnd => Expression (Comp)),
4103 Reason => CE_Discriminant_Check_Failed));
4104
4105 else
4106 -- Find self-reference in previous discriminant assignment,
4107 -- and replace with proper expression.
4108
4109 declare
4110 Ass : Node_Id;
4111
4112 begin
4113 Ass := First (L);
4114 while Present (Ass) loop
4115 if Nkind (Ass) = N_Assignment_Statement
4116 and then Nkind (Name (Ass)) = N_Selected_Component
4117 and then Chars (Selector_Name (Name (Ass))) =
4118 Chars (Disc)
4119 then
4120 Set_Expression
4121 (Ass, New_Copy_Tree (Expression (Comp)));
4122 exit;
4123 end if;
4124 Next (Ass);
4125 end loop;
4126 end;
4127 end if;
4128 end;
4129 end if;
4130
4131 -- If the component association was specified with a box and the
4132 -- component type has a Default_Initial_Condition, then generate
4133 -- a call to the DIC procedure.
4134
4135 if Has_DIC (Etype (Selector))
4136 and then Was_Default_Init_Box_Association (Comp)
4137 and then Present (DIC_Procedure (Etype (Selector)))
4138 then
4139 Append_To (L,
4140 Build_DIC_Call (Loc,
4141 Make_Selected_Component (Loc,
4142 Prefix => New_Copy_Tree (Target),
4143 Selector_Name => New_Occurrence_Of (Selector, Loc)),
4144 Etype (Selector)));
4145 end if;
4146
4147 Next (Comp);
4148 end loop;
4149
4150 -- If the type is tagged, the tag needs to be initialized (unless we
4151 -- are in VM-mode where tags are implicit). It is done late in the
4152 -- initialization process because in some cases, we call the init
4153 -- proc of an ancestor which will not leave out the right tag.
4154
4155 if Ancestor_Is_Expression then
4156 null;
4157
4158 -- For CPP types we generated a call to the C++ default constructor
4159 -- before the components have been initialized to ensure the proper
4160 -- initialization of the _Tag component (see above).
4161
4162 elsif Is_CPP_Class (Typ) then
4163 null;
4164
4165 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
4166 Instr :=
4167 Make_OK_Assignment_Statement (Loc,
4168 Name =>
4169 Make_Selected_Component (Loc,
4170 Prefix => New_Copy_Tree (Target),
4171 Selector_Name =>
4172 New_Occurrence_Of
4173 (First_Tag_Component (Base_Type (Typ)), Loc)),
4174
4175 Expression =>
4176 Unchecked_Convert_To (RTE (RE_Tag),
4177 New_Occurrence_Of
4178 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
4179 Loc)));
4180
4181 Append_To (L, Instr);
4182
4183 -- Ada 2005 (AI-251): If the tagged type has been derived from an
4184 -- abstract interfaces we must also initialize the tags of the
4185 -- secondary dispatch tables.
4186
4187 if Has_Interfaces (Base_Type (Typ)) then
4188 Init_Secondary_Tags
4189 (Typ => Base_Type (Typ),
4190 Target => Target,
4191 Stmts_List => L,
4192 Init_Tags_List => L);
4193 end if;
4194 end if;
4195
4196 -- If the controllers have not been initialized yet (by lack of non-
4197 -- discriminant components), let's do it now.
4198
4199 Generate_Finalization_Actions;
4200
4201 return L;
4202 end Build_Record_Aggr_Code;
4203
4204 -------------------------------
4205 -- Convert_Aggr_In_Allocator --
4206 -------------------------------
4207
4208 procedure Convert_Aggr_In_Allocator
4209 (Alloc : Node_Id;
4210 Decl : Node_Id;
4211 Aggr : Node_Id)
4212 is
4213 Loc : constant Source_Ptr := Sloc (Aggr);
4214 Typ : constant Entity_Id := Etype (Aggr);
4215 Temp : constant Entity_Id := Defining_Identifier (Decl);
4216
4217 Occ : constant Node_Id :=
4218 Unchecked_Convert_To (Typ,
4219 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
4220
4221 begin
4222 if Is_Array_Type (Typ) then
4223 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
4224
4225 elsif Has_Default_Init_Comps (Aggr) then
4226 declare
4227 L : constant List_Id := New_List;
4228 Init_Stmts : List_Id;
4229
4230 begin
4231 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
4232
4233 if Has_Task (Typ) then
4234 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
4235 Insert_Actions (Alloc, L);
4236 else
4237 Insert_Actions (Alloc, Init_Stmts);
4238 end if;
4239 end;
4240
4241 else
4242 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
4243 end if;
4244 end Convert_Aggr_In_Allocator;
4245
4246 --------------------------------
4247 -- Convert_Aggr_In_Assignment --
4248 --------------------------------
4249
4250 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
4251 Aggr : Node_Id := Expression (N);
4252 Typ : constant Entity_Id := Etype (Aggr);
4253 Occ : constant Node_Id := New_Copy_Tree (Name (N));
4254
4255 begin
4256 if Nkind (Aggr) = N_Qualified_Expression then
4257 Aggr := Expression (Aggr);
4258 end if;
4259
4260 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
4261 end Convert_Aggr_In_Assignment;
4262
4263 ---------------------------------
4264 -- Convert_Aggr_In_Object_Decl --
4265 ---------------------------------
4266
4267 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
4268 Obj : constant Entity_Id := Defining_Identifier (N);
4269 Aggr : Node_Id := Expression (N);
4270 Loc : constant Source_Ptr := Sloc (Aggr);
4271 Typ : constant Entity_Id := Etype (Aggr);
4272 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
4273
4274 Has_Transient_Scope : Boolean := False;
4275
4276 function Discriminants_Ok return Boolean;
4277 -- If the object type is constrained, the discriminants in the
4278 -- aggregate must be checked against the discriminants of the subtype.
4279 -- This cannot be done using Apply_Discriminant_Checks because after
4280 -- expansion there is no aggregate left to check.
4281
4282 ----------------------
4283 -- Discriminants_Ok --
4284 ----------------------
4285
4286 function Discriminants_Ok return Boolean is
4287 Cond : Node_Id := Empty;
4288 Check : Node_Id;
4289 D : Entity_Id;
4290 Disc1 : Elmt_Id;
4291 Disc2 : Elmt_Id;
4292 Val1 : Node_Id;
4293 Val2 : Node_Id;
4294
4295 begin
4296 D := First_Discriminant (Typ);
4297 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
4298 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
4299 while Present (Disc1) and then Present (Disc2) loop
4300 Val1 := Node (Disc1);
4301 Val2 := Node (Disc2);
4302
4303 if not Is_OK_Static_Expression (Val1)
4304 or else not Is_OK_Static_Expression (Val2)
4305 then
4306 Check := Make_Op_Ne (Loc,
4307 Left_Opnd => Duplicate_Subexpr (Val1),
4308 Right_Opnd => Duplicate_Subexpr (Val2));
4309
4310 if No (Cond) then
4311 Cond := Check;
4312
4313 else
4314 Cond := Make_Or_Else (Loc,
4315 Left_Opnd => Cond,
4316 Right_Opnd => Check);
4317 end if;
4318
4319 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
4320 Apply_Compile_Time_Constraint_Error (Aggr,
4321 Msg => "incorrect value for discriminant&??",
4322 Reason => CE_Discriminant_Check_Failed,
4323 Ent => D);
4324 return False;
4325 end if;
4326
4327 Next_Discriminant (D);
4328 Next_Elmt (Disc1);
4329 Next_Elmt (Disc2);
4330 end loop;
4331
4332 -- If any discriminant constraint is nonstatic, emit a check
4333
4334 if Present (Cond) then
4335 Insert_Action (N,
4336 Make_Raise_Constraint_Error (Loc,
4337 Condition => Cond,
4338 Reason => CE_Discriminant_Check_Failed));
4339 end if;
4340
4341 return True;
4342 end Discriminants_Ok;
4343
4344 -- Start of processing for Convert_Aggr_In_Object_Decl
4345
4346 begin
4347 Set_Assignment_OK (Occ);
4348
4349 if Nkind (Aggr) = N_Qualified_Expression then
4350 Aggr := Expression (Aggr);
4351 end if;
4352
4353 if Has_Discriminants (Typ)
4354 and then Typ /= Etype (Obj)
4355 and then Is_Constrained (Etype (Obj))
4356 and then not Discriminants_Ok
4357 then
4358 return;
4359 end if;
4360
4361 -- If the context is an extended return statement, it has its own
4362 -- finalization machinery (i.e. works like a transient scope) and
4363 -- we do not want to create an additional one, because objects on
4364 -- the finalization list of the return must be moved to the caller's
4365 -- finalization list to complete the return.
4366
4367 -- Similarly if the aggregate is limited, it is built in place, and the
4368 -- controlled components are not assigned to intermediate temporaries
4369 -- so there is no need for a transient scope in this case either.
4370
4371 if Requires_Transient_Scope (Typ)
4372 and then Ekind (Current_Scope) /= E_Return_Statement
4373 and then not Is_Limited_Type (Typ)
4374 then
4375 Establish_Transient_Scope (Aggr, Manage_Sec_Stack => False);
4376 Has_Transient_Scope := True;
4377 end if;
4378
4379 declare
4380 Stmts : constant List_Id := Late_Expansion (Aggr, Typ, Occ);
4381 Stmt : Node_Id;
4382 Param : Node_Id;
4383
4384 begin
4385 -- If Obj is already frozen or if N is wrapped in a transient scope,
4386 -- Stmts do not need to be saved in Initialization_Statements since
4387 -- there is no freezing issue.
4388
4389 if Is_Frozen (Obj) or else Has_Transient_Scope then
4390 Insert_Actions_After (N, Stmts);
4391 else
4392 Stmt := Make_Compound_Statement (Sloc (N), Actions => Stmts);
4393 Insert_Action_After (N, Stmt);
4394
4395 -- Insert_Action_After may freeze Obj in which case we should
4396 -- remove the compound statement just created and simply insert
4397 -- Stmts after N.
4398
4399 if Is_Frozen (Obj) then
4400 Remove (Stmt);
4401 Insert_Actions_After (N, Stmts);
4402 else
4403 Set_Initialization_Statements (Obj, Stmt);
4404 end if;
4405 end if;
4406
4407 -- If Typ has controlled components and a call to a Slice_Assign
4408 -- procedure is part of the initialization statements, then we
4409 -- need to initialize the array component since Slice_Assign will
4410 -- need to adjust it.
4411
4412 if Has_Controlled_Component (Typ) then
4413 Stmt := First (Stmts);
4414
4415 while Present (Stmt) loop
4416 if Nkind (Stmt) = N_Procedure_Call_Statement
4417 and then Get_TSS_Name (Entity (Name (Stmt)))
4418 = TSS_Slice_Assign
4419 then
4420 Param := First (Parameter_Associations (Stmt));
4421 Insert_Actions
4422 (Stmt,
4423 Build_Initialization_Call
4424 (Sloc (N), New_Copy_Tree (Param), Etype (Param)));
4425 end if;
4426
4427 Next (Stmt);
4428 end loop;
4429 end if;
4430 end;
4431
4432 Set_No_Initialization (N);
4433 Initialize_Discriminants (N, Typ);
4434 end Convert_Aggr_In_Object_Decl;
4435
4436 -------------------------------------
4437 -- Convert_Array_Aggr_In_Allocator --
4438 -------------------------------------
4439
4440 procedure Convert_Array_Aggr_In_Allocator
4441 (Decl : Node_Id;
4442 Aggr : Node_Id;
4443 Target : Node_Id)
4444 is
4445 Typ : constant Entity_Id := Etype (Aggr);
4446 Ctyp : constant Entity_Id := Component_Type (Typ);
4447 Aggr_Code : List_Id;
4448 New_Aggr : Node_Id;
4449
4450 begin
4451 -- The target is an explicit dereference of the allocated object
4452
4453 -- If the assignment can be done directly by the back end, then
4454 -- reset Set_Expansion_Delayed and do not expand further.
4455
4456 if not CodePeer_Mode
4457 and then not Modify_Tree_For_C
4458 and then Aggr_Assignment_OK_For_Backend (Aggr)
4459 then
4460 New_Aggr := New_Copy_Tree (Aggr);
4461 Set_Expansion_Delayed (New_Aggr, False);
4462
4463 Aggr_Code :=
4464 New_List (
4465 Make_OK_Assignment_Statement (Sloc (New_Aggr),
4466 Name => Target,
4467 Expression => New_Aggr));
4468
4469 -- Or else, generate component assignments to it, as for an aggregate
4470 -- that appears on the right-hand side of an assignment statement.
4471
4472 else
4473 Aggr_Code :=
4474 Build_Array_Aggr_Code (Aggr,
4475 Ctype => Ctyp,
4476 Index => First_Index (Typ),
4477 Into => Target,
4478 Scalar_Comp => Is_Scalar_Type (Ctyp));
4479 end if;
4480
4481 Insert_Actions_After (Decl, Aggr_Code);
4482 end Convert_Array_Aggr_In_Allocator;
4483
4484 ------------------------
4485 -- In_Place_Assign_OK --
4486 ------------------------
4487
4488 function In_Place_Assign_OK
4489 (N : Node_Id;
4490 Target_Object : Entity_Id := Empty) return Boolean
4491 is
4492 Is_Array : constant Boolean := Is_Array_Type (Etype (N));
4493
4494 Aggr_In : Node_Id;
4495 Aggr_Lo : Node_Id;
4496 Aggr_Hi : Node_Id;
4497 Obj_In : Node_Id;
4498 Obj_Lo : Node_Id;
4499 Obj_Hi : Node_Id;
4500 Parent_Kind : Node_Kind;
4501 Parent_Node : Node_Id;
4502
4503 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4504 -- Check recursively that each component of a (sub)aggregate does not
4505 -- depend on the variable being assigned to.
4506
4507 function Safe_Component (Expr : Node_Id) return Boolean;
4508 -- Verify that an expression cannot depend on the target being assigned
4509 -- to. Return true for compile-time known values, stand-alone objects,
4510 -- parameters passed by copy, calls to functions that return by copy,
4511 -- selected components thereof only if the aggregate's type is an array,
4512 -- indexed components and slices thereof only if the aggregate's type is
4513 -- a record, and simple expressions involving only these as operands.
4514 -- This is OK whatever the target because, for a component to overlap
4515 -- with the target, it must be either a direct reference to a component
4516 -- of the target, in which case there must be a matching selection or
4517 -- indexation or slicing, or an indirect reference to such a component,
4518 -- which is excluded by the above condition. Additionally, if the target
4519 -- is statically known, return true for arbitrarily nested selections,
4520 -- indexations or slicings, provided that their ultimate prefix is not
4521 -- the target itself.
4522
4523 --------------------
4524 -- Safe_Aggregate --
4525 --------------------
4526
4527 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4528 Expr : Node_Id;
4529
4530 begin
4531 if Nkind (Parent (Aggr)) = N_Iterated_Component_Association then
4532 return False;
4533 end if;
4534
4535 if Present (Expressions (Aggr)) then
4536 Expr := First (Expressions (Aggr));
4537 while Present (Expr) loop
4538 if Nkind (Expr) = N_Aggregate then
4539 if not Safe_Aggregate (Expr) then
4540 return False;
4541 end if;
4542
4543 elsif not Safe_Component (Expr) then
4544 return False;
4545 end if;
4546
4547 Next (Expr);
4548 end loop;
4549 end if;
4550
4551 if Present (Component_Associations (Aggr)) then
4552 Expr := First (Component_Associations (Aggr));
4553 while Present (Expr) loop
4554 if Nkind (Expression (Expr)) = N_Aggregate then
4555 if not Safe_Aggregate (Expression (Expr)) then
4556 return False;
4557 end if;
4558
4559 -- If association has a box, no way to determine yet whether
4560 -- default can be assigned in place.
4561
4562 elsif Box_Present (Expr) then
4563 return False;
4564
4565 elsif not Safe_Component (Expression (Expr)) then
4566 return False;
4567 end if;
4568
4569 Next (Expr);
4570 end loop;
4571 end if;
4572
4573 return True;
4574 end Safe_Aggregate;
4575
4576 --------------------
4577 -- Safe_Component --
4578 --------------------
4579
4580 function Safe_Component (Expr : Node_Id) return Boolean is
4581 Comp : Node_Id := Expr;
4582
4583 function Check_Component (C : Node_Id; T_OK : Boolean) return Boolean;
4584 -- Do the recursive traversal, after copy. If T_OK is True, return
4585 -- True for a stand-alone object only if the target is statically
4586 -- known and distinct from the object. At the top level, we start
4587 -- with T_OK set to False and set it to True at a deeper level only
4588 -- if we cannot disambiguate the component here without statically
4589 -- knowing the target. Note that this is not optimal, we should do
4590 -- something along the lines of Denotes_Same_Prefix for that.
4591
4592 ---------------------
4593 -- Check_Component --
4594 ---------------------
4595
4596 function Check_Component (C : Node_Id; T_OK : Boolean) return Boolean
4597 is
4598
4599 function SDO (E : Entity_Id) return Uint;
4600 -- Return the Scope Depth Of the enclosing dynamic scope of E
4601
4602 ---------
4603 -- SDO --
4604 ---------
4605
4606 function SDO (E : Entity_Id) return Uint is
4607 begin
4608 return Scope_Depth (Enclosing_Dynamic_Scope (E));
4609 end SDO;
4610
4611 -- Start of processing for Check_Component
4612
4613 begin
4614 if Is_Overloaded (C) then
4615 return False;
4616
4617 elsif Compile_Time_Known_Value (C) then
4618 return True;
4619 end if;
4620
4621 case Nkind (C) is
4622 when N_Attribute_Reference =>
4623 return Check_Component (Prefix (C), T_OK);
4624
4625 when N_Function_Call =>
4626 if Nkind (Name (C)) = N_Explicit_Dereference then
4627 return not Returns_By_Ref (Etype (Name (C)));
4628 else
4629 return not Returns_By_Ref (Entity (Name (C)));
4630 end if;
4631
4632 when N_Indexed_Component | N_Slice =>
4633 -- In a target record, these operations cannot determine
4634 -- alone a component so we can recurse whatever the target.
4635 return Check_Component (Prefix (C), T_OK or else Is_Array);
4636
4637 when N_Selected_Component =>
4638 -- In a target array, this operation cannot determine alone
4639 -- a component so we can recurse whatever the target.
4640 return
4641 Check_Component (Prefix (C), T_OK or else not Is_Array);
4642
4643 when N_Type_Conversion | N_Unchecked_Type_Conversion =>
4644 return Check_Component (Expression (C), T_OK);
4645
4646 when N_Binary_Op =>
4647 return Check_Component (Left_Opnd (C), T_OK)
4648 and then Check_Component (Right_Opnd (C), T_OK);
4649
4650 when N_Unary_Op =>
4651 return Check_Component (Right_Opnd (C), T_OK);
4652
4653 when others =>
4654 if Is_Entity_Name (C) and then Is_Object (Entity (C)) then
4655 -- Case of a formal parameter component. It's either
4656 -- trivial if passed by copy or very annoying if not,
4657 -- because in the latter case it's almost equivalent
4658 -- to a dereference, so the path-based disambiguation
4659 -- logic is totally off and we always need the target.
4660
4661 if Is_Formal (Entity (C)) then
4662
4663 -- If it is passed by copy, then this is safe
4664
4665 if Mechanism (Entity (C)) = By_Copy then
4666 return True;
4667
4668 -- Otherwise, this is safe if the target is present
4669 -- and is at least as deeply nested as the component.
4670
4671 else
4672 return Present (Target_Object)
4673 and then not Is_Formal (Target_Object)
4674 and then SDO (Target_Object) >= SDO (Entity (C));
4675 end if;
4676
4677 -- For a renamed object, recurse
4678
4679 elsif Present (Renamed_Object (Entity (C))) then
4680 return
4681 Check_Component (Renamed_Object (Entity (C)), T_OK);
4682
4683 -- If this is safe whatever the target, we are done
4684
4685 elsif not T_OK then
4686 return True;
4687
4688 -- If there is no target or the component is the target,
4689 -- this is not safe.
4690
4691 elsif No (Target_Object)
4692 or else Entity (C) = Target_Object
4693 then
4694 return False;
4695
4696 -- Case of a formal parameter target. This is safe if it
4697 -- is at most as deeply nested as the component.
4698
4699 elsif Is_Formal (Target_Object) then
4700 return SDO (Target_Object) <= SDO (Entity (C));
4701
4702 -- For distinct stand-alone objects, this is safe
4703
4704 else
4705 return True;
4706 end if;
4707
4708 -- For anything else than an object, this is not safe
4709
4710 else
4711 return False;
4712 end if;
4713 end case;
4714 end Check_Component;
4715
4716 -- Start of processing for Safe_Component
4717
4718 begin
4719 -- If the component appears in an association that may correspond
4720 -- to more than one element, it is not analyzed before expansion
4721 -- into assignments, to avoid side effects. We analyze, but do not
4722 -- resolve the copy, to obtain sufficient entity information for
4723 -- the checks that follow. If component is overloaded we assume
4724 -- an unsafe function call.
4725
4726 if not Analyzed (Comp) then
4727 if Is_Overloaded (Expr) then
4728 return False;
4729
4730 elsif Nkind (Expr) = N_Allocator then
4731
4732 -- For now, too complex to analyze
4733
4734 return False;
4735
4736 elsif Nkind (Parent (Expr)) = N_Iterated_Component_Association then
4737
4738 -- Ditto for iterated component associations, which in general
4739 -- require an enclosing loop and involve nonstatic expressions.
4740
4741 return False;
4742 end if;
4743
4744 Comp := New_Copy_Tree (Expr);
4745 Set_Parent (Comp, Parent (Expr));
4746 Analyze (Comp);
4747 end if;
4748
4749 if Nkind (Comp) = N_Aggregate then
4750 return Safe_Aggregate (Comp);
4751 else
4752 return Check_Component (Comp, False);
4753 end if;
4754 end Safe_Component;
4755
4756 -- Start of processing for In_Place_Assign_OK
4757
4758 begin
4759 -- By-copy semantic cannot be guaranteed for controlled objects
4760
4761 if Needs_Finalization (Etype (N)) then
4762 return False;
4763 end if;
4764
4765 Parent_Node := Parent (N);
4766 Parent_Kind := Nkind (Parent_Node);
4767
4768 if Parent_Kind = N_Qualified_Expression then
4769 Parent_Node := Parent (Parent_Node);
4770 Parent_Kind := Nkind (Parent_Node);
4771 end if;
4772
4773 -- On assignment, sliding can take place, so we cannot do the
4774 -- assignment in place unless the bounds of the aggregate are
4775 -- statically equal to those of the target.
4776
4777 -- If the aggregate is given by an others choice, the bounds are
4778 -- derived from the left-hand side, and the assignment is safe if
4779 -- the expression is.
4780
4781 if Is_Array
4782 and then Present (Component_Associations (N))
4783 and then not Is_Others_Aggregate (N)
4784 then
4785 Aggr_In := First_Index (Etype (N));
4786
4787 -- Context is an assignment
4788
4789 if Parent_Kind = N_Assignment_Statement then
4790 Obj_In := First_Index (Etype (Name (Parent_Node)));
4791
4792 -- Context is an allocator. Check the bounds of the aggregate against
4793 -- those of the designated type, except in the case where the type is
4794 -- unconstrained (and then we can directly return true, see below).
4795
4796 else pragma Assert (Parent_Kind = N_Allocator);
4797 declare
4798 Desig_Typ : constant Entity_Id :=
4799 Designated_Type (Etype (Parent_Node));
4800 begin
4801 if not Is_Constrained (Desig_Typ) then
4802 return True;
4803 end if;
4804
4805 Obj_In := First_Index (Desig_Typ);
4806 end;
4807 end if;
4808
4809 while Present (Aggr_In) loop
4810 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4811 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4812
4813 -- We require static bounds for the target and a static matching
4814 -- of low bound for the aggregate.
4815
4816 if not Compile_Time_Known_Value (Obj_Lo)
4817 or else not Compile_Time_Known_Value (Obj_Hi)
4818 or else not Compile_Time_Known_Value (Aggr_Lo)
4819 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4820 then
4821 return False;
4822
4823 -- For an assignment statement we require static matching of
4824 -- bounds. Ditto for an allocator whose qualified expression
4825 -- is a constrained type. If the expression in the allocator
4826 -- is an unconstrained array, we accept an upper bound that
4827 -- is not static, to allow for nonstatic expressions of the
4828 -- base type. Clearly there are further possibilities (with
4829 -- diminishing returns) for safely building arrays in place
4830 -- here.
4831
4832 elsif Parent_Kind = N_Assignment_Statement
4833 or else Is_Constrained (Etype (Parent_Node))
4834 then
4835 if not Compile_Time_Known_Value (Aggr_Hi)
4836 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4837 then
4838 return False;
4839 end if;
4840 end if;
4841
4842 Next_Index (Aggr_In);
4843 Next_Index (Obj_In);
4844 end loop;
4845 end if;
4846
4847 -- Now check the component values themselves, except for an allocator
4848 -- for which the target is newly allocated memory.
4849
4850 if Parent_Kind = N_Allocator then
4851 return True;
4852 else
4853 return Safe_Aggregate (N);
4854 end if;
4855 end In_Place_Assign_OK;
4856
4857 ----------------------------
4858 -- Convert_To_Assignments --
4859 ----------------------------
4860
4861 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4862 Loc : constant Source_Ptr := Sloc (N);
4863 T : Entity_Id;
4864 Temp : Entity_Id;
4865
4866 Aggr_Code : List_Id;
4867 Instr : Node_Id;
4868 Target_Expr : Node_Id;
4869 Parent_Kind : Node_Kind;
4870 Unc_Decl : Boolean := False;
4871 Parent_Node : Node_Id;
4872
4873 begin
4874 pragma Assert (Nkind (N) in N_Aggregate | N_Extension_Aggregate);
4875 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4876 pragma Assert (Is_Record_Type (Typ));
4877
4878 Parent_Node := Parent (N);
4879 Parent_Kind := Nkind (Parent_Node);
4880
4881 if Parent_Kind = N_Qualified_Expression then
4882 -- Check if we are in an unconstrained declaration because in this
4883 -- case the current delayed expansion mechanism doesn't work when
4884 -- the declared object size depends on the initializing expr.
4885
4886 Parent_Node := Parent (Parent_Node);
4887 Parent_Kind := Nkind (Parent_Node);
4888
4889 if Parent_Kind = N_Object_Declaration then
4890 Unc_Decl :=
4891 not Is_Entity_Name (Object_Definition (Parent_Node))
4892 or else (Nkind (N) = N_Aggregate
4893 and then
4894 Has_Discriminants
4895 (Entity (Object_Definition (Parent_Node))))
4896 or else Is_Class_Wide_Type
4897 (Entity (Object_Definition (Parent_Node)));
4898 end if;
4899 end if;
4900
4901 -- Just set the Delay flag in the cases where the transformation will be
4902 -- done top down from above.
4903
4904 if False
4905
4906 -- Internal aggregate (transformed when expanding the parent)
4907
4908 or else Parent_Kind = N_Aggregate
4909 or else Parent_Kind = N_Extension_Aggregate
4910 or else Parent_Kind = N_Component_Association
4911
4912 -- Allocator (see Convert_Aggr_In_Allocator)
4913
4914 or else Parent_Kind = N_Allocator
4915
4916 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4917
4918 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4919
4920 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4921 -- assignments in init procs are taken into account.
4922
4923 or else (Parent_Kind = N_Assignment_Statement
4924 and then Inside_Init_Proc)
4925
4926 -- (Ada 2005) An inherently limited type in a return statement, which
4927 -- will be handled in a build-in-place fashion, and may be rewritten
4928 -- as an extended return and have its own finalization machinery.
4929 -- In the case of a simple return, the aggregate needs to be delayed
4930 -- until the scope for the return statement has been created, so
4931 -- that any finalization chain will be associated with that scope.
4932 -- For extended returns, we delay expansion to avoid the creation
4933 -- of an unwanted transient scope that could result in premature
4934 -- finalization of the return object (which is built in place
4935 -- within the caller's scope).
4936
4937 or else Is_Build_In_Place_Aggregate_Return (N)
4938 then
4939 Set_Expansion_Delayed (N);
4940 return;
4941 end if;
4942
4943 -- Otherwise, if a transient scope is required, create it now. If we
4944 -- are within an initialization procedure do not create such, because
4945 -- the target of the assignment must not be declared within a local
4946 -- block, and because cleanup will take place on return from the
4947 -- initialization procedure.
4948
4949 -- Should the condition be more restrictive ???
4950
4951 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4952 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
4953 end if;
4954
4955 -- If the aggregate is nonlimited, create a temporary, since aggregates
4956 -- have "by copy" semantics. If it is limited and context is an
4957 -- assignment, this is a subaggregate for an enclosing aggregate being
4958 -- expanded. It must be built in place, so use target of the current
4959 -- assignment.
4960
4961 if Is_Limited_Type (Typ)
4962 and then Parent_Kind = N_Assignment_Statement
4963 then
4964 Target_Expr := New_Copy_Tree (Name (Parent_Node));
4965 Insert_Actions (Parent_Node,
4966 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4967 Rewrite (Parent_Node, Make_Null_Statement (Loc));
4968
4969 -- Do not declare a temporary to initialize an aggregate assigned to
4970 -- a target when in-place assignment is possible, i.e. preserving the
4971 -- by-copy semantic of aggregates. This avoids large stack usage and
4972 -- generates more efficient code.
4973
4974 elsif Parent_Kind = N_Assignment_Statement
4975 and then In_Place_Assign_OK (N, Get_Base_Object (Name (Parent_Node)))
4976 then
4977 declare
4978 Lhs : constant Node_Id := Name (Parent_Node);
4979 begin
4980 -- Apply discriminant check if required
4981
4982 if Has_Discriminants (Etype (N)) then
4983 Apply_Discriminant_Check (N, Etype (Lhs), Lhs);
4984 end if;
4985
4986 -- The check just above may have replaced the aggregate with a CE
4987
4988 if Nkind (N) in N_Aggregate | N_Extension_Aggregate then
4989 Target_Expr := New_Copy_Tree (Lhs);
4990 Insert_Actions (Parent_Node,
4991 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4992 Rewrite (Parent_Node, Make_Null_Statement (Loc));
4993 end if;
4994 end;
4995
4996 else
4997 Temp := Make_Temporary (Loc, 'A', N);
4998
4999 -- If the type inherits unknown discriminants, use the view with
5000 -- known discriminants if available.
5001
5002 if Has_Unknown_Discriminants (Typ)
5003 and then Present (Underlying_Record_View (Typ))
5004 then
5005 T := Underlying_Record_View (Typ);
5006 else
5007 T := Typ;
5008 end if;
5009
5010 Instr :=
5011 Make_Object_Declaration (Loc,
5012 Defining_Identifier => Temp,
5013 Object_Definition => New_Occurrence_Of (T, Loc));
5014
5015 Set_No_Initialization (Instr);
5016 Insert_Action (N, Instr);
5017 Initialize_Discriminants (Instr, T);
5018
5019 Target_Expr := New_Occurrence_Of (Temp, Loc);
5020 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
5021
5022 -- Save the last assignment statement associated with the aggregate
5023 -- when building a controlled object. This reference is utilized by
5024 -- the finalization machinery when marking an object as successfully
5025 -- initialized.
5026
5027 if Needs_Finalization (T) then
5028 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
5029 end if;
5030
5031 Insert_Actions (N, Aggr_Code);
5032 Rewrite (N, New_Occurrence_Of (Temp, Loc));
5033 Analyze_And_Resolve (N, T);
5034 end if;
5035 end Convert_To_Assignments;
5036
5037 ---------------------------
5038 -- Convert_To_Positional --
5039 ---------------------------
5040
5041 procedure Convert_To_Positional
5042 (N : Node_Id;
5043 Handle_Bit_Packed : Boolean := False)
5044 is
5045 Typ : constant Entity_Id := Etype (N);
5046 Dims : constant Nat := Number_Dimensions (Typ);
5047 Max_Others_Replicate : constant Nat := Max_Aggregate_Size (N);
5048
5049 Static_Components : Boolean := True;
5050
5051 procedure Check_Static_Components;
5052 -- Check whether all components of the aggregate are compile-time known
5053 -- values, and can be passed as is to the back-end without further
5054 -- expansion.
5055
5056 function Flatten
5057 (N : Node_Id;
5058 Dims : Nat;
5059 Ix : Node_Id;
5060 Ixb : Node_Id) return Boolean;
5061 -- Convert the aggregate into a purely positional form if possible after
5062 -- checking that the bounds of all dimensions are known to be static.
5063
5064 function Is_Flat (N : Node_Id; Dims : Nat) return Boolean;
5065 -- Return True if the aggregate N is flat (which is not trivial in the
5066 -- case of multidimensional aggregates).
5067
5068 function Is_Static_Element (N : Node_Id; Dims : Nat) return Boolean;
5069 -- Return True if N, an element of a component association list, i.e.
5070 -- N_Component_Association or N_Iterated_Component_Association, has a
5071 -- compile-time known value and can be passed as is to the back-end
5072 -- without further expansion.
5073 -- An Iterated_Component_Association is treated as nonstatic in most
5074 -- cases for now, so there are possibilities for optimization.
5075
5076 -----------------------------
5077 -- Check_Static_Components --
5078 -----------------------------
5079
5080 -- Could use some comments in this body ???
5081
5082 procedure Check_Static_Components is
5083 Assoc : Node_Id;
5084 Expr : Node_Id;
5085
5086 begin
5087 Static_Components := True;
5088
5089 if Nkind (N) = N_String_Literal then
5090 null;
5091
5092 elsif Present (Expressions (N)) then
5093 Expr := First (Expressions (N));
5094 while Present (Expr) loop
5095 if Nkind (Expr) /= N_Aggregate
5096 or else not Compile_Time_Known_Aggregate (Expr)
5097 or else Expansion_Delayed (Expr)
5098 then
5099 Static_Components := False;
5100 exit;
5101 end if;
5102
5103 Next (Expr);
5104 end loop;
5105 end if;
5106
5107 if Nkind (N) = N_Aggregate
5108 and then Present (Component_Associations (N))
5109 then
5110 Assoc := First (Component_Associations (N));
5111 while Present (Assoc) loop
5112 if not Is_Static_Element (Assoc, Dims) then
5113 Static_Components := False;
5114 exit;
5115 end if;
5116
5117 Next (Assoc);
5118 end loop;
5119 end if;
5120 end Check_Static_Components;
5121
5122 -------------
5123 -- Flatten --
5124 -------------
5125
5126 function Flatten
5127 (N : Node_Id;
5128 Dims : Nat;
5129 Ix : Node_Id;
5130 Ixb : Node_Id) return Boolean
5131 is
5132 Loc : constant Source_Ptr := Sloc (N);
5133 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
5134 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
5135 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
5136
5137 function Cannot_Flatten_Next_Aggr (Expr : Node_Id) return Boolean;
5138 -- Return true if Expr is an aggregate for the next dimension that
5139 -- cannot be recursively flattened.
5140
5141 ------------------------------
5142 -- Cannot_Flatten_Next_Aggr --
5143 ------------------------------
5144
5145 function Cannot_Flatten_Next_Aggr (Expr : Node_Id) return Boolean is
5146 begin
5147 return Nkind (Expr) = N_Aggregate
5148 and then Present (Next_Index (Ix))
5149 and then not
5150 Flatten (Expr, Dims - 1, Next_Index (Ix), Next_Index (Ixb));
5151 end Cannot_Flatten_Next_Aggr;
5152
5153 -- Local variables
5154
5155 Lov : Uint;
5156 Hiv : Uint;
5157 Others_Present : Boolean;
5158
5159 -- Start of processing for Flatten
5160
5161 begin
5162 if Nkind (Original_Node (N)) = N_String_Literal then
5163 return True;
5164 end if;
5165
5166 if not Compile_Time_Known_Value (Lo)
5167 or else not Compile_Time_Known_Value (Hi)
5168 then
5169 return False;
5170 end if;
5171
5172 Lov := Expr_Value (Lo);
5173 Hiv := Expr_Value (Hi);
5174
5175 -- Check if there is an others choice
5176
5177 Others_Present := False;
5178
5179 if Present (Component_Associations (N)) then
5180 declare
5181 Assoc : Node_Id;
5182 Choice : Node_Id;
5183
5184 begin
5185 Assoc := First (Component_Associations (N));
5186 while Present (Assoc) loop
5187
5188 -- If this is a box association, flattening is in general
5189 -- not possible because at this point we cannot tell if the
5190 -- default is static or even exists.
5191
5192 if Box_Present (Assoc) then
5193 return False;
5194
5195 elsif Nkind (Assoc) = N_Iterated_Component_Association then
5196 return False;
5197 end if;
5198
5199 Choice := First (Choice_List (Assoc));
5200
5201 while Present (Choice) loop
5202 if Nkind (Choice) = N_Others_Choice then
5203 Others_Present := True;
5204 end if;
5205
5206 Next (Choice);
5207 end loop;
5208
5209 Next (Assoc);
5210 end loop;
5211 end;
5212 end if;
5213
5214 -- If the low bound is not known at compile time and others is not
5215 -- present we can proceed since the bounds can be obtained from the
5216 -- aggregate.
5217
5218 if Hiv < Lov
5219 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
5220 then
5221 return False;
5222 end if;
5223
5224 -- Determine if set of alternatives is suitable for conversion and
5225 -- build an array containing the values in sequence.
5226
5227 declare
5228 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
5229 of Node_Id := (others => Empty);
5230 -- The values in the aggregate sorted appropriately
5231
5232 Vlist : List_Id;
5233 -- Same data as Vals in list form
5234
5235 Rep_Count : Nat;
5236 -- Used to validate Max_Others_Replicate limit
5237
5238 Elmt : Node_Id;
5239 Expr : Node_Id;
5240 Num : Int := UI_To_Int (Lov);
5241 Choice_Index : Int;
5242 Choice : Node_Id;
5243 Lo, Hi : Node_Id;
5244
5245 begin
5246 if Present (Expressions (N)) then
5247 Elmt := First (Expressions (N));
5248 while Present (Elmt) loop
5249 -- In the case of a multidimensional array, check that the
5250 -- aggregate can be recursively flattened.
5251
5252 if Cannot_Flatten_Next_Aggr (Elmt) then
5253 return False;
5254 end if;
5255
5256 -- Duplicate expression for each index it covers
5257
5258 Vals (Num) := New_Copy_Tree (Elmt);
5259 Num := Num + 1;
5260
5261 Next (Elmt);
5262 end loop;
5263 end if;
5264
5265 if No (Component_Associations (N)) then
5266 return True;
5267 end if;
5268
5269 Elmt := First (Component_Associations (N));
5270
5271 Component_Loop : while Present (Elmt) loop
5272 Expr := Expression (Elmt);
5273
5274 -- In the case of a multidimensional array, check that the
5275 -- aggregate can be recursively flattened.
5276
5277 if Cannot_Flatten_Next_Aggr (Expr) then
5278 return False;
5279 end if;
5280
5281 Choice := First (Choice_List (Elmt));
5282 Choice_Loop : while Present (Choice) loop
5283
5284 -- If we have an others choice, fill in the missing elements
5285 -- subject to the limit established by Max_Others_Replicate.
5286
5287 if Nkind (Choice) = N_Others_Choice then
5288 Rep_Count := 0;
5289
5290 -- If the expression involves a construct that generates
5291 -- a loop, we must generate individual assignments and
5292 -- no flattening is possible.
5293
5294 if Nkind (Expr) = N_Quantified_Expression then
5295 return False;
5296 end if;
5297
5298 for J in Vals'Range loop
5299 if No (Vals (J)) then
5300 Vals (J) := New_Copy_Tree (Expr);
5301 Rep_Count := Rep_Count + 1;
5302
5303 -- Check for maximum others replication. Note that
5304 -- we skip this test if either of the restrictions
5305 -- No_Implicit_Loops or No_Elaboration_Code is
5306 -- active, if this is a preelaborable unit or
5307 -- a predefined unit, or if the unit must be
5308 -- placed in data memory. This also ensures that
5309 -- predefined units get the same level of constant
5310 -- folding in Ada 95 and Ada 2005, where their
5311 -- categorization has changed.
5312
5313 declare
5314 P : constant Entity_Id :=
5315 Cunit_Entity (Current_Sem_Unit);
5316
5317 begin
5318 -- Check if duplication is always OK and, if so,
5319 -- continue processing.
5320
5321 if Restriction_Active (No_Implicit_Loops) then
5322 null;
5323
5324 -- If duplication is not always OK, continue
5325 -- only if either the element is static or is
5326 -- an aggregate (we already know it is OK).
5327
5328 elsif not Is_Static_Element (Elmt, Dims)
5329 and then Nkind (Expr) /= N_Aggregate
5330 then
5331 return False;
5332
5333 -- Check if duplication is OK for elaboration
5334 -- purposes and, if so, continue processing.
5335
5336 elsif Restriction_Active (No_Elaboration_Code)
5337 or else
5338 (Ekind (Current_Scope) = E_Package
5339 and then
5340 Static_Elaboration_Desired (Current_Scope))
5341 or else Is_Preelaborated (P)
5342 or else (Ekind (P) = E_Package_Body
5343 and then
5344 Is_Preelaborated (Spec_Entity (P)))
5345 or else
5346 Is_Predefined_Unit (Get_Source_Unit (P))
5347 then
5348 null;
5349
5350 -- Otherwise, check that the replication count
5351 -- is not too high.
5352
5353 elsif Rep_Count > Max_Others_Replicate then
5354 return False;
5355 end if;
5356 end;
5357 end if;
5358 end loop;
5359
5360 if Rep_Count = 0
5361 and then Warn_On_Redundant_Constructs
5362 then
5363 Error_Msg_N ("there are no others?r?", Elmt);
5364 end if;
5365
5366 exit Component_Loop;
5367
5368 -- Case of a subtype mark, identifier or expanded name
5369
5370 elsif Is_Entity_Name (Choice)
5371 and then Is_Type (Entity (Choice))
5372 then
5373 Lo := Type_Low_Bound (Etype (Choice));
5374 Hi := Type_High_Bound (Etype (Choice));
5375
5376 -- Case of subtype indication
5377
5378 elsif Nkind (Choice) = N_Subtype_Indication then
5379 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
5380 Hi := High_Bound (Range_Expression (Constraint (Choice)));
5381
5382 -- Case of a range
5383
5384 elsif Nkind (Choice) = N_Range then
5385 Lo := Low_Bound (Choice);
5386 Hi := High_Bound (Choice);
5387
5388 -- Normal subexpression case
5389
5390 else pragma Assert (Nkind (Choice) in N_Subexpr);
5391 if not Compile_Time_Known_Value (Choice) then
5392 return False;
5393
5394 else
5395 Choice_Index := UI_To_Int (Expr_Value (Choice));
5396
5397 if Choice_Index in Vals'Range then
5398 Vals (Choice_Index) := New_Copy_Tree (Expr);
5399 goto Continue;
5400
5401 -- Choice is statically out-of-range, will be
5402 -- rewritten to raise Constraint_Error.
5403
5404 else
5405 return False;
5406 end if;
5407 end if;
5408 end if;
5409
5410 -- Range cases merge with Lo,Hi set
5411
5412 if not Compile_Time_Known_Value (Lo)
5413 or else
5414 not Compile_Time_Known_Value (Hi)
5415 then
5416 return False;
5417
5418 else
5419 for J in UI_To_Int (Expr_Value (Lo)) ..
5420 UI_To_Int (Expr_Value (Hi))
5421 loop
5422 Vals (J) := New_Copy_Tree (Expr);
5423 end loop;
5424 end if;
5425
5426 <<Continue>>
5427 Next (Choice);
5428 end loop Choice_Loop;
5429
5430 Next (Elmt);
5431 end loop Component_Loop;
5432
5433 -- If we get here the conversion is possible
5434
5435 Vlist := New_List;
5436 for J in Vals'Range loop
5437 Append (Vals (J), Vlist);
5438 end loop;
5439
5440 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
5441 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
5442 return True;
5443 end;
5444 end Flatten;
5445
5446 -------------
5447 -- Is_Flat --
5448 -------------
5449
5450 function Is_Flat (N : Node_Id; Dims : Nat) return Boolean is
5451 Elmt : Node_Id;
5452
5453 begin
5454 if Dims = 0 then
5455 return True;
5456
5457 elsif Nkind (N) = N_Aggregate then
5458 if Present (Component_Associations (N)) then
5459 return False;
5460
5461 else
5462 Elmt := First (Expressions (N));
5463 while Present (Elmt) loop
5464 if not Is_Flat (Elmt, Dims - 1) then
5465 return False;
5466 end if;
5467
5468 Next (Elmt);
5469 end loop;
5470
5471 return True;
5472 end if;
5473 else
5474 return True;
5475 end if;
5476 end Is_Flat;
5477
5478 -------------------------
5479 -- Is_Static_Element --
5480 -------------------------
5481
5482 function Is_Static_Element (N : Node_Id; Dims : Nat) return Boolean is
5483 Expr : constant Node_Id := Expression (N);
5484
5485 begin
5486 -- In most cases the interesting expressions are unambiguously static
5487
5488 if Compile_Time_Known_Value (Expr) then
5489 return True;
5490
5491 elsif Nkind (N) = N_Iterated_Component_Association then
5492 return False;
5493
5494 elsif Nkind (Expr) = N_Aggregate
5495 and then Compile_Time_Known_Aggregate (Expr)
5496 and then not Expansion_Delayed (Expr)
5497 then
5498 return True;
5499
5500 -- However, one may write static expressions that are syntactically
5501 -- ambiguous, so preanalyze the expression before checking it again,
5502 -- but only at the innermost level for a multidimensional array.
5503
5504 elsif Dims = 1 then
5505 Preanalyze_And_Resolve (Expr, Component_Type (Typ));
5506 return Compile_Time_Known_Value (Expr);
5507
5508 else
5509 return False;
5510 end if;
5511 end Is_Static_Element;
5512
5513 -- Start of processing for Convert_To_Positional
5514
5515 begin
5516 -- Only convert to positional when generating C in case of an
5517 -- object declaration, this is the only case where aggregates are
5518 -- supported in C.
5519
5520 if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
5521 return;
5522 end if;
5523
5524 -- Ada 2005 (AI-287): Do not convert in case of default initialized
5525 -- components because in this case will need to call the corresponding
5526 -- IP procedure.
5527
5528 if Has_Default_Init_Comps (N) then
5529 return;
5530 end if;
5531
5532 -- A subaggregate may have been flattened but is not known to be
5533 -- Compile_Time_Known. Set that flag in cases that cannot require
5534 -- elaboration code, so that the aggregate can be used as the
5535 -- initial value of a thread-local variable.
5536
5537 if Is_Flat (N, Dims) then
5538 if Static_Array_Aggregate (N) then
5539 Set_Compile_Time_Known_Aggregate (N);
5540 end if;
5541
5542 return;
5543 end if;
5544
5545 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
5546 return;
5547 end if;
5548
5549 -- Do not convert to positional if controlled components are involved
5550 -- since these require special processing
5551
5552 if Has_Controlled_Component (Typ) then
5553 return;
5554 end if;
5555
5556 Check_Static_Components;
5557
5558 -- If the size is known, or all the components are static, try to
5559 -- build a fully positional aggregate.
5560
5561 -- The size of the type may not be known for an aggregate with
5562 -- discriminated array components, but if the components are static
5563 -- it is still possible to verify statically that the length is
5564 -- compatible with the upper bound of the type, and therefore it is
5565 -- worth flattening such aggregates as well.
5566
5567 if Aggr_Size_OK (N)
5568 and then
5569 Flatten (N, Dims, First_Index (Typ), First_Index (Base_Type (Typ)))
5570 then
5571 if Static_Components then
5572 Set_Compile_Time_Known_Aggregate (N);
5573 Set_Expansion_Delayed (N, False);
5574 end if;
5575
5576 Analyze_And_Resolve (N, Typ);
5577 end if;
5578
5579 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
5580 -- that will still require initialization code.
5581
5582 if (Ekind (Current_Scope) = E_Package
5583 and then Static_Elaboration_Desired (Current_Scope))
5584 and then Nkind (Parent (N)) = N_Object_Declaration
5585 then
5586 declare
5587 Expr : Node_Id;
5588
5589 begin
5590 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
5591 Expr := First (Expressions (N));
5592 while Present (Expr) loop
5593 if not Compile_Time_Known_Value (Expr) then
5594 Error_Msg_N
5595 ("non-static object requires elaboration code??", N);
5596 exit;
5597 end if;
5598
5599 Next (Expr);
5600 end loop;
5601
5602 if Present (Component_Associations (N)) then
5603 Error_Msg_N ("object requires elaboration code??", N);
5604 end if;
5605 end if;
5606 end;
5607 end if;
5608 end Convert_To_Positional;
5609
5610 ----------------------------
5611 -- Expand_Array_Aggregate --
5612 ----------------------------
5613
5614 -- Array aggregate expansion proceeds as follows:
5615
5616 -- 1. If requested we generate code to perform all the array aggregate
5617 -- bound checks, specifically
5618
5619 -- (a) Check that the index range defined by aggregate bounds is
5620 -- compatible with corresponding index subtype.
5621
5622 -- (b) If an others choice is present check that no aggregate
5623 -- index is outside the bounds of the index constraint.
5624
5625 -- (c) For multidimensional arrays make sure that all subaggregates
5626 -- corresponding to the same dimension have the same bounds.
5627
5628 -- 2. Check for packed array aggregate which can be converted to a
5629 -- constant so that the aggregate disappears completely.
5630
5631 -- 3. Check case of nested aggregate. Generally nested aggregates are
5632 -- handled during the processing of the parent aggregate.
5633
5634 -- 4. Check if the aggregate can be statically processed. If this is the
5635 -- case pass it as is to Gigi. Note that a necessary condition for
5636 -- static processing is that the aggregate be fully positional.
5637
5638 -- 5. If in-place aggregate expansion is possible (i.e. no need to create
5639 -- a temporary) then mark the aggregate as such and return. Otherwise
5640 -- create a new temporary and generate the appropriate initialization
5641 -- code.
5642
5643 procedure Expand_Array_Aggregate (N : Node_Id) is
5644 Loc : constant Source_Ptr := Sloc (N);
5645
5646 Typ : constant Entity_Id := Etype (N);
5647 Ctyp : constant Entity_Id := Component_Type (Typ);
5648 -- Typ is the correct constrained array subtype of the aggregate
5649 -- Ctyp is the corresponding component type.
5650
5651 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
5652 -- Number of aggregate index dimensions
5653
5654 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
5655 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
5656 -- Low and High bounds of the constraint for each aggregate index
5657
5658 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
5659 -- The type of each index
5660
5661 In_Place_Assign_OK_For_Declaration : Boolean := False;
5662 -- True if we are to generate an in-place assignment for a declaration
5663
5664 Maybe_In_Place_OK : Boolean;
5665 -- If the type is neither controlled nor packed and the aggregate
5666 -- is the expression in an assignment, assignment in place may be
5667 -- possible, provided other conditions are met on the LHS.
5668
5669 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
5670 (others => False);
5671 -- If Others_Present (J) is True, then there is an others choice in one
5672 -- of the subaggregates of N at dimension J.
5673
5674 procedure Build_Constrained_Type (Positional : Boolean);
5675 -- If the subtype is not static or unconstrained, build a constrained
5676 -- type using the computable sizes of the aggregate and its sub-
5677 -- aggregates.
5678
5679 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
5680 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
5681 -- by Index_Bounds.
5682
5683 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
5684 -- Checks that in a multidimensional array aggregate all subaggregates
5685 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
5686 -- an array subaggregate. Dim is the dimension corresponding to the
5687 -- subaggregate.
5688
5689 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
5690 -- Computes the values of array Others_Present. Sub_Aggr is the array
5691 -- subaggregate we start the computation from. Dim is the dimension
5692 -- corresponding to the subaggregate.
5693
5694 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
5695 -- Checks that if an others choice is present in any subaggregate, no
5696 -- aggregate index is outside the bounds of the index constraint.
5697 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
5698 -- to the subaggregate.
5699
5700 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
5701 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
5702 -- built directly into the target of the assignment it must be free
5703 -- of side effects. N is the LHS of an assignment.
5704
5705 ----------------------------
5706 -- Build_Constrained_Type --
5707 ----------------------------
5708
5709 procedure Build_Constrained_Type (Positional : Boolean) is
5710 Loc : constant Source_Ptr := Sloc (N);
5711 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
5712 Comp : Node_Id;
5713 Decl : Node_Id;
5714 Typ : constant Entity_Id := Etype (N);
5715 Indexes : constant List_Id := New_List;
5716 Num : Nat;
5717 Sub_Agg : Node_Id;
5718
5719 begin
5720 -- If the aggregate is purely positional, all its subaggregates
5721 -- have the same size. We collect the dimensions from the first
5722 -- subaggregate at each level.
5723
5724 if Positional then
5725 Sub_Agg := N;
5726
5727 for D in 1 .. Number_Dimensions (Typ) loop
5728 Sub_Agg := First (Expressions (Sub_Agg));
5729
5730 Comp := Sub_Agg;
5731 Num := 0;
5732 while Present (Comp) loop
5733 Num := Num + 1;
5734 Next (Comp);
5735 end loop;
5736
5737 Append_To (Indexes,
5738 Make_Range (Loc,
5739 Low_Bound => Make_Integer_Literal (Loc, 1),
5740 High_Bound => Make_Integer_Literal (Loc, Num)));
5741 end loop;
5742
5743 else
5744 -- We know the aggregate type is unconstrained and the aggregate
5745 -- is not processable by the back end, therefore not necessarily
5746 -- positional. Retrieve each dimension bounds (computed earlier).
5747
5748 for D in 1 .. Number_Dimensions (Typ) loop
5749 Append_To (Indexes,
5750 Make_Range (Loc,
5751 Low_Bound => Aggr_Low (D),
5752 High_Bound => Aggr_High (D)));
5753 end loop;
5754 end if;
5755
5756 Decl :=
5757 Make_Full_Type_Declaration (Loc,
5758 Defining_Identifier => Agg_Type,
5759 Type_Definition =>
5760 Make_Constrained_Array_Definition (Loc,
5761 Discrete_Subtype_Definitions => Indexes,
5762 Component_Definition =>
5763 Make_Component_Definition (Loc,
5764 Aliased_Present => False,
5765 Subtype_Indication =>
5766 New_Occurrence_Of (Component_Type (Typ), Loc))));
5767
5768 Insert_Action (N, Decl);
5769 Analyze (Decl);
5770 Set_Etype (N, Agg_Type);
5771 Set_Is_Itype (Agg_Type);
5772 Freeze_Itype (Agg_Type, N);
5773 end Build_Constrained_Type;
5774
5775 ------------------
5776 -- Check_Bounds --
5777 ------------------
5778
5779 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
5780 Aggr_Lo : Node_Id;
5781 Aggr_Hi : Node_Id;
5782
5783 Ind_Lo : Node_Id;
5784 Ind_Hi : Node_Id;
5785
5786 Cond : Node_Id := Empty;
5787
5788 begin
5789 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
5790 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
5791
5792 -- Generate the following test:
5793
5794 -- [constraint_error when
5795 -- Aggr_Lo <= Aggr_Hi and then
5796 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5797
5798 -- As an optimization try to see if some tests are trivially vacuous
5799 -- because we are comparing an expression against itself.
5800
5801 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
5802 Cond := Empty;
5803
5804 elsif Aggr_Hi = Ind_Hi then
5805 Cond :=
5806 Make_Op_Lt (Loc,
5807 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5808 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
5809
5810 elsif Aggr_Lo = Ind_Lo then
5811 Cond :=
5812 Make_Op_Gt (Loc,
5813 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5814 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
5815
5816 else
5817 Cond :=
5818 Make_Or_Else (Loc,
5819 Left_Opnd =>
5820 Make_Op_Lt (Loc,
5821 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5822 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
5823
5824 Right_Opnd =>
5825 Make_Op_Gt (Loc,
5826 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5827 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
5828 end if;
5829
5830 if Present (Cond) then
5831 Cond :=
5832 Make_And_Then (Loc,
5833 Left_Opnd =>
5834 Make_Op_Le (Loc,
5835 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5836 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
5837
5838 Right_Opnd => Cond);
5839
5840 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5841 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5842 Insert_Action (N,
5843 Make_Raise_Constraint_Error (Loc,
5844 Condition => Cond,
5845 Reason => CE_Range_Check_Failed));
5846 end if;
5847 end Check_Bounds;
5848
5849 ----------------------------
5850 -- Check_Same_Aggr_Bounds --
5851 ----------------------------
5852
5853 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5854 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
5855 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
5856 -- The bounds of this specific subaggregate
5857
5858 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5859 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5860 -- The bounds of the aggregate for this dimension
5861
5862 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5863 -- The index type for this dimension.xxx
5864
5865 Cond : Node_Id := Empty;
5866 Assoc : Node_Id;
5867 Expr : Node_Id;
5868
5869 begin
5870 -- If index checks are on generate the test
5871
5872 -- [constraint_error when
5873 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5874
5875 -- As an optimization try to see if some tests are trivially vacuos
5876 -- because we are comparing an expression against itself. Also for
5877 -- the first dimension the test is trivially vacuous because there
5878 -- is just one aggregate for dimension 1.
5879
5880 if Index_Checks_Suppressed (Ind_Typ) then
5881 Cond := Empty;
5882
5883 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5884 then
5885 Cond := Empty;
5886
5887 elsif Aggr_Hi = Sub_Hi then
5888 Cond :=
5889 Make_Op_Ne (Loc,
5890 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5891 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5892
5893 elsif Aggr_Lo = Sub_Lo then
5894 Cond :=
5895 Make_Op_Ne (Loc,
5896 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5897 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5898
5899 else
5900 Cond :=
5901 Make_Or_Else (Loc,
5902 Left_Opnd =>
5903 Make_Op_Ne (Loc,
5904 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5905 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5906
5907 Right_Opnd =>
5908 Make_Op_Ne (Loc,
5909 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5910 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5911 end if;
5912
5913 if Present (Cond) then
5914 Insert_Action (N,
5915 Make_Raise_Constraint_Error (Loc,
5916 Condition => Cond,
5917 Reason => CE_Length_Check_Failed));
5918 end if;
5919
5920 -- Now look inside the subaggregate to see if there is more work
5921
5922 if Dim < Aggr_Dimension then
5923
5924 -- Process positional components
5925
5926 if Present (Expressions (Sub_Aggr)) then
5927 Expr := First (Expressions (Sub_Aggr));
5928 while Present (Expr) loop
5929 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5930 Next (Expr);
5931 end loop;
5932 end if;
5933
5934 -- Process component associations
5935
5936 if Present (Component_Associations (Sub_Aggr)) then
5937 Assoc := First (Component_Associations (Sub_Aggr));
5938 while Present (Assoc) loop
5939 Expr := Expression (Assoc);
5940 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5941 Next (Assoc);
5942 end loop;
5943 end if;
5944 end if;
5945 end Check_Same_Aggr_Bounds;
5946
5947 ----------------------------
5948 -- Compute_Others_Present --
5949 ----------------------------
5950
5951 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5952 Assoc : Node_Id;
5953 Expr : Node_Id;
5954
5955 begin
5956 if Present (Component_Associations (Sub_Aggr)) then
5957 Assoc := Last (Component_Associations (Sub_Aggr));
5958
5959 if Nkind (First (Choice_List (Assoc))) = N_Others_Choice then
5960 Others_Present (Dim) := True;
5961
5962 -- An others_clause may be superfluous if previous components
5963 -- cover the full given range of a constrained array. In such
5964 -- a case an others_clause does not contribute any additional
5965 -- components and has not been analyzed. We analyze it now to
5966 -- detect type errors in the expression, even though no code
5967 -- will be generated for it.
5968
5969 if Dim = Aggr_Dimension
5970 and then Nkind (Assoc) /= N_Iterated_Component_Association
5971 and then not Analyzed (Expression (Assoc))
5972 and then not Box_Present (Assoc)
5973 then
5974 Preanalyze_And_Resolve (Expression (Assoc), Ctyp);
5975 end if;
5976 end if;
5977 end if;
5978
5979 -- Now look inside the subaggregate to see if there is more work
5980
5981 if Dim < Aggr_Dimension then
5982
5983 -- Process positional components
5984
5985 if Present (Expressions (Sub_Aggr)) then
5986 Expr := First (Expressions (Sub_Aggr));
5987 while Present (Expr) loop
5988 Compute_Others_Present (Expr, Dim + 1);
5989 Next (Expr);
5990 end loop;
5991 end if;
5992
5993 -- Process component associations
5994
5995 if Present (Component_Associations (Sub_Aggr)) then
5996 Assoc := First (Component_Associations (Sub_Aggr));
5997 while Present (Assoc) loop
5998 Expr := Expression (Assoc);
5999 Compute_Others_Present (Expr, Dim + 1);
6000 Next (Assoc);
6001 end loop;
6002 end if;
6003 end if;
6004 end Compute_Others_Present;
6005
6006 ------------------
6007 -- Others_Check --
6008 ------------------
6009
6010 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
6011 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
6012 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
6013 -- The bounds of the aggregate for this dimension
6014
6015 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
6016 -- The index type for this dimension
6017
6018 Need_To_Check : Boolean := False;
6019
6020 Choices_Lo : Node_Id := Empty;
6021 Choices_Hi : Node_Id := Empty;
6022 -- The lowest and highest discrete choices for a named subaggregate
6023
6024 Nb_Choices : Int := -1;
6025 -- The number of discrete non-others choices in this subaggregate
6026
6027 Nb_Elements : Uint := Uint_0;
6028 -- The number of elements in a positional aggregate
6029
6030 Cond : Node_Id := Empty;
6031
6032 Assoc : Node_Id;
6033 Choice : Node_Id;
6034 Expr : Node_Id;
6035
6036 begin
6037 -- Check if we have an others choice. If we do make sure that this
6038 -- subaggregate contains at least one element in addition to the
6039 -- others choice.
6040
6041 if Range_Checks_Suppressed (Ind_Typ) then
6042 Need_To_Check := False;
6043
6044 elsif Present (Expressions (Sub_Aggr))
6045 and then Present (Component_Associations (Sub_Aggr))
6046 then
6047 Need_To_Check := True;
6048
6049 elsif Present (Component_Associations (Sub_Aggr)) then
6050 Assoc := Last (Component_Associations (Sub_Aggr));
6051
6052 if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then
6053 Need_To_Check := False;
6054
6055 else
6056 -- Count the number of discrete choices. Start with -1 because
6057 -- the others choice does not count.
6058
6059 -- Is there some reason we do not use List_Length here ???
6060
6061 Nb_Choices := -1;
6062 Assoc := First (Component_Associations (Sub_Aggr));
6063 while Present (Assoc) loop
6064 Choice := First (Choice_List (Assoc));
6065 while Present (Choice) loop
6066 Nb_Choices := Nb_Choices + 1;
6067 Next (Choice);
6068 end loop;
6069
6070 Next (Assoc);
6071 end loop;
6072
6073 -- If there is only an others choice nothing to do
6074
6075 Need_To_Check := (Nb_Choices > 0);
6076 end if;
6077
6078 else
6079 Need_To_Check := False;
6080 end if;
6081
6082 -- If we are dealing with a positional subaggregate with an others
6083 -- choice then compute the number or positional elements.
6084
6085 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
6086 Expr := First (Expressions (Sub_Aggr));
6087 Nb_Elements := Uint_0;
6088 while Present (Expr) loop
6089 Nb_Elements := Nb_Elements + 1;
6090 Next (Expr);
6091 end loop;
6092
6093 -- If the aggregate contains discrete choices and an others choice
6094 -- compute the smallest and largest discrete choice values.
6095
6096 elsif Need_To_Check then
6097 Compute_Choices_Lo_And_Choices_Hi : declare
6098
6099 Table : Case_Table_Type (1 .. Nb_Choices);
6100 -- Used to sort all the different choice values
6101
6102 J : Pos := 1;
6103 Low : Node_Id;
6104 High : Node_Id;
6105
6106 begin
6107 Assoc := First (Component_Associations (Sub_Aggr));
6108 while Present (Assoc) loop
6109 Choice := First (Choice_List (Assoc));
6110 while Present (Choice) loop
6111 if Nkind (Choice) = N_Others_Choice then
6112 exit;
6113 end if;
6114
6115 Get_Index_Bounds (Choice, Low, High);
6116 Table (J).Choice_Lo := Low;
6117 Table (J).Choice_Hi := High;
6118
6119 J := J + 1;
6120 Next (Choice);
6121 end loop;
6122
6123 Next (Assoc);
6124 end loop;
6125
6126 -- Sort the discrete choices
6127
6128 Sort_Case_Table (Table);
6129
6130 Choices_Lo := Table (1).Choice_Lo;
6131 Choices_Hi := Table (Nb_Choices).Choice_Hi;
6132 end Compute_Choices_Lo_And_Choices_Hi;
6133 end if;
6134
6135 -- If no others choice in this subaggregate, or the aggregate
6136 -- comprises only an others choice, nothing to do.
6137
6138 if not Need_To_Check then
6139 Cond := Empty;
6140
6141 -- If we are dealing with an aggregate containing an others choice
6142 -- and positional components, we generate the following test:
6143
6144 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
6145 -- Ind_Typ'Pos (Aggr_Hi)
6146 -- then
6147 -- raise Constraint_Error;
6148 -- end if;
6149
6150 -- in the general case, but the following simpler test:
6151
6152 -- [constraint_error when
6153 -- Aggr_Lo + (Nb_Elements - 1) > Aggr_Hi];
6154
6155 -- instead if the index type is a signed integer.
6156
6157 elsif Nb_Elements > Uint_0 then
6158 if Nb_Elements = Uint_1 then
6159 Cond :=
6160 Make_Op_Gt (Loc,
6161 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
6162 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi));
6163
6164 elsif Is_Signed_Integer_Type (Ind_Typ) then
6165 Cond :=
6166 Make_Op_Gt (Loc,
6167 Left_Opnd =>
6168 Make_Op_Add (Loc,
6169 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
6170 Right_Opnd =>
6171 Make_Integer_Literal (Loc, Nb_Elements - 1)),
6172 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi));
6173
6174 else
6175 Cond :=
6176 Make_Op_Gt (Loc,
6177 Left_Opnd =>
6178 Make_Op_Add (Loc,
6179 Left_Opnd =>
6180 Make_Attribute_Reference (Loc,
6181 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
6182 Attribute_Name => Name_Pos,
6183 Expressions =>
6184 New_List
6185 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
6186 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
6187
6188 Right_Opnd =>
6189 Make_Attribute_Reference (Loc,
6190 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
6191 Attribute_Name => Name_Pos,
6192 Expressions => New_List (
6193 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
6194 end if;
6195
6196 -- If we are dealing with an aggregate containing an others choice
6197 -- and discrete choices we generate the following test:
6198
6199 -- [constraint_error when
6200 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
6201
6202 else
6203 Cond :=
6204 Make_Or_Else (Loc,
6205 Left_Opnd =>
6206 Make_Op_Lt (Loc,
6207 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
6208 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
6209
6210 Right_Opnd =>
6211 Make_Op_Gt (Loc,
6212 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
6213 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
6214 end if;
6215
6216 if Present (Cond) then
6217 Insert_Action (N,
6218 Make_Raise_Constraint_Error (Loc,
6219 Condition => Cond,
6220 Reason => CE_Length_Check_Failed));
6221 -- Questionable reason code, shouldn't that be a
6222 -- CE_Range_Check_Failed ???
6223 end if;
6224
6225 -- Now look inside the subaggregate to see if there is more work
6226
6227 if Dim < Aggr_Dimension then
6228
6229 -- Process positional components
6230
6231 if Present (Expressions (Sub_Aggr)) then
6232 Expr := First (Expressions (Sub_Aggr));
6233 while Present (Expr) loop
6234 Others_Check (Expr, Dim + 1);
6235 Next (Expr);
6236 end loop;
6237 end if;
6238
6239 -- Process component associations
6240
6241 if Present (Component_Associations (Sub_Aggr)) then
6242 Assoc := First (Component_Associations (Sub_Aggr));
6243 while Present (Assoc) loop
6244 Expr := Expression (Assoc);
6245 Others_Check (Expr, Dim + 1);
6246 Next (Assoc);
6247 end loop;
6248 end if;
6249 end if;
6250 end Others_Check;
6251
6252 -------------------------
6253 -- Safe_Left_Hand_Side --
6254 -------------------------
6255
6256 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
6257 function Is_Safe_Index (Indx : Node_Id) return Boolean;
6258 -- If the left-hand side includes an indexed component, check that
6259 -- the indexes are free of side effects.
6260
6261 -------------------
6262 -- Is_Safe_Index --
6263 -------------------
6264
6265 function Is_Safe_Index (Indx : Node_Id) return Boolean is
6266 begin
6267 if Is_Entity_Name (Indx) then
6268 return True;
6269
6270 elsif Nkind (Indx) = N_Integer_Literal then
6271 return True;
6272
6273 elsif Nkind (Indx) = N_Function_Call
6274 and then Is_Entity_Name (Name (Indx))
6275 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
6276 then
6277 return True;
6278
6279 elsif Nkind (Indx) = N_Type_Conversion
6280 and then Is_Safe_Index (Expression (Indx))
6281 then
6282 return True;
6283
6284 else
6285 return False;
6286 end if;
6287 end Is_Safe_Index;
6288
6289 -- Start of processing for Safe_Left_Hand_Side
6290
6291 begin
6292 if Is_Entity_Name (N) then
6293 return True;
6294
6295 elsif Nkind (N) in N_Explicit_Dereference | N_Selected_Component
6296 and then Safe_Left_Hand_Side (Prefix (N))
6297 then
6298 return True;
6299
6300 elsif Nkind (N) = N_Indexed_Component
6301 and then Safe_Left_Hand_Side (Prefix (N))
6302 and then Is_Safe_Index (First (Expressions (N)))
6303 then
6304 return True;
6305
6306 elsif Nkind (N) = N_Unchecked_Type_Conversion then
6307 return Safe_Left_Hand_Side (Expression (N));
6308
6309 else
6310 return False;
6311 end if;
6312 end Safe_Left_Hand_Side;
6313
6314 -- Local variables
6315
6316 Tmp : Entity_Id;
6317 -- Holds the temporary aggregate value
6318
6319 Tmp_Decl : Node_Id;
6320 -- Holds the declaration of Tmp
6321
6322 Aggr_Code : List_Id;
6323 Parent_Node : Node_Id;
6324 Parent_Kind : Node_Kind;
6325
6326 -- Start of processing for Expand_Array_Aggregate
6327
6328 begin
6329 -- Do not touch the special aggregates of attributes used for Asm calls
6330
6331 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
6332 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
6333 then
6334 return;
6335
6336 -- Do not expand an aggregate for an array type which contains tasks if
6337 -- the aggregate is associated with an unexpanded return statement of a
6338 -- build-in-place function. The aggregate is expanded when the related
6339 -- return statement (rewritten into an extended return) is processed.
6340 -- This delay ensures that any temporaries and initialization code
6341 -- generated for the aggregate appear in the proper return block and
6342 -- use the correct _chain and _master.
6343
6344 elsif Has_Task (Base_Type (Etype (N)))
6345 and then Nkind (Parent (N)) = N_Simple_Return_Statement
6346 and then Is_Build_In_Place_Function
6347 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
6348 then
6349 return;
6350
6351 -- Do not attempt expansion if error already detected. We may reach this
6352 -- point in spite of previous errors when compiling with -gnatq, to
6353 -- force all possible errors (this is the usual ACATS mode).
6354
6355 elsif Error_Posted (N) then
6356 return;
6357 end if;
6358
6359 -- If the semantic analyzer has determined that aggregate N will raise
6360 -- Constraint_Error at run time, then the aggregate node has been
6361 -- replaced with an N_Raise_Constraint_Error node and we should
6362 -- never get here.
6363
6364 pragma Assert (not Raises_Constraint_Error (N));
6365
6366 -- STEP 1a
6367
6368 -- Check that the index range defined by aggregate bounds is
6369 -- compatible with corresponding index subtype.
6370
6371 Index_Compatibility_Check : declare
6372 Aggr_Index_Range : Node_Id := First_Index (Typ);
6373 -- The current aggregate index range
6374
6375 Index_Constraint : Node_Id := First_Index (Etype (Typ));
6376 -- The corresponding index constraint against which we have to
6377 -- check the above aggregate index range.
6378
6379 begin
6380 Compute_Others_Present (N, 1);
6381
6382 for J in 1 .. Aggr_Dimension loop
6383 -- There is no need to emit a check if an others choice is present
6384 -- for this array aggregate dimension since in this case one of
6385 -- N's subaggregates has taken its bounds from the context and
6386 -- these bounds must have been checked already. In addition all
6387 -- subaggregates corresponding to the same dimension must all have
6388 -- the same bounds (checked in (c) below).
6389
6390 if not Range_Checks_Suppressed (Etype (Index_Constraint))
6391 and then not Others_Present (J)
6392 then
6393 -- We don't use Checks.Apply_Range_Check here because it emits
6394 -- a spurious check. Namely it checks that the range defined by
6395 -- the aggregate bounds is nonempty. But we know this already
6396 -- if we get here.
6397
6398 Check_Bounds (Aggr_Index_Range, Index_Constraint);
6399 end if;
6400
6401 -- Save the low and high bounds of the aggregate index as well as
6402 -- the index type for later use in checks (b) and (c) below.
6403
6404 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
6405 Aggr_High (J) := High_Bound (Aggr_Index_Range);
6406
6407 Aggr_Index_Typ (J) := Etype (Index_Constraint);
6408
6409 Next_Index (Aggr_Index_Range);
6410 Next_Index (Index_Constraint);
6411 end loop;
6412 end Index_Compatibility_Check;
6413
6414 -- STEP 1b
6415
6416 -- If an others choice is present check that no aggregate index is
6417 -- outside the bounds of the index constraint.
6418
6419 Others_Check (N, 1);
6420
6421 -- STEP 1c
6422
6423 -- For multidimensional arrays make sure that all subaggregates
6424 -- corresponding to the same dimension have the same bounds.
6425
6426 if Aggr_Dimension > 1 then
6427 Check_Same_Aggr_Bounds (N, 1);
6428 end if;
6429
6430 -- STEP 1d
6431
6432 -- If we have a default component value, or simple initialization is
6433 -- required for the component type, then we replace <> in component
6434 -- associations by the required default value.
6435
6436 declare
6437 Default_Val : Node_Id;
6438 Assoc : Node_Id;
6439
6440 begin
6441 if (Present (Default_Aspect_Component_Value (Typ))
6442 or else Needs_Simple_Initialization (Ctyp))
6443 and then Present (Component_Associations (N))
6444 then
6445 Assoc := First (Component_Associations (N));
6446 while Present (Assoc) loop
6447 if Nkind (Assoc) = N_Component_Association
6448 and then Box_Present (Assoc)
6449 then
6450 Set_Box_Present (Assoc, False);
6451
6452 if Present (Default_Aspect_Component_Value (Typ)) then
6453 Default_Val := Default_Aspect_Component_Value (Typ);
6454 else
6455 Default_Val := Get_Simple_Init_Val (Ctyp, N);
6456 end if;
6457
6458 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
6459 Analyze_And_Resolve (Expression (Assoc), Ctyp);
6460 end if;
6461
6462 Next (Assoc);
6463 end loop;
6464 end if;
6465 end;
6466
6467 -- STEP 2
6468
6469 -- Here we test for is packed array aggregate that we can handle at
6470 -- compile time. If so, return with transformation done. Note that we do
6471 -- this even if the aggregate is nested, because once we have done this
6472 -- processing, there is no more nested aggregate.
6473
6474 if Packed_Array_Aggregate_Handled (N) then
6475 return;
6476 end if;
6477
6478 -- At this point we try to convert to positional form
6479
6480 Convert_To_Positional (N);
6481
6482 -- If the result is no longer an aggregate (e.g. it may be a string
6483 -- literal, or a temporary which has the needed value), then we are
6484 -- done, since there is no longer a nested aggregate.
6485
6486 if Nkind (N) /= N_Aggregate then
6487 return;
6488
6489 -- We are also done if the result is an analyzed aggregate, indicating
6490 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6491 -- aggregate.
6492
6493 elsif Analyzed (N) and then Is_Rewrite_Substitution (N) then
6494 return;
6495 end if;
6496
6497 -- If all aggregate components are compile-time known and the aggregate
6498 -- has been flattened, nothing left to do. The same occurs if the
6499 -- aggregate is used to initialize the components of a statically
6500 -- allocated dispatch table.
6501
6502 if Compile_Time_Known_Aggregate (N)
6503 or else Is_Static_Dispatch_Table_Aggregate (N)
6504 then
6505 Set_Expansion_Delayed (N, False);
6506 return;
6507 end if;
6508
6509 -- Now see if back end processing is possible
6510
6511 if Backend_Processing_Possible (N) then
6512
6513 -- If the aggregate is static but the constraints are not, build
6514 -- a static subtype for the aggregate, so that Gigi can place it
6515 -- in static memory. Perform an unchecked_conversion to the non-
6516 -- static type imposed by the context.
6517
6518 declare
6519 Itype : constant Entity_Id := Etype (N);
6520 Index : Node_Id;
6521 Needs_Type : Boolean := False;
6522
6523 begin
6524 Index := First_Index (Itype);
6525 while Present (Index) loop
6526 if not Is_OK_Static_Subtype (Etype (Index)) then
6527 Needs_Type := True;
6528 exit;
6529 else
6530 Next_Index (Index);
6531 end if;
6532 end loop;
6533
6534 if Needs_Type then
6535 Build_Constrained_Type (Positional => True);
6536 Rewrite (N, Unchecked_Convert_To (Itype, N));
6537 Analyze (N);
6538 end if;
6539 end;
6540
6541 return;
6542 end if;
6543
6544 -- STEP 3
6545
6546 -- Delay expansion for nested aggregates: it will be taken care of when
6547 -- the parent aggregate is expanded.
6548
6549 Parent_Node := Parent (N);
6550 Parent_Kind := Nkind (Parent_Node);
6551
6552 if Parent_Kind = N_Qualified_Expression then
6553 Parent_Node := Parent (Parent_Node);
6554 Parent_Kind := Nkind (Parent_Node);
6555 end if;
6556
6557 if Parent_Kind = N_Aggregate
6558 or else Parent_Kind = N_Extension_Aggregate
6559 or else Parent_Kind = N_Component_Association
6560 or else (Parent_Kind = N_Object_Declaration
6561 and then Needs_Finalization (Typ))
6562 or else (Parent_Kind = N_Assignment_Statement
6563 and then Inside_Init_Proc)
6564 then
6565 Set_Expansion_Delayed (N, not Static_Array_Aggregate (N));
6566 return;
6567 end if;
6568
6569 -- STEP 4
6570
6571 -- Check whether in-place aggregate expansion is possible
6572
6573 -- For object declarations we build the aggregate in place, unless
6574 -- the array is bit-packed.
6575
6576 -- For assignments we do the assignment in place if all the component
6577 -- associations have compile-time known values, or are default-
6578 -- initialized limited components, e.g. tasks. For other cases we
6579 -- create a temporary. The analysis for safety of on-line assignment
6580 -- is delicate, i.e. we don't know how to do it fully yet ???
6581
6582 -- For allocators we assign to the designated object in place if the
6583 -- aggregate meets the same conditions as other in-place assignments.
6584 -- In this case the aggregate may not come from source but was created
6585 -- for default initialization, e.g. with Initialize_Scalars.
6586
6587 if Requires_Transient_Scope (Typ) then
6588 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6589 end if;
6590
6591 -- An array of limited components is built in place
6592
6593 if Is_Limited_Type (Typ) then
6594 Maybe_In_Place_OK := True;
6595
6596 elsif Has_Default_Init_Comps (N) then
6597 Maybe_In_Place_OK := False;
6598
6599 elsif Is_Bit_Packed_Array (Typ)
6600 or else Has_Controlled_Component (Typ)
6601 then
6602 Maybe_In_Place_OK := False;
6603
6604 elsif Parent_Kind = N_Assignment_Statement then
6605 Maybe_In_Place_OK :=
6606 In_Place_Assign_OK (N, Get_Base_Object (Name (Parent_Node)));
6607
6608 elsif Parent_Kind = N_Allocator then
6609 Maybe_In_Place_OK := In_Place_Assign_OK (N);
6610
6611 else
6612 Maybe_In_Place_OK := False;
6613 end if;
6614
6615 -- If this is an array of tasks, it will be expanded into build-in-place
6616 -- assignments. Build an activation chain for the tasks now.
6617
6618 if Has_Task (Etype (N)) then
6619 Build_Activation_Chain_Entity (N);
6620 end if;
6621
6622 -- Perform in-place expansion of aggregate in an object declaration.
6623 -- Note: actions generated for the aggregate will be captured in an
6624 -- expression-with-actions statement so that they can be transferred
6625 -- to freeze actions later if there is an address clause for the
6626 -- object. (Note: we don't use a block statement because this would
6627 -- cause generated freeze nodes to be elaborated in the wrong scope).
6628
6629 -- Arrays of limited components must be built in place. The code
6630 -- previously excluded controlled components but this is an old
6631 -- oversight: the rules in 7.6 (17) are clear.
6632
6633 if Comes_From_Source (Parent_Node)
6634 and then Parent_Kind = N_Object_Declaration
6635 and then Present (Expression (Parent_Node))
6636 and then not
6637 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
6638 and then not Is_Bit_Packed_Array (Typ)
6639 then
6640 In_Place_Assign_OK_For_Declaration := True;
6641 Tmp := Defining_Identifier (Parent_Node);
6642 Set_No_Initialization (Parent_Node);
6643 Set_Expression (Parent_Node, Empty);
6644
6645 -- Set kind and type of the entity, for use in the analysis
6646 -- of the subsequent assignments. If the nominal type is not
6647 -- constrained, build a subtype from the known bounds of the
6648 -- aggregate. If the declaration has a subtype mark, use it,
6649 -- otherwise use the itype of the aggregate.
6650
6651 Set_Ekind (Tmp, E_Variable);
6652
6653 if not Is_Constrained (Typ) then
6654 Build_Constrained_Type (Positional => False);
6655
6656 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6657 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6658 then
6659 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6660
6661 else
6662 Set_Size_Known_At_Compile_Time (Typ, False);
6663 Set_Etype (Tmp, Typ);
6664 end if;
6665
6666 elsif Maybe_In_Place_OK and then Parent_Kind = N_Allocator then
6667 Set_Expansion_Delayed (N);
6668 return;
6669
6670 -- Limited arrays in return statements are expanded when
6671 -- enclosing construct is expanded.
6672
6673 elsif Maybe_In_Place_OK
6674 and then Parent_Kind = N_Simple_Return_Statement
6675 then
6676 Set_Expansion_Delayed (N);
6677 return;
6678
6679 -- In the remaining cases the aggregate appears in the RHS of an
6680 -- assignment, which may be part of the expansion of an object
6681 -- delaration. If the aggregate is an actual in a call, itself
6682 -- possibly in a RHS, building it in the target is not possible.
6683
6684 elsif Maybe_In_Place_OK
6685 and then Nkind (Parent_Node) not in N_Subprogram_Call
6686 and then Safe_Left_Hand_Side (Name (Parent_Node))
6687 then
6688 Tmp := Name (Parent_Node);
6689
6690 if Etype (Tmp) /= Etype (N) then
6691 Apply_Length_Check (N, Etype (Tmp));
6692
6693 if Nkind (N) = N_Raise_Constraint_Error then
6694
6695 -- Static error, nothing further to expand
6696
6697 return;
6698 end if;
6699 end if;
6700
6701 -- If a slice assignment has an aggregate with a single others_choice,
6702 -- the assignment can be done in place even if bounds are not static,
6703 -- by converting it into a loop over the discrete range of the slice.
6704
6705 elsif Maybe_In_Place_OK
6706 and then Nkind (Name (Parent_Node)) = N_Slice
6707 and then Is_Others_Aggregate (N)
6708 then
6709 Tmp := Name (Parent_Node);
6710
6711 -- Set type of aggregate to be type of lhs in assignment, in order
6712 -- to suppress redundant length checks.
6713
6714 Set_Etype (N, Etype (Tmp));
6715
6716 -- Step 5
6717
6718 -- In-place aggregate expansion is not possible
6719
6720 else
6721 Maybe_In_Place_OK := False;
6722 Tmp := Make_Temporary (Loc, 'A', N);
6723 Tmp_Decl :=
6724 Make_Object_Declaration (Loc,
6725 Defining_Identifier => Tmp,
6726 Object_Definition => New_Occurrence_Of (Typ, Loc));
6727 Set_No_Initialization (Tmp_Decl, True);
6728 Set_Warnings_Off (Tmp);
6729
6730 -- If we are within a loop, the temporary will be pushed on the
6731 -- stack at each iteration. If the aggregate is the expression
6732 -- for an allocator, it will be immediately copied to the heap
6733 -- and can be reclaimed at once. We create a transient scope
6734 -- around the aggregate for this purpose.
6735
6736 if Ekind (Current_Scope) = E_Loop
6737 and then Parent_Kind = N_Allocator
6738 then
6739 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6740 end if;
6741
6742 Insert_Action (N, Tmp_Decl);
6743 end if;
6744
6745 -- Construct and insert the aggregate code. We can safely suppress index
6746 -- checks because this code is guaranteed not to raise CE on index
6747 -- checks. However we should *not* suppress all checks.
6748
6749 declare
6750 Target : Node_Id;
6751
6752 begin
6753 if Nkind (Tmp) = N_Defining_Identifier then
6754 Target := New_Occurrence_Of (Tmp, Loc);
6755
6756 else
6757 if Has_Default_Init_Comps (N)
6758 and then not Maybe_In_Place_OK
6759 then
6760 -- Ada 2005 (AI-287): This case has not been analyzed???
6761
6762 raise Program_Error;
6763 end if;
6764
6765 -- Name in assignment is explicit dereference
6766
6767 Target := New_Copy (Tmp);
6768 end if;
6769
6770 -- If we are to generate an in-place assignment for a declaration or
6771 -- an assignment statement, and the assignment can be done directly
6772 -- by the back end, then do not expand further.
6773
6774 -- ??? We can also do that if in-place expansion is not possible but
6775 -- then we could go into an infinite recursion.
6776
6777 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6778 and then not CodePeer_Mode
6779 and then not Modify_Tree_For_C
6780 and then not Possible_Bit_Aligned_Component (Target)
6781 and then not Is_Possibly_Unaligned_Slice (Target)
6782 and then Aggr_Assignment_OK_For_Backend (N)
6783 then
6784 if Maybe_In_Place_OK then
6785 return;
6786 end if;
6787
6788 Aggr_Code :=
6789 New_List (
6790 Make_Assignment_Statement (Loc,
6791 Name => Target,
6792 Expression => New_Copy_Tree (N)));
6793
6794 else
6795 Aggr_Code :=
6796 Build_Array_Aggr_Code (N,
6797 Ctype => Ctyp,
6798 Index => First_Index (Typ),
6799 Into => Target,
6800 Scalar_Comp => Is_Scalar_Type (Ctyp));
6801 end if;
6802
6803 -- Save the last assignment statement associated with the aggregate
6804 -- when building a controlled object. This reference is utilized by
6805 -- the finalization machinery when marking an object as successfully
6806 -- initialized.
6807
6808 if Needs_Finalization (Typ)
6809 and then Is_Entity_Name (Target)
6810 and then Present (Entity (Target))
6811 and then Ekind (Entity (Target)) in E_Constant | E_Variable
6812 then
6813 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6814 end if;
6815 end;
6816
6817 -- If the aggregate is the expression in a declaration, the expanded
6818 -- code must be inserted after it. The defining entity might not come
6819 -- from source if this is part of an inlined body, but the declaration
6820 -- itself will.
6821 -- The test below looks very specialized and kludgy???
6822
6823 if Comes_From_Source (Tmp)
6824 or else
6825 (Nkind (Parent (N)) = N_Object_Declaration
6826 and then Comes_From_Source (Parent (N))
6827 and then Tmp = Defining_Entity (Parent (N)))
6828 then
6829 if Parent_Kind /= N_Object_Declaration or else Is_Frozen (Tmp) then
6830 Insert_Actions_After (Parent_Node, Aggr_Code);
6831 else
6832 declare
6833 Comp_Stmt : constant Node_Id :=
6834 Make_Compound_Statement
6835 (Sloc (Parent_Node), Actions => Aggr_Code);
6836 begin
6837 Insert_Action_After (Parent_Node, Comp_Stmt);
6838 Set_Initialization_Statements (Tmp, Comp_Stmt);
6839 end;
6840 end if;
6841 else
6842 Insert_Actions (N, Aggr_Code);
6843 end if;
6844
6845 -- If the aggregate has been assigned in place, remove the original
6846 -- assignment.
6847
6848 if Parent_Kind = N_Assignment_Statement and then Maybe_In_Place_OK then
6849 Rewrite (Parent_Node, Make_Null_Statement (Loc));
6850
6851 -- Or else, if a temporary was created, replace the aggregate with it
6852
6853 elsif Parent_Kind /= N_Object_Declaration
6854 or else Tmp /= Defining_Identifier (Parent_Node)
6855 then
6856 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6857 Analyze_And_Resolve (N, Typ);
6858 end if;
6859 end Expand_Array_Aggregate;
6860
6861 ------------------------
6862 -- Expand_N_Aggregate --
6863 ------------------------
6864
6865 procedure Expand_N_Aggregate (N : Node_Id) is
6866 T : constant Entity_Id := Etype (N);
6867 begin
6868 -- Record aggregate case
6869
6870 if Is_Record_Type (T)
6871 and then not Is_Private_Type (T)
6872 then
6873 Expand_Record_Aggregate (N);
6874
6875 elsif Has_Aspect (T, Aspect_Aggregate) then
6876 Expand_Container_Aggregate (N);
6877
6878 -- Array aggregate case
6879
6880 else
6881 -- A special case, if we have a string subtype with bounds 1 .. N,
6882 -- where N is known at compile time, and the aggregate is of the
6883 -- form (others => 'x'), with a single choice and no expressions,
6884 -- and N is less than 80 (an arbitrary limit for now), then replace
6885 -- the aggregate by the equivalent string literal (but do not mark
6886 -- it as static since it is not).
6887
6888 -- Note: this entire circuit is redundant with respect to code in
6889 -- Expand_Array_Aggregate that collapses others choices to positional
6890 -- form, but there are two problems with that circuit:
6891
6892 -- a) It is limited to very small cases due to ill-understood
6893 -- interactions with bootstrapping. That limit is removed by
6894 -- use of the No_Implicit_Loops restriction.
6895
6896 -- b) It incorrectly ends up with the resulting expressions being
6897 -- considered static when they are not. For example, the
6898 -- following test should fail:
6899
6900 -- pragma Restrictions (No_Implicit_Loops);
6901 -- package NonSOthers4 is
6902 -- B : constant String (1 .. 6) := (others => 'A');
6903 -- DH : constant String (1 .. 8) := B & "BB";
6904 -- X : Integer;
6905 -- pragma Export (C, X, Link_Name => DH);
6906 -- end;
6907
6908 -- But it succeeds (DH looks static to pragma Export)
6909
6910 -- To be sorted out ???
6911
6912 if Present (Component_Associations (N)) then
6913 declare
6914 CA : constant Node_Id := First (Component_Associations (N));
6915 MX : constant := 80;
6916
6917 begin
6918 if Nkind (First (Choice_List (CA))) = N_Others_Choice
6919 and then Nkind (Expression (CA)) = N_Character_Literal
6920 and then No (Expressions (N))
6921 then
6922 declare
6923 X : constant Node_Id := First_Index (T);
6924 EC : constant Node_Id := Expression (CA);
6925 CV : constant Uint := Char_Literal_Value (EC);
6926 CC : constant Int := UI_To_Int (CV);
6927
6928 begin
6929 if Nkind (X) = N_Range
6930 and then Compile_Time_Known_Value (Low_Bound (X))
6931 and then Expr_Value (Low_Bound (X)) = 1
6932 and then Compile_Time_Known_Value (High_Bound (X))
6933 then
6934 declare
6935 Hi : constant Uint := Expr_Value (High_Bound (X));
6936
6937 begin
6938 if Hi <= MX then
6939 Start_String;
6940
6941 for J in 1 .. UI_To_Int (Hi) loop
6942 Store_String_Char (Char_Code (CC));
6943 end loop;
6944
6945 Rewrite (N,
6946 Make_String_Literal (Sloc (N),
6947 Strval => End_String));
6948
6949 if CC >= Int (2 ** 16) then
6950 Set_Has_Wide_Wide_Character (N);
6951 elsif CC >= Int (2 ** 8) then
6952 Set_Has_Wide_Character (N);
6953 end if;
6954
6955 Analyze_And_Resolve (N, T);
6956 Set_Is_Static_Expression (N, False);
6957 return;
6958 end if;
6959 end;
6960 end if;
6961 end;
6962 end if;
6963 end;
6964 end if;
6965
6966 -- Not that special case, so normal expansion of array aggregate
6967
6968 Expand_Array_Aggregate (N);
6969 end if;
6970
6971 exception
6972 when RE_Not_Available =>
6973 return;
6974 end Expand_N_Aggregate;
6975
6976 --------------------------------
6977 -- Expand_Container_Aggregate --
6978 --------------------------------
6979
6980 procedure Expand_Container_Aggregate (N : Node_Id) is
6981 Loc : constant Source_Ptr := Sloc (N);
6982 Typ : constant Entity_Id := Etype (N);
6983 Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate);
6984
6985 Empty_Subp : Node_Id := Empty;
6986 Add_Named_Subp : Node_Id := Empty;
6987 Add_Unnamed_Subp : Node_Id := Empty;
6988 New_Indexed_Subp : Node_Id := Empty;
6989 Assign_Indexed_Subp : Node_Id := Empty;
6990
6991 Aggr_Code : constant List_Id := New_List;
6992 Temp : constant Entity_Id := Make_Temporary (Loc, 'C', N);
6993
6994 Comp : Node_Id;
6995 Decl : Node_Id;
6996 Default : Node_Id;
6997 Init_Stat : Node_Id;
6998 Siz : Int;
6999
7000 -- The following are used when the size of the aggregate is not
7001 -- static and requires a dynamic evaluation.
7002 Siz_Decl : Node_Id;
7003 Siz_Exp : Node_Id := Empty;
7004 Count_Type : Entity_Id;
7005
7006 function Aggregate_Size return Int;
7007 -- Compute number of entries in aggregate, including choices
7008 -- that cover a range or subtype, as well as iterated constructs.
7009 -- Return -1 if the size is not known statically, in which case
7010 -- allocate a default size for the aggregate, or build an expression
7011 -- to estimate the size dynamically.
7012
7013 function Build_Siz_Exp (Comp : Node_Id) return Int;
7014 -- When the aggregate contains a single Iterated_Component_Association
7015 -- or Element_Association with non-static bounds, build an expression
7016 -- to be used as the allocated size of the container. This may be an
7017 -- overestimate if a filter is present, but is a safe approximation.
7018
7019 procedure Expand_Iterated_Component (Comp : Node_Id);
7020 -- Handle iterated_component_association and iterated_Element
7021 -- association by generating a loop over the specified range,
7022 -- given either by a loop parameter specification or an iterator
7023 -- specification.
7024
7025 --------------------
7026 -- Aggregate_Size --
7027 --------------------
7028
7029 function Aggregate_Size return Int is
7030 Comp : Node_Id;
7031 Choice : Node_Id;
7032 Lo, Hi : Node_Id;
7033 Siz : Int := 0;
7034
7035 procedure Add_Range_Size;
7036 -- Compute number of components specified by a component association
7037 -- given by a range or subtype name.
7038
7039 --------------------
7040 -- Add_Range_Size --
7041 --------------------
7042
7043 procedure Add_Range_Size is
7044 begin
7045 -- The bounds of the discrete range are integers or enumeration
7046 -- literals
7047
7048 if Nkind (Lo) = N_Integer_Literal then
7049 Siz := Siz + UI_To_Int (Intval (Hi))
7050 - UI_To_Int (Intval (Lo)) + 1;
7051 else
7052 Siz := Siz + UI_To_Int (Enumeration_Pos (Hi))
7053 - UI_To_Int (Enumeration_Pos (Lo)) + 1;
7054 end if;
7055 end Add_Range_Size;
7056
7057 begin
7058 -- Aggregate is either all positional or all named.
7059
7060 if Present (Expressions (N)) then
7061 Siz := List_Length (Expressions (N));
7062 end if;
7063
7064 if Present (Component_Associations (N)) then
7065 Comp := First (Component_Associations (N));
7066 -- If there is a single component association it can be
7067 -- an iterated component with dynamic bounds or an element
7068 -- iterator over an iterable object. If it is an array
7069 -- we can use the attribute Length to get its size;
7070 -- for a predefined container the function Length plays
7071 -- the same role. There is no available mechanism for
7072 -- user-defined containers. For now we treat all of these
7073 -- as dynamic.
7074
7075 if List_Length (Component_Associations (N)) = 1
7076 and then Nkind (Comp) in N_Iterated_Component_Association |
7077 N_Iterated_Element_Association
7078 then
7079 return Build_Siz_Exp (Comp);
7080 end if;
7081
7082 -- Otherwise all associations must specify static sizes.
7083
7084 while Present (Comp) loop
7085 Choice := First (Choice_List (Comp));
7086
7087 while Present (Choice) loop
7088 Analyze (Choice);
7089
7090 if Nkind (Choice) = N_Range then
7091 Lo := Low_Bound (Choice);
7092 Hi := High_Bound (Choice);
7093 Add_Range_Size;
7094
7095 elsif Is_Entity_Name (Choice)
7096 and then Is_Type (Entity (Choice))
7097 then
7098 Lo := Type_Low_Bound (Entity (Choice));
7099 Hi := Type_High_Bound (Entity (Choice));
7100 Add_Range_Size;
7101
7102 Rewrite (Choice,
7103 Make_Range (Loc,
7104 New_Copy_Tree (Lo),
7105 New_Copy_Tree (Hi)));
7106
7107 else
7108 -- Single choice (syntax excludes a subtype
7109 -- indication).
7110
7111 Siz := Siz + 1;
7112 end if;
7113
7114 Next (Choice);
7115 end loop;
7116 Next (Comp);
7117 end loop;
7118 end if;
7119
7120 return Siz;
7121 end Aggregate_Size;
7122
7123 -------------------
7124 -- Build_Siz_Exp --
7125 -------------------
7126
7127 function Build_Siz_Exp (Comp : Node_Id) return Int is
7128 Lo, Hi : Node_Id;
7129 begin
7130 if Nkind (Comp) = N_Range then
7131 Lo := Low_Bound (Comp);
7132 Hi := High_Bound (Comp);
7133 Analyze (Lo);
7134 Analyze (Hi);
7135
7136 -- Compute static size when possible.
7137
7138 if Is_Static_Expression (Lo)
7139 and then Is_Static_Expression (Hi)
7140 then
7141 if Nkind (Lo) = N_Integer_Literal then
7142 Siz := UI_To_Int (Intval (Hi)) - UI_To_Int (Intval (Lo)) + 1;
7143 else
7144 Siz := UI_To_Int (Enumeration_Pos (Hi))
7145 - UI_To_Int (Enumeration_Pos (Lo)) + 1;
7146 end if;
7147 return Siz;
7148
7149 else
7150 Siz_Exp :=
7151 Make_Op_Add (Sloc (Comp),
7152 Left_Opnd =>
7153 Make_Op_Subtract (Sloc (Comp),
7154 Left_Opnd => New_Copy_Tree (Hi),
7155 Right_Opnd => New_Copy_Tree (Lo)),
7156 Right_Opnd =>
7157 Make_Integer_Literal (Loc, 1));
7158 return -1;
7159 end if;
7160
7161 elsif Nkind (Comp) = N_Iterated_Component_Association then
7162 return Build_Siz_Exp (First (Discrete_Choices (Comp)));
7163
7164 elsif Nkind (Comp) = N_Iterated_Element_Association then
7165 return -1; -- TBD, build expression for size of the domain
7166
7167 else
7168 return -1;
7169 end if;
7170 end Build_Siz_Exp;
7171
7172 -------------------------------
7173 -- Expand_Iterated_Component --
7174 -------------------------------
7175
7176 procedure Expand_Iterated_Component (Comp : Node_Id) is
7177 Expr : constant Node_Id := Expression (Comp);
7178
7179 Key_Expr : Node_Id := Empty;
7180 Loop_Id : Entity_Id;
7181 L_Range : Node_Id;
7182 L_Iteration_Scheme : Node_Id;
7183 Loop_Stat : Node_Id;
7184 Params : List_Id;
7185 Stats : List_Id;
7186
7187 begin
7188 if Nkind (Comp) = N_Iterated_Element_Association then
7189 Key_Expr := Key_Expression (Comp);
7190
7191 -- We create a new entity as loop identifier in all cases,
7192 -- as is done for generated loops elsewhere, as the loop
7193 -- structure has been previously analyzed.
7194
7195 if Present (Iterator_Specification (Comp)) then
7196
7197 -- Either an Iterator_Specification of a Loop_Parameter_
7198 -- Specification is present.
7199
7200 L_Iteration_Scheme :=
7201 Make_Iteration_Scheme (Loc,
7202 Iterator_Specification => Iterator_Specification (Comp));
7203 Loop_Id :=
7204 Make_Defining_Identifier (Loc,
7205 Chars => Chars (Defining_Identifier
7206 (Iterator_Specification (Comp))));
7207 Set_Defining_Identifier
7208 (Iterator_Specification (L_Iteration_Scheme), Loop_Id);
7209
7210 else
7211 L_Iteration_Scheme :=
7212 Make_Iteration_Scheme (Loc,
7213 Loop_Parameter_Specification =>
7214 Loop_Parameter_Specification (Comp));
7215 Loop_Id :=
7216 Make_Defining_Identifier (Loc,
7217 Chars => Chars (Defining_Identifier
7218 (Loop_Parameter_Specification (Comp))));
7219 Set_Defining_Identifier
7220 (Loop_Parameter_Specification
7221 (L_Iteration_Scheme), Loop_Id);
7222 end if;
7223 else
7224
7225 -- Iterated_Component_Association.
7226
7227 Loop_Id :=
7228 Make_Defining_Identifier (Loc,
7229 Chars => Chars (Defining_Identifier (Comp)));
7230
7231 if Present (Iterator_Specification (Comp)) then
7232 L_Iteration_Scheme :=
7233 Make_Iteration_Scheme (Loc,
7234 Iterator_Specification => Iterator_Specification (Comp));
7235
7236 else
7237 -- Loop_Parameter_Specifcation is parsed with a choice list.
7238 -- where the range is the first (and only) choice.
7239
7240 L_Range := Relocate_Node (First (Discrete_Choices (Comp)));
7241
7242 L_Iteration_Scheme :=
7243 Make_Iteration_Scheme (Loc,
7244 Loop_Parameter_Specification =>
7245 Make_Loop_Parameter_Specification (Loc,
7246 Defining_Identifier => Loop_Id,
7247 Discrete_Subtype_Definition => L_Range));
7248 end if;
7249 end if;
7250
7251 -- Build insertion statement. For a positional aggregate, only the
7252 -- expression is needed. For a named aggregate, the loop variable,
7253 -- whose type is that of the key, is an additional parameter for
7254 -- the insertion operation.
7255 -- If a Key_Expression is present, it serves as the additional
7256 -- parameter. Otherwise the key is given by the loop parameter
7257 -- itself.
7258
7259 if Present (Add_Unnamed_Subp)
7260 and then No (Add_Named_Subp)
7261 then
7262 Stats := New_List
7263 (Make_Procedure_Call_Statement (Loc,
7264 Name => New_Occurrence_Of (Entity (Add_Unnamed_Subp), Loc),
7265 Parameter_Associations =>
7266 New_List (New_Occurrence_Of (Temp, Loc),
7267 New_Copy_Tree (Expr))));
7268 else
7269 -- Named or indexed aggregate, for which a key is present,
7270 -- possibly with a specified key_expression.
7271
7272 if Present (Key_Expr) then
7273 Params := New_List (New_Occurrence_Of (Temp, Loc),
7274 New_Copy_Tree (Key_Expr),
7275 New_Copy_Tree (Expr));
7276 else
7277 Params := New_List (New_Occurrence_Of (Temp, Loc),
7278 New_Occurrence_Of (Loop_Id, Loc),
7279 New_Copy_Tree (Expr));
7280 end if;
7281
7282 Stats := New_List
7283 (Make_Procedure_Call_Statement (Loc,
7284 Name => New_Occurrence_Of (Entity (Add_Named_Subp), Loc),
7285 Parameter_Associations => Params));
7286 end if;
7287
7288 Loop_Stat := Make_Implicit_Loop_Statement
7289 (Node => N,
7290 Identifier => Empty,
7291 Iteration_Scheme => L_Iteration_Scheme,
7292 Statements => Stats);
7293 Append (Loop_Stat, Aggr_Code);
7294
7295 end Expand_Iterated_Component;
7296
7297 -- Start of processing for Expand_Container_Aggregate
7298
7299 begin
7300 Parse_Aspect_Aggregate (Asp,
7301 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
7302 New_Indexed_Subp, Assign_Indexed_Subp);
7303
7304 -- The constructor for bounded containers is a function with
7305 -- a parameter that sets the size of the container. If the
7306 -- size cannot be determined statically we use a default value
7307 -- or a dynamic expression.
7308
7309 Siz := Aggregate_Size;
7310
7311 if Ekind (Entity (Empty_Subp)) = E_Function
7312 and then Present (First_Formal (Entity (Empty_Subp)))
7313 then
7314 Default := Default_Value (First_Formal (Entity (Empty_Subp)));
7315
7316 -- If aggregate size is not static, we can use default value
7317 -- of formal parameter for allocation. We assume that this
7318 -- (implementation-dependent) value is static, even though
7319 -- the AI does not require it.
7320
7321 -- Create declaration for size: a constant literal in the simple
7322 -- case, an expression if iterated component associations may be
7323 -- involved, the default otherwise.
7324
7325 Count_Type := Etype (First_Formal (Entity (Empty_Subp)));
7326 if Siz = -1 then
7327 if No (Siz_Exp) then
7328 Siz := UI_To_Int (Intval (Default));
7329 Siz_Exp := Make_Integer_Literal (Loc, Siz);
7330
7331 else
7332 Siz_Exp := Make_Type_Conversion (Loc,
7333 Subtype_Mark =>
7334 New_Occurrence_Of (Count_Type, Loc),
7335 Expression => Siz_Exp);
7336 end if;
7337
7338 else
7339 Siz_Exp := Make_Integer_Literal (Loc, Siz);
7340 end if;
7341
7342 Siz_Decl := Make_Object_Declaration (Loc,
7343 Defining_Identifier => Make_Temporary (Loc, 'S', N),
7344 Object_Definition =>
7345 New_Occurrence_Of (Count_Type, Loc),
7346 Expression => Siz_Exp);
7347 Append (Siz_Decl, Aggr_Code);
7348
7349 if Nkind (Siz_Exp) = N_Integer_Literal then
7350 Init_Stat :=
7351 Make_Object_Declaration (Loc,
7352 Defining_Identifier => Temp,
7353 Object_Definition => New_Occurrence_Of (Typ, Loc),
7354 Expression => Make_Function_Call (Loc,
7355 Name => New_Occurrence_Of (Entity (Empty_Subp), Loc),
7356 Parameter_Associations =>
7357 New_List
7358 (New_Occurrence_Of
7359 (Defining_Identifier (Siz_Decl), Loc))));
7360
7361 else
7362 Init_Stat :=
7363 Make_Object_Declaration (Loc,
7364 Defining_Identifier => Temp,
7365 Object_Definition => New_Occurrence_Of (Typ, Loc),
7366 Expression => Make_Function_Call (Loc,
7367 Name =>
7368 New_Occurrence_Of (Entity (New_Indexed_Subp), Loc),
7369 Parameter_Associations =>
7370 New_List (
7371 Make_Integer_Literal (Loc, 1),
7372 New_Occurrence_Of
7373 (Defining_Identifier (Siz_Decl), Loc))));
7374 end if;
7375
7376 Append (Init_Stat, Aggr_Code);
7377
7378 -- Size is dynamic: Create declaration for object, and intitialize
7379 -- with a call to the null container, or an assignment to it.
7380
7381 else
7382 Decl :=
7383 Make_Object_Declaration (Loc,
7384 Defining_Identifier => Temp,
7385 Object_Definition => New_Occurrence_Of (Typ, Loc));
7386
7387 Insert_Action (N, Decl);
7388
7389 -- The Empty entity is either a parameterless function, or
7390 -- a constant.
7391
7392 if Ekind (Entity (Empty_Subp)) = E_Function then
7393 Init_Stat := Make_Assignment_Statement (Loc,
7394 Name => New_Occurrence_Of (Temp, Loc),
7395 Expression => Make_Function_Call (Loc,
7396 Name => New_Occurrence_Of (Entity (Empty_Subp), Loc)));
7397
7398 else
7399 Init_Stat := Make_Assignment_Statement (Loc,
7400 Name => New_Occurrence_Of (Temp, Loc),
7401 Expression => New_Occurrence_Of (Entity (Empty_Subp), Loc));
7402 end if;
7403
7404 Append (Init_Stat, Aggr_Code);
7405 end if;
7406
7407 ---------------------------
7408 -- Positional aggregate --
7409 ---------------------------
7410
7411 -- If the aggregate is positional the aspect must include
7412 -- an Add_Unnamed subprogram.
7413
7414 if Present (Add_Unnamed_Subp) then
7415 if Present (Expressions (N)) then
7416 declare
7417 Insert : constant Entity_Id := Entity (Add_Unnamed_Subp);
7418 Comp : Node_Id;
7419 Stat : Node_Id;
7420
7421 begin
7422 Comp := First (Expressions (N));
7423 while Present (Comp) loop
7424 Stat := Make_Procedure_Call_Statement (Loc,
7425 Name => New_Occurrence_Of (Insert, Loc),
7426 Parameter_Associations =>
7427 New_List (New_Occurrence_Of (Temp, Loc),
7428 New_Copy_Tree (Comp)));
7429 Append (Stat, Aggr_Code);
7430 Next (Comp);
7431 end loop;
7432 end;
7433 end if;
7434
7435 -- Indexed aggregates are handled below. Unnamed aggregates
7436 -- such as sets may include iterated component associations.
7437
7438 if No (New_Indexed_Subp) then
7439 Comp := First (Component_Associations (N));
7440 while Present (Comp) loop
7441 if Nkind (Comp) = N_Iterated_Component_Association then
7442 Expand_Iterated_Component (Comp);
7443 end if;
7444 Next (Comp);
7445 end loop;
7446 end if;
7447
7448 ---------------------
7449 -- Named_Aggregate --
7450 ---------------------
7451
7452 elsif Present (Add_Named_Subp) then
7453 declare
7454 Insert : constant Entity_Id := Entity (Add_Named_Subp);
7455 Stat : Node_Id;
7456 Key : Node_Id;
7457 begin
7458 Comp := First (Component_Associations (N));
7459
7460 -- Each component association may contain several choices;
7461 -- generate an insertion statement for each.
7462
7463 while Present (Comp) loop
7464 if Nkind (Comp) in N_Iterated_Component_Association
7465 | N_Iterated_Element_Association
7466 then
7467 Expand_Iterated_Component (Comp);
7468 else
7469 Key := First (Choices (Comp));
7470
7471 while Present (Key) loop
7472 Stat := Make_Procedure_Call_Statement (Loc,
7473 Name => New_Occurrence_Of (Insert, Loc),
7474 Parameter_Associations =>
7475 New_List (New_Occurrence_Of (Temp, Loc),
7476 New_Copy_Tree (Key),
7477 New_Copy_Tree (Expression (Comp))));
7478 Append (Stat, Aggr_Code);
7479
7480 Next (Key);
7481 end loop;
7482 end if;
7483
7484 Next (Comp);
7485 end loop;
7486 end;
7487 end if;
7488
7489 -----------------------
7490 -- Indexed_Aggregate --
7491 -----------------------
7492
7493 -- For an indexed aggregate there must be an Assigned_Indexeed
7494 -- subprogram. Note that unlike array aggregates, a container
7495 -- aggregate must be fully positional or fully indexed. In the
7496 -- first case the expansion has already taken place.
7497 -- TBA: the keys for an indexed aggregate must provide a dense
7498 -- range with no repetitions.
7499
7500 if Present (Assign_Indexed_Subp)
7501 and then Present (Component_Associations (N))
7502 then
7503 declare
7504 Insert : constant Entity_Id := Entity (Assign_Indexed_Subp);
7505 Index_Type : constant Entity_Id :=
7506 Etype (Next_Formal (First_Formal (Insert)));
7507
7508 function Expand_Range_Component
7509 (Rng : Node_Id;
7510 Expr : Node_Id) return Node_Id;
7511 -- Transform a component assoication with a range into an
7512 -- explicit loop. If the choice is a subtype name, it is
7513 -- rewritten as a range with the corresponding bounds, which
7514 -- are known to be static.
7515
7516 Comp : Node_Id;
7517 Index : Node_Id;
7518 Pos : Int := 0;
7519 Stat : Node_Id;
7520 Key : Node_Id;
7521
7522 -----------------------------
7523 -- Expand_Raange_Component --
7524 -----------------------------
7525
7526 function Expand_Range_Component
7527 (Rng : Node_Id;
7528 Expr : Node_Id) return Node_Id
7529 is
7530 Loop_Id : constant Entity_Id :=
7531 Make_Temporary (Loc, 'T');
7532
7533 L_Iteration_Scheme : Node_Id;
7534 Stats : List_Id;
7535
7536 begin
7537 L_Iteration_Scheme :=
7538 Make_Iteration_Scheme (Loc,
7539 Loop_Parameter_Specification =>
7540 Make_Loop_Parameter_Specification (Loc,
7541 Defining_Identifier => Loop_Id,
7542 Discrete_Subtype_Definition => New_Copy_Tree (Rng)));
7543
7544 Stats := New_List
7545 (Make_Procedure_Call_Statement (Loc,
7546 Name =>
7547 New_Occurrence_Of (Entity (Assign_Indexed_Subp), Loc),
7548 Parameter_Associations =>
7549 New_List (New_Occurrence_Of (Temp, Loc),
7550 New_Occurrence_Of (Loop_Id, Loc),
7551 New_Copy_Tree (Expr))));
7552
7553 return Make_Implicit_Loop_Statement
7554 (Node => N,
7555 Identifier => Empty,
7556 Iteration_Scheme => L_Iteration_Scheme,
7557 Statements => Stats);
7558 end Expand_Range_Component;
7559
7560 begin
7561 if Siz > 0 then
7562
7563 -- Modify the call to the constructor to allocate the
7564 -- required size for the aggregwte : call the provided
7565 -- constructor rather than the Empty aggregate.
7566
7567 Index := Make_Op_Add (Loc,
7568 Left_Opnd => New_Copy_Tree (Type_Low_Bound (Index_Type)),
7569 Right_Opnd => Make_Integer_Literal (Loc, Siz - 1));
7570
7571 Set_Expression (Init_Stat,
7572 Make_Function_Call (Loc,
7573 Name =>
7574 New_Occurrence_Of (Entity (New_Indexed_Subp), Loc),
7575 Parameter_Associations =>
7576 New_List (
7577 New_Copy_Tree (Type_Low_Bound (Index_Type)),
7578 Index)));
7579 end if;
7580
7581 if Present (Expressions (N)) then
7582 Comp := First (Expressions (N));
7583
7584 while Present (Comp) loop
7585
7586 -- Compute index position for successive components
7587 -- in the list of expressions, and use the indexed
7588 -- assignment procedure for each.
7589
7590 Index := Make_Op_Add (Loc,
7591 Left_Opnd => Type_Low_Bound (Index_Type),
7592 Right_Opnd => Make_Integer_Literal (Loc, Pos));
7593
7594 Stat := Make_Procedure_Call_Statement (Loc,
7595 Name => New_Occurrence_Of (Insert, Loc),
7596 Parameter_Associations =>
7597 New_List (New_Occurrence_Of (Temp, Loc),
7598 Index,
7599 New_Copy_Tree (Comp)));
7600
7601 Pos := Pos + 1;
7602
7603 Append (Stat, Aggr_Code);
7604 Next (Comp);
7605 end loop;
7606 end if;
7607
7608 if Present (Component_Associations (N)) then
7609 Comp := First (Component_Associations (N));
7610
7611 -- The choice may be a static value, or a range with
7612 -- static bounds.
7613
7614 while Present (Comp) loop
7615 if Nkind (Comp) = N_Component_Association then
7616 Key := First (Choices (Comp));
7617 while Present (Key) loop
7618
7619 -- If the expression is a box, the corresponding
7620 -- component (s) is left uninitialized.
7621
7622 if Box_Present (Comp) then
7623 goto Next_Key;
7624
7625 elsif Nkind (Key) = N_Range then
7626
7627 -- Create loop for tne specified range,
7628 -- with copies of the expression.
7629
7630 Stat :=
7631 Expand_Range_Component (Key, Expression (Comp));
7632
7633 else
7634 Stat := Make_Procedure_Call_Statement (Loc,
7635 Name => New_Occurrence_Of
7636 (Entity (Assign_Indexed_Subp), Loc),
7637 Parameter_Associations =>
7638 New_List (New_Occurrence_Of (Temp, Loc),
7639 New_Copy_Tree (Key),
7640 New_Copy_Tree (Expression (Comp))));
7641 end if;
7642
7643 Append (Stat, Aggr_Code);
7644
7645 <<Next_Key>>
7646 Next (Key);
7647 end loop;
7648
7649 else
7650 -- Iterated component association. Discard
7651 -- positional insertion procedure.
7652
7653 Add_Named_Subp := Assign_Indexed_Subp;
7654 Add_Unnamed_Subp := Empty;
7655 Expand_Iterated_Component (Comp);
7656 end if;
7657
7658 Next (Comp);
7659 end loop;
7660 end if;
7661 end;
7662 end if;
7663
7664 Insert_Actions (N, Aggr_Code);
7665 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7666 Analyze_And_Resolve (N, Typ);
7667 end Expand_Container_Aggregate;
7668
7669 ------------------------------
7670 -- Expand_N_Delta_Aggregate --
7671 ------------------------------
7672
7673 procedure Expand_N_Delta_Aggregate (N : Node_Id) is
7674 Loc : constant Source_Ptr := Sloc (N);
7675 Typ : constant Entity_Id := Etype (Expression (N));
7676 Decl : Node_Id;
7677
7678 begin
7679 Decl :=
7680 Make_Object_Declaration (Loc,
7681 Defining_Identifier => Make_Temporary (Loc, 'T'),
7682 Object_Definition => New_Occurrence_Of (Typ, Loc),
7683 Expression => New_Copy_Tree (Expression (N)));
7684
7685 if Is_Array_Type (Etype (N)) then
7686 Expand_Delta_Array_Aggregate (N, New_List (Decl));
7687 else
7688 Expand_Delta_Record_Aggregate (N, New_List (Decl));
7689 end if;
7690 end Expand_N_Delta_Aggregate;
7691
7692 ----------------------------------
7693 -- Expand_Delta_Array_Aggregate --
7694 ----------------------------------
7695
7696 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is
7697 Loc : constant Source_Ptr := Sloc (N);
7698 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
7699 Assoc : Node_Id;
7700
7701 function Generate_Loop (C : Node_Id) return Node_Id;
7702 -- Generate a loop containing individual component assignments for
7703 -- choices that are ranges, subtype indications, subtype names, and
7704 -- iterated component associations.
7705
7706 -------------------
7707 -- Generate_Loop --
7708 -------------------
7709
7710 function Generate_Loop (C : Node_Id) return Node_Id is
7711 Sl : constant Source_Ptr := Sloc (C);
7712 Ix : Entity_Id;
7713
7714 begin
7715 if Nkind (Parent (C)) = N_Iterated_Component_Association then
7716 Ix :=
7717 Make_Defining_Identifier (Loc,
7718 Chars => (Chars (Defining_Identifier (Parent (C)))));
7719 else
7720 Ix := Make_Temporary (Sl, 'I');
7721 end if;
7722
7723 return
7724 Make_Loop_Statement (Loc,
7725 Iteration_Scheme =>
7726 Make_Iteration_Scheme (Sl,
7727 Loop_Parameter_Specification =>
7728 Make_Loop_Parameter_Specification (Sl,
7729 Defining_Identifier => Ix,
7730 Discrete_Subtype_Definition => New_Copy_Tree (C))),
7731
7732 Statements => New_List (
7733 Make_Assignment_Statement (Sl,
7734 Name =>
7735 Make_Indexed_Component (Sl,
7736 Prefix => New_Occurrence_Of (Temp, Sl),
7737 Expressions => New_List (New_Occurrence_Of (Ix, Sl))),
7738 Expression => New_Copy_Tree (Expression (Assoc)))),
7739 End_Label => Empty);
7740 end Generate_Loop;
7741
7742 -- Local variables
7743
7744 Choice : Node_Id;
7745
7746 -- Start of processing for Expand_Delta_Array_Aggregate
7747
7748 begin
7749 Assoc := First (Component_Associations (N));
7750 while Present (Assoc) loop
7751 Choice := First (Choice_List (Assoc));
7752 if Nkind (Assoc) = N_Iterated_Component_Association then
7753 while Present (Choice) loop
7754 Append_To (Deltas, Generate_Loop (Choice));
7755 Next (Choice);
7756 end loop;
7757
7758 else
7759 while Present (Choice) loop
7760
7761 -- Choice can be given by a range, a subtype indication, a
7762 -- subtype name, a scalar value, or an entity.
7763
7764 if Nkind (Choice) = N_Range
7765 or else (Is_Entity_Name (Choice)
7766 and then Is_Type (Entity (Choice)))
7767 then
7768 Append_To (Deltas, Generate_Loop (Choice));
7769
7770 elsif Nkind (Choice) = N_Subtype_Indication then
7771 Append_To (Deltas,
7772 Generate_Loop (Range_Expression (Constraint (Choice))));
7773
7774 else
7775 Append_To (Deltas,
7776 Make_Assignment_Statement (Sloc (Choice),
7777 Name =>
7778 Make_Indexed_Component (Sloc (Choice),
7779 Prefix => New_Occurrence_Of (Temp, Loc),
7780 Expressions => New_List (New_Copy_Tree (Choice))),
7781 Expression => New_Copy_Tree (Expression (Assoc))));
7782 end if;
7783
7784 Next (Choice);
7785 end loop;
7786 end if;
7787
7788 Next (Assoc);
7789 end loop;
7790
7791 Insert_Actions (N, Deltas);
7792 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7793 end Expand_Delta_Array_Aggregate;
7794
7795 -----------------------------------
7796 -- Expand_Delta_Record_Aggregate --
7797 -----------------------------------
7798
7799 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is
7800 Loc : constant Source_Ptr := Sloc (N);
7801 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
7802 Assoc : Node_Id;
7803 Choice : Node_Id;
7804
7805 begin
7806 Assoc := First (Component_Associations (N));
7807
7808 while Present (Assoc) loop
7809 Choice := First (Choice_List (Assoc));
7810 while Present (Choice) loop
7811 Append_To (Deltas,
7812 Make_Assignment_Statement (Sloc (Choice),
7813 Name =>
7814 Make_Selected_Component (Sloc (Choice),
7815 Prefix => New_Occurrence_Of (Temp, Loc),
7816 Selector_Name => Make_Identifier (Loc, Chars (Choice))),
7817 Expression => New_Copy_Tree (Expression (Assoc))));
7818 Next (Choice);
7819 end loop;
7820
7821 Next (Assoc);
7822 end loop;
7823
7824 Insert_Actions (N, Deltas);
7825 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7826 end Expand_Delta_Record_Aggregate;
7827
7828 ----------------------------------
7829 -- Expand_N_Extension_Aggregate --
7830 ----------------------------------
7831
7832 -- If the ancestor part is an expression, add a component association for
7833 -- the parent field. If the type of the ancestor part is not the direct
7834 -- parent of the expected type, build recursively the needed ancestors.
7835 -- If the ancestor part is a subtype_mark, replace aggregate with a
7836 -- declaration for a temporary of the expected type, followed by
7837 -- individual assignments to the given components.
7838
7839 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
7840 A : constant Node_Id := Ancestor_Part (N);
7841 Loc : constant Source_Ptr := Sloc (N);
7842 Typ : constant Entity_Id := Etype (N);
7843
7844 begin
7845 -- If the ancestor is a subtype mark, an init proc must be called
7846 -- on the resulting object which thus has to be materialized in
7847 -- the front-end
7848
7849 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
7850 Convert_To_Assignments (N, Typ);
7851
7852 -- The extension aggregate is transformed into a record aggregate
7853 -- of the following form (c1 and c2 are inherited components)
7854
7855 -- (Exp with c3 => a, c4 => b)
7856 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
7857
7858 else
7859 Set_Etype (N, Typ);
7860
7861 if Tagged_Type_Expansion then
7862 Expand_Record_Aggregate (N,
7863 Orig_Tag =>
7864 New_Occurrence_Of
7865 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
7866 Parent_Expr => A);
7867
7868 -- No tag is needed in the case of a VM
7869
7870 else
7871 Expand_Record_Aggregate (N, Parent_Expr => A);
7872 end if;
7873 end if;
7874
7875 exception
7876 when RE_Not_Available =>
7877 return;
7878 end Expand_N_Extension_Aggregate;
7879
7880 -----------------------------
7881 -- Expand_Record_Aggregate --
7882 -----------------------------
7883
7884 procedure Expand_Record_Aggregate
7885 (N : Node_Id;
7886 Orig_Tag : Node_Id := Empty;
7887 Parent_Expr : Node_Id := Empty)
7888 is
7889 Loc : constant Source_Ptr := Sloc (N);
7890 Comps : constant List_Id := Component_Associations (N);
7891 Typ : constant Entity_Id := Etype (N);
7892 Base_Typ : constant Entity_Id := Base_Type (Typ);
7893
7894 Static_Components : Boolean := True;
7895 -- Flag to indicate whether all components are compile-time known,
7896 -- and the aggregate can be constructed statically and handled by
7897 -- the back-end. Set to False by Component_OK_For_Backend.
7898
7899 procedure Build_Back_End_Aggregate;
7900 -- Build a proper aggregate to be handled by the back-end
7901
7902 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
7903 -- Returns true if N is an expression of composite type which can be
7904 -- fully evaluated at compile time without raising constraint error.
7905 -- Such expressions can be passed as is to Gigi without any expansion.
7906 --
7907 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
7908 -- set and constants whose expression is such an aggregate, recursively.
7909
7910 function Component_OK_For_Backend return Boolean;
7911 -- Check for presence of a component which makes it impossible for the
7912 -- backend to process the aggregate, thus requiring the use of a series
7913 -- of assignment statements. Cases checked for are a nested aggregate
7914 -- needing Late_Expansion, the presence of a tagged component which may
7915 -- need tag adjustment, and a bit unaligned component reference.
7916 --
7917 -- We also force expansion into assignments if a component is of a
7918 -- mutable type (including a private type with discriminants) because
7919 -- in that case the size of the component to be copied may be smaller
7920 -- than the side of the target, and there is no simple way for gigi
7921 -- to compute the size of the object to be copied.
7922 --
7923 -- NOTE: This is part of the ongoing work to define precisely the
7924 -- interface between front-end and back-end handling of aggregates.
7925 -- In general it is desirable to pass aggregates as they are to gigi,
7926 -- in order to minimize elaboration code. This is one case where the
7927 -- semantics of Ada complicate the analysis and lead to anomalies in
7928 -- the gcc back-end if the aggregate is not expanded into assignments.
7929 --
7930 -- NOTE: This sets the global Static_Components to False in most, but
7931 -- not all, cases when it returns False.
7932
7933 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
7934 -- Return True if any element of L has Has_Per_Object_Constraint set.
7935 -- L should be the Choices component of an N_Component_Association.
7936
7937 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
7938 -- If any ancestor of the current type is private, the aggregate
7939 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
7940 -- because it will not be set when type and its parent are in the
7941 -- same scope, and the parent component needs expansion.
7942
7943 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
7944 -- For nested aggregates return the ultimate enclosing aggregate; for
7945 -- non-nested aggregates return N.
7946
7947 ------------------------------
7948 -- Build_Back_End_Aggregate --
7949 ------------------------------
7950
7951 procedure Build_Back_End_Aggregate is
7952 Comp : Entity_Id;
7953 New_Comp : Node_Id;
7954 Tag_Value : Node_Id;
7955
7956 begin
7957 if Nkind (N) = N_Aggregate then
7958
7959 -- If the aggregate is static and can be handled by the back-end,
7960 -- nothing left to do.
7961
7962 if Static_Components then
7963 Set_Compile_Time_Known_Aggregate (N);
7964 Set_Expansion_Delayed (N, False);
7965 end if;
7966 end if;
7967
7968 -- If no discriminants, nothing special to do
7969
7970 if not Has_Discriminants (Typ) then
7971 null;
7972
7973 -- Case of discriminants present
7974
7975 elsif Is_Derived_Type (Typ) then
7976
7977 -- For untagged types, non-stored discriminants are replaced with
7978 -- stored discriminants, which are the ones that gigi uses to
7979 -- describe the type and its components.
7980
7981 Generate_Aggregate_For_Derived_Type : declare
7982 procedure Prepend_Stored_Values (T : Entity_Id);
7983 -- Scan the list of stored discriminants of the type, and add
7984 -- their values to the aggregate being built.
7985
7986 ---------------------------
7987 -- Prepend_Stored_Values --
7988 ---------------------------
7989
7990 procedure Prepend_Stored_Values (T : Entity_Id) is
7991 Discr : Entity_Id;
7992 First_Comp : Node_Id := Empty;
7993
7994 begin
7995 Discr := First_Stored_Discriminant (T);
7996 while Present (Discr) loop
7997 New_Comp :=
7998 Make_Component_Association (Loc,
7999 Choices => New_List (
8000 New_Occurrence_Of (Discr, Loc)),
8001 Expression =>
8002 New_Copy_Tree
8003 (Get_Discriminant_Value
8004 (Discr,
8005 Typ,
8006 Discriminant_Constraint (Typ))));
8007
8008 if No (First_Comp) then
8009 Prepend_To (Component_Associations (N), New_Comp);
8010 else
8011 Insert_After (First_Comp, New_Comp);
8012 end if;
8013
8014 First_Comp := New_Comp;
8015 Next_Stored_Discriminant (Discr);
8016 end loop;
8017 end Prepend_Stored_Values;
8018
8019 -- Local variables
8020
8021 Constraints : constant List_Id := New_List;
8022
8023 Discr : Entity_Id;
8024 Decl : Node_Id;
8025 Num_Disc : Nat := 0;
8026 Num_Gird : Nat := 0;
8027
8028 -- Start of processing for Generate_Aggregate_For_Derived_Type
8029
8030 begin
8031 -- Remove the associations for the discriminant of derived type
8032
8033 declare
8034 First_Comp : Node_Id;
8035
8036 begin
8037 First_Comp := First (Component_Associations (N));
8038 while Present (First_Comp) loop
8039 Comp := First_Comp;
8040 Next (First_Comp);
8041
8042 if Ekind (Entity (First (Choices (Comp)))) =
8043 E_Discriminant
8044 then
8045 Remove (Comp);
8046 Num_Disc := Num_Disc + 1;
8047 end if;
8048 end loop;
8049 end;
8050
8051 -- Insert stored discriminant associations in the correct
8052 -- order. If there are more stored discriminants than new
8053 -- discriminants, there is at least one new discriminant that
8054 -- constrains more than one of the stored discriminants. In
8055 -- this case we need to construct a proper subtype of the
8056 -- parent type, in order to supply values to all the
8057 -- components. Otherwise there is one-one correspondence
8058 -- between the constraints and the stored discriminants.
8059
8060 Discr := First_Stored_Discriminant (Base_Type (Typ));
8061 while Present (Discr) loop
8062 Num_Gird := Num_Gird + 1;
8063 Next_Stored_Discriminant (Discr);
8064 end loop;
8065
8066 -- Case of more stored discriminants than new discriminants
8067
8068 if Num_Gird > Num_Disc then
8069
8070 -- Create a proper subtype of the parent type, which is the
8071 -- proper implementation type for the aggregate, and convert
8072 -- it to the intended target type.
8073
8074 Discr := First_Stored_Discriminant (Base_Type (Typ));
8075 while Present (Discr) loop
8076 New_Comp :=
8077 New_Copy_Tree
8078 (Get_Discriminant_Value
8079 (Discr,
8080 Typ,
8081 Discriminant_Constraint (Typ)));
8082
8083 Append (New_Comp, Constraints);
8084 Next_Stored_Discriminant (Discr);
8085 end loop;
8086
8087 Decl :=
8088 Make_Subtype_Declaration (Loc,
8089 Defining_Identifier => Make_Temporary (Loc, 'T'),
8090 Subtype_Indication =>
8091 Make_Subtype_Indication (Loc,
8092 Subtype_Mark =>
8093 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
8094 Constraint =>
8095 Make_Index_Or_Discriminant_Constraint
8096 (Loc, Constraints)));
8097
8098 Insert_Action (N, Decl);
8099 Prepend_Stored_Values (Base_Type (Typ));
8100
8101 Set_Etype (N, Defining_Identifier (Decl));
8102 Set_Analyzed (N);
8103
8104 Rewrite (N, Unchecked_Convert_To (Typ, N));
8105 Analyze (N);
8106
8107 -- Case where we do not have fewer new discriminants than
8108 -- stored discriminants, so in this case we can simply use the
8109 -- stored discriminants of the subtype.
8110
8111 else
8112 Prepend_Stored_Values (Typ);
8113 end if;
8114 end Generate_Aggregate_For_Derived_Type;
8115 end if;
8116
8117 if Is_Tagged_Type (Typ) then
8118
8119 -- In the tagged case, _parent and _tag component must be created
8120
8121 -- Reset Null_Present unconditionally. Tagged records always have
8122 -- at least one field (the tag or the parent).
8123
8124 Set_Null_Record_Present (N, False);
8125
8126 -- When the current aggregate comes from the expansion of an
8127 -- extension aggregate, the parent expr is replaced by an
8128 -- aggregate formed by selected components of this expr.
8129
8130 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
8131 Comp := First_Component_Or_Discriminant (Typ);
8132 while Present (Comp) loop
8133
8134 -- Skip all expander-generated components
8135
8136 if not Comes_From_Source (Original_Record_Component (Comp))
8137 then
8138 null;
8139
8140 else
8141 New_Comp :=
8142 Make_Selected_Component (Loc,
8143 Prefix =>
8144 Unchecked_Convert_To (Typ,
8145 Duplicate_Subexpr (Parent_Expr, True)),
8146 Selector_Name => New_Occurrence_Of (Comp, Loc));
8147
8148 Append_To (Comps,
8149 Make_Component_Association (Loc,
8150 Choices => New_List (
8151 New_Occurrence_Of (Comp, Loc)),
8152 Expression => New_Comp));
8153
8154 Analyze_And_Resolve (New_Comp, Etype (Comp));
8155 end if;
8156
8157 Next_Component_Or_Discriminant (Comp);
8158 end loop;
8159 end if;
8160
8161 -- Compute the value for the Tag now, if the type is a root it
8162 -- will be included in the aggregate right away, otherwise it will
8163 -- be propagated to the parent aggregate.
8164
8165 if Present (Orig_Tag) then
8166 Tag_Value := Orig_Tag;
8167
8168 elsif not Tagged_Type_Expansion then
8169 Tag_Value := Empty;
8170
8171 else
8172 Tag_Value :=
8173 New_Occurrence_Of
8174 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
8175 end if;
8176
8177 -- For a derived type, an aggregate for the parent is formed with
8178 -- all the inherited components.
8179
8180 if Is_Derived_Type (Typ) then
8181 declare
8182 First_Comp : Node_Id;
8183 Parent_Comps : List_Id;
8184 Parent_Aggr : Node_Id;
8185 Parent_Name : Node_Id;
8186
8187 begin
8188 -- Remove the inherited component association from the
8189 -- aggregate and store them in the parent aggregate
8190
8191 First_Comp := First (Component_Associations (N));
8192 Parent_Comps := New_List;
8193 while Present (First_Comp)
8194 and then
8195 Scope (Original_Record_Component
8196 (Entity (First (Choices (First_Comp))))) /=
8197 Base_Typ
8198 loop
8199 Comp := First_Comp;
8200 Next (First_Comp);
8201 Remove (Comp);
8202 Append (Comp, Parent_Comps);
8203 end loop;
8204
8205 Parent_Aggr :=
8206 Make_Aggregate (Loc,
8207 Component_Associations => Parent_Comps);
8208 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
8209
8210 -- Find the _parent component
8211
8212 Comp := First_Component (Typ);
8213 while Chars (Comp) /= Name_uParent loop
8214 Next_Component (Comp);
8215 end loop;
8216
8217 Parent_Name := New_Occurrence_Of (Comp, Loc);
8218
8219 -- Insert the parent aggregate
8220
8221 Prepend_To (Component_Associations (N),
8222 Make_Component_Association (Loc,
8223 Choices => New_List (Parent_Name),
8224 Expression => Parent_Aggr));
8225
8226 -- Expand recursively the parent propagating the right Tag
8227
8228 Expand_Record_Aggregate
8229 (Parent_Aggr, Tag_Value, Parent_Expr);
8230
8231 -- The ancestor part may be a nested aggregate that has
8232 -- delayed expansion: recheck now.
8233
8234 if not Component_OK_For_Backend then
8235 Convert_To_Assignments (N, Typ);
8236 end if;
8237 end;
8238
8239 -- For a root type, the tag component is added (unless compiling
8240 -- for the VMs, where tags are implicit).
8241
8242 elsif Tagged_Type_Expansion then
8243 declare
8244 Tag_Name : constant Node_Id :=
8245 New_Occurrence_Of
8246 (First_Tag_Component (Typ), Loc);
8247 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
8248 Conv_Node : constant Node_Id :=
8249 Unchecked_Convert_To (Typ_Tag, Tag_Value);
8250
8251 begin
8252 Set_Etype (Conv_Node, Typ_Tag);
8253 Prepend_To (Component_Associations (N),
8254 Make_Component_Association (Loc,
8255 Choices => New_List (Tag_Name),
8256 Expression => Conv_Node));
8257 end;
8258 end if;
8259 end if;
8260 end Build_Back_End_Aggregate;
8261
8262 ----------------------------------------
8263 -- Compile_Time_Known_Composite_Value --
8264 ----------------------------------------
8265
8266 function Compile_Time_Known_Composite_Value
8267 (N : Node_Id) return Boolean
8268 is
8269 begin
8270 -- If we have an entity name, then see if it is the name of a
8271 -- constant and if so, test the corresponding constant value.
8272
8273 if Is_Entity_Name (N) then
8274 declare
8275 E : constant Entity_Id := Entity (N);
8276 V : Node_Id;
8277 begin
8278 if Ekind (E) /= E_Constant then
8279 return False;
8280 else
8281 V := Constant_Value (E);
8282 return Present (V)
8283 and then Compile_Time_Known_Composite_Value (V);
8284 end if;
8285 end;
8286
8287 -- We have a value, see if it is compile time known
8288
8289 else
8290 if Nkind (N) = N_Aggregate then
8291 return Compile_Time_Known_Aggregate (N);
8292 end if;
8293
8294 -- All other types of values are not known at compile time
8295
8296 return False;
8297 end if;
8298
8299 end Compile_Time_Known_Composite_Value;
8300
8301 ------------------------------
8302 -- Component_OK_For_Backend --
8303 ------------------------------
8304
8305 function Component_OK_For_Backend return Boolean is
8306 C : Node_Id;
8307 Expr_Q : Node_Id;
8308
8309 begin
8310 if No (Comps) then
8311 return True;
8312 end if;
8313
8314 C := First (Comps);
8315 while Present (C) loop
8316
8317 -- If the component has box initialization, expansion is needed
8318 -- and component is not ready for backend.
8319
8320 if Box_Present (C) then
8321 return False;
8322 end if;
8323
8324 if Nkind (Expression (C)) = N_Qualified_Expression then
8325 Expr_Q := Expression (Expression (C));
8326 else
8327 Expr_Q := Expression (C);
8328 end if;
8329
8330 -- Return False for array components whose bounds raise
8331 -- constraint error.
8332
8333 declare
8334 Comp : constant Entity_Id := First (Choices (C));
8335 Indx : Node_Id;
8336
8337 begin
8338 if Present (Etype (Comp))
8339 and then Is_Array_Type (Etype (Comp))
8340 then
8341 Indx := First_Index (Etype (Comp));
8342 while Present (Indx) loop
8343 if Nkind (Type_Low_Bound (Etype (Indx))) =
8344 N_Raise_Constraint_Error
8345 or else Nkind (Type_High_Bound (Etype (Indx))) =
8346 N_Raise_Constraint_Error
8347 then
8348 return False;
8349 end if;
8350
8351 Next_Index (Indx);
8352 end loop;
8353 end if;
8354 end;
8355
8356 -- Return False if the aggregate has any associations for tagged
8357 -- components that may require tag adjustment.
8358
8359 -- These are cases where the source expression may have a tag that
8360 -- could differ from the component tag (e.g., can occur for type
8361 -- conversions and formal parameters). (Tag adjustment not needed
8362 -- if Tagged_Type_Expansion because object tags are implicit in
8363 -- the machine.)
8364
8365 if Is_Tagged_Type (Etype (Expr_Q))
8366 and then
8367 (Nkind (Expr_Q) = N_Type_Conversion
8368 or else
8369 (Is_Entity_Name (Expr_Q)
8370 and then Is_Formal (Entity (Expr_Q))))
8371 and then Tagged_Type_Expansion
8372 then
8373 Static_Components := False;
8374 return False;
8375
8376 elsif Is_Delayed_Aggregate (Expr_Q) then
8377 Static_Components := False;
8378 return False;
8379
8380 elsif Nkind (Expr_Q) = N_Quantified_Expression then
8381 Static_Components := False;
8382 return False;
8383
8384 elsif Possible_Bit_Aligned_Component (Expr_Q) then
8385 Static_Components := False;
8386 return False;
8387
8388 elsif Modify_Tree_For_C
8389 and then Nkind (C) = N_Component_Association
8390 and then Has_Per_Object_Constraint (Choices (C))
8391 then
8392 Static_Components := False;
8393 return False;
8394
8395 elsif Modify_Tree_For_C
8396 and then Nkind (Expr_Q) = N_Identifier
8397 and then Is_Array_Type (Etype (Expr_Q))
8398 then
8399 Static_Components := False;
8400 return False;
8401
8402 elsif Modify_Tree_For_C
8403 and then Nkind (Expr_Q) = N_Type_Conversion
8404 and then Is_Array_Type (Etype (Expr_Q))
8405 then
8406 Static_Components := False;
8407 return False;
8408 end if;
8409
8410 if Is_Elementary_Type (Etype (Expr_Q)) then
8411 if not Compile_Time_Known_Value (Expr_Q) then
8412 Static_Components := False;
8413 end if;
8414
8415 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
8416 Static_Components := False;
8417
8418 if Is_Private_Type (Etype (Expr_Q))
8419 and then Has_Discriminants (Etype (Expr_Q))
8420 then
8421 return False;
8422 end if;
8423 end if;
8424
8425 Next (C);
8426 end loop;
8427
8428 return True;
8429 end Component_OK_For_Backend;
8430
8431 -------------------------------
8432 -- Has_Per_Object_Constraint --
8433 -------------------------------
8434
8435 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
8436 N : Node_Id := First (L);
8437 begin
8438 while Present (N) loop
8439 if Is_Entity_Name (N)
8440 and then Present (Entity (N))
8441 and then Has_Per_Object_Constraint (Entity (N))
8442 then
8443 return True;
8444 end if;
8445
8446 Next (N);
8447 end loop;
8448
8449 return False;
8450 end Has_Per_Object_Constraint;
8451
8452 -----------------------------------
8453 -- Has_Visible_Private_Ancestor --
8454 -----------------------------------
8455
8456 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
8457 R : constant Entity_Id := Root_Type (Id);
8458 T1 : Entity_Id := Id;
8459
8460 begin
8461 loop
8462 if Is_Private_Type (T1) then
8463 return True;
8464
8465 elsif T1 = R then
8466 return False;
8467
8468 else
8469 T1 := Etype (T1);
8470 end if;
8471 end loop;
8472 end Has_Visible_Private_Ancestor;
8473
8474 -------------------------
8475 -- Top_Level_Aggregate --
8476 -------------------------
8477
8478 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
8479 Aggr : Node_Id;
8480
8481 begin
8482 Aggr := N;
8483 while Present (Parent (Aggr))
8484 and then Nkind (Parent (Aggr)) in
8485 N_Aggregate | N_Component_Association
8486 loop
8487 Aggr := Parent (Aggr);
8488 end loop;
8489
8490 return Aggr;
8491 end Top_Level_Aggregate;
8492
8493 -- Local variables
8494
8495 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
8496
8497 -- Start of processing for Expand_Record_Aggregate
8498
8499 begin
8500 -- If the aggregate is to be assigned to a full access variable, we have
8501 -- to prevent a piecemeal assignment even if the aggregate is to be
8502 -- expanded. We create a temporary for the aggregate, and assign the
8503 -- temporary instead, so that the back end can generate an atomic move
8504 -- for it.
8505
8506 if Is_Full_Access_Aggregate (N) then
8507 return;
8508
8509 -- No special management required for aggregates used to initialize
8510 -- statically allocated dispatch tables
8511
8512 elsif Is_Static_Dispatch_Table_Aggregate (N) then
8513 return;
8514 end if;
8515
8516 -- If the pragma Aggregate_Individually_Assign is set, always convert to
8517 -- assignments.
8518
8519 if Aggregate_Individually_Assign then
8520 Convert_To_Assignments (N, Typ);
8521
8522 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
8523 -- are build-in-place function calls. The assignments will each turn
8524 -- into a build-in-place function call. If components are all static,
8525 -- we can pass the aggregate to the back end regardless of limitedness.
8526
8527 -- Extension aggregates, aggregates in extended return statements, and
8528 -- aggregates for C++ imported types must be expanded.
8529
8530 elsif Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
8531 if Nkind (Parent (N)) not in
8532 N_Component_Association | N_Object_Declaration
8533 then
8534 Convert_To_Assignments (N, Typ);
8535
8536 elsif Nkind (N) = N_Extension_Aggregate
8537 or else Convention (Typ) = Convention_CPP
8538 then
8539 Convert_To_Assignments (N, Typ);
8540
8541 elsif not Size_Known_At_Compile_Time (Typ)
8542 or else not Component_OK_For_Backend
8543 or else not Static_Components
8544 then
8545 Convert_To_Assignments (N, Typ);
8546
8547 -- In all other cases, build a proper aggregate to be handled by
8548 -- the back-end.
8549
8550 else
8551 Build_Back_End_Aggregate;
8552 end if;
8553
8554 -- Gigi doesn't properly handle temporaries of variable size so we
8555 -- generate it in the front-end
8556
8557 elsif not Size_Known_At_Compile_Time (Typ)
8558 and then Tagged_Type_Expansion
8559 then
8560 Convert_To_Assignments (N, Typ);
8561
8562 -- An aggregate used to initialize a controlled object must be turned
8563 -- into component assignments as the components themselves may require
8564 -- finalization actions such as adjustment.
8565
8566 elsif Needs_Finalization (Typ) then
8567 Convert_To_Assignments (N, Typ);
8568
8569 -- Ada 2005 (AI-287): In case of default initialized components we
8570 -- convert the aggregate into assignments.
8571
8572 elsif Has_Default_Init_Comps (N) then
8573 Convert_To_Assignments (N, Typ);
8574
8575 -- Check components
8576
8577 elsif not Component_OK_For_Backend then
8578 Convert_To_Assignments (N, Typ);
8579
8580 -- If an ancestor is private, some components are not inherited and we
8581 -- cannot expand into a record aggregate.
8582
8583 elsif Has_Visible_Private_Ancestor (Typ) then
8584 Convert_To_Assignments (N, Typ);
8585
8586 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
8587 -- is not able to handle the aggregate for Late_Request.
8588
8589 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
8590 Convert_To_Assignments (N, Typ);
8591
8592 -- If the tagged types covers interface types we need to initialize all
8593 -- hidden components containing pointers to secondary dispatch tables.
8594
8595 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
8596 Convert_To_Assignments (N, Typ);
8597
8598 -- If some components are mutable, the size of the aggregate component
8599 -- may be distinct from the default size of the type component, so
8600 -- we need to expand to insure that the back-end copies the proper
8601 -- size of the data. However, if the aggregate is the initial value of
8602 -- a constant, the target is immutable and might be built statically
8603 -- if components are appropriate.
8604
8605 elsif Has_Mutable_Components (Typ)
8606 and then
8607 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
8608 or else not Constant_Present (Parent (Top_Level_Aggr))
8609 or else not Static_Components)
8610 then
8611 Convert_To_Assignments (N, Typ);
8612
8613 -- If the type involved has bit aligned components, then we are not sure
8614 -- that the back end can handle this case correctly.
8615
8616 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
8617 Convert_To_Assignments (N, Typ);
8618
8619 -- When generating C, only generate an aggregate when declaring objects
8620 -- since C does not support aggregates in e.g. assignment statements.
8621
8622 elsif Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
8623 Convert_To_Assignments (N, Typ);
8624
8625 -- In all other cases, build a proper aggregate to be handled by gigi
8626
8627 else
8628 Build_Back_End_Aggregate;
8629 end if;
8630 end Expand_Record_Aggregate;
8631
8632 ---------------------
8633 -- Get_Base_Object --
8634 ---------------------
8635
8636 function Get_Base_Object (N : Node_Id) return Entity_Id is
8637 R : Node_Id;
8638
8639 begin
8640 R := Get_Referenced_Object (N);
8641
8642 while Nkind (R) in N_Indexed_Component | N_Selected_Component | N_Slice
8643 loop
8644 R := Get_Referenced_Object (Prefix (R));
8645 end loop;
8646
8647 if Is_Entity_Name (R) and then Is_Object (Entity (R)) then
8648 return Entity (R);
8649 else
8650 return Empty;
8651 end if;
8652 end Get_Base_Object;
8653
8654 ----------------------------
8655 -- Has_Default_Init_Comps --
8656 ----------------------------
8657
8658 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
8659 Comps : constant List_Id := Component_Associations (N);
8660 C : Node_Id;
8661 Expr : Node_Id;
8662
8663 begin
8664 pragma Assert (Nkind (N) in N_Aggregate | N_Extension_Aggregate);
8665
8666 if No (Comps) then
8667 return False;
8668 end if;
8669
8670 if Has_Self_Reference (N) then
8671 return True;
8672 end if;
8673
8674 -- Check if any direct component has default initialized components
8675
8676 C := First (Comps);
8677 while Present (C) loop
8678 if Box_Present (C) then
8679 return True;
8680 end if;
8681
8682 Next (C);
8683 end loop;
8684
8685 -- Recursive call in case of aggregate expression
8686
8687 C := First (Comps);
8688 while Present (C) loop
8689 Expr := Expression (C);
8690
8691 if Present (Expr)
8692 and then Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
8693 and then Has_Default_Init_Comps (Expr)
8694 then
8695 return True;
8696 end if;
8697
8698 Next (C);
8699 end loop;
8700
8701 return False;
8702 end Has_Default_Init_Comps;
8703
8704 ----------------------------------------
8705 -- Is_Build_In_Place_Aggregate_Return --
8706 ----------------------------------------
8707
8708 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean is
8709 P : Node_Id := Parent (N);
8710
8711 begin
8712 while Nkind (P) = N_Qualified_Expression loop
8713 P := Parent (P);
8714 end loop;
8715
8716 if Nkind (P) = N_Simple_Return_Statement then
8717 null;
8718
8719 elsif Nkind (Parent (P)) = N_Extended_Return_Statement then
8720 P := Parent (P);
8721
8722 else
8723 return False;
8724 end if;
8725
8726 return
8727 Is_Build_In_Place_Function
8728 (Return_Applies_To (Return_Statement_Entity (P)));
8729 end Is_Build_In_Place_Aggregate_Return;
8730
8731 --------------------------
8732 -- Is_Delayed_Aggregate --
8733 --------------------------
8734
8735 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
8736 Node : Node_Id := N;
8737 Kind : Node_Kind := Nkind (Node);
8738
8739 begin
8740 if Kind = N_Qualified_Expression then
8741 Node := Expression (Node);
8742 Kind := Nkind (Node);
8743 end if;
8744
8745 if Kind not in N_Aggregate | N_Extension_Aggregate then
8746 return False;
8747 else
8748 return Expansion_Delayed (Node);
8749 end if;
8750 end Is_Delayed_Aggregate;
8751
8752 --------------------------------
8753 -- Is_CCG_Supported_Aggregate --
8754 --------------------------------
8755
8756 function Is_CCG_Supported_Aggregate
8757 (N : Node_Id) return Boolean
8758 is
8759 P : Node_Id := Parent (N);
8760
8761 begin
8762 -- Aggregates are not supported for nonstandard rep clauses, since they
8763 -- may lead to extra padding fields in CCG.
8764
8765 if Is_Record_Type (Etype (N))
8766 and then Has_Non_Standard_Rep (Etype (N))
8767 then
8768 return False;
8769 end if;
8770
8771 while Present (P) and then Nkind (P) = N_Aggregate loop
8772 P := Parent (P);
8773 end loop;
8774
8775 -- Check cases where aggregates are supported by the CCG backend
8776
8777 if Nkind (P) = N_Object_Declaration then
8778 declare
8779 P_Typ : constant Entity_Id := Etype (Defining_Identifier (P));
8780
8781 begin
8782 if Is_Record_Type (P_Typ) then
8783 return True;
8784 else
8785 return Compile_Time_Known_Bounds (P_Typ);
8786 end if;
8787 end;
8788
8789 elsif Nkind (P) = N_Qualified_Expression then
8790 if Nkind (Parent (P)) = N_Object_Declaration then
8791 declare
8792 P_Typ : constant Entity_Id :=
8793 Etype (Defining_Identifier (Parent (P)));
8794 begin
8795 if Is_Record_Type (P_Typ) then
8796 return True;
8797 else
8798 return Compile_Time_Known_Bounds (P_Typ);
8799 end if;
8800 end;
8801
8802 elsif Nkind (Parent (P)) = N_Allocator then
8803 return True;
8804 end if;
8805 end if;
8806
8807 return False;
8808 end Is_CCG_Supported_Aggregate;
8809
8810 ----------------------------------------
8811 -- Is_Static_Dispatch_Table_Aggregate --
8812 ----------------------------------------
8813
8814 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
8815 Typ : constant Entity_Id := Base_Type (Etype (N));
8816
8817 begin
8818 return Building_Static_Dispatch_Tables
8819 and then Tagged_Type_Expansion
8820
8821 -- Avoid circularity when rebuilding the compiler
8822
8823 and then not Is_RTU (Cunit_Entity (Get_Source_Unit (N)), Ada_Tags)
8824 and then (Is_RTE (Typ, RE_Dispatch_Table_Wrapper)
8825 or else
8826 Is_RTE (Typ, RE_Address_Array)
8827 or else
8828 Is_RTE (Typ, RE_Type_Specific_Data)
8829 or else
8830 Is_RTE (Typ, RE_Tag_Table)
8831 or else
8832 Is_RTE (Typ, RE_Object_Specific_Data)
8833 or else
8834 Is_RTE (Typ, RE_Interface_Data)
8835 or else
8836 Is_RTE (Typ, RE_Interfaces_Array)
8837 or else
8838 Is_RTE (Typ, RE_Interface_Data_Element));
8839 end Is_Static_Dispatch_Table_Aggregate;
8840
8841 -----------------------------
8842 -- Is_Two_Dim_Packed_Array --
8843 -----------------------------
8844
8845 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
8846 C : constant Int := UI_To_Int (Component_Size (Typ));
8847 begin
8848 return Number_Dimensions (Typ) = 2
8849 and then Is_Bit_Packed_Array (Typ)
8850 and then (C = 1 or else C = 2 or else C = 4);
8851 end Is_Two_Dim_Packed_Array;
8852
8853 --------------------
8854 -- Late_Expansion --
8855 --------------------
8856
8857 function Late_Expansion
8858 (N : Node_Id;
8859 Typ : Entity_Id;
8860 Target : Node_Id) return List_Id
8861 is
8862 Aggr_Code : List_Id;
8863 New_Aggr : Node_Id;
8864
8865 begin
8866 if Is_Array_Type (Typ) then
8867 -- If the assignment can be done directly by the back end, then
8868 -- reset Set_Expansion_Delayed and do not expand further.
8869
8870 if not CodePeer_Mode
8871 and then not Modify_Tree_For_C
8872 and then not Possible_Bit_Aligned_Component (Target)
8873 and then not Is_Possibly_Unaligned_Slice (Target)
8874 and then Aggr_Assignment_OK_For_Backend (N)
8875 then
8876 New_Aggr := New_Copy_Tree (N);
8877 Set_Expansion_Delayed (New_Aggr, False);
8878
8879 Aggr_Code :=
8880 New_List (
8881 Make_OK_Assignment_Statement (Sloc (New_Aggr),
8882 Name => Target,
8883 Expression => New_Aggr));
8884
8885 -- Or else, generate component assignments to it
8886
8887 else
8888 Aggr_Code :=
8889 Build_Array_Aggr_Code
8890 (N => N,
8891 Ctype => Component_Type (Typ),
8892 Index => First_Index (Typ),
8893 Into => Target,
8894 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
8895 Indexes => No_List);
8896 end if;
8897
8898 -- Directly or indirectly (e.g. access protected procedure) a record
8899
8900 else
8901 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
8902 end if;
8903
8904 -- Save the last assignment statement associated with the aggregate
8905 -- when building a controlled object. This reference is utilized by
8906 -- the finalization machinery when marking an object as successfully
8907 -- initialized.
8908
8909 if Needs_Finalization (Typ)
8910 and then Is_Entity_Name (Target)
8911 and then Present (Entity (Target))
8912 and then Ekind (Entity (Target)) in E_Constant | E_Variable
8913 then
8914 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
8915 end if;
8916
8917 return Aggr_Code;
8918 end Late_Expansion;
8919
8920 ----------------------------------
8921 -- Make_OK_Assignment_Statement --
8922 ----------------------------------
8923
8924 function Make_OK_Assignment_Statement
8925 (Sloc : Source_Ptr;
8926 Name : Node_Id;
8927 Expression : Node_Id) return Node_Id
8928 is
8929 begin
8930 Set_Assignment_OK (Name);
8931 return Make_Assignment_Statement (Sloc, Name, Expression);
8932 end Make_OK_Assignment_Statement;
8933
8934 ------------------------
8935 -- Max_Aggregate_Size --
8936 ------------------------
8937
8938 function Max_Aggregate_Size
8939 (N : Node_Id;
8940 Default_Size : Nat := 5000) return Nat
8941 is
8942 function Use_Small_Size (N : Node_Id) return Boolean;
8943 -- True if we should return a very small size, which means large
8944 -- aggregates will be implemented as a loop when possible (potentially
8945 -- transformed to memset calls).
8946
8947 function Aggr_Context (N : Node_Id) return Node_Id;
8948 -- Return the context in which the aggregate appears, not counting
8949 -- qualified expressions and similar.
8950
8951 ------------------
8952 -- Aggr_Context --
8953 ------------------
8954
8955 function Aggr_Context (N : Node_Id) return Node_Id is
8956 Result : Node_Id := Parent (N);
8957 begin
8958 if Nkind (Result) in N_Qualified_Expression
8959 | N_Type_Conversion
8960 | N_Unchecked_Type_Conversion
8961 | N_If_Expression
8962 | N_Case_Expression
8963 | N_Component_Association
8964 | N_Aggregate
8965 then
8966 Result := Aggr_Context (Result);
8967 end if;
8968
8969 return Result;
8970 end Aggr_Context;
8971
8972 --------------------
8973 -- Use_Small_Size --
8974 --------------------
8975
8976 function Use_Small_Size (N : Node_Id) return Boolean is
8977 C : constant Node_Id := Aggr_Context (N);
8978 -- The decision depends on the context in which the aggregate occurs,
8979 -- and for variable declarations, whether we are nested inside a
8980 -- subprogram.
8981 begin
8982 case Nkind (C) is
8983 -- True for assignment statements and similar
8984
8985 when N_Assignment_Statement
8986 | N_Simple_Return_Statement
8987 | N_Allocator
8988 | N_Attribute_Reference
8989 =>
8990 return True;
8991
8992 -- True for nested variable declarations. False for library level
8993 -- variables, and for constants (whether or not nested).
8994
8995 when N_Object_Declaration =>
8996 return not Constant_Present (C)
8997 and then Ekind (Current_Scope) in Subprogram_Kind;
8998
8999 -- False for all other contexts
9000
9001 when others =>
9002 return False;
9003 end case;
9004 end Use_Small_Size;
9005
9006 -- Local variables
9007
9008 Typ : constant Entity_Id := Etype (N);
9009
9010 -- Start of processing for Max_Aggregate_Size
9011
9012 begin
9013 -- We use a small limit in CodePeer mode where we favor loops instead of
9014 -- thousands of single assignments (from large aggregates).
9015
9016 -- We also increase the limit to 2**24 (about 16 million) if
9017 -- Restrictions (No_Elaboration_Code) or Restrictions
9018 -- (No_Implicit_Loops) is specified, since in either case we are at risk
9019 -- of declaring the program illegal because of this limit. We also
9020 -- increase the limit when Static_Elaboration_Desired, given that this
9021 -- means that objects are intended to be placed in data memory.
9022
9023 -- Same if the aggregate is for a packed two-dimensional array, because
9024 -- if components are static it is much more efficient to construct a
9025 -- one-dimensional equivalent array with static components.
9026
9027 if CodePeer_Mode then
9028 return 100;
9029 elsif Restriction_Active (No_Elaboration_Code)
9030 or else Restriction_Active (No_Implicit_Loops)
9031 or else Is_Two_Dim_Packed_Array (Typ)
9032 or else (Ekind (Current_Scope) = E_Package
9033 and then Static_Elaboration_Desired (Current_Scope))
9034 then
9035 return 2 ** 24;
9036 elsif Use_Small_Size (N) then
9037 return 64;
9038 end if;
9039
9040 return Default_Size;
9041 end Max_Aggregate_Size;
9042
9043 -----------------------
9044 -- Number_Of_Choices --
9045 -----------------------
9046
9047 function Number_Of_Choices (N : Node_Id) return Nat is
9048 Assoc : Node_Id;
9049 Choice : Node_Id;
9050
9051 Nb_Choices : Nat := 0;
9052
9053 begin
9054 if Present (Expressions (N)) then
9055 return 0;
9056 end if;
9057
9058 Assoc := First (Component_Associations (N));
9059 while Present (Assoc) loop
9060 Choice := First (Choice_List (Assoc));
9061 while Present (Choice) loop
9062 if Nkind (Choice) /= N_Others_Choice then
9063 Nb_Choices := Nb_Choices + 1;
9064 end if;
9065
9066 Next (Choice);
9067 end loop;
9068
9069 Next (Assoc);
9070 end loop;
9071
9072 return Nb_Choices;
9073 end Number_Of_Choices;
9074
9075 ------------------------------------
9076 -- Packed_Array_Aggregate_Handled --
9077 ------------------------------------
9078
9079 -- The current version of this procedure will handle at compile time
9080 -- any array aggregate that meets these conditions:
9081
9082 -- One and two dimensional, bit packed
9083 -- Underlying packed type is modular type
9084 -- Bounds are within 32-bit Int range
9085 -- All bounds and values are static
9086
9087 -- Note: for now, in the 2-D case, we only handle component sizes of
9088 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
9089
9090 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
9091 Loc : constant Source_Ptr := Sloc (N);
9092 Typ : constant Entity_Id := Etype (N);
9093 Ctyp : constant Entity_Id := Component_Type (Typ);
9094
9095 Not_Handled : exception;
9096 -- Exception raised if this aggregate cannot be handled
9097
9098 begin
9099 -- Handle one- or two dimensional bit packed array
9100
9101 if not Is_Bit_Packed_Array (Typ)
9102 or else Number_Dimensions (Typ) > 2
9103 then
9104 return False;
9105 end if;
9106
9107 -- If two-dimensional, check whether it can be folded, and transformed
9108 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
9109 -- the original type.
9110
9111 if Number_Dimensions (Typ) = 2 then
9112 return Two_Dim_Packed_Array_Handled (N);
9113 end if;
9114
9115 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
9116 return False;
9117 end if;
9118
9119 if not Is_Scalar_Type (Ctyp) then
9120 return False;
9121 end if;
9122
9123 declare
9124 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
9125
9126 Lo : Node_Id;
9127 Hi : Node_Id;
9128 -- Bounds of index type
9129
9130 Lob : Uint;
9131 Hib : Uint;
9132 -- Values of bounds if compile time known
9133
9134 function Get_Component_Val (N : Node_Id) return Uint;
9135 -- Given a expression value N of the component type Ctyp, returns a
9136 -- value of Csiz (component size) bits representing this value. If
9137 -- the value is nonstatic or any other reason exists why the value
9138 -- cannot be returned, then Not_Handled is raised.
9139
9140 -----------------------
9141 -- Get_Component_Val --
9142 -----------------------
9143
9144 function Get_Component_Val (N : Node_Id) return Uint is
9145 Val : Uint;
9146
9147 begin
9148 -- We have to analyze the expression here before doing any further
9149 -- processing here. The analysis of such expressions is deferred
9150 -- till expansion to prevent some problems of premature analysis.
9151
9152 Analyze_And_Resolve (N, Ctyp);
9153
9154 -- Must have a compile time value. String literals have to be
9155 -- converted into temporaries as well, because they cannot easily
9156 -- be converted into their bit representation.
9157
9158 if not Compile_Time_Known_Value (N)
9159 or else Nkind (N) = N_String_Literal
9160 then
9161 raise Not_Handled;
9162 end if;
9163
9164 Val := Expr_Rep_Value (N);
9165
9166 -- Adjust for bias, and strip proper number of bits
9167
9168 if Has_Biased_Representation (Ctyp) then
9169 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
9170 end if;
9171
9172 return Val mod Uint_2 ** Csiz;
9173 end Get_Component_Val;
9174
9175 -- Here we know we have a one dimensional bit packed array
9176
9177 begin
9178 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
9179
9180 -- Cannot do anything if bounds are dynamic
9181
9182 if not Compile_Time_Known_Value (Lo)
9183 or else
9184 not Compile_Time_Known_Value (Hi)
9185 then
9186 return False;
9187 end if;
9188
9189 -- Or are silly out of range of int bounds
9190
9191 Lob := Expr_Value (Lo);
9192 Hib := Expr_Value (Hi);
9193
9194 if not UI_Is_In_Int_Range (Lob)
9195 or else
9196 not UI_Is_In_Int_Range (Hib)
9197 then
9198 return False;
9199 end if;
9200
9201 -- At this stage we have a suitable aggregate for handling at compile
9202 -- time. The only remaining checks are that the values of expressions
9203 -- in the aggregate are compile-time known (checks are performed by
9204 -- Get_Component_Val), and that any subtypes or ranges are statically
9205 -- known.
9206
9207 -- If the aggregate is not fully positional at this stage, then
9208 -- convert it to positional form. Either this will fail, in which
9209 -- case we can do nothing, or it will succeed, in which case we have
9210 -- succeeded in handling the aggregate and transforming it into a
9211 -- modular value, or it will stay an aggregate, in which case we
9212 -- have failed to create a packed value for it.
9213
9214 if Present (Component_Associations (N)) then
9215 Convert_To_Positional (N, Handle_Bit_Packed => True);
9216 return Nkind (N) /= N_Aggregate;
9217 end if;
9218
9219 -- Otherwise we are all positional, so convert to proper value
9220
9221 declare
9222 Lov : constant Int := UI_To_Int (Lob);
9223 Hiv : constant Int := UI_To_Int (Hib);
9224
9225 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
9226 -- The length of the array (number of elements)
9227
9228 Aggregate_Val : Uint;
9229 -- Value of aggregate. The value is set in the low order bits of
9230 -- this value. For the little-endian case, the values are stored
9231 -- from low-order to high-order and for the big-endian case the
9232 -- values are stored from high-order to low-order. Note that gigi
9233 -- will take care of the conversions to left justify the value in
9234 -- the big endian case (because of left justified modular type
9235 -- processing), so we do not have to worry about that here.
9236
9237 Lit : Node_Id;
9238 -- Integer literal for resulting constructed value
9239
9240 Shift : Nat;
9241 -- Shift count from low order for next value
9242
9243 Incr : Int;
9244 -- Shift increment for loop
9245
9246 Expr : Node_Id;
9247 -- Next expression from positional parameters of aggregate
9248
9249 Left_Justified : Boolean;
9250 -- Set True if we are filling the high order bits of the target
9251 -- value (i.e. the value is left justified).
9252
9253 begin
9254 -- For little endian, we fill up the low order bits of the target
9255 -- value. For big endian we fill up the high order bits of the
9256 -- target value (which is a left justified modular value).
9257
9258 Left_Justified := Bytes_Big_Endian;
9259
9260 -- Switch justification if using -gnatd8
9261
9262 if Debug_Flag_8 then
9263 Left_Justified := not Left_Justified;
9264 end if;
9265
9266 -- Switch justfification if reverse storage order
9267
9268 if Reverse_Storage_Order (Base_Type (Typ)) then
9269 Left_Justified := not Left_Justified;
9270 end if;
9271
9272 if Left_Justified then
9273 Shift := Csiz * (Len - 1);
9274 Incr := -Csiz;
9275 else
9276 Shift := 0;
9277 Incr := +Csiz;
9278 end if;
9279
9280 -- Loop to set the values
9281
9282 if Len = 0 then
9283 Aggregate_Val := Uint_0;
9284 else
9285 Expr := First (Expressions (N));
9286 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
9287
9288 for J in 2 .. Len loop
9289 Shift := Shift + Incr;
9290 Next (Expr);
9291 Aggregate_Val :=
9292 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
9293 end loop;
9294 end if;
9295
9296 -- Now we can rewrite with the proper value
9297
9298 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
9299 Set_Print_In_Hex (Lit);
9300
9301 -- Construct the expression using this literal. Note that it is
9302 -- important to qualify the literal with its proper modular type
9303 -- since universal integer does not have the required range and
9304 -- also this is a left justified modular type, which is important
9305 -- in the big-endian case.
9306
9307 Rewrite (N,
9308 Unchecked_Convert_To (Typ,
9309 Make_Qualified_Expression (Loc,
9310 Subtype_Mark =>
9311 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
9312 Expression => Lit)));
9313
9314 Analyze_And_Resolve (N, Typ);
9315 return True;
9316 end;
9317 end;
9318
9319 exception
9320 when Not_Handled =>
9321 return False;
9322 end Packed_Array_Aggregate_Handled;
9323
9324 ----------------------------
9325 -- Has_Mutable_Components --
9326 ----------------------------
9327
9328 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
9329 Comp : Entity_Id;
9330 Ctyp : Entity_Id;
9331
9332 begin
9333 Comp := First_Component (Typ);
9334 while Present (Comp) loop
9335 Ctyp := Underlying_Type (Etype (Comp));
9336 if Is_Record_Type (Ctyp)
9337 and then Has_Discriminants (Ctyp)
9338 and then not Is_Constrained (Ctyp)
9339 then
9340 return True;
9341 end if;
9342
9343 Next_Component (Comp);
9344 end loop;
9345
9346 return False;
9347 end Has_Mutable_Components;
9348
9349 ------------------------------
9350 -- Initialize_Discriminants --
9351 ------------------------------
9352
9353 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
9354 Loc : constant Source_Ptr := Sloc (N);
9355 Bas : constant Entity_Id := Base_Type (Typ);
9356 Par : constant Entity_Id := Etype (Bas);
9357 Decl : constant Node_Id := Parent (Par);
9358 Ref : Node_Id;
9359
9360 begin
9361 if Is_Tagged_Type (Bas)
9362 and then Is_Derived_Type (Bas)
9363 and then Has_Discriminants (Par)
9364 and then Has_Discriminants (Bas)
9365 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
9366 and then Nkind (Decl) = N_Full_Type_Declaration
9367 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
9368 and then
9369 Present (Variant_Part (Component_List (Type_Definition (Decl))))
9370 and then Nkind (N) /= N_Extension_Aggregate
9371 then
9372
9373 -- Call init proc to set discriminants.
9374 -- There should eventually be a special procedure for this ???
9375
9376 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
9377 Insert_Actions_After (N,
9378 Build_Initialization_Call (Sloc (N), Ref, Typ));
9379 end if;
9380 end Initialize_Discriminants;
9381
9382 ----------------
9383 -- Must_Slide --
9384 ----------------
9385
9386 function Must_Slide
9387 (Obj_Type : Entity_Id;
9388 Typ : Entity_Id) return Boolean
9389 is
9390 L1, L2, H1, H2 : Node_Id;
9391
9392 begin
9393 -- No sliding if the type of the object is not established yet, if it is
9394 -- an unconstrained type whose actual subtype comes from the aggregate,
9395 -- or if the two types are identical.
9396
9397 if not Is_Array_Type (Obj_Type) then
9398 return False;
9399
9400 elsif not Is_Constrained (Obj_Type) then
9401 return False;
9402
9403 elsif Typ = Obj_Type then
9404 return False;
9405
9406 else
9407 -- Sliding can only occur along the first dimension
9408
9409 Get_Index_Bounds (First_Index (Typ), L1, H1);
9410 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
9411
9412 if not Is_OK_Static_Expression (L1) or else
9413 not Is_OK_Static_Expression (L2) or else
9414 not Is_OK_Static_Expression (H1) or else
9415 not Is_OK_Static_Expression (H2)
9416 then
9417 return False;
9418 else
9419 return Expr_Value (L1) /= Expr_Value (L2)
9420 or else
9421 Expr_Value (H1) /= Expr_Value (H2);
9422 end if;
9423 end if;
9424 end Must_Slide;
9425
9426 ---------------------------------
9427 -- Process_Transient_Component --
9428 ---------------------------------
9429
9430 procedure Process_Transient_Component
9431 (Loc : Source_Ptr;
9432 Comp_Typ : Entity_Id;
9433 Init_Expr : Node_Id;
9434 Fin_Call : out Node_Id;
9435 Hook_Clear : out Node_Id;
9436 Aggr : Node_Id := Empty;
9437 Stmts : List_Id := No_List)
9438 is
9439 procedure Add_Item (Item : Node_Id);
9440 -- Insert arbitrary node Item into the tree depending on the values of
9441 -- Aggr and Stmts.
9442
9443 --------------
9444 -- Add_Item --
9445 --------------
9446
9447 procedure Add_Item (Item : Node_Id) is
9448 begin
9449 if Present (Aggr) then
9450 Insert_Action (Aggr, Item);
9451 else
9452 pragma Assert (Present (Stmts));
9453 Append_To (Stmts, Item);
9454 end if;
9455 end Add_Item;
9456
9457 -- Local variables
9458
9459 Hook_Assign : Node_Id;
9460 Hook_Decl : Node_Id;
9461 Ptr_Decl : Node_Id;
9462 Res_Decl : Node_Id;
9463 Res_Id : Entity_Id;
9464 Res_Typ : Entity_Id;
9465
9466 -- Start of processing for Process_Transient_Component
9467
9468 begin
9469 -- Add the access type, which provides a reference to the function
9470 -- result. Generate:
9471
9472 -- type Res_Typ is access all Comp_Typ;
9473
9474 Res_Typ := Make_Temporary (Loc, 'A');
9475 Set_Ekind (Res_Typ, E_General_Access_Type);
9476 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
9477
9478 Add_Item
9479 (Make_Full_Type_Declaration (Loc,
9480 Defining_Identifier => Res_Typ,
9481 Type_Definition =>
9482 Make_Access_To_Object_Definition (Loc,
9483 All_Present => True,
9484 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
9485
9486 -- Add the temporary which captures the result of the function call.
9487 -- Generate:
9488
9489 -- Res : constant Res_Typ := Init_Expr'Reference;
9490
9491 -- Note that this temporary is effectively a transient object because
9492 -- its lifetime is bounded by the current array or record component.
9493
9494 Res_Id := Make_Temporary (Loc, 'R');
9495 Set_Ekind (Res_Id, E_Constant);
9496 Set_Etype (Res_Id, Res_Typ);
9497
9498 -- Mark the transient object as successfully processed to avoid double
9499 -- finalization.
9500
9501 Set_Is_Finalized_Transient (Res_Id);
9502
9503 -- Signal the general finalization machinery that this transient object
9504 -- should not be considered for finalization actions because its cleanup
9505 -- will be performed by Process_Transient_Component_Completion.
9506
9507 Set_Is_Ignored_Transient (Res_Id);
9508
9509 Res_Decl :=
9510 Make_Object_Declaration (Loc,
9511 Defining_Identifier => Res_Id,
9512 Constant_Present => True,
9513 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
9514 Expression =>
9515 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
9516
9517 Add_Item (Res_Decl);
9518
9519 -- Construct all pieces necessary to hook and finalize the transient
9520 -- result.
9521
9522 Build_Transient_Object_Statements
9523 (Obj_Decl => Res_Decl,
9524 Fin_Call => Fin_Call,
9525 Hook_Assign => Hook_Assign,
9526 Hook_Clear => Hook_Clear,
9527 Hook_Decl => Hook_Decl,
9528 Ptr_Decl => Ptr_Decl);
9529
9530 -- Add the access type which provides a reference to the transient
9531 -- result. Generate:
9532
9533 -- type Ptr_Typ is access all Comp_Typ;
9534
9535 Add_Item (Ptr_Decl);
9536
9537 -- Add the temporary which acts as a hook to the transient result.
9538 -- Generate:
9539
9540 -- Hook : Ptr_Typ := null;
9541
9542 Add_Item (Hook_Decl);
9543
9544 -- Attach the transient result to the hook. Generate:
9545
9546 -- Hook := Ptr_Typ (Res);
9547
9548 Add_Item (Hook_Assign);
9549
9550 -- The original initialization expression now references the value of
9551 -- the temporary function result. Generate:
9552
9553 -- Res.all
9554
9555 Rewrite (Init_Expr,
9556 Make_Explicit_Dereference (Loc,
9557 Prefix => New_Occurrence_Of (Res_Id, Loc)));
9558 end Process_Transient_Component;
9559
9560 --------------------------------------------
9561 -- Process_Transient_Component_Completion --
9562 --------------------------------------------
9563
9564 procedure Process_Transient_Component_Completion
9565 (Loc : Source_Ptr;
9566 Aggr : Node_Id;
9567 Fin_Call : Node_Id;
9568 Hook_Clear : Node_Id;
9569 Stmts : List_Id)
9570 is
9571 Exceptions_OK : constant Boolean :=
9572 not Restriction_Active (No_Exception_Propagation);
9573
9574 begin
9575 pragma Assert (Present (Hook_Clear));
9576
9577 -- Generate the following code if exception propagation is allowed:
9578
9579 -- declare
9580 -- Abort : constant Boolean := Triggered_By_Abort;
9581 -- <or>
9582 -- Abort : constant Boolean := False; -- no abort
9583
9584 -- E : Exception_Occurrence;
9585 -- Raised : Boolean := False;
9586
9587 -- begin
9588 -- [Abort_Defer;]
9589
9590 -- begin
9591 -- Hook := null;
9592 -- [Deep_]Finalize (Res.all);
9593
9594 -- exception
9595 -- when others =>
9596 -- if not Raised then
9597 -- Raised := True;
9598 -- Save_Occurrence (E,
9599 -- Get_Curent_Excep.all.all);
9600 -- end if;
9601 -- end;
9602
9603 -- [Abort_Undefer;]
9604
9605 -- if Raised and then not Abort then
9606 -- Raise_From_Controlled_Operation (E);
9607 -- end if;
9608 -- end;
9609
9610 if Exceptions_OK then
9611 Abort_And_Exception : declare
9612 Blk_Decls : constant List_Id := New_List;
9613 Blk_Stmts : constant List_Id := New_List;
9614 Fin_Stmts : constant List_Id := New_List;
9615
9616 Fin_Data : Finalization_Exception_Data;
9617
9618 begin
9619 -- Create the declarations of the two flags and the exception
9620 -- occurrence.
9621
9622 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
9623
9624 -- Generate:
9625 -- Abort_Defer;
9626
9627 if Abort_Allowed then
9628 Append_To (Blk_Stmts,
9629 Build_Runtime_Call (Loc, RE_Abort_Defer));
9630 end if;
9631
9632 -- Wrap the hook clear and the finalization call in order to trap
9633 -- a potential exception.
9634
9635 Append_To (Fin_Stmts, Hook_Clear);
9636
9637 if Present (Fin_Call) then
9638 Append_To (Fin_Stmts, Fin_Call);
9639 end if;
9640
9641 Append_To (Blk_Stmts,
9642 Make_Block_Statement (Loc,
9643 Handled_Statement_Sequence =>
9644 Make_Handled_Sequence_Of_Statements (Loc,
9645 Statements => Fin_Stmts,
9646 Exception_Handlers => New_List (
9647 Build_Exception_Handler (Fin_Data)))));
9648
9649 -- Generate:
9650 -- Abort_Undefer;
9651
9652 if Abort_Allowed then
9653 Append_To (Blk_Stmts,
9654 Build_Runtime_Call (Loc, RE_Abort_Undefer));
9655 end if;
9656
9657 -- Reraise the potential exception with a proper "upgrade" to
9658 -- Program_Error if needed.
9659
9660 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
9661
9662 -- Wrap everything in a block
9663
9664 Append_To (Stmts,
9665 Make_Block_Statement (Loc,
9666 Declarations => Blk_Decls,
9667 Handled_Statement_Sequence =>
9668 Make_Handled_Sequence_Of_Statements (Loc,
9669 Statements => Blk_Stmts)));
9670 end Abort_And_Exception;
9671
9672 -- Generate the following code if exception propagation is not allowed
9673 -- and aborts are allowed:
9674
9675 -- begin
9676 -- Abort_Defer;
9677 -- Hook := null;
9678 -- [Deep_]Finalize (Res.all);
9679 -- at end
9680 -- Abort_Undefer_Direct;
9681 -- end;
9682
9683 elsif Abort_Allowed then
9684 Abort_Only : declare
9685 Blk_Stmts : constant List_Id := New_List;
9686
9687 begin
9688 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
9689 Append_To (Blk_Stmts, Hook_Clear);
9690
9691 if Present (Fin_Call) then
9692 Append_To (Blk_Stmts, Fin_Call);
9693 end if;
9694
9695 Append_To (Stmts,
9696 Build_Abort_Undefer_Block (Loc,
9697 Stmts => Blk_Stmts,
9698 Context => Aggr));
9699 end Abort_Only;
9700
9701 -- Otherwise generate:
9702
9703 -- Hook := null;
9704 -- [Deep_]Finalize (Res.all);
9705
9706 else
9707 Append_To (Stmts, Hook_Clear);
9708
9709 if Present (Fin_Call) then
9710 Append_To (Stmts, Fin_Call);
9711 end if;
9712 end if;
9713 end Process_Transient_Component_Completion;
9714
9715 ---------------------
9716 -- Sort_Case_Table --
9717 ---------------------
9718
9719 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
9720 L : constant Int := Case_Table'First;
9721 U : constant Int := Case_Table'Last;
9722 K : Int;
9723 J : Int;
9724 T : Case_Bounds;
9725
9726 begin
9727 K := L;
9728 while K /= U loop
9729 T := Case_Table (K + 1);
9730
9731 J := K + 1;
9732 while J /= L
9733 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
9734 Expr_Value (T.Choice_Lo)
9735 loop
9736 Case_Table (J) := Case_Table (J - 1);
9737 J := J - 1;
9738 end loop;
9739
9740 Case_Table (J) := T;
9741 K := K + 1;
9742 end loop;
9743 end Sort_Case_Table;
9744
9745 ----------------------------
9746 -- Static_Array_Aggregate --
9747 ----------------------------
9748
9749 function Static_Array_Aggregate (N : Node_Id) return Boolean is
9750 function Is_Static_Component (Nod : Node_Id) return Boolean;
9751 -- Return True if Nod has a compile-time known value and can be passed
9752 -- as is to the back-end without further expansion.
9753
9754 ---------------------------
9755 -- Is_Static_Component --
9756 ---------------------------
9757
9758 function Is_Static_Component (Nod : Node_Id) return Boolean is
9759 begin
9760 if Nkind (Nod) in N_Integer_Literal | N_Real_Literal then
9761 return True;
9762
9763 elsif Is_Entity_Name (Nod)
9764 and then Present (Entity (Nod))
9765 and then Ekind (Entity (Nod)) = E_Enumeration_Literal
9766 then
9767 return True;
9768
9769 elsif Nkind (Nod) = N_Aggregate
9770 and then Compile_Time_Known_Aggregate (Nod)
9771 then
9772 return True;
9773
9774 else
9775 return False;
9776 end if;
9777 end Is_Static_Component;
9778
9779 -- Local variables
9780
9781 Bounds : constant Node_Id := Aggregate_Bounds (N);
9782 Typ : constant Entity_Id := Etype (N);
9783
9784 Agg : Node_Id;
9785 Expr : Node_Id;
9786 Lo : Node_Id;
9787 Hi : Node_Id;
9788
9789 -- Start of processing for Static_Array_Aggregate
9790
9791 begin
9792 if Is_Packed (Typ) or else Has_Discriminants (Component_Type (Typ)) then
9793 return False;
9794 end if;
9795
9796 if Present (Bounds)
9797 and then Nkind (Bounds) = N_Range
9798 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
9799 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
9800 then
9801 Lo := Low_Bound (Bounds);
9802 Hi := High_Bound (Bounds);
9803
9804 if No (Component_Associations (N)) then
9805
9806 -- Verify that all components are static
9807
9808 Expr := First (Expressions (N));
9809 while Present (Expr) loop
9810 if not Is_Static_Component (Expr) then
9811 return False;
9812 end if;
9813
9814 Next (Expr);
9815 end loop;
9816
9817 return True;
9818
9819 else
9820 -- We allow only a single named association, either a static
9821 -- range or an others_clause, with a static expression.
9822
9823 Expr := First (Component_Associations (N));
9824
9825 if Present (Expressions (N)) then
9826 return False;
9827
9828 elsif Present (Next (Expr)) then
9829 return False;
9830
9831 elsif Present (Next (First (Choice_List (Expr)))) then
9832 return False;
9833
9834 else
9835 -- The aggregate is static if all components are literals,
9836 -- or else all its components are static aggregates for the
9837 -- component type. We also limit the size of a static aggregate
9838 -- to prevent runaway static expressions.
9839
9840 if not Is_Static_Component (Expression (Expr)) then
9841 return False;
9842 end if;
9843
9844 if not Aggr_Size_OK (N) then
9845 return False;
9846 end if;
9847
9848 -- Create a positional aggregate with the right number of
9849 -- copies of the expression.
9850
9851 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
9852
9853 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
9854 loop
9855 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
9856
9857 -- The copied expression must be analyzed and resolved.
9858 -- Besides setting the type, this ensures that static
9859 -- expressions are appropriately marked as such.
9860
9861 Analyze_And_Resolve
9862 (Last (Expressions (Agg)), Component_Type (Typ));
9863 end loop;
9864
9865 Set_Aggregate_Bounds (Agg, Bounds);
9866 Set_Etype (Agg, Typ);
9867 Set_Analyzed (Agg);
9868 Rewrite (N, Agg);
9869 Set_Compile_Time_Known_Aggregate (N);
9870
9871 return True;
9872 end if;
9873 end if;
9874
9875 else
9876 return False;
9877 end if;
9878 end Static_Array_Aggregate;
9879
9880 ----------------------------------
9881 -- Two_Dim_Packed_Array_Handled --
9882 ----------------------------------
9883
9884 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
9885 Loc : constant Source_Ptr := Sloc (N);
9886 Typ : constant Entity_Id := Etype (N);
9887 Ctyp : constant Entity_Id := Component_Type (Typ);
9888 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
9889 Packed_Array : constant Entity_Id :=
9890 Packed_Array_Impl_Type (Base_Type (Typ));
9891
9892 One_Comp : Node_Id;
9893 -- Expression in original aggregate
9894
9895 One_Dim : Node_Id;
9896 -- One-dimensional subaggregate
9897
9898 begin
9899
9900 -- For now, only deal with cases where an integral number of elements
9901 -- fit in a single byte. This includes the most common boolean case.
9902
9903 if not (Comp_Size = 1 or else
9904 Comp_Size = 2 or else
9905 Comp_Size = 4)
9906 then
9907 return False;
9908 end if;
9909
9910 Convert_To_Positional (N, Handle_Bit_Packed => True);
9911
9912 -- Verify that all components are static
9913
9914 if Nkind (N) = N_Aggregate
9915 and then Compile_Time_Known_Aggregate (N)
9916 then
9917 null;
9918
9919 -- The aggregate may have been reanalyzed and converted already
9920
9921 elsif Nkind (N) /= N_Aggregate then
9922 return True;
9923
9924 -- If component associations remain, the aggregate is not static
9925
9926 elsif Present (Component_Associations (N)) then
9927 return False;
9928
9929 else
9930 One_Dim := First (Expressions (N));
9931 while Present (One_Dim) loop
9932 if Present (Component_Associations (One_Dim)) then
9933 return False;
9934 end if;
9935
9936 One_Comp := First (Expressions (One_Dim));
9937 while Present (One_Comp) loop
9938 if not Is_OK_Static_Expression (One_Comp) then
9939 return False;
9940 end if;
9941
9942 Next (One_Comp);
9943 end loop;
9944
9945 Next (One_Dim);
9946 end loop;
9947 end if;
9948
9949 -- Two-dimensional aggregate is now fully positional so pack one
9950 -- dimension to create a static one-dimensional array, and rewrite
9951 -- as an unchecked conversion to the original type.
9952
9953 declare
9954 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
9955 -- The packed array type is a byte array
9956
9957 Packed_Num : Nat;
9958 -- Number of components accumulated in current byte
9959
9960 Comps : List_Id;
9961 -- Assembled list of packed values for equivalent aggregate
9962
9963 Comp_Val : Uint;
9964 -- Integer value of component
9965
9966 Incr : Int;
9967 -- Step size for packing
9968
9969 Init_Shift : Int;
9970 -- Endian-dependent start position for packing
9971
9972 Shift : Int;
9973 -- Current insertion position
9974
9975 Val : Int;
9976 -- Component of packed array being assembled
9977
9978 begin
9979 Comps := New_List;
9980 Val := 0;
9981 Packed_Num := 0;
9982
9983 -- Account for endianness. See corresponding comment in
9984 -- Packed_Array_Aggregate_Handled concerning the following.
9985
9986 if Bytes_Big_Endian
9987 xor Debug_Flag_8
9988 xor Reverse_Storage_Order (Base_Type (Typ))
9989 then
9990 Init_Shift := Byte_Size - Comp_Size;
9991 Incr := -Comp_Size;
9992 else
9993 Init_Shift := 0;
9994 Incr := +Comp_Size;
9995 end if;
9996
9997 -- Iterate over each subaggregate
9998
9999 Shift := Init_Shift;
10000 One_Dim := First (Expressions (N));
10001 while Present (One_Dim) loop
10002 One_Comp := First (Expressions (One_Dim));
10003 while Present (One_Comp) loop
10004 if Packed_Num = Byte_Size / Comp_Size then
10005
10006 -- Byte is complete, add to list of expressions
10007
10008 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
10009 Val := 0;
10010 Shift := Init_Shift;
10011 Packed_Num := 0;
10012
10013 else
10014 Comp_Val := Expr_Rep_Value (One_Comp);
10015
10016 -- Adjust for bias, and strip proper number of bits
10017
10018 if Has_Biased_Representation (Ctyp) then
10019 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
10020 end if;
10021
10022 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
10023 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
10024 Shift := Shift + Incr;
10025 Next (One_Comp);
10026 Packed_Num := Packed_Num + 1;
10027 end if;
10028 end loop;
10029
10030 Next (One_Dim);
10031 end loop;
10032
10033 if Packed_Num > 0 then
10034
10035 -- Add final incomplete byte if present
10036
10037 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
10038 end if;
10039
10040 Rewrite (N,
10041 Unchecked_Convert_To (Typ,
10042 Make_Qualified_Expression (Loc,
10043 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
10044 Expression => Make_Aggregate (Loc, Expressions => Comps))));
10045 Analyze_And_Resolve (N);
10046 return True;
10047 end;
10048 end Two_Dim_Packed_Array_Handled;
10049
10050 end Exp_Aggr;