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