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