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