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