2018-08-29 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / exp_aggr.adb
blobf65230f5a72f78119c6777813ed5d24df974c393
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);
6358 -- If we are within a loop, the temporary will be pushed on the
6359 -- stack at each iteration. If the aggregate is the expression
6360 -- for an allocator, it will be immediately copied to the heap
6361 -- and can be reclaimed at once. We create a transient scope
6362 -- around the aggregate for this purpose.
6364 if Ekind (Current_Scope) = E_Loop
6365 and then Nkind (Parent (Parent (N))) = N_Allocator
6366 then
6367 Establish_Transient_Scope (N, Manage_Sec_Stack => False);
6368 end if;
6370 Insert_Action (N, Tmp_Decl);
6371 end if;
6373 -- Construct and insert the aggregate code. We can safely suppress index
6374 -- checks because this code is guaranteed not to raise CE on index
6375 -- checks. However we should *not* suppress all checks.
6377 declare
6378 Target : Node_Id;
6380 begin
6381 if Nkind (Tmp) = N_Defining_Identifier then
6382 Target := New_Occurrence_Of (Tmp, Loc);
6384 else
6385 if Has_Default_Init_Comps (N)
6386 and then not Maybe_In_Place_OK
6387 then
6388 -- Ada 2005 (AI-287): This case has not been analyzed???
6390 raise Program_Error;
6391 end if;
6393 -- Name in assignment is explicit dereference
6395 Target := New_Copy (Tmp);
6396 end if;
6398 -- If we are to generate an in place assignment for a declaration or
6399 -- an assignment statement, and the assignment can be done directly
6400 -- by the back end, then do not expand further.
6402 -- ??? We can also do that if in place expansion is not possible but
6403 -- then we could go into an infinite recursion.
6405 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6406 and then not CodePeer_Mode
6407 and then not Modify_Tree_For_C
6408 and then not Possible_Bit_Aligned_Component (Target)
6409 and then not Is_Possibly_Unaligned_Slice (Target)
6410 and then Aggr_Assignment_OK_For_Backend (N)
6411 then
6412 if Maybe_In_Place_OK then
6413 return;
6414 end if;
6416 Aggr_Code :=
6417 New_List (
6418 Make_Assignment_Statement (Loc,
6419 Name => Target,
6420 Expression => New_Copy_Tree (N)));
6422 else
6423 Aggr_Code :=
6424 Build_Array_Aggr_Code (N,
6425 Ctype => Ctyp,
6426 Index => First_Index (Typ),
6427 Into => Target,
6428 Scalar_Comp => Is_Scalar_Type (Ctyp));
6429 end if;
6431 -- Save the last assignment statement associated with the aggregate
6432 -- when building a controlled object. This reference is utilized by
6433 -- the finalization machinery when marking an object as successfully
6434 -- initialized.
6436 if Needs_Finalization (Typ)
6437 and then Is_Entity_Name (Target)
6438 and then Present (Entity (Target))
6439 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6440 then
6441 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6442 end if;
6443 end;
6445 -- If the aggregate is the expression in a declaration, the expanded
6446 -- code must be inserted after it. The defining entity might not come
6447 -- from source if this is part of an inlined body, but the declaration
6448 -- itself will.
6450 if Comes_From_Source (Tmp)
6451 or else
6452 (Nkind (Parent (N)) = N_Object_Declaration
6453 and then Comes_From_Source (Parent (N))
6454 and then Tmp = Defining_Entity (Parent (N)))
6455 then
6456 declare
6457 Node_After : constant Node_Id := Next (Parent_Node);
6459 begin
6460 Insert_Actions_After (Parent_Node, Aggr_Code);
6462 if Parent_Kind = N_Object_Declaration then
6463 Collect_Initialization_Statements
6464 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
6465 end if;
6466 end;
6468 else
6469 Insert_Actions (N, Aggr_Code);
6470 end if;
6472 -- If the aggregate has been assigned in place, remove the original
6473 -- assignment.
6475 if Nkind (Parent (N)) = N_Assignment_Statement
6476 and then Maybe_In_Place_OK
6477 then
6478 Rewrite (Parent (N), Make_Null_Statement (Loc));
6480 elsif Nkind (Parent (N)) /= N_Object_Declaration
6481 or else Tmp /= Defining_Identifier (Parent (N))
6482 then
6483 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6484 Analyze_And_Resolve (N, Typ);
6485 end if;
6486 end Expand_Array_Aggregate;
6488 ------------------------
6489 -- Expand_N_Aggregate --
6490 ------------------------
6492 procedure Expand_N_Aggregate (N : Node_Id) is
6493 begin
6494 -- Record aggregate case
6496 if Is_Record_Type (Etype (N)) then
6497 Expand_Record_Aggregate (N);
6499 -- Array aggregate case
6501 else
6502 -- A special case, if we have a string subtype with bounds 1 .. N,
6503 -- where N is known at compile time, and the aggregate is of the
6504 -- form (others => 'x'), with a single choice and no expressions,
6505 -- and N is less than 80 (an arbitrary limit for now), then replace
6506 -- the aggregate by the equivalent string literal (but do not mark
6507 -- it as static since it is not).
6509 -- Note: this entire circuit is redundant with respect to code in
6510 -- Expand_Array_Aggregate that collapses others choices to positional
6511 -- form, but there are two problems with that circuit:
6513 -- a) It is limited to very small cases due to ill-understood
6514 -- interactions with bootstrapping. That limit is removed by
6515 -- use of the No_Implicit_Loops restriction.
6517 -- b) It incorrectly ends up with the resulting expressions being
6518 -- considered static when they are not. For example, the
6519 -- following test should fail:
6521 -- pragma Restrictions (No_Implicit_Loops);
6522 -- package NonSOthers4 is
6523 -- B : constant String (1 .. 6) := (others => 'A');
6524 -- DH : constant String (1 .. 8) := B & "BB";
6525 -- X : Integer;
6526 -- pragma Export (C, X, Link_Name => DH);
6527 -- end;
6529 -- But it succeeds (DH looks static to pragma Export)
6531 -- To be sorted out ???
6533 if Present (Component_Associations (N)) then
6534 declare
6535 CA : constant Node_Id := First (Component_Associations (N));
6536 MX : constant := 80;
6538 begin
6539 if Nkind (First (Choice_List (CA))) = N_Others_Choice
6540 and then Nkind (Expression (CA)) = N_Character_Literal
6541 and then No (Expressions (N))
6542 then
6543 declare
6544 T : constant Entity_Id := Etype (N);
6545 X : constant Node_Id := First_Index (T);
6546 EC : constant Node_Id := Expression (CA);
6547 CV : constant Uint := Char_Literal_Value (EC);
6548 CC : constant Int := UI_To_Int (CV);
6550 begin
6551 if Nkind (X) = N_Range
6552 and then Compile_Time_Known_Value (Low_Bound (X))
6553 and then Expr_Value (Low_Bound (X)) = 1
6554 and then Compile_Time_Known_Value (High_Bound (X))
6555 then
6556 declare
6557 Hi : constant Uint := Expr_Value (High_Bound (X));
6559 begin
6560 if Hi <= MX then
6561 Start_String;
6563 for J in 1 .. UI_To_Int (Hi) loop
6564 Store_String_Char (Char_Code (CC));
6565 end loop;
6567 Rewrite (N,
6568 Make_String_Literal (Sloc (N),
6569 Strval => End_String));
6571 if CC >= Int (2 ** 16) then
6572 Set_Has_Wide_Wide_Character (N);
6573 elsif CC >= Int (2 ** 8) then
6574 Set_Has_Wide_Character (N);
6575 end if;
6577 Analyze_And_Resolve (N, T);
6578 Set_Is_Static_Expression (N, False);
6579 return;
6580 end if;
6581 end;
6582 end if;
6583 end;
6584 end if;
6585 end;
6586 end if;
6588 -- Not that special case, so normal expansion of array aggregate
6590 Expand_Array_Aggregate (N);
6591 end if;
6593 exception
6594 when RE_Not_Available =>
6595 return;
6596 end Expand_N_Aggregate;
6598 ------------------------------
6599 -- Expand_N_Delta_Aggregate --
6600 ------------------------------
6602 procedure Expand_N_Delta_Aggregate (N : Node_Id) is
6603 Loc : constant Source_Ptr := Sloc (N);
6604 Typ : constant Entity_Id := Etype (N);
6605 Decl : Node_Id;
6607 begin
6608 Decl :=
6609 Make_Object_Declaration (Loc,
6610 Defining_Identifier => Make_Temporary (Loc, 'T'),
6611 Object_Definition => New_Occurrence_Of (Typ, Loc),
6612 Expression => New_Copy_Tree (Expression (N)));
6614 if Is_Array_Type (Etype (N)) then
6615 Expand_Delta_Array_Aggregate (N, New_List (Decl));
6616 else
6617 Expand_Delta_Record_Aggregate (N, New_List (Decl));
6618 end if;
6619 end Expand_N_Delta_Aggregate;
6621 ----------------------------------
6622 -- Expand_Delta_Array_Aggregate --
6623 ----------------------------------
6625 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is
6626 Loc : constant Source_Ptr := Sloc (N);
6627 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6628 Assoc : Node_Id;
6630 function Generate_Loop (C : Node_Id) return Node_Id;
6631 -- Generate a loop containing individual component assignments for
6632 -- choices that are ranges, subtype indications, subtype names, and
6633 -- iterated component associations.
6635 -------------------
6636 -- Generate_Loop --
6637 -------------------
6639 function Generate_Loop (C : Node_Id) return Node_Id is
6640 Sl : constant Source_Ptr := Sloc (C);
6641 Ix : Entity_Id;
6643 begin
6644 if Nkind (Parent (C)) = N_Iterated_Component_Association then
6645 Ix :=
6646 Make_Defining_Identifier (Loc,
6647 Chars => (Chars (Defining_Identifier (Parent (C)))));
6648 else
6649 Ix := Make_Temporary (Sl, 'I');
6650 end if;
6652 return
6653 Make_Loop_Statement (Loc,
6654 Iteration_Scheme =>
6655 Make_Iteration_Scheme (Sl,
6656 Loop_Parameter_Specification =>
6657 Make_Loop_Parameter_Specification (Sl,
6658 Defining_Identifier => Ix,
6659 Discrete_Subtype_Definition => New_Copy_Tree (C))),
6661 Statements => New_List (
6662 Make_Assignment_Statement (Sl,
6663 Name =>
6664 Make_Indexed_Component (Sl,
6665 Prefix => New_Occurrence_Of (Temp, Sl),
6666 Expressions => New_List (New_Occurrence_Of (Ix, Sl))),
6667 Expression => New_Copy_Tree (Expression (Assoc)))),
6668 End_Label => Empty);
6669 end Generate_Loop;
6671 -- Local variables
6673 Choice : Node_Id;
6675 -- Start of processing for Expand_Delta_Array_Aggregate
6677 begin
6678 Assoc := First (Component_Associations (N));
6679 while Present (Assoc) loop
6680 Choice := First (Choice_List (Assoc));
6681 if Nkind (Assoc) = N_Iterated_Component_Association then
6682 while Present (Choice) loop
6683 Append_To (Deltas, Generate_Loop (Choice));
6684 Next (Choice);
6685 end loop;
6687 else
6688 while Present (Choice) loop
6690 -- Choice can be given by a range, a subtype indication, a
6691 -- subtype name, a scalar value, or an entity.
6693 if Nkind (Choice) = N_Range
6694 or else (Is_Entity_Name (Choice)
6695 and then Is_Type (Entity (Choice)))
6696 then
6697 Append_To (Deltas, Generate_Loop (Choice));
6699 elsif Nkind (Choice) = N_Subtype_Indication then
6700 Append_To (Deltas,
6701 Generate_Loop (Range_Expression (Constraint (Choice))));
6703 else
6704 Append_To (Deltas,
6705 Make_Assignment_Statement (Sloc (Choice),
6706 Name =>
6707 Make_Indexed_Component (Sloc (Choice),
6708 Prefix => New_Occurrence_Of (Temp, Loc),
6709 Expressions => New_List (New_Copy_Tree (Choice))),
6710 Expression => New_Copy_Tree (Expression (Assoc))));
6711 end if;
6713 Next (Choice);
6714 end loop;
6715 end if;
6717 Next (Assoc);
6718 end loop;
6720 Insert_Actions (N, Deltas);
6721 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6722 end Expand_Delta_Array_Aggregate;
6724 -----------------------------------
6725 -- Expand_Delta_Record_Aggregate --
6726 -----------------------------------
6728 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is
6729 Loc : constant Source_Ptr := Sloc (N);
6730 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6731 Assoc : Node_Id;
6732 Choice : Node_Id;
6734 begin
6735 Assoc := First (Component_Associations (N));
6737 while Present (Assoc) loop
6738 Choice := First (Choice_List (Assoc));
6739 while Present (Choice) loop
6740 Append_To (Deltas,
6741 Make_Assignment_Statement (Sloc (Choice),
6742 Name =>
6743 Make_Selected_Component (Sloc (Choice),
6744 Prefix => New_Occurrence_Of (Temp, Loc),
6745 Selector_Name => Make_Identifier (Loc, Chars (Choice))),
6746 Expression => New_Copy_Tree (Expression (Assoc))));
6747 Next (Choice);
6748 end loop;
6750 Next (Assoc);
6751 end loop;
6753 Insert_Actions (N, Deltas);
6754 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6755 end Expand_Delta_Record_Aggregate;
6757 ----------------------------------
6758 -- Expand_N_Extension_Aggregate --
6759 ----------------------------------
6761 -- If the ancestor part is an expression, add a component association for
6762 -- the parent field. If the type of the ancestor part is not the direct
6763 -- parent of the expected type, build recursively the needed ancestors.
6764 -- If the ancestor part is a subtype_mark, replace aggregate with a
6765 -- declaration for a temporary of the expected type, followed by
6766 -- individual assignments to the given components.
6768 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
6769 A : constant Node_Id := Ancestor_Part (N);
6770 Loc : constant Source_Ptr := Sloc (N);
6771 Typ : constant Entity_Id := Etype (N);
6773 begin
6774 -- If the ancestor is a subtype mark, an init proc must be called
6775 -- on the resulting object which thus has to be materialized in
6776 -- the front-end
6778 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
6779 Convert_To_Assignments (N, Typ);
6781 -- The extension aggregate is transformed into a record aggregate
6782 -- of the following form (c1 and c2 are inherited components)
6784 -- (Exp with c3 => a, c4 => b)
6785 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6787 else
6788 Set_Etype (N, Typ);
6790 if Tagged_Type_Expansion then
6791 Expand_Record_Aggregate (N,
6792 Orig_Tag =>
6793 New_Occurrence_Of
6794 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
6795 Parent_Expr => A);
6797 -- No tag is needed in the case of a VM
6799 else
6800 Expand_Record_Aggregate (N, Parent_Expr => A);
6801 end if;
6802 end if;
6804 exception
6805 when RE_Not_Available =>
6806 return;
6807 end Expand_N_Extension_Aggregate;
6809 -----------------------------
6810 -- Expand_Record_Aggregate --
6811 -----------------------------
6813 procedure Expand_Record_Aggregate
6814 (N : Node_Id;
6815 Orig_Tag : Node_Id := Empty;
6816 Parent_Expr : Node_Id := Empty)
6818 Loc : constant Source_Ptr := Sloc (N);
6819 Comps : constant List_Id := Component_Associations (N);
6820 Typ : constant Entity_Id := Etype (N);
6821 Base_Typ : constant Entity_Id := Base_Type (Typ);
6823 Static_Components : Boolean := True;
6824 -- Flag to indicate whether all components are compile-time known,
6825 -- and the aggregate can be constructed statically and handled by
6826 -- the back-end. Set to False by Component_OK_For_Backend.
6828 procedure Build_Back_End_Aggregate;
6829 -- Build a proper aggregate to be handled by the back-end
6831 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
6832 -- Returns true if N is an expression of composite type which can be
6833 -- fully evaluated at compile time without raising constraint error.
6834 -- Such expressions can be passed as is to Gigi without any expansion.
6836 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6837 -- set and constants whose expression is such an aggregate, recursively.
6839 function Component_OK_For_Backend return Boolean;
6840 -- Check for presence of a component which makes it impossible for the
6841 -- backend to process the aggregate, thus requiring the use of a series
6842 -- of assignment statements. Cases checked for are a nested aggregate
6843 -- needing Late_Expansion, the presence of a tagged component which may
6844 -- need tag adjustment, and a bit unaligned component reference.
6846 -- We also force expansion into assignments if a component is of a
6847 -- mutable type (including a private type with discriminants) because
6848 -- in that case the size of the component to be copied may be smaller
6849 -- than the side of the target, and there is no simple way for gigi
6850 -- to compute the size of the object to be copied.
6852 -- NOTE: This is part of the ongoing work to define precisely the
6853 -- interface between front-end and back-end handling of aggregates.
6854 -- In general it is desirable to pass aggregates as they are to gigi,
6855 -- in order to minimize elaboration code. This is one case where the
6856 -- semantics of Ada complicate the analysis and lead to anomalies in
6857 -- the gcc back-end if the aggregate is not expanded into assignments.
6859 -- NOTE: This sets the global Static_Components to False in most, but
6860 -- not all, cases when it returns False.
6862 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
6863 -- Return True if any element of L has Has_Per_Object_Constraint set.
6864 -- L should be the Choices component of an N_Component_Association.
6866 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
6867 -- If any ancestor of the current type is private, the aggregate
6868 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6869 -- because it will not be set when type and its parent are in the
6870 -- same scope, and the parent component needs expansion.
6872 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
6873 -- For nested aggregates return the ultimate enclosing aggregate; for
6874 -- non-nested aggregates return N.
6876 ------------------------------
6877 -- Build_Back_End_Aggregate --
6878 ------------------------------
6880 procedure Build_Back_End_Aggregate is
6881 Comp : Entity_Id;
6882 New_Comp : Node_Id;
6883 Tag_Value : Node_Id;
6885 begin
6886 if Nkind (N) = N_Aggregate then
6888 -- If the aggregate is static and can be handled by the back-end,
6889 -- nothing left to do.
6891 if Static_Components then
6892 Set_Compile_Time_Known_Aggregate (N);
6893 Set_Expansion_Delayed (N, False);
6894 end if;
6895 end if;
6897 -- If no discriminants, nothing special to do
6899 if not Has_Discriminants (Typ) then
6900 null;
6902 -- Case of discriminants present
6904 elsif Is_Derived_Type (Typ) then
6906 -- For untagged types, non-stored discriminants are replaced with
6907 -- stored discriminants, which are the ones that gigi uses to
6908 -- describe the type and its components.
6910 Generate_Aggregate_For_Derived_Type : declare
6911 procedure Prepend_Stored_Values (T : Entity_Id);
6912 -- Scan the list of stored discriminants of the type, and add
6913 -- their values to the aggregate being built.
6915 ---------------------------
6916 -- Prepend_Stored_Values --
6917 ---------------------------
6919 procedure Prepend_Stored_Values (T : Entity_Id) is
6920 Discr : Entity_Id;
6921 First_Comp : Node_Id := Empty;
6923 begin
6924 Discr := First_Stored_Discriminant (T);
6925 while Present (Discr) loop
6926 New_Comp :=
6927 Make_Component_Association (Loc,
6928 Choices => New_List (
6929 New_Occurrence_Of (Discr, Loc)),
6930 Expression =>
6931 New_Copy_Tree
6932 (Get_Discriminant_Value
6933 (Discr,
6934 Typ,
6935 Discriminant_Constraint (Typ))));
6937 if No (First_Comp) then
6938 Prepend_To (Component_Associations (N), New_Comp);
6939 else
6940 Insert_After (First_Comp, New_Comp);
6941 end if;
6943 First_Comp := New_Comp;
6944 Next_Stored_Discriminant (Discr);
6945 end loop;
6946 end Prepend_Stored_Values;
6948 -- Local variables
6950 Constraints : constant List_Id := New_List;
6952 Discr : Entity_Id;
6953 Decl : Node_Id;
6954 Num_Disc : Nat := 0;
6955 Num_Gird : Nat := 0;
6957 -- Start of processing for Generate_Aggregate_For_Derived_Type
6959 begin
6960 -- Remove the associations for the discriminant of derived type
6962 declare
6963 First_Comp : Node_Id;
6965 begin
6966 First_Comp := First (Component_Associations (N));
6967 while Present (First_Comp) loop
6968 Comp := First_Comp;
6969 Next (First_Comp);
6971 if Ekind (Entity (First (Choices (Comp)))) =
6972 E_Discriminant
6973 then
6974 Remove (Comp);
6975 Num_Disc := Num_Disc + 1;
6976 end if;
6977 end loop;
6978 end;
6980 -- Insert stored discriminant associations in the correct
6981 -- order. If there are more stored discriminants than new
6982 -- discriminants, there is at least one new discriminant that
6983 -- constrains more than one of the stored discriminants. In
6984 -- this case we need to construct a proper subtype of the
6985 -- parent type, in order to supply values to all the
6986 -- components. Otherwise there is one-one correspondence
6987 -- between the constraints and the stored discriminants.
6989 Discr := First_Stored_Discriminant (Base_Type (Typ));
6990 while Present (Discr) loop
6991 Num_Gird := Num_Gird + 1;
6992 Next_Stored_Discriminant (Discr);
6993 end loop;
6995 -- Case of more stored discriminants than new discriminants
6997 if Num_Gird > Num_Disc then
6999 -- Create a proper subtype of the parent type, which is the
7000 -- proper implementation type for the aggregate, and convert
7001 -- it to the intended target type.
7003 Discr := First_Stored_Discriminant (Base_Type (Typ));
7004 while Present (Discr) loop
7005 New_Comp :=
7006 New_Copy_Tree
7007 (Get_Discriminant_Value
7008 (Discr,
7009 Typ,
7010 Discriminant_Constraint (Typ)));
7012 Append (New_Comp, Constraints);
7013 Next_Stored_Discriminant (Discr);
7014 end loop;
7016 Decl :=
7017 Make_Subtype_Declaration (Loc,
7018 Defining_Identifier => Make_Temporary (Loc, 'T'),
7019 Subtype_Indication =>
7020 Make_Subtype_Indication (Loc,
7021 Subtype_Mark =>
7022 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
7023 Constraint =>
7024 Make_Index_Or_Discriminant_Constraint
7025 (Loc, Constraints)));
7027 Insert_Action (N, Decl);
7028 Prepend_Stored_Values (Base_Type (Typ));
7030 Set_Etype (N, Defining_Identifier (Decl));
7031 Set_Analyzed (N);
7033 Rewrite (N, Unchecked_Convert_To (Typ, N));
7034 Analyze (N);
7036 -- Case where we do not have fewer new discriminants than
7037 -- stored discriminants, so in this case we can simply use the
7038 -- stored discriminants of the subtype.
7040 else
7041 Prepend_Stored_Values (Typ);
7042 end if;
7043 end Generate_Aggregate_For_Derived_Type;
7044 end if;
7046 if Is_Tagged_Type (Typ) then
7048 -- In the tagged case, _parent and _tag component must be created
7050 -- Reset Null_Present unconditionally. Tagged records always have
7051 -- at least one field (the tag or the parent).
7053 Set_Null_Record_Present (N, False);
7055 -- When the current aggregate comes from the expansion of an
7056 -- extension aggregate, the parent expr is replaced by an
7057 -- aggregate formed by selected components of this expr.
7059 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
7060 Comp := First_Component_Or_Discriminant (Typ);
7061 while Present (Comp) loop
7063 -- Skip all expander-generated components
7065 if not Comes_From_Source (Original_Record_Component (Comp))
7066 then
7067 null;
7069 else
7070 New_Comp :=
7071 Make_Selected_Component (Loc,
7072 Prefix =>
7073 Unchecked_Convert_To (Typ,
7074 Duplicate_Subexpr (Parent_Expr, True)),
7075 Selector_Name => New_Occurrence_Of (Comp, Loc));
7077 Append_To (Comps,
7078 Make_Component_Association (Loc,
7079 Choices => New_List (
7080 New_Occurrence_Of (Comp, Loc)),
7081 Expression => New_Comp));
7083 Analyze_And_Resolve (New_Comp, Etype (Comp));
7084 end if;
7086 Next_Component_Or_Discriminant (Comp);
7087 end loop;
7088 end if;
7090 -- Compute the value for the Tag now, if the type is a root it
7091 -- will be included in the aggregate right away, otherwise it will
7092 -- be propagated to the parent aggregate.
7094 if Present (Orig_Tag) then
7095 Tag_Value := Orig_Tag;
7097 elsif not Tagged_Type_Expansion then
7098 Tag_Value := Empty;
7100 else
7101 Tag_Value :=
7102 New_Occurrence_Of
7103 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
7104 end if;
7106 -- For a derived type, an aggregate for the parent is formed with
7107 -- all the inherited components.
7109 if Is_Derived_Type (Typ) then
7110 declare
7111 First_Comp : Node_Id;
7112 Parent_Comps : List_Id;
7113 Parent_Aggr : Node_Id;
7114 Parent_Name : Node_Id;
7116 begin
7117 -- Remove the inherited component association from the
7118 -- aggregate and store them in the parent aggregate
7120 First_Comp := First (Component_Associations (N));
7121 Parent_Comps := New_List;
7122 while Present (First_Comp)
7123 and then
7124 Scope (Original_Record_Component
7125 (Entity (First (Choices (First_Comp))))) /=
7126 Base_Typ
7127 loop
7128 Comp := First_Comp;
7129 Next (First_Comp);
7130 Remove (Comp);
7131 Append (Comp, Parent_Comps);
7132 end loop;
7134 Parent_Aggr :=
7135 Make_Aggregate (Loc,
7136 Component_Associations => Parent_Comps);
7137 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
7139 -- Find the _parent component
7141 Comp := First_Component (Typ);
7142 while Chars (Comp) /= Name_uParent loop
7143 Comp := Next_Component (Comp);
7144 end loop;
7146 Parent_Name := New_Occurrence_Of (Comp, Loc);
7148 -- Insert the parent aggregate
7150 Prepend_To (Component_Associations (N),
7151 Make_Component_Association (Loc,
7152 Choices => New_List (Parent_Name),
7153 Expression => Parent_Aggr));
7155 -- Expand recursively the parent propagating the right Tag
7157 Expand_Record_Aggregate
7158 (Parent_Aggr, Tag_Value, Parent_Expr);
7160 -- The ancestor part may be a nested aggregate that has
7161 -- delayed expansion: recheck now.
7163 if not Component_OK_For_Backend then
7164 Convert_To_Assignments (N, Typ);
7165 end if;
7166 end;
7168 -- For a root type, the tag component is added (unless compiling
7169 -- for the VMs, where tags are implicit).
7171 elsif Tagged_Type_Expansion then
7172 declare
7173 Tag_Name : constant Node_Id :=
7174 New_Occurrence_Of
7175 (First_Tag_Component (Typ), Loc);
7176 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7177 Conv_Node : constant Node_Id :=
7178 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7180 begin
7181 Set_Etype (Conv_Node, Typ_Tag);
7182 Prepend_To (Component_Associations (N),
7183 Make_Component_Association (Loc,
7184 Choices => New_List (Tag_Name),
7185 Expression => Conv_Node));
7186 end;
7187 end if;
7188 end if;
7189 end Build_Back_End_Aggregate;
7191 ----------------------------------------
7192 -- Compile_Time_Known_Composite_Value --
7193 ----------------------------------------
7195 function Compile_Time_Known_Composite_Value
7196 (N : Node_Id) return Boolean
7198 begin
7199 -- If we have an entity name, then see if it is the name of a
7200 -- constant and if so, test the corresponding constant value.
7202 if Is_Entity_Name (N) then
7203 declare
7204 E : constant Entity_Id := Entity (N);
7205 V : Node_Id;
7206 begin
7207 if Ekind (E) /= E_Constant then
7208 return False;
7209 else
7210 V := Constant_Value (E);
7211 return Present (V)
7212 and then Compile_Time_Known_Composite_Value (V);
7213 end if;
7214 end;
7216 -- We have a value, see if it is compile time known
7218 else
7219 if Nkind (N) = N_Aggregate then
7220 return Compile_Time_Known_Aggregate (N);
7221 end if;
7223 -- All other types of values are not known at compile time
7225 return False;
7226 end if;
7228 end Compile_Time_Known_Composite_Value;
7230 ------------------------------
7231 -- Component_OK_For_Backend --
7232 ------------------------------
7234 function Component_OK_For_Backend return Boolean is
7235 C : Node_Id;
7236 Expr_Q : Node_Id;
7238 begin
7239 if No (Comps) then
7240 return True;
7241 end if;
7243 C := First (Comps);
7244 while Present (C) loop
7246 -- If the component has box initialization, expansion is needed
7247 -- and component is not ready for backend.
7249 if Box_Present (C) then
7250 return False;
7251 end if;
7253 if Nkind (Expression (C)) = N_Qualified_Expression then
7254 Expr_Q := Expression (Expression (C));
7255 else
7256 Expr_Q := Expression (C);
7257 end if;
7259 -- Return False for array components whose bounds raise
7260 -- constraint error.
7262 declare
7263 Comp : constant Entity_Id := First (Choices (C));
7264 Indx : Node_Id;
7266 begin
7267 if Present (Etype (Comp))
7268 and then Is_Array_Type (Etype (Comp))
7269 then
7270 Indx := First_Index (Etype (Comp));
7271 while Present (Indx) loop
7272 if Nkind (Type_Low_Bound (Etype (Indx))) =
7273 N_Raise_Constraint_Error
7274 or else Nkind (Type_High_Bound (Etype (Indx))) =
7275 N_Raise_Constraint_Error
7276 then
7277 return False;
7278 end if;
7280 Indx := Next_Index (Indx);
7281 end loop;
7282 end if;
7283 end;
7285 -- Return False if the aggregate has any associations for tagged
7286 -- components that may require tag adjustment.
7288 -- These are cases where the source expression may have a tag that
7289 -- could differ from the component tag (e.g., can occur for type
7290 -- conversions and formal parameters). (Tag adjustment not needed
7291 -- if Tagged_Type_Expansion because object tags are implicit in
7292 -- the machine.)
7294 if Is_Tagged_Type (Etype (Expr_Q))
7295 and then
7296 (Nkind (Expr_Q) = N_Type_Conversion
7297 or else
7298 (Is_Entity_Name (Expr_Q)
7299 and then Is_Formal (Entity (Expr_Q))))
7300 and then Tagged_Type_Expansion
7301 then
7302 Static_Components := False;
7303 return False;
7305 elsif Is_Delayed_Aggregate (Expr_Q) then
7306 Static_Components := False;
7307 return False;
7309 elsif Nkind (Expr_Q) = N_Quantified_Expression then
7310 Static_Components := False;
7311 return False;
7313 elsif Possible_Bit_Aligned_Component (Expr_Q) then
7314 Static_Components := False;
7315 return False;
7317 elsif Modify_Tree_For_C
7318 and then Nkind (C) = N_Component_Association
7319 and then Has_Per_Object_Constraint (Choices (C))
7320 then
7321 Static_Components := False;
7322 return False;
7324 elsif Modify_Tree_For_C
7325 and then Nkind (Expr_Q) = N_Identifier
7326 and then Is_Array_Type (Etype (Expr_Q))
7327 then
7328 Static_Components := False;
7329 return False;
7331 elsif Modify_Tree_For_C
7332 and then Nkind (Expr_Q) = N_Type_Conversion
7333 and then Is_Array_Type (Etype (Expr_Q))
7334 then
7335 Static_Components := False;
7336 return False;
7337 end if;
7339 if Is_Elementary_Type (Etype (Expr_Q)) then
7340 if not Compile_Time_Known_Value (Expr_Q) then
7341 Static_Components := False;
7342 end if;
7344 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
7345 Static_Components := False;
7347 if Is_Private_Type (Etype (Expr_Q))
7348 and then Has_Discriminants (Etype (Expr_Q))
7349 then
7350 return False;
7351 end if;
7352 end if;
7354 Next (C);
7355 end loop;
7357 return True;
7358 end Component_OK_For_Backend;
7360 -------------------------------
7361 -- Has_Per_Object_Constraint --
7362 -------------------------------
7364 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
7365 N : Node_Id := First (L);
7366 begin
7367 while Present (N) loop
7368 if Is_Entity_Name (N)
7369 and then Present (Entity (N))
7370 and then Has_Per_Object_Constraint (Entity (N))
7371 then
7372 return True;
7373 end if;
7375 Next (N);
7376 end loop;
7378 return False;
7379 end Has_Per_Object_Constraint;
7381 -----------------------------------
7382 -- Has_Visible_Private_Ancestor --
7383 -----------------------------------
7385 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
7386 R : constant Entity_Id := Root_Type (Id);
7387 T1 : Entity_Id := Id;
7389 begin
7390 loop
7391 if Is_Private_Type (T1) then
7392 return True;
7394 elsif T1 = R then
7395 return False;
7397 else
7398 T1 := Etype (T1);
7399 end if;
7400 end loop;
7401 end Has_Visible_Private_Ancestor;
7403 -------------------------
7404 -- Top_Level_Aggregate --
7405 -------------------------
7407 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
7408 Aggr : Node_Id;
7410 begin
7411 Aggr := N;
7412 while Present (Parent (Aggr))
7413 and then Nkind_In (Parent (Aggr), N_Aggregate,
7414 N_Component_Association)
7415 loop
7416 Aggr := Parent (Aggr);
7417 end loop;
7419 return Aggr;
7420 end Top_Level_Aggregate;
7422 -- Local variables
7424 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
7426 -- Start of processing for Expand_Record_Aggregate
7428 begin
7429 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7430 -- to prevent a piecemeal assignment even if the aggregate is to be
7431 -- expanded. We create a temporary for the aggregate, and assign the
7432 -- temporary instead, so that the back end can generate an atomic move
7433 -- for it.
7435 if Is_Atomic_VFA_Aggregate (N) then
7436 return;
7438 -- No special management required for aggregates used to initialize
7439 -- statically allocated dispatch tables
7441 elsif Is_Static_Dispatch_Table_Aggregate (N) then
7442 return;
7443 end if;
7445 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7446 -- are build-in-place function calls. The assignments will each turn
7447 -- into a build-in-place function call. If components are all static,
7448 -- we can pass the aggregate to the back end regardless of limitedness.
7450 -- Extension aggregates, aggregates in extended return statements, and
7451 -- aggregates for C++ imported types must be expanded.
7453 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
7454 if not Nkind_In (Parent (N), N_Component_Association,
7455 N_Object_Declaration)
7456 then
7457 Convert_To_Assignments (N, Typ);
7459 elsif Nkind (N) = N_Extension_Aggregate
7460 or else Convention (Typ) = Convention_CPP
7461 then
7462 Convert_To_Assignments (N, Typ);
7464 elsif not Size_Known_At_Compile_Time (Typ)
7465 or else not Component_OK_For_Backend
7466 or else not Static_Components
7467 then
7468 Convert_To_Assignments (N, Typ);
7470 -- In all other cases, build a proper aggregate to be handled by
7471 -- the back-end
7473 else
7474 Build_Back_End_Aggregate;
7475 end if;
7477 -- Gigi doesn't properly handle temporaries of variable size so we
7478 -- generate it in the front-end
7480 elsif not Size_Known_At_Compile_Time (Typ)
7481 and then Tagged_Type_Expansion
7482 then
7483 Convert_To_Assignments (N, Typ);
7485 -- An aggregate used to initialize a controlled object must be turned
7486 -- into component assignments as the components themselves may require
7487 -- finalization actions such as adjustment.
7489 elsif Needs_Finalization (Typ) then
7490 Convert_To_Assignments (N, Typ);
7492 -- Ada 2005 (AI-287): In case of default initialized components we
7493 -- convert the aggregate into assignments.
7495 elsif Has_Default_Init_Comps (N) then
7496 Convert_To_Assignments (N, Typ);
7498 -- Check components
7500 elsif not Component_OK_For_Backend then
7501 Convert_To_Assignments (N, Typ);
7503 -- If an ancestor is private, some components are not inherited and we
7504 -- cannot expand into a record aggregate.
7506 elsif Has_Visible_Private_Ancestor (Typ) then
7507 Convert_To_Assignments (N, Typ);
7509 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7510 -- is not able to handle the aggregate for Late_Request.
7512 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
7513 Convert_To_Assignments (N, Typ);
7515 -- If the tagged types covers interface types we need to initialize all
7516 -- hidden components containing pointers to secondary dispatch tables.
7518 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
7519 Convert_To_Assignments (N, Typ);
7521 -- If some components are mutable, the size of the aggregate component
7522 -- may be distinct from the default size of the type component, so
7523 -- we need to expand to insure that the back-end copies the proper
7524 -- size of the data. However, if the aggregate is the initial value of
7525 -- a constant, the target is immutable and might be built statically
7526 -- if components are appropriate.
7528 elsif Has_Mutable_Components (Typ)
7529 and then
7530 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
7531 or else not Constant_Present (Parent (Top_Level_Aggr))
7532 or else not Static_Components)
7533 then
7534 Convert_To_Assignments (N, Typ);
7536 -- If the type involved has bit aligned components, then we are not sure
7537 -- that the back end can handle this case correctly.
7539 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
7540 Convert_To_Assignments (N, Typ);
7542 -- When generating C, only generate an aggregate when declaring objects
7543 -- since C does not support aggregates in e.g. assignment statements.
7545 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
7546 Convert_To_Assignments (N, Typ);
7548 -- In all other cases, build a proper aggregate to be handled by gigi
7550 else
7551 Build_Back_End_Aggregate;
7552 end if;
7553 end Expand_Record_Aggregate;
7555 ----------------------------
7556 -- Has_Default_Init_Comps --
7557 ----------------------------
7559 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
7560 Comps : constant List_Id := Component_Associations (N);
7561 C : Node_Id;
7562 Expr : Node_Id;
7564 begin
7565 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
7567 if No (Comps) then
7568 return False;
7569 end if;
7571 if Has_Self_Reference (N) then
7572 return True;
7573 end if;
7575 -- Check if any direct component has default initialized components
7577 C := First (Comps);
7578 while Present (C) loop
7579 if Box_Present (C) then
7580 return True;
7581 end if;
7583 Next (C);
7584 end loop;
7586 -- Recursive call in case of aggregate expression
7588 C := First (Comps);
7589 while Present (C) loop
7590 Expr := Expression (C);
7592 if Present (Expr)
7593 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
7594 and then Has_Default_Init_Comps (Expr)
7595 then
7596 return True;
7597 end if;
7599 Next (C);
7600 end loop;
7602 return False;
7603 end Has_Default_Init_Comps;
7605 ----------------------------------------
7606 -- Is_Build_In_Place_Aggregate_Return --
7607 ----------------------------------------
7609 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean is
7610 P : Node_Id := Parent (N);
7612 begin
7613 while Nkind (P) = N_Qualified_Expression loop
7614 P := Parent (P);
7615 end loop;
7617 if Nkind (P) = N_Simple_Return_Statement then
7618 null;
7620 elsif Nkind (Parent (P)) = N_Extended_Return_Statement then
7621 P := Parent (P);
7623 else
7624 return False;
7625 end if;
7627 return
7628 Is_Build_In_Place_Function
7629 (Return_Applies_To (Return_Statement_Entity (P)));
7630 end Is_Build_In_Place_Aggregate_Return;
7632 --------------------------
7633 -- Is_Delayed_Aggregate --
7634 --------------------------
7636 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
7637 Node : Node_Id := N;
7638 Kind : Node_Kind := Nkind (Node);
7640 begin
7641 if Kind = N_Qualified_Expression then
7642 Node := Expression (Node);
7643 Kind := Nkind (Node);
7644 end if;
7646 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
7647 return False;
7648 else
7649 return Expansion_Delayed (Node);
7650 end if;
7651 end Is_Delayed_Aggregate;
7653 ---------------------------
7654 -- In_Object_Declaration --
7655 ---------------------------
7657 function In_Object_Declaration (N : Node_Id) return Boolean is
7658 P : Node_Id := Parent (N);
7659 begin
7660 while Present (P) loop
7661 if Nkind (P) = N_Object_Declaration then
7662 return True;
7663 end if;
7665 P := Parent (P);
7666 end loop;
7668 return False;
7669 end In_Object_Declaration;
7671 ----------------------------------------
7672 -- Is_Static_Dispatch_Table_Aggregate --
7673 ----------------------------------------
7675 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
7676 Typ : constant Entity_Id := Base_Type (Etype (N));
7678 begin
7679 return Building_Static_Dispatch_Tables
7680 and then Tagged_Type_Expansion
7681 and then RTU_Loaded (Ada_Tags)
7683 -- Avoid circularity when rebuilding the compiler
7685 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
7686 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
7687 or else
7688 Typ = RTE (RE_Address_Array)
7689 or else
7690 Typ = RTE (RE_Type_Specific_Data)
7691 or else
7692 Typ = RTE (RE_Tag_Table)
7693 or else
7694 (RTE_Available (RE_Interface_Data)
7695 and then Typ = RTE (RE_Interface_Data))
7696 or else
7697 (RTE_Available (RE_Interfaces_Array)
7698 and then Typ = RTE (RE_Interfaces_Array))
7699 or else
7700 (RTE_Available (RE_Interface_Data_Element)
7701 and then Typ = RTE (RE_Interface_Data_Element)));
7702 end Is_Static_Dispatch_Table_Aggregate;
7704 -----------------------------
7705 -- Is_Two_Dim_Packed_Array --
7706 -----------------------------
7708 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
7709 C : constant Int := UI_To_Int (Component_Size (Typ));
7710 begin
7711 return Number_Dimensions (Typ) = 2
7712 and then Is_Bit_Packed_Array (Typ)
7713 and then (C = 1 or else C = 2 or else C = 4);
7714 end Is_Two_Dim_Packed_Array;
7716 --------------------
7717 -- Late_Expansion --
7718 --------------------
7720 function Late_Expansion
7721 (N : Node_Id;
7722 Typ : Entity_Id;
7723 Target : Node_Id) return List_Id
7725 Aggr_Code : List_Id;
7727 begin
7728 if Is_Array_Type (Etype (N)) then
7729 Aggr_Code :=
7730 Build_Array_Aggr_Code
7731 (N => N,
7732 Ctype => Component_Type (Etype (N)),
7733 Index => First_Index (Typ),
7734 Into => Target,
7735 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
7736 Indexes => No_List);
7738 -- Directly or indirectly (e.g. access protected procedure) a record
7740 else
7741 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
7742 end if;
7744 -- Save the last assignment statement associated with the aggregate
7745 -- when building a controlled object. This reference is utilized by
7746 -- the finalization machinery when marking an object as successfully
7747 -- initialized.
7749 if Needs_Finalization (Typ)
7750 and then Is_Entity_Name (Target)
7751 and then Present (Entity (Target))
7752 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
7753 then
7754 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
7755 end if;
7757 return Aggr_Code;
7758 end Late_Expansion;
7760 ----------------------------------
7761 -- Make_OK_Assignment_Statement --
7762 ----------------------------------
7764 function Make_OK_Assignment_Statement
7765 (Sloc : Source_Ptr;
7766 Name : Node_Id;
7767 Expression : Node_Id) return Node_Id
7769 begin
7770 Set_Assignment_OK (Name);
7771 return Make_Assignment_Statement (Sloc, Name, Expression);
7772 end Make_OK_Assignment_Statement;
7774 -----------------------
7775 -- Number_Of_Choices --
7776 -----------------------
7778 function Number_Of_Choices (N : Node_Id) return Nat is
7779 Assoc : Node_Id;
7780 Choice : Node_Id;
7782 Nb_Choices : Nat := 0;
7784 begin
7785 if Present (Expressions (N)) then
7786 return 0;
7787 end if;
7789 Assoc := First (Component_Associations (N));
7790 while Present (Assoc) loop
7791 Choice := First (Choice_List (Assoc));
7792 while Present (Choice) loop
7793 if Nkind (Choice) /= N_Others_Choice then
7794 Nb_Choices := Nb_Choices + 1;
7795 end if;
7797 Next (Choice);
7798 end loop;
7800 Next (Assoc);
7801 end loop;
7803 return Nb_Choices;
7804 end Number_Of_Choices;
7806 ------------------------------------
7807 -- Packed_Array_Aggregate_Handled --
7808 ------------------------------------
7810 -- The current version of this procedure will handle at compile time
7811 -- any array aggregate that meets these conditions:
7813 -- One and two dimensional, bit packed
7814 -- Underlying packed type is modular type
7815 -- Bounds are within 32-bit Int range
7816 -- All bounds and values are static
7818 -- Note: for now, in the 2-D case, we only handle component sizes of
7819 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7821 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
7822 Loc : constant Source_Ptr := Sloc (N);
7823 Typ : constant Entity_Id := Etype (N);
7824 Ctyp : constant Entity_Id := Component_Type (Typ);
7826 Not_Handled : exception;
7827 -- Exception raised if this aggregate cannot be handled
7829 begin
7830 -- Handle one- or two dimensional bit packed array
7832 if not Is_Bit_Packed_Array (Typ)
7833 or else Number_Dimensions (Typ) > 2
7834 then
7835 return False;
7836 end if;
7838 -- If two-dimensional, check whether it can be folded, and transformed
7839 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7840 -- the original type.
7842 if Number_Dimensions (Typ) = 2 then
7843 return Two_Dim_Packed_Array_Handled (N);
7844 end if;
7846 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
7847 return False;
7848 end if;
7850 if not Is_Scalar_Type (Component_Type (Typ))
7851 and then Has_Non_Standard_Rep (Component_Type (Typ))
7852 then
7853 return False;
7854 end if;
7856 declare
7857 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
7859 Lo : Node_Id;
7860 Hi : Node_Id;
7861 -- Bounds of index type
7863 Lob : Uint;
7864 Hib : Uint;
7865 -- Values of bounds if compile time known
7867 function Get_Component_Val (N : Node_Id) return Uint;
7868 -- Given a expression value N of the component type Ctyp, returns a
7869 -- value of Csiz (component size) bits representing this value. If
7870 -- the value is nonstatic or any other reason exists why the value
7871 -- cannot be returned, then Not_Handled is raised.
7873 -----------------------
7874 -- Get_Component_Val --
7875 -----------------------
7877 function Get_Component_Val (N : Node_Id) return Uint is
7878 Val : Uint;
7880 begin
7881 -- We have to analyze the expression here before doing any further
7882 -- processing here. The analysis of such expressions is deferred
7883 -- till expansion to prevent some problems of premature analysis.
7885 Analyze_And_Resolve (N, Ctyp);
7887 -- Must have a compile time value. String literals have to be
7888 -- converted into temporaries as well, because they cannot easily
7889 -- be converted into their bit representation.
7891 if not Compile_Time_Known_Value (N)
7892 or else Nkind (N) = N_String_Literal
7893 then
7894 raise Not_Handled;
7895 end if;
7897 Val := Expr_Rep_Value (N);
7899 -- Adjust for bias, and strip proper number of bits
7901 if Has_Biased_Representation (Ctyp) then
7902 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
7903 end if;
7905 return Val mod Uint_2 ** Csiz;
7906 end Get_Component_Val;
7908 -- Here we know we have a one dimensional bit packed array
7910 begin
7911 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
7913 -- Cannot do anything if bounds are dynamic
7915 if not Compile_Time_Known_Value (Lo)
7916 or else
7917 not Compile_Time_Known_Value (Hi)
7918 then
7919 return False;
7920 end if;
7922 -- Or are silly out of range of int bounds
7924 Lob := Expr_Value (Lo);
7925 Hib := Expr_Value (Hi);
7927 if not UI_Is_In_Int_Range (Lob)
7928 or else
7929 not UI_Is_In_Int_Range (Hib)
7930 then
7931 return False;
7932 end if;
7934 -- At this stage we have a suitable aggregate for handling at compile
7935 -- time. The only remaining checks are that the values of expressions
7936 -- in the aggregate are compile-time known (checks are performed by
7937 -- Get_Component_Val), and that any subtypes or ranges are statically
7938 -- known.
7940 -- If the aggregate is not fully positional at this stage, then
7941 -- convert it to positional form. Either this will fail, in which
7942 -- case we can do nothing, or it will succeed, in which case we have
7943 -- succeeded in handling the aggregate and transforming it into a
7944 -- modular value, or it will stay an aggregate, in which case we
7945 -- have failed to create a packed value for it.
7947 if Present (Component_Associations (N)) then
7948 Convert_To_Positional
7949 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7950 return Nkind (N) /= N_Aggregate;
7951 end if;
7953 -- Otherwise we are all positional, so convert to proper value
7955 declare
7956 Lov : constant Int := UI_To_Int (Lob);
7957 Hiv : constant Int := UI_To_Int (Hib);
7959 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
7960 -- The length of the array (number of elements)
7962 Aggregate_Val : Uint;
7963 -- Value of aggregate. The value is set in the low order bits of
7964 -- this value. For the little-endian case, the values are stored
7965 -- from low-order to high-order and for the big-endian case the
7966 -- values are stored from high-order to low-order. Note that gigi
7967 -- will take care of the conversions to left justify the value in
7968 -- the big endian case (because of left justified modular type
7969 -- processing), so we do not have to worry about that here.
7971 Lit : Node_Id;
7972 -- Integer literal for resulting constructed value
7974 Shift : Nat;
7975 -- Shift count from low order for next value
7977 Incr : Int;
7978 -- Shift increment for loop
7980 Expr : Node_Id;
7981 -- Next expression from positional parameters of aggregate
7983 Left_Justified : Boolean;
7984 -- Set True if we are filling the high order bits of the target
7985 -- value (i.e. the value is left justified).
7987 begin
7988 -- For little endian, we fill up the low order bits of the target
7989 -- value. For big endian we fill up the high order bits of the
7990 -- target value (which is a left justified modular value).
7992 Left_Justified := Bytes_Big_Endian;
7994 -- Switch justification if using -gnatd8
7996 if Debug_Flag_8 then
7997 Left_Justified := not Left_Justified;
7998 end if;
8000 -- Switch justfification if reverse storage order
8002 if Reverse_Storage_Order (Base_Type (Typ)) then
8003 Left_Justified := not Left_Justified;
8004 end if;
8006 if Left_Justified then
8007 Shift := Csiz * (Len - 1);
8008 Incr := -Csiz;
8009 else
8010 Shift := 0;
8011 Incr := +Csiz;
8012 end if;
8014 -- Loop to set the values
8016 if Len = 0 then
8017 Aggregate_Val := Uint_0;
8018 else
8019 Expr := First (Expressions (N));
8020 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
8022 for J in 2 .. Len loop
8023 Shift := Shift + Incr;
8024 Next (Expr);
8025 Aggregate_Val :=
8026 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
8027 end loop;
8028 end if;
8030 -- Now we can rewrite with the proper value
8032 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
8033 Set_Print_In_Hex (Lit);
8035 -- Construct the expression using this literal. Note that it is
8036 -- important to qualify the literal with its proper modular type
8037 -- since universal integer does not have the required range and
8038 -- also this is a left justified modular type, which is important
8039 -- in the big-endian case.
8041 Rewrite (N,
8042 Unchecked_Convert_To (Typ,
8043 Make_Qualified_Expression (Loc,
8044 Subtype_Mark =>
8045 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
8046 Expression => Lit)));
8048 Analyze_And_Resolve (N, Typ);
8049 return True;
8050 end;
8051 end;
8053 exception
8054 when Not_Handled =>
8055 return False;
8056 end Packed_Array_Aggregate_Handled;
8058 ----------------------------
8059 -- Has_Mutable_Components --
8060 ----------------------------
8062 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
8063 Comp : Entity_Id;
8065 begin
8066 Comp := First_Component (Typ);
8067 while Present (Comp) loop
8068 if Is_Record_Type (Etype (Comp))
8069 and then Has_Discriminants (Etype (Comp))
8070 and then not Is_Constrained (Etype (Comp))
8071 then
8072 return True;
8073 end if;
8075 Next_Component (Comp);
8076 end loop;
8078 return False;
8079 end Has_Mutable_Components;
8081 ------------------------------
8082 -- Initialize_Discriminants --
8083 ------------------------------
8085 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
8086 Loc : constant Source_Ptr := Sloc (N);
8087 Bas : constant Entity_Id := Base_Type (Typ);
8088 Par : constant Entity_Id := Etype (Bas);
8089 Decl : constant Node_Id := Parent (Par);
8090 Ref : Node_Id;
8092 begin
8093 if Is_Tagged_Type (Bas)
8094 and then Is_Derived_Type (Bas)
8095 and then Has_Discriminants (Par)
8096 and then Has_Discriminants (Bas)
8097 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
8098 and then Nkind (Decl) = N_Full_Type_Declaration
8099 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
8100 and then
8101 Present (Variant_Part (Component_List (Type_Definition (Decl))))
8102 and then Nkind (N) /= N_Extension_Aggregate
8103 then
8105 -- Call init proc to set discriminants.
8106 -- There should eventually be a special procedure for this ???
8108 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
8109 Insert_Actions_After (N,
8110 Build_Initialization_Call (Sloc (N), Ref, Typ));
8111 end if;
8112 end Initialize_Discriminants;
8114 ----------------
8115 -- Must_Slide --
8116 ----------------
8118 function Must_Slide
8119 (Obj_Type : Entity_Id;
8120 Typ : Entity_Id) return Boolean
8122 L1, L2, H1, H2 : Node_Id;
8124 begin
8125 -- No sliding if the type of the object is not established yet, if it is
8126 -- an unconstrained type whose actual subtype comes from the aggregate,
8127 -- or if the two types are identical.
8129 if not Is_Array_Type (Obj_Type) then
8130 return False;
8132 elsif not Is_Constrained (Obj_Type) then
8133 return False;
8135 elsif Typ = Obj_Type then
8136 return False;
8138 else
8139 -- Sliding can only occur along the first dimension
8141 Get_Index_Bounds (First_Index (Typ), L1, H1);
8142 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
8144 if not Is_OK_Static_Expression (L1) or else
8145 not Is_OK_Static_Expression (L2) or else
8146 not Is_OK_Static_Expression (H1) or else
8147 not Is_OK_Static_Expression (H2)
8148 then
8149 return False;
8150 else
8151 return Expr_Value (L1) /= Expr_Value (L2)
8152 or else
8153 Expr_Value (H1) /= Expr_Value (H2);
8154 end if;
8155 end if;
8156 end Must_Slide;
8158 ---------------------------------
8159 -- Process_Transient_Component --
8160 ---------------------------------
8162 procedure Process_Transient_Component
8163 (Loc : Source_Ptr;
8164 Comp_Typ : Entity_Id;
8165 Init_Expr : Node_Id;
8166 Fin_Call : out Node_Id;
8167 Hook_Clear : out Node_Id;
8168 Aggr : Node_Id := Empty;
8169 Stmts : List_Id := No_List)
8171 procedure Add_Item (Item : Node_Id);
8172 -- Insert arbitrary node Item into the tree depending on the values of
8173 -- Aggr and Stmts.
8175 --------------
8176 -- Add_Item --
8177 --------------
8179 procedure Add_Item (Item : Node_Id) is
8180 begin
8181 if Present (Aggr) then
8182 Insert_Action (Aggr, Item);
8183 else
8184 pragma Assert (Present (Stmts));
8185 Append_To (Stmts, Item);
8186 end if;
8187 end Add_Item;
8189 -- Local variables
8191 Hook_Assign : Node_Id;
8192 Hook_Decl : Node_Id;
8193 Ptr_Decl : Node_Id;
8194 Res_Decl : Node_Id;
8195 Res_Id : Entity_Id;
8196 Res_Typ : Entity_Id;
8198 -- Start of processing for Process_Transient_Component
8200 begin
8201 -- Add the access type, which provides a reference to the function
8202 -- result. Generate:
8204 -- type Res_Typ is access all Comp_Typ;
8206 Res_Typ := Make_Temporary (Loc, 'A');
8207 Set_Ekind (Res_Typ, E_General_Access_Type);
8208 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
8210 Add_Item
8211 (Make_Full_Type_Declaration (Loc,
8212 Defining_Identifier => Res_Typ,
8213 Type_Definition =>
8214 Make_Access_To_Object_Definition (Loc,
8215 All_Present => True,
8216 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
8218 -- Add the temporary which captures the result of the function call.
8219 -- Generate:
8221 -- Res : constant Res_Typ := Init_Expr'Reference;
8223 -- Note that this temporary is effectively a transient object because
8224 -- its lifetime is bounded by the current array or record component.
8226 Res_Id := Make_Temporary (Loc, 'R');
8227 Set_Ekind (Res_Id, E_Constant);
8228 Set_Etype (Res_Id, Res_Typ);
8230 -- Mark the transient object as successfully processed to avoid double
8231 -- finalization.
8233 Set_Is_Finalized_Transient (Res_Id);
8235 -- Signal the general finalization machinery that this transient object
8236 -- should not be considered for finalization actions because its cleanup
8237 -- will be performed by Process_Transient_Component_Completion.
8239 Set_Is_Ignored_Transient (Res_Id);
8241 Res_Decl :=
8242 Make_Object_Declaration (Loc,
8243 Defining_Identifier => Res_Id,
8244 Constant_Present => True,
8245 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
8246 Expression =>
8247 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
8249 Add_Item (Res_Decl);
8251 -- Construct all pieces necessary to hook and finalize the transient
8252 -- result.
8254 Build_Transient_Object_Statements
8255 (Obj_Decl => Res_Decl,
8256 Fin_Call => Fin_Call,
8257 Hook_Assign => Hook_Assign,
8258 Hook_Clear => Hook_Clear,
8259 Hook_Decl => Hook_Decl,
8260 Ptr_Decl => Ptr_Decl);
8262 -- Add the access type which provides a reference to the transient
8263 -- result. Generate:
8265 -- type Ptr_Typ is access all Comp_Typ;
8267 Add_Item (Ptr_Decl);
8269 -- Add the temporary which acts as a hook to the transient result.
8270 -- Generate:
8272 -- Hook : Ptr_Typ := null;
8274 Add_Item (Hook_Decl);
8276 -- Attach the transient result to the hook. Generate:
8278 -- Hook := Ptr_Typ (Res);
8280 Add_Item (Hook_Assign);
8282 -- The original initialization expression now references the value of
8283 -- the temporary function result. Generate:
8285 -- Res.all
8287 Rewrite (Init_Expr,
8288 Make_Explicit_Dereference (Loc,
8289 Prefix => New_Occurrence_Of (Res_Id, Loc)));
8290 end Process_Transient_Component;
8292 --------------------------------------------
8293 -- Process_Transient_Component_Completion --
8294 --------------------------------------------
8296 procedure Process_Transient_Component_Completion
8297 (Loc : Source_Ptr;
8298 Aggr : Node_Id;
8299 Fin_Call : Node_Id;
8300 Hook_Clear : Node_Id;
8301 Stmts : List_Id)
8303 Exceptions_OK : constant Boolean :=
8304 not Restriction_Active (No_Exception_Propagation);
8306 begin
8307 pragma Assert (Present (Hook_Clear));
8309 -- Generate the following code if exception propagation is allowed:
8311 -- declare
8312 -- Abort : constant Boolean := Triggered_By_Abort;
8313 -- <or>
8314 -- Abort : constant Boolean := False; -- no abort
8316 -- E : Exception_Occurrence;
8317 -- Raised : Boolean := False;
8319 -- begin
8320 -- [Abort_Defer;]
8322 -- begin
8323 -- Hook := null;
8324 -- [Deep_]Finalize (Res.all);
8326 -- exception
8327 -- when others =>
8328 -- if not Raised then
8329 -- Raised := True;
8330 -- Save_Occurrence (E,
8331 -- Get_Curent_Excep.all.all);
8332 -- end if;
8333 -- end;
8335 -- [Abort_Undefer;]
8337 -- if Raised and then not Abort then
8338 -- Raise_From_Controlled_Operation (E);
8339 -- end if;
8340 -- end;
8342 if Exceptions_OK then
8343 Abort_And_Exception : declare
8344 Blk_Decls : constant List_Id := New_List;
8345 Blk_Stmts : constant List_Id := New_List;
8346 Fin_Stmts : constant List_Id := New_List;
8348 Fin_Data : Finalization_Exception_Data;
8350 begin
8351 -- Create the declarations of the two flags and the exception
8352 -- occurrence.
8354 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
8356 -- Generate:
8357 -- Abort_Defer;
8359 if Abort_Allowed then
8360 Append_To (Blk_Stmts,
8361 Build_Runtime_Call (Loc, RE_Abort_Defer));
8362 end if;
8364 -- Wrap the hook clear and the finalization call in order to trap
8365 -- a potential exception.
8367 Append_To (Fin_Stmts, Hook_Clear);
8369 if Present (Fin_Call) then
8370 Append_To (Fin_Stmts, Fin_Call);
8371 end if;
8373 Append_To (Blk_Stmts,
8374 Make_Block_Statement (Loc,
8375 Handled_Statement_Sequence =>
8376 Make_Handled_Sequence_Of_Statements (Loc,
8377 Statements => Fin_Stmts,
8378 Exception_Handlers => New_List (
8379 Build_Exception_Handler (Fin_Data)))));
8381 -- Generate:
8382 -- Abort_Undefer;
8384 if Abort_Allowed then
8385 Append_To (Blk_Stmts,
8386 Build_Runtime_Call (Loc, RE_Abort_Undefer));
8387 end if;
8389 -- Reraise the potential exception with a proper "upgrade" to
8390 -- Program_Error if needed.
8392 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
8394 -- Wrap everything in a block
8396 Append_To (Stmts,
8397 Make_Block_Statement (Loc,
8398 Declarations => Blk_Decls,
8399 Handled_Statement_Sequence =>
8400 Make_Handled_Sequence_Of_Statements (Loc,
8401 Statements => Blk_Stmts)));
8402 end Abort_And_Exception;
8404 -- Generate the following code if exception propagation is not allowed
8405 -- and aborts are allowed:
8407 -- begin
8408 -- Abort_Defer;
8409 -- Hook := null;
8410 -- [Deep_]Finalize (Res.all);
8411 -- at end
8412 -- Abort_Undefer_Direct;
8413 -- end;
8415 elsif Abort_Allowed then
8416 Abort_Only : declare
8417 Blk_Stmts : constant List_Id := New_List;
8419 begin
8420 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8421 Append_To (Blk_Stmts, Hook_Clear);
8423 if Present (Fin_Call) then
8424 Append_To (Blk_Stmts, Fin_Call);
8425 end if;
8427 Append_To (Stmts,
8428 Build_Abort_Undefer_Block (Loc,
8429 Stmts => Blk_Stmts,
8430 Context => Aggr));
8431 end Abort_Only;
8433 -- Otherwise generate:
8435 -- Hook := null;
8436 -- [Deep_]Finalize (Res.all);
8438 else
8439 Append_To (Stmts, Hook_Clear);
8441 if Present (Fin_Call) then
8442 Append_To (Stmts, Fin_Call);
8443 end if;
8444 end if;
8445 end Process_Transient_Component_Completion;
8447 ---------------------
8448 -- Sort_Case_Table --
8449 ---------------------
8451 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
8452 L : constant Int := Case_Table'First;
8453 U : constant Int := Case_Table'Last;
8454 K : Int;
8455 J : Int;
8456 T : Case_Bounds;
8458 begin
8459 K := L;
8460 while K /= U loop
8461 T := Case_Table (K + 1);
8463 J := K + 1;
8464 while J /= L
8465 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
8466 Expr_Value (T.Choice_Lo)
8467 loop
8468 Case_Table (J) := Case_Table (J - 1);
8469 J := J - 1;
8470 end loop;
8472 Case_Table (J) := T;
8473 K := K + 1;
8474 end loop;
8475 end Sort_Case_Table;
8477 ----------------------------
8478 -- Static_Array_Aggregate --
8479 ----------------------------
8481 function Static_Array_Aggregate (N : Node_Id) return Boolean is
8482 Bounds : constant Node_Id := Aggregate_Bounds (N);
8484 Typ : constant Entity_Id := Etype (N);
8485 Comp_Type : constant Entity_Id := Component_Type (Typ);
8486 Agg : Node_Id;
8487 Expr : Node_Id;
8488 Lo : Node_Id;
8489 Hi : Node_Id;
8491 begin
8492 if Is_Tagged_Type (Typ)
8493 or else Is_Controlled (Typ)
8494 or else Is_Packed (Typ)
8495 then
8496 return False;
8497 end if;
8499 if Present (Bounds)
8500 and then Nkind (Bounds) = N_Range
8501 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
8502 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
8503 then
8504 Lo := Low_Bound (Bounds);
8505 Hi := High_Bound (Bounds);
8507 if No (Component_Associations (N)) then
8509 -- Verify that all components are static integers
8511 Expr := First (Expressions (N));
8512 while Present (Expr) loop
8513 if Nkind (Expr) /= N_Integer_Literal then
8514 return False;
8515 end if;
8517 Next (Expr);
8518 end loop;
8520 return True;
8522 else
8523 -- We allow only a single named association, either a static
8524 -- range or an others_clause, with a static expression.
8526 Expr := First (Component_Associations (N));
8528 if Present (Expressions (N)) then
8529 return False;
8531 elsif Present (Next (Expr)) then
8532 return False;
8534 elsif Present (Next (First (Choice_List (Expr)))) then
8535 return False;
8537 else
8538 -- The aggregate is static if all components are literals,
8539 -- or else all its components are static aggregates for the
8540 -- component type. We also limit the size of a static aggregate
8541 -- to prevent runaway static expressions.
8543 if Is_Array_Type (Comp_Type)
8544 or else Is_Record_Type (Comp_Type)
8545 then
8546 if Nkind (Expression (Expr)) /= N_Aggregate
8547 or else
8548 not Compile_Time_Known_Aggregate (Expression (Expr))
8549 then
8550 return False;
8551 end if;
8553 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
8554 return False;
8555 end if;
8557 if not Aggr_Size_OK (N, Typ) then
8558 return False;
8559 end if;
8561 -- Create a positional aggregate with the right number of
8562 -- copies of the expression.
8564 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
8566 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
8567 loop
8568 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
8570 -- The copied expression must be analyzed and resolved.
8571 -- Besides setting the type, this ensures that static
8572 -- expressions are appropriately marked as such.
8574 Analyze_And_Resolve
8575 (Last (Expressions (Agg)), Component_Type (Typ));
8576 end loop;
8578 Set_Aggregate_Bounds (Agg, Bounds);
8579 Set_Etype (Agg, Typ);
8580 Set_Analyzed (Agg);
8581 Rewrite (N, Agg);
8582 Set_Compile_Time_Known_Aggregate (N);
8584 return True;
8585 end if;
8586 end if;
8588 else
8589 return False;
8590 end if;
8591 end Static_Array_Aggregate;
8593 ----------------------------------
8594 -- Two_Dim_Packed_Array_Handled --
8595 ----------------------------------
8597 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
8598 Loc : constant Source_Ptr := Sloc (N);
8599 Typ : constant Entity_Id := Etype (N);
8600 Ctyp : constant Entity_Id := Component_Type (Typ);
8601 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
8602 Packed_Array : constant Entity_Id :=
8603 Packed_Array_Impl_Type (Base_Type (Typ));
8605 One_Comp : Node_Id;
8606 -- Expression in original aggregate
8608 One_Dim : Node_Id;
8609 -- One-dimensional subaggregate
8611 begin
8613 -- For now, only deal with cases where an integral number of elements
8614 -- fit in a single byte. This includes the most common boolean case.
8616 if not (Comp_Size = 1 or else
8617 Comp_Size = 2 or else
8618 Comp_Size = 4)
8619 then
8620 return False;
8621 end if;
8623 Convert_To_Positional
8624 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8626 -- Verify that all components are static
8628 if Nkind (N) = N_Aggregate
8629 and then Compile_Time_Known_Aggregate (N)
8630 then
8631 null;
8633 -- The aggregate may have been reanalyzed and converted already
8635 elsif Nkind (N) /= N_Aggregate then
8636 return True;
8638 -- If component associations remain, the aggregate is not static
8640 elsif Present (Component_Associations (N)) then
8641 return False;
8643 else
8644 One_Dim := First (Expressions (N));
8645 while Present (One_Dim) loop
8646 if Present (Component_Associations (One_Dim)) then
8647 return False;
8648 end if;
8650 One_Comp := First (Expressions (One_Dim));
8651 while Present (One_Comp) loop
8652 if not Is_OK_Static_Expression (One_Comp) then
8653 return False;
8654 end if;
8656 Next (One_Comp);
8657 end loop;
8659 Next (One_Dim);
8660 end loop;
8661 end if;
8663 -- Two-dimensional aggregate is now fully positional so pack one
8664 -- dimension to create a static one-dimensional array, and rewrite
8665 -- as an unchecked conversion to the original type.
8667 declare
8668 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
8669 -- The packed array type is a byte array
8671 Packed_Num : Nat;
8672 -- Number of components accumulated in current byte
8674 Comps : List_Id;
8675 -- Assembled list of packed values for equivalent aggregate
8677 Comp_Val : Uint;
8678 -- Integer value of component
8680 Incr : Int;
8681 -- Step size for packing
8683 Init_Shift : Int;
8684 -- Endian-dependent start position for packing
8686 Shift : Int;
8687 -- Current insertion position
8689 Val : Int;
8690 -- Component of packed array being assembled
8692 begin
8693 Comps := New_List;
8694 Val := 0;
8695 Packed_Num := 0;
8697 -- Account for endianness. See corresponding comment in
8698 -- Packed_Array_Aggregate_Handled concerning the following.
8700 if Bytes_Big_Endian
8701 xor Debug_Flag_8
8702 xor Reverse_Storage_Order (Base_Type (Typ))
8703 then
8704 Init_Shift := Byte_Size - Comp_Size;
8705 Incr := -Comp_Size;
8706 else
8707 Init_Shift := 0;
8708 Incr := +Comp_Size;
8709 end if;
8711 -- Iterate over each subaggregate
8713 Shift := Init_Shift;
8714 One_Dim := First (Expressions (N));
8715 while Present (One_Dim) loop
8716 One_Comp := First (Expressions (One_Dim));
8717 while Present (One_Comp) loop
8718 if Packed_Num = Byte_Size / Comp_Size then
8720 -- Byte is complete, add to list of expressions
8722 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8723 Val := 0;
8724 Shift := Init_Shift;
8725 Packed_Num := 0;
8727 else
8728 Comp_Val := Expr_Rep_Value (One_Comp);
8730 -- Adjust for bias, and strip proper number of bits
8732 if Has_Biased_Representation (Ctyp) then
8733 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
8734 end if;
8736 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
8737 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
8738 Shift := Shift + Incr;
8739 One_Comp := Next (One_Comp);
8740 Packed_Num := Packed_Num + 1;
8741 end if;
8742 end loop;
8744 One_Dim := Next (One_Dim);
8745 end loop;
8747 if Packed_Num > 0 then
8749 -- Add final incomplete byte if present
8751 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8752 end if;
8754 Rewrite (N,
8755 Unchecked_Convert_To (Typ,
8756 Make_Qualified_Expression (Loc,
8757 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
8758 Expression => Make_Aggregate (Loc, Expressions => Comps))));
8759 Analyze_And_Resolve (N);
8760 return True;
8761 end;
8762 end Two_Dim_Packed_Array_Handled;
8764 end Exp_Aggr;