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