Update LOCAL_PATCHES after libsanitizer merge.
[official-gcc.git] / gcc / ada / exp_aggr.adb
blob1928cb9be3cdf22c356dcec41b498003ea545617
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 := 32;
288 Handle_Bit_Packed : Boolean := False);
289 -- If possible, convert named notation to positional notation. This
290 -- conversion is possible only in some static cases. If the conversion is
291 -- possible, then N is rewritten with the analyzed converted aggregate.
292 -- The parameter Max_Others_Replicate controls the maximum number of
293 -- values corresponding to an others choice that will be converted to
294 -- positional notation (the default of 32 is the normal limit, and reflects
295 -- the fact that normally the loop is better than a lot of separate
296 -- assignments). Note that this limit gets overridden in any case if
297 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
298 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
299 -- not expect the back end to handle bit packed arrays, so the normal case
300 -- of conversion is pointless), but in the special case of a call from
301 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
302 -- these are cases we handle in there.
304 procedure Expand_Array_Aggregate (N : Node_Id);
305 -- This is the top-level routine to perform array aggregate expansion.
306 -- N is the N_Aggregate node to be expanded.
308 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
309 -- For two-dimensional packed aggregates with constant bounds and constant
310 -- components, it is preferable to pack the inner aggregates because the
311 -- whole matrix can then be presented to the back-end as a one-dimensional
312 -- list of literals. This is much more efficient than expanding into single
313 -- component assignments. This function determines if the type Typ is for
314 -- an array that is suitable for this optimization: it returns True if Typ
315 -- is a two dimensional bit packed array with component size 1, 2, or 4.
317 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
318 -- Given an array aggregate, this function handles the case of a packed
319 -- array aggregate with all constant values, where the aggregate can be
320 -- evaluated at compile time. If this is possible, then N is rewritten
321 -- to be its proper compile time value with all the components properly
322 -- assembled. The expression is analyzed and resolved and True is returned.
323 -- If this transformation is not possible, N is unchanged and False is
324 -- returned.
326 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
327 -- If the type of the aggregate is a two-dimensional bit_packed array
328 -- it may be transformed into an array of bytes with constant values,
329 -- and presented to the back-end as a static value. The function returns
330 -- false if this transformation cannot be performed. THis is similar to,
331 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
333 ------------------
334 -- Aggr_Size_OK --
335 ------------------
337 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
338 Lo : Node_Id;
339 Hi : Node_Id;
340 Indx : Node_Id;
341 Siz : Int;
342 Lov : Uint;
343 Hiv : Uint;
345 Max_Aggr_Size : Nat;
346 -- Determines the maximum size of an array aggregate produced by
347 -- converting named to positional notation (e.g. from others clauses).
348 -- This avoids running away with attempts to convert huge aggregates,
349 -- which hit memory limits in the backend.
351 function Component_Count (T : Entity_Id) return Nat;
352 -- The limit is applied to the total number of subcomponents that the
353 -- aggregate will have, which is the number of static expressions
354 -- that will appear in the flattened array. This requires a recursive
355 -- computation of the number of scalar components of the structure.
357 ---------------------
358 -- Component_Count --
359 ---------------------
361 function Component_Count (T : Entity_Id) return Nat is
362 Res : Nat := 0;
363 Comp : Entity_Id;
365 begin
366 if Is_Scalar_Type (T) then
367 return 1;
369 elsif Is_Record_Type (T) then
370 Comp := First_Component (T);
371 while Present (Comp) loop
372 Res := Res + Component_Count (Etype (Comp));
373 Next_Component (Comp);
374 end loop;
376 return Res;
378 elsif Is_Array_Type (T) then
379 declare
380 Lo : constant Node_Id :=
381 Type_Low_Bound (Etype (First_Index (T)));
382 Hi : constant Node_Id :=
383 Type_High_Bound (Etype (First_Index (T)));
385 Siz : constant Nat := Component_Count (Component_Type (T));
387 begin
388 -- Check for superflat arrays, i.e. arrays with such bounds
389 -- as 4 .. 2, to insure that this function never returns a
390 -- meaningless negative value.
392 if not Compile_Time_Known_Value (Lo)
393 or else not Compile_Time_Known_Value (Hi)
394 or else Expr_Value (Hi) < Expr_Value (Lo)
395 then
396 return 0;
398 else
399 -- If the number of components is greater than Int'Last,
400 -- then return Int'Last, so caller will return False (Aggr
401 -- size is not OK). Otherwise, UI_To_Int will crash.
403 declare
404 UI : constant Uint :=
405 Expr_Value (Hi) - Expr_Value (Lo) + 1;
406 begin
407 if UI_Is_In_Int_Range (UI) then
408 return Siz * UI_To_Int (UI);
409 else
410 return Int'Last;
411 end if;
412 end;
413 end if;
414 end;
416 else
417 -- Can only be a null for an access type
419 return 1;
420 end if;
421 end Component_Count;
423 -- Start of processing for Aggr_Size_OK
425 begin
426 -- The normal aggregate limit is 500000, but we increase this limit to
427 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
428 -- Restrictions (No_Implicit_Loops) is specified, since in either case
429 -- we are at risk of declaring the program illegal because of this
430 -- limit. We also increase the limit when Static_Elaboration_Desired,
431 -- given that this means that objects are intended to be placed in data
432 -- memory.
434 -- We also increase the limit if the aggregate is for a packed two-
435 -- dimensional array, because if components are static it is much more
436 -- efficient to construct a one-dimensional equivalent array with static
437 -- components.
439 -- Conversely, we decrease the maximum size if none of the above
440 -- requirements apply, and if the aggregate has a single component
441 -- association, which will be more efficient if implemented with a loop.
443 -- Finally, we use a small limit in CodePeer mode where we favor loops
444 -- instead of thousands of single assignments (from large aggregates).
446 Max_Aggr_Size := 500000;
448 if CodePeer_Mode then
449 Max_Aggr_Size := 100;
451 elsif Restriction_Active (No_Elaboration_Code)
452 or else Restriction_Active (No_Implicit_Loops)
453 or else Is_Two_Dim_Packed_Array (Typ)
454 or else (Ekind (Current_Scope) = E_Package
455 and then Static_Elaboration_Desired (Current_Scope))
456 then
457 Max_Aggr_Size := 2 ** 24;
459 elsif No (Expressions (N))
460 and then No (Next (First (Component_Associations (N))))
461 then
462 Max_Aggr_Size := 5000;
463 end if;
465 Siz := Component_Count (Component_Type (Typ));
467 Indx := First_Index (Typ);
468 while Present (Indx) loop
469 Lo := Type_Low_Bound (Etype (Indx));
470 Hi := Type_High_Bound (Etype (Indx));
472 -- Bounds need to be known at compile time
474 if not Compile_Time_Known_Value (Lo)
475 or else not Compile_Time_Known_Value (Hi)
476 then
477 return False;
478 end if;
480 Lov := Expr_Value (Lo);
481 Hiv := Expr_Value (Hi);
483 -- A flat array is always safe
485 if Hiv < Lov then
486 return True;
487 end if;
489 -- One-component aggregates are suspicious, and if the context type
490 -- is an object declaration with nonstatic bounds it will trip gcc;
491 -- such an aggregate must be expanded into a single assignment.
493 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
494 declare
495 Index_Type : constant Entity_Id :=
496 Etype
497 (First_Index (Etype (Defining_Identifier (Parent (N)))));
498 Indx : Node_Id;
500 begin
501 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
502 or else not Compile_Time_Known_Value
503 (Type_High_Bound (Index_Type))
504 then
505 if Present (Component_Associations (N)) then
506 Indx :=
507 First
508 (Choice_List (First (Component_Associations (N))));
510 if Is_Entity_Name (Indx)
511 and then not Is_Type (Entity (Indx))
512 then
513 Error_Msg_N
514 ("single component aggregate in "
515 & "non-static context??", Indx);
516 Error_Msg_N ("\maybe subtype name was meant??", Indx);
517 end if;
518 end if;
520 return False;
521 end if;
522 end;
523 end if;
525 declare
526 Rng : constant Uint := Hiv - Lov + 1;
528 begin
529 -- Check if size is too large
531 if not UI_Is_In_Int_Range (Rng) then
532 return False;
533 end if;
535 Siz := Siz * UI_To_Int (Rng);
536 end;
538 if Siz <= 0
539 or else Siz > Max_Aggr_Size
540 then
541 return False;
542 end if;
544 -- Bounds must be in integer range, for later array construction
546 if not UI_Is_In_Int_Range (Lov)
547 or else
548 not UI_Is_In_Int_Range (Hiv)
549 then
550 return False;
551 end if;
553 Next_Index (Indx);
554 end loop;
556 return True;
557 end Aggr_Size_OK;
559 ---------------------------------
560 -- Backend_Processing_Possible --
561 ---------------------------------
563 -- Backend processing by Gigi/gcc is possible only if all the following
564 -- conditions are met:
566 -- 1. N is fully positional
568 -- 2. N is not a bit-packed array aggregate;
570 -- 3. The size of N's array type must be known at compile time. Note
571 -- that this implies that the component size is also known
573 -- 4. The array type of N does not follow the Fortran layout convention
574 -- or if it does it must be 1 dimensional.
576 -- 5. The array component type may not be tagged (which could necessitate
577 -- reassignment of proper tags).
579 -- 6. The array component type must not have unaligned bit components
581 -- 7. None of the components of the aggregate may be bit unaligned
582 -- components.
584 -- 8. There cannot be delayed components, since we do not know enough
585 -- at this stage to know if back end processing is possible.
587 -- 9. There cannot be any discriminated record components, since the
588 -- back end cannot handle this complex case.
590 -- 10. No controlled actions need to be generated for components
592 -- 11. When generating C code, N must be part of a N_Object_Declaration
594 -- 12. When generating C code, N must not include function calls
596 function Backend_Processing_Possible (N : Node_Id) return Boolean is
597 Typ : constant Entity_Id := Etype (N);
598 -- Typ is the correct constrained array subtype of the aggregate
600 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
601 -- This routine checks components of aggregate N, enforcing checks
602 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
603 -- are performed on subaggregates. The Index value is the current index
604 -- being checked in the multidimensional case.
606 ---------------------
607 -- Component_Check --
608 ---------------------
610 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
611 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
612 -- Given a type conversion or an unchecked type conversion N, return
613 -- its innermost original expression.
615 ----------------------------------
616 -- Ultimate_Original_Expression --
617 ----------------------------------
619 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
620 Expr : Node_Id := Original_Node (N);
622 begin
623 while Nkind_In (Expr, N_Type_Conversion,
624 N_Unchecked_Type_Conversion)
625 loop
626 Expr := Original_Node (Expression (Expr));
627 end loop;
629 return Expr;
630 end Ultimate_Original_Expression;
632 -- Local variables
634 Expr : Node_Id;
636 -- Start of processing for Component_Check
638 begin
639 -- Checks 1: (no component associations)
641 if Present (Component_Associations (N)) then
642 return False;
643 end if;
645 -- Checks 11: The C code generator cannot handle aggregates that are
646 -- not part of an object declaration.
648 if Modify_Tree_For_C then
649 declare
650 Par : Node_Id := Parent (N);
652 begin
653 -- Skip enclosing nested aggregates and their qualified
654 -- expressions.
656 while Nkind (Par) = N_Aggregate
657 or else Nkind (Par) = N_Qualified_Expression
658 loop
659 Par := Parent (Par);
660 end loop;
662 if Nkind (Par) /= N_Object_Declaration then
663 return False;
664 end if;
665 end;
666 end if;
668 -- Checks on components
670 -- Recurse to check subaggregates, which may appear in qualified
671 -- expressions. If delayed, the front-end will have to expand.
672 -- If the component is a discriminated record, treat as nonstatic,
673 -- as the back-end cannot handle this properly.
675 Expr := First (Expressions (N));
676 while Present (Expr) loop
678 -- Checks 8: (no delayed components)
680 if Is_Delayed_Aggregate (Expr) then
681 return False;
682 end if;
684 -- Checks 9: (no discriminated records)
686 if Present (Etype (Expr))
687 and then Is_Record_Type (Etype (Expr))
688 and then Has_Discriminants (Etype (Expr))
689 then
690 return False;
691 end if;
693 -- Checks 7. Component must not be bit aligned component
695 if Possible_Bit_Aligned_Component (Expr) then
696 return False;
697 end if;
699 -- Checks 12: (no function call)
701 if Modify_Tree_For_C
702 and then
703 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
704 then
705 return False;
706 end if;
708 -- Recursion to following indexes for multiple dimension case
710 if Present (Next_Index (Index))
711 and then not Component_Check (Expr, Next_Index (Index))
712 then
713 return False;
714 end if;
716 -- All checks for that component finished, on to next
718 Next (Expr);
719 end loop;
721 return True;
722 end Component_Check;
724 -- Start of processing for Backend_Processing_Possible
726 begin
727 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
729 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
730 return False;
731 end if;
733 -- If component is limited, aggregate must be expanded because each
734 -- component assignment must be built in place.
736 if Is_Limited_View (Component_Type (Typ)) then
737 return False;
738 end if;
740 -- Checks 4 (array must not be multidimensional Fortran case)
742 if Convention (Typ) = Convention_Fortran
743 and then Number_Dimensions (Typ) > 1
744 then
745 return False;
746 end if;
748 -- Checks 3 (size of array must be known at compile time)
750 if not Size_Known_At_Compile_Time (Typ) then
751 return False;
752 end if;
754 -- Checks on components
756 if not Component_Check (N, First_Index (Typ)) then
757 return False;
758 end if;
760 -- Checks 5 (if the component type is tagged, then we may need to do
761 -- tag adjustments. Perhaps this should be refined to check for any
762 -- component associations that actually need tag adjustment, similar
763 -- to the test in Component_OK_For_Backend for record aggregates with
764 -- tagged components, but not clear whether it's worthwhile ???; in the
765 -- case of virtual machines (no Tagged_Type_Expansion), object tags are
766 -- handled implicitly).
768 if Is_Tagged_Type (Component_Type (Typ))
769 and then Tagged_Type_Expansion
770 then
771 return False;
772 end if;
774 -- Checks 6 (component type must not have bit aligned components)
776 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
777 return False;
778 end if;
780 -- Backend processing is possible
782 Set_Size_Known_At_Compile_Time (Etype (N), True);
783 return True;
784 end Backend_Processing_Possible;
786 ---------------------------
787 -- Build_Array_Aggr_Code --
788 ---------------------------
790 -- The code that we generate from a one dimensional aggregate is
792 -- 1. If the subaggregate contains discrete choices we
794 -- (a) Sort the discrete choices
796 -- (b) Otherwise for each discrete choice that specifies a range we
797 -- emit a loop. If a range specifies a maximum of three values, or
798 -- we are dealing with an expression we emit a sequence of
799 -- assignments instead of a loop.
801 -- (c) Generate the remaining loops to cover the others choice if any
803 -- 2. If the aggregate contains positional elements we
805 -- (a) translate the positional elements in a series of assignments
807 -- (b) Generate a final loop to cover the others choice if any.
808 -- Note that this final loop has to be a while loop since the case
810 -- L : Integer := Integer'Last;
811 -- H : Integer := Integer'Last;
812 -- A : array (L .. H) := (1, others =>0);
814 -- cannot be handled by a for loop. Thus for the following
816 -- array (L .. H) := (.. positional elements.., others =>E);
818 -- we always generate something like:
820 -- J : Index_Type := Index_Of_Last_Positional_Element;
821 -- while J < H loop
822 -- J := Index_Base'Succ (J)
823 -- Tmp (J) := E;
824 -- end loop;
826 function Build_Array_Aggr_Code
827 (N : Node_Id;
828 Ctype : Entity_Id;
829 Index : Node_Id;
830 Into : Node_Id;
831 Scalar_Comp : Boolean;
832 Indexes : List_Id := No_List) return List_Id
834 Loc : constant Source_Ptr := Sloc (N);
835 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
836 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
837 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
839 function Add (Val : Int; To : Node_Id) return Node_Id;
840 -- Returns an expression where Val is added to expression To, unless
841 -- To+Val is provably out of To's base type range. To must be an
842 -- already analyzed expression.
844 function Empty_Range (L, H : Node_Id) return Boolean;
845 -- Returns True if the range defined by L .. H is certainly empty
847 function Equal (L, H : Node_Id) return Boolean;
848 -- Returns True if L = H for sure
850 function Index_Base_Name return Node_Id;
851 -- Returns a new reference to the index type name
853 function Gen_Assign
854 (Ind : Node_Id;
855 Expr : Node_Id;
856 In_Loop : Boolean := False) return List_Id;
857 -- Ind must be a side-effect-free expression. If the input aggregate N
858 -- to Build_Loop contains no subaggregates, then this function returns
859 -- the assignment statement:
861 -- Into (Indexes, Ind) := Expr;
863 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
864 -- when the assignment appears within a generated loop.
866 -- Ada 2005 (AI-287): In case of default initialized component, Expr
867 -- is empty and we generate a call to the corresponding IP subprogram.
869 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
870 -- Nodes L and H must be side-effect-free expressions. If the input
871 -- aggregate N to Build_Loop contains no subaggregates, this routine
872 -- returns the for loop statement:
874 -- for J in Index_Base'(L) .. Index_Base'(H) loop
875 -- Into (Indexes, J) := Expr;
876 -- end loop;
878 -- Otherwise we call Build_Code recursively. As an optimization if the
879 -- loop covers 3 or fewer scalar elements we generate a sequence of
880 -- assignments.
881 -- If the component association that generates the loop comes from an
882 -- Iterated_Component_Association, the loop parameter has the name of
883 -- the corresponding parameter in the original construct.
885 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
886 -- Nodes L and H must be side-effect-free expressions. If the input
887 -- aggregate N to Build_Loop contains no subaggregates, this routine
888 -- returns the while loop statement:
890 -- J : Index_Base := L;
891 -- while J < H loop
892 -- J := Index_Base'Succ (J);
893 -- Into (Indexes, J) := Expr;
894 -- end loop;
896 -- Otherwise we call Build_Code recursively
898 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
899 -- For an association with a box, use value given by aspect
900 -- Default_Component_Value of array type if specified, else use
901 -- value given by aspect Default_Value for component type itself
902 -- if specified, else return Empty.
904 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
905 function Local_Expr_Value (E : Node_Id) return Uint;
906 -- These two Local routines are used to replace the corresponding ones
907 -- in sem_eval because while processing the bounds of an aggregate with
908 -- discrete choices whose index type is an enumeration, we build static
909 -- expressions not recognized by Compile_Time_Known_Value as such since
910 -- they have not yet been analyzed and resolved. All the expressions in
911 -- question are things like Index_Base_Name'Val (Const) which we can
912 -- easily recognize as being constant.
914 ---------
915 -- Add --
916 ---------
918 function Add (Val : Int; To : Node_Id) return Node_Id is
919 Expr_Pos : Node_Id;
920 Expr : Node_Id;
921 To_Pos : Node_Id;
922 U_To : Uint;
923 U_Val : constant Uint := UI_From_Int (Val);
925 begin
926 -- Note: do not try to optimize the case of Val = 0, because
927 -- we need to build a new node with the proper Sloc value anyway.
929 -- First test if we can do constant folding
931 if Local_Compile_Time_Known_Value (To) then
932 U_To := Local_Expr_Value (To) + Val;
934 -- Determine if our constant is outside the range of the index.
935 -- If so return an Empty node. This empty node will be caught
936 -- by Empty_Range below.
938 if Compile_Time_Known_Value (Index_Base_L)
939 and then U_To < Expr_Value (Index_Base_L)
940 then
941 return Empty;
943 elsif Compile_Time_Known_Value (Index_Base_H)
944 and then U_To > Expr_Value (Index_Base_H)
945 then
946 return Empty;
947 end if;
949 Expr_Pos := Make_Integer_Literal (Loc, U_To);
950 Set_Is_Static_Expression (Expr_Pos);
952 if not Is_Enumeration_Type (Index_Base) then
953 Expr := Expr_Pos;
955 -- If we are dealing with enumeration return
956 -- Index_Base'Val (Expr_Pos)
958 else
959 Expr :=
960 Make_Attribute_Reference
961 (Loc,
962 Prefix => Index_Base_Name,
963 Attribute_Name => Name_Val,
964 Expressions => New_List (Expr_Pos));
965 end if;
967 return Expr;
968 end if;
970 -- If we are here no constant folding possible
972 if not Is_Enumeration_Type (Index_Base) then
973 Expr :=
974 Make_Op_Add (Loc,
975 Left_Opnd => Duplicate_Subexpr (To),
976 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
978 -- If we are dealing with enumeration return
979 -- Index_Base'Val (Index_Base'Pos (To) + Val)
981 else
982 To_Pos :=
983 Make_Attribute_Reference
984 (Loc,
985 Prefix => Index_Base_Name,
986 Attribute_Name => Name_Pos,
987 Expressions => New_List (Duplicate_Subexpr (To)));
989 Expr_Pos :=
990 Make_Op_Add (Loc,
991 Left_Opnd => To_Pos,
992 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
994 Expr :=
995 Make_Attribute_Reference
996 (Loc,
997 Prefix => Index_Base_Name,
998 Attribute_Name => Name_Val,
999 Expressions => New_List (Expr_Pos));
1000 end if;
1002 return Expr;
1003 end Add;
1005 -----------------
1006 -- Empty_Range --
1007 -----------------
1009 function Empty_Range (L, H : Node_Id) return Boolean is
1010 Is_Empty : Boolean := False;
1011 Low : Node_Id;
1012 High : Node_Id;
1014 begin
1015 -- First check if L or H were already detected as overflowing the
1016 -- index base range type by function Add above. If this is so Add
1017 -- returns the empty node.
1019 if No (L) or else No (H) then
1020 return True;
1021 end if;
1023 for J in 1 .. 3 loop
1024 case J is
1026 -- L > H range is empty
1028 when 1 =>
1029 Low := L;
1030 High := H;
1032 -- B_L > H range must be empty
1034 when 2 =>
1035 Low := Index_Base_L;
1036 High := H;
1038 -- L > B_H range must be empty
1040 when 3 =>
1041 Low := L;
1042 High := Index_Base_H;
1043 end case;
1045 if Local_Compile_Time_Known_Value (Low)
1046 and then
1047 Local_Compile_Time_Known_Value (High)
1048 then
1049 Is_Empty :=
1050 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
1051 end if;
1053 exit when Is_Empty;
1054 end loop;
1056 return Is_Empty;
1057 end Empty_Range;
1059 -----------
1060 -- Equal --
1061 -----------
1063 function Equal (L, H : Node_Id) return Boolean is
1064 begin
1065 if L = H then
1066 return True;
1068 elsif Local_Compile_Time_Known_Value (L)
1069 and then
1070 Local_Compile_Time_Known_Value (H)
1071 then
1072 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1073 end if;
1075 return False;
1076 end Equal;
1078 ----------------
1079 -- Gen_Assign --
1080 ----------------
1082 function Gen_Assign
1083 (Ind : Node_Id;
1084 Expr : Node_Id;
1085 In_Loop : Boolean := False) return List_Id
1087 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1088 -- Collect insert_actions generated in the construction of a loop,
1089 -- and prepend them to the sequence of assignments to complete the
1090 -- eventual body of the loop.
1092 procedure Initialize_Array_Component
1093 (Arr_Comp : Node_Id;
1094 Comp_Typ : Node_Id;
1095 Init_Expr : Node_Id;
1096 Stmts : List_Id);
1097 -- Perform the initialization of array component Arr_Comp with
1098 -- expected type Comp_Typ. Init_Expr denotes the initialization
1099 -- expression of the array component. All generated code is added
1100 -- to list Stmts.
1102 procedure Initialize_Ctrl_Array_Component
1103 (Arr_Comp : Node_Id;
1104 Comp_Typ : Entity_Id;
1105 Init_Expr : Node_Id;
1106 Stmts : List_Id);
1107 -- Perform the initialization of array component Arr_Comp when its
1108 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1109 -- the initialization expression of the array component. All hook-
1110 -- related declarations are inserted prior to aggregate N. Remaining
1111 -- code is added to list Stmts.
1113 ----------------------
1114 -- Add_Loop_Actions --
1115 ----------------------
1117 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1118 Res : List_Id;
1120 begin
1121 -- Ada 2005 (AI-287): Do nothing else in case of default
1122 -- initialized component.
1124 if No (Expr) then
1125 return Lis;
1127 elsif Nkind (Parent (Expr)) = N_Component_Association
1128 and then Present (Loop_Actions (Parent (Expr)))
1129 then
1130 Append_List (Lis, Loop_Actions (Parent (Expr)));
1131 Res := Loop_Actions (Parent (Expr));
1132 Set_Loop_Actions (Parent (Expr), No_List);
1133 return Res;
1135 else
1136 return Lis;
1137 end if;
1138 end Add_Loop_Actions;
1140 --------------------------------
1141 -- Initialize_Array_Component --
1142 --------------------------------
1144 procedure Initialize_Array_Component
1145 (Arr_Comp : Node_Id;
1146 Comp_Typ : Node_Id;
1147 Init_Expr : Node_Id;
1148 Stmts : List_Id)
1150 Exceptions_OK : constant Boolean :=
1151 not Restriction_Active
1152 (No_Exception_Propagation);
1154 Finalization_OK : constant Boolean :=
1155 Present (Comp_Typ)
1156 and then Needs_Finalization (Comp_Typ);
1158 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
1159 Adj_Call : Node_Id;
1160 Blk_Stmts : List_Id;
1161 Init_Stmt : Node_Id;
1163 begin
1164 -- Protect the initialization statements from aborts. Generate:
1166 -- Abort_Defer;
1168 if Finalization_OK and Abort_Allowed then
1169 if Exceptions_OK then
1170 Blk_Stmts := New_List;
1171 else
1172 Blk_Stmts := Stmts;
1173 end if;
1175 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
1177 -- Otherwise aborts are not allowed. All generated code is added
1178 -- directly to the input list.
1180 else
1181 Blk_Stmts := Stmts;
1182 end if;
1184 -- Initialize the array element. Generate:
1186 -- Arr_Comp := Init_Expr;
1188 -- Note that the initialization expression is replicated because
1189 -- it has to be reevaluated within a generated loop.
1191 Init_Stmt :=
1192 Make_OK_Assignment_Statement (Loc,
1193 Name => New_Copy_Tree (Arr_Comp),
1194 Expression => New_Copy_Tree (Init_Expr));
1195 Set_No_Ctrl_Actions (Init_Stmt);
1197 -- If this is an aggregate for an array of arrays, each
1198 -- subaggregate will be expanded as well, and even with
1199 -- No_Ctrl_Actions the assignments of inner components will
1200 -- require attachment in their assignments to temporaries. These
1201 -- temporaries must be finalized for each subaggregate. Generate:
1203 -- begin
1204 -- Arr_Comp := Init_Expr;
1205 -- end;
1207 if Finalization_OK and then Is_Array_Type (Comp_Typ) then
1208 Init_Stmt :=
1209 Make_Block_Statement (Loc,
1210 Handled_Statement_Sequence =>
1211 Make_Handled_Sequence_Of_Statements (Loc,
1212 Statements => New_List (Init_Stmt)));
1213 end if;
1215 Append_To (Blk_Stmts, Init_Stmt);
1217 -- Adjust the tag due to a possible view conversion. Generate:
1219 -- Arr_Comp._tag := Full_TypP;
1221 if Tagged_Type_Expansion
1222 and then Present (Comp_Typ)
1223 and then Is_Tagged_Type (Comp_Typ)
1224 then
1225 Append_To (Blk_Stmts,
1226 Make_OK_Assignment_Statement (Loc,
1227 Name =>
1228 Make_Selected_Component (Loc,
1229 Prefix => New_Copy_Tree (Arr_Comp),
1230 Selector_Name =>
1231 New_Occurrence_Of
1232 (First_Tag_Component (Full_Typ), Loc)),
1234 Expression =>
1235 Unchecked_Convert_To (RTE (RE_Tag),
1236 New_Occurrence_Of
1237 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1238 Loc))));
1239 end if;
1241 -- Adjust the array component. Controlled subaggregates are not
1242 -- considered because each of their individual elements will
1243 -- receive an adjustment of its own. Generate:
1245 -- [Deep_]Adjust (Arr_Comp);
1247 if Finalization_OK
1248 and then not Is_Limited_Type (Comp_Typ)
1249 and then not Is_Build_In_Place_Function_Call (Init_Expr)
1250 and then not
1251 (Is_Array_Type (Comp_Typ)
1252 and then Is_Controlled (Component_Type (Comp_Typ))
1253 and then Nkind (Expr) = N_Aggregate)
1254 then
1255 Adj_Call :=
1256 Make_Adjust_Call
1257 (Obj_Ref => New_Copy_Tree (Arr_Comp),
1258 Typ => Comp_Typ);
1260 -- Guard against a missing [Deep_]Adjust when the component
1261 -- type was not frozen properly.
1263 if Present (Adj_Call) then
1264 Append_To (Blk_Stmts, Adj_Call);
1265 end if;
1266 end if;
1268 -- Complete the protection of the initialization statements
1270 if Finalization_OK and Abort_Allowed then
1272 -- Wrap the initialization statements in a block to catch a
1273 -- potential exception. Generate:
1275 -- begin
1276 -- Abort_Defer;
1277 -- Arr_Comp := Init_Expr;
1278 -- Arr_Comp._tag := Full_TypP;
1279 -- [Deep_]Adjust (Arr_Comp);
1280 -- at end
1281 -- Abort_Undefer_Direct;
1282 -- end;
1284 if Exceptions_OK then
1285 Append_To (Stmts,
1286 Build_Abort_Undefer_Block (Loc,
1287 Stmts => Blk_Stmts,
1288 Context => N));
1290 -- Otherwise exceptions are not propagated. Generate:
1292 -- Abort_Defer;
1293 -- Arr_Comp := Init_Expr;
1294 -- Arr_Comp._tag := Full_TypP;
1295 -- [Deep_]Adjust (Arr_Comp);
1296 -- Abort_Undefer;
1298 else
1299 Append_To (Blk_Stmts,
1300 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1301 end if;
1302 end if;
1303 end Initialize_Array_Component;
1305 -------------------------------------
1306 -- Initialize_Ctrl_Array_Component --
1307 -------------------------------------
1309 procedure Initialize_Ctrl_Array_Component
1310 (Arr_Comp : Node_Id;
1311 Comp_Typ : Entity_Id;
1312 Init_Expr : Node_Id;
1313 Stmts : List_Id)
1315 Act_Aggr : Node_Id;
1316 Act_Stmts : List_Id;
1317 Expr : Node_Id;
1318 Fin_Call : Node_Id;
1319 Hook_Clear : Node_Id;
1321 In_Place_Expansion : Boolean;
1322 -- Flag set when a nonlimited controlled function call requires
1323 -- in-place expansion.
1325 begin
1326 -- Duplicate the initialization expression in case the context is
1327 -- a multi choice list or an "others" choice which plugs various
1328 -- holes in the aggregate. As a result the expression is no longer
1329 -- shared between the various components and is reevaluated for
1330 -- each such component.
1332 Expr := New_Copy_Tree (Init_Expr);
1333 Set_Parent (Expr, Parent (Init_Expr));
1335 -- Perform a preliminary analysis and resolution to determine what
1336 -- the initialization expression denotes. An unanalyzed function
1337 -- call may appear as an identifier or an indexed component.
1339 if Nkind_In (Expr, N_Function_Call,
1340 N_Identifier,
1341 N_Indexed_Component)
1342 and then not Analyzed (Expr)
1343 then
1344 Preanalyze_And_Resolve (Expr, Comp_Typ);
1345 end if;
1347 In_Place_Expansion :=
1348 Nkind (Expr) = N_Function_Call
1349 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
1351 -- The initialization expression is a controlled function call.
1352 -- Perform in-place removal of side effects to avoid creating a
1353 -- transient scope, which leads to premature finalization.
1355 -- This in-place expansion is not performed for limited transient
1356 -- objects because the initialization is already done in-place.
1358 if In_Place_Expansion then
1360 -- Suppress the removal of side effects by general analysis
1361 -- because this behavior is emulated here. This avoids the
1362 -- generation of a transient scope, which leads to out-of-order
1363 -- adjustment and finalization.
1365 Set_No_Side_Effect_Removal (Expr);
1367 -- When the transient component initialization is related to a
1368 -- range or an "others", keep all generated statements within
1369 -- the enclosing loop. This way the controlled function call
1370 -- will be evaluated at each iteration, and its result will be
1371 -- finalized at the end of each iteration.
1373 if In_Loop then
1374 Act_Aggr := Empty;
1375 Act_Stmts := Stmts;
1377 -- Otherwise this is a single component initialization. Hook-
1378 -- related statements are inserted prior to the aggregate.
1380 else
1381 Act_Aggr := N;
1382 Act_Stmts := No_List;
1383 end if;
1385 -- Install all hook-related declarations and prepare the clean
1386 -- up statements.
1388 Process_Transient_Component
1389 (Loc => Loc,
1390 Comp_Typ => Comp_Typ,
1391 Init_Expr => Expr,
1392 Fin_Call => Fin_Call,
1393 Hook_Clear => Hook_Clear,
1394 Aggr => Act_Aggr,
1395 Stmts => Act_Stmts);
1396 end if;
1398 -- Use the noncontrolled component initialization circuitry to
1399 -- assign the result of the function call to the array element.
1400 -- This also performs subaggregate wrapping, tag adjustment, and
1401 -- [deep] adjustment of the array element.
1403 Initialize_Array_Component
1404 (Arr_Comp => Arr_Comp,
1405 Comp_Typ => Comp_Typ,
1406 Init_Expr => Expr,
1407 Stmts => Stmts);
1409 -- At this point the array element is fully initialized. Complete
1410 -- the processing of the controlled array component by finalizing
1411 -- the transient function result.
1413 if In_Place_Expansion then
1414 Process_Transient_Component_Completion
1415 (Loc => Loc,
1416 Aggr => N,
1417 Fin_Call => Fin_Call,
1418 Hook_Clear => Hook_Clear,
1419 Stmts => Stmts);
1420 end if;
1421 end Initialize_Ctrl_Array_Component;
1423 -- Local variables
1425 Stmts : constant List_Id := New_List;
1427 Comp_Typ : Entity_Id := Empty;
1428 Expr_Q : Node_Id;
1429 Indexed_Comp : Node_Id;
1430 Init_Call : Node_Id;
1431 New_Indexes : List_Id;
1433 -- Start of processing for Gen_Assign
1435 begin
1436 if No (Indexes) then
1437 New_Indexes := New_List;
1438 else
1439 New_Indexes := New_Copy_List_Tree (Indexes);
1440 end if;
1442 Append_To (New_Indexes, Ind);
1444 if Present (Next_Index (Index)) then
1445 return
1446 Add_Loop_Actions (
1447 Build_Array_Aggr_Code
1448 (N => Expr,
1449 Ctype => Ctype,
1450 Index => Next_Index (Index),
1451 Into => Into,
1452 Scalar_Comp => Scalar_Comp,
1453 Indexes => New_Indexes));
1454 end if;
1456 -- If we get here then we are at a bottom-level (sub-)aggregate
1458 Indexed_Comp :=
1459 Checks_Off
1460 (Make_Indexed_Component (Loc,
1461 Prefix => New_Copy_Tree (Into),
1462 Expressions => New_Indexes));
1464 Set_Assignment_OK (Indexed_Comp);
1466 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1467 -- is not present (and therefore we also initialize Expr_Q to empty).
1469 if No (Expr) then
1470 Expr_Q := Empty;
1471 elsif Nkind (Expr) = N_Qualified_Expression then
1472 Expr_Q := Expression (Expr);
1473 else
1474 Expr_Q := Expr;
1475 end if;
1477 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1478 Comp_Typ := Component_Type (Etype (N));
1479 pragma Assert (Comp_Typ = Ctype); -- AI-287
1481 elsif Present (Next (First (New_Indexes))) then
1483 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1484 -- component because we have received the component type in
1485 -- the formal parameter Ctype.
1487 -- ??? Some assert pragmas have been added to check if this new
1488 -- formal can be used to replace this code in all cases.
1490 if Present (Expr) then
1492 -- This is a multidimensional array. Recover the component type
1493 -- from the outermost aggregate, because subaggregates do not
1494 -- have an assigned type.
1496 declare
1497 P : Node_Id;
1499 begin
1500 P := Parent (Expr);
1501 while Present (P) loop
1502 if Nkind (P) = N_Aggregate
1503 and then Present (Etype (P))
1504 then
1505 Comp_Typ := Component_Type (Etype (P));
1506 exit;
1508 else
1509 P := Parent (P);
1510 end if;
1511 end loop;
1513 pragma Assert (Comp_Typ = Ctype); -- AI-287
1514 end;
1515 end if;
1516 end if;
1518 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1519 -- default initialized components (otherwise Expr_Q is not present).
1521 if Present (Expr_Q)
1522 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1523 then
1524 -- At this stage the Expression may not have been analyzed yet
1525 -- because the array aggregate code has not been updated to use
1526 -- the Expansion_Delayed flag and avoid analysis altogether to
1527 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1528 -- the analysis of non-array aggregates now in order to get the
1529 -- value of Expansion_Delayed flag for the inner aggregate ???
1531 -- In the case of an iterated component association, the analysis
1532 -- of the generated loop will analyze the expression in the
1533 -- proper context, in which the loop parameter is visible.
1535 if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then
1536 if Nkind (Parent (Expr_Q)) = N_Iterated_Component_Association
1537 or else Nkind (Parent (Parent ((Expr_Q)))) =
1538 N_Iterated_Component_Association
1539 then
1540 null;
1541 else
1542 Analyze_And_Resolve (Expr_Q, Comp_Typ);
1543 end if;
1544 end if;
1546 if Is_Delayed_Aggregate (Expr_Q) then
1548 -- This is either a subaggregate of a multidimensional array,
1549 -- or a component of an array type whose component type is
1550 -- also an array. In the latter case, the expression may have
1551 -- component associations that provide different bounds from
1552 -- those of the component type, and sliding must occur. Instead
1553 -- of decomposing the current aggregate assignment, force the
1554 -- reanalysis of the assignment, so that a temporary will be
1555 -- generated in the usual fashion, and sliding will take place.
1557 if Nkind (Parent (N)) = N_Assignment_Statement
1558 and then Is_Array_Type (Comp_Typ)
1559 and then Present (Component_Associations (Expr_Q))
1560 and then Must_Slide (Comp_Typ, Etype (Expr_Q))
1561 then
1562 Set_Expansion_Delayed (Expr_Q, False);
1563 Set_Analyzed (Expr_Q, False);
1565 else
1566 return
1567 Add_Loop_Actions (
1568 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1569 end if;
1570 end if;
1571 end if;
1573 if Present (Expr) then
1575 -- Handle an initialization expression of a controlled type in
1576 -- case it denotes a function call. In general such a scenario
1577 -- will produce a transient scope, but this will lead to wrong
1578 -- order of initialization, adjustment, and finalization in the
1579 -- context of aggregates.
1581 -- Target (1) := Ctrl_Func_Call;
1583 -- begin -- scope
1584 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1585 -- Target (1) := Trans_Obj;
1586 -- Finalize (Trans_Obj);
1587 -- end;
1588 -- Target (1)._tag := ...;
1589 -- Adjust (Target (1));
1591 -- In the example above, the call to Finalize occurs too early
1592 -- and as a result it may leave the array component in a bad
1593 -- state. Finalization of the transient object should really
1594 -- happen after adjustment.
1596 -- To avoid this scenario, perform in-place side-effect removal
1597 -- of the function call. This eliminates the transient property
1598 -- of the function result and ensures correct order of actions.
1600 -- Res : ... := Ctrl_Func_Call;
1601 -- Target (1) := Res;
1602 -- Target (1)._tag := ...;
1603 -- Adjust (Target (1));
1604 -- Finalize (Res);
1606 if Present (Comp_Typ)
1607 and then Needs_Finalization (Comp_Typ)
1608 and then Nkind (Expr) /= N_Aggregate
1609 then
1610 Initialize_Ctrl_Array_Component
1611 (Arr_Comp => Indexed_Comp,
1612 Comp_Typ => Comp_Typ,
1613 Init_Expr => Expr,
1614 Stmts => Stmts);
1616 -- Otherwise perform simple component initialization
1618 else
1619 Initialize_Array_Component
1620 (Arr_Comp => Indexed_Comp,
1621 Comp_Typ => Comp_Typ,
1622 Init_Expr => Expr,
1623 Stmts => Stmts);
1624 end if;
1626 -- Ada 2005 (AI-287): In case of default initialized component, call
1627 -- the initialization subprogram associated with the component type.
1628 -- If the component type is an access type, add an explicit null
1629 -- assignment, because for the back-end there is an initialization
1630 -- present for the whole aggregate, and no default initialization
1631 -- will take place.
1633 -- In addition, if the component type is controlled, we must call
1634 -- its Initialize procedure explicitly, because there is no explicit
1635 -- object creation that will invoke it otherwise.
1637 else
1638 if Present (Base_Init_Proc (Base_Type (Ctype)))
1639 or else Has_Task (Base_Type (Ctype))
1640 then
1641 Append_List_To (Stmts,
1642 Build_Initialization_Call (Loc,
1643 Id_Ref => Indexed_Comp,
1644 Typ => Ctype,
1645 With_Default_Init => True));
1647 -- If the component type has invariants, add an invariant
1648 -- check after the component is default-initialized. It will
1649 -- be analyzed and resolved before the code for initialization
1650 -- of other components.
1652 if Has_Invariants (Ctype) then
1653 Set_Etype (Indexed_Comp, Ctype);
1654 Append_To (Stmts, Make_Invariant_Call (Indexed_Comp));
1655 end if;
1657 elsif Is_Access_Type (Ctype) then
1658 Append_To (Stmts,
1659 Make_Assignment_Statement (Loc,
1660 Name => New_Copy_Tree (Indexed_Comp),
1661 Expression => Make_Null (Loc)));
1662 end if;
1664 if Needs_Finalization (Ctype) then
1665 Init_Call :=
1666 Make_Init_Call
1667 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1668 Typ => Ctype);
1670 -- Guard against a missing [Deep_]Initialize when the component
1671 -- type was not properly frozen.
1673 if Present (Init_Call) then
1674 Append_To (Stmts, Init_Call);
1675 end if;
1676 end if;
1677 end if;
1679 return Add_Loop_Actions (Stmts);
1680 end Gen_Assign;
1682 --------------
1683 -- Gen_Loop --
1684 --------------
1686 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1687 Is_Iterated_Component : constant Boolean :=
1688 Nkind (Parent (Expr)) = N_Iterated_Component_Association;
1690 L_J : Node_Id;
1692 L_L : Node_Id;
1693 -- Index_Base'(L)
1695 L_H : Node_Id;
1696 -- Index_Base'(H)
1698 L_Range : Node_Id;
1699 -- Index_Base'(L) .. Index_Base'(H)
1701 L_Iteration_Scheme : Node_Id;
1702 -- L_J in Index_Base'(L) .. Index_Base'(H)
1704 L_Body : List_Id;
1705 -- The statements to execute in the loop
1707 S : constant List_Id := New_List;
1708 -- List of statements
1710 Tcopy : Node_Id;
1711 -- Copy of expression tree, used for checking purposes
1713 begin
1714 -- If loop bounds define an empty range return the null statement
1716 if Empty_Range (L, H) then
1717 Append_To (S, Make_Null_Statement (Loc));
1719 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1720 -- default initialized component.
1722 if No (Expr) then
1723 null;
1725 else
1726 -- The expression must be type-checked even though no component
1727 -- of the aggregate will have this value. This is done only for
1728 -- actual components of the array, not for subaggregates. Do
1729 -- the check on a copy, because the expression may be shared
1730 -- among several choices, some of which might be non-null.
1732 if Present (Etype (N))
1733 and then Is_Array_Type (Etype (N))
1734 and then No (Next_Index (Index))
1735 then
1736 Expander_Mode_Save_And_Set (False);
1737 Tcopy := New_Copy_Tree (Expr);
1738 Set_Parent (Tcopy, N);
1739 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1740 Expander_Mode_Restore;
1741 end if;
1742 end if;
1744 return S;
1746 -- If loop bounds are the same then generate an assignment, unless
1747 -- the parent construct is an Iterated_Component_Association.
1749 elsif Equal (L, H) and then not Is_Iterated_Component then
1750 return Gen_Assign (New_Copy_Tree (L), Expr);
1752 -- If H - L <= 2 then generate a sequence of assignments when we are
1753 -- processing the bottom most aggregate and it contains scalar
1754 -- components.
1756 elsif No (Next_Index (Index))
1757 and then Scalar_Comp
1758 and then Local_Compile_Time_Known_Value (L)
1759 and then Local_Compile_Time_Known_Value (H)
1760 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1761 and then not Is_Iterated_Component
1762 then
1763 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1764 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1766 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1767 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1768 end if;
1770 return S;
1771 end if;
1773 -- Otherwise construct the loop, starting with the loop index L_J
1775 if Is_Iterated_Component then
1776 L_J :=
1777 Make_Defining_Identifier (Loc,
1778 Chars => (Chars (Defining_Identifier (Parent (Expr)))));
1780 else
1781 L_J := Make_Temporary (Loc, 'J', L);
1782 end if;
1784 -- Construct "L .. H" in Index_Base. We use a qualified expression
1785 -- for the bound to convert to the index base, but we don't need
1786 -- to do that if we already have the base type at hand.
1788 if Etype (L) = Index_Base then
1789 L_L := L;
1790 else
1791 L_L :=
1792 Make_Qualified_Expression (Loc,
1793 Subtype_Mark => Index_Base_Name,
1794 Expression => New_Copy_Tree (L));
1795 end if;
1797 if Etype (H) = Index_Base then
1798 L_H := H;
1799 else
1800 L_H :=
1801 Make_Qualified_Expression (Loc,
1802 Subtype_Mark => Index_Base_Name,
1803 Expression => New_Copy_Tree (H));
1804 end if;
1806 L_Range :=
1807 Make_Range (Loc,
1808 Low_Bound => L_L,
1809 High_Bound => L_H);
1811 -- Construct "for L_J in Index_Base range L .. H"
1813 L_Iteration_Scheme :=
1814 Make_Iteration_Scheme
1815 (Loc,
1816 Loop_Parameter_Specification =>
1817 Make_Loop_Parameter_Specification
1818 (Loc,
1819 Defining_Identifier => L_J,
1820 Discrete_Subtype_Definition => L_Range));
1822 -- Construct the statements to execute in the loop body
1824 L_Body :=
1825 Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr, In_Loop => True);
1827 -- Construct the final loop
1829 Append_To (S,
1830 Make_Implicit_Loop_Statement
1831 (Node => N,
1832 Identifier => Empty,
1833 Iteration_Scheme => L_Iteration_Scheme,
1834 Statements => L_Body));
1836 -- A small optimization: if the aggregate is initialized with a box
1837 -- and the component type has no initialization procedure, remove the
1838 -- useless empty loop.
1840 if Nkind (First (S)) = N_Loop_Statement
1841 and then Is_Empty_List (Statements (First (S)))
1842 then
1843 return New_List (Make_Null_Statement (Loc));
1844 else
1845 return S;
1846 end if;
1847 end Gen_Loop;
1849 ---------------
1850 -- Gen_While --
1851 ---------------
1853 -- The code built is
1855 -- W_J : Index_Base := L;
1856 -- while W_J < H loop
1857 -- W_J := Index_Base'Succ (W);
1858 -- L_Body;
1859 -- end loop;
1861 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1862 W_J : Node_Id;
1864 W_Decl : Node_Id;
1865 -- W_J : Base_Type := L;
1867 W_Iteration_Scheme : Node_Id;
1868 -- while W_J < H
1870 W_Index_Succ : Node_Id;
1871 -- Index_Base'Succ (J)
1873 W_Increment : Node_Id;
1874 -- W_J := Index_Base'Succ (W)
1876 W_Body : constant List_Id := New_List;
1877 -- The statements to execute in the loop
1879 S : constant List_Id := New_List;
1880 -- list of statement
1882 begin
1883 -- If loop bounds define an empty range or are equal return null
1885 if Empty_Range (L, H) or else Equal (L, H) then
1886 Append_To (S, Make_Null_Statement (Loc));
1887 return S;
1888 end if;
1890 -- Build the decl of W_J
1892 W_J := Make_Temporary (Loc, 'J', L);
1893 W_Decl :=
1894 Make_Object_Declaration
1895 (Loc,
1896 Defining_Identifier => W_J,
1897 Object_Definition => Index_Base_Name,
1898 Expression => L);
1900 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1901 -- that in this particular case L is a fresh Expr generated by
1902 -- Add which we are the only ones to use.
1904 Append_To (S, W_Decl);
1906 -- Construct " while W_J < H"
1908 W_Iteration_Scheme :=
1909 Make_Iteration_Scheme
1910 (Loc,
1911 Condition => Make_Op_Lt
1912 (Loc,
1913 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1914 Right_Opnd => New_Copy_Tree (H)));
1916 -- Construct the statements to execute in the loop body
1918 W_Index_Succ :=
1919 Make_Attribute_Reference
1920 (Loc,
1921 Prefix => Index_Base_Name,
1922 Attribute_Name => Name_Succ,
1923 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1925 W_Increment :=
1926 Make_OK_Assignment_Statement
1927 (Loc,
1928 Name => New_Occurrence_Of (W_J, Loc),
1929 Expression => W_Index_Succ);
1931 Append_To (W_Body, W_Increment);
1933 Append_List_To (W_Body,
1934 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr, In_Loop => True));
1936 -- Construct the final loop
1938 Append_To (S,
1939 Make_Implicit_Loop_Statement
1940 (Node => N,
1941 Identifier => Empty,
1942 Iteration_Scheme => W_Iteration_Scheme,
1943 Statements => W_Body));
1945 return S;
1946 end Gen_While;
1948 --------------------
1949 -- Get_Assoc_Expr --
1950 --------------------
1952 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1953 Typ : constant Entity_Id := Base_Type (Etype (N));
1955 begin
1956 if Box_Present (Assoc) then
1957 if Is_Scalar_Type (Ctype) then
1958 if Present (Default_Aspect_Component_Value (Typ)) then
1959 return Default_Aspect_Component_Value (Typ);
1960 elsif Present (Default_Aspect_Value (Ctype)) then
1961 return Default_Aspect_Value (Ctype);
1962 else
1963 return Empty;
1964 end if;
1966 else
1967 return Empty;
1968 end if;
1970 else
1971 return Expression (Assoc);
1972 end if;
1973 end Get_Assoc_Expr;
1975 ---------------------
1976 -- Index_Base_Name --
1977 ---------------------
1979 function Index_Base_Name return Node_Id is
1980 begin
1981 return New_Occurrence_Of (Index_Base, Sloc (N));
1982 end Index_Base_Name;
1984 ------------------------------------
1985 -- Local_Compile_Time_Known_Value --
1986 ------------------------------------
1988 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1989 begin
1990 return Compile_Time_Known_Value (E)
1991 or else
1992 (Nkind (E) = N_Attribute_Reference
1993 and then Attribute_Name (E) = Name_Val
1994 and then Compile_Time_Known_Value (First (Expressions (E))));
1995 end Local_Compile_Time_Known_Value;
1997 ----------------------
1998 -- Local_Expr_Value --
1999 ----------------------
2001 function Local_Expr_Value (E : Node_Id) return Uint is
2002 begin
2003 if Compile_Time_Known_Value (E) then
2004 return Expr_Value (E);
2005 else
2006 return Expr_Value (First (Expressions (E)));
2007 end if;
2008 end Local_Expr_Value;
2010 -- Local variables
2012 New_Code : constant List_Id := New_List;
2014 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
2015 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
2016 -- The aggregate bounds of this specific subaggregate. Note that if the
2017 -- code generated by Build_Array_Aggr_Code is executed then these bounds
2018 -- are OK. Otherwise a Constraint_Error would have been raised.
2020 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
2021 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
2022 -- After Duplicate_Subexpr these are side-effect free
2024 Assoc : Node_Id;
2025 Choice : Node_Id;
2026 Expr : Node_Id;
2027 High : Node_Id;
2028 Low : Node_Id;
2029 Typ : Entity_Id;
2031 Nb_Choices : Nat := 0;
2032 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
2033 -- Used to sort all the different choice values
2035 Nb_Elements : Int;
2036 -- Number of elements in the positional aggregate
2038 Others_Assoc : Node_Id := Empty;
2040 -- Start of processing for Build_Array_Aggr_Code
2042 begin
2043 -- First before we start, a special case. if we have a bit packed
2044 -- array represented as a modular type, then clear the value to
2045 -- zero first, to ensure that unused bits are properly cleared.
2047 Typ := Etype (N);
2049 if Present (Typ)
2050 and then Is_Bit_Packed_Array (Typ)
2051 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
2052 then
2053 Append_To (New_Code,
2054 Make_Assignment_Statement (Loc,
2055 Name => New_Copy_Tree (Into),
2056 Expression =>
2057 Unchecked_Convert_To (Typ,
2058 Make_Integer_Literal (Loc, Uint_0))));
2059 end if;
2061 -- If the component type contains tasks, we need to build a Master
2062 -- entity in the current scope, because it will be needed if build-
2063 -- in-place functions are called in the expanded code.
2065 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
2066 Build_Master_Entity (Defining_Identifier (Parent (N)));
2067 end if;
2069 -- STEP 1: Process component associations
2071 -- For those associations that may generate a loop, initialize
2072 -- Loop_Actions to collect inserted actions that may be crated.
2074 -- Skip this if no component associations
2076 if No (Expressions (N)) then
2078 -- STEP 1 (a): Sort the discrete choices
2080 Assoc := First (Component_Associations (N));
2081 while Present (Assoc) loop
2082 Choice := First (Choice_List (Assoc));
2083 while Present (Choice) loop
2084 if Nkind (Choice) = N_Others_Choice then
2085 Set_Loop_Actions (Assoc, New_List);
2086 Others_Assoc := Assoc;
2087 exit;
2088 end if;
2090 Get_Index_Bounds (Choice, Low, High);
2092 if Low /= High then
2093 Set_Loop_Actions (Assoc, New_List);
2094 end if;
2096 Nb_Choices := Nb_Choices + 1;
2098 Table (Nb_Choices) :=
2099 (Choice_Lo => Low,
2100 Choice_Hi => High,
2101 Choice_Node => Get_Assoc_Expr (Assoc));
2103 Next (Choice);
2104 end loop;
2106 Next (Assoc);
2107 end loop;
2109 -- If there is more than one set of choices these must be static
2110 -- and we can therefore sort them. Remember that Nb_Choices does not
2111 -- account for an others choice.
2113 if Nb_Choices > 1 then
2114 Sort_Case_Table (Table);
2115 end if;
2117 -- STEP 1 (b): take care of the whole set of discrete choices
2119 for J in 1 .. Nb_Choices loop
2120 Low := Table (J).Choice_Lo;
2121 High := Table (J).Choice_Hi;
2122 Expr := Table (J).Choice_Node;
2123 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2124 end loop;
2126 -- STEP 1 (c): generate the remaining loops to cover others choice
2127 -- We don't need to generate loops over empty gaps, but if there is
2128 -- a single empty range we must analyze the expression for semantics
2130 if Present (Others_Assoc) then
2131 declare
2132 First : Boolean := True;
2134 begin
2135 for J in 0 .. Nb_Choices loop
2136 if J = 0 then
2137 Low := Aggr_Low;
2138 else
2139 Low := Add (1, To => Table (J).Choice_Hi);
2140 end if;
2142 if J = Nb_Choices then
2143 High := Aggr_High;
2144 else
2145 High := Add (-1, To => Table (J + 1).Choice_Lo);
2146 end if;
2148 -- If this is an expansion within an init proc, make
2149 -- sure that discriminant references are replaced by
2150 -- the corresponding discriminal.
2152 if Inside_Init_Proc then
2153 if Is_Entity_Name (Low)
2154 and then Ekind (Entity (Low)) = E_Discriminant
2155 then
2156 Set_Entity (Low, Discriminal (Entity (Low)));
2157 end if;
2159 if Is_Entity_Name (High)
2160 and then Ekind (Entity (High)) = E_Discriminant
2161 then
2162 Set_Entity (High, Discriminal (Entity (High)));
2163 end if;
2164 end if;
2166 if First
2167 or else not Empty_Range (Low, High)
2168 then
2169 First := False;
2170 Append_List
2171 (Gen_Loop (Low, High,
2172 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
2173 end if;
2174 end loop;
2175 end;
2176 end if;
2178 -- STEP 2: Process positional components
2180 else
2181 -- STEP 2 (a): Generate the assignments for each positional element
2182 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2183 -- Aggr_L is analyzed and Add wants an analyzed expression.
2185 Expr := First (Expressions (N));
2186 Nb_Elements := -1;
2187 while Present (Expr) loop
2188 Nb_Elements := Nb_Elements + 1;
2189 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2190 To => New_Code);
2191 Next (Expr);
2192 end loop;
2194 -- STEP 2 (b): Generate final loop if an others choice is present
2195 -- Here Nb_Elements gives the offset of the last positional element.
2197 if Present (Component_Associations (N)) then
2198 Assoc := Last (Component_Associations (N));
2200 -- Ada 2005 (AI-287)
2202 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2203 Aggr_High,
2204 Get_Assoc_Expr (Assoc)), -- AI-287
2205 To => New_Code);
2206 end if;
2207 end if;
2209 return New_Code;
2210 end Build_Array_Aggr_Code;
2212 ----------------------------
2213 -- Build_Record_Aggr_Code --
2214 ----------------------------
2216 function Build_Record_Aggr_Code
2217 (N : Node_Id;
2218 Typ : Entity_Id;
2219 Lhs : Node_Id) return List_Id
2221 Loc : constant Source_Ptr := Sloc (N);
2222 L : constant List_Id := New_List;
2223 N_Typ : constant Entity_Id := Etype (N);
2225 Comp : Node_Id;
2226 Instr : Node_Id;
2227 Ref : Node_Id;
2228 Target : Entity_Id;
2229 Comp_Type : Entity_Id;
2230 Selector : Entity_Id;
2231 Comp_Expr : Node_Id;
2232 Expr_Q : Node_Id;
2234 -- If this is an internal aggregate, the External_Final_List is an
2235 -- expression for the controller record of the enclosing type.
2237 -- If the current aggregate has several controlled components, this
2238 -- expression will appear in several calls to attach to the finali-
2239 -- zation list, and it must not be shared.
2241 Ancestor_Is_Expression : Boolean := False;
2242 Ancestor_Is_Subtype_Mark : Boolean := False;
2244 Init_Typ : Entity_Id := Empty;
2246 Finalization_Done : Boolean := False;
2247 -- True if Generate_Finalization_Actions has already been called; calls
2248 -- after the first do nothing.
2250 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2251 -- Returns the value that the given discriminant of an ancestor type
2252 -- should receive (in the absence of a conflict with the value provided
2253 -- by an ancestor part of an extension aggregate).
2255 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2256 -- Check that each of the discriminant values defined by the ancestor
2257 -- part of an extension aggregate match the corresponding values
2258 -- provided by either an association of the aggregate or by the
2259 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2261 function Compatible_Int_Bounds
2262 (Agg_Bounds : Node_Id;
2263 Typ_Bounds : Node_Id) return Boolean;
2264 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2265 -- assumed that both bounds are integer ranges.
2267 procedure Generate_Finalization_Actions;
2268 -- Deal with the various controlled type data structure initializations
2269 -- (but only if it hasn't been done already).
2271 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2272 -- Returns the first discriminant association in the constraint
2273 -- associated with T, if any, otherwise returns Empty.
2275 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2276 -- If the ancestor part is an unconstrained type and further ancestors
2277 -- do not provide discriminants for it, check aggregate components for
2278 -- values of the discriminants.
2280 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2281 -- If Typ is derived, and constrains discriminants of the parent type,
2282 -- these discriminants are not components of the aggregate, and must be
2283 -- initialized. The assignments are appended to List. The same is done
2284 -- if Typ derives fron an already constrained subtype of a discriminated
2285 -- parent type.
2287 procedure Init_Stored_Discriminants;
2288 -- If the type is derived and has inherited discriminants, generate
2289 -- explicit assignments for each, using the store constraint of the
2290 -- type. Note that both visible and stored discriminants must be
2291 -- initialized in case the derived type has some renamed and some
2292 -- constrained discriminants.
2294 procedure Init_Visible_Discriminants;
2295 -- If type has discriminants, retrieve their values from aggregate,
2296 -- and generate explicit assignments for each. This does not include
2297 -- discriminants inherited from ancestor, which are handled above.
2298 -- The type of the aggregate is a subtype created ealier using the
2299 -- given values of the discriminant components of the aggregate.
2301 procedure Initialize_Ctrl_Record_Component
2302 (Rec_Comp : Node_Id;
2303 Comp_Typ : Entity_Id;
2304 Init_Expr : Node_Id;
2305 Stmts : List_Id);
2306 -- Perform the initialization of controlled record component Rec_Comp.
2307 -- Comp_Typ is the component type. Init_Expr is the initialization
2308 -- expression for the record component. Hook-related declarations are
2309 -- inserted prior to aggregate N using Insert_Action. All remaining
2310 -- generated code is added to list Stmts.
2312 procedure Initialize_Record_Component
2313 (Rec_Comp : Node_Id;
2314 Comp_Typ : Entity_Id;
2315 Init_Expr : Node_Id;
2316 Stmts : List_Id);
2317 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2318 -- is the component type. Init_Expr is the initialization expression
2319 -- of the record component. All generated code is added to list Stmts.
2321 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2322 -- Check whether Bounds is a range node and its lower and higher bounds
2323 -- are integers literals.
2325 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2326 -- If the aggregate contains a self-reference, traverse each expression
2327 -- to replace a possible self-reference with a reference to the proper
2328 -- component of the target of the assignment.
2330 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2331 -- If default expression of a component mentions a discriminant of the
2332 -- type, it must be rewritten as the discriminant of the target object.
2334 ---------------------------------
2335 -- Ancestor_Discriminant_Value --
2336 ---------------------------------
2338 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2339 Assoc : Node_Id;
2340 Assoc_Elmt : Elmt_Id;
2341 Aggr_Comp : Entity_Id;
2342 Corresp_Disc : Entity_Id;
2343 Current_Typ : Entity_Id := Base_Type (Typ);
2344 Parent_Typ : Entity_Id;
2345 Parent_Disc : Entity_Id;
2346 Save_Assoc : Node_Id := Empty;
2348 begin
2349 -- First check any discriminant associations to see if any of them
2350 -- provide a value for the discriminant.
2352 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2353 Assoc := First (Component_Associations (N));
2354 while Present (Assoc) loop
2355 Aggr_Comp := Entity (First (Choices (Assoc)));
2357 if Ekind (Aggr_Comp) = E_Discriminant then
2358 Save_Assoc := Expression (Assoc);
2360 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2361 while Present (Corresp_Disc) loop
2363 -- If found a corresponding discriminant then return the
2364 -- value given in the aggregate. (Note: this is not
2365 -- correct in the presence of side effects. ???)
2367 if Disc = Corresp_Disc then
2368 return Duplicate_Subexpr (Expression (Assoc));
2369 end if;
2371 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2372 end loop;
2373 end if;
2375 Next (Assoc);
2376 end loop;
2377 end if;
2379 -- No match found in aggregate, so chain up parent types to find
2380 -- a constraint that defines the value of the discriminant.
2382 Parent_Typ := Etype (Current_Typ);
2383 while Current_Typ /= Parent_Typ loop
2384 if Has_Discriminants (Parent_Typ)
2385 and then not Has_Unknown_Discriminants (Parent_Typ)
2386 then
2387 Parent_Disc := First_Discriminant (Parent_Typ);
2389 -- We either get the association from the subtype indication
2390 -- of the type definition itself, or from the discriminant
2391 -- constraint associated with the type entity (which is
2392 -- preferable, but it's not always present ???)
2394 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2395 then
2396 Assoc := Get_Constraint_Association (Current_Typ);
2397 Assoc_Elmt := No_Elmt;
2398 else
2399 Assoc_Elmt :=
2400 First_Elmt (Discriminant_Constraint (Current_Typ));
2401 Assoc := Node (Assoc_Elmt);
2402 end if;
2404 -- Traverse the discriminants of the parent type looking
2405 -- for one that corresponds.
2407 while Present (Parent_Disc) and then Present (Assoc) loop
2408 Corresp_Disc := Parent_Disc;
2409 while Present (Corresp_Disc)
2410 and then Disc /= Corresp_Disc
2411 loop
2412 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2413 end loop;
2415 if Disc = Corresp_Disc then
2416 if Nkind (Assoc) = N_Discriminant_Association then
2417 Assoc := Expression (Assoc);
2418 end if;
2420 -- If the located association directly denotes
2421 -- a discriminant, then use the value of a saved
2422 -- association of the aggregate. This is an approach
2423 -- used to handle certain cases involving multiple
2424 -- discriminants mapped to a single discriminant of
2425 -- a descendant. It's not clear how to locate the
2426 -- appropriate discriminant value for such cases. ???
2428 if Is_Entity_Name (Assoc)
2429 and then Ekind (Entity (Assoc)) = E_Discriminant
2430 then
2431 Assoc := Save_Assoc;
2432 end if;
2434 return Duplicate_Subexpr (Assoc);
2435 end if;
2437 Next_Discriminant (Parent_Disc);
2439 if No (Assoc_Elmt) then
2440 Next (Assoc);
2442 else
2443 Next_Elmt (Assoc_Elmt);
2445 if Present (Assoc_Elmt) then
2446 Assoc := Node (Assoc_Elmt);
2447 else
2448 Assoc := Empty;
2449 end if;
2450 end if;
2451 end loop;
2452 end if;
2454 Current_Typ := Parent_Typ;
2455 Parent_Typ := Etype (Current_Typ);
2456 end loop;
2458 -- In some cases there's no ancestor value to locate (such as
2459 -- when an ancestor part given by an expression defines the
2460 -- discriminant value).
2462 return Empty;
2463 end Ancestor_Discriminant_Value;
2465 ----------------------------------
2466 -- Check_Ancestor_Discriminants --
2467 ----------------------------------
2469 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2470 Discr : Entity_Id;
2471 Disc_Value : Node_Id;
2472 Cond : Node_Id;
2474 begin
2475 Discr := First_Discriminant (Base_Type (Anc_Typ));
2476 while Present (Discr) loop
2477 Disc_Value := Ancestor_Discriminant_Value (Discr);
2479 if Present (Disc_Value) then
2480 Cond := Make_Op_Ne (Loc,
2481 Left_Opnd =>
2482 Make_Selected_Component (Loc,
2483 Prefix => New_Copy_Tree (Target),
2484 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2485 Right_Opnd => Disc_Value);
2487 Append_To (L,
2488 Make_Raise_Constraint_Error (Loc,
2489 Condition => Cond,
2490 Reason => CE_Discriminant_Check_Failed));
2491 end if;
2493 Next_Discriminant (Discr);
2494 end loop;
2495 end Check_Ancestor_Discriminants;
2497 ---------------------------
2498 -- Compatible_Int_Bounds --
2499 ---------------------------
2501 function Compatible_Int_Bounds
2502 (Agg_Bounds : Node_Id;
2503 Typ_Bounds : Node_Id) return Boolean
2505 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2506 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2507 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2508 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2509 begin
2510 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2511 end Compatible_Int_Bounds;
2513 -----------------------------------
2514 -- Generate_Finalization_Actions --
2515 -----------------------------------
2517 procedure Generate_Finalization_Actions is
2518 begin
2519 -- Do the work only the first time this is called
2521 if Finalization_Done then
2522 return;
2523 end if;
2525 Finalization_Done := True;
2527 -- Determine the external finalization list. It is either the
2528 -- finalization list of the outer scope or the one coming from an
2529 -- outer aggregate. When the target is not a temporary, the proper
2530 -- scope is the scope of the target rather than the potentially
2531 -- transient current scope.
2533 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2534 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2535 Set_Assignment_OK (Ref);
2537 Append_To (L,
2538 Make_Procedure_Call_Statement (Loc,
2539 Name =>
2540 New_Occurrence_Of
2541 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2542 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2543 end if;
2544 end Generate_Finalization_Actions;
2546 --------------------------------
2547 -- Get_Constraint_Association --
2548 --------------------------------
2550 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2551 Indic : Node_Id;
2552 Typ : Entity_Id;
2554 begin
2555 Typ := T;
2557 -- If type is private, get constraint from full view. This was
2558 -- previously done in an instance context, but is needed whenever
2559 -- the ancestor part has a discriminant, possibly inherited through
2560 -- multiple derivations.
2562 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2563 Typ := Full_View (Typ);
2564 end if;
2566 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2568 -- Verify that the subtype indication carries a constraint
2570 if Nkind (Indic) = N_Subtype_Indication
2571 and then Present (Constraint (Indic))
2572 then
2573 return First (Constraints (Constraint (Indic)));
2574 end if;
2576 return Empty;
2577 end Get_Constraint_Association;
2579 -------------------------------------
2580 -- Get_Explicit_Discriminant_Value --
2581 -------------------------------------
2583 function Get_Explicit_Discriminant_Value
2584 (D : Entity_Id) return Node_Id
2586 Assoc : Node_Id;
2587 Choice : Node_Id;
2588 Val : Node_Id;
2590 begin
2591 -- The aggregate has been normalized and all associations have a
2592 -- single choice.
2594 Assoc := First (Component_Associations (N));
2595 while Present (Assoc) loop
2596 Choice := First (Choices (Assoc));
2598 if Chars (Choice) = Chars (D) then
2599 Val := Expression (Assoc);
2600 Remove (Assoc);
2601 return Val;
2602 end if;
2604 Next (Assoc);
2605 end loop;
2607 return Empty;
2608 end Get_Explicit_Discriminant_Value;
2610 -------------------------------
2611 -- Init_Hidden_Discriminants --
2612 -------------------------------
2614 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2615 function Is_Completely_Hidden_Discriminant
2616 (Discr : Entity_Id) return Boolean;
2617 -- Determine whether Discr is a completely hidden discriminant of
2618 -- type Typ.
2620 ---------------------------------------
2621 -- Is_Completely_Hidden_Discriminant --
2622 ---------------------------------------
2624 function Is_Completely_Hidden_Discriminant
2625 (Discr : Entity_Id) return Boolean
2627 Item : Entity_Id;
2629 begin
2630 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2631 -- completely hidden discriminants.
2633 Item := First_Entity (Typ);
2634 while Present (Item) loop
2635 if Ekind (Item) = E_Discriminant
2636 and then Is_Completely_Hidden (Item)
2637 and then Chars (Original_Record_Component (Item)) =
2638 Chars (Discr)
2639 then
2640 return True;
2641 end if;
2643 Next_Entity (Item);
2644 end loop;
2646 return False;
2647 end Is_Completely_Hidden_Discriminant;
2649 -- Local variables
2651 Base_Typ : Entity_Id;
2652 Discr : Entity_Id;
2653 Discr_Constr : Elmt_Id;
2654 Discr_Init : Node_Id;
2655 Discr_Val : Node_Id;
2656 In_Aggr_Type : Boolean;
2657 Par_Typ : Entity_Id;
2659 -- Start of processing for Init_Hidden_Discriminants
2661 begin
2662 -- The constraints on the hidden discriminants, if present, are kept
2663 -- in the Stored_Constraint list of the type itself, or in that of
2664 -- the base type. If not in the constraints of the aggregate itself,
2665 -- we examine ancestors to find discriminants that are not renamed
2666 -- by other discriminants but constrained explicitly.
2668 In_Aggr_Type := True;
2670 Base_Typ := Base_Type (Typ);
2671 while Is_Derived_Type (Base_Typ)
2672 and then
2673 (Present (Stored_Constraint (Base_Typ))
2674 or else
2675 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2676 loop
2677 Par_Typ := Etype (Base_Typ);
2679 if not Has_Discriminants (Par_Typ) then
2680 return;
2681 end if;
2683 Discr := First_Discriminant (Par_Typ);
2685 -- We know that one of the stored-constraint lists is present
2687 if Present (Stored_Constraint (Base_Typ)) then
2688 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2690 -- For private extension, stored constraint may be on full view
2692 elsif Is_Private_Type (Base_Typ)
2693 and then Present (Full_View (Base_Typ))
2694 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2695 then
2696 Discr_Constr :=
2697 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2699 else
2700 Discr_Constr := First_Elmt (Stored_Constraint (Typ));
2701 end if;
2703 while Present (Discr) and then Present (Discr_Constr) loop
2704 Discr_Val := Node (Discr_Constr);
2706 -- The parent discriminant is renamed in the derived type,
2707 -- nothing to initialize.
2709 -- type Deriv_Typ (Discr : ...)
2710 -- is new Parent_Typ (Discr => Discr);
2712 if Is_Entity_Name (Discr_Val)
2713 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2714 then
2715 null;
2717 -- When the parent discriminant is constrained at the type
2718 -- extension level, it does not appear in the derived type.
2720 -- type Deriv_Typ (Discr : ...)
2721 -- is new Parent_Typ (Discr => Discr,
2722 -- Hidden_Discr => Expression);
2724 elsif Is_Completely_Hidden_Discriminant (Discr) then
2725 null;
2727 -- Otherwise initialize the discriminant
2729 else
2730 Discr_Init :=
2731 Make_OK_Assignment_Statement (Loc,
2732 Name =>
2733 Make_Selected_Component (Loc,
2734 Prefix => New_Copy_Tree (Target),
2735 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2736 Expression => New_Copy_Tree (Discr_Val));
2738 Append_To (List, Discr_Init);
2739 end if;
2741 Next_Elmt (Discr_Constr);
2742 Next_Discriminant (Discr);
2743 end loop;
2745 In_Aggr_Type := False;
2746 Base_Typ := Base_Type (Par_Typ);
2747 end loop;
2748 end Init_Hidden_Discriminants;
2750 --------------------------------
2751 -- Init_Visible_Discriminants --
2752 --------------------------------
2754 procedure Init_Visible_Discriminants is
2755 Discriminant : Entity_Id;
2756 Discriminant_Value : Node_Id;
2758 begin
2759 Discriminant := First_Discriminant (Typ);
2760 while Present (Discriminant) loop
2761 Comp_Expr :=
2762 Make_Selected_Component (Loc,
2763 Prefix => New_Copy_Tree (Target),
2764 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2766 Discriminant_Value :=
2767 Get_Discriminant_Value
2768 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2770 Instr :=
2771 Make_OK_Assignment_Statement (Loc,
2772 Name => Comp_Expr,
2773 Expression => New_Copy_Tree (Discriminant_Value));
2775 Append_To (L, Instr);
2777 Next_Discriminant (Discriminant);
2778 end loop;
2779 end Init_Visible_Discriminants;
2781 -------------------------------
2782 -- Init_Stored_Discriminants --
2783 -------------------------------
2785 procedure Init_Stored_Discriminants is
2786 Discriminant : Entity_Id;
2787 Discriminant_Value : Node_Id;
2789 begin
2790 Discriminant := First_Stored_Discriminant (Typ);
2791 while Present (Discriminant) loop
2792 Comp_Expr :=
2793 Make_Selected_Component (Loc,
2794 Prefix => New_Copy_Tree (Target),
2795 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2797 Discriminant_Value :=
2798 Get_Discriminant_Value
2799 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2801 Instr :=
2802 Make_OK_Assignment_Statement (Loc,
2803 Name => Comp_Expr,
2804 Expression => New_Copy_Tree (Discriminant_Value));
2806 Append_To (L, Instr);
2808 Next_Stored_Discriminant (Discriminant);
2809 end loop;
2810 end Init_Stored_Discriminants;
2812 --------------------------------------
2813 -- Initialize_Ctrl_Record_Component --
2814 --------------------------------------
2816 procedure Initialize_Ctrl_Record_Component
2817 (Rec_Comp : Node_Id;
2818 Comp_Typ : Entity_Id;
2819 Init_Expr : Node_Id;
2820 Stmts : List_Id)
2822 Fin_Call : Node_Id;
2823 Hook_Clear : Node_Id;
2825 In_Place_Expansion : Boolean;
2826 -- Flag set when a nonlimited controlled function call requires
2827 -- in-place expansion.
2829 begin
2830 -- Perform a preliminary analysis and resolution to determine what
2831 -- the initialization expression denotes. Unanalyzed function calls
2832 -- may appear as identifiers or indexed components.
2834 if Nkind_In (Init_Expr, N_Function_Call,
2835 N_Identifier,
2836 N_Indexed_Component)
2837 and then not Analyzed (Init_Expr)
2838 then
2839 Preanalyze_And_Resolve (Init_Expr, Comp_Typ);
2840 end if;
2842 In_Place_Expansion :=
2843 Nkind (Init_Expr) = N_Function_Call
2844 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
2846 -- The initialization expression is a controlled function call.
2847 -- Perform in-place removal of side effects to avoid creating a
2848 -- transient scope.
2850 -- This in-place expansion is not performed for limited transient
2851 -- objects because the initialization is already done in place.
2853 if In_Place_Expansion then
2855 -- Suppress the removal of side effects by general analysis
2856 -- because this behavior is emulated here. This avoids the
2857 -- generation of a transient scope, which leads to out-of-order
2858 -- adjustment and finalization.
2860 Set_No_Side_Effect_Removal (Init_Expr);
2862 -- Install all hook-related declarations and prepare the clean up
2863 -- statements. The generated code follows the initialization order
2864 -- of individual components and discriminants, rather than being
2865 -- inserted prior to the aggregate. This ensures that a transient
2866 -- component which mentions a discriminant has proper visibility
2867 -- of the discriminant.
2869 Process_Transient_Component
2870 (Loc => Loc,
2871 Comp_Typ => Comp_Typ,
2872 Init_Expr => Init_Expr,
2873 Fin_Call => Fin_Call,
2874 Hook_Clear => Hook_Clear,
2875 Stmts => Stmts);
2876 end if;
2878 -- Use the noncontrolled component initialization circuitry to
2879 -- assign the result of the function call to the record component.
2880 -- This also performs tag adjustment and [deep] adjustment of the
2881 -- record component.
2883 Initialize_Record_Component
2884 (Rec_Comp => Rec_Comp,
2885 Comp_Typ => Comp_Typ,
2886 Init_Expr => Init_Expr,
2887 Stmts => Stmts);
2889 -- At this point the record component is fully initialized. Complete
2890 -- the processing of the controlled record component by finalizing
2891 -- the transient function result.
2893 if In_Place_Expansion then
2894 Process_Transient_Component_Completion
2895 (Loc => Loc,
2896 Aggr => N,
2897 Fin_Call => Fin_Call,
2898 Hook_Clear => Hook_Clear,
2899 Stmts => Stmts);
2900 end if;
2901 end Initialize_Ctrl_Record_Component;
2903 ---------------------------------
2904 -- Initialize_Record_Component --
2905 ---------------------------------
2907 procedure Initialize_Record_Component
2908 (Rec_Comp : Node_Id;
2909 Comp_Typ : Entity_Id;
2910 Init_Expr : Node_Id;
2911 Stmts : List_Id)
2913 Exceptions_OK : constant Boolean :=
2914 not Restriction_Active (No_Exception_Propagation);
2916 Finalization_OK : constant Boolean := Needs_Finalization (Comp_Typ);
2918 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
2919 Adj_Call : Node_Id;
2920 Blk_Stmts : List_Id;
2921 Init_Stmt : Node_Id;
2923 begin
2924 -- Protect the initialization statements from aborts. Generate:
2926 -- Abort_Defer;
2928 if Finalization_OK and Abort_Allowed then
2929 if Exceptions_OK then
2930 Blk_Stmts := New_List;
2931 else
2932 Blk_Stmts := Stmts;
2933 end if;
2935 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
2937 -- Otherwise aborts are not allowed. All generated code is added
2938 -- directly to the input list.
2940 else
2941 Blk_Stmts := Stmts;
2942 end if;
2944 -- Initialize the record component. Generate:
2946 -- Rec_Comp := Init_Expr;
2948 -- Note that the initialization expression is NOT replicated because
2949 -- only a single component may be initialized by it.
2951 Init_Stmt :=
2952 Make_OK_Assignment_Statement (Loc,
2953 Name => New_Copy_Tree (Rec_Comp),
2954 Expression => Init_Expr);
2955 Set_No_Ctrl_Actions (Init_Stmt);
2957 Append_To (Blk_Stmts, Init_Stmt);
2959 -- Adjust the tag due to a possible view conversion. Generate:
2961 -- Rec_Comp._tag := Full_TypeP;
2963 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
2964 Append_To (Blk_Stmts,
2965 Make_OK_Assignment_Statement (Loc,
2966 Name =>
2967 Make_Selected_Component (Loc,
2968 Prefix => New_Copy_Tree (Rec_Comp),
2969 Selector_Name =>
2970 New_Occurrence_Of
2971 (First_Tag_Component (Full_Typ), Loc)),
2973 Expression =>
2974 Unchecked_Convert_To (RTE (RE_Tag),
2975 New_Occurrence_Of
2976 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
2977 Loc))));
2978 end if;
2980 -- Adjust the component. Generate:
2982 -- [Deep_]Adjust (Rec_Comp);
2984 if Finalization_OK
2985 and then not Is_Limited_Type (Comp_Typ)
2986 and then not Is_Build_In_Place_Function_Call (Init_Expr)
2987 then
2988 Adj_Call :=
2989 Make_Adjust_Call
2990 (Obj_Ref => New_Copy_Tree (Rec_Comp),
2991 Typ => Comp_Typ);
2993 -- Guard against a missing [Deep_]Adjust when the component type
2994 -- was not properly frozen.
2996 if Present (Adj_Call) then
2997 Append_To (Blk_Stmts, Adj_Call);
2998 end if;
2999 end if;
3001 -- Complete the protection of the initialization statements
3003 if Finalization_OK and Abort_Allowed then
3005 -- Wrap the initialization statements in a block to catch a
3006 -- potential exception. Generate:
3008 -- begin
3009 -- Abort_Defer;
3010 -- Rec_Comp := Init_Expr;
3011 -- Rec_Comp._tag := Full_TypP;
3012 -- [Deep_]Adjust (Rec_Comp);
3013 -- at end
3014 -- Abort_Undefer_Direct;
3015 -- end;
3017 if Exceptions_OK then
3018 Append_To (Stmts,
3019 Build_Abort_Undefer_Block (Loc,
3020 Stmts => Blk_Stmts,
3021 Context => N));
3023 -- Otherwise exceptions are not propagated. Generate:
3025 -- Abort_Defer;
3026 -- Rec_Comp := Init_Expr;
3027 -- Rec_Comp._tag := Full_TypP;
3028 -- [Deep_]Adjust (Rec_Comp);
3029 -- Abort_Undefer;
3031 else
3032 Append_To (Blk_Stmts,
3033 Build_Runtime_Call (Loc, RE_Abort_Undefer));
3034 end if;
3035 end if;
3036 end Initialize_Record_Component;
3038 -------------------------
3039 -- Is_Int_Range_Bounds --
3040 -------------------------
3042 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
3043 begin
3044 return Nkind (Bounds) = N_Range
3045 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
3046 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
3047 end Is_Int_Range_Bounds;
3049 ------------------
3050 -- Replace_Type --
3051 ------------------
3053 function Replace_Type (Expr : Node_Id) return Traverse_Result is
3054 begin
3055 -- Note regarding the Root_Type test below: Aggregate components for
3056 -- self-referential types include attribute references to the current
3057 -- instance, of the form: Typ'access, etc.. These references are
3058 -- rewritten as references to the target of the aggregate: the
3059 -- left-hand side of an assignment, the entity in a declaration,
3060 -- or a temporary. Without this test, we would improperly extended
3061 -- this rewriting to attribute references whose prefix was not the
3062 -- type of the aggregate.
3064 if Nkind (Expr) = N_Attribute_Reference
3065 and then Is_Entity_Name (Prefix (Expr))
3066 and then Is_Type (Entity (Prefix (Expr)))
3067 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
3068 then
3069 if Is_Entity_Name (Lhs) then
3070 Rewrite (Prefix (Expr), New_Occurrence_Of (Entity (Lhs), Loc));
3072 else
3073 Rewrite (Expr,
3074 Make_Attribute_Reference (Loc,
3075 Attribute_Name => Name_Unrestricted_Access,
3076 Prefix => New_Copy_Tree (Lhs)));
3077 Set_Analyzed (Parent (Expr), False);
3078 end if;
3079 end if;
3081 return OK;
3082 end Replace_Type;
3084 --------------------------
3085 -- Rewrite_Discriminant --
3086 --------------------------
3088 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
3089 begin
3090 if Is_Entity_Name (Expr)
3091 and then Present (Entity (Expr))
3092 and then Ekind (Entity (Expr)) = E_In_Parameter
3093 and then Present (Discriminal_Link (Entity (Expr)))
3094 and then Scope (Discriminal_Link (Entity (Expr))) =
3095 Base_Type (Etype (N))
3096 then
3097 Rewrite (Expr,
3098 Make_Selected_Component (Loc,
3099 Prefix => New_Copy_Tree (Lhs),
3100 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
3101 end if;
3103 return OK;
3104 end Rewrite_Discriminant;
3106 procedure Replace_Discriminants is
3107 new Traverse_Proc (Rewrite_Discriminant);
3109 procedure Replace_Self_Reference is
3110 new Traverse_Proc (Replace_Type);
3112 -- Start of processing for Build_Record_Aggr_Code
3114 begin
3115 if Has_Self_Reference (N) then
3116 Replace_Self_Reference (N);
3117 end if;
3119 -- If the target of the aggregate is class-wide, we must convert it
3120 -- to the actual type of the aggregate, so that the proper components
3121 -- are visible. We know already that the types are compatible.
3123 if Present (Etype (Lhs))
3124 and then Is_Class_Wide_Type (Etype (Lhs))
3125 then
3126 Target := Unchecked_Convert_To (Typ, Lhs);
3127 else
3128 Target := Lhs;
3129 end if;
3131 -- Deal with the ancestor part of extension aggregates or with the
3132 -- discriminants of the root type.
3134 if Nkind (N) = N_Extension_Aggregate then
3135 declare
3136 Ancestor : constant Node_Id := Ancestor_Part (N);
3137 Adj_Call : Node_Id;
3138 Assign : List_Id;
3140 begin
3141 -- If the ancestor part is a subtype mark "T", we generate
3143 -- init-proc (T (tmp)); if T is constrained and
3144 -- init-proc (S (tmp)); where S applies an appropriate
3145 -- constraint if T is unconstrained
3147 if Is_Entity_Name (Ancestor)
3148 and then Is_Type (Entity (Ancestor))
3149 then
3150 Ancestor_Is_Subtype_Mark := True;
3152 if Is_Constrained (Entity (Ancestor)) then
3153 Init_Typ := Entity (Ancestor);
3155 -- For an ancestor part given by an unconstrained type mark,
3156 -- create a subtype constrained by appropriate corresponding
3157 -- discriminant values coming from either associations of the
3158 -- aggregate or a constraint on a parent type. The subtype will
3159 -- be used to generate the correct default value for the
3160 -- ancestor part.
3162 elsif Has_Discriminants (Entity (Ancestor)) then
3163 declare
3164 Anc_Typ : constant Entity_Id := Entity (Ancestor);
3165 Anc_Constr : constant List_Id := New_List;
3166 Discrim : Entity_Id;
3167 Disc_Value : Node_Id;
3168 New_Indic : Node_Id;
3169 Subt_Decl : Node_Id;
3171 begin
3172 Discrim := First_Discriminant (Anc_Typ);
3173 while Present (Discrim) loop
3174 Disc_Value := Ancestor_Discriminant_Value (Discrim);
3176 -- If no usable discriminant in ancestors, check
3177 -- whether aggregate has an explicit value for it.
3179 if No (Disc_Value) then
3180 Disc_Value :=
3181 Get_Explicit_Discriminant_Value (Discrim);
3182 end if;
3184 Append_To (Anc_Constr, Disc_Value);
3185 Next_Discriminant (Discrim);
3186 end loop;
3188 New_Indic :=
3189 Make_Subtype_Indication (Loc,
3190 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
3191 Constraint =>
3192 Make_Index_Or_Discriminant_Constraint (Loc,
3193 Constraints => Anc_Constr));
3195 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
3197 Subt_Decl :=
3198 Make_Subtype_Declaration (Loc,
3199 Defining_Identifier => Init_Typ,
3200 Subtype_Indication => New_Indic);
3202 -- Itypes must be analyzed with checks off Declaration
3203 -- must have a parent for proper handling of subsidiary
3204 -- actions.
3206 Set_Parent (Subt_Decl, N);
3207 Analyze (Subt_Decl, Suppress => All_Checks);
3208 end;
3209 end if;
3211 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3212 Set_Assignment_OK (Ref);
3214 if not Is_Interface (Init_Typ) then
3215 Append_List_To (L,
3216 Build_Initialization_Call (Loc,
3217 Id_Ref => Ref,
3218 Typ => Init_Typ,
3219 In_Init_Proc => Within_Init_Proc,
3220 With_Default_Init => Has_Default_Init_Comps (N)
3221 or else
3222 Has_Task (Base_Type (Init_Typ))));
3224 if Is_Constrained (Entity (Ancestor))
3225 and then Has_Discriminants (Entity (Ancestor))
3226 then
3227 Check_Ancestor_Discriminants (Entity (Ancestor));
3228 end if;
3229 end if;
3231 -- Handle calls to C++ constructors
3233 elsif Is_CPP_Constructor_Call (Ancestor) then
3234 Init_Typ := Etype (Ancestor);
3235 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3236 Set_Assignment_OK (Ref);
3238 Append_List_To (L,
3239 Build_Initialization_Call (Loc,
3240 Id_Ref => Ref,
3241 Typ => Init_Typ,
3242 In_Init_Proc => Within_Init_Proc,
3243 With_Default_Init => Has_Default_Init_Comps (N),
3244 Constructor_Ref => Ancestor));
3246 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3247 -- limited type, a recursive call expands the ancestor. Note that
3248 -- in the limited case, the ancestor part must be either a
3249 -- function call (possibly qualified) or aggregate (definitely
3250 -- qualified).
3252 elsif Is_Limited_Type (Etype (Ancestor))
3253 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
3254 N_Extension_Aggregate)
3255 then
3256 Ancestor_Is_Expression := True;
3258 -- Set up finalization data for enclosing record, because
3259 -- controlled subcomponents of the ancestor part will be
3260 -- attached to it.
3262 Generate_Finalization_Actions;
3264 Append_List_To (L,
3265 Build_Record_Aggr_Code
3266 (N => Unqualify (Ancestor),
3267 Typ => Etype (Unqualify (Ancestor)),
3268 Lhs => Target));
3270 -- If the ancestor part is an expression "E", we generate
3272 -- T (tmp) := E;
3274 -- In Ada 2005, this includes the case of a (possibly qualified)
3275 -- limited function call. The assignment will turn into a
3276 -- build-in-place function call (for further details, see
3277 -- Make_Build_In_Place_Call_In_Assignment).
3279 else
3280 Ancestor_Is_Expression := True;
3281 Init_Typ := Etype (Ancestor);
3283 -- If the ancestor part is an aggregate, force its full
3284 -- expansion, which was delayed.
3286 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
3287 N_Extension_Aggregate)
3288 then
3289 Set_Analyzed (Ancestor, False);
3290 Set_Analyzed (Expression (Ancestor), False);
3291 end if;
3293 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3294 Set_Assignment_OK (Ref);
3296 -- Make the assignment without usual controlled actions, since
3297 -- we only want to Adjust afterwards, but not to Finalize
3298 -- beforehand. Add manual Adjust when necessary.
3300 Assign := New_List (
3301 Make_OK_Assignment_Statement (Loc,
3302 Name => Ref,
3303 Expression => Ancestor));
3304 Set_No_Ctrl_Actions (First (Assign));
3306 -- Assign the tag now to make sure that the dispatching call in
3307 -- the subsequent deep_adjust works properly (unless
3308 -- Tagged_Type_Expansion where tags are implicit).
3310 if Tagged_Type_Expansion then
3311 Instr :=
3312 Make_OK_Assignment_Statement (Loc,
3313 Name =>
3314 Make_Selected_Component (Loc,
3315 Prefix => New_Copy_Tree (Target),
3316 Selector_Name =>
3317 New_Occurrence_Of
3318 (First_Tag_Component (Base_Type (Typ)), Loc)),
3320 Expression =>
3321 Unchecked_Convert_To (RTE (RE_Tag),
3322 New_Occurrence_Of
3323 (Node (First_Elmt
3324 (Access_Disp_Table (Base_Type (Typ)))),
3325 Loc)));
3327 Set_Assignment_OK (Name (Instr));
3328 Append_To (Assign, Instr);
3330 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3331 -- also initialize tags of the secondary dispatch tables.
3333 if Has_Interfaces (Base_Type (Typ)) then
3334 Init_Secondary_Tags
3335 (Typ => Base_Type (Typ),
3336 Target => Target,
3337 Stmts_List => Assign,
3338 Init_Tags_List => Assign);
3339 end if;
3340 end if;
3342 -- Call Adjust manually
3344 if Needs_Finalization (Etype (Ancestor))
3345 and then not Is_Limited_Type (Etype (Ancestor))
3346 and then not Is_Build_In_Place_Function_Call (Ancestor)
3347 then
3348 Adj_Call :=
3349 Make_Adjust_Call
3350 (Obj_Ref => New_Copy_Tree (Ref),
3351 Typ => Etype (Ancestor));
3353 -- Guard against a missing [Deep_]Adjust when the ancestor
3354 -- type was not properly frozen.
3356 if Present (Adj_Call) then
3357 Append_To (Assign, Adj_Call);
3358 end if;
3359 end if;
3361 Append_To (L,
3362 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
3364 if Has_Discriminants (Init_Typ) then
3365 Check_Ancestor_Discriminants (Init_Typ);
3366 end if;
3367 end if;
3369 pragma Assert (Nkind (N) = N_Extension_Aggregate);
3370 pragma Assert
3371 (not (Ancestor_Is_Expression and Ancestor_Is_Subtype_Mark));
3372 end;
3374 -- Generate assignments of hidden discriminants. If the base type is
3375 -- an unchecked union, the discriminants are unknown to the back-end
3376 -- and absent from a value of the type, so assignments for them are
3377 -- not emitted.
3379 if Has_Discriminants (Typ)
3380 and then not Is_Unchecked_Union (Base_Type (Typ))
3381 then
3382 Init_Hidden_Discriminants (Typ, L);
3383 end if;
3385 -- Normal case (not an extension aggregate)
3387 else
3388 -- Generate the discriminant expressions, component by component.
3389 -- If the base type is an unchecked union, the discriminants are
3390 -- unknown to the back-end and absent from a value of the type, so
3391 -- assignments for them are not emitted.
3393 if Has_Discriminants (Typ)
3394 and then not Is_Unchecked_Union (Base_Type (Typ))
3395 then
3396 Init_Hidden_Discriminants (Typ, L);
3398 -- Generate discriminant init values for the visible discriminants
3400 Init_Visible_Discriminants;
3402 if Is_Derived_Type (N_Typ) then
3403 Init_Stored_Discriminants;
3404 end if;
3405 end if;
3406 end if;
3408 -- For CPP types we generate an implicit call to the C++ default
3409 -- constructor to ensure the proper initialization of the _Tag
3410 -- component.
3412 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3413 Invoke_Constructor : declare
3414 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3416 procedure Invoke_IC_Proc (T : Entity_Id);
3417 -- Recursive routine used to climb to parents. Required because
3418 -- parents must be initialized before descendants to ensure
3419 -- propagation of inherited C++ slots.
3421 --------------------
3422 -- Invoke_IC_Proc --
3423 --------------------
3425 procedure Invoke_IC_Proc (T : Entity_Id) is
3426 begin
3427 -- Avoid generating extra calls. Initialization required
3428 -- only for types defined from the level of derivation of
3429 -- type of the constructor and the type of the aggregate.
3431 if T = CPP_Parent then
3432 return;
3433 end if;
3435 Invoke_IC_Proc (Etype (T));
3437 -- Generate call to the IC routine
3439 if Present (CPP_Init_Proc (T)) then
3440 Append_To (L,
3441 Make_Procedure_Call_Statement (Loc,
3442 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3443 end if;
3444 end Invoke_IC_Proc;
3446 -- Start of processing for Invoke_Constructor
3448 begin
3449 -- Implicit invocation of the C++ constructor
3451 if Nkind (N) = N_Aggregate then
3452 Append_To (L,
3453 Make_Procedure_Call_Statement (Loc,
3454 Name =>
3455 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3456 Parameter_Associations => New_List (
3457 Unchecked_Convert_To (CPP_Parent,
3458 New_Copy_Tree (Lhs)))));
3459 end if;
3461 Invoke_IC_Proc (Typ);
3462 end Invoke_Constructor;
3463 end if;
3465 -- Generate the assignments, component by component
3467 -- tmp.comp1 := Expr1_From_Aggr;
3468 -- tmp.comp2 := Expr2_From_Aggr;
3469 -- ....
3471 Comp := First (Component_Associations (N));
3472 while Present (Comp) loop
3473 Selector := Entity (First (Choices (Comp)));
3475 -- C++ constructors
3477 if Is_CPP_Constructor_Call (Expression (Comp)) then
3478 Append_List_To (L,
3479 Build_Initialization_Call (Loc,
3480 Id_Ref =>
3481 Make_Selected_Component (Loc,
3482 Prefix => New_Copy_Tree (Target),
3483 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3484 Typ => Etype (Selector),
3485 Enclos_Type => Typ,
3486 With_Default_Init => True,
3487 Constructor_Ref => Expression (Comp)));
3489 -- Ada 2005 (AI-287): For each default-initialized component generate
3490 -- a call to the corresponding IP subprogram if available.
3492 elsif Box_Present (Comp)
3493 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3494 then
3495 if Ekind (Selector) /= E_Discriminant then
3496 Generate_Finalization_Actions;
3497 end if;
3499 -- Ada 2005 (AI-287): If the component type has tasks then
3500 -- generate the activation chain and master entities (except
3501 -- in case of an allocator because in that case these entities
3502 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3504 declare
3505 Ctype : constant Entity_Id := Etype (Selector);
3506 Inside_Allocator : Boolean := False;
3507 P : Node_Id := Parent (N);
3509 begin
3510 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3511 while Present (P) loop
3512 if Nkind (P) = N_Allocator then
3513 Inside_Allocator := True;
3514 exit;
3515 end if;
3517 P := Parent (P);
3518 end loop;
3520 if not Inside_Init_Proc and not Inside_Allocator then
3521 Build_Activation_Chain_Entity (N);
3522 end if;
3523 end if;
3524 end;
3526 Append_List_To (L,
3527 Build_Initialization_Call (Loc,
3528 Id_Ref => Make_Selected_Component (Loc,
3529 Prefix => New_Copy_Tree (Target),
3530 Selector_Name =>
3531 New_Occurrence_Of (Selector, Loc)),
3532 Typ => Etype (Selector),
3533 Enclos_Type => Typ,
3534 With_Default_Init => True));
3536 -- Prepare for component assignment
3538 elsif Ekind (Selector) /= E_Discriminant
3539 or else Nkind (N) = N_Extension_Aggregate
3540 then
3541 -- All the discriminants have now been assigned
3543 -- This is now a good moment to initialize and attach all the
3544 -- controllers. Their position may depend on the discriminants.
3546 if Ekind (Selector) /= E_Discriminant then
3547 Generate_Finalization_Actions;
3548 end if;
3550 Comp_Type := Underlying_Type (Etype (Selector));
3551 Comp_Expr :=
3552 Make_Selected_Component (Loc,
3553 Prefix => New_Copy_Tree (Target),
3554 Selector_Name => New_Occurrence_Of (Selector, Loc));
3556 if Nkind (Expression (Comp)) = N_Qualified_Expression then
3557 Expr_Q := Expression (Expression (Comp));
3558 else
3559 Expr_Q := Expression (Comp);
3560 end if;
3562 -- Now either create the assignment or generate the code for the
3563 -- inner aggregate top-down.
3565 if Is_Delayed_Aggregate (Expr_Q) then
3567 -- We have the following case of aggregate nesting inside
3568 -- an object declaration:
3570 -- type Arr_Typ is array (Integer range <>) of ...;
3572 -- type Rec_Typ (...) is record
3573 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3574 -- end record;
3576 -- Obj_Rec_Typ : Rec_Typ := (...,
3577 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3579 -- The length of the ranges of the aggregate and Obj_Add_Typ
3580 -- are equal (B - A = Y - X), but they do not coincide (X /=
3581 -- A and B /= Y). This case requires array sliding which is
3582 -- performed in the following manner:
3584 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3585 -- Temp : Arr_Sub;
3586 -- Temp (X) := (...);
3587 -- ...
3588 -- Temp (Y) := (...);
3589 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3591 if Ekind (Comp_Type) = E_Array_Subtype
3592 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3593 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3594 and then not
3595 Compatible_Int_Bounds
3596 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3597 Typ_Bounds => First_Index (Comp_Type))
3598 then
3599 -- Create the array subtype with bounds equal to those of
3600 -- the corresponding aggregate.
3602 declare
3603 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3605 SubD : constant Node_Id :=
3606 Make_Subtype_Declaration (Loc,
3607 Defining_Identifier => SubE,
3608 Subtype_Indication =>
3609 Make_Subtype_Indication (Loc,
3610 Subtype_Mark =>
3611 New_Occurrence_Of (Etype (Comp_Type), Loc),
3612 Constraint =>
3613 Make_Index_Or_Discriminant_Constraint
3614 (Loc,
3615 Constraints => New_List (
3616 New_Copy_Tree
3617 (Aggregate_Bounds (Expr_Q))))));
3619 -- Create a temporary array of the above subtype which
3620 -- will be used to capture the aggregate assignments.
3622 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3624 TmpD : constant Node_Id :=
3625 Make_Object_Declaration (Loc,
3626 Defining_Identifier => TmpE,
3627 Object_Definition => New_Occurrence_Of (SubE, Loc));
3629 begin
3630 Set_No_Initialization (TmpD);
3631 Append_To (L, SubD);
3632 Append_To (L, TmpD);
3634 -- Expand aggregate into assignments to the temp array
3636 Append_List_To (L,
3637 Late_Expansion (Expr_Q, Comp_Type,
3638 New_Occurrence_Of (TmpE, Loc)));
3640 -- Slide
3642 Append_To (L,
3643 Make_Assignment_Statement (Loc,
3644 Name => New_Copy_Tree (Comp_Expr),
3645 Expression => New_Occurrence_Of (TmpE, Loc)));
3646 end;
3648 -- Normal case (sliding not required)
3650 else
3651 Append_List_To (L,
3652 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3653 end if;
3655 -- Expr_Q is not delayed aggregate
3657 else
3658 if Has_Discriminants (Typ) then
3659 Replace_Discriminants (Expr_Q);
3661 -- If the component is an array type that depends on
3662 -- discriminants, and the expression is a single Others
3663 -- clause, create an explicit subtype for it because the
3664 -- backend has troubles recovering the actual bounds.
3666 if Nkind (Expr_Q) = N_Aggregate
3667 and then Is_Array_Type (Comp_Type)
3668 and then Present (Component_Associations (Expr_Q))
3669 then
3670 declare
3671 Assoc : constant Node_Id :=
3672 First (Component_Associations (Expr_Q));
3673 Decl : Node_Id;
3675 begin
3676 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3677 then
3678 Decl :=
3679 Build_Actual_Subtype_Of_Component
3680 (Comp_Type, Comp_Expr);
3682 -- If the component type does not in fact depend on
3683 -- discriminants, the subtype declaration is empty.
3685 if Present (Decl) then
3686 Append_To (L, Decl);
3687 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3688 end if;
3689 end if;
3690 end;
3691 end if;
3692 end if;
3694 if Modify_Tree_For_C
3695 and then Nkind (Expr_Q) = N_Aggregate
3696 and then Is_Array_Type (Etype (Expr_Q))
3697 and then Present (First_Index (Etype (Expr_Q)))
3698 then
3699 declare
3700 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3701 begin
3702 Append_List_To (L,
3703 Build_Array_Aggr_Code
3704 (N => Expr_Q,
3705 Ctype => Component_Type (Expr_Q_Type),
3706 Index => First_Index (Expr_Q_Type),
3707 Into => Comp_Expr,
3708 Scalar_Comp =>
3709 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
3710 end;
3712 else
3713 -- Handle an initialization expression of a controlled type
3714 -- in case it denotes a function call. In general such a
3715 -- scenario will produce a transient scope, but this will
3716 -- lead to wrong order of initialization, adjustment, and
3717 -- finalization in the context of aggregates.
3719 -- Target.Comp := Ctrl_Func_Call;
3721 -- begin -- scope
3722 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3723 -- Target.Comp := Trans_Obj;
3724 -- Finalize (Trans_Obj);
3725 -- end
3726 -- Target.Comp._tag := ...;
3727 -- Adjust (Target.Comp);
3729 -- In the example above, the call to Finalize occurs too
3730 -- early and as a result it may leave the record component
3731 -- in a bad state. Finalization of the transient object
3732 -- should really happen after adjustment.
3734 -- To avoid this scenario, perform in-place side-effect
3735 -- removal of the function call. This eliminates the
3736 -- transient property of the function result and ensures
3737 -- correct order of actions.
3739 -- Res : ... := Ctrl_Func_Call;
3740 -- Target.Comp := Res;
3741 -- Target.Comp._tag := ...;
3742 -- Adjust (Target.Comp);
3743 -- Finalize (Res);
3745 if Needs_Finalization (Comp_Type)
3746 and then Nkind (Expr_Q) /= N_Aggregate
3747 then
3748 Initialize_Ctrl_Record_Component
3749 (Rec_Comp => Comp_Expr,
3750 Comp_Typ => Etype (Selector),
3751 Init_Expr => Expr_Q,
3752 Stmts => L);
3754 -- Otherwise perform single component initialization
3756 else
3757 Initialize_Record_Component
3758 (Rec_Comp => Comp_Expr,
3759 Comp_Typ => Etype (Selector),
3760 Init_Expr => Expr_Q,
3761 Stmts => L);
3762 end if;
3763 end if;
3764 end if;
3766 -- comment would be good here ???
3768 elsif Ekind (Selector) = E_Discriminant
3769 and then Nkind (N) /= N_Extension_Aggregate
3770 and then Nkind (Parent (N)) = N_Component_Association
3771 and then Is_Constrained (Typ)
3772 then
3773 -- We must check that the discriminant value imposed by the
3774 -- context is the same as the value given in the subaggregate,
3775 -- because after the expansion into assignments there is no
3776 -- record on which to perform a regular discriminant check.
3778 declare
3779 D_Val : Elmt_Id;
3780 Disc : Entity_Id;
3782 begin
3783 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3784 Disc := First_Discriminant (Typ);
3785 while Chars (Disc) /= Chars (Selector) loop
3786 Next_Discriminant (Disc);
3787 Next_Elmt (D_Val);
3788 end loop;
3790 pragma Assert (Present (D_Val));
3792 -- This check cannot performed for components that are
3793 -- constrained by a current instance, because this is not a
3794 -- value that can be compared with the actual constraint.
3796 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3797 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3798 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3799 then
3800 Append_To (L,
3801 Make_Raise_Constraint_Error (Loc,
3802 Condition =>
3803 Make_Op_Ne (Loc,
3804 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3805 Right_Opnd => Expression (Comp)),
3806 Reason => CE_Discriminant_Check_Failed));
3808 else
3809 -- Find self-reference in previous discriminant assignment,
3810 -- and replace with proper expression.
3812 declare
3813 Ass : Node_Id;
3815 begin
3816 Ass := First (L);
3817 while Present (Ass) loop
3818 if Nkind (Ass) = N_Assignment_Statement
3819 and then Nkind (Name (Ass)) = N_Selected_Component
3820 and then Chars (Selector_Name (Name (Ass))) =
3821 Chars (Disc)
3822 then
3823 Set_Expression
3824 (Ass, New_Copy_Tree (Expression (Comp)));
3825 exit;
3826 end if;
3827 Next (Ass);
3828 end loop;
3829 end;
3830 end if;
3831 end;
3832 end if;
3834 Next (Comp);
3835 end loop;
3837 -- If the type is tagged, the tag needs to be initialized (unless we
3838 -- are in VM-mode where tags are implicit). It is done late in the
3839 -- initialization process because in some cases, we call the init
3840 -- proc of an ancestor which will not leave out the right tag.
3842 if Ancestor_Is_Expression then
3843 null;
3845 -- For CPP types we generated a call to the C++ default constructor
3846 -- before the components have been initialized to ensure the proper
3847 -- initialization of the _Tag component (see above).
3849 elsif Is_CPP_Class (Typ) then
3850 null;
3852 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3853 Instr :=
3854 Make_OK_Assignment_Statement (Loc,
3855 Name =>
3856 Make_Selected_Component (Loc,
3857 Prefix => New_Copy_Tree (Target),
3858 Selector_Name =>
3859 New_Occurrence_Of
3860 (First_Tag_Component (Base_Type (Typ)), Loc)),
3862 Expression =>
3863 Unchecked_Convert_To (RTE (RE_Tag),
3864 New_Occurrence_Of
3865 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3866 Loc)));
3868 Append_To (L, Instr);
3870 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3871 -- abstract interfaces we must also initialize the tags of the
3872 -- secondary dispatch tables.
3874 if Has_Interfaces (Base_Type (Typ)) then
3875 Init_Secondary_Tags
3876 (Typ => Base_Type (Typ),
3877 Target => Target,
3878 Stmts_List => L,
3879 Init_Tags_List => L);
3880 end if;
3881 end if;
3883 -- If the controllers have not been initialized yet (by lack of non-
3884 -- discriminant components), let's do it now.
3886 Generate_Finalization_Actions;
3888 return L;
3889 end Build_Record_Aggr_Code;
3891 ---------------------------------------
3892 -- Collect_Initialization_Statements --
3893 ---------------------------------------
3895 procedure Collect_Initialization_Statements
3896 (Obj : Entity_Id;
3897 N : Node_Id;
3898 Node_After : Node_Id)
3900 Loc : constant Source_Ptr := Sloc (N);
3901 Init_Actions : constant List_Id := New_List;
3902 Init_Node : Node_Id;
3903 Comp_Stmt : Node_Id;
3905 begin
3906 -- Nothing to do if Obj is already frozen, as in this case we known we
3907 -- won't need to move the initialization statements about later on.
3909 if Is_Frozen (Obj) then
3910 return;
3911 end if;
3913 Init_Node := N;
3914 while Next (Init_Node) /= Node_After loop
3915 Append_To (Init_Actions, Remove_Next (Init_Node));
3916 end loop;
3918 if not Is_Empty_List (Init_Actions) then
3919 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3920 Insert_Action_After (Init_Node, Comp_Stmt);
3921 Set_Initialization_Statements (Obj, Comp_Stmt);
3922 end if;
3923 end Collect_Initialization_Statements;
3925 -------------------------------
3926 -- Convert_Aggr_In_Allocator --
3927 -------------------------------
3929 procedure Convert_Aggr_In_Allocator
3930 (Alloc : Node_Id;
3931 Decl : Node_Id;
3932 Aggr : Node_Id)
3934 Loc : constant Source_Ptr := Sloc (Aggr);
3935 Typ : constant Entity_Id := Etype (Aggr);
3936 Temp : constant Entity_Id := Defining_Identifier (Decl);
3938 Occ : constant Node_Id :=
3939 Unchecked_Convert_To (Typ,
3940 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3942 begin
3943 if Is_Array_Type (Typ) then
3944 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3946 elsif Has_Default_Init_Comps (Aggr) then
3947 declare
3948 L : constant List_Id := New_List;
3949 Init_Stmts : List_Id;
3951 begin
3952 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3954 if Has_Task (Typ) then
3955 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3956 Insert_Actions (Alloc, L);
3957 else
3958 Insert_Actions (Alloc, Init_Stmts);
3959 end if;
3960 end;
3962 else
3963 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3964 end if;
3965 end Convert_Aggr_In_Allocator;
3967 --------------------------------
3968 -- Convert_Aggr_In_Assignment --
3969 --------------------------------
3971 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3972 Aggr : Node_Id := Expression (N);
3973 Typ : constant Entity_Id := Etype (Aggr);
3974 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3976 begin
3977 if Nkind (Aggr) = N_Qualified_Expression then
3978 Aggr := Expression (Aggr);
3979 end if;
3981 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3982 end Convert_Aggr_In_Assignment;
3984 ---------------------------------
3985 -- Convert_Aggr_In_Object_Decl --
3986 ---------------------------------
3988 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3989 Obj : constant Entity_Id := Defining_Identifier (N);
3990 Aggr : Node_Id := Expression (N);
3991 Loc : constant Source_Ptr := Sloc (Aggr);
3992 Typ : constant Entity_Id := Etype (Aggr);
3993 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3995 function Discriminants_Ok return Boolean;
3996 -- If the object type is constrained, the discriminants in the
3997 -- aggregate must be checked against the discriminants of the subtype.
3998 -- This cannot be done using Apply_Discriminant_Checks because after
3999 -- expansion there is no aggregate left to check.
4001 ----------------------
4002 -- Discriminants_Ok --
4003 ----------------------
4005 function Discriminants_Ok return Boolean is
4006 Cond : Node_Id := Empty;
4007 Check : Node_Id;
4008 D : Entity_Id;
4009 Disc1 : Elmt_Id;
4010 Disc2 : Elmt_Id;
4011 Val1 : Node_Id;
4012 Val2 : Node_Id;
4014 begin
4015 D := First_Discriminant (Typ);
4016 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
4017 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
4018 while Present (Disc1) and then Present (Disc2) loop
4019 Val1 := Node (Disc1);
4020 Val2 := Node (Disc2);
4022 if not Is_OK_Static_Expression (Val1)
4023 or else not Is_OK_Static_Expression (Val2)
4024 then
4025 Check := Make_Op_Ne (Loc,
4026 Left_Opnd => Duplicate_Subexpr (Val1),
4027 Right_Opnd => Duplicate_Subexpr (Val2));
4029 if No (Cond) then
4030 Cond := Check;
4032 else
4033 Cond := Make_Or_Else (Loc,
4034 Left_Opnd => Cond,
4035 Right_Opnd => Check);
4036 end if;
4038 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
4039 Apply_Compile_Time_Constraint_Error (Aggr,
4040 Msg => "incorrect value for discriminant&??",
4041 Reason => CE_Discriminant_Check_Failed,
4042 Ent => D);
4043 return False;
4044 end if;
4046 Next_Discriminant (D);
4047 Next_Elmt (Disc1);
4048 Next_Elmt (Disc2);
4049 end loop;
4051 -- If any discriminant constraint is nonstatic, emit a check
4053 if Present (Cond) then
4054 Insert_Action (N,
4055 Make_Raise_Constraint_Error (Loc,
4056 Condition => Cond,
4057 Reason => CE_Discriminant_Check_Failed));
4058 end if;
4060 return True;
4061 end Discriminants_Ok;
4063 -- Start of processing for Convert_Aggr_In_Object_Decl
4065 begin
4066 Set_Assignment_OK (Occ);
4068 if Nkind (Aggr) = N_Qualified_Expression then
4069 Aggr := Expression (Aggr);
4070 end if;
4072 if Has_Discriminants (Typ)
4073 and then Typ /= Etype (Obj)
4074 and then Is_Constrained (Etype (Obj))
4075 and then not Discriminants_Ok
4076 then
4077 return;
4078 end if;
4080 -- If the context is an extended return statement, it has its own
4081 -- finalization machinery (i.e. works like a transient scope) and
4082 -- we do not want to create an additional one, because objects on
4083 -- the finalization list of the return must be moved to the caller's
4084 -- finalization list to complete the return.
4086 -- However, if the aggregate is limited, it is built in place, and the
4087 -- controlled components are not assigned to intermediate temporaries
4088 -- so there is no need for a transient scope in this case either.
4090 if Requires_Transient_Scope (Typ)
4091 and then Ekind (Current_Scope) /= E_Return_Statement
4092 and then not Is_Limited_Type (Typ)
4093 then
4094 Establish_Transient_Scope (Aggr, Manage_Sec_Stack => False);
4095 end if;
4097 declare
4098 Node_After : constant Node_Id := Next (N);
4099 begin
4100 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
4101 Collect_Initialization_Statements (Obj, N, Node_After);
4102 end;
4104 Set_No_Initialization (N);
4105 Initialize_Discriminants (N, Typ);
4106 end Convert_Aggr_In_Object_Decl;
4108 -------------------------------------
4109 -- Convert_Array_Aggr_In_Allocator --
4110 -------------------------------------
4112 procedure Convert_Array_Aggr_In_Allocator
4113 (Decl : Node_Id;
4114 Aggr : Node_Id;
4115 Target : Node_Id)
4117 Aggr_Code : List_Id;
4118 Typ : constant Entity_Id := Etype (Aggr);
4119 Ctyp : constant Entity_Id := Component_Type (Typ);
4121 begin
4122 -- The target is an explicit dereference of the allocated object.
4123 -- Generate component assignments to it, as for an aggregate that
4124 -- appears on the right-hand side of an assignment statement.
4126 Aggr_Code :=
4127 Build_Array_Aggr_Code (Aggr,
4128 Ctype => Ctyp,
4129 Index => First_Index (Typ),
4130 Into => Target,
4131 Scalar_Comp => Is_Scalar_Type (Ctyp));
4133 Insert_Actions_After (Decl, Aggr_Code);
4134 end Convert_Array_Aggr_In_Allocator;
4136 ----------------------------
4137 -- Convert_To_Assignments --
4138 ----------------------------
4140 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4141 Loc : constant Source_Ptr := Sloc (N);
4142 T : Entity_Id;
4143 Temp : Entity_Id;
4145 Aggr_Code : List_Id;
4146 Instr : Node_Id;
4147 Target_Expr : Node_Id;
4148 Parent_Kind : Node_Kind;
4149 Unc_Decl : Boolean := False;
4150 Parent_Node : Node_Id;
4152 begin
4153 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
4154 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4155 pragma Assert (Is_Record_Type (Typ));
4157 Parent_Node := Parent (N);
4158 Parent_Kind := Nkind (Parent_Node);
4160 if Parent_Kind = N_Qualified_Expression then
4161 -- Check if we are in an unconstrained declaration because in this
4162 -- case the current delayed expansion mechanism doesn't work when
4163 -- the declared object size depends on the initializing expr.
4165 Parent_Node := Parent (Parent_Node);
4166 Parent_Kind := Nkind (Parent_Node);
4168 if Parent_Kind = N_Object_Declaration then
4169 Unc_Decl :=
4170 not Is_Entity_Name (Object_Definition (Parent_Node))
4171 or else (Nkind (N) = N_Aggregate
4172 and then
4173 Has_Discriminants
4174 (Entity (Object_Definition (Parent_Node))))
4175 or else Is_Class_Wide_Type
4176 (Entity (Object_Definition (Parent_Node)));
4177 end if;
4178 end if;
4180 -- Just set the Delay flag in the cases where the transformation will be
4181 -- done top down from above.
4183 if False
4185 -- Internal aggregate (transformed when expanding the parent)
4187 or else Parent_Kind = N_Aggregate
4188 or else Parent_Kind = N_Extension_Aggregate
4189 or else Parent_Kind = N_Component_Association
4191 -- Allocator (see Convert_Aggr_In_Allocator)
4193 or else Parent_Kind = N_Allocator
4195 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4197 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4199 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4200 -- assignments in init procs are taken into account.
4202 or else (Parent_Kind = N_Assignment_Statement
4203 and then Inside_Init_Proc)
4205 -- (Ada 2005) An inherently limited type in a return statement, which
4206 -- will be handled in a build-in-place fashion, and may be rewritten
4207 -- as an extended return and have its own finalization machinery.
4208 -- In the case of a simple return, the aggregate needs to be delayed
4209 -- until the scope for the return statement has been created, so
4210 -- that any finalization chain will be associated with that scope.
4211 -- For extended returns, we delay expansion to avoid the creation
4212 -- of an unwanted transient scope that could result in premature
4213 -- finalization of the return object (which is built in place
4214 -- within the caller's scope).
4216 or else Is_Build_In_Place_Aggregate_Return (N)
4217 then
4218 Set_Expansion_Delayed (N);
4219 return;
4220 end if;
4222 -- Otherwise, if a transient scope is required, create it now. If we
4223 -- are within an initialization procedure do not create such, because
4224 -- the target of the assignment must not be declared within a local
4225 -- block, and because cleanup will take place on return from the
4226 -- initialization procedure.
4228 -- Should the condition be more restrictive ???
4230 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4231 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
4232 end if;
4234 -- If the aggregate is nonlimited, create a temporary. If it is limited
4235 -- and context is an assignment, this is a subaggregate for an enclosing
4236 -- aggregate being expanded. It must be built in place, so use target of
4237 -- the current assignment.
4239 if Is_Limited_Type (Typ)
4240 and then Nkind (Parent (N)) = N_Assignment_Statement
4241 then
4242 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4243 Insert_Actions (Parent (N),
4244 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4245 Rewrite (Parent (N), Make_Null_Statement (Loc));
4247 else
4248 Temp := Make_Temporary (Loc, 'A', N);
4250 -- If the type inherits unknown discriminants, use the view with
4251 -- known discriminants if available.
4253 if Has_Unknown_Discriminants (Typ)
4254 and then Present (Underlying_Record_View (Typ))
4255 then
4256 T := Underlying_Record_View (Typ);
4257 else
4258 T := Typ;
4259 end if;
4261 Instr :=
4262 Make_Object_Declaration (Loc,
4263 Defining_Identifier => Temp,
4264 Object_Definition => New_Occurrence_Of (T, Loc));
4266 Set_No_Initialization (Instr);
4267 Insert_Action (N, Instr);
4268 Initialize_Discriminants (Instr, T);
4270 Target_Expr := New_Occurrence_Of (Temp, Loc);
4271 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
4273 -- Save the last assignment statement associated with the aggregate
4274 -- when building a controlled object. This reference is utilized by
4275 -- the finalization machinery when marking an object as successfully
4276 -- initialized.
4278 if Needs_Finalization (T) then
4279 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
4280 end if;
4282 Insert_Actions (N, Aggr_Code);
4283 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4284 Analyze_And_Resolve (N, T);
4285 end if;
4286 end Convert_To_Assignments;
4288 ---------------------------
4289 -- Convert_To_Positional --
4290 ---------------------------
4292 procedure Convert_To_Positional
4293 (N : Node_Id;
4294 Max_Others_Replicate : Nat := 32;
4295 Handle_Bit_Packed : Boolean := False)
4297 Typ : constant Entity_Id := Etype (N);
4299 Static_Components : Boolean := True;
4301 procedure Check_Static_Components;
4302 -- Check whether all components of the aggregate are compile-time known
4303 -- values, and can be passed as is to the back-end without further
4304 -- expansion.
4305 -- An Iterated_Component_Association is treated as nonstatic, but there
4306 -- are possibilities for optimization here.
4308 function Flatten
4309 (N : Node_Id;
4310 Ix : Node_Id;
4311 Ixb : Node_Id) return Boolean;
4312 -- Convert the aggregate into a purely positional form if possible. On
4313 -- entry the bounds of all dimensions are known to be static, and the
4314 -- total number of components is safe enough to expand.
4316 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
4317 -- Return True iff the array N is flat (which is not trivial in the case
4318 -- of multidimensional aggregates).
4320 -----------------------------
4321 -- Check_Static_Components --
4322 -----------------------------
4324 -- Could use some comments in this body ???
4326 procedure Check_Static_Components is
4327 Expr : Node_Id;
4329 begin
4330 Static_Components := True;
4332 if Nkind (N) = N_String_Literal then
4333 null;
4335 elsif Present (Expressions (N)) then
4336 Expr := First (Expressions (N));
4337 while Present (Expr) loop
4338 if Nkind (Expr) /= N_Aggregate
4339 or else not Compile_Time_Known_Aggregate (Expr)
4340 or else Expansion_Delayed (Expr)
4341 then
4342 Static_Components := False;
4343 exit;
4344 end if;
4346 Next (Expr);
4347 end loop;
4348 end if;
4350 if Nkind (N) = N_Aggregate
4351 and then Present (Component_Associations (N))
4352 then
4353 Expr := First (Component_Associations (N));
4354 while Present (Expr) loop
4355 if Nkind_In (Expression (Expr), N_Integer_Literal,
4356 N_Real_Literal)
4357 then
4358 null;
4360 elsif Is_Entity_Name (Expression (Expr))
4361 and then Present (Entity (Expression (Expr)))
4362 and then Ekind (Entity (Expression (Expr))) =
4363 E_Enumeration_Literal
4364 then
4365 null;
4367 elsif Nkind (Expression (Expr)) /= N_Aggregate
4368 or else not Compile_Time_Known_Aggregate (Expression (Expr))
4369 or else Expansion_Delayed (Expression (Expr))
4370 or else Nkind_In (Expr, N_Iterated_Component_Association,
4371 N_Quantified_Expression)
4372 then
4373 Static_Components := False;
4374 exit;
4375 end if;
4377 Next (Expr);
4378 end loop;
4379 end if;
4380 end Check_Static_Components;
4382 -------------
4383 -- Flatten --
4384 -------------
4386 function Flatten
4387 (N : Node_Id;
4388 Ix : Node_Id;
4389 Ixb : Node_Id) return Boolean
4391 Loc : constant Source_Ptr := Sloc (N);
4392 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
4393 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
4394 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
4395 Lov : Uint;
4396 Hiv : Uint;
4398 Others_Present : Boolean := False;
4400 begin
4401 if Nkind (Original_Node (N)) = N_String_Literal then
4402 return True;
4403 end if;
4405 if not Compile_Time_Known_Value (Lo)
4406 or else not Compile_Time_Known_Value (Hi)
4407 then
4408 return False;
4409 end if;
4411 Lov := Expr_Value (Lo);
4412 Hiv := Expr_Value (Hi);
4414 -- Check if there is an others choice
4416 if Present (Component_Associations (N)) then
4417 declare
4418 Assoc : Node_Id;
4419 Choice : Node_Id;
4421 begin
4422 Assoc := First (Component_Associations (N));
4423 while Present (Assoc) loop
4425 -- If this is a box association, flattening is in general
4426 -- not possible because at this point we cannot tell if the
4427 -- default is static or even exists.
4429 if Box_Present (Assoc) then
4430 return False;
4432 elsif Nkind (Assoc) = N_Iterated_Component_Association then
4433 return False;
4434 end if;
4436 Choice := First (Choice_List (Assoc));
4438 while Present (Choice) loop
4439 if Nkind (Choice) = N_Others_Choice then
4440 Others_Present := True;
4441 end if;
4443 Next (Choice);
4444 end loop;
4446 Next (Assoc);
4447 end loop;
4448 end;
4449 end if;
4451 -- If the low bound is not known at compile time and others is not
4452 -- present we can proceed since the bounds can be obtained from the
4453 -- aggregate.
4455 if Hiv < Lov
4456 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
4457 then
4458 return False;
4459 end if;
4461 -- Determine if set of alternatives is suitable for conversion and
4462 -- build an array containing the values in sequence.
4464 declare
4465 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
4466 of Node_Id := (others => Empty);
4467 -- The values in the aggregate sorted appropriately
4469 Vlist : List_Id;
4470 -- Same data as Vals in list form
4472 Rep_Count : Nat;
4473 -- Used to validate Max_Others_Replicate limit
4475 Elmt : Node_Id;
4476 Num : Int := UI_To_Int (Lov);
4477 Choice_Index : Int;
4478 Choice : Node_Id;
4479 Lo, Hi : Node_Id;
4481 begin
4482 if Present (Expressions (N)) then
4483 Elmt := First (Expressions (N));
4484 while Present (Elmt) loop
4485 if Nkind (Elmt) = N_Aggregate
4486 and then Present (Next_Index (Ix))
4487 and then
4488 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
4489 then
4490 return False;
4491 end if;
4493 -- Duplicate expression for each index it covers
4495 Vals (Num) := New_Copy_Tree (Elmt);
4496 Num := Num + 1;
4498 Next (Elmt);
4499 end loop;
4500 end if;
4502 if No (Component_Associations (N)) then
4503 return True;
4504 end if;
4506 Elmt := First (Component_Associations (N));
4508 if Nkind (Expression (Elmt)) = N_Aggregate then
4509 if Present (Next_Index (Ix))
4510 and then
4511 not Flatten
4512 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
4513 then
4514 return False;
4515 end if;
4516 end if;
4518 Component_Loop : while Present (Elmt) loop
4519 Choice := First (Choice_List (Elmt));
4520 Choice_Loop : while Present (Choice) loop
4522 -- If we have an others choice, fill in the missing elements
4523 -- subject to the limit established by Max_Others_Replicate.
4524 -- If the expression involves a construct that generates
4525 -- a loop, we must generate individual assignments and
4526 -- no flattening is possible.
4528 if Nkind (Choice) = N_Others_Choice then
4529 Rep_Count := 0;
4531 if Nkind_In (Expression (Elmt),
4532 N_Iterated_Component_Association,
4533 N_Quantified_Expression)
4534 then
4535 return False;
4536 end if;
4538 for J in Vals'Range loop
4539 if No (Vals (J)) then
4540 Vals (J) := New_Copy_Tree (Expression (Elmt));
4541 Rep_Count := Rep_Count + 1;
4543 -- Check for maximum others replication. Note that
4544 -- we skip this test if either of the restrictions
4545 -- No_Elaboration_Code or No_Implicit_Loops is
4546 -- active, if this is a preelaborable unit or
4547 -- a predefined unit, or if the unit must be
4548 -- placed in data memory. This also ensures that
4549 -- predefined units get the same level of constant
4550 -- folding in Ada 95 and Ada 2005, where their
4551 -- categorization has changed.
4553 declare
4554 P : constant Entity_Id :=
4555 Cunit_Entity (Current_Sem_Unit);
4557 begin
4558 -- Check if duplication OK and if so continue
4559 -- processing.
4561 if Restriction_Active (No_Elaboration_Code)
4562 or else Restriction_Active (No_Implicit_Loops)
4563 or else
4564 (Ekind (Current_Scope) = E_Package
4565 and then Static_Elaboration_Desired
4566 (Current_Scope))
4567 or else Is_Preelaborated (P)
4568 or else (Ekind (P) = E_Package_Body
4569 and then
4570 Is_Preelaborated (Spec_Entity (P)))
4571 or else
4572 Is_Predefined_Unit (Get_Source_Unit (P))
4573 then
4574 null;
4576 -- If duplication not OK, then we return False
4577 -- if the replication count is too high
4579 elsif Rep_Count > Max_Others_Replicate then
4580 return False;
4582 -- Continue on if duplication not OK, but the
4583 -- replication count is not excessive.
4585 else
4586 null;
4587 end if;
4588 end;
4589 end if;
4590 end loop;
4592 if Rep_Count = 0
4593 and then Warn_On_Redundant_Constructs
4594 then
4595 Error_Msg_N ("there are no others?r?", Elmt);
4596 end if;
4598 exit Component_Loop;
4600 -- Case of a subtype mark, identifier or expanded name
4602 elsif Is_Entity_Name (Choice)
4603 and then Is_Type (Entity (Choice))
4604 then
4605 Lo := Type_Low_Bound (Etype (Choice));
4606 Hi := Type_High_Bound (Etype (Choice));
4608 -- Case of subtype indication
4610 elsif Nkind (Choice) = N_Subtype_Indication then
4611 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
4612 Hi := High_Bound (Range_Expression (Constraint (Choice)));
4614 -- Case of a range
4616 elsif Nkind (Choice) = N_Range then
4617 Lo := Low_Bound (Choice);
4618 Hi := High_Bound (Choice);
4620 -- Normal subexpression case
4622 else pragma Assert (Nkind (Choice) in N_Subexpr);
4623 if not Compile_Time_Known_Value (Choice) then
4624 return False;
4626 else
4627 Choice_Index := UI_To_Int (Expr_Value (Choice));
4629 if Choice_Index in Vals'Range then
4630 Vals (Choice_Index) :=
4631 New_Copy_Tree (Expression (Elmt));
4632 goto Continue;
4634 -- Choice is statically out-of-range, will be
4635 -- rewritten to raise Constraint_Error.
4637 else
4638 return False;
4639 end if;
4640 end if;
4641 end if;
4643 -- Range cases merge with Lo,Hi set
4645 if not Compile_Time_Known_Value (Lo)
4646 or else
4647 not Compile_Time_Known_Value (Hi)
4648 then
4649 return False;
4651 else
4652 for J in UI_To_Int (Expr_Value (Lo)) ..
4653 UI_To_Int (Expr_Value (Hi))
4654 loop
4655 Vals (J) := New_Copy_Tree (Expression (Elmt));
4656 end loop;
4657 end if;
4659 <<Continue>>
4660 Next (Choice);
4661 end loop Choice_Loop;
4663 Next (Elmt);
4664 end loop Component_Loop;
4666 -- If we get here the conversion is possible
4668 Vlist := New_List;
4669 for J in Vals'Range loop
4670 Append (Vals (J), Vlist);
4671 end loop;
4673 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4674 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4675 return True;
4676 end;
4677 end Flatten;
4679 -------------
4680 -- Is_Flat --
4681 -------------
4683 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4684 Elmt : Node_Id;
4686 begin
4687 if Dims = 0 then
4688 return True;
4690 elsif Nkind (N) = N_Aggregate then
4691 if Present (Component_Associations (N)) then
4692 return False;
4694 else
4695 Elmt := First (Expressions (N));
4696 while Present (Elmt) loop
4697 if not Is_Flat (Elmt, Dims - 1) then
4698 return False;
4699 end if;
4701 Next (Elmt);
4702 end loop;
4704 return True;
4705 end if;
4706 else
4707 return True;
4708 end if;
4709 end Is_Flat;
4711 -- Start of processing for Convert_To_Positional
4713 begin
4714 -- Only convert to positional when generating C in case of an
4715 -- object declaration, this is the only case where aggregates are
4716 -- supported in C.
4718 if Modify_Tree_For_C and then not In_Object_Declaration (N) then
4719 return;
4720 end if;
4722 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4723 -- components because in this case will need to call the corresponding
4724 -- IP procedure.
4726 if Has_Default_Init_Comps (N) then
4727 return;
4728 end if;
4730 -- A subaggregate may have been flattened but is not known to be
4731 -- Compile_Time_Known. Set that flag in cases that cannot require
4732 -- elaboration code, so that the aggregate can be used as the
4733 -- initial value of a thread-local variable.
4735 if Is_Flat (N, Number_Dimensions (Typ)) then
4736 Check_Static_Components;
4737 if Static_Components then
4738 if Is_Packed (Etype (N))
4739 or else
4740 (Is_Record_Type (Component_Type (Etype (N)))
4741 and then Has_Discriminants (Component_Type (Etype (N))))
4742 then
4743 null;
4744 else
4745 Set_Compile_Time_Known_Aggregate (N);
4746 end if;
4747 end if;
4749 return;
4750 end if;
4752 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4753 return;
4754 end if;
4756 -- Do not convert to positional if controlled components are involved
4757 -- since these require special processing
4759 if Has_Controlled_Component (Typ) then
4760 return;
4761 end if;
4763 Check_Static_Components;
4765 -- If the size is known, or all the components are static, try to
4766 -- build a fully positional aggregate.
4768 -- The size of the type may not be known for an aggregate with
4769 -- discriminated array components, but if the components are static
4770 -- it is still possible to verify statically that the length is
4771 -- compatible with the upper bound of the type, and therefore it is
4772 -- worth flattening such aggregates as well.
4774 -- For now the back-end expands these aggregates into individual
4775 -- assignments to the target anyway, but it is conceivable that
4776 -- it will eventually be able to treat such aggregates statically???
4778 if Aggr_Size_OK (N, Typ)
4779 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4780 then
4781 if Static_Components then
4782 Set_Compile_Time_Known_Aggregate (N);
4783 Set_Expansion_Delayed (N, False);
4784 end if;
4786 Analyze_And_Resolve (N, Typ);
4787 end if;
4789 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4790 -- that will still require initialization code.
4792 if (Ekind (Current_Scope) = E_Package
4793 and then Static_Elaboration_Desired (Current_Scope))
4794 and then Nkind (Parent (N)) = N_Object_Declaration
4795 then
4796 declare
4797 Expr : Node_Id;
4799 begin
4800 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4801 Expr := First (Expressions (N));
4802 while Present (Expr) loop
4803 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
4804 or else
4805 (Is_Entity_Name (Expr)
4806 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
4807 then
4808 null;
4810 else
4811 Error_Msg_N
4812 ("non-static object requires elaboration code??", N);
4813 exit;
4814 end if;
4816 Next (Expr);
4817 end loop;
4819 if Present (Component_Associations (N)) then
4820 Error_Msg_N ("object requires elaboration code??", N);
4821 end if;
4822 end if;
4823 end;
4824 end if;
4825 end Convert_To_Positional;
4827 ----------------------------
4828 -- Expand_Array_Aggregate --
4829 ----------------------------
4831 -- Array aggregate expansion proceeds as follows:
4833 -- 1. If requested we generate code to perform all the array aggregate
4834 -- bound checks, specifically
4836 -- (a) Check that the index range defined by aggregate bounds is
4837 -- compatible with corresponding index subtype.
4839 -- (b) If an others choice is present check that no aggregate
4840 -- index is outside the bounds of the index constraint.
4842 -- (c) For multidimensional arrays make sure that all subaggregates
4843 -- corresponding to the same dimension have the same bounds.
4845 -- 2. Check for packed array aggregate which can be converted to a
4846 -- constant so that the aggregate disappears completely.
4848 -- 3. Check case of nested aggregate. Generally nested aggregates are
4849 -- handled during the processing of the parent aggregate.
4851 -- 4. Check if the aggregate can be statically processed. If this is the
4852 -- case pass it as is to Gigi. Note that a necessary condition for
4853 -- static processing is that the aggregate be fully positional.
4855 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4856 -- a temporary) then mark the aggregate as such and return. Otherwise
4857 -- create a new temporary and generate the appropriate initialization
4858 -- code.
4860 procedure Expand_Array_Aggregate (N : Node_Id) is
4861 Loc : constant Source_Ptr := Sloc (N);
4863 Typ : constant Entity_Id := Etype (N);
4864 Ctyp : constant Entity_Id := Component_Type (Typ);
4865 -- Typ is the correct constrained array subtype of the aggregate
4866 -- Ctyp is the corresponding component type.
4868 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4869 -- Number of aggregate index dimensions
4871 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4872 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4873 -- Low and High bounds of the constraint for each aggregate index
4875 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4876 -- The type of each index
4878 In_Place_Assign_OK_For_Declaration : Boolean := False;
4879 -- True if we are to generate an in place assignment for a declaration
4881 Maybe_In_Place_OK : Boolean;
4882 -- If the type is neither controlled nor packed and the aggregate
4883 -- is the expression in an assignment, assignment in place may be
4884 -- possible, provided other conditions are met on the LHS.
4886 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4887 (others => False);
4888 -- If Others_Present (J) is True, then there is an others choice in one
4889 -- of the subaggregates of N at dimension J.
4891 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4892 -- Returns true if an aggregate assignment can be done by the back end
4894 procedure Build_Constrained_Type (Positional : Boolean);
4895 -- If the subtype is not static or unconstrained, build a constrained
4896 -- type using the computable sizes of the aggregate and its sub-
4897 -- aggregates.
4899 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4900 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4901 -- by Index_Bounds.
4903 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4904 -- Checks that in a multidimensional array aggregate all subaggregates
4905 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4906 -- an array subaggregate. Dim is the dimension corresponding to the
4907 -- subaggregate.
4909 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4910 -- Computes the values of array Others_Present. Sub_Aggr is the array
4911 -- subaggregate we start the computation from. Dim is the dimension
4912 -- corresponding to the subaggregate.
4914 function In_Place_Assign_OK return Boolean;
4915 -- Simple predicate to determine whether an aggregate assignment can
4916 -- be done in place, because none of the new values can depend on the
4917 -- components of the target of the assignment.
4919 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4920 -- Checks that if an others choice is present in any subaggregate, no
4921 -- aggregate index is outside the bounds of the index constraint.
4922 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4923 -- to the subaggregate.
4925 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4926 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4927 -- built directly into the target of the assignment it must be free
4928 -- of side effects.
4930 ------------------------------------
4931 -- Aggr_Assignment_OK_For_Backend --
4932 ------------------------------------
4934 -- Backend processing by Gigi/gcc is possible only if all the following
4935 -- conditions are met:
4937 -- 1. N consists of a single OTHERS choice, possibly recursively
4939 -- 2. The array type has no null ranges (the purpose of this is to
4940 -- avoid a bogus warning for an out-of-range value).
4942 -- 3. The array type has no atomic components
4944 -- 4. The component type is elementary
4946 -- 5. The component size is a multiple of Storage_Unit
4948 -- 6. The component size is Storage_Unit or the value is of the form
4949 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4950 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4951 -- the 8-bit value M, concatenated together.
4953 -- The ultimate goal is to generate a call to a fast memset routine
4954 -- specifically optimized for the target.
4956 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4957 Csiz : Uint;
4958 Ctyp : Entity_Id;
4959 Expr : Node_Id;
4960 High : Node_Id;
4961 Index : Entity_Id;
4962 Low : Node_Id;
4963 Nunits : Int;
4964 Remainder : Uint;
4965 Value : Uint;
4967 begin
4968 -- Recurse as far as possible to find the innermost component type
4970 Ctyp := Etype (N);
4971 Expr := N;
4972 while Is_Array_Type (Ctyp) loop
4973 if Nkind (Expr) /= N_Aggregate
4974 or else not Is_Others_Aggregate (Expr)
4975 then
4976 return False;
4977 end if;
4979 Index := First_Index (Ctyp);
4980 while Present (Index) loop
4981 Get_Index_Bounds (Index, Low, High);
4983 if Is_Null_Range (Low, High) then
4984 return False;
4985 end if;
4987 Next_Index (Index);
4988 end loop;
4990 Expr := Expression (First (Component_Associations (Expr)));
4992 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4993 if Nkind (Expr) /= N_Aggregate
4994 or else not Is_Others_Aggregate (Expr)
4995 then
4996 return False;
4997 end if;
4999 Expr := Expression (First (Component_Associations (Expr)));
5000 end loop;
5002 if Has_Atomic_Components (Ctyp) then
5003 return False;
5004 end if;
5006 Csiz := Component_Size (Ctyp);
5007 Ctyp := Component_Type (Ctyp);
5009 if Is_Atomic_Or_VFA (Ctyp) then
5010 return False;
5011 end if;
5012 end loop;
5014 -- An Iterated_Component_Association involves a loop (in most cases)
5015 -- and is never static.
5017 if Nkind (Parent (Expr)) = N_Iterated_Component_Association then
5018 return False;
5019 end if;
5021 -- Access types need to be dealt with specially
5023 if Is_Access_Type (Ctyp) then
5025 -- Component_Size is not set by Layout_Type if the component
5026 -- type is an access type ???
5028 Csiz := Esize (Ctyp);
5030 -- Fat pointers are rejected as they are not really elementary
5031 -- for the backend.
5033 if Csiz /= System_Address_Size then
5034 return False;
5035 end if;
5037 -- The supported expressions are NULL and constants, others are
5038 -- rejected upfront to avoid being analyzed below, which can be
5039 -- problematic for some of them, for example allocators.
5041 if Nkind (Expr) /= N_Null and then not Is_Entity_Name (Expr) then
5042 return False;
5043 end if;
5045 -- Scalar types are OK if their size is a multiple of Storage_Unit
5047 elsif Is_Scalar_Type (Ctyp) then
5048 if Csiz mod System_Storage_Unit /= 0 then
5049 return False;
5050 end if;
5052 -- Composite types are rejected
5054 else
5055 return False;
5056 end if;
5058 -- The expression needs to be analyzed if True is returned
5060 Analyze_And_Resolve (Expr, Ctyp);
5062 -- Strip away any conversions from the expression as they simply
5063 -- qualify the real expression.
5065 while Nkind_In (Expr, N_Unchecked_Type_Conversion,
5066 N_Type_Conversion)
5067 loop
5068 Expr := Expression (Expr);
5069 end loop;
5071 Nunits := UI_To_Int (Csiz) / System_Storage_Unit;
5073 if Nunits = 1 then
5074 return True;
5075 end if;
5077 if not Compile_Time_Known_Value (Expr) then
5078 return False;
5079 end if;
5081 -- The only supported value for floating point is 0.0
5083 if Is_Floating_Point_Type (Ctyp) then
5084 return Expr_Value_R (Expr) = Ureal_0;
5085 end if;
5087 -- For other types, we can look into the value as an integer
5089 Value := Expr_Value (Expr);
5091 if Has_Biased_Representation (Ctyp) then
5092 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
5093 end if;
5095 -- Values 0 and -1 immediately satisfy the last check
5097 if Value = Uint_0 or else Value = Uint_Minus_1 then
5098 return True;
5099 end if;
5101 -- We need to work with an unsigned value
5103 if Value < 0 then
5104 Value := Value + 2**(System_Storage_Unit * Nunits);
5105 end if;
5107 Remainder := Value rem 2**System_Storage_Unit;
5109 for J in 1 .. Nunits - 1 loop
5110 Value := Value / 2**System_Storage_Unit;
5112 if Value rem 2**System_Storage_Unit /= Remainder then
5113 return False;
5114 end if;
5115 end loop;
5117 return True;
5118 end Aggr_Assignment_OK_For_Backend;
5120 ----------------------------
5121 -- Build_Constrained_Type --
5122 ----------------------------
5124 procedure Build_Constrained_Type (Positional : Boolean) is
5125 Loc : constant Source_Ptr := Sloc (N);
5126 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
5127 Comp : Node_Id;
5128 Decl : Node_Id;
5129 Typ : constant Entity_Id := Etype (N);
5130 Indexes : constant List_Id := New_List;
5131 Num : Nat;
5132 Sub_Agg : Node_Id;
5134 begin
5135 -- If the aggregate is purely positional, all its subaggregates
5136 -- have the same size. We collect the dimensions from the first
5137 -- subaggregate at each level.
5139 if Positional then
5140 Sub_Agg := N;
5142 for D in 1 .. Number_Dimensions (Typ) loop
5143 Sub_Agg := First (Expressions (Sub_Agg));
5145 Comp := Sub_Agg;
5146 Num := 0;
5147 while Present (Comp) loop
5148 Num := Num + 1;
5149 Next (Comp);
5150 end loop;
5152 Append_To (Indexes,
5153 Make_Range (Loc,
5154 Low_Bound => Make_Integer_Literal (Loc, 1),
5155 High_Bound => Make_Integer_Literal (Loc, Num)));
5156 end loop;
5158 else
5159 -- We know the aggregate type is unconstrained and the aggregate
5160 -- is not processable by the back end, therefore not necessarily
5161 -- positional. Retrieve each dimension bounds (computed earlier).
5163 for D in 1 .. Number_Dimensions (Typ) loop
5164 Append_To (Indexes,
5165 Make_Range (Loc,
5166 Low_Bound => Aggr_Low (D),
5167 High_Bound => Aggr_High (D)));
5168 end loop;
5169 end if;
5171 Decl :=
5172 Make_Full_Type_Declaration (Loc,
5173 Defining_Identifier => Agg_Type,
5174 Type_Definition =>
5175 Make_Constrained_Array_Definition (Loc,
5176 Discrete_Subtype_Definitions => Indexes,
5177 Component_Definition =>
5178 Make_Component_Definition (Loc,
5179 Aliased_Present => False,
5180 Subtype_Indication =>
5181 New_Occurrence_Of (Component_Type (Typ), Loc))));
5183 Insert_Action (N, Decl);
5184 Analyze (Decl);
5185 Set_Etype (N, Agg_Type);
5186 Set_Is_Itype (Agg_Type);
5187 Freeze_Itype (Agg_Type, N);
5188 end Build_Constrained_Type;
5190 ------------------
5191 -- Check_Bounds --
5192 ------------------
5194 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
5195 Aggr_Lo : Node_Id;
5196 Aggr_Hi : Node_Id;
5198 Ind_Lo : Node_Id;
5199 Ind_Hi : Node_Id;
5201 Cond : Node_Id := Empty;
5203 begin
5204 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
5205 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
5207 -- Generate the following test:
5209 -- [constraint_error when
5210 -- Aggr_Lo <= Aggr_Hi and then
5211 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5213 -- As an optimization try to see if some tests are trivially vacuous
5214 -- because we are comparing an expression against itself.
5216 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
5217 Cond := Empty;
5219 elsif Aggr_Hi = Ind_Hi then
5220 Cond :=
5221 Make_Op_Lt (Loc,
5222 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5223 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
5225 elsif Aggr_Lo = Ind_Lo then
5226 Cond :=
5227 Make_Op_Gt (Loc,
5228 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5229 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
5231 else
5232 Cond :=
5233 Make_Or_Else (Loc,
5234 Left_Opnd =>
5235 Make_Op_Lt (Loc,
5236 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5237 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
5239 Right_Opnd =>
5240 Make_Op_Gt (Loc,
5241 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5242 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
5243 end if;
5245 if Present (Cond) then
5246 Cond :=
5247 Make_And_Then (Loc,
5248 Left_Opnd =>
5249 Make_Op_Le (Loc,
5250 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5251 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
5253 Right_Opnd => Cond);
5255 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5256 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5257 Insert_Action (N,
5258 Make_Raise_Constraint_Error (Loc,
5259 Condition => Cond,
5260 Reason => CE_Range_Check_Failed));
5261 end if;
5262 end Check_Bounds;
5264 ----------------------------
5265 -- Check_Same_Aggr_Bounds --
5266 ----------------------------
5268 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5269 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
5270 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
5271 -- The bounds of this specific subaggregate
5273 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5274 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5275 -- The bounds of the aggregate for this dimension
5277 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5278 -- The index type for this dimension.xxx
5280 Cond : Node_Id := Empty;
5281 Assoc : Node_Id;
5282 Expr : Node_Id;
5284 begin
5285 -- If index checks are on generate the test
5287 -- [constraint_error when
5288 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5290 -- As an optimization try to see if some tests are trivially vacuos
5291 -- because we are comparing an expression against itself. Also for
5292 -- the first dimension the test is trivially vacuous because there
5293 -- is just one aggregate for dimension 1.
5295 if Index_Checks_Suppressed (Ind_Typ) then
5296 Cond := Empty;
5298 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5299 then
5300 Cond := Empty;
5302 elsif Aggr_Hi = Sub_Hi then
5303 Cond :=
5304 Make_Op_Ne (Loc,
5305 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5306 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5308 elsif Aggr_Lo = Sub_Lo then
5309 Cond :=
5310 Make_Op_Ne (Loc,
5311 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5312 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5314 else
5315 Cond :=
5316 Make_Or_Else (Loc,
5317 Left_Opnd =>
5318 Make_Op_Ne (Loc,
5319 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5320 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5322 Right_Opnd =>
5323 Make_Op_Ne (Loc,
5324 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5325 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5326 end if;
5328 if Present (Cond) then
5329 Insert_Action (N,
5330 Make_Raise_Constraint_Error (Loc,
5331 Condition => Cond,
5332 Reason => CE_Length_Check_Failed));
5333 end if;
5335 -- Now look inside the subaggregate to see if there is more work
5337 if Dim < Aggr_Dimension then
5339 -- Process positional components
5341 if Present (Expressions (Sub_Aggr)) then
5342 Expr := First (Expressions (Sub_Aggr));
5343 while Present (Expr) loop
5344 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5345 Next (Expr);
5346 end loop;
5347 end if;
5349 -- Process component associations
5351 if Present (Component_Associations (Sub_Aggr)) then
5352 Assoc := First (Component_Associations (Sub_Aggr));
5353 while Present (Assoc) loop
5354 Expr := Expression (Assoc);
5355 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5356 Next (Assoc);
5357 end loop;
5358 end if;
5359 end if;
5360 end Check_Same_Aggr_Bounds;
5362 ----------------------------
5363 -- Compute_Others_Present --
5364 ----------------------------
5366 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5367 Assoc : Node_Id;
5368 Expr : Node_Id;
5370 begin
5371 if Present (Component_Associations (Sub_Aggr)) then
5372 Assoc := Last (Component_Associations (Sub_Aggr));
5374 if Nkind (First (Choice_List (Assoc))) = N_Others_Choice then
5375 Others_Present (Dim) := True;
5376 end if;
5377 end if;
5379 -- Now look inside the subaggregate to see if there is more work
5381 if Dim < Aggr_Dimension then
5383 -- Process positional components
5385 if Present (Expressions (Sub_Aggr)) then
5386 Expr := First (Expressions (Sub_Aggr));
5387 while Present (Expr) loop
5388 Compute_Others_Present (Expr, Dim + 1);
5389 Next (Expr);
5390 end loop;
5391 end if;
5393 -- Process component associations
5395 if Present (Component_Associations (Sub_Aggr)) then
5396 Assoc := First (Component_Associations (Sub_Aggr));
5397 while Present (Assoc) loop
5398 Expr := Expression (Assoc);
5399 Compute_Others_Present (Expr, Dim + 1);
5400 Next (Assoc);
5401 end loop;
5402 end if;
5403 end if;
5404 end Compute_Others_Present;
5406 ------------------------
5407 -- In_Place_Assign_OK --
5408 ------------------------
5410 function In_Place_Assign_OK return Boolean is
5411 Aggr_In : Node_Id;
5412 Aggr_Lo : Node_Id;
5413 Aggr_Hi : Node_Id;
5414 Obj_In : Node_Id;
5415 Obj_Lo : Node_Id;
5416 Obj_Hi : Node_Id;
5418 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
5419 -- Check recursively that each component of a (sub)aggregate does not
5420 -- depend on the variable being assigned to.
5422 function Safe_Component (Expr : Node_Id) return Boolean;
5423 -- Verify that an expression cannot depend on the variable being
5424 -- assigned to. Room for improvement here (but less than before).
5426 --------------------
5427 -- Safe_Aggregate --
5428 --------------------
5430 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
5431 Expr : Node_Id;
5433 begin
5434 if Nkind (Parent (Aggr)) = N_Iterated_Component_Association then
5435 return False;
5436 end if;
5438 if Present (Expressions (Aggr)) then
5439 Expr := First (Expressions (Aggr));
5440 while Present (Expr) loop
5441 if Nkind (Expr) = N_Aggregate then
5442 if not Safe_Aggregate (Expr) then
5443 return False;
5444 end if;
5446 elsif not Safe_Component (Expr) then
5447 return False;
5448 end if;
5450 Next (Expr);
5451 end loop;
5452 end if;
5454 if Present (Component_Associations (Aggr)) then
5455 Expr := First (Component_Associations (Aggr));
5456 while Present (Expr) loop
5457 if Nkind (Expression (Expr)) = N_Aggregate then
5458 if not Safe_Aggregate (Expression (Expr)) then
5459 return False;
5460 end if;
5462 -- If association has a box, no way to determine yet
5463 -- whether default can be assigned in place.
5465 elsif Box_Present (Expr) then
5466 return False;
5468 elsif not Safe_Component (Expression (Expr)) then
5469 return False;
5470 end if;
5472 Next (Expr);
5473 end loop;
5474 end if;
5476 return True;
5477 end Safe_Aggregate;
5479 --------------------
5480 -- Safe_Component --
5481 --------------------
5483 function Safe_Component (Expr : Node_Id) return Boolean is
5484 Comp : Node_Id := Expr;
5486 function Check_Component (Comp : Node_Id) return Boolean;
5487 -- Do the recursive traversal, after copy
5489 ---------------------
5490 -- Check_Component --
5491 ---------------------
5493 function Check_Component (Comp : Node_Id) return Boolean is
5494 begin
5495 if Is_Overloaded (Comp) then
5496 return False;
5497 end if;
5499 return Compile_Time_Known_Value (Comp)
5501 or else (Is_Entity_Name (Comp)
5502 and then Present (Entity (Comp))
5503 and then No (Renamed_Object (Entity (Comp))))
5505 or else (Nkind (Comp) = N_Attribute_Reference
5506 and then Check_Component (Prefix (Comp)))
5508 or else (Nkind (Comp) in N_Binary_Op
5509 and then Check_Component (Left_Opnd (Comp))
5510 and then Check_Component (Right_Opnd (Comp)))
5512 or else (Nkind (Comp) in N_Unary_Op
5513 and then Check_Component (Right_Opnd (Comp)))
5515 or else (Nkind (Comp) = N_Selected_Component
5516 and then Check_Component (Prefix (Comp)))
5518 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
5519 and then Check_Component (Expression (Comp)));
5520 end Check_Component;
5522 -- Start of processing for Safe_Component
5524 begin
5525 -- If the component appears in an association that may correspond
5526 -- to more than one element, it is not analyzed before expansion
5527 -- into assignments, to avoid side effects. We analyze, but do not
5528 -- resolve the copy, to obtain sufficient entity information for
5529 -- the checks that follow. If component is overloaded we assume
5530 -- an unsafe function call.
5532 if not Analyzed (Comp) then
5533 if Is_Overloaded (Expr) then
5534 return False;
5536 elsif Nkind (Expr) = N_Aggregate
5537 and then not Is_Others_Aggregate (Expr)
5538 then
5539 return False;
5541 elsif Nkind (Expr) = N_Allocator then
5543 -- For now, too complex to analyze
5545 return False;
5547 elsif Nkind (Parent (Expr)) =
5548 N_Iterated_Component_Association
5549 then
5550 -- Ditto for iterated component associations, which in
5551 -- general require an enclosing loop and involve nonstatic
5552 -- expressions.
5554 return False;
5555 end if;
5557 Comp := New_Copy_Tree (Expr);
5558 Set_Parent (Comp, Parent (Expr));
5559 Analyze (Comp);
5560 end if;
5562 if Nkind (Comp) = N_Aggregate then
5563 return Safe_Aggregate (Comp);
5564 else
5565 return Check_Component (Comp);
5566 end if;
5567 end Safe_Component;
5569 -- Start of processing for In_Place_Assign_OK
5571 begin
5572 if Present (Component_Associations (N)) then
5574 -- On assignment, sliding can take place, so we cannot do the
5575 -- assignment in place unless the bounds of the aggregate are
5576 -- statically equal to those of the target.
5578 -- If the aggregate is given by an others choice, the bounds are
5579 -- derived from the left-hand side, and the assignment is safe if
5580 -- the expression is.
5582 if Is_Others_Aggregate (N) then
5583 return
5584 Safe_Component
5585 (Expression (First (Component_Associations (N))));
5586 end if;
5588 Aggr_In := First_Index (Etype (N));
5590 if Nkind (Parent (N)) = N_Assignment_Statement then
5591 Obj_In := First_Index (Etype (Name (Parent (N))));
5593 else
5594 -- Context is an allocator. Check bounds of aggregate against
5595 -- given type in qualified expression.
5597 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
5598 Obj_In :=
5599 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
5600 end if;
5602 while Present (Aggr_In) loop
5603 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
5604 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
5606 if not Compile_Time_Known_Value (Aggr_Lo)
5607 or else not Compile_Time_Known_Value (Obj_Lo)
5608 or else not Compile_Time_Known_Value (Obj_Hi)
5609 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
5610 then
5611 return False;
5613 -- For an assignment statement we require static matching of
5614 -- bounds. Ditto for an allocator whose qualified expression
5615 -- is a constrained type. If the expression in the allocator
5616 -- is an unconstrained array, we accept an upper bound that
5617 -- is not static, to allow for nonstatic expressions of the
5618 -- base type. Clearly there are further possibilities (with
5619 -- diminishing returns) for safely building arrays in place
5620 -- here.
5622 elsif Nkind (Parent (N)) = N_Assignment_Statement
5623 or else Is_Constrained (Etype (Parent (N)))
5624 then
5625 if not Compile_Time_Known_Value (Aggr_Hi)
5626 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
5627 then
5628 return False;
5629 end if;
5630 end if;
5632 Next_Index (Aggr_In);
5633 Next_Index (Obj_In);
5634 end loop;
5635 end if;
5637 -- Now check the component values themselves
5639 return Safe_Aggregate (N);
5640 end In_Place_Assign_OK;
5642 ------------------
5643 -- Others_Check --
5644 ------------------
5646 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
5647 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5648 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5649 -- The bounds of the aggregate for this dimension
5651 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5652 -- The index type for this dimension
5654 Need_To_Check : Boolean := False;
5656 Choices_Lo : Node_Id := Empty;
5657 Choices_Hi : Node_Id := Empty;
5658 -- The lowest and highest discrete choices for a named subaggregate
5660 Nb_Choices : Int := -1;
5661 -- The number of discrete non-others choices in this subaggregate
5663 Nb_Elements : Uint := Uint_0;
5664 -- The number of elements in a positional aggregate
5666 Cond : Node_Id := Empty;
5668 Assoc : Node_Id;
5669 Choice : Node_Id;
5670 Expr : Node_Id;
5672 begin
5673 -- Check if we have an others choice. If we do make sure that this
5674 -- subaggregate contains at least one element in addition to the
5675 -- others choice.
5677 if Range_Checks_Suppressed (Ind_Typ) then
5678 Need_To_Check := False;
5680 elsif Present (Expressions (Sub_Aggr))
5681 and then Present (Component_Associations (Sub_Aggr))
5682 then
5683 Need_To_Check := True;
5685 elsif Present (Component_Associations (Sub_Aggr)) then
5686 Assoc := Last (Component_Associations (Sub_Aggr));
5688 if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then
5689 Need_To_Check := False;
5691 else
5692 -- Count the number of discrete choices. Start with -1 because
5693 -- the others choice does not count.
5695 -- Is there some reason we do not use List_Length here ???
5697 Nb_Choices := -1;
5698 Assoc := First (Component_Associations (Sub_Aggr));
5699 while Present (Assoc) loop
5700 Choice := First (Choice_List (Assoc));
5701 while Present (Choice) loop
5702 Nb_Choices := Nb_Choices + 1;
5703 Next (Choice);
5704 end loop;
5706 Next (Assoc);
5707 end loop;
5709 -- If there is only an others choice nothing to do
5711 Need_To_Check := (Nb_Choices > 0);
5712 end if;
5714 else
5715 Need_To_Check := False;
5716 end if;
5718 -- If we are dealing with a positional subaggregate with an others
5719 -- choice then compute the number or positional elements.
5721 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
5722 Expr := First (Expressions (Sub_Aggr));
5723 Nb_Elements := Uint_0;
5724 while Present (Expr) loop
5725 Nb_Elements := Nb_Elements + 1;
5726 Next (Expr);
5727 end loop;
5729 -- If the aggregate contains discrete choices and an others choice
5730 -- compute the smallest and largest discrete choice values.
5732 elsif Need_To_Check then
5733 Compute_Choices_Lo_And_Choices_Hi : declare
5735 Table : Case_Table_Type (1 .. Nb_Choices);
5736 -- Used to sort all the different choice values
5738 J : Pos := 1;
5739 Low : Node_Id;
5740 High : Node_Id;
5742 begin
5743 Assoc := First (Component_Associations (Sub_Aggr));
5744 while Present (Assoc) loop
5745 Choice := First (Choice_List (Assoc));
5746 while Present (Choice) loop
5747 if Nkind (Choice) = N_Others_Choice then
5748 exit;
5749 end if;
5751 Get_Index_Bounds (Choice, Low, High);
5752 Table (J).Choice_Lo := Low;
5753 Table (J).Choice_Hi := High;
5755 J := J + 1;
5756 Next (Choice);
5757 end loop;
5759 Next (Assoc);
5760 end loop;
5762 -- Sort the discrete choices
5764 Sort_Case_Table (Table);
5766 Choices_Lo := Table (1).Choice_Lo;
5767 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5768 end Compute_Choices_Lo_And_Choices_Hi;
5769 end if;
5771 -- If no others choice in this subaggregate, or the aggregate
5772 -- comprises only an others choice, nothing to do.
5774 if not Need_To_Check then
5775 Cond := Empty;
5777 -- If we are dealing with an aggregate containing an others choice
5778 -- and positional components, we generate the following test:
5780 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5781 -- Ind_Typ'Pos (Aggr_Hi)
5782 -- then
5783 -- raise Constraint_Error;
5784 -- end if;
5786 elsif Nb_Elements > Uint_0 then
5787 Cond :=
5788 Make_Op_Gt (Loc,
5789 Left_Opnd =>
5790 Make_Op_Add (Loc,
5791 Left_Opnd =>
5792 Make_Attribute_Reference (Loc,
5793 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5794 Attribute_Name => Name_Pos,
5795 Expressions =>
5796 New_List
5797 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5798 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5800 Right_Opnd =>
5801 Make_Attribute_Reference (Loc,
5802 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5803 Attribute_Name => Name_Pos,
5804 Expressions => New_List (
5805 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5807 -- If we are dealing with an aggregate containing an others choice
5808 -- and discrete choices we generate the following test:
5810 -- [constraint_error when
5811 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5813 else
5814 Cond :=
5815 Make_Or_Else (Loc,
5816 Left_Opnd =>
5817 Make_Op_Lt (Loc,
5818 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5819 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5821 Right_Opnd =>
5822 Make_Op_Gt (Loc,
5823 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5824 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5825 end if;
5827 if Present (Cond) then
5828 Insert_Action (N,
5829 Make_Raise_Constraint_Error (Loc,
5830 Condition => Cond,
5831 Reason => CE_Length_Check_Failed));
5832 -- Questionable reason code, shouldn't that be a
5833 -- CE_Range_Check_Failed ???
5834 end if;
5836 -- Now look inside the subaggregate to see if there is more work
5838 if Dim < Aggr_Dimension then
5840 -- Process positional components
5842 if Present (Expressions (Sub_Aggr)) then
5843 Expr := First (Expressions (Sub_Aggr));
5844 while Present (Expr) loop
5845 Others_Check (Expr, Dim + 1);
5846 Next (Expr);
5847 end loop;
5848 end if;
5850 -- Process component associations
5852 if Present (Component_Associations (Sub_Aggr)) then
5853 Assoc := First (Component_Associations (Sub_Aggr));
5854 while Present (Assoc) loop
5855 Expr := Expression (Assoc);
5856 Others_Check (Expr, Dim + 1);
5857 Next (Assoc);
5858 end loop;
5859 end if;
5860 end if;
5861 end Others_Check;
5863 -------------------------
5864 -- Safe_Left_Hand_Side --
5865 -------------------------
5867 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5868 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5869 -- If the left-hand side includes an indexed component, check that
5870 -- the indexes are free of side effects.
5872 -------------------
5873 -- Is_Safe_Index --
5874 -------------------
5876 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5877 begin
5878 if Is_Entity_Name (Indx) then
5879 return True;
5881 elsif Nkind (Indx) = N_Integer_Literal then
5882 return True;
5884 elsif Nkind (Indx) = N_Function_Call
5885 and then Is_Entity_Name (Name (Indx))
5886 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5887 then
5888 return True;
5890 elsif Nkind (Indx) = N_Type_Conversion
5891 and then Is_Safe_Index (Expression (Indx))
5892 then
5893 return True;
5895 else
5896 return False;
5897 end if;
5898 end Is_Safe_Index;
5900 -- Start of processing for Safe_Left_Hand_Side
5902 begin
5903 if Is_Entity_Name (N) then
5904 return True;
5906 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5907 and then Safe_Left_Hand_Side (Prefix (N))
5908 then
5909 return True;
5911 elsif Nkind (N) = N_Indexed_Component
5912 and then Safe_Left_Hand_Side (Prefix (N))
5913 and then Is_Safe_Index (First (Expressions (N)))
5914 then
5915 return True;
5917 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5918 return Safe_Left_Hand_Side (Expression (N));
5920 else
5921 return False;
5922 end if;
5923 end Safe_Left_Hand_Side;
5925 -- Local variables
5927 Tmp : Entity_Id;
5928 -- Holds the temporary aggregate value
5930 Tmp_Decl : Node_Id;
5931 -- Holds the declaration of Tmp
5933 Aggr_Code : List_Id;
5934 Parent_Node : Node_Id;
5935 Parent_Kind : Node_Kind;
5937 -- Start of processing for Expand_Array_Aggregate
5939 begin
5940 -- Do not touch the special aggregates of attributes used for Asm calls
5942 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5943 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5944 then
5945 return;
5947 -- Do not expand an aggregate for an array type which contains tasks if
5948 -- the aggregate is associated with an unexpanded return statement of a
5949 -- build-in-place function. The aggregate is expanded when the related
5950 -- return statement (rewritten into an extended return) is processed.
5951 -- This delay ensures that any temporaries and initialization code
5952 -- generated for the aggregate appear in the proper return block and
5953 -- use the correct _chain and _master.
5955 elsif Has_Task (Base_Type (Etype (N)))
5956 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5957 and then Is_Build_In_Place_Function
5958 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5959 then
5960 return;
5962 -- Do not attempt expansion if error already detected. We may reach this
5963 -- point in spite of previous errors when compiling with -gnatq, to
5964 -- force all possible errors (this is the usual ACATS mode).
5966 elsif Error_Posted (N) then
5967 return;
5968 end if;
5970 -- If the semantic analyzer has determined that aggregate N will raise
5971 -- Constraint_Error at run time, then the aggregate node has been
5972 -- replaced with an N_Raise_Constraint_Error node and we should
5973 -- never get here.
5975 pragma Assert (not Raises_Constraint_Error (N));
5977 -- STEP 1a
5979 -- Check that the index range defined by aggregate bounds is
5980 -- compatible with corresponding index subtype.
5982 Index_Compatibility_Check : declare
5983 Aggr_Index_Range : Node_Id := First_Index (Typ);
5984 -- The current aggregate index range
5986 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5987 -- The corresponding index constraint against which we have to
5988 -- check the above aggregate index range.
5990 begin
5991 Compute_Others_Present (N, 1);
5993 for J in 1 .. Aggr_Dimension loop
5994 -- There is no need to emit a check if an others choice is present
5995 -- for this array aggregate dimension since in this case one of
5996 -- N's subaggregates has taken its bounds from the context and
5997 -- these bounds must have been checked already. In addition all
5998 -- subaggregates corresponding to the same dimension must all have
5999 -- the same bounds (checked in (c) below).
6001 if not Range_Checks_Suppressed (Etype (Index_Constraint))
6002 and then not Others_Present (J)
6003 then
6004 -- We don't use Checks.Apply_Range_Check here because it emits
6005 -- a spurious check. Namely it checks that the range defined by
6006 -- the aggregate bounds is nonempty. But we know this already
6007 -- if we get here.
6009 Check_Bounds (Aggr_Index_Range, Index_Constraint);
6010 end if;
6012 -- Save the low and high bounds of the aggregate index as well as
6013 -- the index type for later use in checks (b) and (c) below.
6015 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
6016 Aggr_High (J) := High_Bound (Aggr_Index_Range);
6018 Aggr_Index_Typ (J) := Etype (Index_Constraint);
6020 Next_Index (Aggr_Index_Range);
6021 Next_Index (Index_Constraint);
6022 end loop;
6023 end Index_Compatibility_Check;
6025 -- STEP 1b
6027 -- If an others choice is present check that no aggregate index is
6028 -- outside the bounds of the index constraint.
6030 Others_Check (N, 1);
6032 -- STEP 1c
6034 -- For multidimensional arrays make sure that all subaggregates
6035 -- corresponding to the same dimension have the same bounds.
6037 if Aggr_Dimension > 1 then
6038 Check_Same_Aggr_Bounds (N, 1);
6039 end if;
6041 -- STEP 1d
6043 -- If we have a default component value, or simple initialization is
6044 -- required for the component type, then we replace <> in component
6045 -- associations by the required default value.
6047 declare
6048 Default_Val : Node_Id;
6049 Assoc : Node_Id;
6051 begin
6052 if (Present (Default_Aspect_Component_Value (Typ))
6053 or else Needs_Simple_Initialization (Ctyp))
6054 and then Present (Component_Associations (N))
6055 then
6056 Assoc := First (Component_Associations (N));
6057 while Present (Assoc) loop
6058 if Nkind (Assoc) = N_Component_Association
6059 and then Box_Present (Assoc)
6060 then
6061 Set_Box_Present (Assoc, False);
6063 if Present (Default_Aspect_Component_Value (Typ)) then
6064 Default_Val := Default_Aspect_Component_Value (Typ);
6065 else
6066 Default_Val := Get_Simple_Init_Val (Ctyp, N);
6067 end if;
6069 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
6070 Analyze_And_Resolve (Expression (Assoc), Ctyp);
6071 end if;
6073 Next (Assoc);
6074 end loop;
6075 end if;
6076 end;
6078 -- STEP 2
6080 -- Here we test for is packed array aggregate that we can handle at
6081 -- compile time. If so, return with transformation done. Note that we do
6082 -- this even if the aggregate is nested, because once we have done this
6083 -- processing, there is no more nested aggregate.
6085 if Packed_Array_Aggregate_Handled (N) then
6086 return;
6087 end if;
6089 -- At this point we try to convert to positional form
6091 if Ekind (Current_Scope) = E_Package
6092 and then Static_Elaboration_Desired (Current_Scope)
6093 then
6094 Convert_To_Positional (N, Max_Others_Replicate => 100);
6095 else
6096 Convert_To_Positional (N);
6097 end if;
6099 -- if the result is no longer an aggregate (e.g. it may be a string
6100 -- literal, or a temporary which has the needed value), then we are
6101 -- done, since there is no longer a nested aggregate.
6103 if Nkind (N) /= N_Aggregate then
6104 return;
6106 -- We are also done if the result is an analyzed aggregate, indicating
6107 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6108 -- aggregate.
6110 elsif Analyzed (N) and then Is_Rewrite_Substitution (N) then
6111 return;
6112 end if;
6114 -- If all aggregate components are compile-time known and the aggregate
6115 -- has been flattened, nothing left to do. The same occurs if the
6116 -- aggregate is used to initialize the components of a statically
6117 -- allocated dispatch table.
6119 if Compile_Time_Known_Aggregate (N)
6120 or else Is_Static_Dispatch_Table_Aggregate (N)
6121 then
6122 Set_Expansion_Delayed (N, False);
6123 return;
6124 end if;
6126 -- Now see if back end processing is possible
6128 if Backend_Processing_Possible (N) then
6130 -- If the aggregate is static but the constraints are not, build
6131 -- a static subtype for the aggregate, so that Gigi can place it
6132 -- in static memory. Perform an unchecked_conversion to the non-
6133 -- static type imposed by the context.
6135 declare
6136 Itype : constant Entity_Id := Etype (N);
6137 Index : Node_Id;
6138 Needs_Type : Boolean := False;
6140 begin
6141 Index := First_Index (Itype);
6142 while Present (Index) loop
6143 if not Is_OK_Static_Subtype (Etype (Index)) then
6144 Needs_Type := True;
6145 exit;
6146 else
6147 Next_Index (Index);
6148 end if;
6149 end loop;
6151 if Needs_Type then
6152 Build_Constrained_Type (Positional => True);
6153 Rewrite (N, Unchecked_Convert_To (Itype, N));
6154 Analyze (N);
6155 end if;
6156 end;
6158 return;
6159 end if;
6161 -- STEP 3
6163 -- Delay expansion for nested aggregates: it will be taken care of when
6164 -- the parent aggregate is expanded.
6166 Parent_Node := Parent (N);
6167 Parent_Kind := Nkind (Parent_Node);
6169 if Parent_Kind = N_Qualified_Expression then
6170 Parent_Node := Parent (Parent_Node);
6171 Parent_Kind := Nkind (Parent_Node);
6172 end if;
6174 if Parent_Kind = N_Aggregate
6175 or else Parent_Kind = N_Extension_Aggregate
6176 or else Parent_Kind = N_Component_Association
6177 or else (Parent_Kind = N_Object_Declaration
6178 and then Needs_Finalization (Typ))
6179 or else (Parent_Kind = N_Assignment_Statement
6180 and then Inside_Init_Proc)
6181 then
6182 if Static_Array_Aggregate (N)
6183 or else Compile_Time_Known_Aggregate (N)
6184 then
6185 Set_Expansion_Delayed (N, False);
6186 return;
6187 else
6188 Set_Expansion_Delayed (N);
6189 return;
6190 end if;
6191 end if;
6193 -- STEP 4
6195 -- Look if in place aggregate expansion is possible
6197 -- For object declarations we build the aggregate in place, unless
6198 -- the array is bit-packed.
6200 -- For assignments we do the assignment in place if all the component
6201 -- associations have compile-time known values, or are default-
6202 -- initialized limited components, e.g. tasks. For other cases we
6203 -- create a temporary. The analysis for safety of on-line assignment
6204 -- is delicate, i.e. we don't know how to do it fully yet ???
6206 -- For allocators we assign to the designated object in place if the
6207 -- aggregate meets the same conditions as other in-place assignments.
6208 -- In this case the aggregate may not come from source but was created
6209 -- for default initialization, e.g. with Initialize_Scalars.
6211 if Requires_Transient_Scope (Typ) then
6212 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6213 end if;
6215 -- An array of limited components is built in place
6217 if Is_Limited_Type (Typ) then
6218 Maybe_In_Place_OK := True;
6220 elsif Has_Default_Init_Comps (N) then
6221 Maybe_In_Place_OK := False;
6223 elsif Is_Bit_Packed_Array (Typ)
6224 or else Has_Controlled_Component (Typ)
6225 then
6226 Maybe_In_Place_OK := False;
6228 else
6229 Maybe_In_Place_OK :=
6230 (Nkind (Parent (N)) = N_Assignment_Statement
6231 and then In_Place_Assign_OK)
6233 or else
6234 (Nkind (Parent (Parent (N))) = N_Allocator
6235 and then In_Place_Assign_OK);
6236 end if;
6238 -- If this is an array of tasks, it will be expanded into build-in-place
6239 -- assignments. Build an activation chain for the tasks now.
6241 if Has_Task (Etype (N)) then
6242 Build_Activation_Chain_Entity (N);
6243 end if;
6245 -- Perform in-place expansion of aggregate in an object declaration.
6246 -- Note: actions generated for the aggregate will be captured in an
6247 -- expression-with-actions statement so that they can be transferred
6248 -- to freeze actions later if there is an address clause for the
6249 -- object. (Note: we don't use a block statement because this would
6250 -- cause generated freeze nodes to be elaborated in the wrong scope).
6252 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6253 -- expected to appear in qualified form. In-place expansion eliminates
6254 -- the qualification and eventually violates this SPARK 05 restiction.
6256 -- Arrays of limited components must be built in place. The code
6257 -- previously excluded controlled components but this is an old
6258 -- oversight: the rules in 7.6 (17) are clear.
6260 if (not Has_Default_Init_Comps (N)
6261 or else Is_Limited_Type (Etype (N)))
6262 and then Comes_From_Source (Parent_Node)
6263 and then Parent_Kind = N_Object_Declaration
6264 and then Present (Expression (Parent_Node))
6265 and then not
6266 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
6267 and then not Is_Bit_Packed_Array (Typ)
6268 and then not Restriction_Check_Required (SPARK_05)
6269 then
6270 In_Place_Assign_OK_For_Declaration := True;
6271 Tmp := Defining_Identifier (Parent_Node);
6272 Set_No_Initialization (Parent_Node);
6273 Set_Expression (Parent_Node, Empty);
6275 -- Set kind and type of the entity, for use in the analysis
6276 -- of the subsequent assignments. If the nominal type is not
6277 -- constrained, build a subtype from the known bounds of the
6278 -- aggregate. If the declaration has a subtype mark, use it,
6279 -- otherwise use the itype of the aggregate.
6281 Set_Ekind (Tmp, E_Variable);
6283 if not Is_Constrained (Typ) then
6284 Build_Constrained_Type (Positional => False);
6286 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6287 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6288 then
6289 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6291 else
6292 Set_Size_Known_At_Compile_Time (Typ, False);
6293 Set_Etype (Tmp, Typ);
6294 end if;
6296 elsif Maybe_In_Place_OK
6297 and then Nkind (Parent (N)) = N_Qualified_Expression
6298 and then Nkind (Parent (Parent (N))) = N_Allocator
6299 then
6300 Set_Expansion_Delayed (N);
6301 return;
6303 -- Limited arrays in return statements are expanded when
6304 -- enclosing construct is expanded.
6306 elsif Maybe_In_Place_OK
6307 and then Nkind (Parent (N)) = N_Simple_Return_Statement
6308 then
6309 Set_Expansion_Delayed (N);
6310 return;
6312 -- In the remaining cases the aggregate is the RHS of an assignment
6314 elsif Maybe_In_Place_OK
6315 and then Safe_Left_Hand_Side (Name (Parent (N)))
6316 then
6317 Tmp := Name (Parent (N));
6319 if Etype (Tmp) /= Etype (N) then
6320 Apply_Length_Check (N, Etype (Tmp));
6322 if Nkind (N) = N_Raise_Constraint_Error then
6324 -- Static error, nothing further to expand
6326 return;
6327 end if;
6328 end if;
6330 -- If a slice assignment has an aggregate with a single others_choice,
6331 -- the assignment can be done in place even if bounds are not static,
6332 -- by converting it into a loop over the discrete range of the slice.
6334 elsif Maybe_In_Place_OK
6335 and then Nkind (Name (Parent (N))) = N_Slice
6336 and then Is_Others_Aggregate (N)
6337 then
6338 Tmp := Name (Parent (N));
6340 -- Set type of aggregate to be type of lhs in assignment, in order
6341 -- to suppress redundant length checks.
6343 Set_Etype (N, Etype (Tmp));
6345 -- Step 5
6347 -- In place aggregate expansion is not possible
6349 else
6350 Maybe_In_Place_OK := False;
6351 Tmp := Make_Temporary (Loc, 'A', N);
6352 Tmp_Decl :=
6353 Make_Object_Declaration (Loc,
6354 Defining_Identifier => Tmp,
6355 Object_Definition => New_Occurrence_Of (Typ, Loc));
6356 Set_No_Initialization (Tmp_Decl, True);
6357 Set_Warnings_Off (Tmp);
6359 -- If we are within a loop, the temporary will be pushed on the
6360 -- stack at each iteration. If the aggregate is the expression
6361 -- for an allocator, it will be immediately copied to the heap
6362 -- and can be reclaimed at once. We create a transient scope
6363 -- around the aggregate for this purpose.
6365 if Ekind (Current_Scope) = E_Loop
6366 and then Nkind (Parent (Parent (N))) = N_Allocator
6367 then
6368 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6369 end if;
6371 Insert_Action (N, Tmp_Decl);
6372 end if;
6374 -- Construct and insert the aggregate code. We can safely suppress index
6375 -- checks because this code is guaranteed not to raise CE on index
6376 -- checks. However we should *not* suppress all checks.
6378 declare
6379 Target : Node_Id;
6381 begin
6382 if Nkind (Tmp) = N_Defining_Identifier then
6383 Target := New_Occurrence_Of (Tmp, Loc);
6385 else
6386 if Has_Default_Init_Comps (N)
6387 and then not Maybe_In_Place_OK
6388 then
6389 -- Ada 2005 (AI-287): This case has not been analyzed???
6391 raise Program_Error;
6392 end if;
6394 -- Name in assignment is explicit dereference
6396 Target := New_Copy (Tmp);
6397 end if;
6399 -- If we are to generate an in place assignment for a declaration or
6400 -- an assignment statement, and the assignment can be done directly
6401 -- by the back end, then do not expand further.
6403 -- ??? We can also do that if in place expansion is not possible but
6404 -- then we could go into an infinite recursion.
6406 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6407 and then not CodePeer_Mode
6408 and then not Modify_Tree_For_C
6409 and then not Possible_Bit_Aligned_Component (Target)
6410 and then not Is_Possibly_Unaligned_Slice (Target)
6411 and then Aggr_Assignment_OK_For_Backend (N)
6412 then
6413 if Maybe_In_Place_OK then
6414 return;
6415 end if;
6417 Aggr_Code :=
6418 New_List (
6419 Make_Assignment_Statement (Loc,
6420 Name => Target,
6421 Expression => New_Copy_Tree (N)));
6423 else
6424 Aggr_Code :=
6425 Build_Array_Aggr_Code (N,
6426 Ctype => Ctyp,
6427 Index => First_Index (Typ),
6428 Into => Target,
6429 Scalar_Comp => Is_Scalar_Type (Ctyp));
6430 end if;
6432 -- Save the last assignment statement associated with the aggregate
6433 -- when building a controlled object. This reference is utilized by
6434 -- the finalization machinery when marking an object as successfully
6435 -- initialized.
6437 if Needs_Finalization (Typ)
6438 and then Is_Entity_Name (Target)
6439 and then Present (Entity (Target))
6440 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6441 then
6442 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6443 end if;
6444 end;
6446 -- If the aggregate is the expression in a declaration, the expanded
6447 -- code must be inserted after it. The defining entity might not come
6448 -- from source if this is part of an inlined body, but the declaration
6449 -- itself will.
6451 if Comes_From_Source (Tmp)
6452 or else
6453 (Nkind (Parent (N)) = N_Object_Declaration
6454 and then Comes_From_Source (Parent (N))
6455 and then Tmp = Defining_Entity (Parent (N)))
6456 then
6457 declare
6458 Node_After : constant Node_Id := Next (Parent_Node);
6460 begin
6461 Insert_Actions_After (Parent_Node, Aggr_Code);
6463 if Parent_Kind = N_Object_Declaration then
6464 Collect_Initialization_Statements
6465 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
6466 end if;
6467 end;
6469 else
6470 Insert_Actions (N, Aggr_Code);
6471 end if;
6473 -- If the aggregate has been assigned in place, remove the original
6474 -- assignment.
6476 if Nkind (Parent (N)) = N_Assignment_Statement
6477 and then Maybe_In_Place_OK
6478 then
6479 Rewrite (Parent (N), Make_Null_Statement (Loc));
6481 elsif Nkind (Parent (N)) /= N_Object_Declaration
6482 or else Tmp /= Defining_Identifier (Parent (N))
6483 then
6484 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6485 Analyze_And_Resolve (N, Typ);
6486 end if;
6487 end Expand_Array_Aggregate;
6489 ------------------------
6490 -- Expand_N_Aggregate --
6491 ------------------------
6493 procedure Expand_N_Aggregate (N : Node_Id) is
6494 begin
6495 -- Record aggregate case
6497 if Is_Record_Type (Etype (N)) then
6498 Expand_Record_Aggregate (N);
6500 -- Array aggregate case
6502 else
6503 -- A special case, if we have a string subtype with bounds 1 .. N,
6504 -- where N is known at compile time, and the aggregate is of the
6505 -- form (others => 'x'), with a single choice and no expressions,
6506 -- and N is less than 80 (an arbitrary limit for now), then replace
6507 -- the aggregate by the equivalent string literal (but do not mark
6508 -- it as static since it is not).
6510 -- Note: this entire circuit is redundant with respect to code in
6511 -- Expand_Array_Aggregate that collapses others choices to positional
6512 -- form, but there are two problems with that circuit:
6514 -- a) It is limited to very small cases due to ill-understood
6515 -- interactions with bootstrapping. That limit is removed by
6516 -- use of the No_Implicit_Loops restriction.
6518 -- b) It incorrectly ends up with the resulting expressions being
6519 -- considered static when they are not. For example, the
6520 -- following test should fail:
6522 -- pragma Restrictions (No_Implicit_Loops);
6523 -- package NonSOthers4 is
6524 -- B : constant String (1 .. 6) := (others => 'A');
6525 -- DH : constant String (1 .. 8) := B & "BB";
6526 -- X : Integer;
6527 -- pragma Export (C, X, Link_Name => DH);
6528 -- end;
6530 -- But it succeeds (DH looks static to pragma Export)
6532 -- To be sorted out ???
6534 if Present (Component_Associations (N)) then
6535 declare
6536 CA : constant Node_Id := First (Component_Associations (N));
6537 MX : constant := 80;
6539 begin
6540 if Nkind (First (Choice_List (CA))) = N_Others_Choice
6541 and then Nkind (Expression (CA)) = N_Character_Literal
6542 and then No (Expressions (N))
6543 then
6544 declare
6545 T : constant Entity_Id := Etype (N);
6546 X : constant Node_Id := First_Index (T);
6547 EC : constant Node_Id := Expression (CA);
6548 CV : constant Uint := Char_Literal_Value (EC);
6549 CC : constant Int := UI_To_Int (CV);
6551 begin
6552 if Nkind (X) = N_Range
6553 and then Compile_Time_Known_Value (Low_Bound (X))
6554 and then Expr_Value (Low_Bound (X)) = 1
6555 and then Compile_Time_Known_Value (High_Bound (X))
6556 then
6557 declare
6558 Hi : constant Uint := Expr_Value (High_Bound (X));
6560 begin
6561 if Hi <= MX then
6562 Start_String;
6564 for J in 1 .. UI_To_Int (Hi) loop
6565 Store_String_Char (Char_Code (CC));
6566 end loop;
6568 Rewrite (N,
6569 Make_String_Literal (Sloc (N),
6570 Strval => End_String));
6572 if CC >= Int (2 ** 16) then
6573 Set_Has_Wide_Wide_Character (N);
6574 elsif CC >= Int (2 ** 8) then
6575 Set_Has_Wide_Character (N);
6576 end if;
6578 Analyze_And_Resolve (N, T);
6579 Set_Is_Static_Expression (N, False);
6580 return;
6581 end if;
6582 end;
6583 end if;
6584 end;
6585 end if;
6586 end;
6587 end if;
6589 -- Not that special case, so normal expansion of array aggregate
6591 Expand_Array_Aggregate (N);
6592 end if;
6594 exception
6595 when RE_Not_Available =>
6596 return;
6597 end Expand_N_Aggregate;
6599 ------------------------------
6600 -- Expand_N_Delta_Aggregate --
6601 ------------------------------
6603 procedure Expand_N_Delta_Aggregate (N : Node_Id) is
6604 Loc : constant Source_Ptr := Sloc (N);
6605 Typ : constant Entity_Id := Etype (N);
6606 Decl : Node_Id;
6608 begin
6609 Decl :=
6610 Make_Object_Declaration (Loc,
6611 Defining_Identifier => Make_Temporary (Loc, 'T'),
6612 Object_Definition => New_Occurrence_Of (Typ, Loc),
6613 Expression => New_Copy_Tree (Expression (N)));
6615 if Is_Array_Type (Etype (N)) then
6616 Expand_Delta_Array_Aggregate (N, New_List (Decl));
6617 else
6618 Expand_Delta_Record_Aggregate (N, New_List (Decl));
6619 end if;
6620 end Expand_N_Delta_Aggregate;
6622 ----------------------------------
6623 -- Expand_Delta_Array_Aggregate --
6624 ----------------------------------
6626 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is
6627 Loc : constant Source_Ptr := Sloc (N);
6628 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6629 Assoc : Node_Id;
6631 function Generate_Loop (C : Node_Id) return Node_Id;
6632 -- Generate a loop containing individual component assignments for
6633 -- choices that are ranges, subtype indications, subtype names, and
6634 -- iterated component associations.
6636 -------------------
6637 -- Generate_Loop --
6638 -------------------
6640 function Generate_Loop (C : Node_Id) return Node_Id is
6641 Sl : constant Source_Ptr := Sloc (C);
6642 Ix : Entity_Id;
6644 begin
6645 if Nkind (Parent (C)) = N_Iterated_Component_Association then
6646 Ix :=
6647 Make_Defining_Identifier (Loc,
6648 Chars => (Chars (Defining_Identifier (Parent (C)))));
6649 else
6650 Ix := Make_Temporary (Sl, 'I');
6651 end if;
6653 return
6654 Make_Loop_Statement (Loc,
6655 Iteration_Scheme =>
6656 Make_Iteration_Scheme (Sl,
6657 Loop_Parameter_Specification =>
6658 Make_Loop_Parameter_Specification (Sl,
6659 Defining_Identifier => Ix,
6660 Discrete_Subtype_Definition => New_Copy_Tree (C))),
6662 Statements => New_List (
6663 Make_Assignment_Statement (Sl,
6664 Name =>
6665 Make_Indexed_Component (Sl,
6666 Prefix => New_Occurrence_Of (Temp, Sl),
6667 Expressions => New_List (New_Occurrence_Of (Ix, Sl))),
6668 Expression => New_Copy_Tree (Expression (Assoc)))),
6669 End_Label => Empty);
6670 end Generate_Loop;
6672 -- Local variables
6674 Choice : Node_Id;
6676 -- Start of processing for Expand_Delta_Array_Aggregate
6678 begin
6679 Assoc := First (Component_Associations (N));
6680 while Present (Assoc) loop
6681 Choice := First (Choice_List (Assoc));
6682 if Nkind (Assoc) = N_Iterated_Component_Association then
6683 while Present (Choice) loop
6684 Append_To (Deltas, Generate_Loop (Choice));
6685 Next (Choice);
6686 end loop;
6688 else
6689 while Present (Choice) loop
6691 -- Choice can be given by a range, a subtype indication, a
6692 -- subtype name, a scalar value, or an entity.
6694 if Nkind (Choice) = N_Range
6695 or else (Is_Entity_Name (Choice)
6696 and then Is_Type (Entity (Choice)))
6697 then
6698 Append_To (Deltas, Generate_Loop (Choice));
6700 elsif Nkind (Choice) = N_Subtype_Indication then
6701 Append_To (Deltas,
6702 Generate_Loop (Range_Expression (Constraint (Choice))));
6704 else
6705 Append_To (Deltas,
6706 Make_Assignment_Statement (Sloc (Choice),
6707 Name =>
6708 Make_Indexed_Component (Sloc (Choice),
6709 Prefix => New_Occurrence_Of (Temp, Loc),
6710 Expressions => New_List (New_Copy_Tree (Choice))),
6711 Expression => New_Copy_Tree (Expression (Assoc))));
6712 end if;
6714 Next (Choice);
6715 end loop;
6716 end if;
6718 Next (Assoc);
6719 end loop;
6721 Insert_Actions (N, Deltas);
6722 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6723 end Expand_Delta_Array_Aggregate;
6725 -----------------------------------
6726 -- Expand_Delta_Record_Aggregate --
6727 -----------------------------------
6729 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is
6730 Loc : constant Source_Ptr := Sloc (N);
6731 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6732 Assoc : Node_Id;
6733 Choice : Node_Id;
6735 begin
6736 Assoc := First (Component_Associations (N));
6738 while Present (Assoc) loop
6739 Choice := First (Choice_List (Assoc));
6740 while Present (Choice) loop
6741 Append_To (Deltas,
6742 Make_Assignment_Statement (Sloc (Choice),
6743 Name =>
6744 Make_Selected_Component (Sloc (Choice),
6745 Prefix => New_Occurrence_Of (Temp, Loc),
6746 Selector_Name => Make_Identifier (Loc, Chars (Choice))),
6747 Expression => New_Copy_Tree (Expression (Assoc))));
6748 Next (Choice);
6749 end loop;
6751 Next (Assoc);
6752 end loop;
6754 Insert_Actions (N, Deltas);
6755 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6756 end Expand_Delta_Record_Aggregate;
6758 ----------------------------------
6759 -- Expand_N_Extension_Aggregate --
6760 ----------------------------------
6762 -- If the ancestor part is an expression, add a component association for
6763 -- the parent field. If the type of the ancestor part is not the direct
6764 -- parent of the expected type, build recursively the needed ancestors.
6765 -- If the ancestor part is a subtype_mark, replace aggregate with a
6766 -- declaration for a temporary of the expected type, followed by
6767 -- individual assignments to the given components.
6769 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
6770 A : constant Node_Id := Ancestor_Part (N);
6771 Loc : constant Source_Ptr := Sloc (N);
6772 Typ : constant Entity_Id := Etype (N);
6774 begin
6775 -- If the ancestor is a subtype mark, an init proc must be called
6776 -- on the resulting object which thus has to be materialized in
6777 -- the front-end
6779 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
6780 Convert_To_Assignments (N, Typ);
6782 -- The extension aggregate is transformed into a record aggregate
6783 -- of the following form (c1 and c2 are inherited components)
6785 -- (Exp with c3 => a, c4 => b)
6786 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6788 else
6789 Set_Etype (N, Typ);
6791 if Tagged_Type_Expansion then
6792 Expand_Record_Aggregate (N,
6793 Orig_Tag =>
6794 New_Occurrence_Of
6795 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
6796 Parent_Expr => A);
6798 -- No tag is needed in the case of a VM
6800 else
6801 Expand_Record_Aggregate (N, Parent_Expr => A);
6802 end if;
6803 end if;
6805 exception
6806 when RE_Not_Available =>
6807 return;
6808 end Expand_N_Extension_Aggregate;
6810 -----------------------------
6811 -- Expand_Record_Aggregate --
6812 -----------------------------
6814 procedure Expand_Record_Aggregate
6815 (N : Node_Id;
6816 Orig_Tag : Node_Id := Empty;
6817 Parent_Expr : Node_Id := Empty)
6819 Loc : constant Source_Ptr := Sloc (N);
6820 Comps : constant List_Id := Component_Associations (N);
6821 Typ : constant Entity_Id := Etype (N);
6822 Base_Typ : constant Entity_Id := Base_Type (Typ);
6824 Static_Components : Boolean := True;
6825 -- Flag to indicate whether all components are compile-time known,
6826 -- and the aggregate can be constructed statically and handled by
6827 -- the back-end. Set to False by Component_OK_For_Backend.
6829 procedure Build_Back_End_Aggregate;
6830 -- Build a proper aggregate to be handled by the back-end
6832 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
6833 -- Returns true if N is an expression of composite type which can be
6834 -- fully evaluated at compile time without raising constraint error.
6835 -- Such expressions can be passed as is to Gigi without any expansion.
6837 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6838 -- set and constants whose expression is such an aggregate, recursively.
6840 function Component_OK_For_Backend return Boolean;
6841 -- Check for presence of a component which makes it impossible for the
6842 -- backend to process the aggregate, thus requiring the use of a series
6843 -- of assignment statements. Cases checked for are a nested aggregate
6844 -- needing Late_Expansion, the presence of a tagged component which may
6845 -- need tag adjustment, and a bit unaligned component reference.
6847 -- We also force expansion into assignments if a component is of a
6848 -- mutable type (including a private type with discriminants) because
6849 -- in that case the size of the component to be copied may be smaller
6850 -- than the side of the target, and there is no simple way for gigi
6851 -- to compute the size of the object to be copied.
6853 -- NOTE: This is part of the ongoing work to define precisely the
6854 -- interface between front-end and back-end handling of aggregates.
6855 -- In general it is desirable to pass aggregates as they are to gigi,
6856 -- in order to minimize elaboration code. This is one case where the
6857 -- semantics of Ada complicate the analysis and lead to anomalies in
6858 -- the gcc back-end if the aggregate is not expanded into assignments.
6860 -- NOTE: This sets the global Static_Components to False in most, but
6861 -- not all, cases when it returns False.
6863 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
6864 -- Return True if any element of L has Has_Per_Object_Constraint set.
6865 -- L should be the Choices component of an N_Component_Association.
6867 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
6868 -- If any ancestor of the current type is private, the aggregate
6869 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6870 -- because it will not be set when type and its parent are in the
6871 -- same scope, and the parent component needs expansion.
6873 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
6874 -- For nested aggregates return the ultimate enclosing aggregate; for
6875 -- non-nested aggregates return N.
6877 ------------------------------
6878 -- Build_Back_End_Aggregate --
6879 ------------------------------
6881 procedure Build_Back_End_Aggregate is
6882 Comp : Entity_Id;
6883 New_Comp : Node_Id;
6884 Tag_Value : Node_Id;
6886 begin
6887 if Nkind (N) = N_Aggregate then
6889 -- If the aggregate is static and can be handled by the back-end,
6890 -- nothing left to do.
6892 if Static_Components then
6893 Set_Compile_Time_Known_Aggregate (N);
6894 Set_Expansion_Delayed (N, False);
6895 end if;
6896 end if;
6898 -- If no discriminants, nothing special to do
6900 if not Has_Discriminants (Typ) then
6901 null;
6903 -- Case of discriminants present
6905 elsif Is_Derived_Type (Typ) then
6907 -- For untagged types, non-stored discriminants are replaced with
6908 -- stored discriminants, which are the ones that gigi uses to
6909 -- describe the type and its components.
6911 Generate_Aggregate_For_Derived_Type : declare
6912 procedure Prepend_Stored_Values (T : Entity_Id);
6913 -- Scan the list of stored discriminants of the type, and add
6914 -- their values to the aggregate being built.
6916 ---------------------------
6917 -- Prepend_Stored_Values --
6918 ---------------------------
6920 procedure Prepend_Stored_Values (T : Entity_Id) is
6921 Discr : Entity_Id;
6922 First_Comp : Node_Id := Empty;
6924 begin
6925 Discr := First_Stored_Discriminant (T);
6926 while Present (Discr) loop
6927 New_Comp :=
6928 Make_Component_Association (Loc,
6929 Choices => New_List (
6930 New_Occurrence_Of (Discr, Loc)),
6931 Expression =>
6932 New_Copy_Tree
6933 (Get_Discriminant_Value
6934 (Discr,
6935 Typ,
6936 Discriminant_Constraint (Typ))));
6938 if No (First_Comp) then
6939 Prepend_To (Component_Associations (N), New_Comp);
6940 else
6941 Insert_After (First_Comp, New_Comp);
6942 end if;
6944 First_Comp := New_Comp;
6945 Next_Stored_Discriminant (Discr);
6946 end loop;
6947 end Prepend_Stored_Values;
6949 -- Local variables
6951 Constraints : constant List_Id := New_List;
6953 Discr : Entity_Id;
6954 Decl : Node_Id;
6955 Num_Disc : Nat := 0;
6956 Num_Gird : Nat := 0;
6958 -- Start of processing for Generate_Aggregate_For_Derived_Type
6960 begin
6961 -- Remove the associations for the discriminant of derived type
6963 declare
6964 First_Comp : Node_Id;
6966 begin
6967 First_Comp := First (Component_Associations (N));
6968 while Present (First_Comp) loop
6969 Comp := First_Comp;
6970 Next (First_Comp);
6972 if Ekind (Entity (First (Choices (Comp)))) =
6973 E_Discriminant
6974 then
6975 Remove (Comp);
6976 Num_Disc := Num_Disc + 1;
6977 end if;
6978 end loop;
6979 end;
6981 -- Insert stored discriminant associations in the correct
6982 -- order. If there are more stored discriminants than new
6983 -- discriminants, there is at least one new discriminant that
6984 -- constrains more than one of the stored discriminants. In
6985 -- this case we need to construct a proper subtype of the
6986 -- parent type, in order to supply values to all the
6987 -- components. Otherwise there is one-one correspondence
6988 -- between the constraints and the stored discriminants.
6990 Discr := First_Stored_Discriminant (Base_Type (Typ));
6991 while Present (Discr) loop
6992 Num_Gird := Num_Gird + 1;
6993 Next_Stored_Discriminant (Discr);
6994 end loop;
6996 -- Case of more stored discriminants than new discriminants
6998 if Num_Gird > Num_Disc then
7000 -- Create a proper subtype of the parent type, which is the
7001 -- proper implementation type for the aggregate, and convert
7002 -- it to the intended target type.
7004 Discr := First_Stored_Discriminant (Base_Type (Typ));
7005 while Present (Discr) loop
7006 New_Comp :=
7007 New_Copy_Tree
7008 (Get_Discriminant_Value
7009 (Discr,
7010 Typ,
7011 Discriminant_Constraint (Typ)));
7013 Append (New_Comp, Constraints);
7014 Next_Stored_Discriminant (Discr);
7015 end loop;
7017 Decl :=
7018 Make_Subtype_Declaration (Loc,
7019 Defining_Identifier => Make_Temporary (Loc, 'T'),
7020 Subtype_Indication =>
7021 Make_Subtype_Indication (Loc,
7022 Subtype_Mark =>
7023 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
7024 Constraint =>
7025 Make_Index_Or_Discriminant_Constraint
7026 (Loc, Constraints)));
7028 Insert_Action (N, Decl);
7029 Prepend_Stored_Values (Base_Type (Typ));
7031 Set_Etype (N, Defining_Identifier (Decl));
7032 Set_Analyzed (N);
7034 Rewrite (N, Unchecked_Convert_To (Typ, N));
7035 Analyze (N);
7037 -- Case where we do not have fewer new discriminants than
7038 -- stored discriminants, so in this case we can simply use the
7039 -- stored discriminants of the subtype.
7041 else
7042 Prepend_Stored_Values (Typ);
7043 end if;
7044 end Generate_Aggregate_For_Derived_Type;
7045 end if;
7047 if Is_Tagged_Type (Typ) then
7049 -- In the tagged case, _parent and _tag component must be created
7051 -- Reset Null_Present unconditionally. Tagged records always have
7052 -- at least one field (the tag or the parent).
7054 Set_Null_Record_Present (N, False);
7056 -- When the current aggregate comes from the expansion of an
7057 -- extension aggregate, the parent expr is replaced by an
7058 -- aggregate formed by selected components of this expr.
7060 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
7061 Comp := First_Component_Or_Discriminant (Typ);
7062 while Present (Comp) loop
7064 -- Skip all expander-generated components
7066 if not Comes_From_Source (Original_Record_Component (Comp))
7067 then
7068 null;
7070 else
7071 New_Comp :=
7072 Make_Selected_Component (Loc,
7073 Prefix =>
7074 Unchecked_Convert_To (Typ,
7075 Duplicate_Subexpr (Parent_Expr, True)),
7076 Selector_Name => New_Occurrence_Of (Comp, Loc));
7078 Append_To (Comps,
7079 Make_Component_Association (Loc,
7080 Choices => New_List (
7081 New_Occurrence_Of (Comp, Loc)),
7082 Expression => New_Comp));
7084 Analyze_And_Resolve (New_Comp, Etype (Comp));
7085 end if;
7087 Next_Component_Or_Discriminant (Comp);
7088 end loop;
7089 end if;
7091 -- Compute the value for the Tag now, if the type is a root it
7092 -- will be included in the aggregate right away, otherwise it will
7093 -- be propagated to the parent aggregate.
7095 if Present (Orig_Tag) then
7096 Tag_Value := Orig_Tag;
7098 elsif not Tagged_Type_Expansion then
7099 Tag_Value := Empty;
7101 else
7102 Tag_Value :=
7103 New_Occurrence_Of
7104 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
7105 end if;
7107 -- For a derived type, an aggregate for the parent is formed with
7108 -- all the inherited components.
7110 if Is_Derived_Type (Typ) then
7111 declare
7112 First_Comp : Node_Id;
7113 Parent_Comps : List_Id;
7114 Parent_Aggr : Node_Id;
7115 Parent_Name : Node_Id;
7117 begin
7118 -- Remove the inherited component association from the
7119 -- aggregate and store them in the parent aggregate
7121 First_Comp := First (Component_Associations (N));
7122 Parent_Comps := New_List;
7123 while Present (First_Comp)
7124 and then
7125 Scope (Original_Record_Component
7126 (Entity (First (Choices (First_Comp))))) /=
7127 Base_Typ
7128 loop
7129 Comp := First_Comp;
7130 Next (First_Comp);
7131 Remove (Comp);
7132 Append (Comp, Parent_Comps);
7133 end loop;
7135 Parent_Aggr :=
7136 Make_Aggregate (Loc,
7137 Component_Associations => Parent_Comps);
7138 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
7140 -- Find the _parent component
7142 Comp := First_Component (Typ);
7143 while Chars (Comp) /= Name_uParent loop
7144 Comp := Next_Component (Comp);
7145 end loop;
7147 Parent_Name := New_Occurrence_Of (Comp, Loc);
7149 -- Insert the parent aggregate
7151 Prepend_To (Component_Associations (N),
7152 Make_Component_Association (Loc,
7153 Choices => New_List (Parent_Name),
7154 Expression => Parent_Aggr));
7156 -- Expand recursively the parent propagating the right Tag
7158 Expand_Record_Aggregate
7159 (Parent_Aggr, Tag_Value, Parent_Expr);
7161 -- The ancestor part may be a nested aggregate that has
7162 -- delayed expansion: recheck now.
7164 if not Component_OK_For_Backend then
7165 Convert_To_Assignments (N, Typ);
7166 end if;
7167 end;
7169 -- For a root type, the tag component is added (unless compiling
7170 -- for the VMs, where tags are implicit).
7172 elsif Tagged_Type_Expansion then
7173 declare
7174 Tag_Name : constant Node_Id :=
7175 New_Occurrence_Of
7176 (First_Tag_Component (Typ), Loc);
7177 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7178 Conv_Node : constant Node_Id :=
7179 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7181 begin
7182 Set_Etype (Conv_Node, Typ_Tag);
7183 Prepend_To (Component_Associations (N),
7184 Make_Component_Association (Loc,
7185 Choices => New_List (Tag_Name),
7186 Expression => Conv_Node));
7187 end;
7188 end if;
7189 end if;
7190 end Build_Back_End_Aggregate;
7192 ----------------------------------------
7193 -- Compile_Time_Known_Composite_Value --
7194 ----------------------------------------
7196 function Compile_Time_Known_Composite_Value
7197 (N : Node_Id) return Boolean
7199 begin
7200 -- If we have an entity name, then see if it is the name of a
7201 -- constant and if so, test the corresponding constant value.
7203 if Is_Entity_Name (N) then
7204 declare
7205 E : constant Entity_Id := Entity (N);
7206 V : Node_Id;
7207 begin
7208 if Ekind (E) /= E_Constant then
7209 return False;
7210 else
7211 V := Constant_Value (E);
7212 return Present (V)
7213 and then Compile_Time_Known_Composite_Value (V);
7214 end if;
7215 end;
7217 -- We have a value, see if it is compile time known
7219 else
7220 if Nkind (N) = N_Aggregate then
7221 return Compile_Time_Known_Aggregate (N);
7222 end if;
7224 -- All other types of values are not known at compile time
7226 return False;
7227 end if;
7229 end Compile_Time_Known_Composite_Value;
7231 ------------------------------
7232 -- Component_OK_For_Backend --
7233 ------------------------------
7235 function Component_OK_For_Backend return Boolean is
7236 C : Node_Id;
7237 Expr_Q : Node_Id;
7239 begin
7240 if No (Comps) then
7241 return True;
7242 end if;
7244 C := First (Comps);
7245 while Present (C) loop
7247 -- If the component has box initialization, expansion is needed
7248 -- and component is not ready for backend.
7250 if Box_Present (C) then
7251 return False;
7252 end if;
7254 if Nkind (Expression (C)) = N_Qualified_Expression then
7255 Expr_Q := Expression (Expression (C));
7256 else
7257 Expr_Q := Expression (C);
7258 end if;
7260 -- Return False for array components whose bounds raise
7261 -- constraint error.
7263 declare
7264 Comp : constant Entity_Id := First (Choices (C));
7265 Indx : Node_Id;
7267 begin
7268 if Present (Etype (Comp))
7269 and then Is_Array_Type (Etype (Comp))
7270 then
7271 Indx := First_Index (Etype (Comp));
7272 while Present (Indx) loop
7273 if Nkind (Type_Low_Bound (Etype (Indx))) =
7274 N_Raise_Constraint_Error
7275 or else Nkind (Type_High_Bound (Etype (Indx))) =
7276 N_Raise_Constraint_Error
7277 then
7278 return False;
7279 end if;
7281 Indx := Next_Index (Indx);
7282 end loop;
7283 end if;
7284 end;
7286 -- Return False if the aggregate has any associations for tagged
7287 -- components that may require tag adjustment.
7289 -- These are cases where the source expression may have a tag that
7290 -- could differ from the component tag (e.g., can occur for type
7291 -- conversions and formal parameters). (Tag adjustment not needed
7292 -- if Tagged_Type_Expansion because object tags are implicit in
7293 -- the machine.)
7295 if Is_Tagged_Type (Etype (Expr_Q))
7296 and then
7297 (Nkind (Expr_Q) = N_Type_Conversion
7298 or else
7299 (Is_Entity_Name (Expr_Q)
7300 and then Is_Formal (Entity (Expr_Q))))
7301 and then Tagged_Type_Expansion
7302 then
7303 Static_Components := False;
7304 return False;
7306 elsif Is_Delayed_Aggregate (Expr_Q) then
7307 Static_Components := False;
7308 return False;
7310 elsif Nkind (Expr_Q) = N_Quantified_Expression then
7311 Static_Components := False;
7312 return False;
7314 elsif Possible_Bit_Aligned_Component (Expr_Q) then
7315 Static_Components := False;
7316 return False;
7318 elsif Modify_Tree_For_C
7319 and then Nkind (C) = N_Component_Association
7320 and then Has_Per_Object_Constraint (Choices (C))
7321 then
7322 Static_Components := False;
7323 return False;
7325 elsif Modify_Tree_For_C
7326 and then Nkind (Expr_Q) = N_Identifier
7327 and then Is_Array_Type (Etype (Expr_Q))
7328 then
7329 Static_Components := False;
7330 return False;
7332 elsif Modify_Tree_For_C
7333 and then Nkind (Expr_Q) = N_Type_Conversion
7334 and then Is_Array_Type (Etype (Expr_Q))
7335 then
7336 Static_Components := False;
7337 return False;
7338 end if;
7340 if Is_Elementary_Type (Etype (Expr_Q)) then
7341 if not Compile_Time_Known_Value (Expr_Q) then
7342 Static_Components := False;
7343 end if;
7345 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
7346 Static_Components := False;
7348 if Is_Private_Type (Etype (Expr_Q))
7349 and then Has_Discriminants (Etype (Expr_Q))
7350 then
7351 return False;
7352 end if;
7353 end if;
7355 Next (C);
7356 end loop;
7358 return True;
7359 end Component_OK_For_Backend;
7361 -------------------------------
7362 -- Has_Per_Object_Constraint --
7363 -------------------------------
7365 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
7366 N : Node_Id := First (L);
7367 begin
7368 while Present (N) loop
7369 if Is_Entity_Name (N)
7370 and then Present (Entity (N))
7371 and then Has_Per_Object_Constraint (Entity (N))
7372 then
7373 return True;
7374 end if;
7376 Next (N);
7377 end loop;
7379 return False;
7380 end Has_Per_Object_Constraint;
7382 -----------------------------------
7383 -- Has_Visible_Private_Ancestor --
7384 -----------------------------------
7386 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
7387 R : constant Entity_Id := Root_Type (Id);
7388 T1 : Entity_Id := Id;
7390 begin
7391 loop
7392 if Is_Private_Type (T1) then
7393 return True;
7395 elsif T1 = R then
7396 return False;
7398 else
7399 T1 := Etype (T1);
7400 end if;
7401 end loop;
7402 end Has_Visible_Private_Ancestor;
7404 -------------------------
7405 -- Top_Level_Aggregate --
7406 -------------------------
7408 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
7409 Aggr : Node_Id;
7411 begin
7412 Aggr := N;
7413 while Present (Parent (Aggr))
7414 and then Nkind_In (Parent (Aggr), N_Aggregate,
7415 N_Component_Association)
7416 loop
7417 Aggr := Parent (Aggr);
7418 end loop;
7420 return Aggr;
7421 end Top_Level_Aggregate;
7423 -- Local variables
7425 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
7427 -- Start of processing for Expand_Record_Aggregate
7429 begin
7430 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7431 -- to prevent a piecemeal assignment even if the aggregate is to be
7432 -- expanded. We create a temporary for the aggregate, and assign the
7433 -- temporary instead, so that the back end can generate an atomic move
7434 -- for it.
7436 if Is_Atomic_VFA_Aggregate (N) then
7437 return;
7439 -- No special management required for aggregates used to initialize
7440 -- statically allocated dispatch tables
7442 elsif Is_Static_Dispatch_Table_Aggregate (N) then
7443 return;
7444 end if;
7446 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7447 -- are build-in-place function calls. The assignments will each turn
7448 -- into a build-in-place function call. If components are all static,
7449 -- we can pass the aggregate to the back end regardless of limitedness.
7451 -- Extension aggregates, aggregates in extended return statements, and
7452 -- aggregates for C++ imported types must be expanded.
7454 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
7455 if not Nkind_In (Parent (N), N_Component_Association,
7456 N_Object_Declaration)
7457 then
7458 Convert_To_Assignments (N, Typ);
7460 elsif Nkind (N) = N_Extension_Aggregate
7461 or else Convention (Typ) = Convention_CPP
7462 then
7463 Convert_To_Assignments (N, Typ);
7465 elsif not Size_Known_At_Compile_Time (Typ)
7466 or else not Component_OK_For_Backend
7467 or else not Static_Components
7468 then
7469 Convert_To_Assignments (N, Typ);
7471 -- In all other cases, build a proper aggregate to be handled by
7472 -- the back-end
7474 else
7475 Build_Back_End_Aggregate;
7476 end if;
7478 -- Gigi doesn't properly handle temporaries of variable size so we
7479 -- generate it in the front-end
7481 elsif not Size_Known_At_Compile_Time (Typ)
7482 and then Tagged_Type_Expansion
7483 then
7484 Convert_To_Assignments (N, Typ);
7486 -- An aggregate used to initialize a controlled object must be turned
7487 -- into component assignments as the components themselves may require
7488 -- finalization actions such as adjustment.
7490 elsif Needs_Finalization (Typ) then
7491 Convert_To_Assignments (N, Typ);
7493 -- Ada 2005 (AI-287): In case of default initialized components we
7494 -- convert the aggregate into assignments.
7496 elsif Has_Default_Init_Comps (N) then
7497 Convert_To_Assignments (N, Typ);
7499 -- Check components
7501 elsif not Component_OK_For_Backend then
7502 Convert_To_Assignments (N, Typ);
7504 -- If an ancestor is private, some components are not inherited and we
7505 -- cannot expand into a record aggregate.
7507 elsif Has_Visible_Private_Ancestor (Typ) then
7508 Convert_To_Assignments (N, Typ);
7510 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7511 -- is not able to handle the aggregate for Late_Request.
7513 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
7514 Convert_To_Assignments (N, Typ);
7516 -- If the tagged types covers interface types we need to initialize all
7517 -- hidden components containing pointers to secondary dispatch tables.
7519 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
7520 Convert_To_Assignments (N, Typ);
7522 -- If some components are mutable, the size of the aggregate component
7523 -- may be distinct from the default size of the type component, so
7524 -- we need to expand to insure that the back-end copies the proper
7525 -- size of the data. However, if the aggregate is the initial value of
7526 -- a constant, the target is immutable and might be built statically
7527 -- if components are appropriate.
7529 elsif Has_Mutable_Components (Typ)
7530 and then
7531 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
7532 or else not Constant_Present (Parent (Top_Level_Aggr))
7533 or else not Static_Components)
7534 then
7535 Convert_To_Assignments (N, Typ);
7537 -- If the type involved has bit aligned components, then we are not sure
7538 -- that the back end can handle this case correctly.
7540 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
7541 Convert_To_Assignments (N, Typ);
7543 -- When generating C, only generate an aggregate when declaring objects
7544 -- since C does not support aggregates in e.g. assignment statements.
7546 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
7547 Convert_To_Assignments (N, Typ);
7549 -- In all other cases, build a proper aggregate to be handled by gigi
7551 else
7552 Build_Back_End_Aggregate;
7553 end if;
7554 end Expand_Record_Aggregate;
7556 ----------------------------
7557 -- Has_Default_Init_Comps --
7558 ----------------------------
7560 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
7561 Comps : constant List_Id := Component_Associations (N);
7562 C : Node_Id;
7563 Expr : Node_Id;
7565 begin
7566 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
7568 if No (Comps) then
7569 return False;
7570 end if;
7572 if Has_Self_Reference (N) then
7573 return True;
7574 end if;
7576 -- Check if any direct component has default initialized components
7578 C := First (Comps);
7579 while Present (C) loop
7580 if Box_Present (C) then
7581 return True;
7582 end if;
7584 Next (C);
7585 end loop;
7587 -- Recursive call in case of aggregate expression
7589 C := First (Comps);
7590 while Present (C) loop
7591 Expr := Expression (C);
7593 if Present (Expr)
7594 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
7595 and then Has_Default_Init_Comps (Expr)
7596 then
7597 return True;
7598 end if;
7600 Next (C);
7601 end loop;
7603 return False;
7604 end Has_Default_Init_Comps;
7606 ----------------------------------------
7607 -- Is_Build_In_Place_Aggregate_Return --
7608 ----------------------------------------
7610 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean is
7611 P : Node_Id := Parent (N);
7613 begin
7614 while Nkind (P) = N_Qualified_Expression loop
7615 P := Parent (P);
7616 end loop;
7618 if Nkind (P) = N_Simple_Return_Statement then
7619 null;
7621 elsif Nkind (Parent (P)) = N_Extended_Return_Statement then
7622 P := Parent (P);
7624 else
7625 return False;
7626 end if;
7628 return
7629 Is_Build_In_Place_Function
7630 (Return_Applies_To (Return_Statement_Entity (P)));
7631 end Is_Build_In_Place_Aggregate_Return;
7633 --------------------------
7634 -- Is_Delayed_Aggregate --
7635 --------------------------
7637 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
7638 Node : Node_Id := N;
7639 Kind : Node_Kind := Nkind (Node);
7641 begin
7642 if Kind = N_Qualified_Expression then
7643 Node := Expression (Node);
7644 Kind := Nkind (Node);
7645 end if;
7647 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
7648 return False;
7649 else
7650 return Expansion_Delayed (Node);
7651 end if;
7652 end Is_Delayed_Aggregate;
7654 ---------------------------
7655 -- In_Object_Declaration --
7656 ---------------------------
7658 function In_Object_Declaration (N : Node_Id) return Boolean is
7659 P : Node_Id := Parent (N);
7660 begin
7661 while Present (P) loop
7662 if Nkind (P) = N_Object_Declaration then
7663 return True;
7664 end if;
7666 P := Parent (P);
7667 end loop;
7669 return False;
7670 end In_Object_Declaration;
7672 ----------------------------------------
7673 -- Is_Static_Dispatch_Table_Aggregate --
7674 ----------------------------------------
7676 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
7677 Typ : constant Entity_Id := Base_Type (Etype (N));
7679 begin
7680 return Building_Static_Dispatch_Tables
7681 and then Tagged_Type_Expansion
7682 and then RTU_Loaded (Ada_Tags)
7684 -- Avoid circularity when rebuilding the compiler
7686 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
7687 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
7688 or else
7689 Typ = RTE (RE_Address_Array)
7690 or else
7691 Typ = RTE (RE_Type_Specific_Data)
7692 or else
7693 Typ = RTE (RE_Tag_Table)
7694 or else
7695 (RTE_Available (RE_Interface_Data)
7696 and then Typ = RTE (RE_Interface_Data))
7697 or else
7698 (RTE_Available (RE_Interfaces_Array)
7699 and then Typ = RTE (RE_Interfaces_Array))
7700 or else
7701 (RTE_Available (RE_Interface_Data_Element)
7702 and then Typ = RTE (RE_Interface_Data_Element)));
7703 end Is_Static_Dispatch_Table_Aggregate;
7705 -----------------------------
7706 -- Is_Two_Dim_Packed_Array --
7707 -----------------------------
7709 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
7710 C : constant Int := UI_To_Int (Component_Size (Typ));
7711 begin
7712 return Number_Dimensions (Typ) = 2
7713 and then Is_Bit_Packed_Array (Typ)
7714 and then (C = 1 or else C = 2 or else C = 4);
7715 end Is_Two_Dim_Packed_Array;
7717 --------------------
7718 -- Late_Expansion --
7719 --------------------
7721 function Late_Expansion
7722 (N : Node_Id;
7723 Typ : Entity_Id;
7724 Target : Node_Id) return List_Id
7726 Aggr_Code : List_Id;
7728 begin
7729 if Is_Array_Type (Etype (N)) then
7730 Aggr_Code :=
7731 Build_Array_Aggr_Code
7732 (N => N,
7733 Ctype => Component_Type (Etype (N)),
7734 Index => First_Index (Typ),
7735 Into => Target,
7736 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
7737 Indexes => No_List);
7739 -- Directly or indirectly (e.g. access protected procedure) a record
7741 else
7742 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
7743 end if;
7745 -- Save the last assignment statement associated with the aggregate
7746 -- when building a controlled object. This reference is utilized by
7747 -- the finalization machinery when marking an object as successfully
7748 -- initialized.
7750 if Needs_Finalization (Typ)
7751 and then Is_Entity_Name (Target)
7752 and then Present (Entity (Target))
7753 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
7754 then
7755 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
7756 end if;
7758 return Aggr_Code;
7759 end Late_Expansion;
7761 ----------------------------------
7762 -- Make_OK_Assignment_Statement --
7763 ----------------------------------
7765 function Make_OK_Assignment_Statement
7766 (Sloc : Source_Ptr;
7767 Name : Node_Id;
7768 Expression : Node_Id) return Node_Id
7770 begin
7771 Set_Assignment_OK (Name);
7772 return Make_Assignment_Statement (Sloc, Name, Expression);
7773 end Make_OK_Assignment_Statement;
7775 -----------------------
7776 -- Number_Of_Choices --
7777 -----------------------
7779 function Number_Of_Choices (N : Node_Id) return Nat is
7780 Assoc : Node_Id;
7781 Choice : Node_Id;
7783 Nb_Choices : Nat := 0;
7785 begin
7786 if Present (Expressions (N)) then
7787 return 0;
7788 end if;
7790 Assoc := First (Component_Associations (N));
7791 while Present (Assoc) loop
7792 Choice := First (Choice_List (Assoc));
7793 while Present (Choice) loop
7794 if Nkind (Choice) /= N_Others_Choice then
7795 Nb_Choices := Nb_Choices + 1;
7796 end if;
7798 Next (Choice);
7799 end loop;
7801 Next (Assoc);
7802 end loop;
7804 return Nb_Choices;
7805 end Number_Of_Choices;
7807 ------------------------------------
7808 -- Packed_Array_Aggregate_Handled --
7809 ------------------------------------
7811 -- The current version of this procedure will handle at compile time
7812 -- any array aggregate that meets these conditions:
7814 -- One and two dimensional, bit packed
7815 -- Underlying packed type is modular type
7816 -- Bounds are within 32-bit Int range
7817 -- All bounds and values are static
7819 -- Note: for now, in the 2-D case, we only handle component sizes of
7820 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7822 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
7823 Loc : constant Source_Ptr := Sloc (N);
7824 Typ : constant Entity_Id := Etype (N);
7825 Ctyp : constant Entity_Id := Component_Type (Typ);
7827 Not_Handled : exception;
7828 -- Exception raised if this aggregate cannot be handled
7830 begin
7831 -- Handle one- or two dimensional bit packed array
7833 if not Is_Bit_Packed_Array (Typ)
7834 or else Number_Dimensions (Typ) > 2
7835 then
7836 return False;
7837 end if;
7839 -- If two-dimensional, check whether it can be folded, and transformed
7840 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7841 -- the original type.
7843 if Number_Dimensions (Typ) = 2 then
7844 return Two_Dim_Packed_Array_Handled (N);
7845 end if;
7847 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
7848 return False;
7849 end if;
7851 if not Is_Scalar_Type (Component_Type (Typ))
7852 and then Has_Non_Standard_Rep (Component_Type (Typ))
7853 then
7854 return False;
7855 end if;
7857 declare
7858 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
7860 Lo : Node_Id;
7861 Hi : Node_Id;
7862 -- Bounds of index type
7864 Lob : Uint;
7865 Hib : Uint;
7866 -- Values of bounds if compile time known
7868 function Get_Component_Val (N : Node_Id) return Uint;
7869 -- Given a expression value N of the component type Ctyp, returns a
7870 -- value of Csiz (component size) bits representing this value. If
7871 -- the value is nonstatic or any other reason exists why the value
7872 -- cannot be returned, then Not_Handled is raised.
7874 -----------------------
7875 -- Get_Component_Val --
7876 -----------------------
7878 function Get_Component_Val (N : Node_Id) return Uint is
7879 Val : Uint;
7881 begin
7882 -- We have to analyze the expression here before doing any further
7883 -- processing here. The analysis of such expressions is deferred
7884 -- till expansion to prevent some problems of premature analysis.
7886 Analyze_And_Resolve (N, Ctyp);
7888 -- Must have a compile time value. String literals have to be
7889 -- converted into temporaries as well, because they cannot easily
7890 -- be converted into their bit representation.
7892 if not Compile_Time_Known_Value (N)
7893 or else Nkind (N) = N_String_Literal
7894 then
7895 raise Not_Handled;
7896 end if;
7898 Val := Expr_Rep_Value (N);
7900 -- Adjust for bias, and strip proper number of bits
7902 if Has_Biased_Representation (Ctyp) then
7903 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
7904 end if;
7906 return Val mod Uint_2 ** Csiz;
7907 end Get_Component_Val;
7909 -- Here we know we have a one dimensional bit packed array
7911 begin
7912 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
7914 -- Cannot do anything if bounds are dynamic
7916 if not Compile_Time_Known_Value (Lo)
7917 or else
7918 not Compile_Time_Known_Value (Hi)
7919 then
7920 return False;
7921 end if;
7923 -- Or are silly out of range of int bounds
7925 Lob := Expr_Value (Lo);
7926 Hib := Expr_Value (Hi);
7928 if not UI_Is_In_Int_Range (Lob)
7929 or else
7930 not UI_Is_In_Int_Range (Hib)
7931 then
7932 return False;
7933 end if;
7935 -- At this stage we have a suitable aggregate for handling at compile
7936 -- time. The only remaining checks are that the values of expressions
7937 -- in the aggregate are compile-time known (checks are performed by
7938 -- Get_Component_Val), and that any subtypes or ranges are statically
7939 -- known.
7941 -- If the aggregate is not fully positional at this stage, then
7942 -- convert it to positional form. Either this will fail, in which
7943 -- case we can do nothing, or it will succeed, in which case we have
7944 -- succeeded in handling the aggregate and transforming it into a
7945 -- modular value, or it will stay an aggregate, in which case we
7946 -- have failed to create a packed value for it.
7948 if Present (Component_Associations (N)) then
7949 Convert_To_Positional
7950 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7951 return Nkind (N) /= N_Aggregate;
7952 end if;
7954 -- Otherwise we are all positional, so convert to proper value
7956 declare
7957 Lov : constant Int := UI_To_Int (Lob);
7958 Hiv : constant Int := UI_To_Int (Hib);
7960 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
7961 -- The length of the array (number of elements)
7963 Aggregate_Val : Uint;
7964 -- Value of aggregate. The value is set in the low order bits of
7965 -- this value. For the little-endian case, the values are stored
7966 -- from low-order to high-order and for the big-endian case the
7967 -- values are stored from high-order to low-order. Note that gigi
7968 -- will take care of the conversions to left justify the value in
7969 -- the big endian case (because of left justified modular type
7970 -- processing), so we do not have to worry about that here.
7972 Lit : Node_Id;
7973 -- Integer literal for resulting constructed value
7975 Shift : Nat;
7976 -- Shift count from low order for next value
7978 Incr : Int;
7979 -- Shift increment for loop
7981 Expr : Node_Id;
7982 -- Next expression from positional parameters of aggregate
7984 Left_Justified : Boolean;
7985 -- Set True if we are filling the high order bits of the target
7986 -- value (i.e. the value is left justified).
7988 begin
7989 -- For little endian, we fill up the low order bits of the target
7990 -- value. For big endian we fill up the high order bits of the
7991 -- target value (which is a left justified modular value).
7993 Left_Justified := Bytes_Big_Endian;
7995 -- Switch justification if using -gnatd8
7997 if Debug_Flag_8 then
7998 Left_Justified := not Left_Justified;
7999 end if;
8001 -- Switch justfification if reverse storage order
8003 if Reverse_Storage_Order (Base_Type (Typ)) then
8004 Left_Justified := not Left_Justified;
8005 end if;
8007 if Left_Justified then
8008 Shift := Csiz * (Len - 1);
8009 Incr := -Csiz;
8010 else
8011 Shift := 0;
8012 Incr := +Csiz;
8013 end if;
8015 -- Loop to set the values
8017 if Len = 0 then
8018 Aggregate_Val := Uint_0;
8019 else
8020 Expr := First (Expressions (N));
8021 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
8023 for J in 2 .. Len loop
8024 Shift := Shift + Incr;
8025 Next (Expr);
8026 Aggregate_Val :=
8027 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
8028 end loop;
8029 end if;
8031 -- Now we can rewrite with the proper value
8033 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
8034 Set_Print_In_Hex (Lit);
8036 -- Construct the expression using this literal. Note that it is
8037 -- important to qualify the literal with its proper modular type
8038 -- since universal integer does not have the required range and
8039 -- also this is a left justified modular type, which is important
8040 -- in the big-endian case.
8042 Rewrite (N,
8043 Unchecked_Convert_To (Typ,
8044 Make_Qualified_Expression (Loc,
8045 Subtype_Mark =>
8046 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
8047 Expression => Lit)));
8049 Analyze_And_Resolve (N, Typ);
8050 return True;
8051 end;
8052 end;
8054 exception
8055 when Not_Handled =>
8056 return False;
8057 end Packed_Array_Aggregate_Handled;
8059 ----------------------------
8060 -- Has_Mutable_Components --
8061 ----------------------------
8063 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
8064 Comp : Entity_Id;
8066 begin
8067 Comp := First_Component (Typ);
8068 while Present (Comp) loop
8069 if Is_Record_Type (Etype (Comp))
8070 and then Has_Discriminants (Etype (Comp))
8071 and then not Is_Constrained (Etype (Comp))
8072 then
8073 return True;
8074 end if;
8076 Next_Component (Comp);
8077 end loop;
8079 return False;
8080 end Has_Mutable_Components;
8082 ------------------------------
8083 -- Initialize_Discriminants --
8084 ------------------------------
8086 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
8087 Loc : constant Source_Ptr := Sloc (N);
8088 Bas : constant Entity_Id := Base_Type (Typ);
8089 Par : constant Entity_Id := Etype (Bas);
8090 Decl : constant Node_Id := Parent (Par);
8091 Ref : Node_Id;
8093 begin
8094 if Is_Tagged_Type (Bas)
8095 and then Is_Derived_Type (Bas)
8096 and then Has_Discriminants (Par)
8097 and then Has_Discriminants (Bas)
8098 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
8099 and then Nkind (Decl) = N_Full_Type_Declaration
8100 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
8101 and then
8102 Present (Variant_Part (Component_List (Type_Definition (Decl))))
8103 and then Nkind (N) /= N_Extension_Aggregate
8104 then
8106 -- Call init proc to set discriminants.
8107 -- There should eventually be a special procedure for this ???
8109 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
8110 Insert_Actions_After (N,
8111 Build_Initialization_Call (Sloc (N), Ref, Typ));
8112 end if;
8113 end Initialize_Discriminants;
8115 ----------------
8116 -- Must_Slide --
8117 ----------------
8119 function Must_Slide
8120 (Obj_Type : Entity_Id;
8121 Typ : Entity_Id) return Boolean
8123 L1, L2, H1, H2 : Node_Id;
8125 begin
8126 -- No sliding if the type of the object is not established yet, if it is
8127 -- an unconstrained type whose actual subtype comes from the aggregate,
8128 -- or if the two types are identical.
8130 if not Is_Array_Type (Obj_Type) then
8131 return False;
8133 elsif not Is_Constrained (Obj_Type) then
8134 return False;
8136 elsif Typ = Obj_Type then
8137 return False;
8139 else
8140 -- Sliding can only occur along the first dimension
8142 Get_Index_Bounds (First_Index (Typ), L1, H1);
8143 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
8145 if not Is_OK_Static_Expression (L1) or else
8146 not Is_OK_Static_Expression (L2) or else
8147 not Is_OK_Static_Expression (H1) or else
8148 not Is_OK_Static_Expression (H2)
8149 then
8150 return False;
8151 else
8152 return Expr_Value (L1) /= Expr_Value (L2)
8153 or else
8154 Expr_Value (H1) /= Expr_Value (H2);
8155 end if;
8156 end if;
8157 end Must_Slide;
8159 ---------------------------------
8160 -- Process_Transient_Component --
8161 ---------------------------------
8163 procedure Process_Transient_Component
8164 (Loc : Source_Ptr;
8165 Comp_Typ : Entity_Id;
8166 Init_Expr : Node_Id;
8167 Fin_Call : out Node_Id;
8168 Hook_Clear : out Node_Id;
8169 Aggr : Node_Id := Empty;
8170 Stmts : List_Id := No_List)
8172 procedure Add_Item (Item : Node_Id);
8173 -- Insert arbitrary node Item into the tree depending on the values of
8174 -- Aggr and Stmts.
8176 --------------
8177 -- Add_Item --
8178 --------------
8180 procedure Add_Item (Item : Node_Id) is
8181 begin
8182 if Present (Aggr) then
8183 Insert_Action (Aggr, Item);
8184 else
8185 pragma Assert (Present (Stmts));
8186 Append_To (Stmts, Item);
8187 end if;
8188 end Add_Item;
8190 -- Local variables
8192 Hook_Assign : Node_Id;
8193 Hook_Decl : Node_Id;
8194 Ptr_Decl : Node_Id;
8195 Res_Decl : Node_Id;
8196 Res_Id : Entity_Id;
8197 Res_Typ : Entity_Id;
8199 -- Start of processing for Process_Transient_Component
8201 begin
8202 -- Add the access type, which provides a reference to the function
8203 -- result. Generate:
8205 -- type Res_Typ is access all Comp_Typ;
8207 Res_Typ := Make_Temporary (Loc, 'A');
8208 Set_Ekind (Res_Typ, E_General_Access_Type);
8209 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
8211 Add_Item
8212 (Make_Full_Type_Declaration (Loc,
8213 Defining_Identifier => Res_Typ,
8214 Type_Definition =>
8215 Make_Access_To_Object_Definition (Loc,
8216 All_Present => True,
8217 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
8219 -- Add the temporary which captures the result of the function call.
8220 -- Generate:
8222 -- Res : constant Res_Typ := Init_Expr'Reference;
8224 -- Note that this temporary is effectively a transient object because
8225 -- its lifetime is bounded by the current array or record component.
8227 Res_Id := Make_Temporary (Loc, 'R');
8228 Set_Ekind (Res_Id, E_Constant);
8229 Set_Etype (Res_Id, Res_Typ);
8231 -- Mark the transient object as successfully processed to avoid double
8232 -- finalization.
8234 Set_Is_Finalized_Transient (Res_Id);
8236 -- Signal the general finalization machinery that this transient object
8237 -- should not be considered for finalization actions because its cleanup
8238 -- will be performed by Process_Transient_Component_Completion.
8240 Set_Is_Ignored_Transient (Res_Id);
8242 Res_Decl :=
8243 Make_Object_Declaration (Loc,
8244 Defining_Identifier => Res_Id,
8245 Constant_Present => True,
8246 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
8247 Expression =>
8248 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
8250 Add_Item (Res_Decl);
8252 -- Construct all pieces necessary to hook and finalize the transient
8253 -- result.
8255 Build_Transient_Object_Statements
8256 (Obj_Decl => Res_Decl,
8257 Fin_Call => Fin_Call,
8258 Hook_Assign => Hook_Assign,
8259 Hook_Clear => Hook_Clear,
8260 Hook_Decl => Hook_Decl,
8261 Ptr_Decl => Ptr_Decl);
8263 -- Add the access type which provides a reference to the transient
8264 -- result. Generate:
8266 -- type Ptr_Typ is access all Comp_Typ;
8268 Add_Item (Ptr_Decl);
8270 -- Add the temporary which acts as a hook to the transient result.
8271 -- Generate:
8273 -- Hook : Ptr_Typ := null;
8275 Add_Item (Hook_Decl);
8277 -- Attach the transient result to the hook. Generate:
8279 -- Hook := Ptr_Typ (Res);
8281 Add_Item (Hook_Assign);
8283 -- The original initialization expression now references the value of
8284 -- the temporary function result. Generate:
8286 -- Res.all
8288 Rewrite (Init_Expr,
8289 Make_Explicit_Dereference (Loc,
8290 Prefix => New_Occurrence_Of (Res_Id, Loc)));
8291 end Process_Transient_Component;
8293 --------------------------------------------
8294 -- Process_Transient_Component_Completion --
8295 --------------------------------------------
8297 procedure Process_Transient_Component_Completion
8298 (Loc : Source_Ptr;
8299 Aggr : Node_Id;
8300 Fin_Call : Node_Id;
8301 Hook_Clear : Node_Id;
8302 Stmts : List_Id)
8304 Exceptions_OK : constant Boolean :=
8305 not Restriction_Active (No_Exception_Propagation);
8307 begin
8308 pragma Assert (Present (Hook_Clear));
8310 -- Generate the following code if exception propagation is allowed:
8312 -- declare
8313 -- Abort : constant Boolean := Triggered_By_Abort;
8314 -- <or>
8315 -- Abort : constant Boolean := False; -- no abort
8317 -- E : Exception_Occurrence;
8318 -- Raised : Boolean := False;
8320 -- begin
8321 -- [Abort_Defer;]
8323 -- begin
8324 -- Hook := null;
8325 -- [Deep_]Finalize (Res.all);
8327 -- exception
8328 -- when others =>
8329 -- if not Raised then
8330 -- Raised := True;
8331 -- Save_Occurrence (E,
8332 -- Get_Curent_Excep.all.all);
8333 -- end if;
8334 -- end;
8336 -- [Abort_Undefer;]
8338 -- if Raised and then not Abort then
8339 -- Raise_From_Controlled_Operation (E);
8340 -- end if;
8341 -- end;
8343 if Exceptions_OK then
8344 Abort_And_Exception : declare
8345 Blk_Decls : constant List_Id := New_List;
8346 Blk_Stmts : constant List_Id := New_List;
8347 Fin_Stmts : constant List_Id := New_List;
8349 Fin_Data : Finalization_Exception_Data;
8351 begin
8352 -- Create the declarations of the two flags and the exception
8353 -- occurrence.
8355 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
8357 -- Generate:
8358 -- Abort_Defer;
8360 if Abort_Allowed then
8361 Append_To (Blk_Stmts,
8362 Build_Runtime_Call (Loc, RE_Abort_Defer));
8363 end if;
8365 -- Wrap the hook clear and the finalization call in order to trap
8366 -- a potential exception.
8368 Append_To (Fin_Stmts, Hook_Clear);
8370 if Present (Fin_Call) then
8371 Append_To (Fin_Stmts, Fin_Call);
8372 end if;
8374 Append_To (Blk_Stmts,
8375 Make_Block_Statement (Loc,
8376 Handled_Statement_Sequence =>
8377 Make_Handled_Sequence_Of_Statements (Loc,
8378 Statements => Fin_Stmts,
8379 Exception_Handlers => New_List (
8380 Build_Exception_Handler (Fin_Data)))));
8382 -- Generate:
8383 -- Abort_Undefer;
8385 if Abort_Allowed then
8386 Append_To (Blk_Stmts,
8387 Build_Runtime_Call (Loc, RE_Abort_Undefer));
8388 end if;
8390 -- Reraise the potential exception with a proper "upgrade" to
8391 -- Program_Error if needed.
8393 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
8395 -- Wrap everything in a block
8397 Append_To (Stmts,
8398 Make_Block_Statement (Loc,
8399 Declarations => Blk_Decls,
8400 Handled_Statement_Sequence =>
8401 Make_Handled_Sequence_Of_Statements (Loc,
8402 Statements => Blk_Stmts)));
8403 end Abort_And_Exception;
8405 -- Generate the following code if exception propagation is not allowed
8406 -- and aborts are allowed:
8408 -- begin
8409 -- Abort_Defer;
8410 -- Hook := null;
8411 -- [Deep_]Finalize (Res.all);
8412 -- at end
8413 -- Abort_Undefer_Direct;
8414 -- end;
8416 elsif Abort_Allowed then
8417 Abort_Only : declare
8418 Blk_Stmts : constant List_Id := New_List;
8420 begin
8421 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8422 Append_To (Blk_Stmts, Hook_Clear);
8424 if Present (Fin_Call) then
8425 Append_To (Blk_Stmts, Fin_Call);
8426 end if;
8428 Append_To (Stmts,
8429 Build_Abort_Undefer_Block (Loc,
8430 Stmts => Blk_Stmts,
8431 Context => Aggr));
8432 end Abort_Only;
8434 -- Otherwise generate:
8436 -- Hook := null;
8437 -- [Deep_]Finalize (Res.all);
8439 else
8440 Append_To (Stmts, Hook_Clear);
8442 if Present (Fin_Call) then
8443 Append_To (Stmts, Fin_Call);
8444 end if;
8445 end if;
8446 end Process_Transient_Component_Completion;
8448 ---------------------
8449 -- Sort_Case_Table --
8450 ---------------------
8452 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
8453 L : constant Int := Case_Table'First;
8454 U : constant Int := Case_Table'Last;
8455 K : Int;
8456 J : Int;
8457 T : Case_Bounds;
8459 begin
8460 K := L;
8461 while K /= U loop
8462 T := Case_Table (K + 1);
8464 J := K + 1;
8465 while J /= L
8466 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
8467 Expr_Value (T.Choice_Lo)
8468 loop
8469 Case_Table (J) := Case_Table (J - 1);
8470 J := J - 1;
8471 end loop;
8473 Case_Table (J) := T;
8474 K := K + 1;
8475 end loop;
8476 end Sort_Case_Table;
8478 ----------------------------
8479 -- Static_Array_Aggregate --
8480 ----------------------------
8482 function Static_Array_Aggregate (N : Node_Id) return Boolean is
8483 Bounds : constant Node_Id := Aggregate_Bounds (N);
8485 Typ : constant Entity_Id := Etype (N);
8486 Comp_Type : constant Entity_Id := Component_Type (Typ);
8487 Agg : Node_Id;
8488 Expr : Node_Id;
8489 Lo : Node_Id;
8490 Hi : Node_Id;
8492 begin
8493 if Is_Tagged_Type (Typ)
8494 or else Is_Controlled (Typ)
8495 or else Is_Packed (Typ)
8496 then
8497 return False;
8498 end if;
8500 if Present (Bounds)
8501 and then Nkind (Bounds) = N_Range
8502 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
8503 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
8504 then
8505 Lo := Low_Bound (Bounds);
8506 Hi := High_Bound (Bounds);
8508 if No (Component_Associations (N)) then
8510 -- Verify that all components are static integers
8512 Expr := First (Expressions (N));
8513 while Present (Expr) loop
8514 if Nkind (Expr) /= N_Integer_Literal then
8515 return False;
8516 end if;
8518 Next (Expr);
8519 end loop;
8521 return True;
8523 else
8524 -- We allow only a single named association, either a static
8525 -- range or an others_clause, with a static expression.
8527 Expr := First (Component_Associations (N));
8529 if Present (Expressions (N)) then
8530 return False;
8532 elsif Present (Next (Expr)) then
8533 return False;
8535 elsif Present (Next (First (Choice_List (Expr)))) then
8536 return False;
8538 else
8539 -- The aggregate is static if all components are literals,
8540 -- or else all its components are static aggregates for the
8541 -- component type. We also limit the size of a static aggregate
8542 -- to prevent runaway static expressions.
8544 if Is_Array_Type (Comp_Type)
8545 or else Is_Record_Type (Comp_Type)
8546 then
8547 if Nkind (Expression (Expr)) /= N_Aggregate
8548 or else
8549 not Compile_Time_Known_Aggregate (Expression (Expr))
8550 then
8551 return False;
8552 end if;
8554 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
8555 return False;
8556 end if;
8558 if not Aggr_Size_OK (N, Typ) then
8559 return False;
8560 end if;
8562 -- Create a positional aggregate with the right number of
8563 -- copies of the expression.
8565 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
8567 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
8568 loop
8569 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
8571 -- The copied expression must be analyzed and resolved.
8572 -- Besides setting the type, this ensures that static
8573 -- expressions are appropriately marked as such.
8575 Analyze_And_Resolve
8576 (Last (Expressions (Agg)), Component_Type (Typ));
8577 end loop;
8579 Set_Aggregate_Bounds (Agg, Bounds);
8580 Set_Etype (Agg, Typ);
8581 Set_Analyzed (Agg);
8582 Rewrite (N, Agg);
8583 Set_Compile_Time_Known_Aggregate (N);
8585 return True;
8586 end if;
8587 end if;
8589 else
8590 return False;
8591 end if;
8592 end Static_Array_Aggregate;
8594 ----------------------------------
8595 -- Two_Dim_Packed_Array_Handled --
8596 ----------------------------------
8598 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
8599 Loc : constant Source_Ptr := Sloc (N);
8600 Typ : constant Entity_Id := Etype (N);
8601 Ctyp : constant Entity_Id := Component_Type (Typ);
8602 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
8603 Packed_Array : constant Entity_Id :=
8604 Packed_Array_Impl_Type (Base_Type (Typ));
8606 One_Comp : Node_Id;
8607 -- Expression in original aggregate
8609 One_Dim : Node_Id;
8610 -- One-dimensional subaggregate
8612 begin
8614 -- For now, only deal with cases where an integral number of elements
8615 -- fit in a single byte. This includes the most common boolean case.
8617 if not (Comp_Size = 1 or else
8618 Comp_Size = 2 or else
8619 Comp_Size = 4)
8620 then
8621 return False;
8622 end if;
8624 Convert_To_Positional
8625 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8627 -- Verify that all components are static
8629 if Nkind (N) = N_Aggregate
8630 and then Compile_Time_Known_Aggregate (N)
8631 then
8632 null;
8634 -- The aggregate may have been reanalyzed and converted already
8636 elsif Nkind (N) /= N_Aggregate then
8637 return True;
8639 -- If component associations remain, the aggregate is not static
8641 elsif Present (Component_Associations (N)) then
8642 return False;
8644 else
8645 One_Dim := First (Expressions (N));
8646 while Present (One_Dim) loop
8647 if Present (Component_Associations (One_Dim)) then
8648 return False;
8649 end if;
8651 One_Comp := First (Expressions (One_Dim));
8652 while Present (One_Comp) loop
8653 if not Is_OK_Static_Expression (One_Comp) then
8654 return False;
8655 end if;
8657 Next (One_Comp);
8658 end loop;
8660 Next (One_Dim);
8661 end loop;
8662 end if;
8664 -- Two-dimensional aggregate is now fully positional so pack one
8665 -- dimension to create a static one-dimensional array, and rewrite
8666 -- as an unchecked conversion to the original type.
8668 declare
8669 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
8670 -- The packed array type is a byte array
8672 Packed_Num : Nat;
8673 -- Number of components accumulated in current byte
8675 Comps : List_Id;
8676 -- Assembled list of packed values for equivalent aggregate
8678 Comp_Val : Uint;
8679 -- Integer value of component
8681 Incr : Int;
8682 -- Step size for packing
8684 Init_Shift : Int;
8685 -- Endian-dependent start position for packing
8687 Shift : Int;
8688 -- Current insertion position
8690 Val : Int;
8691 -- Component of packed array being assembled
8693 begin
8694 Comps := New_List;
8695 Val := 0;
8696 Packed_Num := 0;
8698 -- Account for endianness. See corresponding comment in
8699 -- Packed_Array_Aggregate_Handled concerning the following.
8701 if Bytes_Big_Endian
8702 xor Debug_Flag_8
8703 xor Reverse_Storage_Order (Base_Type (Typ))
8704 then
8705 Init_Shift := Byte_Size - Comp_Size;
8706 Incr := -Comp_Size;
8707 else
8708 Init_Shift := 0;
8709 Incr := +Comp_Size;
8710 end if;
8712 -- Iterate over each subaggregate
8714 Shift := Init_Shift;
8715 One_Dim := First (Expressions (N));
8716 while Present (One_Dim) loop
8717 One_Comp := First (Expressions (One_Dim));
8718 while Present (One_Comp) loop
8719 if Packed_Num = Byte_Size / Comp_Size then
8721 -- Byte is complete, add to list of expressions
8723 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8724 Val := 0;
8725 Shift := Init_Shift;
8726 Packed_Num := 0;
8728 else
8729 Comp_Val := Expr_Rep_Value (One_Comp);
8731 -- Adjust for bias, and strip proper number of bits
8733 if Has_Biased_Representation (Ctyp) then
8734 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
8735 end if;
8737 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
8738 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
8739 Shift := Shift + Incr;
8740 One_Comp := Next (One_Comp);
8741 Packed_Num := Packed_Num + 1;
8742 end if;
8743 end loop;
8745 One_Dim := Next (One_Dim);
8746 end loop;
8748 if Packed_Num > 0 then
8750 -- Add final incomplete byte if present
8752 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8753 end if;
8755 Rewrite (N,
8756 Unchecked_Convert_To (Typ,
8757 Make_Qualified_Expression (Loc,
8758 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
8759 Expression => Make_Aggregate (Loc, Expressions => Comps))));
8760 Analyze_And_Resolve (N);
8761 return True;
8762 end;
8763 end Two_Dim_Packed_Array_Handled;
8765 end Exp_Aggr;