Debian Codegraph @

gcc/ada/exp_aggr.adb

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