Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / ada / exp_aggr.adb
blobf1e7fb4cfbb2f7e62e2c9daf9ecaf5bfbd953ffe
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-2007, 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 Expander; use Expander;
32 with Exp_Util; use Exp_Util;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Tss; use Exp_Tss;
37 with Freeze; use Freeze;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Namet; use Namet;
41 with Nmake; use Nmake;
42 with Nlists; use Nlists;
43 with Opt; use Opt;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Ttypes; use Ttypes;
48 with Sem; use Sem;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Eval; use Sem_Eval;
51 with Sem_Res; use Sem_Res;
52 with Sem_Util; use Sem_Util;
53 with Sinfo; use Sinfo;
54 with Snames; use Snames;
55 with Stand; use Stand;
56 with Targparm; use Targparm;
57 with Tbuild; use Tbuild;
58 with Uintp; use Uintp;
60 package body Exp_Aggr is
62 type Case_Bounds is record
63 Choice_Lo : Node_Id;
64 Choice_Hi : Node_Id;
65 Choice_Node : Node_Id;
66 end record;
68 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
69 -- Table type used by Check_Case_Choices procedure
71 function Must_Slide
72 (Obj_Type : Entity_Id;
73 Typ : Entity_Id) return Boolean;
74 -- A static array aggregate in an object declaration can in most cases be
75 -- expanded in place. The one exception is when the aggregate is given
76 -- with component associations that specify different bounds from those of
77 -- the type definition in the object declaration. In this pathological
78 -- case the aggregate must slide, and we must introduce an intermediate
79 -- temporary to hold it.
81 -- The same holds in an assignment to one-dimensional array of arrays,
82 -- when a component may be given with bounds that differ from those of the
83 -- component type.
85 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
86 -- Sort the Case Table using the Lower Bound of each Choice as the key.
87 -- A simple insertion sort is used since the number of choices in a case
88 -- statement of variant part will usually be small and probably in near
89 -- sorted order.
91 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
92 -- N is an aggregate (record or array). Checks the presence of default
93 -- initialization (<>) in any component (Ada 2005: AI-287)
95 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
96 -- Returns true if N is an aggregate used to initialize the components
97 -- of an statically allocated dispatch table.
99 ------------------------------------------------------
100 -- Local subprograms for Record Aggregate Expansion --
101 ------------------------------------------------------
103 procedure Expand_Record_Aggregate
104 (N : Node_Id;
105 Orig_Tag : Node_Id := Empty;
106 Parent_Expr : Node_Id := Empty);
107 -- This is the top level procedure for record aggregate expansion.
108 -- Expansion for record aggregates needs expand aggregates for tagged
109 -- record types. Specifically Expand_Record_Aggregate adds the Tag
110 -- field in front of the Component_Association list that was created
111 -- during resolution by Resolve_Record_Aggregate.
113 -- N is the record aggregate node.
114 -- Orig_Tag is the value of the Tag that has to be provided for this
115 -- specific aggregate. It carries the tag corresponding to the type
116 -- of the outermost aggregate during the recursive expansion
117 -- Parent_Expr is the ancestor part of the original extension
118 -- aggregate
120 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
121 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
122 -- aggregate (which can only be a record type, this procedure is only used
123 -- for record types). Transform the given aggregate into a sequence of
124 -- assignments performed component by component.
126 function Build_Record_Aggr_Code
127 (N : Node_Id;
128 Typ : Entity_Id;
129 Lhs : Node_Id;
130 Flist : Node_Id := Empty;
131 Obj : Entity_Id := Empty;
132 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
133 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
134 -- aggregate. Target is an expression containing the location on which the
135 -- component by component assignments will take place. Returns the list of
136 -- assignments plus all other adjustments needed for tagged and controlled
137 -- types. Flist is an expression representing the finalization list on
138 -- which to attach the controlled components if any. Obj is present in the
139 -- object declaration and dynamic allocation cases, it contains an entity
140 -- that allows to know if the value being created needs to be attached to
141 -- the final list in case of pragma Finalize_Storage_Only.
143 -- ???
144 -- The meaning of the Obj formal is extremely unclear. *What* entity
145 -- should be passed? For the object declaration case we may guess that
146 -- this is the object being declared, but what about the allocator case?
148 -- Is_Limited_Ancestor_Expansion indicates that the function has been
149 -- called recursively to expand the limited ancestor to avoid copying it.
151 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
152 -- Return true if one of the component is of a discriminated type with
153 -- defaults. An aggregate for a type with mutable components must be
154 -- expanded into individual assignments.
156 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
157 -- If the type of the aggregate is a type extension with renamed discrimi-
158 -- nants, we must initialize the hidden discriminants of the parent.
159 -- Otherwise, the target object must not be initialized. The discriminants
160 -- are initialized by calling the initialization procedure for the type.
161 -- This is incorrect if the initialization of other components has any
162 -- side effects. We restrict this call to the case where the parent type
163 -- has a variant part, because this is the only case where the hidden
164 -- discriminants are accessed, namely when calling discriminant checking
165 -- functions of the parent type, and when applying a stream attribute to
166 -- an object of the derived type.
168 -----------------------------------------------------
169 -- Local Subprograms for Array Aggregate Expansion --
170 -----------------------------------------------------
172 function Aggr_Size_OK (Typ : Entity_Id) return Boolean;
173 -- Very large static aggregates present problems to the back-end, and
174 -- are transformed into assignments and loops. This function verifies
175 -- that the total number of components of an aggregate is acceptable
176 -- for transformation into a purely positional static form. It is called
177 -- prior to calling Flatten.
179 procedure Convert_Array_Aggr_In_Allocator
180 (Decl : Node_Id;
181 Aggr : Node_Id;
182 Target : Node_Id);
183 -- If the aggregate appears within an allocator and can be expanded in
184 -- place, this routine generates the individual assignments to components
185 -- of the designated object. This is an optimization over the general
186 -- case, where a temporary is first created on the stack and then used to
187 -- construct the allocated object on the heap.
189 procedure Convert_To_Positional
190 (N : Node_Id;
191 Max_Others_Replicate : Nat := 5;
192 Handle_Bit_Packed : Boolean := False);
193 -- If possible, convert named notation to positional notation. This
194 -- conversion is possible only in some static cases. If the conversion is
195 -- possible, then N is rewritten with the analyzed converted aggregate.
196 -- The parameter Max_Others_Replicate controls the maximum number of
197 -- values corresponding to an others choice that will be converted to
198 -- positional notation (the default of 5 is the normal limit, and reflects
199 -- the fact that normally the loop is better than a lot of separate
200 -- assignments). Note that this limit gets overridden in any case if
201 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
202 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
203 -- not expect the back end to handle bit packed arrays, so the normal case
204 -- of conversion is pointless), but in the special case of a call from
205 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
206 -- these are cases we handle in there.
208 procedure Expand_Array_Aggregate (N : Node_Id);
209 -- This is the top-level routine to perform array aggregate expansion.
210 -- N is the N_Aggregate node to be expanded.
212 function Backend_Processing_Possible (N : Node_Id) return Boolean;
213 -- This function checks if array aggregate N can be processed directly
214 -- by Gigi. If this is the case True is returned.
216 function Build_Array_Aggr_Code
217 (N : Node_Id;
218 Ctype : Entity_Id;
219 Index : Node_Id;
220 Into : Node_Id;
221 Scalar_Comp : Boolean;
222 Indices : List_Id := No_List;
223 Flist : Node_Id := Empty) return List_Id;
224 -- This recursive routine returns a list of statements containing the
225 -- loops and assignments that are needed for the expansion of the array
226 -- aggregate N.
228 -- N is the (sub-)aggregate node to be expanded into code. This node
229 -- has been fully analyzed, and its Etype is properly set.
231 -- Index is the index node corresponding to the array sub-aggregate N.
233 -- Into is the target expression into which we are copying the aggregate.
234 -- Note that this node may not have been analyzed yet, and so the Etype
235 -- field may not be set.
237 -- Scalar_Comp is True if the component type of the aggregate is scalar.
239 -- Indices is the current list of expressions used to index the
240 -- object we are writing into.
242 -- Flist is an expression representing the finalization list on which
243 -- to attach the controlled components if any.
245 function Number_Of_Choices (N : Node_Id) return Nat;
246 -- Returns the number of discrete choices (not including the others choice
247 -- if present) contained in (sub-)aggregate N.
249 function Late_Expansion
250 (N : Node_Id;
251 Typ : Entity_Id;
252 Target : Node_Id;
253 Flist : Node_Id := Empty;
254 Obj : Entity_Id := Empty) return List_Id;
255 -- N is a nested (record or array) aggregate that has been marked with
256 -- 'Delay_Expansion'. Typ is the expected type of the aggregate and Target
257 -- is a (duplicable) expression that will hold the result of the aggregate
258 -- expansion. Flist is the finalization list to be used to attach
259 -- controlled components. 'Obj' when non empty, carries the original
260 -- object being initialized in order to know if it needs to be attached to
261 -- the previous parameter which may not be the case in the case where
262 -- Finalize_Storage_Only is set. Basically this procedure is used to
263 -- implement top-down expansions of nested aggregates. This is necessary
264 -- for avoiding temporaries at each level as well as for propagating the
265 -- right internal finalization list.
267 function Make_OK_Assignment_Statement
268 (Sloc : Source_Ptr;
269 Name : Node_Id;
270 Expression : Node_Id) return Node_Id;
271 -- This is like Make_Assignment_Statement, except that Assignment_OK
272 -- is set in the left operand. All assignments built by this unit
273 -- use this routine. This is needed to deal with assignments to
274 -- initialized constants that are done in place.
276 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
277 -- Given an array aggregate, this function handles the case of a packed
278 -- array aggregate with all constant values, where the aggregate can be
279 -- evaluated at compile time. If this is possible, then N is rewritten
280 -- to be its proper compile time value with all the components properly
281 -- assembled. The expression is analyzed and resolved and True is
282 -- returned. If this transformation is not possible, N is unchanged
283 -- and False is returned
285 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
286 -- If a slice assignment has an aggregate with a single others_choice,
287 -- the assignment can be done in place even if bounds are not static,
288 -- by converting it into a loop over the discrete range of the slice.
290 ------------------
291 -- Aggr_Size_OK --
292 ------------------
294 function Aggr_Size_OK (Typ : Entity_Id) return Boolean is
295 Lo : Node_Id;
296 Hi : Node_Id;
297 Indx : Node_Id;
298 Siz : Int;
299 Lov : Uint;
300 Hiv : Uint;
302 -- The following constant determines the maximum size of an
303 -- aggregate produced by converting named to positional
304 -- notation (e.g. from others clauses). This avoids running
305 -- away with attempts to convert huge aggregates, which hit
306 -- memory limits in the backend.
308 -- The normal limit is 5000, but we increase this limit to
309 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code)
310 -- or Restrictions (No_Implicit_Loops) is specified, since in
311 -- either case, we are at risk of declaring the program illegal
312 -- because of this limit.
314 Max_Aggr_Size : constant Nat :=
315 5000 + (2 ** 24 - 5000) *
316 Boolean'Pos
317 (Restriction_Active (No_Elaboration_Code)
318 or else
319 Restriction_Active (No_Implicit_Loops));
321 function Component_Count (T : Entity_Id) return Int;
322 -- The limit is applied to the total number of components that the
323 -- aggregate will have, which is the number of static expressions
324 -- that will appear in the flattened array. This requires a recursive
325 -- computation of the the number of scalar components of the structure.
327 ---------------------
328 -- Component_Count --
329 ---------------------
331 function Component_Count (T : Entity_Id) return Int is
332 Res : Int := 0;
333 Comp : Entity_Id;
335 begin
336 if Is_Scalar_Type (T) then
337 return 1;
339 elsif Is_Record_Type (T) then
340 Comp := First_Component (T);
341 while Present (Comp) loop
342 Res := Res + Component_Count (Etype (Comp));
343 Next_Component (Comp);
344 end loop;
346 return Res;
348 elsif Is_Array_Type (T) then
349 declare
350 Lo : constant Node_Id :=
351 Type_Low_Bound (Etype (First_Index (T)));
352 Hi : constant Node_Id :=
353 Type_High_Bound (Etype (First_Index (T)));
355 Siz : constant Int := Component_Count (Component_Type (T));
357 begin
358 if not Compile_Time_Known_Value (Lo)
359 or else not Compile_Time_Known_Value (Hi)
360 then
361 return 0;
362 else
363 return
364 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
365 end if;
366 end;
368 else
369 -- Can only be a null for an access type
371 return 1;
372 end if;
373 end Component_Count;
375 -- Start of processing for Aggr_Size_OK
377 begin
378 Siz := Component_Count (Component_Type (Typ));
380 Indx := First_Index (Typ);
381 while Present (Indx) loop
382 Lo := Type_Low_Bound (Etype (Indx));
383 Hi := Type_High_Bound (Etype (Indx));
385 -- Bounds need to be known at compile time
387 if not Compile_Time_Known_Value (Lo)
388 or else not Compile_Time_Known_Value (Hi)
389 then
390 return False;
391 end if;
393 Lov := Expr_Value (Lo);
394 Hiv := Expr_Value (Hi);
396 -- A flat array is always safe
398 if Hiv < Lov then
399 return True;
400 end if;
402 declare
403 Rng : constant Uint := Hiv - Lov + 1;
405 begin
406 -- Check if size is too large
408 if not UI_Is_In_Int_Range (Rng) then
409 return False;
410 end if;
412 Siz := Siz * UI_To_Int (Rng);
413 end;
415 if Siz <= 0
416 or else Siz > Max_Aggr_Size
417 then
418 return False;
419 end if;
421 -- Bounds must be in integer range, for later array construction
423 if not UI_Is_In_Int_Range (Lov)
424 or else
425 not UI_Is_In_Int_Range (Hiv)
426 then
427 return False;
428 end if;
430 Next_Index (Indx);
431 end loop;
433 return True;
434 end Aggr_Size_OK;
436 ---------------------------------
437 -- Backend_Processing_Possible --
438 ---------------------------------
440 -- Backend processing by Gigi/gcc is possible only if all the following
441 -- conditions are met:
443 -- 1. N is fully positional
445 -- 2. N is not a bit-packed array aggregate;
447 -- 3. The size of N's array type must be known at compile time. Note
448 -- that this implies that the component size is also known
450 -- 4. The array type of N does not follow the Fortran layout convention
451 -- or if it does it must be 1 dimensional.
453 -- 5. The array component type may not be tagged (which could necessitate
454 -- reassignment of proper tags).
456 -- 6. The array component type must not have unaligned bit components
458 -- 7. None of the components of the aggregate may be bit unaligned
459 -- components.
461 -- 8. There cannot be delayed components, since we do not know enough
462 -- at this stage to know if back end processing is possible.
464 -- 9. There cannot be any discriminated record components, since the
465 -- back end cannot handle this complex case.
467 function Backend_Processing_Possible (N : Node_Id) return Boolean is
468 Typ : constant Entity_Id := Etype (N);
469 -- Typ is the correct constrained array subtype of the aggregate
471 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
472 -- This routine checks components of aggregate N, enforcing checks
473 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
474 -- performed on subaggregates. The Index value is the current index
475 -- being checked in the multi-dimensional case.
477 ---------------------
478 -- Component_Check --
479 ---------------------
481 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
482 Expr : Node_Id;
484 begin
485 -- Checks 1: (no component associations)
487 if Present (Component_Associations (N)) then
488 return False;
489 end if;
491 -- Checks on components
493 -- Recurse to check subaggregates, which may appear in qualified
494 -- expressions. If delayed, the front-end will have to expand.
495 -- If the component is a discriminated record, treat as non-static,
496 -- as the back-end cannot handle this properly.
498 Expr := First (Expressions (N));
499 while Present (Expr) loop
501 -- Checks 8: (no delayed components)
503 if Is_Delayed_Aggregate (Expr) then
504 return False;
505 end if;
507 -- Checks 9: (no discriminated records)
509 if Present (Etype (Expr))
510 and then Is_Record_Type (Etype (Expr))
511 and then Has_Discriminants (Etype (Expr))
512 then
513 return False;
514 end if;
516 -- Checks 7. Component must not be bit aligned component
518 if Possible_Bit_Aligned_Component (Expr) then
519 return False;
520 end if;
522 -- Recursion to following indexes for multiple dimension case
524 if Present (Next_Index (Index))
525 and then not Component_Check (Expr, Next_Index (Index))
526 then
527 return False;
528 end if;
530 -- All checks for that component finished, on to next
532 Next (Expr);
533 end loop;
535 return True;
536 end Component_Check;
538 -- Start of processing for Backend_Processing_Possible
540 begin
541 -- Checks 2 (array must not be bit packed)
543 if Is_Bit_Packed_Array (Typ) then
544 return False;
545 end if;
547 -- Checks 4 (array must not be multi-dimensional Fortran case)
549 if Convention (Typ) = Convention_Fortran
550 and then Number_Dimensions (Typ) > 1
551 then
552 return False;
553 end if;
555 -- Checks 3 (size of array must be known at compile time)
557 if not Size_Known_At_Compile_Time (Typ) then
558 return False;
559 end if;
561 -- Checks on components
563 if not Component_Check (N, First_Index (Typ)) then
564 return False;
565 end if;
567 -- Checks 5 (if the component type is tagged, then we may need to do
568 -- tag adjustments. Perhaps this should be refined to check for any
569 -- component associations that actually need tag adjustment, similar
570 -- to the test in Component_Not_OK_For_Backend for record aggregates
571 -- with tagged components, but not clear whether it's worthwhile ???;
572 -- in the case of the JVM, object tags are handled implicitly)
574 if Is_Tagged_Type (Component_Type (Typ)) and then VM_Target = No_VM then
575 return False;
576 end if;
578 -- Checks 6 (component type must not have bit aligned components)
580 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
581 return False;
582 end if;
584 -- Backend processing is possible
586 Set_Size_Known_At_Compile_Time (Etype (N), True);
587 return True;
588 end Backend_Processing_Possible;
590 ---------------------------
591 -- Build_Array_Aggr_Code --
592 ---------------------------
594 -- The code that we generate from a one dimensional aggregate is
596 -- 1. If the sub-aggregate contains discrete choices we
598 -- (a) Sort the discrete choices
600 -- (b) Otherwise for each discrete choice that specifies a range we
601 -- emit a loop. If a range specifies a maximum of three values, or
602 -- we are dealing with an expression we emit a sequence of
603 -- assignments instead of a loop.
605 -- (c) Generate the remaining loops to cover the others choice if any
607 -- 2. If the aggregate contains positional elements we
609 -- (a) translate the positional elements in a series of assignments
611 -- (b) Generate a final loop to cover the others choice if any.
612 -- Note that this final loop has to be a while loop since the case
614 -- L : Integer := Integer'Last;
615 -- H : Integer := Integer'Last;
616 -- A : array (L .. H) := (1, others =>0);
618 -- cannot be handled by a for loop. Thus for the following
620 -- array (L .. H) := (.. positional elements.., others =>E);
622 -- we always generate something like:
624 -- J : Index_Type := Index_Of_Last_Positional_Element;
625 -- while J < H loop
626 -- J := Index_Base'Succ (J)
627 -- Tmp (J) := E;
628 -- end loop;
630 function Build_Array_Aggr_Code
631 (N : Node_Id;
632 Ctype : Entity_Id;
633 Index : Node_Id;
634 Into : Node_Id;
635 Scalar_Comp : Boolean;
636 Indices : List_Id := No_List;
637 Flist : Node_Id := Empty) return List_Id
639 Loc : constant Source_Ptr := Sloc (N);
640 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
641 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
642 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
644 function Add (Val : Int; To : Node_Id) return Node_Id;
645 -- Returns an expression where Val is added to expression To, unless
646 -- To+Val is provably out of To's base type range. To must be an
647 -- already analyzed expression.
649 function Empty_Range (L, H : Node_Id) return Boolean;
650 -- Returns True if the range defined by L .. H is certainly empty
652 function Equal (L, H : Node_Id) return Boolean;
653 -- Returns True if L = H for sure
655 function Index_Base_Name return Node_Id;
656 -- Returns a new reference to the index type name
658 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
659 -- Ind must be a side-effect free expression. If the input aggregate
660 -- N to Build_Loop contains no sub-aggregates, then this function
661 -- returns the assignment statement:
663 -- Into (Indices, Ind) := Expr;
665 -- Otherwise we call Build_Code recursively
667 -- Ada 2005 (AI-287): In case of default initialized component, Expr
668 -- is empty and we generate a call to the corresponding IP subprogram.
670 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
671 -- Nodes L and H must be side-effect free expressions.
672 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
673 -- This routine returns the for loop statement
675 -- for J in Index_Base'(L) .. Index_Base'(H) loop
676 -- Into (Indices, J) := Expr;
677 -- end loop;
679 -- Otherwise we call Build_Code recursively.
680 -- As an optimization if the loop covers 3 or less scalar elements we
681 -- generate a sequence of assignments.
683 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
684 -- Nodes L and H must be side-effect free expressions.
685 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
686 -- This routine returns the while loop statement
688 -- J : Index_Base := L;
689 -- while J < H loop
690 -- J := Index_Base'Succ (J);
691 -- Into (Indices, J) := Expr;
692 -- end loop;
694 -- Otherwise we call Build_Code recursively
696 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
697 function Local_Expr_Value (E : Node_Id) return Uint;
698 -- These two Local routines are used to replace the corresponding ones
699 -- in sem_eval because while processing the bounds of an aggregate with
700 -- discrete choices whose index type is an enumeration, we build static
701 -- expressions not recognized by Compile_Time_Known_Value as such since
702 -- they have not yet been analyzed and resolved. All the expressions in
703 -- question are things like Index_Base_Name'Val (Const) which we can
704 -- easily recognize as being constant.
706 ---------
707 -- Add --
708 ---------
710 function Add (Val : Int; To : Node_Id) return Node_Id is
711 Expr_Pos : Node_Id;
712 Expr : Node_Id;
713 To_Pos : Node_Id;
714 U_To : Uint;
715 U_Val : constant Uint := UI_From_Int (Val);
717 begin
718 -- Note: do not try to optimize the case of Val = 0, because
719 -- we need to build a new node with the proper Sloc value anyway.
721 -- First test if we can do constant folding
723 if Local_Compile_Time_Known_Value (To) then
724 U_To := Local_Expr_Value (To) + Val;
726 -- Determine if our constant is outside the range of the index.
727 -- If so return an Empty node. This empty node will be caught
728 -- by Empty_Range below.
730 if Compile_Time_Known_Value (Index_Base_L)
731 and then U_To < Expr_Value (Index_Base_L)
732 then
733 return Empty;
735 elsif Compile_Time_Known_Value (Index_Base_H)
736 and then U_To > Expr_Value (Index_Base_H)
737 then
738 return Empty;
739 end if;
741 Expr_Pos := Make_Integer_Literal (Loc, U_To);
742 Set_Is_Static_Expression (Expr_Pos);
744 if not Is_Enumeration_Type (Index_Base) then
745 Expr := Expr_Pos;
747 -- If we are dealing with enumeration return
748 -- Index_Base'Val (Expr_Pos)
750 else
751 Expr :=
752 Make_Attribute_Reference
753 (Loc,
754 Prefix => Index_Base_Name,
755 Attribute_Name => Name_Val,
756 Expressions => New_List (Expr_Pos));
757 end if;
759 return Expr;
760 end if;
762 -- If we are here no constant folding possible
764 if not Is_Enumeration_Type (Index_Base) then
765 Expr :=
766 Make_Op_Add (Loc,
767 Left_Opnd => Duplicate_Subexpr (To),
768 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
770 -- If we are dealing with enumeration return
771 -- Index_Base'Val (Index_Base'Pos (To) + Val)
773 else
774 To_Pos :=
775 Make_Attribute_Reference
776 (Loc,
777 Prefix => Index_Base_Name,
778 Attribute_Name => Name_Pos,
779 Expressions => New_List (Duplicate_Subexpr (To)));
781 Expr_Pos :=
782 Make_Op_Add (Loc,
783 Left_Opnd => To_Pos,
784 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
786 Expr :=
787 Make_Attribute_Reference
788 (Loc,
789 Prefix => Index_Base_Name,
790 Attribute_Name => Name_Val,
791 Expressions => New_List (Expr_Pos));
792 end if;
794 return Expr;
795 end Add;
797 -----------------
798 -- Empty_Range --
799 -----------------
801 function Empty_Range (L, H : Node_Id) return Boolean is
802 Is_Empty : Boolean := False;
803 Low : Node_Id;
804 High : Node_Id;
806 begin
807 -- First check if L or H were already detected as overflowing the
808 -- index base range type by function Add above. If this is so Add
809 -- returns the empty node.
811 if No (L) or else No (H) then
812 return True;
813 end if;
815 for J in 1 .. 3 loop
816 case J is
818 -- L > H range is empty
820 when 1 =>
821 Low := L;
822 High := H;
824 -- B_L > H range must be empty
826 when 2 =>
827 Low := Index_Base_L;
828 High := H;
830 -- L > B_H range must be empty
832 when 3 =>
833 Low := L;
834 High := Index_Base_H;
835 end case;
837 if Local_Compile_Time_Known_Value (Low)
838 and then Local_Compile_Time_Known_Value (High)
839 then
840 Is_Empty :=
841 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
842 end if;
844 exit when Is_Empty;
845 end loop;
847 return Is_Empty;
848 end Empty_Range;
850 -----------
851 -- Equal --
852 -----------
854 function Equal (L, H : Node_Id) return Boolean is
855 begin
856 if L = H then
857 return True;
859 elsif Local_Compile_Time_Known_Value (L)
860 and then Local_Compile_Time_Known_Value (H)
861 then
862 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
863 end if;
865 return False;
866 end Equal;
868 ----------------
869 -- Gen_Assign --
870 ----------------
872 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
873 L : constant List_Id := New_List;
874 F : Entity_Id;
875 A : Node_Id;
877 New_Indices : List_Id;
878 Indexed_Comp : Node_Id;
879 Expr_Q : Node_Id;
880 Comp_Type : Entity_Id := Empty;
882 function Add_Loop_Actions (Lis : List_Id) return List_Id;
883 -- Collect insert_actions generated in the construction of a
884 -- loop, and prepend them to the sequence of assignments to
885 -- complete the eventual body of the loop.
887 ----------------------
888 -- Add_Loop_Actions --
889 ----------------------
891 function Add_Loop_Actions (Lis : List_Id) return List_Id is
892 Res : List_Id;
894 begin
895 -- Ada 2005 (AI-287): Do nothing else in case of default
896 -- initialized component.
898 if No (Expr) then
899 return Lis;
901 elsif Nkind (Parent (Expr)) = N_Component_Association
902 and then Present (Loop_Actions (Parent (Expr)))
903 then
904 Append_List (Lis, Loop_Actions (Parent (Expr)));
905 Res := Loop_Actions (Parent (Expr));
906 Set_Loop_Actions (Parent (Expr), No_List);
907 return Res;
909 else
910 return Lis;
911 end if;
912 end Add_Loop_Actions;
914 -- Start of processing for Gen_Assign
916 begin
917 if No (Indices) then
918 New_Indices := New_List;
919 else
920 New_Indices := New_Copy_List_Tree (Indices);
921 end if;
923 Append_To (New_Indices, Ind);
925 if Present (Flist) then
926 F := New_Copy_Tree (Flist);
928 elsif Present (Etype (N)) and then Controlled_Type (Etype (N)) then
929 if Is_Entity_Name (Into)
930 and then Present (Scope (Entity (Into)))
931 then
932 F := Find_Final_List (Scope (Entity (Into)));
933 else
934 F := Find_Final_List (Current_Scope);
935 end if;
936 else
937 F := Empty;
938 end if;
940 if Present (Next_Index (Index)) then
941 return
942 Add_Loop_Actions (
943 Build_Array_Aggr_Code
944 (N => Expr,
945 Ctype => Ctype,
946 Index => Next_Index (Index),
947 Into => Into,
948 Scalar_Comp => Scalar_Comp,
949 Indices => New_Indices,
950 Flist => F));
951 end if;
953 -- If we get here then we are at a bottom-level (sub-)aggregate
955 Indexed_Comp :=
956 Checks_Off
957 (Make_Indexed_Component (Loc,
958 Prefix => New_Copy_Tree (Into),
959 Expressions => New_Indices));
961 Set_Assignment_OK (Indexed_Comp);
963 -- Ada 2005 (AI-287): In case of default initialized component, Expr
964 -- is not present (and therefore we also initialize Expr_Q to empty).
966 if No (Expr) then
967 Expr_Q := Empty;
968 elsif Nkind (Expr) = N_Qualified_Expression then
969 Expr_Q := Expression (Expr);
970 else
971 Expr_Q := Expr;
972 end if;
974 if Present (Etype (N))
975 and then Etype (N) /= Any_Composite
976 then
977 Comp_Type := Component_Type (Etype (N));
978 pragma Assert (Comp_Type = Ctype); -- AI-287
980 elsif Present (Next (First (New_Indices))) then
982 -- Ada 2005 (AI-287): Do nothing in case of default initialized
983 -- component because we have received the component type in
984 -- the formal parameter Ctype.
986 -- ??? Some assert pragmas have been added to check if this new
987 -- formal can be used to replace this code in all cases.
989 if Present (Expr) then
991 -- This is a multidimensional array. Recover the component
992 -- type from the outermost aggregate, because subaggregates
993 -- do not have an assigned type.
995 declare
996 P : Node_Id;
998 begin
999 P := Parent (Expr);
1000 while Present (P) loop
1001 if Nkind (P) = N_Aggregate
1002 and then Present (Etype (P))
1003 then
1004 Comp_Type := Component_Type (Etype (P));
1005 exit;
1007 else
1008 P := Parent (P);
1009 end if;
1010 end loop;
1012 pragma Assert (Comp_Type = Ctype); -- AI-287
1013 end;
1014 end if;
1015 end if;
1017 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1018 -- default initialized components (otherwise Expr_Q is not present).
1020 if Present (Expr_Q)
1021 and then (Nkind (Expr_Q) = N_Aggregate
1022 or else Nkind (Expr_Q) = N_Extension_Aggregate)
1023 then
1024 -- At this stage the Expression may not have been
1025 -- analyzed yet because the array aggregate code has not
1026 -- been updated to use the Expansion_Delayed flag and
1027 -- avoid analysis altogether to solve the same problem
1028 -- (see Resolve_Aggr_Expr). So let us do the analysis of
1029 -- non-array aggregates now in order to get the value of
1030 -- Expansion_Delayed flag for the inner aggregate ???
1032 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1033 Analyze_And_Resolve (Expr_Q, Comp_Type);
1034 end if;
1036 if Is_Delayed_Aggregate (Expr_Q) then
1038 -- This is either a subaggregate of a multidimentional array,
1039 -- or a component of an array type whose component type is
1040 -- also an array. In the latter case, the expression may have
1041 -- component associations that provide different bounds from
1042 -- those of the component type, and sliding must occur. Instead
1043 -- of decomposing the current aggregate assignment, force the
1044 -- re-analysis of the assignment, so that a temporary will be
1045 -- generated in the usual fashion, and sliding will take place.
1047 if Nkind (Parent (N)) = N_Assignment_Statement
1048 and then Is_Array_Type (Comp_Type)
1049 and then Present (Component_Associations (Expr_Q))
1050 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1051 then
1052 Set_Expansion_Delayed (Expr_Q, False);
1053 Set_Analyzed (Expr_Q, False);
1055 else
1056 return
1057 Add_Loop_Actions (
1058 Late_Expansion (
1059 Expr_Q, Etype (Expr_Q), Indexed_Comp, F));
1060 end if;
1061 end if;
1062 end if;
1064 -- Ada 2005 (AI-287): In case of default initialized component, call
1065 -- the initialization subprogram associated with the component type.
1066 -- If the component type is an access type, add an explicit null
1067 -- assignment, because for the back-end there is an initialization
1068 -- present for the whole aggregate, and no default initialization
1069 -- will take place.
1071 -- In addition, if the component type is controlled, we must call
1072 -- its Initialize procedure explicitly, because there is no explicit
1073 -- object creation that will invoke it otherwise.
1075 if No (Expr) then
1076 if Present (Base_Init_Proc (Base_Type (Ctype)))
1077 or else Has_Task (Base_Type (Ctype))
1078 then
1079 Append_List_To (L,
1080 Build_Initialization_Call (Loc,
1081 Id_Ref => Indexed_Comp,
1082 Typ => Ctype,
1083 With_Default_Init => True));
1085 elsif Is_Access_Type (Ctype) then
1086 Append_To (L,
1087 Make_Assignment_Statement (Loc,
1088 Name => Indexed_Comp,
1089 Expression => Make_Null (Loc)));
1090 end if;
1092 if Controlled_Type (Ctype) then
1093 Append_List_To (L,
1094 Make_Init_Call (
1095 Ref => New_Copy_Tree (Indexed_Comp),
1096 Typ => Ctype,
1097 Flist_Ref => Find_Final_List (Current_Scope),
1098 With_Attach => Make_Integer_Literal (Loc, 1)));
1099 end if;
1101 else
1102 -- Now generate the assignment with no associated controlled
1103 -- actions since the target of the assignment may not have been
1104 -- initialized, it is not possible to Finalize it as expected by
1105 -- normal controlled assignment. The rest of the controlled
1106 -- actions are done manually with the proper finalization list
1107 -- coming from the context.
1109 A :=
1110 Make_OK_Assignment_Statement (Loc,
1111 Name => Indexed_Comp,
1112 Expression => New_Copy_Tree (Expr));
1114 if Present (Comp_Type) and then Controlled_Type (Comp_Type) then
1115 Set_No_Ctrl_Actions (A);
1117 -- If this is an aggregate for an array of arrays, each
1118 -- sub-aggregate will be expanded as well, and even with
1119 -- No_Ctrl_Actions the assignments of inner components will
1120 -- require attachment in their assignments to temporaries.
1121 -- These temporaries must be finalized for each subaggregate,
1122 -- to prevent multiple attachments of the same temporary
1123 -- location to same finalization chain (and consequently
1124 -- circular lists). To ensure that finalization takes place
1125 -- for each subaggregate we wrap the assignment in a block.
1127 if Is_Array_Type (Comp_Type)
1128 and then Nkind (Expr) = N_Aggregate
1129 then
1130 A :=
1131 Make_Block_Statement (Loc,
1132 Handled_Statement_Sequence =>
1133 Make_Handled_Sequence_Of_Statements (Loc,
1134 Statements => New_List (A)));
1135 end if;
1136 end if;
1138 Append_To (L, A);
1140 -- Adjust the tag if tagged (because of possible view
1141 -- conversions), unless compiling for the Java VM where
1142 -- tags are implicit.
1144 if Present (Comp_Type)
1145 and then Is_Tagged_Type (Comp_Type)
1146 and then VM_Target = No_VM
1147 then
1148 A :=
1149 Make_OK_Assignment_Statement (Loc,
1150 Name =>
1151 Make_Selected_Component (Loc,
1152 Prefix => New_Copy_Tree (Indexed_Comp),
1153 Selector_Name =>
1154 New_Reference_To
1155 (First_Tag_Component (Comp_Type), Loc)),
1157 Expression =>
1158 Unchecked_Convert_To (RTE (RE_Tag),
1159 New_Reference_To
1160 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
1161 Loc)));
1163 Append_To (L, A);
1164 end if;
1166 -- Adjust and attach the component to the proper final list, which
1167 -- can be the controller of the outer record object or the final
1168 -- list associated with the scope.
1170 -- If the component is itself an array of controlled types, whose
1171 -- value is given by a sub-aggregate, then the attach calls have
1172 -- been generated when individual subcomponent are assigned, and
1173 -- and must not be done again to prevent malformed finalization
1174 -- chains (see comments above, concerning the creation of a block
1175 -- to hold inner finalization actions).
1177 if Present (Comp_Type)
1178 and then Controlled_Type (Comp_Type)
1179 and then not Is_Limited_Type (Comp_Type)
1180 and then
1181 (not Is_Array_Type (Comp_Type)
1182 or else not Is_Controlled (Component_Type (Comp_Type))
1183 or else Nkind (Expr) /= N_Aggregate)
1184 then
1185 Append_List_To (L,
1186 Make_Adjust_Call (
1187 Ref => New_Copy_Tree (Indexed_Comp),
1188 Typ => Comp_Type,
1189 Flist_Ref => F,
1190 With_Attach => Make_Integer_Literal (Loc, 1)));
1191 end if;
1192 end if;
1194 return Add_Loop_Actions (L);
1195 end Gen_Assign;
1197 --------------
1198 -- Gen_Loop --
1199 --------------
1201 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1202 L_J : Node_Id;
1204 L_Range : Node_Id;
1205 -- Index_Base'(L) .. Index_Base'(H)
1207 L_Iteration_Scheme : Node_Id;
1208 -- L_J in Index_Base'(L) .. Index_Base'(H)
1210 L_Body : List_Id;
1211 -- The statements to execute in the loop
1213 S : constant List_Id := New_List;
1214 -- List of statements
1216 Tcopy : Node_Id;
1217 -- Copy of expression tree, used for checking purposes
1219 begin
1220 -- If loop bounds define an empty range return the null statement
1222 if Empty_Range (L, H) then
1223 Append_To (S, Make_Null_Statement (Loc));
1225 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1226 -- default initialized component.
1228 if No (Expr) then
1229 null;
1231 else
1232 -- The expression must be type-checked even though no component
1233 -- of the aggregate will have this value. This is done only for
1234 -- actual components of the array, not for subaggregates. Do
1235 -- the check on a copy, because the expression may be shared
1236 -- among several choices, some of which might be non-null.
1238 if Present (Etype (N))
1239 and then Is_Array_Type (Etype (N))
1240 and then No (Next_Index (Index))
1241 then
1242 Expander_Mode_Save_And_Set (False);
1243 Tcopy := New_Copy_Tree (Expr);
1244 Set_Parent (Tcopy, N);
1245 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1246 Expander_Mode_Restore;
1247 end if;
1248 end if;
1250 return S;
1252 -- If loop bounds are the same then generate an assignment
1254 elsif Equal (L, H) then
1255 return Gen_Assign (New_Copy_Tree (L), Expr);
1257 -- If H - L <= 2 then generate a sequence of assignments when we are
1258 -- processing the bottom most aggregate and it contains scalar
1259 -- components.
1261 elsif No (Next_Index (Index))
1262 and then Scalar_Comp
1263 and then Local_Compile_Time_Known_Value (L)
1264 and then Local_Compile_Time_Known_Value (H)
1265 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1266 then
1268 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1269 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1271 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1272 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1273 end if;
1275 return S;
1276 end if;
1278 -- Otherwise construct the loop, starting with the loop index L_J
1280 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1282 -- Construct "L .. H"
1284 L_Range :=
1285 Make_Range
1286 (Loc,
1287 Low_Bound => Make_Qualified_Expression
1288 (Loc,
1289 Subtype_Mark => Index_Base_Name,
1290 Expression => L),
1291 High_Bound => Make_Qualified_Expression
1292 (Loc,
1293 Subtype_Mark => Index_Base_Name,
1294 Expression => H));
1296 -- Construct "for L_J in Index_Base range L .. H"
1298 L_Iteration_Scheme :=
1299 Make_Iteration_Scheme
1300 (Loc,
1301 Loop_Parameter_Specification =>
1302 Make_Loop_Parameter_Specification
1303 (Loc,
1304 Defining_Identifier => L_J,
1305 Discrete_Subtype_Definition => L_Range));
1307 -- Construct the statements to execute in the loop body
1309 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1311 -- Construct the final loop
1313 Append_To (S, Make_Implicit_Loop_Statement
1314 (Node => N,
1315 Identifier => Empty,
1316 Iteration_Scheme => L_Iteration_Scheme,
1317 Statements => L_Body));
1319 -- A small optimization: if the aggregate is initialized with a box
1320 -- and the component type has no initialization procedure, remove the
1321 -- useless empty loop.
1323 if Nkind (First (S)) = N_Loop_Statement
1324 and then Is_Empty_List (Statements (First (S)))
1325 then
1326 return New_List (Make_Null_Statement (Loc));
1327 else
1328 return S;
1329 end if;
1330 end Gen_Loop;
1332 ---------------
1333 -- Gen_While --
1334 ---------------
1336 -- The code built is
1338 -- W_J : Index_Base := L;
1339 -- while W_J < H loop
1340 -- W_J := Index_Base'Succ (W);
1341 -- L_Body;
1342 -- end loop;
1344 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1345 W_J : Node_Id;
1347 W_Decl : Node_Id;
1348 -- W_J : Base_Type := L;
1350 W_Iteration_Scheme : Node_Id;
1351 -- while W_J < H
1353 W_Index_Succ : Node_Id;
1354 -- Index_Base'Succ (J)
1356 W_Increment : Node_Id;
1357 -- W_J := Index_Base'Succ (W)
1359 W_Body : constant List_Id := New_List;
1360 -- The statements to execute in the loop
1362 S : constant List_Id := New_List;
1363 -- list of statement
1365 begin
1366 -- If loop bounds define an empty range or are equal return null
1368 if Empty_Range (L, H) or else Equal (L, H) then
1369 Append_To (S, Make_Null_Statement (Loc));
1370 return S;
1371 end if;
1373 -- Build the decl of W_J
1375 W_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
1376 W_Decl :=
1377 Make_Object_Declaration
1378 (Loc,
1379 Defining_Identifier => W_J,
1380 Object_Definition => Index_Base_Name,
1381 Expression => L);
1383 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1384 -- that in this particular case L is a fresh Expr generated by
1385 -- Add which we are the only ones to use.
1387 Append_To (S, W_Decl);
1389 -- Construct " while W_J < H"
1391 W_Iteration_Scheme :=
1392 Make_Iteration_Scheme
1393 (Loc,
1394 Condition => Make_Op_Lt
1395 (Loc,
1396 Left_Opnd => New_Reference_To (W_J, Loc),
1397 Right_Opnd => New_Copy_Tree (H)));
1399 -- Construct the statements to execute in the loop body
1401 W_Index_Succ :=
1402 Make_Attribute_Reference
1403 (Loc,
1404 Prefix => Index_Base_Name,
1405 Attribute_Name => Name_Succ,
1406 Expressions => New_List (New_Reference_To (W_J, Loc)));
1408 W_Increment :=
1409 Make_OK_Assignment_Statement
1410 (Loc,
1411 Name => New_Reference_To (W_J, Loc),
1412 Expression => W_Index_Succ);
1414 Append_To (W_Body, W_Increment);
1415 Append_List_To (W_Body,
1416 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1418 -- Construct the final loop
1420 Append_To (S, Make_Implicit_Loop_Statement
1421 (Node => N,
1422 Identifier => Empty,
1423 Iteration_Scheme => W_Iteration_Scheme,
1424 Statements => W_Body));
1426 return S;
1427 end Gen_While;
1429 ---------------------
1430 -- Index_Base_Name --
1431 ---------------------
1433 function Index_Base_Name return Node_Id is
1434 begin
1435 return New_Reference_To (Index_Base, Sloc (N));
1436 end Index_Base_Name;
1438 ------------------------------------
1439 -- Local_Compile_Time_Known_Value --
1440 ------------------------------------
1442 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1443 begin
1444 return Compile_Time_Known_Value (E)
1445 or else
1446 (Nkind (E) = N_Attribute_Reference
1447 and then Attribute_Name (E) = Name_Val
1448 and then Compile_Time_Known_Value (First (Expressions (E))));
1449 end Local_Compile_Time_Known_Value;
1451 ----------------------
1452 -- Local_Expr_Value --
1453 ----------------------
1455 function Local_Expr_Value (E : Node_Id) return Uint is
1456 begin
1457 if Compile_Time_Known_Value (E) then
1458 return Expr_Value (E);
1459 else
1460 return Expr_Value (First (Expressions (E)));
1461 end if;
1462 end Local_Expr_Value;
1464 -- Build_Array_Aggr_Code Variables
1466 Assoc : Node_Id;
1467 Choice : Node_Id;
1468 Expr : Node_Id;
1469 Typ : Entity_Id;
1471 Others_Expr : Node_Id := Empty;
1472 Others_Box_Present : Boolean := False;
1474 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1475 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1476 -- The aggregate bounds of this specific sub-aggregate. Note that if
1477 -- the code generated by Build_Array_Aggr_Code is executed then these
1478 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1480 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1481 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1482 -- After Duplicate_Subexpr these are side-effect free
1484 Low : Node_Id;
1485 High : Node_Id;
1487 Nb_Choices : Nat := 0;
1488 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1489 -- Used to sort all the different choice values
1491 Nb_Elements : Int;
1492 -- Number of elements in the positional aggregate
1494 New_Code : constant List_Id := New_List;
1496 -- Start of processing for Build_Array_Aggr_Code
1498 begin
1499 -- First before we start, a special case. if we have a bit packed
1500 -- array represented as a modular type, then clear the value to
1501 -- zero first, to ensure that unused bits are properly cleared.
1503 Typ := Etype (N);
1505 if Present (Typ)
1506 and then Is_Bit_Packed_Array (Typ)
1507 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1508 then
1509 Append_To (New_Code,
1510 Make_Assignment_Statement (Loc,
1511 Name => New_Copy_Tree (Into),
1512 Expression =>
1513 Unchecked_Convert_To (Typ,
1514 Make_Integer_Literal (Loc, Uint_0))));
1515 end if;
1517 -- STEP 1: Process component associations
1519 -- For those associations that may generate a loop, initialize
1520 -- Loop_Actions to collect inserted actions that may be crated.
1522 -- Skip this if no component associations
1524 if No (Expressions (N)) then
1526 -- STEP 1 (a): Sort the discrete choices
1528 Assoc := First (Component_Associations (N));
1529 while Present (Assoc) loop
1530 Choice := First (Choices (Assoc));
1531 while Present (Choice) loop
1532 if Nkind (Choice) = N_Others_Choice then
1533 Set_Loop_Actions (Assoc, New_List);
1535 if Box_Present (Assoc) then
1536 Others_Box_Present := True;
1537 else
1538 Others_Expr := Expression (Assoc);
1539 end if;
1540 exit;
1541 end if;
1543 Get_Index_Bounds (Choice, Low, High);
1545 if Low /= High then
1546 Set_Loop_Actions (Assoc, New_List);
1547 end if;
1549 Nb_Choices := Nb_Choices + 1;
1550 if Box_Present (Assoc) then
1551 Table (Nb_Choices) := (Choice_Lo => Low,
1552 Choice_Hi => High,
1553 Choice_Node => Empty);
1554 else
1555 Table (Nb_Choices) := (Choice_Lo => Low,
1556 Choice_Hi => High,
1557 Choice_Node => Expression (Assoc));
1558 end if;
1559 Next (Choice);
1560 end loop;
1562 Next (Assoc);
1563 end loop;
1565 -- If there is more than one set of choices these must be static
1566 -- and we can therefore sort them. Remember that Nb_Choices does not
1567 -- account for an others choice.
1569 if Nb_Choices > 1 then
1570 Sort_Case_Table (Table);
1571 end if;
1573 -- STEP 1 (b): take care of the whole set of discrete choices
1575 for J in 1 .. Nb_Choices loop
1576 Low := Table (J).Choice_Lo;
1577 High := Table (J).Choice_Hi;
1578 Expr := Table (J).Choice_Node;
1579 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1580 end loop;
1582 -- STEP 1 (c): generate the remaining loops to cover others choice
1583 -- We don't need to generate loops over empty gaps, but if there is
1584 -- a single empty range we must analyze the expression for semantics
1586 if Present (Others_Expr) or else Others_Box_Present then
1587 declare
1588 First : Boolean := True;
1590 begin
1591 for J in 0 .. Nb_Choices loop
1592 if J = 0 then
1593 Low := Aggr_Low;
1594 else
1595 Low := Add (1, To => Table (J).Choice_Hi);
1596 end if;
1598 if J = Nb_Choices then
1599 High := Aggr_High;
1600 else
1601 High := Add (-1, To => Table (J + 1).Choice_Lo);
1602 end if;
1604 -- If this is an expansion within an init proc, make
1605 -- sure that discriminant references are replaced by
1606 -- the corresponding discriminal.
1608 if Inside_Init_Proc then
1609 if Is_Entity_Name (Low)
1610 and then Ekind (Entity (Low)) = E_Discriminant
1611 then
1612 Set_Entity (Low, Discriminal (Entity (Low)));
1613 end if;
1615 if Is_Entity_Name (High)
1616 and then Ekind (Entity (High)) = E_Discriminant
1617 then
1618 Set_Entity (High, Discriminal (Entity (High)));
1619 end if;
1620 end if;
1622 if First
1623 or else not Empty_Range (Low, High)
1624 then
1625 First := False;
1626 Append_List
1627 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1628 end if;
1629 end loop;
1630 end;
1631 end if;
1633 -- STEP 2: Process positional components
1635 else
1636 -- STEP 2 (a): Generate the assignments for each positional element
1637 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1638 -- Aggr_L is analyzed and Add wants an analyzed expression.
1640 Expr := First (Expressions (N));
1641 Nb_Elements := -1;
1642 while Present (Expr) loop
1643 Nb_Elements := Nb_Elements + 1;
1644 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1645 To => New_Code);
1646 Next (Expr);
1647 end loop;
1649 -- STEP 2 (b): Generate final loop if an others choice is present
1650 -- Here Nb_Elements gives the offset of the last positional element.
1652 if Present (Component_Associations (N)) then
1653 Assoc := Last (Component_Associations (N));
1655 -- Ada 2005 (AI-287)
1657 if Box_Present (Assoc) then
1658 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1659 Aggr_High,
1660 Empty),
1661 To => New_Code);
1662 else
1663 Expr := Expression (Assoc);
1665 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1666 Aggr_High,
1667 Expr), -- AI-287
1668 To => New_Code);
1669 end if;
1670 end if;
1671 end if;
1673 return New_Code;
1674 end Build_Array_Aggr_Code;
1676 ----------------------------
1677 -- Build_Record_Aggr_Code --
1678 ----------------------------
1680 ----------------------------
1681 -- Build_Record_Aggr_Code --
1682 ----------------------------
1684 function Build_Record_Aggr_Code
1685 (N : Node_Id;
1686 Typ : Entity_Id;
1687 Lhs : Node_Id;
1688 Flist : Node_Id := Empty;
1689 Obj : Entity_Id := Empty;
1690 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1692 Loc : constant Source_Ptr := Sloc (N);
1693 L : constant List_Id := New_List;
1694 N_Typ : constant Entity_Id := Etype (N);
1696 Comp : Node_Id;
1697 Instr : Node_Id;
1698 Ref : Node_Id;
1699 Target : Entity_Id;
1700 F : Node_Id;
1701 Comp_Type : Entity_Id;
1702 Selector : Entity_Id;
1703 Comp_Expr : Node_Id;
1704 Expr_Q : Node_Id;
1706 Internal_Final_List : Node_Id := Empty;
1708 -- If this is an internal aggregate, the External_Final_List is an
1709 -- expression for the controller record of the enclosing type.
1711 -- If the current aggregate has several controlled components, this
1712 -- expression will appear in several calls to attach to the finali-
1713 -- zation list, and it must not be shared.
1715 External_Final_List : Node_Id;
1716 Ancestor_Is_Expression : Boolean := False;
1717 Ancestor_Is_Subtype_Mark : Boolean := False;
1719 Init_Typ : Entity_Id := Empty;
1720 Attach : Node_Id;
1722 Ctrl_Stuff_Done : Boolean := False;
1723 -- True if Gen_Ctrl_Actions_For_Aggr has already been called; calls
1724 -- after the first do nothing.
1726 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1727 -- Returns the value that the given discriminant of an ancestor type
1728 -- should receive (in the absence of a conflict with the value provided
1729 -- by an ancestor part of an extension aggregate).
1731 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1732 -- Check that each of the discriminant values defined by the ancestor
1733 -- part of an extension aggregate match the corresponding values
1734 -- provided by either an association of the aggregate or by the
1735 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1737 function Compatible_Int_Bounds
1738 (Agg_Bounds : Node_Id;
1739 Typ_Bounds : Node_Id) return Boolean;
1740 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1741 -- assumed that both bounds are integer ranges.
1743 procedure Gen_Ctrl_Actions_For_Aggr;
1744 -- Deal with the various controlled type data structure initializations
1745 -- (but only if it hasn't been done already).
1747 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1748 -- Returns the first discriminant association in the constraint
1749 -- associated with T, if any, otherwise returns Empty.
1751 function Init_Controller
1752 (Target : Node_Id;
1753 Typ : Entity_Id;
1754 F : Node_Id;
1755 Attach : Node_Id;
1756 Init_Pr : Boolean) return List_Id;
1757 -- Returns the list of statements necessary to initialize the internal
1758 -- controller of the (possible) ancestor typ into target and attach it
1759 -- to finalization list F. Init_Pr conditions the call to the init proc
1760 -- since it may already be done due to ancestor initialization.
1762 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1763 -- Check whether Bounds is a range node and its lower and higher bounds
1764 -- are integers literals.
1766 ---------------------------------
1767 -- Ancestor_Discriminant_Value --
1768 ---------------------------------
1770 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1771 Assoc : Node_Id;
1772 Assoc_Elmt : Elmt_Id;
1773 Aggr_Comp : Entity_Id;
1774 Corresp_Disc : Entity_Id;
1775 Current_Typ : Entity_Id := Base_Type (Typ);
1776 Parent_Typ : Entity_Id;
1777 Parent_Disc : Entity_Id;
1778 Save_Assoc : Node_Id := Empty;
1780 begin
1781 -- First check any discriminant associations to see if any of them
1782 -- provide a value for the discriminant.
1784 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1785 Assoc := First (Component_Associations (N));
1786 while Present (Assoc) loop
1787 Aggr_Comp := Entity (First (Choices (Assoc)));
1789 if Ekind (Aggr_Comp) = E_Discriminant then
1790 Save_Assoc := Expression (Assoc);
1792 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1793 while Present (Corresp_Disc) loop
1795 -- If found a corresponding discriminant then return the
1796 -- value given in the aggregate. (Note: this is not
1797 -- correct in the presence of side effects. ???)
1799 if Disc = Corresp_Disc then
1800 return Duplicate_Subexpr (Expression (Assoc));
1801 end if;
1803 Corresp_Disc :=
1804 Corresponding_Discriminant (Corresp_Disc);
1805 end loop;
1806 end if;
1808 Next (Assoc);
1809 end loop;
1810 end if;
1812 -- No match found in aggregate, so chain up parent types to find
1813 -- a constraint that defines the value of the discriminant.
1815 Parent_Typ := Etype (Current_Typ);
1816 while Current_Typ /= Parent_Typ loop
1817 if Has_Discriminants (Parent_Typ) then
1818 Parent_Disc := First_Discriminant (Parent_Typ);
1820 -- We either get the association from the subtype indication
1821 -- of the type definition itself, or from the discriminant
1822 -- constraint associated with the type entity (which is
1823 -- preferable, but it's not always present ???)
1825 if Is_Empty_Elmt_List (
1826 Discriminant_Constraint (Current_Typ))
1827 then
1828 Assoc := Get_Constraint_Association (Current_Typ);
1829 Assoc_Elmt := No_Elmt;
1830 else
1831 Assoc_Elmt :=
1832 First_Elmt (Discriminant_Constraint (Current_Typ));
1833 Assoc := Node (Assoc_Elmt);
1834 end if;
1836 -- Traverse the discriminants of the parent type looking
1837 -- for one that corresponds.
1839 while Present (Parent_Disc) and then Present (Assoc) loop
1840 Corresp_Disc := Parent_Disc;
1841 while Present (Corresp_Disc)
1842 and then Disc /= Corresp_Disc
1843 loop
1844 Corresp_Disc :=
1845 Corresponding_Discriminant (Corresp_Disc);
1846 end loop;
1848 if Disc = Corresp_Disc then
1849 if Nkind (Assoc) = N_Discriminant_Association then
1850 Assoc := Expression (Assoc);
1851 end if;
1853 -- If the located association directly denotes a
1854 -- discriminant, then use the value of a saved
1855 -- association of the aggregate. This is a kludge to
1856 -- handle certain cases involving multiple discriminants
1857 -- mapped to a single discriminant of a descendant. It's
1858 -- not clear how to locate the appropriate discriminant
1859 -- value for such cases. ???
1861 if Is_Entity_Name (Assoc)
1862 and then Ekind (Entity (Assoc)) = E_Discriminant
1863 then
1864 Assoc := Save_Assoc;
1865 end if;
1867 return Duplicate_Subexpr (Assoc);
1868 end if;
1870 Next_Discriminant (Parent_Disc);
1872 if No (Assoc_Elmt) then
1873 Next (Assoc);
1874 else
1875 Next_Elmt (Assoc_Elmt);
1876 if Present (Assoc_Elmt) then
1877 Assoc := Node (Assoc_Elmt);
1878 else
1879 Assoc := Empty;
1880 end if;
1881 end if;
1882 end loop;
1883 end if;
1885 Current_Typ := Parent_Typ;
1886 Parent_Typ := Etype (Current_Typ);
1887 end loop;
1889 -- In some cases there's no ancestor value to locate (such as
1890 -- when an ancestor part given by an expression defines the
1891 -- discriminant value).
1893 return Empty;
1894 end Ancestor_Discriminant_Value;
1896 ----------------------------------
1897 -- Check_Ancestor_Discriminants --
1898 ----------------------------------
1900 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1901 Discr : Entity_Id;
1902 Disc_Value : Node_Id;
1903 Cond : Node_Id;
1905 begin
1906 Discr := First_Discriminant (Base_Type (Anc_Typ));
1907 while Present (Discr) loop
1908 Disc_Value := Ancestor_Discriminant_Value (Discr);
1910 if Present (Disc_Value) then
1911 Cond := Make_Op_Ne (Loc,
1912 Left_Opnd =>
1913 Make_Selected_Component (Loc,
1914 Prefix => New_Copy_Tree (Target),
1915 Selector_Name => New_Occurrence_Of (Discr, Loc)),
1916 Right_Opnd => Disc_Value);
1918 Append_To (L,
1919 Make_Raise_Constraint_Error (Loc,
1920 Condition => Cond,
1921 Reason => CE_Discriminant_Check_Failed));
1922 end if;
1924 Next_Discriminant (Discr);
1925 end loop;
1926 end Check_Ancestor_Discriminants;
1928 ---------------------------
1929 -- Compatible_Int_Bounds --
1930 ---------------------------
1932 function Compatible_Int_Bounds
1933 (Agg_Bounds : Node_Id;
1934 Typ_Bounds : Node_Id) return Boolean
1936 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
1937 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
1938 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
1939 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
1940 begin
1941 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
1942 end Compatible_Int_Bounds;
1944 --------------------------------
1945 -- Get_Constraint_Association --
1946 --------------------------------
1948 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
1949 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
1950 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
1952 begin
1953 -- ??? Also need to cover case of a type mark denoting a subtype
1954 -- with constraint.
1956 if Nkind (Indic) = N_Subtype_Indication
1957 and then Present (Constraint (Indic))
1958 then
1959 return First (Constraints (Constraint (Indic)));
1960 end if;
1962 return Empty;
1963 end Get_Constraint_Association;
1965 ---------------------
1966 -- Init_Controller --
1967 ---------------------
1969 function Init_Controller
1970 (Target : Node_Id;
1971 Typ : Entity_Id;
1972 F : Node_Id;
1973 Attach : Node_Id;
1974 Init_Pr : Boolean) return List_Id
1976 L : constant List_Id := New_List;
1977 Ref : Node_Id;
1978 RC : RE_Id;
1979 Target_Type : Entity_Id;
1981 begin
1982 -- Generate:
1983 -- init-proc (target._controller);
1984 -- initialize (target._controller);
1985 -- Attach_to_Final_List (target._controller, F);
1987 Ref :=
1988 Make_Selected_Component (Loc,
1989 Prefix => Convert_To (Typ, New_Copy_Tree (Target)),
1990 Selector_Name => Make_Identifier (Loc, Name_uController));
1991 Set_Assignment_OK (Ref);
1993 -- Ada 2005 (AI-287): Give support to aggregates of limited
1994 -- types. If the type is intrinsically_limited the controller
1995 -- is limited as well. If it is tagged and limited then so is
1996 -- the controller. Otherwise an untagged type may have limited
1997 -- components without its full view being limited, so the
1998 -- controller is not limited.
2000 if Nkind (Target) = N_Identifier then
2001 Target_Type := Etype (Target);
2003 elsif Nkind (Target) = N_Selected_Component then
2004 Target_Type := Etype (Selector_Name (Target));
2006 elsif Nkind (Target) = N_Unchecked_Type_Conversion then
2007 Target_Type := Etype (Target);
2009 elsif Nkind (Target) = N_Unchecked_Expression
2010 and then Nkind (Expression (Target)) = N_Indexed_Component
2011 then
2012 Target_Type := Etype (Prefix (Expression (Target)));
2014 else
2015 Target_Type := Etype (Target);
2016 end if;
2018 -- If the target has not been analyzed yet, as will happen with
2019 -- delayed expansion, use the given type (either the aggregate
2020 -- type or an ancestor) to determine limitedness.
2022 if No (Target_Type) then
2023 Target_Type := Typ;
2024 end if;
2026 if (Is_Tagged_Type (Target_Type))
2027 and then Is_Limited_Type (Target_Type)
2028 then
2029 RC := RE_Limited_Record_Controller;
2031 elsif Is_Inherently_Limited_Type (Target_Type) then
2032 RC := RE_Limited_Record_Controller;
2034 else
2035 RC := RE_Record_Controller;
2036 end if;
2038 if Init_Pr then
2039 Append_List_To (L,
2040 Build_Initialization_Call (Loc,
2041 Id_Ref => Ref,
2042 Typ => RTE (RC),
2043 In_Init_Proc => Within_Init_Proc));
2044 end if;
2046 Append_To (L,
2047 Make_Procedure_Call_Statement (Loc,
2048 Name =>
2049 New_Reference_To (
2050 Find_Prim_Op (RTE (RC), Name_Initialize), Loc),
2051 Parameter_Associations =>
2052 New_List (New_Copy_Tree (Ref))));
2054 Append_To (L,
2055 Make_Attach_Call (
2056 Obj_Ref => New_Copy_Tree (Ref),
2057 Flist_Ref => F,
2058 With_Attach => Attach));
2060 return L;
2061 end Init_Controller;
2063 -------------------------
2064 -- Is_Int_Range_Bounds --
2065 -------------------------
2067 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2068 begin
2069 return Nkind (Bounds) = N_Range
2070 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2071 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2072 end Is_Int_Range_Bounds;
2074 -------------------------------
2075 -- Gen_Ctrl_Actions_For_Aggr --
2076 -------------------------------
2078 procedure Gen_Ctrl_Actions_For_Aggr is
2079 Alloc : Node_Id := Empty;
2081 begin
2082 -- Do the work only the first time this is called
2084 if Ctrl_Stuff_Done then
2085 return;
2086 end if;
2088 Ctrl_Stuff_Done := True;
2090 if Present (Obj)
2091 and then Finalize_Storage_Only (Typ)
2092 and then
2093 (Is_Library_Level_Entity (Obj)
2094 or else Entity (Constant_Value (RTE (RE_Garbage_Collected))) =
2095 Standard_True)
2097 -- why not Is_True (Expr_Value (RTE (RE_Garbaage_Collected) ???
2098 then
2099 Attach := Make_Integer_Literal (Loc, 0);
2101 elsif Nkind (Parent (N)) = N_Qualified_Expression
2102 and then Nkind (Parent (Parent (N))) = N_Allocator
2103 then
2104 Alloc := Parent (Parent (N));
2105 Attach := Make_Integer_Literal (Loc, 2);
2107 else
2108 Attach := Make_Integer_Literal (Loc, 1);
2109 end if;
2111 -- Determine the external finalization list. It is either the
2112 -- finalization list of the outer-scope or the one coming from
2113 -- an outer aggregate. When the target is not a temporary, the
2114 -- proper scope is the scope of the target rather than the
2115 -- potentially transient current scope.
2117 if Controlled_Type (Typ) then
2119 -- The current aggregate belongs to an allocator which creates
2120 -- an object through an anonymous access type or acts as the root
2121 -- of a coextension chain.
2123 if Present (Alloc)
2124 and then
2125 (Is_Coextension_Root (Alloc)
2126 or else Ekind (Etype (Alloc)) = E_Anonymous_Access_Type)
2127 then
2128 if No (Associated_Final_Chain (Etype (Alloc))) then
2129 Build_Final_List (Alloc, Etype (Alloc));
2130 end if;
2132 External_Final_List :=
2133 Make_Selected_Component (Loc,
2134 Prefix =>
2135 New_Reference_To (
2136 Associated_Final_Chain (Etype (Alloc)), Loc),
2137 Selector_Name =>
2138 Make_Identifier (Loc, Name_F));
2140 elsif Present (Flist) then
2141 External_Final_List := New_Copy_Tree (Flist);
2143 elsif Is_Entity_Name (Target)
2144 and then Present (Scope (Entity (Target)))
2145 then
2146 External_Final_List :=
2147 Find_Final_List (Scope (Entity (Target)));
2149 else
2150 External_Final_List := Find_Final_List (Current_Scope);
2151 end if;
2152 else
2153 External_Final_List := Empty;
2154 end if;
2156 -- Initialize and attach the outer object in the is_controlled case
2158 if Is_Controlled (Typ) then
2159 if Ancestor_Is_Subtype_Mark then
2160 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2161 Set_Assignment_OK (Ref);
2162 Append_To (L,
2163 Make_Procedure_Call_Statement (Loc,
2164 Name =>
2165 New_Reference_To
2166 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2167 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2168 end if;
2170 if not Has_Controlled_Component (Typ) then
2171 Ref := New_Copy_Tree (Target);
2172 Set_Assignment_OK (Ref);
2174 -- This is an aggregate of a coextension. Do not produce a
2175 -- finalization call, but rather attach the reference of the
2176 -- aggregate to its coextension chain.
2178 if Present (Alloc)
2179 and then Is_Dynamic_Coextension (Alloc)
2180 then
2181 if No (Coextensions (Alloc)) then
2182 Set_Coextensions (Alloc, New_Elmt_List);
2183 end if;
2185 Append_Elmt (Ref, Coextensions (Alloc));
2186 else
2187 Append_To (L,
2188 Make_Attach_Call (
2189 Obj_Ref => Ref,
2190 Flist_Ref => New_Copy_Tree (External_Final_List),
2191 With_Attach => Attach));
2192 end if;
2193 end if;
2194 end if;
2196 -- In the Has_Controlled component case, all the intermediate
2197 -- controllers must be initialized.
2199 if Has_Controlled_Component (Typ)
2200 and not Is_Limited_Ancestor_Expansion
2201 then
2202 declare
2203 Inner_Typ : Entity_Id;
2204 Outer_Typ : Entity_Id;
2205 At_Root : Boolean;
2207 begin
2208 -- Find outer type with a controller
2210 Outer_Typ := Base_Type (Typ);
2211 while Outer_Typ /= Init_Typ
2212 and then not Has_New_Controlled_Component (Outer_Typ)
2213 loop
2214 Outer_Typ := Etype (Outer_Typ);
2215 end loop;
2217 -- Attach it to the outer record controller to the
2218 -- external final list
2220 if Outer_Typ = Init_Typ then
2221 Append_List_To (L,
2222 Init_Controller (
2223 Target => Target,
2224 Typ => Outer_Typ,
2225 F => External_Final_List,
2226 Attach => Attach,
2227 Init_Pr => False));
2229 At_Root := True;
2230 Inner_Typ := Init_Typ;
2232 else
2233 Append_List_To (L,
2234 Init_Controller (
2235 Target => Target,
2236 Typ => Outer_Typ,
2237 F => External_Final_List,
2238 Attach => Attach,
2239 Init_Pr => True));
2241 Inner_Typ := Etype (Outer_Typ);
2242 At_Root :=
2243 not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ;
2244 end if;
2246 -- The outer object has to be attached as well
2248 if Is_Controlled (Typ) then
2249 Ref := New_Copy_Tree (Target);
2250 Set_Assignment_OK (Ref);
2251 Append_To (L,
2252 Make_Attach_Call (
2253 Obj_Ref => Ref,
2254 Flist_Ref => New_Copy_Tree (External_Final_List),
2255 With_Attach => New_Copy_Tree (Attach)));
2256 end if;
2258 -- Initialize the internal controllers for tagged types with
2259 -- more than one controller.
2261 while not At_Root and then Inner_Typ /= Init_Typ loop
2262 if Has_New_Controlled_Component (Inner_Typ) then
2263 F :=
2264 Make_Selected_Component (Loc,
2265 Prefix =>
2266 Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2267 Selector_Name =>
2268 Make_Identifier (Loc, Name_uController));
2269 F :=
2270 Make_Selected_Component (Loc,
2271 Prefix => F,
2272 Selector_Name => Make_Identifier (Loc, Name_F));
2274 Append_List_To (L,
2275 Init_Controller (
2276 Target => Target,
2277 Typ => Inner_Typ,
2278 F => F,
2279 Attach => Make_Integer_Literal (Loc, 1),
2280 Init_Pr => True));
2281 Outer_Typ := Inner_Typ;
2282 end if;
2284 -- Stop at the root
2286 At_Root := Inner_Typ = Etype (Inner_Typ);
2287 Inner_Typ := Etype (Inner_Typ);
2288 end loop;
2290 -- If not done yet attach the controller of the ancestor part
2292 if Outer_Typ /= Init_Typ
2293 and then Inner_Typ = Init_Typ
2294 and then Has_Controlled_Component (Init_Typ)
2295 then
2296 F :=
2297 Make_Selected_Component (Loc,
2298 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2299 Selector_Name =>
2300 Make_Identifier (Loc, Name_uController));
2301 F :=
2302 Make_Selected_Component (Loc,
2303 Prefix => F,
2304 Selector_Name => Make_Identifier (Loc, Name_F));
2306 Attach := Make_Integer_Literal (Loc, 1);
2307 Append_List_To (L,
2308 Init_Controller (
2309 Target => Target,
2310 Typ => Init_Typ,
2311 F => F,
2312 Attach => Attach,
2313 Init_Pr => False));
2315 -- Note: Init_Pr is False because the ancestor part has
2316 -- already been initialized either way (by default, if
2317 -- given by a type name, otherwise from the expression).
2319 end if;
2320 end;
2321 end if;
2322 end Gen_Ctrl_Actions_For_Aggr;
2324 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2325 -- If the aggregate contains a self-reference, traverse each
2326 -- expression to replace a possible self-reference with a reference
2327 -- to the proper component of the target of the assignment.
2329 ------------------
2330 -- Replace_Type --
2331 ------------------
2333 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2334 begin
2335 if Nkind (Expr) = N_Attribute_Reference
2336 and then Is_Entity_Name (Prefix (Expr))
2337 and then Is_Type (Entity (Prefix (Expr)))
2338 then
2339 if Is_Entity_Name (Lhs) then
2340 Rewrite (Prefix (Expr),
2341 New_Occurrence_Of (Entity (Lhs), Loc));
2343 elsif Nkind (Lhs) = N_Selected_Component then
2344 Rewrite (Expr,
2345 Make_Attribute_Reference (Loc,
2346 Attribute_Name => Name_Unrestricted_Access,
2347 Prefix => New_Copy_Tree (Prefix (Lhs))));
2348 Set_Analyzed (Parent (Expr), False);
2350 else
2351 Rewrite (Expr,
2352 Make_Attribute_Reference (Loc,
2353 Attribute_Name => Name_Unrestricted_Access,
2354 Prefix => New_Copy_Tree (Lhs)));
2355 Set_Analyzed (Parent (Expr), False);
2356 end if;
2357 end if;
2359 return OK;
2360 end Replace_Type;
2362 procedure Replace_Self_Reference is
2363 new Traverse_Proc (Replace_Type);
2365 -- Start of processing for Build_Record_Aggr_Code
2367 begin
2368 if Has_Self_Reference (N) then
2369 Replace_Self_Reference (N);
2370 end if;
2372 -- If the target of the aggregate is class-wide, we must convert it
2373 -- to the actual type of the aggregate, so that the proper components
2374 -- are visible. We know already that the types are compatible.
2376 if Present (Etype (Lhs))
2377 and then Is_Interface (Etype (Lhs))
2378 then
2379 Target := Unchecked_Convert_To (Typ, Lhs);
2380 else
2381 Target := Lhs;
2382 end if;
2384 -- Deal with the ancestor part of extension aggregates or with the
2385 -- discriminants of the root type.
2387 if Nkind (N) = N_Extension_Aggregate then
2388 declare
2389 A : constant Node_Id := Ancestor_Part (N);
2390 Assign : List_Id;
2392 begin
2393 -- If the ancestor part is a subtype mark "T", we generate
2395 -- init-proc (T(tmp)); if T is constrained and
2396 -- init-proc (S(tmp)); where S applies an appropriate
2397 -- constraint if T is unconstrained
2399 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2400 Ancestor_Is_Subtype_Mark := True;
2402 if Is_Constrained (Entity (A)) then
2403 Init_Typ := Entity (A);
2405 -- For an ancestor part given by an unconstrained type mark,
2406 -- create a subtype constrained by appropriate corresponding
2407 -- discriminant values coming from either associations of the
2408 -- aggregate or a constraint on a parent type. The subtype will
2409 -- be used to generate the correct default value for the
2410 -- ancestor part.
2412 elsif Has_Discriminants (Entity (A)) then
2413 declare
2414 Anc_Typ : constant Entity_Id := Entity (A);
2415 Anc_Constr : constant List_Id := New_List;
2416 Discrim : Entity_Id;
2417 Disc_Value : Node_Id;
2418 New_Indic : Node_Id;
2419 Subt_Decl : Node_Id;
2421 begin
2422 Discrim := First_Discriminant (Anc_Typ);
2423 while Present (Discrim) loop
2424 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2425 Append_To (Anc_Constr, Disc_Value);
2426 Next_Discriminant (Discrim);
2427 end loop;
2429 New_Indic :=
2430 Make_Subtype_Indication (Loc,
2431 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2432 Constraint =>
2433 Make_Index_Or_Discriminant_Constraint (Loc,
2434 Constraints => Anc_Constr));
2436 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2438 Subt_Decl :=
2439 Make_Subtype_Declaration (Loc,
2440 Defining_Identifier => Init_Typ,
2441 Subtype_Indication => New_Indic);
2443 -- Itypes must be analyzed with checks off Declaration
2444 -- must have a parent for proper handling of subsidiary
2445 -- actions.
2447 Set_Parent (Subt_Decl, N);
2448 Analyze (Subt_Decl, Suppress => All_Checks);
2449 end;
2450 end if;
2452 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2453 Set_Assignment_OK (Ref);
2455 if Has_Default_Init_Comps (N)
2456 or else Has_Task (Base_Type (Init_Typ))
2457 then
2458 Append_List_To (L,
2459 Build_Initialization_Call (Loc,
2460 Id_Ref => Ref,
2461 Typ => Init_Typ,
2462 In_Init_Proc => Within_Init_Proc,
2463 With_Default_Init => True));
2464 else
2465 Append_List_To (L,
2466 Build_Initialization_Call (Loc,
2467 Id_Ref => Ref,
2468 Typ => Init_Typ,
2469 In_Init_Proc => Within_Init_Proc));
2470 end if;
2472 if Is_Constrained (Entity (A))
2473 and then Has_Discriminants (Entity (A))
2474 then
2475 Check_Ancestor_Discriminants (Entity (A));
2476 end if;
2478 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2479 -- limited type, a recursive call expands the ancestor. Note that
2480 -- in the limited case, the ancestor part must be either a
2481 -- function call (possibly qualified, or wrapped in an unchecked
2482 -- conversion) or aggregate (definitely qualified).
2484 elsif Is_Limited_Type (Etype (A))
2485 and then Nkind (Unqualify (A)) /= N_Function_Call -- aggregate?
2486 and then
2487 (Nkind (Unqualify (A)) /= N_Unchecked_Type_Conversion
2488 or else
2489 Nkind (Expression (Unqualify (A))) /= N_Function_Call)
2490 then
2491 Ancestor_Is_Expression := True;
2493 -- Set up finalization data for enclosing record, because
2494 -- controlled subcomponents of the ancestor part will be
2495 -- attached to it.
2497 Gen_Ctrl_Actions_For_Aggr;
2499 Append_List_To (L,
2500 Build_Record_Aggr_Code (
2501 N => Unqualify (A),
2502 Typ => Etype (Unqualify (A)),
2503 Lhs => Target,
2504 Flist => Flist,
2505 Obj => Obj,
2506 Is_Limited_Ancestor_Expansion => True));
2508 -- If the ancestor part is an expression "E", we generate
2510 -- T(tmp) := E;
2512 -- In Ada 2005, this includes the case of a (possibly qualified)
2513 -- limited function call. The assignment will turn into a
2514 -- build-in-place function call (for further details, see
2515 -- Make_Build_In_Place_Call_In_Assignment).
2517 else
2518 Ancestor_Is_Expression := True;
2519 Init_Typ := Etype (A);
2521 -- If the ancestor part is an aggregate, force its full
2522 -- expansion, which was delayed.
2524 if Nkind (Unqualify (A)) = N_Aggregate
2525 or else Nkind (Unqualify (A)) = N_Extension_Aggregate
2526 then
2527 Set_Analyzed (A, False);
2528 Set_Analyzed (Expression (A), False);
2529 end if;
2531 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2532 Set_Assignment_OK (Ref);
2534 -- Make the assignment without usual controlled actions since
2535 -- we only want the post adjust but not the pre finalize here
2536 -- Add manual adjust when necessary
2538 Assign := New_List (
2539 Make_OK_Assignment_Statement (Loc,
2540 Name => Ref,
2541 Expression => A));
2542 Set_No_Ctrl_Actions (First (Assign));
2544 -- Assign the tag now to make sure that the dispatching call in
2545 -- the subsequent deep_adjust works properly (unless VM_Target,
2546 -- where tags are implicit).
2548 if VM_Target = No_VM then
2549 Instr :=
2550 Make_OK_Assignment_Statement (Loc,
2551 Name =>
2552 Make_Selected_Component (Loc,
2553 Prefix => New_Copy_Tree (Target),
2554 Selector_Name =>
2555 New_Reference_To
2556 (First_Tag_Component (Base_Type (Typ)), Loc)),
2558 Expression =>
2559 Unchecked_Convert_To (RTE (RE_Tag),
2560 New_Reference_To
2561 (Node (First_Elmt
2562 (Access_Disp_Table (Base_Type (Typ)))),
2563 Loc)));
2565 Set_Assignment_OK (Name (Instr));
2566 Append_To (Assign, Instr);
2568 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2569 -- also initialize tags of the secondary dispatch tables.
2571 if Present (Abstract_Interfaces (Base_Type (Typ)))
2572 and then not
2573 Is_Empty_Elmt_List
2574 (Abstract_Interfaces (Base_Type (Typ)))
2575 then
2576 Init_Secondary_Tags
2577 (Typ => Base_Type (Typ),
2578 Target => Target,
2579 Stmts_List => Assign);
2580 end if;
2581 end if;
2583 -- Call Adjust manually
2585 if Controlled_Type (Etype (A))
2586 and then not Is_Limited_Type (Etype (A))
2587 then
2588 Append_List_To (Assign,
2589 Make_Adjust_Call (
2590 Ref => New_Copy_Tree (Ref),
2591 Typ => Etype (A),
2592 Flist_Ref => New_Reference_To (
2593 RTE (RE_Global_Final_List), Loc),
2594 With_Attach => Make_Integer_Literal (Loc, 0)));
2595 end if;
2597 Append_To (L,
2598 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2600 if Has_Discriminants (Init_Typ) then
2601 Check_Ancestor_Discriminants (Init_Typ);
2602 end if;
2603 end if;
2604 end;
2606 -- Normal case (not an extension aggregate)
2608 else
2609 -- Generate the discriminant expressions, component by component.
2610 -- If the base type is an unchecked union, the discriminants are
2611 -- unknown to the back-end and absent from a value of the type, so
2612 -- assignments for them are not emitted.
2614 if Has_Discriminants (Typ)
2615 and then not Is_Unchecked_Union (Base_Type (Typ))
2616 then
2617 -- If the type is derived, and constrains discriminants of the
2618 -- parent type, these discriminants are not components of the
2619 -- aggregate, and must be initialized explicitly. They are not
2620 -- visible components of the object, but can become visible with
2621 -- a view conversion to the ancestor.
2623 declare
2624 Btype : Entity_Id;
2625 Parent_Type : Entity_Id;
2626 Disc : Entity_Id;
2627 Discr_Val : Elmt_Id;
2629 begin
2630 Btype := Base_Type (Typ);
2631 while Is_Derived_Type (Btype)
2632 and then Present (Stored_Constraint (Btype))
2633 loop
2634 Parent_Type := Etype (Btype);
2636 Disc := First_Discriminant (Parent_Type);
2637 Discr_Val :=
2638 First_Elmt (Stored_Constraint (Base_Type (Typ)));
2639 while Present (Discr_Val) loop
2641 -- Only those discriminants of the parent that are not
2642 -- renamed by discriminants of the derived type need to
2643 -- be added explicitly.
2645 if not Is_Entity_Name (Node (Discr_Val))
2646 or else
2647 Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2648 then
2649 Comp_Expr :=
2650 Make_Selected_Component (Loc,
2651 Prefix => New_Copy_Tree (Target),
2652 Selector_Name => New_Occurrence_Of (Disc, Loc));
2654 Instr :=
2655 Make_OK_Assignment_Statement (Loc,
2656 Name => Comp_Expr,
2657 Expression => New_Copy_Tree (Node (Discr_Val)));
2659 Set_No_Ctrl_Actions (Instr);
2660 Append_To (L, Instr);
2661 end if;
2663 Next_Discriminant (Disc);
2664 Next_Elmt (Discr_Val);
2665 end loop;
2667 Btype := Base_Type (Parent_Type);
2668 end loop;
2669 end;
2671 -- Generate discriminant init values for the visible discriminants
2673 declare
2674 Discriminant : Entity_Id;
2675 Discriminant_Value : Node_Id;
2677 begin
2678 Discriminant := First_Stored_Discriminant (Typ);
2679 while Present (Discriminant) loop
2680 Comp_Expr :=
2681 Make_Selected_Component (Loc,
2682 Prefix => New_Copy_Tree (Target),
2683 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2685 Discriminant_Value :=
2686 Get_Discriminant_Value (
2687 Discriminant,
2688 N_Typ,
2689 Discriminant_Constraint (N_Typ));
2691 Instr :=
2692 Make_OK_Assignment_Statement (Loc,
2693 Name => Comp_Expr,
2694 Expression => New_Copy_Tree (Discriminant_Value));
2696 Set_No_Ctrl_Actions (Instr);
2697 Append_To (L, Instr);
2699 Next_Stored_Discriminant (Discriminant);
2700 end loop;
2701 end;
2702 end if;
2703 end if;
2705 -- Generate the assignments, component by component
2707 -- tmp.comp1 := Expr1_From_Aggr;
2708 -- tmp.comp2 := Expr2_From_Aggr;
2709 -- ....
2711 Comp := First (Component_Associations (N));
2712 while Present (Comp) loop
2713 Selector := Entity (First (Choices (Comp)));
2715 -- Ada 2005 (AI-287): For each default-initialized component generate
2716 -- a call to the corresponding IP subprogram if available.
2718 if Box_Present (Comp)
2719 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2720 then
2721 if Ekind (Selector) /= E_Discriminant then
2722 Gen_Ctrl_Actions_For_Aggr;
2723 end if;
2725 -- Ada 2005 (AI-287): If the component type has tasks then
2726 -- generate the activation chain and master entities (except
2727 -- in case of an allocator because in that case these entities
2728 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2730 declare
2731 Ctype : constant Entity_Id := Etype (Selector);
2732 Inside_Allocator : Boolean := False;
2733 P : Node_Id := Parent (N);
2735 begin
2736 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2737 while Present (P) loop
2738 if Nkind (P) = N_Allocator then
2739 Inside_Allocator := True;
2740 exit;
2741 end if;
2743 P := Parent (P);
2744 end loop;
2746 if not Inside_Init_Proc and not Inside_Allocator then
2747 Build_Activation_Chain_Entity (N);
2748 end if;
2749 end if;
2750 end;
2752 Append_List_To (L,
2753 Build_Initialization_Call (Loc,
2754 Id_Ref => Make_Selected_Component (Loc,
2755 Prefix => New_Copy_Tree (Target),
2756 Selector_Name => New_Occurrence_Of (Selector,
2757 Loc)),
2758 Typ => Etype (Selector),
2759 Enclos_Type => Typ,
2760 With_Default_Init => True));
2762 goto Next_Comp;
2763 end if;
2765 -- Prepare for component assignment
2767 if Ekind (Selector) /= E_Discriminant
2768 or else Nkind (N) = N_Extension_Aggregate
2769 then
2770 -- All the discriminants have now been assigned
2772 -- This is now a good moment to initialize and attach all the
2773 -- controllers. Their position may depend on the discriminants.
2775 if Ekind (Selector) /= E_Discriminant then
2776 Gen_Ctrl_Actions_For_Aggr;
2777 end if;
2779 Comp_Type := Etype (Selector);
2780 Comp_Expr :=
2781 Make_Selected_Component (Loc,
2782 Prefix => New_Copy_Tree (Target),
2783 Selector_Name => New_Occurrence_Of (Selector, Loc));
2785 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2786 Expr_Q := Expression (Expression (Comp));
2787 else
2788 Expr_Q := Expression (Comp);
2789 end if;
2791 -- The controller is the one of the parent type defining the
2792 -- component (in case of inherited components).
2794 if Controlled_Type (Comp_Type) then
2795 Internal_Final_List :=
2796 Make_Selected_Component (Loc,
2797 Prefix => Convert_To (
2798 Scope (Original_Record_Component (Selector)),
2799 New_Copy_Tree (Target)),
2800 Selector_Name =>
2801 Make_Identifier (Loc, Name_uController));
2803 Internal_Final_List :=
2804 Make_Selected_Component (Loc,
2805 Prefix => Internal_Final_List,
2806 Selector_Name => Make_Identifier (Loc, Name_F));
2808 -- The internal final list can be part of a constant object
2810 Set_Assignment_OK (Internal_Final_List);
2812 else
2813 Internal_Final_List := Empty;
2814 end if;
2816 -- Now either create the assignment or generate the code for the
2817 -- inner aggregate top-down.
2819 if Is_Delayed_Aggregate (Expr_Q) then
2821 -- We have the following case of aggregate nesting inside
2822 -- an object declaration:
2824 -- type Arr_Typ is array (Integer range <>) of ...;
2826 -- type Rec_Typ (...) is record
2827 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2828 -- end record;
2830 -- Obj_Rec_Typ : Rec_Typ := (...,
2831 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2833 -- The length of the ranges of the aggregate and Obj_Add_Typ
2834 -- are equal (B - A = Y - X), but they do not coincide (X /=
2835 -- A and B /= Y). This case requires array sliding which is
2836 -- performed in the following manner:
2838 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2839 -- Temp : Arr_Sub;
2840 -- Temp (X) := (...);
2841 -- ...
2842 -- Temp (Y) := (...);
2843 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2845 if Ekind (Comp_Type) = E_Array_Subtype
2846 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2847 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2848 and then not
2849 Compatible_Int_Bounds
2850 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2851 Typ_Bounds => First_Index (Comp_Type))
2852 then
2853 -- Create the array subtype with bounds equal to those of
2854 -- the corresponding aggregate.
2856 declare
2857 SubE : constant Entity_Id :=
2858 Make_Defining_Identifier (Loc,
2859 New_Internal_Name ('T'));
2861 SubD : constant Node_Id :=
2862 Make_Subtype_Declaration (Loc,
2863 Defining_Identifier =>
2864 SubE,
2865 Subtype_Indication =>
2866 Make_Subtype_Indication (Loc,
2867 Subtype_Mark => New_Reference_To (
2868 Etype (Comp_Type), Loc),
2869 Constraint =>
2870 Make_Index_Or_Discriminant_Constraint (
2871 Loc, Constraints => New_List (
2872 New_Copy_Tree (Aggregate_Bounds (
2873 Expr_Q))))));
2875 -- Create a temporary array of the above subtype which
2876 -- will be used to capture the aggregate assignments.
2878 TmpE : constant Entity_Id :=
2879 Make_Defining_Identifier (Loc,
2880 New_Internal_Name ('A'));
2882 TmpD : constant Node_Id :=
2883 Make_Object_Declaration (Loc,
2884 Defining_Identifier =>
2885 TmpE,
2886 Object_Definition =>
2887 New_Reference_To (SubE, Loc));
2889 begin
2890 Set_No_Initialization (TmpD);
2891 Append_To (L, SubD);
2892 Append_To (L, TmpD);
2894 -- Expand aggregate into assignments to the temp array
2896 Append_List_To (L,
2897 Late_Expansion (Expr_Q, Comp_Type,
2898 New_Reference_To (TmpE, Loc), Internal_Final_List));
2900 -- Slide
2902 Append_To (L,
2903 Make_Assignment_Statement (Loc,
2904 Name => New_Copy_Tree (Comp_Expr),
2905 Expression => New_Reference_To (TmpE, Loc)));
2907 -- Do not pass the original aggregate to Gigi as is,
2908 -- since it will potentially clobber the front or the end
2909 -- of the array. Setting the expression to empty is safe
2910 -- since all aggregates are expanded into assignments.
2912 if Present (Obj) then
2913 Set_Expression (Parent (Obj), Empty);
2914 end if;
2915 end;
2917 -- Normal case (sliding not required)
2919 else
2920 Append_List_To (L,
2921 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr,
2922 Internal_Final_List));
2923 end if;
2925 -- Expr_Q is not delayed aggregate
2927 else
2928 Instr :=
2929 Make_OK_Assignment_Statement (Loc,
2930 Name => Comp_Expr,
2931 Expression => Expression (Comp));
2933 Set_No_Ctrl_Actions (Instr);
2934 Append_To (L, Instr);
2936 -- Adjust the tag if tagged (because of possible view
2937 -- conversions), unless compiling for a VM where tags are
2938 -- implicit.
2940 -- tmp.comp._tag := comp_typ'tag;
2942 if Is_Tagged_Type (Comp_Type) and then VM_Target = No_VM then
2943 Instr :=
2944 Make_OK_Assignment_Statement (Loc,
2945 Name =>
2946 Make_Selected_Component (Loc,
2947 Prefix => New_Copy_Tree (Comp_Expr),
2948 Selector_Name =>
2949 New_Reference_To
2950 (First_Tag_Component (Comp_Type), Loc)),
2952 Expression =>
2953 Unchecked_Convert_To (RTE (RE_Tag),
2954 New_Reference_To
2955 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
2956 Loc)));
2958 Append_To (L, Instr);
2959 end if;
2961 -- Adjust and Attach the component to the proper controller
2963 -- Adjust (tmp.comp);
2964 -- Attach_To_Final_List (tmp.comp,
2965 -- comp_typ (tmp)._record_controller.f)
2967 if Controlled_Type (Comp_Type)
2968 and then not Is_Limited_Type (Comp_Type)
2969 then
2970 Append_List_To (L,
2971 Make_Adjust_Call (
2972 Ref => New_Copy_Tree (Comp_Expr),
2973 Typ => Comp_Type,
2974 Flist_Ref => Internal_Final_List,
2975 With_Attach => Make_Integer_Literal (Loc, 1)));
2976 end if;
2977 end if;
2979 -- ???
2981 elsif Ekind (Selector) = E_Discriminant
2982 and then Nkind (N) /= N_Extension_Aggregate
2983 and then Nkind (Parent (N)) = N_Component_Association
2984 and then Is_Constrained (Typ)
2985 then
2986 -- We must check that the discriminant value imposed by the
2987 -- context is the same as the value given in the subaggregate,
2988 -- because after the expansion into assignments there is no
2989 -- record on which to perform a regular discriminant check.
2991 declare
2992 D_Val : Elmt_Id;
2993 Disc : Entity_Id;
2995 begin
2996 D_Val := First_Elmt (Discriminant_Constraint (Typ));
2997 Disc := First_Discriminant (Typ);
2998 while Chars (Disc) /= Chars (Selector) loop
2999 Next_Discriminant (Disc);
3000 Next_Elmt (D_Val);
3001 end loop;
3003 pragma Assert (Present (D_Val));
3005 -- This check cannot performed for components that are
3006 -- constrained by a current instance, because this is not a
3007 -- value that can be compared with the actual constraint.
3009 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3010 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3011 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3012 then
3013 Append_To (L,
3014 Make_Raise_Constraint_Error (Loc,
3015 Condition =>
3016 Make_Op_Ne (Loc,
3017 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3018 Right_Opnd => Expression (Comp)),
3019 Reason => CE_Discriminant_Check_Failed));
3021 else
3022 -- Find self-reference in previous discriminant assignment,
3023 -- and replace with proper expression.
3025 declare
3026 Ass : Node_Id;
3028 begin
3029 Ass := First (L);
3030 while Present (Ass) loop
3031 if Nkind (Ass) = N_Assignment_Statement
3032 and then Nkind (Name (Ass)) = N_Selected_Component
3033 and then Chars (Selector_Name (Name (Ass))) =
3034 Chars (Disc)
3035 then
3036 Set_Expression
3037 (Ass, New_Copy_Tree (Expression (Comp)));
3038 exit;
3039 end if;
3040 Next (Ass);
3041 end loop;
3042 end;
3043 end if;
3044 end;
3045 end if;
3047 <<Next_Comp>>
3049 Next (Comp);
3050 end loop;
3052 -- If the type is tagged, the tag needs to be initialized (unless
3053 -- compiling for the Java VM where tags are implicit). It is done
3054 -- late in the initialization process because in some cases, we call
3055 -- the init proc of an ancestor which will not leave out the right tag
3057 if Ancestor_Is_Expression then
3058 null;
3060 elsif Is_Tagged_Type (Typ) and then VM_Target = No_VM then
3061 Instr :=
3062 Make_OK_Assignment_Statement (Loc,
3063 Name =>
3064 Make_Selected_Component (Loc,
3065 Prefix => New_Copy_Tree (Target),
3066 Selector_Name =>
3067 New_Reference_To
3068 (First_Tag_Component (Base_Type (Typ)), Loc)),
3070 Expression =>
3071 Unchecked_Convert_To (RTE (RE_Tag),
3072 New_Reference_To
3073 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3074 Loc)));
3076 Append_To (L, Instr);
3078 -- Ada 2005 (AI-251): If the tagged type has been derived from
3079 -- abstract interfaces we must also initialize the tags of the
3080 -- secondary dispatch tables.
3082 if Present (Abstract_Interfaces (Base_Type (Typ)))
3083 and then not
3084 Is_Empty_Elmt_List (Abstract_Interfaces (Base_Type (Typ)))
3085 then
3086 Init_Secondary_Tags
3087 (Typ => Base_Type (Typ),
3088 Target => Target,
3089 Stmts_List => L);
3090 end if;
3091 end if;
3093 -- If the controllers have not been initialized yet (by lack of non-
3094 -- discriminant components), let's do it now.
3096 Gen_Ctrl_Actions_For_Aggr;
3098 return L;
3099 end Build_Record_Aggr_Code;
3101 -------------------------------
3102 -- Convert_Aggr_In_Allocator --
3103 -------------------------------
3105 procedure Convert_Aggr_In_Allocator
3106 (Alloc : Node_Id;
3107 Decl : Node_Id;
3108 Aggr : Node_Id)
3110 Loc : constant Source_Ptr := Sloc (Aggr);
3111 Typ : constant Entity_Id := Etype (Aggr);
3112 Temp : constant Entity_Id := Defining_Identifier (Decl);
3114 Occ : constant Node_Id :=
3115 Unchecked_Convert_To (Typ,
3116 Make_Explicit_Dereference (Loc,
3117 New_Reference_To (Temp, Loc)));
3119 Access_Type : constant Entity_Id := Etype (Temp);
3120 Flist : Entity_Id;
3122 begin
3123 -- If the allocator is for an access discriminant, there is no
3124 -- finalization list for the anonymous access type, and the eventual
3125 -- finalization of the object is handled through the coextension
3126 -- mechanism. If the enclosing object is not dynamically allocated,
3127 -- the access discriminant is itself placed on the stack. Otherwise,
3128 -- some other finalization list is used (see exp_ch4.adb).
3130 -- Decl has been inserted in the code ahead of the allocator, using
3131 -- Insert_Actions. We use Insert_Actions below as well, to ensure that
3132 -- subsequent insertions are done in the proper order. Using (for
3133 -- example) Insert_Actions_After to place the expanded aggregate
3134 -- immediately after Decl may lead to out-of-order references if the
3135 -- allocator has generated a finalization list, as when the designated
3136 -- object is controlled and there is an open transient scope.
3138 if Ekind (Access_Type) = E_Anonymous_Access_Type
3139 and then Nkind (Associated_Node_For_Itype (Access_Type)) =
3140 N_Discriminant_Specification
3141 then
3142 Flist := Empty;
3143 else
3144 Flist := Find_Final_List (Access_Type);
3145 end if;
3147 if Is_Array_Type (Typ) then
3148 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3150 elsif Has_Default_Init_Comps (Aggr) then
3151 declare
3152 L : constant List_Id := New_List;
3153 Init_Stmts : List_Id;
3155 begin
3156 Init_Stmts :=
3157 Late_Expansion
3158 (Aggr, Typ, Occ,
3159 Flist,
3160 Associated_Final_Chain (Base_Type (Access_Type)));
3162 -- ??? Dubious actual for Obj: expect 'the original object being
3163 -- initialized'
3165 if Has_Task (Typ) then
3166 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3167 Insert_Actions (Alloc, L);
3168 else
3169 Insert_Actions (Alloc, Init_Stmts);
3170 end if;
3171 end;
3173 else
3174 Insert_Actions (Alloc,
3175 Late_Expansion
3176 (Aggr, Typ, Occ, Flist,
3177 Associated_Final_Chain (Base_Type (Access_Type))));
3179 -- ??? Dubious actual for Obj: expect 'the original object being
3180 -- initialized'
3182 end if;
3183 end Convert_Aggr_In_Allocator;
3185 --------------------------------
3186 -- Convert_Aggr_In_Assignment --
3187 --------------------------------
3189 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3190 Aggr : Node_Id := Expression (N);
3191 Typ : constant Entity_Id := Etype (Aggr);
3192 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3194 begin
3195 if Nkind (Aggr) = N_Qualified_Expression then
3196 Aggr := Expression (Aggr);
3197 end if;
3199 Insert_Actions_After (N,
3200 Late_Expansion
3201 (Aggr, Typ, Occ,
3202 Find_Final_List (Typ, New_Copy_Tree (Occ))));
3203 end Convert_Aggr_In_Assignment;
3205 ---------------------------------
3206 -- Convert_Aggr_In_Object_Decl --
3207 ---------------------------------
3209 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3210 Obj : constant Entity_Id := Defining_Identifier (N);
3211 Aggr : Node_Id := Expression (N);
3212 Loc : constant Source_Ptr := Sloc (Aggr);
3213 Typ : constant Entity_Id := Etype (Aggr);
3214 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3216 function Discriminants_Ok return Boolean;
3217 -- If the object type is constrained, the discriminants in the
3218 -- aggregate must be checked against the discriminants of the subtype.
3219 -- This cannot be done using Apply_Discriminant_Checks because after
3220 -- expansion there is no aggregate left to check.
3222 ----------------------
3223 -- Discriminants_Ok --
3224 ----------------------
3226 function Discriminants_Ok return Boolean is
3227 Cond : Node_Id := Empty;
3228 Check : Node_Id;
3229 D : Entity_Id;
3230 Disc1 : Elmt_Id;
3231 Disc2 : Elmt_Id;
3232 Val1 : Node_Id;
3233 Val2 : Node_Id;
3235 begin
3236 D := First_Discriminant (Typ);
3237 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3238 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3239 while Present (Disc1) and then Present (Disc2) loop
3240 Val1 := Node (Disc1);
3241 Val2 := Node (Disc2);
3243 if not Is_OK_Static_Expression (Val1)
3244 or else not Is_OK_Static_Expression (Val2)
3245 then
3246 Check := Make_Op_Ne (Loc,
3247 Left_Opnd => Duplicate_Subexpr (Val1),
3248 Right_Opnd => Duplicate_Subexpr (Val2));
3250 if No (Cond) then
3251 Cond := Check;
3253 else
3254 Cond := Make_Or_Else (Loc,
3255 Left_Opnd => Cond,
3256 Right_Opnd => Check);
3257 end if;
3259 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3260 Apply_Compile_Time_Constraint_Error (Aggr,
3261 Msg => "incorrect value for discriminant&?",
3262 Reason => CE_Discriminant_Check_Failed,
3263 Ent => D);
3264 return False;
3265 end if;
3267 Next_Discriminant (D);
3268 Next_Elmt (Disc1);
3269 Next_Elmt (Disc2);
3270 end loop;
3272 -- If any discriminant constraint is non-static, emit a check
3274 if Present (Cond) then
3275 Insert_Action (N,
3276 Make_Raise_Constraint_Error (Loc,
3277 Condition => Cond,
3278 Reason => CE_Discriminant_Check_Failed));
3279 end if;
3281 return True;
3282 end Discriminants_Ok;
3284 -- Start of processing for Convert_Aggr_In_Object_Decl
3286 begin
3287 Set_Assignment_OK (Occ);
3289 if Nkind (Aggr) = N_Qualified_Expression then
3290 Aggr := Expression (Aggr);
3291 end if;
3293 if Has_Discriminants (Typ)
3294 and then Typ /= Etype (Obj)
3295 and then Is_Constrained (Etype (Obj))
3296 and then not Discriminants_Ok
3297 then
3298 return;
3299 end if;
3301 -- If the context is an extended return statement, it has its own
3302 -- finalization machinery (i.e. works like a transient scope) and
3303 -- we do not want to create an additional one, because objects on
3304 -- the finalization list of the return must be moved to the caller's
3305 -- finalization list to complete the return.
3307 -- However, if the aggregate is limited, it is built in place, and the
3308 -- controlled components are not assigned to intermediate temporaries
3309 -- so there is no need for a transient scope in this case either.
3311 if Requires_Transient_Scope (Typ)
3312 and then Ekind (Current_Scope) /= E_Return_Statement
3313 and then not Is_Limited_Type (Typ)
3314 then
3315 Establish_Transient_Scope (Aggr, Sec_Stack =>
3316 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3317 end if;
3319 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
3320 Set_No_Initialization (N);
3321 Initialize_Discriminants (N, Typ);
3322 end Convert_Aggr_In_Object_Decl;
3324 -------------------------------------
3325 -- Convert_Array_Aggr_In_Allocator --
3326 -------------------------------------
3328 procedure Convert_Array_Aggr_In_Allocator
3329 (Decl : Node_Id;
3330 Aggr : Node_Id;
3331 Target : Node_Id)
3333 Aggr_Code : List_Id;
3334 Typ : constant Entity_Id := Etype (Aggr);
3335 Ctyp : constant Entity_Id := Component_Type (Typ);
3337 begin
3338 -- The target is an explicit dereference of the allocated object.
3339 -- Generate component assignments to it, as for an aggregate that
3340 -- appears on the right-hand side of an assignment statement.
3342 Aggr_Code :=
3343 Build_Array_Aggr_Code (Aggr,
3344 Ctype => Ctyp,
3345 Index => First_Index (Typ),
3346 Into => Target,
3347 Scalar_Comp => Is_Scalar_Type (Ctyp));
3349 Insert_Actions_After (Decl, Aggr_Code);
3350 end Convert_Array_Aggr_In_Allocator;
3352 ----------------------------
3353 -- Convert_To_Assignments --
3354 ----------------------------
3356 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3357 Loc : constant Source_Ptr := Sloc (N);
3358 Temp : Entity_Id;
3360 Instr : Node_Id;
3361 Target_Expr : Node_Id;
3362 Parent_Kind : Node_Kind;
3363 Unc_Decl : Boolean := False;
3364 Parent_Node : Node_Id;
3366 begin
3367 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3368 pragma Assert (Is_Record_Type (Typ));
3370 Parent_Node := Parent (N);
3371 Parent_Kind := Nkind (Parent_Node);
3373 if Parent_Kind = N_Qualified_Expression then
3375 -- Check if we are in a unconstrained declaration because in this
3376 -- case the current delayed expansion mechanism doesn't work when
3377 -- the declared object size depend on the initializing expr.
3379 begin
3380 Parent_Node := Parent (Parent_Node);
3381 Parent_Kind := Nkind (Parent_Node);
3383 if Parent_Kind = N_Object_Declaration then
3384 Unc_Decl :=
3385 not Is_Entity_Name (Object_Definition (Parent_Node))
3386 or else Has_Discriminants
3387 (Entity (Object_Definition (Parent_Node)))
3388 or else Is_Class_Wide_Type
3389 (Entity (Object_Definition (Parent_Node)));
3390 end if;
3391 end;
3392 end if;
3394 -- Just set the Delay flag in the cases where the transformation will be
3395 -- done top down from above.
3397 if False
3399 -- Internal aggregate (transformed when expanding the parent)
3401 or else Parent_Kind = N_Aggregate
3402 or else Parent_Kind = N_Extension_Aggregate
3403 or else Parent_Kind = N_Component_Association
3405 -- Allocator (see Convert_Aggr_In_Allocator)
3407 or else Parent_Kind = N_Allocator
3409 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3411 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3413 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3414 -- assignments in init procs are taken into account.
3416 or else (Parent_Kind = N_Assignment_Statement
3417 and then Inside_Init_Proc)
3419 -- (Ada 2005) An inherently limited type in a return statement,
3420 -- which will be handled in a build-in-place fashion, and may be
3421 -- rewritten as an extended return and have its own finalization
3422 -- machinery. In the case of a simple return, the aggregate needs
3423 -- to be delayed until the scope for the return statement has been
3424 -- created, so that any finalization chain will be associated with
3425 -- that scope. For extended returns, we delay expansion to avoid the
3426 -- creation of an unwanted transient scope that could result in
3427 -- premature finalization of the return object (which is built in
3428 -- in place within the caller's scope).
3430 or else
3431 (Is_Inherently_Limited_Type (Typ)
3432 and then
3433 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3434 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3435 then
3436 Set_Expansion_Delayed (N);
3437 return;
3438 end if;
3440 if Requires_Transient_Scope (Typ) then
3441 Establish_Transient_Scope
3442 (N, Sec_Stack =>
3443 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3444 end if;
3446 -- If the aggregate is non-limited, create a temporary. If it is
3447 -- limited and the context is an assignment, this is a subaggregate
3448 -- for an enclosing aggregate being expanded. It must be built in place,
3449 -- so use the target of the current assignment.
3451 if Is_Limited_Type (Typ)
3452 and then Nkind (Parent (N)) = N_Assignment_Statement
3453 then
3454 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3455 Insert_Actions
3456 (Parent (N), Build_Record_Aggr_Code (N, Typ, Target_Expr));
3457 Rewrite (Parent (N), Make_Null_Statement (Loc));
3459 else
3460 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3462 Instr :=
3463 Make_Object_Declaration (Loc,
3464 Defining_Identifier => Temp,
3465 Object_Definition => New_Occurrence_Of (Typ, Loc));
3467 Set_No_Initialization (Instr);
3468 Insert_Action (N, Instr);
3469 Initialize_Discriminants (Instr, Typ);
3470 Target_Expr := New_Occurrence_Of (Temp, Loc);
3471 Insert_Actions (N, Build_Record_Aggr_Code (N, Typ, Target_Expr));
3472 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3473 Analyze_And_Resolve (N, Typ);
3474 end if;
3475 end Convert_To_Assignments;
3477 ---------------------------
3478 -- Convert_To_Positional --
3479 ---------------------------
3481 procedure Convert_To_Positional
3482 (N : Node_Id;
3483 Max_Others_Replicate : Nat := 5;
3484 Handle_Bit_Packed : Boolean := False)
3486 Typ : constant Entity_Id := Etype (N);
3488 Static_Components : Boolean := True;
3490 procedure Check_Static_Components;
3491 -- Check whether all components of the aggregate are compile-time known
3492 -- values, and can be passed as is to the back-end without further
3493 -- expansion.
3495 function Flatten
3496 (N : Node_Id;
3497 Ix : Node_Id;
3498 Ixb : Node_Id) return Boolean;
3499 -- Convert the aggregate into a purely positional form if possible. On
3500 -- entry the bounds of all dimensions are known to be static, and the
3501 -- total number of components is safe enough to expand.
3503 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3504 -- Return True iff the array N is flat (which is not rivial in the case
3505 -- of multidimensionsl aggregates).
3507 -----------------------------
3508 -- Check_Static_Components --
3509 -----------------------------
3511 procedure Check_Static_Components is
3512 Expr : Node_Id;
3514 begin
3515 Static_Components := True;
3517 if Nkind (N) = N_String_Literal then
3518 null;
3520 elsif Present (Expressions (N)) then
3521 Expr := First (Expressions (N));
3522 while Present (Expr) loop
3523 if Nkind (Expr) /= N_Aggregate
3524 or else not Compile_Time_Known_Aggregate (Expr)
3525 or else Expansion_Delayed (Expr)
3526 then
3527 Static_Components := False;
3528 exit;
3529 end if;
3531 Next (Expr);
3532 end loop;
3533 end if;
3535 if Nkind (N) = N_Aggregate
3536 and then Present (Component_Associations (N))
3537 then
3538 Expr := First (Component_Associations (N));
3539 while Present (Expr) loop
3540 if Nkind (Expression (Expr)) = N_Integer_Literal then
3541 null;
3543 elsif Nkind (Expression (Expr)) /= N_Aggregate
3544 or else
3545 not Compile_Time_Known_Aggregate (Expression (Expr))
3546 or else Expansion_Delayed (Expression (Expr))
3547 then
3548 Static_Components := False;
3549 exit;
3550 end if;
3552 Next (Expr);
3553 end loop;
3554 end if;
3555 end Check_Static_Components;
3557 -------------
3558 -- Flatten --
3559 -------------
3561 function Flatten
3562 (N : Node_Id;
3563 Ix : Node_Id;
3564 Ixb : Node_Id) return Boolean
3566 Loc : constant Source_Ptr := Sloc (N);
3567 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3568 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3569 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3570 Lov : Uint;
3571 Hiv : Uint;
3573 begin
3574 if Nkind (Original_Node (N)) = N_String_Literal then
3575 return True;
3576 end if;
3578 if not Compile_Time_Known_Value (Lo)
3579 or else not Compile_Time_Known_Value (Hi)
3580 then
3581 return False;
3582 end if;
3584 Lov := Expr_Value (Lo);
3585 Hiv := Expr_Value (Hi);
3587 if Hiv < Lov
3588 or else not Compile_Time_Known_Value (Blo)
3589 then
3590 return False;
3591 end if;
3593 -- Determine if set of alternatives is suitable for conversion and
3594 -- build an array containing the values in sequence.
3596 declare
3597 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3598 of Node_Id := (others => Empty);
3599 -- The values in the aggregate sorted appropriately
3601 Vlist : List_Id;
3602 -- Same data as Vals in list form
3604 Rep_Count : Nat;
3605 -- Used to validate Max_Others_Replicate limit
3607 Elmt : Node_Id;
3608 Num : Int := UI_To_Int (Lov);
3609 Choice : Node_Id;
3610 Lo, Hi : Node_Id;
3612 begin
3613 if Present (Expressions (N)) then
3614 Elmt := First (Expressions (N));
3615 while Present (Elmt) loop
3616 if Nkind (Elmt) = N_Aggregate
3617 and then Present (Next_Index (Ix))
3618 and then
3619 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3620 then
3621 return False;
3622 end if;
3624 Vals (Num) := Relocate_Node (Elmt);
3625 Num := Num + 1;
3627 Next (Elmt);
3628 end loop;
3629 end if;
3631 if No (Component_Associations (N)) then
3632 return True;
3633 end if;
3635 Elmt := First (Component_Associations (N));
3637 if Nkind (Expression (Elmt)) = N_Aggregate then
3638 if Present (Next_Index (Ix))
3639 and then
3640 not Flatten
3641 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3642 then
3643 return False;
3644 end if;
3645 end if;
3647 Component_Loop : while Present (Elmt) loop
3648 Choice := First (Choices (Elmt));
3649 Choice_Loop : while Present (Choice) loop
3651 -- If we have an others choice, fill in the missing elements
3652 -- subject to the limit established by Max_Others_Replicate.
3654 if Nkind (Choice) = N_Others_Choice then
3655 Rep_Count := 0;
3657 for J in Vals'Range loop
3658 if No (Vals (J)) then
3659 Vals (J) := New_Copy_Tree (Expression (Elmt));
3660 Rep_Count := Rep_Count + 1;
3662 -- Check for maximum others replication. Note that
3663 -- we skip this test if either of the restrictions
3664 -- No_Elaboration_Code or No_Implicit_Loops is
3665 -- active, or if this is a preelaborable unit.
3667 declare
3668 P : constant Entity_Id :=
3669 Cunit_Entity (Current_Sem_Unit);
3671 begin
3672 if Restriction_Active (No_Elaboration_Code)
3673 or else Restriction_Active (No_Implicit_Loops)
3674 or else Is_Preelaborated (P)
3675 or else (Ekind (P) = E_Package_Body
3676 and then
3677 Is_Preelaborated (Spec_Entity (P)))
3678 then
3679 null;
3681 elsif Rep_Count > Max_Others_Replicate then
3682 return False;
3683 end if;
3684 end;
3685 end if;
3686 end loop;
3688 exit Component_Loop;
3690 -- Case of a subtype mark
3692 elsif Nkind (Choice) = N_Identifier
3693 and then Is_Type (Entity (Choice))
3694 then
3695 Lo := Type_Low_Bound (Etype (Choice));
3696 Hi := Type_High_Bound (Etype (Choice));
3698 -- Case of subtype indication
3700 elsif Nkind (Choice) = N_Subtype_Indication then
3701 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3702 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3704 -- Case of a range
3706 elsif Nkind (Choice) = N_Range then
3707 Lo := Low_Bound (Choice);
3708 Hi := High_Bound (Choice);
3710 -- Normal subexpression case
3712 else pragma Assert (Nkind (Choice) in N_Subexpr);
3713 if not Compile_Time_Known_Value (Choice) then
3714 return False;
3716 else
3717 Vals (UI_To_Int (Expr_Value (Choice))) :=
3718 New_Copy_Tree (Expression (Elmt));
3719 goto Continue;
3720 end if;
3721 end if;
3723 -- Range cases merge with Lo,Hi said
3725 if not Compile_Time_Known_Value (Lo)
3726 or else
3727 not Compile_Time_Known_Value (Hi)
3728 then
3729 return False;
3730 else
3731 for J in UI_To_Int (Expr_Value (Lo)) ..
3732 UI_To_Int (Expr_Value (Hi))
3733 loop
3734 Vals (J) := New_Copy_Tree (Expression (Elmt));
3735 end loop;
3736 end if;
3738 <<Continue>>
3739 Next (Choice);
3740 end loop Choice_Loop;
3742 Next (Elmt);
3743 end loop Component_Loop;
3745 -- If we get here the conversion is possible
3747 Vlist := New_List;
3748 for J in Vals'Range loop
3749 Append (Vals (J), Vlist);
3750 end loop;
3752 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3753 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3754 return True;
3755 end;
3756 end Flatten;
3758 -------------
3759 -- Is_Flat --
3760 -------------
3762 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3763 Elmt : Node_Id;
3765 begin
3766 if Dims = 0 then
3767 return True;
3769 elsif Nkind (N) = N_Aggregate then
3770 if Present (Component_Associations (N)) then
3771 return False;
3773 else
3774 Elmt := First (Expressions (N));
3775 while Present (Elmt) loop
3776 if not Is_Flat (Elmt, Dims - 1) then
3777 return False;
3778 end if;
3780 Next (Elmt);
3781 end loop;
3783 return True;
3784 end if;
3785 else
3786 return True;
3787 end if;
3788 end Is_Flat;
3790 -- Start of processing for Convert_To_Positional
3792 begin
3793 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3794 -- components because in this case will need to call the corresponding
3795 -- IP procedure.
3797 if Has_Default_Init_Comps (N) then
3798 return;
3799 end if;
3801 if Is_Flat (N, Number_Dimensions (Typ)) then
3802 return;
3803 end if;
3805 if Is_Bit_Packed_Array (Typ)
3806 and then not Handle_Bit_Packed
3807 then
3808 return;
3809 end if;
3811 -- Do not convert to positional if controlled components are involved
3812 -- since these require special processing
3814 if Has_Controlled_Component (Typ) then
3815 return;
3816 end if;
3818 Check_Static_Components;
3820 -- If the size is known, or all the components are static, try to
3821 -- build a fully positional aggregate.
3823 -- The size of the type may not be known for an aggregate with
3824 -- discriminated array components, but if the components are static
3825 -- it is still possible to verify statically that the length is
3826 -- compatible with the upper bound of the type, and therefore it is
3827 -- worth flattening such aggregates as well.
3829 -- For now the back-end expands these aggregates into individual
3830 -- assignments to the target anyway, but it is conceivable that
3831 -- it will eventually be able to treat such aggregates statically???
3833 if Aggr_Size_OK (Typ)
3834 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3835 then
3836 if Static_Components then
3837 Set_Compile_Time_Known_Aggregate (N);
3838 Set_Expansion_Delayed (N, False);
3839 end if;
3841 Analyze_And_Resolve (N, Typ);
3842 end if;
3843 end Convert_To_Positional;
3845 ----------------------------
3846 -- Expand_Array_Aggregate --
3847 ----------------------------
3849 -- Array aggregate expansion proceeds as follows:
3851 -- 1. If requested we generate code to perform all the array aggregate
3852 -- bound checks, specifically
3854 -- (a) Check that the index range defined by aggregate bounds is
3855 -- compatible with corresponding index subtype.
3857 -- (b) If an others choice is present check that no aggregate
3858 -- index is outside the bounds of the index constraint.
3860 -- (c) For multidimensional arrays make sure that all subaggregates
3861 -- corresponding to the same dimension have the same bounds.
3863 -- 2. Check for packed array aggregate which can be converted to a
3864 -- constant so that the aggregate disappeares completely.
3866 -- 3. Check case of nested aggregate. Generally nested aggregates are
3867 -- handled during the processing of the parent aggregate.
3869 -- 4. Check if the aggregate can be statically processed. If this is the
3870 -- case pass it as is to Gigi. Note that a necessary condition for
3871 -- static processing is that the aggregate be fully positional.
3873 -- 5. If in place aggregate expansion is possible (i.e. no need to create
3874 -- a temporary) then mark the aggregate as such and return. Otherwise
3875 -- create a new temporary and generate the appropriate initialization
3876 -- code.
3878 procedure Expand_Array_Aggregate (N : Node_Id) is
3879 Loc : constant Source_Ptr := Sloc (N);
3881 Typ : constant Entity_Id := Etype (N);
3882 Ctyp : constant Entity_Id := Component_Type (Typ);
3883 -- Typ is the correct constrained array subtype of the aggregate
3884 -- Ctyp is the corresponding component type.
3886 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
3887 -- Number of aggregate index dimensions
3889 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
3890 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
3891 -- Low and High bounds of the constraint for each aggregate index
3893 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
3894 -- The type of each index
3896 Maybe_In_Place_OK : Boolean;
3897 -- If the type is neither controlled nor packed and the aggregate
3898 -- is the expression in an assignment, assignment in place may be
3899 -- possible, provided other conditions are met on the LHS.
3901 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
3902 (others => False);
3903 -- If Others_Present (J) is True, then there is an others choice
3904 -- in one of the sub-aggregates of N at dimension J.
3906 procedure Build_Constrained_Type (Positional : Boolean);
3907 -- If the subtype is not static or unconstrained, build a constrained
3908 -- type using the computable sizes of the aggregate and its sub-
3909 -- aggregates.
3911 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
3912 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
3913 -- by Index_Bounds.
3915 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
3916 -- Checks that in a multi-dimensional array aggregate all subaggregates
3917 -- corresponding to the same dimension have the same bounds.
3918 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3919 -- corresponding to the sub-aggregate.
3921 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
3922 -- Computes the values of array Others_Present. Sub_Aggr is the
3923 -- array sub-aggregate we start the computation from. Dim is the
3924 -- dimension corresponding to the sub-aggregate.
3926 function Has_Address_Clause (D : Node_Id) return Boolean;
3927 -- If the aggregate is the expression in an object declaration, it
3928 -- cannot be expanded in place. This function does a lookahead in the
3929 -- current declarative part to find an address clause for the object
3930 -- being declared.
3932 function In_Place_Assign_OK return Boolean;
3933 -- Simple predicate to determine whether an aggregate assignment can
3934 -- be done in place, because none of the new values can depend on the
3935 -- components of the target of the assignment.
3937 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
3938 -- Checks that if an others choice is present in any sub-aggregate no
3939 -- aggregate index is outside the bounds of the index constraint.
3940 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
3941 -- corresponding to the sub-aggregate.
3943 ----------------------------
3944 -- Build_Constrained_Type --
3945 ----------------------------
3947 procedure Build_Constrained_Type (Positional : Boolean) is
3948 Loc : constant Source_Ptr := Sloc (N);
3949 Agg_Type : Entity_Id;
3950 Comp : Node_Id;
3951 Decl : Node_Id;
3952 Typ : constant Entity_Id := Etype (N);
3953 Indices : constant List_Id := New_List;
3954 Num : Int;
3955 Sub_Agg : Node_Id;
3957 begin
3958 Agg_Type :=
3959 Make_Defining_Identifier (
3960 Loc, New_Internal_Name ('A'));
3962 -- If the aggregate is purely positional, all its subaggregates
3963 -- have the same size. We collect the dimensions from the first
3964 -- subaggregate at each level.
3966 if Positional then
3967 Sub_Agg := N;
3969 for D in 1 .. Number_Dimensions (Typ) loop
3970 Sub_Agg := First (Expressions (Sub_Agg));
3972 Comp := Sub_Agg;
3973 Num := 0;
3974 while Present (Comp) loop
3975 Num := Num + 1;
3976 Next (Comp);
3977 end loop;
3979 Append (
3980 Make_Range (Loc,
3981 Low_Bound => Make_Integer_Literal (Loc, 1),
3982 High_Bound =>
3983 Make_Integer_Literal (Loc, Num)),
3984 Indices);
3985 end loop;
3987 else
3988 -- We know the aggregate type is unconstrained and the aggregate
3989 -- is not processable by the back end, therefore not necessarily
3990 -- positional. Retrieve each dimension bounds (computed earlier).
3991 -- earlier.
3993 for D in 1 .. Number_Dimensions (Typ) loop
3994 Append (
3995 Make_Range (Loc,
3996 Low_Bound => Aggr_Low (D),
3997 High_Bound => Aggr_High (D)),
3998 Indices);
3999 end loop;
4000 end if;
4002 Decl :=
4003 Make_Full_Type_Declaration (Loc,
4004 Defining_Identifier => Agg_Type,
4005 Type_Definition =>
4006 Make_Constrained_Array_Definition (Loc,
4007 Discrete_Subtype_Definitions => Indices,
4008 Component_Definition =>
4009 Make_Component_Definition (Loc,
4010 Aliased_Present => False,
4011 Subtype_Indication =>
4012 New_Occurrence_Of (Component_Type (Typ), Loc))));
4014 Insert_Action (N, Decl);
4015 Analyze (Decl);
4016 Set_Etype (N, Agg_Type);
4017 Set_Is_Itype (Agg_Type);
4018 Freeze_Itype (Agg_Type, N);
4019 end Build_Constrained_Type;
4021 ------------------
4022 -- Check_Bounds --
4023 ------------------
4025 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4026 Aggr_Lo : Node_Id;
4027 Aggr_Hi : Node_Id;
4029 Ind_Lo : Node_Id;
4030 Ind_Hi : Node_Id;
4032 Cond : Node_Id := Empty;
4034 begin
4035 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4036 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4038 -- Generate the following test:
4040 -- [constraint_error when
4041 -- Aggr_Lo <= Aggr_Hi and then
4042 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4044 -- As an optimization try to see if some tests are trivially vacuos
4045 -- because we are comparing an expression against itself.
4047 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4048 Cond := Empty;
4050 elsif Aggr_Hi = Ind_Hi then
4051 Cond :=
4052 Make_Op_Lt (Loc,
4053 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4054 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4056 elsif Aggr_Lo = Ind_Lo then
4057 Cond :=
4058 Make_Op_Gt (Loc,
4059 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4060 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4062 else
4063 Cond :=
4064 Make_Or_Else (Loc,
4065 Left_Opnd =>
4066 Make_Op_Lt (Loc,
4067 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4068 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4070 Right_Opnd =>
4071 Make_Op_Gt (Loc,
4072 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4073 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4074 end if;
4076 if Present (Cond) then
4077 Cond :=
4078 Make_And_Then (Loc,
4079 Left_Opnd =>
4080 Make_Op_Le (Loc,
4081 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4082 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4084 Right_Opnd => Cond);
4086 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4087 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4088 Insert_Action (N,
4089 Make_Raise_Constraint_Error (Loc,
4090 Condition => Cond,
4091 Reason => CE_Length_Check_Failed));
4092 end if;
4093 end Check_Bounds;
4095 ----------------------------
4096 -- Check_Same_Aggr_Bounds --
4097 ----------------------------
4099 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4100 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4101 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4102 -- The bounds of this specific sub-aggregate
4104 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4105 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4106 -- The bounds of the aggregate for this dimension
4108 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4109 -- The index type for this dimension.xxx
4111 Cond : Node_Id := Empty;
4112 Assoc : Node_Id;
4113 Expr : Node_Id;
4115 begin
4116 -- If index checks are on generate the test
4118 -- [constraint_error when
4119 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4121 -- As an optimization try to see if some tests are trivially vacuos
4122 -- because we are comparing an expression against itself. Also for
4123 -- the first dimension the test is trivially vacuous because there
4124 -- is just one aggregate for dimension 1.
4126 if Index_Checks_Suppressed (Ind_Typ) then
4127 Cond := Empty;
4129 elsif Dim = 1
4130 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4131 then
4132 Cond := Empty;
4134 elsif Aggr_Hi = Sub_Hi then
4135 Cond :=
4136 Make_Op_Ne (Loc,
4137 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4138 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4140 elsif Aggr_Lo = Sub_Lo then
4141 Cond :=
4142 Make_Op_Ne (Loc,
4143 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4144 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4146 else
4147 Cond :=
4148 Make_Or_Else (Loc,
4149 Left_Opnd =>
4150 Make_Op_Ne (Loc,
4151 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4152 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4154 Right_Opnd =>
4155 Make_Op_Ne (Loc,
4156 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4157 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4158 end if;
4160 if Present (Cond) then
4161 Insert_Action (N,
4162 Make_Raise_Constraint_Error (Loc,
4163 Condition => Cond,
4164 Reason => CE_Length_Check_Failed));
4165 end if;
4167 -- Now look inside the sub-aggregate to see if there is more work
4169 if Dim < Aggr_Dimension then
4171 -- Process positional components
4173 if Present (Expressions (Sub_Aggr)) then
4174 Expr := First (Expressions (Sub_Aggr));
4175 while Present (Expr) loop
4176 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4177 Next (Expr);
4178 end loop;
4179 end if;
4181 -- Process component associations
4183 if Present (Component_Associations (Sub_Aggr)) then
4184 Assoc := First (Component_Associations (Sub_Aggr));
4185 while Present (Assoc) loop
4186 Expr := Expression (Assoc);
4187 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4188 Next (Assoc);
4189 end loop;
4190 end if;
4191 end if;
4192 end Check_Same_Aggr_Bounds;
4194 ----------------------------
4195 -- Compute_Others_Present --
4196 ----------------------------
4198 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4199 Assoc : Node_Id;
4200 Expr : Node_Id;
4202 begin
4203 if Present (Component_Associations (Sub_Aggr)) then
4204 Assoc := Last (Component_Associations (Sub_Aggr));
4206 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4207 Others_Present (Dim) := True;
4208 end if;
4209 end if;
4211 -- Now look inside the sub-aggregate to see if there is more work
4213 if Dim < Aggr_Dimension then
4215 -- Process positional components
4217 if Present (Expressions (Sub_Aggr)) then
4218 Expr := First (Expressions (Sub_Aggr));
4219 while Present (Expr) loop
4220 Compute_Others_Present (Expr, Dim + 1);
4221 Next (Expr);
4222 end loop;
4223 end if;
4225 -- Process component associations
4227 if Present (Component_Associations (Sub_Aggr)) then
4228 Assoc := First (Component_Associations (Sub_Aggr));
4229 while Present (Assoc) loop
4230 Expr := Expression (Assoc);
4231 Compute_Others_Present (Expr, Dim + 1);
4232 Next (Assoc);
4233 end loop;
4234 end if;
4235 end if;
4236 end Compute_Others_Present;
4238 ------------------------
4239 -- Has_Address_Clause --
4240 ------------------------
4242 function Has_Address_Clause (D : Node_Id) return Boolean is
4243 Id : constant Entity_Id := Defining_Identifier (D);
4244 Decl : Node_Id;
4246 begin
4247 Decl := Next (D);
4248 while Present (Decl) loop
4249 if Nkind (Decl) = N_At_Clause
4250 and then Chars (Identifier (Decl)) = Chars (Id)
4251 then
4252 return True;
4254 elsif Nkind (Decl) = N_Attribute_Definition_Clause
4255 and then Chars (Decl) = Name_Address
4256 and then Chars (Name (Decl)) = Chars (Id)
4257 then
4258 return True;
4259 end if;
4261 Next (Decl);
4262 end loop;
4264 return False;
4265 end Has_Address_Clause;
4267 ------------------------
4268 -- In_Place_Assign_OK --
4269 ------------------------
4271 function In_Place_Assign_OK return Boolean is
4272 Aggr_In : Node_Id;
4273 Aggr_Lo : Node_Id;
4274 Aggr_Hi : Node_Id;
4275 Obj_In : Node_Id;
4276 Obj_Lo : Node_Id;
4277 Obj_Hi : Node_Id;
4279 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
4280 -- Aggregates that consist of a single Others choice are safe
4281 -- if the single expression is.
4283 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4284 -- Check recursively that each component of a (sub)aggregate does
4285 -- not depend on the variable being assigned to.
4287 function Safe_Component (Expr : Node_Id) return Boolean;
4288 -- Verify that an expression cannot depend on the variable being
4289 -- assigned to. Room for improvement here (but less than before).
4291 -------------------------
4292 -- Is_Others_Aggregate --
4293 -------------------------
4295 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
4296 begin
4297 return No (Expressions (Aggr))
4298 and then Nkind
4299 (First (Choices (First (Component_Associations (Aggr)))))
4300 = N_Others_Choice;
4301 end Is_Others_Aggregate;
4303 --------------------
4304 -- Safe_Aggregate --
4305 --------------------
4307 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4308 Expr : Node_Id;
4310 begin
4311 if Present (Expressions (Aggr)) then
4312 Expr := First (Expressions (Aggr));
4313 while Present (Expr) loop
4314 if Nkind (Expr) = N_Aggregate then
4315 if not Safe_Aggregate (Expr) then
4316 return False;
4317 end if;
4319 elsif not Safe_Component (Expr) then
4320 return False;
4321 end if;
4323 Next (Expr);
4324 end loop;
4325 end if;
4327 if Present (Component_Associations (Aggr)) then
4328 Expr := First (Component_Associations (Aggr));
4329 while Present (Expr) loop
4330 if Nkind (Expression (Expr)) = N_Aggregate then
4331 if not Safe_Aggregate (Expression (Expr)) then
4332 return False;
4333 end if;
4335 elsif not Safe_Component (Expression (Expr)) then
4336 return False;
4337 end if;
4339 Next (Expr);
4340 end loop;
4341 end if;
4343 return True;
4344 end Safe_Aggregate;
4346 --------------------
4347 -- Safe_Component --
4348 --------------------
4350 function Safe_Component (Expr : Node_Id) return Boolean is
4351 Comp : Node_Id := Expr;
4353 function Check_Component (Comp : Node_Id) return Boolean;
4354 -- Do the recursive traversal, after copy
4356 ---------------------
4357 -- Check_Component --
4358 ---------------------
4360 function Check_Component (Comp : Node_Id) return Boolean is
4361 begin
4362 if Is_Overloaded (Comp) then
4363 return False;
4364 end if;
4366 return Compile_Time_Known_Value (Comp)
4368 or else (Is_Entity_Name (Comp)
4369 and then Present (Entity (Comp))
4370 and then No (Renamed_Object (Entity (Comp))))
4372 or else (Nkind (Comp) = N_Attribute_Reference
4373 and then Check_Component (Prefix (Comp)))
4375 or else (Nkind (Comp) in N_Binary_Op
4376 and then Check_Component (Left_Opnd (Comp))
4377 and then Check_Component (Right_Opnd (Comp)))
4379 or else (Nkind (Comp) in N_Unary_Op
4380 and then Check_Component (Right_Opnd (Comp)))
4382 or else (Nkind (Comp) = N_Selected_Component
4383 and then Check_Component (Prefix (Comp)))
4385 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4386 and then Check_Component (Expression (Comp)));
4387 end Check_Component;
4389 -- Start of processing for Safe_Component
4391 begin
4392 -- If the component appears in an association that may
4393 -- correspond to more than one element, it is not analyzed
4394 -- before the expansion into assignments, to avoid side effects.
4395 -- We analyze, but do not resolve the copy, to obtain sufficient
4396 -- entity information for the checks that follow. If component is
4397 -- overloaded we assume an unsafe function call.
4399 if not Analyzed (Comp) then
4400 if Is_Overloaded (Expr) then
4401 return False;
4403 elsif Nkind (Expr) = N_Aggregate
4404 and then not Is_Others_Aggregate (Expr)
4405 then
4406 return False;
4408 elsif Nkind (Expr) = N_Allocator then
4410 -- For now, too complex to analyze
4412 return False;
4413 end if;
4415 Comp := New_Copy_Tree (Expr);
4416 Set_Parent (Comp, Parent (Expr));
4417 Analyze (Comp);
4418 end if;
4420 if Nkind (Comp) = N_Aggregate then
4421 return Safe_Aggregate (Comp);
4422 else
4423 return Check_Component (Comp);
4424 end if;
4425 end Safe_Component;
4427 -- Start of processing for In_Place_Assign_OK
4429 begin
4430 if Present (Component_Associations (N)) then
4432 -- On assignment, sliding can take place, so we cannot do the
4433 -- assignment in place unless the bounds of the aggregate are
4434 -- statically equal to those of the target.
4436 -- If the aggregate is given by an others choice, the bounds
4437 -- are derived from the left-hand side, and the assignment is
4438 -- safe if the expression is.
4440 if Is_Others_Aggregate (N) then
4441 return
4442 Safe_Component
4443 (Expression (First (Component_Associations (N))));
4444 end if;
4446 Aggr_In := First_Index (Etype (N));
4447 if Nkind (Parent (N)) = N_Assignment_Statement then
4448 Obj_In := First_Index (Etype (Name (Parent (N))));
4450 else
4451 -- Context is an allocator. Check bounds of aggregate
4452 -- against given type in qualified expression.
4454 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4455 Obj_In :=
4456 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4457 end if;
4459 while Present (Aggr_In) loop
4460 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4461 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4463 if not Compile_Time_Known_Value (Aggr_Lo)
4464 or else not Compile_Time_Known_Value (Aggr_Hi)
4465 or else not Compile_Time_Known_Value (Obj_Lo)
4466 or else not Compile_Time_Known_Value (Obj_Hi)
4467 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4468 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4469 then
4470 return False;
4471 end if;
4473 Next_Index (Aggr_In);
4474 Next_Index (Obj_In);
4475 end loop;
4476 end if;
4478 -- Now check the component values themselves
4480 return Safe_Aggregate (N);
4481 end In_Place_Assign_OK;
4483 ------------------
4484 -- Others_Check --
4485 ------------------
4487 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4488 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4489 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4490 -- The bounds of the aggregate for this dimension
4492 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4493 -- The index type for this dimension
4495 Need_To_Check : Boolean := False;
4497 Choices_Lo : Node_Id := Empty;
4498 Choices_Hi : Node_Id := Empty;
4499 -- The lowest and highest discrete choices for a named sub-aggregate
4501 Nb_Choices : Int := -1;
4502 -- The number of discrete non-others choices in this sub-aggregate
4504 Nb_Elements : Uint := Uint_0;
4505 -- The number of elements in a positional aggregate
4507 Cond : Node_Id := Empty;
4509 Assoc : Node_Id;
4510 Choice : Node_Id;
4511 Expr : Node_Id;
4513 begin
4514 -- Check if we have an others choice. If we do make sure that this
4515 -- sub-aggregate contains at least one element in addition to the
4516 -- others choice.
4518 if Range_Checks_Suppressed (Ind_Typ) then
4519 Need_To_Check := False;
4521 elsif Present (Expressions (Sub_Aggr))
4522 and then Present (Component_Associations (Sub_Aggr))
4523 then
4524 Need_To_Check := True;
4526 elsif Present (Component_Associations (Sub_Aggr)) then
4527 Assoc := Last (Component_Associations (Sub_Aggr));
4529 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4530 Need_To_Check := False;
4532 else
4533 -- Count the number of discrete choices. Start with -1 because
4534 -- the others choice does not count.
4536 Nb_Choices := -1;
4537 Assoc := First (Component_Associations (Sub_Aggr));
4538 while Present (Assoc) loop
4539 Choice := First (Choices (Assoc));
4540 while Present (Choice) loop
4541 Nb_Choices := Nb_Choices + 1;
4542 Next (Choice);
4543 end loop;
4545 Next (Assoc);
4546 end loop;
4548 -- If there is only an others choice nothing to do
4550 Need_To_Check := (Nb_Choices > 0);
4551 end if;
4553 else
4554 Need_To_Check := False;
4555 end if;
4557 -- If we are dealing with a positional sub-aggregate with an others
4558 -- choice then compute the number or positional elements.
4560 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4561 Expr := First (Expressions (Sub_Aggr));
4562 Nb_Elements := Uint_0;
4563 while Present (Expr) loop
4564 Nb_Elements := Nb_Elements + 1;
4565 Next (Expr);
4566 end loop;
4568 -- If the aggregate contains discrete choices and an others choice
4569 -- compute the smallest and largest discrete choice values.
4571 elsif Need_To_Check then
4572 Compute_Choices_Lo_And_Choices_Hi : declare
4574 Table : Case_Table_Type (1 .. Nb_Choices);
4575 -- Used to sort all the different choice values
4577 J : Pos := 1;
4578 Low : Node_Id;
4579 High : Node_Id;
4581 begin
4582 Assoc := First (Component_Associations (Sub_Aggr));
4583 while Present (Assoc) loop
4584 Choice := First (Choices (Assoc));
4585 while Present (Choice) loop
4586 if Nkind (Choice) = N_Others_Choice then
4587 exit;
4588 end if;
4590 Get_Index_Bounds (Choice, Low, High);
4591 Table (J).Choice_Lo := Low;
4592 Table (J).Choice_Hi := High;
4594 J := J + 1;
4595 Next (Choice);
4596 end loop;
4598 Next (Assoc);
4599 end loop;
4601 -- Sort the discrete choices
4603 Sort_Case_Table (Table);
4605 Choices_Lo := Table (1).Choice_Lo;
4606 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4607 end Compute_Choices_Lo_And_Choices_Hi;
4608 end if;
4610 -- If no others choice in this sub-aggregate, or the aggregate
4611 -- comprises only an others choice, nothing to do.
4613 if not Need_To_Check then
4614 Cond := Empty;
4616 -- If we are dealing with an aggregate containing an others choice
4617 -- and positional components, we generate the following test:
4619 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4620 -- Ind_Typ'Pos (Aggr_Hi)
4621 -- then
4622 -- raise Constraint_Error;
4623 -- end if;
4625 elsif Nb_Elements > Uint_0 then
4626 Cond :=
4627 Make_Op_Gt (Loc,
4628 Left_Opnd =>
4629 Make_Op_Add (Loc,
4630 Left_Opnd =>
4631 Make_Attribute_Reference (Loc,
4632 Prefix => New_Reference_To (Ind_Typ, Loc),
4633 Attribute_Name => Name_Pos,
4634 Expressions =>
4635 New_List
4636 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4637 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4639 Right_Opnd =>
4640 Make_Attribute_Reference (Loc,
4641 Prefix => New_Reference_To (Ind_Typ, Loc),
4642 Attribute_Name => Name_Pos,
4643 Expressions => New_List (
4644 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4646 -- If we are dealing with an aggregate containing an others choice
4647 -- and discrete choices we generate the following test:
4649 -- [constraint_error when
4650 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4652 else
4653 Cond :=
4654 Make_Or_Else (Loc,
4655 Left_Opnd =>
4656 Make_Op_Lt (Loc,
4657 Left_Opnd =>
4658 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4659 Right_Opnd =>
4660 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4662 Right_Opnd =>
4663 Make_Op_Gt (Loc,
4664 Left_Opnd =>
4665 Duplicate_Subexpr (Choices_Hi),
4666 Right_Opnd =>
4667 Duplicate_Subexpr (Aggr_Hi)));
4668 end if;
4670 if Present (Cond) then
4671 Insert_Action (N,
4672 Make_Raise_Constraint_Error (Loc,
4673 Condition => Cond,
4674 Reason => CE_Length_Check_Failed));
4675 end if;
4677 -- Now look inside the sub-aggregate to see if there is more work
4679 if Dim < Aggr_Dimension then
4681 -- Process positional components
4683 if Present (Expressions (Sub_Aggr)) then
4684 Expr := First (Expressions (Sub_Aggr));
4685 while Present (Expr) loop
4686 Others_Check (Expr, Dim + 1);
4687 Next (Expr);
4688 end loop;
4689 end if;
4691 -- Process component associations
4693 if Present (Component_Associations (Sub_Aggr)) then
4694 Assoc := First (Component_Associations (Sub_Aggr));
4695 while Present (Assoc) loop
4696 Expr := Expression (Assoc);
4697 Others_Check (Expr, Dim + 1);
4698 Next (Assoc);
4699 end loop;
4700 end if;
4701 end if;
4702 end Others_Check;
4704 -- Remaining Expand_Array_Aggregate variables
4706 Tmp : Entity_Id;
4707 -- Holds the temporary aggregate value
4709 Tmp_Decl : Node_Id;
4710 -- Holds the declaration of Tmp
4712 Aggr_Code : List_Id;
4713 Parent_Node : Node_Id;
4714 Parent_Kind : Node_Kind;
4716 -- Start of processing for Expand_Array_Aggregate
4718 begin
4719 -- Do not touch the special aggregates of attributes used for Asm calls
4721 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
4722 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
4723 then
4724 return;
4725 end if;
4727 -- If the semantic analyzer has determined that aggregate N will raise
4728 -- Constraint_Error at run-time, then the aggregate node has been
4729 -- replaced with an N_Raise_Constraint_Error node and we should
4730 -- never get here.
4732 pragma Assert (not Raises_Constraint_Error (N));
4734 -- STEP 1a
4736 -- Check that the index range defined by aggregate bounds is
4737 -- compatible with corresponding index subtype.
4739 Index_Compatibility_Check : declare
4740 Aggr_Index_Range : Node_Id := First_Index (Typ);
4741 -- The current aggregate index range
4743 Index_Constraint : Node_Id := First_Index (Etype (Typ));
4744 -- The corresponding index constraint against which we have to
4745 -- check the above aggregate index range.
4747 begin
4748 Compute_Others_Present (N, 1);
4750 for J in 1 .. Aggr_Dimension loop
4751 -- There is no need to emit a check if an others choice is
4752 -- present for this array aggregate dimension since in this
4753 -- case one of N's sub-aggregates has taken its bounds from the
4754 -- context and these bounds must have been checked already. In
4755 -- addition all sub-aggregates corresponding to the same
4756 -- dimension must all have the same bounds (checked in (c) below).
4758 if not Range_Checks_Suppressed (Etype (Index_Constraint))
4759 and then not Others_Present (J)
4760 then
4761 -- We don't use Checks.Apply_Range_Check here because it emits
4762 -- a spurious check. Namely it checks that the range defined by
4763 -- the aggregate bounds is non empty. But we know this already
4764 -- if we get here.
4766 Check_Bounds (Aggr_Index_Range, Index_Constraint);
4767 end if;
4769 -- Save the low and high bounds of the aggregate index as well as
4770 -- the index type for later use in checks (b) and (c) below.
4772 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
4773 Aggr_High (J) := High_Bound (Aggr_Index_Range);
4775 Aggr_Index_Typ (J) := Etype (Index_Constraint);
4777 Next_Index (Aggr_Index_Range);
4778 Next_Index (Index_Constraint);
4779 end loop;
4780 end Index_Compatibility_Check;
4782 -- STEP 1b
4784 -- If an others choice is present check that no aggregate index is
4785 -- outside the bounds of the index constraint.
4787 Others_Check (N, 1);
4789 -- STEP 1c
4791 -- For multidimensional arrays make sure that all subaggregates
4792 -- corresponding to the same dimension have the same bounds.
4794 if Aggr_Dimension > 1 then
4795 Check_Same_Aggr_Bounds (N, 1);
4796 end if;
4798 -- STEP 2
4800 -- Here we test for is packed array aggregate that we can handle at
4801 -- compile time. If so, return with transformation done. Note that we do
4802 -- this even if the aggregate is nested, because once we have done this
4803 -- processing, there is no more nested aggregate!
4805 if Packed_Array_Aggregate_Handled (N) then
4806 return;
4807 end if;
4809 -- At this point we try to convert to positional form
4811 if Ekind (Current_Scope) = E_Package
4812 and then Static_Elaboration_Desired (Current_Scope)
4813 then
4814 Convert_To_Positional (N, Max_Others_Replicate => 100);
4816 else
4817 Convert_To_Positional (N);
4818 end if;
4820 -- if the result is no longer an aggregate (e.g. it may be a string
4821 -- literal, or a temporary which has the needed value), then we are
4822 -- done, since there is no longer a nested aggregate.
4824 if Nkind (N) /= N_Aggregate then
4825 return;
4827 -- We are also done if the result is an analyzed aggregate
4828 -- This case could use more comments ???
4830 elsif Analyzed (N)
4831 and then N /= Original_Node (N)
4832 then
4833 return;
4834 end if;
4836 -- If all aggregate components are compile-time known and the aggregate
4837 -- has been flattened, nothing left to do. The same occurs if the
4838 -- aggregate is used to initialize the components of an statically
4839 -- allocated dispatch table.
4841 if Compile_Time_Known_Aggregate (N)
4842 or else Is_Static_Dispatch_Table_Aggregate (N)
4843 then
4844 Set_Expansion_Delayed (N, False);
4845 return;
4846 end if;
4848 -- Now see if back end processing is possible
4850 if Backend_Processing_Possible (N) then
4852 -- If the aggregate is static but the constraints are not, build
4853 -- a static subtype for the aggregate, so that Gigi can place it
4854 -- in static memory. Perform an unchecked_conversion to the non-
4855 -- static type imposed by the context.
4857 declare
4858 Itype : constant Entity_Id := Etype (N);
4859 Index : Node_Id;
4860 Needs_Type : Boolean := False;
4862 begin
4863 Index := First_Index (Itype);
4864 while Present (Index) loop
4865 if not Is_Static_Subtype (Etype (Index)) then
4866 Needs_Type := True;
4867 exit;
4868 else
4869 Next_Index (Index);
4870 end if;
4871 end loop;
4873 if Needs_Type then
4874 Build_Constrained_Type (Positional => True);
4875 Rewrite (N, Unchecked_Convert_To (Itype, N));
4876 Analyze (N);
4877 end if;
4878 end;
4880 return;
4881 end if;
4883 -- STEP 3
4885 -- Delay expansion for nested aggregates it will be taken care of
4886 -- when the parent aggregate is expanded
4888 Parent_Node := Parent (N);
4889 Parent_Kind := Nkind (Parent_Node);
4891 if Parent_Kind = N_Qualified_Expression then
4892 Parent_Node := Parent (Parent_Node);
4893 Parent_Kind := Nkind (Parent_Node);
4894 end if;
4896 if Parent_Kind = N_Aggregate
4897 or else Parent_Kind = N_Extension_Aggregate
4898 or else Parent_Kind = N_Component_Association
4899 or else (Parent_Kind = N_Object_Declaration
4900 and then Controlled_Type (Typ))
4901 or else (Parent_Kind = N_Assignment_Statement
4902 and then Inside_Init_Proc)
4903 then
4904 if Static_Array_Aggregate (N)
4905 or else Compile_Time_Known_Aggregate (N)
4906 then
4907 Set_Expansion_Delayed (N, False);
4908 return;
4909 else
4910 Set_Expansion_Delayed (N);
4911 return;
4912 end if;
4913 end if;
4915 -- STEP 4
4917 -- Look if in place aggregate expansion is possible
4919 -- For object declarations we build the aggregate in place, unless
4920 -- the array is bit-packed or the component is controlled.
4922 -- For assignments we do the assignment in place if all the component
4923 -- associations have compile-time known values. For other cases we
4924 -- create a temporary. The analysis for safety of on-line assignment
4925 -- is delicate, i.e. we don't know how to do it fully yet ???
4927 -- For allocators we assign to the designated object in place if the
4928 -- aggregate meets the same conditions as other in-place assignments.
4929 -- In this case the aggregate may not come from source but was created
4930 -- for default initialization, e.g. with Initialize_Scalars.
4932 if Requires_Transient_Scope (Typ) then
4933 Establish_Transient_Scope
4934 (N, Sec_Stack => Has_Controlled_Component (Typ));
4935 end if;
4937 if Has_Default_Init_Comps (N) then
4938 Maybe_In_Place_OK := False;
4940 elsif Is_Bit_Packed_Array (Typ)
4941 or else Has_Controlled_Component (Typ)
4942 then
4943 Maybe_In_Place_OK := False;
4945 else
4946 Maybe_In_Place_OK :=
4947 (Nkind (Parent (N)) = N_Assignment_Statement
4948 and then Comes_From_Source (N)
4949 and then In_Place_Assign_OK)
4951 or else
4952 (Nkind (Parent (Parent (N))) = N_Allocator
4953 and then In_Place_Assign_OK);
4954 end if;
4956 if not Has_Default_Init_Comps (N)
4957 and then Comes_From_Source (Parent (N))
4958 and then Nkind (Parent (N)) = N_Object_Declaration
4959 and then not
4960 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
4961 and then N = Expression (Parent (N))
4962 and then not Is_Bit_Packed_Array (Typ)
4963 and then not Has_Controlled_Component (Typ)
4964 and then not Has_Address_Clause (Parent (N))
4965 then
4966 Tmp := Defining_Identifier (Parent (N));
4967 Set_No_Initialization (Parent (N));
4968 Set_Expression (Parent (N), Empty);
4970 -- Set the type of the entity, for use in the analysis of the
4971 -- subsequent indexed assignments. If the nominal type is not
4972 -- constrained, build a subtype from the known bounds of the
4973 -- aggregate. If the declaration has a subtype mark, use it,
4974 -- otherwise use the itype of the aggregate.
4976 if not Is_Constrained (Typ) then
4977 Build_Constrained_Type (Positional => False);
4978 elsif Is_Entity_Name (Object_Definition (Parent (N)))
4979 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
4980 then
4981 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
4982 else
4983 Set_Size_Known_At_Compile_Time (Typ, False);
4984 Set_Etype (Tmp, Typ);
4985 end if;
4987 elsif Maybe_In_Place_OK
4988 and then Nkind (Parent (N)) = N_Qualified_Expression
4989 and then Nkind (Parent (Parent (N))) = N_Allocator
4990 then
4991 Set_Expansion_Delayed (N);
4992 return;
4994 -- In the remaining cases the aggregate is the RHS of an assignment
4996 elsif Maybe_In_Place_OK
4997 and then Is_Entity_Name (Name (Parent (N)))
4998 then
4999 Tmp := Entity (Name (Parent (N)));
5001 if Etype (Tmp) /= Etype (N) then
5002 Apply_Length_Check (N, Etype (Tmp));
5004 if Nkind (N) = N_Raise_Constraint_Error then
5006 -- Static error, nothing further to expand
5008 return;
5009 end if;
5010 end if;
5012 elsif Maybe_In_Place_OK
5013 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
5014 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5015 then
5016 Tmp := Name (Parent (N));
5018 if Etype (Tmp) /= Etype (N) then
5019 Apply_Length_Check (N, Etype (Tmp));
5020 end if;
5022 elsif Maybe_In_Place_OK
5023 and then Nkind (Name (Parent (N))) = N_Slice
5024 and then Safe_Slice_Assignment (N)
5025 then
5026 -- Safe_Slice_Assignment rewrites assignment as a loop
5028 return;
5030 -- Step 5
5032 -- In place aggregate expansion is not possible
5034 else
5035 Maybe_In_Place_OK := False;
5036 Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5037 Tmp_Decl :=
5038 Make_Object_Declaration
5039 (Loc,
5040 Defining_Identifier => Tmp,
5041 Object_Definition => New_Occurrence_Of (Typ, Loc));
5042 Set_No_Initialization (Tmp_Decl, True);
5044 -- If we are within a loop, the temporary will be pushed on the
5045 -- stack at each iteration. If the aggregate is the expression for
5046 -- an allocator, it will be immediately copied to the heap and can
5047 -- be reclaimed at once. We create a transient scope around the
5048 -- aggregate for this purpose.
5050 if Ekind (Current_Scope) = E_Loop
5051 and then Nkind (Parent (Parent (N))) = N_Allocator
5052 then
5053 Establish_Transient_Scope (N, False);
5054 end if;
5056 Insert_Action (N, Tmp_Decl);
5057 end if;
5059 -- Construct and insert the aggregate code. We can safely suppress
5060 -- index checks because this code is guaranteed not to raise CE
5061 -- on index checks. However we should *not* suppress all checks.
5063 declare
5064 Target : Node_Id;
5066 begin
5067 if Nkind (Tmp) = N_Defining_Identifier then
5068 Target := New_Reference_To (Tmp, Loc);
5070 else
5072 if Has_Default_Init_Comps (N) then
5074 -- Ada 2005 (AI-287): This case has not been analyzed???
5076 raise Program_Error;
5077 end if;
5079 -- Name in assignment is explicit dereference
5081 Target := New_Copy (Tmp);
5082 end if;
5084 Aggr_Code :=
5085 Build_Array_Aggr_Code (N,
5086 Ctype => Ctyp,
5087 Index => First_Index (Typ),
5088 Into => Target,
5089 Scalar_Comp => Is_Scalar_Type (Ctyp));
5090 end;
5092 if Comes_From_Source (Tmp) then
5093 Insert_Actions_After (Parent (N), Aggr_Code);
5095 else
5096 Insert_Actions (N, Aggr_Code);
5097 end if;
5099 -- If the aggregate has been assigned in place, remove the original
5100 -- assignment.
5102 if Nkind (Parent (N)) = N_Assignment_Statement
5103 and then Maybe_In_Place_OK
5104 then
5105 Rewrite (Parent (N), Make_Null_Statement (Loc));
5107 elsif Nkind (Parent (N)) /= N_Object_Declaration
5108 or else Tmp /= Defining_Identifier (Parent (N))
5109 then
5110 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5111 Analyze_And_Resolve (N, Typ);
5112 end if;
5113 end Expand_Array_Aggregate;
5115 ------------------------
5116 -- Expand_N_Aggregate --
5117 ------------------------
5119 procedure Expand_N_Aggregate (N : Node_Id) is
5120 begin
5121 if Is_Record_Type (Etype (N)) then
5122 Expand_Record_Aggregate (N);
5123 else
5124 Expand_Array_Aggregate (N);
5125 end if;
5126 exception
5127 when RE_Not_Available =>
5128 return;
5129 end Expand_N_Aggregate;
5131 ----------------------------------
5132 -- Expand_N_Extension_Aggregate --
5133 ----------------------------------
5135 -- If the ancestor part is an expression, add a component association for
5136 -- the parent field. If the type of the ancestor part is not the direct
5137 -- parent of the expected type, build recursively the needed ancestors.
5138 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5139 -- ration for a temporary of the expected type, followed by individual
5140 -- assignments to the given components.
5142 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5143 Loc : constant Source_Ptr := Sloc (N);
5144 A : constant Node_Id := Ancestor_Part (N);
5145 Typ : constant Entity_Id := Etype (N);
5147 begin
5148 -- If the ancestor is a subtype mark, an init proc must be called
5149 -- on the resulting object which thus has to be materialized in
5150 -- the front-end
5152 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5153 Convert_To_Assignments (N, Typ);
5155 -- The extension aggregate is transformed into a record aggregate
5156 -- of the following form (c1 and c2 are inherited components)
5158 -- (Exp with c3 => a, c4 => b)
5159 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c1 => a, c2 => b)
5161 else
5162 Set_Etype (N, Typ);
5164 if VM_Target = No_VM then
5165 Expand_Record_Aggregate (N,
5166 Orig_Tag =>
5167 New_Occurrence_Of
5168 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5169 Parent_Expr => A);
5170 else
5171 -- No tag is needed in the case of a VM
5172 Expand_Record_Aggregate (N,
5173 Parent_Expr => A);
5174 end if;
5175 end if;
5177 exception
5178 when RE_Not_Available =>
5179 return;
5180 end Expand_N_Extension_Aggregate;
5182 -----------------------------
5183 -- Expand_Record_Aggregate --
5184 -----------------------------
5186 procedure Expand_Record_Aggregate
5187 (N : Node_Id;
5188 Orig_Tag : Node_Id := Empty;
5189 Parent_Expr : Node_Id := Empty)
5191 Loc : constant Source_Ptr := Sloc (N);
5192 Comps : constant List_Id := Component_Associations (N);
5193 Typ : constant Entity_Id := Etype (N);
5194 Base_Typ : constant Entity_Id := Base_Type (Typ);
5196 Static_Components : Boolean := True;
5197 -- Flag to indicate whether all components are compile-time known,
5198 -- and the aggregate can be constructed statically and handled by
5199 -- the back-end.
5201 function Component_Not_OK_For_Backend return Boolean;
5202 -- Check for presence of component which makes it impossible for the
5203 -- backend to process the aggregate, thus requiring the use of a series
5204 -- of assignment statements. Cases checked for are a nested aggregate
5205 -- needing Late_Expansion, the presence of a tagged component which may
5206 -- need tag adjustment, and a bit unaligned component reference.
5208 -- We also force expansion into assignments if a component is of a
5209 -- mutable type (including a private type with discriminants) because
5210 -- in that case the size of the component to be copied may be smaller
5211 -- than the side of the target, and there is no simple way for gigi
5212 -- to compute the size of the object to be copied.
5214 -- NOTE: This is part of the ongoing work to define precisely the
5215 -- interface between front-end and back-end handling of aggregates.
5216 -- In general it is desirable to pass aggregates as they are to gigi,
5217 -- in order to minimize elaboration code. This is one case where the
5218 -- semantics of Ada complicate the analysis and lead to anomalies in
5219 -- the gcc back-end if the aggregate is not expanded into assignments.
5221 ----------------------------------
5222 -- Component_Not_OK_For_Backend --
5223 ----------------------------------
5225 function Component_Not_OK_For_Backend return Boolean is
5226 C : Node_Id;
5227 Expr_Q : Node_Id;
5229 begin
5230 if No (Comps) then
5231 return False;
5232 end if;
5234 C := First (Comps);
5235 while Present (C) loop
5236 if Nkind (Expression (C)) = N_Qualified_Expression then
5237 Expr_Q := Expression (Expression (C));
5238 else
5239 Expr_Q := Expression (C);
5240 end if;
5242 -- Return true if the aggregate has any associations for tagged
5243 -- components that may require tag adjustment.
5245 -- These are cases where the source expression may have a tag that
5246 -- could differ from the component tag (e.g., can occur for type
5247 -- conversions and formal parameters). (Tag adjustment not needed
5248 -- if VM_Target because object tags are implicit in the machine.)
5250 if Is_Tagged_Type (Etype (Expr_Q))
5251 and then (Nkind (Expr_Q) = N_Type_Conversion
5252 or else (Is_Entity_Name (Expr_Q)
5253 and then
5254 Ekind (Entity (Expr_Q)) in Formal_Kind))
5255 and then VM_Target = No_VM
5256 then
5257 Static_Components := False;
5258 return True;
5260 elsif Is_Delayed_Aggregate (Expr_Q) then
5261 Static_Components := False;
5262 return True;
5264 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5265 Static_Components := False;
5266 return True;
5267 end if;
5269 if Is_Scalar_Type (Etype (Expr_Q)) then
5270 if not Compile_Time_Known_Value (Expr_Q) then
5271 Static_Components := False;
5272 end if;
5274 elsif Nkind (Expr_Q) /= N_Aggregate
5275 or else not Compile_Time_Known_Aggregate (Expr_Q)
5276 then
5277 Static_Components := False;
5279 if Is_Private_Type (Etype (Expr_Q))
5280 and then Has_Discriminants (Etype (Expr_Q))
5281 then
5282 return True;
5283 end if;
5284 end if;
5286 Next (C);
5287 end loop;
5289 return False;
5290 end Component_Not_OK_For_Backend;
5292 -- Remaining Expand_Record_Aggregate variables
5294 Tag_Value : Node_Id;
5295 Comp : Entity_Id;
5296 New_Comp : Node_Id;
5298 -- Start of processing for Expand_Record_Aggregate
5300 begin
5301 -- If the aggregate is to be assigned to an atomic variable, we
5302 -- have to prevent a piecemeal assignment even if the aggregate
5303 -- is to be expanded. We create a temporary for the aggregate, and
5304 -- assign the temporary instead, so that the back end can generate
5305 -- an atomic move for it.
5307 if Is_Atomic (Typ)
5308 and then (Nkind (Parent (N)) = N_Object_Declaration
5309 or else Nkind (Parent (N)) = N_Assignment_Statement)
5310 and then Comes_From_Source (Parent (N))
5311 then
5312 Expand_Atomic_Aggregate (N, Typ);
5313 return;
5315 -- No special management required for aggregates used to initialize
5316 -- statically allocated dispatch tables
5318 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5319 return;
5320 end if;
5322 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5323 -- are build-in-place function calls. This test could be more specific,
5324 -- but doing it for all inherently limited aggregates seems harmless.
5325 -- The assignments will turn into build-in-place function calls (see
5326 -- Make_Build_In_Place_Call_In_Assignment).
5328 if Ada_Version >= Ada_05 and then Is_Inherently_Limited_Type (Typ) then
5329 Convert_To_Assignments (N, Typ);
5331 -- Gigi doesn't handle properly temporaries of variable size
5332 -- so we generate it in the front-end
5334 elsif not Size_Known_At_Compile_Time (Typ) then
5335 Convert_To_Assignments (N, Typ);
5337 -- Temporaries for controlled aggregates need to be attached to a
5338 -- final chain in order to be properly finalized, so it has to
5339 -- be created in the front-end
5341 elsif Is_Controlled (Typ)
5342 or else Has_Controlled_Component (Base_Type (Typ))
5343 then
5344 Convert_To_Assignments (N, Typ);
5346 -- Ada 2005 (AI-287): In case of default initialized components we
5347 -- convert the aggregate into assignments.
5349 elsif Has_Default_Init_Comps (N) then
5350 Convert_To_Assignments (N, Typ);
5352 -- Check components
5354 elsif Component_Not_OK_For_Backend then
5355 Convert_To_Assignments (N, Typ);
5357 -- If an ancestor is private, some components are not inherited and
5358 -- we cannot expand into a record aggregate
5360 elsif Has_Private_Ancestor (Typ) then
5361 Convert_To_Assignments (N, Typ);
5363 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5364 -- is not able to handle the aggregate for Late_Request.
5366 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5367 Convert_To_Assignments (N, Typ);
5369 -- If the tagged types covers interface types we need to initialize all
5370 -- hidden components containing pointers to secondary dispatch tables.
5372 elsif Is_Tagged_Type (Typ) and then Has_Abstract_Interfaces (Typ) then
5373 Convert_To_Assignments (N, Typ);
5375 -- If some components are mutable, the size of the aggregate component
5376 -- may be distinct from the default size of the type component, so
5377 -- we need to expand to insure that the back-end copies the proper
5378 -- size of the data.
5380 elsif Has_Mutable_Components (Typ) then
5381 Convert_To_Assignments (N, Typ);
5383 -- If the type involved has any non-bit aligned components, then we are
5384 -- not sure that the back end can handle this case correctly.
5386 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5387 Convert_To_Assignments (N, Typ);
5389 -- In all other cases, build a proper aggregate handlable by gigi
5391 else
5392 if Nkind (N) = N_Aggregate then
5394 -- If the aggregate is static and can be handled by the back-end,
5395 -- nothing left to do.
5397 if Static_Components then
5398 Set_Compile_Time_Known_Aggregate (N);
5399 Set_Expansion_Delayed (N, False);
5400 end if;
5401 end if;
5403 -- If no discriminants, nothing special to do
5405 if not Has_Discriminants (Typ) then
5406 null;
5408 -- Case of discriminants present
5410 elsif Is_Derived_Type (Typ) then
5412 -- For untagged types, non-stored discriminants are replaced
5413 -- with stored discriminants, which are the ones that gigi uses
5414 -- to describe the type and its components.
5416 Generate_Aggregate_For_Derived_Type : declare
5417 Constraints : constant List_Id := New_List;
5418 First_Comp : Node_Id;
5419 Discriminant : Entity_Id;
5420 Decl : Node_Id;
5421 Num_Disc : Int := 0;
5422 Num_Gird : Int := 0;
5424 procedure Prepend_Stored_Values (T : Entity_Id);
5425 -- Scan the list of stored discriminants of the type, and add
5426 -- their values to the aggregate being built.
5428 ---------------------------
5429 -- Prepend_Stored_Values --
5430 ---------------------------
5432 procedure Prepend_Stored_Values (T : Entity_Id) is
5433 begin
5434 Discriminant := First_Stored_Discriminant (T);
5435 while Present (Discriminant) loop
5436 New_Comp :=
5437 Make_Component_Association (Loc,
5438 Choices =>
5439 New_List (New_Occurrence_Of (Discriminant, Loc)),
5441 Expression =>
5442 New_Copy_Tree (
5443 Get_Discriminant_Value (
5444 Discriminant,
5445 Typ,
5446 Discriminant_Constraint (Typ))));
5448 if No (First_Comp) then
5449 Prepend_To (Component_Associations (N), New_Comp);
5450 else
5451 Insert_After (First_Comp, New_Comp);
5452 end if;
5454 First_Comp := New_Comp;
5455 Next_Stored_Discriminant (Discriminant);
5456 end loop;
5457 end Prepend_Stored_Values;
5459 -- Start of processing for Generate_Aggregate_For_Derived_Type
5461 begin
5462 -- Remove the associations for the discriminant of derived type
5464 First_Comp := First (Component_Associations (N));
5465 while Present (First_Comp) loop
5466 Comp := First_Comp;
5467 Next (First_Comp);
5469 if Ekind (Entity
5470 (First (Choices (Comp)))) = E_Discriminant
5471 then
5472 Remove (Comp);
5473 Num_Disc := Num_Disc + 1;
5474 end if;
5475 end loop;
5477 -- Insert stored discriminant associations in the correct
5478 -- order. If there are more stored discriminants than new
5479 -- discriminants, there is at least one new discriminant that
5480 -- constrains more than one of the stored discriminants. In
5481 -- this case we need to construct a proper subtype of the
5482 -- parent type, in order to supply values to all the
5483 -- components. Otherwise there is one-one correspondence
5484 -- between the constraints and the stored discriminants.
5486 First_Comp := Empty;
5488 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5489 while Present (Discriminant) loop
5490 Num_Gird := Num_Gird + 1;
5491 Next_Stored_Discriminant (Discriminant);
5492 end loop;
5494 -- Case of more stored discriminants than new discriminants
5496 if Num_Gird > Num_Disc then
5498 -- Create a proper subtype of the parent type, which is the
5499 -- proper implementation type for the aggregate, and convert
5500 -- it to the intended target type.
5502 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5503 while Present (Discriminant) loop
5504 New_Comp :=
5505 New_Copy_Tree (
5506 Get_Discriminant_Value (
5507 Discriminant,
5508 Typ,
5509 Discriminant_Constraint (Typ)));
5510 Append (New_Comp, Constraints);
5511 Next_Stored_Discriminant (Discriminant);
5512 end loop;
5514 Decl :=
5515 Make_Subtype_Declaration (Loc,
5516 Defining_Identifier =>
5517 Make_Defining_Identifier (Loc,
5518 New_Internal_Name ('T')),
5519 Subtype_Indication =>
5520 Make_Subtype_Indication (Loc,
5521 Subtype_Mark =>
5522 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5523 Constraint =>
5524 Make_Index_Or_Discriminant_Constraint
5525 (Loc, Constraints)));
5527 Insert_Action (N, Decl);
5528 Prepend_Stored_Values (Base_Type (Typ));
5530 Set_Etype (N, Defining_Identifier (Decl));
5531 Set_Analyzed (N);
5533 Rewrite (N, Unchecked_Convert_To (Typ, N));
5534 Analyze (N);
5536 -- Case where we do not have fewer new discriminants than
5537 -- stored discriminants, so in this case we can simply use the
5538 -- stored discriminants of the subtype.
5540 else
5541 Prepend_Stored_Values (Typ);
5542 end if;
5543 end Generate_Aggregate_For_Derived_Type;
5544 end if;
5546 if Is_Tagged_Type (Typ) then
5548 -- The tagged case, _parent and _tag component must be created
5550 -- Reset null_present unconditionally. tagged records always have
5551 -- at least one field (the tag or the parent)
5553 Set_Null_Record_Present (N, False);
5555 -- When the current aggregate comes from the expansion of an
5556 -- extension aggregate, the parent expr is replaced by an
5557 -- aggregate formed by selected components of this expr
5559 if Present (Parent_Expr)
5560 and then Is_Empty_List (Comps)
5561 then
5562 Comp := First_Component_Or_Discriminant (Typ);
5563 while Present (Comp) loop
5565 -- Skip all expander-generated components
5568 not Comes_From_Source (Original_Record_Component (Comp))
5569 then
5570 null;
5572 else
5573 New_Comp :=
5574 Make_Selected_Component (Loc,
5575 Prefix =>
5576 Unchecked_Convert_To (Typ,
5577 Duplicate_Subexpr (Parent_Expr, True)),
5579 Selector_Name => New_Occurrence_Of (Comp, Loc));
5581 Append_To (Comps,
5582 Make_Component_Association (Loc,
5583 Choices =>
5584 New_List (New_Occurrence_Of (Comp, Loc)),
5585 Expression =>
5586 New_Comp));
5588 Analyze_And_Resolve (New_Comp, Etype (Comp));
5589 end if;
5591 Next_Component_Or_Discriminant (Comp);
5592 end loop;
5593 end if;
5595 -- Compute the value for the Tag now, if the type is a root it
5596 -- will be included in the aggregate right away, otherwise it will
5597 -- be propagated to the parent aggregate
5599 if Present (Orig_Tag) then
5600 Tag_Value := Orig_Tag;
5601 elsif VM_Target /= No_VM then
5602 Tag_Value := Empty;
5603 else
5604 Tag_Value :=
5605 New_Occurrence_Of
5606 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5607 end if;
5609 -- For a derived type, an aggregate for the parent is formed with
5610 -- all the inherited components.
5612 if Is_Derived_Type (Typ) then
5614 declare
5615 First_Comp : Node_Id;
5616 Parent_Comps : List_Id;
5617 Parent_Aggr : Node_Id;
5618 Parent_Name : Node_Id;
5620 begin
5621 -- Remove the inherited component association from the
5622 -- aggregate and store them in the parent aggregate
5624 First_Comp := First (Component_Associations (N));
5625 Parent_Comps := New_List;
5626 while Present (First_Comp)
5627 and then Scope (Original_Record_Component (
5628 Entity (First (Choices (First_Comp))))) /= Base_Typ
5629 loop
5630 Comp := First_Comp;
5631 Next (First_Comp);
5632 Remove (Comp);
5633 Append (Comp, Parent_Comps);
5634 end loop;
5636 Parent_Aggr := Make_Aggregate (Loc,
5637 Component_Associations => Parent_Comps);
5638 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
5640 -- Find the _parent component
5642 Comp := First_Component (Typ);
5643 while Chars (Comp) /= Name_uParent loop
5644 Comp := Next_Component (Comp);
5645 end loop;
5647 Parent_Name := New_Occurrence_Of (Comp, Loc);
5649 -- Insert the parent aggregate
5651 Prepend_To (Component_Associations (N),
5652 Make_Component_Association (Loc,
5653 Choices => New_List (Parent_Name),
5654 Expression => Parent_Aggr));
5656 -- Expand recursively the parent propagating the right Tag
5658 Expand_Record_Aggregate (
5659 Parent_Aggr, Tag_Value, Parent_Expr);
5660 end;
5662 -- For a root type, the tag component is added (unless compiling
5663 -- for the VMs, where tags are implicit).
5665 elsif VM_Target = No_VM then
5666 declare
5667 Tag_Name : constant Node_Id :=
5668 New_Occurrence_Of
5669 (First_Tag_Component (Typ), Loc);
5670 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
5671 Conv_Node : constant Node_Id :=
5672 Unchecked_Convert_To (Typ_Tag, Tag_Value);
5674 begin
5675 Set_Etype (Conv_Node, Typ_Tag);
5676 Prepend_To (Component_Associations (N),
5677 Make_Component_Association (Loc,
5678 Choices => New_List (Tag_Name),
5679 Expression => Conv_Node));
5680 end;
5681 end if;
5682 end if;
5683 end if;
5685 end Expand_Record_Aggregate;
5687 ----------------------------
5688 -- Has_Default_Init_Comps --
5689 ----------------------------
5691 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
5692 Comps : constant List_Id := Component_Associations (N);
5693 C : Node_Id;
5694 Expr : Node_Id;
5695 begin
5696 pragma Assert (Nkind (N) = N_Aggregate
5697 or else Nkind (N) = N_Extension_Aggregate);
5699 if No (Comps) then
5700 return False;
5701 end if;
5703 if Has_Self_Reference (N) then
5704 return True;
5705 end if;
5707 -- Check if any direct component has default initialized components
5709 C := First (Comps);
5710 while Present (C) loop
5711 if Box_Present (C) then
5712 return True;
5713 end if;
5715 Next (C);
5716 end loop;
5718 -- Recursive call in case of aggregate expression
5720 C := First (Comps);
5721 while Present (C) loop
5722 Expr := Expression (C);
5724 if Present (Expr)
5725 and then (Nkind (Expr) = N_Aggregate
5726 or else Nkind (Expr) = N_Extension_Aggregate)
5727 and then Has_Default_Init_Comps (Expr)
5728 then
5729 return True;
5730 end if;
5732 Next (C);
5733 end loop;
5735 return False;
5736 end Has_Default_Init_Comps;
5738 --------------------------
5739 -- Is_Delayed_Aggregate --
5740 --------------------------
5742 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
5743 Node : Node_Id := N;
5744 Kind : Node_Kind := Nkind (Node);
5746 begin
5747 if Kind = N_Qualified_Expression then
5748 Node := Expression (Node);
5749 Kind := Nkind (Node);
5750 end if;
5752 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
5753 return False;
5754 else
5755 return Expansion_Delayed (Node);
5756 end if;
5757 end Is_Delayed_Aggregate;
5759 ----------------------------------------
5760 -- Is_Static_Dispatch_Table_Aggregate --
5761 ----------------------------------------
5763 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
5764 Typ : constant Entity_Id := Base_Type (Etype (N));
5766 begin
5767 return Static_Dispatch_Tables
5768 and then VM_Target = No_VM
5769 and then RTU_Loaded (Ada_Tags)
5771 -- Avoid circularity when rebuilding the compiler
5773 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
5774 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
5775 or else
5776 Typ = RTE (RE_Address_Array)
5777 or else
5778 Typ = RTE (RE_Type_Specific_Data)
5779 or else
5780 Typ = RTE (RE_Tag_Table)
5781 or else
5782 (RTE_Available (RE_Interface_Data)
5783 and then Typ = RTE (RE_Interface_Data))
5784 or else
5785 (RTE_Available (RE_Interfaces_Array)
5786 and then Typ = RTE (RE_Interfaces_Array))
5787 or else
5788 (RTE_Available (RE_Interface_Data_Element)
5789 and then Typ = RTE (RE_Interface_Data_Element)));
5790 end Is_Static_Dispatch_Table_Aggregate;
5792 --------------------
5793 -- Late_Expansion --
5794 --------------------
5796 function Late_Expansion
5797 (N : Node_Id;
5798 Typ : Entity_Id;
5799 Target : Node_Id;
5800 Flist : Node_Id := Empty;
5801 Obj : Entity_Id := Empty) return List_Id
5803 begin
5804 if Is_Record_Type (Etype (N)) then
5805 return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj);
5807 else pragma Assert (Is_Array_Type (Etype (N)));
5808 return
5809 Build_Array_Aggr_Code
5810 (N => N,
5811 Ctype => Component_Type (Etype (N)),
5812 Index => First_Index (Typ),
5813 Into => Target,
5814 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
5815 Indices => No_List,
5816 Flist => Flist);
5817 end if;
5818 end Late_Expansion;
5820 ----------------------------------
5821 -- Make_OK_Assignment_Statement --
5822 ----------------------------------
5824 function Make_OK_Assignment_Statement
5825 (Sloc : Source_Ptr;
5826 Name : Node_Id;
5827 Expression : Node_Id) return Node_Id
5829 begin
5830 Set_Assignment_OK (Name);
5832 return Make_Assignment_Statement (Sloc, Name, Expression);
5833 end Make_OK_Assignment_Statement;
5835 -----------------------
5836 -- Number_Of_Choices --
5837 -----------------------
5839 function Number_Of_Choices (N : Node_Id) return Nat is
5840 Assoc : Node_Id;
5841 Choice : Node_Id;
5843 Nb_Choices : Nat := 0;
5845 begin
5846 if Present (Expressions (N)) then
5847 return 0;
5848 end if;
5850 Assoc := First (Component_Associations (N));
5851 while Present (Assoc) loop
5852 Choice := First (Choices (Assoc));
5853 while Present (Choice) loop
5854 if Nkind (Choice) /= N_Others_Choice then
5855 Nb_Choices := Nb_Choices + 1;
5856 end if;
5858 Next (Choice);
5859 end loop;
5861 Next (Assoc);
5862 end loop;
5864 return Nb_Choices;
5865 end Number_Of_Choices;
5867 ------------------------------------
5868 -- Packed_Array_Aggregate_Handled --
5869 ------------------------------------
5871 -- The current version of this procedure will handle at compile time
5872 -- any array aggregate that meets these conditions:
5874 -- One dimensional, bit packed
5875 -- Underlying packed type is modular type
5876 -- Bounds are within 32-bit Int range
5877 -- All bounds and values are static
5879 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
5880 Loc : constant Source_Ptr := Sloc (N);
5881 Typ : constant Entity_Id := Etype (N);
5882 Ctyp : constant Entity_Id := Component_Type (Typ);
5884 Not_Handled : exception;
5885 -- Exception raised if this aggregate cannot be handled
5887 begin
5888 -- For now, handle only one dimensional bit packed arrays
5890 if not Is_Bit_Packed_Array (Typ)
5891 or else Number_Dimensions (Typ) > 1
5892 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
5893 then
5894 return False;
5895 end if;
5897 if not Is_Scalar_Type (Component_Type (Typ))
5898 and then Has_Non_Standard_Rep (Component_Type (Typ))
5899 then
5900 return False;
5901 end if;
5903 declare
5904 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
5906 Lo : Node_Id;
5907 Hi : Node_Id;
5908 -- Bounds of index type
5910 Lob : Uint;
5911 Hib : Uint;
5912 -- Values of bounds if compile time known
5914 function Get_Component_Val (N : Node_Id) return Uint;
5915 -- Given a expression value N of the component type Ctyp, returns a
5916 -- value of Csiz (component size) bits representing this value. If
5917 -- the value is non-static or any other reason exists why the value
5918 -- cannot be returned, then Not_Handled is raised.
5920 -----------------------
5921 -- Get_Component_Val --
5922 -----------------------
5924 function Get_Component_Val (N : Node_Id) return Uint is
5925 Val : Uint;
5927 begin
5928 -- We have to analyze the expression here before doing any further
5929 -- processing here. The analysis of such expressions is deferred
5930 -- till expansion to prevent some problems of premature analysis.
5932 Analyze_And_Resolve (N, Ctyp);
5934 -- Must have a compile time value. String literals have to be
5935 -- converted into temporaries as well, because they cannot easily
5936 -- be converted into their bit representation.
5938 if not Compile_Time_Known_Value (N)
5939 or else Nkind (N) = N_String_Literal
5940 then
5941 raise Not_Handled;
5942 end if;
5944 Val := Expr_Rep_Value (N);
5946 -- Adjust for bias, and strip proper number of bits
5948 if Has_Biased_Representation (Ctyp) then
5949 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
5950 end if;
5952 return Val mod Uint_2 ** Csiz;
5953 end Get_Component_Val;
5955 -- Here we know we have a one dimensional bit packed array
5957 begin
5958 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
5960 -- Cannot do anything if bounds are dynamic
5962 if not Compile_Time_Known_Value (Lo)
5963 or else
5964 not Compile_Time_Known_Value (Hi)
5965 then
5966 return False;
5967 end if;
5969 -- Or are silly out of range of int bounds
5971 Lob := Expr_Value (Lo);
5972 Hib := Expr_Value (Hi);
5974 if not UI_Is_In_Int_Range (Lob)
5975 or else
5976 not UI_Is_In_Int_Range (Hib)
5977 then
5978 return False;
5979 end if;
5981 -- At this stage we have a suitable aggregate for handling at compile
5982 -- time (the only remaining checks are that the values of expressions
5983 -- in the aggregate are compile time known (check is performed by
5984 -- Get_Component_Val), and that any subtypes or ranges are statically
5985 -- known.
5987 -- If the aggregate is not fully positional at this stage, then
5988 -- convert it to positional form. Either this will fail, in which
5989 -- case we can do nothing, or it will succeed, in which case we have
5990 -- succeeded in handling the aggregate, or it will stay an aggregate,
5991 -- in which case we have failed to handle this case.
5993 if Present (Component_Associations (N)) then
5994 Convert_To_Positional
5995 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
5996 return Nkind (N) /= N_Aggregate;
5997 end if;
5999 -- Otherwise we are all positional, so convert to proper value
6001 declare
6002 Lov : constant Int := UI_To_Int (Lob);
6003 Hiv : constant Int := UI_To_Int (Hib);
6005 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6006 -- The length of the array (number of elements)
6008 Aggregate_Val : Uint;
6009 -- Value of aggregate. The value is set in the low order bits of
6010 -- this value. For the little-endian case, the values are stored
6011 -- from low-order to high-order and for the big-endian case the
6012 -- values are stored from high-order to low-order. Note that gigi
6013 -- will take care of the conversions to left justify the value in
6014 -- the big endian case (because of left justified modular type
6015 -- processing), so we do not have to worry about that here.
6017 Lit : Node_Id;
6018 -- Integer literal for resulting constructed value
6020 Shift : Nat;
6021 -- Shift count from low order for next value
6023 Incr : Int;
6024 -- Shift increment for loop
6026 Expr : Node_Id;
6027 -- Next expression from positional parameters of aggregate
6029 begin
6030 -- For little endian, we fill up the low order bits of the target
6031 -- value. For big endian we fill up the high order bits of the
6032 -- target value (which is a left justified modular value).
6034 if Bytes_Big_Endian xor Debug_Flag_8 then
6035 Shift := Csiz * (Len - 1);
6036 Incr := -Csiz;
6037 else
6038 Shift := 0;
6039 Incr := +Csiz;
6040 end if;
6042 -- Loop to set the values
6044 if Len = 0 then
6045 Aggregate_Val := Uint_0;
6046 else
6047 Expr := First (Expressions (N));
6048 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6050 for J in 2 .. Len loop
6051 Shift := Shift + Incr;
6052 Next (Expr);
6053 Aggregate_Val :=
6054 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6055 end loop;
6056 end if;
6058 -- Now we can rewrite with the proper value
6060 Lit :=
6061 Make_Integer_Literal (Loc,
6062 Intval => Aggregate_Val);
6063 Set_Print_In_Hex (Lit);
6065 -- Construct the expression using this literal. Note that it is
6066 -- important to qualify the literal with its proper modular type
6067 -- since universal integer does not have the required range and
6068 -- also this is a left justified modular type, which is important
6069 -- in the big-endian case.
6071 Rewrite (N,
6072 Unchecked_Convert_To (Typ,
6073 Make_Qualified_Expression (Loc,
6074 Subtype_Mark =>
6075 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6076 Expression => Lit)));
6078 Analyze_And_Resolve (N, Typ);
6079 return True;
6080 end;
6081 end;
6083 exception
6084 when Not_Handled =>
6085 return False;
6086 end Packed_Array_Aggregate_Handled;
6088 ----------------------------
6089 -- Has_Mutable_Components --
6090 ----------------------------
6092 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6093 Comp : Entity_Id;
6095 begin
6096 Comp := First_Component (Typ);
6097 while Present (Comp) loop
6098 if Is_Record_Type (Etype (Comp))
6099 and then Has_Discriminants (Etype (Comp))
6100 and then not Is_Constrained (Etype (Comp))
6101 then
6102 return True;
6103 end if;
6105 Next_Component (Comp);
6106 end loop;
6108 return False;
6109 end Has_Mutable_Components;
6111 ------------------------------
6112 -- Initialize_Discriminants --
6113 ------------------------------
6115 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6116 Loc : constant Source_Ptr := Sloc (N);
6117 Bas : constant Entity_Id := Base_Type (Typ);
6118 Par : constant Entity_Id := Etype (Bas);
6119 Decl : constant Node_Id := Parent (Par);
6120 Ref : Node_Id;
6122 begin
6123 if Is_Tagged_Type (Bas)
6124 and then Is_Derived_Type (Bas)
6125 and then Has_Discriminants (Par)
6126 and then Has_Discriminants (Bas)
6127 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6128 and then Nkind (Decl) = N_Full_Type_Declaration
6129 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6130 and then Present
6131 (Variant_Part (Component_List (Type_Definition (Decl))))
6132 and then Nkind (N) /= N_Extension_Aggregate
6133 then
6135 -- Call init proc to set discriminants.
6136 -- There should eventually be a special procedure for this ???
6138 Ref := New_Reference_To (Defining_Identifier (N), Loc);
6139 Insert_Actions_After (N,
6140 Build_Initialization_Call (Sloc (N), Ref, Typ));
6141 end if;
6142 end Initialize_Discriminants;
6144 ----------------
6145 -- Must_Slide --
6146 ----------------
6148 function Must_Slide
6149 (Obj_Type : Entity_Id;
6150 Typ : Entity_Id) return Boolean
6152 L1, L2, H1, H2 : Node_Id;
6153 begin
6154 -- No sliding if the type of the object is not established yet, if it is
6155 -- an unconstrained type whose actual subtype comes from the aggregate,
6156 -- or if the two types are identical.
6158 if not Is_Array_Type (Obj_Type) then
6159 return False;
6161 elsif not Is_Constrained (Obj_Type) then
6162 return False;
6164 elsif Typ = Obj_Type then
6165 return False;
6167 else
6168 -- Sliding can only occur along the first dimension
6170 Get_Index_Bounds (First_Index (Typ), L1, H1);
6171 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6173 if not Is_Static_Expression (L1)
6174 or else not Is_Static_Expression (L2)
6175 or else not Is_Static_Expression (H1)
6176 or else not Is_Static_Expression (H2)
6177 then
6178 return False;
6179 else
6180 return Expr_Value (L1) /= Expr_Value (L2)
6181 or else Expr_Value (H1) /= Expr_Value (H2);
6182 end if;
6183 end if;
6184 end Must_Slide;
6186 ---------------------------
6187 -- Safe_Slice_Assignment --
6188 ---------------------------
6190 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6191 Loc : constant Source_Ptr := Sloc (Parent (N));
6192 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6193 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6194 Expr : Node_Id;
6195 L_J : Entity_Id;
6196 L_Iter : Node_Id;
6197 L_Body : Node_Id;
6198 Stat : Node_Id;
6200 begin
6201 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6203 if Comes_From_Source (N)
6204 and then No (Expressions (N))
6205 and then Nkind (First (Choices (First (Component_Associations (N)))))
6206 = N_Others_Choice
6207 then
6208 Expr :=
6209 Expression (First (Component_Associations (N)));
6210 L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
6212 L_Iter :=
6213 Make_Iteration_Scheme (Loc,
6214 Loop_Parameter_Specification =>
6215 Make_Loop_Parameter_Specification
6216 (Loc,
6217 Defining_Identifier => L_J,
6218 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6220 L_Body :=
6221 Make_Assignment_Statement (Loc,
6222 Name =>
6223 Make_Indexed_Component (Loc,
6224 Prefix => Relocate_Node (Pref),
6225 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6226 Expression => Relocate_Node (Expr));
6228 -- Construct the final loop
6230 Stat :=
6231 Make_Implicit_Loop_Statement
6232 (Node => Parent (N),
6233 Identifier => Empty,
6234 Iteration_Scheme => L_Iter,
6235 Statements => New_List (L_Body));
6237 -- Set type of aggregate to be type of lhs in assignment,
6238 -- to suppress redundant length checks.
6240 Set_Etype (N, Etype (Name (Parent (N))));
6242 Rewrite (Parent (N), Stat);
6243 Analyze (Parent (N));
6244 return True;
6246 else
6247 return False;
6248 end if;
6249 end Safe_Slice_Assignment;
6251 ---------------------
6252 -- Sort_Case_Table --
6253 ---------------------
6255 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6256 L : constant Int := Case_Table'First;
6257 U : constant Int := Case_Table'Last;
6258 K : Int;
6259 J : Int;
6260 T : Case_Bounds;
6262 begin
6263 K := L;
6264 while K /= U loop
6265 T := Case_Table (K + 1);
6267 J := K + 1;
6268 while J /= L
6269 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6270 Expr_Value (T.Choice_Lo)
6271 loop
6272 Case_Table (J) := Case_Table (J - 1);
6273 J := J - 1;
6274 end loop;
6276 Case_Table (J) := T;
6277 K := K + 1;
6278 end loop;
6279 end Sort_Case_Table;
6281 ----------------------------
6282 -- Static_Array_Aggregate --
6283 ----------------------------
6285 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6286 Bounds : constant Node_Id := Aggregate_Bounds (N);
6288 Typ : constant Entity_Id := Etype (N);
6289 Comp_Type : constant Entity_Id := Component_Type (Typ);
6290 Agg : Node_Id;
6291 Expr : Node_Id;
6292 Lo : Node_Id;
6293 Hi : Node_Id;
6295 begin
6296 if Is_Tagged_Type (Typ)
6297 or else Is_Controlled (Typ)
6298 or else Is_Packed (Typ)
6299 then
6300 return False;
6301 end if;
6303 if Present (Bounds)
6304 and then Nkind (Bounds) = N_Range
6305 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6306 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6307 then
6308 Lo := Low_Bound (Bounds);
6309 Hi := High_Bound (Bounds);
6311 if No (Component_Associations (N)) then
6313 -- Verify that all components are static integers
6315 Expr := First (Expressions (N));
6316 while Present (Expr) loop
6317 if Nkind (Expr) /= N_Integer_Literal then
6318 return False;
6319 end if;
6321 Next (Expr);
6322 end loop;
6324 return True;
6326 else
6327 -- We allow only a single named association, either a static
6328 -- range or an others_clause, with a static expression.
6330 Expr := First (Component_Associations (N));
6332 if Present (Expressions (N)) then
6333 return False;
6335 elsif Present (Next (Expr)) then
6336 return False;
6338 elsif Present (Next (First (Choices (Expr)))) then
6339 return False;
6341 else
6342 -- The aggregate is static if all components are literals, or
6343 -- else all its components are static aggregates for the
6344 -- component type.
6346 if Is_Array_Type (Comp_Type)
6347 or else Is_Record_Type (Comp_Type)
6348 then
6349 if Nkind (Expression (Expr)) /= N_Aggregate
6350 or else
6351 not Compile_Time_Known_Aggregate (Expression (Expr))
6352 then
6353 return False;
6354 end if;
6356 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6357 return False;
6358 end if;
6360 -- Create a positional aggregate with the right number of
6361 -- copies of the expression.
6363 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6365 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6366 loop
6367 Append_To
6368 (Expressions (Agg), New_Copy (Expression (Expr)));
6369 Set_Etype (Last (Expressions (Agg)), Component_Type (Typ));
6370 end loop;
6372 Set_Aggregate_Bounds (Agg, Bounds);
6373 Set_Etype (Agg, Typ);
6374 Set_Analyzed (Agg);
6375 Rewrite (N, Agg);
6376 Set_Compile_Time_Known_Aggregate (N);
6378 return True;
6379 end if;
6380 end if;
6382 else
6383 return False;
6384 end if;
6385 end Static_Array_Aggregate;
6386 end Exp_Aggr;