Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / gcc / ada / exp_aggr.adb
blob64d8127e5a2f0092e6aa0b97ba066eb266dfbda2
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ A G G R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Tss; use Exp_Tss;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Lib; use Lib;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Nlists; use Nlists;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Ttypes; use Ttypes;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Res; use Sem_Res;
56 with Sem_Util; use Sem_Util;
57 with Sinfo; use Sinfo;
58 with Snames; use Snames;
59 with Stand; use Stand;
60 with Targparm; use Targparm;
61 with Tbuild; use Tbuild;
62 with Uintp; use Uintp;
64 package body Exp_Aggr is
66 type Case_Bounds is record
67 Choice_Lo : Node_Id;
68 Choice_Hi : Node_Id;
69 Choice_Node : Node_Id;
70 end record;
72 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
73 -- Table type used by Check_Case_Choices procedure
75 function Must_Slide
76 (Obj_Type : Entity_Id;
77 Typ : Entity_Id) return Boolean;
78 -- A static array aggregate in an object declaration can in most cases be
79 -- expanded in place. The one exception is when the aggregate is given
80 -- with component associations that specify different bounds from those of
81 -- the type definition in the object declaration. In this pathological
82 -- case the aggregate must slide, and we must introduce an intermediate
83 -- temporary to hold it.
85 -- The same holds in an assignment to one-dimensional array of arrays,
86 -- when a component may be given with bounds that differ from those of the
87 -- component type.
89 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
90 -- Sort the Case Table using the Lower Bound of each Choice as the key.
91 -- A simple insertion sort is used since the number of choices in a case
92 -- statement of variant part will usually be small and probably in near
93 -- sorted order.
95 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
96 -- N is an aggregate (record or array). Checks the presence of default
97 -- initialization (<>) in any component (Ada 2005: AI-287).
99 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
100 -- Returns true if N is an aggregate used to initialize the components
101 -- of an statically allocated dispatch table.
103 ------------------------------------------------------
104 -- Local subprograms for Record Aggregate Expansion --
105 ------------------------------------------------------
107 procedure Expand_Record_Aggregate
108 (N : Node_Id;
109 Orig_Tag : Node_Id := Empty;
110 Parent_Expr : Node_Id := Empty);
111 -- This is the top level procedure for record aggregate expansion.
112 -- Expansion for record aggregates needs expand aggregates for tagged
113 -- record types. Specifically Expand_Record_Aggregate adds the Tag
114 -- field in front of the Component_Association list that was created
115 -- during resolution by Resolve_Record_Aggregate.
117 -- N is the record aggregate node.
118 -- Orig_Tag is the value of the Tag that has to be provided for this
119 -- specific aggregate. It carries the tag corresponding to the type
120 -- of the outermost aggregate during the recursive expansion
121 -- Parent_Expr is the ancestor part of the original extension
122 -- aggregate
124 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
125 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
126 -- aggregate (which can only be a record type, this procedure is only used
127 -- for record types). Transform the given aggregate into a sequence of
128 -- assignments performed component by component.
130 function Build_Record_Aggr_Code
131 (N : Node_Id;
132 Typ : Entity_Id;
133 Lhs : Node_Id;
134 Flist : Node_Id := Empty;
135 Obj : Entity_Id := Empty;
136 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id;
137 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
138 -- aggregate. Target is an expression containing the location on which the
139 -- component by component assignments will take place. Returns the list of
140 -- assignments plus all other adjustments needed for tagged and controlled
141 -- types. Flist is an expression representing the finalization list on
142 -- which to attach the controlled components if any. Obj is present in the
143 -- object declaration and dynamic allocation cases, it contains an entity
144 -- that allows to know if the value being created needs to be attached to
145 -- the final list in case of pragma Finalize_Storage_Only.
147 -- ???
148 -- The meaning of the Obj formal is extremely unclear. *What* entity
149 -- should be passed? For the object declaration case we may guess that
150 -- this is the object being declared, but what about the allocator case?
152 -- Is_Limited_Ancestor_Expansion indicates that the function has been
153 -- called recursively to expand the limited ancestor to avoid copying it.
155 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
156 -- Return true if one of the component is of a discriminated type with
157 -- defaults. An aggregate for a type with mutable components must be
158 -- expanded into individual assignments.
160 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
161 -- If the type of the aggregate is a type extension with renamed discrimi-
162 -- nants, we must initialize the hidden discriminants of the parent.
163 -- Otherwise, the target object must not be initialized. The discriminants
164 -- are initialized by calling the initialization procedure for the type.
165 -- This is incorrect if the initialization of other components has any
166 -- side effects. We restrict this call to the case where the parent type
167 -- has a variant part, because this is the only case where the hidden
168 -- discriminants are accessed, namely when calling discriminant checking
169 -- functions of the parent type, and when applying a stream attribute to
170 -- an object of the derived type.
172 -----------------------------------------------------
173 -- Local Subprograms for Array Aggregate Expansion --
174 -----------------------------------------------------
176 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
177 -- Very large static aggregates present problems to the back-end, and are
178 -- transformed into assignments and loops. This function verifies that the
179 -- total number of components of an aggregate is acceptable for rewriting
180 -- into a purely positional static form. Aggr_Size_OK must be called before
181 -- calling Flatten.
183 -- This function also detects and warns about one-component aggregates that
184 -- appear in a non-static context. Even if the component value is static,
185 -- such an aggregate must be expanded into an assignment.
187 procedure Convert_Array_Aggr_In_Allocator
188 (Decl : Node_Id;
189 Aggr : Node_Id;
190 Target : Node_Id);
191 -- If the aggregate appears within an allocator and can be expanded in
192 -- place, this routine generates the individual assignments to components
193 -- of the designated object. This is an optimization over the general
194 -- case, where a temporary is first created on the stack and then used to
195 -- construct the allocated object on the heap.
197 procedure Convert_To_Positional
198 (N : Node_Id;
199 Max_Others_Replicate : Nat := 5;
200 Handle_Bit_Packed : Boolean := False);
201 -- If possible, convert named notation to positional notation. This
202 -- conversion is possible only in some static cases. If the conversion is
203 -- possible, then N is rewritten with the analyzed converted aggregate.
204 -- The parameter Max_Others_Replicate controls the maximum number of
205 -- values corresponding to an others choice that will be converted to
206 -- positional notation (the default of 5 is the normal limit, and reflects
207 -- the fact that normally the loop is better than a lot of separate
208 -- assignments). Note that this limit gets overridden in any case if
209 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
210 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
211 -- not expect the back end to handle bit packed arrays, so the normal case
212 -- of conversion is pointless), but in the special case of a call from
213 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
214 -- these are cases we handle in there.
216 procedure Expand_Array_Aggregate (N : Node_Id);
217 -- This is the top-level routine to perform array aggregate expansion.
218 -- N is the N_Aggregate node to be expanded.
220 function Backend_Processing_Possible (N : Node_Id) return Boolean;
221 -- This function checks if array aggregate N can be processed directly
222 -- by the backend. If this is the case True is returned.
224 function Build_Array_Aggr_Code
225 (N : Node_Id;
226 Ctype : Entity_Id;
227 Index : Node_Id;
228 Into : Node_Id;
229 Scalar_Comp : Boolean;
230 Indexes : List_Id := No_List;
231 Flist : Node_Id := Empty) return List_Id;
232 -- This recursive routine returns a list of statements containing the
233 -- loops and assignments that are needed for the expansion of the array
234 -- aggregate N.
236 -- N is the (sub-)aggregate node to be expanded into code. This node
237 -- has been fully analyzed, and its Etype is properly set.
239 -- Index is the index node corresponding to the array sub-aggregate N.
241 -- Into is the target expression into which we are copying the aggregate.
242 -- Note that this node may not have been analyzed yet, and so the Etype
243 -- field may not be set.
245 -- Scalar_Comp is True if the component type of the aggregate is scalar.
247 -- Indexes is the current list of expressions used to index the
248 -- object we are writing into.
250 -- Flist is an expression representing the finalization list on which
251 -- to attach the controlled components if any.
253 function Number_Of_Choices (N : Node_Id) return Nat;
254 -- Returns the number of discrete choices (not including the others choice
255 -- if present) contained in (sub-)aggregate N.
257 function Late_Expansion
258 (N : Node_Id;
259 Typ : Entity_Id;
260 Target : Node_Id;
261 Flist : Node_Id := Empty;
262 Obj : Entity_Id := Empty) return List_Id;
263 -- N is a nested (record or array) aggregate that has been marked with
264 -- 'Delay_Expansion'. Typ is the expected type of the aggregate and Target
265 -- is a (duplicable) expression that will hold the result of the aggregate
266 -- expansion. Flist is the finalization list to be used to attach
267 -- controlled components. 'Obj' when non empty, carries the original
268 -- object being initialized in order to know if it needs to be attached to
269 -- the previous parameter which may not be the case in the case where
270 -- Finalize_Storage_Only is set. Basically this procedure is used to
271 -- implement top-down expansions of nested aggregates. This is necessary
272 -- for avoiding temporaries at each level as well as for propagating the
273 -- right internal finalization list.
275 function Make_OK_Assignment_Statement
276 (Sloc : Source_Ptr;
277 Name : Node_Id;
278 Expression : Node_Id) return Node_Id;
279 -- This is like Make_Assignment_Statement, except that Assignment_OK
280 -- is set in the left operand. All assignments built by this unit
281 -- use this routine. This is needed to deal with assignments to
282 -- initialized constants that are done in place.
284 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
285 -- Given an array aggregate, this function handles the case of a packed
286 -- array aggregate with all constant values, where the aggregate can be
287 -- evaluated at compile time. If this is possible, then N is rewritten
288 -- to be its proper compile time value with all the components properly
289 -- assembled. The expression is analyzed and resolved and True is
290 -- returned. If this transformation is not possible, N is unchanged
291 -- and False is returned
293 function Safe_Slice_Assignment (N : Node_Id) return Boolean;
294 -- If a slice assignment has an aggregate with a single others_choice,
295 -- the assignment can be done in place even if bounds are not static,
296 -- by converting it into a loop over the discrete range of the slice.
298 ------------------
299 -- Aggr_Size_OK --
300 ------------------
302 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
303 Lo : Node_Id;
304 Hi : Node_Id;
305 Indx : Node_Id;
306 Siz : Int;
307 Lov : Uint;
308 Hiv : Uint;
310 -- The following constant determines the maximum size of an
311 -- array aggregate produced by converting named to positional
312 -- notation (e.g. from others clauses). This avoids running
313 -- away with attempts to convert huge aggregates, which hit
314 -- memory limits in the backend.
316 -- The normal limit is 5000, but we increase this limit to
317 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code)
318 -- or Restrictions (No_Implicit_Loops) is specified, since in
319 -- either case, we are at risk of declaring the program illegal
320 -- because of this limit.
322 Max_Aggr_Size : constant Nat :=
323 5000 + (2 ** 24 - 5000) *
324 Boolean'Pos
325 (Restriction_Active (No_Elaboration_Code)
326 or else
327 Restriction_Active (No_Implicit_Loops));
329 function Component_Count (T : Entity_Id) return Int;
330 -- The limit is applied to the total number of components that the
331 -- aggregate will have, which is the number of static expressions
332 -- that will appear in the flattened array. This requires a recursive
333 -- computation of the number of scalar components of the structure.
335 ---------------------
336 -- Component_Count --
337 ---------------------
339 function Component_Count (T : Entity_Id) return Int is
340 Res : Int := 0;
341 Comp : Entity_Id;
343 begin
344 if Is_Scalar_Type (T) then
345 return 1;
347 elsif Is_Record_Type (T) then
348 Comp := First_Component (T);
349 while Present (Comp) loop
350 Res := Res + Component_Count (Etype (Comp));
351 Next_Component (Comp);
352 end loop;
354 return Res;
356 elsif Is_Array_Type (T) then
357 declare
358 Lo : constant Node_Id :=
359 Type_Low_Bound (Etype (First_Index (T)));
360 Hi : constant Node_Id :=
361 Type_High_Bound (Etype (First_Index (T)));
363 Siz : constant Int := Component_Count (Component_Type (T));
365 begin
366 if not Compile_Time_Known_Value (Lo)
367 or else not Compile_Time_Known_Value (Hi)
368 then
369 return 0;
370 else
371 return
372 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
373 end if;
374 end;
376 else
377 -- Can only be a null for an access type
379 return 1;
380 end if;
381 end Component_Count;
383 -- Start of processing for Aggr_Size_OK
385 begin
386 Siz := Component_Count (Component_Type (Typ));
388 Indx := First_Index (Typ);
389 while Present (Indx) loop
390 Lo := Type_Low_Bound (Etype (Indx));
391 Hi := Type_High_Bound (Etype (Indx));
393 -- Bounds need to be known at compile time
395 if not Compile_Time_Known_Value (Lo)
396 or else not Compile_Time_Known_Value (Hi)
397 then
398 return False;
399 end if;
401 Lov := Expr_Value (Lo);
402 Hiv := Expr_Value (Hi);
404 -- A flat array is always safe
406 if Hiv < Lov then
407 return True;
408 end if;
410 -- One-component aggregates are suspicious, and if the context type
411 -- is an object declaration with non-static bounds it will trip gcc;
412 -- such an aggregate must be expanded into a single assignment.
414 if Hiv = Lov
415 and then Nkind (Parent (N)) = N_Object_Declaration
416 then
417 declare
418 Index_Type : constant Entity_Id :=
419 Etype
420 (First_Index
421 (Etype (Defining_Identifier (Parent (N)))));
422 Indx : Node_Id;
424 begin
425 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
426 or else not Compile_Time_Known_Value
427 (Type_High_Bound (Index_Type))
428 then
429 if Present (Component_Associations (N)) then
430 Indx :=
431 First (Choices (First (Component_Associations (N))));
432 if Is_Entity_Name (Indx)
433 and then not Is_Type (Entity (Indx))
434 then
435 Error_Msg_N
436 ("single component aggregate in non-static context?",
437 Indx);
438 Error_Msg_N ("\maybe subtype name was meant?", Indx);
439 end if;
440 end if;
442 return False;
443 end if;
444 end;
445 end if;
447 declare
448 Rng : constant Uint := Hiv - Lov + 1;
450 begin
451 -- Check if size is too large
453 if not UI_Is_In_Int_Range (Rng) then
454 return False;
455 end if;
457 Siz := Siz * UI_To_Int (Rng);
458 end;
460 if Siz <= 0
461 or else Siz > Max_Aggr_Size
462 then
463 return False;
464 end if;
466 -- Bounds must be in integer range, for later array construction
468 if not UI_Is_In_Int_Range (Lov)
469 or else
470 not UI_Is_In_Int_Range (Hiv)
471 then
472 return False;
473 end if;
475 Next_Index (Indx);
476 end loop;
478 return True;
479 end Aggr_Size_OK;
481 ---------------------------------
482 -- Backend_Processing_Possible --
483 ---------------------------------
485 -- Backend processing by Gigi/gcc is possible only if all the following
486 -- conditions are met:
488 -- 1. N is fully positional
490 -- 2. N is not a bit-packed array aggregate;
492 -- 3. The size of N's array type must be known at compile time. Note
493 -- that this implies that the component size is also known
495 -- 4. The array type of N does not follow the Fortran layout convention
496 -- or if it does it must be 1 dimensional.
498 -- 5. The array component type may not be tagged (which could necessitate
499 -- reassignment of proper tags).
501 -- 6. The array component type must not have unaligned bit components
503 -- 7. None of the components of the aggregate may be bit unaligned
504 -- components.
506 -- 8. There cannot be delayed components, since we do not know enough
507 -- at this stage to know if back end processing is possible.
509 -- 9. There cannot be any discriminated record components, since the
510 -- back end cannot handle this complex case.
512 -- 10. No controlled actions need to be generated for components
514 -- 11. For a VM back end, the array should have no aliased components
516 function Backend_Processing_Possible (N : Node_Id) return Boolean is
517 Typ : constant Entity_Id := Etype (N);
518 -- Typ is the correct constrained array subtype of the aggregate
520 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
521 -- This routine checks components of aggregate N, enforcing checks
522 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
523 -- performed on subaggregates. The Index value is the current index
524 -- being checked in the multi-dimensional case.
526 ---------------------
527 -- Component_Check --
528 ---------------------
530 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
531 Expr : Node_Id;
533 begin
534 -- Checks 1: (no component associations)
536 if Present (Component_Associations (N)) then
537 return False;
538 end if;
540 -- Checks on components
542 -- Recurse to check subaggregates, which may appear in qualified
543 -- expressions. If delayed, the front-end will have to expand.
544 -- If the component is a discriminated record, treat as non-static,
545 -- as the back-end cannot handle this properly.
547 Expr := First (Expressions (N));
548 while Present (Expr) loop
550 -- Checks 8: (no delayed components)
552 if Is_Delayed_Aggregate (Expr) then
553 return False;
554 end if;
556 -- Checks 9: (no discriminated records)
558 if Present (Etype (Expr))
559 and then Is_Record_Type (Etype (Expr))
560 and then Has_Discriminants (Etype (Expr))
561 then
562 return False;
563 end if;
565 -- Checks 7. Component must not be bit aligned component
567 if Possible_Bit_Aligned_Component (Expr) then
568 return False;
569 end if;
571 -- Recursion to following indexes for multiple dimension case
573 if Present (Next_Index (Index))
574 and then not Component_Check (Expr, Next_Index (Index))
575 then
576 return False;
577 end if;
579 -- All checks for that component finished, on to next
581 Next (Expr);
582 end loop;
584 return True;
585 end Component_Check;
587 -- Start of processing for Backend_Processing_Possible
589 begin
590 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
592 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
593 return False;
594 end if;
596 -- If component is limited, aggregate must be expanded because each
597 -- component assignment must be built in place.
599 if Is_Immutably_Limited_Type (Component_Type (Typ)) then
600 return False;
601 end if;
603 -- Checks 4 (array must not be multi-dimensional Fortran case)
605 if Convention (Typ) = Convention_Fortran
606 and then Number_Dimensions (Typ) > 1
607 then
608 return False;
609 end if;
611 -- Checks 3 (size of array must be known at compile time)
613 if not Size_Known_At_Compile_Time (Typ) then
614 return False;
615 end if;
617 -- Checks on components
619 if not Component_Check (N, First_Index (Typ)) then
620 return False;
621 end if;
623 -- Checks 5 (if the component type is tagged, then we may need to do
624 -- tag adjustments. Perhaps this should be refined to check for any
625 -- component associations that actually need tag adjustment, similar
626 -- to the test in Component_Not_OK_For_Backend for record aggregates
627 -- with tagged components, but not clear whether it's worthwhile ???;
628 -- in the case of the JVM, object tags are handled implicitly)
630 if Is_Tagged_Type (Component_Type (Typ))
631 and then Tagged_Type_Expansion
632 then
633 return False;
634 end if;
636 -- Checks 6 (component type must not have bit aligned components)
638 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
639 return False;
640 end if;
642 -- Checks 11: Array aggregates with aliased components are currently
643 -- not well supported by the VM backend; disable temporarily this
644 -- backend processing until it is definitely supported.
646 if VM_Target /= No_VM
647 and then Has_Aliased_Components (Base_Type (Typ))
648 then
649 return False;
650 end if;
652 -- Backend processing is possible
654 Set_Size_Known_At_Compile_Time (Etype (N), True);
655 return True;
656 end Backend_Processing_Possible;
658 ---------------------------
659 -- Build_Array_Aggr_Code --
660 ---------------------------
662 -- The code that we generate from a one dimensional aggregate is
664 -- 1. If the sub-aggregate contains discrete choices we
666 -- (a) Sort the discrete choices
668 -- (b) Otherwise for each discrete choice that specifies a range we
669 -- emit a loop. If a range specifies a maximum of three values, or
670 -- we are dealing with an expression we emit a sequence of
671 -- assignments instead of a loop.
673 -- (c) Generate the remaining loops to cover the others choice if any
675 -- 2. If the aggregate contains positional elements we
677 -- (a) translate the positional elements in a series of assignments
679 -- (b) Generate a final loop to cover the others choice if any.
680 -- Note that this final loop has to be a while loop since the case
682 -- L : Integer := Integer'Last;
683 -- H : Integer := Integer'Last;
684 -- A : array (L .. H) := (1, others =>0);
686 -- cannot be handled by a for loop. Thus for the following
688 -- array (L .. H) := (.. positional elements.., others =>E);
690 -- we always generate something like:
692 -- J : Index_Type := Index_Of_Last_Positional_Element;
693 -- while J < H loop
694 -- J := Index_Base'Succ (J)
695 -- Tmp (J) := E;
696 -- end loop;
698 function Build_Array_Aggr_Code
699 (N : Node_Id;
700 Ctype : Entity_Id;
701 Index : Node_Id;
702 Into : Node_Id;
703 Scalar_Comp : Boolean;
704 Indexes : List_Id := No_List;
705 Flist : Node_Id := Empty) return List_Id
707 Loc : constant Source_Ptr := Sloc (N);
708 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
709 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
710 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
712 function Add (Val : Int; To : Node_Id) return Node_Id;
713 -- Returns an expression where Val is added to expression To, unless
714 -- To+Val is provably out of To's base type range. To must be an
715 -- already analyzed expression.
717 function Empty_Range (L, H : Node_Id) return Boolean;
718 -- Returns True if the range defined by L .. H is certainly empty
720 function Equal (L, H : Node_Id) return Boolean;
721 -- Returns True if L = H for sure
723 function Index_Base_Name return Node_Id;
724 -- Returns a new reference to the index type name
726 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
727 -- Ind must be a side-effect free expression. If the input aggregate
728 -- N to Build_Loop contains no sub-aggregates, then this function
729 -- returns the assignment statement:
731 -- Into (Indexes, Ind) := Expr;
733 -- Otherwise we call Build_Code recursively
735 -- Ada 2005 (AI-287): In case of default initialized component, Expr
736 -- is empty and we generate a call to the corresponding IP subprogram.
738 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
739 -- Nodes L and H must be side-effect free expressions.
740 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
741 -- This routine returns the for loop statement
743 -- for J in Index_Base'(L) .. Index_Base'(H) loop
744 -- Into (Indexes, J) := Expr;
745 -- end loop;
747 -- Otherwise we call Build_Code recursively.
748 -- As an optimization if the loop covers 3 or less scalar elements we
749 -- generate a sequence of assignments.
751 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
752 -- Nodes L and H must be side-effect free expressions.
753 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
754 -- This routine returns the while loop statement
756 -- J : Index_Base := L;
757 -- while J < H loop
758 -- J := Index_Base'Succ (J);
759 -- Into (Indexes, J) := Expr;
760 -- end loop;
762 -- Otherwise we call Build_Code recursively
764 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
765 function Local_Expr_Value (E : Node_Id) return Uint;
766 -- These two Local routines are used to replace the corresponding ones
767 -- in sem_eval because while processing the bounds of an aggregate with
768 -- discrete choices whose index type is an enumeration, we build static
769 -- expressions not recognized by Compile_Time_Known_Value as such since
770 -- they have not yet been analyzed and resolved. All the expressions in
771 -- question are things like Index_Base_Name'Val (Const) which we can
772 -- easily recognize as being constant.
774 ---------
775 -- Add --
776 ---------
778 function Add (Val : Int; To : Node_Id) return Node_Id is
779 Expr_Pos : Node_Id;
780 Expr : Node_Id;
781 To_Pos : Node_Id;
782 U_To : Uint;
783 U_Val : constant Uint := UI_From_Int (Val);
785 begin
786 -- Note: do not try to optimize the case of Val = 0, because
787 -- we need to build a new node with the proper Sloc value anyway.
789 -- First test if we can do constant folding
791 if Local_Compile_Time_Known_Value (To) then
792 U_To := Local_Expr_Value (To) + Val;
794 -- Determine if our constant is outside the range of the index.
795 -- If so return an Empty node. This empty node will be caught
796 -- by Empty_Range below.
798 if Compile_Time_Known_Value (Index_Base_L)
799 and then U_To < Expr_Value (Index_Base_L)
800 then
801 return Empty;
803 elsif Compile_Time_Known_Value (Index_Base_H)
804 and then U_To > Expr_Value (Index_Base_H)
805 then
806 return Empty;
807 end if;
809 Expr_Pos := Make_Integer_Literal (Loc, U_To);
810 Set_Is_Static_Expression (Expr_Pos);
812 if not Is_Enumeration_Type (Index_Base) then
813 Expr := Expr_Pos;
815 -- If we are dealing with enumeration return
816 -- Index_Base'Val (Expr_Pos)
818 else
819 Expr :=
820 Make_Attribute_Reference
821 (Loc,
822 Prefix => Index_Base_Name,
823 Attribute_Name => Name_Val,
824 Expressions => New_List (Expr_Pos));
825 end if;
827 return Expr;
828 end if;
830 -- If we are here no constant folding possible
832 if not Is_Enumeration_Type (Index_Base) then
833 Expr :=
834 Make_Op_Add (Loc,
835 Left_Opnd => Duplicate_Subexpr (To),
836 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
838 -- If we are dealing with enumeration return
839 -- Index_Base'Val (Index_Base'Pos (To) + Val)
841 else
842 To_Pos :=
843 Make_Attribute_Reference
844 (Loc,
845 Prefix => Index_Base_Name,
846 Attribute_Name => Name_Pos,
847 Expressions => New_List (Duplicate_Subexpr (To)));
849 Expr_Pos :=
850 Make_Op_Add (Loc,
851 Left_Opnd => To_Pos,
852 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
854 Expr :=
855 Make_Attribute_Reference
856 (Loc,
857 Prefix => Index_Base_Name,
858 Attribute_Name => Name_Val,
859 Expressions => New_List (Expr_Pos));
860 end if;
862 return Expr;
863 end Add;
865 -----------------
866 -- Empty_Range --
867 -----------------
869 function Empty_Range (L, H : Node_Id) return Boolean is
870 Is_Empty : Boolean := False;
871 Low : Node_Id;
872 High : Node_Id;
874 begin
875 -- First check if L or H were already detected as overflowing the
876 -- index base range type by function Add above. If this is so Add
877 -- returns the empty node.
879 if No (L) or else No (H) then
880 return True;
881 end if;
883 for J in 1 .. 3 loop
884 case J is
886 -- L > H range is empty
888 when 1 =>
889 Low := L;
890 High := H;
892 -- B_L > H range must be empty
894 when 2 =>
895 Low := Index_Base_L;
896 High := H;
898 -- L > B_H range must be empty
900 when 3 =>
901 Low := L;
902 High := Index_Base_H;
903 end case;
905 if Local_Compile_Time_Known_Value (Low)
906 and then Local_Compile_Time_Known_Value (High)
907 then
908 Is_Empty :=
909 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
910 end if;
912 exit when Is_Empty;
913 end loop;
915 return Is_Empty;
916 end Empty_Range;
918 -----------
919 -- Equal --
920 -----------
922 function Equal (L, H : Node_Id) return Boolean is
923 begin
924 if L = H then
925 return True;
927 elsif Local_Compile_Time_Known_Value (L)
928 and then Local_Compile_Time_Known_Value (H)
929 then
930 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
931 end if;
933 return False;
934 end Equal;
936 ----------------
937 -- Gen_Assign --
938 ----------------
940 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
941 L : constant List_Id := New_List;
942 F : Entity_Id;
943 A : Node_Id;
945 New_Indexes : List_Id;
946 Indexed_Comp : Node_Id;
947 Expr_Q : Node_Id;
948 Comp_Type : Entity_Id := Empty;
950 function Add_Loop_Actions (Lis : List_Id) return List_Id;
951 -- Collect insert_actions generated in the construction of a
952 -- loop, and prepend them to the sequence of assignments to
953 -- complete the eventual body of the loop.
955 ----------------------
956 -- Add_Loop_Actions --
957 ----------------------
959 function Add_Loop_Actions (Lis : List_Id) return List_Id is
960 Res : List_Id;
962 begin
963 -- Ada 2005 (AI-287): Do nothing else in case of default
964 -- initialized component.
966 if No (Expr) then
967 return Lis;
969 elsif Nkind (Parent (Expr)) = N_Component_Association
970 and then Present (Loop_Actions (Parent (Expr)))
971 then
972 Append_List (Lis, Loop_Actions (Parent (Expr)));
973 Res := Loop_Actions (Parent (Expr));
974 Set_Loop_Actions (Parent (Expr), No_List);
975 return Res;
977 else
978 return Lis;
979 end if;
980 end Add_Loop_Actions;
982 -- Start of processing for Gen_Assign
984 begin
985 if No (Indexes) then
986 New_Indexes := New_List;
987 else
988 New_Indexes := New_Copy_List_Tree (Indexes);
989 end if;
991 Append_To (New_Indexes, Ind);
993 if Present (Flist) then
994 F := New_Copy_Tree (Flist);
996 elsif Present (Etype (N)) and then Needs_Finalization (Etype (N)) then
997 if Is_Entity_Name (Into)
998 and then Present (Scope (Entity (Into)))
999 then
1000 F := Find_Final_List (Scope (Entity (Into)));
1001 else
1002 F := Find_Final_List (Current_Scope);
1003 end if;
1004 else
1005 F := Empty;
1006 end if;
1008 if Present (Next_Index (Index)) then
1009 return
1010 Add_Loop_Actions (
1011 Build_Array_Aggr_Code
1012 (N => Expr,
1013 Ctype => Ctype,
1014 Index => Next_Index (Index),
1015 Into => Into,
1016 Scalar_Comp => Scalar_Comp,
1017 Indexes => New_Indexes,
1018 Flist => F));
1019 end if;
1021 -- If we get here then we are at a bottom-level (sub-)aggregate
1023 Indexed_Comp :=
1024 Checks_Off
1025 (Make_Indexed_Component (Loc,
1026 Prefix => New_Copy_Tree (Into),
1027 Expressions => New_Indexes));
1029 Set_Assignment_OK (Indexed_Comp);
1031 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1032 -- is not present (and therefore we also initialize Expr_Q to empty).
1034 if No (Expr) then
1035 Expr_Q := Empty;
1036 elsif Nkind (Expr) = N_Qualified_Expression then
1037 Expr_Q := Expression (Expr);
1038 else
1039 Expr_Q := Expr;
1040 end if;
1042 if Present (Etype (N))
1043 and then Etype (N) /= Any_Composite
1044 then
1045 Comp_Type := Component_Type (Etype (N));
1046 pragma Assert (Comp_Type = Ctype); -- AI-287
1048 elsif Present (Next (First (New_Indexes))) then
1050 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1051 -- component because we have received the component type in
1052 -- the formal parameter Ctype.
1054 -- ??? Some assert pragmas have been added to check if this new
1055 -- formal can be used to replace this code in all cases.
1057 if Present (Expr) then
1059 -- This is a multidimensional array. Recover the component
1060 -- type from the outermost aggregate, because subaggregates
1061 -- do not have an assigned type.
1063 declare
1064 P : Node_Id;
1066 begin
1067 P := Parent (Expr);
1068 while Present (P) loop
1069 if Nkind (P) = N_Aggregate
1070 and then Present (Etype (P))
1071 then
1072 Comp_Type := Component_Type (Etype (P));
1073 exit;
1075 else
1076 P := Parent (P);
1077 end if;
1078 end loop;
1080 pragma Assert (Comp_Type = Ctype); -- AI-287
1081 end;
1082 end if;
1083 end if;
1085 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1086 -- default initialized components (otherwise Expr_Q is not present).
1088 if Present (Expr_Q)
1089 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1090 then
1091 -- At this stage the Expression may not have been analyzed yet
1092 -- because the array aggregate code has not been updated to use
1093 -- the Expansion_Delayed flag and avoid analysis altogether to
1094 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1095 -- the analysis of non-array aggregates now in order to get the
1096 -- value of Expansion_Delayed flag for the inner aggregate ???
1098 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1099 Analyze_And_Resolve (Expr_Q, Comp_Type);
1100 end if;
1102 if Is_Delayed_Aggregate (Expr_Q) then
1104 -- This is either a subaggregate of a multidimensional array,
1105 -- or a component of an array type whose component type is
1106 -- also an array. In the latter case, the expression may have
1107 -- component associations that provide different bounds from
1108 -- those of the component type, and sliding must occur. Instead
1109 -- of decomposing the current aggregate assignment, force the
1110 -- re-analysis of the assignment, so that a temporary will be
1111 -- generated in the usual fashion, and sliding will take place.
1113 if Nkind (Parent (N)) = N_Assignment_Statement
1114 and then Is_Array_Type (Comp_Type)
1115 and then Present (Component_Associations (Expr_Q))
1116 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1117 then
1118 Set_Expansion_Delayed (Expr_Q, False);
1119 Set_Analyzed (Expr_Q, False);
1121 else
1122 return
1123 Add_Loop_Actions (
1124 Late_Expansion (
1125 Expr_Q, Etype (Expr_Q), Indexed_Comp, F));
1126 end if;
1127 end if;
1128 end if;
1130 -- Ada 2005 (AI-287): In case of default initialized component, call
1131 -- the initialization subprogram associated with the component type.
1132 -- If the component type is an access type, add an explicit null
1133 -- assignment, because for the back-end there is an initialization
1134 -- present for the whole aggregate, and no default initialization
1135 -- will take place.
1137 -- In addition, if the component type is controlled, we must call
1138 -- its Initialize procedure explicitly, because there is no explicit
1139 -- object creation that will invoke it otherwise.
1141 if No (Expr) then
1142 if Present (Base_Init_Proc (Base_Type (Ctype)))
1143 or else Has_Task (Base_Type (Ctype))
1144 then
1145 Append_List_To (L,
1146 Build_Initialization_Call (Loc,
1147 Id_Ref => Indexed_Comp,
1148 Typ => Ctype,
1149 With_Default_Init => True));
1151 elsif Is_Access_Type (Ctype) then
1152 Append_To (L,
1153 Make_Assignment_Statement (Loc,
1154 Name => Indexed_Comp,
1155 Expression => Make_Null (Loc)));
1156 end if;
1158 if Needs_Finalization (Ctype) then
1159 Append_List_To (L,
1160 Make_Init_Call (
1161 Ref => New_Copy_Tree (Indexed_Comp),
1162 Typ => Ctype,
1163 Flist_Ref => Find_Final_List (Current_Scope),
1164 With_Attach => Make_Integer_Literal (Loc, 1)));
1165 end if;
1167 else
1168 -- Now generate the assignment with no associated controlled
1169 -- actions since the target of the assignment may not have been
1170 -- initialized, it is not possible to Finalize it as expected by
1171 -- normal controlled assignment. The rest of the controlled
1172 -- actions are done manually with the proper finalization list
1173 -- coming from the context.
1175 A :=
1176 Make_OK_Assignment_Statement (Loc,
1177 Name => Indexed_Comp,
1178 Expression => New_Copy_Tree (Expr));
1180 if Present (Comp_Type) and then Needs_Finalization (Comp_Type) then
1181 Set_No_Ctrl_Actions (A);
1183 -- If this is an aggregate for an array of arrays, each
1184 -- sub-aggregate will be expanded as well, and even with
1185 -- No_Ctrl_Actions the assignments of inner components will
1186 -- require attachment in their assignments to temporaries.
1187 -- These temporaries must be finalized for each subaggregate,
1188 -- to prevent multiple attachments of the same temporary
1189 -- location to same finalization chain (and consequently
1190 -- circular lists). To ensure that finalization takes place
1191 -- for each subaggregate we wrap the assignment in a block.
1193 if Is_Array_Type (Comp_Type)
1194 and then Nkind (Expr) = N_Aggregate
1195 then
1196 A :=
1197 Make_Block_Statement (Loc,
1198 Handled_Statement_Sequence =>
1199 Make_Handled_Sequence_Of_Statements (Loc,
1200 Statements => New_List (A)));
1201 end if;
1202 end if;
1204 Append_To (L, A);
1206 -- Adjust the tag if tagged (because of possible view
1207 -- conversions), unless compiling for a VM where
1208 -- tags are implicit.
1210 if Present (Comp_Type)
1211 and then Is_Tagged_Type (Comp_Type)
1212 and then Tagged_Type_Expansion
1213 then
1214 A :=
1215 Make_OK_Assignment_Statement (Loc,
1216 Name =>
1217 Make_Selected_Component (Loc,
1218 Prefix => New_Copy_Tree (Indexed_Comp),
1219 Selector_Name =>
1220 New_Reference_To
1221 (First_Tag_Component (Comp_Type), Loc)),
1223 Expression =>
1224 Unchecked_Convert_To (RTE (RE_Tag),
1225 New_Reference_To
1226 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
1227 Loc)));
1229 Append_To (L, A);
1230 end if;
1232 -- Adjust and attach the component to the proper final list, which
1233 -- can be the controller of the outer record object or the final
1234 -- list associated with the scope.
1236 -- If the component is itself an array of controlled types, whose
1237 -- value is given by a sub-aggregate, then the attach calls have
1238 -- been generated when individual subcomponent are assigned, and
1239 -- must not be done again to prevent malformed finalization chains
1240 -- (see comments above, concerning the creation of a block to hold
1241 -- inner finalization actions).
1243 if Present (Comp_Type)
1244 and then Needs_Finalization (Comp_Type)
1245 and then not Is_Limited_Type (Comp_Type)
1246 and then not
1247 (Is_Array_Type (Comp_Type)
1248 and then Is_Controlled (Component_Type (Comp_Type))
1249 and then Nkind (Expr) = N_Aggregate)
1250 then
1251 Append_List_To (L,
1252 Make_Adjust_Call (
1253 Ref => New_Copy_Tree (Indexed_Comp),
1254 Typ => Comp_Type,
1255 Flist_Ref => F,
1256 With_Attach => Make_Integer_Literal (Loc, 1)));
1257 end if;
1258 end if;
1260 return Add_Loop_Actions (L);
1261 end Gen_Assign;
1263 --------------
1264 -- Gen_Loop --
1265 --------------
1267 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1268 L_J : Node_Id;
1270 L_L : Node_Id;
1271 -- Index_Base'(L)
1273 L_H : Node_Id;
1274 -- Index_Base'(H)
1276 L_Range : Node_Id;
1277 -- Index_Base'(L) .. Index_Base'(H)
1279 L_Iteration_Scheme : Node_Id;
1280 -- L_J in Index_Base'(L) .. Index_Base'(H)
1282 L_Body : List_Id;
1283 -- The statements to execute in the loop
1285 S : constant List_Id := New_List;
1286 -- List of statements
1288 Tcopy : Node_Id;
1289 -- Copy of expression tree, used for checking purposes
1291 begin
1292 -- If loop bounds define an empty range return the null statement
1294 if Empty_Range (L, H) then
1295 Append_To (S, Make_Null_Statement (Loc));
1297 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1298 -- default initialized component.
1300 if No (Expr) then
1301 null;
1303 else
1304 -- The expression must be type-checked even though no component
1305 -- of the aggregate will have this value. This is done only for
1306 -- actual components of the array, not for subaggregates. Do
1307 -- the check on a copy, because the expression may be shared
1308 -- among several choices, some of which might be non-null.
1310 if Present (Etype (N))
1311 and then Is_Array_Type (Etype (N))
1312 and then No (Next_Index (Index))
1313 then
1314 Expander_Mode_Save_And_Set (False);
1315 Tcopy := New_Copy_Tree (Expr);
1316 Set_Parent (Tcopy, N);
1317 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1318 Expander_Mode_Restore;
1319 end if;
1320 end if;
1322 return S;
1324 -- If loop bounds are the same then generate an assignment
1326 elsif Equal (L, H) then
1327 return Gen_Assign (New_Copy_Tree (L), Expr);
1329 -- If H - L <= 2 then generate a sequence of assignments when we are
1330 -- processing the bottom most aggregate and it contains scalar
1331 -- components.
1333 elsif No (Next_Index (Index))
1334 and then Scalar_Comp
1335 and then Local_Compile_Time_Known_Value (L)
1336 and then Local_Compile_Time_Known_Value (H)
1337 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1338 then
1340 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1341 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1343 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1344 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1345 end if;
1347 return S;
1348 end if;
1350 -- Otherwise construct the loop, starting with the loop index L_J
1352 L_J := Make_Temporary (Loc, 'J', L);
1354 -- Construct "L .. H" in Index_Base. We use a qualified expression
1355 -- for the bound to convert to the index base, but we don't need
1356 -- to do that if we already have the base type at hand.
1358 if Etype (L) = Index_Base then
1359 L_L := L;
1360 else
1361 L_L :=
1362 Make_Qualified_Expression (Loc,
1363 Subtype_Mark => Index_Base_Name,
1364 Expression => L);
1365 end if;
1367 if Etype (H) = Index_Base then
1368 L_H := H;
1369 else
1370 L_H :=
1371 Make_Qualified_Expression (Loc,
1372 Subtype_Mark => Index_Base_Name,
1373 Expression => H);
1374 end if;
1376 L_Range :=
1377 Make_Range (Loc,
1378 Low_Bound => L_L,
1379 High_Bound => L_H);
1381 -- Construct "for L_J in Index_Base range L .. H"
1383 L_Iteration_Scheme :=
1384 Make_Iteration_Scheme
1385 (Loc,
1386 Loop_Parameter_Specification =>
1387 Make_Loop_Parameter_Specification
1388 (Loc,
1389 Defining_Identifier => L_J,
1390 Discrete_Subtype_Definition => L_Range));
1392 -- Construct the statements to execute in the loop body
1394 L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr);
1396 -- Construct the final loop
1398 Append_To (S, Make_Implicit_Loop_Statement
1399 (Node => N,
1400 Identifier => Empty,
1401 Iteration_Scheme => L_Iteration_Scheme,
1402 Statements => L_Body));
1404 -- A small optimization: if the aggregate is initialized with a box
1405 -- and the component type has no initialization procedure, remove the
1406 -- useless empty loop.
1408 if Nkind (First (S)) = N_Loop_Statement
1409 and then Is_Empty_List (Statements (First (S)))
1410 then
1411 return New_List (Make_Null_Statement (Loc));
1412 else
1413 return S;
1414 end if;
1415 end Gen_Loop;
1417 ---------------
1418 -- Gen_While --
1419 ---------------
1421 -- The code built is
1423 -- W_J : Index_Base := L;
1424 -- while W_J < H loop
1425 -- W_J := Index_Base'Succ (W);
1426 -- L_Body;
1427 -- end loop;
1429 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1430 W_J : Node_Id;
1432 W_Decl : Node_Id;
1433 -- W_J : Base_Type := L;
1435 W_Iteration_Scheme : Node_Id;
1436 -- while W_J < H
1438 W_Index_Succ : Node_Id;
1439 -- Index_Base'Succ (J)
1441 W_Increment : Node_Id;
1442 -- W_J := Index_Base'Succ (W)
1444 W_Body : constant List_Id := New_List;
1445 -- The statements to execute in the loop
1447 S : constant List_Id := New_List;
1448 -- list of statement
1450 begin
1451 -- If loop bounds define an empty range or are equal return null
1453 if Empty_Range (L, H) or else Equal (L, H) then
1454 Append_To (S, Make_Null_Statement (Loc));
1455 return S;
1456 end if;
1458 -- Build the decl of W_J
1460 W_J := Make_Temporary (Loc, 'J', L);
1461 W_Decl :=
1462 Make_Object_Declaration
1463 (Loc,
1464 Defining_Identifier => W_J,
1465 Object_Definition => Index_Base_Name,
1466 Expression => L);
1468 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1469 -- that in this particular case L is a fresh Expr generated by
1470 -- Add which we are the only ones to use.
1472 Append_To (S, W_Decl);
1474 -- Construct " while W_J < H"
1476 W_Iteration_Scheme :=
1477 Make_Iteration_Scheme
1478 (Loc,
1479 Condition => Make_Op_Lt
1480 (Loc,
1481 Left_Opnd => New_Reference_To (W_J, Loc),
1482 Right_Opnd => New_Copy_Tree (H)));
1484 -- Construct the statements to execute in the loop body
1486 W_Index_Succ :=
1487 Make_Attribute_Reference
1488 (Loc,
1489 Prefix => Index_Base_Name,
1490 Attribute_Name => Name_Succ,
1491 Expressions => New_List (New_Reference_To (W_J, Loc)));
1493 W_Increment :=
1494 Make_OK_Assignment_Statement
1495 (Loc,
1496 Name => New_Reference_To (W_J, Loc),
1497 Expression => W_Index_Succ);
1499 Append_To (W_Body, W_Increment);
1500 Append_List_To (W_Body,
1501 Gen_Assign (New_Reference_To (W_J, Loc), Expr));
1503 -- Construct the final loop
1505 Append_To (S, Make_Implicit_Loop_Statement
1506 (Node => N,
1507 Identifier => Empty,
1508 Iteration_Scheme => W_Iteration_Scheme,
1509 Statements => W_Body));
1511 return S;
1512 end Gen_While;
1514 ---------------------
1515 -- Index_Base_Name --
1516 ---------------------
1518 function Index_Base_Name return Node_Id is
1519 begin
1520 return New_Reference_To (Index_Base, Sloc (N));
1521 end Index_Base_Name;
1523 ------------------------------------
1524 -- Local_Compile_Time_Known_Value --
1525 ------------------------------------
1527 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1528 begin
1529 return Compile_Time_Known_Value (E)
1530 or else
1531 (Nkind (E) = N_Attribute_Reference
1532 and then Attribute_Name (E) = Name_Val
1533 and then Compile_Time_Known_Value (First (Expressions (E))));
1534 end Local_Compile_Time_Known_Value;
1536 ----------------------
1537 -- Local_Expr_Value --
1538 ----------------------
1540 function Local_Expr_Value (E : Node_Id) return Uint is
1541 begin
1542 if Compile_Time_Known_Value (E) then
1543 return Expr_Value (E);
1544 else
1545 return Expr_Value (First (Expressions (E)));
1546 end if;
1547 end Local_Expr_Value;
1549 -- Build_Array_Aggr_Code Variables
1551 Assoc : Node_Id;
1552 Choice : Node_Id;
1553 Expr : Node_Id;
1554 Typ : Entity_Id;
1556 Others_Expr : Node_Id := Empty;
1557 Others_Box_Present : Boolean := False;
1559 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1560 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1561 -- The aggregate bounds of this specific sub-aggregate. Note that if
1562 -- the code generated by Build_Array_Aggr_Code is executed then these
1563 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1565 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1566 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1567 -- After Duplicate_Subexpr these are side-effect free
1569 Low : Node_Id;
1570 High : Node_Id;
1572 Nb_Choices : Nat := 0;
1573 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1574 -- Used to sort all the different choice values
1576 Nb_Elements : Int;
1577 -- Number of elements in the positional aggregate
1579 New_Code : constant List_Id := New_List;
1581 -- Start of processing for Build_Array_Aggr_Code
1583 begin
1584 -- First before we start, a special case. if we have a bit packed
1585 -- array represented as a modular type, then clear the value to
1586 -- zero first, to ensure that unused bits are properly cleared.
1588 Typ := Etype (N);
1590 if Present (Typ)
1591 and then Is_Bit_Packed_Array (Typ)
1592 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ))
1593 then
1594 Append_To (New_Code,
1595 Make_Assignment_Statement (Loc,
1596 Name => New_Copy_Tree (Into),
1597 Expression =>
1598 Unchecked_Convert_To (Typ,
1599 Make_Integer_Literal (Loc, Uint_0))));
1600 end if;
1602 -- If the component type contains tasks, we need to build a Master
1603 -- entity in the current scope, because it will be needed if build-
1604 -- in-place functions are called in the expanded code.
1606 if Nkind (Parent (N)) = N_Object_Declaration
1607 and then Has_Task (Typ)
1608 then
1609 Build_Master_Entity (Defining_Identifier (Parent (N)));
1610 end if;
1612 -- STEP 1: Process component associations
1614 -- For those associations that may generate a loop, initialize
1615 -- Loop_Actions to collect inserted actions that may be crated.
1617 -- Skip this if no component associations
1619 if No (Expressions (N)) then
1621 -- STEP 1 (a): Sort the discrete choices
1623 Assoc := First (Component_Associations (N));
1624 while Present (Assoc) loop
1625 Choice := First (Choices (Assoc));
1626 while Present (Choice) loop
1627 if Nkind (Choice) = N_Others_Choice then
1628 Set_Loop_Actions (Assoc, New_List);
1630 if Box_Present (Assoc) then
1631 Others_Box_Present := True;
1632 else
1633 Others_Expr := Expression (Assoc);
1634 end if;
1635 exit;
1636 end if;
1638 Get_Index_Bounds (Choice, Low, High);
1640 if Low /= High then
1641 Set_Loop_Actions (Assoc, New_List);
1642 end if;
1644 Nb_Choices := Nb_Choices + 1;
1645 if Box_Present (Assoc) then
1646 Table (Nb_Choices) := (Choice_Lo => Low,
1647 Choice_Hi => High,
1648 Choice_Node => Empty);
1649 else
1650 Table (Nb_Choices) := (Choice_Lo => Low,
1651 Choice_Hi => High,
1652 Choice_Node => Expression (Assoc));
1653 end if;
1654 Next (Choice);
1655 end loop;
1657 Next (Assoc);
1658 end loop;
1660 -- If there is more than one set of choices these must be static
1661 -- and we can therefore sort them. Remember that Nb_Choices does not
1662 -- account for an others choice.
1664 if Nb_Choices > 1 then
1665 Sort_Case_Table (Table);
1666 end if;
1668 -- STEP 1 (b): take care of the whole set of discrete choices
1670 for J in 1 .. Nb_Choices loop
1671 Low := Table (J).Choice_Lo;
1672 High := Table (J).Choice_Hi;
1673 Expr := Table (J).Choice_Node;
1674 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1675 end loop;
1677 -- STEP 1 (c): generate the remaining loops to cover others choice
1678 -- We don't need to generate loops over empty gaps, but if there is
1679 -- a single empty range we must analyze the expression for semantics
1681 if Present (Others_Expr) or else Others_Box_Present then
1682 declare
1683 First : Boolean := True;
1685 begin
1686 for J in 0 .. Nb_Choices loop
1687 if J = 0 then
1688 Low := Aggr_Low;
1689 else
1690 Low := Add (1, To => Table (J).Choice_Hi);
1691 end if;
1693 if J = Nb_Choices then
1694 High := Aggr_High;
1695 else
1696 High := Add (-1, To => Table (J + 1).Choice_Lo);
1697 end if;
1699 -- If this is an expansion within an init proc, make
1700 -- sure that discriminant references are replaced by
1701 -- the corresponding discriminal.
1703 if Inside_Init_Proc then
1704 if Is_Entity_Name (Low)
1705 and then Ekind (Entity (Low)) = E_Discriminant
1706 then
1707 Set_Entity (Low, Discriminal (Entity (Low)));
1708 end if;
1710 if Is_Entity_Name (High)
1711 and then Ekind (Entity (High)) = E_Discriminant
1712 then
1713 Set_Entity (High, Discriminal (Entity (High)));
1714 end if;
1715 end if;
1717 if First
1718 or else not Empty_Range (Low, High)
1719 then
1720 First := False;
1721 Append_List
1722 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1723 end if;
1724 end loop;
1725 end;
1726 end if;
1728 -- STEP 2: Process positional components
1730 else
1731 -- STEP 2 (a): Generate the assignments for each positional element
1732 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1733 -- Aggr_L is analyzed and Add wants an analyzed expression.
1735 Expr := First (Expressions (N));
1736 Nb_Elements := -1;
1737 while Present (Expr) loop
1738 Nb_Elements := Nb_Elements + 1;
1739 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1740 To => New_Code);
1741 Next (Expr);
1742 end loop;
1744 -- STEP 2 (b): Generate final loop if an others choice is present
1745 -- Here Nb_Elements gives the offset of the last positional element.
1747 if Present (Component_Associations (N)) then
1748 Assoc := Last (Component_Associations (N));
1750 -- Ada 2005 (AI-287)
1752 if Box_Present (Assoc) then
1753 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1754 Aggr_High,
1755 Empty),
1756 To => New_Code);
1757 else
1758 Expr := Expression (Assoc);
1760 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1761 Aggr_High,
1762 Expr), -- AI-287
1763 To => New_Code);
1764 end if;
1765 end if;
1766 end if;
1768 return New_Code;
1769 end Build_Array_Aggr_Code;
1771 ----------------------------
1772 -- Build_Record_Aggr_Code --
1773 ----------------------------
1775 function Build_Record_Aggr_Code
1776 (N : Node_Id;
1777 Typ : Entity_Id;
1778 Lhs : Node_Id;
1779 Flist : Node_Id := Empty;
1780 Obj : Entity_Id := Empty;
1781 Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id
1783 Loc : constant Source_Ptr := Sloc (N);
1784 L : constant List_Id := New_List;
1785 N_Typ : constant Entity_Id := Etype (N);
1787 Comp : Node_Id;
1788 Instr : Node_Id;
1789 Ref : Node_Id;
1790 Target : Entity_Id;
1791 F : Node_Id;
1792 Comp_Type : Entity_Id;
1793 Selector : Entity_Id;
1794 Comp_Expr : Node_Id;
1795 Expr_Q : Node_Id;
1797 Internal_Final_List : Node_Id := Empty;
1799 -- If this is an internal aggregate, the External_Final_List is an
1800 -- expression for the controller record of the enclosing type.
1802 -- If the current aggregate has several controlled components, this
1803 -- expression will appear in several calls to attach to the finali-
1804 -- zation list, and it must not be shared.
1806 External_Final_List : Node_Id;
1807 Ancestor_Is_Expression : Boolean := False;
1808 Ancestor_Is_Subtype_Mark : Boolean := False;
1810 Init_Typ : Entity_Id := Empty;
1811 Attach : Node_Id;
1813 Ctrl_Stuff_Done : Boolean := False;
1814 -- True if Gen_Ctrl_Actions_For_Aggr has already been called; calls
1815 -- after the first do nothing.
1817 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1818 -- Returns the value that the given discriminant of an ancestor type
1819 -- should receive (in the absence of a conflict with the value provided
1820 -- by an ancestor part of an extension aggregate).
1822 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1823 -- Check that each of the discriminant values defined by the ancestor
1824 -- part of an extension aggregate match the corresponding values
1825 -- provided by either an association of the aggregate or by the
1826 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1828 function Compatible_Int_Bounds
1829 (Agg_Bounds : Node_Id;
1830 Typ_Bounds : Node_Id) return Boolean;
1831 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1832 -- assumed that both bounds are integer ranges.
1834 procedure Gen_Ctrl_Actions_For_Aggr;
1835 -- Deal with the various controlled type data structure initializations
1836 -- (but only if it hasn't been done already).
1838 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1839 -- Returns the first discriminant association in the constraint
1840 -- associated with T, if any, otherwise returns Empty.
1842 function Init_Controller
1843 (Target : Node_Id;
1844 Typ : Entity_Id;
1845 F : Node_Id;
1846 Attach : Node_Id;
1847 Init_Pr : Boolean) return List_Id;
1848 -- Returns the list of statements necessary to initialize the internal
1849 -- controller of the (possible) ancestor typ into target and attach it
1850 -- to finalization list F. Init_Pr conditions the call to the init proc
1851 -- since it may already be done due to ancestor initialization.
1853 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1854 -- Check whether Bounds is a range node and its lower and higher bounds
1855 -- are integers literals.
1857 ---------------------------------
1858 -- Ancestor_Discriminant_Value --
1859 ---------------------------------
1861 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1862 Assoc : Node_Id;
1863 Assoc_Elmt : Elmt_Id;
1864 Aggr_Comp : Entity_Id;
1865 Corresp_Disc : Entity_Id;
1866 Current_Typ : Entity_Id := Base_Type (Typ);
1867 Parent_Typ : Entity_Id;
1868 Parent_Disc : Entity_Id;
1869 Save_Assoc : Node_Id := Empty;
1871 begin
1872 -- First check any discriminant associations to see if any of them
1873 -- provide a value for the discriminant.
1875 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1876 Assoc := First (Component_Associations (N));
1877 while Present (Assoc) loop
1878 Aggr_Comp := Entity (First (Choices (Assoc)));
1880 if Ekind (Aggr_Comp) = E_Discriminant then
1881 Save_Assoc := Expression (Assoc);
1883 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1884 while Present (Corresp_Disc) loop
1886 -- If found a corresponding discriminant then return the
1887 -- value given in the aggregate. (Note: this is not
1888 -- correct in the presence of side effects. ???)
1890 if Disc = Corresp_Disc then
1891 return Duplicate_Subexpr (Expression (Assoc));
1892 end if;
1894 Corresp_Disc :=
1895 Corresponding_Discriminant (Corresp_Disc);
1896 end loop;
1897 end if;
1899 Next (Assoc);
1900 end loop;
1901 end if;
1903 -- No match found in aggregate, so chain up parent types to find
1904 -- a constraint that defines the value of the discriminant.
1906 Parent_Typ := Etype (Current_Typ);
1907 while Current_Typ /= Parent_Typ loop
1908 if Has_Discriminants (Parent_Typ)
1909 and then not Has_Unknown_Discriminants (Parent_Typ)
1910 then
1911 Parent_Disc := First_Discriminant (Parent_Typ);
1913 -- We either get the association from the subtype indication
1914 -- of the type definition itself, or from the discriminant
1915 -- constraint associated with the type entity (which is
1916 -- preferable, but it's not always present ???)
1918 if Is_Empty_Elmt_List (
1919 Discriminant_Constraint (Current_Typ))
1920 then
1921 Assoc := Get_Constraint_Association (Current_Typ);
1922 Assoc_Elmt := No_Elmt;
1923 else
1924 Assoc_Elmt :=
1925 First_Elmt (Discriminant_Constraint (Current_Typ));
1926 Assoc := Node (Assoc_Elmt);
1927 end if;
1929 -- Traverse the discriminants of the parent type looking
1930 -- for one that corresponds.
1932 while Present (Parent_Disc) and then Present (Assoc) loop
1933 Corresp_Disc := Parent_Disc;
1934 while Present (Corresp_Disc)
1935 and then Disc /= Corresp_Disc
1936 loop
1937 Corresp_Disc :=
1938 Corresponding_Discriminant (Corresp_Disc);
1939 end loop;
1941 if Disc = Corresp_Disc then
1942 if Nkind (Assoc) = N_Discriminant_Association then
1943 Assoc := Expression (Assoc);
1944 end if;
1946 -- If the located association directly denotes a
1947 -- discriminant, then use the value of a saved
1948 -- association of the aggregate. This is a kludge to
1949 -- handle certain cases involving multiple discriminants
1950 -- mapped to a single discriminant of a descendant. It's
1951 -- not clear how to locate the appropriate discriminant
1952 -- value for such cases. ???
1954 if Is_Entity_Name (Assoc)
1955 and then Ekind (Entity (Assoc)) = E_Discriminant
1956 then
1957 Assoc := Save_Assoc;
1958 end if;
1960 return Duplicate_Subexpr (Assoc);
1961 end if;
1963 Next_Discriminant (Parent_Disc);
1965 if No (Assoc_Elmt) then
1966 Next (Assoc);
1967 else
1968 Next_Elmt (Assoc_Elmt);
1969 if Present (Assoc_Elmt) then
1970 Assoc := Node (Assoc_Elmt);
1971 else
1972 Assoc := Empty;
1973 end if;
1974 end if;
1975 end loop;
1976 end if;
1978 Current_Typ := Parent_Typ;
1979 Parent_Typ := Etype (Current_Typ);
1980 end loop;
1982 -- In some cases there's no ancestor value to locate (such as
1983 -- when an ancestor part given by an expression defines the
1984 -- discriminant value).
1986 return Empty;
1987 end Ancestor_Discriminant_Value;
1989 ----------------------------------
1990 -- Check_Ancestor_Discriminants --
1991 ----------------------------------
1993 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1994 Discr : Entity_Id;
1995 Disc_Value : Node_Id;
1996 Cond : Node_Id;
1998 begin
1999 Discr := First_Discriminant (Base_Type (Anc_Typ));
2000 while Present (Discr) loop
2001 Disc_Value := Ancestor_Discriminant_Value (Discr);
2003 if Present (Disc_Value) then
2004 Cond := Make_Op_Ne (Loc,
2005 Left_Opnd =>
2006 Make_Selected_Component (Loc,
2007 Prefix => New_Copy_Tree (Target),
2008 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2009 Right_Opnd => Disc_Value);
2011 Append_To (L,
2012 Make_Raise_Constraint_Error (Loc,
2013 Condition => Cond,
2014 Reason => CE_Discriminant_Check_Failed));
2015 end if;
2017 Next_Discriminant (Discr);
2018 end loop;
2019 end Check_Ancestor_Discriminants;
2021 ---------------------------
2022 -- Compatible_Int_Bounds --
2023 ---------------------------
2025 function Compatible_Int_Bounds
2026 (Agg_Bounds : Node_Id;
2027 Typ_Bounds : Node_Id) return Boolean
2029 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2030 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2031 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2032 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2033 begin
2034 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2035 end Compatible_Int_Bounds;
2037 --------------------------------
2038 -- Get_Constraint_Association --
2039 --------------------------------
2041 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2042 Typ_Def : constant Node_Id := Type_Definition (Parent (T));
2043 Indic : constant Node_Id := Subtype_Indication (Typ_Def);
2045 begin
2046 -- ??? Also need to cover case of a type mark denoting a subtype
2047 -- with constraint.
2049 if Nkind (Indic) = N_Subtype_Indication
2050 and then Present (Constraint (Indic))
2051 then
2052 return First (Constraints (Constraint (Indic)));
2053 end if;
2055 return Empty;
2056 end Get_Constraint_Association;
2058 ---------------------
2059 -- Init_Controller --
2060 ---------------------
2062 function Init_Controller
2063 (Target : Node_Id;
2064 Typ : Entity_Id;
2065 F : Node_Id;
2066 Attach : Node_Id;
2067 Init_Pr : Boolean) return List_Id
2069 L : constant List_Id := New_List;
2070 Ref : Node_Id;
2071 RC : RE_Id;
2072 Target_Type : Entity_Id;
2074 begin
2075 -- Generate:
2076 -- init-proc (target._controller);
2077 -- initialize (target._controller);
2078 -- Attach_to_Final_List (target._controller, F);
2080 Ref :=
2081 Make_Selected_Component (Loc,
2082 Prefix => Convert_To (Typ, New_Copy_Tree (Target)),
2083 Selector_Name => Make_Identifier (Loc, Name_uController));
2084 Set_Assignment_OK (Ref);
2086 -- Ada 2005 (AI-287): Give support to aggregates of limited types.
2087 -- If the type is intrinsically limited the controller is limited as
2088 -- well. If it is tagged and limited then so is the controller.
2089 -- Otherwise an untagged type may have limited components without its
2090 -- full view being limited, so the controller is not limited.
2092 if Nkind (Target) = N_Identifier then
2093 Target_Type := Etype (Target);
2095 elsif Nkind (Target) = N_Selected_Component then
2096 Target_Type := Etype (Selector_Name (Target));
2098 elsif Nkind (Target) = N_Unchecked_Type_Conversion then
2099 Target_Type := Etype (Target);
2101 elsif Nkind (Target) = N_Unchecked_Expression
2102 and then Nkind (Expression (Target)) = N_Indexed_Component
2103 then
2104 Target_Type := Etype (Prefix (Expression (Target)));
2106 else
2107 Target_Type := Etype (Target);
2108 end if;
2110 -- If the target has not been analyzed yet, as will happen with
2111 -- delayed expansion, use the given type (either the aggregate type
2112 -- or an ancestor) to determine limitedness.
2114 if No (Target_Type) then
2115 Target_Type := Typ;
2116 end if;
2118 if (Is_Tagged_Type (Target_Type))
2119 and then Is_Limited_Type (Target_Type)
2120 then
2121 RC := RE_Limited_Record_Controller;
2123 elsif Is_Immutably_Limited_Type (Target_Type) then
2124 RC := RE_Limited_Record_Controller;
2126 else
2127 RC := RE_Record_Controller;
2128 end if;
2130 if Init_Pr then
2131 Append_List_To (L,
2132 Build_Initialization_Call (Loc,
2133 Id_Ref => Ref,
2134 Typ => RTE (RC),
2135 In_Init_Proc => Within_Init_Proc));
2136 end if;
2138 Append_To (L,
2139 Make_Procedure_Call_Statement (Loc,
2140 Name =>
2141 New_Reference_To (
2142 Find_Prim_Op (RTE (RC), Name_Initialize), Loc),
2143 Parameter_Associations =>
2144 New_List (New_Copy_Tree (Ref))));
2146 Append_To (L,
2147 Make_Attach_Call (
2148 Obj_Ref => New_Copy_Tree (Ref),
2149 Flist_Ref => F,
2150 With_Attach => Attach));
2152 return L;
2153 end Init_Controller;
2155 -------------------------
2156 -- Is_Int_Range_Bounds --
2157 -------------------------
2159 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2160 begin
2161 return Nkind (Bounds) = N_Range
2162 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2163 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2164 end Is_Int_Range_Bounds;
2166 -------------------------------
2167 -- Gen_Ctrl_Actions_For_Aggr --
2168 -------------------------------
2170 procedure Gen_Ctrl_Actions_For_Aggr is
2171 Alloc : Node_Id := Empty;
2173 begin
2174 -- Do the work only the first time this is called
2176 if Ctrl_Stuff_Done then
2177 return;
2178 end if;
2180 Ctrl_Stuff_Done := True;
2182 if Present (Obj)
2183 and then Finalize_Storage_Only (Typ)
2184 and then
2185 (Is_Library_Level_Entity (Obj)
2186 or else Entity (Constant_Value (RTE (RE_Garbage_Collected))) =
2187 Standard_True)
2189 -- why not Is_True (Expr_Value (RTE (RE_Garbaage_Collected) ???
2190 then
2191 Attach := Make_Integer_Literal (Loc, 0);
2193 elsif Nkind (Parent (N)) = N_Qualified_Expression
2194 and then Nkind (Parent (Parent (N))) = N_Allocator
2195 then
2196 Alloc := Parent (Parent (N));
2197 Attach := Make_Integer_Literal (Loc, 2);
2199 else
2200 Attach := Make_Integer_Literal (Loc, 1);
2201 end if;
2203 -- Determine the external finalization list. It is either the
2204 -- finalization list of the outer-scope or the one coming from
2205 -- an outer aggregate. When the target is not a temporary, the
2206 -- proper scope is the scope of the target rather than the
2207 -- potentially transient current scope.
2209 if Needs_Finalization (Typ) then
2211 -- The current aggregate belongs to an allocator which creates
2212 -- an object through an anonymous access type or acts as the root
2213 -- of a coextension chain.
2215 if Present (Alloc)
2216 and then
2217 (Is_Coextension_Root (Alloc)
2218 or else Ekind (Etype (Alloc)) = E_Anonymous_Access_Type)
2219 then
2220 if No (Associated_Final_Chain (Etype (Alloc))) then
2221 Build_Final_List (Alloc, Etype (Alloc));
2222 end if;
2224 External_Final_List :=
2225 Make_Selected_Component (Loc,
2226 Prefix =>
2227 New_Reference_To (
2228 Associated_Final_Chain (Etype (Alloc)), Loc),
2229 Selector_Name => Make_Identifier (Loc, Name_F));
2231 elsif Present (Flist) then
2232 External_Final_List := New_Copy_Tree (Flist);
2234 elsif Is_Entity_Name (Target)
2235 and then Present (Scope (Entity (Target)))
2236 then
2237 External_Final_List :=
2238 Find_Final_List (Scope (Entity (Target)));
2240 else
2241 External_Final_List := Find_Final_List (Current_Scope);
2242 end if;
2243 else
2244 External_Final_List := Empty;
2245 end if;
2247 -- Initialize and attach the outer object in the is_controlled case
2249 if Is_Controlled (Typ) then
2250 if Ancestor_Is_Subtype_Mark then
2251 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2252 Set_Assignment_OK (Ref);
2253 Append_To (L,
2254 Make_Procedure_Call_Statement (Loc,
2255 Name =>
2256 New_Reference_To
2257 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2258 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2259 end if;
2261 if not Has_Controlled_Component (Typ) then
2262 Ref := New_Copy_Tree (Target);
2263 Set_Assignment_OK (Ref);
2265 -- This is an aggregate of a coextension. Do not produce a
2266 -- finalization call, but rather attach the reference of the
2267 -- aggregate to its coextension chain.
2269 if Present (Alloc)
2270 and then Is_Dynamic_Coextension (Alloc)
2271 then
2272 if No (Coextensions (Alloc)) then
2273 Set_Coextensions (Alloc, New_Elmt_List);
2274 end if;
2276 Append_Elmt (Ref, Coextensions (Alloc));
2277 else
2278 Append_To (L,
2279 Make_Attach_Call (
2280 Obj_Ref => Ref,
2281 Flist_Ref => New_Copy_Tree (External_Final_List),
2282 With_Attach => Attach));
2283 end if;
2284 end if;
2285 end if;
2287 -- In the Has_Controlled component case, all the intermediate
2288 -- controllers must be initialized.
2290 if Has_Controlled_Component (Typ)
2291 and not Is_Limited_Ancestor_Expansion
2292 then
2293 declare
2294 Inner_Typ : Entity_Id;
2295 Outer_Typ : Entity_Id;
2296 At_Root : Boolean;
2298 begin
2299 -- Find outer type with a controller
2301 Outer_Typ := Base_Type (Typ);
2302 while Outer_Typ /= Init_Typ
2303 and then not Has_New_Controlled_Component (Outer_Typ)
2304 loop
2305 Outer_Typ := Etype (Outer_Typ);
2306 end loop;
2308 -- Attach it to the outer record controller to the external
2309 -- final list.
2311 if Outer_Typ = Init_Typ then
2312 Append_List_To (L,
2313 Init_Controller (
2314 Target => Target,
2315 Typ => Outer_Typ,
2316 F => External_Final_List,
2317 Attach => Attach,
2318 Init_Pr => False));
2320 At_Root := True;
2321 Inner_Typ := Init_Typ;
2323 else
2324 Append_List_To (L,
2325 Init_Controller (
2326 Target => Target,
2327 Typ => Outer_Typ,
2328 F => External_Final_List,
2329 Attach => Attach,
2330 Init_Pr => True));
2332 Inner_Typ := Etype (Outer_Typ);
2333 At_Root :=
2334 not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ;
2335 end if;
2337 -- The outer object has to be attached as well
2339 if Is_Controlled (Typ) then
2340 Ref := New_Copy_Tree (Target);
2341 Set_Assignment_OK (Ref);
2342 Append_To (L,
2343 Make_Attach_Call (
2344 Obj_Ref => Ref,
2345 Flist_Ref => New_Copy_Tree (External_Final_List),
2346 With_Attach => New_Copy_Tree (Attach)));
2347 end if;
2349 -- Initialize the internal controllers for tagged types with
2350 -- more than one controller.
2352 while not At_Root and then Inner_Typ /= Init_Typ loop
2353 if Has_New_Controlled_Component (Inner_Typ) then
2354 F :=
2355 Make_Selected_Component (Loc,
2356 Prefix =>
2357 Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2358 Selector_Name =>
2359 Make_Identifier (Loc, Name_uController));
2360 F :=
2361 Make_Selected_Component (Loc,
2362 Prefix => F,
2363 Selector_Name => Make_Identifier (Loc, Name_F));
2365 Append_List_To (L,
2366 Init_Controller (
2367 Target => Target,
2368 Typ => Inner_Typ,
2369 F => F,
2370 Attach => Make_Integer_Literal (Loc, 1),
2371 Init_Pr => True));
2372 Outer_Typ := Inner_Typ;
2373 end if;
2375 -- Stop at the root
2377 At_Root := Inner_Typ = Etype (Inner_Typ);
2378 Inner_Typ := Etype (Inner_Typ);
2379 end loop;
2381 -- If not done yet attach the controller of the ancestor part
2383 if Outer_Typ /= Init_Typ
2384 and then Inner_Typ = Init_Typ
2385 and then Has_Controlled_Component (Init_Typ)
2386 then
2387 F :=
2388 Make_Selected_Component (Loc,
2389 Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)),
2390 Selector_Name =>
2391 Make_Identifier (Loc, Name_uController));
2392 F :=
2393 Make_Selected_Component (Loc,
2394 Prefix => F,
2395 Selector_Name => Make_Identifier (Loc, Name_F));
2397 Attach := Make_Integer_Literal (Loc, 1);
2398 Append_List_To (L,
2399 Init_Controller (
2400 Target => Target,
2401 Typ => Init_Typ,
2402 F => F,
2403 Attach => Attach,
2404 Init_Pr => False));
2406 -- Note: Init_Pr is False because the ancestor part has
2407 -- already been initialized either way (by default, if
2408 -- given by a type name, otherwise from the expression).
2410 end if;
2411 end;
2412 end if;
2413 end Gen_Ctrl_Actions_For_Aggr;
2415 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2416 -- If default expression of a component mentions a discriminant of the
2417 -- type, it must be rewritten as the discriminant of the target object.
2419 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2420 -- If the aggregate contains a self-reference, traverse each expression
2421 -- to replace a possible self-reference with a reference to the proper
2422 -- component of the target of the assignment.
2424 --------------------------
2425 -- Rewrite_Discriminant --
2426 --------------------------
2428 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2429 begin
2430 if Is_Entity_Name (Expr)
2431 and then Present (Entity (Expr))
2432 and then Ekind (Entity (Expr)) = E_In_Parameter
2433 and then Present (Discriminal_Link (Entity (Expr)))
2434 and then Scope (Discriminal_Link (Entity (Expr)))
2435 = Base_Type (Etype (N))
2436 then
2437 Rewrite (Expr,
2438 Make_Selected_Component (Loc,
2439 Prefix => New_Copy_Tree (Lhs),
2440 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2441 end if;
2442 return OK;
2443 end Rewrite_Discriminant;
2445 ------------------
2446 -- Replace_Type --
2447 ------------------
2449 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2450 begin
2451 -- Note regarding the Root_Type test below: Aggregate components for
2452 -- self-referential types include attribute references to the current
2453 -- instance, of the form: Typ'access, etc.. These references are
2454 -- rewritten as references to the target of the aggregate: the
2455 -- left-hand side of an assignment, the entity in a declaration,
2456 -- or a temporary. Without this test, we would improperly extended
2457 -- this rewriting to attribute references whose prefix was not the
2458 -- type of the aggregate.
2460 if Nkind (Expr) = N_Attribute_Reference
2461 and then Is_Entity_Name (Prefix (Expr))
2462 and then Is_Type (Entity (Prefix (Expr)))
2463 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2464 then
2465 if Is_Entity_Name (Lhs) then
2466 Rewrite (Prefix (Expr),
2467 New_Occurrence_Of (Entity (Lhs), Loc));
2469 elsif Nkind (Lhs) = N_Selected_Component then
2470 Rewrite (Expr,
2471 Make_Attribute_Reference (Loc,
2472 Attribute_Name => Name_Unrestricted_Access,
2473 Prefix => New_Copy_Tree (Prefix (Lhs))));
2474 Set_Analyzed (Parent (Expr), False);
2476 else
2477 Rewrite (Expr,
2478 Make_Attribute_Reference (Loc,
2479 Attribute_Name => Name_Unrestricted_Access,
2480 Prefix => New_Copy_Tree (Lhs)));
2481 Set_Analyzed (Parent (Expr), False);
2482 end if;
2483 end if;
2485 return OK;
2486 end Replace_Type;
2488 procedure Replace_Self_Reference is
2489 new Traverse_Proc (Replace_Type);
2491 procedure Replace_Discriminants is
2492 new Traverse_Proc (Rewrite_Discriminant);
2494 -- Start of processing for Build_Record_Aggr_Code
2496 begin
2497 if Has_Self_Reference (N) then
2498 Replace_Self_Reference (N);
2499 end if;
2501 -- If the target of the aggregate is class-wide, we must convert it
2502 -- to the actual type of the aggregate, so that the proper components
2503 -- are visible. We know already that the types are compatible.
2505 if Present (Etype (Lhs))
2506 and then Is_Class_Wide_Type (Etype (Lhs))
2507 then
2508 Target := Unchecked_Convert_To (Typ, Lhs);
2509 else
2510 Target := Lhs;
2511 end if;
2513 -- Deal with the ancestor part of extension aggregates or with the
2514 -- discriminants of the root type.
2516 if Nkind (N) = N_Extension_Aggregate then
2517 declare
2518 A : constant Node_Id := Ancestor_Part (N);
2519 Assign : List_Id;
2521 begin
2522 -- If the ancestor part is a subtype mark "T", we generate
2524 -- init-proc (T(tmp)); if T is constrained and
2525 -- init-proc (S(tmp)); where S applies an appropriate
2526 -- constraint if T is unconstrained
2528 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
2529 Ancestor_Is_Subtype_Mark := True;
2531 if Is_Constrained (Entity (A)) then
2532 Init_Typ := Entity (A);
2534 -- For an ancestor part given by an unconstrained type mark,
2535 -- create a subtype constrained by appropriate corresponding
2536 -- discriminant values coming from either associations of the
2537 -- aggregate or a constraint on a parent type. The subtype will
2538 -- be used to generate the correct default value for the
2539 -- ancestor part.
2541 elsif Has_Discriminants (Entity (A)) then
2542 declare
2543 Anc_Typ : constant Entity_Id := Entity (A);
2544 Anc_Constr : constant List_Id := New_List;
2545 Discrim : Entity_Id;
2546 Disc_Value : Node_Id;
2547 New_Indic : Node_Id;
2548 Subt_Decl : Node_Id;
2550 begin
2551 Discrim := First_Discriminant (Anc_Typ);
2552 while Present (Discrim) loop
2553 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2554 Append_To (Anc_Constr, Disc_Value);
2555 Next_Discriminant (Discrim);
2556 end loop;
2558 New_Indic :=
2559 Make_Subtype_Indication (Loc,
2560 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2561 Constraint =>
2562 Make_Index_Or_Discriminant_Constraint (Loc,
2563 Constraints => Anc_Constr));
2565 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2567 Subt_Decl :=
2568 Make_Subtype_Declaration (Loc,
2569 Defining_Identifier => Init_Typ,
2570 Subtype_Indication => New_Indic);
2572 -- Itypes must be analyzed with checks off Declaration
2573 -- must have a parent for proper handling of subsidiary
2574 -- actions.
2576 Set_Parent (Subt_Decl, N);
2577 Analyze (Subt_Decl, Suppress => All_Checks);
2578 end;
2579 end if;
2581 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2582 Set_Assignment_OK (Ref);
2584 if not Is_Interface (Init_Typ) then
2585 Append_List_To (L,
2586 Build_Initialization_Call (Loc,
2587 Id_Ref => Ref,
2588 Typ => Init_Typ,
2589 In_Init_Proc => Within_Init_Proc,
2590 With_Default_Init => Has_Default_Init_Comps (N)
2591 or else
2592 Has_Task (Base_Type (Init_Typ))));
2594 if Is_Constrained (Entity (A))
2595 and then Has_Discriminants (Entity (A))
2596 then
2597 Check_Ancestor_Discriminants (Entity (A));
2598 end if;
2599 end if;
2601 -- Handle calls to C++ constructors
2603 elsif Is_CPP_Constructor_Call (A) then
2604 Init_Typ := Etype (A);
2605 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2606 Set_Assignment_OK (Ref);
2608 Append_List_To (L,
2609 Build_Initialization_Call (Loc,
2610 Id_Ref => Ref,
2611 Typ => Init_Typ,
2612 In_Init_Proc => Within_Init_Proc,
2613 With_Default_Init => Has_Default_Init_Comps (N),
2614 Constructor_Ref => A));
2616 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2617 -- limited type, a recursive call expands the ancestor. Note that
2618 -- in the limited case, the ancestor part must be either a
2619 -- function call (possibly qualified, or wrapped in an unchecked
2620 -- conversion) or aggregate (definitely qualified).
2621 -- The ancestor part can also be a function call (that may be
2622 -- transformed into an explicit dereference) or a qualification
2623 -- of one such.
2625 elsif Is_Limited_Type (Etype (A))
2626 and then Nkind_In (Unqualify (A), N_Aggregate,
2627 N_Extension_Aggregate)
2628 then
2629 Ancestor_Is_Expression := True;
2631 -- Set up finalization data for enclosing record, because
2632 -- controlled subcomponents of the ancestor part will be
2633 -- attached to it.
2635 Gen_Ctrl_Actions_For_Aggr;
2637 Append_List_To (L,
2638 Build_Record_Aggr_Code (
2639 N => Unqualify (A),
2640 Typ => Etype (Unqualify (A)),
2641 Lhs => Target,
2642 Flist => Flist,
2643 Obj => Obj,
2644 Is_Limited_Ancestor_Expansion => True));
2646 -- If the ancestor part is an expression "E", we generate
2648 -- T(tmp) := E;
2650 -- In Ada 2005, this includes the case of a (possibly qualified)
2651 -- limited function call. The assignment will turn into a
2652 -- build-in-place function call (for further details, see
2653 -- Make_Build_In_Place_Call_In_Assignment).
2655 else
2656 Ancestor_Is_Expression := True;
2657 Init_Typ := Etype (A);
2659 -- If the ancestor part is an aggregate, force its full
2660 -- expansion, which was delayed.
2662 if Nkind_In (Unqualify (A), N_Aggregate,
2663 N_Extension_Aggregate)
2664 then
2665 Set_Analyzed (A, False);
2666 Set_Analyzed (Expression (A), False);
2667 end if;
2669 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2670 Set_Assignment_OK (Ref);
2672 -- Make the assignment without usual controlled actions since
2673 -- we only want the post adjust but not the pre finalize here
2674 -- Add manual adjust when necessary.
2676 Assign := New_List (
2677 Make_OK_Assignment_Statement (Loc,
2678 Name => Ref,
2679 Expression => A));
2680 Set_No_Ctrl_Actions (First (Assign));
2682 -- Assign the tag now to make sure that the dispatching call in
2683 -- the subsequent deep_adjust works properly (unless VM_Target,
2684 -- where tags are implicit).
2686 if Tagged_Type_Expansion then
2687 Instr :=
2688 Make_OK_Assignment_Statement (Loc,
2689 Name =>
2690 Make_Selected_Component (Loc,
2691 Prefix => New_Copy_Tree (Target),
2692 Selector_Name =>
2693 New_Reference_To
2694 (First_Tag_Component (Base_Type (Typ)), Loc)),
2696 Expression =>
2697 Unchecked_Convert_To (RTE (RE_Tag),
2698 New_Reference_To
2699 (Node (First_Elmt
2700 (Access_Disp_Table (Base_Type (Typ)))),
2701 Loc)));
2703 Set_Assignment_OK (Name (Instr));
2704 Append_To (Assign, Instr);
2706 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2707 -- also initialize tags of the secondary dispatch tables.
2709 if Has_Interfaces (Base_Type (Typ)) then
2710 Init_Secondary_Tags
2711 (Typ => Base_Type (Typ),
2712 Target => Target,
2713 Stmts_List => Assign);
2714 end if;
2715 end if;
2717 -- Call Adjust manually
2719 if Needs_Finalization (Etype (A))
2720 and then not Is_Limited_Type (Etype (A))
2721 then
2722 Append_List_To (Assign,
2723 Make_Adjust_Call (
2724 Ref => New_Copy_Tree (Ref),
2725 Typ => Etype (A),
2726 Flist_Ref => New_Reference_To (
2727 RTE (RE_Global_Final_List), Loc),
2728 With_Attach => Make_Integer_Literal (Loc, 0)));
2729 end if;
2731 Append_To (L,
2732 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2734 if Has_Discriminants (Init_Typ) then
2735 Check_Ancestor_Discriminants (Init_Typ);
2736 end if;
2737 end if;
2738 end;
2740 -- Normal case (not an extension aggregate)
2742 else
2743 -- Generate the discriminant expressions, component by component.
2744 -- If the base type is an unchecked union, the discriminants are
2745 -- unknown to the back-end and absent from a value of the type, so
2746 -- assignments for them are not emitted.
2748 if Has_Discriminants (Typ)
2749 and then not Is_Unchecked_Union (Base_Type (Typ))
2750 then
2751 -- If the type is derived, and constrains discriminants of the
2752 -- parent type, these discriminants are not components of the
2753 -- aggregate, and must be initialized explicitly. They are not
2754 -- visible components of the object, but can become visible with
2755 -- a view conversion to the ancestor.
2757 declare
2758 Btype : Entity_Id;
2759 Parent_Type : Entity_Id;
2760 Disc : Entity_Id;
2761 Discr_Val : Elmt_Id;
2763 begin
2764 Btype := Base_Type (Typ);
2765 while Is_Derived_Type (Btype)
2766 and then Present (Stored_Constraint (Btype))
2767 loop
2768 Parent_Type := Etype (Btype);
2770 Disc := First_Discriminant (Parent_Type);
2771 Discr_Val :=
2772 First_Elmt (Stored_Constraint (Base_Type (Typ)));
2773 while Present (Discr_Val) loop
2775 -- Only those discriminants of the parent that are not
2776 -- renamed by discriminants of the derived type need to
2777 -- be added explicitly.
2779 if not Is_Entity_Name (Node (Discr_Val))
2780 or else
2781 Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2782 then
2783 Comp_Expr :=
2784 Make_Selected_Component (Loc,
2785 Prefix => New_Copy_Tree (Target),
2786 Selector_Name => New_Occurrence_Of (Disc, Loc));
2788 Instr :=
2789 Make_OK_Assignment_Statement (Loc,
2790 Name => Comp_Expr,
2791 Expression => New_Copy_Tree (Node (Discr_Val)));
2793 Set_No_Ctrl_Actions (Instr);
2794 Append_To (L, Instr);
2795 end if;
2797 Next_Discriminant (Disc);
2798 Next_Elmt (Discr_Val);
2799 end loop;
2801 Btype := Base_Type (Parent_Type);
2802 end loop;
2803 end;
2805 -- Generate discriminant init values for the visible discriminants
2807 declare
2808 Discriminant : Entity_Id;
2809 Discriminant_Value : Node_Id;
2811 begin
2812 Discriminant := First_Stored_Discriminant (Typ);
2813 while Present (Discriminant) loop
2814 Comp_Expr :=
2815 Make_Selected_Component (Loc,
2816 Prefix => New_Copy_Tree (Target),
2817 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2819 Discriminant_Value :=
2820 Get_Discriminant_Value (
2821 Discriminant,
2822 N_Typ,
2823 Discriminant_Constraint (N_Typ));
2825 Instr :=
2826 Make_OK_Assignment_Statement (Loc,
2827 Name => Comp_Expr,
2828 Expression => New_Copy_Tree (Discriminant_Value));
2830 Set_No_Ctrl_Actions (Instr);
2831 Append_To (L, Instr);
2833 Next_Stored_Discriminant (Discriminant);
2834 end loop;
2835 end;
2836 end if;
2837 end if;
2839 -- For CPP types we generate an implicit call to the C++ default
2840 -- constructor to ensure the proper initialization of the _Tag
2841 -- component.
2843 if Is_CPP_Class (Root_Type (Typ))
2844 and then CPP_Num_Prims (Typ) > 0
2845 then
2846 Invoke_Constructor : declare
2847 CPP_Parent : constant Entity_Id :=
2848 Enclosing_CPP_Parent (Typ);
2850 procedure Invoke_IC_Proc (T : Entity_Id);
2851 -- Recursive routine used to climb to parents. Required because
2852 -- parents must be initialized before descendants to ensure
2853 -- propagation of inherited C++ slots.
2855 --------------------
2856 -- Invoke_IC_Proc --
2857 --------------------
2859 procedure Invoke_IC_Proc (T : Entity_Id) is
2860 begin
2861 -- Avoid generating extra calls. Initialization required
2862 -- only for types defined from the level of derivation of
2863 -- type of the constructor and the type of the aggregate.
2865 if T = CPP_Parent then
2866 return;
2867 end if;
2869 Invoke_IC_Proc (Etype (T));
2871 -- Generate call to the IC routine
2873 if Present (CPP_Init_Proc (T)) then
2874 Append_To (L,
2875 Make_Procedure_Call_Statement (Loc,
2876 New_Reference_To (CPP_Init_Proc (T), Loc)));
2877 end if;
2878 end Invoke_IC_Proc;
2880 -- Start of processing for Invoke_Constructor
2882 begin
2883 -- Implicit invocation of the C++ constructor
2885 if Nkind (N) = N_Aggregate then
2886 Append_To (L,
2887 Make_Procedure_Call_Statement (Loc,
2888 Name =>
2889 New_Reference_To
2890 (Base_Init_Proc (CPP_Parent), Loc),
2891 Parameter_Associations => New_List (
2892 Unchecked_Convert_To (CPP_Parent,
2893 New_Copy_Tree (Lhs)))));
2894 end if;
2896 Invoke_IC_Proc (Typ);
2897 end Invoke_Constructor;
2898 end if;
2900 -- Generate the assignments, component by component
2902 -- tmp.comp1 := Expr1_From_Aggr;
2903 -- tmp.comp2 := Expr2_From_Aggr;
2904 -- ....
2906 Comp := First (Component_Associations (N));
2907 while Present (Comp) loop
2908 Selector := Entity (First (Choices (Comp)));
2910 -- C++ constructors
2912 if Is_CPP_Constructor_Call (Expression (Comp)) then
2913 Append_List_To (L,
2914 Build_Initialization_Call (Loc,
2915 Id_Ref => Make_Selected_Component (Loc,
2916 Prefix => New_Copy_Tree (Target),
2917 Selector_Name =>
2918 New_Occurrence_Of (Selector, Loc)),
2919 Typ => Etype (Selector),
2920 Enclos_Type => Typ,
2921 With_Default_Init => True,
2922 Constructor_Ref => Expression (Comp)));
2924 -- Ada 2005 (AI-287): For each default-initialized component generate
2925 -- a call to the corresponding IP subprogram if available.
2927 elsif Box_Present (Comp)
2928 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2929 then
2930 if Ekind (Selector) /= E_Discriminant then
2931 Gen_Ctrl_Actions_For_Aggr;
2932 end if;
2934 -- Ada 2005 (AI-287): If the component type has tasks then
2935 -- generate the activation chain and master entities (except
2936 -- in case of an allocator because in that case these entities
2937 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2939 declare
2940 Ctype : constant Entity_Id := Etype (Selector);
2941 Inside_Allocator : Boolean := False;
2942 P : Node_Id := Parent (N);
2944 begin
2945 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2946 while Present (P) loop
2947 if Nkind (P) = N_Allocator then
2948 Inside_Allocator := True;
2949 exit;
2950 end if;
2952 P := Parent (P);
2953 end loop;
2955 if not Inside_Init_Proc and not Inside_Allocator then
2956 Build_Activation_Chain_Entity (N);
2957 end if;
2958 end if;
2959 end;
2961 Append_List_To (L,
2962 Build_Initialization_Call (Loc,
2963 Id_Ref => Make_Selected_Component (Loc,
2964 Prefix => New_Copy_Tree (Target),
2965 Selector_Name =>
2966 New_Occurrence_Of (Selector, Loc)),
2967 Typ => Etype (Selector),
2968 Enclos_Type => Typ,
2969 With_Default_Init => True));
2971 -- Prepare for component assignment
2973 elsif Ekind (Selector) /= E_Discriminant
2974 or else Nkind (N) = N_Extension_Aggregate
2975 then
2976 -- All the discriminants have now been assigned
2978 -- This is now a good moment to initialize and attach all the
2979 -- controllers. Their position may depend on the discriminants.
2981 if Ekind (Selector) /= E_Discriminant then
2982 Gen_Ctrl_Actions_For_Aggr;
2983 end if;
2985 Comp_Type := Etype (Selector);
2986 Comp_Expr :=
2987 Make_Selected_Component (Loc,
2988 Prefix => New_Copy_Tree (Target),
2989 Selector_Name => New_Occurrence_Of (Selector, Loc));
2991 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2992 Expr_Q := Expression (Expression (Comp));
2993 else
2994 Expr_Q := Expression (Comp);
2995 end if;
2997 -- The controller is the one of the parent type defining the
2998 -- component (in case of inherited components).
3000 if Needs_Finalization (Comp_Type) then
3001 Internal_Final_List :=
3002 Make_Selected_Component (Loc,
3003 Prefix => Convert_To
3004 (Scope (Original_Record_Component (Selector)),
3005 New_Copy_Tree (Target)),
3006 Selector_Name => Make_Identifier (Loc, Name_uController));
3008 Internal_Final_List :=
3009 Make_Selected_Component (Loc,
3010 Prefix => Internal_Final_List,
3011 Selector_Name => Make_Identifier (Loc, Name_F));
3013 -- The internal final list can be part of a constant object
3015 Set_Assignment_OK (Internal_Final_List);
3017 else
3018 Internal_Final_List := Empty;
3019 end if;
3021 -- Now either create the assignment or generate the code for the
3022 -- inner aggregate top-down.
3024 if Is_Delayed_Aggregate (Expr_Q) then
3026 -- We have the following case of aggregate nesting inside
3027 -- an object declaration:
3029 -- type Arr_Typ is array (Integer range <>) of ...;
3031 -- type Rec_Typ (...) is record
3032 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3033 -- end record;
3035 -- Obj_Rec_Typ : Rec_Typ := (...,
3036 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3038 -- The length of the ranges of the aggregate and Obj_Add_Typ
3039 -- are equal (B - A = Y - X), but they do not coincide (X /=
3040 -- A and B /= Y). This case requires array sliding which is
3041 -- performed in the following manner:
3043 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3044 -- Temp : Arr_Sub;
3045 -- Temp (X) := (...);
3046 -- ...
3047 -- Temp (Y) := (...);
3048 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3050 if Ekind (Comp_Type) = E_Array_Subtype
3051 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3052 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3053 and then not
3054 Compatible_Int_Bounds
3055 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3056 Typ_Bounds => First_Index (Comp_Type))
3057 then
3058 -- Create the array subtype with bounds equal to those of
3059 -- the corresponding aggregate.
3061 declare
3062 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3064 SubD : constant Node_Id :=
3065 Make_Subtype_Declaration (Loc,
3066 Defining_Identifier => SubE,
3067 Subtype_Indication =>
3068 Make_Subtype_Indication (Loc,
3069 Subtype_Mark =>
3070 New_Reference_To
3071 (Etype (Comp_Type), Loc),
3072 Constraint =>
3073 Make_Index_Or_Discriminant_Constraint
3074 (Loc,
3075 Constraints => New_List (
3076 New_Copy_Tree
3077 (Aggregate_Bounds (Expr_Q))))));
3079 -- Create a temporary array of the above subtype which
3080 -- will be used to capture the aggregate assignments.
3082 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3084 TmpD : constant Node_Id :=
3085 Make_Object_Declaration (Loc,
3086 Defining_Identifier => TmpE,
3087 Object_Definition =>
3088 New_Reference_To (SubE, Loc));
3090 begin
3091 Set_No_Initialization (TmpD);
3092 Append_To (L, SubD);
3093 Append_To (L, TmpD);
3095 -- Expand aggregate into assignments to the temp array
3097 Append_List_To (L,
3098 Late_Expansion (Expr_Q, Comp_Type,
3099 New_Reference_To (TmpE, Loc), Internal_Final_List));
3101 -- Slide
3103 Append_To (L,
3104 Make_Assignment_Statement (Loc,
3105 Name => New_Copy_Tree (Comp_Expr),
3106 Expression => New_Reference_To (TmpE, Loc)));
3108 -- Do not pass the original aggregate to Gigi as is,
3109 -- since it will potentially clobber the front or the end
3110 -- of the array. Setting the expression to empty is safe
3111 -- since all aggregates are expanded into assignments.
3113 if Present (Obj) then
3114 Set_Expression (Parent (Obj), Empty);
3115 end if;
3116 end;
3118 -- Normal case (sliding not required)
3120 else
3121 Append_List_To (L,
3122 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr,
3123 Internal_Final_List));
3124 end if;
3126 -- Expr_Q is not delayed aggregate
3128 else
3129 if Has_Discriminants (Typ) then
3130 Replace_Discriminants (Expr_Q);
3131 end if;
3133 Instr :=
3134 Make_OK_Assignment_Statement (Loc,
3135 Name => Comp_Expr,
3136 Expression => Expr_Q);
3138 Set_No_Ctrl_Actions (Instr);
3139 Append_To (L, Instr);
3141 -- Adjust the tag if tagged (because of possible view
3142 -- conversions), unless compiling for a VM where tags are
3143 -- implicit.
3145 -- tmp.comp._tag := comp_typ'tag;
3147 if Is_Tagged_Type (Comp_Type)
3148 and then Tagged_Type_Expansion
3149 then
3150 Instr :=
3151 Make_OK_Assignment_Statement (Loc,
3152 Name =>
3153 Make_Selected_Component (Loc,
3154 Prefix => New_Copy_Tree (Comp_Expr),
3155 Selector_Name =>
3156 New_Reference_To
3157 (First_Tag_Component (Comp_Type), Loc)),
3159 Expression =>
3160 Unchecked_Convert_To (RTE (RE_Tag),
3161 New_Reference_To
3162 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
3163 Loc)));
3165 Append_To (L, Instr);
3166 end if;
3168 -- Adjust and Attach the component to the proper controller
3170 -- Adjust (tmp.comp);
3171 -- Attach_To_Final_List (tmp.comp,
3172 -- comp_typ (tmp)._record_controller.f)
3174 if Needs_Finalization (Comp_Type)
3175 and then not Is_Limited_Type (Comp_Type)
3176 then
3177 Append_List_To (L,
3178 Make_Adjust_Call (
3179 Ref => New_Copy_Tree (Comp_Expr),
3180 Typ => Comp_Type,
3181 Flist_Ref => Internal_Final_List,
3182 With_Attach => Make_Integer_Literal (Loc, 1)));
3183 end if;
3184 end if;
3186 -- ???
3188 elsif Ekind (Selector) = E_Discriminant
3189 and then Nkind (N) /= N_Extension_Aggregate
3190 and then Nkind (Parent (N)) = N_Component_Association
3191 and then Is_Constrained (Typ)
3192 then
3193 -- We must check that the discriminant value imposed by the
3194 -- context is the same as the value given in the subaggregate,
3195 -- because after the expansion into assignments there is no
3196 -- record on which to perform a regular discriminant check.
3198 declare
3199 D_Val : Elmt_Id;
3200 Disc : Entity_Id;
3202 begin
3203 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3204 Disc := First_Discriminant (Typ);
3205 while Chars (Disc) /= Chars (Selector) loop
3206 Next_Discriminant (Disc);
3207 Next_Elmt (D_Val);
3208 end loop;
3210 pragma Assert (Present (D_Val));
3212 -- This check cannot performed for components that are
3213 -- constrained by a current instance, because this is not a
3214 -- value that can be compared with the actual constraint.
3216 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3217 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3218 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3219 then
3220 Append_To (L,
3221 Make_Raise_Constraint_Error (Loc,
3222 Condition =>
3223 Make_Op_Ne (Loc,
3224 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3225 Right_Opnd => Expression (Comp)),
3226 Reason => CE_Discriminant_Check_Failed));
3228 else
3229 -- Find self-reference in previous discriminant assignment,
3230 -- and replace with proper expression.
3232 declare
3233 Ass : Node_Id;
3235 begin
3236 Ass := First (L);
3237 while Present (Ass) loop
3238 if Nkind (Ass) = N_Assignment_Statement
3239 and then Nkind (Name (Ass)) = N_Selected_Component
3240 and then Chars (Selector_Name (Name (Ass))) =
3241 Chars (Disc)
3242 then
3243 Set_Expression
3244 (Ass, New_Copy_Tree (Expression (Comp)));
3245 exit;
3246 end if;
3247 Next (Ass);
3248 end loop;
3249 end;
3250 end if;
3251 end;
3252 end if;
3254 Next (Comp);
3255 end loop;
3257 -- If the type is tagged, the tag needs to be initialized (unless
3258 -- compiling for the Java VM where tags are implicit). It is done
3259 -- late in the initialization process because in some cases, we call
3260 -- the init proc of an ancestor which will not leave out the right tag
3262 if Ancestor_Is_Expression then
3263 null;
3265 -- For CPP types we generated a call to the C++ default constructor
3266 -- before the components have been initialized to ensure the proper
3267 -- initialization of the _Tag component (see above).
3269 elsif Is_CPP_Class (Typ) then
3270 null;
3272 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3273 Instr :=
3274 Make_OK_Assignment_Statement (Loc,
3275 Name =>
3276 Make_Selected_Component (Loc,
3277 Prefix => New_Copy_Tree (Target),
3278 Selector_Name =>
3279 New_Reference_To
3280 (First_Tag_Component (Base_Type (Typ)), Loc)),
3282 Expression =>
3283 Unchecked_Convert_To (RTE (RE_Tag),
3284 New_Reference_To
3285 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3286 Loc)));
3288 Append_To (L, Instr);
3290 -- Ada 2005 (AI-251): If the tagged type has been derived from
3291 -- abstract interfaces we must also initialize the tags of the
3292 -- secondary dispatch tables.
3294 if Has_Interfaces (Base_Type (Typ)) then
3295 Init_Secondary_Tags
3296 (Typ => Base_Type (Typ),
3297 Target => Target,
3298 Stmts_List => L);
3299 end if;
3300 end if;
3302 -- If the controllers have not been initialized yet (by lack of non-
3303 -- discriminant components), let's do it now.
3305 Gen_Ctrl_Actions_For_Aggr;
3307 return L;
3308 end Build_Record_Aggr_Code;
3310 -------------------------------
3311 -- Convert_Aggr_In_Allocator --
3312 -------------------------------
3314 procedure Convert_Aggr_In_Allocator
3315 (Alloc : Node_Id;
3316 Decl : Node_Id;
3317 Aggr : Node_Id)
3319 Loc : constant Source_Ptr := Sloc (Aggr);
3320 Typ : constant Entity_Id := Etype (Aggr);
3321 Temp : constant Entity_Id := Defining_Identifier (Decl);
3323 Occ : constant Node_Id :=
3324 Unchecked_Convert_To (Typ,
3325 Make_Explicit_Dereference (Loc,
3326 New_Reference_To (Temp, Loc)));
3328 Access_Type : constant Entity_Id := Etype (Temp);
3329 Flist : Entity_Id;
3331 begin
3332 -- If the allocator is for an access discriminant, there is no
3333 -- finalization list for the anonymous access type, and the eventual
3334 -- finalization of the object is handled through the coextension
3335 -- mechanism. If the enclosing object is not dynamically allocated,
3336 -- the access discriminant is itself placed on the stack. Otherwise,
3337 -- some other finalization list is used (see exp_ch4.adb).
3339 -- Decl has been inserted in the code ahead of the allocator, using
3340 -- Insert_Actions. We use Insert_Actions below as well, to ensure that
3341 -- subsequent insertions are done in the proper order. Using (for
3342 -- example) Insert_Actions_After to place the expanded aggregate
3343 -- immediately after Decl may lead to out-of-order references if the
3344 -- allocator has generated a finalization list, as when the designated
3345 -- object is controlled and there is an open transient scope.
3347 if Ekind (Access_Type) = E_Anonymous_Access_Type
3348 and then Nkind (Associated_Node_For_Itype (Access_Type)) =
3349 N_Discriminant_Specification
3350 then
3351 Flist := Empty;
3353 elsif Needs_Finalization (Typ) then
3354 Flist := Find_Final_List (Access_Type);
3356 -- Otherwise there are no controlled actions to be performed.
3358 else
3359 Flist := Empty;
3360 end if;
3362 if Is_Array_Type (Typ) then
3363 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3365 elsif Has_Default_Init_Comps (Aggr) then
3366 declare
3367 L : constant List_Id := New_List;
3368 Init_Stmts : List_Id;
3370 begin
3371 Init_Stmts :=
3372 Late_Expansion
3373 (Aggr, Typ, Occ,
3374 Flist,
3375 Associated_Final_Chain (Base_Type (Access_Type)));
3377 -- ??? Dubious actual for Obj: expect 'the original object being
3378 -- initialized'
3380 if Has_Task (Typ) then
3381 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3382 Insert_Actions (Alloc, L);
3383 else
3384 Insert_Actions (Alloc, Init_Stmts);
3385 end if;
3386 end;
3388 else
3389 Insert_Actions (Alloc,
3390 Late_Expansion
3391 (Aggr, Typ, Occ, Flist,
3392 Associated_Final_Chain (Base_Type (Access_Type))));
3394 -- ??? Dubious actual for Obj: expect 'the original object being
3395 -- initialized'
3397 end if;
3398 end Convert_Aggr_In_Allocator;
3400 --------------------------------
3401 -- Convert_Aggr_In_Assignment --
3402 --------------------------------
3404 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3405 Aggr : Node_Id := Expression (N);
3406 Typ : constant Entity_Id := Etype (Aggr);
3407 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3409 begin
3410 if Nkind (Aggr) = N_Qualified_Expression then
3411 Aggr := Expression (Aggr);
3412 end if;
3414 Insert_Actions_After (N,
3415 Late_Expansion
3416 (Aggr, Typ, Occ,
3417 Find_Final_List (Typ, New_Copy_Tree (Occ))));
3418 end Convert_Aggr_In_Assignment;
3420 ---------------------------------
3421 -- Convert_Aggr_In_Object_Decl --
3422 ---------------------------------
3424 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3425 Obj : constant Entity_Id := Defining_Identifier (N);
3426 Aggr : Node_Id := Expression (N);
3427 Loc : constant Source_Ptr := Sloc (Aggr);
3428 Typ : constant Entity_Id := Etype (Aggr);
3429 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3431 function Discriminants_Ok return Boolean;
3432 -- If the object type is constrained, the discriminants in the
3433 -- aggregate must be checked against the discriminants of the subtype.
3434 -- This cannot be done using Apply_Discriminant_Checks because after
3435 -- expansion there is no aggregate left to check.
3437 ----------------------
3438 -- Discriminants_Ok --
3439 ----------------------
3441 function Discriminants_Ok return Boolean is
3442 Cond : Node_Id := Empty;
3443 Check : Node_Id;
3444 D : Entity_Id;
3445 Disc1 : Elmt_Id;
3446 Disc2 : Elmt_Id;
3447 Val1 : Node_Id;
3448 Val2 : Node_Id;
3450 begin
3451 D := First_Discriminant (Typ);
3452 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3453 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3454 while Present (Disc1) and then Present (Disc2) loop
3455 Val1 := Node (Disc1);
3456 Val2 := Node (Disc2);
3458 if not Is_OK_Static_Expression (Val1)
3459 or else not Is_OK_Static_Expression (Val2)
3460 then
3461 Check := Make_Op_Ne (Loc,
3462 Left_Opnd => Duplicate_Subexpr (Val1),
3463 Right_Opnd => Duplicate_Subexpr (Val2));
3465 if No (Cond) then
3466 Cond := Check;
3468 else
3469 Cond := Make_Or_Else (Loc,
3470 Left_Opnd => Cond,
3471 Right_Opnd => Check);
3472 end if;
3474 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3475 Apply_Compile_Time_Constraint_Error (Aggr,
3476 Msg => "incorrect value for discriminant&?",
3477 Reason => CE_Discriminant_Check_Failed,
3478 Ent => D);
3479 return False;
3480 end if;
3482 Next_Discriminant (D);
3483 Next_Elmt (Disc1);
3484 Next_Elmt (Disc2);
3485 end loop;
3487 -- If any discriminant constraint is non-static, emit a check
3489 if Present (Cond) then
3490 Insert_Action (N,
3491 Make_Raise_Constraint_Error (Loc,
3492 Condition => Cond,
3493 Reason => CE_Discriminant_Check_Failed));
3494 end if;
3496 return True;
3497 end Discriminants_Ok;
3499 -- Start of processing for Convert_Aggr_In_Object_Decl
3501 begin
3502 Set_Assignment_OK (Occ);
3504 if Nkind (Aggr) = N_Qualified_Expression then
3505 Aggr := Expression (Aggr);
3506 end if;
3508 if Has_Discriminants (Typ)
3509 and then Typ /= Etype (Obj)
3510 and then Is_Constrained (Etype (Obj))
3511 and then not Discriminants_Ok
3512 then
3513 return;
3514 end if;
3516 -- If the context is an extended return statement, it has its own
3517 -- finalization machinery (i.e. works like a transient scope) and
3518 -- we do not want to create an additional one, because objects on
3519 -- the finalization list of the return must be moved to the caller's
3520 -- finalization list to complete the return.
3522 -- However, if the aggregate is limited, it is built in place, and the
3523 -- controlled components are not assigned to intermediate temporaries
3524 -- so there is no need for a transient scope in this case either.
3526 if Requires_Transient_Scope (Typ)
3527 and then Ekind (Current_Scope) /= E_Return_Statement
3528 and then not Is_Limited_Type (Typ)
3529 then
3530 Establish_Transient_Scope
3531 (Aggr,
3532 Sec_Stack =>
3533 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3534 end if;
3536 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj));
3537 Set_No_Initialization (N);
3538 Initialize_Discriminants (N, Typ);
3539 end Convert_Aggr_In_Object_Decl;
3541 -------------------------------------
3542 -- Convert_Array_Aggr_In_Allocator --
3543 -------------------------------------
3545 procedure Convert_Array_Aggr_In_Allocator
3546 (Decl : Node_Id;
3547 Aggr : Node_Id;
3548 Target : Node_Id)
3550 Aggr_Code : List_Id;
3551 Typ : constant Entity_Id := Etype (Aggr);
3552 Ctyp : constant Entity_Id := Component_Type (Typ);
3554 begin
3555 -- The target is an explicit dereference of the allocated object.
3556 -- Generate component assignments to it, as for an aggregate that
3557 -- appears on the right-hand side of an assignment statement.
3559 Aggr_Code :=
3560 Build_Array_Aggr_Code (Aggr,
3561 Ctype => Ctyp,
3562 Index => First_Index (Typ),
3563 Into => Target,
3564 Scalar_Comp => Is_Scalar_Type (Ctyp));
3566 Insert_Actions_After (Decl, Aggr_Code);
3567 end Convert_Array_Aggr_In_Allocator;
3569 ----------------------------
3570 -- Convert_To_Assignments --
3571 ----------------------------
3573 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3574 Loc : constant Source_Ptr := Sloc (N);
3575 T : Entity_Id;
3576 Temp : Entity_Id;
3578 Instr : Node_Id;
3579 Target_Expr : Node_Id;
3580 Parent_Kind : Node_Kind;
3581 Unc_Decl : Boolean := False;
3582 Parent_Node : Node_Id;
3584 begin
3585 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3586 pragma Assert (Is_Record_Type (Typ));
3588 Parent_Node := Parent (N);
3589 Parent_Kind := Nkind (Parent_Node);
3591 if Parent_Kind = N_Qualified_Expression then
3593 -- Check if we are in a unconstrained declaration because in this
3594 -- case the current delayed expansion mechanism doesn't work when
3595 -- the declared object size depend on the initializing expr.
3597 begin
3598 Parent_Node := Parent (Parent_Node);
3599 Parent_Kind := Nkind (Parent_Node);
3601 if Parent_Kind = N_Object_Declaration then
3602 Unc_Decl :=
3603 not Is_Entity_Name (Object_Definition (Parent_Node))
3604 or else Has_Discriminants
3605 (Entity (Object_Definition (Parent_Node)))
3606 or else Is_Class_Wide_Type
3607 (Entity (Object_Definition (Parent_Node)));
3608 end if;
3609 end;
3610 end if;
3612 -- Just set the Delay flag in the cases where the transformation will be
3613 -- done top down from above.
3615 if False
3617 -- Internal aggregate (transformed when expanding the parent)
3619 or else Parent_Kind = N_Aggregate
3620 or else Parent_Kind = N_Extension_Aggregate
3621 or else Parent_Kind = N_Component_Association
3623 -- Allocator (see Convert_Aggr_In_Allocator)
3625 or else Parent_Kind = N_Allocator
3627 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3629 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3631 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3632 -- assignments in init procs are taken into account.
3634 or else (Parent_Kind = N_Assignment_Statement
3635 and then Inside_Init_Proc)
3637 -- (Ada 2005) An inherently limited type in a return statement,
3638 -- which will be handled in a build-in-place fashion, and may be
3639 -- rewritten as an extended return and have its own finalization
3640 -- machinery. In the case of a simple return, the aggregate needs
3641 -- to be delayed until the scope for the return statement has been
3642 -- created, so that any finalization chain will be associated with
3643 -- that scope. For extended returns, we delay expansion to avoid the
3644 -- creation of an unwanted transient scope that could result in
3645 -- premature finalization of the return object (which is built in
3646 -- in place within the caller's scope).
3648 or else
3649 (Is_Immutably_Limited_Type (Typ)
3650 and then
3651 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3652 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3653 then
3654 Set_Expansion_Delayed (N);
3655 return;
3656 end if;
3658 if Requires_Transient_Scope (Typ) then
3659 Establish_Transient_Scope
3660 (N, Sec_Stack =>
3661 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3662 end if;
3664 -- If the aggregate is non-limited, create a temporary. If it is limited
3665 -- and the context is an assignment, this is a subaggregate for an
3666 -- enclosing aggregate being expanded. It must be built in place, so use
3667 -- the target of the current assignment.
3669 if Is_Limited_Type (Typ)
3670 and then Nkind (Parent (N)) = N_Assignment_Statement
3671 then
3672 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3673 Insert_Actions
3674 (Parent (N), Build_Record_Aggr_Code (N, Typ, Target_Expr));
3675 Rewrite (Parent (N), Make_Null_Statement (Loc));
3677 else
3678 Temp := Make_Temporary (Loc, 'A', N);
3680 -- If the type inherits unknown discriminants, use the view with
3681 -- known discriminants if available.
3683 if Has_Unknown_Discriminants (Typ)
3684 and then Present (Underlying_Record_View (Typ))
3685 then
3686 T := Underlying_Record_View (Typ);
3687 else
3688 T := Typ;
3689 end if;
3691 Instr :=
3692 Make_Object_Declaration (Loc,
3693 Defining_Identifier => Temp,
3694 Object_Definition => New_Occurrence_Of (T, Loc));
3696 Set_No_Initialization (Instr);
3697 Insert_Action (N, Instr);
3698 Initialize_Discriminants (Instr, T);
3699 Target_Expr := New_Occurrence_Of (Temp, Loc);
3700 Insert_Actions (N, Build_Record_Aggr_Code (N, T, Target_Expr));
3701 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3702 Analyze_And_Resolve (N, T);
3703 end if;
3704 end Convert_To_Assignments;
3706 ---------------------------
3707 -- Convert_To_Positional --
3708 ---------------------------
3710 procedure Convert_To_Positional
3711 (N : Node_Id;
3712 Max_Others_Replicate : Nat := 5;
3713 Handle_Bit_Packed : Boolean := False)
3715 Typ : constant Entity_Id := Etype (N);
3717 Static_Components : Boolean := True;
3719 procedure Check_Static_Components;
3720 -- Check whether all components of the aggregate are compile-time known
3721 -- values, and can be passed as is to the back-end without further
3722 -- expansion.
3724 function Flatten
3725 (N : Node_Id;
3726 Ix : Node_Id;
3727 Ixb : Node_Id) return Boolean;
3728 -- Convert the aggregate into a purely positional form if possible. On
3729 -- entry the bounds of all dimensions are known to be static, and the
3730 -- total number of components is safe enough to expand.
3732 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3733 -- Return True iff the array N is flat (which is not trivial in the case
3734 -- of multidimensional aggregates).
3736 -----------------------------
3737 -- Check_Static_Components --
3738 -----------------------------
3740 procedure Check_Static_Components is
3741 Expr : Node_Id;
3743 begin
3744 Static_Components := True;
3746 if Nkind (N) = N_String_Literal then
3747 null;
3749 elsif Present (Expressions (N)) then
3750 Expr := First (Expressions (N));
3751 while Present (Expr) loop
3752 if Nkind (Expr) /= N_Aggregate
3753 or else not Compile_Time_Known_Aggregate (Expr)
3754 or else Expansion_Delayed (Expr)
3755 then
3756 Static_Components := False;
3757 exit;
3758 end if;
3760 Next (Expr);
3761 end loop;
3762 end if;
3764 if Nkind (N) = N_Aggregate
3765 and then Present (Component_Associations (N))
3766 then
3767 Expr := First (Component_Associations (N));
3768 while Present (Expr) loop
3769 if Nkind_In (Expression (Expr), N_Integer_Literal,
3770 N_Real_Literal)
3771 then
3772 null;
3774 elsif Is_Entity_Name (Expression (Expr))
3775 and then Present (Entity (Expression (Expr)))
3776 and then Ekind (Entity (Expression (Expr))) =
3777 E_Enumeration_Literal
3778 then
3779 null;
3781 elsif Nkind (Expression (Expr)) /= N_Aggregate
3782 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3783 or else Expansion_Delayed (Expression (Expr))
3784 then
3785 Static_Components := False;
3786 exit;
3787 end if;
3789 Next (Expr);
3790 end loop;
3791 end if;
3792 end Check_Static_Components;
3794 -------------
3795 -- Flatten --
3796 -------------
3798 function Flatten
3799 (N : Node_Id;
3800 Ix : Node_Id;
3801 Ixb : Node_Id) return Boolean
3803 Loc : constant Source_Ptr := Sloc (N);
3804 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3805 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3806 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3807 Lov : Uint;
3808 Hiv : Uint;
3810 begin
3811 if Nkind (Original_Node (N)) = N_String_Literal then
3812 return True;
3813 end if;
3815 if not Compile_Time_Known_Value (Lo)
3816 or else not Compile_Time_Known_Value (Hi)
3817 then
3818 return False;
3819 end if;
3821 Lov := Expr_Value (Lo);
3822 Hiv := Expr_Value (Hi);
3824 if Hiv < Lov
3825 or else not Compile_Time_Known_Value (Blo)
3826 then
3827 return False;
3828 end if;
3830 -- Determine if set of alternatives is suitable for conversion and
3831 -- build an array containing the values in sequence.
3833 declare
3834 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3835 of Node_Id := (others => Empty);
3836 -- The values in the aggregate sorted appropriately
3838 Vlist : List_Id;
3839 -- Same data as Vals in list form
3841 Rep_Count : Nat;
3842 -- Used to validate Max_Others_Replicate limit
3844 Elmt : Node_Id;
3845 Num : Int := UI_To_Int (Lov);
3846 Choice_Index : Int;
3847 Choice : Node_Id;
3848 Lo, Hi : Node_Id;
3850 begin
3851 if Present (Expressions (N)) then
3852 Elmt := First (Expressions (N));
3853 while Present (Elmt) loop
3854 if Nkind (Elmt) = N_Aggregate
3855 and then Present (Next_Index (Ix))
3856 and then
3857 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3858 then
3859 return False;
3860 end if;
3862 Vals (Num) := Relocate_Node (Elmt);
3863 Num := Num + 1;
3865 Next (Elmt);
3866 end loop;
3867 end if;
3869 if No (Component_Associations (N)) then
3870 return True;
3871 end if;
3873 Elmt := First (Component_Associations (N));
3875 if Nkind (Expression (Elmt)) = N_Aggregate then
3876 if Present (Next_Index (Ix))
3877 and then
3878 not Flatten
3879 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3880 then
3881 return False;
3882 end if;
3883 end if;
3885 Component_Loop : while Present (Elmt) loop
3886 Choice := First (Choices (Elmt));
3887 Choice_Loop : while Present (Choice) loop
3889 -- If we have an others choice, fill in the missing elements
3890 -- subject to the limit established by Max_Others_Replicate.
3892 if Nkind (Choice) = N_Others_Choice then
3893 Rep_Count := 0;
3895 for J in Vals'Range loop
3896 if No (Vals (J)) then
3897 Vals (J) := New_Copy_Tree (Expression (Elmt));
3898 Rep_Count := Rep_Count + 1;
3900 -- Check for maximum others replication. Note that
3901 -- we skip this test if either of the restrictions
3902 -- No_Elaboration_Code or No_Implicit_Loops is
3903 -- active, if this is a preelaborable unit or a
3904 -- predefined unit. This ensures that predefined
3905 -- units get the same level of constant folding in
3906 -- Ada 95 and Ada 05, where their categorization
3907 -- has changed.
3909 declare
3910 P : constant Entity_Id :=
3911 Cunit_Entity (Current_Sem_Unit);
3913 begin
3914 -- Check if duplication OK and if so continue
3915 -- processing.
3917 if Restriction_Active (No_Elaboration_Code)
3918 or else Restriction_Active (No_Implicit_Loops)
3919 or else Is_Preelaborated (P)
3920 or else (Ekind (P) = E_Package_Body
3921 and then
3922 Is_Preelaborated (Spec_Entity (P)))
3923 or else
3924 Is_Predefined_File_Name
3925 (Unit_File_Name (Get_Source_Unit (P)))
3926 then
3927 null;
3929 -- If duplication not OK, then we return False
3930 -- if the replication count is too high
3932 elsif Rep_Count > Max_Others_Replicate then
3933 return False;
3935 -- Continue on if duplication not OK, but the
3936 -- replication count is not excessive.
3938 else
3939 null;
3940 end if;
3941 end;
3942 end if;
3943 end loop;
3945 exit Component_Loop;
3947 -- Case of a subtype mark, identifier or expanded name
3949 elsif Is_Entity_Name (Choice)
3950 and then Is_Type (Entity (Choice))
3951 then
3952 Lo := Type_Low_Bound (Etype (Choice));
3953 Hi := Type_High_Bound (Etype (Choice));
3955 -- Case of subtype indication
3957 elsif Nkind (Choice) = N_Subtype_Indication then
3958 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3959 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3961 -- Case of a range
3963 elsif Nkind (Choice) = N_Range then
3964 Lo := Low_Bound (Choice);
3965 Hi := High_Bound (Choice);
3967 -- Normal subexpression case
3969 else pragma Assert (Nkind (Choice) in N_Subexpr);
3970 if not Compile_Time_Known_Value (Choice) then
3971 return False;
3973 else
3974 Choice_Index := UI_To_Int (Expr_Value (Choice));
3975 if Choice_Index in Vals'Range then
3976 Vals (Choice_Index) :=
3977 New_Copy_Tree (Expression (Elmt));
3978 goto Continue;
3980 else
3981 -- Choice is statically out-of-range, will be
3982 -- rewritten to raise Constraint_Error.
3984 return False;
3985 end if;
3986 end if;
3987 end if;
3989 -- Range cases merge with Lo,Hi set
3991 if not Compile_Time_Known_Value (Lo)
3992 or else
3993 not Compile_Time_Known_Value (Hi)
3994 then
3995 return False;
3996 else
3997 for J in UI_To_Int (Expr_Value (Lo)) ..
3998 UI_To_Int (Expr_Value (Hi))
3999 loop
4000 Vals (J) := New_Copy_Tree (Expression (Elmt));
4001 end loop;
4002 end if;
4004 <<Continue>>
4005 Next (Choice);
4006 end loop Choice_Loop;
4008 Next (Elmt);
4009 end loop Component_Loop;
4011 -- If we get here the conversion is possible
4013 Vlist := New_List;
4014 for J in Vals'Range loop
4015 Append (Vals (J), Vlist);
4016 end loop;
4018 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4019 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4020 return True;
4021 end;
4022 end Flatten;
4024 -------------
4025 -- Is_Flat --
4026 -------------
4028 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4029 Elmt : Node_Id;
4031 begin
4032 if Dims = 0 then
4033 return True;
4035 elsif Nkind (N) = N_Aggregate then
4036 if Present (Component_Associations (N)) then
4037 return False;
4039 else
4040 Elmt := First (Expressions (N));
4041 while Present (Elmt) loop
4042 if not Is_Flat (Elmt, Dims - 1) then
4043 return False;
4044 end if;
4046 Next (Elmt);
4047 end loop;
4049 return True;
4050 end if;
4051 else
4052 return True;
4053 end if;
4054 end Is_Flat;
4056 -- Start of processing for Convert_To_Positional
4058 begin
4059 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4060 -- components because in this case will need to call the corresponding
4061 -- IP procedure.
4063 if Has_Default_Init_Comps (N) then
4064 return;
4065 end if;
4067 if Is_Flat (N, Number_Dimensions (Typ)) then
4068 return;
4069 end if;
4071 if Is_Bit_Packed_Array (Typ)
4072 and then not Handle_Bit_Packed
4073 then
4074 return;
4075 end if;
4077 -- Do not convert to positional if controlled components are involved
4078 -- since these require special processing
4080 if Has_Controlled_Component (Typ) then
4081 return;
4082 end if;
4084 Check_Static_Components;
4086 -- If the size is known, or all the components are static, try to
4087 -- build a fully positional aggregate.
4089 -- The size of the type may not be known for an aggregate with
4090 -- discriminated array components, but if the components are static
4091 -- it is still possible to verify statically that the length is
4092 -- compatible with the upper bound of the type, and therefore it is
4093 -- worth flattening such aggregates as well.
4095 -- For now the back-end expands these aggregates into individual
4096 -- assignments to the target anyway, but it is conceivable that
4097 -- it will eventually be able to treat such aggregates statically???
4099 if Aggr_Size_OK (N, Typ)
4100 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4101 then
4102 if Static_Components then
4103 Set_Compile_Time_Known_Aggregate (N);
4104 Set_Expansion_Delayed (N, False);
4105 end if;
4107 Analyze_And_Resolve (N, Typ);
4108 end if;
4109 end Convert_To_Positional;
4111 ----------------------------
4112 -- Expand_Array_Aggregate --
4113 ----------------------------
4115 -- Array aggregate expansion proceeds as follows:
4117 -- 1. If requested we generate code to perform all the array aggregate
4118 -- bound checks, specifically
4120 -- (a) Check that the index range defined by aggregate bounds is
4121 -- compatible with corresponding index subtype.
4123 -- (b) If an others choice is present check that no aggregate
4124 -- index is outside the bounds of the index constraint.
4126 -- (c) For multidimensional arrays make sure that all subaggregates
4127 -- corresponding to the same dimension have the same bounds.
4129 -- 2. Check for packed array aggregate which can be converted to a
4130 -- constant so that the aggregate disappeares completely.
4132 -- 3. Check case of nested aggregate. Generally nested aggregates are
4133 -- handled during the processing of the parent aggregate.
4135 -- 4. Check if the aggregate can be statically processed. If this is the
4136 -- case pass it as is to Gigi. Note that a necessary condition for
4137 -- static processing is that the aggregate be fully positional.
4139 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4140 -- a temporary) then mark the aggregate as such and return. Otherwise
4141 -- create a new temporary and generate the appropriate initialization
4142 -- code.
4144 procedure Expand_Array_Aggregate (N : Node_Id) is
4145 Loc : constant Source_Ptr := Sloc (N);
4147 Typ : constant Entity_Id := Etype (N);
4148 Ctyp : constant Entity_Id := Component_Type (Typ);
4149 -- Typ is the correct constrained array subtype of the aggregate
4150 -- Ctyp is the corresponding component type.
4152 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4153 -- Number of aggregate index dimensions
4155 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4156 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4157 -- Low and High bounds of the constraint for each aggregate index
4159 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4160 -- The type of each index
4162 Maybe_In_Place_OK : Boolean;
4163 -- If the type is neither controlled nor packed and the aggregate
4164 -- is the expression in an assignment, assignment in place may be
4165 -- possible, provided other conditions are met on the LHS.
4167 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4168 (others => False);
4169 -- If Others_Present (J) is True, then there is an others choice
4170 -- in one of the sub-aggregates of N at dimension J.
4172 procedure Build_Constrained_Type (Positional : Boolean);
4173 -- If the subtype is not static or unconstrained, build a constrained
4174 -- type using the computable sizes of the aggregate and its sub-
4175 -- aggregates.
4177 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4178 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4179 -- by Index_Bounds.
4181 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4182 -- Checks that in a multi-dimensional array aggregate all subaggregates
4183 -- corresponding to the same dimension have the same bounds.
4184 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4185 -- corresponding to the sub-aggregate.
4187 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4188 -- Computes the values of array Others_Present. Sub_Aggr is the
4189 -- array sub-aggregate we start the computation from. Dim is the
4190 -- dimension corresponding to the sub-aggregate.
4192 function In_Place_Assign_OK return Boolean;
4193 -- Simple predicate to determine whether an aggregate assignment can
4194 -- be done in place, because none of the new values can depend on the
4195 -- components of the target of the assignment.
4197 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4198 -- Checks that if an others choice is present in any sub-aggregate no
4199 -- aggregate index is outside the bounds of the index constraint.
4200 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4201 -- corresponding to the sub-aggregate.
4203 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4204 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4205 -- built directly into the target of the assignment it must be free
4206 -- of side-effects.
4208 ----------------------------
4209 -- Build_Constrained_Type --
4210 ----------------------------
4212 procedure Build_Constrained_Type (Positional : Boolean) is
4213 Loc : constant Source_Ptr := Sloc (N);
4214 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4215 Comp : Node_Id;
4216 Decl : Node_Id;
4217 Typ : constant Entity_Id := Etype (N);
4218 Indexes : constant List_Id := New_List;
4219 Num : Int;
4220 Sub_Agg : Node_Id;
4222 begin
4223 -- If the aggregate is purely positional, all its subaggregates
4224 -- have the same size. We collect the dimensions from the first
4225 -- subaggregate at each level.
4227 if Positional then
4228 Sub_Agg := N;
4230 for D in 1 .. Number_Dimensions (Typ) loop
4231 Sub_Agg := First (Expressions (Sub_Agg));
4233 Comp := Sub_Agg;
4234 Num := 0;
4235 while Present (Comp) loop
4236 Num := Num + 1;
4237 Next (Comp);
4238 end loop;
4240 Append_To (Indexes,
4241 Make_Range (Loc,
4242 Low_Bound => Make_Integer_Literal (Loc, 1),
4243 High_Bound => Make_Integer_Literal (Loc, Num)));
4244 end loop;
4246 else
4247 -- We know the aggregate type is unconstrained and the aggregate
4248 -- is not processable by the back end, therefore not necessarily
4249 -- positional. Retrieve each dimension bounds (computed earlier).
4251 for D in 1 .. Number_Dimensions (Typ) loop
4252 Append (
4253 Make_Range (Loc,
4254 Low_Bound => Aggr_Low (D),
4255 High_Bound => Aggr_High (D)),
4256 Indexes);
4257 end loop;
4258 end if;
4260 Decl :=
4261 Make_Full_Type_Declaration (Loc,
4262 Defining_Identifier => Agg_Type,
4263 Type_Definition =>
4264 Make_Constrained_Array_Definition (Loc,
4265 Discrete_Subtype_Definitions => Indexes,
4266 Component_Definition =>
4267 Make_Component_Definition (Loc,
4268 Aliased_Present => False,
4269 Subtype_Indication =>
4270 New_Occurrence_Of (Component_Type (Typ), Loc))));
4272 Insert_Action (N, Decl);
4273 Analyze (Decl);
4274 Set_Etype (N, Agg_Type);
4275 Set_Is_Itype (Agg_Type);
4276 Freeze_Itype (Agg_Type, N);
4277 end Build_Constrained_Type;
4279 ------------------
4280 -- Check_Bounds --
4281 ------------------
4283 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4284 Aggr_Lo : Node_Id;
4285 Aggr_Hi : Node_Id;
4287 Ind_Lo : Node_Id;
4288 Ind_Hi : Node_Id;
4290 Cond : Node_Id := Empty;
4292 begin
4293 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4294 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4296 -- Generate the following test:
4298 -- [constraint_error when
4299 -- Aggr_Lo <= Aggr_Hi and then
4300 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4302 -- As an optimization try to see if some tests are trivially vacuous
4303 -- because we are comparing an expression against itself.
4305 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4306 Cond := Empty;
4308 elsif Aggr_Hi = Ind_Hi then
4309 Cond :=
4310 Make_Op_Lt (Loc,
4311 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4312 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4314 elsif Aggr_Lo = Ind_Lo then
4315 Cond :=
4316 Make_Op_Gt (Loc,
4317 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4318 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4320 else
4321 Cond :=
4322 Make_Or_Else (Loc,
4323 Left_Opnd =>
4324 Make_Op_Lt (Loc,
4325 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4326 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4328 Right_Opnd =>
4329 Make_Op_Gt (Loc,
4330 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4331 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4332 end if;
4334 if Present (Cond) then
4335 Cond :=
4336 Make_And_Then (Loc,
4337 Left_Opnd =>
4338 Make_Op_Le (Loc,
4339 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4340 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4342 Right_Opnd => Cond);
4344 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4345 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4346 Insert_Action (N,
4347 Make_Raise_Constraint_Error (Loc,
4348 Condition => Cond,
4349 Reason => CE_Length_Check_Failed));
4350 end if;
4351 end Check_Bounds;
4353 ----------------------------
4354 -- Check_Same_Aggr_Bounds --
4355 ----------------------------
4357 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4358 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4359 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4360 -- The bounds of this specific sub-aggregate
4362 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4363 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4364 -- The bounds of the aggregate for this dimension
4366 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4367 -- The index type for this dimension.xxx
4369 Cond : Node_Id := Empty;
4370 Assoc : Node_Id;
4371 Expr : Node_Id;
4373 begin
4374 -- If index checks are on generate the test
4376 -- [constraint_error when
4377 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4379 -- As an optimization try to see if some tests are trivially vacuos
4380 -- because we are comparing an expression against itself. Also for
4381 -- the first dimension the test is trivially vacuous because there
4382 -- is just one aggregate for dimension 1.
4384 if Index_Checks_Suppressed (Ind_Typ) then
4385 Cond := Empty;
4387 elsif Dim = 1
4388 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4389 then
4390 Cond := Empty;
4392 elsif Aggr_Hi = Sub_Hi then
4393 Cond :=
4394 Make_Op_Ne (Loc,
4395 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4396 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4398 elsif Aggr_Lo = Sub_Lo then
4399 Cond :=
4400 Make_Op_Ne (Loc,
4401 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4402 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4404 else
4405 Cond :=
4406 Make_Or_Else (Loc,
4407 Left_Opnd =>
4408 Make_Op_Ne (Loc,
4409 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4410 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4412 Right_Opnd =>
4413 Make_Op_Ne (Loc,
4414 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4415 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4416 end if;
4418 if Present (Cond) then
4419 Insert_Action (N,
4420 Make_Raise_Constraint_Error (Loc,
4421 Condition => Cond,
4422 Reason => CE_Length_Check_Failed));
4423 end if;
4425 -- Now look inside the sub-aggregate to see if there is more work
4427 if Dim < Aggr_Dimension then
4429 -- Process positional components
4431 if Present (Expressions (Sub_Aggr)) then
4432 Expr := First (Expressions (Sub_Aggr));
4433 while Present (Expr) loop
4434 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4435 Next (Expr);
4436 end loop;
4437 end if;
4439 -- Process component associations
4441 if Present (Component_Associations (Sub_Aggr)) then
4442 Assoc := First (Component_Associations (Sub_Aggr));
4443 while Present (Assoc) loop
4444 Expr := Expression (Assoc);
4445 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4446 Next (Assoc);
4447 end loop;
4448 end if;
4449 end if;
4450 end Check_Same_Aggr_Bounds;
4452 ----------------------------
4453 -- Compute_Others_Present --
4454 ----------------------------
4456 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4457 Assoc : Node_Id;
4458 Expr : Node_Id;
4460 begin
4461 if Present (Component_Associations (Sub_Aggr)) then
4462 Assoc := Last (Component_Associations (Sub_Aggr));
4464 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4465 Others_Present (Dim) := True;
4466 end if;
4467 end if;
4469 -- Now look inside the sub-aggregate to see if there is more work
4471 if Dim < Aggr_Dimension then
4473 -- Process positional components
4475 if Present (Expressions (Sub_Aggr)) then
4476 Expr := First (Expressions (Sub_Aggr));
4477 while Present (Expr) loop
4478 Compute_Others_Present (Expr, Dim + 1);
4479 Next (Expr);
4480 end loop;
4481 end if;
4483 -- Process component associations
4485 if Present (Component_Associations (Sub_Aggr)) then
4486 Assoc := First (Component_Associations (Sub_Aggr));
4487 while Present (Assoc) loop
4488 Expr := Expression (Assoc);
4489 Compute_Others_Present (Expr, Dim + 1);
4490 Next (Assoc);
4491 end loop;
4492 end if;
4493 end if;
4494 end Compute_Others_Present;
4496 ------------------------
4497 -- In_Place_Assign_OK --
4498 ------------------------
4500 function In_Place_Assign_OK return Boolean is
4501 Aggr_In : Node_Id;
4502 Aggr_Lo : Node_Id;
4503 Aggr_Hi : Node_Id;
4504 Obj_In : Node_Id;
4505 Obj_Lo : Node_Id;
4506 Obj_Hi : Node_Id;
4508 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean;
4509 -- Aggregates that consist of a single Others choice are safe
4510 -- if the single expression is.
4512 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4513 -- Check recursively that each component of a (sub)aggregate does
4514 -- not depend on the variable being assigned to.
4516 function Safe_Component (Expr : Node_Id) return Boolean;
4517 -- Verify that an expression cannot depend on the variable being
4518 -- assigned to. Room for improvement here (but less than before).
4520 -------------------------
4521 -- Is_Others_Aggregate --
4522 -------------------------
4524 function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is
4525 begin
4526 return No (Expressions (Aggr))
4527 and then Nkind
4528 (First (Choices (First (Component_Associations (Aggr)))))
4529 = N_Others_Choice;
4530 end Is_Others_Aggregate;
4532 --------------------
4533 -- Safe_Aggregate --
4534 --------------------
4536 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4537 Expr : Node_Id;
4539 begin
4540 if Present (Expressions (Aggr)) then
4541 Expr := First (Expressions (Aggr));
4542 while Present (Expr) loop
4543 if Nkind (Expr) = N_Aggregate then
4544 if not Safe_Aggregate (Expr) then
4545 return False;
4546 end if;
4548 elsif not Safe_Component (Expr) then
4549 return False;
4550 end if;
4552 Next (Expr);
4553 end loop;
4554 end if;
4556 if Present (Component_Associations (Aggr)) then
4557 Expr := First (Component_Associations (Aggr));
4558 while Present (Expr) loop
4559 if Nkind (Expression (Expr)) = N_Aggregate then
4560 if not Safe_Aggregate (Expression (Expr)) then
4561 return False;
4562 end if;
4564 elsif not Safe_Component (Expression (Expr)) then
4565 return False;
4566 end if;
4568 Next (Expr);
4569 end loop;
4570 end if;
4572 return True;
4573 end Safe_Aggregate;
4575 --------------------
4576 -- Safe_Component --
4577 --------------------
4579 function Safe_Component (Expr : Node_Id) return Boolean is
4580 Comp : Node_Id := Expr;
4582 function Check_Component (Comp : Node_Id) return Boolean;
4583 -- Do the recursive traversal, after copy
4585 ---------------------
4586 -- Check_Component --
4587 ---------------------
4589 function Check_Component (Comp : Node_Id) return Boolean is
4590 begin
4591 if Is_Overloaded (Comp) then
4592 return False;
4593 end if;
4595 return Compile_Time_Known_Value (Comp)
4597 or else (Is_Entity_Name (Comp)
4598 and then Present (Entity (Comp))
4599 and then No (Renamed_Object (Entity (Comp))))
4601 or else (Nkind (Comp) = N_Attribute_Reference
4602 and then Check_Component (Prefix (Comp)))
4604 or else (Nkind (Comp) in N_Binary_Op
4605 and then Check_Component (Left_Opnd (Comp))
4606 and then Check_Component (Right_Opnd (Comp)))
4608 or else (Nkind (Comp) in N_Unary_Op
4609 and then Check_Component (Right_Opnd (Comp)))
4611 or else (Nkind (Comp) = N_Selected_Component
4612 and then Check_Component (Prefix (Comp)))
4614 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4615 and then Check_Component (Expression (Comp)));
4616 end Check_Component;
4618 -- Start of processing for Safe_Component
4620 begin
4621 -- If the component appears in an association that may
4622 -- correspond to more than one element, it is not analyzed
4623 -- before the expansion into assignments, to avoid side effects.
4624 -- We analyze, but do not resolve the copy, to obtain sufficient
4625 -- entity information for the checks that follow. If component is
4626 -- overloaded we assume an unsafe function call.
4628 if not Analyzed (Comp) then
4629 if Is_Overloaded (Expr) then
4630 return False;
4632 elsif Nkind (Expr) = N_Aggregate
4633 and then not Is_Others_Aggregate (Expr)
4634 then
4635 return False;
4637 elsif Nkind (Expr) = N_Allocator then
4639 -- For now, too complex to analyze
4641 return False;
4642 end if;
4644 Comp := New_Copy_Tree (Expr);
4645 Set_Parent (Comp, Parent (Expr));
4646 Analyze (Comp);
4647 end if;
4649 if Nkind (Comp) = N_Aggregate then
4650 return Safe_Aggregate (Comp);
4651 else
4652 return Check_Component (Comp);
4653 end if;
4654 end Safe_Component;
4656 -- Start of processing for In_Place_Assign_OK
4658 begin
4659 if Present (Component_Associations (N)) then
4661 -- On assignment, sliding can take place, so we cannot do the
4662 -- assignment in place unless the bounds of the aggregate are
4663 -- statically equal to those of the target.
4665 -- If the aggregate is given by an others choice, the bounds
4666 -- are derived from the left-hand side, and the assignment is
4667 -- safe if the expression is.
4669 if Is_Others_Aggregate (N) then
4670 return
4671 Safe_Component
4672 (Expression (First (Component_Associations (N))));
4673 end if;
4675 Aggr_In := First_Index (Etype (N));
4677 if Nkind (Parent (N)) = N_Assignment_Statement then
4678 Obj_In := First_Index (Etype (Name (Parent (N))));
4680 else
4681 -- Context is an allocator. Check bounds of aggregate
4682 -- against given type in qualified expression.
4684 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4685 Obj_In :=
4686 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4687 end if;
4689 while Present (Aggr_In) loop
4690 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4691 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4693 if not Compile_Time_Known_Value (Aggr_Lo)
4694 or else not Compile_Time_Known_Value (Aggr_Hi)
4695 or else not Compile_Time_Known_Value (Obj_Lo)
4696 or else not Compile_Time_Known_Value (Obj_Hi)
4697 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4698 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4699 then
4700 return False;
4701 end if;
4703 Next_Index (Aggr_In);
4704 Next_Index (Obj_In);
4705 end loop;
4706 end if;
4708 -- Now check the component values themselves
4710 return Safe_Aggregate (N);
4711 end In_Place_Assign_OK;
4713 ------------------
4714 -- Others_Check --
4715 ------------------
4717 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4718 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4719 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4720 -- The bounds of the aggregate for this dimension
4722 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4723 -- The index type for this dimension
4725 Need_To_Check : Boolean := False;
4727 Choices_Lo : Node_Id := Empty;
4728 Choices_Hi : Node_Id := Empty;
4729 -- The lowest and highest discrete choices for a named sub-aggregate
4731 Nb_Choices : Int := -1;
4732 -- The number of discrete non-others choices in this sub-aggregate
4734 Nb_Elements : Uint := Uint_0;
4735 -- The number of elements in a positional aggregate
4737 Cond : Node_Id := Empty;
4739 Assoc : Node_Id;
4740 Choice : Node_Id;
4741 Expr : Node_Id;
4743 begin
4744 -- Check if we have an others choice. If we do make sure that this
4745 -- sub-aggregate contains at least one element in addition to the
4746 -- others choice.
4748 if Range_Checks_Suppressed (Ind_Typ) then
4749 Need_To_Check := False;
4751 elsif Present (Expressions (Sub_Aggr))
4752 and then Present (Component_Associations (Sub_Aggr))
4753 then
4754 Need_To_Check := True;
4756 elsif Present (Component_Associations (Sub_Aggr)) then
4757 Assoc := Last (Component_Associations (Sub_Aggr));
4759 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4760 Need_To_Check := False;
4762 else
4763 -- Count the number of discrete choices. Start with -1 because
4764 -- the others choice does not count.
4766 Nb_Choices := -1;
4767 Assoc := First (Component_Associations (Sub_Aggr));
4768 while Present (Assoc) loop
4769 Choice := First (Choices (Assoc));
4770 while Present (Choice) loop
4771 Nb_Choices := Nb_Choices + 1;
4772 Next (Choice);
4773 end loop;
4775 Next (Assoc);
4776 end loop;
4778 -- If there is only an others choice nothing to do
4780 Need_To_Check := (Nb_Choices > 0);
4781 end if;
4783 else
4784 Need_To_Check := False;
4785 end if;
4787 -- If we are dealing with a positional sub-aggregate with an others
4788 -- choice then compute the number or positional elements.
4790 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4791 Expr := First (Expressions (Sub_Aggr));
4792 Nb_Elements := Uint_0;
4793 while Present (Expr) loop
4794 Nb_Elements := Nb_Elements + 1;
4795 Next (Expr);
4796 end loop;
4798 -- If the aggregate contains discrete choices and an others choice
4799 -- compute the smallest and largest discrete choice values.
4801 elsif Need_To_Check then
4802 Compute_Choices_Lo_And_Choices_Hi : declare
4804 Table : Case_Table_Type (1 .. Nb_Choices);
4805 -- Used to sort all the different choice values
4807 J : Pos := 1;
4808 Low : Node_Id;
4809 High : Node_Id;
4811 begin
4812 Assoc := First (Component_Associations (Sub_Aggr));
4813 while Present (Assoc) loop
4814 Choice := First (Choices (Assoc));
4815 while Present (Choice) loop
4816 if Nkind (Choice) = N_Others_Choice then
4817 exit;
4818 end if;
4820 Get_Index_Bounds (Choice, Low, High);
4821 Table (J).Choice_Lo := Low;
4822 Table (J).Choice_Hi := High;
4824 J := J + 1;
4825 Next (Choice);
4826 end loop;
4828 Next (Assoc);
4829 end loop;
4831 -- Sort the discrete choices
4833 Sort_Case_Table (Table);
4835 Choices_Lo := Table (1).Choice_Lo;
4836 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4837 end Compute_Choices_Lo_And_Choices_Hi;
4838 end if;
4840 -- If no others choice in this sub-aggregate, or the aggregate
4841 -- comprises only an others choice, nothing to do.
4843 if not Need_To_Check then
4844 Cond := Empty;
4846 -- If we are dealing with an aggregate containing an others choice
4847 -- and positional components, we generate the following test:
4849 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4850 -- Ind_Typ'Pos (Aggr_Hi)
4851 -- then
4852 -- raise Constraint_Error;
4853 -- end if;
4855 elsif Nb_Elements > Uint_0 then
4856 Cond :=
4857 Make_Op_Gt (Loc,
4858 Left_Opnd =>
4859 Make_Op_Add (Loc,
4860 Left_Opnd =>
4861 Make_Attribute_Reference (Loc,
4862 Prefix => New_Reference_To (Ind_Typ, Loc),
4863 Attribute_Name => Name_Pos,
4864 Expressions =>
4865 New_List
4866 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4867 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4869 Right_Opnd =>
4870 Make_Attribute_Reference (Loc,
4871 Prefix => New_Reference_To (Ind_Typ, Loc),
4872 Attribute_Name => Name_Pos,
4873 Expressions => New_List (
4874 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4876 -- If we are dealing with an aggregate containing an others choice
4877 -- and discrete choices we generate the following test:
4879 -- [constraint_error when
4880 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4882 else
4883 Cond :=
4884 Make_Or_Else (Loc,
4885 Left_Opnd =>
4886 Make_Op_Lt (Loc,
4887 Left_Opnd =>
4888 Duplicate_Subexpr_Move_Checks (Choices_Lo),
4889 Right_Opnd =>
4890 Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4892 Right_Opnd =>
4893 Make_Op_Gt (Loc,
4894 Left_Opnd =>
4895 Duplicate_Subexpr (Choices_Hi),
4896 Right_Opnd =>
4897 Duplicate_Subexpr (Aggr_Hi)));
4898 end if;
4900 if Present (Cond) then
4901 Insert_Action (N,
4902 Make_Raise_Constraint_Error (Loc,
4903 Condition => Cond,
4904 Reason => CE_Length_Check_Failed));
4905 -- Questionable reason code, shouldn't that be a
4906 -- CE_Range_Check_Failed ???
4907 end if;
4909 -- Now look inside the sub-aggregate to see if there is more work
4911 if Dim < Aggr_Dimension then
4913 -- Process positional components
4915 if Present (Expressions (Sub_Aggr)) then
4916 Expr := First (Expressions (Sub_Aggr));
4917 while Present (Expr) loop
4918 Others_Check (Expr, Dim + 1);
4919 Next (Expr);
4920 end loop;
4921 end if;
4923 -- Process component associations
4925 if Present (Component_Associations (Sub_Aggr)) then
4926 Assoc := First (Component_Associations (Sub_Aggr));
4927 while Present (Assoc) loop
4928 Expr := Expression (Assoc);
4929 Others_Check (Expr, Dim + 1);
4930 Next (Assoc);
4931 end loop;
4932 end if;
4933 end if;
4934 end Others_Check;
4936 -------------------------
4937 -- Safe_Left_Hand_Side --
4938 -------------------------
4940 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
4941 function Is_Safe_Index (Indx : Node_Id) return Boolean;
4942 -- If the left-hand side includes an indexed component, check that
4943 -- the indexes are free of side-effect.
4945 -------------------
4946 -- Is_Safe_Index --
4947 -------------------
4949 function Is_Safe_Index (Indx : Node_Id) return Boolean is
4950 begin
4951 if Is_Entity_Name (Indx) then
4952 return True;
4954 elsif Nkind (Indx) = N_Integer_Literal then
4955 return True;
4957 elsif Nkind (Indx) = N_Function_Call
4958 and then Is_Entity_Name (Name (Indx))
4959 and then
4960 Has_Pragma_Pure_Function (Entity (Name (Indx)))
4961 then
4962 return True;
4964 elsif Nkind (Indx) = N_Type_Conversion
4965 and then Is_Safe_Index (Expression (Indx))
4966 then
4967 return True;
4969 else
4970 return False;
4971 end if;
4972 end Is_Safe_Index;
4974 -- Start of processing for Safe_Left_Hand_Side
4976 begin
4977 if Is_Entity_Name (N) then
4978 return True;
4980 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
4981 and then Safe_Left_Hand_Side (Prefix (N))
4982 then
4983 return True;
4985 elsif Nkind (N) = N_Indexed_Component
4986 and then Safe_Left_Hand_Side (Prefix (N))
4987 and then
4988 Is_Safe_Index (First (Expressions (N)))
4989 then
4990 return True;
4992 elsif Nkind (N) = N_Unchecked_Type_Conversion then
4993 return Safe_Left_Hand_Side (Expression (N));
4995 else
4996 return False;
4997 end if;
4998 end Safe_Left_Hand_Side;
5000 -- Local variables
5002 Tmp : Entity_Id;
5003 -- Holds the temporary aggregate value
5005 Tmp_Decl : Node_Id;
5006 -- Holds the declaration of Tmp
5008 Aggr_Code : List_Id;
5009 Parent_Node : Node_Id;
5010 Parent_Kind : Node_Kind;
5012 -- Start of processing for Expand_Array_Aggregate
5014 begin
5015 -- Do not touch the special aggregates of attributes used for Asm calls
5017 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5018 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5019 then
5020 return;
5021 end if;
5023 -- If the semantic analyzer has determined that aggregate N will raise
5024 -- Constraint_Error at run time, then the aggregate node has been
5025 -- replaced with an N_Raise_Constraint_Error node and we should
5026 -- never get here.
5028 pragma Assert (not Raises_Constraint_Error (N));
5030 -- STEP 1a
5032 -- Check that the index range defined by aggregate bounds is
5033 -- compatible with corresponding index subtype.
5035 Index_Compatibility_Check : declare
5036 Aggr_Index_Range : Node_Id := First_Index (Typ);
5037 -- The current aggregate index range
5039 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5040 -- The corresponding index constraint against which we have to
5041 -- check the above aggregate index range.
5043 begin
5044 Compute_Others_Present (N, 1);
5046 for J in 1 .. Aggr_Dimension loop
5047 -- There is no need to emit a check if an others choice is
5048 -- present for this array aggregate dimension since in this
5049 -- case one of N's sub-aggregates has taken its bounds from the
5050 -- context and these bounds must have been checked already. In
5051 -- addition all sub-aggregates corresponding to the same
5052 -- dimension must all have the same bounds (checked in (c) below).
5054 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5055 and then not Others_Present (J)
5056 then
5057 -- We don't use Checks.Apply_Range_Check here because it emits
5058 -- a spurious check. Namely it checks that the range defined by
5059 -- the aggregate bounds is non empty. But we know this already
5060 -- if we get here.
5062 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5063 end if;
5065 -- Save the low and high bounds of the aggregate index as well as
5066 -- the index type for later use in checks (b) and (c) below.
5068 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5069 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5071 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5073 Next_Index (Aggr_Index_Range);
5074 Next_Index (Index_Constraint);
5075 end loop;
5076 end Index_Compatibility_Check;
5078 -- STEP 1b
5080 -- If an others choice is present check that no aggregate index is
5081 -- outside the bounds of the index constraint.
5083 Others_Check (N, 1);
5085 -- STEP 1c
5087 -- For multidimensional arrays make sure that all subaggregates
5088 -- corresponding to the same dimension have the same bounds.
5090 if Aggr_Dimension > 1 then
5091 Check_Same_Aggr_Bounds (N, 1);
5092 end if;
5094 -- STEP 2
5096 -- Here we test for is packed array aggregate that we can handle at
5097 -- compile time. If so, return with transformation done. Note that we do
5098 -- this even if the aggregate is nested, because once we have done this
5099 -- processing, there is no more nested aggregate!
5101 if Packed_Array_Aggregate_Handled (N) then
5102 return;
5103 end if;
5105 -- At this point we try to convert to positional form
5107 if Ekind (Current_Scope) = E_Package
5108 and then Static_Elaboration_Desired (Current_Scope)
5109 then
5110 Convert_To_Positional (N, Max_Others_Replicate => 100);
5112 else
5113 Convert_To_Positional (N);
5114 end if;
5116 -- if the result is no longer an aggregate (e.g. it may be a string
5117 -- literal, or a temporary which has the needed value), then we are
5118 -- done, since there is no longer a nested aggregate.
5120 if Nkind (N) /= N_Aggregate then
5121 return;
5123 -- We are also done if the result is an analyzed aggregate
5124 -- This case could use more comments ???
5126 elsif Analyzed (N)
5127 and then N /= Original_Node (N)
5128 then
5129 return;
5130 end if;
5132 -- If all aggregate components are compile-time known and the aggregate
5133 -- has been flattened, nothing left to do. The same occurs if the
5134 -- aggregate is used to initialize the components of an statically
5135 -- allocated dispatch table.
5137 if Compile_Time_Known_Aggregate (N)
5138 or else Is_Static_Dispatch_Table_Aggregate (N)
5139 then
5140 Set_Expansion_Delayed (N, False);
5141 return;
5142 end if;
5144 -- Now see if back end processing is possible
5146 if Backend_Processing_Possible (N) then
5148 -- If the aggregate is static but the constraints are not, build
5149 -- a static subtype for the aggregate, so that Gigi can place it
5150 -- in static memory. Perform an unchecked_conversion to the non-
5151 -- static type imposed by the context.
5153 declare
5154 Itype : constant Entity_Id := Etype (N);
5155 Index : Node_Id;
5156 Needs_Type : Boolean := False;
5158 begin
5159 Index := First_Index (Itype);
5160 while Present (Index) loop
5161 if not Is_Static_Subtype (Etype (Index)) then
5162 Needs_Type := True;
5163 exit;
5164 else
5165 Next_Index (Index);
5166 end if;
5167 end loop;
5169 if Needs_Type then
5170 Build_Constrained_Type (Positional => True);
5171 Rewrite (N, Unchecked_Convert_To (Itype, N));
5172 Analyze (N);
5173 end if;
5174 end;
5176 return;
5177 end if;
5179 -- STEP 3
5181 -- Delay expansion for nested aggregates: it will be taken care of
5182 -- when the parent aggregate is expanded.
5184 Parent_Node := Parent (N);
5185 Parent_Kind := Nkind (Parent_Node);
5187 if Parent_Kind = N_Qualified_Expression then
5188 Parent_Node := Parent (Parent_Node);
5189 Parent_Kind := Nkind (Parent_Node);
5190 end if;
5192 if Parent_Kind = N_Aggregate
5193 or else Parent_Kind = N_Extension_Aggregate
5194 or else Parent_Kind = N_Component_Association
5195 or else (Parent_Kind = N_Object_Declaration
5196 and then Needs_Finalization (Typ))
5197 or else (Parent_Kind = N_Assignment_Statement
5198 and then Inside_Init_Proc)
5199 then
5200 if Static_Array_Aggregate (N)
5201 or else Compile_Time_Known_Aggregate (N)
5202 then
5203 Set_Expansion_Delayed (N, False);
5204 return;
5205 else
5206 Set_Expansion_Delayed (N);
5207 return;
5208 end if;
5209 end if;
5211 -- STEP 4
5213 -- Look if in place aggregate expansion is possible
5215 -- For object declarations we build the aggregate in place, unless
5216 -- the array is bit-packed or the component is controlled.
5218 -- For assignments we do the assignment in place if all the component
5219 -- associations have compile-time known values. For other cases we
5220 -- create a temporary. The analysis for safety of on-line assignment
5221 -- is delicate, i.e. we don't know how to do it fully yet ???
5223 -- For allocators we assign to the designated object in place if the
5224 -- aggregate meets the same conditions as other in-place assignments.
5225 -- In this case the aggregate may not come from source but was created
5226 -- for default initialization, e.g. with Initialize_Scalars.
5228 if Requires_Transient_Scope (Typ) then
5229 Establish_Transient_Scope
5230 (N, Sec_Stack => Has_Controlled_Component (Typ));
5231 end if;
5233 if Has_Default_Init_Comps (N) then
5234 Maybe_In_Place_OK := False;
5236 elsif Is_Bit_Packed_Array (Typ)
5237 or else Has_Controlled_Component (Typ)
5238 then
5239 Maybe_In_Place_OK := False;
5241 else
5242 Maybe_In_Place_OK :=
5243 (Nkind (Parent (N)) = N_Assignment_Statement
5244 and then Comes_From_Source (N)
5245 and then In_Place_Assign_OK)
5247 or else
5248 (Nkind (Parent (Parent (N))) = N_Allocator
5249 and then In_Place_Assign_OK);
5250 end if;
5252 -- If this is an array of tasks, it will be expanded into build-in-place
5253 -- assignments. Build an activation chain for the tasks now.
5255 if Has_Task (Etype (N)) then
5256 Build_Activation_Chain_Entity (N);
5257 end if;
5259 -- Should document these individual tests ???
5261 if not Has_Default_Init_Comps (N)
5262 and then Comes_From_Source (Parent (N))
5263 and then Nkind (Parent (N)) = N_Object_Declaration
5264 and then not
5265 Must_Slide (Etype (Defining_Identifier (Parent (N))), Typ)
5266 and then N = Expression (Parent (N))
5267 and then not Is_Bit_Packed_Array (Typ)
5268 and then not Has_Controlled_Component (Typ)
5270 -- If the aggregate is the expression in an object declaration, it
5271 -- cannot be expanded in place. Lookahead in the current declarative
5272 -- part to find an address clause for the object being declared. If
5273 -- one is present, we cannot build in place. Unclear comment???
5275 and then not Has_Following_Address_Clause (Parent (N))
5276 then
5277 Tmp := Defining_Identifier (Parent (N));
5278 Set_No_Initialization (Parent (N));
5279 Set_Expression (Parent (N), Empty);
5281 -- Set the type of the entity, for use in the analysis of the
5282 -- subsequent indexed assignments. If the nominal type is not
5283 -- constrained, build a subtype from the known bounds of the
5284 -- aggregate. If the declaration has a subtype mark, use it,
5285 -- otherwise use the itype of the aggregate.
5287 if not Is_Constrained (Typ) then
5288 Build_Constrained_Type (Positional => False);
5289 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5290 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5291 then
5292 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5293 else
5294 Set_Size_Known_At_Compile_Time (Typ, False);
5295 Set_Etype (Tmp, Typ);
5296 end if;
5298 elsif Maybe_In_Place_OK
5299 and then Nkind (Parent (N)) = N_Qualified_Expression
5300 and then Nkind (Parent (Parent (N))) = N_Allocator
5301 then
5302 Set_Expansion_Delayed (N);
5303 return;
5305 -- In the remaining cases the aggregate is the RHS of an assignment
5307 elsif Maybe_In_Place_OK
5308 and then Safe_Left_Hand_Side (Name (Parent (N)))
5309 then
5310 Tmp := Name (Parent (N));
5312 if Etype (Tmp) /= Etype (N) then
5313 Apply_Length_Check (N, Etype (Tmp));
5315 if Nkind (N) = N_Raise_Constraint_Error then
5317 -- Static error, nothing further to expand
5319 return;
5320 end if;
5321 end if;
5323 elsif Maybe_In_Place_OK
5324 and then Nkind (Name (Parent (N))) = N_Slice
5325 and then Safe_Slice_Assignment (N)
5326 then
5327 -- Safe_Slice_Assignment rewrites assignment as a loop
5329 return;
5331 -- Step 5
5333 -- In place aggregate expansion is not possible
5335 else
5336 Maybe_In_Place_OK := False;
5337 Tmp := Make_Temporary (Loc, 'A', N);
5338 Tmp_Decl :=
5339 Make_Object_Declaration
5340 (Loc,
5341 Defining_Identifier => Tmp,
5342 Object_Definition => New_Occurrence_Of (Typ, Loc));
5343 Set_No_Initialization (Tmp_Decl, True);
5345 -- If we are within a loop, the temporary will be pushed on the
5346 -- stack at each iteration. If the aggregate is the expression for an
5347 -- allocator, it will be immediately copied to the heap and can
5348 -- be reclaimed at once. We create a transient scope around the
5349 -- aggregate for this purpose.
5351 if Ekind (Current_Scope) = E_Loop
5352 and then Nkind (Parent (Parent (N))) = N_Allocator
5353 then
5354 Establish_Transient_Scope (N, False);
5355 end if;
5357 Insert_Action (N, Tmp_Decl);
5358 end if;
5360 -- Construct and insert the aggregate code. We can safely suppress index
5361 -- checks because this code is guaranteed not to raise CE on index
5362 -- checks. However we should *not* suppress all checks.
5364 declare
5365 Target : Node_Id;
5367 begin
5368 if Nkind (Tmp) = N_Defining_Identifier then
5369 Target := New_Reference_To (Tmp, Loc);
5371 else
5373 if Has_Default_Init_Comps (N) then
5375 -- Ada 2005 (AI-287): This case has not been analyzed???
5377 raise Program_Error;
5378 end if;
5380 -- Name in assignment is explicit dereference
5382 Target := New_Copy (Tmp);
5383 end if;
5385 Aggr_Code :=
5386 Build_Array_Aggr_Code (N,
5387 Ctype => Ctyp,
5388 Index => First_Index (Typ),
5389 Into => Target,
5390 Scalar_Comp => Is_Scalar_Type (Ctyp));
5391 end;
5393 if Comes_From_Source (Tmp) then
5394 Insert_Actions_After (Parent (N), Aggr_Code);
5396 else
5397 Insert_Actions (N, Aggr_Code);
5398 end if;
5400 -- If the aggregate has been assigned in place, remove the original
5401 -- assignment.
5403 if Nkind (Parent (N)) = N_Assignment_Statement
5404 and then Maybe_In_Place_OK
5405 then
5406 Rewrite (Parent (N), Make_Null_Statement (Loc));
5408 elsif Nkind (Parent (N)) /= N_Object_Declaration
5409 or else Tmp /= Defining_Identifier (Parent (N))
5410 then
5411 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5412 Analyze_And_Resolve (N, Typ);
5413 end if;
5414 end Expand_Array_Aggregate;
5416 ------------------------
5417 -- Expand_N_Aggregate --
5418 ------------------------
5420 procedure Expand_N_Aggregate (N : Node_Id) is
5421 begin
5422 if Is_Record_Type (Etype (N)) then
5423 Expand_Record_Aggregate (N);
5424 else
5425 Expand_Array_Aggregate (N);
5426 end if;
5427 exception
5428 when RE_Not_Available =>
5429 return;
5430 end Expand_N_Aggregate;
5432 ----------------------------------
5433 -- Expand_N_Extension_Aggregate --
5434 ----------------------------------
5436 -- If the ancestor part is an expression, add a component association for
5437 -- the parent field. If the type of the ancestor part is not the direct
5438 -- parent of the expected type, build recursively the needed ancestors.
5439 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5440 -- ration for a temporary of the expected type, followed by individual
5441 -- assignments to the given components.
5443 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5444 Loc : constant Source_Ptr := Sloc (N);
5445 A : constant Node_Id := Ancestor_Part (N);
5446 Typ : constant Entity_Id := Etype (N);
5448 begin
5449 -- If the ancestor is a subtype mark, an init proc must be called
5450 -- on the resulting object which thus has to be materialized in
5451 -- the front-end
5453 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5454 Convert_To_Assignments (N, Typ);
5456 -- The extension aggregate is transformed into a record aggregate
5457 -- of the following form (c1 and c2 are inherited components)
5459 -- (Exp with c3 => a, c4 => b)
5460 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5462 else
5463 Set_Etype (N, Typ);
5465 if Tagged_Type_Expansion then
5466 Expand_Record_Aggregate (N,
5467 Orig_Tag =>
5468 New_Occurrence_Of
5469 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5470 Parent_Expr => A);
5471 else
5472 -- No tag is needed in the case of a VM
5473 Expand_Record_Aggregate (N,
5474 Parent_Expr => A);
5475 end if;
5476 end if;
5478 exception
5479 when RE_Not_Available =>
5480 return;
5481 end Expand_N_Extension_Aggregate;
5483 -----------------------------
5484 -- Expand_Record_Aggregate --
5485 -----------------------------
5487 procedure Expand_Record_Aggregate
5488 (N : Node_Id;
5489 Orig_Tag : Node_Id := Empty;
5490 Parent_Expr : Node_Id := Empty)
5492 Loc : constant Source_Ptr := Sloc (N);
5493 Comps : constant List_Id := Component_Associations (N);
5494 Typ : constant Entity_Id := Etype (N);
5495 Base_Typ : constant Entity_Id := Base_Type (Typ);
5497 Static_Components : Boolean := True;
5498 -- Flag to indicate whether all components are compile-time known,
5499 -- and the aggregate can be constructed statically and handled by
5500 -- the back-end.
5502 function Component_Not_OK_For_Backend return Boolean;
5503 -- Check for presence of component which makes it impossible for the
5504 -- backend to process the aggregate, thus requiring the use of a series
5505 -- of assignment statements. Cases checked for are a nested aggregate
5506 -- needing Late_Expansion, the presence of a tagged component which may
5507 -- need tag adjustment, and a bit unaligned component reference.
5509 -- We also force expansion into assignments if a component is of a
5510 -- mutable type (including a private type with discriminants) because
5511 -- in that case the size of the component to be copied may be smaller
5512 -- than the side of the target, and there is no simple way for gigi
5513 -- to compute the size of the object to be copied.
5515 -- NOTE: This is part of the ongoing work to define precisely the
5516 -- interface between front-end and back-end handling of aggregates.
5517 -- In general it is desirable to pass aggregates as they are to gigi,
5518 -- in order to minimize elaboration code. This is one case where the
5519 -- semantics of Ada complicate the analysis and lead to anomalies in
5520 -- the gcc back-end if the aggregate is not expanded into assignments.
5522 ----------------------------------
5523 -- Component_Not_OK_For_Backend --
5524 ----------------------------------
5526 function Component_Not_OK_For_Backend return Boolean is
5527 C : Node_Id;
5528 Expr_Q : Node_Id;
5530 begin
5531 if No (Comps) then
5532 return False;
5533 end if;
5535 C := First (Comps);
5536 while Present (C) loop
5538 -- If the component has box initialization, expansion is needed
5539 -- and component is not ready for backend.
5541 if Box_Present (C) then
5542 return True;
5543 end if;
5545 if Nkind (Expression (C)) = N_Qualified_Expression then
5546 Expr_Q := Expression (Expression (C));
5547 else
5548 Expr_Q := Expression (C);
5549 end if;
5551 -- Return true if the aggregate has any associations for tagged
5552 -- components that may require tag adjustment.
5554 -- These are cases where the source expression may have a tag that
5555 -- could differ from the component tag (e.g., can occur for type
5556 -- conversions and formal parameters). (Tag adjustment not needed
5557 -- if VM_Target because object tags are implicit in the machine.)
5559 if Is_Tagged_Type (Etype (Expr_Q))
5560 and then (Nkind (Expr_Q) = N_Type_Conversion
5561 or else (Is_Entity_Name (Expr_Q)
5562 and then
5563 Ekind (Entity (Expr_Q)) in Formal_Kind))
5564 and then Tagged_Type_Expansion
5565 then
5566 Static_Components := False;
5567 return True;
5569 elsif Is_Delayed_Aggregate (Expr_Q) then
5570 Static_Components := False;
5571 return True;
5573 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5574 Static_Components := False;
5575 return True;
5576 end if;
5578 if Is_Scalar_Type (Etype (Expr_Q)) then
5579 if not Compile_Time_Known_Value (Expr_Q) then
5580 Static_Components := False;
5581 end if;
5583 elsif Nkind (Expr_Q) /= N_Aggregate
5584 or else not Compile_Time_Known_Aggregate (Expr_Q)
5585 then
5586 Static_Components := False;
5588 if Is_Private_Type (Etype (Expr_Q))
5589 and then Has_Discriminants (Etype (Expr_Q))
5590 then
5591 return True;
5592 end if;
5593 end if;
5595 Next (C);
5596 end loop;
5598 return False;
5599 end Component_Not_OK_For_Backend;
5601 -- Remaining Expand_Record_Aggregate variables
5603 Tag_Value : Node_Id;
5604 Comp : Entity_Id;
5605 New_Comp : Node_Id;
5607 -- Start of processing for Expand_Record_Aggregate
5609 begin
5610 -- If the aggregate is to be assigned to an atomic variable, we
5611 -- have to prevent a piecemeal assignment even if the aggregate
5612 -- is to be expanded. We create a temporary for the aggregate, and
5613 -- assign the temporary instead, so that the back end can generate
5614 -- an atomic move for it.
5616 if Is_Atomic (Typ)
5617 and then Comes_From_Source (Parent (N))
5618 and then Is_Atomic_Aggregate (N, Typ)
5619 then
5620 return;
5622 -- No special management required for aggregates used to initialize
5623 -- statically allocated dispatch tables
5625 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5626 return;
5627 end if;
5629 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5630 -- are build-in-place function calls. The assignments will each turn
5631 -- into a build-in-place function call. If components are all static,
5632 -- we can pass the aggregate to the backend regardless of limitedness.
5634 -- Extension aggregates, aggregates in extended return statements, and
5635 -- aggregates for C++ imported types must be expanded.
5637 if Ada_Version >= Ada_2005 and then Is_Immutably_Limited_Type (Typ) then
5638 if not Nkind_In (Parent (N), N_Object_Declaration,
5639 N_Component_Association)
5640 then
5641 Convert_To_Assignments (N, Typ);
5643 elsif Nkind (N) = N_Extension_Aggregate
5644 or else Convention (Typ) = Convention_CPP
5645 then
5646 Convert_To_Assignments (N, Typ);
5648 elsif not Size_Known_At_Compile_Time (Typ)
5649 or else Component_Not_OK_For_Backend
5650 or else not Static_Components
5651 then
5652 Convert_To_Assignments (N, Typ);
5654 else
5655 Set_Compile_Time_Known_Aggregate (N);
5656 Set_Expansion_Delayed (N, False);
5657 end if;
5659 -- Gigi doesn't handle properly temporaries of variable size
5660 -- so we generate it in the front-end
5662 elsif not Size_Known_At_Compile_Time (Typ) then
5663 Convert_To_Assignments (N, Typ);
5665 -- Temporaries for controlled aggregates need to be attached to a
5666 -- final chain in order to be properly finalized, so it has to
5667 -- be created in the front-end
5669 elsif Is_Controlled (Typ)
5670 or else Has_Controlled_Component (Base_Type (Typ))
5671 then
5672 Convert_To_Assignments (N, Typ);
5674 -- Ada 2005 (AI-287): In case of default initialized components we
5675 -- convert the aggregate into assignments.
5677 elsif Has_Default_Init_Comps (N) then
5678 Convert_To_Assignments (N, Typ);
5680 -- Check components
5682 elsif Component_Not_OK_For_Backend then
5683 Convert_To_Assignments (N, Typ);
5685 -- If an ancestor is private, some components are not inherited and
5686 -- we cannot expand into a record aggregate
5688 elsif Has_Private_Ancestor (Typ) then
5689 Convert_To_Assignments (N, Typ);
5691 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
5692 -- is not able to handle the aggregate for Late_Request.
5694 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
5695 Convert_To_Assignments (N, Typ);
5697 -- If the tagged types covers interface types we need to initialize all
5698 -- hidden components containing pointers to secondary dispatch tables.
5700 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
5701 Convert_To_Assignments (N, Typ);
5703 -- If some components are mutable, the size of the aggregate component
5704 -- may be distinct from the default size of the type component, so
5705 -- we need to expand to insure that the back-end copies the proper
5706 -- size of the data.
5708 elsif Has_Mutable_Components (Typ) then
5709 Convert_To_Assignments (N, Typ);
5711 -- If the type involved has any non-bit aligned components, then we are
5712 -- not sure that the back end can handle this case correctly.
5714 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
5715 Convert_To_Assignments (N, Typ);
5717 -- In all other cases, build a proper aggregate handlable by gigi
5719 else
5720 if Nkind (N) = N_Aggregate then
5722 -- If the aggregate is static and can be handled by the back-end,
5723 -- nothing left to do.
5725 if Static_Components then
5726 Set_Compile_Time_Known_Aggregate (N);
5727 Set_Expansion_Delayed (N, False);
5728 end if;
5729 end if;
5731 -- If no discriminants, nothing special to do
5733 if not Has_Discriminants (Typ) then
5734 null;
5736 -- Case of discriminants present
5738 elsif Is_Derived_Type (Typ) then
5740 -- For untagged types, non-stored discriminants are replaced
5741 -- with stored discriminants, which are the ones that gigi uses
5742 -- to describe the type and its components.
5744 Generate_Aggregate_For_Derived_Type : declare
5745 Constraints : constant List_Id := New_List;
5746 First_Comp : Node_Id;
5747 Discriminant : Entity_Id;
5748 Decl : Node_Id;
5749 Num_Disc : Int := 0;
5750 Num_Gird : Int := 0;
5752 procedure Prepend_Stored_Values (T : Entity_Id);
5753 -- Scan the list of stored discriminants of the type, and add
5754 -- their values to the aggregate being built.
5756 ---------------------------
5757 -- Prepend_Stored_Values --
5758 ---------------------------
5760 procedure Prepend_Stored_Values (T : Entity_Id) is
5761 begin
5762 Discriminant := First_Stored_Discriminant (T);
5763 while Present (Discriminant) loop
5764 New_Comp :=
5765 Make_Component_Association (Loc,
5766 Choices =>
5767 New_List (New_Occurrence_Of (Discriminant, Loc)),
5769 Expression =>
5770 New_Copy_Tree (
5771 Get_Discriminant_Value (
5772 Discriminant,
5773 Typ,
5774 Discriminant_Constraint (Typ))));
5776 if No (First_Comp) then
5777 Prepend_To (Component_Associations (N), New_Comp);
5778 else
5779 Insert_After (First_Comp, New_Comp);
5780 end if;
5782 First_Comp := New_Comp;
5783 Next_Stored_Discriminant (Discriminant);
5784 end loop;
5785 end Prepend_Stored_Values;
5787 -- Start of processing for Generate_Aggregate_For_Derived_Type
5789 begin
5790 -- Remove the associations for the discriminant of derived type
5792 First_Comp := First (Component_Associations (N));
5793 while Present (First_Comp) loop
5794 Comp := First_Comp;
5795 Next (First_Comp);
5797 if Ekind (Entity
5798 (First (Choices (Comp)))) = E_Discriminant
5799 then
5800 Remove (Comp);
5801 Num_Disc := Num_Disc + 1;
5802 end if;
5803 end loop;
5805 -- Insert stored discriminant associations in the correct
5806 -- order. If there are more stored discriminants than new
5807 -- discriminants, there is at least one new discriminant that
5808 -- constrains more than one of the stored discriminants. In
5809 -- this case we need to construct a proper subtype of the
5810 -- parent type, in order to supply values to all the
5811 -- components. Otherwise there is one-one correspondence
5812 -- between the constraints and the stored discriminants.
5814 First_Comp := Empty;
5816 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5817 while Present (Discriminant) loop
5818 Num_Gird := Num_Gird + 1;
5819 Next_Stored_Discriminant (Discriminant);
5820 end loop;
5822 -- Case of more stored discriminants than new discriminants
5824 if Num_Gird > Num_Disc then
5826 -- Create a proper subtype of the parent type, which is the
5827 -- proper implementation type for the aggregate, and convert
5828 -- it to the intended target type.
5830 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
5831 while Present (Discriminant) loop
5832 New_Comp :=
5833 New_Copy_Tree (
5834 Get_Discriminant_Value (
5835 Discriminant,
5836 Typ,
5837 Discriminant_Constraint (Typ)));
5838 Append (New_Comp, Constraints);
5839 Next_Stored_Discriminant (Discriminant);
5840 end loop;
5842 Decl :=
5843 Make_Subtype_Declaration (Loc,
5844 Defining_Identifier => Make_Temporary (Loc, 'T'),
5845 Subtype_Indication =>
5846 Make_Subtype_Indication (Loc,
5847 Subtype_Mark =>
5848 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
5849 Constraint =>
5850 Make_Index_Or_Discriminant_Constraint
5851 (Loc, Constraints)));
5853 Insert_Action (N, Decl);
5854 Prepend_Stored_Values (Base_Type (Typ));
5856 Set_Etype (N, Defining_Identifier (Decl));
5857 Set_Analyzed (N);
5859 Rewrite (N, Unchecked_Convert_To (Typ, N));
5860 Analyze (N);
5862 -- Case where we do not have fewer new discriminants than
5863 -- stored discriminants, so in this case we can simply use the
5864 -- stored discriminants of the subtype.
5866 else
5867 Prepend_Stored_Values (Typ);
5868 end if;
5869 end Generate_Aggregate_For_Derived_Type;
5870 end if;
5872 if Is_Tagged_Type (Typ) then
5874 -- The tagged case, _parent and _tag component must be created
5876 -- Reset null_present unconditionally. tagged records always have
5877 -- at least one field (the tag or the parent)
5879 Set_Null_Record_Present (N, False);
5881 -- When the current aggregate comes from the expansion of an
5882 -- extension aggregate, the parent expr is replaced by an
5883 -- aggregate formed by selected components of this expr
5885 if Present (Parent_Expr)
5886 and then Is_Empty_List (Comps)
5887 then
5888 Comp := First_Component_Or_Discriminant (Typ);
5889 while Present (Comp) loop
5891 -- Skip all expander-generated components
5894 not Comes_From_Source (Original_Record_Component (Comp))
5895 then
5896 null;
5898 else
5899 New_Comp :=
5900 Make_Selected_Component (Loc,
5901 Prefix =>
5902 Unchecked_Convert_To (Typ,
5903 Duplicate_Subexpr (Parent_Expr, True)),
5905 Selector_Name => New_Occurrence_Of (Comp, Loc));
5907 Append_To (Comps,
5908 Make_Component_Association (Loc,
5909 Choices =>
5910 New_List (New_Occurrence_Of (Comp, Loc)),
5911 Expression =>
5912 New_Comp));
5914 Analyze_And_Resolve (New_Comp, Etype (Comp));
5915 end if;
5917 Next_Component_Or_Discriminant (Comp);
5918 end loop;
5919 end if;
5921 -- Compute the value for the Tag now, if the type is a root it
5922 -- will be included in the aggregate right away, otherwise it will
5923 -- be propagated to the parent aggregate
5925 if Present (Orig_Tag) then
5926 Tag_Value := Orig_Tag;
5927 elsif not Tagged_Type_Expansion then
5928 Tag_Value := Empty;
5929 else
5930 Tag_Value :=
5931 New_Occurrence_Of
5932 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
5933 end if;
5935 -- For a derived type, an aggregate for the parent is formed with
5936 -- all the inherited components.
5938 if Is_Derived_Type (Typ) then
5940 declare
5941 First_Comp : Node_Id;
5942 Parent_Comps : List_Id;
5943 Parent_Aggr : Node_Id;
5944 Parent_Name : Node_Id;
5946 begin
5947 -- Remove the inherited component association from the
5948 -- aggregate and store them in the parent aggregate
5950 First_Comp := First (Component_Associations (N));
5951 Parent_Comps := New_List;
5952 while Present (First_Comp)
5953 and then Scope (Original_Record_Component (
5954 Entity (First (Choices (First_Comp))))) /= Base_Typ
5955 loop
5956 Comp := First_Comp;
5957 Next (First_Comp);
5958 Remove (Comp);
5959 Append (Comp, Parent_Comps);
5960 end loop;
5962 Parent_Aggr := Make_Aggregate (Loc,
5963 Component_Associations => Parent_Comps);
5964 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
5966 -- Find the _parent component
5968 Comp := First_Component (Typ);
5969 while Chars (Comp) /= Name_uParent loop
5970 Comp := Next_Component (Comp);
5971 end loop;
5973 Parent_Name := New_Occurrence_Of (Comp, Loc);
5975 -- Insert the parent aggregate
5977 Prepend_To (Component_Associations (N),
5978 Make_Component_Association (Loc,
5979 Choices => New_List (Parent_Name),
5980 Expression => Parent_Aggr));
5982 -- Expand recursively the parent propagating the right Tag
5984 Expand_Record_Aggregate (
5985 Parent_Aggr, Tag_Value, Parent_Expr);
5986 end;
5988 -- For a root type, the tag component is added (unless compiling
5989 -- for the VMs, where tags are implicit).
5991 elsif Tagged_Type_Expansion then
5992 declare
5993 Tag_Name : constant Node_Id :=
5994 New_Occurrence_Of
5995 (First_Tag_Component (Typ), Loc);
5996 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
5997 Conv_Node : constant Node_Id :=
5998 Unchecked_Convert_To (Typ_Tag, Tag_Value);
6000 begin
6001 Set_Etype (Conv_Node, Typ_Tag);
6002 Prepend_To (Component_Associations (N),
6003 Make_Component_Association (Loc,
6004 Choices => New_List (Tag_Name),
6005 Expression => Conv_Node));
6006 end;
6007 end if;
6008 end if;
6009 end if;
6011 end Expand_Record_Aggregate;
6013 ----------------------------
6014 -- Has_Default_Init_Comps --
6015 ----------------------------
6017 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
6018 Comps : constant List_Id := Component_Associations (N);
6019 C : Node_Id;
6020 Expr : Node_Id;
6021 begin
6022 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
6024 if No (Comps) then
6025 return False;
6026 end if;
6028 if Has_Self_Reference (N) then
6029 return True;
6030 end if;
6032 -- Check if any direct component has default initialized components
6034 C := First (Comps);
6035 while Present (C) loop
6036 if Box_Present (C) then
6037 return True;
6038 end if;
6040 Next (C);
6041 end loop;
6043 -- Recursive call in case of aggregate expression
6045 C := First (Comps);
6046 while Present (C) loop
6047 Expr := Expression (C);
6049 if Present (Expr)
6050 and then
6051 Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
6052 and then Has_Default_Init_Comps (Expr)
6053 then
6054 return True;
6055 end if;
6057 Next (C);
6058 end loop;
6060 return False;
6061 end Has_Default_Init_Comps;
6063 --------------------------
6064 -- Is_Delayed_Aggregate --
6065 --------------------------
6067 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
6068 Node : Node_Id := N;
6069 Kind : Node_Kind := Nkind (Node);
6071 begin
6072 if Kind = N_Qualified_Expression then
6073 Node := Expression (Node);
6074 Kind := Nkind (Node);
6075 end if;
6077 if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then
6078 return False;
6079 else
6080 return Expansion_Delayed (Node);
6081 end if;
6082 end Is_Delayed_Aggregate;
6084 ----------------------------------------
6085 -- Is_Static_Dispatch_Table_Aggregate --
6086 ----------------------------------------
6088 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
6089 Typ : constant Entity_Id := Base_Type (Etype (N));
6091 begin
6092 return Static_Dispatch_Tables
6093 and then Tagged_Type_Expansion
6094 and then RTU_Loaded (Ada_Tags)
6096 -- Avoid circularity when rebuilding the compiler
6098 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
6099 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
6100 or else
6101 Typ = RTE (RE_Address_Array)
6102 or else
6103 Typ = RTE (RE_Type_Specific_Data)
6104 or else
6105 Typ = RTE (RE_Tag_Table)
6106 or else
6107 (RTE_Available (RE_Interface_Data)
6108 and then Typ = RTE (RE_Interface_Data))
6109 or else
6110 (RTE_Available (RE_Interfaces_Array)
6111 and then Typ = RTE (RE_Interfaces_Array))
6112 or else
6113 (RTE_Available (RE_Interface_Data_Element)
6114 and then Typ = RTE (RE_Interface_Data_Element)));
6115 end Is_Static_Dispatch_Table_Aggregate;
6117 --------------------
6118 -- Late_Expansion --
6119 --------------------
6121 function Late_Expansion
6122 (N : Node_Id;
6123 Typ : Entity_Id;
6124 Target : Node_Id;
6125 Flist : Node_Id := Empty;
6126 Obj : Entity_Id := Empty) return List_Id
6128 begin
6129 if Is_Record_Type (Etype (N)) then
6130 return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj);
6132 else pragma Assert (Is_Array_Type (Etype (N)));
6133 return
6134 Build_Array_Aggr_Code
6135 (N => N,
6136 Ctype => Component_Type (Etype (N)),
6137 Index => First_Index (Typ),
6138 Into => Target,
6139 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
6140 Indexes => No_List,
6141 Flist => Flist);
6142 end if;
6143 end Late_Expansion;
6145 ----------------------------------
6146 -- Make_OK_Assignment_Statement --
6147 ----------------------------------
6149 function Make_OK_Assignment_Statement
6150 (Sloc : Source_Ptr;
6151 Name : Node_Id;
6152 Expression : Node_Id) return Node_Id
6154 begin
6155 Set_Assignment_OK (Name);
6157 return Make_Assignment_Statement (Sloc, Name, Expression);
6158 end Make_OK_Assignment_Statement;
6160 -----------------------
6161 -- Number_Of_Choices --
6162 -----------------------
6164 function Number_Of_Choices (N : Node_Id) return Nat is
6165 Assoc : Node_Id;
6166 Choice : Node_Id;
6168 Nb_Choices : Nat := 0;
6170 begin
6171 if Present (Expressions (N)) then
6172 return 0;
6173 end if;
6175 Assoc := First (Component_Associations (N));
6176 while Present (Assoc) loop
6177 Choice := First (Choices (Assoc));
6178 while Present (Choice) loop
6179 if Nkind (Choice) /= N_Others_Choice then
6180 Nb_Choices := Nb_Choices + 1;
6181 end if;
6183 Next (Choice);
6184 end loop;
6186 Next (Assoc);
6187 end loop;
6189 return Nb_Choices;
6190 end Number_Of_Choices;
6192 ------------------------------------
6193 -- Packed_Array_Aggregate_Handled --
6194 ------------------------------------
6196 -- The current version of this procedure will handle at compile time
6197 -- any array aggregate that meets these conditions:
6199 -- One dimensional, bit packed
6200 -- Underlying packed type is modular type
6201 -- Bounds are within 32-bit Int range
6202 -- All bounds and values are static
6204 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6205 Loc : constant Source_Ptr := Sloc (N);
6206 Typ : constant Entity_Id := Etype (N);
6207 Ctyp : constant Entity_Id := Component_Type (Typ);
6209 Not_Handled : exception;
6210 -- Exception raised if this aggregate cannot be handled
6212 begin
6213 -- For now, handle only one dimensional bit packed arrays
6215 if not Is_Bit_Packed_Array (Typ)
6216 or else Number_Dimensions (Typ) > 1
6217 or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ))
6218 then
6219 return False;
6220 end if;
6222 if not Is_Scalar_Type (Component_Type (Typ))
6223 and then Has_Non_Standard_Rep (Component_Type (Typ))
6224 then
6225 return False;
6226 end if;
6228 declare
6229 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6231 Lo : Node_Id;
6232 Hi : Node_Id;
6233 -- Bounds of index type
6235 Lob : Uint;
6236 Hib : Uint;
6237 -- Values of bounds if compile time known
6239 function Get_Component_Val (N : Node_Id) return Uint;
6240 -- Given a expression value N of the component type Ctyp, returns a
6241 -- value of Csiz (component size) bits representing this value. If
6242 -- the value is non-static or any other reason exists why the value
6243 -- cannot be returned, then Not_Handled is raised.
6245 -----------------------
6246 -- Get_Component_Val --
6247 -----------------------
6249 function Get_Component_Val (N : Node_Id) return Uint is
6250 Val : Uint;
6252 begin
6253 -- We have to analyze the expression here before doing any further
6254 -- processing here. The analysis of such expressions is deferred
6255 -- till expansion to prevent some problems of premature analysis.
6257 Analyze_And_Resolve (N, Ctyp);
6259 -- Must have a compile time value. String literals have to be
6260 -- converted into temporaries as well, because they cannot easily
6261 -- be converted into their bit representation.
6263 if not Compile_Time_Known_Value (N)
6264 or else Nkind (N) = N_String_Literal
6265 then
6266 raise Not_Handled;
6267 end if;
6269 Val := Expr_Rep_Value (N);
6271 -- Adjust for bias, and strip proper number of bits
6273 if Has_Biased_Representation (Ctyp) then
6274 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6275 end if;
6277 return Val mod Uint_2 ** Csiz;
6278 end Get_Component_Val;
6280 -- Here we know we have a one dimensional bit packed array
6282 begin
6283 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6285 -- Cannot do anything if bounds are dynamic
6287 if not Compile_Time_Known_Value (Lo)
6288 or else
6289 not Compile_Time_Known_Value (Hi)
6290 then
6291 return False;
6292 end if;
6294 -- Or are silly out of range of int bounds
6296 Lob := Expr_Value (Lo);
6297 Hib := Expr_Value (Hi);
6299 if not UI_Is_In_Int_Range (Lob)
6300 or else
6301 not UI_Is_In_Int_Range (Hib)
6302 then
6303 return False;
6304 end if;
6306 -- At this stage we have a suitable aggregate for handling at compile
6307 -- time (the only remaining checks are that the values of expressions
6308 -- in the aggregate are compile time known (check is performed by
6309 -- Get_Component_Val), and that any subtypes or ranges are statically
6310 -- known.
6312 -- If the aggregate is not fully positional at this stage, then
6313 -- convert it to positional form. Either this will fail, in which
6314 -- case we can do nothing, or it will succeed, in which case we have
6315 -- succeeded in handling the aggregate, or it will stay an aggregate,
6316 -- in which case we have failed to handle this case.
6318 if Present (Component_Associations (N)) then
6319 Convert_To_Positional
6320 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6321 return Nkind (N) /= N_Aggregate;
6322 end if;
6324 -- Otherwise we are all positional, so convert to proper value
6326 declare
6327 Lov : constant Int := UI_To_Int (Lob);
6328 Hiv : constant Int := UI_To_Int (Hib);
6330 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6331 -- The length of the array (number of elements)
6333 Aggregate_Val : Uint;
6334 -- Value of aggregate. The value is set in the low order bits of
6335 -- this value. For the little-endian case, the values are stored
6336 -- from low-order to high-order and for the big-endian case the
6337 -- values are stored from high-order to low-order. Note that gigi
6338 -- will take care of the conversions to left justify the value in
6339 -- the big endian case (because of left justified modular type
6340 -- processing), so we do not have to worry about that here.
6342 Lit : Node_Id;
6343 -- Integer literal for resulting constructed value
6345 Shift : Nat;
6346 -- Shift count from low order for next value
6348 Incr : Int;
6349 -- Shift increment for loop
6351 Expr : Node_Id;
6352 -- Next expression from positional parameters of aggregate
6354 begin
6355 -- For little endian, we fill up the low order bits of the target
6356 -- value. For big endian we fill up the high order bits of the
6357 -- target value (which is a left justified modular value).
6359 if Bytes_Big_Endian xor Debug_Flag_8 then
6360 Shift := Csiz * (Len - 1);
6361 Incr := -Csiz;
6362 else
6363 Shift := 0;
6364 Incr := +Csiz;
6365 end if;
6367 -- Loop to set the values
6369 if Len = 0 then
6370 Aggregate_Val := Uint_0;
6371 else
6372 Expr := First (Expressions (N));
6373 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6375 for J in 2 .. Len loop
6376 Shift := Shift + Incr;
6377 Next (Expr);
6378 Aggregate_Val :=
6379 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6380 end loop;
6381 end if;
6383 -- Now we can rewrite with the proper value
6385 Lit :=
6386 Make_Integer_Literal (Loc,
6387 Intval => Aggregate_Val);
6388 Set_Print_In_Hex (Lit);
6390 -- Construct the expression using this literal. Note that it is
6391 -- important to qualify the literal with its proper modular type
6392 -- since universal integer does not have the required range and
6393 -- also this is a left justified modular type, which is important
6394 -- in the big-endian case.
6396 Rewrite (N,
6397 Unchecked_Convert_To (Typ,
6398 Make_Qualified_Expression (Loc,
6399 Subtype_Mark =>
6400 New_Occurrence_Of (Packed_Array_Type (Typ), Loc),
6401 Expression => Lit)));
6403 Analyze_And_Resolve (N, Typ);
6404 return True;
6405 end;
6406 end;
6408 exception
6409 when Not_Handled =>
6410 return False;
6411 end Packed_Array_Aggregate_Handled;
6413 ----------------------------
6414 -- Has_Mutable_Components --
6415 ----------------------------
6417 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6418 Comp : Entity_Id;
6420 begin
6421 Comp := First_Component (Typ);
6422 while Present (Comp) loop
6423 if Is_Record_Type (Etype (Comp))
6424 and then Has_Discriminants (Etype (Comp))
6425 and then not Is_Constrained (Etype (Comp))
6426 then
6427 return True;
6428 end if;
6430 Next_Component (Comp);
6431 end loop;
6433 return False;
6434 end Has_Mutable_Components;
6436 ------------------------------
6437 -- Initialize_Discriminants --
6438 ------------------------------
6440 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6441 Loc : constant Source_Ptr := Sloc (N);
6442 Bas : constant Entity_Id := Base_Type (Typ);
6443 Par : constant Entity_Id := Etype (Bas);
6444 Decl : constant Node_Id := Parent (Par);
6445 Ref : Node_Id;
6447 begin
6448 if Is_Tagged_Type (Bas)
6449 and then Is_Derived_Type (Bas)
6450 and then Has_Discriminants (Par)
6451 and then Has_Discriminants (Bas)
6452 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6453 and then Nkind (Decl) = N_Full_Type_Declaration
6454 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6455 and then Present
6456 (Variant_Part (Component_List (Type_Definition (Decl))))
6457 and then Nkind (N) /= N_Extension_Aggregate
6458 then
6460 -- Call init proc to set discriminants.
6461 -- There should eventually be a special procedure for this ???
6463 Ref := New_Reference_To (Defining_Identifier (N), Loc);
6464 Insert_Actions_After (N,
6465 Build_Initialization_Call (Sloc (N), Ref, Typ));
6466 end if;
6467 end Initialize_Discriminants;
6469 ----------------
6470 -- Must_Slide --
6471 ----------------
6473 function Must_Slide
6474 (Obj_Type : Entity_Id;
6475 Typ : Entity_Id) return Boolean
6477 L1, L2, H1, H2 : Node_Id;
6478 begin
6479 -- No sliding if the type of the object is not established yet, if it is
6480 -- an unconstrained type whose actual subtype comes from the aggregate,
6481 -- or if the two types are identical.
6483 if not Is_Array_Type (Obj_Type) then
6484 return False;
6486 elsif not Is_Constrained (Obj_Type) then
6487 return False;
6489 elsif Typ = Obj_Type then
6490 return False;
6492 else
6493 -- Sliding can only occur along the first dimension
6495 Get_Index_Bounds (First_Index (Typ), L1, H1);
6496 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6498 if not Is_Static_Expression (L1)
6499 or else not Is_Static_Expression (L2)
6500 or else not Is_Static_Expression (H1)
6501 or else not Is_Static_Expression (H2)
6502 then
6503 return False;
6504 else
6505 return Expr_Value (L1) /= Expr_Value (L2)
6506 or else Expr_Value (H1) /= Expr_Value (H2);
6507 end if;
6508 end if;
6509 end Must_Slide;
6511 ---------------------------
6512 -- Safe_Slice_Assignment --
6513 ---------------------------
6515 function Safe_Slice_Assignment (N : Node_Id) return Boolean is
6516 Loc : constant Source_Ptr := Sloc (Parent (N));
6517 Pref : constant Node_Id := Prefix (Name (Parent (N)));
6518 Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N)));
6519 Expr : Node_Id;
6520 L_J : Entity_Id;
6521 L_Iter : Node_Id;
6522 L_Body : Node_Id;
6523 Stat : Node_Id;
6525 begin
6526 -- Generate: for J in Range loop Pref (J) := Expr; end loop;
6528 if Comes_From_Source (N)
6529 and then No (Expressions (N))
6530 and then Nkind (First (Choices (First (Component_Associations (N)))))
6531 = N_Others_Choice
6532 then
6533 Expr := Expression (First (Component_Associations (N)));
6534 L_J := Make_Temporary (Loc, 'J');
6536 L_Iter :=
6537 Make_Iteration_Scheme (Loc,
6538 Loop_Parameter_Specification =>
6539 Make_Loop_Parameter_Specification
6540 (Loc,
6541 Defining_Identifier => L_J,
6542 Discrete_Subtype_Definition => Relocate_Node (Range_Node)));
6544 L_Body :=
6545 Make_Assignment_Statement (Loc,
6546 Name =>
6547 Make_Indexed_Component (Loc,
6548 Prefix => Relocate_Node (Pref),
6549 Expressions => New_List (New_Occurrence_Of (L_J, Loc))),
6550 Expression => Relocate_Node (Expr));
6552 -- Construct the final loop
6554 Stat :=
6555 Make_Implicit_Loop_Statement
6556 (Node => Parent (N),
6557 Identifier => Empty,
6558 Iteration_Scheme => L_Iter,
6559 Statements => New_List (L_Body));
6561 -- Set type of aggregate to be type of lhs in assignment,
6562 -- to suppress redundant length checks.
6564 Set_Etype (N, Etype (Name (Parent (N))));
6566 Rewrite (Parent (N), Stat);
6567 Analyze (Parent (N));
6568 return True;
6570 else
6571 return False;
6572 end if;
6573 end Safe_Slice_Assignment;
6575 ---------------------
6576 -- Sort_Case_Table --
6577 ---------------------
6579 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
6580 L : constant Int := Case_Table'First;
6581 U : constant Int := Case_Table'Last;
6582 K : Int;
6583 J : Int;
6584 T : Case_Bounds;
6586 begin
6587 K := L;
6588 while K /= U loop
6589 T := Case_Table (K + 1);
6591 J := K + 1;
6592 while J /= L
6593 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
6594 Expr_Value (T.Choice_Lo)
6595 loop
6596 Case_Table (J) := Case_Table (J - 1);
6597 J := J - 1;
6598 end loop;
6600 Case_Table (J) := T;
6601 K := K + 1;
6602 end loop;
6603 end Sort_Case_Table;
6605 ----------------------------
6606 -- Static_Array_Aggregate --
6607 ----------------------------
6609 function Static_Array_Aggregate (N : Node_Id) return Boolean is
6610 Bounds : constant Node_Id := Aggregate_Bounds (N);
6612 Typ : constant Entity_Id := Etype (N);
6613 Comp_Type : constant Entity_Id := Component_Type (Typ);
6614 Agg : Node_Id;
6615 Expr : Node_Id;
6616 Lo : Node_Id;
6617 Hi : Node_Id;
6619 begin
6620 if Is_Tagged_Type (Typ)
6621 or else Is_Controlled (Typ)
6622 or else Is_Packed (Typ)
6623 then
6624 return False;
6625 end if;
6627 if Present (Bounds)
6628 and then Nkind (Bounds) = N_Range
6629 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
6630 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
6631 then
6632 Lo := Low_Bound (Bounds);
6633 Hi := High_Bound (Bounds);
6635 if No (Component_Associations (N)) then
6637 -- Verify that all components are static integers
6639 Expr := First (Expressions (N));
6640 while Present (Expr) loop
6641 if Nkind (Expr) /= N_Integer_Literal then
6642 return False;
6643 end if;
6645 Next (Expr);
6646 end loop;
6648 return True;
6650 else
6651 -- We allow only a single named association, either a static
6652 -- range or an others_clause, with a static expression.
6654 Expr := First (Component_Associations (N));
6656 if Present (Expressions (N)) then
6657 return False;
6659 elsif Present (Next (Expr)) then
6660 return False;
6662 elsif Present (Next (First (Choices (Expr)))) then
6663 return False;
6665 else
6666 -- The aggregate is static if all components are literals,
6667 -- or else all its components are static aggregates for the
6668 -- component type. We also limit the size of a static aggregate
6669 -- to prevent runaway static expressions.
6671 if Is_Array_Type (Comp_Type)
6672 or else Is_Record_Type (Comp_Type)
6673 then
6674 if Nkind (Expression (Expr)) /= N_Aggregate
6675 or else
6676 not Compile_Time_Known_Aggregate (Expression (Expr))
6677 then
6678 return False;
6679 end if;
6681 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
6682 return False;
6684 elsif not Aggr_Size_OK (N, Typ) then
6685 return False;
6686 end if;
6688 -- Create a positional aggregate with the right number of
6689 -- copies of the expression.
6691 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
6693 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
6694 loop
6695 Append_To
6696 (Expressions (Agg), New_Copy (Expression (Expr)));
6698 -- The copied expression must be analyzed and resolved.
6699 -- Besides setting the type, this ensures that static
6700 -- expressions are appropriately marked as such.
6702 Analyze_And_Resolve
6703 (Last (Expressions (Agg)), Component_Type (Typ));
6704 end loop;
6706 Set_Aggregate_Bounds (Agg, Bounds);
6707 Set_Etype (Agg, Typ);
6708 Set_Analyzed (Agg);
6709 Rewrite (N, Agg);
6710 Set_Compile_Time_Known_Aggregate (N);
6712 return True;
6713 end if;
6714 end if;
6716 else
6717 return False;
6718 end if;
6719 end Static_Array_Aggregate;
6721 end Exp_Aggr;