]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/exp_aggr.adb
[Ada] Get rid of more references to Universal_Integer in expanded code
[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 declare
2047 Zero : constant Node_Id := Make_Integer_Literal (Loc, Uint_0);
2048 begin
2049 Analyze_And_Resolve (Zero, Packed_Array_Impl_Type (Typ));
2050 Append_To (New_Code,
2051 Make_Assignment_Statement (Loc,
2052 Name => New_Copy_Tree (Into),
2053 Expression => Unchecked_Convert_To (Typ, Zero)));
2054 end;
2055 end if;
2056
2057 -- If the component type contains tasks, we need to build a Master
2058 -- entity in the current scope, because it will be needed if build-
2059 -- in-place functions are called in the expanded code.
2060
2061 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
2062 Build_Master_Entity (Defining_Identifier (Parent (N)));
2063 end if;
2064
2065 -- STEP 1: Process component associations
2066
2067 -- For those associations that may generate a loop, initialize
2068 -- Loop_Actions to collect inserted actions that may be crated.
2069
2070 -- Skip this if no component associations
2071
2072 if No (Expressions (N)) then
2073
2074 -- STEP 1 (a): Sort the discrete choices
2075
2076 Assoc := First (Component_Associations (N));
2077 while Present (Assoc) loop
2078 Choice := First (Choice_List (Assoc));
2079 while Present (Choice) loop
2080 if Nkind (Choice) = N_Others_Choice then
2081 Others_Assoc := Assoc;
2082 exit;
2083 end if;
2084
2085 Get_Index_Bounds (Choice, Low, High);
2086
2087 if Low /= High then
2088 Set_Loop_Actions (Assoc, New_List);
2089 end if;
2090
2091 Nb_Choices := Nb_Choices + 1;
2092
2093 Table (Nb_Choices) :=
2094 (Choice_Lo => Low,
2095 Choice_Hi => High,
2096 Choice_Node => Get_Assoc_Expr (Assoc));
2097
2098 Next (Choice);
2099 end loop;
2100
2101 Next (Assoc);
2102 end loop;
2103
2104 -- If there is more than one set of choices these must be static
2105 -- and we can therefore sort them. Remember that Nb_Choices does not
2106 -- account for an others choice.
2107
2108 if Nb_Choices > 1 then
2109 Sort_Case_Table (Table);
2110 end if;
2111
2112 -- STEP 1 (b): take care of the whole set of discrete choices
2113
2114 for J in 1 .. Nb_Choices loop
2115 Low := Table (J).Choice_Lo;
2116 High := Table (J).Choice_Hi;
2117 Expr := Table (J).Choice_Node;
2118 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2119 end loop;
2120
2121 -- STEP 1 (c): generate the remaining loops to cover others choice
2122 -- We don't need to generate loops over empty gaps, but if there is
2123 -- a single empty range we must analyze the expression for semantics
2124
2125 if Present (Others_Assoc) then
2126 declare
2127 First : Boolean := True;
2128 Dup_Expr : Node_Id;
2129
2130 begin
2131 for J in 0 .. Nb_Choices loop
2132 if J = 0 then
2133 Low := Aggr_Low;
2134 else
2135 Low := Add (1, To => Table (J).Choice_Hi);
2136 end if;
2137
2138 if J = Nb_Choices then
2139 High := Aggr_High;
2140 else
2141 High := Add (-1, To => Table (J + 1).Choice_Lo);
2142 end if;
2143
2144 -- If this is an expansion within an init proc, make
2145 -- sure that discriminant references are replaced by
2146 -- the corresponding discriminal.
2147
2148 if Inside_Init_Proc then
2149 if Is_Entity_Name (Low)
2150 and then Ekind (Entity (Low)) = E_Discriminant
2151 then
2152 Set_Entity (Low, Discriminal (Entity (Low)));
2153 end if;
2154
2155 if Is_Entity_Name (High)
2156 and then Ekind (Entity (High)) = E_Discriminant
2157 then
2158 Set_Entity (High, Discriminal (Entity (High)));
2159 end if;
2160 end if;
2161
2162 if First
2163 or else not Empty_Range (Low, High)
2164 then
2165 First := False;
2166
2167 -- Duplicate the expression in case we will be generating
2168 -- several loops. As a result the expression is no longer
2169 -- shared between the loops and is reevaluated for each
2170 -- such loop.
2171
2172 Expr := Get_Assoc_Expr (Others_Assoc);
2173 Dup_Expr := New_Copy_Tree (Expr);
2174 Set_Parent (Dup_Expr, Parent (Expr));
2175
2176 Set_Loop_Actions (Others_Assoc, New_List);
2177 Append_List
2178 (Gen_Loop (Low, High, Dup_Expr), To => New_Code);
2179 end if;
2180 end loop;
2181 end;
2182 end if;
2183
2184 -- STEP 2: Process positional components
2185
2186 else
2187 -- STEP 2 (a): Generate the assignments for each positional element
2188 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2189 -- Aggr_L is analyzed and Add wants an analyzed expression.
2190
2191 Expr := First (Expressions (N));
2192 Nb_Elements := -1;
2193 while Present (Expr) loop
2194 Nb_Elements := Nb_Elements + 1;
2195 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2196 To => New_Code);
2197 Next (Expr);
2198 end loop;
2199
2200 -- STEP 2 (b): Generate final loop if an others choice is present
2201 -- Here Nb_Elements gives the offset of the last positional element.
2202
2203 if Present (Component_Associations (N)) then
2204 Assoc := Last (Component_Associations (N));
2205
2206 -- Ada 2005 (AI-287)
2207
2208 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2209 Aggr_High,
2210 Get_Assoc_Expr (Assoc)), -- AI-287
2211 To => New_Code);
2212 end if;
2213 end if;
2214
2215 return New_Code;
2216 end Build_Array_Aggr_Code;
2217
2218 ----------------------------
2219 -- Build_Record_Aggr_Code --
2220 ----------------------------
2221
2222 function Build_Record_Aggr_Code
2223 (N : Node_Id;
2224 Typ : Entity_Id;
2225 Lhs : Node_Id) return List_Id
2226 is
2227 Loc : constant Source_Ptr := Sloc (N);
2228 L : constant List_Id := New_List;
2229 N_Typ : constant Entity_Id := Etype (N);
2230
2231 Comp : Node_Id;
2232 Instr : Node_Id;
2233 Ref : Node_Id;
2234 Target : Entity_Id;
2235 Comp_Type : Entity_Id;
2236 Selector : Entity_Id;
2237 Comp_Expr : Node_Id;
2238 Expr_Q : Node_Id;
2239
2240 -- If this is an internal aggregate, the External_Final_List is an
2241 -- expression for the controller record of the enclosing type.
2242
2243 -- If the current aggregate has several controlled components, this
2244 -- expression will appear in several calls to attach to the finali-
2245 -- zation list, and it must not be shared.
2246
2247 Ancestor_Is_Expression : Boolean := False;
2248 Ancestor_Is_Subtype_Mark : Boolean := False;
2249
2250 Init_Typ : Entity_Id := Empty;
2251
2252 Finalization_Done : Boolean := False;
2253 -- True if Generate_Finalization_Actions has already been called; calls
2254 -- after the first do nothing.
2255
2256 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2257 -- Returns the value that the given discriminant of an ancestor type
2258 -- should receive (in the absence of a conflict with the value provided
2259 -- by an ancestor part of an extension aggregate).
2260
2261 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2262 -- Check that each of the discriminant values defined by the ancestor
2263 -- part of an extension aggregate match the corresponding values
2264 -- provided by either an association of the aggregate or by the
2265 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2266
2267 function Compatible_Int_Bounds
2268 (Agg_Bounds : Node_Id;
2269 Typ_Bounds : Node_Id) return Boolean;
2270 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2271 -- assumed that both bounds are integer ranges.
2272
2273 procedure Generate_Finalization_Actions;
2274 -- Deal with the various controlled type data structure initializations
2275 -- (but only if it hasn't been done already).
2276
2277 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2278 -- Returns the first discriminant association in the constraint
2279 -- associated with T, if any, otherwise returns Empty.
2280
2281 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2282 -- If the ancestor part is an unconstrained type and further ancestors
2283 -- do not provide discriminants for it, check aggregate components for
2284 -- values of the discriminants.
2285
2286 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2287 -- If Typ is derived, and constrains discriminants of the parent type,
2288 -- these discriminants are not components of the aggregate, and must be
2289 -- initialized. The assignments are appended to List. The same is done
2290 -- if Typ derives fron an already constrained subtype of a discriminated
2291 -- parent type.
2292
2293 procedure Init_Stored_Discriminants;
2294 -- If the type is derived and has inherited discriminants, generate
2295 -- explicit assignments for each, using the store constraint of the
2296 -- type. Note that both visible and stored discriminants must be
2297 -- initialized in case the derived type has some renamed and some
2298 -- constrained discriminants.
2299
2300 procedure Init_Visible_Discriminants;
2301 -- If type has discriminants, retrieve their values from aggregate,
2302 -- and generate explicit assignments for each. This does not include
2303 -- discriminants inherited from ancestor, which are handled above.
2304 -- The type of the aggregate is a subtype created ealier using the
2305 -- given values of the discriminant components of the aggregate.
2306
2307 procedure Initialize_Ctrl_Record_Component
2308 (Rec_Comp : Node_Id;
2309 Comp_Typ : Entity_Id;
2310 Init_Expr : Node_Id;
2311 Stmts : List_Id);
2312 -- Perform the initialization of controlled record component Rec_Comp.
2313 -- Comp_Typ is the component type. Init_Expr is the initialization
2314 -- expression for the record component. Hook-related declarations are
2315 -- inserted prior to aggregate N using Insert_Action. All remaining
2316 -- generated code is added to list Stmts.
2317
2318 procedure Initialize_Record_Component
2319 (Rec_Comp : Node_Id;
2320 Comp_Typ : Entity_Id;
2321 Init_Expr : Node_Id;
2322 Stmts : List_Id);
2323 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2324 -- is the component type. Init_Expr is the initialization expression
2325 -- of the record component. All generated code is added to list Stmts.
2326
2327 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2328 -- Check whether Bounds is a range node and its lower and higher bounds
2329 -- are integers literals.
2330
2331 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2332 -- If the aggregate contains a self-reference, traverse each expression
2333 -- to replace a possible self-reference with a reference to the proper
2334 -- component of the target of the assignment.
2335
2336 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2337 -- If default expression of a component mentions a discriminant of the
2338 -- type, it must be rewritten as the discriminant of the target object.
2339
2340 ---------------------------------
2341 -- Ancestor_Discriminant_Value --
2342 ---------------------------------
2343
2344 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2345 Assoc : Node_Id;
2346 Assoc_Elmt : Elmt_Id;
2347 Aggr_Comp : Entity_Id;
2348 Corresp_Disc : Entity_Id;
2349 Current_Typ : Entity_Id := Base_Type (Typ);
2350 Parent_Typ : Entity_Id;
2351 Parent_Disc : Entity_Id;
2352 Save_Assoc : Node_Id := Empty;
2353
2354 begin
2355 -- First check any discriminant associations to see if any of them
2356 -- provide a value for the discriminant.
2357
2358 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2359 Assoc := First (Component_Associations (N));
2360 while Present (Assoc) loop
2361 Aggr_Comp := Entity (First (Choices (Assoc)));
2362
2363 if Ekind (Aggr_Comp) = E_Discriminant then
2364 Save_Assoc := Expression (Assoc);
2365
2366 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2367 while Present (Corresp_Disc) loop
2368
2369 -- If found a corresponding discriminant then return the
2370 -- value given in the aggregate. (Note: this is not
2371 -- correct in the presence of side effects. ???)
2372
2373 if Disc = Corresp_Disc then
2374 return Duplicate_Subexpr (Expression (Assoc));
2375 end if;
2376
2377 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2378 end loop;
2379 end if;
2380
2381 Next (Assoc);
2382 end loop;
2383 end if;
2384
2385 -- No match found in aggregate, so chain up parent types to find
2386 -- a constraint that defines the value of the discriminant.
2387
2388 Parent_Typ := Etype (Current_Typ);
2389 while Current_Typ /= Parent_Typ loop
2390 if Has_Discriminants (Parent_Typ)
2391 and then not Has_Unknown_Discriminants (Parent_Typ)
2392 then
2393 Parent_Disc := First_Discriminant (Parent_Typ);
2394
2395 -- We either get the association from the subtype indication
2396 -- of the type definition itself, or from the discriminant
2397 -- constraint associated with the type entity (which is
2398 -- preferable, but it's not always present ???)
2399
2400 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2401 then
2402 Assoc := Get_Constraint_Association (Current_Typ);
2403 Assoc_Elmt := No_Elmt;
2404 else
2405 Assoc_Elmt :=
2406 First_Elmt (Discriminant_Constraint (Current_Typ));
2407 Assoc := Node (Assoc_Elmt);
2408 end if;
2409
2410 -- Traverse the discriminants of the parent type looking
2411 -- for one that corresponds.
2412
2413 while Present (Parent_Disc) and then Present (Assoc) loop
2414 Corresp_Disc := Parent_Disc;
2415 while Present (Corresp_Disc)
2416 and then Disc /= Corresp_Disc
2417 loop
2418 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2419 end loop;
2420
2421 if Disc = Corresp_Disc then
2422 if Nkind (Assoc) = N_Discriminant_Association then
2423 Assoc := Expression (Assoc);
2424 end if;
2425
2426 -- If the located association directly denotes
2427 -- a discriminant, then use the value of a saved
2428 -- association of the aggregate. This is an approach
2429 -- used to handle certain cases involving multiple
2430 -- discriminants mapped to a single discriminant of
2431 -- a descendant. It's not clear how to locate the
2432 -- appropriate discriminant value for such cases. ???
2433
2434 if Is_Entity_Name (Assoc)
2435 and then Ekind (Entity (Assoc)) = E_Discriminant
2436 then
2437 Assoc := Save_Assoc;
2438 end if;
2439
2440 return Duplicate_Subexpr (Assoc);
2441 end if;
2442
2443 Next_Discriminant (Parent_Disc);
2444
2445 if No (Assoc_Elmt) then
2446 Next (Assoc);
2447
2448 else
2449 Next_Elmt (Assoc_Elmt);
2450
2451 if Present (Assoc_Elmt) then
2452 Assoc := Node (Assoc_Elmt);
2453 else
2454 Assoc := Empty;
2455 end if;
2456 end if;
2457 end loop;
2458 end if;
2459
2460 Current_Typ := Parent_Typ;
2461 Parent_Typ := Etype (Current_Typ);
2462 end loop;
2463
2464 -- In some cases there's no ancestor value to locate (such as
2465 -- when an ancestor part given by an expression defines the
2466 -- discriminant value).
2467
2468 return Empty;
2469 end Ancestor_Discriminant_Value;
2470
2471 ----------------------------------
2472 -- Check_Ancestor_Discriminants --
2473 ----------------------------------
2474
2475 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2476 Discr : Entity_Id;
2477 Disc_Value : Node_Id;
2478 Cond : Node_Id;
2479
2480 begin
2481 Discr := First_Discriminant (Base_Type (Anc_Typ));
2482 while Present (Discr) loop
2483 Disc_Value := Ancestor_Discriminant_Value (Discr);
2484
2485 if Present (Disc_Value) then
2486 Cond := Make_Op_Ne (Loc,
2487 Left_Opnd =>
2488 Make_Selected_Component (Loc,
2489 Prefix => New_Copy_Tree (Target),
2490 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2491 Right_Opnd => Disc_Value);
2492
2493 Append_To (L,
2494 Make_Raise_Constraint_Error (Loc,
2495 Condition => Cond,
2496 Reason => CE_Discriminant_Check_Failed));
2497 end if;
2498
2499 Next_Discriminant (Discr);
2500 end loop;
2501 end Check_Ancestor_Discriminants;
2502
2503 ---------------------------
2504 -- Compatible_Int_Bounds --
2505 ---------------------------
2506
2507 function Compatible_Int_Bounds
2508 (Agg_Bounds : Node_Id;
2509 Typ_Bounds : Node_Id) return Boolean
2510 is
2511 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2512 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2513 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2514 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2515 begin
2516 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2517 end Compatible_Int_Bounds;
2518
2519 -----------------------------------
2520 -- Generate_Finalization_Actions --
2521 -----------------------------------
2522
2523 procedure Generate_Finalization_Actions is
2524 begin
2525 -- Do the work only the first time this is called
2526
2527 if Finalization_Done then
2528 return;
2529 end if;
2530
2531 Finalization_Done := True;
2532
2533 -- Determine the external finalization list. It is either the
2534 -- finalization list of the outer scope or the one coming from an
2535 -- outer aggregate. When the target is not a temporary, the proper
2536 -- scope is the scope of the target rather than the potentially
2537 -- transient current scope.
2538
2539 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2540 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2541 Set_Assignment_OK (Ref);
2542
2543 Append_To (L,
2544 Make_Procedure_Call_Statement (Loc,
2545 Name =>
2546 New_Occurrence_Of
2547 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2548 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2549 end if;
2550 end Generate_Finalization_Actions;
2551
2552 --------------------------------
2553 -- Get_Constraint_Association --
2554 --------------------------------
2555
2556 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2557 Indic : Node_Id;
2558 Typ : Entity_Id;
2559
2560 begin
2561 Typ := T;
2562
2563 -- If type is private, get constraint from full view. This was
2564 -- previously done in an instance context, but is needed whenever
2565 -- the ancestor part has a discriminant, possibly inherited through
2566 -- multiple derivations.
2567
2568 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2569 Typ := Full_View (Typ);
2570 end if;
2571
2572 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2573
2574 -- Verify that the subtype indication carries a constraint
2575
2576 if Nkind (Indic) = N_Subtype_Indication
2577 and then Present (Constraint (Indic))
2578 then
2579 return First (Constraints (Constraint (Indic)));
2580 end if;
2581
2582 return Empty;
2583 end Get_Constraint_Association;
2584
2585 -------------------------------------
2586 -- Get_Explicit_Discriminant_Value --
2587 -------------------------------------
2588
2589 function Get_Explicit_Discriminant_Value
2590 (D : Entity_Id) return Node_Id
2591 is
2592 Assoc : Node_Id;
2593 Choice : Node_Id;
2594 Val : Node_Id;
2595
2596 begin
2597 -- The aggregate has been normalized and all associations have a
2598 -- single choice.
2599
2600 Assoc := First (Component_Associations (N));
2601 while Present (Assoc) loop
2602 Choice := First (Choices (Assoc));
2603
2604 if Chars (Choice) = Chars (D) then
2605 Val := Expression (Assoc);
2606 Remove (Assoc);
2607 return Val;
2608 end if;
2609
2610 Next (Assoc);
2611 end loop;
2612
2613 return Empty;
2614 end Get_Explicit_Discriminant_Value;
2615
2616 -------------------------------
2617 -- Init_Hidden_Discriminants --
2618 -------------------------------
2619
2620 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2621 function Is_Completely_Hidden_Discriminant
2622 (Discr : Entity_Id) return Boolean;
2623 -- Determine whether Discr is a completely hidden discriminant of
2624 -- type Typ.
2625
2626 ---------------------------------------
2627 -- Is_Completely_Hidden_Discriminant --
2628 ---------------------------------------
2629
2630 function Is_Completely_Hidden_Discriminant
2631 (Discr : Entity_Id) return Boolean
2632 is
2633 Item : Entity_Id;
2634
2635 begin
2636 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2637 -- completely hidden discriminants.
2638
2639 Item := First_Entity (Typ);
2640 while Present (Item) loop
2641 if Ekind (Item) = E_Discriminant
2642 and then Is_Completely_Hidden (Item)
2643 and then Chars (Original_Record_Component (Item)) =
2644 Chars (Discr)
2645 then
2646 return True;
2647 end if;
2648
2649 Next_Entity (Item);
2650 end loop;
2651
2652 return False;
2653 end Is_Completely_Hidden_Discriminant;
2654
2655 -- Local variables
2656
2657 Base_Typ : Entity_Id;
2658 Discr : Entity_Id;
2659 Discr_Constr : Elmt_Id;
2660 Discr_Init : Node_Id;
2661 Discr_Val : Node_Id;
2662 In_Aggr_Type : Boolean;
2663 Par_Typ : Entity_Id;
2664
2665 -- Start of processing for Init_Hidden_Discriminants
2666
2667 begin
2668 -- The constraints on the hidden discriminants, if present, are kept
2669 -- in the Stored_Constraint list of the type itself, or in that of
2670 -- the base type. If not in the constraints of the aggregate itself,
2671 -- we examine ancestors to find discriminants that are not renamed
2672 -- by other discriminants but constrained explicitly.
2673
2674 In_Aggr_Type := True;
2675
2676 Base_Typ := Base_Type (Typ);
2677 while Is_Derived_Type (Base_Typ)
2678 and then
2679 (Present (Stored_Constraint (Base_Typ))
2680 or else
2681 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2682 loop
2683 Par_Typ := Etype (Base_Typ);
2684
2685 if not Has_Discriminants (Par_Typ) then
2686 return;
2687 end if;
2688
2689 Discr := First_Discriminant (Par_Typ);
2690
2691 -- We know that one of the stored-constraint lists is present
2692
2693 if Present (Stored_Constraint (Base_Typ)) then
2694 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2695
2696 -- For private extension, stored constraint may be on full view
2697
2698 elsif Is_Private_Type (Base_Typ)
2699 and then Present (Full_View (Base_Typ))
2700 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2701 then
2702 Discr_Constr :=
2703 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2704
2705 -- Otherwise, no discriminant to process
2706
2707 else
2708 Discr_Constr := No_Elmt;
2709 end if;
2710
2711 while Present (Discr) and then Present (Discr_Constr) loop
2712 Discr_Val := Node (Discr_Constr);
2713
2714 -- The parent discriminant is renamed in the derived type,
2715 -- nothing to initialize.
2716
2717 -- type Deriv_Typ (Discr : ...)
2718 -- is new Parent_Typ (Discr => Discr);
2719
2720 if Is_Entity_Name (Discr_Val)
2721 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2722 then
2723 null;
2724
2725 -- When the parent discriminant is constrained at the type
2726 -- extension level, it does not appear in the derived type.
2727
2728 -- type Deriv_Typ (Discr : ...)
2729 -- is new Parent_Typ (Discr => Discr,
2730 -- Hidden_Discr => Expression);
2731
2732 elsif Is_Completely_Hidden_Discriminant (Discr) then
2733 null;
2734
2735 -- Otherwise initialize the discriminant
2736
2737 else
2738 Discr_Init :=
2739 Make_OK_Assignment_Statement (Loc,
2740 Name =>
2741 Make_Selected_Component (Loc,
2742 Prefix => New_Copy_Tree (Target),
2743 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2744 Expression => New_Copy_Tree (Discr_Val));
2745
2746 Append_To (List, Discr_Init);
2747 end if;
2748
2749 Next_Elmt (Discr_Constr);
2750 Next_Discriminant (Discr);
2751 end loop;
2752
2753 In_Aggr_Type := False;
2754 Base_Typ := Base_Type (Par_Typ);
2755 end loop;
2756 end Init_Hidden_Discriminants;
2757
2758 --------------------------------
2759 -- Init_Visible_Discriminants --
2760 --------------------------------
2761
2762 procedure Init_Visible_Discriminants is
2763 Discriminant : Entity_Id;
2764 Discriminant_Value : Node_Id;
2765
2766 begin
2767 Discriminant := First_Discriminant (Typ);
2768 while Present (Discriminant) loop
2769 Comp_Expr :=
2770 Make_Selected_Component (Loc,
2771 Prefix => New_Copy_Tree (Target),
2772 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2773
2774 Discriminant_Value :=
2775 Get_Discriminant_Value
2776 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2777
2778 Instr :=
2779 Make_OK_Assignment_Statement (Loc,
2780 Name => Comp_Expr,
2781 Expression => New_Copy_Tree (Discriminant_Value));
2782
2783 Append_To (L, Instr);
2784
2785 Next_Discriminant (Discriminant);
2786 end loop;
2787 end Init_Visible_Discriminants;
2788
2789 -------------------------------
2790 -- Init_Stored_Discriminants --
2791 -------------------------------
2792
2793 procedure Init_Stored_Discriminants is
2794 Discriminant : Entity_Id;
2795 Discriminant_Value : Node_Id;
2796
2797 begin
2798 Discriminant := First_Stored_Discriminant (Typ);
2799 while Present (Discriminant) loop
2800 Comp_Expr :=
2801 Make_Selected_Component (Loc,
2802 Prefix => New_Copy_Tree (Target),
2803 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2804
2805 Discriminant_Value :=
2806 Get_Discriminant_Value
2807 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2808
2809 Instr :=
2810 Make_OK_Assignment_Statement (Loc,
2811 Name => Comp_Expr,
2812 Expression => New_Copy_Tree (Discriminant_Value));
2813
2814 Append_To (L, Instr);
2815
2816 Next_Stored_Discriminant (Discriminant);
2817 end loop;
2818 end Init_Stored_Discriminants;
2819
2820 --------------------------------------
2821 -- Initialize_Ctrl_Record_Component --
2822 --------------------------------------
2823
2824 procedure Initialize_Ctrl_Record_Component
2825 (Rec_Comp : Node_Id;
2826 Comp_Typ : Entity_Id;
2827 Init_Expr : Node_Id;
2828 Stmts : List_Id)
2829 is
2830 Fin_Call : Node_Id;
2831 Hook_Clear : Node_Id;
2832
2833 In_Place_Expansion : Boolean;
2834 -- Flag set when a nonlimited controlled function call requires
2835 -- in-place expansion.
2836
2837 begin
2838 -- Perform a preliminary analysis and resolution to determine what
2839 -- the initialization expression denotes. Unanalyzed function calls
2840 -- may appear as identifiers or indexed components.
2841
2842 if Nkind_In (Init_Expr, N_Function_Call,
2843 N_Identifier,
2844 N_Indexed_Component)
2845 and then not Analyzed (Init_Expr)
2846 then
2847 Preanalyze_And_Resolve (Init_Expr, Comp_Typ);
2848 end if;
2849
2850 In_Place_Expansion :=
2851 Nkind (Init_Expr) = N_Function_Call
2852 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
2853
2854 -- The initialization expression is a controlled function call.
2855 -- Perform in-place removal of side effects to avoid creating a
2856 -- transient scope.
2857
2858 -- This in-place expansion is not performed for limited transient
2859 -- objects because the initialization is already done in place.
2860
2861 if In_Place_Expansion then
2862
2863 -- Suppress the removal of side effects by general analysis
2864 -- because this behavior is emulated here. This avoids the
2865 -- generation of a transient scope, which leads to out-of-order
2866 -- adjustment and finalization.
2867
2868 Set_No_Side_Effect_Removal (Init_Expr);
2869
2870 -- Install all hook-related declarations and prepare the clean up
2871 -- statements. The generated code follows the initialization order
2872 -- of individual components and discriminants, rather than being
2873 -- inserted prior to the aggregate. This ensures that a transient
2874 -- component which mentions a discriminant has proper visibility
2875 -- of the discriminant.
2876
2877 Process_Transient_Component
2878 (Loc => Loc,
2879 Comp_Typ => Comp_Typ,
2880 Init_Expr => Init_Expr,
2881 Fin_Call => Fin_Call,
2882 Hook_Clear => Hook_Clear,
2883 Stmts => Stmts);
2884 end if;
2885
2886 -- Use the noncontrolled component initialization circuitry to
2887 -- assign the result of the function call to the record component.
2888 -- This also performs tag adjustment and [deep] adjustment of the
2889 -- record component.
2890
2891 Initialize_Record_Component
2892 (Rec_Comp => Rec_Comp,
2893 Comp_Typ => Comp_Typ,
2894 Init_Expr => Init_Expr,
2895 Stmts => Stmts);
2896
2897 -- At this point the record component is fully initialized. Complete
2898 -- the processing of the controlled record component by finalizing
2899 -- the transient function result.
2900
2901 if In_Place_Expansion then
2902 Process_Transient_Component_Completion
2903 (Loc => Loc,
2904 Aggr => N,
2905 Fin_Call => Fin_Call,
2906 Hook_Clear => Hook_Clear,
2907 Stmts => Stmts);
2908 end if;
2909 end Initialize_Ctrl_Record_Component;
2910
2911 ---------------------------------
2912 -- Initialize_Record_Component --
2913 ---------------------------------
2914
2915 procedure Initialize_Record_Component
2916 (Rec_Comp : Node_Id;
2917 Comp_Typ : Entity_Id;
2918 Init_Expr : Node_Id;
2919 Stmts : List_Id)
2920 is
2921 Exceptions_OK : constant Boolean :=
2922 not Restriction_Active (No_Exception_Propagation);
2923
2924 Finalization_OK : constant Boolean := Needs_Finalization (Comp_Typ);
2925
2926 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
2927 Adj_Call : Node_Id;
2928 Blk_Stmts : List_Id;
2929 Init_Stmt : Node_Id;
2930
2931 begin
2932 -- Protect the initialization statements from aborts. Generate:
2933
2934 -- Abort_Defer;
2935
2936 if Finalization_OK and Abort_Allowed then
2937 if Exceptions_OK then
2938 Blk_Stmts := New_List;
2939 else
2940 Blk_Stmts := Stmts;
2941 end if;
2942
2943 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
2944
2945 -- Otherwise aborts are not allowed. All generated code is added
2946 -- directly to the input list.
2947
2948 else
2949 Blk_Stmts := Stmts;
2950 end if;
2951
2952 -- Initialize the record component. Generate:
2953
2954 -- Rec_Comp := Init_Expr;
2955
2956 -- Note that the initialization expression is NOT replicated because
2957 -- only a single component may be initialized by it.
2958
2959 Init_Stmt :=
2960 Make_OK_Assignment_Statement (Loc,
2961 Name => New_Copy_Tree (Rec_Comp),
2962 Expression => Init_Expr);
2963 Set_No_Ctrl_Actions (Init_Stmt);
2964
2965 Append_To (Blk_Stmts, Init_Stmt);
2966
2967 -- Adjust the tag due to a possible view conversion. Generate:
2968
2969 -- Rec_Comp._tag := Full_TypeP;
2970
2971 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
2972 Append_To (Blk_Stmts,
2973 Make_OK_Assignment_Statement (Loc,
2974 Name =>
2975 Make_Selected_Component (Loc,
2976 Prefix => New_Copy_Tree (Rec_Comp),
2977 Selector_Name =>
2978 New_Occurrence_Of
2979 (First_Tag_Component (Full_Typ), Loc)),
2980
2981 Expression =>
2982 Unchecked_Convert_To (RTE (RE_Tag),
2983 New_Occurrence_Of
2984 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
2985 Loc))));
2986 end if;
2987
2988 -- Adjust the component. Generate:
2989
2990 -- [Deep_]Adjust (Rec_Comp);
2991
2992 if Finalization_OK
2993 and then not Is_Limited_Type (Comp_Typ)
2994 and then not Is_Build_In_Place_Function_Call (Init_Expr)
2995 then
2996 Adj_Call :=
2997 Make_Adjust_Call
2998 (Obj_Ref => New_Copy_Tree (Rec_Comp),
2999 Typ => Comp_Typ);
3000
3001 -- Guard against a missing [Deep_]Adjust when the component type
3002 -- was not properly frozen.
3003
3004 if Present (Adj_Call) then
3005 Append_To (Blk_Stmts, Adj_Call);
3006 end if;
3007 end if;
3008
3009 -- Complete the protection of the initialization statements
3010
3011 if Finalization_OK and Abort_Allowed then
3012
3013 -- Wrap the initialization statements in a block to catch a
3014 -- potential exception. Generate:
3015
3016 -- begin
3017 -- Abort_Defer;
3018 -- Rec_Comp := Init_Expr;
3019 -- Rec_Comp._tag := Full_TypP;
3020 -- [Deep_]Adjust (Rec_Comp);
3021 -- at end
3022 -- Abort_Undefer_Direct;
3023 -- end;
3024
3025 if Exceptions_OK then
3026 Append_To (Stmts,
3027 Build_Abort_Undefer_Block (Loc,
3028 Stmts => Blk_Stmts,
3029 Context => N));
3030
3031 -- Otherwise exceptions are not propagated. Generate:
3032
3033 -- Abort_Defer;
3034 -- Rec_Comp := Init_Expr;
3035 -- Rec_Comp._tag := Full_TypP;
3036 -- [Deep_]Adjust (Rec_Comp);
3037 -- Abort_Undefer;
3038
3039 else
3040 Append_To (Blk_Stmts,
3041 Build_Runtime_Call (Loc, RE_Abort_Undefer));
3042 end if;
3043 end if;
3044 end Initialize_Record_Component;
3045
3046 -------------------------
3047 -- Is_Int_Range_Bounds --
3048 -------------------------
3049
3050 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
3051 begin
3052 return Nkind (Bounds) = N_Range
3053 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
3054 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
3055 end Is_Int_Range_Bounds;
3056
3057 ------------------
3058 -- Replace_Type --
3059 ------------------
3060
3061 function Replace_Type (Expr : Node_Id) return Traverse_Result is
3062 begin
3063 -- Note regarding the Root_Type test below: Aggregate components for
3064 -- self-referential types include attribute references to the current
3065 -- instance, of the form: Typ'access, etc.. These references are
3066 -- rewritten as references to the target of the aggregate: the
3067 -- left-hand side of an assignment, the entity in a declaration,
3068 -- or a temporary. Without this test, we would improperly extended
3069 -- this rewriting to attribute references whose prefix was not the
3070 -- type of the aggregate.
3071
3072 if Nkind (Expr) = N_Attribute_Reference
3073 and then Is_Entity_Name (Prefix (Expr))
3074 and then Is_Type (Entity (Prefix (Expr)))
3075 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
3076 then
3077 if Is_Entity_Name (Lhs) then
3078 Rewrite (Prefix (Expr), New_Occurrence_Of (Entity (Lhs), Loc));
3079
3080 else
3081 Rewrite (Expr,
3082 Make_Attribute_Reference (Loc,
3083 Attribute_Name => Name_Unrestricted_Access,
3084 Prefix => New_Copy_Tree (Lhs)));
3085 Set_Analyzed (Parent (Expr), False);
3086 end if;
3087 end if;
3088
3089 return OK;
3090 end Replace_Type;
3091
3092 --------------------------
3093 -- Rewrite_Discriminant --
3094 --------------------------
3095
3096 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
3097 begin
3098 if Is_Entity_Name (Expr)
3099 and then Present (Entity (Expr))
3100 and then Ekind (Entity (Expr)) = E_In_Parameter
3101 and then Present (Discriminal_Link (Entity (Expr)))
3102 and then Scope (Discriminal_Link (Entity (Expr))) =
3103 Base_Type (Etype (N))
3104 then
3105 Rewrite (Expr,
3106 Make_Selected_Component (Loc,
3107 Prefix => New_Copy_Tree (Lhs),
3108 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
3109
3110 -- The generated code will be reanalyzed, but if the reference
3111 -- to the discriminant appears within an already analyzed
3112 -- expression (e.g. a conditional) we must set its proper entity
3113 -- now. Context is an initialization procedure.
3114
3115 Analyze (Expr);
3116 end if;
3117
3118 return OK;
3119 end Rewrite_Discriminant;
3120
3121 procedure Replace_Discriminants is
3122 new Traverse_Proc (Rewrite_Discriminant);
3123
3124 procedure Replace_Self_Reference is
3125 new Traverse_Proc (Replace_Type);
3126
3127 -- Start of processing for Build_Record_Aggr_Code
3128
3129 begin
3130 if Has_Self_Reference (N) then
3131 Replace_Self_Reference (N);
3132 end if;
3133
3134 -- If the target of the aggregate is class-wide, we must convert it
3135 -- to the actual type of the aggregate, so that the proper components
3136 -- are visible. We know already that the types are compatible.
3137
3138 if Present (Etype (Lhs))
3139 and then Is_Class_Wide_Type (Etype (Lhs))
3140 then
3141 Target := Unchecked_Convert_To (Typ, Lhs);
3142 else
3143 Target := Lhs;
3144 end if;
3145
3146 -- Deal with the ancestor part of extension aggregates or with the
3147 -- discriminants of the root type.
3148
3149 if Nkind (N) = N_Extension_Aggregate then
3150 declare
3151 Ancestor : constant Node_Id := Ancestor_Part (N);
3152 Adj_Call : Node_Id;
3153 Assign : List_Id;
3154
3155 begin
3156 -- If the ancestor part is a subtype mark "T", we generate
3157
3158 -- init-proc (T (tmp)); if T is constrained and
3159 -- init-proc (S (tmp)); where S applies an appropriate
3160 -- constraint if T is unconstrained
3161
3162 if Is_Entity_Name (Ancestor)
3163 and then Is_Type (Entity (Ancestor))
3164 then
3165 Ancestor_Is_Subtype_Mark := True;
3166
3167 if Is_Constrained (Entity (Ancestor)) then
3168 Init_Typ := Entity (Ancestor);
3169
3170 -- For an ancestor part given by an unconstrained type mark,
3171 -- create a subtype constrained by appropriate corresponding
3172 -- discriminant values coming from either associations of the
3173 -- aggregate or a constraint on a parent type. The subtype will
3174 -- be used to generate the correct default value for the
3175 -- ancestor part.
3176
3177 elsif Has_Discriminants (Entity (Ancestor)) then
3178 declare
3179 Anc_Typ : constant Entity_Id := Entity (Ancestor);
3180 Anc_Constr : constant List_Id := New_List;
3181 Discrim : Entity_Id;
3182 Disc_Value : Node_Id;
3183 New_Indic : Node_Id;
3184 Subt_Decl : Node_Id;
3185
3186 begin
3187 Discrim := First_Discriminant (Anc_Typ);
3188 while Present (Discrim) loop
3189 Disc_Value := Ancestor_Discriminant_Value (Discrim);
3190
3191 -- If no usable discriminant in ancestors, check
3192 -- whether aggregate has an explicit value for it.
3193
3194 if No (Disc_Value) then
3195 Disc_Value :=
3196 Get_Explicit_Discriminant_Value (Discrim);
3197 end if;
3198
3199 Append_To (Anc_Constr, Disc_Value);
3200 Next_Discriminant (Discrim);
3201 end loop;
3202
3203 New_Indic :=
3204 Make_Subtype_Indication (Loc,
3205 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
3206 Constraint =>
3207 Make_Index_Or_Discriminant_Constraint (Loc,
3208 Constraints => Anc_Constr));
3209
3210 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
3211
3212 Subt_Decl :=
3213 Make_Subtype_Declaration (Loc,
3214 Defining_Identifier => Init_Typ,
3215 Subtype_Indication => New_Indic);
3216
3217 -- Itypes must be analyzed with checks off Declaration
3218 -- must have a parent for proper handling of subsidiary
3219 -- actions.
3220
3221 Set_Parent (Subt_Decl, N);
3222 Analyze (Subt_Decl, Suppress => All_Checks);
3223 end;
3224 end if;
3225
3226 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3227 Set_Assignment_OK (Ref);
3228
3229 if not Is_Interface (Init_Typ) then
3230 Append_List_To (L,
3231 Build_Initialization_Call (Loc,
3232 Id_Ref => Ref,
3233 Typ => Init_Typ,
3234 In_Init_Proc => Within_Init_Proc,
3235 With_Default_Init => Has_Default_Init_Comps (N)
3236 or else
3237 Has_Task (Base_Type (Init_Typ))));
3238
3239 if Is_Constrained (Entity (Ancestor))
3240 and then Has_Discriminants (Entity (Ancestor))
3241 then
3242 Check_Ancestor_Discriminants (Entity (Ancestor));
3243 end if;
3244 end if;
3245
3246 -- Handle calls to C++ constructors
3247
3248 elsif Is_CPP_Constructor_Call (Ancestor) then
3249 Init_Typ := Etype (Ancestor);
3250 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3251 Set_Assignment_OK (Ref);
3252
3253 Append_List_To (L,
3254 Build_Initialization_Call (Loc,
3255 Id_Ref => Ref,
3256 Typ => Init_Typ,
3257 In_Init_Proc => Within_Init_Proc,
3258 With_Default_Init => Has_Default_Init_Comps (N),
3259 Constructor_Ref => Ancestor));
3260
3261 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3262 -- limited type, a recursive call expands the ancestor. Note that
3263 -- in the limited case, the ancestor part must be either a
3264 -- function call (possibly qualified) or aggregate (definitely
3265 -- qualified).
3266
3267 elsif Is_Limited_Type (Etype (Ancestor))
3268 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
3269 N_Extension_Aggregate)
3270 then
3271 Ancestor_Is_Expression := True;
3272
3273 -- Set up finalization data for enclosing record, because
3274 -- controlled subcomponents of the ancestor part will be
3275 -- attached to it.
3276
3277 Generate_Finalization_Actions;
3278
3279 Append_List_To (L,
3280 Build_Record_Aggr_Code
3281 (N => Unqualify (Ancestor),
3282 Typ => Etype (Unqualify (Ancestor)),
3283 Lhs => Target));
3284
3285 -- If the ancestor part is an expression "E", we generate
3286
3287 -- T (tmp) := E;
3288
3289 -- In Ada 2005, this includes the case of a (possibly qualified)
3290 -- limited function call. The assignment will turn into a
3291 -- build-in-place function call (for further details, see
3292 -- Make_Build_In_Place_Call_In_Assignment).
3293
3294 else
3295 Ancestor_Is_Expression := True;
3296 Init_Typ := Etype (Ancestor);
3297
3298 -- If the ancestor part is an aggregate, force its full
3299 -- expansion, which was delayed.
3300
3301 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
3302 N_Extension_Aggregate)
3303 then
3304 Set_Analyzed (Ancestor, False);
3305 Set_Analyzed (Expression (Ancestor), False);
3306 end if;
3307
3308 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3309 Set_Assignment_OK (Ref);
3310
3311 -- Make the assignment without usual controlled actions, since
3312 -- we only want to Adjust afterwards, but not to Finalize
3313 -- beforehand. Add manual Adjust when necessary.
3314
3315 Assign := New_List (
3316 Make_OK_Assignment_Statement (Loc,
3317 Name => Ref,
3318 Expression => Ancestor));
3319 Set_No_Ctrl_Actions (First (Assign));
3320
3321 -- Assign the tag now to make sure that the dispatching call in
3322 -- the subsequent deep_adjust works properly (unless
3323 -- Tagged_Type_Expansion where tags are implicit).
3324
3325 if Tagged_Type_Expansion then
3326 Instr :=
3327 Make_OK_Assignment_Statement (Loc,
3328 Name =>
3329 Make_Selected_Component (Loc,
3330 Prefix => New_Copy_Tree (Target),
3331 Selector_Name =>
3332 New_Occurrence_Of
3333 (First_Tag_Component (Base_Type (Typ)), Loc)),
3334
3335 Expression =>
3336 Unchecked_Convert_To (RTE (RE_Tag),
3337 New_Occurrence_Of
3338 (Node (First_Elmt
3339 (Access_Disp_Table (Base_Type (Typ)))),
3340 Loc)));
3341
3342 Set_Assignment_OK (Name (Instr));
3343 Append_To (Assign, Instr);
3344
3345 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3346 -- also initialize tags of the secondary dispatch tables.
3347
3348 if Has_Interfaces (Base_Type (Typ)) then
3349 Init_Secondary_Tags
3350 (Typ => Base_Type (Typ),
3351 Target => Target,
3352 Stmts_List => Assign,
3353 Init_Tags_List => Assign);
3354 end if;
3355 end if;
3356
3357 -- Call Adjust manually
3358
3359 if Needs_Finalization (Etype (Ancestor))
3360 and then not Is_Limited_Type (Etype (Ancestor))
3361 and then not Is_Build_In_Place_Function_Call (Ancestor)
3362 then
3363 Adj_Call :=
3364 Make_Adjust_Call
3365 (Obj_Ref => New_Copy_Tree (Ref),
3366 Typ => Etype (Ancestor));
3367
3368 -- Guard against a missing [Deep_]Adjust when the ancestor
3369 -- type was not properly frozen.
3370
3371 if Present (Adj_Call) then
3372 Append_To (Assign, Adj_Call);
3373 end if;
3374 end if;
3375
3376 Append_To (L,
3377 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
3378
3379 if Has_Discriminants (Init_Typ) then
3380 Check_Ancestor_Discriminants (Init_Typ);
3381 end if;
3382 end if;
3383
3384 pragma Assert (Nkind (N) = N_Extension_Aggregate);
3385 pragma Assert
3386 (not (Ancestor_Is_Expression and Ancestor_Is_Subtype_Mark));
3387 end;
3388
3389 -- Generate assignments of hidden discriminants. If the base type is
3390 -- an unchecked union, the discriminants are unknown to the back-end
3391 -- and absent from a value of the type, so assignments for them are
3392 -- not emitted.
3393
3394 if Has_Discriminants (Typ)
3395 and then not Is_Unchecked_Union (Base_Type (Typ))
3396 then
3397 Init_Hidden_Discriminants (Typ, L);
3398 end if;
3399
3400 -- Normal case (not an extension aggregate)
3401
3402 else
3403 -- Generate the discriminant expressions, component by component.
3404 -- If the base type is an unchecked union, the discriminants are
3405 -- unknown to the back-end and absent from a value of the type, so
3406 -- assignments for them are not emitted.
3407
3408 if Has_Discriminants (Typ)
3409 and then not Is_Unchecked_Union (Base_Type (Typ))
3410 then
3411 Init_Hidden_Discriminants (Typ, L);
3412
3413 -- Generate discriminant init values for the visible discriminants
3414
3415 Init_Visible_Discriminants;
3416
3417 if Is_Derived_Type (N_Typ) then
3418 Init_Stored_Discriminants;
3419 end if;
3420 end if;
3421 end if;
3422
3423 -- For CPP types we generate an implicit call to the C++ default
3424 -- constructor to ensure the proper initialization of the _Tag
3425 -- component.
3426
3427 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3428 Invoke_Constructor : declare
3429 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3430
3431 procedure Invoke_IC_Proc (T : Entity_Id);
3432 -- Recursive routine used to climb to parents. Required because
3433 -- parents must be initialized before descendants to ensure
3434 -- propagation of inherited C++ slots.
3435
3436 --------------------
3437 -- Invoke_IC_Proc --
3438 --------------------
3439
3440 procedure Invoke_IC_Proc (T : Entity_Id) is
3441 begin
3442 -- Avoid generating extra calls. Initialization required
3443 -- only for types defined from the level of derivation of
3444 -- type of the constructor and the type of the aggregate.
3445
3446 if T = CPP_Parent then
3447 return;
3448 end if;
3449
3450 Invoke_IC_Proc (Etype (T));
3451
3452 -- Generate call to the IC routine
3453
3454 if Present (CPP_Init_Proc (T)) then
3455 Append_To (L,
3456 Make_Procedure_Call_Statement (Loc,
3457 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3458 end if;
3459 end Invoke_IC_Proc;
3460
3461 -- Start of processing for Invoke_Constructor
3462
3463 begin
3464 -- Implicit invocation of the C++ constructor
3465
3466 if Nkind (N) = N_Aggregate then
3467 Append_To (L,
3468 Make_Procedure_Call_Statement (Loc,
3469 Name =>
3470 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3471 Parameter_Associations => New_List (
3472 Unchecked_Convert_To (CPP_Parent,
3473 New_Copy_Tree (Lhs)))));
3474 end if;
3475
3476 Invoke_IC_Proc (Typ);
3477 end Invoke_Constructor;
3478 end if;
3479
3480 -- Generate the assignments, component by component
3481
3482 -- tmp.comp1 := Expr1_From_Aggr;
3483 -- tmp.comp2 := Expr2_From_Aggr;
3484 -- ....
3485
3486 Comp := First (Component_Associations (N));
3487 while Present (Comp) loop
3488 Selector := Entity (First (Choices (Comp)));
3489
3490 -- C++ constructors
3491
3492 if Is_CPP_Constructor_Call (Expression (Comp)) then
3493 Append_List_To (L,
3494 Build_Initialization_Call (Loc,
3495 Id_Ref =>
3496 Make_Selected_Component (Loc,
3497 Prefix => New_Copy_Tree (Target),
3498 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3499 Typ => Etype (Selector),
3500 Enclos_Type => Typ,
3501 With_Default_Init => True,
3502 Constructor_Ref => Expression (Comp)));
3503
3504 -- Ada 2005 (AI-287): For each default-initialized component generate
3505 -- a call to the corresponding IP subprogram if available.
3506
3507 elsif Box_Present (Comp)
3508 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3509 then
3510 if Ekind (Selector) /= E_Discriminant then
3511 Generate_Finalization_Actions;
3512 end if;
3513
3514 -- Ada 2005 (AI-287): If the component type has tasks then
3515 -- generate the activation chain and master entities (except
3516 -- in case of an allocator because in that case these entities
3517 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3518
3519 declare
3520 Ctype : constant Entity_Id := Etype (Selector);
3521 Inside_Allocator : Boolean := False;
3522 P : Node_Id := Parent (N);
3523
3524 begin
3525 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3526 while Present (P) loop
3527 if Nkind (P) = N_Allocator then
3528 Inside_Allocator := True;
3529 exit;
3530 end if;
3531
3532 P := Parent (P);
3533 end loop;
3534
3535 if not Inside_Init_Proc and not Inside_Allocator then
3536 Build_Activation_Chain_Entity (N);
3537 end if;
3538 end if;
3539 end;
3540
3541 Append_List_To (L,
3542 Build_Initialization_Call (Loc,
3543 Id_Ref => Make_Selected_Component (Loc,
3544 Prefix => New_Copy_Tree (Target),
3545 Selector_Name =>
3546 New_Occurrence_Of (Selector, Loc)),
3547 Typ => Etype (Selector),
3548 Enclos_Type => Typ,
3549 With_Default_Init => True));
3550
3551 -- Prepare for component assignment
3552
3553 elsif Ekind (Selector) /= E_Discriminant
3554 or else Nkind (N) = N_Extension_Aggregate
3555 then
3556 -- All the discriminants have now been assigned
3557
3558 -- This is now a good moment to initialize and attach all the
3559 -- controllers. Their position may depend on the discriminants.
3560
3561 if Ekind (Selector) /= E_Discriminant then
3562 Generate_Finalization_Actions;
3563 end if;
3564
3565 Comp_Type := Underlying_Type (Etype (Selector));
3566 Comp_Expr :=
3567 Make_Selected_Component (Loc,
3568 Prefix => New_Copy_Tree (Target),
3569 Selector_Name => New_Occurrence_Of (Selector, Loc));
3570
3571 if Nkind (Expression (Comp)) = N_Qualified_Expression then
3572 Expr_Q := Expression (Expression (Comp));
3573 else
3574 Expr_Q := Expression (Comp);
3575 end if;
3576
3577 -- Now either create the assignment or generate the code for the
3578 -- inner aggregate top-down.
3579
3580 if Is_Delayed_Aggregate (Expr_Q) then
3581
3582 -- We have the following case of aggregate nesting inside
3583 -- an object declaration:
3584
3585 -- type Arr_Typ is array (Integer range <>) of ...;
3586
3587 -- type Rec_Typ (...) is record
3588 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3589 -- end record;
3590
3591 -- Obj_Rec_Typ : Rec_Typ := (...,
3592 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3593
3594 -- The length of the ranges of the aggregate and Obj_Add_Typ
3595 -- are equal (B - A = Y - X), but they do not coincide (X /=
3596 -- A and B /= Y). This case requires array sliding which is
3597 -- performed in the following manner:
3598
3599 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3600 -- Temp : Arr_Sub;
3601 -- Temp (X) := (...);
3602 -- ...
3603 -- Temp (Y) := (...);
3604 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3605
3606 if Ekind (Comp_Type) = E_Array_Subtype
3607 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3608 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3609 and then not
3610 Compatible_Int_Bounds
3611 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3612 Typ_Bounds => First_Index (Comp_Type))
3613 then
3614 -- Create the array subtype with bounds equal to those of
3615 -- the corresponding aggregate.
3616
3617 declare
3618 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3619
3620 SubD : constant Node_Id :=
3621 Make_Subtype_Declaration (Loc,
3622 Defining_Identifier => SubE,
3623 Subtype_Indication =>
3624 Make_Subtype_Indication (Loc,
3625 Subtype_Mark =>
3626 New_Occurrence_Of (Etype (Comp_Type), Loc),
3627 Constraint =>
3628 Make_Index_Or_Discriminant_Constraint
3629 (Loc,
3630 Constraints => New_List (
3631 New_Copy_Tree
3632 (Aggregate_Bounds (Expr_Q))))));
3633
3634 -- Create a temporary array of the above subtype which
3635 -- will be used to capture the aggregate assignments.
3636
3637 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3638
3639 TmpD : constant Node_Id :=
3640 Make_Object_Declaration (Loc,
3641 Defining_Identifier => TmpE,
3642 Object_Definition => New_Occurrence_Of (SubE, Loc));
3643
3644 begin
3645 Set_No_Initialization (TmpD);
3646 Append_To (L, SubD);
3647 Append_To (L, TmpD);
3648
3649 -- Expand aggregate into assignments to the temp array
3650
3651 Append_List_To (L,
3652 Late_Expansion (Expr_Q, Comp_Type,
3653 New_Occurrence_Of (TmpE, Loc)));
3654
3655 -- Slide
3656
3657 Append_To (L,
3658 Make_Assignment_Statement (Loc,
3659 Name => New_Copy_Tree (Comp_Expr),
3660 Expression => New_Occurrence_Of (TmpE, Loc)));
3661 end;
3662
3663 -- Normal case (sliding not required)
3664
3665 else
3666 Append_List_To (L,
3667 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3668 end if;
3669
3670 -- Expr_Q is not delayed aggregate
3671
3672 else
3673 if Has_Discriminants (Typ) then
3674 Replace_Discriminants (Expr_Q);
3675
3676 -- If the component is an array type that depends on
3677 -- discriminants, and the expression is a single Others
3678 -- clause, create an explicit subtype for it because the
3679 -- backend has troubles recovering the actual bounds.
3680
3681 if Nkind (Expr_Q) = N_Aggregate
3682 and then Is_Array_Type (Comp_Type)
3683 and then Present (Component_Associations (Expr_Q))
3684 then
3685 declare
3686 Assoc : constant Node_Id :=
3687 First (Component_Associations (Expr_Q));
3688 Decl : Node_Id;
3689
3690 begin
3691 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3692 then
3693 Decl :=
3694 Build_Actual_Subtype_Of_Component
3695 (Comp_Type, Comp_Expr);
3696
3697 -- If the component type does not in fact depend on
3698 -- discriminants, the subtype declaration is empty.
3699
3700 if Present (Decl) then
3701 Append_To (L, Decl);
3702 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3703 end if;
3704 end if;
3705 end;
3706 end if;
3707 end if;
3708
3709 if Modify_Tree_For_C
3710 and then Nkind (Expr_Q) = N_Aggregate
3711 and then Is_Array_Type (Etype (Expr_Q))
3712 and then Present (First_Index (Etype (Expr_Q)))
3713 then
3714 declare
3715 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3716 begin
3717 Append_List_To (L,
3718 Build_Array_Aggr_Code
3719 (N => Expr_Q,
3720 Ctype => Component_Type (Expr_Q_Type),
3721 Index => First_Index (Expr_Q_Type),
3722 Into => Comp_Expr,
3723 Scalar_Comp =>
3724 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
3725 end;
3726
3727 else
3728 -- Handle an initialization expression of a controlled type
3729 -- in case it denotes a function call. In general such a
3730 -- scenario will produce a transient scope, but this will
3731 -- lead to wrong order of initialization, adjustment, and
3732 -- finalization in the context of aggregates.
3733
3734 -- Target.Comp := Ctrl_Func_Call;
3735
3736 -- begin -- scope
3737 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3738 -- Target.Comp := Trans_Obj;
3739 -- Finalize (Trans_Obj);
3740 -- end
3741 -- Target.Comp._tag := ...;
3742 -- Adjust (Target.Comp);
3743
3744 -- In the example above, the call to Finalize occurs too
3745 -- early and as a result it may leave the record component
3746 -- in a bad state. Finalization of the transient object
3747 -- should really happen after adjustment.
3748
3749 -- To avoid this scenario, perform in-place side-effect
3750 -- removal of the function call. This eliminates the
3751 -- transient property of the function result and ensures
3752 -- correct order of actions.
3753
3754 -- Res : ... := Ctrl_Func_Call;
3755 -- Target.Comp := Res;
3756 -- Target.Comp._tag := ...;
3757 -- Adjust (Target.Comp);
3758 -- Finalize (Res);
3759
3760 if Needs_Finalization (Comp_Type)
3761 and then Nkind (Expr_Q) /= N_Aggregate
3762 then
3763 Initialize_Ctrl_Record_Component
3764 (Rec_Comp => Comp_Expr,
3765 Comp_Typ => Etype (Selector),
3766 Init_Expr => Expr_Q,
3767 Stmts => L);
3768
3769 -- Otherwise perform single component initialization
3770
3771 else
3772 Initialize_Record_Component
3773 (Rec_Comp => Comp_Expr,
3774 Comp_Typ => Etype (Selector),
3775 Init_Expr => Expr_Q,
3776 Stmts => L);
3777 end if;
3778 end if;
3779 end if;
3780
3781 -- comment would be good here ???
3782
3783 elsif Ekind (Selector) = E_Discriminant
3784 and then Nkind (N) /= N_Extension_Aggregate
3785 and then Nkind (Parent (N)) = N_Component_Association
3786 and then Is_Constrained (Typ)
3787 then
3788 -- We must check that the discriminant value imposed by the
3789 -- context is the same as the value given in the subaggregate,
3790 -- because after the expansion into assignments there is no
3791 -- record on which to perform a regular discriminant check.
3792
3793 declare
3794 D_Val : Elmt_Id;
3795 Disc : Entity_Id;
3796
3797 begin
3798 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3799 Disc := First_Discriminant (Typ);
3800 while Chars (Disc) /= Chars (Selector) loop
3801 Next_Discriminant (Disc);
3802 Next_Elmt (D_Val);
3803 end loop;
3804
3805 pragma Assert (Present (D_Val));
3806
3807 -- This check cannot performed for components that are
3808 -- constrained by a current instance, because this is not a
3809 -- value that can be compared with the actual constraint.
3810
3811 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3812 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3813 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3814 then
3815 Append_To (L,
3816 Make_Raise_Constraint_Error (Loc,
3817 Condition =>
3818 Make_Op_Ne (Loc,
3819 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3820 Right_Opnd => Expression (Comp)),
3821 Reason => CE_Discriminant_Check_Failed));
3822
3823 else
3824 -- Find self-reference in previous discriminant assignment,
3825 -- and replace with proper expression.
3826
3827 declare
3828 Ass : Node_Id;
3829
3830 begin
3831 Ass := First (L);
3832 while Present (Ass) loop
3833 if Nkind (Ass) = N_Assignment_Statement
3834 and then Nkind (Name (Ass)) = N_Selected_Component
3835 and then Chars (Selector_Name (Name (Ass))) =
3836 Chars (Disc)
3837 then
3838 Set_Expression
3839 (Ass, New_Copy_Tree (Expression (Comp)));
3840 exit;
3841 end if;
3842 Next (Ass);
3843 end loop;
3844 end;
3845 end if;
3846 end;
3847 end if;
3848
3849 Next (Comp);
3850 end loop;
3851
3852 -- If the type is tagged, the tag needs to be initialized (unless we
3853 -- are in VM-mode where tags are implicit). It is done late in the
3854 -- initialization process because in some cases, we call the init
3855 -- proc of an ancestor which will not leave out the right tag.
3856
3857 if Ancestor_Is_Expression then
3858 null;
3859
3860 -- For CPP types we generated a call to the C++ default constructor
3861 -- before the components have been initialized to ensure the proper
3862 -- initialization of the _Tag component (see above).
3863
3864 elsif Is_CPP_Class (Typ) then
3865 null;
3866
3867 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3868 Instr :=
3869 Make_OK_Assignment_Statement (Loc,
3870 Name =>
3871 Make_Selected_Component (Loc,
3872 Prefix => New_Copy_Tree (Target),
3873 Selector_Name =>
3874 New_Occurrence_Of
3875 (First_Tag_Component (Base_Type (Typ)), Loc)),
3876
3877 Expression =>
3878 Unchecked_Convert_To (RTE (RE_Tag),
3879 New_Occurrence_Of
3880 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3881 Loc)));
3882
3883 Append_To (L, Instr);
3884
3885 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3886 -- abstract interfaces we must also initialize the tags of the
3887 -- secondary dispatch tables.
3888
3889 if Has_Interfaces (Base_Type (Typ)) then
3890 Init_Secondary_Tags
3891 (Typ => Base_Type (Typ),
3892 Target => Target,
3893 Stmts_List => L,
3894 Init_Tags_List => L);
3895 end if;
3896 end if;
3897
3898 -- If the controllers have not been initialized yet (by lack of non-
3899 -- discriminant components), let's do it now.
3900
3901 Generate_Finalization_Actions;
3902
3903 return L;
3904 end Build_Record_Aggr_Code;
3905
3906 ---------------------------------------
3907 -- Collect_Initialization_Statements --
3908 ---------------------------------------
3909
3910 procedure Collect_Initialization_Statements
3911 (Obj : Entity_Id;
3912 N : Node_Id;
3913 Node_After : Node_Id)
3914 is
3915 Loc : constant Source_Ptr := Sloc (N);
3916 Init_Actions : constant List_Id := New_List;
3917 Init_Node : Node_Id;
3918 Comp_Stmt : Node_Id;
3919
3920 begin
3921 -- Nothing to do if Obj is already frozen, as in this case we known we
3922 -- won't need to move the initialization statements about later on.
3923
3924 if Is_Frozen (Obj) then
3925 return;
3926 end if;
3927
3928 Init_Node := N;
3929 while Next (Init_Node) /= Node_After loop
3930 Append_To (Init_Actions, Remove_Next (Init_Node));
3931 end loop;
3932
3933 if not Is_Empty_List (Init_Actions) then
3934 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3935 Insert_Action_After (Init_Node, Comp_Stmt);
3936 Set_Initialization_Statements (Obj, Comp_Stmt);
3937 end if;
3938 end Collect_Initialization_Statements;
3939
3940 -------------------------------
3941 -- Convert_Aggr_In_Allocator --
3942 -------------------------------
3943
3944 procedure Convert_Aggr_In_Allocator
3945 (Alloc : Node_Id;
3946 Decl : Node_Id;
3947 Aggr : Node_Id)
3948 is
3949 Loc : constant Source_Ptr := Sloc (Aggr);
3950 Typ : constant Entity_Id := Etype (Aggr);
3951 Temp : constant Entity_Id := Defining_Identifier (Decl);
3952
3953 Occ : constant Node_Id :=
3954 Unchecked_Convert_To (Typ,
3955 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3956
3957 begin
3958 if Is_Array_Type (Typ) then
3959 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3960
3961 elsif Has_Default_Init_Comps (Aggr) then
3962 declare
3963 L : constant List_Id := New_List;
3964 Init_Stmts : List_Id;
3965
3966 begin
3967 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3968
3969 if Has_Task (Typ) then
3970 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3971 Insert_Actions (Alloc, L);
3972 else
3973 Insert_Actions (Alloc, Init_Stmts);
3974 end if;
3975 end;
3976
3977 else
3978 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3979 end if;
3980 end Convert_Aggr_In_Allocator;
3981
3982 --------------------------------
3983 -- Convert_Aggr_In_Assignment --
3984 --------------------------------
3985
3986 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3987 Aggr : Node_Id := Expression (N);
3988 Typ : constant Entity_Id := Etype (Aggr);
3989 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3990
3991 begin
3992 if Nkind (Aggr) = N_Qualified_Expression then
3993 Aggr := Expression (Aggr);
3994 end if;
3995
3996 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3997 end Convert_Aggr_In_Assignment;
3998
3999 ---------------------------------
4000 -- Convert_Aggr_In_Object_Decl --
4001 ---------------------------------
4002
4003 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
4004 Obj : constant Entity_Id := Defining_Identifier (N);
4005 Aggr : Node_Id := Expression (N);
4006 Loc : constant Source_Ptr := Sloc (Aggr);
4007 Typ : constant Entity_Id := Etype (Aggr);
4008 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
4009
4010 function Discriminants_Ok return Boolean;
4011 -- If the object type is constrained, the discriminants in the
4012 -- aggregate must be checked against the discriminants of the subtype.
4013 -- This cannot be done using Apply_Discriminant_Checks because after
4014 -- expansion there is no aggregate left to check.
4015
4016 ----------------------
4017 -- Discriminants_Ok --
4018 ----------------------
4019
4020 function Discriminants_Ok return Boolean is
4021 Cond : Node_Id := Empty;
4022 Check : Node_Id;
4023 D : Entity_Id;
4024 Disc1 : Elmt_Id;
4025 Disc2 : Elmt_Id;
4026 Val1 : Node_Id;
4027 Val2 : Node_Id;
4028
4029 begin
4030 D := First_Discriminant (Typ);
4031 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
4032 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
4033 while Present (Disc1) and then Present (Disc2) loop
4034 Val1 := Node (Disc1);
4035 Val2 := Node (Disc2);
4036
4037 if not Is_OK_Static_Expression (Val1)
4038 or else not Is_OK_Static_Expression (Val2)
4039 then
4040 Check := Make_Op_Ne (Loc,
4041 Left_Opnd => Duplicate_Subexpr (Val1),
4042 Right_Opnd => Duplicate_Subexpr (Val2));
4043
4044 if No (Cond) then
4045 Cond := Check;
4046
4047 else
4048 Cond := Make_Or_Else (Loc,
4049 Left_Opnd => Cond,
4050 Right_Opnd => Check);
4051 end if;
4052
4053 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
4054 Apply_Compile_Time_Constraint_Error (Aggr,
4055 Msg => "incorrect value for discriminant&??",
4056 Reason => CE_Discriminant_Check_Failed,
4057 Ent => D);
4058 return False;
4059 end if;
4060
4061 Next_Discriminant (D);
4062 Next_Elmt (Disc1);
4063 Next_Elmt (Disc2);
4064 end loop;
4065
4066 -- If any discriminant constraint is nonstatic, emit a check
4067
4068 if Present (Cond) then
4069 Insert_Action (N,
4070 Make_Raise_Constraint_Error (Loc,
4071 Condition => Cond,
4072 Reason => CE_Discriminant_Check_Failed));
4073 end if;
4074
4075 return True;
4076 end Discriminants_Ok;
4077
4078 -- Start of processing for Convert_Aggr_In_Object_Decl
4079
4080 begin
4081 Set_Assignment_OK (Occ);
4082
4083 if Nkind (Aggr) = N_Qualified_Expression then
4084 Aggr := Expression (Aggr);
4085 end if;
4086
4087 if Has_Discriminants (Typ)
4088 and then Typ /= Etype (Obj)
4089 and then Is_Constrained (Etype (Obj))
4090 and then not Discriminants_Ok
4091 then
4092 return;
4093 end if;
4094
4095 -- If the context is an extended return statement, it has its own
4096 -- finalization machinery (i.e. works like a transient scope) and
4097 -- we do not want to create an additional one, because objects on
4098 -- the finalization list of the return must be moved to the caller's
4099 -- finalization list to complete the return.
4100
4101 -- However, if the aggregate is limited, it is built in place, and the
4102 -- controlled components are not assigned to intermediate temporaries
4103 -- so there is no need for a transient scope in this case either.
4104
4105 if Requires_Transient_Scope (Typ)
4106 and then Ekind (Current_Scope) /= E_Return_Statement
4107 and then not Is_Limited_Type (Typ)
4108 then
4109 Establish_Transient_Scope (Aggr, Manage_Sec_Stack => False);
4110 end if;
4111
4112 declare
4113 Node_After : constant Node_Id := Next (N);
4114 begin
4115 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
4116 Collect_Initialization_Statements (Obj, N, Node_After);
4117 end;
4118
4119 Set_No_Initialization (N);
4120 Initialize_Discriminants (N, Typ);
4121 end Convert_Aggr_In_Object_Decl;
4122
4123 -------------------------------------
4124 -- Convert_Array_Aggr_In_Allocator --
4125 -------------------------------------
4126
4127 procedure Convert_Array_Aggr_In_Allocator
4128 (Decl : Node_Id;
4129 Aggr : Node_Id;
4130 Target : Node_Id)
4131 is
4132 Aggr_Code : List_Id;
4133 Typ : constant Entity_Id := Etype (Aggr);
4134 Ctyp : constant Entity_Id := Component_Type (Typ);
4135
4136 begin
4137 -- The target is an explicit dereference of the allocated object.
4138 -- Generate component assignments to it, as for an aggregate that
4139 -- appears on the right-hand side of an assignment statement.
4140
4141 Aggr_Code :=
4142 Build_Array_Aggr_Code (Aggr,
4143 Ctype => Ctyp,
4144 Index => First_Index (Typ),
4145 Into => Target,
4146 Scalar_Comp => Is_Scalar_Type (Ctyp));
4147
4148 Insert_Actions_After (Decl, Aggr_Code);
4149 end Convert_Array_Aggr_In_Allocator;
4150
4151 ------------------------
4152 -- In_Place_Assign_OK --
4153 ------------------------
4154
4155 function In_Place_Assign_OK (N : Node_Id) return Boolean is
4156 Is_Array : constant Boolean := Is_Array_Type (Etype (N));
4157
4158 Aggr_In : Node_Id;
4159 Aggr_Lo : Node_Id;
4160 Aggr_Hi : Node_Id;
4161 Obj_In : Node_Id;
4162 Obj_Lo : Node_Id;
4163 Obj_Hi : Node_Id;
4164
4165 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4166 -- Check recursively that each component of a (sub)aggregate does not
4167 -- depend on the variable being assigned to.
4168
4169 function Safe_Component (Expr : Node_Id) return Boolean;
4170 -- Verify that an expression cannot depend on the variable being
4171 -- assigned to. Room for improvement here (but less than before).
4172
4173 --------------------
4174 -- Safe_Aggregate --
4175 --------------------
4176
4177 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4178 Expr : Node_Id;
4179
4180 begin
4181 if Nkind (Parent (Aggr)) = N_Iterated_Component_Association then
4182 return False;
4183 end if;
4184
4185 if Present (Expressions (Aggr)) then
4186 Expr := First (Expressions (Aggr));
4187 while Present (Expr) loop
4188 if Nkind (Expr) = N_Aggregate then
4189 if not Safe_Aggregate (Expr) then
4190 return False;
4191 end if;
4192
4193 elsif not Safe_Component (Expr) then
4194 return False;
4195 end if;
4196
4197 Next (Expr);
4198 end loop;
4199 end if;
4200
4201 if Present (Component_Associations (Aggr)) then
4202 Expr := First (Component_Associations (Aggr));
4203 while Present (Expr) loop
4204 if Nkind (Expression (Expr)) = N_Aggregate then
4205 if not Safe_Aggregate (Expression (Expr)) then
4206 return False;
4207 end if;
4208
4209 -- If association has a box, no way to determine yet whether
4210 -- default can be assigned in place.
4211
4212 elsif Box_Present (Expr) then
4213 return False;
4214
4215 elsif not Safe_Component (Expression (Expr)) then
4216 return False;
4217 end if;
4218
4219 Next (Expr);
4220 end loop;
4221 end if;
4222
4223 return True;
4224 end Safe_Aggregate;
4225
4226 --------------------
4227 -- Safe_Component --
4228 --------------------
4229
4230 function Safe_Component (Expr : Node_Id) return Boolean is
4231 Comp : Node_Id := Expr;
4232
4233 function Check_Component (Comp : Node_Id) return Boolean;
4234 -- Do the recursive traversal, after copy
4235
4236 ---------------------
4237 -- Check_Component --
4238 ---------------------
4239
4240 function Check_Component (Comp : Node_Id) return Boolean is
4241 begin
4242 if Is_Overloaded (Comp) then
4243 return False;
4244 end if;
4245
4246 return Compile_Time_Known_Value (Comp)
4247
4248 or else (Is_Entity_Name (Comp)
4249 and then Present (Entity (Comp))
4250 and then Ekind (Entity (Comp)) not in Type_Kind
4251 and then No (Renamed_Object (Entity (Comp))))
4252
4253 or else (Nkind (Comp) = N_Attribute_Reference
4254 and then Check_Component (Prefix (Comp)))
4255
4256 or else (Nkind (Comp) in N_Binary_Op
4257 and then Check_Component (Left_Opnd (Comp))
4258 and then Check_Component (Right_Opnd (Comp)))
4259
4260 or else (Nkind (Comp) in N_Unary_Op
4261 and then Check_Component (Right_Opnd (Comp)))
4262
4263 or else (Nkind (Comp) = N_Selected_Component
4264 and then Is_Array
4265 and then Check_Component (Prefix (Comp)))
4266
4267 or else (Nkind_In (Comp, N_Type_Conversion,
4268 N_Unchecked_Type_Conversion)
4269 and then Check_Component (Expression (Comp)));
4270 end Check_Component;
4271
4272 -- Start of processing for Safe_Component
4273
4274 begin
4275 -- If the component appears in an association that may correspond
4276 -- to more than one element, it is not analyzed before expansion
4277 -- into assignments, to avoid side effects. We analyze, but do not
4278 -- resolve the copy, to obtain sufficient entity information for
4279 -- the checks that follow. If component is overloaded we assume
4280 -- an unsafe function call.
4281
4282 if not Analyzed (Comp) then
4283 if Is_Overloaded (Expr) then
4284 return False;
4285
4286 elsif Nkind (Expr) = N_Aggregate
4287 and then not Is_Others_Aggregate (Expr)
4288 then
4289 return False;
4290
4291 elsif Nkind (Expr) = N_Allocator then
4292
4293 -- For now, too complex to analyze
4294
4295 return False;
4296
4297 elsif Nkind (Parent (Expr)) = N_Iterated_Component_Association then
4298
4299 -- Ditto for iterated component associations, which in general
4300 -- require an enclosing loop and involve nonstatic expressions.
4301
4302 return False;
4303 end if;
4304
4305 Comp := New_Copy_Tree (Expr);
4306 Set_Parent (Comp, Parent (Expr));
4307 Analyze (Comp);
4308 end if;
4309
4310 if Nkind (Comp) = N_Aggregate then
4311 return Safe_Aggregate (Comp);
4312 else
4313 return Check_Component (Comp);
4314 end if;
4315 end Safe_Component;
4316
4317 -- Start of processing for In_Place_Assign_OK
4318
4319 begin
4320 -- By-copy semantic cannot be guaranteed for controlled objects or
4321 -- objects with discriminants.
4322
4323 if Needs_Finalization (Etype (N))
4324 or else Has_Discriminants (Etype (N))
4325 then
4326 return False;
4327
4328 elsif Is_Array and then Present (Component_Associations (N)) then
4329
4330 -- On assignment, sliding can take place, so we cannot do the
4331 -- assignment in place unless the bounds of the aggregate are
4332 -- statically equal to those of the target.
4333
4334 -- If the aggregate is given by an others choice, the bounds are
4335 -- derived from the left-hand side, and the assignment is safe if
4336 -- the expression is.
4337
4338 if Is_Others_Aggregate (N) then
4339 return
4340 Safe_Component
4341 (Expression (First (Component_Associations (N))));
4342 end if;
4343
4344 Aggr_In := First_Index (Etype (N));
4345
4346 if Nkind (Parent (N)) = N_Assignment_Statement then
4347 Obj_In := First_Index (Etype (Name (Parent (N))));
4348
4349 else
4350 -- Context is an allocator. Check bounds of aggregate against
4351 -- given type in qualified expression.
4352
4353 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4354 Obj_In := First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4355 end if;
4356
4357 while Present (Aggr_In) loop
4358 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4359 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4360
4361 if not Compile_Time_Known_Value (Aggr_Lo)
4362 or else not Compile_Time_Known_Value (Obj_Lo)
4363 or else not Compile_Time_Known_Value (Obj_Hi)
4364 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4365 then
4366 return False;
4367
4368 -- For an assignment statement we require static matching of
4369 -- bounds. Ditto for an allocator whose qualified expression
4370 -- is a constrained type. If the expression in the allocator
4371 -- is an unconstrained array, we accept an upper bound that
4372 -- is not static, to allow for nonstatic expressions of the
4373 -- base type. Clearly there are further possibilities (with
4374 -- diminishing returns) for safely building arrays in place
4375 -- here.
4376
4377 elsif Nkind (Parent (N)) = N_Assignment_Statement
4378 or else Is_Constrained (Etype (Parent (N)))
4379 then
4380 if not Compile_Time_Known_Value (Aggr_Hi)
4381 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4382 then
4383 return False;
4384 end if;
4385 end if;
4386
4387 Next_Index (Aggr_In);
4388 Next_Index (Obj_In);
4389 end loop;
4390 end if;
4391
4392 -- Now check the component values themselves
4393
4394 return Safe_Aggregate (N);
4395 end In_Place_Assign_OK;
4396
4397 ----------------------------
4398 -- Convert_To_Assignments --
4399 ----------------------------
4400
4401 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4402 Loc : constant Source_Ptr := Sloc (N);
4403 T : Entity_Id;
4404 Temp : Entity_Id;
4405
4406 Aggr_Code : List_Id;
4407 Instr : Node_Id;
4408 Target_Expr : Node_Id;
4409 Parent_Kind : Node_Kind;
4410 Unc_Decl : Boolean := False;
4411 Parent_Node : Node_Id;
4412
4413 begin
4414 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
4415 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4416 pragma Assert (Is_Record_Type (Typ));
4417
4418 Parent_Node := Parent (N);
4419 Parent_Kind := Nkind (Parent_Node);
4420
4421 if Parent_Kind = N_Qualified_Expression then
4422 -- Check if we are in an unconstrained declaration because in this
4423 -- case the current delayed expansion mechanism doesn't work when
4424 -- the declared object size depends on the initializing expr.
4425
4426 Parent_Node := Parent (Parent_Node);
4427 Parent_Kind := Nkind (Parent_Node);
4428
4429 if Parent_Kind = N_Object_Declaration then
4430 Unc_Decl :=
4431 not Is_Entity_Name (Object_Definition (Parent_Node))
4432 or else (Nkind (N) = N_Aggregate
4433 and then
4434 Has_Discriminants
4435 (Entity (Object_Definition (Parent_Node))))
4436 or else Is_Class_Wide_Type
4437 (Entity (Object_Definition (Parent_Node)));
4438 end if;
4439 end if;
4440
4441 -- Just set the Delay flag in the cases where the transformation will be
4442 -- done top down from above.
4443
4444 if False
4445
4446 -- Internal aggregate (transformed when expanding the parent)
4447
4448 or else Parent_Kind = N_Aggregate
4449 or else Parent_Kind = N_Extension_Aggregate
4450 or else Parent_Kind = N_Component_Association
4451
4452 -- Allocator (see Convert_Aggr_In_Allocator)
4453
4454 or else Parent_Kind = N_Allocator
4455
4456 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4457
4458 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4459
4460 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4461 -- assignments in init procs are taken into account.
4462
4463 or else (Parent_Kind = N_Assignment_Statement
4464 and then Inside_Init_Proc)
4465
4466 -- (Ada 2005) An inherently limited type in a return statement, which
4467 -- will be handled in a build-in-place fashion, and may be rewritten
4468 -- as an extended return and have its own finalization machinery.
4469 -- In the case of a simple return, the aggregate needs to be delayed
4470 -- until the scope for the return statement has been created, so
4471 -- that any finalization chain will be associated with that scope.
4472 -- For extended returns, we delay expansion to avoid the creation
4473 -- of an unwanted transient scope that could result in premature
4474 -- finalization of the return object (which is built in place
4475 -- within the caller's scope).
4476
4477 or else Is_Build_In_Place_Aggregate_Return (N)
4478 then
4479 Set_Expansion_Delayed (N);
4480 return;
4481 end if;
4482
4483 -- Otherwise, if a transient scope is required, create it now. If we
4484 -- are within an initialization procedure do not create such, because
4485 -- the target of the assignment must not be declared within a local
4486 -- block, and because cleanup will take place on return from the
4487 -- initialization procedure.
4488
4489 -- Should the condition be more restrictive ???
4490
4491 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4492 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
4493 end if;
4494
4495 -- If the aggregate is nonlimited, create a temporary, since aggregates
4496 -- have "by copy" semantics. If it is limited and context is an
4497 -- assignment, this is a subaggregate for an enclosing aggregate being
4498 -- expanded. It must be built in place, so use target of the current
4499 -- assignment.
4500
4501 if Is_Limited_Type (Typ)
4502 and then Nkind (Parent (N)) = N_Assignment_Statement
4503 then
4504 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4505 Insert_Actions (Parent (N),
4506 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4507 Rewrite (Parent (N), Make_Null_Statement (Loc));
4508
4509 -- Do not declare a temporary to initialize an aggregate assigned to an
4510 -- identifier when in-place assignment is possible, preserving the
4511 -- by-copy semantic of aggregates. This avoids large stack usage and
4512 -- generates more efficient code.
4513
4514 elsif Nkind (Parent (N)) = N_Assignment_Statement
4515 and then Nkind (Name (Parent (N))) = N_Identifier
4516 and then In_Place_Assign_OK (N)
4517 then
4518 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4519 Insert_Actions (Parent (N),
4520 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4521 Rewrite (Parent (N), Make_Null_Statement (Loc));
4522
4523 else
4524 Temp := Make_Temporary (Loc, 'A', N);
4525
4526 -- If the type inherits unknown discriminants, use the view with
4527 -- known discriminants if available.
4528
4529 if Has_Unknown_Discriminants (Typ)
4530 and then Present (Underlying_Record_View (Typ))
4531 then
4532 T := Underlying_Record_View (Typ);
4533 else
4534 T := Typ;
4535 end if;
4536
4537 Instr :=
4538 Make_Object_Declaration (Loc,
4539 Defining_Identifier => Temp,
4540 Object_Definition => New_Occurrence_Of (T, Loc));
4541
4542 Set_No_Initialization (Instr);
4543 Insert_Action (N, Instr);
4544 Initialize_Discriminants (Instr, T);
4545
4546 Target_Expr := New_Occurrence_Of (Temp, Loc);
4547 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
4548
4549 -- Save the last assignment statement associated with the aggregate
4550 -- when building a controlled object. This reference is utilized by
4551 -- the finalization machinery when marking an object as successfully
4552 -- initialized.
4553
4554 if Needs_Finalization (T) then
4555 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
4556 end if;
4557
4558 Insert_Actions (N, Aggr_Code);
4559 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4560 Analyze_And_Resolve (N, T);
4561 end if;
4562 end Convert_To_Assignments;
4563
4564 ---------------------------
4565 -- Convert_To_Positional --
4566 ---------------------------
4567
4568 procedure Convert_To_Positional
4569 (N : Node_Id;
4570 Max_Others_Replicate : Nat := 32;
4571 Handle_Bit_Packed : Boolean := False)
4572 is
4573 Typ : constant Entity_Id := Etype (N);
4574
4575 Static_Components : Boolean := True;
4576
4577 procedure Check_Static_Components;
4578 -- Check whether all components of the aggregate are compile-time known
4579 -- values, and can be passed as is to the back-end without further
4580 -- expansion.
4581
4582 function Flatten
4583 (N : Node_Id;
4584 Ix : Node_Id;
4585 Ixb : Node_Id) return Boolean;
4586 -- Convert the aggregate into a purely positional form if possible. On
4587 -- entry the bounds of all dimensions are known to be static, and the
4588 -- total number of components is safe enough to expand.
4589
4590 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
4591 -- Return True iff the array N is flat (which is not trivial in the case
4592 -- of multidimensional aggregates).
4593
4594 function Is_Static_Element (N : Node_Id) return Boolean;
4595 -- Return True if N, an element of a component association list, i.e.
4596 -- N_Component_Association or N_Iterated_Component_Association, has a
4597 -- compile-time known value and can be passed as is to the back-end
4598 -- without further expansion.
4599 -- An Iterated_Component_Association is treated as nonstatic in most
4600 -- cases for now, so there are possibilities for optimization.
4601
4602 -----------------------------
4603 -- Check_Static_Components --
4604 -----------------------------
4605
4606 -- Could use some comments in this body ???
4607
4608 procedure Check_Static_Components is
4609 Assoc : Node_Id;
4610 Expr : Node_Id;
4611
4612 begin
4613 Static_Components := True;
4614
4615 if Nkind (N) = N_String_Literal then
4616 null;
4617
4618 elsif Present (Expressions (N)) then
4619 Expr := First (Expressions (N));
4620 while Present (Expr) loop
4621 if Nkind (Expr) /= N_Aggregate
4622 or else not Compile_Time_Known_Aggregate (Expr)
4623 or else Expansion_Delayed (Expr)
4624 then
4625 Static_Components := False;
4626 exit;
4627 end if;
4628
4629 Next (Expr);
4630 end loop;
4631 end if;
4632
4633 if Nkind (N) = N_Aggregate
4634 and then Present (Component_Associations (N))
4635 then
4636 Assoc := First (Component_Associations (N));
4637 while Present (Assoc) loop
4638 if not Is_Static_Element (Assoc) then
4639 Static_Components := False;
4640 exit;
4641 end if;
4642
4643 Next (Assoc);
4644 end loop;
4645 end if;
4646 end Check_Static_Components;
4647
4648 -------------
4649 -- Flatten --
4650 -------------
4651
4652 function Flatten
4653 (N : Node_Id;
4654 Ix : Node_Id;
4655 Ixb : Node_Id) return Boolean
4656 is
4657 Loc : constant Source_Ptr := Sloc (N);
4658 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
4659 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
4660 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
4661 Lov : Uint;
4662 Hiv : Uint;
4663
4664 Others_Present : Boolean := False;
4665
4666 begin
4667 if Nkind (Original_Node (N)) = N_String_Literal then
4668 return True;
4669 end if;
4670
4671 if not Compile_Time_Known_Value (Lo)
4672 or else not Compile_Time_Known_Value (Hi)
4673 then
4674 return False;
4675 end if;
4676
4677 Lov := Expr_Value (Lo);
4678 Hiv := Expr_Value (Hi);
4679
4680 -- Check if there is an others choice
4681
4682 if Present (Component_Associations (N)) then
4683 declare
4684 Assoc : Node_Id;
4685 Choice : Node_Id;
4686
4687 begin
4688 Assoc := First (Component_Associations (N));
4689 while Present (Assoc) loop
4690
4691 -- If this is a box association, flattening is in general
4692 -- not possible because at this point we cannot tell if the
4693 -- default is static or even exists.
4694
4695 if Box_Present (Assoc) then
4696 return False;
4697
4698 elsif Nkind (Assoc) = N_Iterated_Component_Association then
4699 return False;
4700 end if;
4701
4702 Choice := First (Choice_List (Assoc));
4703
4704 while Present (Choice) loop
4705 if Nkind (Choice) = N_Others_Choice then
4706 Others_Present := True;
4707 end if;
4708
4709 Next (Choice);
4710 end loop;
4711
4712 Next (Assoc);
4713 end loop;
4714 end;
4715 end if;
4716
4717 -- If the low bound is not known at compile time and others is not
4718 -- present we can proceed since the bounds can be obtained from the
4719 -- aggregate.
4720
4721 if Hiv < Lov
4722 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
4723 then
4724 return False;
4725 end if;
4726
4727 -- Determine if set of alternatives is suitable for conversion and
4728 -- build an array containing the values in sequence.
4729
4730 declare
4731 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
4732 of Node_Id := (others => Empty);
4733 -- The values in the aggregate sorted appropriately
4734
4735 Vlist : List_Id;
4736 -- Same data as Vals in list form
4737
4738 Rep_Count : Nat;
4739 -- Used to validate Max_Others_Replicate limit
4740
4741 Elmt : Node_Id;
4742 Num : Int := UI_To_Int (Lov);
4743 Choice_Index : Int;
4744 Choice : Node_Id;
4745 Lo, Hi : Node_Id;
4746
4747 begin
4748 if Present (Expressions (N)) then
4749 Elmt := First (Expressions (N));
4750 while Present (Elmt) loop
4751 if Nkind (Elmt) = N_Aggregate
4752 and then Present (Next_Index (Ix))
4753 and then
4754 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
4755 then
4756 return False;
4757 end if;
4758
4759 -- Duplicate expression for each index it covers
4760
4761 Vals (Num) := New_Copy_Tree (Elmt);
4762 Num := Num + 1;
4763
4764 Next (Elmt);
4765 end loop;
4766 end if;
4767
4768 if No (Component_Associations (N)) then
4769 return True;
4770 end if;
4771
4772 Elmt := First (Component_Associations (N));
4773
4774 if Nkind (Expression (Elmt)) = N_Aggregate then
4775 if Present (Next_Index (Ix))
4776 and then
4777 not Flatten
4778 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
4779 then
4780 return False;
4781 end if;
4782 end if;
4783
4784 Component_Loop : while Present (Elmt) loop
4785 Choice := First (Choice_List (Elmt));
4786 Choice_Loop : while Present (Choice) loop
4787
4788 -- If we have an others choice, fill in the missing elements
4789 -- subject to the limit established by Max_Others_Replicate.
4790
4791 if Nkind (Choice) = N_Others_Choice then
4792 Rep_Count := 0;
4793
4794 -- If the expression involves a construct that generates
4795 -- a loop, we must generate individual assignments and
4796 -- no flattening is possible.
4797
4798 if Nkind (Expression (Elmt)) = N_Quantified_Expression
4799 then
4800 return False;
4801 end if;
4802
4803 for J in Vals'Range loop
4804 if No (Vals (J)) then
4805 Vals (J) := New_Copy_Tree (Expression (Elmt));
4806 Rep_Count := Rep_Count + 1;
4807
4808 -- Check for maximum others replication. Note that
4809 -- we skip this test if either of the restrictions
4810 -- No_Elaboration_Code or No_Implicit_Loops is
4811 -- active, if this is a preelaborable unit or
4812 -- a predefined unit, or if the unit must be
4813 -- placed in data memory. This also ensures that
4814 -- predefined units get the same level of constant
4815 -- folding in Ada 95 and Ada 2005, where their
4816 -- categorization has changed.
4817
4818 declare
4819 P : constant Entity_Id :=
4820 Cunit_Entity (Current_Sem_Unit);
4821
4822 begin
4823 -- Check if duplication is always OK and, if so,
4824 -- continue processing.
4825
4826 if Restriction_Active (No_Elaboration_Code)
4827 or else Restriction_Active (No_Implicit_Loops)
4828 or else
4829 (Ekind (Current_Scope) = E_Package
4830 and then Static_Elaboration_Desired
4831 (Current_Scope))
4832 or else Is_Preelaborated (P)
4833 or else (Ekind (P) = E_Package_Body
4834 and then
4835 Is_Preelaborated (Spec_Entity (P)))
4836 or else
4837 Is_Predefined_Unit (Get_Source_Unit (P))
4838 then
4839 null;
4840
4841 -- If duplication is not always OK, continue
4842 -- only if either the element is static or is
4843 -- an aggregate which can itself be flattened,
4844 -- and the replication count is not too high.
4845
4846 elsif (Is_Static_Element (Elmt)
4847 or else
4848 (Nkind (Expression (Elmt)) = N_Aggregate
4849 and then Present (Next_Index (Ix))))
4850 and then Rep_Count <= Max_Others_Replicate
4851 then
4852 null;
4853
4854 -- Return False in all the other cases
4855
4856 else
4857 return False;
4858 end if;
4859 end;
4860 end if;
4861 end loop;
4862
4863 if Rep_Count = 0
4864 and then Warn_On_Redundant_Constructs
4865 then
4866 Error_Msg_N ("there are no others?r?", Elmt);
4867 end if;
4868
4869 exit Component_Loop;
4870
4871 -- Case of a subtype mark, identifier or expanded name
4872
4873 elsif Is_Entity_Name (Choice)
4874 and then Is_Type (Entity (Choice))
4875 then
4876 Lo := Type_Low_Bound (Etype (Choice));
4877 Hi := Type_High_Bound (Etype (Choice));
4878
4879 -- Case of subtype indication
4880
4881 elsif Nkind (Choice) = N_Subtype_Indication then
4882 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
4883 Hi := High_Bound (Range_Expression (Constraint (Choice)));
4884
4885 -- Case of a range
4886
4887 elsif Nkind (Choice) = N_Range then
4888 Lo := Low_Bound (Choice);
4889 Hi := High_Bound (Choice);
4890
4891 -- Normal subexpression case
4892
4893 else pragma Assert (Nkind (Choice) in N_Subexpr);
4894 if not Compile_Time_Known_Value (Choice) then
4895 return False;
4896
4897 else
4898 Choice_Index := UI_To_Int (Expr_Value (Choice));
4899
4900 if Choice_Index in Vals'Range then
4901 Vals (Choice_Index) :=
4902 New_Copy_Tree (Expression (Elmt));
4903 goto Continue;
4904
4905 -- Choice is statically out-of-range, will be
4906 -- rewritten to raise Constraint_Error.
4907
4908 else
4909 return False;
4910 end if;
4911 end if;
4912 end if;
4913
4914 -- Range cases merge with Lo,Hi set
4915
4916 if not Compile_Time_Known_Value (Lo)
4917 or else
4918 not Compile_Time_Known_Value (Hi)
4919 then
4920 return False;
4921
4922 else
4923 for J in UI_To_Int (Expr_Value (Lo)) ..
4924 UI_To_Int (Expr_Value (Hi))
4925 loop
4926 Vals (J) := New_Copy_Tree (Expression (Elmt));
4927 end loop;
4928 end if;
4929
4930 <<Continue>>
4931 Next (Choice);
4932 end loop Choice_Loop;
4933
4934 Next (Elmt);
4935 end loop Component_Loop;
4936
4937 -- If we get here the conversion is possible
4938
4939 Vlist := New_List;
4940 for J in Vals'Range loop
4941 Append (Vals (J), Vlist);
4942 end loop;
4943
4944 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4945 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4946 return True;
4947 end;
4948 end Flatten;
4949
4950 -------------
4951 -- Is_Flat --
4952 -------------
4953
4954 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4955 Elmt : Node_Id;
4956
4957 begin
4958 if Dims = 0 then
4959 return True;
4960
4961 elsif Nkind (N) = N_Aggregate then
4962 if Present (Component_Associations (N)) then
4963 return False;
4964
4965 else
4966 Elmt := First (Expressions (N));
4967 while Present (Elmt) loop
4968 if not Is_Flat (Elmt, Dims - 1) then
4969 return False;
4970 end if;
4971
4972 Next (Elmt);
4973 end loop;
4974
4975 return True;
4976 end if;
4977 else
4978 return True;
4979 end if;
4980 end Is_Flat;
4981
4982 -------------------------
4983 -- Is_Static_Element --
4984 -------------------------
4985
4986 function Is_Static_Element (N : Node_Id) return Boolean is
4987 Expr : constant Node_Id := Expression (N);
4988
4989 begin
4990 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal) then
4991 return True;
4992
4993 elsif Is_Entity_Name (Expr)
4994 and then Present (Entity (Expr))
4995 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
4996 then
4997 return True;
4998
4999 elsif Nkind (N) = N_Iterated_Component_Association then
5000 return False;
5001
5002 elsif Nkind (Expr) = N_Aggregate
5003 and then Compile_Time_Known_Aggregate (Expr)
5004 and then not Expansion_Delayed (Expr)
5005 then
5006 return True;
5007
5008 else
5009 return False;
5010 end if;
5011 end Is_Static_Element;
5012
5013 -- Start of processing for Convert_To_Positional
5014
5015 begin
5016 -- Only convert to positional when generating C in case of an
5017 -- object declaration, this is the only case where aggregates are
5018 -- supported in C.
5019
5020 if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
5021 return;
5022 end if;
5023
5024 -- Ada 2005 (AI-287): Do not convert in case of default initialized
5025 -- components because in this case will need to call the corresponding
5026 -- IP procedure.
5027
5028 if Has_Default_Init_Comps (N) then
5029 return;
5030 end if;
5031
5032 -- A subaggregate may have been flattened but is not known to be
5033 -- Compile_Time_Known. Set that flag in cases that cannot require
5034 -- elaboration code, so that the aggregate can be used as the
5035 -- initial value of a thread-local variable.
5036
5037 if Is_Flat (N, Number_Dimensions (Typ)) then
5038 if Static_Array_Aggregate (N) then
5039 Set_Compile_Time_Known_Aggregate (N);
5040 end if;
5041
5042 return;
5043 end if;
5044
5045 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
5046 return;
5047 end if;
5048
5049 -- Do not convert to positional if controlled components are involved
5050 -- since these require special processing
5051
5052 if Has_Controlled_Component (Typ) then
5053 return;
5054 end if;
5055
5056 Check_Static_Components;
5057
5058 -- If the size is known, or all the components are static, try to
5059 -- build a fully positional aggregate.
5060
5061 -- The size of the type may not be known for an aggregate with
5062 -- discriminated array components, but if the components are static
5063 -- it is still possible to verify statically that the length is
5064 -- compatible with the upper bound of the type, and therefore it is
5065 -- worth flattening such aggregates as well.
5066
5067 -- For now the back-end expands these aggregates into individual
5068 -- assignments to the target anyway, but it is conceivable that
5069 -- it will eventually be able to treat such aggregates statically???
5070
5071 if Aggr_Size_OK (N, Typ)
5072 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
5073 then
5074 if Static_Components then
5075 Set_Compile_Time_Known_Aggregate (N);
5076 Set_Expansion_Delayed (N, False);
5077 end if;
5078
5079 Analyze_And_Resolve (N, Typ);
5080 end if;
5081
5082 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
5083 -- that will still require initialization code.
5084
5085 if (Ekind (Current_Scope) = E_Package
5086 and then Static_Elaboration_Desired (Current_Scope))
5087 and then Nkind (Parent (N)) = N_Object_Declaration
5088 then
5089 declare
5090 Expr : Node_Id;
5091
5092 begin
5093 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
5094 Expr := First (Expressions (N));
5095 while Present (Expr) loop
5096 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
5097 or else
5098 (Is_Entity_Name (Expr)
5099 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
5100 then
5101 null;
5102
5103 else
5104 Error_Msg_N
5105 ("non-static object requires elaboration code??", N);
5106 exit;
5107 end if;
5108
5109 Next (Expr);
5110 end loop;
5111
5112 if Present (Component_Associations (N)) then
5113 Error_Msg_N ("object requires elaboration code??", N);
5114 end if;
5115 end if;
5116 end;
5117 end if;
5118 end Convert_To_Positional;
5119
5120 ----------------------------
5121 -- Expand_Array_Aggregate --
5122 ----------------------------
5123
5124 -- Array aggregate expansion proceeds as follows:
5125
5126 -- 1. If requested we generate code to perform all the array aggregate
5127 -- bound checks, specifically
5128
5129 -- (a) Check that the index range defined by aggregate bounds is
5130 -- compatible with corresponding index subtype.
5131
5132 -- (b) If an others choice is present check that no aggregate
5133 -- index is outside the bounds of the index constraint.
5134
5135 -- (c) For multidimensional arrays make sure that all subaggregates
5136 -- corresponding to the same dimension have the same bounds.
5137
5138 -- 2. Check for packed array aggregate which can be converted to a
5139 -- constant so that the aggregate disappears completely.
5140
5141 -- 3. Check case of nested aggregate. Generally nested aggregates are
5142 -- handled during the processing of the parent aggregate.
5143
5144 -- 4. Check if the aggregate can be statically processed. If this is the
5145 -- case pass it as is to Gigi. Note that a necessary condition for
5146 -- static processing is that the aggregate be fully positional.
5147
5148 -- 5. If in-place aggregate expansion is possible (i.e. no need to create
5149 -- a temporary) then mark the aggregate as such and return. Otherwise
5150 -- create a new temporary and generate the appropriate initialization
5151 -- code.
5152
5153 procedure Expand_Array_Aggregate (N : Node_Id) is
5154 Loc : constant Source_Ptr := Sloc (N);
5155
5156 Typ : constant Entity_Id := Etype (N);
5157 Ctyp : constant Entity_Id := Component_Type (Typ);
5158 -- Typ is the correct constrained array subtype of the aggregate
5159 -- Ctyp is the corresponding component type.
5160
5161 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
5162 -- Number of aggregate index dimensions
5163
5164 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
5165 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
5166 -- Low and High bounds of the constraint for each aggregate index
5167
5168 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
5169 -- The type of each index
5170
5171 In_Place_Assign_OK_For_Declaration : Boolean := False;
5172 -- True if we are to generate an in-place assignment for a declaration
5173
5174 Maybe_In_Place_OK : Boolean;
5175 -- If the type is neither controlled nor packed and the aggregate
5176 -- is the expression in an assignment, assignment in place may be
5177 -- possible, provided other conditions are met on the LHS.
5178
5179 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
5180 (others => False);
5181 -- If Others_Present (J) is True, then there is an others choice in one
5182 -- of the subaggregates of N at dimension J.
5183
5184 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
5185 -- Returns true if an aggregate assignment can be done by the back end
5186
5187 procedure Build_Constrained_Type (Positional : Boolean);
5188 -- If the subtype is not static or unconstrained, build a constrained
5189 -- type using the computable sizes of the aggregate and its sub-
5190 -- aggregates.
5191
5192 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
5193 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
5194 -- by Index_Bounds.
5195
5196 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
5197 -- Checks that in a multidimensional array aggregate all subaggregates
5198 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
5199 -- an array subaggregate. Dim is the dimension corresponding to the
5200 -- subaggregate.
5201
5202 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
5203 -- Computes the values of array Others_Present. Sub_Aggr is the array
5204 -- subaggregate we start the computation from. Dim is the dimension
5205 -- corresponding to the subaggregate.
5206
5207 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
5208 -- Checks that if an others choice is present in any subaggregate, no
5209 -- aggregate index is outside the bounds of the index constraint.
5210 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
5211 -- to the subaggregate.
5212
5213 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
5214 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
5215 -- built directly into the target of the assignment it must be free
5216 -- of side effects.
5217
5218 ------------------------------------
5219 -- Aggr_Assignment_OK_For_Backend --
5220 ------------------------------------
5221
5222 -- Backend processing by Gigi/gcc is possible only if all the following
5223 -- conditions are met:
5224
5225 -- 1. N consists of a single OTHERS choice, possibly recursively
5226
5227 -- 2. The array type has no null ranges (the purpose of this is to
5228 -- avoid a bogus warning for an out-of-range value).
5229
5230 -- 3. The array type has no atomic components
5231
5232 -- 4. The component type is elementary
5233
5234 -- 5. The component size is a multiple of Storage_Unit
5235
5236 -- 6. The component size is Storage_Unit or the value is of the form
5237 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
5238 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
5239 -- the 8-bit value M, concatenated together.
5240
5241 -- The ultimate goal is to generate a call to a fast memset routine
5242 -- specifically optimized for the target.
5243
5244 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
5245 Csiz : Uint := No_Uint;
5246 Ctyp : Entity_Id;
5247 Expr : Node_Id;
5248 High : Node_Id;
5249 Index : Entity_Id;
5250 Low : Node_Id;
5251 Nunits : Int;
5252 Remainder : Uint;
5253 Value : Uint;
5254
5255 begin
5256 -- Back end doesn't know about <>
5257
5258 if Has_Default_Init_Comps (N) then
5259 return False;
5260 end if;
5261
5262 -- Recurse as far as possible to find the innermost component type
5263
5264 Ctyp := Etype (N);
5265 Expr := N;
5266 while Is_Array_Type (Ctyp) loop
5267 if Nkind (Expr) /= N_Aggregate
5268 or else not Is_Others_Aggregate (Expr)
5269 then
5270 return False;
5271 end if;
5272
5273 Index := First_Index (Ctyp);
5274 while Present (Index) loop
5275 Get_Index_Bounds (Index, Low, High);
5276
5277 if Is_Null_Range (Low, High) then
5278 return False;
5279 end if;
5280
5281 Next_Index (Index);
5282 end loop;
5283
5284 Expr := Expression (First (Component_Associations (Expr)));
5285
5286 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
5287 if Nkind (Expr) /= N_Aggregate
5288 or else not Is_Others_Aggregate (Expr)
5289 then
5290 return False;
5291 end if;
5292
5293 Expr := Expression (First (Component_Associations (Expr)));
5294 end loop;
5295
5296 if Has_Atomic_Components (Ctyp) then
5297 return False;
5298 end if;
5299
5300 Csiz := Component_Size (Ctyp);
5301 Ctyp := Component_Type (Ctyp);
5302
5303 if Is_Atomic_Or_VFA (Ctyp) then
5304 return False;
5305 end if;
5306 end loop;
5307
5308 -- An Iterated_Component_Association involves a loop (in most cases)
5309 -- and is never static.
5310
5311 if Nkind (Parent (Expr)) = N_Iterated_Component_Association then
5312 return False;
5313 end if;
5314
5315 -- Access types need to be dealt with specially
5316
5317 if Is_Access_Type (Ctyp) then
5318
5319 -- Component_Size is not set by Layout_Type if the component
5320 -- type is an access type ???
5321
5322 Csiz := Esize (Ctyp);
5323
5324 -- Fat pointers are rejected as they are not really elementary
5325 -- for the backend.
5326
5327 if Csiz /= System_Address_Size then
5328 return False;
5329 end if;
5330
5331 -- The supported expressions are NULL and constants, others are
5332 -- rejected upfront to avoid being analyzed below, which can be
5333 -- problematic for some of them, for example allocators.
5334
5335 if Nkind (Expr) /= N_Null and then not Is_Entity_Name (Expr) then
5336 return False;
5337 end if;
5338
5339 -- Scalar types are OK if their size is a multiple of Storage_Unit
5340
5341 elsif Is_Scalar_Type (Ctyp) then
5342 pragma Assert (Csiz /= No_Uint);
5343
5344 if Csiz mod System_Storage_Unit /= 0 then
5345 return False;
5346 end if;
5347
5348 -- Composite types are rejected
5349
5350 else
5351 return False;
5352 end if;
5353
5354 -- If the expression has side effects (e.g. contains calls with
5355 -- potential side effects) reject as well. We only preanalyze the
5356 -- expression to prevent the removal of intended side effects.
5357
5358 Preanalyze_And_Resolve (Expr, Ctyp);
5359
5360 if not Side_Effect_Free (Expr) then
5361 return False;
5362 end if;
5363
5364 -- The expression needs to be analyzed if True is returned
5365
5366 Analyze_And_Resolve (Expr, Ctyp);
5367
5368 -- Strip away any conversions from the expression as they simply
5369 -- qualify the real expression.
5370
5371 while Nkind_In (Expr, N_Unchecked_Type_Conversion,
5372 N_Type_Conversion)
5373 loop
5374 Expr := Expression (Expr);
5375 end loop;
5376
5377 Nunits := UI_To_Int (Csiz) / System_Storage_Unit;
5378
5379 if Nunits = 1 then
5380 return True;
5381 end if;
5382
5383 if not Compile_Time_Known_Value (Expr) then
5384 return False;
5385 end if;
5386
5387 -- The only supported value for floating point is 0.0
5388
5389 if Is_Floating_Point_Type (Ctyp) then
5390 return Expr_Value_R (Expr) = Ureal_0;
5391 end if;
5392
5393 -- For other types, we can look into the value as an integer
5394
5395 Value := Expr_Value (Expr);
5396
5397 if Has_Biased_Representation (Ctyp) then
5398 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
5399 end if;
5400
5401 -- Values 0 and -1 immediately satisfy the last check
5402
5403 if Value = Uint_0 or else Value = Uint_Minus_1 then
5404 return True;
5405 end if;
5406
5407 -- We need to work with an unsigned value
5408
5409 if Value < 0 then
5410 Value := Value + 2**(System_Storage_Unit * Nunits);
5411 end if;
5412
5413 Remainder := Value rem 2**System_Storage_Unit;
5414
5415 for J in 1 .. Nunits - 1 loop
5416 Value := Value / 2**System_Storage_Unit;
5417
5418 if Value rem 2**System_Storage_Unit /= Remainder then
5419 return False;
5420 end if;
5421 end loop;
5422
5423 return True;
5424 end Aggr_Assignment_OK_For_Backend;
5425
5426 ----------------------------
5427 -- Build_Constrained_Type --
5428 ----------------------------
5429
5430 procedure Build_Constrained_Type (Positional : Boolean) is
5431 Loc : constant Source_Ptr := Sloc (N);
5432 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
5433 Comp : Node_Id;
5434 Decl : Node_Id;
5435 Typ : constant Entity_Id := Etype (N);
5436 Indexes : constant List_Id := New_List;
5437 Num : Nat;
5438 Sub_Agg : Node_Id;
5439
5440 begin
5441 -- If the aggregate is purely positional, all its subaggregates
5442 -- have the same size. We collect the dimensions from the first
5443 -- subaggregate at each level.
5444
5445 if Positional then
5446 Sub_Agg := N;
5447
5448 for D in 1 .. Number_Dimensions (Typ) loop
5449 Sub_Agg := First (Expressions (Sub_Agg));
5450
5451 Comp := Sub_Agg;
5452 Num := 0;
5453 while Present (Comp) loop
5454 Num := Num + 1;
5455 Next (Comp);
5456 end loop;
5457
5458 Append_To (Indexes,
5459 Make_Range (Loc,
5460 Low_Bound => Make_Integer_Literal (Loc, 1),
5461 High_Bound => Make_Integer_Literal (Loc, Num)));
5462 end loop;
5463
5464 else
5465 -- We know the aggregate type is unconstrained and the aggregate
5466 -- is not processable by the back end, therefore not necessarily
5467 -- positional. Retrieve each dimension bounds (computed earlier).
5468
5469 for D in 1 .. Number_Dimensions (Typ) loop
5470 Append_To (Indexes,
5471 Make_Range (Loc,
5472 Low_Bound => Aggr_Low (D),
5473 High_Bound => Aggr_High (D)));
5474 end loop;
5475 end if;
5476
5477 Decl :=
5478 Make_Full_Type_Declaration (Loc,
5479 Defining_Identifier => Agg_Type,
5480 Type_Definition =>
5481 Make_Constrained_Array_Definition (Loc,
5482 Discrete_Subtype_Definitions => Indexes,
5483 Component_Definition =>
5484 Make_Component_Definition (Loc,
5485 Aliased_Present => False,
5486 Subtype_Indication =>
5487 New_Occurrence_Of (Component_Type (Typ), Loc))));
5488
5489 Insert_Action (N, Decl);
5490 Analyze (Decl);
5491 Set_Etype (N, Agg_Type);
5492 Set_Is_Itype (Agg_Type);
5493 Freeze_Itype (Agg_Type, N);
5494 end Build_Constrained_Type;
5495
5496 ------------------
5497 -- Check_Bounds --
5498 ------------------
5499
5500 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
5501 Aggr_Lo : Node_Id;
5502 Aggr_Hi : Node_Id;
5503
5504 Ind_Lo : Node_Id;
5505 Ind_Hi : Node_Id;
5506
5507 Cond : Node_Id := Empty;
5508
5509 begin
5510 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
5511 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
5512
5513 -- Generate the following test:
5514
5515 -- [constraint_error when
5516 -- Aggr_Lo <= Aggr_Hi and then
5517 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5518
5519 -- As an optimization try to see if some tests are trivially vacuous
5520 -- because we are comparing an expression against itself.
5521
5522 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
5523 Cond := Empty;
5524
5525 elsif Aggr_Hi = Ind_Hi then
5526 Cond :=
5527 Make_Op_Lt (Loc,
5528 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5529 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
5530
5531 elsif Aggr_Lo = Ind_Lo then
5532 Cond :=
5533 Make_Op_Gt (Loc,
5534 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5535 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
5536
5537 else
5538 Cond :=
5539 Make_Or_Else (Loc,
5540 Left_Opnd =>
5541 Make_Op_Lt (Loc,
5542 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5543 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
5544
5545 Right_Opnd =>
5546 Make_Op_Gt (Loc,
5547 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5548 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
5549 end if;
5550
5551 if Present (Cond) then
5552 Cond :=
5553 Make_And_Then (Loc,
5554 Left_Opnd =>
5555 Make_Op_Le (Loc,
5556 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5557 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
5558
5559 Right_Opnd => Cond);
5560
5561 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5562 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5563 Insert_Action (N,
5564 Make_Raise_Constraint_Error (Loc,
5565 Condition => Cond,
5566 Reason => CE_Range_Check_Failed));
5567 end if;
5568 end Check_Bounds;
5569
5570 ----------------------------
5571 -- Check_Same_Aggr_Bounds --
5572 ----------------------------
5573
5574 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5575 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
5576 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
5577 -- The bounds of this specific subaggregate
5578
5579 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5580 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5581 -- The bounds of the aggregate for this dimension
5582
5583 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5584 -- The index type for this dimension.xxx
5585
5586 Cond : Node_Id := Empty;
5587 Assoc : Node_Id;
5588 Expr : Node_Id;
5589
5590 begin
5591 -- If index checks are on generate the test
5592
5593 -- [constraint_error when
5594 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5595
5596 -- As an optimization try to see if some tests are trivially vacuos
5597 -- because we are comparing an expression against itself. Also for
5598 -- the first dimension the test is trivially vacuous because there
5599 -- is just one aggregate for dimension 1.
5600
5601 if Index_Checks_Suppressed (Ind_Typ) then
5602 Cond := Empty;
5603
5604 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5605 then
5606 Cond := Empty;
5607
5608 elsif Aggr_Hi = Sub_Hi then
5609 Cond :=
5610 Make_Op_Ne (Loc,
5611 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5612 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5613
5614 elsif Aggr_Lo = Sub_Lo then
5615 Cond :=
5616 Make_Op_Ne (Loc,
5617 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5618 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5619
5620 else
5621 Cond :=
5622 Make_Or_Else (Loc,
5623 Left_Opnd =>
5624 Make_Op_Ne (Loc,
5625 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5626 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5627
5628 Right_Opnd =>
5629 Make_Op_Ne (Loc,
5630 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5631 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5632 end if;
5633
5634 if Present (Cond) then
5635 Insert_Action (N,
5636 Make_Raise_Constraint_Error (Loc,
5637 Condition => Cond,
5638 Reason => CE_Length_Check_Failed));
5639 end if;
5640
5641 -- Now look inside the subaggregate to see if there is more work
5642
5643 if Dim < Aggr_Dimension then
5644
5645 -- Process positional components
5646
5647 if Present (Expressions (Sub_Aggr)) then
5648 Expr := First (Expressions (Sub_Aggr));
5649 while Present (Expr) loop
5650 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5651 Next (Expr);
5652 end loop;
5653 end if;
5654
5655 -- Process component associations
5656
5657 if Present (Component_Associations (Sub_Aggr)) then
5658 Assoc := First (Component_Associations (Sub_Aggr));
5659 while Present (Assoc) loop
5660 Expr := Expression (Assoc);
5661 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5662 Next (Assoc);
5663 end loop;
5664 end if;
5665 end if;
5666 end Check_Same_Aggr_Bounds;
5667
5668 ----------------------------
5669 -- Compute_Others_Present --
5670 ----------------------------
5671
5672 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5673 Assoc : Node_Id;
5674 Expr : Node_Id;
5675
5676 begin
5677 if Present (Component_Associations (Sub_Aggr)) then
5678 Assoc := Last (Component_Associations (Sub_Aggr));
5679
5680 if Nkind (First (Choice_List (Assoc))) = N_Others_Choice then
5681 Others_Present (Dim) := True;
5682 end if;
5683 end if;
5684
5685 -- Now look inside the subaggregate to see if there is more work
5686
5687 if Dim < Aggr_Dimension then
5688
5689 -- Process positional components
5690
5691 if Present (Expressions (Sub_Aggr)) then
5692 Expr := First (Expressions (Sub_Aggr));
5693 while Present (Expr) loop
5694 Compute_Others_Present (Expr, Dim + 1);
5695 Next (Expr);
5696 end loop;
5697 end if;
5698
5699 -- Process component associations
5700
5701 if Present (Component_Associations (Sub_Aggr)) then
5702 Assoc := First (Component_Associations (Sub_Aggr));
5703 while Present (Assoc) loop
5704 Expr := Expression (Assoc);
5705 Compute_Others_Present (Expr, Dim + 1);
5706 Next (Assoc);
5707 end loop;
5708 end if;
5709 end if;
5710 end Compute_Others_Present;
5711
5712 ------------------
5713 -- Others_Check --
5714 ------------------
5715
5716 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
5717 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5718 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5719 -- The bounds of the aggregate for this dimension
5720
5721 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5722 -- The index type for this dimension
5723
5724 Need_To_Check : Boolean := False;
5725
5726 Choices_Lo : Node_Id := Empty;
5727 Choices_Hi : Node_Id := Empty;
5728 -- The lowest and highest discrete choices for a named subaggregate
5729
5730 Nb_Choices : Int := -1;
5731 -- The number of discrete non-others choices in this subaggregate
5732
5733 Nb_Elements : Uint := Uint_0;
5734 -- The number of elements in a positional aggregate
5735
5736 Cond : Node_Id := Empty;
5737
5738 Assoc : Node_Id;
5739 Choice : Node_Id;
5740 Expr : Node_Id;
5741
5742 begin
5743 -- Check if we have an others choice. If we do make sure that this
5744 -- subaggregate contains at least one element in addition to the
5745 -- others choice.
5746
5747 if Range_Checks_Suppressed (Ind_Typ) then
5748 Need_To_Check := False;
5749
5750 elsif Present (Expressions (Sub_Aggr))
5751 and then Present (Component_Associations (Sub_Aggr))
5752 then
5753 Need_To_Check := True;
5754
5755 elsif Present (Component_Associations (Sub_Aggr)) then
5756 Assoc := Last (Component_Associations (Sub_Aggr));
5757
5758 if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then
5759 Need_To_Check := False;
5760
5761 else
5762 -- Count the number of discrete choices. Start with -1 because
5763 -- the others choice does not count.
5764
5765 -- Is there some reason we do not use List_Length here ???
5766
5767 Nb_Choices := -1;
5768 Assoc := First (Component_Associations (Sub_Aggr));
5769 while Present (Assoc) loop
5770 Choice := First (Choice_List (Assoc));
5771 while Present (Choice) loop
5772 Nb_Choices := Nb_Choices + 1;
5773 Next (Choice);
5774 end loop;
5775
5776 Next (Assoc);
5777 end loop;
5778
5779 -- If there is only an others choice nothing to do
5780
5781 Need_To_Check := (Nb_Choices > 0);
5782 end if;
5783
5784 else
5785 Need_To_Check := False;
5786 end if;
5787
5788 -- If we are dealing with a positional subaggregate with an others
5789 -- choice then compute the number or positional elements.
5790
5791 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
5792 Expr := First (Expressions (Sub_Aggr));
5793 Nb_Elements := Uint_0;
5794 while Present (Expr) loop
5795 Nb_Elements := Nb_Elements + 1;
5796 Next (Expr);
5797 end loop;
5798
5799 -- If the aggregate contains discrete choices and an others choice
5800 -- compute the smallest and largest discrete choice values.
5801
5802 elsif Need_To_Check then
5803 Compute_Choices_Lo_And_Choices_Hi : declare
5804
5805 Table : Case_Table_Type (1 .. Nb_Choices);
5806 -- Used to sort all the different choice values
5807
5808 J : Pos := 1;
5809 Low : Node_Id;
5810 High : Node_Id;
5811
5812 begin
5813 Assoc := First (Component_Associations (Sub_Aggr));
5814 while Present (Assoc) loop
5815 Choice := First (Choice_List (Assoc));
5816 while Present (Choice) loop
5817 if Nkind (Choice) = N_Others_Choice then
5818 exit;
5819 end if;
5820
5821 Get_Index_Bounds (Choice, Low, High);
5822 Table (J).Choice_Lo := Low;
5823 Table (J).Choice_Hi := High;
5824
5825 J := J + 1;
5826 Next (Choice);
5827 end loop;
5828
5829 Next (Assoc);
5830 end loop;
5831
5832 -- Sort the discrete choices
5833
5834 Sort_Case_Table (Table);
5835
5836 Choices_Lo := Table (1).Choice_Lo;
5837 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5838 end Compute_Choices_Lo_And_Choices_Hi;
5839 end if;
5840
5841 -- If no others choice in this subaggregate, or the aggregate
5842 -- comprises only an others choice, nothing to do.
5843
5844 if not Need_To_Check then
5845 Cond := Empty;
5846
5847 -- If we are dealing with an aggregate containing an others choice
5848 -- and positional components, we generate the following test:
5849
5850 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5851 -- Ind_Typ'Pos (Aggr_Hi)
5852 -- then
5853 -- raise Constraint_Error;
5854 -- end if;
5855
5856 -- in the general case, but the following simpler test:
5857
5858 -- [constraint_error when
5859 -- Aggr_Lo + (Nb_Elements - 1) > Aggr_Hi];
5860
5861 -- instead if the index type is a signed integer.
5862
5863 elsif Nb_Elements > Uint_0 then
5864 if Nb_Elements = Uint_1 then
5865 Cond :=
5866 Make_Op_Gt (Loc,
5867 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5868 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi));
5869
5870 elsif Is_Signed_Integer_Type (Ind_Typ) then
5871 Cond :=
5872 Make_Op_Gt (Loc,
5873 Left_Opnd =>
5874 Make_Op_Add (Loc,
5875 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5876 Right_Opnd =>
5877 Make_Integer_Literal (Loc, Nb_Elements - 1)),
5878 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi));
5879
5880 else
5881 Cond :=
5882 Make_Op_Gt (Loc,
5883 Left_Opnd =>
5884 Make_Op_Add (Loc,
5885 Left_Opnd =>
5886 Make_Attribute_Reference (Loc,
5887 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5888 Attribute_Name => Name_Pos,
5889 Expressions =>
5890 New_List
5891 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5892 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5893
5894 Right_Opnd =>
5895 Make_Attribute_Reference (Loc,
5896 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5897 Attribute_Name => Name_Pos,
5898 Expressions => New_List (
5899 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5900 end if;
5901
5902 -- If we are dealing with an aggregate containing an others choice
5903 -- and discrete choices we generate the following test:
5904
5905 -- [constraint_error when
5906 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5907
5908 else
5909 Cond :=
5910 Make_Or_Else (Loc,
5911 Left_Opnd =>
5912 Make_Op_Lt (Loc,
5913 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5914 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5915
5916 Right_Opnd =>
5917 Make_Op_Gt (Loc,
5918 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5919 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5920 end if;
5921
5922 if Present (Cond) then
5923 Insert_Action (N,
5924 Make_Raise_Constraint_Error (Loc,
5925 Condition => Cond,
5926 Reason => CE_Length_Check_Failed));
5927 -- Questionable reason code, shouldn't that be a
5928 -- CE_Range_Check_Failed ???
5929 end if;
5930
5931 -- Now look inside the subaggregate to see if there is more work
5932
5933 if Dim < Aggr_Dimension then
5934
5935 -- Process positional components
5936
5937 if Present (Expressions (Sub_Aggr)) then
5938 Expr := First (Expressions (Sub_Aggr));
5939 while Present (Expr) loop
5940 Others_Check (Expr, Dim + 1);
5941 Next (Expr);
5942 end loop;
5943 end if;
5944
5945 -- Process component associations
5946
5947 if Present (Component_Associations (Sub_Aggr)) then
5948 Assoc := First (Component_Associations (Sub_Aggr));
5949 while Present (Assoc) loop
5950 Expr := Expression (Assoc);
5951 Others_Check (Expr, Dim + 1);
5952 Next (Assoc);
5953 end loop;
5954 end if;
5955 end if;
5956 end Others_Check;
5957
5958 -------------------------
5959 -- Safe_Left_Hand_Side --
5960 -------------------------
5961
5962 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5963 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5964 -- If the left-hand side includes an indexed component, check that
5965 -- the indexes are free of side effects.
5966
5967 -------------------
5968 -- Is_Safe_Index --
5969 -------------------
5970
5971 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5972 begin
5973 if Is_Entity_Name (Indx) then
5974 return True;
5975
5976 elsif Nkind (Indx) = N_Integer_Literal then
5977 return True;
5978
5979 elsif Nkind (Indx) = N_Function_Call
5980 and then Is_Entity_Name (Name (Indx))
5981 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5982 then
5983 return True;
5984
5985 elsif Nkind (Indx) = N_Type_Conversion
5986 and then Is_Safe_Index (Expression (Indx))
5987 then
5988 return True;
5989
5990 else
5991 return False;
5992 end if;
5993 end Is_Safe_Index;
5994
5995 -- Start of processing for Safe_Left_Hand_Side
5996
5997 begin
5998 if Is_Entity_Name (N) then
5999 return True;
6000
6001 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
6002 and then Safe_Left_Hand_Side (Prefix (N))
6003 then
6004 return True;
6005
6006 elsif Nkind (N) = N_Indexed_Component
6007 and then Safe_Left_Hand_Side (Prefix (N))
6008 and then Is_Safe_Index (First (Expressions (N)))
6009 then
6010 return True;
6011
6012 elsif Nkind (N) = N_Unchecked_Type_Conversion then
6013 return Safe_Left_Hand_Side (Expression (N));
6014
6015 else
6016 return False;
6017 end if;
6018 end Safe_Left_Hand_Side;
6019
6020 -- Local variables
6021
6022 Tmp : Entity_Id;
6023 -- Holds the temporary aggregate value
6024
6025 Tmp_Decl : Node_Id;
6026 -- Holds the declaration of Tmp
6027
6028 Aggr_Code : List_Id;
6029 Parent_Node : Node_Id;
6030 Parent_Kind : Node_Kind;
6031
6032 -- Start of processing for Expand_Array_Aggregate
6033
6034 begin
6035 -- Do not touch the special aggregates of attributes used for Asm calls
6036
6037 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
6038 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
6039 then
6040 return;
6041
6042 -- Do not expand an aggregate for an array type which contains tasks if
6043 -- the aggregate is associated with an unexpanded return statement of a
6044 -- build-in-place function. The aggregate is expanded when the related
6045 -- return statement (rewritten into an extended return) is processed.
6046 -- This delay ensures that any temporaries and initialization code
6047 -- generated for the aggregate appear in the proper return block and
6048 -- use the correct _chain and _master.
6049
6050 elsif Has_Task (Base_Type (Etype (N)))
6051 and then Nkind (Parent (N)) = N_Simple_Return_Statement
6052 and then Is_Build_In_Place_Function
6053 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
6054 then
6055 return;
6056
6057 -- Do not attempt expansion if error already detected. We may reach this
6058 -- point in spite of previous errors when compiling with -gnatq, to
6059 -- force all possible errors (this is the usual ACATS mode).
6060
6061 elsif Error_Posted (N) then
6062 return;
6063 end if;
6064
6065 -- If the semantic analyzer has determined that aggregate N will raise
6066 -- Constraint_Error at run time, then the aggregate node has been
6067 -- replaced with an N_Raise_Constraint_Error node and we should
6068 -- never get here.
6069
6070 pragma Assert (not Raises_Constraint_Error (N));
6071
6072 -- STEP 1a
6073
6074 -- Check that the index range defined by aggregate bounds is
6075 -- compatible with corresponding index subtype.
6076
6077 Index_Compatibility_Check : declare
6078 Aggr_Index_Range : Node_Id := First_Index (Typ);
6079 -- The current aggregate index range
6080
6081 Index_Constraint : Node_Id := First_Index (Etype (Typ));
6082 -- The corresponding index constraint against which we have to
6083 -- check the above aggregate index range.
6084
6085 begin
6086 Compute_Others_Present (N, 1);
6087
6088 for J in 1 .. Aggr_Dimension loop
6089 -- There is no need to emit a check if an others choice is present
6090 -- for this array aggregate dimension since in this case one of
6091 -- N's subaggregates has taken its bounds from the context and
6092 -- these bounds must have been checked already. In addition all
6093 -- subaggregates corresponding to the same dimension must all have
6094 -- the same bounds (checked in (c) below).
6095
6096 if not Range_Checks_Suppressed (Etype (Index_Constraint))
6097 and then not Others_Present (J)
6098 then
6099 -- We don't use Checks.Apply_Range_Check here because it emits
6100 -- a spurious check. Namely it checks that the range defined by
6101 -- the aggregate bounds is nonempty. But we know this already
6102 -- if we get here.
6103
6104 Check_Bounds (Aggr_Index_Range, Index_Constraint);
6105 end if;
6106
6107 -- Save the low and high bounds of the aggregate index as well as
6108 -- the index type for later use in checks (b) and (c) below.
6109
6110 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
6111 Aggr_High (J) := High_Bound (Aggr_Index_Range);
6112
6113 Aggr_Index_Typ (J) := Etype (Index_Constraint);
6114
6115 Next_Index (Aggr_Index_Range);
6116 Next_Index (Index_Constraint);
6117 end loop;
6118 end Index_Compatibility_Check;
6119
6120 -- STEP 1b
6121
6122 -- If an others choice is present check that no aggregate index is
6123 -- outside the bounds of the index constraint.
6124
6125 Others_Check (N, 1);
6126
6127 -- STEP 1c
6128
6129 -- For multidimensional arrays make sure that all subaggregates
6130 -- corresponding to the same dimension have the same bounds.
6131
6132 if Aggr_Dimension > 1 then
6133 Check_Same_Aggr_Bounds (N, 1);
6134 end if;
6135
6136 -- STEP 1d
6137
6138 -- If we have a default component value, or simple initialization is
6139 -- required for the component type, then we replace <> in component
6140 -- associations by the required default value.
6141
6142 declare
6143 Default_Val : Node_Id;
6144 Assoc : Node_Id;
6145
6146 begin
6147 if (Present (Default_Aspect_Component_Value (Typ))
6148 or else Needs_Simple_Initialization (Ctyp))
6149 and then Present (Component_Associations (N))
6150 then
6151 Assoc := First (Component_Associations (N));
6152 while Present (Assoc) loop
6153 if Nkind (Assoc) = N_Component_Association
6154 and then Box_Present (Assoc)
6155 then
6156 Set_Box_Present (Assoc, False);
6157
6158 if Present (Default_Aspect_Component_Value (Typ)) then
6159 Default_Val := Default_Aspect_Component_Value (Typ);
6160 else
6161 Default_Val := Get_Simple_Init_Val (Ctyp, N);
6162 end if;
6163
6164 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
6165 Analyze_And_Resolve (Expression (Assoc), Ctyp);
6166 end if;
6167
6168 Next (Assoc);
6169 end loop;
6170 end if;
6171 end;
6172
6173 -- STEP 2
6174
6175 -- Here we test for is packed array aggregate that we can handle at
6176 -- compile time. If so, return with transformation done. Note that we do
6177 -- this even if the aggregate is nested, because once we have done this
6178 -- processing, there is no more nested aggregate.
6179
6180 if Packed_Array_Aggregate_Handled (N) then
6181 return;
6182 end if;
6183
6184 -- At this point we try to convert to positional form
6185
6186 if Ekind (Current_Scope) = E_Package
6187 and then Static_Elaboration_Desired (Current_Scope)
6188 then
6189 Convert_To_Positional (N, Max_Others_Replicate => 100);
6190 else
6191 Convert_To_Positional (N);
6192 end if;
6193
6194 -- if the result is no longer an aggregate (e.g. it may be a string
6195 -- literal, or a temporary which has the needed value), then we are
6196 -- done, since there is no longer a nested aggregate.
6197
6198 if Nkind (N) /= N_Aggregate then
6199 return;
6200
6201 -- We are also done if the result is an analyzed aggregate, indicating
6202 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6203 -- aggregate.
6204
6205 elsif Analyzed (N) and then Is_Rewrite_Substitution (N) then
6206 return;
6207 end if;
6208
6209 -- If all aggregate components are compile-time known and the aggregate
6210 -- has been flattened, nothing left to do. The same occurs if the
6211 -- aggregate is used to initialize the components of a statically
6212 -- allocated dispatch table.
6213
6214 if Compile_Time_Known_Aggregate (N)
6215 or else Is_Static_Dispatch_Table_Aggregate (N)
6216 then
6217 Set_Expansion_Delayed (N, False);
6218 return;
6219 end if;
6220
6221 -- Now see if back end processing is possible
6222
6223 if Backend_Processing_Possible (N) then
6224
6225 -- If the aggregate is static but the constraints are not, build
6226 -- a static subtype for the aggregate, so that Gigi can place it
6227 -- in static memory. Perform an unchecked_conversion to the non-
6228 -- static type imposed by the context.
6229
6230 declare
6231 Itype : constant Entity_Id := Etype (N);
6232 Index : Node_Id;
6233 Needs_Type : Boolean := False;
6234
6235 begin
6236 Index := First_Index (Itype);
6237 while Present (Index) loop
6238 if not Is_OK_Static_Subtype (Etype (Index)) then
6239 Needs_Type := True;
6240 exit;
6241 else
6242 Next_Index (Index);
6243 end if;
6244 end loop;
6245
6246 if Needs_Type then
6247 Build_Constrained_Type (Positional => True);
6248 Rewrite (N, Unchecked_Convert_To (Itype, N));
6249 Analyze (N);
6250 end if;
6251 end;
6252
6253 return;
6254 end if;
6255
6256 -- STEP 3
6257
6258 -- Delay expansion for nested aggregates: it will be taken care of when
6259 -- the parent aggregate is expanded.
6260
6261 Parent_Node := Parent (N);
6262 Parent_Kind := Nkind (Parent_Node);
6263
6264 if Parent_Kind = N_Qualified_Expression then
6265 Parent_Node := Parent (Parent_Node);
6266 Parent_Kind := Nkind (Parent_Node);
6267 end if;
6268
6269 if Parent_Kind = N_Aggregate
6270 or else Parent_Kind = N_Extension_Aggregate
6271 or else Parent_Kind = N_Component_Association
6272 or else (Parent_Kind = N_Object_Declaration
6273 and then Needs_Finalization (Typ))
6274 or else (Parent_Kind = N_Assignment_Statement
6275 and then Inside_Init_Proc)
6276 then
6277 Set_Expansion_Delayed (N, not Static_Array_Aggregate (N));
6278 return;
6279 end if;
6280
6281 -- STEP 4
6282
6283 -- Check whether in-place aggregate expansion is possible
6284
6285 -- For object declarations we build the aggregate in place, unless
6286 -- the array is bit-packed.
6287
6288 -- For assignments we do the assignment in place if all the component
6289 -- associations have compile-time known values, or are default-
6290 -- initialized limited components, e.g. tasks. For other cases we
6291 -- create a temporary. The analysis for safety of on-line assignment
6292 -- is delicate, i.e. we don't know how to do it fully yet ???
6293
6294 -- For allocators we assign to the designated object in place if the
6295 -- aggregate meets the same conditions as other in-place assignments.
6296 -- In this case the aggregate may not come from source but was created
6297 -- for default initialization, e.g. with Initialize_Scalars.
6298
6299 if Requires_Transient_Scope (Typ) then
6300 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6301 end if;
6302
6303 -- An array of limited components is built in place
6304
6305 if Is_Limited_Type (Typ) then
6306 Maybe_In_Place_OK := True;
6307
6308 elsif Has_Default_Init_Comps (N) then
6309 Maybe_In_Place_OK := False;
6310
6311 elsif Is_Bit_Packed_Array (Typ)
6312 or else Has_Controlled_Component (Typ)
6313 then
6314 Maybe_In_Place_OK := False;
6315
6316 else
6317 Maybe_In_Place_OK :=
6318 (Nkind (Parent (N)) = N_Assignment_Statement
6319 and then In_Place_Assign_OK (N))
6320
6321 or else
6322 (Nkind (Parent (Parent (N))) = N_Allocator
6323 and then In_Place_Assign_OK (N));
6324 end if;
6325
6326 -- If this is an array of tasks, it will be expanded into build-in-place
6327 -- assignments. Build an activation chain for the tasks now.
6328
6329 if Has_Task (Etype (N)) then
6330 Build_Activation_Chain_Entity (N);
6331 end if;
6332
6333 -- Perform in-place expansion of aggregate in an object declaration.
6334 -- Note: actions generated for the aggregate will be captured in an
6335 -- expression-with-actions statement so that they can be transferred
6336 -- to freeze actions later if there is an address clause for the
6337 -- object. (Note: we don't use a block statement because this would
6338 -- cause generated freeze nodes to be elaborated in the wrong scope).
6339
6340 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6341 -- expected to appear in qualified form. In-place expansion eliminates
6342 -- the qualification and eventually violates this SPARK 05 restiction.
6343
6344 -- Arrays of limited components must be built in place. The code
6345 -- previously excluded controlled components but this is an old
6346 -- oversight: the rules in 7.6 (17) are clear.
6347
6348 if Comes_From_Source (Parent_Node)
6349 and then Parent_Kind = N_Object_Declaration
6350 and then Present (Expression (Parent_Node))
6351 and then not
6352 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
6353 and then not Is_Bit_Packed_Array (Typ)
6354 and then not Restriction_Check_Required (SPARK_05)
6355 then
6356 In_Place_Assign_OK_For_Declaration := True;
6357 Tmp := Defining_Identifier (Parent_Node);
6358 Set_No_Initialization (Parent_Node);
6359 Set_Expression (Parent_Node, Empty);
6360
6361 -- Set kind and type of the entity, for use in the analysis
6362 -- of the subsequent assignments. If the nominal type is not
6363 -- constrained, build a subtype from the known bounds of the
6364 -- aggregate. If the declaration has a subtype mark, use it,
6365 -- otherwise use the itype of the aggregate.
6366
6367 Set_Ekind (Tmp, E_Variable);
6368
6369 if not Is_Constrained (Typ) then
6370 Build_Constrained_Type (Positional => False);
6371
6372 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6373 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6374 then
6375 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6376
6377 else
6378 Set_Size_Known_At_Compile_Time (Typ, False);
6379 Set_Etype (Tmp, Typ);
6380 end if;
6381
6382 elsif Maybe_In_Place_OK
6383 and then Nkind (Parent (N)) = N_Qualified_Expression
6384 and then Nkind (Parent (Parent (N))) = N_Allocator
6385 then
6386 Set_Expansion_Delayed (N);
6387 return;
6388
6389 -- Limited arrays in return statements are expanded when
6390 -- enclosing construct is expanded.
6391
6392 elsif Maybe_In_Place_OK
6393 and then Nkind (Parent (N)) = N_Simple_Return_Statement
6394 then
6395 Set_Expansion_Delayed (N);
6396 return;
6397
6398 -- In the remaining cases the aggregate is the RHS of an assignment
6399
6400 elsif Maybe_In_Place_OK
6401 and then Safe_Left_Hand_Side (Name (Parent (N)))
6402 then
6403 Tmp := Name (Parent (N));
6404
6405 if Etype (Tmp) /= Etype (N) then
6406 Apply_Length_Check (N, Etype (Tmp));
6407
6408 if Nkind (N) = N_Raise_Constraint_Error then
6409
6410 -- Static error, nothing further to expand
6411
6412 return;
6413 end if;
6414 end if;
6415
6416 -- If a slice assignment has an aggregate with a single others_choice,
6417 -- the assignment can be done in place even if bounds are not static,
6418 -- by converting it into a loop over the discrete range of the slice.
6419
6420 elsif Maybe_In_Place_OK
6421 and then Nkind (Name (Parent (N))) = N_Slice
6422 and then Is_Others_Aggregate (N)
6423 then
6424 Tmp := Name (Parent (N));
6425
6426 -- Set type of aggregate to be type of lhs in assignment, in order
6427 -- to suppress redundant length checks.
6428
6429 Set_Etype (N, Etype (Tmp));
6430
6431 -- Step 5
6432
6433 -- In-place aggregate expansion is not possible
6434
6435 else
6436 Maybe_In_Place_OK := False;
6437 Tmp := Make_Temporary (Loc, 'A', N);
6438 Tmp_Decl :=
6439 Make_Object_Declaration (Loc,
6440 Defining_Identifier => Tmp,
6441 Object_Definition => New_Occurrence_Of (Typ, Loc));
6442 Set_No_Initialization (Tmp_Decl, True);
6443 Set_Warnings_Off (Tmp);
6444
6445 -- If we are within a loop, the temporary will be pushed on the
6446 -- stack at each iteration. If the aggregate is the expression
6447 -- for an allocator, it will be immediately copied to the heap
6448 -- and can be reclaimed at once. We create a transient scope
6449 -- around the aggregate for this purpose.
6450
6451 if Ekind (Current_Scope) = E_Loop
6452 and then Nkind (Parent (Parent (N))) = N_Allocator
6453 then
6454 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6455 end if;
6456
6457 Insert_Action (N, Tmp_Decl);
6458 end if;
6459
6460 -- Construct and insert the aggregate code. We can safely suppress index
6461 -- checks because this code is guaranteed not to raise CE on index
6462 -- checks. However we should *not* suppress all checks.
6463
6464 declare
6465 Target : Node_Id;
6466
6467 begin
6468 if Nkind (Tmp) = N_Defining_Identifier then
6469 Target := New_Occurrence_Of (Tmp, Loc);
6470
6471 else
6472 if Has_Default_Init_Comps (N)
6473 and then not Maybe_In_Place_OK
6474 then
6475 -- Ada 2005 (AI-287): This case has not been analyzed???
6476
6477 raise Program_Error;
6478 end if;
6479
6480 -- Name in assignment is explicit dereference
6481
6482 Target := New_Copy (Tmp);
6483 end if;
6484
6485 -- If we are to generate an in-place assignment for a declaration or
6486 -- an assignment statement, and the assignment can be done directly
6487 -- by the back end, then do not expand further.
6488
6489 -- ??? We can also do that if in-place expansion is not possible but
6490 -- then we could go into an infinite recursion.
6491
6492 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6493 and then not CodePeer_Mode
6494 and then not Modify_Tree_For_C
6495 and then not Possible_Bit_Aligned_Component (Target)
6496 and then not Is_Possibly_Unaligned_Slice (Target)
6497 and then Aggr_Assignment_OK_For_Backend (N)
6498 then
6499 if Maybe_In_Place_OK then
6500 return;
6501 end if;
6502
6503 Aggr_Code :=
6504 New_List (
6505 Make_Assignment_Statement (Loc,
6506 Name => Target,
6507 Expression => New_Copy_Tree (N)));
6508
6509 else
6510 Aggr_Code :=
6511 Build_Array_Aggr_Code (N,
6512 Ctype => Ctyp,
6513 Index => First_Index (Typ),
6514 Into => Target,
6515 Scalar_Comp => Is_Scalar_Type (Ctyp));
6516 end if;
6517
6518 -- Save the last assignment statement associated with the aggregate
6519 -- when building a controlled object. This reference is utilized by
6520 -- the finalization machinery when marking an object as successfully
6521 -- initialized.
6522
6523 if Needs_Finalization (Typ)
6524 and then Is_Entity_Name (Target)
6525 and then Present (Entity (Target))
6526 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6527 then
6528 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6529 end if;
6530 end;
6531
6532 -- If the aggregate is the expression in a declaration, the expanded
6533 -- code must be inserted after it. The defining entity might not come
6534 -- from source if this is part of an inlined body, but the declaration
6535 -- itself will.
6536
6537 if Comes_From_Source (Tmp)
6538 or else
6539 (Nkind (Parent (N)) = N_Object_Declaration
6540 and then Comes_From_Source (Parent (N))
6541 and then Tmp = Defining_Entity (Parent (N)))
6542 then
6543 declare
6544 Node_After : constant Node_Id := Next (Parent_Node);
6545
6546 begin
6547 Insert_Actions_After (Parent_Node, Aggr_Code);
6548
6549 if Parent_Kind = N_Object_Declaration then
6550 Collect_Initialization_Statements
6551 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
6552 end if;
6553 end;
6554
6555 else
6556 Insert_Actions (N, Aggr_Code);
6557 end if;
6558
6559 -- If the aggregate has been assigned in place, remove the original
6560 -- assignment.
6561
6562 if Nkind (Parent (N)) = N_Assignment_Statement
6563 and then Maybe_In_Place_OK
6564 then
6565 Rewrite (Parent (N), Make_Null_Statement (Loc));
6566
6567 elsif Nkind (Parent (N)) /= N_Object_Declaration
6568 or else Tmp /= Defining_Identifier (Parent (N))
6569 then
6570 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6571 Analyze_And_Resolve (N, Typ);
6572 end if;
6573 end Expand_Array_Aggregate;
6574
6575 ------------------------
6576 -- Expand_N_Aggregate --
6577 ------------------------
6578
6579 procedure Expand_N_Aggregate (N : Node_Id) is
6580 begin
6581 -- Record aggregate case
6582
6583 if Is_Record_Type (Etype (N)) then
6584 Expand_Record_Aggregate (N);
6585
6586 -- Array aggregate case
6587
6588 else
6589 -- A special case, if we have a string subtype with bounds 1 .. N,
6590 -- where N is known at compile time, and the aggregate is of the
6591 -- form (others => 'x'), with a single choice and no expressions,
6592 -- and N is less than 80 (an arbitrary limit for now), then replace
6593 -- the aggregate by the equivalent string literal (but do not mark
6594 -- it as static since it is not).
6595
6596 -- Note: this entire circuit is redundant with respect to code in
6597 -- Expand_Array_Aggregate that collapses others choices to positional
6598 -- form, but there are two problems with that circuit:
6599
6600 -- a) It is limited to very small cases due to ill-understood
6601 -- interactions with bootstrapping. That limit is removed by
6602 -- use of the No_Implicit_Loops restriction.
6603
6604 -- b) It incorrectly ends up with the resulting expressions being
6605 -- considered static when they are not. For example, the
6606 -- following test should fail:
6607
6608 -- pragma Restrictions (No_Implicit_Loops);
6609 -- package NonSOthers4 is
6610 -- B : constant String (1 .. 6) := (others => 'A');
6611 -- DH : constant String (1 .. 8) := B & "BB";
6612 -- X : Integer;
6613 -- pragma Export (C, X, Link_Name => DH);
6614 -- end;
6615
6616 -- But it succeeds (DH looks static to pragma Export)
6617
6618 -- To be sorted out ???
6619
6620 if Present (Component_Associations (N)) then
6621 declare
6622 CA : constant Node_Id := First (Component_Associations (N));
6623 MX : constant := 80;
6624
6625 begin
6626 if Nkind (First (Choice_List (CA))) = N_Others_Choice
6627 and then Nkind (Expression (CA)) = N_Character_Literal
6628 and then No (Expressions (N))
6629 then
6630 declare
6631 T : constant Entity_Id := Etype (N);
6632 X : constant Node_Id := First_Index (T);
6633 EC : constant Node_Id := Expression (CA);
6634 CV : constant Uint := Char_Literal_Value (EC);
6635 CC : constant Int := UI_To_Int (CV);
6636
6637 begin
6638 if Nkind (X) = N_Range
6639 and then Compile_Time_Known_Value (Low_Bound (X))
6640 and then Expr_Value (Low_Bound (X)) = 1
6641 and then Compile_Time_Known_Value (High_Bound (X))
6642 then
6643 declare
6644 Hi : constant Uint := Expr_Value (High_Bound (X));
6645
6646 begin
6647 if Hi <= MX then
6648 Start_String;
6649
6650 for J in 1 .. UI_To_Int (Hi) loop
6651 Store_String_Char (Char_Code (CC));
6652 end loop;
6653
6654 Rewrite (N,
6655 Make_String_Literal (Sloc (N),
6656 Strval => End_String));
6657
6658 if CC >= Int (2 ** 16) then
6659 Set_Has_Wide_Wide_Character (N);
6660 elsif CC >= Int (2 ** 8) then
6661 Set_Has_Wide_Character (N);
6662 end if;
6663
6664 Analyze_And_Resolve (N, T);
6665 Set_Is_Static_Expression (N, False);
6666 return;
6667 end if;
6668 end;
6669 end if;
6670 end;
6671 end if;
6672 end;
6673 end if;
6674
6675 -- Not that special case, so normal expansion of array aggregate
6676
6677 Expand_Array_Aggregate (N);
6678 end if;
6679
6680 exception
6681 when RE_Not_Available =>
6682 return;
6683 end Expand_N_Aggregate;
6684
6685 ------------------------------
6686 -- Expand_N_Delta_Aggregate --
6687 ------------------------------
6688
6689 procedure Expand_N_Delta_Aggregate (N : Node_Id) is
6690 Loc : constant Source_Ptr := Sloc (N);
6691 Typ : constant Entity_Id := Etype (N);
6692 Decl : Node_Id;
6693
6694 begin
6695 Decl :=
6696 Make_Object_Declaration (Loc,
6697 Defining_Identifier => Make_Temporary (Loc, 'T'),
6698 Object_Definition => New_Occurrence_Of (Typ, Loc),
6699 Expression => New_Copy_Tree (Expression (N)));
6700
6701 if Is_Array_Type (Etype (N)) then
6702 Expand_Delta_Array_Aggregate (N, New_List (Decl));
6703 else
6704 Expand_Delta_Record_Aggregate (N, New_List (Decl));
6705 end if;
6706 end Expand_N_Delta_Aggregate;
6707
6708 ----------------------------------
6709 -- Expand_Delta_Array_Aggregate --
6710 ----------------------------------
6711
6712 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is
6713 Loc : constant Source_Ptr := Sloc (N);
6714 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6715 Assoc : Node_Id;
6716
6717 function Generate_Loop (C : Node_Id) return Node_Id;
6718 -- Generate a loop containing individual component assignments for
6719 -- choices that are ranges, subtype indications, subtype names, and
6720 -- iterated component associations.
6721
6722 -------------------
6723 -- Generate_Loop --
6724 -------------------
6725
6726 function Generate_Loop (C : Node_Id) return Node_Id is
6727 Sl : constant Source_Ptr := Sloc (C);
6728 Ix : Entity_Id;
6729
6730 begin
6731 if Nkind (Parent (C)) = N_Iterated_Component_Association then
6732 Ix :=
6733 Make_Defining_Identifier (Loc,
6734 Chars => (Chars (Defining_Identifier (Parent (C)))));
6735 else
6736 Ix := Make_Temporary (Sl, 'I');
6737 end if;
6738
6739 return
6740 Make_Loop_Statement (Loc,
6741 Iteration_Scheme =>
6742 Make_Iteration_Scheme (Sl,
6743 Loop_Parameter_Specification =>
6744 Make_Loop_Parameter_Specification (Sl,
6745 Defining_Identifier => Ix,
6746 Discrete_Subtype_Definition => New_Copy_Tree (C))),
6747
6748 Statements => New_List (
6749 Make_Assignment_Statement (Sl,
6750 Name =>
6751 Make_Indexed_Component (Sl,
6752 Prefix => New_Occurrence_Of (Temp, Sl),
6753 Expressions => New_List (New_Occurrence_Of (Ix, Sl))),
6754 Expression => New_Copy_Tree (Expression (Assoc)))),
6755 End_Label => Empty);
6756 end Generate_Loop;
6757
6758 -- Local variables
6759
6760 Choice : Node_Id;
6761
6762 -- Start of processing for Expand_Delta_Array_Aggregate
6763
6764 begin
6765 Assoc := First (Component_Associations (N));
6766 while Present (Assoc) loop
6767 Choice := First (Choice_List (Assoc));
6768 if Nkind (Assoc) = N_Iterated_Component_Association then
6769 while Present (Choice) loop
6770 Append_To (Deltas, Generate_Loop (Choice));
6771 Next (Choice);
6772 end loop;
6773
6774 else
6775 while Present (Choice) loop
6776
6777 -- Choice can be given by a range, a subtype indication, a
6778 -- subtype name, a scalar value, or an entity.
6779
6780 if Nkind (Choice) = N_Range
6781 or else (Is_Entity_Name (Choice)
6782 and then Is_Type (Entity (Choice)))
6783 then
6784 Append_To (Deltas, Generate_Loop (Choice));
6785
6786 elsif Nkind (Choice) = N_Subtype_Indication then
6787 Append_To (Deltas,
6788 Generate_Loop (Range_Expression (Constraint (Choice))));
6789
6790 else
6791 Append_To (Deltas,
6792 Make_Assignment_Statement (Sloc (Choice),
6793 Name =>
6794 Make_Indexed_Component (Sloc (Choice),
6795 Prefix => New_Occurrence_Of (Temp, Loc),
6796 Expressions => New_List (New_Copy_Tree (Choice))),
6797 Expression => New_Copy_Tree (Expression (Assoc))));
6798 end if;
6799
6800 Next (Choice);
6801 end loop;
6802 end if;
6803
6804 Next (Assoc);
6805 end loop;
6806
6807 Insert_Actions (N, Deltas);
6808 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6809 end Expand_Delta_Array_Aggregate;
6810
6811 -----------------------------------
6812 -- Expand_Delta_Record_Aggregate --
6813 -----------------------------------
6814
6815 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is
6816 Loc : constant Source_Ptr := Sloc (N);
6817 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6818 Assoc : Node_Id;
6819 Choice : Node_Id;
6820
6821 begin
6822 Assoc := First (Component_Associations (N));
6823
6824 while Present (Assoc) loop
6825 Choice := First (Choice_List (Assoc));
6826 while Present (Choice) loop
6827 Append_To (Deltas,
6828 Make_Assignment_Statement (Sloc (Choice),
6829 Name =>
6830 Make_Selected_Component (Sloc (Choice),
6831 Prefix => New_Occurrence_Of (Temp, Loc),
6832 Selector_Name => Make_Identifier (Loc, Chars (Choice))),
6833 Expression => New_Copy_Tree (Expression (Assoc))));
6834 Next (Choice);
6835 end loop;
6836
6837 Next (Assoc);
6838 end loop;
6839
6840 Insert_Actions (N, Deltas);
6841 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6842 end Expand_Delta_Record_Aggregate;
6843
6844 ----------------------------------
6845 -- Expand_N_Extension_Aggregate --
6846 ----------------------------------
6847
6848 -- If the ancestor part is an expression, add a component association for
6849 -- the parent field. If the type of the ancestor part is not the direct
6850 -- parent of the expected type, build recursively the needed ancestors.
6851 -- If the ancestor part is a subtype_mark, replace aggregate with a
6852 -- declaration for a temporary of the expected type, followed by
6853 -- individual assignments to the given components.
6854
6855 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
6856 A : constant Node_Id := Ancestor_Part (N);
6857 Loc : constant Source_Ptr := Sloc (N);
6858 Typ : constant Entity_Id := Etype (N);
6859
6860 begin
6861 -- If the ancestor is a subtype mark, an init proc must be called
6862 -- on the resulting object which thus has to be materialized in
6863 -- the front-end
6864
6865 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
6866 Convert_To_Assignments (N, Typ);
6867
6868 -- The extension aggregate is transformed into a record aggregate
6869 -- of the following form (c1 and c2 are inherited components)
6870
6871 -- (Exp with c3 => a, c4 => b)
6872 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6873
6874 else
6875 Set_Etype (N, Typ);
6876
6877 if Tagged_Type_Expansion then
6878 Expand_Record_Aggregate (N,
6879 Orig_Tag =>
6880 New_Occurrence_Of
6881 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
6882 Parent_Expr => A);
6883
6884 -- No tag is needed in the case of a VM
6885
6886 else
6887 Expand_Record_Aggregate (N, Parent_Expr => A);
6888 end if;
6889 end if;
6890
6891 exception
6892 when RE_Not_Available =>
6893 return;
6894 end Expand_N_Extension_Aggregate;
6895
6896 -----------------------------
6897 -- Expand_Record_Aggregate --
6898 -----------------------------
6899
6900 procedure Expand_Record_Aggregate
6901 (N : Node_Id;
6902 Orig_Tag : Node_Id := Empty;
6903 Parent_Expr : Node_Id := Empty)
6904 is
6905 Loc : constant Source_Ptr := Sloc (N);
6906 Comps : constant List_Id := Component_Associations (N);
6907 Typ : constant Entity_Id := Etype (N);
6908 Base_Typ : constant Entity_Id := Base_Type (Typ);
6909
6910 Static_Components : Boolean := True;
6911 -- Flag to indicate whether all components are compile-time known,
6912 -- and the aggregate can be constructed statically and handled by
6913 -- the back-end. Set to False by Component_OK_For_Backend.
6914
6915 procedure Build_Back_End_Aggregate;
6916 -- Build a proper aggregate to be handled by the back-end
6917
6918 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
6919 -- Returns true if N is an expression of composite type which can be
6920 -- fully evaluated at compile time without raising constraint error.
6921 -- Such expressions can be passed as is to Gigi without any expansion.
6922 --
6923 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6924 -- set and constants whose expression is such an aggregate, recursively.
6925
6926 function Component_OK_For_Backend return Boolean;
6927 -- Check for presence of a component which makes it impossible for the
6928 -- backend to process the aggregate, thus requiring the use of a series
6929 -- of assignment statements. Cases checked for are a nested aggregate
6930 -- needing Late_Expansion, the presence of a tagged component which may
6931 -- need tag adjustment, and a bit unaligned component reference.
6932 --
6933 -- We also force expansion into assignments if a component is of a
6934 -- mutable type (including a private type with discriminants) because
6935 -- in that case the size of the component to be copied may be smaller
6936 -- than the side of the target, and there is no simple way for gigi
6937 -- to compute the size of the object to be copied.
6938 --
6939 -- NOTE: This is part of the ongoing work to define precisely the
6940 -- interface between front-end and back-end handling of aggregates.
6941 -- In general it is desirable to pass aggregates as they are to gigi,
6942 -- in order to minimize elaboration code. This is one case where the
6943 -- semantics of Ada complicate the analysis and lead to anomalies in
6944 -- the gcc back-end if the aggregate is not expanded into assignments.
6945 --
6946 -- NOTE: This sets the global Static_Components to False in most, but
6947 -- not all, cases when it returns False.
6948
6949 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
6950 -- Return True if any element of L has Has_Per_Object_Constraint set.
6951 -- L should be the Choices component of an N_Component_Association.
6952
6953 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
6954 -- If any ancestor of the current type is private, the aggregate
6955 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6956 -- because it will not be set when type and its parent are in the
6957 -- same scope, and the parent component needs expansion.
6958
6959 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
6960 -- For nested aggregates return the ultimate enclosing aggregate; for
6961 -- non-nested aggregates return N.
6962
6963 ------------------------------
6964 -- Build_Back_End_Aggregate --
6965 ------------------------------
6966
6967 procedure Build_Back_End_Aggregate is
6968 Comp : Entity_Id;
6969 New_Comp : Node_Id;
6970 Tag_Value : Node_Id;
6971
6972 begin
6973 if Nkind (N) = N_Aggregate then
6974
6975 -- If the aggregate is static and can be handled by the back-end,
6976 -- nothing left to do.
6977
6978 if Static_Components then
6979 Set_Compile_Time_Known_Aggregate (N);
6980 Set_Expansion_Delayed (N, False);
6981 end if;
6982 end if;
6983
6984 -- If no discriminants, nothing special to do
6985
6986 if not Has_Discriminants (Typ) then
6987 null;
6988
6989 -- Case of discriminants present
6990
6991 elsif Is_Derived_Type (Typ) then
6992
6993 -- For untagged types, non-stored discriminants are replaced with
6994 -- stored discriminants, which are the ones that gigi uses to
6995 -- describe the type and its components.
6996
6997 Generate_Aggregate_For_Derived_Type : declare
6998 procedure Prepend_Stored_Values (T : Entity_Id);
6999 -- Scan the list of stored discriminants of the type, and add
7000 -- their values to the aggregate being built.
7001
7002 ---------------------------
7003 -- Prepend_Stored_Values --
7004 ---------------------------
7005
7006 procedure Prepend_Stored_Values (T : Entity_Id) is
7007 Discr : Entity_Id;
7008 First_Comp : Node_Id := Empty;
7009
7010 begin
7011 Discr := First_Stored_Discriminant (T);
7012 while Present (Discr) loop
7013 New_Comp :=
7014 Make_Component_Association (Loc,
7015 Choices => New_List (
7016 New_Occurrence_Of (Discr, Loc)),
7017 Expression =>
7018 New_Copy_Tree
7019 (Get_Discriminant_Value
7020 (Discr,
7021 Typ,
7022 Discriminant_Constraint (Typ))));
7023
7024 if No (First_Comp) then
7025 Prepend_To (Component_Associations (N), New_Comp);
7026 else
7027 Insert_After (First_Comp, New_Comp);
7028 end if;
7029
7030 First_Comp := New_Comp;
7031 Next_Stored_Discriminant (Discr);
7032 end loop;
7033 end Prepend_Stored_Values;
7034
7035 -- Local variables
7036
7037 Constraints : constant List_Id := New_List;
7038
7039 Discr : Entity_Id;
7040 Decl : Node_Id;
7041 Num_Disc : Nat := 0;
7042 Num_Gird : Nat := 0;
7043
7044 -- Start of processing for Generate_Aggregate_For_Derived_Type
7045
7046 begin
7047 -- Remove the associations for the discriminant of derived type
7048
7049 declare
7050 First_Comp : Node_Id;
7051
7052 begin
7053 First_Comp := First (Component_Associations (N));
7054 while Present (First_Comp) loop
7055 Comp := First_Comp;
7056 Next (First_Comp);
7057
7058 if Ekind (Entity (First (Choices (Comp)))) =
7059 E_Discriminant
7060 then
7061 Remove (Comp);
7062 Num_Disc := Num_Disc + 1;
7063 end if;
7064 end loop;
7065 end;
7066
7067 -- Insert stored discriminant associations in the correct
7068 -- order. If there are more stored discriminants than new
7069 -- discriminants, there is at least one new discriminant that
7070 -- constrains more than one of the stored discriminants. In
7071 -- this case we need to construct a proper subtype of the
7072 -- parent type, in order to supply values to all the
7073 -- components. Otherwise there is one-one correspondence
7074 -- between the constraints and the stored discriminants.
7075
7076 Discr := First_Stored_Discriminant (Base_Type (Typ));
7077 while Present (Discr) loop
7078 Num_Gird := Num_Gird + 1;
7079 Next_Stored_Discriminant (Discr);
7080 end loop;
7081
7082 -- Case of more stored discriminants than new discriminants
7083
7084 if Num_Gird > Num_Disc then
7085
7086 -- Create a proper subtype of the parent type, which is the
7087 -- proper implementation type for the aggregate, and convert
7088 -- it to the intended target type.
7089
7090 Discr := First_Stored_Discriminant (Base_Type (Typ));
7091 while Present (Discr) loop
7092 New_Comp :=
7093 New_Copy_Tree
7094 (Get_Discriminant_Value
7095 (Discr,
7096 Typ,
7097 Discriminant_Constraint (Typ)));
7098
7099 Append (New_Comp, Constraints);
7100 Next_Stored_Discriminant (Discr);
7101 end loop;
7102
7103 Decl :=
7104 Make_Subtype_Declaration (Loc,
7105 Defining_Identifier => Make_Temporary (Loc, 'T'),
7106 Subtype_Indication =>
7107 Make_Subtype_Indication (Loc,
7108 Subtype_Mark =>
7109 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
7110 Constraint =>
7111 Make_Index_Or_Discriminant_Constraint
7112 (Loc, Constraints)));
7113
7114 Insert_Action (N, Decl);
7115 Prepend_Stored_Values (Base_Type (Typ));
7116
7117 Set_Etype (N, Defining_Identifier (Decl));
7118 Set_Analyzed (N);
7119
7120 Rewrite (N, Unchecked_Convert_To (Typ, N));
7121 Analyze (N);
7122
7123 -- Case where we do not have fewer new discriminants than
7124 -- stored discriminants, so in this case we can simply use the
7125 -- stored discriminants of the subtype.
7126
7127 else
7128 Prepend_Stored_Values (Typ);
7129 end if;
7130 end Generate_Aggregate_For_Derived_Type;
7131 end if;
7132
7133 if Is_Tagged_Type (Typ) then
7134
7135 -- In the tagged case, _parent and _tag component must be created
7136
7137 -- Reset Null_Present unconditionally. Tagged records always have
7138 -- at least one field (the tag or the parent).
7139
7140 Set_Null_Record_Present (N, False);
7141
7142 -- When the current aggregate comes from the expansion of an
7143 -- extension aggregate, the parent expr is replaced by an
7144 -- aggregate formed by selected components of this expr.
7145
7146 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
7147 Comp := First_Component_Or_Discriminant (Typ);
7148 while Present (Comp) loop
7149
7150 -- Skip all expander-generated components
7151
7152 if not Comes_From_Source (Original_Record_Component (Comp))
7153 then
7154 null;
7155
7156 else
7157 New_Comp :=
7158 Make_Selected_Component (Loc,
7159 Prefix =>
7160 Unchecked_Convert_To (Typ,
7161 Duplicate_Subexpr (Parent_Expr, True)),
7162 Selector_Name => New_Occurrence_Of (Comp, Loc));
7163
7164 Append_To (Comps,
7165 Make_Component_Association (Loc,
7166 Choices => New_List (
7167 New_Occurrence_Of (Comp, Loc)),
7168 Expression => New_Comp));
7169
7170 Analyze_And_Resolve (New_Comp, Etype (Comp));
7171 end if;
7172
7173 Next_Component_Or_Discriminant (Comp);
7174 end loop;
7175 end if;
7176
7177 -- Compute the value for the Tag now, if the type is a root it
7178 -- will be included in the aggregate right away, otherwise it will
7179 -- be propagated to the parent aggregate.
7180
7181 if Present (Orig_Tag) then
7182 Tag_Value := Orig_Tag;
7183
7184 elsif not Tagged_Type_Expansion then
7185 Tag_Value := Empty;
7186
7187 else
7188 Tag_Value :=
7189 New_Occurrence_Of
7190 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
7191 end if;
7192
7193 -- For a derived type, an aggregate for the parent is formed with
7194 -- all the inherited components.
7195
7196 if Is_Derived_Type (Typ) then
7197 declare
7198 First_Comp : Node_Id;
7199 Parent_Comps : List_Id;
7200 Parent_Aggr : Node_Id;
7201 Parent_Name : Node_Id;
7202
7203 begin
7204 -- Remove the inherited component association from the
7205 -- aggregate and store them in the parent aggregate
7206
7207 First_Comp := First (Component_Associations (N));
7208 Parent_Comps := New_List;
7209 while Present (First_Comp)
7210 and then
7211 Scope (Original_Record_Component
7212 (Entity (First (Choices (First_Comp))))) /=
7213 Base_Typ
7214 loop
7215 Comp := First_Comp;
7216 Next (First_Comp);
7217 Remove (Comp);
7218 Append (Comp, Parent_Comps);
7219 end loop;
7220
7221 Parent_Aggr :=
7222 Make_Aggregate (Loc,
7223 Component_Associations => Parent_Comps);
7224 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
7225
7226 -- Find the _parent component
7227
7228 Comp := First_Component (Typ);
7229 while Chars (Comp) /= Name_uParent loop
7230 Comp := Next_Component (Comp);
7231 end loop;
7232
7233 Parent_Name := New_Occurrence_Of (Comp, Loc);
7234
7235 -- Insert the parent aggregate
7236
7237 Prepend_To (Component_Associations (N),
7238 Make_Component_Association (Loc,
7239 Choices => New_List (Parent_Name),
7240 Expression => Parent_Aggr));
7241
7242 -- Expand recursively the parent propagating the right Tag
7243
7244 Expand_Record_Aggregate
7245 (Parent_Aggr, Tag_Value, Parent_Expr);
7246
7247 -- The ancestor part may be a nested aggregate that has
7248 -- delayed expansion: recheck now.
7249
7250 if not Component_OK_For_Backend then
7251 Convert_To_Assignments (N, Typ);
7252 end if;
7253 end;
7254
7255 -- For a root type, the tag component is added (unless compiling
7256 -- for the VMs, where tags are implicit).
7257
7258 elsif Tagged_Type_Expansion then
7259 declare
7260 Tag_Name : constant Node_Id :=
7261 New_Occurrence_Of
7262 (First_Tag_Component (Typ), Loc);
7263 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7264 Conv_Node : constant Node_Id :=
7265 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7266
7267 begin
7268 Set_Etype (Conv_Node, Typ_Tag);
7269 Prepend_To (Component_Associations (N),
7270 Make_Component_Association (Loc,
7271 Choices => New_List (Tag_Name),
7272 Expression => Conv_Node));
7273 end;
7274 end if;
7275 end if;
7276 end Build_Back_End_Aggregate;
7277
7278 ----------------------------------------
7279 -- Compile_Time_Known_Composite_Value --
7280 ----------------------------------------
7281
7282 function Compile_Time_Known_Composite_Value
7283 (N : Node_Id) return Boolean
7284 is
7285 begin
7286 -- If we have an entity name, then see if it is the name of a
7287 -- constant and if so, test the corresponding constant value.
7288
7289 if Is_Entity_Name (N) then
7290 declare
7291 E : constant Entity_Id := Entity (N);
7292 V : Node_Id;
7293 begin
7294 if Ekind (E) /= E_Constant then
7295 return False;
7296 else
7297 V := Constant_Value (E);
7298 return Present (V)
7299 and then Compile_Time_Known_Composite_Value (V);
7300 end if;
7301 end;
7302
7303 -- We have a value, see if it is compile time known
7304
7305 else
7306 if Nkind (N) = N_Aggregate then
7307 return Compile_Time_Known_Aggregate (N);
7308 end if;
7309
7310 -- All other types of values are not known at compile time
7311
7312 return False;
7313 end if;
7314
7315 end Compile_Time_Known_Composite_Value;
7316
7317 ------------------------------
7318 -- Component_OK_For_Backend --
7319 ------------------------------
7320
7321 function Component_OK_For_Backend return Boolean is
7322 C : Node_Id;
7323 Expr_Q : Node_Id;
7324
7325 begin
7326 if No (Comps) then
7327 return True;
7328 end if;
7329
7330 C := First (Comps);
7331 while Present (C) loop
7332
7333 -- If the component has box initialization, expansion is needed
7334 -- and component is not ready for backend.
7335
7336 if Box_Present (C) then
7337 return False;
7338 end if;
7339
7340 if Nkind (Expression (C)) = N_Qualified_Expression then
7341 Expr_Q := Expression (Expression (C));
7342 else
7343 Expr_Q := Expression (C);
7344 end if;
7345
7346 -- Return False for array components whose bounds raise
7347 -- constraint error.
7348
7349 declare
7350 Comp : constant Entity_Id := First (Choices (C));
7351 Indx : Node_Id;
7352
7353 begin
7354 if Present (Etype (Comp))
7355 and then Is_Array_Type (Etype (Comp))
7356 then
7357 Indx := First_Index (Etype (Comp));
7358 while Present (Indx) loop
7359 if Nkind (Type_Low_Bound (Etype (Indx))) =
7360 N_Raise_Constraint_Error
7361 or else Nkind (Type_High_Bound (Etype (Indx))) =
7362 N_Raise_Constraint_Error
7363 then
7364 return False;
7365 end if;
7366
7367 Indx := Next_Index (Indx);
7368 end loop;
7369 end if;
7370 end;
7371
7372 -- Return False if the aggregate has any associations for tagged
7373 -- components that may require tag adjustment.
7374
7375 -- These are cases where the source expression may have a tag that
7376 -- could differ from the component tag (e.g., can occur for type
7377 -- conversions and formal parameters). (Tag adjustment not needed
7378 -- if Tagged_Type_Expansion because object tags are implicit in
7379 -- the machine.)
7380
7381 if Is_Tagged_Type (Etype (Expr_Q))
7382 and then
7383 (Nkind (Expr_Q) = N_Type_Conversion
7384 or else
7385 (Is_Entity_Name (Expr_Q)
7386 and then Is_Formal (Entity (Expr_Q))))
7387 and then Tagged_Type_Expansion
7388 then
7389 Static_Components := False;
7390 return False;
7391
7392 elsif Is_Delayed_Aggregate (Expr_Q) then
7393 Static_Components := False;
7394 return False;
7395
7396 elsif Nkind (Expr_Q) = N_Quantified_Expression then
7397 Static_Components := False;
7398 return False;
7399
7400 elsif Possible_Bit_Aligned_Component (Expr_Q) then
7401 Static_Components := False;
7402 return False;
7403
7404 elsif Modify_Tree_For_C
7405 and then Nkind (C) = N_Component_Association
7406 and then Has_Per_Object_Constraint (Choices (C))
7407 then
7408 Static_Components := False;
7409 return False;
7410
7411 elsif Modify_Tree_For_C
7412 and then Nkind (Expr_Q) = N_Identifier
7413 and then Is_Array_Type (Etype (Expr_Q))
7414 then
7415 Static_Components := False;
7416 return False;
7417
7418 elsif Modify_Tree_For_C
7419 and then Nkind (Expr_Q) = N_Type_Conversion
7420 and then Is_Array_Type (Etype (Expr_Q))
7421 then
7422 Static_Components := False;
7423 return False;
7424 end if;
7425
7426 if Is_Elementary_Type (Etype (Expr_Q)) then
7427 if not Compile_Time_Known_Value (Expr_Q) then
7428 Static_Components := False;
7429 end if;
7430
7431 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
7432 Static_Components := False;
7433
7434 if Is_Private_Type (Etype (Expr_Q))
7435 and then Has_Discriminants (Etype (Expr_Q))
7436 then
7437 return False;
7438 end if;
7439 end if;
7440
7441 Next (C);
7442 end loop;
7443
7444 return True;
7445 end Component_OK_For_Backend;
7446
7447 -------------------------------
7448 -- Has_Per_Object_Constraint --
7449 -------------------------------
7450
7451 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
7452 N : Node_Id := First (L);
7453 begin
7454 while Present (N) loop
7455 if Is_Entity_Name (N)
7456 and then Present (Entity (N))
7457 and then Has_Per_Object_Constraint (Entity (N))
7458 then
7459 return True;
7460 end if;
7461
7462 Next (N);
7463 end loop;
7464
7465 return False;
7466 end Has_Per_Object_Constraint;
7467
7468 -----------------------------------
7469 -- Has_Visible_Private_Ancestor --
7470 -----------------------------------
7471
7472 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
7473 R : constant Entity_Id := Root_Type (Id);
7474 T1 : Entity_Id := Id;
7475
7476 begin
7477 loop
7478 if Is_Private_Type (T1) then
7479 return True;
7480
7481 elsif T1 = R then
7482 return False;
7483
7484 else
7485 T1 := Etype (T1);
7486 end if;
7487 end loop;
7488 end Has_Visible_Private_Ancestor;
7489
7490 -------------------------
7491 -- Top_Level_Aggregate --
7492 -------------------------
7493
7494 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
7495 Aggr : Node_Id;
7496
7497 begin
7498 Aggr := N;
7499 while Present (Parent (Aggr))
7500 and then Nkind_In (Parent (Aggr), N_Aggregate,
7501 N_Component_Association)
7502 loop
7503 Aggr := Parent (Aggr);
7504 end loop;
7505
7506 return Aggr;
7507 end Top_Level_Aggregate;
7508
7509 -- Local variables
7510
7511 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
7512
7513 -- Start of processing for Expand_Record_Aggregate
7514
7515 begin
7516 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7517 -- to prevent a piecemeal assignment even if the aggregate is to be
7518 -- expanded. We create a temporary for the aggregate, and assign the
7519 -- temporary instead, so that the back end can generate an atomic move
7520 -- for it.
7521
7522 if Is_Atomic_VFA_Aggregate (N) then
7523 return;
7524
7525 -- No special management required for aggregates used to initialize
7526 -- statically allocated dispatch tables
7527
7528 elsif Is_Static_Dispatch_Table_Aggregate (N) then
7529 return;
7530 end if;
7531
7532 -- If the pragma Aggregate_Individually_Assign is set, always convert to
7533 -- assignments.
7534
7535 if Aggregate_Individually_Assign then
7536 Convert_To_Assignments (N, Typ);
7537
7538 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7539 -- are build-in-place function calls. The assignments will each turn
7540 -- into a build-in-place function call. If components are all static,
7541 -- we can pass the aggregate to the back end regardless of limitedness.
7542
7543 -- Extension aggregates, aggregates in extended return statements, and
7544 -- aggregates for C++ imported types must be expanded.
7545
7546 elsif Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
7547 if not Nkind_In (Parent (N), N_Component_Association,
7548 N_Object_Declaration)
7549 then
7550 Convert_To_Assignments (N, Typ);
7551
7552 elsif Nkind (N) = N_Extension_Aggregate
7553 or else Convention (Typ) = Convention_CPP
7554 then
7555 Convert_To_Assignments (N, Typ);
7556
7557 elsif not Size_Known_At_Compile_Time (Typ)
7558 or else not Component_OK_For_Backend
7559 or else not Static_Components
7560 then
7561 Convert_To_Assignments (N, Typ);
7562
7563 -- In all other cases, build a proper aggregate to be handled by
7564 -- the back-end
7565
7566 else
7567 Build_Back_End_Aggregate;
7568 end if;
7569
7570 -- Gigi doesn't properly handle temporaries of variable size so we
7571 -- generate it in the front-end
7572
7573 elsif not Size_Known_At_Compile_Time (Typ)
7574 and then Tagged_Type_Expansion
7575 then
7576 Convert_To_Assignments (N, Typ);
7577
7578 -- An aggregate used to initialize a controlled object must be turned
7579 -- into component assignments as the components themselves may require
7580 -- finalization actions such as adjustment.
7581
7582 elsif Needs_Finalization (Typ) then
7583 Convert_To_Assignments (N, Typ);
7584
7585 -- Ada 2005 (AI-287): In case of default initialized components we
7586 -- convert the aggregate into assignments.
7587
7588 elsif Has_Default_Init_Comps (N) then
7589 Convert_To_Assignments (N, Typ);
7590
7591 -- Check components
7592
7593 elsif not Component_OK_For_Backend then
7594 Convert_To_Assignments (N, Typ);
7595
7596 -- If an ancestor is private, some components are not inherited and we
7597 -- cannot expand into a record aggregate.
7598
7599 elsif Has_Visible_Private_Ancestor (Typ) then
7600 Convert_To_Assignments (N, Typ);
7601
7602 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7603 -- is not able to handle the aggregate for Late_Request.
7604
7605 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
7606 Convert_To_Assignments (N, Typ);
7607
7608 -- If the tagged types covers interface types we need to initialize all
7609 -- hidden components containing pointers to secondary dispatch tables.
7610
7611 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
7612 Convert_To_Assignments (N, Typ);
7613
7614 -- If some components are mutable, the size of the aggregate component
7615 -- may be distinct from the default size of the type component, so
7616 -- we need to expand to insure that the back-end copies the proper
7617 -- size of the data. However, if the aggregate is the initial value of
7618 -- a constant, the target is immutable and might be built statically
7619 -- if components are appropriate.
7620
7621 elsif Has_Mutable_Components (Typ)
7622 and then
7623 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
7624 or else not Constant_Present (Parent (Top_Level_Aggr))
7625 or else not Static_Components)
7626 then
7627 Convert_To_Assignments (N, Typ);
7628
7629 -- If the type involved has bit aligned components, then we are not sure
7630 -- that the back end can handle this case correctly.
7631
7632 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
7633 Convert_To_Assignments (N, Typ);
7634
7635 -- When generating C, only generate an aggregate when declaring objects
7636 -- since C does not support aggregates in e.g. assignment statements.
7637
7638 elsif Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then
7639 Convert_To_Assignments (N, Typ);
7640
7641 -- In all other cases, build a proper aggregate to be handled by gigi
7642
7643 else
7644 Build_Back_End_Aggregate;
7645 end if;
7646 end Expand_Record_Aggregate;
7647
7648 ----------------------------
7649 -- Has_Default_Init_Comps --
7650 ----------------------------
7651
7652 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
7653 Comps : constant List_Id := Component_Associations (N);
7654 C : Node_Id;
7655 Expr : Node_Id;
7656
7657 begin
7658 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
7659
7660 if No (Comps) then
7661 return False;
7662 end if;
7663
7664 if Has_Self_Reference (N) then
7665 return True;
7666 end if;
7667
7668 -- Check if any direct component has default initialized components
7669
7670 C := First (Comps);
7671 while Present (C) loop
7672 if Box_Present (C) then
7673 return True;
7674 end if;
7675
7676 Next (C);
7677 end loop;
7678
7679 -- Recursive call in case of aggregate expression
7680
7681 C := First (Comps);
7682 while Present (C) loop
7683 Expr := Expression (C);
7684
7685 if Present (Expr)
7686 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
7687 and then Has_Default_Init_Comps (Expr)
7688 then
7689 return True;
7690 end if;
7691
7692 Next (C);
7693 end loop;
7694
7695 return False;
7696 end Has_Default_Init_Comps;
7697
7698 ----------------------------------------
7699 -- Is_Build_In_Place_Aggregate_Return --
7700 ----------------------------------------
7701
7702 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean is
7703 P : Node_Id := Parent (N);
7704
7705 begin
7706 while Nkind (P) = N_Qualified_Expression loop
7707 P := Parent (P);
7708 end loop;
7709
7710 if Nkind (P) = N_Simple_Return_Statement then
7711 null;
7712
7713 elsif Nkind (Parent (P)) = N_Extended_Return_Statement then
7714 P := Parent (P);
7715
7716 else
7717 return False;
7718 end if;
7719
7720 return
7721 Is_Build_In_Place_Function
7722 (Return_Applies_To (Return_Statement_Entity (P)));
7723 end Is_Build_In_Place_Aggregate_Return;
7724
7725 --------------------------
7726 -- Is_Delayed_Aggregate --
7727 --------------------------
7728
7729 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
7730 Node : Node_Id := N;
7731 Kind : Node_Kind := Nkind (Node);
7732
7733 begin
7734 if Kind = N_Qualified_Expression then
7735 Node := Expression (Node);
7736 Kind := Nkind (Node);
7737 end if;
7738
7739 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
7740 return False;
7741 else
7742 return Expansion_Delayed (Node);
7743 end if;
7744 end Is_Delayed_Aggregate;
7745
7746 --------------------------------
7747 -- Is_CCG_Supported_Aggregate --
7748 --------------------------------
7749
7750 function Is_CCG_Supported_Aggregate
7751 (N : Node_Id) return Boolean
7752 is
7753 P : Node_Id := Parent (N);
7754
7755 begin
7756 -- Aggregates are not supported for nonstandard rep clauses, since they
7757 -- may lead to extra padding fields in CCG.
7758
7759 if Ekind (Etype (N)) in Record_Kind
7760 and then Has_Non_Standard_Rep (Etype (N))
7761 then
7762 return False;
7763 end if;
7764
7765 while Present (P) and then Nkind (P) = N_Aggregate loop
7766 P := Parent (P);
7767 end loop;
7768
7769 -- Check cases where aggregates are supported by the CCG backend
7770
7771 if Nkind (P) = N_Object_Declaration then
7772 declare
7773 P_Typ : constant Entity_Id := Etype (Defining_Identifier (P));
7774
7775 begin
7776 if Is_Record_Type (P_Typ) then
7777 return True;
7778 else
7779 return Compile_Time_Known_Bounds (P_Typ);
7780 end if;
7781 end;
7782
7783 elsif Nkind (P) = N_Qualified_Expression then
7784 if Nkind (Parent (P)) = N_Object_Declaration then
7785 declare
7786 P_Typ : constant Entity_Id :=
7787 Etype (Defining_Identifier (Parent (P)));
7788 begin
7789 if Is_Record_Type (P_Typ) then
7790 return True;
7791 else
7792 return Compile_Time_Known_Bounds (P_Typ);
7793 end if;
7794 end;
7795
7796 elsif Nkind (Parent (P)) = N_Allocator then
7797 return True;
7798 end if;
7799 end if;
7800
7801 return False;
7802 end Is_CCG_Supported_Aggregate;
7803
7804 ----------------------------------------
7805 -- Is_Static_Dispatch_Table_Aggregate --
7806 ----------------------------------------
7807
7808 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
7809 Typ : constant Entity_Id := Base_Type (Etype (N));
7810
7811 begin
7812 return Building_Static_Dispatch_Tables
7813 and then Tagged_Type_Expansion
7814 and then RTU_Loaded (Ada_Tags)
7815
7816 -- Avoid circularity when rebuilding the compiler
7817
7818 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
7819 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
7820 or else
7821 Typ = RTE (RE_Address_Array)
7822 or else
7823 Typ = RTE (RE_Type_Specific_Data)
7824 or else
7825 Typ = RTE (RE_Tag_Table)
7826 or else
7827 (RTE_Available (RE_Interface_Data)
7828 and then Typ = RTE (RE_Interface_Data))
7829 or else
7830 (RTE_Available (RE_Interfaces_Array)
7831 and then Typ = RTE (RE_Interfaces_Array))
7832 or else
7833 (RTE_Available (RE_Interface_Data_Element)
7834 and then Typ = RTE (RE_Interface_Data_Element)));
7835 end Is_Static_Dispatch_Table_Aggregate;
7836
7837 -----------------------------
7838 -- Is_Two_Dim_Packed_Array --
7839 -----------------------------
7840
7841 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
7842 C : constant Int := UI_To_Int (Component_Size (Typ));
7843 begin
7844 return Number_Dimensions (Typ) = 2
7845 and then Is_Bit_Packed_Array (Typ)
7846 and then (C = 1 or else C = 2 or else C = 4);
7847 end Is_Two_Dim_Packed_Array;
7848
7849 --------------------
7850 -- Late_Expansion --
7851 --------------------
7852
7853 function Late_Expansion
7854 (N : Node_Id;
7855 Typ : Entity_Id;
7856 Target : Node_Id) return List_Id
7857 is
7858 Aggr_Code : List_Id;
7859
7860 begin
7861 if Is_Array_Type (Etype (N)) then
7862 Aggr_Code :=
7863 Build_Array_Aggr_Code
7864 (N => N,
7865 Ctype => Component_Type (Etype (N)),
7866 Index => First_Index (Typ),
7867 Into => Target,
7868 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
7869 Indexes => No_List);
7870
7871 -- Directly or indirectly (e.g. access protected procedure) a record
7872
7873 else
7874 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
7875 end if;
7876
7877 -- Save the last assignment statement associated with the aggregate
7878 -- when building a controlled object. This reference is utilized by
7879 -- the finalization machinery when marking an object as successfully
7880 -- initialized.
7881
7882 if Needs_Finalization (Typ)
7883 and then Is_Entity_Name (Target)
7884 and then Present (Entity (Target))
7885 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
7886 then
7887 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
7888 end if;
7889
7890 return Aggr_Code;
7891 end Late_Expansion;
7892
7893 ----------------------------------
7894 -- Make_OK_Assignment_Statement --
7895 ----------------------------------
7896
7897 function Make_OK_Assignment_Statement
7898 (Sloc : Source_Ptr;
7899 Name : Node_Id;
7900 Expression : Node_Id) return Node_Id
7901 is
7902 begin
7903 Set_Assignment_OK (Name);
7904 return Make_Assignment_Statement (Sloc, Name, Expression);
7905 end Make_OK_Assignment_Statement;
7906
7907 -----------------------
7908 -- Number_Of_Choices --
7909 -----------------------
7910
7911 function Number_Of_Choices (N : Node_Id) return Nat is
7912 Assoc : Node_Id;
7913 Choice : Node_Id;
7914
7915 Nb_Choices : Nat := 0;
7916
7917 begin
7918 if Present (Expressions (N)) then
7919 return 0;
7920 end if;
7921
7922 Assoc := First (Component_Associations (N));
7923 while Present (Assoc) loop
7924 Choice := First (Choice_List (Assoc));
7925 while Present (Choice) loop
7926 if Nkind (Choice) /= N_Others_Choice then
7927 Nb_Choices := Nb_Choices + 1;
7928 end if;
7929
7930 Next (Choice);
7931 end loop;
7932
7933 Next (Assoc);
7934 end loop;
7935
7936 return Nb_Choices;
7937 end Number_Of_Choices;
7938
7939 ------------------------------------
7940 -- Packed_Array_Aggregate_Handled --
7941 ------------------------------------
7942
7943 -- The current version of this procedure will handle at compile time
7944 -- any array aggregate that meets these conditions:
7945
7946 -- One and two dimensional, bit packed
7947 -- Underlying packed type is modular type
7948 -- Bounds are within 32-bit Int range
7949 -- All bounds and values are static
7950
7951 -- Note: for now, in the 2-D case, we only handle component sizes of
7952 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7953
7954 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
7955 Loc : constant Source_Ptr := Sloc (N);
7956 Typ : constant Entity_Id := Etype (N);
7957 Ctyp : constant Entity_Id := Component_Type (Typ);
7958
7959 Not_Handled : exception;
7960 -- Exception raised if this aggregate cannot be handled
7961
7962 begin
7963 -- Handle one- or two dimensional bit packed array
7964
7965 if not Is_Bit_Packed_Array (Typ)
7966 or else Number_Dimensions (Typ) > 2
7967 then
7968 return False;
7969 end if;
7970
7971 -- If two-dimensional, check whether it can be folded, and transformed
7972 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7973 -- the original type.
7974
7975 if Number_Dimensions (Typ) = 2 then
7976 return Two_Dim_Packed_Array_Handled (N);
7977 end if;
7978
7979 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
7980 return False;
7981 end if;
7982
7983 if not Is_Scalar_Type (Ctyp) then
7984 return False;
7985 end if;
7986
7987 declare
7988 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
7989
7990 Lo : Node_Id;
7991 Hi : Node_Id;
7992 -- Bounds of index type
7993
7994 Lob : Uint;
7995 Hib : Uint;
7996 -- Values of bounds if compile time known
7997
7998 function Get_Component_Val (N : Node_Id) return Uint;
7999 -- Given a expression value N of the component type Ctyp, returns a
8000 -- value of Csiz (component size) bits representing this value. If
8001 -- the value is nonstatic or any other reason exists why the value
8002 -- cannot be returned, then Not_Handled is raised.
8003
8004 -----------------------
8005 -- Get_Component_Val --
8006 -----------------------
8007
8008 function Get_Component_Val (N : Node_Id) return Uint is
8009 Val : Uint;
8010
8011 begin
8012 -- We have to analyze the expression here before doing any further
8013 -- processing here. The analysis of such expressions is deferred
8014 -- till expansion to prevent some problems of premature analysis.
8015
8016 Analyze_And_Resolve (N, Ctyp);
8017
8018 -- Must have a compile time value. String literals have to be
8019 -- converted into temporaries as well, because they cannot easily
8020 -- be converted into their bit representation.
8021
8022 if not Compile_Time_Known_Value (N)
8023 or else Nkind (N) = N_String_Literal
8024 then
8025 raise Not_Handled;
8026 end if;
8027
8028 Val := Expr_Rep_Value (N);
8029
8030 -- Adjust for bias, and strip proper number of bits
8031
8032 if Has_Biased_Representation (Ctyp) then
8033 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
8034 end if;
8035
8036 return Val mod Uint_2 ** Csiz;
8037 end Get_Component_Val;
8038
8039 -- Here we know we have a one dimensional bit packed array
8040
8041 begin
8042 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
8043
8044 -- Cannot do anything if bounds are dynamic
8045
8046 if not Compile_Time_Known_Value (Lo)
8047 or else
8048 not Compile_Time_Known_Value (Hi)
8049 then
8050 return False;
8051 end if;
8052
8053 -- Or are silly out of range of int bounds
8054
8055 Lob := Expr_Value (Lo);
8056 Hib := Expr_Value (Hi);
8057
8058 if not UI_Is_In_Int_Range (Lob)
8059 or else
8060 not UI_Is_In_Int_Range (Hib)
8061 then
8062 return False;
8063 end if;
8064
8065 -- At this stage we have a suitable aggregate for handling at compile
8066 -- time. The only remaining checks are that the values of expressions
8067 -- in the aggregate are compile-time known (checks are performed by
8068 -- Get_Component_Val), and that any subtypes or ranges are statically
8069 -- known.
8070
8071 -- If the aggregate is not fully positional at this stage, then
8072 -- convert it to positional form. Either this will fail, in which
8073 -- case we can do nothing, or it will succeed, in which case we have
8074 -- succeeded in handling the aggregate and transforming it into a
8075 -- modular value, or it will stay an aggregate, in which case we
8076 -- have failed to create a packed value for it.
8077
8078 if Present (Component_Associations (N)) then
8079 Convert_To_Positional
8080 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8081 return Nkind (N) /= N_Aggregate;
8082 end if;
8083
8084 -- Otherwise we are all positional, so convert to proper value
8085
8086 declare
8087 Lov : constant Int := UI_To_Int (Lob);
8088 Hiv : constant Int := UI_To_Int (Hib);
8089
8090 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
8091 -- The length of the array (number of elements)
8092
8093 Aggregate_Val : Uint;
8094 -- Value of aggregate. The value is set in the low order bits of
8095 -- this value. For the little-endian case, the values are stored
8096 -- from low-order to high-order and for the big-endian case the
8097 -- values are stored from high-order to low-order. Note that gigi
8098 -- will take care of the conversions to left justify the value in
8099 -- the big endian case (because of left justified modular type
8100 -- processing), so we do not have to worry about that here.
8101
8102 Lit : Node_Id;
8103 -- Integer literal for resulting constructed value
8104
8105 Shift : Nat;
8106 -- Shift count from low order for next value
8107
8108 Incr : Int;
8109 -- Shift increment for loop
8110
8111 Expr : Node_Id;
8112 -- Next expression from positional parameters of aggregate
8113
8114 Left_Justified : Boolean;
8115 -- Set True if we are filling the high order bits of the target
8116 -- value (i.e. the value is left justified).
8117
8118 begin
8119 -- For little endian, we fill up the low order bits of the target
8120 -- value. For big endian we fill up the high order bits of the
8121 -- target value (which is a left justified modular value).
8122
8123 Left_Justified := Bytes_Big_Endian;
8124
8125 -- Switch justification if using -gnatd8
8126
8127 if Debug_Flag_8 then
8128 Left_Justified := not Left_Justified;
8129 end if;
8130
8131 -- Switch justfification if reverse storage order
8132
8133 if Reverse_Storage_Order (Base_Type (Typ)) then
8134 Left_Justified := not Left_Justified;
8135 end if;
8136
8137 if Left_Justified then
8138 Shift := Csiz * (Len - 1);
8139 Incr := -Csiz;
8140 else
8141 Shift := 0;
8142 Incr := +Csiz;
8143 end if;
8144
8145 -- Loop to set the values
8146
8147 if Len = 0 then
8148 Aggregate_Val := Uint_0;
8149 else
8150 Expr := First (Expressions (N));
8151 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
8152
8153 for J in 2 .. Len loop
8154 Shift := Shift + Incr;
8155 Next (Expr);
8156 Aggregate_Val :=
8157 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
8158 end loop;
8159 end if;
8160
8161 -- Now we can rewrite with the proper value
8162
8163 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
8164 Set_Print_In_Hex (Lit);
8165
8166 -- Construct the expression using this literal. Note that it is
8167 -- important to qualify the literal with its proper modular type
8168 -- since universal integer does not have the required range and
8169 -- also this is a left justified modular type, which is important
8170 -- in the big-endian case.
8171
8172 Rewrite (N,
8173 Unchecked_Convert_To (Typ,
8174 Make_Qualified_Expression (Loc,
8175 Subtype_Mark =>
8176 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
8177 Expression => Lit)));
8178
8179 Analyze_And_Resolve (N, Typ);
8180 return True;
8181 end;
8182 end;
8183
8184 exception
8185 when Not_Handled =>
8186 return False;
8187 end Packed_Array_Aggregate_Handled;
8188
8189 ----------------------------
8190 -- Has_Mutable_Components --
8191 ----------------------------
8192
8193 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
8194 Comp : Entity_Id;
8195 Ctyp : Entity_Id;
8196
8197 begin
8198 Comp := First_Component (Typ);
8199 while Present (Comp) loop
8200 Ctyp := Underlying_Type (Etype (Comp));
8201 if Is_Record_Type (Ctyp)
8202 and then Has_Discriminants (Ctyp)
8203 and then not Is_Constrained (Ctyp)
8204 then
8205 return True;
8206 end if;
8207
8208 Next_Component (Comp);
8209 end loop;
8210
8211 return False;
8212 end Has_Mutable_Components;
8213
8214 ------------------------------
8215 -- Initialize_Discriminants --
8216 ------------------------------
8217
8218 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
8219 Loc : constant Source_Ptr := Sloc (N);
8220 Bas : constant Entity_Id := Base_Type (Typ);
8221 Par : constant Entity_Id := Etype (Bas);
8222 Decl : constant Node_Id := Parent (Par);
8223 Ref : Node_Id;
8224
8225 begin
8226 if Is_Tagged_Type (Bas)
8227 and then Is_Derived_Type (Bas)
8228 and then Has_Discriminants (Par)
8229 and then Has_Discriminants (Bas)
8230 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
8231 and then Nkind (Decl) = N_Full_Type_Declaration
8232 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
8233 and then
8234 Present (Variant_Part (Component_List (Type_Definition (Decl))))
8235 and then Nkind (N) /= N_Extension_Aggregate
8236 then
8237
8238 -- Call init proc to set discriminants.
8239 -- There should eventually be a special procedure for this ???
8240
8241 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
8242 Insert_Actions_After (N,
8243 Build_Initialization_Call (Sloc (N), Ref, Typ));
8244 end if;
8245 end Initialize_Discriminants;
8246
8247 ----------------
8248 -- Must_Slide --
8249 ----------------
8250
8251 function Must_Slide
8252 (Obj_Type : Entity_Id;
8253 Typ : Entity_Id) return Boolean
8254 is
8255 L1, L2, H1, H2 : Node_Id;
8256
8257 begin
8258 -- No sliding if the type of the object is not established yet, if it is
8259 -- an unconstrained type whose actual subtype comes from the aggregate,
8260 -- or if the two types are identical.
8261
8262 if not Is_Array_Type (Obj_Type) then
8263 return False;
8264
8265 elsif not Is_Constrained (Obj_Type) then
8266 return False;
8267
8268 elsif Typ = Obj_Type then
8269 return False;
8270
8271 else
8272 -- Sliding can only occur along the first dimension
8273
8274 Get_Index_Bounds (First_Index (Typ), L1, H1);
8275 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
8276
8277 if not Is_OK_Static_Expression (L1) or else
8278 not Is_OK_Static_Expression (L2) or else
8279 not Is_OK_Static_Expression (H1) or else
8280 not Is_OK_Static_Expression (H2)
8281 then
8282 return False;
8283 else
8284 return Expr_Value (L1) /= Expr_Value (L2)
8285 or else
8286 Expr_Value (H1) /= Expr_Value (H2);
8287 end if;
8288 end if;
8289 end Must_Slide;
8290
8291 ---------------------------------
8292 -- Process_Transient_Component --
8293 ---------------------------------
8294
8295 procedure Process_Transient_Component
8296 (Loc : Source_Ptr;
8297 Comp_Typ : Entity_Id;
8298 Init_Expr : Node_Id;
8299 Fin_Call : out Node_Id;
8300 Hook_Clear : out Node_Id;
8301 Aggr : Node_Id := Empty;
8302 Stmts : List_Id := No_List)
8303 is
8304 procedure Add_Item (Item : Node_Id);
8305 -- Insert arbitrary node Item into the tree depending on the values of
8306 -- Aggr and Stmts.
8307
8308 --------------
8309 -- Add_Item --
8310 --------------
8311
8312 procedure Add_Item (Item : Node_Id) is
8313 begin
8314 if Present (Aggr) then
8315 Insert_Action (Aggr, Item);
8316 else
8317 pragma Assert (Present (Stmts));
8318 Append_To (Stmts, Item);
8319 end if;
8320 end Add_Item;
8321
8322 -- Local variables
8323
8324 Hook_Assign : Node_Id;
8325 Hook_Decl : Node_Id;
8326 Ptr_Decl : Node_Id;
8327 Res_Decl : Node_Id;
8328 Res_Id : Entity_Id;
8329 Res_Typ : Entity_Id;
8330
8331 -- Start of processing for Process_Transient_Component
8332
8333 begin
8334 -- Add the access type, which provides a reference to the function
8335 -- result. Generate:
8336
8337 -- type Res_Typ is access all Comp_Typ;
8338
8339 Res_Typ := Make_Temporary (Loc, 'A');
8340 Set_Ekind (Res_Typ, E_General_Access_Type);
8341 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
8342
8343 Add_Item
8344 (Make_Full_Type_Declaration (Loc,
8345 Defining_Identifier => Res_Typ,
8346 Type_Definition =>
8347 Make_Access_To_Object_Definition (Loc,
8348 All_Present => True,
8349 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
8350
8351 -- Add the temporary which captures the result of the function call.
8352 -- Generate:
8353
8354 -- Res : constant Res_Typ := Init_Expr'Reference;
8355
8356 -- Note that this temporary is effectively a transient object because
8357 -- its lifetime is bounded by the current array or record component.
8358
8359 Res_Id := Make_Temporary (Loc, 'R');
8360 Set_Ekind (Res_Id, E_Constant);
8361 Set_Etype (Res_Id, Res_Typ);
8362
8363 -- Mark the transient object as successfully processed to avoid double
8364 -- finalization.
8365
8366 Set_Is_Finalized_Transient (Res_Id);
8367
8368 -- Signal the general finalization machinery that this transient object
8369 -- should not be considered for finalization actions because its cleanup
8370 -- will be performed by Process_Transient_Component_Completion.
8371
8372 Set_Is_Ignored_Transient (Res_Id);
8373
8374 Res_Decl :=
8375 Make_Object_Declaration (Loc,
8376 Defining_Identifier => Res_Id,
8377 Constant_Present => True,
8378 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
8379 Expression =>
8380 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
8381
8382 Add_Item (Res_Decl);
8383
8384 -- Construct all pieces necessary to hook and finalize the transient
8385 -- result.
8386
8387 Build_Transient_Object_Statements
8388 (Obj_Decl => Res_Decl,
8389 Fin_Call => Fin_Call,
8390 Hook_Assign => Hook_Assign,
8391 Hook_Clear => Hook_Clear,
8392 Hook_Decl => Hook_Decl,
8393 Ptr_Decl => Ptr_Decl);
8394
8395 -- Add the access type which provides a reference to the transient
8396 -- result. Generate:
8397
8398 -- type Ptr_Typ is access all Comp_Typ;
8399
8400 Add_Item (Ptr_Decl);
8401
8402 -- Add the temporary which acts as a hook to the transient result.
8403 -- Generate:
8404
8405 -- Hook : Ptr_Typ := null;
8406
8407 Add_Item (Hook_Decl);
8408
8409 -- Attach the transient result to the hook. Generate:
8410
8411 -- Hook := Ptr_Typ (Res);
8412
8413 Add_Item (Hook_Assign);
8414
8415 -- The original initialization expression now references the value of
8416 -- the temporary function result. Generate:
8417
8418 -- Res.all
8419
8420 Rewrite (Init_Expr,
8421 Make_Explicit_Dereference (Loc,
8422 Prefix => New_Occurrence_Of (Res_Id, Loc)));
8423 end Process_Transient_Component;
8424
8425 --------------------------------------------
8426 -- Process_Transient_Component_Completion --
8427 --------------------------------------------
8428
8429 procedure Process_Transient_Component_Completion
8430 (Loc : Source_Ptr;
8431 Aggr : Node_Id;
8432 Fin_Call : Node_Id;
8433 Hook_Clear : Node_Id;
8434 Stmts : List_Id)
8435 is
8436 Exceptions_OK : constant Boolean :=
8437 not Restriction_Active (No_Exception_Propagation);
8438
8439 begin
8440 pragma Assert (Present (Hook_Clear));
8441
8442 -- Generate the following code if exception propagation is allowed:
8443
8444 -- declare
8445 -- Abort : constant Boolean := Triggered_By_Abort;
8446 -- <or>
8447 -- Abort : constant Boolean := False; -- no abort
8448
8449 -- E : Exception_Occurrence;
8450 -- Raised : Boolean := False;
8451
8452 -- begin
8453 -- [Abort_Defer;]
8454
8455 -- begin
8456 -- Hook := null;
8457 -- [Deep_]Finalize (Res.all);
8458
8459 -- exception
8460 -- when others =>
8461 -- if not Raised then
8462 -- Raised := True;
8463 -- Save_Occurrence (E,
8464 -- Get_Curent_Excep.all.all);
8465 -- end if;
8466 -- end;
8467
8468 -- [Abort_Undefer;]
8469
8470 -- if Raised and then not Abort then
8471 -- Raise_From_Controlled_Operation (E);
8472 -- end if;
8473 -- end;
8474
8475 if Exceptions_OK then
8476 Abort_And_Exception : declare
8477 Blk_Decls : constant List_Id := New_List;
8478 Blk_Stmts : constant List_Id := New_List;
8479 Fin_Stmts : constant List_Id := New_List;
8480
8481 Fin_Data : Finalization_Exception_Data;
8482
8483 begin
8484 -- Create the declarations of the two flags and the exception
8485 -- occurrence.
8486
8487 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
8488
8489 -- Generate:
8490 -- Abort_Defer;
8491
8492 if Abort_Allowed then
8493 Append_To (Blk_Stmts,
8494 Build_Runtime_Call (Loc, RE_Abort_Defer));
8495 end if;
8496
8497 -- Wrap the hook clear and the finalization call in order to trap
8498 -- a potential exception.
8499
8500 Append_To (Fin_Stmts, Hook_Clear);
8501
8502 if Present (Fin_Call) then
8503 Append_To (Fin_Stmts, Fin_Call);
8504 end if;
8505
8506 Append_To (Blk_Stmts,
8507 Make_Block_Statement (Loc,
8508 Handled_Statement_Sequence =>
8509 Make_Handled_Sequence_Of_Statements (Loc,
8510 Statements => Fin_Stmts,
8511 Exception_Handlers => New_List (
8512 Build_Exception_Handler (Fin_Data)))));
8513
8514 -- Generate:
8515 -- Abort_Undefer;
8516
8517 if Abort_Allowed then
8518 Append_To (Blk_Stmts,
8519 Build_Runtime_Call (Loc, RE_Abort_Undefer));
8520 end if;
8521
8522 -- Reraise the potential exception with a proper "upgrade" to
8523 -- Program_Error if needed.
8524
8525 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
8526
8527 -- Wrap everything in a block
8528
8529 Append_To (Stmts,
8530 Make_Block_Statement (Loc,
8531 Declarations => Blk_Decls,
8532 Handled_Statement_Sequence =>
8533 Make_Handled_Sequence_Of_Statements (Loc,
8534 Statements => Blk_Stmts)));
8535 end Abort_And_Exception;
8536
8537 -- Generate the following code if exception propagation is not allowed
8538 -- and aborts are allowed:
8539
8540 -- begin
8541 -- Abort_Defer;
8542 -- Hook := null;
8543 -- [Deep_]Finalize (Res.all);
8544 -- at end
8545 -- Abort_Undefer_Direct;
8546 -- end;
8547
8548 elsif Abort_Allowed then
8549 Abort_Only : declare
8550 Blk_Stmts : constant List_Id := New_List;
8551
8552 begin
8553 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8554 Append_To (Blk_Stmts, Hook_Clear);
8555
8556 if Present (Fin_Call) then
8557 Append_To (Blk_Stmts, Fin_Call);
8558 end if;
8559
8560 Append_To (Stmts,
8561 Build_Abort_Undefer_Block (Loc,
8562 Stmts => Blk_Stmts,
8563 Context => Aggr));
8564 end Abort_Only;
8565
8566 -- Otherwise generate:
8567
8568 -- Hook := null;
8569 -- [Deep_]Finalize (Res.all);
8570
8571 else
8572 Append_To (Stmts, Hook_Clear);
8573
8574 if Present (Fin_Call) then
8575 Append_To (Stmts, Fin_Call);
8576 end if;
8577 end if;
8578 end Process_Transient_Component_Completion;
8579
8580 ---------------------
8581 -- Sort_Case_Table --
8582 ---------------------
8583
8584 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
8585 L : constant Int := Case_Table'First;
8586 U : constant Int := Case_Table'Last;
8587 K : Int;
8588 J : Int;
8589 T : Case_Bounds;
8590
8591 begin
8592 K := L;
8593 while K /= U loop
8594 T := Case_Table (K + 1);
8595
8596 J := K + 1;
8597 while J /= L
8598 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
8599 Expr_Value (T.Choice_Lo)
8600 loop
8601 Case_Table (J) := Case_Table (J - 1);
8602 J := J - 1;
8603 end loop;
8604
8605 Case_Table (J) := T;
8606 K := K + 1;
8607 end loop;
8608 end Sort_Case_Table;
8609
8610 ----------------------------
8611 -- Static_Array_Aggregate --
8612 ----------------------------
8613
8614 function Static_Array_Aggregate (N : Node_Id) return Boolean is
8615 function Is_Static_Component (Nod : Node_Id) return Boolean;
8616 -- Return True if Nod has a compile-time known value and can be passed
8617 -- as is to the back-end without further expansion.
8618
8619 ---------------------------
8620 -- Is_Static_Component --
8621 ---------------------------
8622
8623 function Is_Static_Component (Nod : Node_Id) return Boolean is
8624 begin
8625 if Nkind_In (Nod, N_Integer_Literal, N_Real_Literal) then
8626 return True;
8627
8628 elsif Is_Entity_Name (Nod)
8629 and then Present (Entity (Nod))
8630 and then Ekind (Entity (Nod)) = E_Enumeration_Literal
8631 then
8632 return True;
8633
8634 elsif Nkind (Nod) = N_Aggregate
8635 and then Compile_Time_Known_Aggregate (Nod)
8636 then
8637 return True;
8638
8639 else
8640 return False;
8641 end if;
8642 end Is_Static_Component;
8643
8644 -- Local variables
8645
8646 Bounds : constant Node_Id := Aggregate_Bounds (N);
8647 Typ : constant Entity_Id := Etype (N);
8648
8649 Agg : Node_Id;
8650 Expr : Node_Id;
8651 Lo : Node_Id;
8652 Hi : Node_Id;
8653
8654 -- Start of processing for Static_Array_Aggregate
8655
8656 begin
8657 if Is_Packed (Typ) or else Has_Discriminants (Component_Type (Typ)) then
8658 return False;
8659 end if;
8660
8661 if Present (Bounds)
8662 and then Nkind (Bounds) = N_Range
8663 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
8664 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
8665 then
8666 Lo := Low_Bound (Bounds);
8667 Hi := High_Bound (Bounds);
8668
8669 if No (Component_Associations (N)) then
8670
8671 -- Verify that all components are static
8672
8673 Expr := First (Expressions (N));
8674 while Present (Expr) loop
8675 if not Is_Static_Component (Expr) then
8676 return False;
8677 end if;
8678
8679 Next (Expr);
8680 end loop;
8681
8682 return True;
8683
8684 else
8685 -- We allow only a single named association, either a static
8686 -- range or an others_clause, with a static expression.
8687
8688 Expr := First (Component_Associations (N));
8689
8690 if Present (Expressions (N)) then
8691 return False;
8692
8693 elsif Present (Next (Expr)) then
8694 return False;
8695
8696 elsif Present (Next (First (Choice_List (Expr)))) then
8697 return False;
8698
8699 else
8700 -- The aggregate is static if all components are literals,
8701 -- or else all its components are static aggregates for the
8702 -- component type. We also limit the size of a static aggregate
8703 -- to prevent runaway static expressions.
8704
8705 if not Is_Static_Component (Expression (Expr)) then
8706 return False;
8707 end if;
8708
8709 if not Aggr_Size_OK (N, Typ) then
8710 return False;
8711 end if;
8712
8713 -- Create a positional aggregate with the right number of
8714 -- copies of the expression.
8715
8716 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
8717
8718 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
8719 loop
8720 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
8721
8722 -- The copied expression must be analyzed and resolved.
8723 -- Besides setting the type, this ensures that static
8724 -- expressions are appropriately marked as such.
8725
8726 Analyze_And_Resolve
8727 (Last (Expressions (Agg)), Component_Type (Typ));
8728 end loop;
8729
8730 Set_Aggregate_Bounds (Agg, Bounds);
8731 Set_Etype (Agg, Typ);
8732 Set_Analyzed (Agg);
8733 Rewrite (N, Agg);
8734 Set_Compile_Time_Known_Aggregate (N);
8735
8736 return True;
8737 end if;
8738 end if;
8739
8740 else
8741 return False;
8742 end if;
8743 end Static_Array_Aggregate;
8744
8745 ----------------------------------
8746 -- Two_Dim_Packed_Array_Handled --
8747 ----------------------------------
8748
8749 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
8750 Loc : constant Source_Ptr := Sloc (N);
8751 Typ : constant Entity_Id := Etype (N);
8752 Ctyp : constant Entity_Id := Component_Type (Typ);
8753 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
8754 Packed_Array : constant Entity_Id :=
8755 Packed_Array_Impl_Type (Base_Type (Typ));
8756
8757 One_Comp : Node_Id;
8758 -- Expression in original aggregate
8759
8760 One_Dim : Node_Id;
8761 -- One-dimensional subaggregate
8762
8763 begin
8764
8765 -- For now, only deal with cases where an integral number of elements
8766 -- fit in a single byte. This includes the most common boolean case.
8767
8768 if not (Comp_Size = 1 or else
8769 Comp_Size = 2 or else
8770 Comp_Size = 4)
8771 then
8772 return False;
8773 end if;
8774
8775 Convert_To_Positional
8776 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8777
8778 -- Verify that all components are static
8779
8780 if Nkind (N) = N_Aggregate
8781 and then Compile_Time_Known_Aggregate (N)
8782 then
8783 null;
8784
8785 -- The aggregate may have been reanalyzed and converted already
8786
8787 elsif Nkind (N) /= N_Aggregate then
8788 return True;
8789
8790 -- If component associations remain, the aggregate is not static
8791
8792 elsif Present (Component_Associations (N)) then
8793 return False;
8794
8795 else
8796 One_Dim := First (Expressions (N));
8797 while Present (One_Dim) loop
8798 if Present (Component_Associations (One_Dim)) then
8799 return False;
8800 end if;
8801
8802 One_Comp := First (Expressions (One_Dim));
8803 while Present (One_Comp) loop
8804 if not Is_OK_Static_Expression (One_Comp) then
8805 return False;
8806 end if;
8807
8808 Next (One_Comp);
8809 end loop;
8810
8811 Next (One_Dim);
8812 end loop;
8813 end if;
8814
8815 -- Two-dimensional aggregate is now fully positional so pack one
8816 -- dimension to create a static one-dimensional array, and rewrite
8817 -- as an unchecked conversion to the original type.
8818
8819 declare
8820 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
8821 -- The packed array type is a byte array
8822
8823 Packed_Num : Nat;
8824 -- Number of components accumulated in current byte
8825
8826 Comps : List_Id;
8827 -- Assembled list of packed values for equivalent aggregate
8828
8829 Comp_Val : Uint;
8830 -- Integer value of component
8831
8832 Incr : Int;
8833 -- Step size for packing
8834
8835 Init_Shift : Int;
8836 -- Endian-dependent start position for packing
8837
8838 Shift : Int;
8839 -- Current insertion position
8840
8841 Val : Int;
8842 -- Component of packed array being assembled
8843
8844 begin
8845 Comps := New_List;
8846 Val := 0;
8847 Packed_Num := 0;
8848
8849 -- Account for endianness. See corresponding comment in
8850 -- Packed_Array_Aggregate_Handled concerning the following.
8851
8852 if Bytes_Big_Endian
8853 xor Debug_Flag_8
8854 xor Reverse_Storage_Order (Base_Type (Typ))
8855 then
8856 Init_Shift := Byte_Size - Comp_Size;
8857 Incr := -Comp_Size;
8858 else
8859 Init_Shift := 0;
8860 Incr := +Comp_Size;
8861 end if;
8862
8863 -- Iterate over each subaggregate
8864
8865 Shift := Init_Shift;
8866 One_Dim := First (Expressions (N));
8867 while Present (One_Dim) loop
8868 One_Comp := First (Expressions (One_Dim));
8869 while Present (One_Comp) loop
8870 if Packed_Num = Byte_Size / Comp_Size then
8871
8872 -- Byte is complete, add to list of expressions
8873
8874 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8875 Val := 0;
8876 Shift := Init_Shift;
8877 Packed_Num := 0;
8878
8879 else
8880 Comp_Val := Expr_Rep_Value (One_Comp);
8881
8882 -- Adjust for bias, and strip proper number of bits
8883
8884 if Has_Biased_Representation (Ctyp) then
8885 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
8886 end if;
8887
8888 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
8889 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
8890 Shift := Shift + Incr;
8891 One_Comp := Next (One_Comp);
8892 Packed_Num := Packed_Num + 1;
8893 end if;
8894 end loop;
8895
8896 One_Dim := Next (One_Dim);
8897 end loop;
8898
8899 if Packed_Num > 0 then
8900
8901 -- Add final incomplete byte if present
8902
8903 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8904 end if;
8905
8906 Rewrite (N,
8907 Unchecked_Convert_To (Typ,
8908 Make_Qualified_Expression (Loc,
8909 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
8910 Expression => Make_Aggregate (Loc, Expressions => Comps))));
8911 Analyze_And_Resolve (N);
8912 return True;
8913 end;
8914 end Two_Dim_Packed_Array_Handled;
8915
8916 end Exp_Aggr;
This page took 0.411268 seconds and 6 git commands to generate.