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