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