Make vect_model_store_cost take a vec_load_store_type
[official-gcc.git] / gcc / ada / exp_aggr.adb
blob92c040ee8abda94cda27dbac06371706574e1a19
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Lib; use Lib;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Ttypes; use Ttypes;
51 with Sem; use Sem;
52 with Sem_Aggr; use Sem_Aggr;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sinfo; use Sinfo;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Stringt; use Stringt;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
64 with Urealp; use Urealp;
66 package body Exp_Aggr is
68 type Case_Bounds is record
69 Choice_Lo : Node_Id;
70 Choice_Hi : Node_Id;
71 Choice_Node : Node_Id;
72 end record;
74 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
75 -- Table type used by Check_Case_Choices procedure
77 procedure Collect_Initialization_Statements
78 (Obj : Entity_Id;
79 N : Node_Id;
80 Node_After : Node_Id);
81 -- If Obj is not frozen, collect actions inserted after N until, but not
82 -- including, Node_After, for initialization of Obj, and move them to an
83 -- expression with actions, which becomes the Initialization_Statements for
84 -- Obj.
86 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id);
87 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id);
89 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
90 -- N is an aggregate (record or array). Checks the presence of default
91 -- initialization (<>) in any component (Ada 2005: AI-287).
93 function In_Object_Declaration (N : Node_Id) return Boolean;
94 -- Return True if N is part of an object declaration, False otherwise
96 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
97 -- Returns true if N is an aggregate used to initialize the components
98 -- of a statically allocated dispatch table.
100 function Late_Expansion
101 (N : Node_Id;
102 Typ : Entity_Id;
103 Target : Node_Id) return List_Id;
104 -- This routine implements top-down expansion of nested aggregates. In
105 -- doing so, it avoids the generation of temporaries at each level. N is
106 -- a nested record or array aggregate with the Expansion_Delayed flag.
107 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
108 -- expression that will hold the result of the aggregate expansion.
110 function Make_OK_Assignment_Statement
111 (Sloc : Source_Ptr;
112 Name : Node_Id;
113 Expression : Node_Id) return Node_Id;
114 -- This is like Make_Assignment_Statement, except that Assignment_OK
115 -- is set in the left operand. All assignments built by this unit use
116 -- this routine. This is needed to deal with assignments to initialized
117 -- constants that are done in place.
119 function Must_Slide
120 (Obj_Type : Entity_Id;
121 Typ : Entity_Id) return Boolean;
122 -- A static array aggregate in an object declaration can in most cases be
123 -- expanded in place. The one exception is when the aggregate is given
124 -- with component associations that specify different bounds from those of
125 -- the type definition in the object declaration. In this pathological
126 -- case the aggregate must slide, and we must introduce an intermediate
127 -- temporary to hold it.
129 -- The same holds in an assignment to one-dimensional array of arrays,
130 -- when a component may be given with bounds that differ from those of the
131 -- component type.
133 function Number_Of_Choices (N : Node_Id) return Nat;
134 -- Returns the number of discrete choices (not including the others choice
135 -- if present) contained in (sub-)aggregate N.
137 procedure Process_Transient_Component
138 (Loc : Source_Ptr;
139 Comp_Typ : Entity_Id;
140 Init_Expr : Node_Id;
141 Fin_Call : out Node_Id;
142 Hook_Clear : out Node_Id;
143 Aggr : Node_Id := Empty;
144 Stmts : List_Id := No_List);
145 -- Subsidiary to the expansion of array and record aggregates. Generate
146 -- part of the necessary code to finalize a transient component. Comp_Typ
147 -- is the component type. Init_Expr is the initialization expression of the
148 -- component which is always a function call. Fin_Call is the finalization
149 -- call used to clean up the transient function result. Hook_Clear is the
150 -- hook reset statement. Aggr and Stmts both control the placement of the
151 -- generated code. Aggr is the related aggregate. If present, all code is
152 -- inserted prior to Aggr using Insert_Action. Stmts is the initialization
153 -- statements of the component. If present, all code is added to Stmts.
155 procedure Process_Transient_Component_Completion
156 (Loc : Source_Ptr;
157 Aggr : Node_Id;
158 Fin_Call : Node_Id;
159 Hook_Clear : Node_Id;
160 Stmts : List_Id);
161 -- Subsidiary to the expansion of array and record aggregates. Generate
162 -- part of the necessary code to finalize a transient component. Aggr is
163 -- the related aggregate. Fin_Clear is the finalization call used to clean
164 -- up the transient component. Hook_Clear is the hook reset statment. Stmts
165 -- is the initialization statement list for the component. All generated
166 -- code is added to Stmts.
168 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
169 -- Sort the Case Table using the Lower Bound of each Choice as the key.
170 -- A simple insertion sort is used since the number of choices in a case
171 -- statement of variant part will usually be small and probably in near
172 -- sorted order.
174 ------------------------------------------------------
175 -- Local subprograms for Record Aggregate Expansion --
176 ------------------------------------------------------
178 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean;
179 -- True if N is an aggregate (possibly qualified or converted) that is
180 -- being returned from a build-in-place function.
182 function Build_Record_Aggr_Code
183 (N : Node_Id;
184 Typ : Entity_Id;
185 Lhs : Node_Id) return List_Id;
186 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
187 -- aggregate. Target is an expression containing the location on which the
188 -- component by component assignments will take place. Returns the list of
189 -- assignments plus all other adjustments needed for tagged and controlled
190 -- types.
192 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
193 -- Transform a record aggregate into a sequence of assignments performed
194 -- component by component. N is an N_Aggregate or N_Extension_Aggregate.
195 -- Typ is the type of the record aggregate.
197 procedure Expand_Record_Aggregate
198 (N : Node_Id;
199 Orig_Tag : Node_Id := Empty;
200 Parent_Expr : Node_Id := Empty);
201 -- This is the top level procedure for record aggregate expansion.
202 -- Expansion for record aggregates needs expand aggregates for tagged
203 -- record types. Specifically Expand_Record_Aggregate adds the Tag
204 -- field in front of the Component_Association list that was created
205 -- during resolution by Resolve_Record_Aggregate.
207 -- N is the record aggregate node.
208 -- Orig_Tag is the value of the Tag that has to be provided for this
209 -- specific aggregate. It carries the tag corresponding to the type
210 -- of the outermost aggregate during the recursive expansion
211 -- Parent_Expr is the ancestor part of the original extension
212 -- aggregate
214 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
215 -- Return true if one of the components is of a discriminated type with
216 -- defaults. An aggregate for a type with mutable components must be
217 -- expanded into individual assignments.
219 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
220 -- If the type of the aggregate is a type extension with renamed discrimi-
221 -- nants, we must initialize the hidden discriminants of the parent.
222 -- Otherwise, the target object must not be initialized. The discriminants
223 -- are initialized by calling the initialization procedure for the type.
224 -- This is incorrect if the initialization of other components has any
225 -- side effects. We restrict this call to the case where the parent type
226 -- has a variant part, because this is the only case where the hidden
227 -- discriminants are accessed, namely when calling discriminant checking
228 -- functions of the parent type, and when applying a stream attribute to
229 -- an object of the derived type.
231 -----------------------------------------------------
232 -- Local Subprograms for Array Aggregate Expansion --
233 -----------------------------------------------------
235 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
236 -- Very large static aggregates present problems to the back-end, and are
237 -- transformed into assignments and loops. This function verifies that the
238 -- total number of components of an aggregate is acceptable for rewriting
239 -- into a purely positional static form. Aggr_Size_OK must be called before
240 -- calling Flatten.
242 -- This function also detects and warns about one-component aggregates that
243 -- appear in a non-static context. Even if the component value is static,
244 -- such an aggregate must be expanded into an assignment.
246 function Backend_Processing_Possible (N : Node_Id) return Boolean;
247 -- This function checks if array aggregate N can be processed directly
248 -- by the backend. If this is the case, True is returned.
250 function Build_Array_Aggr_Code
251 (N : Node_Id;
252 Ctype : Entity_Id;
253 Index : Node_Id;
254 Into : Node_Id;
255 Scalar_Comp : Boolean;
256 Indexes : List_Id := No_List) return List_Id;
257 -- This recursive routine returns a list of statements containing the
258 -- loops and assignments that are needed for the expansion of the array
259 -- aggregate N.
261 -- N is the (sub-)aggregate node to be expanded into code. This node has
262 -- been fully analyzed, and its Etype is properly set.
264 -- Index is the index node corresponding to the array subaggregate N
266 -- Into is the target expression into which we are copying the aggregate.
267 -- Note that this node may not have been analyzed yet, and so the Etype
268 -- field may not be set.
270 -- Scalar_Comp is True if the component type of the aggregate is scalar
272 -- Indexes is the current list of expressions used to index the object we
273 -- are writing into.
275 procedure Convert_Array_Aggr_In_Allocator
276 (Decl : Node_Id;
277 Aggr : Node_Id;
278 Target : Node_Id);
279 -- If the aggregate appears within an allocator and can be expanded in
280 -- place, this routine generates the individual assignments to components
281 -- of the designated object. This is an optimization over the general
282 -- case, where a temporary is first created on the stack and then used to
283 -- construct the allocated object on the heap.
285 procedure Convert_To_Positional
286 (N : Node_Id;
287 Max_Others_Replicate : Nat := 5;
288 Handle_Bit_Packed : Boolean := False);
289 -- If possible, convert named notation to positional notation. This
290 -- conversion is possible only in some static cases. If the conversion is
291 -- possible, then N is rewritten with the analyzed converted aggregate.
292 -- The parameter Max_Others_Replicate controls the maximum number of
293 -- values corresponding to an others choice that will be converted to
294 -- positional notation (the default of 5 is the normal limit, and reflects
295 -- the fact that normally the loop is better than a lot of separate
296 -- assignments). Note that this limit gets overridden in any case if
297 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
298 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
299 -- not expect the back end to handle bit packed arrays, so the normal case
300 -- of conversion is pointless), but in the special case of a call from
301 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
302 -- these are cases we handle in there.
304 -- It would seem useful to have a higher default for Max_Others_Replicate,
305 -- but aggregates in the compiler make this impossible: the compiler
306 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
307 -- is unexpected ???
309 procedure Expand_Array_Aggregate (N : Node_Id);
310 -- This is the top-level routine to perform array aggregate expansion.
311 -- N is the N_Aggregate node to be expanded.
313 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
314 -- For two-dimensional packed aggregates with constant bounds and constant
315 -- components, it is preferable to pack the inner aggregates because the
316 -- whole matrix can then be presented to the back-end as a one-dimensional
317 -- list of literals. This is much more efficient than expanding into single
318 -- component assignments. This function determines if the type Typ is for
319 -- an array that is suitable for this optimization: it returns True if Typ
320 -- is a two dimensional bit packed array with component size 1, 2, or 4.
322 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
323 -- Given an array aggregate, this function handles the case of a packed
324 -- array aggregate with all constant values, where the aggregate can be
325 -- evaluated at compile time. If this is possible, then N is rewritten
326 -- to be its proper compile time value with all the components properly
327 -- assembled. The expression is analyzed and resolved and True is returned.
328 -- If this transformation is not possible, N is unchanged and False is
329 -- returned.
331 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
332 -- If the type of the aggregate is a two-dimensional bit_packed array
333 -- it may be transformed into an array of bytes with constant values,
334 -- and presented to the back-end as a static value. The function returns
335 -- false if this transformation cannot be performed. THis is similar to,
336 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
338 ------------------
339 -- Aggr_Size_OK --
340 ------------------
342 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
343 Lo : Node_Id;
344 Hi : Node_Id;
345 Indx : Node_Id;
346 Siz : Int;
347 Lov : Uint;
348 Hiv : Uint;
350 Max_Aggr_Size : Nat;
351 -- Determines the maximum size of an array aggregate produced by
352 -- converting named to positional notation (e.g. from others clauses).
353 -- This avoids running away with attempts to convert huge aggregates,
354 -- which hit memory limits in the backend.
356 function Component_Count (T : Entity_Id) return Nat;
357 -- The limit is applied to the total number of subcomponents that the
358 -- aggregate will have, which is the number of static expressions
359 -- that will appear in the flattened array. This requires a recursive
360 -- computation of the number of scalar components of the structure.
362 ---------------------
363 -- Component_Count --
364 ---------------------
366 function Component_Count (T : Entity_Id) return Nat is
367 Res : Nat := 0;
368 Comp : Entity_Id;
370 begin
371 if Is_Scalar_Type (T) then
372 return 1;
374 elsif Is_Record_Type (T) then
375 Comp := First_Component (T);
376 while Present (Comp) loop
377 Res := Res + Component_Count (Etype (Comp));
378 Next_Component (Comp);
379 end loop;
381 return Res;
383 elsif Is_Array_Type (T) then
384 declare
385 Lo : constant Node_Id :=
386 Type_Low_Bound (Etype (First_Index (T)));
387 Hi : constant Node_Id :=
388 Type_High_Bound (Etype (First_Index (T)));
390 Siz : constant Nat := Component_Count (Component_Type (T));
392 begin
393 -- Check for superflat arrays, i.e. arrays with such bounds
394 -- as 4 .. 2, to insure that this function never returns a
395 -- meaningless negative value.
397 if not Compile_Time_Known_Value (Lo)
398 or else not Compile_Time_Known_Value (Hi)
399 or else Expr_Value (Hi) < Expr_Value (Lo)
400 then
401 return 0;
403 else
404 -- If the number of components is greater than Int'Last,
405 -- then return Int'Last, so caller will return False (Aggr
406 -- size is not OK). Otherwise, UI_To_Int will crash.
408 declare
409 UI : constant Uint :=
410 Expr_Value (Hi) - Expr_Value (Lo) + 1;
411 begin
412 if UI_Is_In_Int_Range (UI) then
413 return Siz * UI_To_Int (UI);
414 else
415 return Int'Last;
416 end if;
417 end;
418 end if;
419 end;
421 else
422 -- Can only be a null for an access type
424 return 1;
425 end if;
426 end Component_Count;
428 -- Start of processing for Aggr_Size_OK
430 begin
431 -- The normal aggregate limit is 500000, but we increase this limit to
432 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
433 -- Restrictions (No_Implicit_Loops) is specified, since in either case
434 -- we are at risk of declaring the program illegal because of this
435 -- limit. We also increase the limit when Static_Elaboration_Desired,
436 -- given that this means that objects are intended to be placed in data
437 -- memory.
439 -- We also increase the limit if the aggregate is for a packed two-
440 -- dimensional array, because if components are static it is much more
441 -- efficient to construct a one-dimensional equivalent array with static
442 -- components.
444 -- Conversely, we decrease the maximum size if none of the above
445 -- requirements apply, and if the aggregate has a single component
446 -- association, which will be more efficient if implemented with a loop.
448 -- Finally, we use a small limit in CodePeer mode where we favor loops
449 -- instead of thousands of single assignments (from large aggregates).
451 Max_Aggr_Size := 500000;
453 if CodePeer_Mode then
454 Max_Aggr_Size := 100;
456 elsif Restriction_Active (No_Elaboration_Code)
457 or else Restriction_Active (No_Implicit_Loops)
458 or else Is_Two_Dim_Packed_Array (Typ)
459 or else (Ekind (Current_Scope) = E_Package
460 and then Static_Elaboration_Desired (Current_Scope))
461 then
462 Max_Aggr_Size := 2 ** 24;
464 elsif No (Expressions (N))
465 and then No (Next (First (Component_Associations (N))))
466 then
467 Max_Aggr_Size := 5000;
468 end if;
470 Siz := Component_Count (Component_Type (Typ));
472 Indx := First_Index (Typ);
473 while Present (Indx) loop
474 Lo := Type_Low_Bound (Etype (Indx));
475 Hi := Type_High_Bound (Etype (Indx));
477 -- Bounds need to be known at compile time
479 if not Compile_Time_Known_Value (Lo)
480 or else not Compile_Time_Known_Value (Hi)
481 then
482 return False;
483 end if;
485 Lov := Expr_Value (Lo);
486 Hiv := Expr_Value (Hi);
488 -- A flat array is always safe
490 if Hiv < Lov then
491 return True;
492 end if;
494 -- One-component aggregates are suspicious, and if the context type
495 -- is an object declaration with non-static bounds it will trip gcc;
496 -- such an aggregate must be expanded into a single assignment.
498 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
499 declare
500 Index_Type : constant Entity_Id :=
501 Etype
502 (First_Index (Etype (Defining_Identifier (Parent (N)))));
503 Indx : Node_Id;
505 begin
506 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
507 or else not Compile_Time_Known_Value
508 (Type_High_Bound (Index_Type))
509 then
510 if Present (Component_Associations (N)) then
511 Indx :=
512 First
513 (Choice_List (First (Component_Associations (N))));
515 if Is_Entity_Name (Indx)
516 and then not Is_Type (Entity (Indx))
517 then
518 Error_Msg_N
519 ("single component aggregate in "
520 & "non-static context??", Indx);
521 Error_Msg_N ("\maybe subtype name was meant??", Indx);
522 end if;
523 end if;
525 return False;
526 end if;
527 end;
528 end if;
530 declare
531 Rng : constant Uint := Hiv - Lov + 1;
533 begin
534 -- Check if size is too large
536 if not UI_Is_In_Int_Range (Rng) then
537 return False;
538 end if;
540 Siz := Siz * UI_To_Int (Rng);
541 end;
543 if Siz <= 0
544 or else Siz > Max_Aggr_Size
545 then
546 return False;
547 end if;
549 -- Bounds must be in integer range, for later array construction
551 if not UI_Is_In_Int_Range (Lov)
552 or else
553 not UI_Is_In_Int_Range (Hiv)
554 then
555 return False;
556 end if;
558 Next_Index (Indx);
559 end loop;
561 return True;
562 end Aggr_Size_OK;
564 ---------------------------------
565 -- Backend_Processing_Possible --
566 ---------------------------------
568 -- Backend processing by Gigi/gcc is possible only if all the following
569 -- conditions are met:
571 -- 1. N is fully positional
573 -- 2. N is not a bit-packed array aggregate;
575 -- 3. The size of N's array type must be known at compile time. Note
576 -- that this implies that the component size is also known
578 -- 4. The array type of N does not follow the Fortran layout convention
579 -- or if it does it must be 1 dimensional.
581 -- 5. The array component type may not be tagged (which could necessitate
582 -- reassignment of proper tags).
584 -- 6. The array component type must not have unaligned bit components
586 -- 7. None of the components of the aggregate may be bit unaligned
587 -- components.
589 -- 8. There cannot be delayed components, since we do not know enough
590 -- at this stage to know if back end processing is possible.
592 -- 9. There cannot be any discriminated record components, since the
593 -- back end cannot handle this complex case.
595 -- 10. No controlled actions need to be generated for components
597 -- 11. When generating C code, N must be part of a N_Object_Declaration
599 -- 12. When generating C code, N must not include function calls
601 function Backend_Processing_Possible (N : Node_Id) return Boolean is
602 Typ : constant Entity_Id := Etype (N);
603 -- Typ is the correct constrained array subtype of the aggregate
605 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
606 -- This routine checks components of aggregate N, enforcing checks
607 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
608 -- are performed on subaggregates. The Index value is the current index
609 -- being checked in the multidimensional case.
611 ---------------------
612 -- Component_Check --
613 ---------------------
615 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
616 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
617 -- Given a type conversion or an unchecked type conversion N, return
618 -- its innermost original expression.
620 ----------------------------------
621 -- Ultimate_Original_Expression --
622 ----------------------------------
624 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
625 Expr : Node_Id := Original_Node (N);
627 begin
628 while Nkind_In (Expr, N_Type_Conversion,
629 N_Unchecked_Type_Conversion)
630 loop
631 Expr := Original_Node (Expression (Expr));
632 end loop;
634 return Expr;
635 end Ultimate_Original_Expression;
637 -- Local variables
639 Expr : Node_Id;
641 -- Start of processing for Component_Check
643 begin
644 -- Checks 1: (no component associations)
646 if Present (Component_Associations (N)) then
647 return False;
648 end if;
650 -- Checks 11: The C code generator cannot handle aggregates that are
651 -- not part of an object declaration.
653 if Modify_Tree_For_C then
654 declare
655 Par : Node_Id := Parent (N);
657 begin
658 -- Skip enclosing nested aggregates and their qualified
659 -- expressions.
661 while Nkind (Par) = N_Aggregate
662 or else Nkind (Par) = N_Qualified_Expression
663 loop
664 Par := Parent (Par);
665 end loop;
667 if Nkind (Par) /= N_Object_Declaration then
668 return False;
669 end if;
670 end;
671 end if;
673 -- Checks on components
675 -- Recurse to check subaggregates, which may appear in qualified
676 -- expressions. If delayed, the front-end will have to expand.
677 -- If the component is a discriminated record, treat as non-static,
678 -- as the back-end cannot handle this properly.
680 Expr := First (Expressions (N));
681 while Present (Expr) loop
683 -- Checks 8: (no delayed components)
685 if Is_Delayed_Aggregate (Expr) then
686 return False;
687 end if;
689 -- Checks 9: (no discriminated records)
691 if Present (Etype (Expr))
692 and then Is_Record_Type (Etype (Expr))
693 and then Has_Discriminants (Etype (Expr))
694 then
695 return False;
696 end if;
698 -- Checks 7. Component must not be bit aligned component
700 if Possible_Bit_Aligned_Component (Expr) then
701 return False;
702 end if;
704 -- Checks 12: (no function call)
706 if Modify_Tree_For_C
707 and then
708 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
709 then
710 return False;
711 end if;
713 -- Recursion to following indexes for multiple dimension case
715 if Present (Next_Index (Index))
716 and then not Component_Check (Expr, Next_Index (Index))
717 then
718 return False;
719 end if;
721 -- All checks for that component finished, on to next
723 Next (Expr);
724 end loop;
726 return True;
727 end Component_Check;
729 -- Start of processing for Backend_Processing_Possible
731 begin
732 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
734 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
735 return False;
736 end if;
738 -- If component is limited, aggregate must be expanded because each
739 -- component assignment must be built in place.
741 if Is_Limited_View (Component_Type (Typ)) then
742 return False;
743 end if;
745 -- Checks 4 (array must not be multidimensional Fortran case)
747 if Convention (Typ) = Convention_Fortran
748 and then Number_Dimensions (Typ) > 1
749 then
750 return False;
751 end if;
753 -- Checks 3 (size of array must be known at compile time)
755 if not Size_Known_At_Compile_Time (Typ) then
756 return False;
757 end if;
759 -- Checks on components
761 if not Component_Check (N, First_Index (Typ)) then
762 return False;
763 end if;
765 -- Checks 5 (if the component type is tagged, then we may need to do
766 -- tag adjustments. Perhaps this should be refined to check for any
767 -- component associations that actually need tag adjustment, similar
768 -- to the test in Component_OK_For_Backend for record aggregates with
769 -- tagged components, but not clear whether it's worthwhile ???; in the
770 -- case of virtual machines (no Tagged_Type_Expansion), object tags are
771 -- handled implicitly).
773 if Is_Tagged_Type (Component_Type (Typ))
774 and then Tagged_Type_Expansion
775 then
776 return False;
777 end if;
779 -- Checks 6 (component type must not have bit aligned components)
781 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
782 return False;
783 end if;
785 -- Backend processing is possible
787 Set_Size_Known_At_Compile_Time (Etype (N), True);
788 return True;
789 end Backend_Processing_Possible;
791 ---------------------------
792 -- Build_Array_Aggr_Code --
793 ---------------------------
795 -- The code that we generate from a one dimensional aggregate is
797 -- 1. If the subaggregate contains discrete choices we
799 -- (a) Sort the discrete choices
801 -- (b) Otherwise for each discrete choice that specifies a range we
802 -- emit a loop. If a range specifies a maximum of three values, or
803 -- we are dealing with an expression we emit a sequence of
804 -- assignments instead of a loop.
806 -- (c) Generate the remaining loops to cover the others choice if any
808 -- 2. If the aggregate contains positional elements we
810 -- (a) translate the positional elements in a series of assignments
812 -- (b) Generate a final loop to cover the others choice if any.
813 -- Note that this final loop has to be a while loop since the case
815 -- L : Integer := Integer'Last;
816 -- H : Integer := Integer'Last;
817 -- A : array (L .. H) := (1, others =>0);
819 -- cannot be handled by a for loop. Thus for the following
821 -- array (L .. H) := (.. positional elements.., others =>E);
823 -- we always generate something like:
825 -- J : Index_Type := Index_Of_Last_Positional_Element;
826 -- while J < H loop
827 -- J := Index_Base'Succ (J)
828 -- Tmp (J) := E;
829 -- end loop;
831 function Build_Array_Aggr_Code
832 (N : Node_Id;
833 Ctype : Entity_Id;
834 Index : Node_Id;
835 Into : Node_Id;
836 Scalar_Comp : Boolean;
837 Indexes : List_Id := No_List) return List_Id
839 Loc : constant Source_Ptr := Sloc (N);
840 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
841 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
842 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
844 function Add (Val : Int; To : Node_Id) return Node_Id;
845 -- Returns an expression where Val is added to expression To, unless
846 -- To+Val is provably out of To's base type range. To must be an
847 -- already analyzed expression.
849 function Empty_Range (L, H : Node_Id) return Boolean;
850 -- Returns True if the range defined by L .. H is certainly empty
852 function Equal (L, H : Node_Id) return Boolean;
853 -- Returns True if L = H for sure
855 function Index_Base_Name return Node_Id;
856 -- Returns a new reference to the index type name
858 function Gen_Assign
859 (Ind : Node_Id;
860 Expr : Node_Id;
861 In_Loop : Boolean := False) return List_Id;
862 -- Ind must be a side-effect-free expression. If the input aggregate N
863 -- to Build_Loop contains no subaggregates, then this function returns
864 -- the assignment statement:
866 -- Into (Indexes, Ind) := Expr;
868 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
869 -- when the assignment appears within a generated loop.
871 -- Ada 2005 (AI-287): In case of default initialized component, Expr
872 -- is empty and we generate a call to the corresponding IP subprogram.
874 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
875 -- Nodes L and H must be side-effect-free expressions. If the input
876 -- aggregate N to Build_Loop contains no subaggregates, this routine
877 -- returns the for loop statement:
879 -- for J in Index_Base'(L) .. Index_Base'(H) loop
880 -- Into (Indexes, J) := Expr;
881 -- end loop;
883 -- Otherwise we call Build_Code recursively. As an optimization if the
884 -- loop covers 3 or fewer scalar elements we generate a sequence of
885 -- assignments.
886 -- If the component association that generates the loop comes from an
887 -- Iterated_Component_Association, the loop parameter has the name of
888 -- the corresponding parameter in the original construct.
890 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
891 -- Nodes L and H must be side-effect-free expressions. If the input
892 -- aggregate N to Build_Loop contains no subaggregates, this routine
893 -- returns the while loop statement:
895 -- J : Index_Base := L;
896 -- while J < H loop
897 -- J := Index_Base'Succ (J);
898 -- Into (Indexes, J) := Expr;
899 -- end loop;
901 -- Otherwise we call Build_Code recursively
903 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
904 -- For an association with a box, use value given by aspect
905 -- Default_Component_Value of array type if specified, else use
906 -- value given by aspect Default_Value for component type itself
907 -- if specified, else return Empty.
909 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
910 function Local_Expr_Value (E : Node_Id) return Uint;
911 -- These two Local routines are used to replace the corresponding ones
912 -- in sem_eval because while processing the bounds of an aggregate with
913 -- discrete choices whose index type is an enumeration, we build static
914 -- expressions not recognized by Compile_Time_Known_Value as such since
915 -- they have not yet been analyzed and resolved. All the expressions in
916 -- question are things like Index_Base_Name'Val (Const) which we can
917 -- easily recognize as being constant.
919 ---------
920 -- Add --
921 ---------
923 function Add (Val : Int; To : Node_Id) return Node_Id is
924 Expr_Pos : Node_Id;
925 Expr : Node_Id;
926 To_Pos : Node_Id;
927 U_To : Uint;
928 U_Val : constant Uint := UI_From_Int (Val);
930 begin
931 -- Note: do not try to optimize the case of Val = 0, because
932 -- we need to build a new node with the proper Sloc value anyway.
934 -- First test if we can do constant folding
936 if Local_Compile_Time_Known_Value (To) then
937 U_To := Local_Expr_Value (To) + Val;
939 -- Determine if our constant is outside the range of the index.
940 -- If so return an Empty node. This empty node will be caught
941 -- by Empty_Range below.
943 if Compile_Time_Known_Value (Index_Base_L)
944 and then U_To < Expr_Value (Index_Base_L)
945 then
946 return Empty;
948 elsif Compile_Time_Known_Value (Index_Base_H)
949 and then U_To > Expr_Value (Index_Base_H)
950 then
951 return Empty;
952 end if;
954 Expr_Pos := Make_Integer_Literal (Loc, U_To);
955 Set_Is_Static_Expression (Expr_Pos);
957 if not Is_Enumeration_Type (Index_Base) then
958 Expr := Expr_Pos;
960 -- If we are dealing with enumeration return
961 -- Index_Base'Val (Expr_Pos)
963 else
964 Expr :=
965 Make_Attribute_Reference
966 (Loc,
967 Prefix => Index_Base_Name,
968 Attribute_Name => Name_Val,
969 Expressions => New_List (Expr_Pos));
970 end if;
972 return Expr;
973 end if;
975 -- If we are here no constant folding possible
977 if not Is_Enumeration_Type (Index_Base) then
978 Expr :=
979 Make_Op_Add (Loc,
980 Left_Opnd => Duplicate_Subexpr (To),
981 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
983 -- If we are dealing with enumeration return
984 -- Index_Base'Val (Index_Base'Pos (To) + Val)
986 else
987 To_Pos :=
988 Make_Attribute_Reference
989 (Loc,
990 Prefix => Index_Base_Name,
991 Attribute_Name => Name_Pos,
992 Expressions => New_List (Duplicate_Subexpr (To)));
994 Expr_Pos :=
995 Make_Op_Add (Loc,
996 Left_Opnd => To_Pos,
997 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
999 Expr :=
1000 Make_Attribute_Reference
1001 (Loc,
1002 Prefix => Index_Base_Name,
1003 Attribute_Name => Name_Val,
1004 Expressions => New_List (Expr_Pos));
1005 end if;
1007 return Expr;
1008 end Add;
1010 -----------------
1011 -- Empty_Range --
1012 -----------------
1014 function Empty_Range (L, H : Node_Id) return Boolean is
1015 Is_Empty : Boolean := False;
1016 Low : Node_Id;
1017 High : Node_Id;
1019 begin
1020 -- First check if L or H were already detected as overflowing the
1021 -- index base range type by function Add above. If this is so Add
1022 -- returns the empty node.
1024 if No (L) or else No (H) then
1025 return True;
1026 end if;
1028 for J in 1 .. 3 loop
1029 case J is
1031 -- L > H range is empty
1033 when 1 =>
1034 Low := L;
1035 High := H;
1037 -- B_L > H range must be empty
1039 when 2 =>
1040 Low := Index_Base_L;
1041 High := H;
1043 -- L > B_H range must be empty
1045 when 3 =>
1046 Low := L;
1047 High := Index_Base_H;
1048 end case;
1050 if Local_Compile_Time_Known_Value (Low)
1051 and then
1052 Local_Compile_Time_Known_Value (High)
1053 then
1054 Is_Empty :=
1055 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
1056 end if;
1058 exit when Is_Empty;
1059 end loop;
1061 return Is_Empty;
1062 end Empty_Range;
1064 -----------
1065 -- Equal --
1066 -----------
1068 function Equal (L, H : Node_Id) return Boolean is
1069 begin
1070 if L = H then
1071 return True;
1073 elsif Local_Compile_Time_Known_Value (L)
1074 and then
1075 Local_Compile_Time_Known_Value (H)
1076 then
1077 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1078 end if;
1080 return False;
1081 end Equal;
1083 ----------------
1084 -- Gen_Assign --
1085 ----------------
1087 function Gen_Assign
1088 (Ind : Node_Id;
1089 Expr : Node_Id;
1090 In_Loop : Boolean := False) return List_Id
1092 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1093 -- Collect insert_actions generated in the construction of a loop,
1094 -- and prepend them to the sequence of assignments to complete the
1095 -- eventual body of the loop.
1097 procedure Initialize_Array_Component
1098 (Arr_Comp : Node_Id;
1099 Comp_Typ : Node_Id;
1100 Init_Expr : Node_Id;
1101 Stmts : List_Id);
1102 -- Perform the initialization of array component Arr_Comp with
1103 -- expected type Comp_Typ. Init_Expr denotes the initialization
1104 -- expression of the array component. All generated code is added
1105 -- to list Stmts.
1107 procedure Initialize_Ctrl_Array_Component
1108 (Arr_Comp : Node_Id;
1109 Comp_Typ : Entity_Id;
1110 Init_Expr : Node_Id;
1111 Stmts : List_Id);
1112 -- Perform the initialization of array component Arr_Comp when its
1113 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1114 -- the initialization expression of the array component. All hook-
1115 -- related declarations are inserted prior to aggregate N. Remaining
1116 -- code is added to list Stmts.
1118 ----------------------
1119 -- Add_Loop_Actions --
1120 ----------------------
1122 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1123 Res : List_Id;
1125 begin
1126 -- Ada 2005 (AI-287): Do nothing else in case of default
1127 -- initialized component.
1129 if No (Expr) then
1130 return Lis;
1132 elsif Nkind (Parent (Expr)) = N_Component_Association
1133 and then Present (Loop_Actions (Parent (Expr)))
1134 then
1135 Append_List (Lis, Loop_Actions (Parent (Expr)));
1136 Res := Loop_Actions (Parent (Expr));
1137 Set_Loop_Actions (Parent (Expr), No_List);
1138 return Res;
1140 else
1141 return Lis;
1142 end if;
1143 end Add_Loop_Actions;
1145 --------------------------------
1146 -- Initialize_Array_Component --
1147 --------------------------------
1149 procedure Initialize_Array_Component
1150 (Arr_Comp : Node_Id;
1151 Comp_Typ : Node_Id;
1152 Init_Expr : Node_Id;
1153 Stmts : List_Id)
1155 Exceptions_OK : constant Boolean :=
1156 not Restriction_Active
1157 (No_Exception_Propagation);
1159 Finalization_OK : constant Boolean :=
1160 Present (Comp_Typ)
1161 and then Needs_Finalization (Comp_Typ);
1163 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
1164 Adj_Call : Node_Id;
1165 Blk_Stmts : List_Id;
1166 Init_Stmt : Node_Id;
1168 begin
1169 -- Protect the initialization statements from aborts. Generate:
1171 -- Abort_Defer;
1173 if Finalization_OK and Abort_Allowed then
1174 if Exceptions_OK then
1175 Blk_Stmts := New_List;
1176 else
1177 Blk_Stmts := Stmts;
1178 end if;
1180 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
1182 -- Otherwise aborts are not allowed. All generated code is added
1183 -- directly to the input list.
1185 else
1186 Blk_Stmts := Stmts;
1187 end if;
1189 -- Initialize the array element. Generate:
1191 -- Arr_Comp := Init_Expr;
1193 -- Note that the initialization expression is replicated because
1194 -- it has to be reevaluated within a generated loop.
1196 Init_Stmt :=
1197 Make_OK_Assignment_Statement (Loc,
1198 Name => New_Copy_Tree (Arr_Comp),
1199 Expression => New_Copy_Tree (Init_Expr));
1200 Set_No_Ctrl_Actions (Init_Stmt);
1202 -- If this is an aggregate for an array of arrays, each
1203 -- subaggregate will be expanded as well, and even with
1204 -- No_Ctrl_Actions the assignments of inner components will
1205 -- require attachment in their assignments to temporaries. These
1206 -- temporaries must be finalized for each subaggregate. Generate:
1208 -- begin
1209 -- Arr_Comp := Init_Expr;
1210 -- end;
1212 if Finalization_OK and then Is_Array_Type (Comp_Typ) then
1213 Init_Stmt :=
1214 Make_Block_Statement (Loc,
1215 Handled_Statement_Sequence =>
1216 Make_Handled_Sequence_Of_Statements (Loc,
1217 Statements => New_List (Init_Stmt)));
1218 end if;
1220 Append_To (Blk_Stmts, Init_Stmt);
1222 -- Adjust the tag due to a possible view conversion. Generate:
1224 -- Arr_Comp._tag := Full_TypP;
1226 if Tagged_Type_Expansion
1227 and then Present (Comp_Typ)
1228 and then Is_Tagged_Type (Comp_Typ)
1229 then
1230 Append_To (Blk_Stmts,
1231 Make_OK_Assignment_Statement (Loc,
1232 Name =>
1233 Make_Selected_Component (Loc,
1234 Prefix => New_Copy_Tree (Arr_Comp),
1235 Selector_Name =>
1236 New_Occurrence_Of
1237 (First_Tag_Component (Full_Typ), Loc)),
1239 Expression =>
1240 Unchecked_Convert_To (RTE (RE_Tag),
1241 New_Occurrence_Of
1242 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1243 Loc))));
1244 end if;
1246 -- Adjust the array component. Controlled subaggregates are not
1247 -- considered because each of their individual elements will
1248 -- receive an adjustment of its own. Generate:
1250 -- [Deep_]Adjust (Arr_Comp);
1252 if Finalization_OK
1253 and then not Is_Limited_Type (Comp_Typ)
1254 and then not Is_Build_In_Place_Function_Call (Init_Expr)
1255 and then not
1256 (Is_Array_Type (Comp_Typ)
1257 and then Is_Controlled (Component_Type (Comp_Typ))
1258 and then Nkind (Expr) = N_Aggregate)
1259 then
1260 Adj_Call :=
1261 Make_Adjust_Call
1262 (Obj_Ref => New_Copy_Tree (Arr_Comp),
1263 Typ => Comp_Typ);
1265 -- Guard against a missing [Deep_]Adjust when the component
1266 -- type was not frozen properly.
1268 if Present (Adj_Call) then
1269 Append_To (Blk_Stmts, Adj_Call);
1270 end if;
1271 end if;
1273 -- Complete the protection of the initialization statements
1275 if Finalization_OK and Abort_Allowed then
1277 -- Wrap the initialization statements in a block to catch a
1278 -- potential exception. Generate:
1280 -- begin
1281 -- Abort_Defer;
1282 -- Arr_Comp := Init_Expr;
1283 -- Arr_Comp._tag := Full_TypP;
1284 -- [Deep_]Adjust (Arr_Comp);
1285 -- at end
1286 -- Abort_Undefer_Direct;
1287 -- end;
1289 if Exceptions_OK then
1290 Append_To (Stmts,
1291 Build_Abort_Undefer_Block (Loc,
1292 Stmts => Blk_Stmts,
1293 Context => N));
1295 -- Otherwise exceptions are not propagated. Generate:
1297 -- Abort_Defer;
1298 -- Arr_Comp := Init_Expr;
1299 -- Arr_Comp._tag := Full_TypP;
1300 -- [Deep_]Adjust (Arr_Comp);
1301 -- Abort_Undefer;
1303 else
1304 Append_To (Blk_Stmts,
1305 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1306 end if;
1307 end if;
1308 end Initialize_Array_Component;
1310 -------------------------------------
1311 -- Initialize_Ctrl_Array_Component --
1312 -------------------------------------
1314 procedure Initialize_Ctrl_Array_Component
1315 (Arr_Comp : Node_Id;
1316 Comp_Typ : Entity_Id;
1317 Init_Expr : Node_Id;
1318 Stmts : List_Id)
1320 Act_Aggr : Node_Id;
1321 Act_Stmts : List_Id;
1322 Expr : Node_Id;
1323 Fin_Call : Node_Id;
1324 Hook_Clear : Node_Id;
1326 In_Place_Expansion : Boolean;
1327 -- Flag set when a nonlimited controlled function call requires
1328 -- in-place expansion.
1330 begin
1331 -- Duplicate the initialization expression in case the context is
1332 -- a multi choice list or an "others" choice which plugs various
1333 -- holes in the aggregate. As a result the expression is no longer
1334 -- shared between the various components and is reevaluated for
1335 -- each such component.
1337 Expr := New_Copy_Tree (Init_Expr);
1338 Set_Parent (Expr, Parent (Init_Expr));
1340 -- Perform a preliminary analysis and resolution to determine what
1341 -- the initialization expression denotes. An unanalyzed function
1342 -- call may appear as an identifier or an indexed component.
1344 if Nkind_In (Expr, N_Function_Call,
1345 N_Identifier,
1346 N_Indexed_Component)
1347 and then not Analyzed (Expr)
1348 then
1349 Preanalyze_And_Resolve (Expr, Comp_Typ);
1350 end if;
1352 In_Place_Expansion :=
1353 Nkind (Expr) = N_Function_Call
1354 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
1356 -- The initialization expression is a controlled function call.
1357 -- Perform in-place removal of side effects to avoid creating a
1358 -- transient scope, which leads to premature finalization.
1360 -- This in-place expansion is not performed for limited transient
1361 -- objects because the initialization is already done in-place.
1363 if In_Place_Expansion then
1365 -- Suppress the removal of side effects by general analysis
1366 -- because this behavior is emulated here. This avoids the
1367 -- generation of a transient scope, which leads to out-of-order
1368 -- adjustment and finalization.
1370 Set_No_Side_Effect_Removal (Expr);
1372 -- When the transient component initialization is related to a
1373 -- range or an "others", keep all generated statements within
1374 -- the enclosing loop. This way the controlled function call
1375 -- will be evaluated at each iteration, and its result will be
1376 -- finalized at the end of each iteration.
1378 if In_Loop then
1379 Act_Aggr := Empty;
1380 Act_Stmts := Stmts;
1382 -- Otherwise this is a single component initialization. Hook-
1383 -- related statements are inserted prior to the aggregate.
1385 else
1386 Act_Aggr := N;
1387 Act_Stmts := No_List;
1388 end if;
1390 -- Install all hook-related declarations and prepare the clean
1391 -- up statements.
1393 Process_Transient_Component
1394 (Loc => Loc,
1395 Comp_Typ => Comp_Typ,
1396 Init_Expr => Expr,
1397 Fin_Call => Fin_Call,
1398 Hook_Clear => Hook_Clear,
1399 Aggr => Act_Aggr,
1400 Stmts => Act_Stmts);
1401 end if;
1403 -- Use the noncontrolled component initialization circuitry to
1404 -- assign the result of the function call to the array element.
1405 -- This also performs subaggregate wrapping, tag adjustment, and
1406 -- [deep] adjustment of the array element.
1408 Initialize_Array_Component
1409 (Arr_Comp => Arr_Comp,
1410 Comp_Typ => Comp_Typ,
1411 Init_Expr => Expr,
1412 Stmts => Stmts);
1414 -- At this point the array element is fully initialized. Complete
1415 -- the processing of the controlled array component by finalizing
1416 -- the transient function result.
1418 if In_Place_Expansion then
1419 Process_Transient_Component_Completion
1420 (Loc => Loc,
1421 Aggr => N,
1422 Fin_Call => Fin_Call,
1423 Hook_Clear => Hook_Clear,
1424 Stmts => Stmts);
1425 end if;
1426 end Initialize_Ctrl_Array_Component;
1428 -- Local variables
1430 Stmts : constant List_Id := New_List;
1432 Comp_Typ : Entity_Id := Empty;
1433 Expr_Q : Node_Id;
1434 Indexed_Comp : Node_Id;
1435 Init_Call : Node_Id;
1436 New_Indexes : List_Id;
1438 -- Start of processing for Gen_Assign
1440 begin
1441 if No (Indexes) then
1442 New_Indexes := New_List;
1443 else
1444 New_Indexes := New_Copy_List_Tree (Indexes);
1445 end if;
1447 Append_To (New_Indexes, Ind);
1449 if Present (Next_Index (Index)) then
1450 return
1451 Add_Loop_Actions (
1452 Build_Array_Aggr_Code
1453 (N => Expr,
1454 Ctype => Ctype,
1455 Index => Next_Index (Index),
1456 Into => Into,
1457 Scalar_Comp => Scalar_Comp,
1458 Indexes => New_Indexes));
1459 end if;
1461 -- If we get here then we are at a bottom-level (sub-)aggregate
1463 Indexed_Comp :=
1464 Checks_Off
1465 (Make_Indexed_Component (Loc,
1466 Prefix => New_Copy_Tree (Into),
1467 Expressions => New_Indexes));
1469 Set_Assignment_OK (Indexed_Comp);
1471 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1472 -- is not present (and therefore we also initialize Expr_Q to empty).
1474 if No (Expr) then
1475 Expr_Q := Empty;
1476 elsif Nkind (Expr) = N_Qualified_Expression then
1477 Expr_Q := Expression (Expr);
1478 else
1479 Expr_Q := Expr;
1480 end if;
1482 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1483 Comp_Typ := Component_Type (Etype (N));
1484 pragma Assert (Comp_Typ = Ctype); -- AI-287
1486 elsif Present (Next (First (New_Indexes))) then
1488 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1489 -- component because we have received the component type in
1490 -- the formal parameter Ctype.
1492 -- ??? Some assert pragmas have been added to check if this new
1493 -- formal can be used to replace this code in all cases.
1495 if Present (Expr) then
1497 -- This is a multidimensional array. Recover the component type
1498 -- from the outermost aggregate, because subaggregates do not
1499 -- have an assigned type.
1501 declare
1502 P : Node_Id;
1504 begin
1505 P := Parent (Expr);
1506 while Present (P) loop
1507 if Nkind (P) = N_Aggregate
1508 and then Present (Etype (P))
1509 then
1510 Comp_Typ := Component_Type (Etype (P));
1511 exit;
1513 else
1514 P := Parent (P);
1515 end if;
1516 end loop;
1518 pragma Assert (Comp_Typ = Ctype); -- AI-287
1519 end;
1520 end if;
1521 end if;
1523 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1524 -- default initialized components (otherwise Expr_Q is not present).
1526 if Present (Expr_Q)
1527 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1528 then
1529 -- At this stage the Expression may not have been analyzed yet
1530 -- because the array aggregate code has not been updated to use
1531 -- the Expansion_Delayed flag and avoid analysis altogether to
1532 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1533 -- the analysis of non-array aggregates now in order to get the
1534 -- value of Expansion_Delayed flag for the inner aggregate ???
1536 -- In the case of an iterated component association, the analysis
1537 -- of the generated loop will analyze the expression in the
1538 -- proper context, in which the loop parameter is visible.
1540 if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ)
1541 and then
1542 Nkind (Parent (Expr_Q)) /= N_Iterated_Component_Association
1543 then
1544 Analyze_And_Resolve (Expr_Q, Comp_Typ);
1545 end if;
1547 if Is_Delayed_Aggregate (Expr_Q) then
1549 -- This is either a subaggregate of a multidimensional array,
1550 -- or a component of an array type whose component type is
1551 -- also an array. In the latter case, the expression may have
1552 -- component associations that provide different bounds from
1553 -- those of the component type, and sliding must occur. Instead
1554 -- of decomposing the current aggregate assignment, force the
1555 -- reanalysis of the assignment, so that a temporary will be
1556 -- generated in the usual fashion, and sliding will take place.
1558 if Nkind (Parent (N)) = N_Assignment_Statement
1559 and then Is_Array_Type (Comp_Typ)
1560 and then Present (Component_Associations (Expr_Q))
1561 and then Must_Slide (Comp_Typ, Etype (Expr_Q))
1562 then
1563 Set_Expansion_Delayed (Expr_Q, False);
1564 Set_Analyzed (Expr_Q, False);
1566 else
1567 return
1568 Add_Loop_Actions (
1569 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1570 end if;
1571 end if;
1572 end if;
1574 if Present (Expr) then
1576 -- Handle an initialization expression of a controlled type in
1577 -- case it denotes a function call. In general such a scenario
1578 -- will produce a transient scope, but this will lead to wrong
1579 -- order of initialization, adjustment, and finalization in the
1580 -- context of aggregates.
1582 -- Target (1) := Ctrl_Func_Call;
1584 -- begin -- scope
1585 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1586 -- Target (1) := Trans_Obj;
1587 -- Finalize (Trans_Obj);
1588 -- end;
1589 -- Target (1)._tag := ...;
1590 -- Adjust (Target (1));
1592 -- In the example above, the call to Finalize occurs too early
1593 -- and as a result it may leave the array component in a bad
1594 -- state. Finalization of the transient object should really
1595 -- happen after adjustment.
1597 -- To avoid this scenario, perform in-place side-effect removal
1598 -- of the function call. This eliminates the transient property
1599 -- of the function result and ensures correct order of actions.
1601 -- Res : ... := Ctrl_Func_Call;
1602 -- Target (1) := Res;
1603 -- Target (1)._tag := ...;
1604 -- Adjust (Target (1));
1605 -- Finalize (Res);
1607 if Present (Comp_Typ)
1608 and then Needs_Finalization (Comp_Typ)
1609 and then Nkind (Expr) /= N_Aggregate
1610 then
1611 Initialize_Ctrl_Array_Component
1612 (Arr_Comp => Indexed_Comp,
1613 Comp_Typ => Comp_Typ,
1614 Init_Expr => Expr,
1615 Stmts => Stmts);
1617 -- Otherwise perform simple component initialization
1619 else
1620 Initialize_Array_Component
1621 (Arr_Comp => Indexed_Comp,
1622 Comp_Typ => Comp_Typ,
1623 Init_Expr => Expr,
1624 Stmts => Stmts);
1625 end if;
1627 -- Ada 2005 (AI-287): In case of default initialized component, call
1628 -- the initialization subprogram associated with the component type.
1629 -- If the component type is an access type, add an explicit null
1630 -- assignment, because for the back-end there is an initialization
1631 -- present for the whole aggregate, and no default initialization
1632 -- will take place.
1634 -- In addition, if the component type is controlled, we must call
1635 -- its Initialize procedure explicitly, because there is no explicit
1636 -- object creation that will invoke it otherwise.
1638 else
1639 if Present (Base_Init_Proc (Base_Type (Ctype)))
1640 or else Has_Task (Base_Type (Ctype))
1641 then
1642 Append_List_To (Stmts,
1643 Build_Initialization_Call (Loc,
1644 Id_Ref => Indexed_Comp,
1645 Typ => Ctype,
1646 With_Default_Init => True));
1648 -- If the component type has invariants, add an invariant
1649 -- check after the component is default-initialized. It will
1650 -- be analyzed and resolved before the code for initialization
1651 -- of other components.
1653 if Has_Invariants (Ctype) then
1654 Set_Etype (Indexed_Comp, Ctype);
1655 Append_To (Stmts, Make_Invariant_Call (Indexed_Comp));
1656 end if;
1658 elsif Is_Access_Type (Ctype) then
1659 Append_To (Stmts,
1660 Make_Assignment_Statement (Loc,
1661 Name => New_Copy_Tree (Indexed_Comp),
1662 Expression => Make_Null (Loc)));
1663 end if;
1665 if Needs_Finalization (Ctype) then
1666 Init_Call :=
1667 Make_Init_Call
1668 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1669 Typ => Ctype);
1671 -- Guard against a missing [Deep_]Initialize when the component
1672 -- type was not properly frozen.
1674 if Present (Init_Call) then
1675 Append_To (Stmts, Init_Call);
1676 end if;
1677 end if;
1678 end if;
1680 return Add_Loop_Actions (Stmts);
1681 end Gen_Assign;
1683 --------------
1684 -- Gen_Loop --
1685 --------------
1687 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1688 Is_Iterated_Component : constant Boolean :=
1689 Nkind (Parent (Expr)) = N_Iterated_Component_Association;
1691 L_J : Node_Id;
1693 L_L : Node_Id;
1694 -- Index_Base'(L)
1696 L_H : Node_Id;
1697 -- Index_Base'(H)
1699 L_Range : Node_Id;
1700 -- Index_Base'(L) .. Index_Base'(H)
1702 L_Iteration_Scheme : Node_Id;
1703 -- L_J in Index_Base'(L) .. Index_Base'(H)
1705 L_Body : List_Id;
1706 -- The statements to execute in the loop
1708 S : constant List_Id := New_List;
1709 -- List of statements
1711 Tcopy : Node_Id;
1712 -- Copy of expression tree, used for checking purposes
1714 begin
1715 -- If loop bounds define an empty range return the null statement
1717 if Empty_Range (L, H) then
1718 Append_To (S, Make_Null_Statement (Loc));
1720 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1721 -- default initialized component.
1723 if No (Expr) then
1724 null;
1726 else
1727 -- The expression must be type-checked even though no component
1728 -- of the aggregate will have this value. This is done only for
1729 -- actual components of the array, not for subaggregates. Do
1730 -- the check on a copy, because the expression may be shared
1731 -- among several choices, some of which might be non-null.
1733 if Present (Etype (N))
1734 and then Is_Array_Type (Etype (N))
1735 and then No (Next_Index (Index))
1736 then
1737 Expander_Mode_Save_And_Set (False);
1738 Tcopy := New_Copy_Tree (Expr);
1739 Set_Parent (Tcopy, N);
1740 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1741 Expander_Mode_Restore;
1742 end if;
1743 end if;
1745 return S;
1747 -- If loop bounds are the same then generate an assignment, unless
1748 -- the parent construct is an Iterated_Component_Association.
1750 elsif Equal (L, H) and then not Is_Iterated_Component then
1751 return Gen_Assign (New_Copy_Tree (L), Expr);
1753 -- If H - L <= 2 then generate a sequence of assignments when we are
1754 -- processing the bottom most aggregate and it contains scalar
1755 -- components.
1757 elsif No (Next_Index (Index))
1758 and then Scalar_Comp
1759 and then Local_Compile_Time_Known_Value (L)
1760 and then Local_Compile_Time_Known_Value (H)
1761 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1762 and then not Is_Iterated_Component
1763 then
1764 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1765 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1767 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1768 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1769 end if;
1771 return S;
1772 end if;
1774 -- Otherwise construct the loop, starting with the loop index L_J
1776 if Is_Iterated_Component then
1777 L_J :=
1778 Make_Defining_Identifier (Loc,
1779 Chars => (Chars (Defining_Identifier (Parent (Expr)))));
1781 else
1782 L_J := Make_Temporary (Loc, 'J', L);
1783 end if;
1785 -- Construct "L .. H" in Index_Base. We use a qualified expression
1786 -- for the bound to convert to the index base, but we don't need
1787 -- to do that if we already have the base type at hand.
1789 if Etype (L) = Index_Base then
1790 L_L := L;
1791 else
1792 L_L :=
1793 Make_Qualified_Expression (Loc,
1794 Subtype_Mark => Index_Base_Name,
1795 Expression => New_Copy_Tree (L));
1796 end if;
1798 if Etype (H) = Index_Base then
1799 L_H := H;
1800 else
1801 L_H :=
1802 Make_Qualified_Expression (Loc,
1803 Subtype_Mark => Index_Base_Name,
1804 Expression => New_Copy_Tree (H));
1805 end if;
1807 L_Range :=
1808 Make_Range (Loc,
1809 Low_Bound => L_L,
1810 High_Bound => L_H);
1812 -- Construct "for L_J in Index_Base range L .. H"
1814 L_Iteration_Scheme :=
1815 Make_Iteration_Scheme
1816 (Loc,
1817 Loop_Parameter_Specification =>
1818 Make_Loop_Parameter_Specification
1819 (Loc,
1820 Defining_Identifier => L_J,
1821 Discrete_Subtype_Definition => L_Range));
1823 -- Construct the statements to execute in the loop body
1825 L_Body :=
1826 Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr, In_Loop => True);
1828 -- Construct the final loop
1830 Append_To (S,
1831 Make_Implicit_Loop_Statement
1832 (Node => N,
1833 Identifier => Empty,
1834 Iteration_Scheme => L_Iteration_Scheme,
1835 Statements => L_Body));
1837 -- A small optimization: if the aggregate is initialized with a box
1838 -- and the component type has no initialization procedure, remove the
1839 -- useless empty loop.
1841 if Nkind (First (S)) = N_Loop_Statement
1842 and then Is_Empty_List (Statements (First (S)))
1843 then
1844 return New_List (Make_Null_Statement (Loc));
1845 else
1846 return S;
1847 end if;
1848 end Gen_Loop;
1850 ---------------
1851 -- Gen_While --
1852 ---------------
1854 -- The code built is
1856 -- W_J : Index_Base := L;
1857 -- while W_J < H loop
1858 -- W_J := Index_Base'Succ (W);
1859 -- L_Body;
1860 -- end loop;
1862 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1863 W_J : Node_Id;
1865 W_Decl : Node_Id;
1866 -- W_J : Base_Type := L;
1868 W_Iteration_Scheme : Node_Id;
1869 -- while W_J < H
1871 W_Index_Succ : Node_Id;
1872 -- Index_Base'Succ (J)
1874 W_Increment : Node_Id;
1875 -- W_J := Index_Base'Succ (W)
1877 W_Body : constant List_Id := New_List;
1878 -- The statements to execute in the loop
1880 S : constant List_Id := New_List;
1881 -- list of statement
1883 begin
1884 -- If loop bounds define an empty range or are equal return null
1886 if Empty_Range (L, H) or else Equal (L, H) then
1887 Append_To (S, Make_Null_Statement (Loc));
1888 return S;
1889 end if;
1891 -- Build the decl of W_J
1893 W_J := Make_Temporary (Loc, 'J', L);
1894 W_Decl :=
1895 Make_Object_Declaration
1896 (Loc,
1897 Defining_Identifier => W_J,
1898 Object_Definition => Index_Base_Name,
1899 Expression => L);
1901 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1902 -- that in this particular case L is a fresh Expr generated by
1903 -- Add which we are the only ones to use.
1905 Append_To (S, W_Decl);
1907 -- Construct " while W_J < H"
1909 W_Iteration_Scheme :=
1910 Make_Iteration_Scheme
1911 (Loc,
1912 Condition => Make_Op_Lt
1913 (Loc,
1914 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1915 Right_Opnd => New_Copy_Tree (H)));
1917 -- Construct the statements to execute in the loop body
1919 W_Index_Succ :=
1920 Make_Attribute_Reference
1921 (Loc,
1922 Prefix => Index_Base_Name,
1923 Attribute_Name => Name_Succ,
1924 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1926 W_Increment :=
1927 Make_OK_Assignment_Statement
1928 (Loc,
1929 Name => New_Occurrence_Of (W_J, Loc),
1930 Expression => W_Index_Succ);
1932 Append_To (W_Body, W_Increment);
1934 Append_List_To (W_Body,
1935 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr, In_Loop => True));
1937 -- Construct the final loop
1939 Append_To (S,
1940 Make_Implicit_Loop_Statement
1941 (Node => N,
1942 Identifier => Empty,
1943 Iteration_Scheme => W_Iteration_Scheme,
1944 Statements => W_Body));
1946 return S;
1947 end Gen_While;
1949 --------------------
1950 -- Get_Assoc_Expr --
1951 --------------------
1953 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1954 Typ : constant Entity_Id := Base_Type (Etype (N));
1956 begin
1957 if Box_Present (Assoc) then
1958 if Is_Scalar_Type (Ctype) then
1959 if Present (Default_Aspect_Component_Value (Typ)) then
1960 return Default_Aspect_Component_Value (Typ);
1961 elsif Present (Default_Aspect_Value (Ctype)) then
1962 return Default_Aspect_Value (Ctype);
1963 else
1964 return Empty;
1965 end if;
1967 else
1968 return Empty;
1969 end if;
1971 else
1972 return Expression (Assoc);
1973 end if;
1974 end Get_Assoc_Expr;
1976 ---------------------
1977 -- Index_Base_Name --
1978 ---------------------
1980 function Index_Base_Name return Node_Id is
1981 begin
1982 return New_Occurrence_Of (Index_Base, Sloc (N));
1983 end Index_Base_Name;
1985 ------------------------------------
1986 -- Local_Compile_Time_Known_Value --
1987 ------------------------------------
1989 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1990 begin
1991 return Compile_Time_Known_Value (E)
1992 or else
1993 (Nkind (E) = N_Attribute_Reference
1994 and then Attribute_Name (E) = Name_Val
1995 and then Compile_Time_Known_Value (First (Expressions (E))));
1996 end Local_Compile_Time_Known_Value;
1998 ----------------------
1999 -- Local_Expr_Value --
2000 ----------------------
2002 function Local_Expr_Value (E : Node_Id) return Uint is
2003 begin
2004 if Compile_Time_Known_Value (E) then
2005 return Expr_Value (E);
2006 else
2007 return Expr_Value (First (Expressions (E)));
2008 end if;
2009 end Local_Expr_Value;
2011 -- Local variables
2013 New_Code : constant List_Id := New_List;
2015 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
2016 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
2017 -- The aggregate bounds of this specific subaggregate. Note that if the
2018 -- code generated by Build_Array_Aggr_Code is executed then these bounds
2019 -- are OK. Otherwise a Constraint_Error would have been raised.
2021 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
2022 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
2023 -- After Duplicate_Subexpr these are side-effect free
2025 Assoc : Node_Id;
2026 Choice : Node_Id;
2027 Expr : Node_Id;
2028 High : Node_Id;
2029 Low : Node_Id;
2030 Typ : Entity_Id;
2032 Nb_Choices : Nat := 0;
2033 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
2034 -- Used to sort all the different choice values
2036 Nb_Elements : Int;
2037 -- Number of elements in the positional aggregate
2039 Others_Assoc : Node_Id := Empty;
2041 -- Start of processing for Build_Array_Aggr_Code
2043 begin
2044 -- First before we start, a special case. if we have a bit packed
2045 -- array represented as a modular type, then clear the value to
2046 -- zero first, to ensure that unused bits are properly cleared.
2048 Typ := Etype (N);
2050 if Present (Typ)
2051 and then Is_Bit_Packed_Array (Typ)
2052 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
2053 then
2054 Append_To (New_Code,
2055 Make_Assignment_Statement (Loc,
2056 Name => New_Copy_Tree (Into),
2057 Expression =>
2058 Unchecked_Convert_To (Typ,
2059 Make_Integer_Literal (Loc, Uint_0))));
2060 end if;
2062 -- If the component type contains tasks, we need to build a Master
2063 -- entity in the current scope, because it will be needed if build-
2064 -- in-place functions are called in the expanded code.
2066 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
2067 Build_Master_Entity (Defining_Identifier (Parent (N)));
2068 end if;
2070 -- STEP 1: Process component associations
2072 -- For those associations that may generate a loop, initialize
2073 -- Loop_Actions to collect inserted actions that may be crated.
2075 -- Skip this if no component associations
2077 if No (Expressions (N)) then
2079 -- STEP 1 (a): Sort the discrete choices
2081 Assoc := First (Component_Associations (N));
2082 while Present (Assoc) loop
2083 Choice := First (Choice_List (Assoc));
2084 while Present (Choice) loop
2085 if Nkind (Choice) = N_Others_Choice then
2086 Set_Loop_Actions (Assoc, New_List);
2087 Others_Assoc := Assoc;
2088 exit;
2089 end if;
2091 Get_Index_Bounds (Choice, Low, High);
2093 if Low /= High then
2094 Set_Loop_Actions (Assoc, New_List);
2095 end if;
2097 Nb_Choices := Nb_Choices + 1;
2099 Table (Nb_Choices) :=
2100 (Choice_Lo => Low,
2101 Choice_Hi => High,
2102 Choice_Node => Get_Assoc_Expr (Assoc));
2104 Next (Choice);
2105 end loop;
2107 Next (Assoc);
2108 end loop;
2110 -- If there is more than one set of choices these must be static
2111 -- and we can therefore sort them. Remember that Nb_Choices does not
2112 -- account for an others choice.
2114 if Nb_Choices > 1 then
2115 Sort_Case_Table (Table);
2116 end if;
2118 -- STEP 1 (b): take care of the whole set of discrete choices
2120 for J in 1 .. Nb_Choices loop
2121 Low := Table (J).Choice_Lo;
2122 High := Table (J).Choice_Hi;
2123 Expr := Table (J).Choice_Node;
2124 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2125 end loop;
2127 -- STEP 1 (c): generate the remaining loops to cover others choice
2128 -- We don't need to generate loops over empty gaps, but if there is
2129 -- a single empty range we must analyze the expression for semantics
2131 if Present (Others_Assoc) then
2132 declare
2133 First : Boolean := True;
2135 begin
2136 for J in 0 .. Nb_Choices loop
2137 if J = 0 then
2138 Low := Aggr_Low;
2139 else
2140 Low := Add (1, To => Table (J).Choice_Hi);
2141 end if;
2143 if J = Nb_Choices then
2144 High := Aggr_High;
2145 else
2146 High := Add (-1, To => Table (J + 1).Choice_Lo);
2147 end if;
2149 -- If this is an expansion within an init proc, make
2150 -- sure that discriminant references are replaced by
2151 -- the corresponding discriminal.
2153 if Inside_Init_Proc then
2154 if Is_Entity_Name (Low)
2155 and then Ekind (Entity (Low)) = E_Discriminant
2156 then
2157 Set_Entity (Low, Discriminal (Entity (Low)));
2158 end if;
2160 if Is_Entity_Name (High)
2161 and then Ekind (Entity (High)) = E_Discriminant
2162 then
2163 Set_Entity (High, Discriminal (Entity (High)));
2164 end if;
2165 end if;
2167 if First
2168 or else not Empty_Range (Low, High)
2169 then
2170 First := False;
2171 Append_List
2172 (Gen_Loop (Low, High,
2173 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
2174 end if;
2175 end loop;
2176 end;
2177 end if;
2179 -- STEP 2: Process positional components
2181 else
2182 -- STEP 2 (a): Generate the assignments for each positional element
2183 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2184 -- Aggr_L is analyzed and Add wants an analyzed expression.
2186 Expr := First (Expressions (N));
2187 Nb_Elements := -1;
2188 while Present (Expr) loop
2189 Nb_Elements := Nb_Elements + 1;
2190 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2191 To => New_Code);
2192 Next (Expr);
2193 end loop;
2195 -- STEP 2 (b): Generate final loop if an others choice is present
2196 -- Here Nb_Elements gives the offset of the last positional element.
2198 if Present (Component_Associations (N)) then
2199 Assoc := Last (Component_Associations (N));
2201 -- Ada 2005 (AI-287)
2203 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2204 Aggr_High,
2205 Get_Assoc_Expr (Assoc)), -- AI-287
2206 To => New_Code);
2207 end if;
2208 end if;
2210 return New_Code;
2211 end Build_Array_Aggr_Code;
2213 ----------------------------
2214 -- Build_Record_Aggr_Code --
2215 ----------------------------
2217 function Build_Record_Aggr_Code
2218 (N : Node_Id;
2219 Typ : Entity_Id;
2220 Lhs : Node_Id) return List_Id
2222 Loc : constant Source_Ptr := Sloc (N);
2223 L : constant List_Id := New_List;
2224 N_Typ : constant Entity_Id := Etype (N);
2226 Comp : Node_Id;
2227 Instr : Node_Id;
2228 Ref : Node_Id;
2229 Target : Entity_Id;
2230 Comp_Type : Entity_Id;
2231 Selector : Entity_Id;
2232 Comp_Expr : Node_Id;
2233 Expr_Q : Node_Id;
2235 -- If this is an internal aggregate, the External_Final_List is an
2236 -- expression for the controller record of the enclosing type.
2238 -- If the current aggregate has several controlled components, this
2239 -- expression will appear in several calls to attach to the finali-
2240 -- zation list, and it must not be shared.
2242 Ancestor_Is_Expression : Boolean := False;
2243 Ancestor_Is_Subtype_Mark : Boolean := False;
2245 Init_Typ : Entity_Id := Empty;
2247 Finalization_Done : Boolean := False;
2248 -- True if Generate_Finalization_Actions has already been called; calls
2249 -- after the first do nothing.
2251 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2252 -- Returns the value that the given discriminant of an ancestor type
2253 -- should receive (in the absence of a conflict with the value provided
2254 -- by an ancestor part of an extension aggregate).
2256 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2257 -- Check that each of the discriminant values defined by the ancestor
2258 -- part of an extension aggregate match the corresponding values
2259 -- provided by either an association of the aggregate or by the
2260 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2262 function Compatible_Int_Bounds
2263 (Agg_Bounds : Node_Id;
2264 Typ_Bounds : Node_Id) return Boolean;
2265 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2266 -- assumed that both bounds are integer ranges.
2268 procedure Generate_Finalization_Actions;
2269 -- Deal with the various controlled type data structure initializations
2270 -- (but only if it hasn't been done already).
2272 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2273 -- Returns the first discriminant association in the constraint
2274 -- associated with T, if any, otherwise returns Empty.
2276 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2277 -- If the ancestor part is an unconstrained type and further ancestors
2278 -- do not provide discriminants for it, check aggregate components for
2279 -- values of the discriminants.
2281 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2282 -- If Typ is derived, and constrains discriminants of the parent type,
2283 -- these discriminants are not components of the aggregate, and must be
2284 -- initialized. The assignments are appended to List. The same is done
2285 -- if Typ derives fron an already constrained subtype of a discriminated
2286 -- parent type.
2288 procedure Init_Stored_Discriminants;
2289 -- If the type is derived and has inherited discriminants, generate
2290 -- explicit assignments for each, using the store constraint of the
2291 -- type. Note that both visible and stored discriminants must be
2292 -- initialized in case the derived type has some renamed and some
2293 -- constrained discriminants.
2295 procedure Init_Visible_Discriminants;
2296 -- If type has discriminants, retrieve their values from aggregate,
2297 -- and generate explicit assignments for each. This does not include
2298 -- discriminants inherited from ancestor, which are handled above.
2299 -- The type of the aggregate is a subtype created ealier using the
2300 -- given values of the discriminant components of the aggregate.
2302 procedure Initialize_Ctrl_Record_Component
2303 (Rec_Comp : Node_Id;
2304 Comp_Typ : Entity_Id;
2305 Init_Expr : Node_Id;
2306 Stmts : List_Id);
2307 -- Perform the initialization of controlled record component Rec_Comp.
2308 -- Comp_Typ is the component type. Init_Expr is the initialization
2309 -- expression for the record component. Hook-related declarations are
2310 -- inserted prior to aggregate N using Insert_Action. All remaining
2311 -- generated code is added to list Stmts.
2313 procedure Initialize_Record_Component
2314 (Rec_Comp : Node_Id;
2315 Comp_Typ : Entity_Id;
2316 Init_Expr : Node_Id;
2317 Stmts : List_Id);
2318 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2319 -- is the component type. Init_Expr is the initialization expression
2320 -- of the record component. All generated code is added to list Stmts.
2322 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2323 -- Check whether Bounds is a range node and its lower and higher bounds
2324 -- are integers literals.
2326 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2327 -- If the aggregate contains a self-reference, traverse each expression
2328 -- to replace a possible self-reference with a reference to the proper
2329 -- component of the target of the assignment.
2331 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2332 -- If default expression of a component mentions a discriminant of the
2333 -- type, it must be rewritten as the discriminant of the target object.
2335 ---------------------------------
2336 -- Ancestor_Discriminant_Value --
2337 ---------------------------------
2339 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2340 Assoc : Node_Id;
2341 Assoc_Elmt : Elmt_Id;
2342 Aggr_Comp : Entity_Id;
2343 Corresp_Disc : Entity_Id;
2344 Current_Typ : Entity_Id := Base_Type (Typ);
2345 Parent_Typ : Entity_Id;
2346 Parent_Disc : Entity_Id;
2347 Save_Assoc : Node_Id := Empty;
2349 begin
2350 -- First check any discriminant associations to see if any of them
2351 -- provide a value for the discriminant.
2353 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2354 Assoc := First (Component_Associations (N));
2355 while Present (Assoc) loop
2356 Aggr_Comp := Entity (First (Choices (Assoc)));
2358 if Ekind (Aggr_Comp) = E_Discriminant then
2359 Save_Assoc := Expression (Assoc);
2361 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2362 while Present (Corresp_Disc) loop
2364 -- If found a corresponding discriminant then return the
2365 -- value given in the aggregate. (Note: this is not
2366 -- correct in the presence of side effects. ???)
2368 if Disc = Corresp_Disc then
2369 return Duplicate_Subexpr (Expression (Assoc));
2370 end if;
2372 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2373 end loop;
2374 end if;
2376 Next (Assoc);
2377 end loop;
2378 end if;
2380 -- No match found in aggregate, so chain up parent types to find
2381 -- a constraint that defines the value of the discriminant.
2383 Parent_Typ := Etype (Current_Typ);
2384 while Current_Typ /= Parent_Typ loop
2385 if Has_Discriminants (Parent_Typ)
2386 and then not Has_Unknown_Discriminants (Parent_Typ)
2387 then
2388 Parent_Disc := First_Discriminant (Parent_Typ);
2390 -- We either get the association from the subtype indication
2391 -- of the type definition itself, or from the discriminant
2392 -- constraint associated with the type entity (which is
2393 -- preferable, but it's not always present ???)
2395 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2396 then
2397 Assoc := Get_Constraint_Association (Current_Typ);
2398 Assoc_Elmt := No_Elmt;
2399 else
2400 Assoc_Elmt :=
2401 First_Elmt (Discriminant_Constraint (Current_Typ));
2402 Assoc := Node (Assoc_Elmt);
2403 end if;
2405 -- Traverse the discriminants of the parent type looking
2406 -- for one that corresponds.
2408 while Present (Parent_Disc) and then Present (Assoc) loop
2409 Corresp_Disc := Parent_Disc;
2410 while Present (Corresp_Disc)
2411 and then Disc /= Corresp_Disc
2412 loop
2413 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2414 end loop;
2416 if Disc = Corresp_Disc then
2417 if Nkind (Assoc) = N_Discriminant_Association then
2418 Assoc := Expression (Assoc);
2419 end if;
2421 -- If the located association directly denotes
2422 -- a discriminant, then use the value of a saved
2423 -- association of the aggregate. This is an approach
2424 -- used to handle certain cases involving multiple
2425 -- discriminants mapped to a single discriminant of
2426 -- a descendant. It's not clear how to locate the
2427 -- appropriate discriminant value for such cases. ???
2429 if Is_Entity_Name (Assoc)
2430 and then Ekind (Entity (Assoc)) = E_Discriminant
2431 then
2432 Assoc := Save_Assoc;
2433 end if;
2435 return Duplicate_Subexpr (Assoc);
2436 end if;
2438 Next_Discriminant (Parent_Disc);
2440 if No (Assoc_Elmt) then
2441 Next (Assoc);
2443 else
2444 Next_Elmt (Assoc_Elmt);
2446 if Present (Assoc_Elmt) then
2447 Assoc := Node (Assoc_Elmt);
2448 else
2449 Assoc := Empty;
2450 end if;
2451 end if;
2452 end loop;
2453 end if;
2455 Current_Typ := Parent_Typ;
2456 Parent_Typ := Etype (Current_Typ);
2457 end loop;
2459 -- In some cases there's no ancestor value to locate (such as
2460 -- when an ancestor part given by an expression defines the
2461 -- discriminant value).
2463 return Empty;
2464 end Ancestor_Discriminant_Value;
2466 ----------------------------------
2467 -- Check_Ancestor_Discriminants --
2468 ----------------------------------
2470 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2471 Discr : Entity_Id;
2472 Disc_Value : Node_Id;
2473 Cond : Node_Id;
2475 begin
2476 Discr := First_Discriminant (Base_Type (Anc_Typ));
2477 while Present (Discr) loop
2478 Disc_Value := Ancestor_Discriminant_Value (Discr);
2480 if Present (Disc_Value) then
2481 Cond := Make_Op_Ne (Loc,
2482 Left_Opnd =>
2483 Make_Selected_Component (Loc,
2484 Prefix => New_Copy_Tree (Target),
2485 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2486 Right_Opnd => Disc_Value);
2488 Append_To (L,
2489 Make_Raise_Constraint_Error (Loc,
2490 Condition => Cond,
2491 Reason => CE_Discriminant_Check_Failed));
2492 end if;
2494 Next_Discriminant (Discr);
2495 end loop;
2496 end Check_Ancestor_Discriminants;
2498 ---------------------------
2499 -- Compatible_Int_Bounds --
2500 ---------------------------
2502 function Compatible_Int_Bounds
2503 (Agg_Bounds : Node_Id;
2504 Typ_Bounds : Node_Id) return Boolean
2506 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2507 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2508 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2509 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2510 begin
2511 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2512 end Compatible_Int_Bounds;
2514 -----------------------------------
2515 -- Generate_Finalization_Actions --
2516 -----------------------------------
2518 procedure Generate_Finalization_Actions is
2519 begin
2520 -- Do the work only the first time this is called
2522 if Finalization_Done then
2523 return;
2524 end if;
2526 Finalization_Done := True;
2528 -- Determine the external finalization list. It is either the
2529 -- finalization list of the outer scope or the one coming from an
2530 -- outer aggregate. When the target is not a temporary, the proper
2531 -- scope is the scope of the target rather than the potentially
2532 -- transient current scope.
2534 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2535 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2536 Set_Assignment_OK (Ref);
2538 Append_To (L,
2539 Make_Procedure_Call_Statement (Loc,
2540 Name =>
2541 New_Occurrence_Of
2542 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2543 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2544 end if;
2545 end Generate_Finalization_Actions;
2547 --------------------------------
2548 -- Get_Constraint_Association --
2549 --------------------------------
2551 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2552 Indic : Node_Id;
2553 Typ : Entity_Id;
2555 begin
2556 Typ := T;
2558 -- If type is private, get constraint from full view. This was
2559 -- previously done in an instance context, but is needed whenever
2560 -- the ancestor part has a discriminant, possibly inherited through
2561 -- multiple derivations.
2563 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2564 Typ := Full_View (Typ);
2565 end if;
2567 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2569 -- Verify that the subtype indication carries a constraint
2571 if Nkind (Indic) = N_Subtype_Indication
2572 and then Present (Constraint (Indic))
2573 then
2574 return First (Constraints (Constraint (Indic)));
2575 end if;
2577 return Empty;
2578 end Get_Constraint_Association;
2580 -------------------------------------
2581 -- Get_Explicit_Discriminant_Value --
2582 -------------------------------------
2584 function Get_Explicit_Discriminant_Value
2585 (D : Entity_Id) return Node_Id
2587 Assoc : Node_Id;
2588 Choice : Node_Id;
2589 Val : Node_Id;
2591 begin
2592 -- The aggregate has been normalized and all associations have a
2593 -- single choice.
2595 Assoc := First (Component_Associations (N));
2596 while Present (Assoc) loop
2597 Choice := First (Choices (Assoc));
2599 if Chars (Choice) = Chars (D) then
2600 Val := Expression (Assoc);
2601 Remove (Assoc);
2602 return Val;
2603 end if;
2605 Next (Assoc);
2606 end loop;
2608 return Empty;
2609 end Get_Explicit_Discriminant_Value;
2611 -------------------------------
2612 -- Init_Hidden_Discriminants --
2613 -------------------------------
2615 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2616 function Is_Completely_Hidden_Discriminant
2617 (Discr : Entity_Id) return Boolean;
2618 -- Determine whether Discr is a completely hidden discriminant of
2619 -- type Typ.
2621 ---------------------------------------
2622 -- Is_Completely_Hidden_Discriminant --
2623 ---------------------------------------
2625 function Is_Completely_Hidden_Discriminant
2626 (Discr : Entity_Id) return Boolean
2628 Item : Entity_Id;
2630 begin
2631 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2632 -- completely hidden discriminants.
2634 Item := First_Entity (Typ);
2635 while Present (Item) loop
2636 if Ekind (Item) = E_Discriminant
2637 and then Is_Completely_Hidden (Item)
2638 and then Chars (Original_Record_Component (Item)) =
2639 Chars (Discr)
2640 then
2641 return True;
2642 end if;
2644 Next_Entity (Item);
2645 end loop;
2647 return False;
2648 end Is_Completely_Hidden_Discriminant;
2650 -- Local variables
2652 Base_Typ : Entity_Id;
2653 Discr : Entity_Id;
2654 Discr_Constr : Elmt_Id;
2655 Discr_Init : Node_Id;
2656 Discr_Val : Node_Id;
2657 In_Aggr_Type : Boolean;
2658 Par_Typ : Entity_Id;
2660 -- Start of processing for Init_Hidden_Discriminants
2662 begin
2663 -- The constraints on the hidden discriminants, if present, are kept
2664 -- in the Stored_Constraint list of the type itself, or in that of
2665 -- the base type. If not in the constraints of the aggregate itself,
2666 -- we examine ancestors to find discriminants that are not renamed
2667 -- by other discriminants but constrained explicitly.
2669 In_Aggr_Type := True;
2671 Base_Typ := Base_Type (Typ);
2672 while Is_Derived_Type (Base_Typ)
2673 and then
2674 (Present (Stored_Constraint (Base_Typ))
2675 or else
2676 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2677 loop
2678 Par_Typ := Etype (Base_Typ);
2680 if not Has_Discriminants (Par_Typ) then
2681 return;
2682 end if;
2684 Discr := First_Discriminant (Par_Typ);
2686 -- We know that one of the stored-constraint lists is present
2688 if Present (Stored_Constraint (Base_Typ)) then
2689 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2691 -- For private extension, stored constraint may be on full view
2693 elsif Is_Private_Type (Base_Typ)
2694 and then Present (Full_View (Base_Typ))
2695 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2696 then
2697 Discr_Constr :=
2698 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2700 else
2701 Discr_Constr := First_Elmt (Stored_Constraint (Typ));
2702 end if;
2704 while Present (Discr) and then Present (Discr_Constr) loop
2705 Discr_Val := Node (Discr_Constr);
2707 -- The parent discriminant is renamed in the derived type,
2708 -- nothing to initialize.
2710 -- type Deriv_Typ (Discr : ...)
2711 -- is new Parent_Typ (Discr => Discr);
2713 if Is_Entity_Name (Discr_Val)
2714 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2715 then
2716 null;
2718 -- When the parent discriminant is constrained at the type
2719 -- extension level, it does not appear in the derived type.
2721 -- type Deriv_Typ (Discr : ...)
2722 -- is new Parent_Typ (Discr => Discr,
2723 -- Hidden_Discr => Expression);
2725 elsif Is_Completely_Hidden_Discriminant (Discr) then
2726 null;
2728 -- Otherwise initialize the discriminant
2730 else
2731 Discr_Init :=
2732 Make_OK_Assignment_Statement (Loc,
2733 Name =>
2734 Make_Selected_Component (Loc,
2735 Prefix => New_Copy_Tree (Target),
2736 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2737 Expression => New_Copy_Tree (Discr_Val));
2739 Append_To (List, Discr_Init);
2740 end if;
2742 Next_Elmt (Discr_Constr);
2743 Next_Discriminant (Discr);
2744 end loop;
2746 In_Aggr_Type := False;
2747 Base_Typ := Base_Type (Par_Typ);
2748 end loop;
2749 end Init_Hidden_Discriminants;
2751 --------------------------------
2752 -- Init_Visible_Discriminants --
2753 --------------------------------
2755 procedure Init_Visible_Discriminants is
2756 Discriminant : Entity_Id;
2757 Discriminant_Value : Node_Id;
2759 begin
2760 Discriminant := First_Discriminant (Typ);
2761 while Present (Discriminant) loop
2762 Comp_Expr :=
2763 Make_Selected_Component (Loc,
2764 Prefix => New_Copy_Tree (Target),
2765 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2767 Discriminant_Value :=
2768 Get_Discriminant_Value
2769 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2771 Instr :=
2772 Make_OK_Assignment_Statement (Loc,
2773 Name => Comp_Expr,
2774 Expression => New_Copy_Tree (Discriminant_Value));
2776 Append_To (L, Instr);
2778 Next_Discriminant (Discriminant);
2779 end loop;
2780 end Init_Visible_Discriminants;
2782 -------------------------------
2783 -- Init_Stored_Discriminants --
2784 -------------------------------
2786 procedure Init_Stored_Discriminants is
2787 Discriminant : Entity_Id;
2788 Discriminant_Value : Node_Id;
2790 begin
2791 Discriminant := First_Stored_Discriminant (Typ);
2792 while Present (Discriminant) loop
2793 Comp_Expr :=
2794 Make_Selected_Component (Loc,
2795 Prefix => New_Copy_Tree (Target),
2796 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2798 Discriminant_Value :=
2799 Get_Discriminant_Value
2800 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2802 Instr :=
2803 Make_OK_Assignment_Statement (Loc,
2804 Name => Comp_Expr,
2805 Expression => New_Copy_Tree (Discriminant_Value));
2807 Append_To (L, Instr);
2809 Next_Stored_Discriminant (Discriminant);
2810 end loop;
2811 end Init_Stored_Discriminants;
2813 --------------------------------------
2814 -- Initialize_Ctrl_Record_Component --
2815 --------------------------------------
2817 procedure Initialize_Ctrl_Record_Component
2818 (Rec_Comp : Node_Id;
2819 Comp_Typ : Entity_Id;
2820 Init_Expr : Node_Id;
2821 Stmts : List_Id)
2823 Fin_Call : Node_Id;
2824 Hook_Clear : Node_Id;
2826 In_Place_Expansion : Boolean;
2827 -- Flag set when a nonlimited controlled function call requires
2828 -- in-place expansion.
2830 begin
2831 -- Perform a preliminary analysis and resolution to determine what
2832 -- the initialization expression denotes. Unanalyzed function calls
2833 -- may appear as identifiers or indexed components.
2835 if Nkind_In (Init_Expr, N_Function_Call,
2836 N_Identifier,
2837 N_Indexed_Component)
2838 and then not Analyzed (Init_Expr)
2839 then
2840 Preanalyze_And_Resolve (Init_Expr, Comp_Typ);
2841 end if;
2843 In_Place_Expansion :=
2844 Nkind (Init_Expr) = N_Function_Call
2845 and then not Is_Build_In_Place_Result_Type (Comp_Typ);
2847 -- The initialization expression is a controlled function call.
2848 -- Perform in-place removal of side effects to avoid creating a
2849 -- transient scope.
2851 -- This in-place expansion is not performed for limited transient
2852 -- objects because the initialization is already done in place.
2854 if In_Place_Expansion then
2856 -- Suppress the removal of side effects by general analysis
2857 -- because this behavior is emulated here. This avoids the
2858 -- generation of a transient scope, which leads to out-of-order
2859 -- adjustment and finalization.
2861 Set_No_Side_Effect_Removal (Init_Expr);
2863 -- Install all hook-related declarations and prepare the clean up
2864 -- statements.
2866 Process_Transient_Component
2867 (Loc => Loc,
2868 Comp_Typ => Comp_Typ,
2869 Init_Expr => Init_Expr,
2870 Fin_Call => Fin_Call,
2871 Hook_Clear => Hook_Clear,
2872 Aggr => N);
2873 end if;
2875 -- Use the noncontrolled component initialization circuitry to
2876 -- assign the result of the function call to the record component.
2877 -- This also performs tag adjustment and [deep] adjustment of the
2878 -- record component.
2880 Initialize_Record_Component
2881 (Rec_Comp => Rec_Comp,
2882 Comp_Typ => Comp_Typ,
2883 Init_Expr => Init_Expr,
2884 Stmts => Stmts);
2886 -- At this point the record component is fully initialized. Complete
2887 -- the processing of the controlled record component by finalizing
2888 -- the transient function result.
2890 if In_Place_Expansion then
2891 Process_Transient_Component_Completion
2892 (Loc => Loc,
2893 Aggr => N,
2894 Fin_Call => Fin_Call,
2895 Hook_Clear => Hook_Clear,
2896 Stmts => Stmts);
2897 end if;
2898 end Initialize_Ctrl_Record_Component;
2900 ---------------------------------
2901 -- Initialize_Record_Component --
2902 ---------------------------------
2904 procedure Initialize_Record_Component
2905 (Rec_Comp : Node_Id;
2906 Comp_Typ : Entity_Id;
2907 Init_Expr : Node_Id;
2908 Stmts : List_Id)
2910 Exceptions_OK : constant Boolean :=
2911 not Restriction_Active (No_Exception_Propagation);
2913 Finalization_OK : constant Boolean := Needs_Finalization (Comp_Typ);
2915 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
2916 Adj_Call : Node_Id;
2917 Blk_Stmts : List_Id;
2918 Init_Stmt : Node_Id;
2920 begin
2921 -- Protect the initialization statements from aborts. Generate:
2923 -- Abort_Defer;
2925 if Finalization_OK and Abort_Allowed then
2926 if Exceptions_OK then
2927 Blk_Stmts := New_List;
2928 else
2929 Blk_Stmts := Stmts;
2930 end if;
2932 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
2934 -- Otherwise aborts are not allowed. All generated code is added
2935 -- directly to the input list.
2937 else
2938 Blk_Stmts := Stmts;
2939 end if;
2941 -- Initialize the record component. Generate:
2943 -- Rec_Comp := Init_Expr;
2945 -- Note that the initialization expression is NOT replicated because
2946 -- only a single component may be initialized by it.
2948 Init_Stmt :=
2949 Make_OK_Assignment_Statement (Loc,
2950 Name => New_Copy_Tree (Rec_Comp),
2951 Expression => Init_Expr);
2952 Set_No_Ctrl_Actions (Init_Stmt);
2954 Append_To (Blk_Stmts, Init_Stmt);
2956 -- Adjust the tag due to a possible view conversion. Generate:
2958 -- Rec_Comp._tag := Full_TypeP;
2960 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
2961 Append_To (Blk_Stmts,
2962 Make_OK_Assignment_Statement (Loc,
2963 Name =>
2964 Make_Selected_Component (Loc,
2965 Prefix => New_Copy_Tree (Rec_Comp),
2966 Selector_Name =>
2967 New_Occurrence_Of
2968 (First_Tag_Component (Full_Typ), Loc)),
2970 Expression =>
2971 Unchecked_Convert_To (RTE (RE_Tag),
2972 New_Occurrence_Of
2973 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
2974 Loc))));
2975 end if;
2977 -- Adjust the component. Generate:
2979 -- [Deep_]Adjust (Rec_Comp);
2981 if Finalization_OK
2982 and then not Is_Limited_Type (Comp_Typ)
2983 and then not Is_Build_In_Place_Function_Call (Init_Expr)
2984 then
2985 Adj_Call :=
2986 Make_Adjust_Call
2987 (Obj_Ref => New_Copy_Tree (Rec_Comp),
2988 Typ => Comp_Typ);
2990 -- Guard against a missing [Deep_]Adjust when the component type
2991 -- was not properly frozen.
2993 if Present (Adj_Call) then
2994 Append_To (Blk_Stmts, Adj_Call);
2995 end if;
2996 end if;
2998 -- Complete the protection of the initialization statements
3000 if Finalization_OK and Abort_Allowed then
3002 -- Wrap the initialization statements in a block to catch a
3003 -- potential exception. Generate:
3005 -- begin
3006 -- Abort_Defer;
3007 -- Rec_Comp := Init_Expr;
3008 -- Rec_Comp._tag := Full_TypP;
3009 -- [Deep_]Adjust (Rec_Comp);
3010 -- at end
3011 -- Abort_Undefer_Direct;
3012 -- end;
3014 if Exceptions_OK then
3015 Append_To (Stmts,
3016 Build_Abort_Undefer_Block (Loc,
3017 Stmts => Blk_Stmts,
3018 Context => N));
3020 -- Otherwise exceptions are not propagated. Generate:
3022 -- Abort_Defer;
3023 -- Rec_Comp := Init_Expr;
3024 -- Rec_Comp._tag := Full_TypP;
3025 -- [Deep_]Adjust (Rec_Comp);
3026 -- Abort_Undefer;
3028 else
3029 Append_To (Blk_Stmts,
3030 Build_Runtime_Call (Loc, RE_Abort_Undefer));
3031 end if;
3032 end if;
3033 end Initialize_Record_Component;
3035 -------------------------
3036 -- Is_Int_Range_Bounds --
3037 -------------------------
3039 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
3040 begin
3041 return Nkind (Bounds) = N_Range
3042 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
3043 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
3044 end Is_Int_Range_Bounds;
3046 ------------------
3047 -- Replace_Type --
3048 ------------------
3050 function Replace_Type (Expr : Node_Id) return Traverse_Result is
3051 begin
3052 -- Note regarding the Root_Type test below: Aggregate components for
3053 -- self-referential types include attribute references to the current
3054 -- instance, of the form: Typ'access, etc.. These references are
3055 -- rewritten as references to the target of the aggregate: the
3056 -- left-hand side of an assignment, the entity in a declaration,
3057 -- or a temporary. Without this test, we would improperly extended
3058 -- this rewriting to attribute references whose prefix was not the
3059 -- type of the aggregate.
3061 if Nkind (Expr) = N_Attribute_Reference
3062 and then Is_Entity_Name (Prefix (Expr))
3063 and then Is_Type (Entity (Prefix (Expr)))
3064 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
3065 then
3066 if Is_Entity_Name (Lhs) then
3067 Rewrite (Prefix (Expr), New_Occurrence_Of (Entity (Lhs), Loc));
3069 else
3070 Rewrite (Expr,
3071 Make_Attribute_Reference (Loc,
3072 Attribute_Name => Name_Unrestricted_Access,
3073 Prefix => New_Copy_Tree (Lhs)));
3074 Set_Analyzed (Parent (Expr), False);
3075 end if;
3076 end if;
3078 return OK;
3079 end Replace_Type;
3081 --------------------------
3082 -- Rewrite_Discriminant --
3083 --------------------------
3085 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
3086 begin
3087 if Is_Entity_Name (Expr)
3088 and then Present (Entity (Expr))
3089 and then Ekind (Entity (Expr)) = E_In_Parameter
3090 and then Present (Discriminal_Link (Entity (Expr)))
3091 and then Scope (Discriminal_Link (Entity (Expr))) =
3092 Base_Type (Etype (N))
3093 then
3094 Rewrite (Expr,
3095 Make_Selected_Component (Loc,
3096 Prefix => New_Copy_Tree (Lhs),
3097 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
3098 end if;
3100 return OK;
3101 end Rewrite_Discriminant;
3103 procedure Replace_Discriminants is
3104 new Traverse_Proc (Rewrite_Discriminant);
3106 procedure Replace_Self_Reference is
3107 new Traverse_Proc (Replace_Type);
3109 -- Start of processing for Build_Record_Aggr_Code
3111 begin
3112 if Has_Self_Reference (N) then
3113 Replace_Self_Reference (N);
3114 end if;
3116 -- If the target of the aggregate is class-wide, we must convert it
3117 -- to the actual type of the aggregate, so that the proper components
3118 -- are visible. We know already that the types are compatible.
3120 if Present (Etype (Lhs))
3121 and then Is_Class_Wide_Type (Etype (Lhs))
3122 then
3123 Target := Unchecked_Convert_To (Typ, Lhs);
3124 else
3125 Target := Lhs;
3126 end if;
3128 -- Deal with the ancestor part of extension aggregates or with the
3129 -- discriminants of the root type.
3131 if Nkind (N) = N_Extension_Aggregate then
3132 declare
3133 Ancestor : constant Node_Id := Ancestor_Part (N);
3134 Adj_Call : Node_Id;
3135 Assign : List_Id;
3137 begin
3138 -- If the ancestor part is a subtype mark "T", we generate
3140 -- init-proc (T (tmp)); if T is constrained and
3141 -- init-proc (S (tmp)); where S applies an appropriate
3142 -- constraint if T is unconstrained
3144 if Is_Entity_Name (Ancestor)
3145 and then Is_Type (Entity (Ancestor))
3146 then
3147 Ancestor_Is_Subtype_Mark := True;
3149 if Is_Constrained (Entity (Ancestor)) then
3150 Init_Typ := Entity (Ancestor);
3152 -- For an ancestor part given by an unconstrained type mark,
3153 -- create a subtype constrained by appropriate corresponding
3154 -- discriminant values coming from either associations of the
3155 -- aggregate or a constraint on a parent type. The subtype will
3156 -- be used to generate the correct default value for the
3157 -- ancestor part.
3159 elsif Has_Discriminants (Entity (Ancestor)) then
3160 declare
3161 Anc_Typ : constant Entity_Id := Entity (Ancestor);
3162 Anc_Constr : constant List_Id := New_List;
3163 Discrim : Entity_Id;
3164 Disc_Value : Node_Id;
3165 New_Indic : Node_Id;
3166 Subt_Decl : Node_Id;
3168 begin
3169 Discrim := First_Discriminant (Anc_Typ);
3170 while Present (Discrim) loop
3171 Disc_Value := Ancestor_Discriminant_Value (Discrim);
3173 -- If no usable discriminant in ancestors, check
3174 -- whether aggregate has an explicit value for it.
3176 if No (Disc_Value) then
3177 Disc_Value :=
3178 Get_Explicit_Discriminant_Value (Discrim);
3179 end if;
3181 Append_To (Anc_Constr, Disc_Value);
3182 Next_Discriminant (Discrim);
3183 end loop;
3185 New_Indic :=
3186 Make_Subtype_Indication (Loc,
3187 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
3188 Constraint =>
3189 Make_Index_Or_Discriminant_Constraint (Loc,
3190 Constraints => Anc_Constr));
3192 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
3194 Subt_Decl :=
3195 Make_Subtype_Declaration (Loc,
3196 Defining_Identifier => Init_Typ,
3197 Subtype_Indication => New_Indic);
3199 -- Itypes must be analyzed with checks off Declaration
3200 -- must have a parent for proper handling of subsidiary
3201 -- actions.
3203 Set_Parent (Subt_Decl, N);
3204 Analyze (Subt_Decl, Suppress => All_Checks);
3205 end;
3206 end if;
3208 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3209 Set_Assignment_OK (Ref);
3211 if not Is_Interface (Init_Typ) then
3212 Append_List_To (L,
3213 Build_Initialization_Call (Loc,
3214 Id_Ref => Ref,
3215 Typ => Init_Typ,
3216 In_Init_Proc => Within_Init_Proc,
3217 With_Default_Init => Has_Default_Init_Comps (N)
3218 or else
3219 Has_Task (Base_Type (Init_Typ))));
3221 if Is_Constrained (Entity (Ancestor))
3222 and then Has_Discriminants (Entity (Ancestor))
3223 then
3224 Check_Ancestor_Discriminants (Entity (Ancestor));
3225 end if;
3226 end if;
3228 -- Handle calls to C++ constructors
3230 elsif Is_CPP_Constructor_Call (Ancestor) then
3231 Init_Typ := Etype (Ancestor);
3232 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3233 Set_Assignment_OK (Ref);
3235 Append_List_To (L,
3236 Build_Initialization_Call (Loc,
3237 Id_Ref => Ref,
3238 Typ => Init_Typ,
3239 In_Init_Proc => Within_Init_Proc,
3240 With_Default_Init => Has_Default_Init_Comps (N),
3241 Constructor_Ref => Ancestor));
3243 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3244 -- limited type, a recursive call expands the ancestor. Note that
3245 -- in the limited case, the ancestor part must be either a
3246 -- function call (possibly qualified) or aggregate (definitely
3247 -- qualified).
3249 elsif Is_Limited_Type (Etype (Ancestor))
3250 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
3251 N_Extension_Aggregate)
3252 then
3253 Ancestor_Is_Expression := True;
3255 -- Set up finalization data for enclosing record, because
3256 -- controlled subcomponents of the ancestor part will be
3257 -- attached to it.
3259 Generate_Finalization_Actions;
3261 Append_List_To (L,
3262 Build_Record_Aggr_Code
3263 (N => Unqualify (Ancestor),
3264 Typ => Etype (Unqualify (Ancestor)),
3265 Lhs => Target));
3267 -- If the ancestor part is an expression "E", we generate
3269 -- T (tmp) := E;
3271 -- In Ada 2005, this includes the case of a (possibly qualified)
3272 -- limited function call. The assignment will turn into a
3273 -- build-in-place function call (for further details, see
3274 -- Make_Build_In_Place_Call_In_Assignment).
3276 else
3277 Ancestor_Is_Expression := True;
3278 Init_Typ := Etype (Ancestor);
3280 -- If the ancestor part is an aggregate, force its full
3281 -- expansion, which was delayed.
3283 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
3284 N_Extension_Aggregate)
3285 then
3286 Set_Analyzed (Ancestor, False);
3287 Set_Analyzed (Expression (Ancestor), False);
3288 end if;
3290 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3291 Set_Assignment_OK (Ref);
3293 -- Make the assignment without usual controlled actions, since
3294 -- we only want to Adjust afterwards, but not to Finalize
3295 -- beforehand. Add manual Adjust when necessary.
3297 Assign := New_List (
3298 Make_OK_Assignment_Statement (Loc,
3299 Name => Ref,
3300 Expression => Ancestor));
3301 Set_No_Ctrl_Actions (First (Assign));
3303 -- Assign the tag now to make sure that the dispatching call in
3304 -- the subsequent deep_adjust works properly (unless
3305 -- Tagged_Type_Expansion where tags are implicit).
3307 if Tagged_Type_Expansion then
3308 Instr :=
3309 Make_OK_Assignment_Statement (Loc,
3310 Name =>
3311 Make_Selected_Component (Loc,
3312 Prefix => New_Copy_Tree (Target),
3313 Selector_Name =>
3314 New_Occurrence_Of
3315 (First_Tag_Component (Base_Type (Typ)), Loc)),
3317 Expression =>
3318 Unchecked_Convert_To (RTE (RE_Tag),
3319 New_Occurrence_Of
3320 (Node (First_Elmt
3321 (Access_Disp_Table (Base_Type (Typ)))),
3322 Loc)));
3324 Set_Assignment_OK (Name (Instr));
3325 Append_To (Assign, Instr);
3327 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3328 -- also initialize tags of the secondary dispatch tables.
3330 if Has_Interfaces (Base_Type (Typ)) then
3331 Init_Secondary_Tags
3332 (Typ => Base_Type (Typ),
3333 Target => Target,
3334 Stmts_List => Assign,
3335 Init_Tags_List => Assign);
3336 end if;
3337 end if;
3339 -- Call Adjust manually
3341 if Needs_Finalization (Etype (Ancestor))
3342 and then not Is_Limited_Type (Etype (Ancestor))
3343 and then not Is_Build_In_Place_Function_Call (Ancestor)
3344 then
3345 Adj_Call :=
3346 Make_Adjust_Call
3347 (Obj_Ref => New_Copy_Tree (Ref),
3348 Typ => Etype (Ancestor));
3350 -- Guard against a missing [Deep_]Adjust when the ancestor
3351 -- type was not properly frozen.
3353 if Present (Adj_Call) then
3354 Append_To (Assign, Adj_Call);
3355 end if;
3356 end if;
3358 Append_To (L,
3359 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
3361 if Has_Discriminants (Init_Typ) then
3362 Check_Ancestor_Discriminants (Init_Typ);
3363 end if;
3364 end if;
3366 pragma Assert (Nkind (N) = N_Extension_Aggregate);
3367 pragma Assert
3368 (not (Ancestor_Is_Expression and Ancestor_Is_Subtype_Mark));
3369 end;
3371 -- Generate assignments of hidden discriminants. If the base type is
3372 -- an unchecked union, the discriminants are unknown to the back-end
3373 -- and absent from a value of the type, so assignments for them are
3374 -- not emitted.
3376 if Has_Discriminants (Typ)
3377 and then not Is_Unchecked_Union (Base_Type (Typ))
3378 then
3379 Init_Hidden_Discriminants (Typ, L);
3380 end if;
3382 -- Normal case (not an extension aggregate)
3384 else
3385 -- Generate the discriminant expressions, component by component.
3386 -- If the base type is an unchecked union, the discriminants are
3387 -- unknown to the back-end and absent from a value of the type, so
3388 -- assignments for them are not emitted.
3390 if Has_Discriminants (Typ)
3391 and then not Is_Unchecked_Union (Base_Type (Typ))
3392 then
3393 Init_Hidden_Discriminants (Typ, L);
3395 -- Generate discriminant init values for the visible discriminants
3397 Init_Visible_Discriminants;
3399 if Is_Derived_Type (N_Typ) then
3400 Init_Stored_Discriminants;
3401 end if;
3402 end if;
3403 end if;
3405 -- For CPP types we generate an implicit call to the C++ default
3406 -- constructor to ensure the proper initialization of the _Tag
3407 -- component.
3409 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3410 Invoke_Constructor : declare
3411 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3413 procedure Invoke_IC_Proc (T : Entity_Id);
3414 -- Recursive routine used to climb to parents. Required because
3415 -- parents must be initialized before descendants to ensure
3416 -- propagation of inherited C++ slots.
3418 --------------------
3419 -- Invoke_IC_Proc --
3420 --------------------
3422 procedure Invoke_IC_Proc (T : Entity_Id) is
3423 begin
3424 -- Avoid generating extra calls. Initialization required
3425 -- only for types defined from the level of derivation of
3426 -- type of the constructor and the type of the aggregate.
3428 if T = CPP_Parent then
3429 return;
3430 end if;
3432 Invoke_IC_Proc (Etype (T));
3434 -- Generate call to the IC routine
3436 if Present (CPP_Init_Proc (T)) then
3437 Append_To (L,
3438 Make_Procedure_Call_Statement (Loc,
3439 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3440 end if;
3441 end Invoke_IC_Proc;
3443 -- Start of processing for Invoke_Constructor
3445 begin
3446 -- Implicit invocation of the C++ constructor
3448 if Nkind (N) = N_Aggregate then
3449 Append_To (L,
3450 Make_Procedure_Call_Statement (Loc,
3451 Name =>
3452 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3453 Parameter_Associations => New_List (
3454 Unchecked_Convert_To (CPP_Parent,
3455 New_Copy_Tree (Lhs)))));
3456 end if;
3458 Invoke_IC_Proc (Typ);
3459 end Invoke_Constructor;
3460 end if;
3462 -- Generate the assignments, component by component
3464 -- tmp.comp1 := Expr1_From_Aggr;
3465 -- tmp.comp2 := Expr2_From_Aggr;
3466 -- ....
3468 Comp := First (Component_Associations (N));
3469 while Present (Comp) loop
3470 Selector := Entity (First (Choices (Comp)));
3472 -- C++ constructors
3474 if Is_CPP_Constructor_Call (Expression (Comp)) then
3475 Append_List_To (L,
3476 Build_Initialization_Call (Loc,
3477 Id_Ref =>
3478 Make_Selected_Component (Loc,
3479 Prefix => New_Copy_Tree (Target),
3480 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3481 Typ => Etype (Selector),
3482 Enclos_Type => Typ,
3483 With_Default_Init => True,
3484 Constructor_Ref => Expression (Comp)));
3486 -- Ada 2005 (AI-287): For each default-initialized component generate
3487 -- a call to the corresponding IP subprogram if available.
3489 elsif Box_Present (Comp)
3490 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3491 then
3492 if Ekind (Selector) /= E_Discriminant then
3493 Generate_Finalization_Actions;
3494 end if;
3496 -- Ada 2005 (AI-287): If the component type has tasks then
3497 -- generate the activation chain and master entities (except
3498 -- in case of an allocator because in that case these entities
3499 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3501 declare
3502 Ctype : constant Entity_Id := Etype (Selector);
3503 Inside_Allocator : Boolean := False;
3504 P : Node_Id := Parent (N);
3506 begin
3507 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3508 while Present (P) loop
3509 if Nkind (P) = N_Allocator then
3510 Inside_Allocator := True;
3511 exit;
3512 end if;
3514 P := Parent (P);
3515 end loop;
3517 if not Inside_Init_Proc and not Inside_Allocator then
3518 Build_Activation_Chain_Entity (N);
3519 end if;
3520 end if;
3521 end;
3523 Append_List_To (L,
3524 Build_Initialization_Call (Loc,
3525 Id_Ref => Make_Selected_Component (Loc,
3526 Prefix => New_Copy_Tree (Target),
3527 Selector_Name =>
3528 New_Occurrence_Of (Selector, Loc)),
3529 Typ => Etype (Selector),
3530 Enclos_Type => Typ,
3531 With_Default_Init => True));
3533 -- Prepare for component assignment
3535 elsif Ekind (Selector) /= E_Discriminant
3536 or else Nkind (N) = N_Extension_Aggregate
3537 then
3538 -- All the discriminants have now been assigned
3540 -- This is now a good moment to initialize and attach all the
3541 -- controllers. Their position may depend on the discriminants.
3543 if Ekind (Selector) /= E_Discriminant then
3544 Generate_Finalization_Actions;
3545 end if;
3547 Comp_Type := Underlying_Type (Etype (Selector));
3548 Comp_Expr :=
3549 Make_Selected_Component (Loc,
3550 Prefix => New_Copy_Tree (Target),
3551 Selector_Name => New_Occurrence_Of (Selector, Loc));
3553 if Nkind (Expression (Comp)) = N_Qualified_Expression then
3554 Expr_Q := Expression (Expression (Comp));
3555 else
3556 Expr_Q := Expression (Comp);
3557 end if;
3559 -- Now either create the assignment or generate the code for the
3560 -- inner aggregate top-down.
3562 if Is_Delayed_Aggregate (Expr_Q) then
3564 -- We have the following case of aggregate nesting inside
3565 -- an object declaration:
3567 -- type Arr_Typ is array (Integer range <>) of ...;
3569 -- type Rec_Typ (...) is record
3570 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3571 -- end record;
3573 -- Obj_Rec_Typ : Rec_Typ := (...,
3574 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3576 -- The length of the ranges of the aggregate and Obj_Add_Typ
3577 -- are equal (B - A = Y - X), but they do not coincide (X /=
3578 -- A and B /= Y). This case requires array sliding which is
3579 -- performed in the following manner:
3581 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3582 -- Temp : Arr_Sub;
3583 -- Temp (X) := (...);
3584 -- ...
3585 -- Temp (Y) := (...);
3586 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3588 if Ekind (Comp_Type) = E_Array_Subtype
3589 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3590 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3591 and then not
3592 Compatible_Int_Bounds
3593 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3594 Typ_Bounds => First_Index (Comp_Type))
3595 then
3596 -- Create the array subtype with bounds equal to those of
3597 -- the corresponding aggregate.
3599 declare
3600 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3602 SubD : constant Node_Id :=
3603 Make_Subtype_Declaration (Loc,
3604 Defining_Identifier => SubE,
3605 Subtype_Indication =>
3606 Make_Subtype_Indication (Loc,
3607 Subtype_Mark =>
3608 New_Occurrence_Of (Etype (Comp_Type), Loc),
3609 Constraint =>
3610 Make_Index_Or_Discriminant_Constraint
3611 (Loc,
3612 Constraints => New_List (
3613 New_Copy_Tree
3614 (Aggregate_Bounds (Expr_Q))))));
3616 -- Create a temporary array of the above subtype which
3617 -- will be used to capture the aggregate assignments.
3619 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3621 TmpD : constant Node_Id :=
3622 Make_Object_Declaration (Loc,
3623 Defining_Identifier => TmpE,
3624 Object_Definition => New_Occurrence_Of (SubE, Loc));
3626 begin
3627 Set_No_Initialization (TmpD);
3628 Append_To (L, SubD);
3629 Append_To (L, TmpD);
3631 -- Expand aggregate into assignments to the temp array
3633 Append_List_To (L,
3634 Late_Expansion (Expr_Q, Comp_Type,
3635 New_Occurrence_Of (TmpE, Loc)));
3637 -- Slide
3639 Append_To (L,
3640 Make_Assignment_Statement (Loc,
3641 Name => New_Copy_Tree (Comp_Expr),
3642 Expression => New_Occurrence_Of (TmpE, Loc)));
3643 end;
3645 -- Normal case (sliding not required)
3647 else
3648 Append_List_To (L,
3649 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3650 end if;
3652 -- Expr_Q is not delayed aggregate
3654 else
3655 if Has_Discriminants (Typ) then
3656 Replace_Discriminants (Expr_Q);
3658 -- If the component is an array type that depends on
3659 -- discriminants, and the expression is a single Others
3660 -- clause, create an explicit subtype for it because the
3661 -- backend has troubles recovering the actual bounds.
3663 if Nkind (Expr_Q) = N_Aggregate
3664 and then Is_Array_Type (Comp_Type)
3665 and then Present (Component_Associations (Expr_Q))
3666 then
3667 declare
3668 Assoc : constant Node_Id :=
3669 First (Component_Associations (Expr_Q));
3670 Decl : Node_Id;
3672 begin
3673 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3674 then
3675 Decl :=
3676 Build_Actual_Subtype_Of_Component
3677 (Comp_Type, Comp_Expr);
3679 -- If the component type does not in fact depend on
3680 -- discriminants, the subtype declaration is empty.
3682 if Present (Decl) then
3683 Append_To (L, Decl);
3684 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3685 end if;
3686 end if;
3687 end;
3688 end if;
3689 end if;
3691 if Modify_Tree_For_C
3692 and then Nkind (Expr_Q) = N_Aggregate
3693 and then Is_Array_Type (Etype (Expr_Q))
3694 and then Present (First_Index (Etype (Expr_Q)))
3695 then
3696 declare
3697 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3698 begin
3699 Append_List_To (L,
3700 Build_Array_Aggr_Code
3701 (N => Expr_Q,
3702 Ctype => Component_Type (Expr_Q_Type),
3703 Index => First_Index (Expr_Q_Type),
3704 Into => Comp_Expr,
3705 Scalar_Comp =>
3706 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
3707 end;
3709 else
3710 -- Handle an initialization expression of a controlled type
3711 -- in case it denotes a function call. In general such a
3712 -- scenario will produce a transient scope, but this will
3713 -- lead to wrong order of initialization, adjustment, and
3714 -- finalization in the context of aggregates.
3716 -- Target.Comp := Ctrl_Func_Call;
3718 -- begin -- scope
3719 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3720 -- Target.Comp := Trans_Obj;
3721 -- Finalize (Trans_Obj);
3722 -- end
3723 -- Target.Comp._tag := ...;
3724 -- Adjust (Target.Comp);
3726 -- In the example above, the call to Finalize occurs too
3727 -- early and as a result it may leave the record component
3728 -- in a bad state. Finalization of the transient object
3729 -- should really happen after adjustment.
3731 -- To avoid this scenario, perform in-place side-effect
3732 -- removal of the function call. This eliminates the
3733 -- transient property of the function result and ensures
3734 -- correct order of actions.
3736 -- Res : ... := Ctrl_Func_Call;
3737 -- Target.Comp := Res;
3738 -- Target.Comp._tag := ...;
3739 -- Adjust (Target.Comp);
3740 -- Finalize (Res);
3742 if Needs_Finalization (Comp_Type)
3743 and then Nkind (Expr_Q) /= N_Aggregate
3744 then
3745 Initialize_Ctrl_Record_Component
3746 (Rec_Comp => Comp_Expr,
3747 Comp_Typ => Etype (Selector),
3748 Init_Expr => Expr_Q,
3749 Stmts => L);
3751 -- Otherwise perform single component initialization
3753 else
3754 Initialize_Record_Component
3755 (Rec_Comp => Comp_Expr,
3756 Comp_Typ => Etype (Selector),
3757 Init_Expr => Expr_Q,
3758 Stmts => L);
3759 end if;
3760 end if;
3761 end if;
3763 -- comment would be good here ???
3765 elsif Ekind (Selector) = E_Discriminant
3766 and then Nkind (N) /= N_Extension_Aggregate
3767 and then Nkind (Parent (N)) = N_Component_Association
3768 and then Is_Constrained (Typ)
3769 then
3770 -- We must check that the discriminant value imposed by the
3771 -- context is the same as the value given in the subaggregate,
3772 -- because after the expansion into assignments there is no
3773 -- record on which to perform a regular discriminant check.
3775 declare
3776 D_Val : Elmt_Id;
3777 Disc : Entity_Id;
3779 begin
3780 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3781 Disc := First_Discriminant (Typ);
3782 while Chars (Disc) /= Chars (Selector) loop
3783 Next_Discriminant (Disc);
3784 Next_Elmt (D_Val);
3785 end loop;
3787 pragma Assert (Present (D_Val));
3789 -- This check cannot performed for components that are
3790 -- constrained by a current instance, because this is not a
3791 -- value that can be compared with the actual constraint.
3793 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3794 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3795 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3796 then
3797 Append_To (L,
3798 Make_Raise_Constraint_Error (Loc,
3799 Condition =>
3800 Make_Op_Ne (Loc,
3801 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3802 Right_Opnd => Expression (Comp)),
3803 Reason => CE_Discriminant_Check_Failed));
3805 else
3806 -- Find self-reference in previous discriminant assignment,
3807 -- and replace with proper expression.
3809 declare
3810 Ass : Node_Id;
3812 begin
3813 Ass := First (L);
3814 while Present (Ass) loop
3815 if Nkind (Ass) = N_Assignment_Statement
3816 and then Nkind (Name (Ass)) = N_Selected_Component
3817 and then Chars (Selector_Name (Name (Ass))) =
3818 Chars (Disc)
3819 then
3820 Set_Expression
3821 (Ass, New_Copy_Tree (Expression (Comp)));
3822 exit;
3823 end if;
3824 Next (Ass);
3825 end loop;
3826 end;
3827 end if;
3828 end;
3829 end if;
3831 Next (Comp);
3832 end loop;
3834 -- If the type is tagged, the tag needs to be initialized (unless we
3835 -- are in VM-mode where tags are implicit). It is done late in the
3836 -- initialization process because in some cases, we call the init
3837 -- proc of an ancestor which will not leave out the right tag.
3839 if Ancestor_Is_Expression then
3840 null;
3842 -- For CPP types we generated a call to the C++ default constructor
3843 -- before the components have been initialized to ensure the proper
3844 -- initialization of the _Tag component (see above).
3846 elsif Is_CPP_Class (Typ) then
3847 null;
3849 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3850 Instr :=
3851 Make_OK_Assignment_Statement (Loc,
3852 Name =>
3853 Make_Selected_Component (Loc,
3854 Prefix => New_Copy_Tree (Target),
3855 Selector_Name =>
3856 New_Occurrence_Of
3857 (First_Tag_Component (Base_Type (Typ)), Loc)),
3859 Expression =>
3860 Unchecked_Convert_To (RTE (RE_Tag),
3861 New_Occurrence_Of
3862 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3863 Loc)));
3865 Append_To (L, Instr);
3867 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3868 -- abstract interfaces we must also initialize the tags of the
3869 -- secondary dispatch tables.
3871 if Has_Interfaces (Base_Type (Typ)) then
3872 Init_Secondary_Tags
3873 (Typ => Base_Type (Typ),
3874 Target => Target,
3875 Stmts_List => L,
3876 Init_Tags_List => L);
3877 end if;
3878 end if;
3880 -- If the controllers have not been initialized yet (by lack of non-
3881 -- discriminant components), let's do it now.
3883 Generate_Finalization_Actions;
3885 return L;
3886 end Build_Record_Aggr_Code;
3888 ---------------------------------------
3889 -- Collect_Initialization_Statements --
3890 ---------------------------------------
3892 procedure Collect_Initialization_Statements
3893 (Obj : Entity_Id;
3894 N : Node_Id;
3895 Node_After : Node_Id)
3897 Loc : constant Source_Ptr := Sloc (N);
3898 Init_Actions : constant List_Id := New_List;
3899 Init_Node : Node_Id;
3900 Comp_Stmt : Node_Id;
3902 begin
3903 -- Nothing to do if Obj is already frozen, as in this case we known we
3904 -- won't need to move the initialization statements about later on.
3906 if Is_Frozen (Obj) then
3907 return;
3908 end if;
3910 Init_Node := N;
3911 while Next (Init_Node) /= Node_After loop
3912 Append_To (Init_Actions, Remove_Next (Init_Node));
3913 end loop;
3915 if not Is_Empty_List (Init_Actions) then
3916 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3917 Insert_Action_After (Init_Node, Comp_Stmt);
3918 Set_Initialization_Statements (Obj, Comp_Stmt);
3919 end if;
3920 end Collect_Initialization_Statements;
3922 -------------------------------
3923 -- Convert_Aggr_In_Allocator --
3924 -------------------------------
3926 procedure Convert_Aggr_In_Allocator
3927 (Alloc : Node_Id;
3928 Decl : Node_Id;
3929 Aggr : Node_Id)
3931 Loc : constant Source_Ptr := Sloc (Aggr);
3932 Typ : constant Entity_Id := Etype (Aggr);
3933 Temp : constant Entity_Id := Defining_Identifier (Decl);
3935 Occ : constant Node_Id :=
3936 Unchecked_Convert_To (Typ,
3937 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3939 begin
3940 if Is_Array_Type (Typ) then
3941 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3943 elsif Has_Default_Init_Comps (Aggr) then
3944 declare
3945 L : constant List_Id := New_List;
3946 Init_Stmts : List_Id;
3948 begin
3949 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3951 if Has_Task (Typ) then
3952 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3953 Insert_Actions (Alloc, L);
3954 else
3955 Insert_Actions (Alloc, Init_Stmts);
3956 end if;
3957 end;
3959 else
3960 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3961 end if;
3962 end Convert_Aggr_In_Allocator;
3964 --------------------------------
3965 -- Convert_Aggr_In_Assignment --
3966 --------------------------------
3968 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3969 Aggr : Node_Id := Expression (N);
3970 Typ : constant Entity_Id := Etype (Aggr);
3971 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3973 begin
3974 if Nkind (Aggr) = N_Qualified_Expression then
3975 Aggr := Expression (Aggr);
3976 end if;
3978 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3979 end Convert_Aggr_In_Assignment;
3981 ---------------------------------
3982 -- Convert_Aggr_In_Object_Decl --
3983 ---------------------------------
3985 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3986 Obj : constant Entity_Id := Defining_Identifier (N);
3987 Aggr : Node_Id := Expression (N);
3988 Loc : constant Source_Ptr := Sloc (Aggr);
3989 Typ : constant Entity_Id := Etype (Aggr);
3990 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3992 function Discriminants_Ok return Boolean;
3993 -- If the object type is constrained, the discriminants in the
3994 -- aggregate must be checked against the discriminants of the subtype.
3995 -- This cannot be done using Apply_Discriminant_Checks because after
3996 -- expansion there is no aggregate left to check.
3998 ----------------------
3999 -- Discriminants_Ok --
4000 ----------------------
4002 function Discriminants_Ok return Boolean is
4003 Cond : Node_Id := Empty;
4004 Check : Node_Id;
4005 D : Entity_Id;
4006 Disc1 : Elmt_Id;
4007 Disc2 : Elmt_Id;
4008 Val1 : Node_Id;
4009 Val2 : Node_Id;
4011 begin
4012 D := First_Discriminant (Typ);
4013 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
4014 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
4015 while Present (Disc1) and then Present (Disc2) loop
4016 Val1 := Node (Disc1);
4017 Val2 := Node (Disc2);
4019 if not Is_OK_Static_Expression (Val1)
4020 or else not Is_OK_Static_Expression (Val2)
4021 then
4022 Check := Make_Op_Ne (Loc,
4023 Left_Opnd => Duplicate_Subexpr (Val1),
4024 Right_Opnd => Duplicate_Subexpr (Val2));
4026 if No (Cond) then
4027 Cond := Check;
4029 else
4030 Cond := Make_Or_Else (Loc,
4031 Left_Opnd => Cond,
4032 Right_Opnd => Check);
4033 end if;
4035 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
4036 Apply_Compile_Time_Constraint_Error (Aggr,
4037 Msg => "incorrect value for discriminant&??",
4038 Reason => CE_Discriminant_Check_Failed,
4039 Ent => D);
4040 return False;
4041 end if;
4043 Next_Discriminant (D);
4044 Next_Elmt (Disc1);
4045 Next_Elmt (Disc2);
4046 end loop;
4048 -- If any discriminant constraint is non-static, emit a check
4050 if Present (Cond) then
4051 Insert_Action (N,
4052 Make_Raise_Constraint_Error (Loc,
4053 Condition => Cond,
4054 Reason => CE_Discriminant_Check_Failed));
4055 end if;
4057 return True;
4058 end Discriminants_Ok;
4060 -- Start of processing for Convert_Aggr_In_Object_Decl
4062 begin
4063 Set_Assignment_OK (Occ);
4065 if Nkind (Aggr) = N_Qualified_Expression then
4066 Aggr := Expression (Aggr);
4067 end if;
4069 if Has_Discriminants (Typ)
4070 and then Typ /= Etype (Obj)
4071 and then Is_Constrained (Etype (Obj))
4072 and then not Discriminants_Ok
4073 then
4074 return;
4075 end if;
4077 -- If the context is an extended return statement, it has its own
4078 -- finalization machinery (i.e. works like a transient scope) and
4079 -- we do not want to create an additional one, because objects on
4080 -- the finalization list of the return must be moved to the caller's
4081 -- finalization list to complete the return.
4083 -- However, if the aggregate is limited, it is built in place, and the
4084 -- controlled components are not assigned to intermediate temporaries
4085 -- so there is no need for a transient scope in this case either.
4087 if Requires_Transient_Scope (Typ)
4088 and then Ekind (Current_Scope) /= E_Return_Statement
4089 and then not Is_Limited_Type (Typ)
4090 then
4091 Establish_Transient_Scope (Aggr, Sec_Stack => False);
4092 end if;
4094 declare
4095 Node_After : constant Node_Id := Next (N);
4096 begin
4097 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
4098 Collect_Initialization_Statements (Obj, N, Node_After);
4099 end;
4100 Set_No_Initialization (N);
4101 Initialize_Discriminants (N, Typ);
4102 end Convert_Aggr_In_Object_Decl;
4104 -------------------------------------
4105 -- Convert_Array_Aggr_In_Allocator --
4106 -------------------------------------
4108 procedure Convert_Array_Aggr_In_Allocator
4109 (Decl : Node_Id;
4110 Aggr : Node_Id;
4111 Target : Node_Id)
4113 Aggr_Code : List_Id;
4114 Typ : constant Entity_Id := Etype (Aggr);
4115 Ctyp : constant Entity_Id := Component_Type (Typ);
4117 begin
4118 -- The target is an explicit dereference of the allocated object.
4119 -- Generate component assignments to it, as for an aggregate that
4120 -- appears on the right-hand side of an assignment statement.
4122 Aggr_Code :=
4123 Build_Array_Aggr_Code (Aggr,
4124 Ctype => Ctyp,
4125 Index => First_Index (Typ),
4126 Into => Target,
4127 Scalar_Comp => Is_Scalar_Type (Ctyp));
4129 Insert_Actions_After (Decl, Aggr_Code);
4130 end Convert_Array_Aggr_In_Allocator;
4132 ----------------------------
4133 -- Convert_To_Assignments --
4134 ----------------------------
4136 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4137 Loc : constant Source_Ptr := Sloc (N);
4138 T : Entity_Id;
4139 Temp : Entity_Id;
4141 Aggr_Code : List_Id;
4142 Instr : Node_Id;
4143 Target_Expr : Node_Id;
4144 Parent_Kind : Node_Kind;
4145 Unc_Decl : Boolean := False;
4146 Parent_Node : Node_Id;
4148 begin
4149 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
4150 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4151 pragma Assert (Is_Record_Type (Typ));
4153 Parent_Node := Parent (N);
4154 Parent_Kind := Nkind (Parent_Node);
4156 if Parent_Kind = N_Qualified_Expression then
4157 -- Check if we are in an unconstrained declaration because in this
4158 -- case the current delayed expansion mechanism doesn't work when
4159 -- the declared object size depends on the initializing expr.
4161 Parent_Node := Parent (Parent_Node);
4162 Parent_Kind := Nkind (Parent_Node);
4164 if Parent_Kind = N_Object_Declaration then
4165 Unc_Decl :=
4166 not Is_Entity_Name (Object_Definition (Parent_Node))
4167 or else (Nkind (N) = N_Aggregate
4168 and then
4169 Has_Discriminants
4170 (Entity (Object_Definition (Parent_Node))))
4171 or else Is_Class_Wide_Type
4172 (Entity (Object_Definition (Parent_Node)));
4173 end if;
4174 end if;
4176 -- Just set the Delay flag in the cases where the transformation will be
4177 -- done top down from above.
4179 if False
4181 -- Internal aggregate (transformed when expanding the parent)
4183 or else Parent_Kind = N_Aggregate
4184 or else Parent_Kind = N_Extension_Aggregate
4185 or else Parent_Kind = N_Component_Association
4187 -- Allocator (see Convert_Aggr_In_Allocator)
4189 or else Parent_Kind = N_Allocator
4191 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4193 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4195 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4196 -- assignments in init procs are taken into account.
4198 or else (Parent_Kind = N_Assignment_Statement
4199 and then Inside_Init_Proc)
4201 -- (Ada 2005) An inherently limited type in a return statement, which
4202 -- will be handled in a build-in-place fashion, and may be rewritten
4203 -- as an extended return and have its own finalization machinery.
4204 -- In the case of a simple return, the aggregate needs to be delayed
4205 -- until the scope for the return statement has been created, so
4206 -- that any finalization chain will be associated with that scope.
4207 -- For extended returns, we delay expansion to avoid the creation
4208 -- of an unwanted transient scope that could result in premature
4209 -- finalization of the return object (which is built in place
4210 -- within the caller's scope).
4212 or else Is_Build_In_Place_Aggregate_Return (N)
4213 then
4214 Set_Expansion_Delayed (N);
4215 return;
4216 end if;
4218 -- Otherwise, if a transient scope is required, create it now. If we
4219 -- are within an initialization procedure do not create such, because
4220 -- the target of the assignment must not be declared within a local
4221 -- block, and because cleanup will take place on return from the
4222 -- initialization procedure.
4224 -- Should the condition be more restrictive ???
4226 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4227 Establish_Transient_Scope (N, Sec_Stack => False);
4228 end if;
4230 -- If the aggregate is nonlimited, create a temporary. If it is limited
4231 -- and context is an assignment, this is a subaggregate for an enclosing
4232 -- aggregate being expanded. It must be built in place, so use target of
4233 -- the current assignment.
4235 if Is_Limited_Type (Typ)
4236 and then Nkind (Parent (N)) = N_Assignment_Statement
4237 then
4238 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4239 Insert_Actions (Parent (N),
4240 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4241 Rewrite (Parent (N), Make_Null_Statement (Loc));
4243 else
4244 Temp := Make_Temporary (Loc, 'A', N);
4246 -- If the type inherits unknown discriminants, use the view with
4247 -- known discriminants if available.
4249 if Has_Unknown_Discriminants (Typ)
4250 and then Present (Underlying_Record_View (Typ))
4251 then
4252 T := Underlying_Record_View (Typ);
4253 else
4254 T := Typ;
4255 end if;
4257 Instr :=
4258 Make_Object_Declaration (Loc,
4259 Defining_Identifier => Temp,
4260 Object_Definition => New_Occurrence_Of (T, Loc));
4262 Set_No_Initialization (Instr);
4263 Insert_Action (N, Instr);
4264 Initialize_Discriminants (Instr, T);
4266 Target_Expr := New_Occurrence_Of (Temp, Loc);
4267 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
4269 -- Save the last assignment statement associated with the aggregate
4270 -- when building a controlled object. This reference is utilized by
4271 -- the finalization machinery when marking an object as successfully
4272 -- initialized.
4274 if Needs_Finalization (T) then
4275 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
4276 end if;
4278 Insert_Actions (N, Aggr_Code);
4279 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4280 Analyze_And_Resolve (N, T);
4281 end if;
4282 end Convert_To_Assignments;
4284 ---------------------------
4285 -- Convert_To_Positional --
4286 ---------------------------
4288 procedure Convert_To_Positional
4289 (N : Node_Id;
4290 Max_Others_Replicate : Nat := 5;
4291 Handle_Bit_Packed : Boolean := False)
4293 Typ : constant Entity_Id := Etype (N);
4295 Static_Components : Boolean := True;
4297 procedure Check_Static_Components;
4298 -- Check whether all components of the aggregate are compile-time known
4299 -- values, and can be passed as is to the back-end without further
4300 -- expansion.
4301 -- An Iterated_Component_Association is treated as non-static, but there
4302 -- are possibilities for optimization here.
4304 function Flatten
4305 (N : Node_Id;
4306 Ix : Node_Id;
4307 Ixb : Node_Id) return Boolean;
4308 -- Convert the aggregate into a purely positional form if possible. On
4309 -- entry the bounds of all dimensions are known to be static, and the
4310 -- total number of components is safe enough to expand.
4312 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
4313 -- Return True iff the array N is flat (which is not trivial in the case
4314 -- of multidimensional aggregates).
4316 -----------------------------
4317 -- Check_Static_Components --
4318 -----------------------------
4320 -- Could use some comments in this body ???
4322 procedure Check_Static_Components is
4323 Expr : Node_Id;
4325 begin
4326 Static_Components := True;
4328 if Nkind (N) = N_String_Literal then
4329 null;
4331 elsif Present (Expressions (N)) then
4332 Expr := First (Expressions (N));
4333 while Present (Expr) loop
4334 if Nkind (Expr) /= N_Aggregate
4335 or else not Compile_Time_Known_Aggregate (Expr)
4336 or else Expansion_Delayed (Expr)
4337 then
4338 Static_Components := False;
4339 exit;
4340 end if;
4342 Next (Expr);
4343 end loop;
4344 end if;
4346 if Nkind (N) = N_Aggregate
4347 and then Present (Component_Associations (N))
4348 then
4349 Expr := First (Component_Associations (N));
4350 while Present (Expr) loop
4351 if Nkind_In (Expression (Expr), N_Integer_Literal,
4352 N_Real_Literal)
4353 then
4354 null;
4356 elsif Is_Entity_Name (Expression (Expr))
4357 and then Present (Entity (Expression (Expr)))
4358 and then Ekind (Entity (Expression (Expr))) =
4359 E_Enumeration_Literal
4360 then
4361 null;
4363 elsif Nkind (Expression (Expr)) /= N_Aggregate
4364 or else not Compile_Time_Known_Aggregate (Expression (Expr))
4365 or else Expansion_Delayed (Expression (Expr))
4366 or else Nkind (Expr) = N_Iterated_Component_Association
4367 then
4368 Static_Components := False;
4369 exit;
4370 end if;
4372 Next (Expr);
4373 end loop;
4374 end if;
4375 end Check_Static_Components;
4377 -------------
4378 -- Flatten --
4379 -------------
4381 function Flatten
4382 (N : Node_Id;
4383 Ix : Node_Id;
4384 Ixb : Node_Id) return Boolean
4386 Loc : constant Source_Ptr := Sloc (N);
4387 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
4388 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
4389 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
4390 Lov : Uint;
4391 Hiv : Uint;
4393 Others_Present : Boolean := False;
4395 begin
4396 if Nkind (Original_Node (N)) = N_String_Literal then
4397 return True;
4398 end if;
4400 if not Compile_Time_Known_Value (Lo)
4401 or else not Compile_Time_Known_Value (Hi)
4402 then
4403 return False;
4404 end if;
4406 Lov := Expr_Value (Lo);
4407 Hiv := Expr_Value (Hi);
4409 -- Check if there is an others choice
4411 if Present (Component_Associations (N)) then
4412 declare
4413 Assoc : Node_Id;
4414 Choice : Node_Id;
4416 begin
4417 Assoc := First (Component_Associations (N));
4418 while Present (Assoc) loop
4420 -- If this is a box association, flattening is in general
4421 -- not possible because at this point we cannot tell if the
4422 -- default is static or even exists.
4424 if Box_Present (Assoc) then
4425 return False;
4427 elsif Nkind (Assoc) = N_Iterated_Component_Association then
4428 return False;
4429 end if;
4431 Choice := First (Choice_List (Assoc));
4433 while Present (Choice) loop
4434 if Nkind (Choice) = N_Others_Choice then
4435 Others_Present := True;
4436 end if;
4438 Next (Choice);
4439 end loop;
4441 Next (Assoc);
4442 end loop;
4443 end;
4444 end if;
4446 -- If the low bound is not known at compile time and others is not
4447 -- present we can proceed since the bounds can be obtained from the
4448 -- aggregate.
4450 if Hiv < Lov
4451 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
4452 then
4453 return False;
4454 end if;
4456 -- Determine if set of alternatives is suitable for conversion and
4457 -- build an array containing the values in sequence.
4459 declare
4460 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
4461 of Node_Id := (others => Empty);
4462 -- The values in the aggregate sorted appropriately
4464 Vlist : List_Id;
4465 -- Same data as Vals in list form
4467 Rep_Count : Nat;
4468 -- Used to validate Max_Others_Replicate limit
4470 Elmt : Node_Id;
4471 Num : Int := UI_To_Int (Lov);
4472 Choice_Index : Int;
4473 Choice : Node_Id;
4474 Lo, Hi : Node_Id;
4476 begin
4477 if Present (Expressions (N)) then
4478 Elmt := First (Expressions (N));
4479 while Present (Elmt) loop
4480 if Nkind (Elmt) = N_Aggregate
4481 and then Present (Next_Index (Ix))
4482 and then
4483 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
4484 then
4485 return False;
4486 end if;
4488 Vals (Num) := Relocate_Node (Elmt);
4489 Num := Num + 1;
4491 Next (Elmt);
4492 end loop;
4493 end if;
4495 if No (Component_Associations (N)) then
4496 return True;
4497 end if;
4499 Elmt := First (Component_Associations (N));
4501 if Nkind (Expression (Elmt)) = N_Aggregate then
4502 if Present (Next_Index (Ix))
4503 and then
4504 not Flatten
4505 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
4506 then
4507 return False;
4508 end if;
4509 end if;
4511 Component_Loop : while Present (Elmt) loop
4512 Choice := First (Choice_List (Elmt));
4513 Choice_Loop : while Present (Choice) loop
4515 -- If we have an others choice, fill in the missing elements
4516 -- subject to the limit established by Max_Others_Replicate.
4518 if Nkind (Choice) = N_Others_Choice then
4519 Rep_Count := 0;
4521 for J in Vals'Range loop
4522 if No (Vals (J)) then
4523 Vals (J) := New_Copy_Tree (Expression (Elmt));
4524 Rep_Count := Rep_Count + 1;
4526 -- Check for maximum others replication. Note that
4527 -- we skip this test if either of the restrictions
4528 -- No_Elaboration_Code or No_Implicit_Loops is
4529 -- active, if this is a preelaborable unit or
4530 -- a predefined unit, or if the unit must be
4531 -- placed in data memory. This also ensures that
4532 -- predefined units get the same level of constant
4533 -- folding in Ada 95 and Ada 2005, where their
4534 -- categorization has changed.
4536 declare
4537 P : constant Entity_Id :=
4538 Cunit_Entity (Current_Sem_Unit);
4540 begin
4541 -- Check if duplication OK and if so continue
4542 -- processing.
4544 if Restriction_Active (No_Elaboration_Code)
4545 or else Restriction_Active (No_Implicit_Loops)
4546 or else
4547 (Ekind (Current_Scope) = E_Package
4548 and then Static_Elaboration_Desired
4549 (Current_Scope))
4550 or else Is_Preelaborated (P)
4551 or else (Ekind (P) = E_Package_Body
4552 and then
4553 Is_Preelaborated (Spec_Entity (P)))
4554 or else
4555 Is_Predefined_Unit (Get_Source_Unit (P))
4556 then
4557 null;
4559 -- If duplication not OK, then we return False
4560 -- if the replication count is too high
4562 elsif Rep_Count > Max_Others_Replicate then
4563 return False;
4565 -- Continue on if duplication not OK, but the
4566 -- replication count is not excessive.
4568 else
4569 null;
4570 end if;
4571 end;
4572 end if;
4573 end loop;
4575 exit Component_Loop;
4577 -- Case of a subtype mark, identifier or expanded name
4579 elsif Is_Entity_Name (Choice)
4580 and then Is_Type (Entity (Choice))
4581 then
4582 Lo := Type_Low_Bound (Etype (Choice));
4583 Hi := Type_High_Bound (Etype (Choice));
4585 -- Case of subtype indication
4587 elsif Nkind (Choice) = N_Subtype_Indication then
4588 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
4589 Hi := High_Bound (Range_Expression (Constraint (Choice)));
4591 -- Case of a range
4593 elsif Nkind (Choice) = N_Range then
4594 Lo := Low_Bound (Choice);
4595 Hi := High_Bound (Choice);
4597 -- Normal subexpression case
4599 else pragma Assert (Nkind (Choice) in N_Subexpr);
4600 if not Compile_Time_Known_Value (Choice) then
4601 return False;
4603 else
4604 Choice_Index := UI_To_Int (Expr_Value (Choice));
4606 if Choice_Index in Vals'Range then
4607 Vals (Choice_Index) :=
4608 New_Copy_Tree (Expression (Elmt));
4609 goto Continue;
4611 -- Choice is statically out-of-range, will be
4612 -- rewritten to raise Constraint_Error.
4614 else
4615 return False;
4616 end if;
4617 end if;
4618 end if;
4620 -- Range cases merge with Lo,Hi set
4622 if not Compile_Time_Known_Value (Lo)
4623 or else
4624 not Compile_Time_Known_Value (Hi)
4625 then
4626 return False;
4628 else
4629 for J in UI_To_Int (Expr_Value (Lo)) ..
4630 UI_To_Int (Expr_Value (Hi))
4631 loop
4632 Vals (J) := New_Copy_Tree (Expression (Elmt));
4633 end loop;
4634 end if;
4636 <<Continue>>
4637 Next (Choice);
4638 end loop Choice_Loop;
4640 Next (Elmt);
4641 end loop Component_Loop;
4643 -- If we get here the conversion is possible
4645 Vlist := New_List;
4646 for J in Vals'Range loop
4647 Append (Vals (J), Vlist);
4648 end loop;
4650 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4651 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4652 return True;
4653 end;
4654 end Flatten;
4656 -------------
4657 -- Is_Flat --
4658 -------------
4660 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4661 Elmt : Node_Id;
4663 begin
4664 if Dims = 0 then
4665 return True;
4667 elsif Nkind (N) = N_Aggregate then
4668 if Present (Component_Associations (N)) then
4669 return False;
4671 else
4672 Elmt := First (Expressions (N));
4673 while Present (Elmt) loop
4674 if not Is_Flat (Elmt, Dims - 1) then
4675 return False;
4676 end if;
4678 Next (Elmt);
4679 end loop;
4681 return True;
4682 end if;
4683 else
4684 return True;
4685 end if;
4686 end Is_Flat;
4688 -- Start of processing for Convert_To_Positional
4690 begin
4691 -- Only convert to positional when generating C in case of an
4692 -- object declaration, this is the only case where aggregates are
4693 -- supported in C.
4695 if Modify_Tree_For_C and then not In_Object_Declaration (N) then
4696 return;
4697 end if;
4699 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4700 -- components because in this case will need to call the corresponding
4701 -- IP procedure.
4703 if Has_Default_Init_Comps (N) then
4704 return;
4705 end if;
4707 if Is_Flat (N, Number_Dimensions (Typ)) then
4708 return;
4709 end if;
4711 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4712 return;
4713 end if;
4715 -- Do not convert to positional if controlled components are involved
4716 -- since these require special processing
4718 if Has_Controlled_Component (Typ) then
4719 return;
4720 end if;
4722 Check_Static_Components;
4724 -- If the size is known, or all the components are static, try to
4725 -- build a fully positional aggregate.
4727 -- The size of the type may not be known for an aggregate with
4728 -- discriminated array components, but if the components are static
4729 -- it is still possible to verify statically that the length is
4730 -- compatible with the upper bound of the type, and therefore it is
4731 -- worth flattening such aggregates as well.
4733 -- For now the back-end expands these aggregates into individual
4734 -- assignments to the target anyway, but it is conceivable that
4735 -- it will eventually be able to treat such aggregates statically???
4737 if Aggr_Size_OK (N, Typ)
4738 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4739 then
4740 if Static_Components then
4741 Set_Compile_Time_Known_Aggregate (N);
4742 Set_Expansion_Delayed (N, False);
4743 end if;
4745 Analyze_And_Resolve (N, Typ);
4746 end if;
4748 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4749 -- that will still require initialization code.
4751 if (Ekind (Current_Scope) = E_Package
4752 and then Static_Elaboration_Desired (Current_Scope))
4753 and then Nkind (Parent (N)) = N_Object_Declaration
4754 then
4755 declare
4756 Expr : Node_Id;
4758 begin
4759 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4760 Expr := First (Expressions (N));
4761 while Present (Expr) loop
4762 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
4763 or else
4764 (Is_Entity_Name (Expr)
4765 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
4766 then
4767 null;
4769 else
4770 Error_Msg_N
4771 ("non-static object requires elaboration code??", N);
4772 exit;
4773 end if;
4775 Next (Expr);
4776 end loop;
4778 if Present (Component_Associations (N)) then
4779 Error_Msg_N ("object requires elaboration code??", N);
4780 end if;
4781 end if;
4782 end;
4783 end if;
4784 end Convert_To_Positional;
4786 ----------------------------
4787 -- Expand_Array_Aggregate --
4788 ----------------------------
4790 -- Array aggregate expansion proceeds as follows:
4792 -- 1. If requested we generate code to perform all the array aggregate
4793 -- bound checks, specifically
4795 -- (a) Check that the index range defined by aggregate bounds is
4796 -- compatible with corresponding index subtype.
4798 -- (b) If an others choice is present check that no aggregate
4799 -- index is outside the bounds of the index constraint.
4801 -- (c) For multidimensional arrays make sure that all subaggregates
4802 -- corresponding to the same dimension have the same bounds.
4804 -- 2. Check for packed array aggregate which can be converted to a
4805 -- constant so that the aggregate disappears completely.
4807 -- 3. Check case of nested aggregate. Generally nested aggregates are
4808 -- handled during the processing of the parent aggregate.
4810 -- 4. Check if the aggregate can be statically processed. If this is the
4811 -- case pass it as is to Gigi. Note that a necessary condition for
4812 -- static processing is that the aggregate be fully positional.
4814 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4815 -- a temporary) then mark the aggregate as such and return. Otherwise
4816 -- create a new temporary and generate the appropriate initialization
4817 -- code.
4819 procedure Expand_Array_Aggregate (N : Node_Id) is
4820 Loc : constant Source_Ptr := Sloc (N);
4822 Typ : constant Entity_Id := Etype (N);
4823 Ctyp : constant Entity_Id := Component_Type (Typ);
4824 -- Typ is the correct constrained array subtype of the aggregate
4825 -- Ctyp is the corresponding component type.
4827 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4828 -- Number of aggregate index dimensions
4830 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4831 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4832 -- Low and High bounds of the constraint for each aggregate index
4834 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4835 -- The type of each index
4837 In_Place_Assign_OK_For_Declaration : Boolean := False;
4838 -- True if we are to generate an in place assignment for a declaration
4840 Maybe_In_Place_OK : Boolean;
4841 -- If the type is neither controlled nor packed and the aggregate
4842 -- is the expression in an assignment, assignment in place may be
4843 -- possible, provided other conditions are met on the LHS.
4845 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4846 (others => False);
4847 -- If Others_Present (J) is True, then there is an others choice in one
4848 -- of the subaggregates of N at dimension J.
4850 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4851 -- Returns true if an aggregate assignment can be done by the back end
4853 procedure Build_Constrained_Type (Positional : Boolean);
4854 -- If the subtype is not static or unconstrained, build a constrained
4855 -- type using the computable sizes of the aggregate and its sub-
4856 -- aggregates.
4858 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4859 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4860 -- by Index_Bounds.
4862 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4863 -- Checks that in a multidimensional array aggregate all subaggregates
4864 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4865 -- an array subaggregate. Dim is the dimension corresponding to the
4866 -- subaggregate.
4868 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4869 -- Computes the values of array Others_Present. Sub_Aggr is the array
4870 -- subaggregate we start the computation from. Dim is the dimension
4871 -- corresponding to the subaggregate.
4873 function In_Place_Assign_OK return Boolean;
4874 -- Simple predicate to determine whether an aggregate assignment can
4875 -- be done in place, because none of the new values can depend on the
4876 -- components of the target of the assignment.
4878 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4879 -- Checks that if an others choice is present in any subaggregate, no
4880 -- aggregate index is outside the bounds of the index constraint.
4881 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4882 -- to the subaggregate.
4884 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4885 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4886 -- built directly into the target of the assignment it must be free
4887 -- of side effects.
4889 ------------------------------------
4890 -- Aggr_Assignment_OK_For_Backend --
4891 ------------------------------------
4893 -- Backend processing by Gigi/gcc is possible only if all the following
4894 -- conditions are met:
4896 -- 1. N consists of a single OTHERS choice, possibly recursively
4898 -- 2. The array type has no null ranges (the purpose of this is to
4899 -- avoid a bogus warning for an out-of-range value).
4901 -- 3. The array type has no atomic components
4903 -- 4. The component type is elementary
4905 -- 5. The component size is a multiple of Storage_Unit
4907 -- 6. The component size is Storage_Unit or the value is of the form
4908 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4909 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4910 -- the 8-bit value M, concatenated together.
4912 -- The ultimate goal is to generate a call to a fast memset routine
4913 -- specifically optimized for the target.
4915 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4916 Ctyp : Entity_Id;
4917 Index : Entity_Id;
4918 Expr : Node_Id := N;
4919 Low : Node_Id;
4920 High : Node_Id;
4921 Csiz : Uint;
4922 Remainder : Uint;
4923 Value : Uint;
4924 Nunits : Nat;
4926 begin
4927 -- Recurse as far as possible to find the innermost component type
4929 Ctyp := Etype (N);
4930 while Is_Array_Type (Ctyp) loop
4931 if Nkind (Expr) /= N_Aggregate
4932 or else not Is_Others_Aggregate (Expr)
4933 then
4934 return False;
4935 end if;
4937 Index := First_Index (Ctyp);
4938 while Present (Index) loop
4939 Get_Index_Bounds (Index, Low, High);
4941 if Is_Null_Range (Low, High) then
4942 return False;
4943 end if;
4945 Next_Index (Index);
4946 end loop;
4948 Expr := Expression (First (Component_Associations (Expr)));
4950 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4951 if Nkind (Expr) /= N_Aggregate
4952 or else not Is_Others_Aggregate (Expr)
4953 then
4954 return False;
4955 end if;
4957 Expr := Expression (First (Component_Associations (Expr)));
4958 end loop;
4960 if Has_Atomic_Components (Ctyp) then
4961 return False;
4962 end if;
4964 Csiz := Component_Size (Ctyp);
4965 Ctyp := Component_Type (Ctyp);
4967 if Is_Atomic_Or_VFA (Ctyp) then
4968 return False;
4969 end if;
4970 end loop;
4972 -- An Iterated_Component_Association involves a loop (in most cases)
4973 -- and is never static.
4975 if Nkind (Parent (Expr)) = N_Iterated_Component_Association then
4976 return False;
4977 end if;
4979 -- Access types need to be dealt with specially
4981 if Is_Access_Type (Ctyp) then
4983 -- Component_Size is not set by Layout_Type if the component
4984 -- type is an access type ???
4986 Csiz := Esize (Ctyp);
4988 -- Fat pointers are rejected as they are not really elementary
4989 -- for the backend.
4991 if Csiz /= System_Address_Size then
4992 return False;
4993 end if;
4995 -- The supported expressions are NULL and constants, others are
4996 -- rejected upfront to avoid being analyzed below, which can be
4997 -- problematic for some of them, for example allocators.
4999 if Nkind (Expr) /= N_Null and then not Is_Entity_Name (Expr) then
5000 return False;
5001 end if;
5003 -- Scalar types are OK if their size is a multiple of Storage_Unit
5005 elsif Is_Scalar_Type (Ctyp) then
5007 if Csiz mod System_Storage_Unit /= 0 then
5008 return False;
5009 end if;
5011 -- Composite types are rejected
5013 else
5014 return False;
5015 end if;
5017 -- The expression needs to be analyzed if True is returned
5019 Analyze_And_Resolve (Expr, Ctyp);
5021 Nunits := UI_To_Int (Csiz) / System_Storage_Unit;
5023 if Nunits = 1 then
5024 return True;
5025 end if;
5027 if not Compile_Time_Known_Value (Expr) then
5028 return False;
5029 end if;
5031 -- The only supported value for floating point is 0.0
5033 if Is_Floating_Point_Type (Ctyp) then
5034 return Expr_Value_R (Expr) = Ureal_0;
5035 end if;
5037 -- For other types, we can look into the value as an integer
5039 Value := Expr_Value (Expr);
5041 if Has_Biased_Representation (Ctyp) then
5042 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
5043 end if;
5045 -- Values 0 and -1 immediately satisfy the last check
5047 if Value = Uint_0 or else Value = Uint_Minus_1 then
5048 return True;
5049 end if;
5051 -- We need to work with an unsigned value
5053 if Value < 0 then
5054 Value := Value + 2**(System_Storage_Unit * Nunits);
5055 end if;
5057 Remainder := Value rem 2**System_Storage_Unit;
5059 for J in 1 .. Nunits - 1 loop
5060 Value := Value / 2**System_Storage_Unit;
5062 if Value rem 2**System_Storage_Unit /= Remainder then
5063 return False;
5064 end if;
5065 end loop;
5067 return True;
5068 end Aggr_Assignment_OK_For_Backend;
5070 ----------------------------
5071 -- Build_Constrained_Type --
5072 ----------------------------
5074 procedure Build_Constrained_Type (Positional : Boolean) is
5075 Loc : constant Source_Ptr := Sloc (N);
5076 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
5077 Comp : Node_Id;
5078 Decl : Node_Id;
5079 Typ : constant Entity_Id := Etype (N);
5080 Indexes : constant List_Id := New_List;
5081 Num : Nat;
5082 Sub_Agg : Node_Id;
5084 begin
5085 -- If the aggregate is purely positional, all its subaggregates
5086 -- have the same size. We collect the dimensions from the first
5087 -- subaggregate at each level.
5089 if Positional then
5090 Sub_Agg := N;
5092 for D in 1 .. Number_Dimensions (Typ) loop
5093 Sub_Agg := First (Expressions (Sub_Agg));
5095 Comp := Sub_Agg;
5096 Num := 0;
5097 while Present (Comp) loop
5098 Num := Num + 1;
5099 Next (Comp);
5100 end loop;
5102 Append_To (Indexes,
5103 Make_Range (Loc,
5104 Low_Bound => Make_Integer_Literal (Loc, 1),
5105 High_Bound => Make_Integer_Literal (Loc, Num)));
5106 end loop;
5108 else
5109 -- We know the aggregate type is unconstrained and the aggregate
5110 -- is not processable by the back end, therefore not necessarily
5111 -- positional. Retrieve each dimension bounds (computed earlier).
5113 for D in 1 .. Number_Dimensions (Typ) loop
5114 Append_To (Indexes,
5115 Make_Range (Loc,
5116 Low_Bound => Aggr_Low (D),
5117 High_Bound => Aggr_High (D)));
5118 end loop;
5119 end if;
5121 Decl :=
5122 Make_Full_Type_Declaration (Loc,
5123 Defining_Identifier => Agg_Type,
5124 Type_Definition =>
5125 Make_Constrained_Array_Definition (Loc,
5126 Discrete_Subtype_Definitions => Indexes,
5127 Component_Definition =>
5128 Make_Component_Definition (Loc,
5129 Aliased_Present => False,
5130 Subtype_Indication =>
5131 New_Occurrence_Of (Component_Type (Typ), Loc))));
5133 Insert_Action (N, Decl);
5134 Analyze (Decl);
5135 Set_Etype (N, Agg_Type);
5136 Set_Is_Itype (Agg_Type);
5137 Freeze_Itype (Agg_Type, N);
5138 end Build_Constrained_Type;
5140 ------------------
5141 -- Check_Bounds --
5142 ------------------
5144 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
5145 Aggr_Lo : Node_Id;
5146 Aggr_Hi : Node_Id;
5148 Ind_Lo : Node_Id;
5149 Ind_Hi : Node_Id;
5151 Cond : Node_Id := Empty;
5153 begin
5154 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
5155 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
5157 -- Generate the following test:
5159 -- [constraint_error when
5160 -- Aggr_Lo <= Aggr_Hi and then
5161 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5163 -- As an optimization try to see if some tests are trivially vacuous
5164 -- because we are comparing an expression against itself.
5166 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
5167 Cond := Empty;
5169 elsif Aggr_Hi = Ind_Hi then
5170 Cond :=
5171 Make_Op_Lt (Loc,
5172 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5173 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
5175 elsif Aggr_Lo = Ind_Lo then
5176 Cond :=
5177 Make_Op_Gt (Loc,
5178 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5179 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
5181 else
5182 Cond :=
5183 Make_Or_Else (Loc,
5184 Left_Opnd =>
5185 Make_Op_Lt (Loc,
5186 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5187 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
5189 Right_Opnd =>
5190 Make_Op_Gt (Loc,
5191 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5192 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
5193 end if;
5195 if Present (Cond) then
5196 Cond :=
5197 Make_And_Then (Loc,
5198 Left_Opnd =>
5199 Make_Op_Le (Loc,
5200 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5201 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
5203 Right_Opnd => Cond);
5205 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5206 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5207 Insert_Action (N,
5208 Make_Raise_Constraint_Error (Loc,
5209 Condition => Cond,
5210 Reason => CE_Range_Check_Failed));
5211 end if;
5212 end Check_Bounds;
5214 ----------------------------
5215 -- Check_Same_Aggr_Bounds --
5216 ----------------------------
5218 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5219 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
5220 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
5221 -- The bounds of this specific subaggregate
5223 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5224 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5225 -- The bounds of the aggregate for this dimension
5227 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5228 -- The index type for this dimension.xxx
5230 Cond : Node_Id := Empty;
5231 Assoc : Node_Id;
5232 Expr : Node_Id;
5234 begin
5235 -- If index checks are on generate the test
5237 -- [constraint_error when
5238 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5240 -- As an optimization try to see if some tests are trivially vacuos
5241 -- because we are comparing an expression against itself. Also for
5242 -- the first dimension the test is trivially vacuous because there
5243 -- is just one aggregate for dimension 1.
5245 if Index_Checks_Suppressed (Ind_Typ) then
5246 Cond := Empty;
5248 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5249 then
5250 Cond := Empty;
5252 elsif Aggr_Hi = Sub_Hi then
5253 Cond :=
5254 Make_Op_Ne (Loc,
5255 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5256 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5258 elsif Aggr_Lo = Sub_Lo then
5259 Cond :=
5260 Make_Op_Ne (Loc,
5261 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5262 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5264 else
5265 Cond :=
5266 Make_Or_Else (Loc,
5267 Left_Opnd =>
5268 Make_Op_Ne (Loc,
5269 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5270 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5272 Right_Opnd =>
5273 Make_Op_Ne (Loc,
5274 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5275 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5276 end if;
5278 if Present (Cond) then
5279 Insert_Action (N,
5280 Make_Raise_Constraint_Error (Loc,
5281 Condition => Cond,
5282 Reason => CE_Length_Check_Failed));
5283 end if;
5285 -- Now look inside the subaggregate to see if there is more work
5287 if Dim < Aggr_Dimension then
5289 -- Process positional components
5291 if Present (Expressions (Sub_Aggr)) then
5292 Expr := First (Expressions (Sub_Aggr));
5293 while Present (Expr) loop
5294 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5295 Next (Expr);
5296 end loop;
5297 end if;
5299 -- Process component associations
5301 if Present (Component_Associations (Sub_Aggr)) then
5302 Assoc := First (Component_Associations (Sub_Aggr));
5303 while Present (Assoc) loop
5304 Expr := Expression (Assoc);
5305 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5306 Next (Assoc);
5307 end loop;
5308 end if;
5309 end if;
5310 end Check_Same_Aggr_Bounds;
5312 ----------------------------
5313 -- Compute_Others_Present --
5314 ----------------------------
5316 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5317 Assoc : Node_Id;
5318 Expr : Node_Id;
5320 begin
5321 if Present (Component_Associations (Sub_Aggr)) then
5322 Assoc := Last (Component_Associations (Sub_Aggr));
5324 if Nkind (First (Choice_List (Assoc))) = N_Others_Choice then
5325 Others_Present (Dim) := True;
5326 end if;
5327 end if;
5329 -- Now look inside the subaggregate to see if there is more work
5331 if Dim < Aggr_Dimension then
5333 -- Process positional components
5335 if Present (Expressions (Sub_Aggr)) then
5336 Expr := First (Expressions (Sub_Aggr));
5337 while Present (Expr) loop
5338 Compute_Others_Present (Expr, Dim + 1);
5339 Next (Expr);
5340 end loop;
5341 end if;
5343 -- Process component associations
5345 if Present (Component_Associations (Sub_Aggr)) then
5346 Assoc := First (Component_Associations (Sub_Aggr));
5347 while Present (Assoc) loop
5348 Expr := Expression (Assoc);
5349 Compute_Others_Present (Expr, Dim + 1);
5350 Next (Assoc);
5351 end loop;
5352 end if;
5353 end if;
5354 end Compute_Others_Present;
5356 ------------------------
5357 -- In_Place_Assign_OK --
5358 ------------------------
5360 function In_Place_Assign_OK return Boolean is
5361 Aggr_In : Node_Id;
5362 Aggr_Lo : Node_Id;
5363 Aggr_Hi : Node_Id;
5364 Obj_In : Node_Id;
5365 Obj_Lo : Node_Id;
5366 Obj_Hi : Node_Id;
5368 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
5369 -- Check recursively that each component of a (sub)aggregate does not
5370 -- depend on the variable being assigned to.
5372 function Safe_Component (Expr : Node_Id) return Boolean;
5373 -- Verify that an expression cannot depend on the variable being
5374 -- assigned to. Room for improvement here (but less than before).
5376 --------------------
5377 -- Safe_Aggregate --
5378 --------------------
5380 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
5381 Expr : Node_Id;
5383 begin
5384 if Nkind (Parent (Aggr)) = N_Iterated_Component_Association then
5385 return False;
5386 end if;
5388 if Present (Expressions (Aggr)) then
5389 Expr := First (Expressions (Aggr));
5390 while Present (Expr) loop
5391 if Nkind (Expr) = N_Aggregate then
5392 if not Safe_Aggregate (Expr) then
5393 return False;
5394 end if;
5396 elsif not Safe_Component (Expr) then
5397 return False;
5398 end if;
5400 Next (Expr);
5401 end loop;
5402 end if;
5404 if Present (Component_Associations (Aggr)) then
5405 Expr := First (Component_Associations (Aggr));
5406 while Present (Expr) loop
5407 if Nkind (Expression (Expr)) = N_Aggregate then
5408 if not Safe_Aggregate (Expression (Expr)) then
5409 return False;
5410 end if;
5412 -- If association has a box, no way to determine yet
5413 -- whether default can be assigned in place.
5415 elsif Box_Present (Expr) then
5416 return False;
5418 elsif not Safe_Component (Expression (Expr)) then
5419 return False;
5420 end if;
5422 Next (Expr);
5423 end loop;
5424 end if;
5426 return True;
5427 end Safe_Aggregate;
5429 --------------------
5430 -- Safe_Component --
5431 --------------------
5433 function Safe_Component (Expr : Node_Id) return Boolean is
5434 Comp : Node_Id := Expr;
5436 function Check_Component (Comp : Node_Id) return Boolean;
5437 -- Do the recursive traversal, after copy
5439 ---------------------
5440 -- Check_Component --
5441 ---------------------
5443 function Check_Component (Comp : Node_Id) return Boolean is
5444 begin
5445 if Is_Overloaded (Comp) then
5446 return False;
5447 end if;
5449 return Compile_Time_Known_Value (Comp)
5451 or else (Is_Entity_Name (Comp)
5452 and then Present (Entity (Comp))
5453 and then No (Renamed_Object (Entity (Comp))))
5455 or else (Nkind (Comp) = N_Attribute_Reference
5456 and then Check_Component (Prefix (Comp)))
5458 or else (Nkind (Comp) in N_Binary_Op
5459 and then Check_Component (Left_Opnd (Comp))
5460 and then Check_Component (Right_Opnd (Comp)))
5462 or else (Nkind (Comp) in N_Unary_Op
5463 and then Check_Component (Right_Opnd (Comp)))
5465 or else (Nkind (Comp) = N_Selected_Component
5466 and then Check_Component (Prefix (Comp)))
5468 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
5469 and then Check_Component (Expression (Comp)));
5470 end Check_Component;
5472 -- Start of processing for Safe_Component
5474 begin
5475 -- If the component appears in an association that may correspond
5476 -- to more than one element, it is not analyzed before expansion
5477 -- into assignments, to avoid side effects. We analyze, but do not
5478 -- resolve the copy, to obtain sufficient entity information for
5479 -- the checks that follow. If component is overloaded we assume
5480 -- an unsafe function call.
5482 if not Analyzed (Comp) then
5483 if Is_Overloaded (Expr) then
5484 return False;
5486 elsif Nkind (Expr) = N_Aggregate
5487 and then not Is_Others_Aggregate (Expr)
5488 then
5489 return False;
5491 elsif Nkind (Expr) = N_Allocator then
5493 -- For now, too complex to analyze
5495 return False;
5496 end if;
5498 Comp := New_Copy_Tree (Expr);
5499 Set_Parent (Comp, Parent (Expr));
5500 Analyze (Comp);
5501 end if;
5503 if Nkind (Comp) = N_Aggregate then
5504 return Safe_Aggregate (Comp);
5505 else
5506 return Check_Component (Comp);
5507 end if;
5508 end Safe_Component;
5510 -- Start of processing for In_Place_Assign_OK
5512 begin
5513 if Present (Component_Associations (N)) then
5515 -- On assignment, sliding can take place, so we cannot do the
5516 -- assignment in place unless the bounds of the aggregate are
5517 -- statically equal to those of the target.
5519 -- If the aggregate is given by an others choice, the bounds are
5520 -- derived from the left-hand side, and the assignment is safe if
5521 -- the expression is.
5523 if Is_Others_Aggregate (N) then
5524 return
5525 Safe_Component
5526 (Expression (First (Component_Associations (N))));
5527 end if;
5529 Aggr_In := First_Index (Etype (N));
5531 if Nkind (Parent (N)) = N_Assignment_Statement then
5532 Obj_In := First_Index (Etype (Name (Parent (N))));
5534 else
5535 -- Context is an allocator. Check bounds of aggregate against
5536 -- given type in qualified expression.
5538 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
5539 Obj_In :=
5540 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
5541 end if;
5543 while Present (Aggr_In) loop
5544 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
5545 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
5547 if not Compile_Time_Known_Value (Aggr_Lo)
5548 or else not Compile_Time_Known_Value (Obj_Lo)
5549 or else not Compile_Time_Known_Value (Obj_Hi)
5550 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
5551 then
5552 return False;
5554 -- For an assignment statement we require static matching of
5555 -- bounds. Ditto for an allocator whose qualified expression
5556 -- is a constrained type. If the expression in the allocator
5557 -- is an unconstrained array, we accept an upper bound that
5558 -- is not static, to allow for non-static expressions of the
5559 -- base type. Clearly there are further possibilities (with
5560 -- diminishing returns) for safely building arrays in place
5561 -- here.
5563 elsif Nkind (Parent (N)) = N_Assignment_Statement
5564 or else Is_Constrained (Etype (Parent (N)))
5565 then
5566 if not Compile_Time_Known_Value (Aggr_Hi)
5567 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
5568 then
5569 return False;
5570 end if;
5571 end if;
5573 Next_Index (Aggr_In);
5574 Next_Index (Obj_In);
5575 end loop;
5576 end if;
5578 -- Now check the component values themselves
5580 return Safe_Aggregate (N);
5581 end In_Place_Assign_OK;
5583 ------------------
5584 -- Others_Check --
5585 ------------------
5587 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
5588 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5589 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5590 -- The bounds of the aggregate for this dimension
5592 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5593 -- The index type for this dimension
5595 Need_To_Check : Boolean := False;
5597 Choices_Lo : Node_Id := Empty;
5598 Choices_Hi : Node_Id := Empty;
5599 -- The lowest and highest discrete choices for a named subaggregate
5601 Nb_Choices : Int := -1;
5602 -- The number of discrete non-others choices in this subaggregate
5604 Nb_Elements : Uint := Uint_0;
5605 -- The number of elements in a positional aggregate
5607 Cond : Node_Id := Empty;
5609 Assoc : Node_Id;
5610 Choice : Node_Id;
5611 Expr : Node_Id;
5613 begin
5614 -- Check if we have an others choice. If we do make sure that this
5615 -- subaggregate contains at least one element in addition to the
5616 -- others choice.
5618 if Range_Checks_Suppressed (Ind_Typ) then
5619 Need_To_Check := False;
5621 elsif Present (Expressions (Sub_Aggr))
5622 and then Present (Component_Associations (Sub_Aggr))
5623 then
5624 Need_To_Check := True;
5626 elsif Present (Component_Associations (Sub_Aggr)) then
5627 Assoc := Last (Component_Associations (Sub_Aggr));
5629 if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then
5630 Need_To_Check := False;
5632 else
5633 -- Count the number of discrete choices. Start with -1 because
5634 -- the others choice does not count.
5636 -- Is there some reason we do not use List_Length here ???
5638 Nb_Choices := -1;
5639 Assoc := First (Component_Associations (Sub_Aggr));
5640 while Present (Assoc) loop
5641 Choice := First (Choice_List (Assoc));
5642 while Present (Choice) loop
5643 Nb_Choices := Nb_Choices + 1;
5644 Next (Choice);
5645 end loop;
5647 Next (Assoc);
5648 end loop;
5650 -- If there is only an others choice nothing to do
5652 Need_To_Check := (Nb_Choices > 0);
5653 end if;
5655 else
5656 Need_To_Check := False;
5657 end if;
5659 -- If we are dealing with a positional subaggregate with an others
5660 -- choice then compute the number or positional elements.
5662 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
5663 Expr := First (Expressions (Sub_Aggr));
5664 Nb_Elements := Uint_0;
5665 while Present (Expr) loop
5666 Nb_Elements := Nb_Elements + 1;
5667 Next (Expr);
5668 end loop;
5670 -- If the aggregate contains discrete choices and an others choice
5671 -- compute the smallest and largest discrete choice values.
5673 elsif Need_To_Check then
5674 Compute_Choices_Lo_And_Choices_Hi : declare
5676 Table : Case_Table_Type (1 .. Nb_Choices);
5677 -- Used to sort all the different choice values
5679 J : Pos := 1;
5680 Low : Node_Id;
5681 High : Node_Id;
5683 begin
5684 Assoc := First (Component_Associations (Sub_Aggr));
5685 while Present (Assoc) loop
5686 Choice := First (Choice_List (Assoc));
5687 while Present (Choice) loop
5688 if Nkind (Choice) = N_Others_Choice then
5689 exit;
5690 end if;
5692 Get_Index_Bounds (Choice, Low, High);
5693 Table (J).Choice_Lo := Low;
5694 Table (J).Choice_Hi := High;
5696 J := J + 1;
5697 Next (Choice);
5698 end loop;
5700 Next (Assoc);
5701 end loop;
5703 -- Sort the discrete choices
5705 Sort_Case_Table (Table);
5707 Choices_Lo := Table (1).Choice_Lo;
5708 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5709 end Compute_Choices_Lo_And_Choices_Hi;
5710 end if;
5712 -- If no others choice in this subaggregate, or the aggregate
5713 -- comprises only an others choice, nothing to do.
5715 if not Need_To_Check then
5716 Cond := Empty;
5718 -- If we are dealing with an aggregate containing an others choice
5719 -- and positional components, we generate the following test:
5721 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5722 -- Ind_Typ'Pos (Aggr_Hi)
5723 -- then
5724 -- raise Constraint_Error;
5725 -- end if;
5727 elsif Nb_Elements > Uint_0 then
5728 Cond :=
5729 Make_Op_Gt (Loc,
5730 Left_Opnd =>
5731 Make_Op_Add (Loc,
5732 Left_Opnd =>
5733 Make_Attribute_Reference (Loc,
5734 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5735 Attribute_Name => Name_Pos,
5736 Expressions =>
5737 New_List
5738 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5739 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5741 Right_Opnd =>
5742 Make_Attribute_Reference (Loc,
5743 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5744 Attribute_Name => Name_Pos,
5745 Expressions => New_List (
5746 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5748 -- If we are dealing with an aggregate containing an others choice
5749 -- and discrete choices we generate the following test:
5751 -- [constraint_error when
5752 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5754 else
5755 Cond :=
5756 Make_Or_Else (Loc,
5757 Left_Opnd =>
5758 Make_Op_Lt (Loc,
5759 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5760 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5762 Right_Opnd =>
5763 Make_Op_Gt (Loc,
5764 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5765 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5766 end if;
5768 if Present (Cond) then
5769 Insert_Action (N,
5770 Make_Raise_Constraint_Error (Loc,
5771 Condition => Cond,
5772 Reason => CE_Length_Check_Failed));
5773 -- Questionable reason code, shouldn't that be a
5774 -- CE_Range_Check_Failed ???
5775 end if;
5777 -- Now look inside the subaggregate to see if there is more work
5779 if Dim < Aggr_Dimension then
5781 -- Process positional components
5783 if Present (Expressions (Sub_Aggr)) then
5784 Expr := First (Expressions (Sub_Aggr));
5785 while Present (Expr) loop
5786 Others_Check (Expr, Dim + 1);
5787 Next (Expr);
5788 end loop;
5789 end if;
5791 -- Process component associations
5793 if Present (Component_Associations (Sub_Aggr)) then
5794 Assoc := First (Component_Associations (Sub_Aggr));
5795 while Present (Assoc) loop
5796 Expr := Expression (Assoc);
5797 Others_Check (Expr, Dim + 1);
5798 Next (Assoc);
5799 end loop;
5800 end if;
5801 end if;
5802 end Others_Check;
5804 -------------------------
5805 -- Safe_Left_Hand_Side --
5806 -------------------------
5808 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5809 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5810 -- If the left-hand side includes an indexed component, check that
5811 -- the indexes are free of side effects.
5813 -------------------
5814 -- Is_Safe_Index --
5815 -------------------
5817 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5818 begin
5819 if Is_Entity_Name (Indx) then
5820 return True;
5822 elsif Nkind (Indx) = N_Integer_Literal then
5823 return True;
5825 elsif Nkind (Indx) = N_Function_Call
5826 and then Is_Entity_Name (Name (Indx))
5827 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5828 then
5829 return True;
5831 elsif Nkind (Indx) = N_Type_Conversion
5832 and then Is_Safe_Index (Expression (Indx))
5833 then
5834 return True;
5836 else
5837 return False;
5838 end if;
5839 end Is_Safe_Index;
5841 -- Start of processing for Safe_Left_Hand_Side
5843 begin
5844 if Is_Entity_Name (N) then
5845 return True;
5847 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5848 and then Safe_Left_Hand_Side (Prefix (N))
5849 then
5850 return True;
5852 elsif Nkind (N) = N_Indexed_Component
5853 and then Safe_Left_Hand_Side (Prefix (N))
5854 and then Is_Safe_Index (First (Expressions (N)))
5855 then
5856 return True;
5858 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5859 return Safe_Left_Hand_Side (Expression (N));
5861 else
5862 return False;
5863 end if;
5864 end Safe_Left_Hand_Side;
5866 -- Local variables
5868 Tmp : Entity_Id;
5869 -- Holds the temporary aggregate value
5871 Tmp_Decl : Node_Id;
5872 -- Holds the declaration of Tmp
5874 Aggr_Code : List_Id;
5875 Parent_Node : Node_Id;
5876 Parent_Kind : Node_Kind;
5878 -- Start of processing for Expand_Array_Aggregate
5880 begin
5881 -- Do not touch the special aggregates of attributes used for Asm calls
5883 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5884 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5885 then
5886 return;
5888 -- Do not expand an aggregate for an array type which contains tasks if
5889 -- the aggregate is associated with an unexpanded return statement of a
5890 -- build-in-place function. The aggregate is expanded when the related
5891 -- return statement (rewritten into an extended return) is processed.
5892 -- This delay ensures that any temporaries and initialization code
5893 -- generated for the aggregate appear in the proper return block and
5894 -- use the correct _chain and _master.
5896 elsif Has_Task (Base_Type (Etype (N)))
5897 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5898 and then Is_Build_In_Place_Function
5899 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5900 then
5901 return;
5903 -- Do not attempt expansion if error already detected. We may reach this
5904 -- point in spite of previous errors when compiling with -gnatq, to
5905 -- force all possible errors (this is the usual ACATS mode).
5907 elsif Error_Posted (N) then
5908 return;
5909 end if;
5911 -- If the semantic analyzer has determined that aggregate N will raise
5912 -- Constraint_Error at run time, then the aggregate node has been
5913 -- replaced with an N_Raise_Constraint_Error node and we should
5914 -- never get here.
5916 pragma Assert (not Raises_Constraint_Error (N));
5918 -- STEP 1a
5920 -- Check that the index range defined by aggregate bounds is
5921 -- compatible with corresponding index subtype.
5923 Index_Compatibility_Check : declare
5924 Aggr_Index_Range : Node_Id := First_Index (Typ);
5925 -- The current aggregate index range
5927 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5928 -- The corresponding index constraint against which we have to
5929 -- check the above aggregate index range.
5931 begin
5932 Compute_Others_Present (N, 1);
5934 for J in 1 .. Aggr_Dimension loop
5935 -- There is no need to emit a check if an others choice is present
5936 -- for this array aggregate dimension since in this case one of
5937 -- N's subaggregates has taken its bounds from the context and
5938 -- these bounds must have been checked already. In addition all
5939 -- subaggregates corresponding to the same dimension must all have
5940 -- the same bounds (checked in (c) below).
5942 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5943 and then not Others_Present (J)
5944 then
5945 -- We don't use Checks.Apply_Range_Check here because it emits
5946 -- a spurious check. Namely it checks that the range defined by
5947 -- the aggregate bounds is nonempty. But we know this already
5948 -- if we get here.
5950 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5951 end if;
5953 -- Save the low and high bounds of the aggregate index as well as
5954 -- the index type for later use in checks (b) and (c) below.
5956 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5957 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5959 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5961 Next_Index (Aggr_Index_Range);
5962 Next_Index (Index_Constraint);
5963 end loop;
5964 end Index_Compatibility_Check;
5966 -- STEP 1b
5968 -- If an others choice is present check that no aggregate index is
5969 -- outside the bounds of the index constraint.
5971 Others_Check (N, 1);
5973 -- STEP 1c
5975 -- For multidimensional arrays make sure that all subaggregates
5976 -- corresponding to the same dimension have the same bounds.
5978 if Aggr_Dimension > 1 then
5979 Check_Same_Aggr_Bounds (N, 1);
5980 end if;
5982 -- STEP 1d
5984 -- If we have a default component value, or simple initialization is
5985 -- required for the component type, then we replace <> in component
5986 -- associations by the required default value.
5988 declare
5989 Default_Val : Node_Id;
5990 Assoc : Node_Id;
5992 begin
5993 if (Present (Default_Aspect_Component_Value (Typ))
5994 or else Needs_Simple_Initialization (Ctyp))
5995 and then Present (Component_Associations (N))
5996 then
5997 Assoc := First (Component_Associations (N));
5998 while Present (Assoc) loop
5999 if Nkind (Assoc) = N_Component_Association
6000 and then Box_Present (Assoc)
6001 then
6002 Set_Box_Present (Assoc, False);
6004 if Present (Default_Aspect_Component_Value (Typ)) then
6005 Default_Val := Default_Aspect_Component_Value (Typ);
6006 else
6007 Default_Val := Get_Simple_Init_Val (Ctyp, N);
6008 end if;
6010 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
6011 Analyze_And_Resolve (Expression (Assoc), Ctyp);
6012 end if;
6014 Next (Assoc);
6015 end loop;
6016 end if;
6017 end;
6019 -- STEP 2
6021 -- Here we test for is packed array aggregate that we can handle at
6022 -- compile time. If so, return with transformation done. Note that we do
6023 -- this even if the aggregate is nested, because once we have done this
6024 -- processing, there is no more nested aggregate.
6026 if Packed_Array_Aggregate_Handled (N) then
6027 return;
6028 end if;
6030 -- At this point we try to convert to positional form
6032 if Ekind (Current_Scope) = E_Package
6033 and then Static_Elaboration_Desired (Current_Scope)
6034 then
6035 Convert_To_Positional (N, Max_Others_Replicate => 100);
6036 else
6037 Convert_To_Positional (N);
6038 end if;
6040 -- if the result is no longer an aggregate (e.g. it may be a string
6041 -- literal, or a temporary which has the needed value), then we are
6042 -- done, since there is no longer a nested aggregate.
6044 if Nkind (N) /= N_Aggregate then
6045 return;
6047 -- We are also done if the result is an analyzed aggregate, indicating
6048 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
6049 -- aggregate.
6051 elsif Analyzed (N) and then N /= Original_Node (N) then
6052 return;
6053 end if;
6055 -- If all aggregate components are compile-time known and the aggregate
6056 -- has been flattened, nothing left to do. The same occurs if the
6057 -- aggregate is used to initialize the components of a statically
6058 -- allocated dispatch table.
6060 if Compile_Time_Known_Aggregate (N)
6061 or else Is_Static_Dispatch_Table_Aggregate (N)
6062 then
6063 Set_Expansion_Delayed (N, False);
6064 return;
6065 end if;
6067 -- Now see if back end processing is possible
6069 if Backend_Processing_Possible (N) then
6071 -- If the aggregate is static but the constraints are not, build
6072 -- a static subtype for the aggregate, so that Gigi can place it
6073 -- in static memory. Perform an unchecked_conversion to the non-
6074 -- static type imposed by the context.
6076 declare
6077 Itype : constant Entity_Id := Etype (N);
6078 Index : Node_Id;
6079 Needs_Type : Boolean := False;
6081 begin
6082 Index := First_Index (Itype);
6083 while Present (Index) loop
6084 if not Is_OK_Static_Subtype (Etype (Index)) then
6085 Needs_Type := True;
6086 exit;
6087 else
6088 Next_Index (Index);
6089 end if;
6090 end loop;
6092 if Needs_Type then
6093 Build_Constrained_Type (Positional => True);
6094 Rewrite (N, Unchecked_Convert_To (Itype, N));
6095 Analyze (N);
6096 end if;
6097 end;
6099 return;
6100 end if;
6102 -- STEP 3
6104 -- Delay expansion for nested aggregates: it will be taken care of when
6105 -- the parent aggregate is expanded.
6107 Parent_Node := Parent (N);
6108 Parent_Kind := Nkind (Parent_Node);
6110 if Parent_Kind = N_Qualified_Expression then
6111 Parent_Node := Parent (Parent_Node);
6112 Parent_Kind := Nkind (Parent_Node);
6113 end if;
6115 if Parent_Kind = N_Aggregate
6116 or else Parent_Kind = N_Extension_Aggregate
6117 or else Parent_Kind = N_Component_Association
6118 or else (Parent_Kind = N_Object_Declaration
6119 and then Needs_Finalization (Typ))
6120 or else (Parent_Kind = N_Assignment_Statement
6121 and then Inside_Init_Proc)
6122 then
6123 if Static_Array_Aggregate (N)
6124 or else Compile_Time_Known_Aggregate (N)
6125 then
6126 Set_Expansion_Delayed (N, False);
6127 return;
6128 else
6129 Set_Expansion_Delayed (N);
6130 return;
6131 end if;
6132 end if;
6134 -- STEP 4
6136 -- Look if in place aggregate expansion is possible
6138 -- For object declarations we build the aggregate in place, unless
6139 -- the array is bit-packed or the component is controlled.
6141 -- For assignments we do the assignment in place if all the component
6142 -- associations have compile-time known values. For other cases we
6143 -- create a temporary. The analysis for safety of on-line assignment
6144 -- is delicate, i.e. we don't know how to do it fully yet ???
6146 -- For allocators we assign to the designated object in place if the
6147 -- aggregate meets the same conditions as other in-place assignments.
6148 -- In this case the aggregate may not come from source but was created
6149 -- for default initialization, e.g. with Initialize_Scalars.
6151 if Requires_Transient_Scope (Typ) then
6152 Establish_Transient_Scope (N, Sec_Stack => False);
6153 end if;
6155 if Has_Default_Init_Comps (N) then
6156 Maybe_In_Place_OK := False;
6158 elsif Is_Bit_Packed_Array (Typ)
6159 or else Has_Controlled_Component (Typ)
6160 then
6161 Maybe_In_Place_OK := False;
6163 else
6164 Maybe_In_Place_OK :=
6165 (Nkind (Parent (N)) = N_Assignment_Statement
6166 and then In_Place_Assign_OK)
6168 or else
6169 (Nkind (Parent (Parent (N))) = N_Allocator
6170 and then In_Place_Assign_OK);
6171 end if;
6173 -- If this is an array of tasks, it will be expanded into build-in-place
6174 -- assignments. Build an activation chain for the tasks now.
6176 if Has_Task (Etype (N)) then
6177 Build_Activation_Chain_Entity (N);
6178 end if;
6180 -- Perform in-place expansion of aggregate in an object declaration.
6181 -- Note: actions generated for the aggregate will be captured in an
6182 -- expression-with-actions statement so that they can be transferred
6183 -- to freeze actions later if there is an address clause for the
6184 -- object. (Note: we don't use a block statement because this would
6185 -- cause generated freeze nodes to be elaborated in the wrong scope).
6187 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6188 -- expected to appear in qualified form. In-place expansion eliminates
6189 -- the qualification and eventually violates this SPARK 05 restiction.
6191 -- Should document the rest of the guards ???
6193 if not Has_Default_Init_Comps (N)
6194 and then Comes_From_Source (Parent_Node)
6195 and then Parent_Kind = N_Object_Declaration
6196 and then Present (Expression (Parent_Node))
6197 and then not
6198 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
6199 and then not Has_Controlled_Component (Typ)
6200 and then not Is_Bit_Packed_Array (Typ)
6201 and then not Restriction_Check_Required (SPARK_05)
6202 then
6203 In_Place_Assign_OK_For_Declaration := True;
6204 Tmp := Defining_Identifier (Parent_Node);
6205 Set_No_Initialization (Parent_Node);
6206 Set_Expression (Parent_Node, Empty);
6208 -- Set kind and type of the entity, for use in the analysis
6209 -- of the subsequent assignments. If the nominal type is not
6210 -- constrained, build a subtype from the known bounds of the
6211 -- aggregate. If the declaration has a subtype mark, use it,
6212 -- otherwise use the itype of the aggregate.
6214 Set_Ekind (Tmp, E_Variable);
6216 if not Is_Constrained (Typ) then
6217 Build_Constrained_Type (Positional => False);
6219 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6220 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6221 then
6222 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6224 else
6225 Set_Size_Known_At_Compile_Time (Typ, False);
6226 Set_Etype (Tmp, Typ);
6227 end if;
6229 elsif Maybe_In_Place_OK
6230 and then Nkind (Parent (N)) = N_Qualified_Expression
6231 and then Nkind (Parent (Parent (N))) = N_Allocator
6232 then
6233 Set_Expansion_Delayed (N);
6234 return;
6236 -- In the remaining cases the aggregate is the RHS of an assignment
6238 elsif Maybe_In_Place_OK
6239 and then Safe_Left_Hand_Side (Name (Parent (N)))
6240 then
6241 Tmp := Name (Parent (N));
6243 if Etype (Tmp) /= Etype (N) then
6244 Apply_Length_Check (N, Etype (Tmp));
6246 if Nkind (N) = N_Raise_Constraint_Error then
6248 -- Static error, nothing further to expand
6250 return;
6251 end if;
6252 end if;
6254 -- If a slice assignment has an aggregate with a single others_choice,
6255 -- the assignment can be done in place even if bounds are not static,
6256 -- by converting it into a loop over the discrete range of the slice.
6258 elsif Maybe_In_Place_OK
6259 and then Nkind (Name (Parent (N))) = N_Slice
6260 and then Is_Others_Aggregate (N)
6261 then
6262 Tmp := Name (Parent (N));
6264 -- Set type of aggregate to be type of lhs in assignment, in order
6265 -- to suppress redundant length checks.
6267 Set_Etype (N, Etype (Tmp));
6269 -- Step 5
6271 -- In place aggregate expansion is not possible
6273 else
6274 Maybe_In_Place_OK := False;
6275 Tmp := Make_Temporary (Loc, 'A', N);
6276 Tmp_Decl :=
6277 Make_Object_Declaration (Loc,
6278 Defining_Identifier => Tmp,
6279 Object_Definition => New_Occurrence_Of (Typ, Loc));
6280 Set_No_Initialization (Tmp_Decl, True);
6282 -- If we are within a loop, the temporary will be pushed on the
6283 -- stack at each iteration. If the aggregate is the expression for an
6284 -- allocator, it will be immediately copied to the heap and can
6285 -- be reclaimed at once. We create a transient scope around the
6286 -- aggregate for this purpose.
6288 if Ekind (Current_Scope) = E_Loop
6289 and then Nkind (Parent (Parent (N))) = N_Allocator
6290 then
6291 Establish_Transient_Scope (N, Sec_Stack => False);
6292 end if;
6294 Insert_Action (N, Tmp_Decl);
6295 end if;
6297 -- Construct and insert the aggregate code. We can safely suppress index
6298 -- checks because this code is guaranteed not to raise CE on index
6299 -- checks. However we should *not* suppress all checks.
6301 declare
6302 Target : Node_Id;
6304 begin
6305 if Nkind (Tmp) = N_Defining_Identifier then
6306 Target := New_Occurrence_Of (Tmp, Loc);
6308 else
6309 if Has_Default_Init_Comps (N) then
6311 -- Ada 2005 (AI-287): This case has not been analyzed???
6313 raise Program_Error;
6314 end if;
6316 -- Name in assignment is explicit dereference
6318 Target := New_Copy (Tmp);
6319 end if;
6321 -- If we are to generate an in place assignment for a declaration or
6322 -- an assignment statement, and the assignment can be done directly
6323 -- by the back end, then do not expand further.
6325 -- ??? We can also do that if in place expansion is not possible but
6326 -- then we could go into an infinite recursion.
6328 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6329 and then not CodePeer_Mode
6330 and then not Modify_Tree_For_C
6331 and then not Possible_Bit_Aligned_Component (Target)
6332 and then not Is_Possibly_Unaligned_Slice (Target)
6333 and then Aggr_Assignment_OK_For_Backend (N)
6334 then
6335 if Maybe_In_Place_OK then
6336 return;
6337 end if;
6339 Aggr_Code :=
6340 New_List (
6341 Make_Assignment_Statement (Loc,
6342 Name => Target,
6343 Expression => New_Copy_Tree (N)));
6345 else
6346 Aggr_Code :=
6347 Build_Array_Aggr_Code (N,
6348 Ctype => Ctyp,
6349 Index => First_Index (Typ),
6350 Into => Target,
6351 Scalar_Comp => Is_Scalar_Type (Ctyp));
6352 end if;
6354 -- Save the last assignment statement associated with the aggregate
6355 -- when building a controlled object. This reference is utilized by
6356 -- the finalization machinery when marking an object as successfully
6357 -- initialized.
6359 if Needs_Finalization (Typ)
6360 and then Is_Entity_Name (Target)
6361 and then Present (Entity (Target))
6362 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6363 then
6364 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6365 end if;
6366 end;
6368 -- If the aggregate is the expression in a declaration, the expanded
6369 -- code must be inserted after it. The defining entity might not come
6370 -- from source if this is part of an inlined body, but the declaration
6371 -- itself will.
6373 if Comes_From_Source (Tmp)
6374 or else
6375 (Nkind (Parent (N)) = N_Object_Declaration
6376 and then Comes_From_Source (Parent (N))
6377 and then Tmp = Defining_Entity (Parent (N)))
6378 then
6379 declare
6380 Node_After : constant Node_Id := Next (Parent_Node);
6382 begin
6383 Insert_Actions_After (Parent_Node, Aggr_Code);
6385 if Parent_Kind = N_Object_Declaration then
6386 Collect_Initialization_Statements
6387 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
6388 end if;
6389 end;
6391 else
6392 Insert_Actions (N, Aggr_Code);
6393 end if;
6395 -- If the aggregate has been assigned in place, remove the original
6396 -- assignment.
6398 if Nkind (Parent (N)) = N_Assignment_Statement
6399 and then Maybe_In_Place_OK
6400 then
6401 Rewrite (Parent (N), Make_Null_Statement (Loc));
6403 elsif Nkind (Parent (N)) /= N_Object_Declaration
6404 or else Tmp /= Defining_Identifier (Parent (N))
6405 then
6406 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6407 Analyze_And_Resolve (N, Typ);
6408 end if;
6409 end Expand_Array_Aggregate;
6411 ------------------------
6412 -- Expand_N_Aggregate --
6413 ------------------------
6415 procedure Expand_N_Aggregate (N : Node_Id) is
6416 begin
6417 -- Record aggregate case
6419 if Is_Record_Type (Etype (N)) then
6420 Expand_Record_Aggregate (N);
6422 -- Array aggregate case
6424 else
6425 -- A special case, if we have a string subtype with bounds 1 .. N,
6426 -- where N is known at compile time, and the aggregate is of the
6427 -- form (others => 'x'), with a single choice and no expressions,
6428 -- and N is less than 80 (an arbitrary limit for now), then replace
6429 -- the aggregate by the equivalent string literal (but do not mark
6430 -- it as static since it is not).
6432 -- Note: this entire circuit is redundant with respect to code in
6433 -- Expand_Array_Aggregate that collapses others choices to positional
6434 -- form, but there are two problems with that circuit:
6436 -- a) It is limited to very small cases due to ill-understood
6437 -- interactions with bootstrapping. That limit is removed by
6438 -- use of the No_Implicit_Loops restriction.
6440 -- b) It incorrectly ends up with the resulting expressions being
6441 -- considered static when they are not. For example, the
6442 -- following test should fail:
6444 -- pragma Restrictions (No_Implicit_Loops);
6445 -- package NonSOthers4 is
6446 -- B : constant String (1 .. 6) := (others => 'A');
6447 -- DH : constant String (1 .. 8) := B & "BB";
6448 -- X : Integer;
6449 -- pragma Export (C, X, Link_Name => DH);
6450 -- end;
6452 -- But it succeeds (DH looks static to pragma Export)
6454 -- To be sorted out ???
6456 if Present (Component_Associations (N)) then
6457 declare
6458 CA : constant Node_Id := First (Component_Associations (N));
6459 MX : constant := 80;
6461 begin
6462 if Nkind (First (Choice_List (CA))) = N_Others_Choice
6463 and then Nkind (Expression (CA)) = N_Character_Literal
6464 and then No (Expressions (N))
6465 then
6466 declare
6467 T : constant Entity_Id := Etype (N);
6468 X : constant Node_Id := First_Index (T);
6469 EC : constant Node_Id := Expression (CA);
6470 CV : constant Uint := Char_Literal_Value (EC);
6471 CC : constant Int := UI_To_Int (CV);
6473 begin
6474 if Nkind (X) = N_Range
6475 and then Compile_Time_Known_Value (Low_Bound (X))
6476 and then Expr_Value (Low_Bound (X)) = 1
6477 and then Compile_Time_Known_Value (High_Bound (X))
6478 then
6479 declare
6480 Hi : constant Uint := Expr_Value (High_Bound (X));
6482 begin
6483 if Hi <= MX then
6484 Start_String;
6486 for J in 1 .. UI_To_Int (Hi) loop
6487 Store_String_Char (Char_Code (CC));
6488 end loop;
6490 Rewrite (N,
6491 Make_String_Literal (Sloc (N),
6492 Strval => End_String));
6494 if CC >= Int (2 ** 16) then
6495 Set_Has_Wide_Wide_Character (N);
6496 elsif CC >= Int (2 ** 8) then
6497 Set_Has_Wide_Character (N);
6498 end if;
6500 Analyze_And_Resolve (N, T);
6501 Set_Is_Static_Expression (N, False);
6502 return;
6503 end if;
6504 end;
6505 end if;
6506 end;
6507 end if;
6508 end;
6509 end if;
6511 -- Not that special case, so normal expansion of array aggregate
6513 Expand_Array_Aggregate (N);
6514 end if;
6516 exception
6517 when RE_Not_Available =>
6518 return;
6519 end Expand_N_Aggregate;
6521 ------------------------------
6522 -- Expand_N_Delta_Aggregate --
6523 ------------------------------
6525 procedure Expand_N_Delta_Aggregate (N : Node_Id) is
6526 Loc : constant Source_Ptr := Sloc (N);
6527 Typ : constant Entity_Id := Etype (N);
6528 Decl : Node_Id;
6530 begin
6531 Decl :=
6532 Make_Object_Declaration (Loc,
6533 Defining_Identifier => Make_Temporary (Loc, 'T'),
6534 Object_Definition => New_Occurrence_Of (Typ, Loc),
6535 Expression => New_Copy_Tree (Expression (N)));
6537 if Is_Array_Type (Etype (N)) then
6538 Expand_Delta_Array_Aggregate (N, New_List (Decl));
6539 else
6540 Expand_Delta_Record_Aggregate (N, New_List (Decl));
6541 end if;
6542 end Expand_N_Delta_Aggregate;
6544 ----------------------------------
6545 -- Expand_Delta_Array_Aggregate --
6546 ----------------------------------
6548 procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is
6549 Loc : constant Source_Ptr := Sloc (N);
6550 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6551 Assoc : Node_Id;
6553 function Generate_Loop (C : Node_Id) return Node_Id;
6554 -- Generate a loop containing individual component assignments for
6555 -- choices that are ranges, subtype indications, subtype names, and
6556 -- iterated component associations.
6558 -------------------
6559 -- Generate_Loop --
6560 -------------------
6562 function Generate_Loop (C : Node_Id) return Node_Id is
6563 Sl : constant Source_Ptr := Sloc (C);
6564 Ix : Entity_Id;
6566 begin
6567 if Nkind (Parent (C)) = N_Iterated_Component_Association then
6568 Ix :=
6569 Make_Defining_Identifier (Loc,
6570 Chars => (Chars (Defining_Identifier (Parent (C)))));
6571 else
6572 Ix := Make_Temporary (Sl, 'I');
6573 end if;
6575 return
6576 Make_Loop_Statement (Loc,
6577 Iteration_Scheme =>
6578 Make_Iteration_Scheme (Sl,
6579 Loop_Parameter_Specification =>
6580 Make_Loop_Parameter_Specification (Sl,
6581 Defining_Identifier => Ix,
6582 Discrete_Subtype_Definition => New_Copy_Tree (C))),
6584 Statements => New_List (
6585 Make_Assignment_Statement (Sl,
6586 Name =>
6587 Make_Indexed_Component (Sl,
6588 Prefix => New_Occurrence_Of (Temp, Sl),
6589 Expressions => New_List (New_Occurrence_Of (Ix, Sl))),
6590 Expression => New_Copy_Tree (Expression (Assoc)))),
6591 End_Label => Empty);
6592 end Generate_Loop;
6594 -- Local variables
6596 Choice : Node_Id;
6598 -- Start of processing for Expand_Delta_Array_Aggregate
6600 begin
6601 Assoc := First (Component_Associations (N));
6602 while Present (Assoc) loop
6603 Choice := First (Choice_List (Assoc));
6604 if Nkind (Assoc) = N_Iterated_Component_Association then
6605 while Present (Choice) loop
6606 Append_To (Deltas, Generate_Loop (Choice));
6607 Next (Choice);
6608 end loop;
6610 else
6611 while Present (Choice) loop
6613 -- Choice can be given by a range, a subtype indication, a
6614 -- subtype name, a scalar value, or an entity.
6616 if Nkind (Choice) = N_Range
6617 or else (Is_Entity_Name (Choice)
6618 and then Is_Type (Entity (Choice)))
6619 then
6620 Append_To (Deltas, Generate_Loop (Choice));
6622 elsif Nkind (Choice) = N_Subtype_Indication then
6623 Append_To (Deltas,
6624 Generate_Loop (Range_Expression (Constraint (Choice))));
6626 else
6627 Append_To (Deltas,
6628 Make_Assignment_Statement (Sloc (Choice),
6629 Name =>
6630 Make_Indexed_Component (Sloc (Choice),
6631 Prefix => New_Occurrence_Of (Temp, Loc),
6632 Expressions => New_List (New_Copy_Tree (Choice))),
6633 Expression => New_Copy_Tree (Expression (Assoc))));
6634 end if;
6636 Next (Choice);
6637 end loop;
6638 end if;
6640 Next (Assoc);
6641 end loop;
6643 Insert_Actions (N, Deltas);
6644 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6645 end Expand_Delta_Array_Aggregate;
6647 -----------------------------------
6648 -- Expand_Delta_Record_Aggregate --
6649 -----------------------------------
6651 procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is
6652 Loc : constant Source_Ptr := Sloc (N);
6653 Temp : constant Entity_Id := Defining_Identifier (First (Deltas));
6654 Assoc : Node_Id;
6655 Choice : Node_Id;
6657 begin
6658 Assoc := First (Component_Associations (N));
6660 while Present (Assoc) loop
6661 Choice := First (Choice_List (Assoc));
6662 while Present (Choice) loop
6663 Append_To (Deltas,
6664 Make_Assignment_Statement (Sloc (Choice),
6665 Name =>
6666 Make_Selected_Component (Sloc (Choice),
6667 Prefix => New_Occurrence_Of (Temp, Loc),
6668 Selector_Name => Make_Identifier (Loc, Chars (Choice))),
6669 Expression => New_Copy_Tree (Expression (Assoc))));
6670 Next (Choice);
6671 end loop;
6673 Next (Assoc);
6674 end loop;
6676 Insert_Actions (N, Deltas);
6677 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6678 end Expand_Delta_Record_Aggregate;
6680 ----------------------------------
6681 -- Expand_N_Extension_Aggregate --
6682 ----------------------------------
6684 -- If the ancestor part is an expression, add a component association for
6685 -- the parent field. If the type of the ancestor part is not the direct
6686 -- parent of the expected type, build recursively the needed ancestors.
6687 -- If the ancestor part is a subtype_mark, replace aggregate with a
6688 -- declaration for a temporary of the expected type, followed by
6689 -- individual assignments to the given components.
6691 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
6692 A : constant Node_Id := Ancestor_Part (N);
6693 Loc : constant Source_Ptr := Sloc (N);
6694 Typ : constant Entity_Id := Etype (N);
6696 begin
6697 -- If the ancestor is a subtype mark, an init proc must be called
6698 -- on the resulting object which thus has to be materialized in
6699 -- the front-end
6701 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
6702 Convert_To_Assignments (N, Typ);
6704 -- The extension aggregate is transformed into a record aggregate
6705 -- of the following form (c1 and c2 are inherited components)
6707 -- (Exp with c3 => a, c4 => b)
6708 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6710 else
6711 Set_Etype (N, Typ);
6713 if Tagged_Type_Expansion then
6714 Expand_Record_Aggregate (N,
6715 Orig_Tag =>
6716 New_Occurrence_Of
6717 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
6718 Parent_Expr => A);
6720 -- No tag is needed in the case of a VM
6722 else
6723 Expand_Record_Aggregate (N, Parent_Expr => A);
6724 end if;
6725 end if;
6727 exception
6728 when RE_Not_Available =>
6729 return;
6730 end Expand_N_Extension_Aggregate;
6732 -----------------------------
6733 -- Expand_Record_Aggregate --
6734 -----------------------------
6736 procedure Expand_Record_Aggregate
6737 (N : Node_Id;
6738 Orig_Tag : Node_Id := Empty;
6739 Parent_Expr : Node_Id := Empty)
6741 Loc : constant Source_Ptr := Sloc (N);
6742 Comps : constant List_Id := Component_Associations (N);
6743 Typ : constant Entity_Id := Etype (N);
6744 Base_Typ : constant Entity_Id := Base_Type (Typ);
6746 Static_Components : Boolean := True;
6747 -- Flag to indicate whether all components are compile-time known,
6748 -- and the aggregate can be constructed statically and handled by
6749 -- the back-end. Set to False by Component_OK_For_Backend.
6751 procedure Build_Back_End_Aggregate;
6752 -- Build a proper aggregate to be handled by the back-end
6754 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
6755 -- Returns true if N is an expression of composite type which can be
6756 -- fully evaluated at compile time without raising constraint error.
6757 -- Such expressions can be passed as is to Gigi without any expansion.
6759 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6760 -- set and constants whose expression is such an aggregate, recursively.
6762 function Component_OK_For_Backend return Boolean;
6763 -- Check for presence of a component which makes it impossible for the
6764 -- backend to process the aggregate, thus requiring the use of a series
6765 -- of assignment statements. Cases checked for are a nested aggregate
6766 -- needing Late_Expansion, the presence of a tagged component which may
6767 -- need tag adjustment, and a bit unaligned component reference.
6769 -- We also force expansion into assignments if a component is of a
6770 -- mutable type (including a private type with discriminants) because
6771 -- in that case the size of the component to be copied may be smaller
6772 -- than the side of the target, and there is no simple way for gigi
6773 -- to compute the size of the object to be copied.
6775 -- NOTE: This is part of the ongoing work to define precisely the
6776 -- interface between front-end and back-end handling of aggregates.
6777 -- In general it is desirable to pass aggregates as they are to gigi,
6778 -- in order to minimize elaboration code. This is one case where the
6779 -- semantics of Ada complicate the analysis and lead to anomalies in
6780 -- the gcc back-end if the aggregate is not expanded into assignments.
6782 -- NOTE: This sets the global Static_Components to False in most, but
6783 -- not all, cases when it returns False.
6785 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
6786 -- Return True if any element of L has Has_Per_Object_Constraint set.
6787 -- L should be the Choices component of an N_Component_Association.
6789 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
6790 -- If any ancestor of the current type is private, the aggregate
6791 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6792 -- because it will not be set when type and its parent are in the
6793 -- same scope, and the parent component needs expansion.
6795 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
6796 -- For nested aggregates return the ultimate enclosing aggregate; for
6797 -- non-nested aggregates return N.
6799 ------------------------------
6800 -- Build_Back_End_Aggregate --
6801 ------------------------------
6803 procedure Build_Back_End_Aggregate is
6804 Comp : Entity_Id;
6805 New_Comp : Node_Id;
6806 Tag_Value : Node_Id;
6808 begin
6809 if Nkind (N) = N_Aggregate then
6811 -- If the aggregate is static and can be handled by the back-end,
6812 -- nothing left to do.
6814 if Static_Components then
6815 Set_Compile_Time_Known_Aggregate (N);
6816 Set_Expansion_Delayed (N, False);
6817 end if;
6818 end if;
6820 -- If no discriminants, nothing special to do
6822 if not Has_Discriminants (Typ) then
6823 null;
6825 -- Case of discriminants present
6827 elsif Is_Derived_Type (Typ) then
6829 -- For untagged types, non-stored discriminants are replaced with
6830 -- stored discriminants, which are the ones that gigi uses to
6831 -- describe the type and its components.
6833 Generate_Aggregate_For_Derived_Type : declare
6834 procedure Prepend_Stored_Values (T : Entity_Id);
6835 -- Scan the list of stored discriminants of the type, and add
6836 -- their values to the aggregate being built.
6838 ---------------------------
6839 -- Prepend_Stored_Values --
6840 ---------------------------
6842 procedure Prepend_Stored_Values (T : Entity_Id) is
6843 Discr : Entity_Id;
6844 First_Comp : Node_Id := Empty;
6846 begin
6847 Discr := First_Stored_Discriminant (T);
6848 while Present (Discr) loop
6849 New_Comp :=
6850 Make_Component_Association (Loc,
6851 Choices => New_List (
6852 New_Occurrence_Of (Discr, Loc)),
6853 Expression =>
6854 New_Copy_Tree
6855 (Get_Discriminant_Value
6856 (Discr,
6857 Typ,
6858 Discriminant_Constraint (Typ))));
6860 if No (First_Comp) then
6861 Prepend_To (Component_Associations (N), New_Comp);
6862 else
6863 Insert_After (First_Comp, New_Comp);
6864 end if;
6866 First_Comp := New_Comp;
6867 Next_Stored_Discriminant (Discr);
6868 end loop;
6869 end Prepend_Stored_Values;
6871 -- Local variables
6873 Constraints : constant List_Id := New_List;
6875 Discr : Entity_Id;
6876 Decl : Node_Id;
6877 Num_Disc : Nat := 0;
6878 Num_Gird : Nat := 0;
6880 -- Start of processing for Generate_Aggregate_For_Derived_Type
6882 begin
6883 -- Remove the associations for the discriminant of derived type
6885 declare
6886 First_Comp : Node_Id;
6888 begin
6889 First_Comp := First (Component_Associations (N));
6890 while Present (First_Comp) loop
6891 Comp := First_Comp;
6892 Next (First_Comp);
6894 if Ekind (Entity (First (Choices (Comp)))) =
6895 E_Discriminant
6896 then
6897 Remove (Comp);
6898 Num_Disc := Num_Disc + 1;
6899 end if;
6900 end loop;
6901 end;
6903 -- Insert stored discriminant associations in the correct
6904 -- order. If there are more stored discriminants than new
6905 -- discriminants, there is at least one new discriminant that
6906 -- constrains more than one of the stored discriminants. In
6907 -- this case we need to construct a proper subtype of the
6908 -- parent type, in order to supply values to all the
6909 -- components. Otherwise there is one-one correspondence
6910 -- between the constraints and the stored discriminants.
6912 Discr := First_Stored_Discriminant (Base_Type (Typ));
6913 while Present (Discr) loop
6914 Num_Gird := Num_Gird + 1;
6915 Next_Stored_Discriminant (Discr);
6916 end loop;
6918 -- Case of more stored discriminants than new discriminants
6920 if Num_Gird > Num_Disc then
6922 -- Create a proper subtype of the parent type, which is the
6923 -- proper implementation type for the aggregate, and convert
6924 -- it to the intended target type.
6926 Discr := First_Stored_Discriminant (Base_Type (Typ));
6927 while Present (Discr) loop
6928 New_Comp :=
6929 New_Copy_Tree
6930 (Get_Discriminant_Value
6931 (Discr,
6932 Typ,
6933 Discriminant_Constraint (Typ)));
6935 Append (New_Comp, Constraints);
6936 Next_Stored_Discriminant (Discr);
6937 end loop;
6939 Decl :=
6940 Make_Subtype_Declaration (Loc,
6941 Defining_Identifier => Make_Temporary (Loc, 'T'),
6942 Subtype_Indication =>
6943 Make_Subtype_Indication (Loc,
6944 Subtype_Mark =>
6945 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
6946 Constraint =>
6947 Make_Index_Or_Discriminant_Constraint
6948 (Loc, Constraints)));
6950 Insert_Action (N, Decl);
6951 Prepend_Stored_Values (Base_Type (Typ));
6953 Set_Etype (N, Defining_Identifier (Decl));
6954 Set_Analyzed (N);
6956 Rewrite (N, Unchecked_Convert_To (Typ, N));
6957 Analyze (N);
6959 -- Case where we do not have fewer new discriminants than
6960 -- stored discriminants, so in this case we can simply use the
6961 -- stored discriminants of the subtype.
6963 else
6964 Prepend_Stored_Values (Typ);
6965 end if;
6966 end Generate_Aggregate_For_Derived_Type;
6967 end if;
6969 if Is_Tagged_Type (Typ) then
6971 -- In the tagged case, _parent and _tag component must be created
6973 -- Reset Null_Present unconditionally. Tagged records always have
6974 -- at least one field (the tag or the parent).
6976 Set_Null_Record_Present (N, False);
6978 -- When the current aggregate comes from the expansion of an
6979 -- extension aggregate, the parent expr is replaced by an
6980 -- aggregate formed by selected components of this expr.
6982 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
6983 Comp := First_Component_Or_Discriminant (Typ);
6984 while Present (Comp) loop
6986 -- Skip all expander-generated components
6988 if not Comes_From_Source (Original_Record_Component (Comp))
6989 then
6990 null;
6992 else
6993 New_Comp :=
6994 Make_Selected_Component (Loc,
6995 Prefix =>
6996 Unchecked_Convert_To (Typ,
6997 Duplicate_Subexpr (Parent_Expr, True)),
6998 Selector_Name => New_Occurrence_Of (Comp, Loc));
7000 Append_To (Comps,
7001 Make_Component_Association (Loc,
7002 Choices => New_List (
7003 New_Occurrence_Of (Comp, Loc)),
7004 Expression => New_Comp));
7006 Analyze_And_Resolve (New_Comp, Etype (Comp));
7007 end if;
7009 Next_Component_Or_Discriminant (Comp);
7010 end loop;
7011 end if;
7013 -- Compute the value for the Tag now, if the type is a root it
7014 -- will be included in the aggregate right away, otherwise it will
7015 -- be propagated to the parent aggregate.
7017 if Present (Orig_Tag) then
7018 Tag_Value := Orig_Tag;
7020 elsif not Tagged_Type_Expansion then
7021 Tag_Value := Empty;
7023 else
7024 Tag_Value :=
7025 New_Occurrence_Of
7026 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
7027 end if;
7029 -- For a derived type, an aggregate for the parent is formed with
7030 -- all the inherited components.
7032 if Is_Derived_Type (Typ) then
7033 declare
7034 First_Comp : Node_Id;
7035 Parent_Comps : List_Id;
7036 Parent_Aggr : Node_Id;
7037 Parent_Name : Node_Id;
7039 begin
7040 -- Remove the inherited component association from the
7041 -- aggregate and store them in the parent aggregate
7043 First_Comp := First (Component_Associations (N));
7044 Parent_Comps := New_List;
7045 while Present (First_Comp)
7046 and then
7047 Scope (Original_Record_Component
7048 (Entity (First (Choices (First_Comp))))) /=
7049 Base_Typ
7050 loop
7051 Comp := First_Comp;
7052 Next (First_Comp);
7053 Remove (Comp);
7054 Append (Comp, Parent_Comps);
7055 end loop;
7057 Parent_Aggr :=
7058 Make_Aggregate (Loc,
7059 Component_Associations => Parent_Comps);
7060 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
7062 -- Find the _parent component
7064 Comp := First_Component (Typ);
7065 while Chars (Comp) /= Name_uParent loop
7066 Comp := Next_Component (Comp);
7067 end loop;
7069 Parent_Name := New_Occurrence_Of (Comp, Loc);
7071 -- Insert the parent aggregate
7073 Prepend_To (Component_Associations (N),
7074 Make_Component_Association (Loc,
7075 Choices => New_List (Parent_Name),
7076 Expression => Parent_Aggr));
7078 -- Expand recursively the parent propagating the right Tag
7080 Expand_Record_Aggregate
7081 (Parent_Aggr, Tag_Value, Parent_Expr);
7083 -- The ancestor part may be a nested aggregate that has
7084 -- delayed expansion: recheck now.
7086 if not Component_OK_For_Backend then
7087 Convert_To_Assignments (N, Typ);
7088 end if;
7089 end;
7091 -- For a root type, the tag component is added (unless compiling
7092 -- for the VMs, where tags are implicit).
7094 elsif Tagged_Type_Expansion then
7095 declare
7096 Tag_Name : constant Node_Id :=
7097 New_Occurrence_Of
7098 (First_Tag_Component (Typ), Loc);
7099 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7100 Conv_Node : constant Node_Id :=
7101 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7103 begin
7104 Set_Etype (Conv_Node, Typ_Tag);
7105 Prepend_To (Component_Associations (N),
7106 Make_Component_Association (Loc,
7107 Choices => New_List (Tag_Name),
7108 Expression => Conv_Node));
7109 end;
7110 end if;
7111 end if;
7112 end Build_Back_End_Aggregate;
7114 ----------------------------------------
7115 -- Compile_Time_Known_Composite_Value --
7116 ----------------------------------------
7118 function Compile_Time_Known_Composite_Value
7119 (N : Node_Id) return Boolean
7121 begin
7122 -- If we have an entity name, then see if it is the name of a
7123 -- constant and if so, test the corresponding constant value.
7125 if Is_Entity_Name (N) then
7126 declare
7127 E : constant Entity_Id := Entity (N);
7128 V : Node_Id;
7129 begin
7130 if Ekind (E) /= E_Constant then
7131 return False;
7132 else
7133 V := Constant_Value (E);
7134 return Present (V)
7135 and then Compile_Time_Known_Composite_Value (V);
7136 end if;
7137 end;
7139 -- We have a value, see if it is compile time known
7141 else
7142 if Nkind (N) = N_Aggregate then
7143 return Compile_Time_Known_Aggregate (N);
7144 end if;
7146 -- All other types of values are not known at compile time
7148 return False;
7149 end if;
7151 end Compile_Time_Known_Composite_Value;
7153 ------------------------------
7154 -- Component_OK_For_Backend --
7155 ------------------------------
7157 function Component_OK_For_Backend return Boolean is
7158 C : Node_Id;
7159 Expr_Q : Node_Id;
7161 begin
7162 if No (Comps) then
7163 return True;
7164 end if;
7166 C := First (Comps);
7167 while Present (C) loop
7169 -- If the component has box initialization, expansion is needed
7170 -- and component is not ready for backend.
7172 if Box_Present (C) then
7173 return False;
7174 end if;
7176 if Nkind (Expression (C)) = N_Qualified_Expression then
7177 Expr_Q := Expression (Expression (C));
7178 else
7179 Expr_Q := Expression (C);
7180 end if;
7182 -- Return False if the aggregate has any associations for tagged
7183 -- components that may require tag adjustment.
7185 -- These are cases where the source expression may have a tag that
7186 -- could differ from the component tag (e.g., can occur for type
7187 -- conversions and formal parameters). (Tag adjustment not needed
7188 -- if Tagged_Type_Expansion because object tags are implicit in
7189 -- the machine.)
7191 if Is_Tagged_Type (Etype (Expr_Q))
7192 and then (Nkind (Expr_Q) = N_Type_Conversion
7193 or else (Is_Entity_Name (Expr_Q)
7194 and then
7195 Ekind (Entity (Expr_Q)) in Formal_Kind))
7196 and then Tagged_Type_Expansion
7197 then
7198 Static_Components := False;
7199 return False;
7201 elsif Is_Delayed_Aggregate (Expr_Q) then
7202 Static_Components := False;
7203 return False;
7205 elsif Possible_Bit_Aligned_Component (Expr_Q) then
7206 Static_Components := False;
7207 return False;
7209 elsif Modify_Tree_For_C
7210 and then Nkind (C) = N_Component_Association
7211 and then Has_Per_Object_Constraint (Choices (C))
7212 then
7213 Static_Components := False;
7214 return False;
7216 elsif Modify_Tree_For_C
7217 and then Nkind (Expr_Q) = N_Identifier
7218 and then Is_Array_Type (Etype (Expr_Q))
7219 then
7220 Static_Components := False;
7221 return False;
7223 elsif Modify_Tree_For_C
7224 and then Nkind (Expr_Q) = N_Type_Conversion
7225 and then Is_Array_Type (Etype (Expr_Q))
7226 then
7227 Static_Components := False;
7228 return False;
7229 end if;
7231 if Is_Elementary_Type (Etype (Expr_Q)) then
7232 if not Compile_Time_Known_Value (Expr_Q) then
7233 Static_Components := False;
7234 end if;
7236 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
7237 Static_Components := False;
7239 if Is_Private_Type (Etype (Expr_Q))
7240 and then Has_Discriminants (Etype (Expr_Q))
7241 then
7242 return False;
7243 end if;
7244 end if;
7246 Next (C);
7247 end loop;
7249 return True;
7250 end Component_OK_For_Backend;
7252 -------------------------------
7253 -- Has_Per_Object_Constraint --
7254 -------------------------------
7256 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
7257 N : Node_Id := First (L);
7258 begin
7259 while Present (N) loop
7260 if Is_Entity_Name (N)
7261 and then Present (Entity (N))
7262 and then Has_Per_Object_Constraint (Entity (N))
7263 then
7264 return True;
7265 end if;
7267 Next (N);
7268 end loop;
7270 return False;
7271 end Has_Per_Object_Constraint;
7273 -----------------------------------
7274 -- Has_Visible_Private_Ancestor --
7275 -----------------------------------
7277 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
7278 R : constant Entity_Id := Root_Type (Id);
7279 T1 : Entity_Id := Id;
7281 begin
7282 loop
7283 if Is_Private_Type (T1) then
7284 return True;
7286 elsif T1 = R then
7287 return False;
7289 else
7290 T1 := Etype (T1);
7291 end if;
7292 end loop;
7293 end Has_Visible_Private_Ancestor;
7295 -------------------------
7296 -- Top_Level_Aggregate --
7297 -------------------------
7299 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
7300 Aggr : Node_Id;
7302 begin
7303 Aggr := N;
7304 while Present (Parent (Aggr))
7305 and then Nkind_In (Parent (Aggr), N_Aggregate,
7306 N_Component_Association)
7307 loop
7308 Aggr := Parent (Aggr);
7309 end loop;
7311 return Aggr;
7312 end Top_Level_Aggregate;
7314 -- Local variables
7316 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
7318 -- Start of processing for Expand_Record_Aggregate
7320 begin
7321 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
7322 -- to prevent a piecemeal assignment even if the aggregate is to be
7323 -- expanded. We create a temporary for the aggregate, and assign the
7324 -- temporary instead, so that the back end can generate an atomic move
7325 -- for it.
7327 if Is_Atomic_VFA_Aggregate (N) then
7328 return;
7330 -- No special management required for aggregates used to initialize
7331 -- statically allocated dispatch tables
7333 elsif Is_Static_Dispatch_Table_Aggregate (N) then
7334 return;
7335 end if;
7337 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
7338 -- are build-in-place function calls. The assignments will each turn
7339 -- into a build-in-place function call. If components are all static,
7340 -- we can pass the aggregate to the back end regardless of limitedness.
7342 -- Extension aggregates, aggregates in extended return statements, and
7343 -- aggregates for C++ imported types must be expanded.
7345 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
7346 if not Nkind_In (Parent (N), N_Component_Association,
7347 N_Object_Declaration)
7348 then
7349 Convert_To_Assignments (N, Typ);
7351 elsif Nkind (N) = N_Extension_Aggregate
7352 or else Convention (Typ) = Convention_CPP
7353 then
7354 Convert_To_Assignments (N, Typ);
7356 elsif not Size_Known_At_Compile_Time (Typ)
7357 or else not Component_OK_For_Backend
7358 or else not Static_Components
7359 then
7360 Convert_To_Assignments (N, Typ);
7362 -- In all other cases, build a proper aggregate to be handled by
7363 -- the back-end
7365 else
7366 Build_Back_End_Aggregate;
7367 end if;
7369 -- Gigi doesn't properly handle temporaries of variable size so we
7370 -- generate it in the front-end
7372 elsif not Size_Known_At_Compile_Time (Typ)
7373 and then Tagged_Type_Expansion
7374 then
7375 Convert_To_Assignments (N, Typ);
7377 -- An aggregate used to initialize a controlled object must be turned
7378 -- into component assignments as the components themselves may require
7379 -- finalization actions such as adjustment.
7381 elsif Needs_Finalization (Typ) then
7382 Convert_To_Assignments (N, Typ);
7384 -- Ada 2005 (AI-287): In case of default initialized components we
7385 -- convert the aggregate into assignments.
7387 elsif Has_Default_Init_Comps (N) then
7388 Convert_To_Assignments (N, Typ);
7390 -- Check components
7392 elsif not Component_OK_For_Backend then
7393 Convert_To_Assignments (N, Typ);
7395 -- If an ancestor is private, some components are not inherited and we
7396 -- cannot expand into a record aggregate.
7398 elsif Has_Visible_Private_Ancestor (Typ) then
7399 Convert_To_Assignments (N, Typ);
7401 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
7402 -- is not able to handle the aggregate for Late_Request.
7404 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
7405 Convert_To_Assignments (N, Typ);
7407 -- If the tagged types covers interface types we need to initialize all
7408 -- hidden components containing pointers to secondary dispatch tables.
7410 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
7411 Convert_To_Assignments (N, Typ);
7413 -- If some components are mutable, the size of the aggregate component
7414 -- may be distinct from the default size of the type component, so
7415 -- we need to expand to insure that the back-end copies the proper
7416 -- size of the data. However, if the aggregate is the initial value of
7417 -- a constant, the target is immutable and might be built statically
7418 -- if components are appropriate.
7420 elsif Has_Mutable_Components (Typ)
7421 and then
7422 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
7423 or else not Constant_Present (Parent (Top_Level_Aggr))
7424 or else not Static_Components)
7425 then
7426 Convert_To_Assignments (N, Typ);
7428 -- If the type involved has bit aligned components, then we are not sure
7429 -- that the back end can handle this case correctly.
7431 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
7432 Convert_To_Assignments (N, Typ);
7434 -- When generating C, only generate an aggregate when declaring objects
7435 -- since C does not support aggregates in e.g. assignment statements.
7437 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
7438 Convert_To_Assignments (N, Typ);
7440 -- In all other cases, build a proper aggregate to be handled by gigi
7442 else
7443 Build_Back_End_Aggregate;
7444 end if;
7445 end Expand_Record_Aggregate;
7447 ----------------------------
7448 -- Has_Default_Init_Comps --
7449 ----------------------------
7451 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
7452 Comps : constant List_Id := Component_Associations (N);
7453 C : Node_Id;
7454 Expr : Node_Id;
7456 begin
7457 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
7459 if No (Comps) then
7460 return False;
7461 end if;
7463 if Has_Self_Reference (N) then
7464 return True;
7465 end if;
7467 -- Check if any direct component has default initialized components
7469 C := First (Comps);
7470 while Present (C) loop
7471 if Box_Present (C) then
7472 return True;
7473 end if;
7475 Next (C);
7476 end loop;
7478 -- Recursive call in case of aggregate expression
7480 C := First (Comps);
7481 while Present (C) loop
7482 Expr := Expression (C);
7484 if Present (Expr)
7485 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
7486 and then Has_Default_Init_Comps (Expr)
7487 then
7488 return True;
7489 end if;
7491 Next (C);
7492 end loop;
7494 return False;
7495 end Has_Default_Init_Comps;
7497 ----------------------------------------
7498 -- Is_Build_In_Place_Aggregate_Return --
7499 ----------------------------------------
7501 function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean is
7502 P : Node_Id := Parent (N);
7504 begin
7505 while Nkind (P) = N_Qualified_Expression loop
7506 P := Parent (P);
7507 end loop;
7509 if Nkind (P) = N_Simple_Return_Statement then
7510 null;
7512 elsif Nkind (Parent (P)) = N_Extended_Return_Statement then
7513 P := Parent (P);
7515 else
7516 return False;
7517 end if;
7519 return
7520 Is_Build_In_Place_Function
7521 (Return_Applies_To (Return_Statement_Entity (P)));
7522 end Is_Build_In_Place_Aggregate_Return;
7524 --------------------------
7525 -- Is_Delayed_Aggregate --
7526 --------------------------
7528 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
7529 Node : Node_Id := N;
7530 Kind : Node_Kind := Nkind (Node);
7532 begin
7533 if Kind = N_Qualified_Expression then
7534 Node := Expression (Node);
7535 Kind := Nkind (Node);
7536 end if;
7538 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
7539 return False;
7540 else
7541 return Expansion_Delayed (Node);
7542 end if;
7543 end Is_Delayed_Aggregate;
7545 ---------------------------
7546 -- In_Object_Declaration --
7547 ---------------------------
7549 function In_Object_Declaration (N : Node_Id) return Boolean is
7550 P : Node_Id := Parent (N);
7551 begin
7552 while Present (P) loop
7553 if Nkind (P) = N_Object_Declaration then
7554 return True;
7555 end if;
7557 P := Parent (P);
7558 end loop;
7560 return False;
7561 end In_Object_Declaration;
7563 ----------------------------------------
7564 -- Is_Static_Dispatch_Table_Aggregate --
7565 ----------------------------------------
7567 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
7568 Typ : constant Entity_Id := Base_Type (Etype (N));
7570 begin
7571 return Building_Static_Dispatch_Tables
7572 and then Tagged_Type_Expansion
7573 and then RTU_Loaded (Ada_Tags)
7575 -- Avoid circularity when rebuilding the compiler
7577 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
7578 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
7579 or else
7580 Typ = RTE (RE_Address_Array)
7581 or else
7582 Typ = RTE (RE_Type_Specific_Data)
7583 or else
7584 Typ = RTE (RE_Tag_Table)
7585 or else
7586 (RTE_Available (RE_Interface_Data)
7587 and then Typ = RTE (RE_Interface_Data))
7588 or else
7589 (RTE_Available (RE_Interfaces_Array)
7590 and then Typ = RTE (RE_Interfaces_Array))
7591 or else
7592 (RTE_Available (RE_Interface_Data_Element)
7593 and then Typ = RTE (RE_Interface_Data_Element)));
7594 end Is_Static_Dispatch_Table_Aggregate;
7596 -----------------------------
7597 -- Is_Two_Dim_Packed_Array --
7598 -----------------------------
7600 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
7601 C : constant Int := UI_To_Int (Component_Size (Typ));
7602 begin
7603 return Number_Dimensions (Typ) = 2
7604 and then Is_Bit_Packed_Array (Typ)
7605 and then (C = 1 or else C = 2 or else C = 4);
7606 end Is_Two_Dim_Packed_Array;
7608 --------------------
7609 -- Late_Expansion --
7610 --------------------
7612 function Late_Expansion
7613 (N : Node_Id;
7614 Typ : Entity_Id;
7615 Target : Node_Id) return List_Id
7617 Aggr_Code : List_Id;
7619 begin
7620 if Is_Array_Type (Etype (N)) then
7621 Aggr_Code :=
7622 Build_Array_Aggr_Code
7623 (N => N,
7624 Ctype => Component_Type (Etype (N)),
7625 Index => First_Index (Typ),
7626 Into => Target,
7627 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
7628 Indexes => No_List);
7630 -- Directly or indirectly (e.g. access protected procedure) a record
7632 else
7633 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
7634 end if;
7636 -- Save the last assignment statement associated with the aggregate
7637 -- when building a controlled object. This reference is utilized by
7638 -- the finalization machinery when marking an object as successfully
7639 -- initialized.
7641 if Needs_Finalization (Typ)
7642 and then Is_Entity_Name (Target)
7643 and then Present (Entity (Target))
7644 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
7645 then
7646 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
7647 end if;
7649 return Aggr_Code;
7650 end Late_Expansion;
7652 ----------------------------------
7653 -- Make_OK_Assignment_Statement --
7654 ----------------------------------
7656 function Make_OK_Assignment_Statement
7657 (Sloc : Source_Ptr;
7658 Name : Node_Id;
7659 Expression : Node_Id) return Node_Id
7661 begin
7662 Set_Assignment_OK (Name);
7663 return Make_Assignment_Statement (Sloc, Name, Expression);
7664 end Make_OK_Assignment_Statement;
7666 -----------------------
7667 -- Number_Of_Choices --
7668 -----------------------
7670 function Number_Of_Choices (N : Node_Id) return Nat is
7671 Assoc : Node_Id;
7672 Choice : Node_Id;
7674 Nb_Choices : Nat := 0;
7676 begin
7677 if Present (Expressions (N)) then
7678 return 0;
7679 end if;
7681 Assoc := First (Component_Associations (N));
7682 while Present (Assoc) loop
7683 Choice := First (Choice_List (Assoc));
7684 while Present (Choice) loop
7685 if Nkind (Choice) /= N_Others_Choice then
7686 Nb_Choices := Nb_Choices + 1;
7687 end if;
7689 Next (Choice);
7690 end loop;
7692 Next (Assoc);
7693 end loop;
7695 return Nb_Choices;
7696 end Number_Of_Choices;
7698 ------------------------------------
7699 -- Packed_Array_Aggregate_Handled --
7700 ------------------------------------
7702 -- The current version of this procedure will handle at compile time
7703 -- any array aggregate that meets these conditions:
7705 -- One and two dimensional, bit packed
7706 -- Underlying packed type is modular type
7707 -- Bounds are within 32-bit Int range
7708 -- All bounds and values are static
7710 -- Note: for now, in the 2-D case, we only handle component sizes of
7711 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7713 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
7714 Loc : constant Source_Ptr := Sloc (N);
7715 Typ : constant Entity_Id := Etype (N);
7716 Ctyp : constant Entity_Id := Component_Type (Typ);
7718 Not_Handled : exception;
7719 -- Exception raised if this aggregate cannot be handled
7721 begin
7722 -- Handle one- or two dimensional bit packed array
7724 if not Is_Bit_Packed_Array (Typ)
7725 or else Number_Dimensions (Typ) > 2
7726 then
7727 return False;
7728 end if;
7730 -- If two-dimensional, check whether it can be folded, and transformed
7731 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7732 -- the original type.
7734 if Number_Dimensions (Typ) = 2 then
7735 return Two_Dim_Packed_Array_Handled (N);
7736 end if;
7738 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
7739 return False;
7740 end if;
7742 if not Is_Scalar_Type (Component_Type (Typ))
7743 and then Has_Non_Standard_Rep (Component_Type (Typ))
7744 then
7745 return False;
7746 end if;
7748 declare
7749 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
7751 Lo : Node_Id;
7752 Hi : Node_Id;
7753 -- Bounds of index type
7755 Lob : Uint;
7756 Hib : Uint;
7757 -- Values of bounds if compile time known
7759 function Get_Component_Val (N : Node_Id) return Uint;
7760 -- Given a expression value N of the component type Ctyp, returns a
7761 -- value of Csiz (component size) bits representing this value. If
7762 -- the value is non-static or any other reason exists why the value
7763 -- cannot be returned, then Not_Handled is raised.
7765 -----------------------
7766 -- Get_Component_Val --
7767 -----------------------
7769 function Get_Component_Val (N : Node_Id) return Uint is
7770 Val : Uint;
7772 begin
7773 -- We have to analyze the expression here before doing any further
7774 -- processing here. The analysis of such expressions is deferred
7775 -- till expansion to prevent some problems of premature analysis.
7777 Analyze_And_Resolve (N, Ctyp);
7779 -- Must have a compile time value. String literals have to be
7780 -- converted into temporaries as well, because they cannot easily
7781 -- be converted into their bit representation.
7783 if not Compile_Time_Known_Value (N)
7784 or else Nkind (N) = N_String_Literal
7785 then
7786 raise Not_Handled;
7787 end if;
7789 Val := Expr_Rep_Value (N);
7791 -- Adjust for bias, and strip proper number of bits
7793 if Has_Biased_Representation (Ctyp) then
7794 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
7795 end if;
7797 return Val mod Uint_2 ** Csiz;
7798 end Get_Component_Val;
7800 -- Here we know we have a one dimensional bit packed array
7802 begin
7803 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
7805 -- Cannot do anything if bounds are dynamic
7807 if not Compile_Time_Known_Value (Lo)
7808 or else
7809 not Compile_Time_Known_Value (Hi)
7810 then
7811 return False;
7812 end if;
7814 -- Or are silly out of range of int bounds
7816 Lob := Expr_Value (Lo);
7817 Hib := Expr_Value (Hi);
7819 if not UI_Is_In_Int_Range (Lob)
7820 or else
7821 not UI_Is_In_Int_Range (Hib)
7822 then
7823 return False;
7824 end if;
7826 -- At this stage we have a suitable aggregate for handling at compile
7827 -- time. The only remaining checks are that the values of expressions
7828 -- in the aggregate are compile-time known (checks are performed by
7829 -- Get_Component_Val), and that any subtypes or ranges are statically
7830 -- known.
7832 -- If the aggregate is not fully positional at this stage, then
7833 -- convert it to positional form. Either this will fail, in which
7834 -- case we can do nothing, or it will succeed, in which case we have
7835 -- succeeded in handling the aggregate and transforming it into a
7836 -- modular value, or it will stay an aggregate, in which case we
7837 -- have failed to create a packed value for it.
7839 if Present (Component_Associations (N)) then
7840 Convert_To_Positional
7841 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7842 return Nkind (N) /= N_Aggregate;
7843 end if;
7845 -- Otherwise we are all positional, so convert to proper value
7847 declare
7848 Lov : constant Int := UI_To_Int (Lob);
7849 Hiv : constant Int := UI_To_Int (Hib);
7851 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
7852 -- The length of the array (number of elements)
7854 Aggregate_Val : Uint;
7855 -- Value of aggregate. The value is set in the low order bits of
7856 -- this value. For the little-endian case, the values are stored
7857 -- from low-order to high-order and for the big-endian case the
7858 -- values are stored from high-order to low-order. Note that gigi
7859 -- will take care of the conversions to left justify the value in
7860 -- the big endian case (because of left justified modular type
7861 -- processing), so we do not have to worry about that here.
7863 Lit : Node_Id;
7864 -- Integer literal for resulting constructed value
7866 Shift : Nat;
7867 -- Shift count from low order for next value
7869 Incr : Int;
7870 -- Shift increment for loop
7872 Expr : Node_Id;
7873 -- Next expression from positional parameters of aggregate
7875 Left_Justified : Boolean;
7876 -- Set True if we are filling the high order bits of the target
7877 -- value (i.e. the value is left justified).
7879 begin
7880 -- For little endian, we fill up the low order bits of the target
7881 -- value. For big endian we fill up the high order bits of the
7882 -- target value (which is a left justified modular value).
7884 Left_Justified := Bytes_Big_Endian;
7886 -- Switch justification if using -gnatd8
7888 if Debug_Flag_8 then
7889 Left_Justified := not Left_Justified;
7890 end if;
7892 -- Switch justfification if reverse storage order
7894 if Reverse_Storage_Order (Base_Type (Typ)) then
7895 Left_Justified := not Left_Justified;
7896 end if;
7898 if Left_Justified then
7899 Shift := Csiz * (Len - 1);
7900 Incr := -Csiz;
7901 else
7902 Shift := 0;
7903 Incr := +Csiz;
7904 end if;
7906 -- Loop to set the values
7908 if Len = 0 then
7909 Aggregate_Val := Uint_0;
7910 else
7911 Expr := First (Expressions (N));
7912 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
7914 for J in 2 .. Len loop
7915 Shift := Shift + Incr;
7916 Next (Expr);
7917 Aggregate_Val :=
7918 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
7919 end loop;
7920 end if;
7922 -- Now we can rewrite with the proper value
7924 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
7925 Set_Print_In_Hex (Lit);
7927 -- Construct the expression using this literal. Note that it is
7928 -- important to qualify the literal with its proper modular type
7929 -- since universal integer does not have the required range and
7930 -- also this is a left justified modular type, which is important
7931 -- in the big-endian case.
7933 Rewrite (N,
7934 Unchecked_Convert_To (Typ,
7935 Make_Qualified_Expression (Loc,
7936 Subtype_Mark =>
7937 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
7938 Expression => Lit)));
7940 Analyze_And_Resolve (N, Typ);
7941 return True;
7942 end;
7943 end;
7945 exception
7946 when Not_Handled =>
7947 return False;
7948 end Packed_Array_Aggregate_Handled;
7950 ----------------------------
7951 -- Has_Mutable_Components --
7952 ----------------------------
7954 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
7955 Comp : Entity_Id;
7957 begin
7958 Comp := First_Component (Typ);
7959 while Present (Comp) loop
7960 if Is_Record_Type (Etype (Comp))
7961 and then Has_Discriminants (Etype (Comp))
7962 and then not Is_Constrained (Etype (Comp))
7963 then
7964 return True;
7965 end if;
7967 Next_Component (Comp);
7968 end loop;
7970 return False;
7971 end Has_Mutable_Components;
7973 ------------------------------
7974 -- Initialize_Discriminants --
7975 ------------------------------
7977 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
7978 Loc : constant Source_Ptr := Sloc (N);
7979 Bas : constant Entity_Id := Base_Type (Typ);
7980 Par : constant Entity_Id := Etype (Bas);
7981 Decl : constant Node_Id := Parent (Par);
7982 Ref : Node_Id;
7984 begin
7985 if Is_Tagged_Type (Bas)
7986 and then Is_Derived_Type (Bas)
7987 and then Has_Discriminants (Par)
7988 and then Has_Discriminants (Bas)
7989 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
7990 and then Nkind (Decl) = N_Full_Type_Declaration
7991 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7992 and then
7993 Present (Variant_Part (Component_List (Type_Definition (Decl))))
7994 and then Nkind (N) /= N_Extension_Aggregate
7995 then
7997 -- Call init proc to set discriminants.
7998 -- There should eventually be a special procedure for this ???
8000 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
8001 Insert_Actions_After (N,
8002 Build_Initialization_Call (Sloc (N), Ref, Typ));
8003 end if;
8004 end Initialize_Discriminants;
8006 ----------------
8007 -- Must_Slide --
8008 ----------------
8010 function Must_Slide
8011 (Obj_Type : Entity_Id;
8012 Typ : Entity_Id) return Boolean
8014 L1, L2, H1, H2 : Node_Id;
8016 begin
8017 -- No sliding if the type of the object is not established yet, if it is
8018 -- an unconstrained type whose actual subtype comes from the aggregate,
8019 -- or if the two types are identical.
8021 if not Is_Array_Type (Obj_Type) then
8022 return False;
8024 elsif not Is_Constrained (Obj_Type) then
8025 return False;
8027 elsif Typ = Obj_Type then
8028 return False;
8030 else
8031 -- Sliding can only occur along the first dimension
8033 Get_Index_Bounds (First_Index (Typ), L1, H1);
8034 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
8036 if not Is_OK_Static_Expression (L1) or else
8037 not Is_OK_Static_Expression (L2) or else
8038 not Is_OK_Static_Expression (H1) or else
8039 not Is_OK_Static_Expression (H2)
8040 then
8041 return False;
8042 else
8043 return Expr_Value (L1) /= Expr_Value (L2)
8044 or else
8045 Expr_Value (H1) /= Expr_Value (H2);
8046 end if;
8047 end if;
8048 end Must_Slide;
8050 ---------------------------------
8051 -- Process_Transient_Component --
8052 ---------------------------------
8054 procedure Process_Transient_Component
8055 (Loc : Source_Ptr;
8056 Comp_Typ : Entity_Id;
8057 Init_Expr : Node_Id;
8058 Fin_Call : out Node_Id;
8059 Hook_Clear : out Node_Id;
8060 Aggr : Node_Id := Empty;
8061 Stmts : List_Id := No_List)
8063 procedure Add_Item (Item : Node_Id);
8064 -- Insert arbitrary node Item into the tree depending on the values of
8065 -- Aggr and Stmts.
8067 --------------
8068 -- Add_Item --
8069 --------------
8071 procedure Add_Item (Item : Node_Id) is
8072 begin
8073 if Present (Aggr) then
8074 Insert_Action (Aggr, Item);
8075 else
8076 pragma Assert (Present (Stmts));
8077 Append_To (Stmts, Item);
8078 end if;
8079 end Add_Item;
8081 -- Local variables
8083 Hook_Assign : Node_Id;
8084 Hook_Decl : Node_Id;
8085 Ptr_Decl : Node_Id;
8086 Res_Decl : Node_Id;
8087 Res_Id : Entity_Id;
8088 Res_Typ : Entity_Id;
8090 -- Start of processing for Process_Transient_Component
8092 begin
8093 -- Add the access type, which provides a reference to the function
8094 -- result. Generate:
8096 -- type Res_Typ is access all Comp_Typ;
8098 Res_Typ := Make_Temporary (Loc, 'A');
8099 Set_Ekind (Res_Typ, E_General_Access_Type);
8100 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
8102 Add_Item
8103 (Make_Full_Type_Declaration (Loc,
8104 Defining_Identifier => Res_Typ,
8105 Type_Definition =>
8106 Make_Access_To_Object_Definition (Loc,
8107 All_Present => True,
8108 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
8110 -- Add the temporary which captures the result of the function call.
8111 -- Generate:
8113 -- Res : constant Res_Typ := Init_Expr'Reference;
8115 -- Note that this temporary is effectively a transient object because
8116 -- its lifetime is bounded by the current array or record component.
8118 Res_Id := Make_Temporary (Loc, 'R');
8119 Set_Ekind (Res_Id, E_Constant);
8120 Set_Etype (Res_Id, Res_Typ);
8122 -- Mark the transient object as successfully processed to avoid double
8123 -- finalization.
8125 Set_Is_Finalized_Transient (Res_Id);
8127 -- Signal the general finalization machinery that this transient object
8128 -- should not be considered for finalization actions because its cleanup
8129 -- will be performed by Process_Transient_Component_Completion.
8131 Set_Is_Ignored_Transient (Res_Id);
8133 Res_Decl :=
8134 Make_Object_Declaration (Loc,
8135 Defining_Identifier => Res_Id,
8136 Constant_Present => True,
8137 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
8138 Expression =>
8139 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
8141 Add_Item (Res_Decl);
8143 -- Construct all pieces necessary to hook and finalize the transient
8144 -- result.
8146 Build_Transient_Object_Statements
8147 (Obj_Decl => Res_Decl,
8148 Fin_Call => Fin_Call,
8149 Hook_Assign => Hook_Assign,
8150 Hook_Clear => Hook_Clear,
8151 Hook_Decl => Hook_Decl,
8152 Ptr_Decl => Ptr_Decl);
8154 -- Add the access type which provides a reference to the transient
8155 -- result. Generate:
8157 -- type Ptr_Typ is access all Comp_Typ;
8159 Add_Item (Ptr_Decl);
8161 -- Add the temporary which acts as a hook to the transient result.
8162 -- Generate:
8164 -- Hook : Ptr_Typ := null;
8166 Add_Item (Hook_Decl);
8168 -- Attach the transient result to the hook. Generate:
8170 -- Hook := Ptr_Typ (Res);
8172 Add_Item (Hook_Assign);
8174 -- The original initialization expression now references the value of
8175 -- the temporary function result. Generate:
8177 -- Res.all
8179 Rewrite (Init_Expr,
8180 Make_Explicit_Dereference (Loc,
8181 Prefix => New_Occurrence_Of (Res_Id, Loc)));
8182 end Process_Transient_Component;
8184 --------------------------------------------
8185 -- Process_Transient_Component_Completion --
8186 --------------------------------------------
8188 procedure Process_Transient_Component_Completion
8189 (Loc : Source_Ptr;
8190 Aggr : Node_Id;
8191 Fin_Call : Node_Id;
8192 Hook_Clear : Node_Id;
8193 Stmts : List_Id)
8195 Exceptions_OK : constant Boolean :=
8196 not Restriction_Active (No_Exception_Propagation);
8198 begin
8199 pragma Assert (Present (Hook_Clear));
8201 -- Generate the following code if exception propagation is allowed:
8203 -- declare
8204 -- Abort : constant Boolean := Triggered_By_Abort;
8205 -- <or>
8206 -- Abort : constant Boolean := False; -- no abort
8208 -- E : Exception_Occurrence;
8209 -- Raised : Boolean := False;
8211 -- begin
8212 -- [Abort_Defer;]
8214 -- begin
8215 -- Hook := null;
8216 -- [Deep_]Finalize (Res.all);
8218 -- exception
8219 -- when others =>
8220 -- if not Raised then
8221 -- Raised := True;
8222 -- Save_Occurrence (E,
8223 -- Get_Curent_Excep.all.all);
8224 -- end if;
8225 -- end;
8227 -- [Abort_Undefer;]
8229 -- if Raised and then not Abort then
8230 -- Raise_From_Controlled_Operation (E);
8231 -- end if;
8232 -- end;
8234 if Exceptions_OK then
8235 Abort_And_Exception : declare
8236 Blk_Decls : constant List_Id := New_List;
8237 Blk_Stmts : constant List_Id := New_List;
8238 Fin_Stmts : constant List_Id := New_List;
8240 Fin_Data : Finalization_Exception_Data;
8242 begin
8243 -- Create the declarations of the two flags and the exception
8244 -- occurrence.
8246 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
8248 -- Generate:
8249 -- Abort_Defer;
8251 if Abort_Allowed then
8252 Append_To (Blk_Stmts,
8253 Build_Runtime_Call (Loc, RE_Abort_Defer));
8254 end if;
8256 -- Wrap the hook clear and the finalization call in order to trap
8257 -- a potential exception.
8259 Append_To (Fin_Stmts, Hook_Clear);
8261 if Present (Fin_Call) then
8262 Append_To (Fin_Stmts, Fin_Call);
8263 end if;
8265 Append_To (Blk_Stmts,
8266 Make_Block_Statement (Loc,
8267 Handled_Statement_Sequence =>
8268 Make_Handled_Sequence_Of_Statements (Loc,
8269 Statements => Fin_Stmts,
8270 Exception_Handlers => New_List (
8271 Build_Exception_Handler (Fin_Data)))));
8273 -- Generate:
8274 -- Abort_Undefer;
8276 if Abort_Allowed then
8277 Append_To (Blk_Stmts,
8278 Build_Runtime_Call (Loc, RE_Abort_Undefer));
8279 end if;
8281 -- Reraise the potential exception with a proper "upgrade" to
8282 -- Program_Error if needed.
8284 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
8286 -- Wrap everything in a block
8288 Append_To (Stmts,
8289 Make_Block_Statement (Loc,
8290 Declarations => Blk_Decls,
8291 Handled_Statement_Sequence =>
8292 Make_Handled_Sequence_Of_Statements (Loc,
8293 Statements => Blk_Stmts)));
8294 end Abort_And_Exception;
8296 -- Generate the following code if exception propagation is not allowed
8297 -- and aborts are allowed:
8299 -- begin
8300 -- Abort_Defer;
8301 -- Hook := null;
8302 -- [Deep_]Finalize (Res.all);
8303 -- at end
8304 -- Abort_Undefer_Direct;
8305 -- end;
8307 elsif Abort_Allowed then
8308 Abort_Only : declare
8309 Blk_Stmts : constant List_Id := New_List;
8311 begin
8312 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8313 Append_To (Blk_Stmts, Hook_Clear);
8315 if Present (Fin_Call) then
8316 Append_To (Blk_Stmts, Fin_Call);
8317 end if;
8319 Append_To (Stmts,
8320 Build_Abort_Undefer_Block (Loc,
8321 Stmts => Blk_Stmts,
8322 Context => Aggr));
8323 end Abort_Only;
8325 -- Otherwise generate:
8327 -- Hook := null;
8328 -- [Deep_]Finalize (Res.all);
8330 else
8331 Append_To (Stmts, Hook_Clear);
8333 if Present (Fin_Call) then
8334 Append_To (Stmts, Fin_Call);
8335 end if;
8336 end if;
8337 end Process_Transient_Component_Completion;
8339 ---------------------
8340 -- Sort_Case_Table --
8341 ---------------------
8343 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
8344 L : constant Int := Case_Table'First;
8345 U : constant Int := Case_Table'Last;
8346 K : Int;
8347 J : Int;
8348 T : Case_Bounds;
8350 begin
8351 K := L;
8352 while K /= U loop
8353 T := Case_Table (K + 1);
8355 J := K + 1;
8356 while J /= L
8357 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
8358 Expr_Value (T.Choice_Lo)
8359 loop
8360 Case_Table (J) := Case_Table (J - 1);
8361 J := J - 1;
8362 end loop;
8364 Case_Table (J) := T;
8365 K := K + 1;
8366 end loop;
8367 end Sort_Case_Table;
8369 ----------------------------
8370 -- Static_Array_Aggregate --
8371 ----------------------------
8373 function Static_Array_Aggregate (N : Node_Id) return Boolean is
8374 Bounds : constant Node_Id := Aggregate_Bounds (N);
8376 Typ : constant Entity_Id := Etype (N);
8377 Comp_Type : constant Entity_Id := Component_Type (Typ);
8378 Agg : Node_Id;
8379 Expr : Node_Id;
8380 Lo : Node_Id;
8381 Hi : Node_Id;
8383 begin
8384 if Is_Tagged_Type (Typ)
8385 or else Is_Controlled (Typ)
8386 or else Is_Packed (Typ)
8387 then
8388 return False;
8389 end if;
8391 if Present (Bounds)
8392 and then Nkind (Bounds) = N_Range
8393 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
8394 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
8395 then
8396 Lo := Low_Bound (Bounds);
8397 Hi := High_Bound (Bounds);
8399 if No (Component_Associations (N)) then
8401 -- Verify that all components are static integers
8403 Expr := First (Expressions (N));
8404 while Present (Expr) loop
8405 if Nkind (Expr) /= N_Integer_Literal then
8406 return False;
8407 end if;
8409 Next (Expr);
8410 end loop;
8412 return True;
8414 else
8415 -- We allow only a single named association, either a static
8416 -- range or an others_clause, with a static expression.
8418 Expr := First (Component_Associations (N));
8420 if Present (Expressions (N)) then
8421 return False;
8423 elsif Present (Next (Expr)) then
8424 return False;
8426 elsif Present (Next (First (Choice_List (Expr)))) then
8427 return False;
8429 else
8430 -- The aggregate is static if all components are literals,
8431 -- or else all its components are static aggregates for the
8432 -- component type. We also limit the size of a static aggregate
8433 -- to prevent runaway static expressions.
8435 if Is_Array_Type (Comp_Type)
8436 or else Is_Record_Type (Comp_Type)
8437 then
8438 if Nkind (Expression (Expr)) /= N_Aggregate
8439 or else
8440 not Compile_Time_Known_Aggregate (Expression (Expr))
8441 then
8442 return False;
8443 end if;
8445 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
8446 return False;
8447 end if;
8449 if not Aggr_Size_OK (N, Typ) then
8450 return False;
8451 end if;
8453 -- Create a positional aggregate with the right number of
8454 -- copies of the expression.
8456 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
8458 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
8459 loop
8460 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
8462 -- The copied expression must be analyzed and resolved.
8463 -- Besides setting the type, this ensures that static
8464 -- expressions are appropriately marked as such.
8466 Analyze_And_Resolve
8467 (Last (Expressions (Agg)), Component_Type (Typ));
8468 end loop;
8470 Set_Aggregate_Bounds (Agg, Bounds);
8471 Set_Etype (Agg, Typ);
8472 Set_Analyzed (Agg);
8473 Rewrite (N, Agg);
8474 Set_Compile_Time_Known_Aggregate (N);
8476 return True;
8477 end if;
8478 end if;
8480 else
8481 return False;
8482 end if;
8483 end Static_Array_Aggregate;
8485 ----------------------------------
8486 -- Two_Dim_Packed_Array_Handled --
8487 ----------------------------------
8489 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
8490 Loc : constant Source_Ptr := Sloc (N);
8491 Typ : constant Entity_Id := Etype (N);
8492 Ctyp : constant Entity_Id := Component_Type (Typ);
8493 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
8494 Packed_Array : constant Entity_Id :=
8495 Packed_Array_Impl_Type (Base_Type (Typ));
8497 One_Comp : Node_Id;
8498 -- Expression in original aggregate
8500 One_Dim : Node_Id;
8501 -- One-dimensional subaggregate
8503 begin
8505 -- For now, only deal with cases where an integral number of elements
8506 -- fit in a single byte. This includes the most common boolean case.
8508 if not (Comp_Size = 1 or else
8509 Comp_Size = 2 or else
8510 Comp_Size = 4)
8511 then
8512 return False;
8513 end if;
8515 Convert_To_Positional
8516 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8518 -- Verify that all components are static
8520 if Nkind (N) = N_Aggregate
8521 and then Compile_Time_Known_Aggregate (N)
8522 then
8523 null;
8525 -- The aggregate may have been reanalyzed and converted already
8527 elsif Nkind (N) /= N_Aggregate then
8528 return True;
8530 -- If component associations remain, the aggregate is not static
8532 elsif Present (Component_Associations (N)) then
8533 return False;
8535 else
8536 One_Dim := First (Expressions (N));
8537 while Present (One_Dim) loop
8538 if Present (Component_Associations (One_Dim)) then
8539 return False;
8540 end if;
8542 One_Comp := First (Expressions (One_Dim));
8543 while Present (One_Comp) loop
8544 if not Is_OK_Static_Expression (One_Comp) then
8545 return False;
8546 end if;
8548 Next (One_Comp);
8549 end loop;
8551 Next (One_Dim);
8552 end loop;
8553 end if;
8555 -- Two-dimensional aggregate is now fully positional so pack one
8556 -- dimension to create a static one-dimensional array, and rewrite
8557 -- as an unchecked conversion to the original type.
8559 declare
8560 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
8561 -- The packed array type is a byte array
8563 Packed_Num : Nat;
8564 -- Number of components accumulated in current byte
8566 Comps : List_Id;
8567 -- Assembled list of packed values for equivalent aggregate
8569 Comp_Val : Uint;
8570 -- Integer value of component
8572 Incr : Int;
8573 -- Step size for packing
8575 Init_Shift : Int;
8576 -- Endian-dependent start position for packing
8578 Shift : Int;
8579 -- Current insertion position
8581 Val : Int;
8582 -- Component of packed array being assembled
8584 begin
8585 Comps := New_List;
8586 Val := 0;
8587 Packed_Num := 0;
8589 -- Account for endianness. See corresponding comment in
8590 -- Packed_Array_Aggregate_Handled concerning the following.
8592 if Bytes_Big_Endian
8593 xor Debug_Flag_8
8594 xor Reverse_Storage_Order (Base_Type (Typ))
8595 then
8596 Init_Shift := Byte_Size - Comp_Size;
8597 Incr := -Comp_Size;
8598 else
8599 Init_Shift := 0;
8600 Incr := +Comp_Size;
8601 end if;
8603 -- Iterate over each subaggregate
8605 Shift := Init_Shift;
8606 One_Dim := First (Expressions (N));
8607 while Present (One_Dim) loop
8608 One_Comp := First (Expressions (One_Dim));
8609 while Present (One_Comp) loop
8610 if Packed_Num = Byte_Size / Comp_Size then
8612 -- Byte is complete, add to list of expressions
8614 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8615 Val := 0;
8616 Shift := Init_Shift;
8617 Packed_Num := 0;
8619 else
8620 Comp_Val := Expr_Rep_Value (One_Comp);
8622 -- Adjust for bias, and strip proper number of bits
8624 if Has_Biased_Representation (Ctyp) then
8625 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
8626 end if;
8628 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
8629 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
8630 Shift := Shift + Incr;
8631 One_Comp := Next (One_Comp);
8632 Packed_Num := Packed_Num + 1;
8633 end if;
8634 end loop;
8636 One_Dim := Next (One_Dim);
8637 end loop;
8639 if Packed_Num > 0 then
8641 -- Add final incomplete byte if present
8643 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8644 end if;
8646 Rewrite (N,
8647 Unchecked_Convert_To (Typ,
8648 Make_Qualified_Expression (Loc,
8649 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
8650 Expression => Make_Aggregate (Loc, Expressions => Comps))));
8651 Analyze_And_Resolve (N);
8652 return True;
8653 end;
8654 end Two_Dim_Packed_Array_Handled;
8656 end Exp_Aggr;