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