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