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