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