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