Merge from mainline (163495:164578).
[official-gcc/graphite-test-results.git] / gcc / ada / exp_aggr.adb
blob27ad463a1b4e27635d8ac3add102d32e2d5f3779
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-2010, 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_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Tss; use Exp_Tss;
39 with Fname; use Fname;
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_Aux; use Sem_Aux;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Res; use Sem_Res;
56 with Sem_Util; use Sem_Util;
57 with Sinfo; use Sinfo;
58 with Snames; use Snames;
59 with Stand; use Stand;
60 with Targparm; use Targparm;
61 with Tbuild; use Tbuild;
62 with Uintp; use Uintp;
64 package body Exp_Aggr is
66 type Case_Bounds is record
67 Choice_Lo : Node_Id;
68 Choice_Hi : Node_Id;
69 Choice_Node : Node_Id;
70 end record;
72 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
73 -- Table type used by Check_Case_Choices procedure
75 function Must_Slide
76 (Obj_Type : Entity_Id;
77 Typ : Entity_Id) return Boolean;
78 -- A static array aggregate in an object declaration can in most cases be
79 -- expanded in place. The one exception is when the aggregate is given
80 -- with component associations that specify different bounds from those of
81 -- the type definition in the object declaration. In this pathological
82 -- case the aggregate must slide, and we must introduce an intermediate
83 -- temporary to hold it.
85 -- The same holds in an assignment to one-dimensional array of arrays,
86 -- when a component may be given with bounds that differ from those of the
87 -- component type.
89 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
90 -- Sort the Case Table using the Lower Bound of each Choice as the key.
91 -- A simple insertion sort is used since the number of choices in a case
92 -- statement of variant part will usually be small and probably in near
93 -- sorted order.
95 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
96 -- N is an aggregate (record or array). Checks the presence of default
97 -- initialization (<>) in any component (Ada 2005: AI-287).
99 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
100 -- Returns true if N is an aggregate used to initialize the components
101 -- of an statically allocated dispatch table.
103 ------------------------------------------------------
104 -- Local subprograms for Record Aggregate Expansion --
105 ------------------------------------------------------
107 procedure Expand_Record_Aggregate
108 (N : Node_Id;
109 Orig_Tag : Node_Id := Empty;
110 Parent_Expr : Node_Id := Empty);
111 -- This is the top level procedure for record aggregate expansion.
112 -- Expansion for record aggregates needs expand aggregates for tagged
113 -- record types. Specifically Expand_Record_Aggregate adds the Tag
114 -- field in front of the Component_Association list that was created
115 -- during resolution by Resolve_Record_Aggregate.
117 -- N is the record aggregate node.
118 -- Orig_Tag is the value of the Tag that has to be provided for this
119 -- specific aggregate. It carries the tag corresponding to the type
120 -- of the outermost aggregate during the recursive expansion
121 -- Parent_Expr is the ancestor part of the original extension
122 -- aggregate
124 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
125 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
126 -- aggregate (which can only be a record type, this procedure is only used
127 -- for record types). Transform the given aggregate into a sequence of
128 -- assignments performed component by component.
130 function Build_Record_Aggr_Code
131 (N : Node_Id;
132 Typ : Entity_Id;
133 Lhs : Node_Id;
134 Flist : Node_Id := Empty;
135 Obj : Entity_Id := Empty;
136 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
137 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
138 -- aggregate. Target is an expression containing the location on which the
139 -- component by component assignments will take place. Returns the list of
140 -- assignments plus all other adjustments needed for tagged and controlled
141 -- types. Flist is an expression representing the finalization list on
142 -- which to attach the controlled components if any. Obj is present in the
143 -- object declaration and dynamic allocation cases, it contains an entity
144 -- that allows to know if the value being created needs to be attached to
145 -- the final list in case of pragma Finalize_Storage_Only.
147 -- ???
148 -- The meaning of the Obj formal is extremely unclear. *What* entity
149 -- should be passed? For the object declaration case we may guess that
150 -- this is the object being declared, but what about the allocator case?
152 -- Is_Limited_Ancestor_Expansion indicates that the function has been
153 -- called recursively to expand the limited ancestor to avoid copying it.
155 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
156 -- Return true if one of the component is of a discriminated type with
157 -- defaults. An aggregate for a type with mutable components must be
158 -- expanded into individual assignments.
160 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
161 -- If the type of the aggregate is a type extension with renamed discrimi-
162 -- nants, we must initialize the hidden discriminants of the parent.
163 -- Otherwise, the target object must not be initialized. The discriminants
164 -- are initialized by calling the initialization procedure for the type.
165 -- This is incorrect if the initialization of other components has any
166 -- side effects. We restrict this call to the case where the parent type
167 -- has a variant part, because this is the only case where the hidden
168 -- discriminants are accessed, namely when calling discriminant checking
169 -- functions of the parent type, and when applying a stream attribute to
170 -- an object of the derived type.
172 -----------------------------------------------------
173 -- Local Subprograms for Array Aggregate Expansion --
174 -----------------------------------------------------
176 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
177 -- Very large static aggregates present problems to the back-end, and are
178 -- transformed into assignments and loops. This function verifies that the
179 -- total number of components of an aggregate is acceptable for rewriting
180 -- into a purely positional static form. Aggr_Size_OK must be called before
181 -- calling Flatten.
183 -- This function also detects and warns about one-component aggregates that
184 -- appear in a non-static context. Even if the component value is static,
185 -- such an aggregate must be expanded into an assignment.
187 procedure Convert_Array_Aggr_In_Allocator
188 (Decl : Node_Id;
189 Aggr : Node_Id;
190 Target : Node_Id);
191 -- If the aggregate appears within an allocator and can be expanded in
192 -- place, this routine generates the individual assignments to components
193 -- of the designated object. This is an optimization over the general
194 -- case, where a temporary is first created on the stack and then used to
195 -- construct the allocated object on the heap.
197 procedure Convert_To_Positional
198 (N : Node_Id;
199 Max_Others_Replicate : Nat := 5;
200 Handle_Bit_Packed : Boolean := False);
201 -- If possible, convert named notation to positional notation. This
202 -- conversion is possible only in some static cases. If the conversion is
203 -- possible, then N is rewritten with the analyzed converted aggregate.
204 -- The parameter Max_Others_Replicate controls the maximum number of
205 -- values corresponding to an others choice that will be converted to
206 -- positional notation (the default of 5 is the normal limit, and reflects
207 -- the fact that normally the loop is better than a lot of separate
208 -- assignments). Note that this limit gets overridden in any case if
209 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
210 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
211 -- not expect the back end to handle bit packed arrays, so the normal case
212 -- of conversion is pointless), but in the special case of a call from
213 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
214 -- these are cases we handle in there.
216 procedure Expand_Array_Aggregate (N : Node_Id);
217 -- This is the top-level routine to perform array aggregate expansion.
218 -- N is the N_Aggregate node to be expanded.
220 function Backend_Processing_Possible (N : Node_Id) return Boolean;
221 -- This function checks if array aggregate N can be processed directly
222 -- by the backend. If this is the case True is returned.
224 function Build_Array_Aggr_Code
225 (N : Node_Id;
226 Ctype : Entity_Id;
227 Index : Node_Id;
228 Into : Node_Id;
229 Scalar_Comp : Boolean;
230 Indices : List_Id := No_List;
231 Flist : Node_Id := Empty) return List_Id;
232 -- This recursive routine returns a list of statements containing the
233 -- loops and assignments that are needed for the expansion of the array
234 -- aggregate N.
236 -- N is the (sub-)aggregate node to be expanded into code. This node
237 -- has been fully analyzed, and its Etype is properly set.
239 -- Index is the index node corresponding to the array sub-aggregate N.
241 -- Into is the target expression into which we are copying the aggregate.
242 -- Note that this node may not have been analyzed yet, and so the Etype
243 -- field may not be set.
245 -- Scalar_Comp is True if the component type of the aggregate is scalar.
247 -- Indices is the current list of expressions used to index the
248 -- object we are writing into.
250 -- Flist is an expression representing the finalization list on which
251 -- to attach the controlled components if any.
253 function Number_Of_Choices (N : Node_Id) return Nat;
254 -- Returns the number of discrete choices (not including the others choice
255 -- if present) contained in (sub-)aggregate N.
257 function Late_Expansion
258 (N : Node_Id;
259 Typ : Entity_Id;
260 Target : Node_Id;
261 Flist : Node_Id := Empty;
262 Obj : Entity_Id := Empty) return List_Id;
263 -- N is a nested (record or array) aggregate that has been marked with
264 -- 'Delay_Expansion'. Typ is the expected type of the aggregate and Target
265 -- is a (duplicable) expression that will hold the result of the aggregate
266 -- expansion. Flist is the finalization list to be used to attach
267 -- controlled components. 'Obj' when non empty, carries the original
268 -- object being initialized in order to know if it needs to be attached to
269 -- the previous parameter which may not be the case in the case where
270 -- Finalize_Storage_Only is set. Basically this procedure is used to
271 -- implement top-down expansions of nested aggregates. This is necessary
272 -- for avoiding temporaries at each level as well as for propagating the
273 -- right internal finalization list.
275 function Make_OK_Assignment_Statement
276 (Sloc : Source_Ptr;
277 Name : Node_Id;
278 Expression : Node_Id) return Node_Id;
279 -- This is like Make_Assignment_Statement, except that Assignment_OK
280 -- is set in the left operand. All assignments built by this unit
281 -- use this routine. This is needed to deal with assignments to
282 -- initialized constants that are done in place.
284 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
285 -- Given an array aggregate, this function handles the case of a packed
286 -- array aggregate with all constant values, where the aggregate can be
287 -- evaluated at compile time. If this is possible, then N is rewritten
288 -- to be its proper compile time value with all the components properly
289 -- assembled. The expression is analyzed and resolved and True is
290 -- returned. If this transformation is not possible, N is unchanged
291 -- and False is returned
293 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
294 -- If a slice assignment has an aggregate with a single others_choice,
295 -- the assignment can be done in place even if bounds are not static,
296 -- by converting it into a loop over the discrete range of the slice.
298 ------------------
299 -- Aggr_Size_OK --
300 ------------------
302 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
303 Lo : Node_Id;
304 Hi : Node_Id;
305 Indx : Node_Id;
306 Siz : Int;
307 Lov : Uint;
308 Hiv : Uint;
310 -- The following constant determines the maximum size of an
311 -- array aggregate produced by converting named to positional
312 -- notation (e.g. from others clauses). This avoids running
313 -- away with attempts to convert huge aggregates, which hit
314 -- memory limits in the backend.
316 -- The normal limit is 5000, but we increase this limit to
317 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code)
318 -- or Restrictions (No_Implicit_Loops) is specified, since in
319 -- either case, we are at risk of declaring the program illegal
320 -- because of this limit.
322 Max_Aggr_Size : constant Nat :=
323 5000 + (2 ** 24 - 5000) *
324 Boolean'Pos
325 (Restriction_Active (No_Elaboration_Code)
326 or else
327 Restriction_Active (No_Implicit_Loops));
329 function Component_Count (T : Entity_Id) return Int;
330 -- The limit is applied to the total number of components that the
331 -- aggregate will have, which is the number of static expressions
332 -- that will appear in the flattened array. This requires a recursive
333 -- computation of the number of scalar components of the structure.
335 ---------------------
336 -- Component_Count --
337 ---------------------
339 function Component_Count (T : Entity_Id) return Int is
340 Res : Int := 0;
341 Comp : Entity_Id;
343 begin
344 if Is_Scalar_Type (T) then
345 return 1;
347 elsif Is_Record_Type (T) then
348 Comp := First_Component (T);
349 while Present (Comp) loop
350 Res := Res + Component_Count (Etype (Comp));
351 Next_Component (Comp);
352 end loop;
354 return Res;
356 elsif Is_Array_Type (T) then
357 declare
358 Lo : constant Node_Id :=
359 Type_Low_Bound (Etype (First_Index (T)));
360 Hi : constant Node_Id :=
361 Type_High_Bound (Etype (First_Index (T)));
363 Siz : constant Int := Component_Count (Component_Type (T));
365 begin
366 if not Compile_Time_Known_Value (Lo)
367 or else not Compile_Time_Known_Value (Hi)
368 then
369 return 0;
370 else
371 return
372 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
373 end if;
374 end;
376 else
377 -- Can only be a null for an access type
379 return 1;
380 end if;
381 end Component_Count;
383 -- Start of processing for Aggr_Size_OK
385 begin
386 Siz := Component_Count (Component_Type (Typ));
388 Indx := First_Index (Typ);
389 while Present (Indx) loop
390 Lo := Type_Low_Bound (Etype (Indx));
391 Hi := Type_High_Bound (Etype (Indx));
393 -- Bounds need to be known at compile time
395 if not Compile_Time_Known_Value (Lo)
396 or else not Compile_Time_Known_Value (Hi)
397 then
398 return False;
399 end if;
401 Lov := Expr_Value (Lo);
402 Hiv := Expr_Value (Hi);
404 -- A flat array is always safe
406 if Hiv < Lov then
407 return True;
408 end if;
410 -- One-component aggregates are suspicious, and if the context type
411 -- is an object declaration with non-static bounds it will trip gcc;
412 -- such an aggregate must be expanded into a single assignment.
414 if Hiv = Lov
415 and then Nkind (Parent (N)) = N_Object_Declaration
416 then
417 declare
418 Index_Type : constant Entity_Id :=
419 Etype
420 (First_Index
421 (Etype (Defining_Identifier (Parent (N)))));
422 Indx : Node_Id;
424 begin
425 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
426 or else not Compile_Time_Known_Value
427 (Type_High_Bound (Index_Type))
428 then
429 if Present (Component_Associations (N)) then
430 Indx :=
431 First (Choices (First (Component_Associations (N))));
432 if Is_Entity_Name (Indx)
433 and then not Is_Type (Entity (Indx))
434 then
435 Error_Msg_N
436 ("single component aggregate in non-static context?",
437 Indx);
438 Error_Msg_N ("\maybe subtype name was meant?", Indx);
439 end if;
440 end if;
442 return False;
443 end if;
444 end;
445 end if;
447 declare
448 Rng : constant Uint := Hiv - Lov + 1;
450 begin
451 -- Check if size is too large
453 if not UI_Is_In_Int_Range (Rng) then
454 return False;
455 end if;
457 Siz := Siz * UI_To_Int (Rng);
458 end;
460 if Siz <= 0
461 or else Siz > Max_Aggr_Size
462 then
463 return False;
464 end if;
466 -- Bounds must be in integer range, for later array construction
468 if not UI_Is_In_Int_Range (Lov)
469 or else
470 not UI_Is_In_Int_Range (Hiv)
471 then
472 return False;
473 end if;
475 Next_Index (Indx);
476 end loop;
478 return True;
479 end Aggr_Size_OK;
481 ---------------------------------
482 -- Backend_Processing_Possible --
483 ---------------------------------
485 -- Backend processing by Gigi/gcc is possible only if all the following
486 -- conditions are met:
488 -- 1. N is fully positional
490 -- 2. N is not a bit-packed array aggregate;
492 -- 3. The size of N's array type must be known at compile time. Note
493 -- that this implies that the component size is also known
495 -- 4. The array type of N does not follow the Fortran layout convention
496 -- or if it does it must be 1 dimensional.
498 -- 5. The array component type may not be tagged (which could necessitate
499 -- reassignment of proper tags).
501 -- 6. The array component type must not have unaligned bit components
503 -- 7. None of the components of the aggregate may be bit unaligned
504 -- components.
506 -- 8. There cannot be delayed components, since we do not know enough
507 -- at this stage to know if back end processing is possible.
509 -- 9. There cannot be any discriminated record components, since the
510 -- back end cannot handle this complex case.
512 -- 10. No controlled actions need to be generated for components
514 -- 11. For a VM back end, the array should have no aliased components
516 function Backend_Processing_Possible (N : Node_Id) return Boolean is
517 Typ : constant Entity_Id := Etype (N);
518 -- Typ is the correct constrained array subtype of the aggregate
520 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
521 -- This routine checks components of aggregate N, enforcing checks
522 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
523 -- performed on subaggregates. The Index value is the current index
524 -- being checked in the multi-dimensional case.
526 ---------------------
527 -- Component_Check --
528 ---------------------
530 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
531 Expr : Node_Id;
533 begin
534 -- Checks 1: (no component associations)
536 if Present (Component_Associations (N)) then
537 return False;
538 end if;
540 -- Checks on components
542 -- Recurse to check subaggregates, which may appear in qualified
543 -- expressions. If delayed, the front-end will have to expand.
544 -- If the component is a discriminated record, treat as non-static,
545 -- as the back-end cannot handle this properly.
547 Expr := First (Expressions (N));
548 while Present (Expr) loop
550 -- Checks 8: (no delayed components)
552 if Is_Delayed_Aggregate (Expr) then
553 return False;
554 end if;
556 -- Checks 9: (no discriminated records)
558 if Present (Etype (Expr))
559 and then Is_Record_Type (Etype (Expr))
560 and then Has_Discriminants (Etype (Expr))
561 then
562 return False;
563 end if;
565 -- Checks 7. Component must not be bit aligned component
567 if Possible_Bit_Aligned_Component (Expr) then
568 return False;
569 end if;
571 -- Recursion to following indexes for multiple dimension case
573 if Present (Next_Index (Index))
574 and then not Component_Check (Expr, Next_Index (Index))
575 then
576 return False;
577 end if;
579 -- All checks for that component finished, on to next
581 Next (Expr);
582 end loop;
584 return True;
585 end Component_Check;
587 -- Start of processing for Backend_Processing_Possible
589 begin
590 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
592 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
593 return False;
594 end if;
596 -- If component is limited, aggregate must be expanded because each
597 -- component assignment must be built in place.
599 if Is_Inherently_Limited_Type (Component_Type (Typ)) then
600 return False;
601 end if;
603 -- Checks 4 (array must not be multi-dimensional Fortran case)
605 if Convention (Typ) = Convention_Fortran
606 and then Number_Dimensions (Typ) > 1
607 then
608 return False;
609 end if;
611 -- Checks 3 (size of array must be known at compile time)
613 if not Size_Known_At_Compile_Time (Typ) then
614 return False;
615 end if;
617 -- Checks on components
619 if not Component_Check (N, First_Index (Typ)) then
620 return False;
621 end if;
623 -- Checks 5 (if the component type is tagged, then we may need to do
624 -- tag adjustments. Perhaps this should be refined to check for any
625 -- component associations that actually need tag adjustment, similar
626 -- to the test in Component_Not_OK_For_Backend for record aggregates
627 -- with tagged components, but not clear whether it's worthwhile ???;
628 -- in the case of the JVM, object tags are handled implicitly)
630 if Is_Tagged_Type (Component_Type (Typ))
631 and then Tagged_Type_Expansion
632 then
633 return False;
634 end if;
636 -- Checks 6 (component type must not have bit aligned components)
638 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
639 return False;
640 end if;
642 -- Checks 11: Array aggregates with aliased components are currently
643 -- not well supported by the VM backend; disable temporarily this
644 -- backend processing until it is definitely supported.
646 if VM_Target /= No_VM
647 and then Has_Aliased_Components (Base_Type (Typ))
648 then
649 return False;
650 end if;
652 -- Backend processing is possible
654 Set_Size_Known_At_Compile_Time (Etype (N), True);
655 return True;
656 end Backend_Processing_Possible;
658 ---------------------------
659 -- Build_Array_Aggr_Code --
660 ---------------------------
662 -- The code that we generate from a one dimensional aggregate is
664 -- 1. If the sub-aggregate contains discrete choices we
666 -- (a) Sort the discrete choices
668 -- (b) Otherwise for each discrete choice that specifies a range we
669 -- emit a loop. If a range specifies a maximum of three values, or
670 -- we are dealing with an expression we emit a sequence of
671 -- assignments instead of a loop.
673 -- (c) Generate the remaining loops to cover the others choice if any
675 -- 2. If the aggregate contains positional elements we
677 -- (a) translate the positional elements in a series of assignments
679 -- (b) Generate a final loop to cover the others choice if any.
680 -- Note that this final loop has to be a while loop since the case
682 -- L : Integer := Integer'Last;
683 -- H : Integer := Integer'Last;
684 -- A : array (L .. H) := (1, others =>0);
686 -- cannot be handled by a for loop. Thus for the following
688 -- array (L .. H) := (.. positional elements.., others =>E);
690 -- we always generate something like:
692 -- J : Index_Type := Index_Of_Last_Positional_Element;
693 -- while J < H loop
694 -- J := Index_Base'Succ (J)
695 -- Tmp (J) := E;
696 -- end loop;
698 function Build_Array_Aggr_Code
699 (N : Node_Id;
700 Ctype : Entity_Id;
701 Index : Node_Id;
702 Into : Node_Id;
703 Scalar_Comp : Boolean;
704 Indices : List_Id := No_List;
705 Flist : Node_Id := Empty) return List_Id
707 Loc : constant Source_Ptr := Sloc (N);
708 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
709 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
710 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
712 function Add (Val : Int; To : Node_Id) return Node_Id;
713 -- Returns an expression where Val is added to expression To, unless
714 -- To+Val is provably out of To's base type range. To must be an
715 -- already analyzed expression.
717 function Empty_Range (L, H : Node_Id) return Boolean;
718 -- Returns True if the range defined by L .. H is certainly empty
720 function Equal (L, H : Node_Id) return Boolean;
721 -- Returns True if L = H for sure
723 function Index_Base_Name return Node_Id;
724 -- Returns a new reference to the index type name
726 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
727 -- Ind must be a side-effect free expression. If the input aggregate
728 -- N to Build_Loop contains no sub-aggregates, then this function
729 -- returns the assignment statement:
731 -- Into (Indices, Ind) := Expr;
733 -- Otherwise we call Build_Code recursively
735 -- Ada 2005 (AI-287): In case of default initialized component, Expr
736 -- is empty and we generate a call to the corresponding IP subprogram.
738 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
739 -- Nodes L and H must be side-effect free expressions.
740 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
741 -- This routine returns the for loop statement
743 -- for J in Index_Base'(L) .. Index_Base'(H) loop
744 -- Into (Indices, J) := Expr;
745 -- end loop;
747 -- Otherwise we call Build_Code recursively.
748 -- As an optimization if the loop covers 3 or less scalar elements we
749 -- generate a sequence of assignments.
751 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
752 -- Nodes L and H must be side-effect free expressions.
753 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
754 -- This routine returns the while loop statement
756 -- J : Index_Base := L;
757 -- while J < H loop
758 -- J := Index_Base'Succ (J);
759 -- Into (Indices, J) := Expr;
760 -- end loop;
762 -- Otherwise we call Build_Code recursively
764 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
765 function Local_Expr_Value (E : Node_Id) return Uint;
766 -- These two Local routines are used to replace the corresponding ones
767 -- in sem_eval because while processing the bounds of an aggregate with
768 -- discrete choices whose index type is an enumeration, we build static
769 -- expressions not recognized by Compile_Time_Known_Value as such since
770 -- they have not yet been analyzed and resolved. All the expressions in
771 -- question are things like Index_Base_Name'Val (Const) which we can
772 -- easily recognize as being constant.
774 ---------
775 -- Add --
776 ---------
778 function Add (Val : Int; To : Node_Id) return Node_Id is
779 Expr_Pos : Node_Id;
780 Expr : Node_Id;
781 To_Pos : Node_Id;
782 U_To : Uint;
783 U_Val : constant Uint := UI_From_Int (Val);
785 begin
786 -- Note: do not try to optimize the case of Val = 0, because
787 -- we need to build a new node with the proper Sloc value anyway.
789 -- First test if we can do constant folding
791 if Local_Compile_Time_Known_Value (To) then
792 U_To := Local_Expr_Value (To) + Val;
794 -- Determine if our constant is outside the range of the index.
795 -- If so return an Empty node. This empty node will be caught
796 -- by Empty_Range below.
798 if Compile_Time_Known_Value (Index_Base_L)
799 and then U_To < Expr_Value (Index_Base_L)
800 then
801 return Empty;
803 elsif Compile_Time_Known_Value (Index_Base_H)
804 and then U_To > Expr_Value (Index_Base_H)
805 then
806 return Empty;
807 end if;
809 Expr_Pos := Make_Integer_Literal (Loc, U_To);
810 Set_Is_Static_Expression (Expr_Pos);
812 if not Is_Enumeration_Type (Index_Base) then
813 Expr := Expr_Pos;
815 -- If we are dealing with enumeration return
816 -- Index_Base'Val (Expr_Pos)
818 else
819 Expr :=
820 Make_Attribute_Reference
821 (Loc,
822 Prefix => Index_Base_Name,
823 Attribute_Name => Name_Val,
824 Expressions => New_List (Expr_Pos));
825 end if;
827 return Expr;
828 end if;
830 -- If we are here no constant folding possible
832 if not Is_Enumeration_Type (Index_Base) then
833 Expr :=
834 Make_Op_Add (Loc,
835 Left_Opnd => Duplicate_Subexpr (To),
836 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
838 -- If we are dealing with enumeration return
839 -- Index_Base'Val (Index_Base'Pos (To) + Val)
841 else
842 To_Pos :=
843 Make_Attribute_Reference
844 (Loc,
845 Prefix => Index_Base_Name,
846 Attribute_Name => Name_Pos,
847 Expressions => New_List (Duplicate_Subexpr (To)));
849 Expr_Pos :=
850 Make_Op_Add (Loc,
851 Left_Opnd => To_Pos,
852 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
854 Expr :=
855 Make_Attribute_Reference
856 (Loc,
857 Prefix => Index_Base_Name,
858 Attribute_Name => Name_Val,
859 Expressions => New_List (Expr_Pos));
860 end if;
862 return Expr;
863 end Add;
865 -----------------
866 -- Empty_Range --
867 -----------------
869 function Empty_Range (L, H : Node_Id) return Boolean is
870 Is_Empty : Boolean := False;
871 Low : Node_Id;
872 High : Node_Id;
874 begin
875 -- First check if L or H were already detected as overflowing the
876 -- index base range type by function Add above. If this is so Add
877 -- returns the empty node.
879 if No (L) or else No (H) then
880 return True;
881 end if;
883 for J in 1 .. 3 loop
884 case J is
886 -- L > H range is empty
888 when 1 =>
889 Low := L;
890 High := H;
892 -- B_L > H range must be empty
894 when 2 =>
895 Low := Index_Base_L;
896 High := H;
898 -- L > B_H range must be empty
900 when 3 =>
901 Low := L;
902 High := Index_Base_H;
903 end case;
905 if Local_Compile_Time_Known_Value (Low)
906 and then Local_Compile_Time_Known_Value (High)
907 then
908 Is_Empty :=
909 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
910 end if;
912 exit when Is_Empty;
913 end loop;
915 return Is_Empty;
916 end Empty_Range;
918 -----------
919 -- Equal --
920 -----------
922 function Equal (L, H : Node_Id) return Boolean is
923 begin
924 if L = H then
925 return True;
927 elsif Local_Compile_Time_Known_Value (L)
928 and then Local_Compile_Time_Known_Value (H)
929 then
930 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
931 end if;
933 return False;
934 end Equal;
936 ----------------
937 -- Gen_Assign --
938 ----------------
940 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
941 L : constant List_Id := New_List;
942 F : Entity_Id;
943 A : Node_Id;
945 New_Indices : List_Id;
946 Indexed_Comp : Node_Id;
947 Expr_Q : Node_Id;
948 Comp_Type : Entity_Id := Empty;
950 function Add_Loop_Actions (Lis : List_Id) return List_Id;
951 -- Collect insert_actions generated in the construction of a
952 -- loop, and prepend them to the sequence of assignments to
953 -- complete the eventual body of the loop.
955 ----------------------
956 -- Add_Loop_Actions --
957 ----------------------
959 function Add_Loop_Actions (Lis : List_Id) return List_Id is
960 Res : List_Id;
962 begin
963 -- Ada 2005 (AI-287): Do nothing else in case of default
964 -- initialized component.
966 if No (Expr) then
967 return Lis;
969 elsif Nkind (Parent (Expr)) = N_Component_Association
970 and then Present (Loop_Actions (Parent (Expr)))
971 then
972 Append_List (Lis, Loop_Actions (Parent (Expr)));
973 Res := Loop_Actions (Parent (Expr));
974 Set_Loop_Actions (Parent (Expr), No_List);
975 return Res;
977 else
978 return Lis;
979 end if;
980 end Add_Loop_Actions;
982 -- Start of processing for Gen_Assign
984 begin
985 if No (Indices) then
986 New_Indices := New_List;
987 else
988 New_Indices := New_Copy_List_Tree (Indices);
989 end if;
991 Append_To (New_Indices, Ind);
993 if Present (Flist) then
994 F := New_Copy_Tree (Flist);
996 elsif Present (Etype (N)) and then Needs_Finalization (Etype (N)) then
997 if Is_Entity_Name (Into)
998 and then Present (Scope (Entity (Into)))
999 then
1000 F := Find_Final_List (Scope (Entity (Into)));
1001 else
1002 F := Find_Final_List (Current_Scope);
1003 end if;
1004 else
1005 F := Empty;
1006 end if;
1008 if Present (Next_Index (Index)) then
1009 return
1010 Add_Loop_Actions (
1011 Build_Array_Aggr_Code
1012 (N => Expr,
1013 Ctype => Ctype,
1014 Index => Next_Index (Index),
1015 Into => Into,
1016 Scalar_Comp => Scalar_Comp,
1017 Indices => New_Indices,
1018 Flist => F));
1019 end if;
1021 -- If we get here then we are at a bottom-level (sub-)aggregate
1023 Indexed_Comp :=
1024 Checks_Off
1025 (Make_Indexed_Component (Loc,
1026 Prefix => New_Copy_Tree (Into),
1027 Expressions => New_Indices));
1029 Set_Assignment_OK (Indexed_Comp);
1031 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1032 -- is not present (and therefore we also initialize Expr_Q to empty).
1034 if No (Expr) then
1035 Expr_Q := Empty;
1036 elsif Nkind (Expr) = N_Qualified_Expression then
1037 Expr_Q := Expression (Expr);
1038 else
1039 Expr_Q := Expr;
1040 end if;
1042 if Present (Etype (N))
1043 and then Etype (N) /= Any_Composite
1044 then
1045 Comp_Type := Component_Type (Etype (N));
1046 pragma Assert (Comp_Type = Ctype); -- AI-287
1048 elsif Present (Next (First (New_Indices))) then
1050 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1051 -- component because we have received the component type in
1052 -- the formal parameter Ctype.
1054 -- ??? Some assert pragmas have been added to check if this new
1055 -- formal can be used to replace this code in all cases.
1057 if Present (Expr) then
1059 -- This is a multidimensional array. Recover the component
1060 -- type from the outermost aggregate, because subaggregates
1061 -- do not have an assigned type.
1063 declare
1064 P : Node_Id;
1066 begin
1067 P := Parent (Expr);
1068 while Present (P) loop
1069 if Nkind (P) = N_Aggregate
1070 and then Present (Etype (P))
1071 then
1072 Comp_Type := Component_Type (Etype (P));
1073 exit;
1075 else
1076 P := Parent (P);
1077 end if;
1078 end loop;
1080 pragma Assert (Comp_Type = Ctype); -- AI-287
1081 end;
1082 end if;
1083 end if;
1085 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1086 -- default initialized components (otherwise Expr_Q is not present).
1088 if Present (Expr_Q)
1089 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1090 then
1091 -- At this stage the Expression may not have been analyzed yet
1092 -- because the array aggregate code has not been updated to use
1093 -- the Expansion_Delayed flag and avoid analysis altogether to
1094 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1095 -- the analysis of non-array aggregates now in order to get the
1096 -- value of Expansion_Delayed flag for the inner aggregate ???
1098 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1099 Analyze_And_Resolve (Expr_Q, Comp_Type);
1100 end if;
1102 if Is_Delayed_Aggregate (Expr_Q) then
1104 -- This is either a subaggregate of a multidimentional array,
1105 -- or a component of an array type whose component type is
1106 -- also an array. In the latter case, the expression may have
1107 -- component associations that provide different bounds from
1108 -- those of the component type, and sliding must occur. Instead
1109 -- of decomposing the current aggregate assignment, force the
1110 -- re-analysis of the assignment, so that a temporary will be
1111 -- generated in the usual fashion, and sliding will take place.
1113 if Nkind (Parent (N)) = N_Assignment_Statement
1114 and then Is_Array_Type (Comp_Type)
1115 and then Present (Component_Associations (Expr_Q))
1116 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1117 then
1118 Set_Expansion_Delayed (Expr_Q, False);
1119 Set_Analyzed (Expr_Q, False);
1121 else
1122 return
1123 Add_Loop_Actions (
1124 Late_Expansion (
1125 Expr_Q, Etype (Expr_Q), Indexed_Comp, F));
1126 end if;
1127 end if;
1128 end if;
1130 -- Ada 2005 (AI-287): In case of default initialized component, call
1131 -- the initialization subprogram associated with the component type.
1132 -- If the component type is an access type, add an explicit null
1133 -- assignment, because for the back-end there is an initialization
1134 -- present for the whole aggregate, and no default initialization
1135 -- will take place.
1137 -- In addition, if the component type is controlled, we must call
1138 -- its Initialize procedure explicitly, because there is no explicit
1139 -- object creation that will invoke it otherwise.
1141 if No (Expr) then
1142 if Present (Base_Init_Proc (Base_Type (Ctype)))
1143 or else Has_Task (Base_Type (Ctype))
1144 then
1145 Append_List_To (L,
1146 Build_Initialization_Call (Loc,
1147 Id_Ref => Indexed_Comp,
1148 Typ => Ctype,
1149 With_Default_Init => True));
1151 elsif Is_Access_Type (Ctype) then
1152 Append_To (L,
1153 Make_Assignment_Statement (Loc,
1154 Name => Indexed_Comp,
1155 Expression => Make_Null (Loc)));
1156 end if;
1158 if Needs_Finalization (Ctype) then
1159 Append_List_To (L,
1160 Make_Init_Call (
1161 Ref => New_Copy_Tree (Indexed_Comp),
1162 Typ => Ctype,
1163 Flist_Ref => Find_Final_List (Current_Scope),
1164 With_Attach => Make_Integer_Literal (Loc, 1)));
1165 end if;
1167 else
1168 -- Now generate the assignment with no associated controlled
1169 -- actions since the target of the assignment may not have been
1170 -- initialized, it is not possible to Finalize it as expected by
1171 -- normal controlled assignment. The rest of the controlled
1172 -- actions are done manually with the proper finalization list
1173 -- coming from the context.
1175 A :=
1176 Make_OK_Assignment_Statement (Loc,
1177 Name => Indexed_Comp,
1178 Expression => New_Copy_Tree (Expr));
1180 if Present (Comp_Type) and then Needs_Finalization (Comp_Type) then
1181 Set_No_Ctrl_Actions (A);
1183 -- If this is an aggregate for an array of arrays, each
1184 -- sub-aggregate will be expanded as well, and even with
1185 -- No_Ctrl_Actions the assignments of inner components will
1186 -- require attachment in their assignments to temporaries.
1187 -- These temporaries must be finalized for each subaggregate,
1188 -- to prevent multiple attachments of the same temporary
1189 -- location to same finalization chain (and consequently
1190 -- circular lists). To ensure that finalization takes place
1191 -- for each subaggregate we wrap the assignment in a block.
1193 if Is_Array_Type (Comp_Type)
1194 and then Nkind (Expr) = N_Aggregate
1195 then
1196 A :=
1197 Make_Block_Statement (Loc,
1198 Handled_Statement_Sequence =>
1199 Make_Handled_Sequence_Of_Statements (Loc,
1200 Statements => New_List (A)));
1201 end if;
1202 end if;
1204 Append_To (L, A);
1206 -- Adjust the tag if tagged (because of possible view
1207 -- conversions), unless compiling for a VM where
1208 -- tags are implicit.
1210 if Present (Comp_Type)
1211 and then Is_Tagged_Type (Comp_Type)
1212 and then Tagged_Type_Expansion
1213 then
1214 A :=
1215 Make_OK_Assignment_Statement (Loc,
1216 Name =>
1217 Make_Selected_Component (Loc,
1218 Prefix => New_Copy_Tree (Indexed_Comp),
1219 Selector_Name =>
1220 New_Reference_To
1221 (First_Tag_Component (Comp_Type), Loc)),
1223 Expression =>
1224 Unchecked_Convert_To (RTE (RE_Tag),
1225 New_Reference_To
1226 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
1227 Loc)));
1229 Append_To (L, A);
1230 end if;
1232 -- Adjust and attach the component to the proper final list, which
1233 -- can be the controller of the outer record object or the final
1234 -- list associated with the scope.
1236 -- If the component is itself an array of controlled types, whose
1237 -- value is given by a sub-aggregate, then the attach calls have
1238 -- been generated when individual subcomponent are assigned, and
1239 -- must not be done again to prevent malformed finalization chains
1240 -- (see comments above, concerning the creation of a block to hold
1241 -- inner finalization actions).
1243 if Present (Comp_Type)
1244 and then Needs_Finalization (Comp_Type)
1245 and then not Is_Limited_Type (Comp_Type)
1246 and then not
1247 (Is_Array_Type (Comp_Type)
1248 and then Is_Controlled (Component_Type (Comp_Type))
1249 and then Nkind (Expr) = N_Aggregate)
1250 then
1251 Append_List_To (L,
1252 Make_Adjust_Call (
1253 Ref => New_Copy_Tree (Indexed_Comp),
1254 Typ => Comp_Type,
1255 Flist_Ref => F,
1256 With_Attach => Make_Integer_Literal (Loc, 1)));
1257 end if;
1258 end if;
1260 return Add_Loop_Actions (L);
1261 end Gen_Assign;
1263 --------------
1264 -- Gen_Loop --
1265 --------------
1267 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1268 L_J : Node_Id;
1270 L_L : Node_Id;
1271 -- Index_Base'(L)
1273 L_H : Node_Id;
1274 -- Index_Base'(H)
1276 L_Range : Node_Id;
1277 -- Index_Base'(L) .. Index_Base'(H)
1279 L_Iteration_Scheme : Node_Id;
1280 -- L_J in Index_Base'(L) .. Index_Base'(H)
1282 L_Body : List_Id;
1283 -- The statements to execute in the loop
1285 S : constant List_Id := New_List;
1286 -- List of statements
1288 Tcopy : Node_Id;
1289 -- Copy of expression tree, used for checking purposes
1291 begin
1292 -- If loop bounds define an empty range return the null statement
1294 if Empty_Range (L, H) then
1295 Append_To (S, Make_Null_Statement (Loc));
1297 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1298 -- default initialized component.
1300 if No (Expr) then
1301 null;
1303 else
1304 -- The expression must be type-checked even though no component
1305 -- of the aggregate will have this value. This is done only for
1306 -- actual components of the array, not for subaggregates. Do
1307 -- the check on a copy, because the expression may be shared
1308 -- among several choices, some of which might be non-null.
1310 if Present (Etype (N))
1311 and then Is_Array_Type (Etype (N))
1312 and then No (Next_Index (Index))
1313 then
1314 Expander_Mode_Save_And_Set (False);
1315 Tcopy := New_Copy_Tree (Expr);
1316 Set_Parent (Tcopy, N);
1317 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1318 Expander_Mode_Restore;
1319 end if;
1320 end if;
1322 return S;
1324 -- If loop bounds are the same then generate an assignment
1326 elsif Equal (L, H) then
1327 return Gen_Assign (New_Copy_Tree (L), Expr);
1329 -- If H - L <= 2 then generate a sequence of assignments when we are
1330 -- processing the bottom most aggregate and it contains scalar
1331 -- components.
1333 elsif No (Next_Index (Index))
1334 and then Scalar_Comp
1335 and then Local_Compile_Time_Known_Value (L)
1336 and then Local_Compile_Time_Known_Value (H)
1337 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1338 then
1340 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1341 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1343 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1344 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1345 end if;
1347 return S;
1348 end if;
1350 -- Otherwise construct the loop, starting with the loop index L_J
1352 L_J := Make_Temporary (Loc, 'J', L);
1354 -- Construct "L .. H" in Index_Base. We use a qualified expression
1355 -- for the bound to convert to the index base, but we don't need
1356 -- to do that if we already have the base type at hand.
1358 if Etype (L) = Index_Base then
1359 L_L := L;
1360 else
1361 L_L :=
1362 Make_Qualified_Expression (Loc,
1363 Subtype_Mark => Index_Base_Name,
1364 Expression => L);
1365 end if;
1367 if Etype (H) = Index_Base then
1368 L_H := H;
1369 else
1370 L_H :=
1371 Make_Qualified_Expression (Loc,
1372 Subtype_Mark => Index_Base_Name,
1373 Expression => H);
1374 end if;
1376 L_Range :=
1377 Make_Range (Loc,
1378 Low_Bound => L_L,
1379 High_Bound => L_H);
1381 -- Construct "for L_J in Index_Base range L .. H"
1383 L_Iteration_Scheme :=
1384 Make_Iteration_Scheme
1385 (Loc,
1386 Loop_Parameter_Specification =>
1387 Make_Loop_Parameter_Specification
1388 (Loc,
1389 Defining_Identifier => L_J,
1390 Discrete_Subtype_Definition => L_Range));
1392 -- Construct the statements to execute in the loop body
1394 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1396 -- Construct the final loop
1398 Append_To (S, Make_Implicit_Loop_Statement
1399 (Node => N,
1400 Identifier => Empty,
1401 Iteration_Scheme => L_Iteration_Scheme,
1402 Statements => L_Body));
1404 -- A small optimization: if the aggregate is initialized with a box
1405 -- and the component type has no initialization procedure, remove the
1406 -- useless empty loop.
1408 if Nkind (First (S)) = N_Loop_Statement
1409 and then Is_Empty_List (Statements (First (S)))
1410 then
1411 return New_List (Make_Null_Statement (Loc));
1412 else
1413 return S;
1414 end if;
1415 end Gen_Loop;
1417 ---------------
1418 -- Gen_While --
1419 ---------------
1421 -- The code built is
1423 -- W_J : Index_Base := L;
1424 -- while W_J < H loop
1425 -- W_J := Index_Base'Succ (W);
1426 -- L_Body;
1427 -- end loop;
1429 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1430 W_J : Node_Id;
1432 W_Decl : Node_Id;
1433 -- W_J : Base_Type := L;
1435 W_Iteration_Scheme : Node_Id;
1436 -- while W_J < H
1438 W_Index_Succ : Node_Id;
1439 -- Index_Base'Succ (J)
1441 W_Increment : Node_Id;
1442 -- W_J := Index_Base'Succ (W)
1444 W_Body : constant List_Id := New_List;
1445 -- The statements to execute in the loop
1447 S : constant List_Id := New_List;
1448 -- list of statement
1450 begin
1451 -- If loop bounds define an empty range or are equal return null
1453 if Empty_Range (L, H) or else Equal (L, H) then
1454 Append_To (S, Make_Null_Statement (Loc));
1455 return S;
1456 end if;
1458 -- Build the decl of W_J
1460 W_J := Make_Temporary (Loc, 'J', L);
1461 W_Decl :=
1462 Make_Object_Declaration
1463 (Loc,
1464 Defining_Identifier => W_J,
1465 Object_Definition => Index_Base_Name,
1466 Expression => L);
1468 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1469 -- that in this particular case L is a fresh Expr generated by
1470 -- Add which we are the only ones to use.
1472 Append_To (S, W_Decl);
1474 -- Construct " while W_J < H"
1476 W_Iteration_Scheme :=
1477 Make_Iteration_Scheme
1478 (Loc,
1479 Condition => Make_Op_Lt
1480 (Loc,
1481 Left_Opnd => New_Reference_To (W_J, Loc),
1482 Right_Opnd => New_Copy_Tree (H)));
1484 -- Construct the statements to execute in the loop body
1486 W_Index_Succ :=
1487 Make_Attribute_Reference
1488 (Loc,
1489 Prefix => Index_Base_Name,
1490 Attribute_Name => Name_Succ,
1491 Expressions => New_List (New_Reference_To (W_J, Loc)));
1493 W_Increment :=
1494 Make_OK_Assignment_Statement
1495 (Loc,
1496 Name => New_Reference_To (W_J, Loc),
1497 Expression => W_Index_Succ);
1499 Append_To (W_Body, W_Increment);
1500 Append_List_To (W_Body,
1501 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1503 -- Construct the final loop
1505 Append_To (S, Make_Implicit_Loop_Statement
1506 (Node => N,
1507 Identifier => Empty,
1508 Iteration_Scheme => W_Iteration_Scheme,
1509 Statements => W_Body));
1511 return S;
1512 end Gen_While;
1514 ---------------------
1515 -- Index_Base_Name --
1516 ---------------------
1518 function Index_Base_Name return Node_Id is
1519 begin
1520 return New_Reference_To (Index_Base, Sloc (N));
1521 end Index_Base_Name;
1523 ------------------------------------
1524 -- Local_Compile_Time_Known_Value --
1525 ------------------------------------
1527 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1528 begin
1529 return Compile_Time_Known_Value (E)
1530 or else
1531 (Nkind (E) = N_Attribute_Reference
1532 and then Attribute_Name (E) = Name_Val
1533 and then Compile_Time_Known_Value (First (Expressions (E))));
1534 end Local_Compile_Time_Known_Value;
1536 ----------------------
1537 -- Local_Expr_Value --
1538 ----------------------
1540 function Local_Expr_Value (E : Node_Id) return Uint is
1541 begin
1542 if Compile_Time_Known_Value (E) then
1543 return Expr_Value (E);
1544 else
1545 return Expr_Value (First (Expressions (E)));
1546 end if;
1547 end Local_Expr_Value;
1549 -- Build_Array_Aggr_Code Variables
1551 Assoc : Node_Id;
1552 Choice : Node_Id;
1553 Expr : Node_Id;
1554 Typ : Entity_Id;
1556 Others_Expr : Node_Id := Empty;
1557 Others_Box_Present : Boolean := False;
1559 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1560 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1561 -- The aggregate bounds of this specific sub-aggregate. Note that if
1562 -- the code generated by Build_Array_Aggr_Code is executed then these
1563 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1565 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1566 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1567 -- After Duplicate_Subexpr these are side-effect free
1569 Low : Node_Id;
1570 High : Node_Id;
1572 Nb_Choices : Nat := 0;
1573 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1574 -- Used to sort all the different choice values
1576 Nb_Elements : Int;
1577 -- Number of elements in the positional aggregate
1579 New_Code : constant List_Id := New_List;
1581 -- Start of processing for Build_Array_Aggr_Code
1583 begin
1584 -- First before we start, a special case. if we have a bit packed
1585 -- array represented as a modular type, then clear the value to
1586 -- zero first, to ensure that unused bits are properly cleared.
1588 Typ := Etype (N);
1590 if Present (Typ)
1591 and then Is_Bit_Packed_Array (Typ)
1592 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1593 then
1594 Append_To (New_Code,
1595 Make_Assignment_Statement (Loc,
1596 Name => New_Copy_Tree (Into),
1597 Expression =>
1598 Unchecked_Convert_To (Typ,
1599 Make_Integer_Literal (Loc, Uint_0))));
1600 end if;
1602 -- If the component type contains tasks, we need to build a Master
1603 -- entity in the current scope, because it will be needed if build-
1604 -- in-place functions are called in the expanded code.
1606 if Nkind (Parent (N)) = N_Object_Declaration
1607 and then Has_Task (Typ)
1608 then
1609 Build_Master_Entity (Defining_Identifier (Parent (N)));
1610 end if;
1612 -- STEP 1: Process component associations
1614 -- For those associations that may generate a loop, initialize
1615 -- Loop_Actions to collect inserted actions that may be crated.
1617 -- Skip this if no component associations
1619 if No (Expressions (N)) then
1621 -- STEP 1 (a): Sort the discrete choices
1623 Assoc := First (Component_Associations (N));
1624 while Present (Assoc) loop
1625 Choice := First (Choices (Assoc));
1626 while Present (Choice) loop
1627 if Nkind (Choice) = N_Others_Choice then
1628 Set_Loop_Actions (Assoc, New_List);
1630 if Box_Present (Assoc) then
1631 Others_Box_Present := True;
1632 else
1633 Others_Expr := Expression (Assoc);
1634 end if;
1635 exit;
1636 end if;
1638 Get_Index_Bounds (Choice, Low, High);
1640 if Low /= High then
1641 Set_Loop_Actions (Assoc, New_List);
1642 end if;
1644 Nb_Choices := Nb_Choices + 1;
1645 if Box_Present (Assoc) then
1646 Table (Nb_Choices) := (Choice_Lo => Low,
1647 Choice_Hi => High,
1648 Choice_Node => Empty);
1649 else
1650 Table (Nb_Choices) := (Choice_Lo => Low,
1651 Choice_Hi => High,
1652 Choice_Node => Expression (Assoc));
1653 end if;
1654 Next (Choice);
1655 end loop;
1657 Next (Assoc);
1658 end loop;
1660 -- If there is more than one set of choices these must be static
1661 -- and we can therefore sort them. Remember that Nb_Choices does not
1662 -- account for an others choice.
1664 if Nb_Choices > 1 then
1665 Sort_Case_Table (Table);
1666 end if;
1668 -- STEP 1 (b): take care of the whole set of discrete choices
1670 for J in 1 .. Nb_Choices loop
1671 Low := Table (J).Choice_Lo;
1672 High := Table (J).Choice_Hi;
1673 Expr := Table (J).Choice_Node;
1674 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1675 end loop;
1677 -- STEP 1 (c): generate the remaining loops to cover others choice
1678 -- We don't need to generate loops over empty gaps, but if there is
1679 -- a single empty range we must analyze the expression for semantics
1681 if Present (Others_Expr) or else Others_Box_Present then
1682 declare
1683 First : Boolean := True;
1685 begin
1686 for J in 0 .. Nb_Choices loop
1687 if J = 0 then
1688 Low := Aggr_Low;
1689 else
1690 Low := Add (1, To => Table (J).Choice_Hi);
1691 end if;
1693 if J = Nb_Choices then
1694 High := Aggr_High;
1695 else
1696 High := Add (-1, To => Table (J + 1).Choice_Lo);
1697 end if;
1699 -- If this is an expansion within an init proc, make
1700 -- sure that discriminant references are replaced by
1701 -- the corresponding discriminal.
1703 if Inside_Init_Proc then
1704 if Is_Entity_Name (Low)
1705 and then Ekind (Entity (Low)) = E_Discriminant
1706 then
1707 Set_Entity (Low, Discriminal (Entity (Low)));
1708 end if;
1710 if Is_Entity_Name (High)
1711 and then Ekind (Entity (High)) = E_Discriminant
1712 then
1713 Set_Entity (High, Discriminal (Entity (High)));
1714 end if;
1715 end if;
1717 if First
1718 or else not Empty_Range (Low, High)
1719 then
1720 First := False;
1721 Append_List
1722 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1723 end if;
1724 end loop;
1725 end;
1726 end if;
1728 -- STEP 2: Process positional components
1730 else
1731 -- STEP 2 (a): Generate the assignments for each positional element
1732 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1733 -- Aggr_L is analyzed and Add wants an analyzed expression.
1735 Expr := First (Expressions (N));
1736 Nb_Elements := -1;
1737 while Present (Expr) loop
1738 Nb_Elements := Nb_Elements + 1;
1739 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1740 To => New_Code);
1741 Next (Expr);
1742 end loop;
1744 -- STEP 2 (b): Generate final loop if an others choice is present
1745 -- Here Nb_Elements gives the offset of the last positional element.
1747 if Present (Component_Associations (N)) then
1748 Assoc := Last (Component_Associations (N));
1750 -- Ada 2005 (AI-287)
1752 if Box_Present (Assoc) then
1753 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1754 Aggr_High,
1755 Empty),
1756 To => New_Code);
1757 else
1758 Expr := Expression (Assoc);
1760 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1761 Aggr_High,
1762 Expr), -- AI-287
1763 To => New_Code);
1764 end if;
1765 end if;
1766 end if;
1768 return New_Code;
1769 end Build_Array_Aggr_Code;
1771 ----------------------------
1772 -- Build_Record_Aggr_Code --
1773 ----------------------------
1775 function Build_Record_Aggr_Code
1776 (N : Node_Id;
1777 Typ : Entity_Id;
1778 Lhs : Node_Id;
1779 Flist : Node_Id := Empty;
1780 Obj : Entity_Id := Empty;
1781 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1783 Loc : constant Source_Ptr := Sloc (N);
1784 L : constant List_Id := New_List;
1785 N_Typ : constant Entity_Id := Etype (N);
1787 Comp : Node_Id;
1788 Instr : Node_Id;
1789 Ref : Node_Id;
1790 Target : Entity_Id;
1791 F : Node_Id;
1792 Comp_Type : Entity_Id;
1793 Selector : Entity_Id;
1794 Comp_Expr : Node_Id;
1795 Expr_Q : Node_Id;
1797 Internal_Final_List : Node_Id := Empty;
1799 -- If this is an internal aggregate, the External_Final_List is an
1800 -- expression for the controller record of the enclosing type.
1802 -- If the current aggregate has several controlled components, this
1803 -- expression will appear in several calls to attach to the finali-
1804 -- zation list, and it must not be shared.
1806 External_Final_List : Node_Id;
1807 Ancestor_Is_Expression : Boolean := False;
1808 Ancestor_Is_Subtype_Mark : Boolean := False;
1810 Init_Typ : Entity_Id := Empty;
1811 Attach : Node_Id;
1813 Ctrl_Stuff_Done : Boolean := False;
1814 -- True if Gen_Ctrl_Actions_For_Aggr has already been called; calls
1815 -- after the first do nothing.
1817 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1818 -- Returns the value that the given discriminant of an ancestor type
1819 -- should receive (in the absence of a conflict with the value provided
1820 -- by an ancestor part of an extension aggregate).
1822 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1823 -- Check that each of the discriminant values defined by the ancestor
1824 -- part of an extension aggregate match the corresponding values
1825 -- provided by either an association of the aggregate or by the
1826 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1828 function Compatible_Int_Bounds
1829 (Agg_Bounds : Node_Id;
1830 Typ_Bounds : Node_Id) return Boolean;
1831 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1832 -- assumed that both bounds are integer ranges.
1834 procedure Gen_Ctrl_Actions_For_Aggr;
1835 -- Deal with the various controlled type data structure initializations
1836 -- (but only if it hasn't been done already).
1838 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1839 -- Returns the first discriminant association in the constraint
1840 -- associated with T, if any, otherwise returns Empty.
1842 function Init_Controller
1843 (Target : Node_Id;
1844 Typ : Entity_Id;
1845 F : Node_Id;
1846 Attach : Node_Id;
1847 Init_Pr : Boolean) return List_Id;
1848 -- Returns the list of statements necessary to initialize the internal
1849 -- controller of the (possible) ancestor typ into target and attach it
1850 -- to finalization list F. Init_Pr conditions the call to the init proc
1851 -- since it may already be done due to ancestor initialization.
1853 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1854 -- Check whether Bounds is a range node and its lower and higher bounds
1855 -- are integers literals.
1857 ---------------------------------
1858 -- Ancestor_Discriminant_Value --
1859 ---------------------------------
1861 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1862 Assoc : Node_Id;
1863 Assoc_Elmt : Elmt_Id;
1864 Aggr_Comp : Entity_Id;
1865 Corresp_Disc : Entity_Id;
1866 Current_Typ : Entity_Id := Base_Type (Typ);
1867 Parent_Typ : Entity_Id;
1868 Parent_Disc : Entity_Id;
1869 Save_Assoc : Node_Id := Empty;
1871 begin
1872 -- First check any discriminant associations to see if any of them
1873 -- provide a value for the discriminant.
1875 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1876 Assoc := First (Component_Associations (N));
1877 while Present (Assoc) loop
1878 Aggr_Comp := Entity (First (Choices (Assoc)));
1880 if Ekind (Aggr_Comp) = E_Discriminant then
1881 Save_Assoc := Expression (Assoc);
1883 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1884 while Present (Corresp_Disc) loop
1886 -- If found a corresponding discriminant then return the
1887 -- value given in the aggregate. (Note: this is not
1888 -- correct in the presence of side effects. ???)
1890 if Disc = Corresp_Disc then
1891 return Duplicate_Subexpr (Expression (Assoc));
1892 end if;
1894 Corresp_Disc :=
1895 Corresponding_Discriminant (Corresp_Disc);
1896 end loop;
1897 end if;
1899 Next (Assoc);
1900 end loop;
1901 end if;
1903 -- No match found in aggregate, so chain up parent types to find
1904 -- a constraint that defines the value of the discriminant.
1906 Parent_Typ := Etype (Current_Typ);
1907 while Current_Typ /= Parent_Typ loop
1908 if Has_Discriminants (Parent_Typ)
1909 and then not Has_Unknown_Discriminants (Parent_Typ)
1910 then
1911 Parent_Disc := First_Discriminant (Parent_Typ);
1913 -- We either get the association from the subtype indication
1914 -- of the type definition itself, or from the discriminant
1915 -- constraint associated with the type entity (which is
1916 -- preferable, but it's not always present ???)
1918 if Is_Empty_Elmt_List (
1919 Discriminant_Constraint (Current_Typ))
1920 then
1921 Assoc := Get_Constraint_Association (Current_Typ);
1922 Assoc_Elmt := No_Elmt;
1923 else
1924 Assoc_Elmt :=
1925 First_Elmt (Discriminant_Constraint (Current_Typ));
1926 Assoc := Node (Assoc_Elmt);
1927 end if;
1929 -- Traverse the discriminants of the parent type looking
1930 -- for one that corresponds.
1932 while Present (Parent_Disc) and then Present (Assoc) loop
1933 Corresp_Disc := Parent_Disc;
1934 while Present (Corresp_Disc)
1935 and then Disc /= Corresp_Disc
1936 loop
1937 Corresp_Disc :=
1938 Corresponding_Discriminant (Corresp_Disc);
1939 end loop;
1941 if Disc = Corresp_Disc then
1942 if Nkind (Assoc) = N_Discriminant_Association then
1943 Assoc := Expression (Assoc);
1944 end if;
1946 -- If the located association directly denotes a
1947 -- discriminant, then use the value of a saved
1948 -- association of the aggregate. This is a kludge to
1949 -- handle certain cases involving multiple discriminants
1950 -- mapped to a single discriminant of a descendant. It's
1951 -- not clear how to locate the appropriate discriminant
1952 -- value for such cases. ???
1954 if Is_Entity_Name (Assoc)
1955 and then Ekind (Entity (Assoc)) = E_Discriminant
1956 then
1957 Assoc := Save_Assoc;
1958 end if;
1960 return Duplicate_Subexpr (Assoc);
1961 end if;
1963 Next_Discriminant (Parent_Disc);
1965 if No (Assoc_Elmt) then
1966 Next (Assoc);
1967 else
1968 Next_Elmt (Assoc_Elmt);
1969 if Present (Assoc_Elmt) then
1970 Assoc := Node (Assoc_Elmt);
1971 else
1972 Assoc := Empty;
1973 end if;
1974 end if;
1975 end loop;
1976 end if;
1978 Current_Typ := Parent_Typ;
1979 Parent_Typ := Etype (Current_Typ);
1980 end loop;
1982 -- In some cases there's no ancestor value to locate (such as
1983 -- when an ancestor part given by an expression defines the
1984 -- discriminant value).
1986 return Empty;
1987 end Ancestor_Discriminant_Value;
1989 ----------------------------------
1990 -- Check_Ancestor_Discriminants --
1991 ----------------------------------
1993 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1994 Discr : Entity_Id;
1995 Disc_Value : Node_Id;
1996 Cond : Node_Id;
1998 begin
1999 Discr := First_Discriminant (Base_Type (Anc_Typ));
2000 while Present (Discr) loop
2001 Disc_Value := Ancestor_Discriminant_Value (Discr);
2003 if Present (Disc_Value) then
2004 Cond := Make_Op_Ne (Loc,
2005 Left_Opnd =>
2006 Make_Selected_Component (Loc,
2007 Prefix => New_Copy_Tree (Target),
2008 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2009 Right_Opnd => Disc_Value);
2011 Append_To (L,
2012 Make_Raise_Constraint_Error (Loc,
2013 Condition => Cond,
2014 Reason => CE_Discriminant_Check_Failed));
2015 end if;
2017 Next_Discriminant (Discr);
2018 end loop;
2019 end Check_Ancestor_Discriminants;
2021 ---------------------------
2022 -- Compatible_Int_Bounds --
2023 ---------------------------
2025 function Compatible_Int_Bounds
2026 (Agg_Bounds : Node_Id;
2027 Typ_Bounds : Node_Id) return Boolean
2029 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2030 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2031 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2032 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2033 begin
2034 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2035 end Compatible_Int_Bounds;
2037 --------------------------------
2038 -- Get_Constraint_Association --
2039 --------------------------------
2041 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2042 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
2043 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
2045 begin
2046 -- ??? Also need to cover case of a type mark denoting a subtype
2047 -- with constraint.
2049 if Nkind (Indic) = N_Subtype_Indication
2050 and then Present (Constraint (Indic))
2051 then
2052 return First (Constraints (Constraint (Indic)));
2053 end if;
2055 return Empty;
2056 end Get_Constraint_Association;
2058 ---------------------
2059 -- Init_Controller --
2060 ---------------------
2062 function Init_Controller
2063 (Target : Node_Id;
2064 Typ : Entity_Id;
2065 F : Node_Id;
2066 Attach : Node_Id;
2067 Init_Pr : Boolean) return List_Id
2069 L : constant List_Id := New_List;
2070 Ref : Node_Id;
2071 RC : RE_Id;
2072 Target_Type : Entity_Id;
2074 begin
2075 -- Generate:
2076 -- init-proc (target._controller);
2077 -- initialize (target._controller);
2078 -- Attach_to_Final_List (target._controller, F);
2080 Ref :=
2081 Make_Selected_Component (Loc,
2082 Prefix => Convert_To (Typ, New_Copy_Tree (Target)),
2083 Selector_Name => Make_Identifier (Loc, Name_uController));
2084 Set_Assignment_OK (Ref);
2086 -- Ada 2005 (AI-287): Give support to aggregates of limited types.
2087 -- If the type is intrinsically limited the controller is limited as
2088 -- well. If it is tagged and limited then so is the controller.
2089 -- Otherwise an untagged type may have limited components without its
2090 -- full view being limited, so the controller is not limited.
2092 if Nkind (Target) = N_Identifier then
2093 Target_Type := Etype (Target);
2095 elsif Nkind (Target) = N_Selected_Component then
2096 Target_Type := Etype (Selector_Name (Target));
2098 elsif Nkind (Target) = N_Unchecked_Type_Conversion then
2099 Target_Type := Etype (Target);
2101 elsif Nkind (Target) = N_Unchecked_Expression
2102 and then Nkind (Expression (Target)) = N_Indexed_Component
2103 then
2104 Target_Type := Etype (Prefix (Expression (Target)));
2106 else
2107 Target_Type := Etype (Target);
2108 end if;
2110 -- If the target has not been analyzed yet, as will happen with
2111 -- delayed expansion, use the given type (either the aggregate type
2112 -- or an ancestor) to determine limitedness.
2114 if No (Target_Type) then
2115 Target_Type := Typ;
2116 end if;
2118 if (Is_Tagged_Type (Target_Type))
2119 and then Is_Limited_Type (Target_Type)
2120 then
2121 RC := RE_Limited_Record_Controller;
2123 elsif Is_Inherently_Limited_Type (Target_Type) then
2124 RC := RE_Limited_Record_Controller;
2126 else
2127 RC := RE_Record_Controller;
2128 end if;
2130 if Init_Pr then
2131 Append_List_To (L,
2132 Build_Initialization_Call (Loc,
2133 Id_Ref => Ref,
2134 Typ => RTE (RC),
2135 In_Init_Proc => Within_Init_Proc));
2136 end if;
2138 Append_To (L,
2139 Make_Procedure_Call_Statement (Loc,
2140 Name =>
2141 New_Reference_To (
2142 Find_Prim_Op (RTE (RC), Name_Initialize), Loc),
2143 Parameter_Associations =>
2144 New_List (New_Copy_Tree (Ref))));
2146 Append_To (L,
2147 Make_Attach_Call (
2148 Obj_Ref => New_Copy_Tree (Ref),
2149 Flist_Ref => F,
2150 With_Attach => Attach));
2152 return L;
2153 end Init_Controller;
2155 -------------------------
2156 -- Is_Int_Range_Bounds --
2157 -------------------------
2159 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2160 begin
2161 return Nkind (Bounds) = N_Range
2162 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2163 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2164 end Is_Int_Range_Bounds;
2166 -------------------------------
2167 -- Gen_Ctrl_Actions_For_Aggr --
2168 -------------------------------
2170 procedure Gen_Ctrl_Actions_For_Aggr is
2171 Alloc : Node_Id := Empty;
2173 begin
2174 -- Do the work only the first time this is called
2176 if Ctrl_Stuff_Done then
2177 return;
2178 end if;
2180 Ctrl_Stuff_Done := True;
2182 if Present (Obj)
2183 and then Finalize_Storage_Only (Typ)
2184 and then
2185 (Is_Library_Level_Entity (Obj)
2186 or else Entity (Constant_Value (RTE (RE_Garbage_Collected))) =
2187 Standard_True)
2189 -- why not Is_True (Expr_Value (RTE (RE_Garbaage_Collected) ???
2190 then
2191 Attach := Make_Integer_Literal (Loc, 0);
2193 elsif Nkind (Parent (N)) = N_Qualified_Expression
2194 and then Nkind (Parent (Parent (N))) = N_Allocator
2195 then
2196 Alloc := Parent (Parent (N));
2197 Attach := Make_Integer_Literal (Loc, 2);
2199 else
2200 Attach := Make_Integer_Literal (Loc, 1);
2201 end if;
2203 -- Determine the external finalization list. It is either the
2204 -- finalization list of the outer-scope or the one coming from
2205 -- an outer aggregate. When the target is not a temporary, the
2206 -- proper scope is the scope of the target rather than the
2207 -- potentially transient current scope.
2209 if Needs_Finalization (Typ) then
2211 -- The current aggregate belongs to an allocator which creates
2212 -- an object through an anonymous access type or acts as the root
2213 -- of a coextension chain.
2215 if Present (Alloc)
2216 and then
2217 (Is_Coextension_Root (Alloc)
2218 or else Ekind (Etype (Alloc)) = E_Anonymous_Access_Type)
2219 then
2220 if No (Associated_Final_Chain (Etype (Alloc))) then
2221 Build_Final_List (Alloc, Etype (Alloc));
2222 end if;
2224 External_Final_List :=
2225 Make_Selected_Component (Loc,
2226 Prefix =>
2227 New_Reference_To (
2228 Associated_Final_Chain (Etype (Alloc)), Loc),
2229 Selector_Name =>
2230 Make_Identifier (Loc, Name_F));
2232 elsif Present (Flist) then
2233 External_Final_List := New_Copy_Tree (Flist);
2235 elsif Is_Entity_Name (Target)
2236 and then Present (Scope (Entity (Target)))
2237 then
2238 External_Final_List :=
2239 Find_Final_List (Scope (Entity (Target)));
2241 else
2242 External_Final_List := Find_Final_List (Current_Scope);
2243 end if;
2244 else
2245 External_Final_List := Empty;
2246 end if;
2248 -- Initialize and attach the outer object in the is_controlled case
2250 if Is_Controlled (Typ) then
2251 if Ancestor_Is_Subtype_Mark then
2252 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2253 Set_Assignment_OK (Ref);
2254 Append_To (L,
2255 Make_Procedure_Call_Statement (Loc,
2256 Name =>
2257 New_Reference_To
2258 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2259 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2260 end if;
2262 if not Has_Controlled_Component (Typ) then
2263 Ref := New_Copy_Tree (Target);
2264 Set_Assignment_OK (Ref);
2266 -- This is an aggregate of a coextension. Do not produce a
2267 -- finalization call, but rather attach the reference of the
2268 -- aggregate to its coextension chain.
2270 if Present (Alloc)
2271 and then Is_Dynamic_Coextension (Alloc)
2272 then
2273 if No (Coextensions (Alloc)) then
2274 Set_Coextensions (Alloc, New_Elmt_List);
2275 end if;
2277 Append_Elmt (Ref, Coextensions (Alloc));
2278 else
2279 Append_To (L,
2280 Make_Attach_Call (
2281 Obj_Ref => Ref,
2282 Flist_Ref => New_Copy_Tree (External_Final_List),
2283 With_Attach => Attach));
2284 end if;
2285 end if;
2286 end if;
2288 -- In the Has_Controlled component case, all the intermediate
2289 -- controllers must be initialized.
2291 if Has_Controlled_Component (Typ)
2292 and not Is_Limited_Ancestor_Expansion
2293 then
2294 declare
2295 Inner_Typ : Entity_Id;
2296 Outer_Typ : Entity_Id;
2297 At_Root : Boolean;
2299 begin
2300 -- Find outer type with a controller
2302 Outer_Typ := Base_Type (Typ);
2303 while Outer_Typ /= Init_Typ
2304 and then not Has_New_Controlled_Component (Outer_Typ)
2305 loop
2306 Outer_Typ := Etype (Outer_Typ);
2307 end loop;
2309 -- Attach it to the outer record controller to the external
2310 -- final list.
2312 if Outer_Typ = Init_Typ then
2313 Append_List_To (L,
2314 Init_Controller (
2315 Target => Target,
2316 Typ => Outer_Typ,
2317 F => External_Final_List,
2318 Attach => Attach,
2319 Init_Pr => False));
2321 At_Root := True;
2322 Inner_Typ := Init_Typ;
2324 else
2325 Append_List_To (L,
2326 Init_Controller (
2327 Target => Target,
2328 Typ => Outer_Typ,
2329 F => External_Final_List,
2330 Attach => Attach,
2331 Init_Pr => True));
2333 Inner_Typ := Etype (Outer_Typ);
2334 At_Root :=
2335 not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ;
2336 end if;
2338 -- The outer object has to be attached as well
2340 if Is_Controlled (Typ) then
2341 Ref := New_Copy_Tree (Target);
2342 Set_Assignment_OK (Ref);
2343 Append_To (L,
2344 Make_Attach_Call (
2345 Obj_Ref => Ref,
2346 Flist_Ref => New_Copy_Tree (External_Final_List),
2347 With_Attach => New_Copy_Tree (Attach)));
2348 end if;
2350 -- Initialize the internal controllers for tagged types with
2351 -- more than one controller.
2353 while not At_Root and then Inner_Typ /= Init_Typ loop
2354 if Has_New_Controlled_Component (Inner_Typ) then
2355 F :=
2356 Make_Selected_Component (Loc,
2357 Prefix =>
2358 Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2359 Selector_Name =>
2360 Make_Identifier (Loc, Name_uController));
2361 F :=
2362 Make_Selected_Component (Loc,
2363 Prefix => F,
2364 Selector_Name => Make_Identifier (Loc, Name_F));
2366 Append_List_To (L,
2367 Init_Controller (
2368 Target => Target,
2369 Typ => Inner_Typ,
2370 F => F,
2371 Attach => Make_Integer_Literal (Loc, 1),
2372 Init_Pr => True));
2373 Outer_Typ := Inner_Typ;
2374 end if;
2376 -- Stop at the root
2378 At_Root := Inner_Typ = Etype (Inner_Typ);
2379 Inner_Typ := Etype (Inner_Typ);
2380 end loop;
2382 -- If not done yet attach the controller of the ancestor part
2384 if Outer_Typ /= Init_Typ
2385 and then Inner_Typ = Init_Typ
2386 and then Has_Controlled_Component (Init_Typ)
2387 then
2388 F :=
2389 Make_Selected_Component (Loc,
2390 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2391 Selector_Name =>
2392 Make_Identifier (Loc, Name_uController));
2393 F :=
2394 Make_Selected_Component (Loc,
2395 Prefix => F,
2396 Selector_Name => Make_Identifier (Loc, Name_F));
2398 Attach := Make_Integer_Literal (Loc, 1);
2399 Append_List_To (L,
2400 Init_Controller (
2401 Target => Target,
2402 Typ => Init_Typ,
2403 F => F,
2404 Attach => Attach,
2405 Init_Pr => False));
2407 -- Note: Init_Pr is False because the ancestor part has
2408 -- already been initialized either way (by default, if
2409 -- given by a type name, otherwise from the expression).
2411 end if;
2412 end;
2413 end if;
2414 end Gen_Ctrl_Actions_For_Aggr;
2416 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2417 -- If default expression of a component mentions a discriminant of the
2418 -- type, it must be rewritten as the discriminant of the target object.
2420 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2421 -- If the aggregate contains a self-reference, traverse each expression
2422 -- to replace a possible self-reference with a reference to the proper
2423 -- component of the target of the assignment.
2425 --------------------------
2426 -- Rewrite_Discriminant --
2427 --------------------------
2429 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2430 begin
2431 if Is_Entity_Name (Expr)
2432 and then Present (Entity (Expr))
2433 and then Ekind (Entity (Expr)) = E_In_Parameter
2434 and then Present (Discriminal_Link (Entity (Expr)))
2435 and then Scope (Discriminal_Link (Entity (Expr)))
2436 = Base_Type (Etype (N))
2437 then
2438 Rewrite (Expr,
2439 Make_Selected_Component (Loc,
2440 Prefix => New_Copy_Tree (Lhs),
2441 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2442 end if;
2443 return OK;
2444 end Rewrite_Discriminant;
2446 ------------------
2447 -- Replace_Type --
2448 ------------------
2450 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2451 begin
2452 -- Note regarding the Root_Type test below: Aggregate components for
2453 -- self-referential types include attribute references to the current
2454 -- instance, of the form: Typ'access, etc.. These references are
2455 -- rewritten as references to the target of the aggregate: the
2456 -- left-hand side of an assignment, the entity in a declaration,
2457 -- or a temporary. Without this test, we would improperly extended
2458 -- this rewriting to attribute references whose prefix was not the
2459 -- type of the aggregate.
2461 if Nkind (Expr) = N_Attribute_Reference
2462 and then Is_Entity_Name (Prefix (Expr))
2463 and then Is_Type (Entity (Prefix (Expr)))
2464 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2465 then
2466 if Is_Entity_Name (Lhs) then
2467 Rewrite (Prefix (Expr),
2468 New_Occurrence_Of (Entity (Lhs), Loc));
2470 elsif Nkind (Lhs) = N_Selected_Component then
2471 Rewrite (Expr,
2472 Make_Attribute_Reference (Loc,
2473 Attribute_Name => Name_Unrestricted_Access,
2474 Prefix => New_Copy_Tree (Prefix (Lhs))));
2475 Set_Analyzed (Parent (Expr), False);
2477 else
2478 Rewrite (Expr,
2479 Make_Attribute_Reference (Loc,
2480 Attribute_Name => Name_Unrestricted_Access,
2481 Prefix => New_Copy_Tree (Lhs)));
2482 Set_Analyzed (Parent (Expr), False);
2483 end if;
2484 end if;
2486 return OK;
2487 end Replace_Type;
2489 procedure Replace_Self_Reference is
2490 new Traverse_Proc (Replace_Type);
2492 procedure Replace_Discriminants is
2493 new Traverse_Proc (Rewrite_Discriminant);
2495 -- Start of processing for Build_Record_Aggr_Code
2497 begin
2498 if Has_Self_Reference (N) then
2499 Replace_Self_Reference (N);
2500 end if;
2502 -- If the target of the aggregate is class-wide, we must convert it
2503 -- to the actual type of the aggregate, so that the proper components
2504 -- are visible. We know already that the types are compatible.
2506 if Present (Etype (Lhs))
2507 and then Is_Class_Wide_Type (Etype (Lhs))
2508 then
2509 Target := Unchecked_Convert_To (Typ, Lhs);
2510 else
2511 Target := Lhs;
2512 end if;
2514 -- Deal with the ancestor part of extension aggregates or with the
2515 -- discriminants of the root type.
2517 if Nkind (N) = N_Extension_Aggregate then
2518 declare
2519 A : constant Node_Id := Ancestor_Part (N);
2520 Assign : List_Id;
2522 begin
2523 -- If the ancestor part is a subtype mark "T", we generate
2525 -- init-proc (T(tmp)); if T is constrained and
2526 -- init-proc (S(tmp)); where S applies an appropriate
2527 -- constraint if T is unconstrained
2529 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2530 Ancestor_Is_Subtype_Mark := True;
2532 if Is_Constrained (Entity (A)) then
2533 Init_Typ := Entity (A);
2535 -- For an ancestor part given by an unconstrained type mark,
2536 -- create a subtype constrained by appropriate corresponding
2537 -- discriminant values coming from either associations of the
2538 -- aggregate or a constraint on a parent type. The subtype will
2539 -- be used to generate the correct default value for the
2540 -- ancestor part.
2542 elsif Has_Discriminants (Entity (A)) then
2543 declare
2544 Anc_Typ : constant Entity_Id := Entity (A);
2545 Anc_Constr : constant List_Id := New_List;
2546 Discrim : Entity_Id;
2547 Disc_Value : Node_Id;
2548 New_Indic : Node_Id;
2549 Subt_Decl : Node_Id;
2551 begin
2552 Discrim := First_Discriminant (Anc_Typ);
2553 while Present (Discrim) loop
2554 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2555 Append_To (Anc_Constr, Disc_Value);
2556 Next_Discriminant (Discrim);
2557 end loop;
2559 New_Indic :=
2560 Make_Subtype_Indication (Loc,
2561 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2562 Constraint =>
2563 Make_Index_Or_Discriminant_Constraint (Loc,
2564 Constraints => Anc_Constr));
2566 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2568 Subt_Decl :=
2569 Make_Subtype_Declaration (Loc,
2570 Defining_Identifier => Init_Typ,
2571 Subtype_Indication => New_Indic);
2573 -- Itypes must be analyzed with checks off Declaration
2574 -- must have a parent for proper handling of subsidiary
2575 -- actions.
2577 Set_Parent (Subt_Decl, N);
2578 Analyze (Subt_Decl, Suppress => All_Checks);
2579 end;
2580 end if;
2582 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2583 Set_Assignment_OK (Ref);
2585 if not Is_Interface (Init_Typ) then
2586 Append_List_To (L,
2587 Build_Initialization_Call (Loc,
2588 Id_Ref => Ref,
2589 Typ => Init_Typ,
2590 In_Init_Proc => Within_Init_Proc,
2591 With_Default_Init => Has_Default_Init_Comps (N)
2592 or else
2593 Has_Task (Base_Type (Init_Typ))));
2595 if Is_Constrained (Entity (A))
2596 and then Has_Discriminants (Entity (A))
2597 then
2598 Check_Ancestor_Discriminants (Entity (A));
2599 end if;
2600 end if;
2602 -- Handle calls to C++ constructors
2604 elsif Is_CPP_Constructor_Call (A) then
2605 Init_Typ := Etype (A);
2606 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2607 Set_Assignment_OK (Ref);
2609 Append_List_To (L,
2610 Build_Initialization_Call (Loc,
2611 Id_Ref => Ref,
2612 Typ => Init_Typ,
2613 In_Init_Proc => Within_Init_Proc,
2614 With_Default_Init => Has_Default_Init_Comps (N),
2615 Constructor_Ref => A));
2617 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2618 -- limited type, a recursive call expands the ancestor. Note that
2619 -- in the limited case, the ancestor part must be either a
2620 -- function call (possibly qualified, or wrapped in an unchecked
2621 -- conversion) or aggregate (definitely qualified).
2622 -- The ancestor part can also be a function call (that may be
2623 -- transformed into an explicit dereference) or a qualification
2624 -- of one such.
2626 elsif Is_Limited_Type (Etype (A))
2627 and then Nkind_In (Unqualify (A), N_Aggregate,
2628 N_Extension_Aggregate)
2629 then
2630 Ancestor_Is_Expression := True;
2632 -- Set up finalization data for enclosing record, because
2633 -- controlled subcomponents of the ancestor part will be
2634 -- attached to it.
2636 Gen_Ctrl_Actions_For_Aggr;
2638 Append_List_To (L,
2639 Build_Record_Aggr_Code (
2640 N => Unqualify (A),
2641 Typ => Etype (Unqualify (A)),
2642 Lhs => Target,
2643 Flist => Flist,
2644 Obj => Obj,
2645 Is_Limited_Ancestor_Expansion => True));
2647 -- If the ancestor part is an expression "E", we generate
2649 -- T(tmp) := E;
2651 -- In Ada 2005, this includes the case of a (possibly qualified)
2652 -- limited function call. The assignment will turn into a
2653 -- build-in-place function call (for further details, see
2654 -- Make_Build_In_Place_Call_In_Assignment).
2656 else
2657 Ancestor_Is_Expression := True;
2658 Init_Typ := Etype (A);
2660 -- If the ancestor part is an aggregate, force its full
2661 -- expansion, which was delayed.
2663 if Nkind_In (Unqualify (A), N_Aggregate,
2664 N_Extension_Aggregate)
2665 then
2666 Set_Analyzed (A, False);
2667 Set_Analyzed (Expression (A), False);
2668 end if;
2670 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2671 Set_Assignment_OK (Ref);
2673 -- Make the assignment without usual controlled actions since
2674 -- we only want the post adjust but not the pre finalize here
2675 -- Add manual adjust when necessary.
2677 Assign := New_List (
2678 Make_OK_Assignment_Statement (Loc,
2679 Name => Ref,
2680 Expression => A));
2681 Set_No_Ctrl_Actions (First (Assign));
2683 -- Assign the tag now to make sure that the dispatching call in
2684 -- the subsequent deep_adjust works properly (unless VM_Target,
2685 -- where tags are implicit).
2687 if Tagged_Type_Expansion then
2688 Instr :=
2689 Make_OK_Assignment_Statement (Loc,
2690 Name =>
2691 Make_Selected_Component (Loc,
2692 Prefix => New_Copy_Tree (Target),
2693 Selector_Name =>
2694 New_Reference_To
2695 (First_Tag_Component (Base_Type (Typ)), Loc)),
2697 Expression =>
2698 Unchecked_Convert_To (RTE (RE_Tag),
2699 New_Reference_To
2700 (Node (First_Elmt
2701 (Access_Disp_Table (Base_Type (Typ)))),
2702 Loc)));
2704 Set_Assignment_OK (Name (Instr));
2705 Append_To (Assign, Instr);
2707 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2708 -- also initialize tags of the secondary dispatch tables.
2710 if Has_Interfaces (Base_Type (Typ)) then
2711 Init_Secondary_Tags
2712 (Typ => Base_Type (Typ),
2713 Target => Target,
2714 Stmts_List => Assign);
2715 end if;
2716 end if;
2718 -- Call Adjust manually
2720 if Needs_Finalization (Etype (A))
2721 and then not Is_Limited_Type (Etype (A))
2722 then
2723 Append_List_To (Assign,
2724 Make_Adjust_Call (
2725 Ref => New_Copy_Tree (Ref),
2726 Typ => Etype (A),
2727 Flist_Ref => New_Reference_To (
2728 RTE (RE_Global_Final_List), Loc),
2729 With_Attach => Make_Integer_Literal (Loc, 0)));
2730 end if;
2732 Append_To (L,
2733 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2735 if Has_Discriminants (Init_Typ) then
2736 Check_Ancestor_Discriminants (Init_Typ);
2737 end if;
2738 end if;
2739 end;
2741 -- Normal case (not an extension aggregate)
2743 else
2744 -- Generate the discriminant expressions, component by component.
2745 -- If the base type is an unchecked union, the discriminants are
2746 -- unknown to the back-end and absent from a value of the type, so
2747 -- assignments for them are not emitted.
2749 if Has_Discriminants (Typ)
2750 and then not Is_Unchecked_Union (Base_Type (Typ))
2751 then
2752 -- If the type is derived, and constrains discriminants of the
2753 -- parent type, these discriminants are not components of the
2754 -- aggregate, and must be initialized explicitly. They are not
2755 -- visible components of the object, but can become visible with
2756 -- a view conversion to the ancestor.
2758 declare
2759 Btype : Entity_Id;
2760 Parent_Type : Entity_Id;
2761 Disc : Entity_Id;
2762 Discr_Val : Elmt_Id;
2764 begin
2765 Btype := Base_Type (Typ);
2766 while Is_Derived_Type (Btype)
2767 and then Present (Stored_Constraint (Btype))
2768 loop
2769 Parent_Type := Etype (Btype);
2771 Disc := First_Discriminant (Parent_Type);
2772 Discr_Val :=
2773 First_Elmt (Stored_Constraint (Base_Type (Typ)));
2774 while Present (Discr_Val) loop
2776 -- Only those discriminants of the parent that are not
2777 -- renamed by discriminants of the derived type need to
2778 -- be added explicitly.
2780 if not Is_Entity_Name (Node (Discr_Val))
2781 or else
2782 Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2783 then
2784 Comp_Expr :=
2785 Make_Selected_Component (Loc,
2786 Prefix => New_Copy_Tree (Target),
2787 Selector_Name => New_Occurrence_Of (Disc, Loc));
2789 Instr :=
2790 Make_OK_Assignment_Statement (Loc,
2791 Name => Comp_Expr,
2792 Expression => New_Copy_Tree (Node (Discr_Val)));
2794 Set_No_Ctrl_Actions (Instr);
2795 Append_To (L, Instr);
2796 end if;
2798 Next_Discriminant (Disc);
2799 Next_Elmt (Discr_Val);
2800 end loop;
2802 Btype := Base_Type (Parent_Type);
2803 end loop;
2804 end;
2806 -- Generate discriminant init values for the visible discriminants
2808 declare
2809 Discriminant : Entity_Id;
2810 Discriminant_Value : Node_Id;
2812 begin
2813 Discriminant := First_Stored_Discriminant (Typ);
2814 while Present (Discriminant) loop
2815 Comp_Expr :=
2816 Make_Selected_Component (Loc,
2817 Prefix => New_Copy_Tree (Target),
2818 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2820 Discriminant_Value :=
2821 Get_Discriminant_Value (
2822 Discriminant,
2823 N_Typ,
2824 Discriminant_Constraint (N_Typ));
2826 Instr :=
2827 Make_OK_Assignment_Statement (Loc,
2828 Name => Comp_Expr,
2829 Expression => New_Copy_Tree (Discriminant_Value));
2831 Set_No_Ctrl_Actions (Instr);
2832 Append_To (L, Instr);
2834 Next_Stored_Discriminant (Discriminant);
2835 end loop;
2836 end;
2837 end if;
2838 end if;
2840 -- For CPP types we generate an implicit call to the C++ default
2841 -- constructor to ensure the proper initialization of the _Tag
2842 -- component.
2844 if Is_CPP_Class (Root_Type (Typ))
2845 and then CPP_Num_Prims (Typ) > 0
2846 then
2847 Invoke_Constructor : declare
2848 CPP_Parent : constant Entity_Id :=
2849 Enclosing_CPP_Parent (Typ);
2851 procedure Invoke_IC_Proc (T : Entity_Id);
2852 -- Recursive routine used to climb to parents. Required because
2853 -- parents must be initialized before descendants to ensure
2854 -- propagation of inherited C++ slots.
2856 --------------------
2857 -- Invoke_IC_Proc --
2858 --------------------
2860 procedure Invoke_IC_Proc (T : Entity_Id) is
2861 begin
2862 -- Avoid generating extra calls. Initialization required
2863 -- only for types defined from the level of derivation of
2864 -- type of the constructor and the type of the aggregate.
2866 if T = CPP_Parent then
2867 return;
2868 end if;
2870 Invoke_IC_Proc (Etype (T));
2872 -- Generate call to the IC routine
2874 if Present (CPP_Init_Proc (T)) then
2875 Append_To (L,
2876 Make_Procedure_Call_Statement (Loc,
2877 New_Reference_To (CPP_Init_Proc (T), Loc)));
2878 end if;
2879 end Invoke_IC_Proc;
2881 -- Start of processing for Invoke_Constructor
2883 begin
2884 -- Implicit invocation of the C++ constructor
2886 if Nkind (N) = N_Aggregate then
2887 Append_To (L,
2888 Make_Procedure_Call_Statement (Loc,
2889 Name =>
2890 New_Reference_To
2891 (Base_Init_Proc (CPP_Parent), Loc),
2892 Parameter_Associations => New_List (
2893 Unchecked_Convert_To (CPP_Parent,
2894 New_Copy_Tree (Lhs)))));
2895 end if;
2897 Invoke_IC_Proc (Typ);
2898 end Invoke_Constructor;
2899 end if;
2901 -- Generate the assignments, component by component
2903 -- tmp.comp1 := Expr1_From_Aggr;
2904 -- tmp.comp2 := Expr2_From_Aggr;
2905 -- ....
2907 Comp := First (Component_Associations (N));
2908 while Present (Comp) loop
2909 Selector := Entity (First (Choices (Comp)));
2911 -- C++ constructors
2913 if Is_CPP_Constructor_Call (Expression (Comp)) then
2914 Append_List_To (L,
2915 Build_Initialization_Call (Loc,
2916 Id_Ref => Make_Selected_Component (Loc,
2917 Prefix => New_Copy_Tree (Target),
2918 Selector_Name =>
2919 New_Occurrence_Of (Selector, Loc)),
2920 Typ => Etype (Selector),
2921 Enclos_Type => Typ,
2922 With_Default_Init => True,
2923 Constructor_Ref => Expression (Comp)));
2925 -- Ada 2005 (AI-287): For each default-initialized component generate
2926 -- a call to the corresponding IP subprogram if available.
2928 elsif Box_Present (Comp)
2929 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2930 then
2931 if Ekind (Selector) /= E_Discriminant then
2932 Gen_Ctrl_Actions_For_Aggr;
2933 end if;
2935 -- Ada 2005 (AI-287): If the component type has tasks then
2936 -- generate the activation chain and master entities (except
2937 -- in case of an allocator because in that case these entities
2938 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2940 declare
2941 Ctype : constant Entity_Id := Etype (Selector);
2942 Inside_Allocator : Boolean := False;
2943 P : Node_Id := Parent (N);
2945 begin
2946 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2947 while Present (P) loop
2948 if Nkind (P) = N_Allocator then
2949 Inside_Allocator := True;
2950 exit;
2951 end if;
2953 P := Parent (P);
2954 end loop;
2956 if not Inside_Init_Proc and not Inside_Allocator then
2957 Build_Activation_Chain_Entity (N);
2958 end if;
2959 end if;
2960 end;
2962 Append_List_To (L,
2963 Build_Initialization_Call (Loc,
2964 Id_Ref => Make_Selected_Component (Loc,
2965 Prefix => New_Copy_Tree (Target),
2966 Selector_Name =>
2967 New_Occurrence_Of (Selector, Loc)),
2968 Typ => Etype (Selector),
2969 Enclos_Type => Typ,
2970 With_Default_Init => True));
2972 -- Prepare for component assignment
2974 elsif Ekind (Selector) /= E_Discriminant
2975 or else Nkind (N) = N_Extension_Aggregate
2976 then
2977 -- All the discriminants have now been assigned
2979 -- This is now a good moment to initialize and attach all the
2980 -- controllers. Their position may depend on the discriminants.
2982 if Ekind (Selector) /= E_Discriminant then
2983 Gen_Ctrl_Actions_For_Aggr;
2984 end if;
2986 Comp_Type := Etype (Selector);
2987 Comp_Expr :=
2988 Make_Selected_Component (Loc,
2989 Prefix => New_Copy_Tree (Target),
2990 Selector_Name => New_Occurrence_Of (Selector, Loc));
2992 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2993 Expr_Q := Expression (Expression (Comp));
2994 else
2995 Expr_Q := Expression (Comp);
2996 end if;
2998 -- The controller is the one of the parent type defining the
2999 -- component (in case of inherited components).
3001 if Needs_Finalization (Comp_Type) then
3002 Internal_Final_List :=
3003 Make_Selected_Component (Loc,
3004 Prefix => Convert_To (
3005 Scope (Original_Record_Component (Selector)),
3006 New_Copy_Tree (Target)),
3007 Selector_Name =>
3008 Make_Identifier (Loc, Name_uController));
3010 Internal_Final_List :=
3011 Make_Selected_Component (Loc,
3012 Prefix => Internal_Final_List,
3013 Selector_Name => Make_Identifier (Loc, Name_F));
3015 -- The internal final list can be part of a constant object
3017 Set_Assignment_OK (Internal_Final_List);
3019 else
3020 Internal_Final_List := Empty;
3021 end if;
3023 -- Now either create the assignment or generate the code for the
3024 -- inner aggregate top-down.
3026 if Is_Delayed_Aggregate (Expr_Q) then
3028 -- We have the following case of aggregate nesting inside
3029 -- an object declaration:
3031 -- type Arr_Typ is array (Integer range <>) of ...;
3033 -- type Rec_Typ (...) is record
3034 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3035 -- end record;
3037 -- Obj_Rec_Typ : Rec_Typ := (...,
3038 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3040 -- The length of the ranges of the aggregate and Obj_Add_Typ
3041 -- are equal (B - A = Y - X), but they do not coincide (X /=
3042 -- A and B /= Y). This case requires array sliding which is
3043 -- performed in the following manner:
3045 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3046 -- Temp : Arr_Sub;
3047 -- Temp (X) := (...);
3048 -- ...
3049 -- Temp (Y) := (...);
3050 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3052 if Ekind (Comp_Type) = E_Array_Subtype
3053 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3054 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3055 and then not
3056 Compatible_Int_Bounds
3057 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3058 Typ_Bounds => First_Index (Comp_Type))
3059 then
3060 -- Create the array subtype with bounds equal to those of
3061 -- the corresponding aggregate.
3063 declare
3064 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3066 SubD : constant Node_Id :=
3067 Make_Subtype_Declaration (Loc,
3068 Defining_Identifier => SubE,
3069 Subtype_Indication =>
3070 Make_Subtype_Indication (Loc,
3071 Subtype_Mark =>
3072 New_Reference_To
3073 (Etype (Comp_Type), Loc),
3074 Constraint =>
3075 Make_Index_Or_Discriminant_Constraint
3076 (Loc,
3077 Constraints => New_List (
3078 New_Copy_Tree
3079 (Aggregate_Bounds (Expr_Q))))));
3081 -- Create a temporary array of the above subtype which
3082 -- will be used to capture the aggregate assignments.
3084 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3086 TmpD : constant Node_Id :=
3087 Make_Object_Declaration (Loc,
3088 Defining_Identifier => TmpE,
3089 Object_Definition =>
3090 New_Reference_To (SubE, Loc));
3092 begin
3093 Set_No_Initialization (TmpD);
3094 Append_To (L, SubD);
3095 Append_To (L, TmpD);
3097 -- Expand aggregate into assignments to the temp array
3099 Append_List_To (L,
3100 Late_Expansion (Expr_Q, Comp_Type,
3101 New_Reference_To (TmpE, Loc), Internal_Final_List));
3103 -- Slide
3105 Append_To (L,
3106 Make_Assignment_Statement (Loc,
3107 Name => New_Copy_Tree (Comp_Expr),
3108 Expression => New_Reference_To (TmpE, Loc)));
3110 -- Do not pass the original aggregate to Gigi as is,
3111 -- since it will potentially clobber the front or the end
3112 -- of the array. Setting the expression to empty is safe
3113 -- since all aggregates are expanded into assignments.
3115 if Present (Obj) then
3116 Set_Expression (Parent (Obj), Empty);
3117 end if;
3118 end;
3120 -- Normal case (sliding not required)
3122 else
3123 Append_List_To (L,
3124 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr,
3125 Internal_Final_List));
3126 end if;
3128 -- Expr_Q is not delayed aggregate
3130 else
3131 if Has_Discriminants (Typ) then
3132 Replace_Discriminants (Expr_Q);
3133 end if;
3135 Instr :=
3136 Make_OK_Assignment_Statement (Loc,
3137 Name => Comp_Expr,
3138 Expression => Expr_Q);
3140 Set_No_Ctrl_Actions (Instr);
3141 Append_To (L, Instr);
3143 -- Adjust the tag if tagged (because of possible view
3144 -- conversions), unless compiling for a VM where tags are
3145 -- implicit.
3147 -- tmp.comp._tag := comp_typ'tag;
3149 if Is_Tagged_Type (Comp_Type)
3150 and then Tagged_Type_Expansion
3151 then
3152 Instr :=
3153 Make_OK_Assignment_Statement (Loc,
3154 Name =>
3155 Make_Selected_Component (Loc,
3156 Prefix => New_Copy_Tree (Comp_Expr),
3157 Selector_Name =>
3158 New_Reference_To
3159 (First_Tag_Component (Comp_Type), Loc)),
3161 Expression =>
3162 Unchecked_Convert_To (RTE (RE_Tag),
3163 New_Reference_To
3164 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
3165 Loc)));
3167 Append_To (L, Instr);
3168 end if;
3170 -- Adjust and Attach the component to the proper controller
3172 -- Adjust (tmp.comp);
3173 -- Attach_To_Final_List (tmp.comp,
3174 -- comp_typ (tmp)._record_controller.f)
3176 if Needs_Finalization (Comp_Type)
3177 and then not Is_Limited_Type (Comp_Type)
3178 then
3179 Append_List_To (L,
3180 Make_Adjust_Call (
3181 Ref => New_Copy_Tree (Comp_Expr),
3182 Typ => Comp_Type,
3183 Flist_Ref => Internal_Final_List,
3184 With_Attach => Make_Integer_Literal (Loc, 1)));
3185 end if;
3186 end if;
3188 -- ???
3190 elsif Ekind (Selector) = E_Discriminant
3191 and then Nkind (N) /= N_Extension_Aggregate
3192 and then Nkind (Parent (N)) = N_Component_Association
3193 and then Is_Constrained (Typ)
3194 then
3195 -- We must check that the discriminant value imposed by the
3196 -- context is the same as the value given in the subaggregate,
3197 -- because after the expansion into assignments there is no
3198 -- record on which to perform a regular discriminant check.
3200 declare
3201 D_Val : Elmt_Id;
3202 Disc : Entity_Id;
3204 begin
3205 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3206 Disc := First_Discriminant (Typ);
3207 while Chars (Disc) /= Chars (Selector) loop
3208 Next_Discriminant (Disc);
3209 Next_Elmt (D_Val);
3210 end loop;
3212 pragma Assert (Present (D_Val));
3214 -- This check cannot performed for components that are
3215 -- constrained by a current instance, because this is not a
3216 -- value that can be compared with the actual constraint.
3218 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3219 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3220 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3221 then
3222 Append_To (L,
3223 Make_Raise_Constraint_Error (Loc,
3224 Condition =>
3225 Make_Op_Ne (Loc,
3226 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3227 Right_Opnd => Expression (Comp)),
3228 Reason => CE_Discriminant_Check_Failed));
3230 else
3231 -- Find self-reference in previous discriminant assignment,
3232 -- and replace with proper expression.
3234 declare
3235 Ass : Node_Id;
3237 begin
3238 Ass := First (L);
3239 while Present (Ass) loop
3240 if Nkind (Ass) = N_Assignment_Statement
3241 and then Nkind (Name (Ass)) = N_Selected_Component
3242 and then Chars (Selector_Name (Name (Ass))) =
3243 Chars (Disc)
3244 then
3245 Set_Expression
3246 (Ass, New_Copy_Tree (Expression (Comp)));
3247 exit;
3248 end if;
3249 Next (Ass);
3250 end loop;
3251 end;
3252 end if;
3253 end;
3254 end if;
3256 Next (Comp);
3257 end loop;
3259 -- If the type is tagged, the tag needs to be initialized (unless
3260 -- compiling for the Java VM where tags are implicit). It is done
3261 -- late in the initialization process because in some cases, we call
3262 -- the init proc of an ancestor which will not leave out the right tag
3264 if Ancestor_Is_Expression then
3265 null;
3267 -- For CPP types we generated a call to the C++ default constructor
3268 -- before the components have been initialized to ensure the proper
3269 -- initialization of the _Tag component (see above).
3271 elsif Is_CPP_Class (Typ) then
3272 null;
3274 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3275 Instr :=
3276 Make_OK_Assignment_Statement (Loc,
3277 Name =>
3278 Make_Selected_Component (Loc,
3279 Prefix => New_Copy_Tree (Target),
3280 Selector_Name =>
3281 New_Reference_To
3282 (First_Tag_Component (Base_Type (Typ)), Loc)),
3284 Expression =>
3285 Unchecked_Convert_To (RTE (RE_Tag),
3286 New_Reference_To
3287 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3288 Loc)));
3290 Append_To (L, Instr);
3292 -- Ada 2005 (AI-251): If the tagged type has been derived from
3293 -- abstract interfaces we must also initialize the tags of the
3294 -- secondary dispatch tables.
3296 if Has_Interfaces (Base_Type (Typ)) then
3297 Init_Secondary_Tags
3298 (Typ => Base_Type (Typ),
3299 Target => Target,
3300 Stmts_List => L);
3301 end if;
3302 end if;
3304 -- If the controllers have not been initialized yet (by lack of non-
3305 -- discriminant components), let's do it now.
3307 Gen_Ctrl_Actions_For_Aggr;
3309 return L;
3310 end Build_Record_Aggr_Code;
3312 -------------------------------
3313 -- Convert_Aggr_In_Allocator --
3314 -------------------------------
3316 procedure Convert_Aggr_In_Allocator
3317 (Alloc : Node_Id;
3318 Decl : Node_Id;
3319 Aggr : Node_Id)
3321 Loc : constant Source_Ptr := Sloc (Aggr);
3322 Typ : constant Entity_Id := Etype (Aggr);
3323 Temp : constant Entity_Id := Defining_Identifier (Decl);
3325 Occ : constant Node_Id :=
3326 Unchecked_Convert_To (Typ,
3327 Make_Explicit_Dereference (Loc,
3328 New_Reference_To (Temp, Loc)));
3330 Access_Type : constant Entity_Id := Etype (Temp);
3331 Flist : Entity_Id;
3333 begin
3334 -- If the allocator is for an access discriminant, there is no
3335 -- finalization list for the anonymous access type, and the eventual
3336 -- finalization of the object is handled through the coextension
3337 -- mechanism. If the enclosing object is not dynamically allocated,
3338 -- the access discriminant is itself placed on the stack. Otherwise,
3339 -- some other finalization list is used (see exp_ch4.adb).
3341 -- Decl has been inserted in the code ahead of the allocator, using
3342 -- Insert_Actions. We use Insert_Actions below as well, to ensure that
3343 -- subsequent insertions are done in the proper order. Using (for
3344 -- example) Insert_Actions_After to place the expanded aggregate
3345 -- immediately after Decl may lead to out-of-order references if the
3346 -- allocator has generated a finalization list, as when the designated
3347 -- object is controlled and there is an open transient scope.
3349 if Ekind (Access_Type) = E_Anonymous_Access_Type
3350 and then Nkind (Associated_Node_For_Itype (Access_Type)) =
3351 N_Discriminant_Specification
3352 then
3353 Flist := Empty;
3355 elsif Needs_Finalization (Typ) then
3356 Flist := Find_Final_List (Access_Type);
3358 -- Otherwise there are no controlled actions to be performed.
3360 else
3361 Flist := Empty;
3362 end if;
3364 if Is_Array_Type (Typ) then
3365 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3367 elsif Has_Default_Init_Comps (Aggr) then
3368 declare
3369 L : constant List_Id := New_List;
3370 Init_Stmts : List_Id;
3372 begin
3373 Init_Stmts :=
3374 Late_Expansion
3375 (Aggr, Typ, Occ,
3376 Flist,
3377 Associated_Final_Chain (Base_Type (Access_Type)));
3379 -- ??? Dubious actual for Obj: expect 'the original object being
3380 -- initialized'
3382 if Has_Task (Typ) then
3383 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3384 Insert_Actions (Alloc, L);
3385 else
3386 Insert_Actions (Alloc, Init_Stmts);
3387 end if;
3388 end;
3390 else
3391 Insert_Actions (Alloc,
3392 Late_Expansion
3393 (Aggr, Typ, Occ, Flist,
3394 Associated_Final_Chain (Base_Type (Access_Type))));
3396 -- ??? Dubious actual for Obj: expect 'the original object being
3397 -- initialized'
3399 end if;
3400 end Convert_Aggr_In_Allocator;
3402 --------------------------------
3403 -- Convert_Aggr_In_Assignment --
3404 --------------------------------
3406 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3407 Aggr : Node_Id := Expression (N);
3408 Typ : constant Entity_Id := Etype (Aggr);
3409 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3411 begin
3412 if Nkind (Aggr) = N_Qualified_Expression then
3413 Aggr := Expression (Aggr);
3414 end if;
3416 Insert_Actions_After (N,
3417 Late_Expansion
3418 (Aggr, Typ, Occ,
3419 Find_Final_List (Typ, New_Copy_Tree (Occ))));
3420 end Convert_Aggr_In_Assignment;
3422 ---------------------------------
3423 -- Convert_Aggr_In_Object_Decl --
3424 ---------------------------------
3426 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3427 Obj : constant Entity_Id := Defining_Identifier (N);
3428 Aggr : Node_Id := Expression (N);
3429 Loc : constant Source_Ptr := Sloc (Aggr);
3430 Typ : constant Entity_Id := Etype (Aggr);
3431 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3433 function Discriminants_Ok return Boolean;
3434 -- If the object type is constrained, the discriminants in the
3435 -- aggregate must be checked against the discriminants of the subtype.
3436 -- This cannot be done using Apply_Discriminant_Checks because after
3437 -- expansion there is no aggregate left to check.
3439 ----------------------
3440 -- Discriminants_Ok --
3441 ----------------------
3443 function Discriminants_Ok return Boolean is
3444 Cond : Node_Id := Empty;
3445 Check : Node_Id;
3446 D : Entity_Id;
3447 Disc1 : Elmt_Id;
3448 Disc2 : Elmt_Id;
3449 Val1 : Node_Id;
3450 Val2 : Node_Id;
3452 begin
3453 D := First_Discriminant (Typ);
3454 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3455 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3456 while Present (Disc1) and then Present (Disc2) loop
3457 Val1 := Node (Disc1);
3458 Val2 := Node (Disc2);
3460 if not Is_OK_Static_Expression (Val1)
3461 or else not Is_OK_Static_Expression (Val2)
3462 then
3463 Check := Make_Op_Ne (Loc,
3464 Left_Opnd => Duplicate_Subexpr (Val1),
3465 Right_Opnd => Duplicate_Subexpr (Val2));
3467 if No (Cond) then
3468 Cond := Check;
3470 else
3471 Cond := Make_Or_Else (Loc,
3472 Left_Opnd => Cond,
3473 Right_Opnd => Check);
3474 end if;
3476 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3477 Apply_Compile_Time_Constraint_Error (Aggr,
3478 Msg => "incorrect value for discriminant&?",
3479 Reason => CE_Discriminant_Check_Failed,
3480 Ent => D);
3481 return False;
3482 end if;
3484 Next_Discriminant (D);
3485 Next_Elmt (Disc1);
3486 Next_Elmt (Disc2);
3487 end loop;
3489 -- If any discriminant constraint is non-static, emit a check
3491 if Present (Cond) then
3492 Insert_Action (N,
3493 Make_Raise_Constraint_Error (Loc,
3494 Condition => Cond,
3495 Reason => CE_Discriminant_Check_Failed));
3496 end if;
3498 return True;
3499 end Discriminants_Ok;
3501 -- Start of processing for Convert_Aggr_In_Object_Decl
3503 begin
3504 Set_Assignment_OK (Occ);
3506 if Nkind (Aggr) = N_Qualified_Expression then
3507 Aggr := Expression (Aggr);
3508 end if;
3510 if Has_Discriminants (Typ)
3511 and then Typ /= Etype (Obj)
3512 and then Is_Constrained (Etype (Obj))
3513 and then not Discriminants_Ok
3514 then
3515 return;
3516 end if;
3518 -- If the context is an extended return statement, it has its own
3519 -- finalization machinery (i.e. works like a transient scope) and
3520 -- we do not want to create an additional one, because objects on
3521 -- the finalization list of the return must be moved to the caller's
3522 -- finalization list to complete the return.
3524 -- However, if the aggregate is limited, it is built in place, and the
3525 -- controlled components are not assigned to intermediate temporaries
3526 -- so there is no need for a transient scope in this case either.
3528 if Requires_Transient_Scope (Typ)
3529 and then Ekind (Current_Scope) /= E_Return_Statement
3530 and then not Is_Limited_Type (Typ)
3531 then
3532 Establish_Transient_Scope
3533 (Aggr,
3534 Sec_Stack =>
3535 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3536 end if;
3538 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
3539 Set_No_Initialization (N);
3540 Initialize_Discriminants (N, Typ);
3541 end Convert_Aggr_In_Object_Decl;
3543 -------------------------------------
3544 -- Convert_Array_Aggr_In_Allocator --
3545 -------------------------------------
3547 procedure Convert_Array_Aggr_In_Allocator
3548 (Decl : Node_Id;
3549 Aggr : Node_Id;
3550 Target : Node_Id)
3552 Aggr_Code : List_Id;
3553 Typ : constant Entity_Id := Etype (Aggr);
3554 Ctyp : constant Entity_Id := Component_Type (Typ);
3556 begin
3557 -- The target is an explicit dereference of the allocated object.
3558 -- Generate component assignments to it, as for an aggregate that
3559 -- appears on the right-hand side of an assignment statement.
3561 Aggr_Code :=
3562 Build_Array_Aggr_Code (Aggr,
3563 Ctype => Ctyp,
3564 Index => First_Index (Typ),
3565 Into => Target,
3566 Scalar_Comp => Is_Scalar_Type (Ctyp));
3568 Insert_Actions_After (Decl, Aggr_Code);
3569 end Convert_Array_Aggr_In_Allocator;
3571 ----------------------------
3572 -- Convert_To_Assignments --
3573 ----------------------------
3575 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3576 Loc : constant Source_Ptr := Sloc (N);
3577 T : Entity_Id;
3578 Temp : Entity_Id;
3580 Instr : Node_Id;
3581 Target_Expr : Node_Id;
3582 Parent_Kind : Node_Kind;
3583 Unc_Decl : Boolean := False;
3584 Parent_Node : Node_Id;
3586 begin
3587 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3588 pragma Assert (Is_Record_Type (Typ));
3590 Parent_Node := Parent (N);
3591 Parent_Kind := Nkind (Parent_Node);
3593 if Parent_Kind = N_Qualified_Expression then
3595 -- Check if we are in a unconstrained declaration because in this
3596 -- case the current delayed expansion mechanism doesn't work when
3597 -- the declared object size depend on the initializing expr.
3599 begin
3600 Parent_Node := Parent (Parent_Node);
3601 Parent_Kind := Nkind (Parent_Node);
3603 if Parent_Kind = N_Object_Declaration then
3604 Unc_Decl :=
3605 not Is_Entity_Name (Object_Definition (Parent_Node))
3606 or else Has_Discriminants
3607 (Entity (Object_Definition (Parent_Node)))
3608 or else Is_Class_Wide_Type
3609 (Entity (Object_Definition (Parent_Node)));
3610 end if;
3611 end;
3612 end if;
3614 -- Just set the Delay flag in the cases where the transformation will be
3615 -- done top down from above.
3617 if False
3619 -- Internal aggregate (transformed when expanding the parent)
3621 or else Parent_Kind = N_Aggregate
3622 or else Parent_Kind = N_Extension_Aggregate
3623 or else Parent_Kind = N_Component_Association
3625 -- Allocator (see Convert_Aggr_In_Allocator)
3627 or else Parent_Kind = N_Allocator
3629 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3631 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3633 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3634 -- assignments in init procs are taken into account.
3636 or else (Parent_Kind = N_Assignment_Statement
3637 and then Inside_Init_Proc)
3639 -- (Ada 2005) An inherently limited type in a return statement,
3640 -- which will be handled in a build-in-place fashion, and may be
3641 -- rewritten as an extended return and have its own finalization
3642 -- machinery. In the case of a simple return, the aggregate needs
3643 -- to be delayed until the scope for the return statement has been
3644 -- created, so that any finalization chain will be associated with
3645 -- that scope. For extended returns, we delay expansion to avoid the
3646 -- creation of an unwanted transient scope that could result in
3647 -- premature finalization of the return object (which is built in
3648 -- in place within the caller's scope).
3650 or else
3651 (Is_Inherently_Limited_Type (Typ)
3652 and then
3653 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3654 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3655 then
3656 Set_Expansion_Delayed (N);
3657 return;
3658 end if;
3660 if Requires_Transient_Scope (Typ) then
3661 Establish_Transient_Scope
3662 (N, Sec_Stack =>
3663 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3664 end if;
3666 -- If the aggregate is non-limited, create a temporary. If it is limited
3667 -- and the context is an assignment, this is a subaggregate for an
3668 -- enclosing aggregate being expanded. It must be built in place, so use
3669 -- the target of the current assignment.
3671 if Is_Limited_Type (Typ)
3672 and then Nkind (Parent (N)) = N_Assignment_Statement
3673 then
3674 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3675 Insert_Actions
3676 (Parent (N), Build_Record_Aggr_Code (N, Typ, Target_Expr));
3677 Rewrite (Parent (N), Make_Null_Statement (Loc));
3679 else
3680 Temp := Make_Temporary (Loc, 'A', N);
3682 -- If the type inherits unknown discriminants, use the view with
3683 -- known discriminants if available.
3685 if Has_Unknown_Discriminants (Typ)
3686 and then Present (Underlying_Record_View (Typ))
3687 then
3688 T := Underlying_Record_View (Typ);
3689 else
3690 T := Typ;
3691 end if;
3693 Instr :=
3694 Make_Object_Declaration (Loc,
3695 Defining_Identifier => Temp,
3696 Object_Definition => New_Occurrence_Of (T, Loc));
3698 Set_No_Initialization (Instr);
3699 Insert_Action (N, Instr);
3700 Initialize_Discriminants (Instr, T);
3701 Target_Expr := New_Occurrence_Of (Temp, Loc);
3702 Insert_Actions (N, Build_Record_Aggr_Code (N, T, Target_Expr));
3703 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3704 Analyze_And_Resolve (N, T);
3705 end if;
3706 end Convert_To_Assignments;
3708 ---------------------------
3709 -- Convert_To_Positional --
3710 ---------------------------
3712 procedure Convert_To_Positional
3713 (N : Node_Id;
3714 Max_Others_Replicate : Nat := 5;
3715 Handle_Bit_Packed : Boolean := False)
3717 Typ : constant Entity_Id := Etype (N);
3719 Static_Components : Boolean := True;
3721 procedure Check_Static_Components;
3722 -- Check whether all components of the aggregate are compile-time known
3723 -- values, and can be passed as is to the back-end without further
3724 -- expansion.
3726 function Flatten
3727 (N : Node_Id;
3728 Ix : Node_Id;
3729 Ixb : Node_Id) return Boolean;
3730 -- Convert the aggregate into a purely positional form if possible. On
3731 -- entry the bounds of all dimensions are known to be static, and the
3732 -- total number of components is safe enough to expand.
3734 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3735 -- Return True iff the array N is flat (which is not trivial in the case
3736 -- of multidimensionsl aggregates).
3738 -----------------------------
3739 -- Check_Static_Components --
3740 -----------------------------
3742 procedure Check_Static_Components is
3743 Expr : Node_Id;
3745 begin
3746 Static_Components := True;
3748 if Nkind (N) = N_String_Literal then
3749 null;
3751 elsif Present (Expressions (N)) then
3752 Expr := First (Expressions (N));
3753 while Present (Expr) loop
3754 if Nkind (Expr) /= N_Aggregate
3755 or else not Compile_Time_Known_Aggregate (Expr)
3756 or else Expansion_Delayed (Expr)
3757 then
3758 Static_Components := False;
3759 exit;
3760 end if;
3762 Next (Expr);
3763 end loop;
3764 end if;
3766 if Nkind (N) = N_Aggregate
3767 and then Present (Component_Associations (N))
3768 then
3769 Expr := First (Component_Associations (N));
3770 while Present (Expr) loop
3771 if Nkind (Expression (Expr)) = N_Integer_Literal then
3772 null;
3774 elsif Nkind (Expression (Expr)) /= N_Aggregate
3775 or else
3776 not Compile_Time_Known_Aggregate (Expression (Expr))
3777 or else Expansion_Delayed (Expression (Expr))
3778 then
3779 Static_Components := False;
3780 exit;
3781 end if;
3783 Next (Expr);
3784 end loop;
3785 end if;
3786 end Check_Static_Components;
3788 -------------
3789 -- Flatten --
3790 -------------
3792 function Flatten
3793 (N : Node_Id;
3794 Ix : Node_Id;
3795 Ixb : Node_Id) return Boolean
3797 Loc : constant Source_Ptr := Sloc (N);
3798 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3799 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3800 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3801 Lov : Uint;
3802 Hiv : Uint;
3804 begin
3805 if Nkind (Original_Node (N)) = N_String_Literal then
3806 return True;
3807 end if;
3809 if not Compile_Time_Known_Value (Lo)
3810 or else not Compile_Time_Known_Value (Hi)
3811 then
3812 return False;
3813 end if;
3815 Lov := Expr_Value (Lo);
3816 Hiv := Expr_Value (Hi);
3818 if Hiv < Lov
3819 or else not Compile_Time_Known_Value (Blo)
3820 then
3821 return False;
3822 end if;
3824 -- Determine if set of alternatives is suitable for conversion and
3825 -- build an array containing the values in sequence.
3827 declare
3828 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3829 of Node_Id := (others => Empty);
3830 -- The values in the aggregate sorted appropriately
3832 Vlist : List_Id;
3833 -- Same data as Vals in list form
3835 Rep_Count : Nat;
3836 -- Used to validate Max_Others_Replicate limit
3838 Elmt : Node_Id;
3839 Num : Int := UI_To_Int (Lov);
3840 Choice_Index : Int;
3841 Choice : Node_Id;
3842 Lo, Hi : Node_Id;
3844 begin
3845 if Present (Expressions (N)) then
3846 Elmt := First (Expressions (N));
3847 while Present (Elmt) loop
3848 if Nkind (Elmt) = N_Aggregate
3849 and then Present (Next_Index (Ix))
3850 and then
3851 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3852 then
3853 return False;
3854 end if;
3856 Vals (Num) := Relocate_Node (Elmt);
3857 Num := Num + 1;
3859 Next (Elmt);
3860 end loop;
3861 end if;
3863 if No (Component_Associations (N)) then
3864 return True;
3865 end if;
3867 Elmt := First (Component_Associations (N));
3869 if Nkind (Expression (Elmt)) = N_Aggregate then
3870 if Present (Next_Index (Ix))
3871 and then
3872 not Flatten
3873 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3874 then
3875 return False;
3876 end if;
3877 end if;
3879 Component_Loop : while Present (Elmt) loop
3880 Choice := First (Choices (Elmt));
3881 Choice_Loop : while Present (Choice) loop
3883 -- If we have an others choice, fill in the missing elements
3884 -- subject to the limit established by Max_Others_Replicate.
3886 if Nkind (Choice) = N_Others_Choice then
3887 Rep_Count := 0;
3889 for J in Vals'Range loop
3890 if No (Vals (J)) then
3891 Vals (J) := New_Copy_Tree (Expression (Elmt));
3892 Rep_Count := Rep_Count + 1;
3894 -- Check for maximum others replication. Note that
3895 -- we skip this test if either of the restrictions
3896 -- No_Elaboration_Code or No_Implicit_Loops is
3897 -- active, if this is a preelaborable unit or a
3898 -- predefined unit. This ensures that predefined
3899 -- units get the same level of constant folding in
3900 -- Ada 95 and Ada 05, where their categorization
3901 -- has changed.
3903 declare
3904 P : constant Entity_Id :=
3905 Cunit_Entity (Current_Sem_Unit);
3907 begin
3908 -- Check if duplication OK and if so continue
3909 -- processing.
3911 if Restriction_Active (No_Elaboration_Code)
3912 or else Restriction_Active (No_Implicit_Loops)
3913 or else Is_Preelaborated (P)
3914 or else (Ekind (P) = E_Package_Body
3915 and then
3916 Is_Preelaborated (Spec_Entity (P)))
3917 or else
3918 Is_Predefined_File_Name
3919 (Unit_File_Name (Get_Source_Unit (P)))
3920 then
3921 null;
3923 -- If duplication not OK, then we return False
3924 -- if the replication count is too high
3926 elsif Rep_Count > Max_Others_Replicate then
3927 return False;
3929 -- Continue on if duplication not OK, but the
3930 -- replication count is not excessive.
3932 else
3933 null;
3934 end if;
3935 end;
3936 end if;
3937 end loop;
3939 exit Component_Loop;
3941 -- Case of a subtype mark
3943 elsif Nkind (Choice) = N_Identifier
3944 and then Is_Type (Entity (Choice))
3945 then
3946 Lo := Type_Low_Bound (Etype (Choice));
3947 Hi := Type_High_Bound (Etype (Choice));
3949 -- Case of subtype indication
3951 elsif Nkind (Choice) = N_Subtype_Indication then
3952 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3953 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3955 -- Case of a range
3957 elsif Nkind (Choice) = N_Range then
3958 Lo := Low_Bound (Choice);
3959 Hi := High_Bound (Choice);
3961 -- Normal subexpression case
3963 else pragma Assert (Nkind (Choice) in N_Subexpr);
3964 if not Compile_Time_Known_Value (Choice) then
3965 return False;
3967 else
3968 Choice_Index := UI_To_Int (Expr_Value (Choice));
3969 if Choice_Index in Vals'Range then
3970 Vals (Choice_Index) :=
3971 New_Copy_Tree (Expression (Elmt));
3972 goto Continue;
3974 else
3975 -- Choice is statically out-of-range, will be
3976 -- rewritten to raise Constraint_Error.
3978 return False;
3979 end if;
3980 end if;
3981 end if;
3983 -- Range cases merge with Lo,Hi set
3985 if not Compile_Time_Known_Value (Lo)
3986 or else
3987 not Compile_Time_Known_Value (Hi)
3988 then
3989 return False;
3990 else
3991 for J in UI_To_Int (Expr_Value (Lo)) ..
3992 UI_To_Int (Expr_Value (Hi))
3993 loop
3994 Vals (J) := New_Copy_Tree (Expression (Elmt));
3995 end loop;
3996 end if;
3998 <<Continue>>
3999 Next (Choice);
4000 end loop Choice_Loop;
4002 Next (Elmt);
4003 end loop Component_Loop;
4005 -- If we get here the conversion is possible
4007 Vlist := New_List;
4008 for J in Vals'Range loop
4009 Append (Vals (J), Vlist);
4010 end loop;
4012 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4013 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4014 return True;
4015 end;
4016 end Flatten;
4018 -------------
4019 -- Is_Flat --
4020 -------------
4022 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4023 Elmt : Node_Id;
4025 begin
4026 if Dims = 0 then
4027 return True;
4029 elsif Nkind (N) = N_Aggregate then
4030 if Present (Component_Associations (N)) then
4031 return False;
4033 else
4034 Elmt := First (Expressions (N));
4035 while Present (Elmt) loop
4036 if not Is_Flat (Elmt, Dims - 1) then
4037 return False;
4038 end if;
4040 Next (Elmt);
4041 end loop;
4043 return True;
4044 end if;
4045 else
4046 return True;
4047 end if;
4048 end Is_Flat;
4050 -- Start of processing for Convert_To_Positional
4052 begin
4053 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4054 -- components because in this case will need to call the corresponding
4055 -- IP procedure.
4057 if Has_Default_Init_Comps (N) then
4058 return;
4059 end if;
4061 if Is_Flat (N, Number_Dimensions (Typ)) then
4062 return;
4063 end if;
4065 if Is_Bit_Packed_Array (Typ)
4066 and then not Handle_Bit_Packed
4067 then
4068 return;
4069 end if;
4071 -- Do not convert to positional if controlled components are involved
4072 -- since these require special processing
4074 if Has_Controlled_Component (Typ) then
4075 return;
4076 end if;
4078 Check_Static_Components;
4080 -- If the size is known, or all the components are static, try to
4081 -- build a fully positional aggregate.
4083 -- The size of the type may not be known for an aggregate with
4084 -- discriminated array components, but if the components are static
4085 -- it is still possible to verify statically that the length is
4086 -- compatible with the upper bound of the type, and therefore it is
4087 -- worth flattening such aggregates as well.
4089 -- For now the back-end expands these aggregates into individual
4090 -- assignments to the target anyway, but it is conceivable that
4091 -- it will eventually be able to treat such aggregates statically???
4093 if Aggr_Size_OK (N, Typ)
4094 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4095 then
4096 if Static_Components then
4097 Set_Compile_Time_Known_Aggregate (N);
4098 Set_Expansion_Delayed (N, False);
4099 end if;
4101 Analyze_And_Resolve (N, Typ);
4102 end if;
4103 end Convert_To_Positional;
4105 ----------------------------
4106 -- Expand_Array_Aggregate --
4107 ----------------------------
4109 -- Array aggregate expansion proceeds as follows:
4111 -- 1. If requested we generate code to perform all the array aggregate
4112 -- bound checks, specifically
4114 -- (a) Check that the index range defined by aggregate bounds is
4115 -- compatible with corresponding index subtype.
4117 -- (b) If an others choice is present check that no aggregate
4118 -- index is outside the bounds of the index constraint.
4120 -- (c) For multidimensional arrays make sure that all subaggregates
4121 -- corresponding to the same dimension have the same bounds.
4123 -- 2. Check for packed array aggregate which can be converted to a
4124 -- constant so that the aggregate disappeares completely.
4126 -- 3. Check case of nested aggregate. Generally nested aggregates are
4127 -- handled during the processing of the parent aggregate.
4129 -- 4. Check if the aggregate can be statically processed. If this is the
4130 -- case pass it as is to Gigi. Note that a necessary condition for
4131 -- static processing is that the aggregate be fully positional.
4133 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4134 -- a temporary) then mark the aggregate as such and return. Otherwise
4135 -- create a new temporary and generate the appropriate initialization
4136 -- code.
4138 procedure Expand_Array_Aggregate (N : Node_Id) is
4139 Loc : constant Source_Ptr := Sloc (N);
4141 Typ : constant Entity_Id := Etype (N);
4142 Ctyp : constant Entity_Id := Component_Type (Typ);
4143 -- Typ is the correct constrained array subtype of the aggregate
4144 -- Ctyp is the corresponding component type.
4146 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4147 -- Number of aggregate index dimensions
4149 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4150 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4151 -- Low and High bounds of the constraint for each aggregate index
4153 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4154 -- The type of each index
4156 Maybe_In_Place_OK : Boolean;
4157 -- If the type is neither controlled nor packed and the aggregate
4158 -- is the expression in an assignment, assignment in place may be
4159 -- possible, provided other conditions are met on the LHS.
4161 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4162 (others => False);
4163 -- If Others_Present (J) is True, then there is an others choice
4164 -- in one of the sub-aggregates of N at dimension J.
4166 procedure Build_Constrained_Type (Positional : Boolean);
4167 -- If the subtype is not static or unconstrained, build a constrained
4168 -- type using the computable sizes of the aggregate and its sub-
4169 -- aggregates.
4171 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4172 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4173 -- by Index_Bounds.
4175 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4176 -- Checks that in a multi-dimensional array aggregate all subaggregates
4177 -- corresponding to the same dimension have the same bounds.
4178 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4179 -- corresponding to the sub-aggregate.
4181 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4182 -- Computes the values of array Others_Present. Sub_Aggr is the
4183 -- array sub-aggregate we start the computation from. Dim is the
4184 -- dimension corresponding to the sub-aggregate.
4186 function In_Place_Assign_OK return Boolean;
4187 -- Simple predicate to determine whether an aggregate assignment can
4188 -- be done in place, because none of the new values can depend on the
4189 -- components of the target of the assignment.
4191 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4192 -- Checks that if an others choice is present in any sub-aggregate no
4193 -- aggregate index is outside the bounds of the index constraint.
4194 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4195 -- corresponding to the sub-aggregate.
4197 ----------------------------
4198 -- Build_Constrained_Type --
4199 ----------------------------
4201 procedure Build_Constrained_Type (Positional : Boolean) is
4202 Loc : constant Source_Ptr := Sloc (N);
4203 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4204 Comp : Node_Id;
4205 Decl : Node_Id;
4206 Typ : constant Entity_Id := Etype (N);
4207 Indices : constant List_Id := New_List;
4208 Num : Int;
4209 Sub_Agg : Node_Id;
4211 begin
4212 -- If the aggregate is purely positional, all its subaggregates
4213 -- have the same size. We collect the dimensions from the first
4214 -- subaggregate at each level.
4216 if Positional then
4217 Sub_Agg := N;
4219 for D in 1 .. Number_Dimensions (Typ) loop
4220 Sub_Agg := First (Expressions (Sub_Agg));
4222 Comp := Sub_Agg;
4223 Num := 0;
4224 while Present (Comp) loop
4225 Num := Num + 1;
4226 Next (Comp);
4227 end loop;
4229 Append_To (Indices,
4230 Make_Range (Loc,
4231 Low_Bound => Make_Integer_Literal (Loc, 1),
4232 High_Bound => Make_Integer_Literal (Loc, Num)));
4233 end loop;
4235 else
4236 -- We know the aggregate type is unconstrained and the aggregate
4237 -- is not processable by the back end, therefore not necessarily
4238 -- positional. Retrieve each dimension bounds (computed earlier).
4240 for D in 1 .. Number_Dimensions (Typ) loop
4241 Append (
4242 Make_Range (Loc,
4243 Low_Bound => Aggr_Low (D),
4244 High_Bound => Aggr_High (D)),
4245 Indices);
4246 end loop;
4247 end if;
4249 Decl :=
4250 Make_Full_Type_Declaration (Loc,
4251 Defining_Identifier => Agg_Type,
4252 Type_Definition =>
4253 Make_Constrained_Array_Definition (Loc,
4254 Discrete_Subtype_Definitions => Indices,
4255 Component_Definition =>
4256 Make_Component_Definition (Loc,
4257 Aliased_Present => False,
4258 Subtype_Indication =>
4259 New_Occurrence_Of (Component_Type (Typ), Loc))));
4261 Insert_Action (N, Decl);
4262 Analyze (Decl);
4263 Set_Etype (N, Agg_Type);
4264 Set_Is_Itype (Agg_Type);
4265 Freeze_Itype (Agg_Type, N);
4266 end Build_Constrained_Type;
4268 ------------------
4269 -- Check_Bounds --
4270 ------------------
4272 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4273 Aggr_Lo : Node_Id;
4274 Aggr_Hi : Node_Id;
4276 Ind_Lo : Node_Id;
4277 Ind_Hi : Node_Id;
4279 Cond : Node_Id := Empty;
4281 begin
4282 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4283 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4285 -- Generate the following test:
4287 -- [constraint_error when
4288 -- Aggr_Lo <= Aggr_Hi and then
4289 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4291 -- As an optimization try to see if some tests are trivially vacuous
4292 -- because we are comparing an expression against itself.
4294 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4295 Cond := Empty;
4297 elsif Aggr_Hi = Ind_Hi then
4298 Cond :=
4299 Make_Op_Lt (Loc,
4300 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4301 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4303 elsif Aggr_Lo = Ind_Lo then
4304 Cond :=
4305 Make_Op_Gt (Loc,
4306 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4307 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4309 else
4310 Cond :=
4311 Make_Or_Else (Loc,
4312 Left_Opnd =>
4313 Make_Op_Lt (Loc,
4314 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4315 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4317 Right_Opnd =>
4318 Make_Op_Gt (Loc,
4319 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4320 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4321 end if;
4323 if Present (Cond) then
4324 Cond :=
4325 Make_And_Then (Loc,
4326 Left_Opnd =>
4327 Make_Op_Le (Loc,
4328 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4329 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4331 Right_Opnd => Cond);
4333 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4334 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4335 Insert_Action (N,
4336 Make_Raise_Constraint_Error (Loc,
4337 Condition => Cond,
4338 Reason => CE_Length_Check_Failed));
4339 end if;
4340 end Check_Bounds;
4342 ----------------------------
4343 -- Check_Same_Aggr_Bounds --
4344 ----------------------------
4346 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4347 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4348 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4349 -- The bounds of this specific sub-aggregate
4351 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4352 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4353 -- The bounds of the aggregate for this dimension
4355 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4356 -- The index type for this dimension.xxx
4358 Cond : Node_Id := Empty;
4359 Assoc : Node_Id;
4360 Expr : Node_Id;
4362 begin
4363 -- If index checks are on generate the test
4365 -- [constraint_error when
4366 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4368 -- As an optimization try to see if some tests are trivially vacuos
4369 -- because we are comparing an expression against itself. Also for
4370 -- the first dimension the test is trivially vacuous because there
4371 -- is just one aggregate for dimension 1.
4373 if Index_Checks_Suppressed (Ind_Typ) then
4374 Cond := Empty;
4376 elsif Dim = 1
4377 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4378 then
4379 Cond := Empty;
4381 elsif Aggr_Hi = Sub_Hi then
4382 Cond :=
4383 Make_Op_Ne (Loc,
4384 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4385 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4387 elsif Aggr_Lo = Sub_Lo then
4388 Cond :=
4389 Make_Op_Ne (Loc,
4390 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4391 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4393 else
4394 Cond :=
4395 Make_Or_Else (Loc,
4396 Left_Opnd =>
4397 Make_Op_Ne (Loc,
4398 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4399 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4401 Right_Opnd =>
4402 Make_Op_Ne (Loc,
4403 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4404 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4405 end if;
4407 if Present (Cond) then
4408 Insert_Action (N,
4409 Make_Raise_Constraint_Error (Loc,
4410 Condition => Cond,
4411 Reason => CE_Length_Check_Failed));
4412 end if;
4414 -- Now look inside the sub-aggregate to see if there is more work
4416 if Dim < Aggr_Dimension then
4418 -- Process positional components
4420 if Present (Expressions (Sub_Aggr)) then
4421 Expr := First (Expressions (Sub_Aggr));
4422 while Present (Expr) loop
4423 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4424 Next (Expr);
4425 end loop;
4426 end if;
4428 -- Process component associations
4430 if Present (Component_Associations (Sub_Aggr)) then
4431 Assoc := First (Component_Associations (Sub_Aggr));
4432 while Present (Assoc) loop
4433 Expr := Expression (Assoc);
4434 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4435 Next (Assoc);
4436 end loop;
4437 end if;
4438 end if;
4439 end Check_Same_Aggr_Bounds;
4441 ----------------------------
4442 -- Compute_Others_Present --
4443 ----------------------------
4445 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4446 Assoc : Node_Id;
4447 Expr : Node_Id;
4449 begin
4450 if Present (Component_Associations (Sub_Aggr)) then
4451 Assoc := Last (Component_Associations (Sub_Aggr));
4453 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4454 Others_Present (Dim) := True;
4455 end if;
4456 end if;
4458 -- Now look inside the sub-aggregate to see if there is more work
4460 if Dim < Aggr_Dimension then
4462 -- Process positional components
4464 if Present (Expressions (Sub_Aggr)) then
4465 Expr := First (Expressions (Sub_Aggr));
4466 while Present (Expr) loop
4467 Compute_Others_Present (Expr, Dim + 1);
4468 Next (Expr);
4469 end loop;
4470 end if;
4472 -- Process component associations
4474 if Present (Component_Associations (Sub_Aggr)) then
4475 Assoc := First (Component_Associations (Sub_Aggr));
4476 while Present (Assoc) loop
4477 Expr := Expression (Assoc);
4478 Compute_Others_Present (Expr, Dim + 1);
4479 Next (Assoc);
4480 end loop;
4481 end if;
4482 end if;
4483 end Compute_Others_Present;
4485 ------------------------
4486 -- In_Place_Assign_OK --
4487 ------------------------
4489 function In_Place_Assign_OK return Boolean is
4490 Aggr_In : Node_Id;
4491 Aggr_Lo : Node_Id;
4492 Aggr_Hi : Node_Id;
4493 Obj_In : Node_Id;
4494 Obj_Lo : Node_Id;
4495 Obj_Hi : Node_Id;
4497 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
4498 -- Aggregates that consist of a single Others choice are safe
4499 -- if the single expression is.
4501 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4502 -- Check recursively that each component of a (sub)aggregate does
4503 -- not depend on the variable being assigned to.
4505 function Safe_Component (Expr : Node_Id) return Boolean;
4506 -- Verify that an expression cannot depend on the variable being
4507 -- assigned to. Room for improvement here (but less than before).
4509 -------------------------
4510 -- Is_Others_Aggregate --
4511 -------------------------
4513 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
4514 begin
4515 return No (Expressions (Aggr))
4516 and then Nkind
4517 (First (Choices (First (Component_Associations (Aggr)))))
4518 = N_Others_Choice;
4519 end Is_Others_Aggregate;
4521 --------------------
4522 -- Safe_Aggregate --
4523 --------------------
4525 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4526 Expr : Node_Id;
4528 begin
4529 if Present (Expressions (Aggr)) then
4530 Expr := First (Expressions (Aggr));
4531 while Present (Expr) loop
4532 if Nkind (Expr) = N_Aggregate then
4533 if not Safe_Aggregate (Expr) then
4534 return False;
4535 end if;
4537 elsif not Safe_Component (Expr) then
4538 return False;
4539 end if;
4541 Next (Expr);
4542 end loop;
4543 end if;
4545 if Present (Component_Associations (Aggr)) then
4546 Expr := First (Component_Associations (Aggr));
4547 while Present (Expr) loop
4548 if Nkind (Expression (Expr)) = N_Aggregate then
4549 if not Safe_Aggregate (Expression (Expr)) then
4550 return False;
4551 end if;
4553 elsif not Safe_Component (Expression (Expr)) then
4554 return False;
4555 end if;
4557 Next (Expr);
4558 end loop;
4559 end if;
4561 return True;
4562 end Safe_Aggregate;
4564 --------------------
4565 -- Safe_Component --
4566 --------------------
4568 function Safe_Component (Expr : Node_Id) return Boolean is
4569 Comp : Node_Id := Expr;
4571 function Check_Component (Comp : Node_Id) return Boolean;
4572 -- Do the recursive traversal, after copy
4574 ---------------------
4575 -- Check_Component --
4576 ---------------------
4578 function Check_Component (Comp : Node_Id) return Boolean is
4579 begin
4580 if Is_Overloaded (Comp) then
4581 return False;
4582 end if;
4584 return Compile_Time_Known_Value (Comp)
4586 or else (Is_Entity_Name (Comp)
4587 and then Present (Entity (Comp))
4588 and then No (Renamed_Object (Entity (Comp))))
4590 or else (Nkind (Comp) = N_Attribute_Reference
4591 and then Check_Component (Prefix (Comp)))
4593 or else (Nkind (Comp) in N_Binary_Op
4594 and then Check_Component (Left_Opnd (Comp))
4595 and then Check_Component (Right_Opnd (Comp)))
4597 or else (Nkind (Comp) in N_Unary_Op
4598 and then Check_Component (Right_Opnd (Comp)))
4600 or else (Nkind (Comp) = N_Selected_Component
4601 and then Check_Component (Prefix (Comp)))
4603 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4604 and then Check_Component (Expression (Comp)));
4605 end Check_Component;
4607 -- Start of processing for Safe_Component
4609 begin
4610 -- If the component appears in an association that may
4611 -- correspond to more than one element, it is not analyzed
4612 -- before the expansion into assignments, to avoid side effects.
4613 -- We analyze, but do not resolve the copy, to obtain sufficient
4614 -- entity information for the checks that follow. If component is
4615 -- overloaded we assume an unsafe function call.
4617 if not Analyzed (Comp) then
4618 if Is_Overloaded (Expr) then
4619 return False;
4621 elsif Nkind (Expr) = N_Aggregate
4622 and then not Is_Others_Aggregate (Expr)
4623 then
4624 return False;
4626 elsif Nkind (Expr) = N_Allocator then
4628 -- For now, too complex to analyze
4630 return False;
4631 end if;
4633 Comp := New_Copy_Tree (Expr);
4634 Set_Parent (Comp, Parent (Expr));
4635 Analyze (Comp);
4636 end if;
4638 if Nkind (Comp) = N_Aggregate then
4639 return Safe_Aggregate (Comp);
4640 else
4641 return Check_Component (Comp);
4642 end if;
4643 end Safe_Component;
4645 -- Start of processing for In_Place_Assign_OK
4647 begin
4648 if Present (Component_Associations (N)) then
4650 -- On assignment, sliding can take place, so we cannot do the
4651 -- assignment in place unless the bounds of the aggregate are
4652 -- statically equal to those of the target.
4654 -- If the aggregate is given by an others choice, the bounds
4655 -- are derived from the left-hand side, and the assignment is
4656 -- safe if the expression is.
4658 if Is_Others_Aggregate (N) then
4659 return
4660 Safe_Component
4661 (Expression (First (Component_Associations (N))));
4662 end if;
4664 Aggr_In := First_Index (Etype (N));
4666 if Nkind (Parent (N)) = N_Assignment_Statement then
4667 Obj_In := First_Index (Etype (Name (Parent (N))));
4669 else
4670 -- Context is an allocator. Check bounds of aggregate
4671 -- against given type in qualified expression.
4673 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4674 Obj_In :=
4675 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4676 end if;
4678 while Present (Aggr_In) loop
4679 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4680 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4682 if not Compile_Time_Known_Value (Aggr_Lo)
4683 or else not Compile_Time_Known_Value (Aggr_Hi)
4684 or else not Compile_Time_Known_Value (Obj_Lo)
4685 or else not Compile_Time_Known_Value (Obj_Hi)
4686 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4687 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4688 then
4689 return False;
4690 end if;
4692 Next_Index (Aggr_In);
4693 Next_Index (Obj_In);
4694 end loop;
4695 end if;
4697 -- Now check the component values themselves
4699 return Safe_Aggregate (N);
4700 end In_Place_Assign_OK;
4702 ------------------
4703 -- Others_Check --
4704 ------------------
4706 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4707 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4708 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4709 -- The bounds of the aggregate for this dimension
4711 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4712 -- The index type for this dimension
4714 Need_To_Check : Boolean := False;
4716 Choices_Lo : Node_Id := Empty;
4717 Choices_Hi : Node_Id := Empty;
4718 -- The lowest and highest discrete choices for a named sub-aggregate
4720 Nb_Choices : Int := -1;
4721 -- The number of discrete non-others choices in this sub-aggregate
4723 Nb_Elements : Uint := Uint_0;
4724 -- The number of elements in a positional aggregate
4726 Cond : Node_Id := Empty;
4728 Assoc : Node_Id;
4729 Choice : Node_Id;
4730 Expr : Node_Id;
4732 begin
4733 -- Check if we have an others choice. If we do make sure that this
4734 -- sub-aggregate contains at least one element in addition to the
4735 -- others choice.
4737 if Range_Checks_Suppressed (Ind_Typ) then
4738 Need_To_Check := False;
4740 elsif Present (Expressions (Sub_Aggr))
4741 and then Present (Component_Associations (Sub_Aggr))
4742 then
4743 Need_To_Check := True;
4745 elsif Present (Component_Associations (Sub_Aggr)) then
4746 Assoc := Last (Component_Associations (Sub_Aggr));
4748 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4749 Need_To_Check := False;
4751 else
4752 -- Count the number of discrete choices. Start with -1 because
4753 -- the others choice does not count.
4755 Nb_Choices := -1;
4756 Assoc := First (Component_Associations (Sub_Aggr));
4757 while Present (Assoc) loop
4758 Choice := First (Choices (Assoc));
4759 while Present (Choice) loop
4760 Nb_Choices := Nb_Choices + 1;
4761 Next (Choice);
4762 end loop;
4764 Next (Assoc);
4765 end loop;
4767 -- If there is only an others choice nothing to do
4769 Need_To_Check := (Nb_Choices > 0);
4770 end if;
4772 else
4773 Need_To_Check := False;
4774 end if;
4776 -- If we are dealing with a positional sub-aggregate with an others
4777 -- choice then compute the number or positional elements.
4779 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4780 Expr := First (Expressions (Sub_Aggr));
4781 Nb_Elements := Uint_0;
4782 while Present (Expr) loop
4783 Nb_Elements := Nb_Elements + 1;
4784 Next (Expr);
4785 end loop;
4787 -- If the aggregate contains discrete choices and an others choice
4788 -- compute the smallest and largest discrete choice values.
4790 elsif Need_To_Check then
4791 Compute_Choices_Lo_And_Choices_Hi : declare
4793 Table : Case_Table_Type (1 .. Nb_Choices);
4794 -- Used to sort all the different choice values
4796 J : Pos := 1;
4797 Low : Node_Id;
4798 High : Node_Id;
4800 begin
4801 Assoc := First (Component_Associations (Sub_Aggr));
4802 while Present (Assoc) loop
4803 Choice := First (Choices (Assoc));
4804 while Present (Choice) loop
4805 if Nkind (Choice) = N_Others_Choice then
4806 exit;
4807 end if;
4809 Get_Index_Bounds (Choice, Low, High);
4810 Table (J).Choice_Lo := Low;
4811 Table (J).Choice_Hi := High;
4813 J := J + 1;
4814 Next (Choice);
4815 end loop;
4817 Next (Assoc);
4818 end loop;
4820 -- Sort the discrete choices
4822 Sort_Case_Table (Table);
4824 Choices_Lo := Table (1).Choice_Lo;
4825 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4826 end Compute_Choices_Lo_And_Choices_Hi;
4827 end if;
4829 -- If no others choice in this sub-aggregate, or the aggregate
4830 -- comprises only an others choice, nothing to do.
4832 if not Need_To_Check then
4833 Cond := Empty;
4835 -- If we are dealing with an aggregate containing an others choice
4836 -- and positional components, we generate the following test:
4838 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4839 -- Ind_Typ'Pos (Aggr_Hi)
4840 -- then
4841 -- raise Constraint_Error;
4842 -- end if;
4844 elsif Nb_Elements > Uint_0 then
4845 Cond :=
4846 Make_Op_Gt (Loc,
4847 Left_Opnd =>
4848 Make_Op_Add (Loc,
4849 Left_Opnd =>
4850 Make_Attribute_Reference (Loc,
4851 Prefix => New_Reference_To (Ind_Typ, Loc),
4852 Attribute_Name => Name_Pos,
4853 Expressions =>
4854 New_List
4855 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4856 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4858 Right_Opnd =>
4859 Make_Attribute_Reference (Loc,
4860 Prefix => New_Reference_To (Ind_Typ, Loc),
4861 Attribute_Name => Name_Pos,
4862 Expressions => New_List (
4863 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4865 -- If we are dealing with an aggregate containing an others choice
4866 -- and discrete choices we generate the following test:
4868 -- [constraint_error when
4869 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4871 else
4872 Cond :=
4873 Make_Or_Else (Loc,
4874 Left_Opnd =>
4875 Make_Op_Lt (Loc,
4876 Left_Opnd =>
4877 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4878 Right_Opnd =>
4879 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4881 Right_Opnd =>
4882 Make_Op_Gt (Loc,
4883 Left_Opnd =>
4884 Duplicate_Subexpr (Choices_Hi),
4885 Right_Opnd =>
4886 Duplicate_Subexpr (Aggr_Hi)));
4887 end if;
4889 if Present (Cond) then
4890 Insert_Action (N,
4891 Make_Raise_Constraint_Error (Loc,
4892 Condition => Cond,
4893 Reason => CE_Length_Check_Failed));
4894 -- Questionable reason code, shouldn't that be a
4895 -- CE_Range_Check_Failed ???
4896 end if;
4898 -- Now look inside the sub-aggregate to see if there is more work
4900 if Dim < Aggr_Dimension then
4902 -- Process positional components
4904 if Present (Expressions (Sub_Aggr)) then
4905 Expr := First (Expressions (Sub_Aggr));
4906 while Present (Expr) loop
4907 Others_Check (Expr, Dim + 1);
4908 Next (Expr);
4909 end loop;
4910 end if;
4912 -- Process component associations
4914 if Present (Component_Associations (Sub_Aggr)) then
4915 Assoc := First (Component_Associations (Sub_Aggr));
4916 while Present (Assoc) loop
4917 Expr := Expression (Assoc);
4918 Others_Check (Expr, Dim + 1);
4919 Next (Assoc);
4920 end loop;
4921 end if;
4922 end if;
4923 end Others_Check;
4925 -- Remaining Expand_Array_Aggregate variables
4927 Tmp : Entity_Id;
4928 -- Holds the temporary aggregate value
4930 Tmp_Decl : Node_Id;
4931 -- Holds the declaration of Tmp
4933 Aggr_Code : List_Id;
4934 Parent_Node : Node_Id;
4935 Parent_Kind : Node_Kind;
4937 -- Start of processing for Expand_Array_Aggregate
4939 begin
4940 -- Do not touch the special aggregates of attributes used for Asm calls
4942 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4943 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4944 then
4945 return;
4946 end if;
4948 -- If the semantic analyzer has determined that aggregate N will raise
4949 -- Constraint_Error at run time, then the aggregate node has been
4950 -- replaced with an N_Raise_Constraint_Error node and we should
4951 -- never get here.
4953 pragma Assert (not Raises_Constraint_Error (N));
4955 -- STEP 1a
4957 -- Check that the index range defined by aggregate bounds is
4958 -- compatible with corresponding index subtype.
4960 Index_Compatibility_Check : declare
4961 Aggr_Index_Range : Node_Id := First_Index (Typ);
4962 -- The current aggregate index range
4964 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4965 -- The corresponding index constraint against which we have to
4966 -- check the above aggregate index range.
4968 begin
4969 Compute_Others_Present (N, 1);
4971 for J in 1 .. Aggr_Dimension loop
4972 -- There is no need to emit a check if an others choice is
4973 -- present for this array aggregate dimension since in this
4974 -- case one of N's sub-aggregates has taken its bounds from the
4975 -- context and these bounds must have been checked already. In
4976 -- addition all sub-aggregates corresponding to the same
4977 -- dimension must all have the same bounds (checked in (c) below).
4979 if not Range_Checks_Suppressed (Etype (Index_Constraint))
4980 and then not Others_Present (J)
4981 then
4982 -- We don't use Checks.Apply_Range_Check here because it emits
4983 -- a spurious check. Namely it checks that the range defined by
4984 -- the aggregate bounds is non empty. But we know this already
4985 -- if we get here.
4987 Check_Bounds (Aggr_Index_Range, Index_Constraint);
4988 end if;
4990 -- Save the low and high bounds of the aggregate index as well as
4991 -- the index type for later use in checks (b) and (c) below.
4993 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
4994 Aggr_High (J) := High_Bound (Aggr_Index_Range);
4996 Aggr_Index_Typ (J) := Etype (Index_Constraint);
4998 Next_Index (Aggr_Index_Range);
4999 Next_Index (Index_Constraint);
5000 end loop;
5001 end Index_Compatibility_Check;
5003 -- STEP 1b
5005 -- If an others choice is present check that no aggregate index is
5006 -- outside the bounds of the index constraint.
5008 Others_Check (N, 1);
5010 -- STEP 1c
5012 -- For multidimensional arrays make sure that all subaggregates
5013 -- corresponding to the same dimension have the same bounds.
5015 if Aggr_Dimension > 1 then
5016 Check_Same_Aggr_Bounds (N, 1);
5017 end if;
5019 -- STEP 2
5021 -- Here we test for is packed array aggregate that we can handle at
5022 -- compile time. If so, return with transformation done. Note that we do
5023 -- this even if the aggregate is nested, because once we have done this
5024 -- processing, there is no more nested aggregate!
5026 if Packed_Array_Aggregate_Handled (N) then
5027 return;
5028 end if;
5030 -- At this point we try to convert to positional form
5032 if Ekind (Current_Scope) = E_Package
5033 and then Static_Elaboration_Desired (Current_Scope)
5034 then
5035 Convert_To_Positional (N, Max_Others_Replicate => 100);
5037 else
5038 Convert_To_Positional (N);
5039 end if;
5041 -- if the result is no longer an aggregate (e.g. it may be a string
5042 -- literal, or a temporary which has the needed value), then we are
5043 -- done, since there is no longer a nested aggregate.
5045 if Nkind (N) /= N_Aggregate then
5046 return;
5048 -- We are also done if the result is an analyzed aggregate
5049 -- This case could use more comments ???
5051 elsif Analyzed (N)
5052 and then N /= Original_Node (N)
5053 then
5054 return;
5055 end if;
5057 -- If all aggregate components are compile-time known and the aggregate
5058 -- has been flattened, nothing left to do. The same occurs if the
5059 -- aggregate is used to initialize the components of an statically
5060 -- allocated dispatch table.
5062 if Compile_Time_Known_Aggregate (N)
5063 or else Is_Static_Dispatch_Table_Aggregate (N)
5064 then
5065 Set_Expansion_Delayed (N, False);
5066 return;
5067 end if;
5069 -- Now see if back end processing is possible
5071 if Backend_Processing_Possible (N) then
5073 -- If the aggregate is static but the constraints are not, build
5074 -- a static subtype for the aggregate, so that Gigi can place it
5075 -- in static memory. Perform an unchecked_conversion to the non-
5076 -- static type imposed by the context.
5078 declare
5079 Itype : constant Entity_Id := Etype (N);
5080 Index : Node_Id;
5081 Needs_Type : Boolean := False;
5083 begin
5084 Index := First_Index (Itype);
5085 while Present (Index) loop
5086 if not Is_Static_Subtype (Etype (Index)) then
5087 Needs_Type := True;
5088 exit;
5089 else
5090 Next_Index (Index);
5091 end if;
5092 end loop;
5094 if Needs_Type then
5095 Build_Constrained_Type (Positional => True);
5096 Rewrite (N, Unchecked_Convert_To (Itype, N));
5097 Analyze (N);
5098 end if;
5099 end;
5101 return;
5102 end if;
5104 -- STEP 3
5106 -- Delay expansion for nested aggregates: it will be taken care of
5107 -- when the parent aggregate is expanded.
5109 Parent_Node := Parent (N);
5110 Parent_Kind := Nkind (Parent_Node);
5112 if Parent_Kind = N_Qualified_Expression then
5113 Parent_Node := Parent (Parent_Node);
5114 Parent_Kind := Nkind (Parent_Node);
5115 end if;
5117 if Parent_Kind = N_Aggregate
5118 or else Parent_Kind = N_Extension_Aggregate
5119 or else Parent_Kind = N_Component_Association
5120 or else (Parent_Kind = N_Object_Declaration
5121 and then Needs_Finalization (Typ))
5122 or else (Parent_Kind = N_Assignment_Statement
5123 and then Inside_Init_Proc)
5124 then
5125 if Static_Array_Aggregate (N)
5126 or else Compile_Time_Known_Aggregate (N)
5127 then
5128 Set_Expansion_Delayed (N, False);
5129 return;
5130 else
5131 Set_Expansion_Delayed (N);
5132 return;
5133 end if;
5134 end if;
5136 -- STEP 4
5138 -- Look if in place aggregate expansion is possible
5140 -- For object declarations we build the aggregate in place, unless
5141 -- the array is bit-packed or the component is controlled.
5143 -- For assignments we do the assignment in place if all the component
5144 -- associations have compile-time known values. For other cases we
5145 -- create a temporary. The analysis for safety of on-line assignment
5146 -- is delicate, i.e. we don't know how to do it fully yet ???
5148 -- For allocators we assign to the designated object in place if the
5149 -- aggregate meets the same conditions as other in-place assignments.
5150 -- In this case the aggregate may not come from source but was created
5151 -- for default initialization, e.g. with Initialize_Scalars.
5153 if Requires_Transient_Scope (Typ) then
5154 Establish_Transient_Scope
5155 (N, Sec_Stack => Has_Controlled_Component (Typ));
5156 end if;
5158 if Has_Default_Init_Comps (N) then
5159 Maybe_In_Place_OK := False;
5161 elsif Is_Bit_Packed_Array (Typ)
5162 or else Has_Controlled_Component (Typ)
5163 then
5164 Maybe_In_Place_OK := False;
5166 else
5167 Maybe_In_Place_OK :=
5168 (Nkind (Parent (N)) = N_Assignment_Statement
5169 and then Comes_From_Source (N)
5170 and then In_Place_Assign_OK)
5172 or else
5173 (Nkind (Parent (Parent (N))) = N_Allocator
5174 and then In_Place_Assign_OK);
5175 end if;
5177 -- If this is an array of tasks, it will be expanded into build-in-place
5178 -- assignments. Build an activation chain for the tasks now.
5180 if Has_Task (Etype (N)) then
5181 Build_Activation_Chain_Entity (N);
5182 end if;
5184 -- Should document these individual tests ???
5186 if not Has_Default_Init_Comps (N)
5187 and then Comes_From_Source (Parent (N))
5188 and then Nkind (Parent (N)) = N_Object_Declaration
5189 and then not
5190 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
5191 and then N = Expression (Parent (N))
5192 and then not Is_Bit_Packed_Array (Typ)
5193 and then not Has_Controlled_Component (Typ)
5195 -- If the aggregate is the expression in an object declaration, it
5196 -- cannot be expanded in place. Lookahead in the current declarative
5197 -- part to find an address clause for the object being declared. If
5198 -- one is present, we cannot build in place. Unclear comment???
5200 and then not Has_Following_Address_Clause (Parent (N))
5201 then
5202 Tmp := Defining_Identifier (Parent (N));
5203 Set_No_Initialization (Parent (N));
5204 Set_Expression (Parent (N), Empty);
5206 -- Set the type of the entity, for use in the analysis of the
5207 -- subsequent indexed assignments. If the nominal type is not
5208 -- constrained, build a subtype from the known bounds of the
5209 -- aggregate. If the declaration has a subtype mark, use it,
5210 -- otherwise use the itype of the aggregate.
5212 if not Is_Constrained (Typ) then
5213 Build_Constrained_Type (Positional => False);
5214 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5215 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5216 then
5217 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5218 else
5219 Set_Size_Known_At_Compile_Time (Typ, False);
5220 Set_Etype (Tmp, Typ);
5221 end if;
5223 elsif Maybe_In_Place_OK
5224 and then Nkind (Parent (N)) = N_Qualified_Expression
5225 and then Nkind (Parent (Parent (N))) = N_Allocator
5226 then
5227 Set_Expansion_Delayed (N);
5228 return;
5230 -- In the remaining cases the aggregate is the RHS of an assignment
5232 elsif Maybe_In_Place_OK
5233 and then Is_Entity_Name (Name (Parent (N)))
5234 then
5235 Tmp := Entity (Name (Parent (N)));
5237 if Etype (Tmp) /= Etype (N) then
5238 Apply_Length_Check (N, Etype (Tmp));
5240 if Nkind (N) = N_Raise_Constraint_Error then
5242 -- Static error, nothing further to expand
5244 return;
5245 end if;
5246 end if;
5248 elsif Maybe_In_Place_OK
5249 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
5250 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5251 then
5252 Tmp := Name (Parent (N));
5254 if Etype (Tmp) /= Etype (N) then
5255 Apply_Length_Check (N, Etype (Tmp));
5256 end if;
5258 elsif Maybe_In_Place_OK
5259 and then Nkind (Name (Parent (N))) = N_Slice
5260 and then Safe_Slice_Assignment (N)
5261 then
5262 -- Safe_Slice_Assignment rewrites assignment as a loop
5264 return;
5266 -- Step 5
5268 -- In place aggregate expansion is not possible
5270 else
5271 Maybe_In_Place_OK := False;
5272 Tmp := Make_Temporary (Loc, 'A', N);
5273 Tmp_Decl :=
5274 Make_Object_Declaration
5275 (Loc,
5276 Defining_Identifier => Tmp,
5277 Object_Definition => New_Occurrence_Of (Typ, Loc));
5278 Set_No_Initialization (Tmp_Decl, True);
5280 -- If we are within a loop, the temporary will be pushed on the
5281 -- stack at each iteration. If the aggregate is the expression for an
5282 -- allocator, it will be immediately copied to the heap and can
5283 -- be reclaimed at once. We create a transient scope around the
5284 -- aggregate for this purpose.
5286 if Ekind (Current_Scope) = E_Loop
5287 and then Nkind (Parent (Parent (N))) = N_Allocator
5288 then
5289 Establish_Transient_Scope (N, False);
5290 end if;
5292 Insert_Action (N, Tmp_Decl);
5293 end if;
5295 -- Construct and insert the aggregate code. We can safely suppress index
5296 -- checks because this code is guaranteed not to raise CE on index
5297 -- checks. However we should *not* suppress all checks.
5299 declare
5300 Target : Node_Id;
5302 begin
5303 if Nkind (Tmp) = N_Defining_Identifier then
5304 Target := New_Reference_To (Tmp, Loc);
5306 else
5308 if Has_Default_Init_Comps (N) then
5310 -- Ada 2005 (AI-287): This case has not been analyzed???
5312 raise Program_Error;
5313 end if;
5315 -- Name in assignment is explicit dereference
5317 Target := New_Copy (Tmp);
5318 end if;
5320 Aggr_Code :=
5321 Build_Array_Aggr_Code (N,
5322 Ctype => Ctyp,
5323 Index => First_Index (Typ),
5324 Into => Target,
5325 Scalar_Comp => Is_Scalar_Type (Ctyp));
5326 end;
5328 if Comes_From_Source (Tmp) then
5329 Insert_Actions_After (Parent (N), Aggr_Code);
5331 else
5332 Insert_Actions (N, Aggr_Code);
5333 end if;
5335 -- If the aggregate has been assigned in place, remove the original
5336 -- assignment.
5338 if Nkind (Parent (N)) = N_Assignment_Statement
5339 and then Maybe_In_Place_OK
5340 then
5341 Rewrite (Parent (N), Make_Null_Statement (Loc));
5343 elsif Nkind (Parent (N)) /= N_Object_Declaration
5344 or else Tmp /= Defining_Identifier (Parent (N))
5345 then
5346 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5347 Analyze_And_Resolve (N, Typ);
5348 end if;
5349 end Expand_Array_Aggregate;
5351 ------------------------
5352 -- Expand_N_Aggregate --
5353 ------------------------
5355 procedure Expand_N_Aggregate (N : Node_Id) is
5356 begin
5357 if Is_Record_Type (Etype (N)) then
5358 Expand_Record_Aggregate (N);
5359 else
5360 Expand_Array_Aggregate (N);
5361 end if;
5362 exception
5363 when RE_Not_Available =>
5364 return;
5365 end Expand_N_Aggregate;
5367 ----------------------------------
5368 -- Expand_N_Extension_Aggregate --
5369 ----------------------------------
5371 -- If the ancestor part is an expression, add a component association for
5372 -- the parent field. If the type of the ancestor part is not the direct
5373 -- parent of the expected type, build recursively the needed ancestors.
5374 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5375 -- ration for a temporary of the expected type, followed by individual
5376 -- assignments to the given components.
5378 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5379 Loc : constant Source_Ptr := Sloc (N);
5380 A : constant Node_Id := Ancestor_Part (N);
5381 Typ : constant Entity_Id := Etype (N);
5383 begin
5384 -- If the ancestor is a subtype mark, an init proc must be called
5385 -- on the resulting object which thus has to be materialized in
5386 -- the front-end
5388 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5389 Convert_To_Assignments (N, Typ);
5391 -- The extension aggregate is transformed into a record aggregate
5392 -- of the following form (c1 and c2 are inherited components)
5394 -- (Exp with c3 => a, c4 => b)
5395 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c1 => a, c2 => b)
5397 else
5398 Set_Etype (N, Typ);
5400 if Tagged_Type_Expansion then
5401 Expand_Record_Aggregate (N,
5402 Orig_Tag =>
5403 New_Occurrence_Of
5404 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5405 Parent_Expr => A);
5406 else
5407 -- No tag is needed in the case of a VM
5408 Expand_Record_Aggregate (N,
5409 Parent_Expr => A);
5410 end if;
5411 end if;
5413 exception
5414 when RE_Not_Available =>
5415 return;
5416 end Expand_N_Extension_Aggregate;
5418 -----------------------------
5419 -- Expand_Record_Aggregate --
5420 -----------------------------
5422 procedure Expand_Record_Aggregate
5423 (N : Node_Id;
5424 Orig_Tag : Node_Id := Empty;
5425 Parent_Expr : Node_Id := Empty)
5427 Loc : constant Source_Ptr := Sloc (N);
5428 Comps : constant List_Id := Component_Associations (N);
5429 Typ : constant Entity_Id := Etype (N);
5430 Base_Typ : constant Entity_Id := Base_Type (Typ);
5432 Static_Components : Boolean := True;
5433 -- Flag to indicate whether all components are compile-time known,
5434 -- and the aggregate can be constructed statically and handled by
5435 -- the back-end.
5437 function Component_Not_OK_For_Backend return Boolean;
5438 -- Check for presence of component which makes it impossible for the
5439 -- backend to process the aggregate, thus requiring the use of a series
5440 -- of assignment statements. Cases checked for are a nested aggregate
5441 -- needing Late_Expansion, the presence of a tagged component which may
5442 -- need tag adjustment, and a bit unaligned component reference.
5444 -- We also force expansion into assignments if a component is of a
5445 -- mutable type (including a private type with discriminants) because
5446 -- in that case the size of the component to be copied may be smaller
5447 -- than the side of the target, and there is no simple way for gigi
5448 -- to compute the size of the object to be copied.
5450 -- NOTE: This is part of the ongoing work to define precisely the
5451 -- interface between front-end and back-end handling of aggregates.
5452 -- In general it is desirable to pass aggregates as they are to gigi,
5453 -- in order to minimize elaboration code. This is one case where the
5454 -- semantics of Ada complicate the analysis and lead to anomalies in
5455 -- the gcc back-end if the aggregate is not expanded into assignments.
5457 ----------------------------------
5458 -- Component_Not_OK_For_Backend --
5459 ----------------------------------
5461 function Component_Not_OK_For_Backend return Boolean is
5462 C : Node_Id;
5463 Expr_Q : Node_Id;
5465 begin
5466 if No (Comps) then
5467 return False;
5468 end if;
5470 C := First (Comps);
5471 while Present (C) loop
5472 if Nkind (Expression (C)) = N_Qualified_Expression then
5473 Expr_Q := Expression (Expression (C));
5474 else
5475 Expr_Q := Expression (C);
5476 end if;
5478 -- Return true if the aggregate has any associations for tagged
5479 -- components that may require tag adjustment.
5481 -- These are cases where the source expression may have a tag that
5482 -- could differ from the component tag (e.g., can occur for type
5483 -- conversions and formal parameters). (Tag adjustment not needed
5484 -- if VM_Target because object tags are implicit in the machine.)
5486 if Is_Tagged_Type (Etype (Expr_Q))
5487 and then (Nkind (Expr_Q) = N_Type_Conversion
5488 or else (Is_Entity_Name (Expr_Q)
5489 and then
5490 Ekind (Entity (Expr_Q)) in Formal_Kind))
5491 and then Tagged_Type_Expansion
5492 then
5493 Static_Components := False;
5494 return True;
5496 elsif Is_Delayed_Aggregate (Expr_Q) then
5497 Static_Components := False;
5498 return True;
5500 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5501 Static_Components := False;
5502 return True;
5503 end if;
5505 if Is_Scalar_Type (Etype (Expr_Q)) then
5506 if not Compile_Time_Known_Value (Expr_Q) then
5507 Static_Components := False;
5508 end if;
5510 elsif Nkind (Expr_Q) /= N_Aggregate
5511 or else not Compile_Time_Known_Aggregate (Expr_Q)
5512 then
5513 Static_Components := False;
5515 if Is_Private_Type (Etype (Expr_Q))
5516 and then Has_Discriminants (Etype (Expr_Q))
5517 then
5518 return True;
5519 end if;
5520 end if;
5522 Next (C);
5523 end loop;
5525 return False;
5526 end Component_Not_OK_For_Backend;
5528 -- Remaining Expand_Record_Aggregate variables
5530 Tag_Value : Node_Id;
5531 Comp : Entity_Id;
5532 New_Comp : Node_Id;
5534 -- Start of processing for Expand_Record_Aggregate
5536 begin
5537 -- If the aggregate is to be assigned to an atomic variable, we
5538 -- have to prevent a piecemeal assignment even if the aggregate
5539 -- is to be expanded. We create a temporary for the aggregate, and
5540 -- assign the temporary instead, so that the back end can generate
5541 -- an atomic move for it.
5543 if Is_Atomic (Typ)
5544 and then Comes_From_Source (Parent (N))
5545 and then Is_Atomic_Aggregate (N, Typ)
5546 then
5547 return;
5549 -- No special management required for aggregates used to initialize
5550 -- statically allocated dispatch tables
5552 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5553 return;
5554 end if;
5556 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5557 -- are build-in-place function calls. This test could be more specific,
5558 -- but doing it for all inherently limited aggregates seems harmless.
5559 -- The assignments will turn into build-in-place function calls (see
5560 -- Make_Build_In_Place_Call_In_Assignment).
5562 if Ada_Version >= Ada_05 and then Is_Inherently_Limited_Type (Typ) then
5563 Convert_To_Assignments (N, Typ);
5565 -- Gigi doesn't handle properly temporaries of variable size
5566 -- so we generate it in the front-end
5568 elsif not Size_Known_At_Compile_Time (Typ) then
5569 Convert_To_Assignments (N, Typ);
5571 -- Temporaries for controlled aggregates need to be attached to a
5572 -- final chain in order to be properly finalized, so it has to
5573 -- be created in the front-end
5575 elsif Is_Controlled (Typ)
5576 or else Has_Controlled_Component (Base_Type (Typ))
5577 then
5578 Convert_To_Assignments (N, Typ);
5580 -- Ada 2005 (AI-287): In case of default initialized components we
5581 -- convert the aggregate into assignments.
5583 elsif Has_Default_Init_Comps (N) then
5584 Convert_To_Assignments (N, Typ);
5586 -- Check components
5588 elsif Component_Not_OK_For_Backend then
5589 Convert_To_Assignments (N, Typ);
5591 -- If an ancestor is private, some components are not inherited and
5592 -- we cannot expand into a record aggregate
5594 elsif Has_Private_Ancestor (Typ) then
5595 Convert_To_Assignments (N, Typ);
5597 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5598 -- is not able to handle the aggregate for Late_Request.
5600 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5601 Convert_To_Assignments (N, Typ);
5603 -- If the tagged types covers interface types we need to initialize all
5604 -- hidden components containing pointers to secondary dispatch tables.
5606 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
5607 Convert_To_Assignments (N, Typ);
5609 -- If some components are mutable, the size of the aggregate component
5610 -- may be distinct from the default size of the type component, so
5611 -- we need to expand to insure that the back-end copies the proper
5612 -- size of the data.
5614 elsif Has_Mutable_Components (Typ) then
5615 Convert_To_Assignments (N, Typ);
5617 -- If the type involved has any non-bit aligned components, then we are
5618 -- not sure that the back end can handle this case correctly.
5620 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5621 Convert_To_Assignments (N, Typ);
5623 -- In all other cases, build a proper aggregate handlable by gigi
5625 else
5626 if Nkind (N) = N_Aggregate then
5628 -- If the aggregate is static and can be handled by the back-end,
5629 -- nothing left to do.
5631 if Static_Components then
5632 Set_Compile_Time_Known_Aggregate (N);
5633 Set_Expansion_Delayed (N, False);
5634 end if;
5635 end if;
5637 -- If no discriminants, nothing special to do
5639 if not Has_Discriminants (Typ) then
5640 null;
5642 -- Case of discriminants present
5644 elsif Is_Derived_Type (Typ) then
5646 -- For untagged types, non-stored discriminants are replaced
5647 -- with stored discriminants, which are the ones that gigi uses
5648 -- to describe the type and its components.
5650 Generate_Aggregate_For_Derived_Type : declare
5651 Constraints : constant List_Id := New_List;
5652 First_Comp : Node_Id;
5653 Discriminant : Entity_Id;
5654 Decl : Node_Id;
5655 Num_Disc : Int := 0;
5656 Num_Gird : Int := 0;
5658 procedure Prepend_Stored_Values (T : Entity_Id);
5659 -- Scan the list of stored discriminants of the type, and add
5660 -- their values to the aggregate being built.
5662 ---------------------------
5663 -- Prepend_Stored_Values --
5664 ---------------------------
5666 procedure Prepend_Stored_Values (T : Entity_Id) is
5667 begin
5668 Discriminant := First_Stored_Discriminant (T);
5669 while Present (Discriminant) loop
5670 New_Comp :=
5671 Make_Component_Association (Loc,
5672 Choices =>
5673 New_List (New_Occurrence_Of (Discriminant, Loc)),
5675 Expression =>
5676 New_Copy_Tree (
5677 Get_Discriminant_Value (
5678 Discriminant,
5679 Typ,
5680 Discriminant_Constraint (Typ))));
5682 if No (First_Comp) then
5683 Prepend_To (Component_Associations (N), New_Comp);
5684 else
5685 Insert_After (First_Comp, New_Comp);
5686 end if;
5688 First_Comp := New_Comp;
5689 Next_Stored_Discriminant (Discriminant);
5690 end loop;
5691 end Prepend_Stored_Values;
5693 -- Start of processing for Generate_Aggregate_For_Derived_Type
5695 begin
5696 -- Remove the associations for the discriminant of derived type
5698 First_Comp := First (Component_Associations (N));
5699 while Present (First_Comp) loop
5700 Comp := First_Comp;
5701 Next (First_Comp);
5703 if Ekind (Entity
5704 (First (Choices (Comp)))) = E_Discriminant
5705 then
5706 Remove (Comp);
5707 Num_Disc := Num_Disc + 1;
5708 end if;
5709 end loop;
5711 -- Insert stored discriminant associations in the correct
5712 -- order. If there are more stored discriminants than new
5713 -- discriminants, there is at least one new discriminant that
5714 -- constrains more than one of the stored discriminants. In
5715 -- this case we need to construct a proper subtype of the
5716 -- parent type, in order to supply values to all the
5717 -- components. Otherwise there is one-one correspondence
5718 -- between the constraints and the stored discriminants.
5720 First_Comp := Empty;
5722 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5723 while Present (Discriminant) loop
5724 Num_Gird := Num_Gird + 1;
5725 Next_Stored_Discriminant (Discriminant);
5726 end loop;
5728 -- Case of more stored discriminants than new discriminants
5730 if Num_Gird > Num_Disc then
5732 -- Create a proper subtype of the parent type, which is the
5733 -- proper implementation type for the aggregate, and convert
5734 -- it to the intended target type.
5736 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5737 while Present (Discriminant) loop
5738 New_Comp :=
5739 New_Copy_Tree (
5740 Get_Discriminant_Value (
5741 Discriminant,
5742 Typ,
5743 Discriminant_Constraint (Typ)));
5744 Append (New_Comp, Constraints);
5745 Next_Stored_Discriminant (Discriminant);
5746 end loop;
5748 Decl :=
5749 Make_Subtype_Declaration (Loc,
5750 Defining_Identifier => Make_Temporary (Loc, 'T'),
5751 Subtype_Indication =>
5752 Make_Subtype_Indication (Loc,
5753 Subtype_Mark =>
5754 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5755 Constraint =>
5756 Make_Index_Or_Discriminant_Constraint
5757 (Loc, Constraints)));
5759 Insert_Action (N, Decl);
5760 Prepend_Stored_Values (Base_Type (Typ));
5762 Set_Etype (N, Defining_Identifier (Decl));
5763 Set_Analyzed (N);
5765 Rewrite (N, Unchecked_Convert_To (Typ, N));
5766 Analyze (N);
5768 -- Case where we do not have fewer new discriminants than
5769 -- stored discriminants, so in this case we can simply use the
5770 -- stored discriminants of the subtype.
5772 else
5773 Prepend_Stored_Values (Typ);
5774 end if;
5775 end Generate_Aggregate_For_Derived_Type;
5776 end if;
5778 if Is_Tagged_Type (Typ) then
5780 -- The tagged case, _parent and _tag component must be created
5782 -- Reset null_present unconditionally. tagged records always have
5783 -- at least one field (the tag or the parent)
5785 Set_Null_Record_Present (N, False);
5787 -- When the current aggregate comes from the expansion of an
5788 -- extension aggregate, the parent expr is replaced by an
5789 -- aggregate formed by selected components of this expr
5791 if Present (Parent_Expr)
5792 and then Is_Empty_List (Comps)
5793 then
5794 Comp := First_Component_Or_Discriminant (Typ);
5795 while Present (Comp) loop
5797 -- Skip all expander-generated components
5800 not Comes_From_Source (Original_Record_Component (Comp))
5801 then
5802 null;
5804 else
5805 New_Comp :=
5806 Make_Selected_Component (Loc,
5807 Prefix =>
5808 Unchecked_Convert_To (Typ,
5809 Duplicate_Subexpr (Parent_Expr, True)),
5811 Selector_Name => New_Occurrence_Of (Comp, Loc));
5813 Append_To (Comps,
5814 Make_Component_Association (Loc,
5815 Choices =>
5816 New_List (New_Occurrence_Of (Comp, Loc)),
5817 Expression =>
5818 New_Comp));
5820 Analyze_And_Resolve (New_Comp, Etype (Comp));
5821 end if;
5823 Next_Component_Or_Discriminant (Comp);
5824 end loop;
5825 end if;
5827 -- Compute the value for the Tag now, if the type is a root it
5828 -- will be included in the aggregate right away, otherwise it will
5829 -- be propagated to the parent aggregate
5831 if Present (Orig_Tag) then
5832 Tag_Value := Orig_Tag;
5833 elsif not Tagged_Type_Expansion then
5834 Tag_Value := Empty;
5835 else
5836 Tag_Value :=
5837 New_Occurrence_Of
5838 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5839 end if;
5841 -- For a derived type, an aggregate for the parent is formed with
5842 -- all the inherited components.
5844 if Is_Derived_Type (Typ) then
5846 declare
5847 First_Comp : Node_Id;
5848 Parent_Comps : List_Id;
5849 Parent_Aggr : Node_Id;
5850 Parent_Name : Node_Id;
5852 begin
5853 -- Remove the inherited component association from the
5854 -- aggregate and store them in the parent aggregate
5856 First_Comp := First (Component_Associations (N));
5857 Parent_Comps := New_List;
5858 while Present (First_Comp)
5859 and then Scope (Original_Record_Component (
5860 Entity (First (Choices (First_Comp))))) /= Base_Typ
5861 loop
5862 Comp := First_Comp;
5863 Next (First_Comp);
5864 Remove (Comp);
5865 Append (Comp, Parent_Comps);
5866 end loop;
5868 Parent_Aggr := Make_Aggregate (Loc,
5869 Component_Associations => Parent_Comps);
5870 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
5872 -- Find the _parent component
5874 Comp := First_Component (Typ);
5875 while Chars (Comp) /= Name_uParent loop
5876 Comp := Next_Component (Comp);
5877 end loop;
5879 Parent_Name := New_Occurrence_Of (Comp, Loc);
5881 -- Insert the parent aggregate
5883 Prepend_To (Component_Associations (N),
5884 Make_Component_Association (Loc,
5885 Choices => New_List (Parent_Name),
5886 Expression => Parent_Aggr));
5888 -- Expand recursively the parent propagating the right Tag
5890 Expand_Record_Aggregate (
5891 Parent_Aggr, Tag_Value, Parent_Expr);
5892 end;
5894 -- For a root type, the tag component is added (unless compiling
5895 -- for the VMs, where tags are implicit).
5897 elsif Tagged_Type_Expansion then
5898 declare
5899 Tag_Name : constant Node_Id :=
5900 New_Occurrence_Of
5901 (First_Tag_Component (Typ), Loc);
5902 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
5903 Conv_Node : constant Node_Id :=
5904 Unchecked_Convert_To (Typ_Tag, Tag_Value);
5906 begin
5907 Set_Etype (Conv_Node, Typ_Tag);
5908 Prepend_To (Component_Associations (N),
5909 Make_Component_Association (Loc,
5910 Choices => New_List (Tag_Name),
5911 Expression => Conv_Node));
5912 end;
5913 end if;
5914 end if;
5915 end if;
5917 end Expand_Record_Aggregate;
5919 ----------------------------
5920 -- Has_Default_Init_Comps --
5921 ----------------------------
5923 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
5924 Comps : constant List_Id := Component_Associations (N);
5925 C : Node_Id;
5926 Expr : Node_Id;
5927 begin
5928 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
5930 if No (Comps) then
5931 return False;
5932 end if;
5934 if Has_Self_Reference (N) then
5935 return True;
5936 end if;
5938 -- Check if any direct component has default initialized components
5940 C := First (Comps);
5941 while Present (C) loop
5942 if Box_Present (C) then
5943 return True;
5944 end if;
5946 Next (C);
5947 end loop;
5949 -- Recursive call in case of aggregate expression
5951 C := First (Comps);
5952 while Present (C) loop
5953 Expr := Expression (C);
5955 if Present (Expr)
5956 and then
5957 Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
5958 and then Has_Default_Init_Comps (Expr)
5959 then
5960 return True;
5961 end if;
5963 Next (C);
5964 end loop;
5966 return False;
5967 end Has_Default_Init_Comps;
5969 --------------------------
5970 -- Is_Delayed_Aggregate --
5971 --------------------------
5973 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
5974 Node : Node_Id := N;
5975 Kind : Node_Kind := Nkind (Node);
5977 begin
5978 if Kind = N_Qualified_Expression then
5979 Node := Expression (Node);
5980 Kind := Nkind (Node);
5981 end if;
5983 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
5984 return False;
5985 else
5986 return Expansion_Delayed (Node);
5987 end if;
5988 end Is_Delayed_Aggregate;
5990 ----------------------------------------
5991 -- Is_Static_Dispatch_Table_Aggregate --
5992 ----------------------------------------
5994 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
5995 Typ : constant Entity_Id := Base_Type (Etype (N));
5997 begin
5998 return Static_Dispatch_Tables
5999 and then Tagged_Type_Expansion
6000 and then RTU_Loaded (Ada_Tags)
6002 -- Avoid circularity when rebuilding the compiler
6004 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
6005 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
6006 or else
6007 Typ = RTE (RE_Address_Array)
6008 or else
6009 Typ = RTE (RE_Type_Specific_Data)
6010 or else
6011 Typ = RTE (RE_Tag_Table)
6012 or else
6013 (RTE_Available (RE_Interface_Data)
6014 and then Typ = RTE (RE_Interface_Data))
6015 or else
6016 (RTE_Available (RE_Interfaces_Array)
6017 and then Typ = RTE (RE_Interfaces_Array))
6018 or else
6019 (RTE_Available (RE_Interface_Data_Element)
6020 and then Typ = RTE (RE_Interface_Data_Element)));
6021 end Is_Static_Dispatch_Table_Aggregate;
6023 --------------------
6024 -- Late_Expansion --
6025 --------------------
6027 function Late_Expansion
6028 (N : Node_Id;
6029 Typ : Entity_Id;
6030 Target : Node_Id;
6031 Flist : Node_Id := Empty;
6032 Obj : Entity_Id := Empty) return List_Id
6034 begin
6035 if Is_Record_Type (Etype (N)) then
6036 return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj);
6038 else pragma Assert (Is_Array_Type (Etype (N)));
6039 return
6040 Build_Array_Aggr_Code
6041 (N => N,
6042 Ctype => Component_Type (Etype (N)),
6043 Index => First_Index (Typ),
6044 Into => Target,
6045 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
6046 Indices => No_List,
6047 Flist => Flist);
6048 end if;
6049 end Late_Expansion;
6051 ----------------------------------
6052 -- Make_OK_Assignment_Statement --
6053 ----------------------------------
6055 function Make_OK_Assignment_Statement
6056 (Sloc : Source_Ptr;
6057 Name : Node_Id;
6058 Expression : Node_Id) return Node_Id
6060 begin
6061 Set_Assignment_OK (Name);
6063 return Make_Assignment_Statement (Sloc, Name, Expression);
6064 end Make_OK_Assignment_Statement;
6066 -----------------------
6067 -- Number_Of_Choices --
6068 -----------------------
6070 function Number_Of_Choices (N : Node_Id) return Nat is
6071 Assoc : Node_Id;
6072 Choice : Node_Id;
6074 Nb_Choices : Nat := 0;
6076 begin
6077 if Present (Expressions (N)) then
6078 return 0;
6079 end if;
6081 Assoc := First (Component_Associations (N));
6082 while Present (Assoc) loop
6083 Choice := First (Choices (Assoc));
6084 while Present (Choice) loop
6085 if Nkind (Choice) /= N_Others_Choice then
6086 Nb_Choices := Nb_Choices + 1;
6087 end if;
6089 Next (Choice);
6090 end loop;
6092 Next (Assoc);
6093 end loop;
6095 return Nb_Choices;
6096 end Number_Of_Choices;
6098 ------------------------------------
6099 -- Packed_Array_Aggregate_Handled --
6100 ------------------------------------
6102 -- The current version of this procedure will handle at compile time
6103 -- any array aggregate that meets these conditions:
6105 -- One dimensional, bit packed
6106 -- Underlying packed type is modular type
6107 -- Bounds are within 32-bit Int range
6108 -- All bounds and values are static
6110 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6111 Loc : constant Source_Ptr := Sloc (N);
6112 Typ : constant Entity_Id := Etype (N);
6113 Ctyp : constant Entity_Id := Component_Type (Typ);
6115 Not_Handled : exception;
6116 -- Exception raised if this aggregate cannot be handled
6118 begin
6119 -- For now, handle only one dimensional bit packed arrays
6121 if not Is_Bit_Packed_Array (Typ)
6122 or else Number_Dimensions (Typ) > 1
6123 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
6124 then
6125 return False;
6126 end if;
6128 if not Is_Scalar_Type (Component_Type (Typ))
6129 and then Has_Non_Standard_Rep (Component_Type (Typ))
6130 then
6131 return False;
6132 end if;
6134 declare
6135 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6137 Lo : Node_Id;
6138 Hi : Node_Id;
6139 -- Bounds of index type
6141 Lob : Uint;
6142 Hib : Uint;
6143 -- Values of bounds if compile time known
6145 function Get_Component_Val (N : Node_Id) return Uint;
6146 -- Given a expression value N of the component type Ctyp, returns a
6147 -- value of Csiz (component size) bits representing this value. If
6148 -- the value is non-static or any other reason exists why the value
6149 -- cannot be returned, then Not_Handled is raised.
6151 -----------------------
6152 -- Get_Component_Val --
6153 -----------------------
6155 function Get_Component_Val (N : Node_Id) return Uint is
6156 Val : Uint;
6158 begin
6159 -- We have to analyze the expression here before doing any further
6160 -- processing here. The analysis of such expressions is deferred
6161 -- till expansion to prevent some problems of premature analysis.
6163 Analyze_And_Resolve (N, Ctyp);
6165 -- Must have a compile time value. String literals have to be
6166 -- converted into temporaries as well, because they cannot easily
6167 -- be converted into their bit representation.
6169 if not Compile_Time_Known_Value (N)
6170 or else Nkind (N) = N_String_Literal
6171 then
6172 raise Not_Handled;
6173 end if;
6175 Val := Expr_Rep_Value (N);
6177 -- Adjust for bias, and strip proper number of bits
6179 if Has_Biased_Representation (Ctyp) then
6180 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6181 end if;
6183 return Val mod Uint_2 ** Csiz;
6184 end Get_Component_Val;
6186 -- Here we know we have a one dimensional bit packed array
6188 begin
6189 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6191 -- Cannot do anything if bounds are dynamic
6193 if not Compile_Time_Known_Value (Lo)
6194 or else
6195 not Compile_Time_Known_Value (Hi)
6196 then
6197 return False;
6198 end if;
6200 -- Or are silly out of range of int bounds
6202 Lob := Expr_Value (Lo);
6203 Hib := Expr_Value (Hi);
6205 if not UI_Is_In_Int_Range (Lob)
6206 or else
6207 not UI_Is_In_Int_Range (Hib)
6208 then
6209 return False;
6210 end if;
6212 -- At this stage we have a suitable aggregate for handling at compile
6213 -- time (the only remaining checks are that the values of expressions
6214 -- in the aggregate are compile time known (check is performed by
6215 -- Get_Component_Val), and that any subtypes or ranges are statically
6216 -- known.
6218 -- If the aggregate is not fully positional at this stage, then
6219 -- convert it to positional form. Either this will fail, in which
6220 -- case we can do nothing, or it will succeed, in which case we have
6221 -- succeeded in handling the aggregate, or it will stay an aggregate,
6222 -- in which case we have failed to handle this case.
6224 if Present (Component_Associations (N)) then
6225 Convert_To_Positional
6226 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6227 return Nkind (N) /= N_Aggregate;
6228 end if;
6230 -- Otherwise we are all positional, so convert to proper value
6232 declare
6233 Lov : constant Int := UI_To_Int (Lob);
6234 Hiv : constant Int := UI_To_Int (Hib);
6236 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6237 -- The length of the array (number of elements)
6239 Aggregate_Val : Uint;
6240 -- Value of aggregate. The value is set in the low order bits of
6241 -- this value. For the little-endian case, the values are stored
6242 -- from low-order to high-order and for the big-endian case the
6243 -- values are stored from high-order to low-order. Note that gigi
6244 -- will take care of the conversions to left justify the value in
6245 -- the big endian case (because of left justified modular type
6246 -- processing), so we do not have to worry about that here.
6248 Lit : Node_Id;
6249 -- Integer literal for resulting constructed value
6251 Shift : Nat;
6252 -- Shift count from low order for next value
6254 Incr : Int;
6255 -- Shift increment for loop
6257 Expr : Node_Id;
6258 -- Next expression from positional parameters of aggregate
6260 begin
6261 -- For little endian, we fill up the low order bits of the target
6262 -- value. For big endian we fill up the high order bits of the
6263 -- target value (which is a left justified modular value).
6265 if Bytes_Big_Endian xor Debug_Flag_8 then
6266 Shift := Csiz * (Len - 1);
6267 Incr := -Csiz;
6268 else
6269 Shift := 0;
6270 Incr := +Csiz;
6271 end if;
6273 -- Loop to set the values
6275 if Len = 0 then
6276 Aggregate_Val := Uint_0;
6277 else
6278 Expr := First (Expressions (N));
6279 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6281 for J in 2 .. Len loop
6282 Shift := Shift + Incr;
6283 Next (Expr);
6284 Aggregate_Val :=
6285 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6286 end loop;
6287 end if;
6289 -- Now we can rewrite with the proper value
6291 Lit :=
6292 Make_Integer_Literal (Loc,
6293 Intval => Aggregate_Val);
6294 Set_Print_In_Hex (Lit);
6296 -- Construct the expression using this literal. Note that it is
6297 -- important to qualify the literal with its proper modular type
6298 -- since universal integer does not have the required range and
6299 -- also this is a left justified modular type, which is important
6300 -- in the big-endian case.
6302 Rewrite (N,
6303 Unchecked_Convert_To (Typ,
6304 Make_Qualified_Expression (Loc,
6305 Subtype_Mark =>
6306 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6307 Expression => Lit)));
6309 Analyze_And_Resolve (N, Typ);
6310 return True;
6311 end;
6312 end;
6314 exception
6315 when Not_Handled =>
6316 return False;
6317 end Packed_Array_Aggregate_Handled;
6319 ----------------------------
6320 -- Has_Mutable_Components --
6321 ----------------------------
6323 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6324 Comp : Entity_Id;
6326 begin
6327 Comp := First_Component (Typ);
6328 while Present (Comp) loop
6329 if Is_Record_Type (Etype (Comp))
6330 and then Has_Discriminants (Etype (Comp))
6331 and then not Is_Constrained (Etype (Comp))
6332 then
6333 return True;
6334 end if;
6336 Next_Component (Comp);
6337 end loop;
6339 return False;
6340 end Has_Mutable_Components;
6342 ------------------------------
6343 -- Initialize_Discriminants --
6344 ------------------------------
6346 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6347 Loc : constant Source_Ptr := Sloc (N);
6348 Bas : constant Entity_Id := Base_Type (Typ);
6349 Par : constant Entity_Id := Etype (Bas);
6350 Decl : constant Node_Id := Parent (Par);
6351 Ref : Node_Id;
6353 begin
6354 if Is_Tagged_Type (Bas)
6355 and then Is_Derived_Type (Bas)
6356 and then Has_Discriminants (Par)
6357 and then Has_Discriminants (Bas)
6358 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6359 and then Nkind (Decl) = N_Full_Type_Declaration
6360 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6361 and then Present
6362 (Variant_Part (Component_List (Type_Definition (Decl))))
6363 and then Nkind (N) /= N_Extension_Aggregate
6364 then
6366 -- Call init proc to set discriminants.
6367 -- There should eventually be a special procedure for this ???
6369 Ref := New_Reference_To (Defining_Identifier (N), Loc);
6370 Insert_Actions_After (N,
6371 Build_Initialization_Call (Sloc (N), Ref, Typ));
6372 end if;
6373 end Initialize_Discriminants;
6375 ----------------
6376 -- Must_Slide --
6377 ----------------
6379 function Must_Slide
6380 (Obj_Type : Entity_Id;
6381 Typ : Entity_Id) return Boolean
6383 L1, L2, H1, H2 : Node_Id;
6384 begin
6385 -- No sliding if the type of the object is not established yet, if it is
6386 -- an unconstrained type whose actual subtype comes from the aggregate,
6387 -- or if the two types are identical.
6389 if not Is_Array_Type (Obj_Type) then
6390 return False;
6392 elsif not Is_Constrained (Obj_Type) then
6393 return False;
6395 elsif Typ = Obj_Type then
6396 return False;
6398 else
6399 -- Sliding can only occur along the first dimension
6401 Get_Index_Bounds (First_Index (Typ), L1, H1);
6402 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6404 if not Is_Static_Expression (L1)
6405 or else not Is_Static_Expression (L2)
6406 or else not Is_Static_Expression (H1)
6407 or else not Is_Static_Expression (H2)
6408 then
6409 return False;
6410 else
6411 return Expr_Value (L1) /= Expr_Value (L2)
6412 or else Expr_Value (H1) /= Expr_Value (H2);
6413 end if;
6414 end if;
6415 end Must_Slide;
6417 ---------------------------
6418 -- Safe_Slice_Assignment --
6419 ---------------------------
6421 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6422 Loc : constant Source_Ptr := Sloc (Parent (N));
6423 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6424 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6425 Expr : Node_Id;
6426 L_J : Entity_Id;
6427 L_Iter : Node_Id;
6428 L_Body : Node_Id;
6429 Stat : Node_Id;
6431 begin
6432 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6434 if Comes_From_Source (N)
6435 and then No (Expressions (N))
6436 and then Nkind (First (Choices (First (Component_Associations (N)))))
6437 = N_Others_Choice
6438 then
6439 Expr := Expression (First (Component_Associations (N)));
6440 L_J := Make_Temporary (Loc, 'J');
6442 L_Iter :=
6443 Make_Iteration_Scheme (Loc,
6444 Loop_Parameter_Specification =>
6445 Make_Loop_Parameter_Specification
6446 (Loc,
6447 Defining_Identifier => L_J,
6448 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6450 L_Body :=
6451 Make_Assignment_Statement (Loc,
6452 Name =>
6453 Make_Indexed_Component (Loc,
6454 Prefix => Relocate_Node (Pref),
6455 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6456 Expression => Relocate_Node (Expr));
6458 -- Construct the final loop
6460 Stat :=
6461 Make_Implicit_Loop_Statement
6462 (Node => Parent (N),
6463 Identifier => Empty,
6464 Iteration_Scheme => L_Iter,
6465 Statements => New_List (L_Body));
6467 -- Set type of aggregate to be type of lhs in assignment,
6468 -- to suppress redundant length checks.
6470 Set_Etype (N, Etype (Name (Parent (N))));
6472 Rewrite (Parent (N), Stat);
6473 Analyze (Parent (N));
6474 return True;
6476 else
6477 return False;
6478 end if;
6479 end Safe_Slice_Assignment;
6481 ---------------------
6482 -- Sort_Case_Table --
6483 ---------------------
6485 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6486 L : constant Int := Case_Table'First;
6487 U : constant Int := Case_Table'Last;
6488 K : Int;
6489 J : Int;
6490 T : Case_Bounds;
6492 begin
6493 K := L;
6494 while K /= U loop
6495 T := Case_Table (K + 1);
6497 J := K + 1;
6498 while J /= L
6499 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6500 Expr_Value (T.Choice_Lo)
6501 loop
6502 Case_Table (J) := Case_Table (J - 1);
6503 J := J - 1;
6504 end loop;
6506 Case_Table (J) := T;
6507 K := K + 1;
6508 end loop;
6509 end Sort_Case_Table;
6511 ----------------------------
6512 -- Static_Array_Aggregate --
6513 ----------------------------
6515 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6516 Bounds : constant Node_Id := Aggregate_Bounds (N);
6518 Typ : constant Entity_Id := Etype (N);
6519 Comp_Type : constant Entity_Id := Component_Type (Typ);
6520 Agg : Node_Id;
6521 Expr : Node_Id;
6522 Lo : Node_Id;
6523 Hi : Node_Id;
6525 begin
6526 if Is_Tagged_Type (Typ)
6527 or else Is_Controlled (Typ)
6528 or else Is_Packed (Typ)
6529 then
6530 return False;
6531 end if;
6533 if Present (Bounds)
6534 and then Nkind (Bounds) = N_Range
6535 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6536 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6537 then
6538 Lo := Low_Bound (Bounds);
6539 Hi := High_Bound (Bounds);
6541 if No (Component_Associations (N)) then
6543 -- Verify that all components are static integers
6545 Expr := First (Expressions (N));
6546 while Present (Expr) loop
6547 if Nkind (Expr) /= N_Integer_Literal then
6548 return False;
6549 end if;
6551 Next (Expr);
6552 end loop;
6554 return True;
6556 else
6557 -- We allow only a single named association, either a static
6558 -- range or an others_clause, with a static expression.
6560 Expr := First (Component_Associations (N));
6562 if Present (Expressions (N)) then
6563 return False;
6565 elsif Present (Next (Expr)) then
6566 return False;
6568 elsif Present (Next (First (Choices (Expr)))) then
6569 return False;
6571 else
6572 -- The aggregate is static if all components are literals,
6573 -- or else all its components are static aggregates for the
6574 -- component type. We also limit the size of a static aggregate
6575 -- to prevent runaway static expressions.
6577 if Is_Array_Type (Comp_Type)
6578 or else Is_Record_Type (Comp_Type)
6579 then
6580 if Nkind (Expression (Expr)) /= N_Aggregate
6581 or else
6582 not Compile_Time_Known_Aggregate (Expression (Expr))
6583 then
6584 return False;
6585 end if;
6587 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6588 return False;
6590 elsif not Aggr_Size_OK (N, Typ) then
6591 return False;
6592 end if;
6594 -- Create a positional aggregate with the right number of
6595 -- copies of the expression.
6597 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6599 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6600 loop
6601 Append_To
6602 (Expressions (Agg), New_Copy (Expression (Expr)));
6604 -- The copied expression must be analyzed and resolved.
6605 -- Besides setting the type, this ensures that static
6606 -- expressions are appropriately marked as such.
6608 Analyze_And_Resolve
6609 (Last (Expressions (Agg)), Component_Type (Typ));
6610 end loop;
6612 Set_Aggregate_Bounds (Agg, Bounds);
6613 Set_Etype (Agg, Typ);
6614 Set_Analyzed (Agg);
6615 Rewrite (N, Agg);
6616 Set_Compile_Time_Known_Aggregate (N);
6618 return True;
6619 end if;
6620 end if;
6622 else
6623 return False;
6624 end if;
6625 end Static_Array_Aggregate;
6627 end Exp_Aggr;