* gcc.dg/store-motion-fgcse-sm.c (dg-final): Cleanup
[official-gcc.git] / gcc / ada / exp_aggr.adb
blob25c8db34782b3efb13c0980f285b33b07b2fca4d
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-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
43 with Lib; use Lib;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Ttypes; use Ttypes;
52 with Sem; use Sem;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Util; use Sem_Util;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Uintp; use Uintp;
67 package body Exp_Aggr is
69 type Case_Bounds is record
70 Choice_Lo : Node_Id;
71 Choice_Hi : Node_Id;
72 Choice_Node : Node_Id;
73 end record;
75 type Case_Table_Type is array (Nat range <>) of Case_Bounds;
76 -- Table type used by Check_Case_Choices procedure
78 procedure Collect_Initialization_Statements
79 (Obj : Entity_Id;
80 N : Node_Id;
81 Node_After : Node_Id);
82 -- If Obj is not frozen, collect actions inserted after N until, but not
83 -- including, Node_After, for initialization of Obj, and move them to an
84 -- expression with actions, which becomes the Initialization_Statements for
85 -- Obj.
87 function Has_Default_Init_Comps (N : Node_Id) return Boolean;
88 -- N is an aggregate (record or array). Checks the presence of default
89 -- initialization (<>) in any component (Ada 2005: AI-287).
91 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
92 -- Returns true if N is an aggregate used to initialize the components
93 -- of a statically allocated dispatch table.
95 function Must_Slide
96 (Obj_Type : Entity_Id;
97 Typ : Entity_Id) return Boolean;
98 -- A static array aggregate in an object declaration can in most cases be
99 -- expanded in place. The one exception is when the aggregate is given
100 -- with component associations that specify different bounds from those of
101 -- the type definition in the object declaration. In this pathological
102 -- case the aggregate must slide, and we must introduce an intermediate
103 -- temporary to hold it.
105 -- The same holds in an assignment to one-dimensional array of arrays,
106 -- when a component may be given with bounds that differ from those of the
107 -- component type.
109 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
110 -- Sort the Case Table using the Lower Bound of each Choice as the key.
111 -- A simple insertion sort is used since the number of choices in a case
112 -- statement of variant part will usually be small and probably in near
113 -- sorted order.
115 ------------------------------------------------------
116 -- Local subprograms for Record Aggregate Expansion --
117 ------------------------------------------------------
119 function Build_Record_Aggr_Code
120 (N : Node_Id;
121 Typ : Entity_Id;
122 Lhs : Node_Id) return List_Id;
123 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
124 -- aggregate. Target is an expression containing the location on which the
125 -- component by component assignments will take place. Returns the list of
126 -- assignments plus all other adjustments needed for tagged and controlled
127 -- types.
129 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
130 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
131 -- aggregate (which can only be a record type, this procedure is only used
132 -- for record types). Transform the given aggregate into a sequence of
133 -- assignments performed component by component.
135 procedure Expand_Record_Aggregate
136 (N : Node_Id;
137 Orig_Tag : Node_Id := Empty;
138 Parent_Expr : Node_Id := Empty);
139 -- This is the top level procedure for record aggregate expansion.
140 -- Expansion for record aggregates needs expand aggregates for tagged
141 -- record types. Specifically Expand_Record_Aggregate adds the Tag
142 -- field in front of the Component_Association list that was created
143 -- during resolution by Resolve_Record_Aggregate.
145 -- N is the record aggregate node.
146 -- Orig_Tag is the value of the Tag that has to be provided for this
147 -- specific aggregate. It carries the tag corresponding to the type
148 -- of the outermost aggregate during the recursive expansion
149 -- Parent_Expr is the ancestor part of the original extension
150 -- aggregate
152 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
153 -- Return true if one of the components is of a discriminated type with
154 -- defaults. An aggregate for a type with mutable components must be
155 -- expanded into individual assignments.
157 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
158 -- If the type of the aggregate is a type extension with renamed discrimi-
159 -- nants, we must initialize the hidden discriminants of the parent.
160 -- Otherwise, the target object must not be initialized. The discriminants
161 -- are initialized by calling the initialization procedure for the type.
162 -- This is incorrect if the initialization of other components has any
163 -- side effects. We restrict this call to the case where the parent type
164 -- has a variant part, because this is the only case where the hidden
165 -- discriminants are accessed, namely when calling discriminant checking
166 -- functions of the parent type, and when applying a stream attribute to
167 -- an object of the derived type.
169 -----------------------------------------------------
170 -- Local Subprograms for Array Aggregate Expansion --
171 -----------------------------------------------------
173 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
174 -- Very large static aggregates present problems to the back-end, and are
175 -- transformed into assignments and loops. This function verifies that the
176 -- total number of components of an aggregate is acceptable for rewriting
177 -- into a purely positional static form. Aggr_Size_OK must be called before
178 -- calling Flatten.
180 -- This function also detects and warns about one-component aggregates that
181 -- appear in a non-static context. Even if the component value is static,
182 -- such an aggregate must be expanded into an assignment.
184 function Backend_Processing_Possible (N : Node_Id) return Boolean;
185 -- This function checks if array aggregate N can be processed directly
186 -- by the backend. If this is the case, True is returned.
188 function Build_Array_Aggr_Code
189 (N : Node_Id;
190 Ctype : Entity_Id;
191 Index : Node_Id;
192 Into : Node_Id;
193 Scalar_Comp : Boolean;
194 Indexes : List_Id := No_List) return List_Id;
195 -- This recursive routine returns a list of statements containing the
196 -- loops and assignments that are needed for the expansion of the array
197 -- aggregate N.
199 -- N is the (sub-)aggregate node to be expanded into code. This node has
200 -- been fully analyzed, and its Etype is properly set.
202 -- Index is the index node corresponding to the array sub-aggregate N
204 -- Into is the target expression into which we are copying the aggregate.
205 -- Note that this node may not have been analyzed yet, and so the Etype
206 -- field may not be set.
208 -- Scalar_Comp is True if the component type of the aggregate is scalar
210 -- Indexes is the current list of expressions used to index the object we
211 -- are writing into.
213 procedure Convert_Array_Aggr_In_Allocator
214 (Decl : Node_Id;
215 Aggr : Node_Id;
216 Target : Node_Id);
217 -- If the aggregate appears within an allocator and can be expanded in
218 -- place, this routine generates the individual assignments to components
219 -- of the designated object. This is an optimization over the general
220 -- case, where a temporary is first created on the stack and then used to
221 -- construct the allocated object on the heap.
223 procedure Convert_To_Positional
224 (N : Node_Id;
225 Max_Others_Replicate : Nat := 5;
226 Handle_Bit_Packed : Boolean := False);
227 -- If possible, convert named notation to positional notation. This
228 -- conversion is possible only in some static cases. If the conversion is
229 -- possible, then N is rewritten with the analyzed converted aggregate.
230 -- The parameter Max_Others_Replicate controls the maximum number of
231 -- values corresponding to an others choice that will be converted to
232 -- positional notation (the default of 5 is the normal limit, and reflects
233 -- the fact that normally the loop is better than a lot of separate
234 -- assignments). Note that this limit gets overridden in any case if
235 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
236 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
237 -- not expect the back end to handle bit packed arrays, so the normal case
238 -- of conversion is pointless), but in the special case of a call from
239 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
240 -- these are cases we handle in there.
242 -- It would seem worthwhile to have a higher default value for Max_Others_
243 -- replicate, but aggregates in the compiler make this impossible: the
244 -- compiler bootstrap fails if Max_Others_Replicate is greater than 25.
245 -- This is unexpected ???
247 procedure Expand_Array_Aggregate (N : Node_Id);
248 -- This is the top-level routine to perform array aggregate expansion.
249 -- N is the N_Aggregate node to be expanded.
251 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
252 -- For two-dimensional packed aggregates with constant bounds and constant
253 -- components, it is preferable to pack the inner aggregates because the
254 -- whole matrix can then be presented to the back-end as a one-dimensional
255 -- list of literals. This is much more efficient than expanding into single
256 -- component assignments. This function determines if the type Typ is for
257 -- an array that is suitable for this optimization: it returns True if Typ
258 -- is a two dimensional bit packed array with component size 1, 2, or 4.
260 function Late_Expansion
261 (N : Node_Id;
262 Typ : Entity_Id;
263 Target : Node_Id) return List_Id;
264 -- This routine implements top-down expansion of nested aggregates. In
265 -- doing so, it avoids the generation of temporaries at each level. N is
266 -- a nested record or array aggregate with the Expansion_Delayed flag.
267 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
268 -- expression that will hold the result of the aggregate expansion.
270 function Make_OK_Assignment_Statement
271 (Sloc : Source_Ptr;
272 Name : Node_Id;
273 Expression : Node_Id) return Node_Id;
274 -- This is like Make_Assignment_Statement, except that Assignment_OK
275 -- is set in the left operand. All assignments built by this unit use
276 -- this routine. This is needed to deal with assignments to initialized
277 -- constants that are done in place.
279 function Number_Of_Choices (N : Node_Id) return Nat;
280 -- Returns the number of discrete choices (not including the others choice
281 -- if present) contained in (sub-)aggregate N.
283 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
284 -- Given an array aggregate, this function handles the case of a packed
285 -- array aggregate with all constant values, where the aggregate can be
286 -- evaluated at compile time. If this is possible, then N is rewritten
287 -- to be its proper compile time value with all the components properly
288 -- assembled. The expression is analyzed and resolved and True is returned.
289 -- If this transformation is not possible, N is unchanged and False is
290 -- returned.
292 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
293 -- If the type of the aggregate is a two-dimensional bit_packed array
294 -- it may be transformed into an array of bytes with constant values,
295 -- and presented to the back-end as a static value. The function returns
296 -- false if this transformation cannot be performed. THis is similar to,
297 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
299 ------------------
300 -- Aggr_Size_OK --
301 ------------------
303 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
304 Lo : Node_Id;
305 Hi : Node_Id;
306 Indx : Node_Id;
307 Siz : Int;
308 Lov : Uint;
309 Hiv : Uint;
311 Max_Aggr_Size : Nat;
312 -- Determines the maximum size of an array aggregate produced by
313 -- converting named to positional notation (e.g. from others clauses).
314 -- This avoids running away with attempts to convert huge aggregates,
315 -- which hit memory limits in the backend.
317 function Component_Count (T : Entity_Id) return Int;
318 -- The limit is applied to the total number of components that the
319 -- aggregate will have, which is the number of static expressions
320 -- that will appear in the flattened array. This requires a recursive
321 -- computation of the number of scalar components of the structure.
323 ---------------------
324 -- Component_Count --
325 ---------------------
327 function Component_Count (T : Entity_Id) return Int is
328 Res : Int := 0;
329 Comp : Entity_Id;
331 begin
332 if Is_Scalar_Type (T) then
333 return 1;
335 elsif Is_Record_Type (T) then
336 Comp := First_Component (T);
337 while Present (Comp) loop
338 Res := Res + Component_Count (Etype (Comp));
339 Next_Component (Comp);
340 end loop;
342 return Res;
344 elsif Is_Array_Type (T) then
345 declare
346 Lo : constant Node_Id :=
347 Type_Low_Bound (Etype (First_Index (T)));
348 Hi : constant Node_Id :=
349 Type_High_Bound (Etype (First_Index (T)));
351 Siz : constant Int := Component_Count (Component_Type (T));
353 begin
354 if not Compile_Time_Known_Value (Lo)
355 or else not Compile_Time_Known_Value (Hi)
356 then
357 return 0;
358 else
359 return
360 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
361 end if;
362 end;
364 else
365 -- Can only be a null for an access type
367 return 1;
368 end if;
369 end Component_Count;
371 -- Start of processing for Aggr_Size_OK
373 begin
374 -- The normal aggregate limit is 50000, but we increase this limit to
375 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
376 -- Restrictions (No_Implicit_Loops) is specified, since in either case
377 -- we are at risk of declaring the program illegal because of this
378 -- limit. We also increase the limit when Static_Elaboration_Desired,
379 -- given that this means that objects are intended to be placed in data
380 -- memory.
382 -- We also increase the limit if the aggregate is for a packed two-
383 -- dimensional array, because if components are static it is much more
384 -- efficient to construct a one-dimensional equivalent array with static
385 -- components.
387 -- Conversely, we decrease the maximum size if none of the above
388 -- requirements apply, and if the aggregate has a single component
389 -- association, which will be more efficient if implemented with a loop.
391 -- Finally, we use a small limit in CodePeer mode where we favor loops
392 -- instead of thousands of single assignments (from large aggregates).
394 Max_Aggr_Size := 50000;
396 if CodePeer_Mode then
397 Max_Aggr_Size := 100;
399 elsif Restriction_Active (No_Elaboration_Code)
400 or else Restriction_Active (No_Implicit_Loops)
401 or else Is_Two_Dim_Packed_Array (Typ)
402 or else (Ekind (Current_Scope) = E_Package
403 and then Static_Elaboration_Desired (Current_Scope))
404 then
405 Max_Aggr_Size := 2 ** 24;
407 elsif No (Expressions (N))
408 and then No (Next (First (Component_Associations (N))))
409 then
410 Max_Aggr_Size := 5000;
411 end if;
413 Siz := Component_Count (Component_Type (Typ));
415 Indx := First_Index (Typ);
416 while Present (Indx) loop
417 Lo := Type_Low_Bound (Etype (Indx));
418 Hi := Type_High_Bound (Etype (Indx));
420 -- Bounds need to be known at compile time
422 if not Compile_Time_Known_Value (Lo)
423 or else not Compile_Time_Known_Value (Hi)
424 then
425 return False;
426 end if;
428 Lov := Expr_Value (Lo);
429 Hiv := Expr_Value (Hi);
431 -- A flat array is always safe
433 if Hiv < Lov then
434 return True;
435 end if;
437 -- One-component aggregates are suspicious, and if the context type
438 -- is an object declaration with non-static bounds it will trip gcc;
439 -- such an aggregate must be expanded into a single assignment.
441 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
442 declare
443 Index_Type : constant Entity_Id :=
444 Etype
445 (First_Index (Etype (Defining_Identifier (Parent (N)))));
446 Indx : Node_Id;
448 begin
449 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
450 or else not Compile_Time_Known_Value
451 (Type_High_Bound (Index_Type))
452 then
453 if Present (Component_Associations (N)) then
454 Indx :=
455 First (Choices (First (Component_Associations (N))));
457 if Is_Entity_Name (Indx)
458 and then not Is_Type (Entity (Indx))
459 then
460 Error_Msg_N
461 ("single component aggregate in "
462 & "non-static context??", Indx);
463 Error_Msg_N ("\maybe subtype name was meant??", Indx);
464 end if;
465 end if;
467 return False;
468 end if;
469 end;
470 end if;
472 declare
473 Rng : constant Uint := Hiv - Lov + 1;
475 begin
476 -- Check if size is too large
478 if not UI_Is_In_Int_Range (Rng) then
479 return False;
480 end if;
482 Siz := Siz * UI_To_Int (Rng);
483 end;
485 if Siz <= 0
486 or else Siz > Max_Aggr_Size
487 then
488 return False;
489 end if;
491 -- Bounds must be in integer range, for later array construction
493 if not UI_Is_In_Int_Range (Lov)
494 or else
495 not UI_Is_In_Int_Range (Hiv)
496 then
497 return False;
498 end if;
500 Next_Index (Indx);
501 end loop;
503 return True;
504 end Aggr_Size_OK;
506 ---------------------------------
507 -- Backend_Processing_Possible --
508 ---------------------------------
510 -- Backend processing by Gigi/gcc is possible only if all the following
511 -- conditions are met:
513 -- 1. N is fully positional
515 -- 2. N is not a bit-packed array aggregate;
517 -- 3. The size of N's array type must be known at compile time. Note
518 -- that this implies that the component size is also known
520 -- 4. The array type of N does not follow the Fortran layout convention
521 -- or if it does it must be 1 dimensional.
523 -- 5. The array component type may not be tagged (which could necessitate
524 -- reassignment of proper tags).
526 -- 6. The array component type must not have unaligned bit components
528 -- 7. None of the components of the aggregate may be bit unaligned
529 -- components.
531 -- 8. There cannot be delayed components, since we do not know enough
532 -- at this stage to know if back end processing is possible.
534 -- 9. There cannot be any discriminated record components, since the
535 -- back end cannot handle this complex case.
537 -- 10. No controlled actions need to be generated for components
539 -- 11. For a VM back end, the array should have no aliased components
541 function Backend_Processing_Possible (N : Node_Id) return Boolean is
542 Typ : constant Entity_Id := Etype (N);
543 -- Typ is the correct constrained array subtype of the aggregate
545 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
546 -- This routine checks components of aggregate N, enforcing checks
547 -- 1, 7, 8, and 9. In the multi-dimensional case, these checks are
548 -- performed on subaggregates. The Index value is the current index
549 -- being checked in the multi-dimensional case.
551 ---------------------
552 -- Component_Check --
553 ---------------------
555 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
556 Expr : Node_Id;
558 begin
559 -- Checks 1: (no component associations)
561 if Present (Component_Associations (N)) then
562 return False;
563 end if;
565 -- Checks on components
567 -- Recurse to check subaggregates, which may appear in qualified
568 -- expressions. If delayed, the front-end will have to expand.
569 -- If the component is a discriminated record, treat as non-static,
570 -- as the back-end cannot handle this properly.
572 Expr := First (Expressions (N));
573 while Present (Expr) loop
575 -- Checks 8: (no delayed components)
577 if Is_Delayed_Aggregate (Expr) then
578 return False;
579 end if;
581 -- Checks 9: (no discriminated records)
583 if Present (Etype (Expr))
584 and then Is_Record_Type (Etype (Expr))
585 and then Has_Discriminants (Etype (Expr))
586 then
587 return False;
588 end if;
590 -- Checks 7. Component must not be bit aligned component
592 if Possible_Bit_Aligned_Component (Expr) then
593 return False;
594 end if;
596 -- Recursion to following indexes for multiple dimension case
598 if Present (Next_Index (Index))
599 and then not Component_Check (Expr, Next_Index (Index))
600 then
601 return False;
602 end if;
604 -- All checks for that component finished, on to next
606 Next (Expr);
607 end loop;
609 return True;
610 end Component_Check;
612 -- Start of processing for Backend_Processing_Possible
614 begin
615 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
617 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
618 return False;
619 end if;
621 -- If component is limited, aggregate must be expanded because each
622 -- component assignment must be built in place.
624 if Is_Limited_View (Component_Type (Typ)) then
625 return False;
626 end if;
628 -- Checks 4 (array must not be multi-dimensional Fortran case)
630 if Convention (Typ) = Convention_Fortran
631 and then Number_Dimensions (Typ) > 1
632 then
633 return False;
634 end if;
636 -- Checks 3 (size of array must be known at compile time)
638 if not Size_Known_At_Compile_Time (Typ) then
639 return False;
640 end if;
642 -- Checks on components
644 if not Component_Check (N, First_Index (Typ)) then
645 return False;
646 end if;
648 -- Checks 5 (if the component type is tagged, then we may need to do
649 -- tag adjustments. Perhaps this should be refined to check for any
650 -- component associations that actually need tag adjustment, similar
651 -- to the test in Component_Not_OK_For_Backend for record aggregates
652 -- with tagged components, but not clear whether it's worthwhile ???;
653 -- in the case of the JVM, object tags are handled implicitly)
655 if Is_Tagged_Type (Component_Type (Typ))
656 and then Tagged_Type_Expansion
657 then
658 return False;
659 end if;
661 -- Checks 6 (component type must not have bit aligned components)
663 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
664 return False;
665 end if;
667 -- Checks 11: Array aggregates with aliased components are currently
668 -- not well supported by the VM backend; disable temporarily this
669 -- backend processing until it is definitely supported.
671 if VM_Target /= No_VM
672 and then Has_Aliased_Components (Base_Type (Typ))
673 then
674 return False;
675 end if;
677 -- Backend processing is possible
679 Set_Size_Known_At_Compile_Time (Etype (N), True);
680 return True;
681 end Backend_Processing_Possible;
683 ---------------------------
684 -- Build_Array_Aggr_Code --
685 ---------------------------
687 -- The code that we generate from a one dimensional aggregate is
689 -- 1. If the sub-aggregate contains discrete choices we
691 -- (a) Sort the discrete choices
693 -- (b) Otherwise for each discrete choice that specifies a range we
694 -- emit a loop. If a range specifies a maximum of three values, or
695 -- we are dealing with an expression we emit a sequence of
696 -- assignments instead of a loop.
698 -- (c) Generate the remaining loops to cover the others choice if any
700 -- 2. If the aggregate contains positional elements we
702 -- (a) translate the positional elements in a series of assignments
704 -- (b) Generate a final loop to cover the others choice if any.
705 -- Note that this final loop has to be a while loop since the case
707 -- L : Integer := Integer'Last;
708 -- H : Integer := Integer'Last;
709 -- A : array (L .. H) := (1, others =>0);
711 -- cannot be handled by a for loop. Thus for the following
713 -- array (L .. H) := (.. positional elements.., others =>E);
715 -- we always generate something like:
717 -- J : Index_Type := Index_Of_Last_Positional_Element;
718 -- while J < H loop
719 -- J := Index_Base'Succ (J)
720 -- Tmp (J) := E;
721 -- end loop;
723 function Build_Array_Aggr_Code
724 (N : Node_Id;
725 Ctype : Entity_Id;
726 Index : Node_Id;
727 Into : Node_Id;
728 Scalar_Comp : Boolean;
729 Indexes : List_Id := No_List) return List_Id
731 Loc : constant Source_Ptr := Sloc (N);
732 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
733 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
734 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
736 function Add (Val : Int; To : Node_Id) return Node_Id;
737 -- Returns an expression where Val is added to expression To, unless
738 -- To+Val is provably out of To's base type range. To must be an
739 -- already analyzed expression.
741 function Empty_Range (L, H : Node_Id) return Boolean;
742 -- Returns True if the range defined by L .. H is certainly empty
744 function Equal (L, H : Node_Id) return Boolean;
745 -- Returns True if L = H for sure
747 function Index_Base_Name return Node_Id;
748 -- Returns a new reference to the index type name
750 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
751 -- Ind must be a side-effect free expression. If the input aggregate
752 -- N to Build_Loop contains no sub-aggregates, then this function
753 -- returns the assignment statement:
755 -- Into (Indexes, Ind) := Expr;
757 -- Otherwise we call Build_Code recursively
759 -- Ada 2005 (AI-287): In case of default initialized component, Expr
760 -- is empty and we generate a call to the corresponding IP subprogram.
762 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
763 -- Nodes L and H must be side-effect free expressions.
764 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
765 -- This routine returns the for loop statement
767 -- for J in Index_Base'(L) .. Index_Base'(H) loop
768 -- Into (Indexes, J) := Expr;
769 -- end loop;
771 -- Otherwise we call Build_Code recursively.
772 -- As an optimization if the loop covers 3 or less scalar elements we
773 -- generate a sequence of assignments.
775 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
776 -- Nodes L and H must be side-effect free expressions.
777 -- If the input aggregate N to Build_Loop contains no sub-aggregates,
778 -- This routine returns the while loop statement
780 -- J : Index_Base := L;
781 -- while J < H loop
782 -- J := Index_Base'Succ (J);
783 -- Into (Indexes, J) := Expr;
784 -- end loop;
786 -- Otherwise we call Build_Code recursively
788 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
789 function Local_Expr_Value (E : Node_Id) return Uint;
790 -- These two Local routines are used to replace the corresponding ones
791 -- in sem_eval because while processing the bounds of an aggregate with
792 -- discrete choices whose index type is an enumeration, we build static
793 -- expressions not recognized by Compile_Time_Known_Value as such since
794 -- they have not yet been analyzed and resolved. All the expressions in
795 -- question are things like Index_Base_Name'Val (Const) which we can
796 -- easily recognize as being constant.
798 ---------
799 -- Add --
800 ---------
802 function Add (Val : Int; To : Node_Id) return Node_Id is
803 Expr_Pos : Node_Id;
804 Expr : Node_Id;
805 To_Pos : Node_Id;
806 U_To : Uint;
807 U_Val : constant Uint := UI_From_Int (Val);
809 begin
810 -- Note: do not try to optimize the case of Val = 0, because
811 -- we need to build a new node with the proper Sloc value anyway.
813 -- First test if we can do constant folding
815 if Local_Compile_Time_Known_Value (To) then
816 U_To := Local_Expr_Value (To) + Val;
818 -- Determine if our constant is outside the range of the index.
819 -- If so return an Empty node. This empty node will be caught
820 -- by Empty_Range below.
822 if Compile_Time_Known_Value (Index_Base_L)
823 and then U_To < Expr_Value (Index_Base_L)
824 then
825 return Empty;
827 elsif Compile_Time_Known_Value (Index_Base_H)
828 and then U_To > Expr_Value (Index_Base_H)
829 then
830 return Empty;
831 end if;
833 Expr_Pos := Make_Integer_Literal (Loc, U_To);
834 Set_Is_Static_Expression (Expr_Pos);
836 if not Is_Enumeration_Type (Index_Base) then
837 Expr := Expr_Pos;
839 -- If we are dealing with enumeration return
840 -- Index_Base'Val (Expr_Pos)
842 else
843 Expr :=
844 Make_Attribute_Reference
845 (Loc,
846 Prefix => Index_Base_Name,
847 Attribute_Name => Name_Val,
848 Expressions => New_List (Expr_Pos));
849 end if;
851 return Expr;
852 end if;
854 -- If we are here no constant folding possible
856 if not Is_Enumeration_Type (Index_Base) then
857 Expr :=
858 Make_Op_Add (Loc,
859 Left_Opnd => Duplicate_Subexpr (To),
860 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
862 -- If we are dealing with enumeration return
863 -- Index_Base'Val (Index_Base'Pos (To) + Val)
865 else
866 To_Pos :=
867 Make_Attribute_Reference
868 (Loc,
869 Prefix => Index_Base_Name,
870 Attribute_Name => Name_Pos,
871 Expressions => New_List (Duplicate_Subexpr (To)));
873 Expr_Pos :=
874 Make_Op_Add (Loc,
875 Left_Opnd => To_Pos,
876 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
878 Expr :=
879 Make_Attribute_Reference
880 (Loc,
881 Prefix => Index_Base_Name,
882 Attribute_Name => Name_Val,
883 Expressions => New_List (Expr_Pos));
884 end if;
886 return Expr;
887 end Add;
889 -----------------
890 -- Empty_Range --
891 -----------------
893 function Empty_Range (L, H : Node_Id) return Boolean is
894 Is_Empty : Boolean := False;
895 Low : Node_Id;
896 High : Node_Id;
898 begin
899 -- First check if L or H were already detected as overflowing the
900 -- index base range type by function Add above. If this is so Add
901 -- returns the empty node.
903 if No (L) or else No (H) then
904 return True;
905 end if;
907 for J in 1 .. 3 loop
908 case J is
910 -- L > H range is empty
912 when 1 =>
913 Low := L;
914 High := H;
916 -- B_L > H range must be empty
918 when 2 =>
919 Low := Index_Base_L;
920 High := H;
922 -- L > B_H range must be empty
924 when 3 =>
925 Low := L;
926 High := Index_Base_H;
927 end case;
929 if Local_Compile_Time_Known_Value (Low)
930 and then
931 Local_Compile_Time_Known_Value (High)
932 then
933 Is_Empty :=
934 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
935 end if;
937 exit when Is_Empty;
938 end loop;
940 return Is_Empty;
941 end Empty_Range;
943 -----------
944 -- Equal --
945 -----------
947 function Equal (L, H : Node_Id) return Boolean is
948 begin
949 if L = H then
950 return True;
952 elsif Local_Compile_Time_Known_Value (L)
953 and then
954 Local_Compile_Time_Known_Value (H)
955 then
956 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
957 end if;
959 return False;
960 end Equal;
962 ----------------
963 -- Gen_Assign --
964 ----------------
966 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
967 L : constant List_Id := New_List;
968 A : Node_Id;
970 New_Indexes : List_Id;
971 Indexed_Comp : Node_Id;
972 Expr_Q : Node_Id;
973 Comp_Type : Entity_Id := Empty;
975 function Add_Loop_Actions (Lis : List_Id) return List_Id;
976 -- Collect insert_actions generated in the construction of a
977 -- loop, and prepend them to the sequence of assignments to
978 -- complete the eventual body of the loop.
980 ----------------------
981 -- Add_Loop_Actions --
982 ----------------------
984 function Add_Loop_Actions (Lis : List_Id) return List_Id is
985 Res : List_Id;
987 begin
988 -- Ada 2005 (AI-287): Do nothing else in case of default
989 -- initialized component.
991 if No (Expr) then
992 return Lis;
994 elsif Nkind (Parent (Expr)) = N_Component_Association
995 and then Present (Loop_Actions (Parent (Expr)))
996 then
997 Append_List (Lis, Loop_Actions (Parent (Expr)));
998 Res := Loop_Actions (Parent (Expr));
999 Set_Loop_Actions (Parent (Expr), No_List);
1000 return Res;
1002 else
1003 return Lis;
1004 end if;
1005 end Add_Loop_Actions;
1007 -- Start of processing for Gen_Assign
1009 begin
1010 if No (Indexes) then
1011 New_Indexes := New_List;
1012 else
1013 New_Indexes := New_Copy_List_Tree (Indexes);
1014 end if;
1016 Append_To (New_Indexes, Ind);
1018 if Present (Next_Index (Index)) then
1019 return
1020 Add_Loop_Actions (
1021 Build_Array_Aggr_Code
1022 (N => Expr,
1023 Ctype => Ctype,
1024 Index => Next_Index (Index),
1025 Into => Into,
1026 Scalar_Comp => Scalar_Comp,
1027 Indexes => New_Indexes));
1028 end if;
1030 -- If we get here then we are at a bottom-level (sub-)aggregate
1032 Indexed_Comp :=
1033 Checks_Off
1034 (Make_Indexed_Component (Loc,
1035 Prefix => New_Copy_Tree (Into),
1036 Expressions => New_Indexes));
1038 Set_Assignment_OK (Indexed_Comp);
1040 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1041 -- is not present (and therefore we also initialize Expr_Q to empty).
1043 if No (Expr) then
1044 Expr_Q := Empty;
1045 elsif Nkind (Expr) = N_Qualified_Expression then
1046 Expr_Q := Expression (Expr);
1047 else
1048 Expr_Q := Expr;
1049 end if;
1051 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1052 Comp_Type := Component_Type (Etype (N));
1053 pragma Assert (Comp_Type = Ctype); -- AI-287
1055 elsif Present (Next (First (New_Indexes))) then
1057 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1058 -- component because we have received the component type in
1059 -- the formal parameter Ctype.
1061 -- ??? Some assert pragmas have been added to check if this new
1062 -- formal can be used to replace this code in all cases.
1064 if Present (Expr) then
1066 -- This is a multidimensional array. Recover the component type
1067 -- from the outermost aggregate, because subaggregates do not
1068 -- have an assigned type.
1070 declare
1071 P : Node_Id;
1073 begin
1074 P := Parent (Expr);
1075 while Present (P) loop
1076 if Nkind (P) = N_Aggregate
1077 and then Present (Etype (P))
1078 then
1079 Comp_Type := Component_Type (Etype (P));
1080 exit;
1082 else
1083 P := Parent (P);
1084 end if;
1085 end loop;
1087 pragma Assert (Comp_Type = Ctype); -- AI-287
1088 end;
1089 end if;
1090 end if;
1092 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1093 -- default initialized components (otherwise Expr_Q is not present).
1095 if Present (Expr_Q)
1096 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1097 then
1098 -- At this stage the Expression may not have been analyzed yet
1099 -- because the array aggregate code has not been updated to use
1100 -- the Expansion_Delayed flag and avoid analysis altogether to
1101 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1102 -- the analysis of non-array aggregates now in order to get the
1103 -- value of Expansion_Delayed flag for the inner aggregate ???
1105 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1106 Analyze_And_Resolve (Expr_Q, Comp_Type);
1107 end if;
1109 if Is_Delayed_Aggregate (Expr_Q) then
1111 -- This is either a subaggregate of a multidimensional array,
1112 -- or a component of an array type whose component type is
1113 -- also an array. In the latter case, the expression may have
1114 -- component associations that provide different bounds from
1115 -- those of the component type, and sliding must occur. Instead
1116 -- of decomposing the current aggregate assignment, force the
1117 -- re-analysis of the assignment, so that a temporary will be
1118 -- generated in the usual fashion, and sliding will take place.
1120 if Nkind (Parent (N)) = N_Assignment_Statement
1121 and then Is_Array_Type (Comp_Type)
1122 and then Present (Component_Associations (Expr_Q))
1123 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1124 then
1125 Set_Expansion_Delayed (Expr_Q, False);
1126 Set_Analyzed (Expr_Q, False);
1128 else
1129 return
1130 Add_Loop_Actions (
1131 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1132 end if;
1133 end if;
1134 end if;
1136 -- Ada 2005 (AI-287): In case of default initialized component, call
1137 -- the initialization subprogram associated with the component type.
1138 -- If the component type is an access type, add an explicit null
1139 -- assignment, because for the back-end there is an initialization
1140 -- present for the whole aggregate, and no default initialization
1141 -- will take place.
1143 -- In addition, if the component type is controlled, we must call
1144 -- its Initialize procedure explicitly, because there is no explicit
1145 -- object creation that will invoke it otherwise.
1147 if No (Expr) then
1148 if Present (Base_Init_Proc (Base_Type (Ctype)))
1149 or else Has_Task (Base_Type (Ctype))
1150 then
1151 Append_List_To (L,
1152 Build_Initialization_Call (Loc,
1153 Id_Ref => Indexed_Comp,
1154 Typ => Ctype,
1155 With_Default_Init => True));
1157 elsif Is_Access_Type (Ctype) then
1158 Append_To (L,
1159 Make_Assignment_Statement (Loc,
1160 Name => Indexed_Comp,
1161 Expression => Make_Null (Loc)));
1162 end if;
1164 if Needs_Finalization (Ctype) then
1165 Append_To (L,
1166 Make_Init_Call
1167 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1168 Typ => Ctype));
1169 end if;
1171 else
1172 A :=
1173 Make_OK_Assignment_Statement (Loc,
1174 Name => Indexed_Comp,
1175 Expression => New_Copy_Tree (Expr));
1177 -- The target of the assignment may not have been initialized,
1178 -- so it is not possible to call Finalize as expected in normal
1179 -- controlled assignments. We must also avoid using the primitive
1180 -- _assign (which depends on a valid target, and may for example
1181 -- perform discriminant checks on it).
1183 -- Both Finalize and usage of _assign are disabled by setting
1184 -- No_Ctrl_Actions on the assignment. The rest of the controlled
1185 -- actions are done manually with the proper finalization list
1186 -- coming from the context.
1188 Set_No_Ctrl_Actions (A);
1190 -- If this is an aggregate for an array of arrays, each
1191 -- sub-aggregate will be expanded as well, and even with
1192 -- No_Ctrl_Actions the assignments of inner components will
1193 -- require attachment in their assignments to temporaries. These
1194 -- temporaries must be finalized for each subaggregate, to prevent
1195 -- multiple attachments of the same temporary location to same
1196 -- finalization chain (and consequently circular lists). To ensure
1197 -- that finalization takes place for each subaggregate we wrap the
1198 -- assignment in a block.
1200 if Present (Comp_Type)
1201 and then Needs_Finalization (Comp_Type)
1202 and then Is_Array_Type (Comp_Type)
1203 and then Present (Expr)
1204 then
1205 A :=
1206 Make_Block_Statement (Loc,
1207 Handled_Statement_Sequence =>
1208 Make_Handled_Sequence_Of_Statements (Loc,
1209 Statements => New_List (A)));
1210 end if;
1212 Append_To (L, A);
1214 -- Adjust the tag if tagged (because of possible view
1215 -- conversions), unless compiling for a VM where tags
1216 -- are implicit.
1218 if Present (Comp_Type)
1219 and then Is_Tagged_Type (Comp_Type)
1220 and then Tagged_Type_Expansion
1221 then
1222 declare
1223 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Type);
1225 begin
1226 A :=
1227 Make_OK_Assignment_Statement (Loc,
1228 Name =>
1229 Make_Selected_Component (Loc,
1230 Prefix => New_Copy_Tree (Indexed_Comp),
1231 Selector_Name =>
1232 New_Occurrence_Of
1233 (First_Tag_Component (Full_Typ), Loc)),
1235 Expression =>
1236 Unchecked_Convert_To (RTE (RE_Tag),
1237 New_Occurrence_Of
1238 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1239 Loc)));
1241 Append_To (L, A);
1242 end;
1243 end if;
1245 -- Adjust and attach the component to the proper final list, which
1246 -- can be the controller of the outer record object or the final
1247 -- list associated with the scope.
1249 -- If the component is itself an array of controlled types, whose
1250 -- value is given by a sub-aggregate, then the attach calls have
1251 -- been generated when individual subcomponent are assigned, and
1252 -- must not be done again to prevent malformed finalization chains
1253 -- (see comments above, concerning the creation of a block to hold
1254 -- inner finalization actions).
1256 if Present (Comp_Type)
1257 and then Needs_Finalization (Comp_Type)
1258 and then not Is_Limited_Type (Comp_Type)
1259 and then not
1260 (Is_Array_Type (Comp_Type)
1261 and then Is_Controlled (Component_Type (Comp_Type))
1262 and then Nkind (Expr) = N_Aggregate)
1263 then
1264 Append_To (L,
1265 Make_Adjust_Call
1266 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1267 Typ => Comp_Type));
1268 end if;
1269 end if;
1271 return Add_Loop_Actions (L);
1272 end Gen_Assign;
1274 --------------
1275 -- Gen_Loop --
1276 --------------
1278 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1279 L_J : Node_Id;
1281 L_L : Node_Id;
1282 -- Index_Base'(L)
1284 L_H : Node_Id;
1285 -- Index_Base'(H)
1287 L_Range : Node_Id;
1288 -- Index_Base'(L) .. Index_Base'(H)
1290 L_Iteration_Scheme : Node_Id;
1291 -- L_J in Index_Base'(L) .. Index_Base'(H)
1293 L_Body : List_Id;
1294 -- The statements to execute in the loop
1296 S : constant List_Id := New_List;
1297 -- List of statements
1299 Tcopy : Node_Id;
1300 -- Copy of expression tree, used for checking purposes
1302 begin
1303 -- If loop bounds define an empty range return the null statement
1305 if Empty_Range (L, H) then
1306 Append_To (S, Make_Null_Statement (Loc));
1308 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1309 -- default initialized component.
1311 if No (Expr) then
1312 null;
1314 else
1315 -- The expression must be type-checked even though no component
1316 -- of the aggregate will have this value. This is done only for
1317 -- actual components of the array, not for subaggregates. Do
1318 -- the check on a copy, because the expression may be shared
1319 -- among several choices, some of which might be non-null.
1321 if Present (Etype (N))
1322 and then Is_Array_Type (Etype (N))
1323 and then No (Next_Index (Index))
1324 then
1325 Expander_Mode_Save_And_Set (False);
1326 Tcopy := New_Copy_Tree (Expr);
1327 Set_Parent (Tcopy, N);
1328 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1329 Expander_Mode_Restore;
1330 end if;
1331 end if;
1333 return S;
1335 -- If loop bounds are the same then generate an assignment
1337 elsif Equal (L, H) then
1338 return Gen_Assign (New_Copy_Tree (L), Expr);
1340 -- If H - L <= 2 then generate a sequence of assignments when we are
1341 -- processing the bottom most aggregate and it contains scalar
1342 -- components.
1344 elsif No (Next_Index (Index))
1345 and then Scalar_Comp
1346 and then Local_Compile_Time_Known_Value (L)
1347 and then Local_Compile_Time_Known_Value (H)
1348 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1349 then
1351 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1352 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1354 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1355 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1356 end if;
1358 return S;
1359 end if;
1361 -- Otherwise construct the loop, starting with the loop index L_J
1363 L_J := Make_Temporary (Loc, 'J', L);
1365 -- Construct "L .. H" in Index_Base. We use a qualified expression
1366 -- for the bound to convert to the index base, but we don't need
1367 -- to do that if we already have the base type at hand.
1369 if Etype (L) = Index_Base then
1370 L_L := L;
1371 else
1372 L_L :=
1373 Make_Qualified_Expression (Loc,
1374 Subtype_Mark => Index_Base_Name,
1375 Expression => L);
1376 end if;
1378 if Etype (H) = Index_Base then
1379 L_H := H;
1380 else
1381 L_H :=
1382 Make_Qualified_Expression (Loc,
1383 Subtype_Mark => Index_Base_Name,
1384 Expression => H);
1385 end if;
1387 L_Range :=
1388 Make_Range (Loc,
1389 Low_Bound => L_L,
1390 High_Bound => L_H);
1392 -- Construct "for L_J in Index_Base range L .. H"
1394 L_Iteration_Scheme :=
1395 Make_Iteration_Scheme
1396 (Loc,
1397 Loop_Parameter_Specification =>
1398 Make_Loop_Parameter_Specification
1399 (Loc,
1400 Defining_Identifier => L_J,
1401 Discrete_Subtype_Definition => L_Range));
1403 -- Construct the statements to execute in the loop body
1405 L_Body := Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr);
1407 -- Construct the final loop
1409 Append_To (S,
1410 Make_Implicit_Loop_Statement
1411 (Node => N,
1412 Identifier => Empty,
1413 Iteration_Scheme => L_Iteration_Scheme,
1414 Statements => L_Body));
1416 -- A small optimization: if the aggregate is initialized with a box
1417 -- and the component type has no initialization procedure, remove the
1418 -- useless empty loop.
1420 if Nkind (First (S)) = N_Loop_Statement
1421 and then Is_Empty_List (Statements (First (S)))
1422 then
1423 return New_List (Make_Null_Statement (Loc));
1424 else
1425 return S;
1426 end if;
1427 end Gen_Loop;
1429 ---------------
1430 -- Gen_While --
1431 ---------------
1433 -- The code built is
1435 -- W_J : Index_Base := L;
1436 -- while W_J < H loop
1437 -- W_J := Index_Base'Succ (W);
1438 -- L_Body;
1439 -- end loop;
1441 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1442 W_J : Node_Id;
1444 W_Decl : Node_Id;
1445 -- W_J : Base_Type := L;
1447 W_Iteration_Scheme : Node_Id;
1448 -- while W_J < H
1450 W_Index_Succ : Node_Id;
1451 -- Index_Base'Succ (J)
1453 W_Increment : Node_Id;
1454 -- W_J := Index_Base'Succ (W)
1456 W_Body : constant List_Id := New_List;
1457 -- The statements to execute in the loop
1459 S : constant List_Id := New_List;
1460 -- list of statement
1462 begin
1463 -- If loop bounds define an empty range or are equal return null
1465 if Empty_Range (L, H) or else Equal (L, H) then
1466 Append_To (S, Make_Null_Statement (Loc));
1467 return S;
1468 end if;
1470 -- Build the decl of W_J
1472 W_J := Make_Temporary (Loc, 'J', L);
1473 W_Decl :=
1474 Make_Object_Declaration
1475 (Loc,
1476 Defining_Identifier => W_J,
1477 Object_Definition => Index_Base_Name,
1478 Expression => L);
1480 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1481 -- that in this particular case L is a fresh Expr generated by
1482 -- Add which we are the only ones to use.
1484 Append_To (S, W_Decl);
1486 -- Construct " while W_J < H"
1488 W_Iteration_Scheme :=
1489 Make_Iteration_Scheme
1490 (Loc,
1491 Condition => Make_Op_Lt
1492 (Loc,
1493 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1494 Right_Opnd => New_Copy_Tree (H)));
1496 -- Construct the statements to execute in the loop body
1498 W_Index_Succ :=
1499 Make_Attribute_Reference
1500 (Loc,
1501 Prefix => Index_Base_Name,
1502 Attribute_Name => Name_Succ,
1503 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1505 W_Increment :=
1506 Make_OK_Assignment_Statement
1507 (Loc,
1508 Name => New_Occurrence_Of (W_J, Loc),
1509 Expression => W_Index_Succ);
1511 Append_To (W_Body, W_Increment);
1512 Append_List_To (W_Body,
1513 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr));
1515 -- Construct the final loop
1517 Append_To (S,
1518 Make_Implicit_Loop_Statement
1519 (Node => N,
1520 Identifier => Empty,
1521 Iteration_Scheme => W_Iteration_Scheme,
1522 Statements => W_Body));
1524 return S;
1525 end Gen_While;
1527 ---------------------
1528 -- Index_Base_Name --
1529 ---------------------
1531 function Index_Base_Name return Node_Id is
1532 begin
1533 return New_Occurrence_Of (Index_Base, Sloc (N));
1534 end Index_Base_Name;
1536 ------------------------------------
1537 -- Local_Compile_Time_Known_Value --
1538 ------------------------------------
1540 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1541 begin
1542 return Compile_Time_Known_Value (E)
1543 or else
1544 (Nkind (E) = N_Attribute_Reference
1545 and then Attribute_Name (E) = Name_Val
1546 and then Compile_Time_Known_Value (First (Expressions (E))));
1547 end Local_Compile_Time_Known_Value;
1549 ----------------------
1550 -- Local_Expr_Value --
1551 ----------------------
1553 function Local_Expr_Value (E : Node_Id) return Uint is
1554 begin
1555 if Compile_Time_Known_Value (E) then
1556 return Expr_Value (E);
1557 else
1558 return Expr_Value (First (Expressions (E)));
1559 end if;
1560 end Local_Expr_Value;
1562 -- Build_Array_Aggr_Code Variables
1564 Assoc : Node_Id;
1565 Choice : Node_Id;
1566 Expr : Node_Id;
1567 Typ : Entity_Id;
1569 Others_Expr : Node_Id := Empty;
1570 Others_Box_Present : Boolean := False;
1572 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1573 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1574 -- The aggregate bounds of this specific sub-aggregate. Note that if
1575 -- the code generated by Build_Array_Aggr_Code is executed then these
1576 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1578 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1579 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1580 -- After Duplicate_Subexpr these are side-effect free
1582 Low : Node_Id;
1583 High : Node_Id;
1585 Nb_Choices : Nat := 0;
1586 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1587 -- Used to sort all the different choice values
1589 Nb_Elements : Int;
1590 -- Number of elements in the positional aggregate
1592 New_Code : constant List_Id := New_List;
1594 -- Start of processing for Build_Array_Aggr_Code
1596 begin
1597 -- First before we start, a special case. if we have a bit packed
1598 -- array represented as a modular type, then clear the value to
1599 -- zero first, to ensure that unused bits are properly cleared.
1601 Typ := Etype (N);
1603 if Present (Typ)
1604 and then Is_Bit_Packed_Array (Typ)
1605 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
1606 then
1607 Append_To (New_Code,
1608 Make_Assignment_Statement (Loc,
1609 Name => New_Copy_Tree (Into),
1610 Expression =>
1611 Unchecked_Convert_To (Typ,
1612 Make_Integer_Literal (Loc, Uint_0))));
1613 end if;
1615 -- If the component type contains tasks, we need to build a Master
1616 -- entity in the current scope, because it will be needed if build-
1617 -- in-place functions are called in the expanded code.
1619 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
1620 Build_Master_Entity (Defining_Identifier (Parent (N)));
1621 end if;
1623 -- STEP 1: Process component associations
1625 -- For those associations that may generate a loop, initialize
1626 -- Loop_Actions to collect inserted actions that may be crated.
1628 -- Skip this if no component associations
1630 if No (Expressions (N)) then
1632 -- STEP 1 (a): Sort the discrete choices
1634 Assoc := First (Component_Associations (N));
1635 while Present (Assoc) loop
1636 Choice := First (Choices (Assoc));
1637 while Present (Choice) loop
1638 if Nkind (Choice) = N_Others_Choice then
1639 Set_Loop_Actions (Assoc, New_List);
1641 if Box_Present (Assoc) then
1642 Others_Box_Present := True;
1643 else
1644 Others_Expr := Expression (Assoc);
1645 end if;
1646 exit;
1647 end if;
1649 Get_Index_Bounds (Choice, Low, High);
1651 if Low /= High then
1652 Set_Loop_Actions (Assoc, New_List);
1653 end if;
1655 Nb_Choices := Nb_Choices + 1;
1656 if Box_Present (Assoc) then
1657 Table (Nb_Choices) := (Choice_Lo => Low,
1658 Choice_Hi => High,
1659 Choice_Node => Empty);
1660 else
1661 Table (Nb_Choices) := (Choice_Lo => Low,
1662 Choice_Hi => High,
1663 Choice_Node => Expression (Assoc));
1664 end if;
1665 Next (Choice);
1666 end loop;
1668 Next (Assoc);
1669 end loop;
1671 -- If there is more than one set of choices these must be static
1672 -- and we can therefore sort them. Remember that Nb_Choices does not
1673 -- account for an others choice.
1675 if Nb_Choices > 1 then
1676 Sort_Case_Table (Table);
1677 end if;
1679 -- STEP 1 (b): take care of the whole set of discrete choices
1681 for J in 1 .. Nb_Choices loop
1682 Low := Table (J).Choice_Lo;
1683 High := Table (J).Choice_Hi;
1684 Expr := Table (J).Choice_Node;
1685 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1686 end loop;
1688 -- STEP 1 (c): generate the remaining loops to cover others choice
1689 -- We don't need to generate loops over empty gaps, but if there is
1690 -- a single empty range we must analyze the expression for semantics
1692 if Present (Others_Expr) or else Others_Box_Present then
1693 declare
1694 First : Boolean := True;
1696 begin
1697 for J in 0 .. Nb_Choices loop
1698 if J = 0 then
1699 Low := Aggr_Low;
1700 else
1701 Low := Add (1, To => Table (J).Choice_Hi);
1702 end if;
1704 if J = Nb_Choices then
1705 High := Aggr_High;
1706 else
1707 High := Add (-1, To => Table (J + 1).Choice_Lo);
1708 end if;
1710 -- If this is an expansion within an init proc, make
1711 -- sure that discriminant references are replaced by
1712 -- the corresponding discriminal.
1714 if Inside_Init_Proc then
1715 if Is_Entity_Name (Low)
1716 and then Ekind (Entity (Low)) = E_Discriminant
1717 then
1718 Set_Entity (Low, Discriminal (Entity (Low)));
1719 end if;
1721 if Is_Entity_Name (High)
1722 and then Ekind (Entity (High)) = E_Discriminant
1723 then
1724 Set_Entity (High, Discriminal (Entity (High)));
1725 end if;
1726 end if;
1728 if First
1729 or else not Empty_Range (Low, High)
1730 then
1731 First := False;
1732 Append_List
1733 (Gen_Loop (Low, High, Others_Expr), To => New_Code);
1734 end if;
1735 end loop;
1736 end;
1737 end if;
1739 -- STEP 2: Process positional components
1741 else
1742 -- STEP 2 (a): Generate the assignments for each positional element
1743 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1744 -- Aggr_L is analyzed and Add wants an analyzed expression.
1746 Expr := First (Expressions (N));
1747 Nb_Elements := -1;
1748 while Present (Expr) loop
1749 Nb_Elements := Nb_Elements + 1;
1750 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1751 To => New_Code);
1752 Next (Expr);
1753 end loop;
1755 -- STEP 2 (b): Generate final loop if an others choice is present
1756 -- Here Nb_Elements gives the offset of the last positional element.
1758 if Present (Component_Associations (N)) then
1759 Assoc := Last (Component_Associations (N));
1761 -- Ada 2005 (AI-287)
1763 if Box_Present (Assoc) then
1764 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1765 Aggr_High,
1766 Empty),
1767 To => New_Code);
1768 else
1769 Expr := Expression (Assoc);
1771 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1772 Aggr_High,
1773 Expr), -- AI-287
1774 To => New_Code);
1775 end if;
1776 end if;
1777 end if;
1779 return New_Code;
1780 end Build_Array_Aggr_Code;
1782 ----------------------------
1783 -- Build_Record_Aggr_Code --
1784 ----------------------------
1786 function Build_Record_Aggr_Code
1787 (N : Node_Id;
1788 Typ : Entity_Id;
1789 Lhs : Node_Id) return List_Id
1791 Loc : constant Source_Ptr := Sloc (N);
1792 L : constant List_Id := New_List;
1793 N_Typ : constant Entity_Id := Etype (N);
1795 Comp : Node_Id;
1796 Instr : Node_Id;
1797 Ref : Node_Id;
1798 Target : Entity_Id;
1799 Comp_Type : Entity_Id;
1800 Selector : Entity_Id;
1801 Comp_Expr : Node_Id;
1802 Expr_Q : Node_Id;
1804 -- If this is an internal aggregate, the External_Final_List is an
1805 -- expression for the controller record of the enclosing type.
1807 -- If the current aggregate has several controlled components, this
1808 -- expression will appear in several calls to attach to the finali-
1809 -- zation list, and it must not be shared.
1811 Ancestor_Is_Expression : Boolean := False;
1812 Ancestor_Is_Subtype_Mark : Boolean := False;
1814 Init_Typ : Entity_Id := Empty;
1816 Finalization_Done : Boolean := False;
1817 -- True if Generate_Finalization_Actions has already been called; calls
1818 -- after the first do nothing.
1820 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1821 -- Returns the value that the given discriminant of an ancestor type
1822 -- should receive (in the absence of a conflict with the value provided
1823 -- by an ancestor part of an extension aggregate).
1825 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1826 -- Check that each of the discriminant values defined by the ancestor
1827 -- part of an extension aggregate match the corresponding values
1828 -- provided by either an association of the aggregate or by the
1829 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1831 function Compatible_Int_Bounds
1832 (Agg_Bounds : Node_Id;
1833 Typ_Bounds : Node_Id) return Boolean;
1834 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1835 -- assumed that both bounds are integer ranges.
1837 procedure Generate_Finalization_Actions;
1838 -- Deal with the various controlled type data structure initializations
1839 -- (but only if it hasn't been done already).
1841 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1842 -- Returns the first discriminant association in the constraint
1843 -- associated with T, if any, otherwise returns Empty.
1845 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
1846 -- If Typ is derived, and constrains discriminants of the parent type,
1847 -- these discriminants are not components of the aggregate, and must be
1848 -- initialized. The assignments are appended to List. The same is done
1849 -- if Typ derives fron an already constrained subtype of a discriminated
1850 -- parent type.
1852 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
1853 -- If the ancestor part is an unconstrained type and further ancestors
1854 -- do not provide discriminants for it, check aggregate components for
1855 -- values of the discriminants.
1857 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1858 -- Check whether Bounds is a range node and its lower and higher bounds
1859 -- are integers literals.
1861 ---------------------------------
1862 -- Ancestor_Discriminant_Value --
1863 ---------------------------------
1865 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1866 Assoc : Node_Id;
1867 Assoc_Elmt : Elmt_Id;
1868 Aggr_Comp : Entity_Id;
1869 Corresp_Disc : Entity_Id;
1870 Current_Typ : Entity_Id := Base_Type (Typ);
1871 Parent_Typ : Entity_Id;
1872 Parent_Disc : Entity_Id;
1873 Save_Assoc : Node_Id := Empty;
1875 begin
1876 -- First check any discriminant associations to see if any of them
1877 -- provide a value for the discriminant.
1879 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1880 Assoc := First (Component_Associations (N));
1881 while Present (Assoc) loop
1882 Aggr_Comp := Entity (First (Choices (Assoc)));
1884 if Ekind (Aggr_Comp) = E_Discriminant then
1885 Save_Assoc := Expression (Assoc);
1887 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1888 while Present (Corresp_Disc) loop
1890 -- If found a corresponding discriminant then return the
1891 -- value given in the aggregate. (Note: this is not
1892 -- correct in the presence of side effects. ???)
1894 if Disc = Corresp_Disc then
1895 return Duplicate_Subexpr (Expression (Assoc));
1896 end if;
1898 Corresp_Disc :=
1899 Corresponding_Discriminant (Corresp_Disc);
1900 end loop;
1901 end if;
1903 Next (Assoc);
1904 end loop;
1905 end if;
1907 -- No match found in aggregate, so chain up parent types to find
1908 -- a constraint that defines the value of the discriminant.
1910 Parent_Typ := Etype (Current_Typ);
1911 while Current_Typ /= Parent_Typ loop
1912 if Has_Discriminants (Parent_Typ)
1913 and then not Has_Unknown_Discriminants (Parent_Typ)
1914 then
1915 Parent_Disc := First_Discriminant (Parent_Typ);
1917 -- We either get the association from the subtype indication
1918 -- of the type definition itself, or from the discriminant
1919 -- constraint associated with the type entity (which is
1920 -- preferable, but it's not always present ???)
1922 if Is_Empty_Elmt_List (
1923 Discriminant_Constraint (Current_Typ))
1924 then
1925 Assoc := Get_Constraint_Association (Current_Typ);
1926 Assoc_Elmt := No_Elmt;
1927 else
1928 Assoc_Elmt :=
1929 First_Elmt (Discriminant_Constraint (Current_Typ));
1930 Assoc := Node (Assoc_Elmt);
1931 end if;
1933 -- Traverse the discriminants of the parent type looking
1934 -- for one that corresponds.
1936 while Present (Parent_Disc) and then Present (Assoc) loop
1937 Corresp_Disc := Parent_Disc;
1938 while Present (Corresp_Disc)
1939 and then Disc /= Corresp_Disc
1940 loop
1941 Corresp_Disc :=
1942 Corresponding_Discriminant (Corresp_Disc);
1943 end loop;
1945 if Disc = Corresp_Disc then
1946 if Nkind (Assoc) = N_Discriminant_Association then
1947 Assoc := Expression (Assoc);
1948 end if;
1950 -- If the located association directly denotes
1951 -- a discriminant, then use the value of a saved
1952 -- association of the aggregate. This is an approach
1953 -- used to handle certain cases involving multiple
1954 -- discriminants mapped to a single discriminant of
1955 -- a descendant. It's not clear how to locate the
1956 -- appropriate discriminant value for such cases. ???
1958 if Is_Entity_Name (Assoc)
1959 and then Ekind (Entity (Assoc)) = E_Discriminant
1960 then
1961 Assoc := Save_Assoc;
1962 end if;
1964 return Duplicate_Subexpr (Assoc);
1965 end if;
1967 Next_Discriminant (Parent_Disc);
1969 if No (Assoc_Elmt) then
1970 Next (Assoc);
1971 else
1972 Next_Elmt (Assoc_Elmt);
1973 if Present (Assoc_Elmt) then
1974 Assoc := Node (Assoc_Elmt);
1975 else
1976 Assoc := Empty;
1977 end if;
1978 end if;
1979 end loop;
1980 end if;
1982 Current_Typ := Parent_Typ;
1983 Parent_Typ := Etype (Current_Typ);
1984 end loop;
1986 -- In some cases there's no ancestor value to locate (such as
1987 -- when an ancestor part given by an expression defines the
1988 -- discriminant value).
1990 return Empty;
1991 end Ancestor_Discriminant_Value;
1993 ----------------------------------
1994 -- Check_Ancestor_Discriminants --
1995 ----------------------------------
1997 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
1998 Discr : Entity_Id;
1999 Disc_Value : Node_Id;
2000 Cond : Node_Id;
2002 begin
2003 Discr := First_Discriminant (Base_Type (Anc_Typ));
2004 while Present (Discr) loop
2005 Disc_Value := Ancestor_Discriminant_Value (Discr);
2007 if Present (Disc_Value) then
2008 Cond := Make_Op_Ne (Loc,
2009 Left_Opnd =>
2010 Make_Selected_Component (Loc,
2011 Prefix => New_Copy_Tree (Target),
2012 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2013 Right_Opnd => Disc_Value);
2015 Append_To (L,
2016 Make_Raise_Constraint_Error (Loc,
2017 Condition => Cond,
2018 Reason => CE_Discriminant_Check_Failed));
2019 end if;
2021 Next_Discriminant (Discr);
2022 end loop;
2023 end Check_Ancestor_Discriminants;
2025 ---------------------------
2026 -- Compatible_Int_Bounds --
2027 ---------------------------
2029 function Compatible_Int_Bounds
2030 (Agg_Bounds : Node_Id;
2031 Typ_Bounds : Node_Id) return Boolean
2033 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2034 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2035 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2036 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2037 begin
2038 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2039 end Compatible_Int_Bounds;
2041 --------------------------------
2042 -- Get_Constraint_Association --
2043 --------------------------------
2045 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2046 Indic : Node_Id;
2047 Typ : Entity_Id;
2049 begin
2050 Typ := T;
2052 -- Handle private types in instances
2054 if In_Instance
2055 and then Is_Private_Type (Typ)
2056 and then Present (Full_View (Typ))
2057 then
2058 Typ := Full_View (Typ);
2059 end if;
2061 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2063 -- ??? Also need to cover case of a type mark denoting a subtype
2064 -- with constraint.
2066 if Nkind (Indic) = N_Subtype_Indication
2067 and then Present (Constraint (Indic))
2068 then
2069 return First (Constraints (Constraint (Indic)));
2070 end if;
2072 return Empty;
2073 end Get_Constraint_Association;
2075 -------------------------------------
2076 -- Get_Explicit_Discriminant_Value --
2077 -------------------------------------
2079 function Get_Explicit_Discriminant_Value
2080 (D : Entity_Id) return Node_Id
2082 Assoc : Node_Id;
2083 Choice : Node_Id;
2084 Val : Node_Id;
2086 begin
2087 -- The aggregate has been normalized and all associations have a
2088 -- single choice.
2090 Assoc := First (Component_Associations (N));
2091 while Present (Assoc) loop
2092 Choice := First (Choices (Assoc));
2094 if Chars (Choice) = Chars (D) then
2095 Val := Expression (Assoc);
2096 Remove (Assoc);
2097 return Val;
2098 end if;
2100 Next (Assoc);
2101 end loop;
2103 return Empty;
2104 end Get_Explicit_Discriminant_Value;
2106 -------------------------------
2107 -- Init_Hidden_Discriminants --
2108 -------------------------------
2110 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2111 Btype : Entity_Id;
2112 Parent_Type : Entity_Id;
2113 Disc : Entity_Id;
2114 Discr_Val : Elmt_Id;
2115 In_Aggr_Type : Boolean;
2117 begin
2118 -- The constraints on the hidden discriminants, if present, are kept
2119 -- in the Stored_Constraint list of the type itself, or in that of
2120 -- the base type. If not in the constraints of the aggregate itself,
2121 -- we examine ancestors to find discriminants that are not renamed
2122 -- by other discriminants but constrained explicitly.
2124 In_Aggr_Type := True;
2126 Btype := Base_Type (Typ);
2127 while Is_Derived_Type (Btype)
2128 and then
2129 (Present (Stored_Constraint (Btype))
2130 or else
2131 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2132 loop
2133 Parent_Type := Etype (Btype);
2135 if not Has_Discriminants (Parent_Type) then
2136 return;
2137 end if;
2139 Disc := First_Discriminant (Parent_Type);
2141 -- We know that one of the stored-constraint lists is present
2143 if Present (Stored_Constraint (Btype)) then
2144 Discr_Val := First_Elmt (Stored_Constraint (Btype));
2146 -- For private extension, stored constraint may be on full view
2148 elsif Is_Private_Type (Btype)
2149 and then Present (Full_View (Btype))
2150 and then Present (Stored_Constraint (Full_View (Btype)))
2151 then
2152 Discr_Val := First_Elmt (Stored_Constraint (Full_View (Btype)));
2154 else
2155 Discr_Val := First_Elmt (Stored_Constraint (Typ));
2156 end if;
2158 while Present (Discr_Val) and then Present (Disc) loop
2160 -- Only those discriminants of the parent that are not
2161 -- renamed by discriminants of the derived type need to
2162 -- be added explicitly.
2164 if not Is_Entity_Name (Node (Discr_Val))
2165 or else Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2166 then
2167 Comp_Expr :=
2168 Make_Selected_Component (Loc,
2169 Prefix => New_Copy_Tree (Target),
2170 Selector_Name => New_Occurrence_Of (Disc, Loc));
2172 Instr :=
2173 Make_OK_Assignment_Statement (Loc,
2174 Name => Comp_Expr,
2175 Expression => New_Copy_Tree (Node (Discr_Val)));
2177 Set_No_Ctrl_Actions (Instr);
2178 Append_To (List, Instr);
2179 end if;
2181 Next_Discriminant (Disc);
2182 Next_Elmt (Discr_Val);
2183 end loop;
2185 In_Aggr_Type := False;
2186 Btype := Base_Type (Parent_Type);
2187 end loop;
2188 end Init_Hidden_Discriminants;
2190 -------------------------
2191 -- Is_Int_Range_Bounds --
2192 -------------------------
2194 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2195 begin
2196 return Nkind (Bounds) = N_Range
2197 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2198 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2199 end Is_Int_Range_Bounds;
2201 -----------------------------------
2202 -- Generate_Finalization_Actions --
2203 -----------------------------------
2205 procedure Generate_Finalization_Actions is
2206 begin
2207 -- Do the work only the first time this is called
2209 if Finalization_Done then
2210 return;
2211 end if;
2213 Finalization_Done := True;
2215 -- Determine the external finalization list. It is either the
2216 -- finalization list of the outer-scope or the one coming from an
2217 -- outer aggregate. When the target is not a temporary, the proper
2218 -- scope is the scope of the target rather than the potentially
2219 -- transient current scope.
2221 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2222 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2223 Set_Assignment_OK (Ref);
2225 Append_To (L,
2226 Make_Procedure_Call_Statement (Loc,
2227 Name =>
2228 New_Occurrence_Of
2229 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2230 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2231 end if;
2232 end Generate_Finalization_Actions;
2234 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2235 -- If default expression of a component mentions a discriminant of the
2236 -- type, it must be rewritten as the discriminant of the target object.
2238 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2239 -- If the aggregate contains a self-reference, traverse each expression
2240 -- to replace a possible self-reference with a reference to the proper
2241 -- component of the target of the assignment.
2243 --------------------------
2244 -- Rewrite_Discriminant --
2245 --------------------------
2247 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2248 begin
2249 if Is_Entity_Name (Expr)
2250 and then Present (Entity (Expr))
2251 and then Ekind (Entity (Expr)) = E_In_Parameter
2252 and then Present (Discriminal_Link (Entity (Expr)))
2253 and then Scope (Discriminal_Link (Entity (Expr))) =
2254 Base_Type (Etype (N))
2255 then
2256 Rewrite (Expr,
2257 Make_Selected_Component (Loc,
2258 Prefix => New_Copy_Tree (Lhs),
2259 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2260 end if;
2262 return OK;
2263 end Rewrite_Discriminant;
2265 ------------------
2266 -- Replace_Type --
2267 ------------------
2269 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2270 begin
2271 -- Note regarding the Root_Type test below: Aggregate components for
2272 -- self-referential types include attribute references to the current
2273 -- instance, of the form: Typ'access, etc.. These references are
2274 -- rewritten as references to the target of the aggregate: the
2275 -- left-hand side of an assignment, the entity in a declaration,
2276 -- or a temporary. Without this test, we would improperly extended
2277 -- this rewriting to attribute references whose prefix was not the
2278 -- type of the aggregate.
2280 if Nkind (Expr) = N_Attribute_Reference
2281 and then Is_Entity_Name (Prefix (Expr))
2282 and then Is_Type (Entity (Prefix (Expr)))
2283 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2284 then
2285 if Is_Entity_Name (Lhs) then
2286 Rewrite (Prefix (Expr),
2287 New_Occurrence_Of (Entity (Lhs), Loc));
2289 elsif Nkind (Lhs) = N_Selected_Component then
2290 Rewrite (Expr,
2291 Make_Attribute_Reference (Loc,
2292 Attribute_Name => Name_Unrestricted_Access,
2293 Prefix => New_Copy_Tree (Lhs)));
2294 Set_Analyzed (Parent (Expr), False);
2296 else
2297 Rewrite (Expr,
2298 Make_Attribute_Reference (Loc,
2299 Attribute_Name => Name_Unrestricted_Access,
2300 Prefix => New_Copy_Tree (Lhs)));
2301 Set_Analyzed (Parent (Expr), False);
2302 end if;
2303 end if;
2305 return OK;
2306 end Replace_Type;
2308 procedure Replace_Self_Reference is
2309 new Traverse_Proc (Replace_Type);
2311 procedure Replace_Discriminants is
2312 new Traverse_Proc (Rewrite_Discriminant);
2314 -- Start of processing for Build_Record_Aggr_Code
2316 begin
2317 if Has_Self_Reference (N) then
2318 Replace_Self_Reference (N);
2319 end if;
2321 -- If the target of the aggregate is class-wide, we must convert it
2322 -- to the actual type of the aggregate, so that the proper components
2323 -- are visible. We know already that the types are compatible.
2325 if Present (Etype (Lhs))
2326 and then Is_Class_Wide_Type (Etype (Lhs))
2327 then
2328 Target := Unchecked_Convert_To (Typ, Lhs);
2329 else
2330 Target := Lhs;
2331 end if;
2333 -- Deal with the ancestor part of extension aggregates or with the
2334 -- discriminants of the root type.
2336 if Nkind (N) = N_Extension_Aggregate then
2337 declare
2338 Ancestor : constant Node_Id := Ancestor_Part (N);
2339 Assign : List_Id;
2341 begin
2342 -- If the ancestor part is a subtype mark "T", we generate
2344 -- init-proc (T (tmp)); if T is constrained and
2345 -- init-proc (S (tmp)); where S applies an appropriate
2346 -- constraint if T is unconstrained
2348 if Is_Entity_Name (Ancestor)
2349 and then Is_Type (Entity (Ancestor))
2350 then
2351 Ancestor_Is_Subtype_Mark := True;
2353 if Is_Constrained (Entity (Ancestor)) then
2354 Init_Typ := Entity (Ancestor);
2356 -- For an ancestor part given by an unconstrained type mark,
2357 -- create a subtype constrained by appropriate corresponding
2358 -- discriminant values coming from either associations of the
2359 -- aggregate or a constraint on a parent type. The subtype will
2360 -- be used to generate the correct default value for the
2361 -- ancestor part.
2363 elsif Has_Discriminants (Entity (Ancestor)) then
2364 declare
2365 Anc_Typ : constant Entity_Id := Entity (Ancestor);
2366 Anc_Constr : constant List_Id := New_List;
2367 Discrim : Entity_Id;
2368 Disc_Value : Node_Id;
2369 New_Indic : Node_Id;
2370 Subt_Decl : Node_Id;
2372 begin
2373 Discrim := First_Discriminant (Anc_Typ);
2374 while Present (Discrim) loop
2375 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2377 -- If no usable discriminant in ancestors, check
2378 -- whether aggregate has an explicit value for it.
2380 if No (Disc_Value) then
2381 Disc_Value :=
2382 Get_Explicit_Discriminant_Value (Discrim);
2383 end if;
2385 Append_To (Anc_Constr, Disc_Value);
2386 Next_Discriminant (Discrim);
2387 end loop;
2389 New_Indic :=
2390 Make_Subtype_Indication (Loc,
2391 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2392 Constraint =>
2393 Make_Index_Or_Discriminant_Constraint (Loc,
2394 Constraints => Anc_Constr));
2396 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2398 Subt_Decl :=
2399 Make_Subtype_Declaration (Loc,
2400 Defining_Identifier => Init_Typ,
2401 Subtype_Indication => New_Indic);
2403 -- Itypes must be analyzed with checks off Declaration
2404 -- must have a parent for proper handling of subsidiary
2405 -- actions.
2407 Set_Parent (Subt_Decl, N);
2408 Analyze (Subt_Decl, Suppress => All_Checks);
2409 end;
2410 end if;
2412 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2413 Set_Assignment_OK (Ref);
2415 if not Is_Interface (Init_Typ) then
2416 Append_List_To (L,
2417 Build_Initialization_Call (Loc,
2418 Id_Ref => Ref,
2419 Typ => Init_Typ,
2420 In_Init_Proc => Within_Init_Proc,
2421 With_Default_Init => Has_Default_Init_Comps (N)
2422 or else
2423 Has_Task (Base_Type (Init_Typ))));
2425 if Is_Constrained (Entity (Ancestor))
2426 and then Has_Discriminants (Entity (Ancestor))
2427 then
2428 Check_Ancestor_Discriminants (Entity (Ancestor));
2429 end if;
2430 end if;
2432 -- Handle calls to C++ constructors
2434 elsif Is_CPP_Constructor_Call (Ancestor) then
2435 Init_Typ := Etype (Ancestor);
2436 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2437 Set_Assignment_OK (Ref);
2439 Append_List_To (L,
2440 Build_Initialization_Call (Loc,
2441 Id_Ref => Ref,
2442 Typ => Init_Typ,
2443 In_Init_Proc => Within_Init_Proc,
2444 With_Default_Init => Has_Default_Init_Comps (N),
2445 Constructor_Ref => Ancestor));
2447 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2448 -- limited type, a recursive call expands the ancestor. Note that
2449 -- in the limited case, the ancestor part must be either a
2450 -- function call (possibly qualified, or wrapped in an unchecked
2451 -- conversion) or aggregate (definitely qualified).
2453 -- The ancestor part can also be a function call (that may be
2454 -- transformed into an explicit dereference) or a qualification
2455 -- of one such.
2457 elsif Is_Limited_Type (Etype (Ancestor))
2458 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
2459 N_Extension_Aggregate)
2460 then
2461 Ancestor_Is_Expression := True;
2463 -- Set up finalization data for enclosing record, because
2464 -- controlled subcomponents of the ancestor part will be
2465 -- attached to it.
2467 Generate_Finalization_Actions;
2469 Append_List_To (L,
2470 Build_Record_Aggr_Code
2471 (N => Unqualify (Ancestor),
2472 Typ => Etype (Unqualify (Ancestor)),
2473 Lhs => Target));
2475 -- If the ancestor part is an expression "E", we generate
2477 -- T (tmp) := E;
2479 -- In Ada 2005, this includes the case of a (possibly qualified)
2480 -- limited function call. The assignment will turn into a
2481 -- build-in-place function call (for further details, see
2482 -- Make_Build_In_Place_Call_In_Assignment).
2484 else
2485 Ancestor_Is_Expression := True;
2486 Init_Typ := Etype (Ancestor);
2488 -- If the ancestor part is an aggregate, force its full
2489 -- expansion, which was delayed.
2491 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
2492 N_Extension_Aggregate)
2493 then
2494 Set_Analyzed (Ancestor, False);
2495 Set_Analyzed (Expression (Ancestor), False);
2496 end if;
2498 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2499 Set_Assignment_OK (Ref);
2501 -- Make the assignment without usual controlled actions, since
2502 -- we only want to Adjust afterwards, but not to Finalize
2503 -- beforehand. Add manual Adjust when necessary.
2505 Assign := New_List (
2506 Make_OK_Assignment_Statement (Loc,
2507 Name => Ref,
2508 Expression => Ancestor));
2509 Set_No_Ctrl_Actions (First (Assign));
2511 -- Assign the tag now to make sure that the dispatching call in
2512 -- the subsequent deep_adjust works properly (unless VM_Target,
2513 -- where tags are implicit).
2515 if Tagged_Type_Expansion then
2516 Instr :=
2517 Make_OK_Assignment_Statement (Loc,
2518 Name =>
2519 Make_Selected_Component (Loc,
2520 Prefix => New_Copy_Tree (Target),
2521 Selector_Name =>
2522 New_Occurrence_Of
2523 (First_Tag_Component (Base_Type (Typ)), Loc)),
2525 Expression =>
2526 Unchecked_Convert_To (RTE (RE_Tag),
2527 New_Occurrence_Of
2528 (Node (First_Elmt
2529 (Access_Disp_Table (Base_Type (Typ)))),
2530 Loc)));
2532 Set_Assignment_OK (Name (Instr));
2533 Append_To (Assign, Instr);
2535 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2536 -- also initialize tags of the secondary dispatch tables.
2538 if Has_Interfaces (Base_Type (Typ)) then
2539 Init_Secondary_Tags
2540 (Typ => Base_Type (Typ),
2541 Target => Target,
2542 Stmts_List => Assign);
2543 end if;
2544 end if;
2546 -- Call Adjust manually
2548 if Needs_Finalization (Etype (Ancestor))
2549 and then not Is_Limited_Type (Etype (Ancestor))
2550 then
2551 Append_To (Assign,
2552 Make_Adjust_Call
2553 (Obj_Ref => New_Copy_Tree (Ref),
2554 Typ => Etype (Ancestor)));
2555 end if;
2557 Append_To (L,
2558 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2560 if Has_Discriminants (Init_Typ) then
2561 Check_Ancestor_Discriminants (Init_Typ);
2562 end if;
2563 end if;
2564 end;
2566 -- Generate assignments of hidden discriminants. If the base type is
2567 -- an unchecked union, the discriminants are unknown to the back-end
2568 -- and absent from a value of the type, so assignments for them are
2569 -- not emitted.
2571 if Has_Discriminants (Typ)
2572 and then not Is_Unchecked_Union (Base_Type (Typ))
2573 then
2574 Init_Hidden_Discriminants (Typ, L);
2575 end if;
2577 -- Normal case (not an extension aggregate)
2579 else
2580 -- Generate the discriminant expressions, component by component.
2581 -- If the base type is an unchecked union, the discriminants are
2582 -- unknown to the back-end and absent from a value of the type, so
2583 -- assignments for them are not emitted.
2585 if Has_Discriminants (Typ)
2586 and then not Is_Unchecked_Union (Base_Type (Typ))
2587 then
2588 Init_Hidden_Discriminants (Typ, L);
2590 -- Generate discriminant init values for the visible discriminants
2592 declare
2593 Discriminant : Entity_Id;
2594 Discriminant_Value : Node_Id;
2596 begin
2597 Discriminant := First_Stored_Discriminant (Typ);
2598 while Present (Discriminant) loop
2599 Comp_Expr :=
2600 Make_Selected_Component (Loc,
2601 Prefix => New_Copy_Tree (Target),
2602 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2604 Discriminant_Value :=
2605 Get_Discriminant_Value (
2606 Discriminant,
2607 N_Typ,
2608 Discriminant_Constraint (N_Typ));
2610 Instr :=
2611 Make_OK_Assignment_Statement (Loc,
2612 Name => Comp_Expr,
2613 Expression => New_Copy_Tree (Discriminant_Value));
2615 Set_No_Ctrl_Actions (Instr);
2616 Append_To (L, Instr);
2618 Next_Stored_Discriminant (Discriminant);
2619 end loop;
2620 end;
2621 end if;
2622 end if;
2624 -- For CPP types we generate an implicit call to the C++ default
2625 -- constructor to ensure the proper initialization of the _Tag
2626 -- component.
2628 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
2629 Invoke_Constructor : declare
2630 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
2632 procedure Invoke_IC_Proc (T : Entity_Id);
2633 -- Recursive routine used to climb to parents. Required because
2634 -- parents must be initialized before descendants to ensure
2635 -- propagation of inherited C++ slots.
2637 --------------------
2638 -- Invoke_IC_Proc --
2639 --------------------
2641 procedure Invoke_IC_Proc (T : Entity_Id) is
2642 begin
2643 -- Avoid generating extra calls. Initialization required
2644 -- only for types defined from the level of derivation of
2645 -- type of the constructor and the type of the aggregate.
2647 if T = CPP_Parent then
2648 return;
2649 end if;
2651 Invoke_IC_Proc (Etype (T));
2653 -- Generate call to the IC routine
2655 if Present (CPP_Init_Proc (T)) then
2656 Append_To (L,
2657 Make_Procedure_Call_Statement (Loc,
2658 New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
2659 end if;
2660 end Invoke_IC_Proc;
2662 -- Start of processing for Invoke_Constructor
2664 begin
2665 -- Implicit invocation of the C++ constructor
2667 if Nkind (N) = N_Aggregate then
2668 Append_To (L,
2669 Make_Procedure_Call_Statement (Loc,
2670 Name =>
2671 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
2672 Parameter_Associations => New_List (
2673 Unchecked_Convert_To (CPP_Parent,
2674 New_Copy_Tree (Lhs)))));
2675 end if;
2677 Invoke_IC_Proc (Typ);
2678 end Invoke_Constructor;
2679 end if;
2681 -- Generate the assignments, component by component
2683 -- tmp.comp1 := Expr1_From_Aggr;
2684 -- tmp.comp2 := Expr2_From_Aggr;
2685 -- ....
2687 Comp := First (Component_Associations (N));
2688 while Present (Comp) loop
2689 Selector := Entity (First (Choices (Comp)));
2691 -- C++ constructors
2693 if Is_CPP_Constructor_Call (Expression (Comp)) then
2694 Append_List_To (L,
2695 Build_Initialization_Call (Loc,
2696 Id_Ref =>
2697 Make_Selected_Component (Loc,
2698 Prefix => New_Copy_Tree (Target),
2699 Selector_Name => New_Occurrence_Of (Selector, Loc)),
2700 Typ => Etype (Selector),
2701 Enclos_Type => Typ,
2702 With_Default_Init => True,
2703 Constructor_Ref => Expression (Comp)));
2705 -- Ada 2005 (AI-287): For each default-initialized component generate
2706 -- a call to the corresponding IP subprogram if available.
2708 elsif Box_Present (Comp)
2709 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2710 then
2711 if Ekind (Selector) /= E_Discriminant then
2712 Generate_Finalization_Actions;
2713 end if;
2715 -- Ada 2005 (AI-287): If the component type has tasks then
2716 -- generate the activation chain and master entities (except
2717 -- in case of an allocator because in that case these entities
2718 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2720 declare
2721 Ctype : constant Entity_Id := Etype (Selector);
2722 Inside_Allocator : Boolean := False;
2723 P : Node_Id := Parent (N);
2725 begin
2726 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2727 while Present (P) loop
2728 if Nkind (P) = N_Allocator then
2729 Inside_Allocator := True;
2730 exit;
2731 end if;
2733 P := Parent (P);
2734 end loop;
2736 if not Inside_Init_Proc and not Inside_Allocator then
2737 Build_Activation_Chain_Entity (N);
2738 end if;
2739 end if;
2740 end;
2742 Append_List_To (L,
2743 Build_Initialization_Call (Loc,
2744 Id_Ref => Make_Selected_Component (Loc,
2745 Prefix => New_Copy_Tree (Target),
2746 Selector_Name =>
2747 New_Occurrence_Of (Selector, Loc)),
2748 Typ => Etype (Selector),
2749 Enclos_Type => Typ,
2750 With_Default_Init => True));
2752 -- Prepare for component assignment
2754 elsif Ekind (Selector) /= E_Discriminant
2755 or else Nkind (N) = N_Extension_Aggregate
2756 then
2757 -- All the discriminants have now been assigned
2759 -- This is now a good moment to initialize and attach all the
2760 -- controllers. Their position may depend on the discriminants.
2762 if Ekind (Selector) /= E_Discriminant then
2763 Generate_Finalization_Actions;
2764 end if;
2766 Comp_Type := Underlying_Type (Etype (Selector));
2767 Comp_Expr :=
2768 Make_Selected_Component (Loc,
2769 Prefix => New_Copy_Tree (Target),
2770 Selector_Name => New_Occurrence_Of (Selector, Loc));
2772 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2773 Expr_Q := Expression (Expression (Comp));
2774 else
2775 Expr_Q := Expression (Comp);
2776 end if;
2778 -- Now either create the assignment or generate the code for the
2779 -- inner aggregate top-down.
2781 if Is_Delayed_Aggregate (Expr_Q) then
2783 -- We have the following case of aggregate nesting inside
2784 -- an object declaration:
2786 -- type Arr_Typ is array (Integer range <>) of ...;
2788 -- type Rec_Typ (...) is record
2789 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2790 -- end record;
2792 -- Obj_Rec_Typ : Rec_Typ := (...,
2793 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2795 -- The length of the ranges of the aggregate and Obj_Add_Typ
2796 -- are equal (B - A = Y - X), but they do not coincide (X /=
2797 -- A and B /= Y). This case requires array sliding which is
2798 -- performed in the following manner:
2800 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2801 -- Temp : Arr_Sub;
2802 -- Temp (X) := (...);
2803 -- ...
2804 -- Temp (Y) := (...);
2805 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2807 if Ekind (Comp_Type) = E_Array_Subtype
2808 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2809 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2810 and then not
2811 Compatible_Int_Bounds
2812 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2813 Typ_Bounds => First_Index (Comp_Type))
2814 then
2815 -- Create the array subtype with bounds equal to those of
2816 -- the corresponding aggregate.
2818 declare
2819 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
2821 SubD : constant Node_Id :=
2822 Make_Subtype_Declaration (Loc,
2823 Defining_Identifier => SubE,
2824 Subtype_Indication =>
2825 Make_Subtype_Indication (Loc,
2826 Subtype_Mark =>
2827 New_Occurrence_Of (Etype (Comp_Type), Loc),
2828 Constraint =>
2829 Make_Index_Or_Discriminant_Constraint
2830 (Loc,
2831 Constraints => New_List (
2832 New_Copy_Tree
2833 (Aggregate_Bounds (Expr_Q))))));
2835 -- Create a temporary array of the above subtype which
2836 -- will be used to capture the aggregate assignments.
2838 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
2840 TmpD : constant Node_Id :=
2841 Make_Object_Declaration (Loc,
2842 Defining_Identifier => TmpE,
2843 Object_Definition => New_Occurrence_Of (SubE, Loc));
2845 begin
2846 Set_No_Initialization (TmpD);
2847 Append_To (L, SubD);
2848 Append_To (L, TmpD);
2850 -- Expand aggregate into assignments to the temp array
2852 Append_List_To (L,
2853 Late_Expansion (Expr_Q, Comp_Type,
2854 New_Occurrence_Of (TmpE, Loc)));
2856 -- Slide
2858 Append_To (L,
2859 Make_Assignment_Statement (Loc,
2860 Name => New_Copy_Tree (Comp_Expr),
2861 Expression => New_Occurrence_Of (TmpE, Loc)));
2862 end;
2864 -- Normal case (sliding not required)
2866 else
2867 Append_List_To (L,
2868 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
2869 end if;
2871 -- Expr_Q is not delayed aggregate
2873 else
2874 if Has_Discriminants (Typ) then
2875 Replace_Discriminants (Expr_Q);
2877 -- If the component is an array type that depends on
2878 -- discriminants, and the expression is a single Others
2879 -- clause, create an explicit subtype for it because the
2880 -- backend has troubles recovering the actual bounds.
2882 if Nkind (Expr_Q) = N_Aggregate
2883 and then Is_Array_Type (Comp_Type)
2884 and then Present (Component_Associations (Expr_Q))
2885 then
2886 declare
2887 Assoc : constant Node_Id :=
2888 First (Component_Associations (Expr_Q));
2889 Decl : Node_Id;
2891 begin
2892 if Nkind (First (Choices (Assoc))) = N_Others_Choice
2893 then
2894 Decl :=
2895 Build_Actual_Subtype_Of_Component
2896 (Comp_Type, Comp_Expr);
2898 -- If the component type does not in fact depend on
2899 -- discriminants, the subtype declaration is empty.
2901 if Present (Decl) then
2902 Append_To (L, Decl);
2903 Set_Etype (Comp_Expr, Defining_Entity (Decl));
2904 end if;
2905 end if;
2906 end;
2907 end if;
2908 end if;
2910 Instr :=
2911 Make_OK_Assignment_Statement (Loc,
2912 Name => Comp_Expr,
2913 Expression => Expr_Q);
2915 Set_No_Ctrl_Actions (Instr);
2916 Append_To (L, Instr);
2918 -- Adjust the tag if tagged (because of possible view
2919 -- conversions), unless compiling for a VM where tags are
2920 -- implicit.
2922 -- tmp.comp._tag := comp_typ'tag;
2924 if Is_Tagged_Type (Comp_Type)
2925 and then Tagged_Type_Expansion
2926 then
2927 Instr :=
2928 Make_OK_Assignment_Statement (Loc,
2929 Name =>
2930 Make_Selected_Component (Loc,
2931 Prefix => New_Copy_Tree (Comp_Expr),
2932 Selector_Name =>
2933 New_Occurrence_Of
2934 (First_Tag_Component (Comp_Type), Loc)),
2936 Expression =>
2937 Unchecked_Convert_To (RTE (RE_Tag),
2938 New_Occurrence_Of
2939 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
2940 Loc)));
2942 Append_To (L, Instr);
2943 end if;
2945 -- Generate:
2946 -- Adjust (tmp.comp);
2948 if Needs_Finalization (Comp_Type)
2949 and then not Is_Limited_Type (Comp_Type)
2950 then
2951 Append_To (L,
2952 Make_Adjust_Call
2953 (Obj_Ref => New_Copy_Tree (Comp_Expr),
2954 Typ => Comp_Type));
2955 end if;
2956 end if;
2958 -- comment would be good here ???
2960 elsif Ekind (Selector) = E_Discriminant
2961 and then Nkind (N) /= N_Extension_Aggregate
2962 and then Nkind (Parent (N)) = N_Component_Association
2963 and then Is_Constrained (Typ)
2964 then
2965 -- We must check that the discriminant value imposed by the
2966 -- context is the same as the value given in the subaggregate,
2967 -- because after the expansion into assignments there is no
2968 -- record on which to perform a regular discriminant check.
2970 declare
2971 D_Val : Elmt_Id;
2972 Disc : Entity_Id;
2974 begin
2975 D_Val := First_Elmt (Discriminant_Constraint (Typ));
2976 Disc := First_Discriminant (Typ);
2977 while Chars (Disc) /= Chars (Selector) loop
2978 Next_Discriminant (Disc);
2979 Next_Elmt (D_Val);
2980 end loop;
2982 pragma Assert (Present (D_Val));
2984 -- This check cannot performed for components that are
2985 -- constrained by a current instance, because this is not a
2986 -- value that can be compared with the actual constraint.
2988 if Nkind (Node (D_Val)) /= N_Attribute_Reference
2989 or else not Is_Entity_Name (Prefix (Node (D_Val)))
2990 or else not Is_Type (Entity (Prefix (Node (D_Val))))
2991 then
2992 Append_To (L,
2993 Make_Raise_Constraint_Error (Loc,
2994 Condition =>
2995 Make_Op_Ne (Loc,
2996 Left_Opnd => New_Copy_Tree (Node (D_Val)),
2997 Right_Opnd => Expression (Comp)),
2998 Reason => CE_Discriminant_Check_Failed));
3000 else
3001 -- Find self-reference in previous discriminant assignment,
3002 -- and replace with proper expression.
3004 declare
3005 Ass : Node_Id;
3007 begin
3008 Ass := First (L);
3009 while Present (Ass) loop
3010 if Nkind (Ass) = N_Assignment_Statement
3011 and then Nkind (Name (Ass)) = N_Selected_Component
3012 and then Chars (Selector_Name (Name (Ass))) =
3013 Chars (Disc)
3014 then
3015 Set_Expression
3016 (Ass, New_Copy_Tree (Expression (Comp)));
3017 exit;
3018 end if;
3019 Next (Ass);
3020 end loop;
3021 end;
3022 end if;
3023 end;
3024 end if;
3026 Next (Comp);
3027 end loop;
3029 -- If the type is tagged, the tag needs to be initialized (unless we
3030 -- are in VM-mode where tags are implicit). It is done late in the
3031 -- initialization process because in some cases, we call the init
3032 -- proc of an ancestor which will not leave out the right tag.
3034 if Ancestor_Is_Expression then
3035 null;
3037 -- For CPP types we generated a call to the C++ default constructor
3038 -- before the components have been initialized to ensure the proper
3039 -- initialization of the _Tag component (see above).
3041 elsif Is_CPP_Class (Typ) then
3042 null;
3044 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3045 Instr :=
3046 Make_OK_Assignment_Statement (Loc,
3047 Name =>
3048 Make_Selected_Component (Loc,
3049 Prefix => New_Copy_Tree (Target),
3050 Selector_Name =>
3051 New_Occurrence_Of
3052 (First_Tag_Component (Base_Type (Typ)), Loc)),
3054 Expression =>
3055 Unchecked_Convert_To (RTE (RE_Tag),
3056 New_Occurrence_Of
3057 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3058 Loc)));
3060 Append_To (L, Instr);
3062 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3063 -- abstract interfaces we must also initialize the tags of the
3064 -- secondary dispatch tables.
3066 if Has_Interfaces (Base_Type (Typ)) then
3067 Init_Secondary_Tags
3068 (Typ => Base_Type (Typ),
3069 Target => Target,
3070 Stmts_List => L);
3071 end if;
3072 end if;
3074 -- If the controllers have not been initialized yet (by lack of non-
3075 -- discriminant components), let's do it now.
3077 Generate_Finalization_Actions;
3079 return L;
3080 end Build_Record_Aggr_Code;
3082 ---------------------------------------
3083 -- Collect_Initialization_Statements --
3084 ---------------------------------------
3086 procedure Collect_Initialization_Statements
3087 (Obj : Entity_Id;
3088 N : Node_Id;
3089 Node_After : Node_Id)
3091 Loc : constant Source_Ptr := Sloc (N);
3092 Init_Actions : constant List_Id := New_List;
3093 Init_Node : Node_Id;
3094 Comp_Stmt : Node_Id;
3096 begin
3097 -- Nothing to do if Obj is already frozen, as in this case we known we
3098 -- won't need to move the initialization statements about later on.
3100 if Is_Frozen (Obj) then
3101 return;
3102 end if;
3104 Init_Node := N;
3105 while Next (Init_Node) /= Node_After loop
3106 Append_To (Init_Actions, Remove_Next (Init_Node));
3107 end loop;
3109 if not Is_Empty_List (Init_Actions) then
3110 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3111 Insert_Action_After (Init_Node, Comp_Stmt);
3112 Set_Initialization_Statements (Obj, Comp_Stmt);
3113 end if;
3114 end Collect_Initialization_Statements;
3116 -------------------------------
3117 -- Convert_Aggr_In_Allocator --
3118 -------------------------------
3120 procedure Convert_Aggr_In_Allocator
3121 (Alloc : Node_Id;
3122 Decl : Node_Id;
3123 Aggr : Node_Id)
3125 Loc : constant Source_Ptr := Sloc (Aggr);
3126 Typ : constant Entity_Id := Etype (Aggr);
3127 Temp : constant Entity_Id := Defining_Identifier (Decl);
3129 Occ : constant Node_Id :=
3130 Unchecked_Convert_To (Typ,
3131 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3133 begin
3134 if Is_Array_Type (Typ) then
3135 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3137 elsif Has_Default_Init_Comps (Aggr) then
3138 declare
3139 L : constant List_Id := New_List;
3140 Init_Stmts : List_Id;
3142 begin
3143 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3145 if Has_Task (Typ) then
3146 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3147 Insert_Actions (Alloc, L);
3148 else
3149 Insert_Actions (Alloc, Init_Stmts);
3150 end if;
3151 end;
3153 else
3154 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3155 end if;
3156 end Convert_Aggr_In_Allocator;
3158 --------------------------------
3159 -- Convert_Aggr_In_Assignment --
3160 --------------------------------
3162 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3163 Aggr : Node_Id := Expression (N);
3164 Typ : constant Entity_Id := Etype (Aggr);
3165 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3167 begin
3168 if Nkind (Aggr) = N_Qualified_Expression then
3169 Aggr := Expression (Aggr);
3170 end if;
3172 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3173 end Convert_Aggr_In_Assignment;
3175 ---------------------------------
3176 -- Convert_Aggr_In_Object_Decl --
3177 ---------------------------------
3179 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3180 Obj : constant Entity_Id := Defining_Identifier (N);
3181 Aggr : Node_Id := Expression (N);
3182 Loc : constant Source_Ptr := Sloc (Aggr);
3183 Typ : constant Entity_Id := Etype (Aggr);
3184 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3186 function Discriminants_Ok return Boolean;
3187 -- If the object type is constrained, the discriminants in the
3188 -- aggregate must be checked against the discriminants of the subtype.
3189 -- This cannot be done using Apply_Discriminant_Checks because after
3190 -- expansion there is no aggregate left to check.
3192 ----------------------
3193 -- Discriminants_Ok --
3194 ----------------------
3196 function Discriminants_Ok return Boolean is
3197 Cond : Node_Id := Empty;
3198 Check : Node_Id;
3199 D : Entity_Id;
3200 Disc1 : Elmt_Id;
3201 Disc2 : Elmt_Id;
3202 Val1 : Node_Id;
3203 Val2 : Node_Id;
3205 begin
3206 D := First_Discriminant (Typ);
3207 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3208 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3209 while Present (Disc1) and then Present (Disc2) loop
3210 Val1 := Node (Disc1);
3211 Val2 := Node (Disc2);
3213 if not Is_OK_Static_Expression (Val1)
3214 or else not Is_OK_Static_Expression (Val2)
3215 then
3216 Check := Make_Op_Ne (Loc,
3217 Left_Opnd => Duplicate_Subexpr (Val1),
3218 Right_Opnd => Duplicate_Subexpr (Val2));
3220 if No (Cond) then
3221 Cond := Check;
3223 else
3224 Cond := Make_Or_Else (Loc,
3225 Left_Opnd => Cond,
3226 Right_Opnd => Check);
3227 end if;
3229 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3230 Apply_Compile_Time_Constraint_Error (Aggr,
3231 Msg => "incorrect value for discriminant&??",
3232 Reason => CE_Discriminant_Check_Failed,
3233 Ent => D);
3234 return False;
3235 end if;
3237 Next_Discriminant (D);
3238 Next_Elmt (Disc1);
3239 Next_Elmt (Disc2);
3240 end loop;
3242 -- If any discriminant constraint is non-static, emit a check
3244 if Present (Cond) then
3245 Insert_Action (N,
3246 Make_Raise_Constraint_Error (Loc,
3247 Condition => Cond,
3248 Reason => CE_Discriminant_Check_Failed));
3249 end if;
3251 return True;
3252 end Discriminants_Ok;
3254 -- Start of processing for Convert_Aggr_In_Object_Decl
3256 begin
3257 Set_Assignment_OK (Occ);
3259 if Nkind (Aggr) = N_Qualified_Expression then
3260 Aggr := Expression (Aggr);
3261 end if;
3263 if Has_Discriminants (Typ)
3264 and then Typ /= Etype (Obj)
3265 and then Is_Constrained (Etype (Obj))
3266 and then not Discriminants_Ok
3267 then
3268 return;
3269 end if;
3271 -- If the context is an extended return statement, it has its own
3272 -- finalization machinery (i.e. works like a transient scope) and
3273 -- we do not want to create an additional one, because objects on
3274 -- the finalization list of the return must be moved to the caller's
3275 -- finalization list to complete the return.
3277 -- However, if the aggregate is limited, it is built in place, and the
3278 -- controlled components are not assigned to intermediate temporaries
3279 -- so there is no need for a transient scope in this case either.
3281 if Requires_Transient_Scope (Typ)
3282 and then Ekind (Current_Scope) /= E_Return_Statement
3283 and then not Is_Limited_Type (Typ)
3284 then
3285 Establish_Transient_Scope
3286 (Aggr,
3287 Sec_Stack =>
3288 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3289 end if;
3291 declare
3292 Node_After : constant Node_Id := Next (N);
3293 begin
3294 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3295 Collect_Initialization_Statements (Obj, N, Node_After);
3296 end;
3297 Set_No_Initialization (N);
3298 Initialize_Discriminants (N, Typ);
3299 end Convert_Aggr_In_Object_Decl;
3301 -------------------------------------
3302 -- Convert_Array_Aggr_In_Allocator --
3303 -------------------------------------
3305 procedure Convert_Array_Aggr_In_Allocator
3306 (Decl : Node_Id;
3307 Aggr : Node_Id;
3308 Target : Node_Id)
3310 Aggr_Code : List_Id;
3311 Typ : constant Entity_Id := Etype (Aggr);
3312 Ctyp : constant Entity_Id := Component_Type (Typ);
3314 begin
3315 -- The target is an explicit dereference of the allocated object.
3316 -- Generate component assignments to it, as for an aggregate that
3317 -- appears on the right-hand side of an assignment statement.
3319 Aggr_Code :=
3320 Build_Array_Aggr_Code (Aggr,
3321 Ctype => Ctyp,
3322 Index => First_Index (Typ),
3323 Into => Target,
3324 Scalar_Comp => Is_Scalar_Type (Ctyp));
3326 Insert_Actions_After (Decl, Aggr_Code);
3327 end Convert_Array_Aggr_In_Allocator;
3329 ----------------------------
3330 -- Convert_To_Assignments --
3331 ----------------------------
3333 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3334 Loc : constant Source_Ptr := Sloc (N);
3335 T : Entity_Id;
3336 Temp : Entity_Id;
3338 Aggr_Code : List_Id;
3339 Instr : Node_Id;
3340 Target_Expr : Node_Id;
3341 Parent_Kind : Node_Kind;
3342 Unc_Decl : Boolean := False;
3343 Parent_Node : Node_Id;
3345 begin
3346 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3347 pragma Assert (Is_Record_Type (Typ));
3349 Parent_Node := Parent (N);
3350 Parent_Kind := Nkind (Parent_Node);
3352 if Parent_Kind = N_Qualified_Expression then
3354 -- Check if we are in a unconstrained declaration because in this
3355 -- case the current delayed expansion mechanism doesn't work when
3356 -- the declared object size depend on the initializing expr.
3358 begin
3359 Parent_Node := Parent (Parent_Node);
3360 Parent_Kind := Nkind (Parent_Node);
3362 if Parent_Kind = N_Object_Declaration then
3363 Unc_Decl :=
3364 not Is_Entity_Name (Object_Definition (Parent_Node))
3365 or else Has_Discriminants
3366 (Entity (Object_Definition (Parent_Node)))
3367 or else Is_Class_Wide_Type
3368 (Entity (Object_Definition (Parent_Node)));
3369 end if;
3370 end;
3371 end if;
3373 -- Just set the Delay flag in the cases where the transformation will be
3374 -- done top down from above.
3376 if False
3378 -- Internal aggregate (transformed when expanding the parent)
3380 or else Parent_Kind = N_Aggregate
3381 or else Parent_Kind = N_Extension_Aggregate
3382 or else Parent_Kind = N_Component_Association
3384 -- Allocator (see Convert_Aggr_In_Allocator)
3386 or else Parent_Kind = N_Allocator
3388 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3390 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3392 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3393 -- assignments in init procs are taken into account.
3395 or else (Parent_Kind = N_Assignment_Statement
3396 and then Inside_Init_Proc)
3398 -- (Ada 2005) An inherently limited type in a return statement, which
3399 -- will be handled in a build-in-place fashion, and may be rewritten
3400 -- as an extended return and have its own finalization machinery.
3401 -- In the case of a simple return, the aggregate needs to be delayed
3402 -- until the scope for the return statement has been created, so
3403 -- that any finalization chain will be associated with that scope.
3404 -- For extended returns, we delay expansion to avoid the creation
3405 -- of an unwanted transient scope that could result in premature
3406 -- finalization of the return object (which is built in place
3407 -- within the caller's scope).
3409 or else
3410 (Is_Limited_View (Typ)
3411 and then
3412 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3413 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3414 then
3415 Set_Expansion_Delayed (N);
3416 return;
3417 end if;
3419 -- Otherwise, if a transient scope is required, create it now. If we
3420 -- are within an initialization procedure do not create such, because
3421 -- the target of the assignment must not be declared within a local
3422 -- block, and because cleanup will take place on return from the
3423 -- initialization procedure.
3424 -- Should the condition be more restrictive ???
3426 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
3427 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
3428 end if;
3430 -- If the aggregate is non-limited, create a temporary. If it is limited
3431 -- and context is an assignment, this is a subaggregate for an enclosing
3432 -- aggregate being expanded. It must be built in place, so use target of
3433 -- the current assignment.
3435 if Is_Limited_Type (Typ)
3436 and then Nkind (Parent (N)) = N_Assignment_Statement
3437 then
3438 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3439 Insert_Actions (Parent (N),
3440 Build_Record_Aggr_Code (N, Typ, Target_Expr));
3441 Rewrite (Parent (N), Make_Null_Statement (Loc));
3443 else
3444 Temp := Make_Temporary (Loc, 'A', N);
3446 -- If the type inherits unknown discriminants, use the view with
3447 -- known discriminants if available.
3449 if Has_Unknown_Discriminants (Typ)
3450 and then Present (Underlying_Record_View (Typ))
3451 then
3452 T := Underlying_Record_View (Typ);
3453 else
3454 T := Typ;
3455 end if;
3457 Instr :=
3458 Make_Object_Declaration (Loc,
3459 Defining_Identifier => Temp,
3460 Object_Definition => New_Occurrence_Of (T, Loc));
3462 Set_No_Initialization (Instr);
3463 Insert_Action (N, Instr);
3464 Initialize_Discriminants (Instr, T);
3466 Target_Expr := New_Occurrence_Of (Temp, Loc);
3467 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
3469 -- Save the last assignment statement associated with the aggregate
3470 -- when building a controlled object. This reference is utilized by
3471 -- the finalization machinery when marking an object as successfully
3472 -- initialized.
3474 if Needs_Finalization (T) then
3475 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
3476 end if;
3478 Insert_Actions (N, Aggr_Code);
3479 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3480 Analyze_And_Resolve (N, T);
3481 end if;
3482 end Convert_To_Assignments;
3484 ---------------------------
3485 -- Convert_To_Positional --
3486 ---------------------------
3488 procedure Convert_To_Positional
3489 (N : Node_Id;
3490 Max_Others_Replicate : Nat := 5;
3491 Handle_Bit_Packed : Boolean := False)
3493 Typ : constant Entity_Id := Etype (N);
3495 Static_Components : Boolean := True;
3497 procedure Check_Static_Components;
3498 -- Check whether all components of the aggregate are compile-time known
3499 -- values, and can be passed as is to the back-end without further
3500 -- expansion.
3502 function Flatten
3503 (N : Node_Id;
3504 Ix : Node_Id;
3505 Ixb : Node_Id) return Boolean;
3506 -- Convert the aggregate into a purely positional form if possible. On
3507 -- entry the bounds of all dimensions are known to be static, and the
3508 -- total number of components is safe enough to expand.
3510 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3511 -- Return True iff the array N is flat (which is not trivial in the case
3512 -- of multidimensional aggregates).
3514 -----------------------------
3515 -- Check_Static_Components --
3516 -----------------------------
3518 -- Could use some comments in this body ???
3520 procedure Check_Static_Components is
3521 Expr : Node_Id;
3523 begin
3524 Static_Components := True;
3526 if Nkind (N) = N_String_Literal then
3527 null;
3529 elsif Present (Expressions (N)) then
3530 Expr := First (Expressions (N));
3531 while Present (Expr) loop
3532 if Nkind (Expr) /= N_Aggregate
3533 or else not Compile_Time_Known_Aggregate (Expr)
3534 or else Expansion_Delayed (Expr)
3535 then
3536 Static_Components := False;
3537 exit;
3538 end if;
3540 Next (Expr);
3541 end loop;
3542 end if;
3544 if Nkind (N) = N_Aggregate
3545 and then Present (Component_Associations (N))
3546 then
3547 Expr := First (Component_Associations (N));
3548 while Present (Expr) loop
3549 if Nkind_In (Expression (Expr), N_Integer_Literal,
3550 N_Real_Literal)
3551 then
3552 null;
3554 elsif Is_Entity_Name (Expression (Expr))
3555 and then Present (Entity (Expression (Expr)))
3556 and then Ekind (Entity (Expression (Expr))) =
3557 E_Enumeration_Literal
3558 then
3559 null;
3561 elsif Nkind (Expression (Expr)) /= N_Aggregate
3562 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3563 or else Expansion_Delayed (Expression (Expr))
3564 then
3565 Static_Components := False;
3566 exit;
3567 end if;
3569 Next (Expr);
3570 end loop;
3571 end if;
3572 end Check_Static_Components;
3574 -------------
3575 -- Flatten --
3576 -------------
3578 function Flatten
3579 (N : Node_Id;
3580 Ix : Node_Id;
3581 Ixb : Node_Id) return Boolean
3583 Loc : constant Source_Ptr := Sloc (N);
3584 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3585 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3586 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3587 Lov : Uint;
3588 Hiv : Uint;
3590 Others_Present : Boolean := False;
3592 begin
3593 if Nkind (Original_Node (N)) = N_String_Literal then
3594 return True;
3595 end if;
3597 if not Compile_Time_Known_Value (Lo)
3598 or else not Compile_Time_Known_Value (Hi)
3599 then
3600 return False;
3601 end if;
3603 Lov := Expr_Value (Lo);
3604 Hiv := Expr_Value (Hi);
3606 -- Check if there is an others choice
3608 if Present (Component_Associations (N)) then
3609 declare
3610 Assoc : Node_Id;
3611 Choice : Node_Id;
3613 begin
3614 Assoc := First (Component_Associations (N));
3615 while Present (Assoc) loop
3617 -- If this is a box association, flattening is in general
3618 -- not possible because at this point we cannot tell if the
3619 -- default is static or even exists.
3621 if Box_Present (Assoc) then
3622 return False;
3623 end if;
3625 Choice := First (Choices (Assoc));
3627 while Present (Choice) loop
3628 if Nkind (Choice) = N_Others_Choice then
3629 Others_Present := True;
3630 end if;
3632 Next (Choice);
3633 end loop;
3635 Next (Assoc);
3636 end loop;
3637 end;
3638 end if;
3640 -- If the low bound is not known at compile time and others is not
3641 -- present we can proceed since the bounds can be obtained from the
3642 -- aggregate.
3644 -- Note: This case is required in VM platforms since their backends
3645 -- normalize array indexes in the range 0 .. N-1. Hence, if we do
3646 -- not flat an array whose bounds cannot be obtained from the type
3647 -- of the index the backend has no way to properly generate the code.
3648 -- See ACATS c460010 for an example.
3650 if Hiv < Lov
3651 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
3652 then
3653 return False;
3654 end if;
3656 -- Determine if set of alternatives is suitable for conversion and
3657 -- build an array containing the values in sequence.
3659 declare
3660 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3661 of Node_Id := (others => Empty);
3662 -- The values in the aggregate sorted appropriately
3664 Vlist : List_Id;
3665 -- Same data as Vals in list form
3667 Rep_Count : Nat;
3668 -- Used to validate Max_Others_Replicate limit
3670 Elmt : Node_Id;
3671 Num : Int := UI_To_Int (Lov);
3672 Choice_Index : Int;
3673 Choice : Node_Id;
3674 Lo, Hi : Node_Id;
3676 begin
3677 if Present (Expressions (N)) then
3678 Elmt := First (Expressions (N));
3679 while Present (Elmt) loop
3680 if Nkind (Elmt) = N_Aggregate
3681 and then Present (Next_Index (Ix))
3682 and then
3683 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3684 then
3685 return False;
3686 end if;
3688 Vals (Num) := Relocate_Node (Elmt);
3689 Num := Num + 1;
3691 Next (Elmt);
3692 end loop;
3693 end if;
3695 if No (Component_Associations (N)) then
3696 return True;
3697 end if;
3699 Elmt := First (Component_Associations (N));
3701 if Nkind (Expression (Elmt)) = N_Aggregate then
3702 if Present (Next_Index (Ix))
3703 and then
3704 not Flatten
3705 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3706 then
3707 return False;
3708 end if;
3709 end if;
3711 Component_Loop : while Present (Elmt) loop
3712 Choice := First (Choices (Elmt));
3713 Choice_Loop : while Present (Choice) loop
3715 -- If we have an others choice, fill in the missing elements
3716 -- subject to the limit established by Max_Others_Replicate.
3718 if Nkind (Choice) = N_Others_Choice then
3719 Rep_Count := 0;
3721 for J in Vals'Range loop
3722 if No (Vals (J)) then
3723 Vals (J) := New_Copy_Tree (Expression (Elmt));
3724 Rep_Count := Rep_Count + 1;
3726 -- Check for maximum others replication. Note that
3727 -- we skip this test if either of the restrictions
3728 -- No_Elaboration_Code or No_Implicit_Loops is
3729 -- active, if this is a preelaborable unit or
3730 -- a predefined unit, or if the unit must be
3731 -- placed in data memory. This also ensures that
3732 -- predefined units get the same level of constant
3733 -- folding in Ada 95 and Ada 2005, where their
3734 -- categorization has changed.
3736 declare
3737 P : constant Entity_Id :=
3738 Cunit_Entity (Current_Sem_Unit);
3740 begin
3741 -- Check if duplication OK and if so continue
3742 -- processing.
3744 if Restriction_Active (No_Elaboration_Code)
3745 or else Restriction_Active (No_Implicit_Loops)
3746 or else
3747 (Ekind (Current_Scope) = E_Package
3748 and then Static_Elaboration_Desired
3749 (Current_Scope))
3750 or else Is_Preelaborated (P)
3751 or else (Ekind (P) = E_Package_Body
3752 and then
3753 Is_Preelaborated (Spec_Entity (P)))
3754 or else
3755 Is_Predefined_File_Name
3756 (Unit_File_Name (Get_Source_Unit (P)))
3757 then
3758 null;
3760 -- If duplication not OK, then we return False
3761 -- if the replication count is too high
3763 elsif Rep_Count > Max_Others_Replicate then
3764 return False;
3766 -- Continue on if duplication not OK, but the
3767 -- replication count is not excessive.
3769 else
3770 null;
3771 end if;
3772 end;
3773 end if;
3774 end loop;
3776 exit Component_Loop;
3778 -- Case of a subtype mark, identifier or expanded name
3780 elsif Is_Entity_Name (Choice)
3781 and then Is_Type (Entity (Choice))
3782 then
3783 Lo := Type_Low_Bound (Etype (Choice));
3784 Hi := Type_High_Bound (Etype (Choice));
3786 -- Case of subtype indication
3788 elsif Nkind (Choice) = N_Subtype_Indication then
3789 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3790 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3792 -- Case of a range
3794 elsif Nkind (Choice) = N_Range then
3795 Lo := Low_Bound (Choice);
3796 Hi := High_Bound (Choice);
3798 -- Normal subexpression case
3800 else pragma Assert (Nkind (Choice) in N_Subexpr);
3801 if not Compile_Time_Known_Value (Choice) then
3802 return False;
3804 else
3805 Choice_Index := UI_To_Int (Expr_Value (Choice));
3807 if Choice_Index in Vals'Range then
3808 Vals (Choice_Index) :=
3809 New_Copy_Tree (Expression (Elmt));
3810 goto Continue;
3812 -- Choice is statically out-of-range, will be
3813 -- rewritten to raise Constraint_Error.
3815 else
3816 return False;
3817 end if;
3818 end if;
3819 end if;
3821 -- Range cases merge with Lo,Hi set
3823 if not Compile_Time_Known_Value (Lo)
3824 or else
3825 not Compile_Time_Known_Value (Hi)
3826 then
3827 return False;
3829 else
3830 for J in UI_To_Int (Expr_Value (Lo)) ..
3831 UI_To_Int (Expr_Value (Hi))
3832 loop
3833 Vals (J) := New_Copy_Tree (Expression (Elmt));
3834 end loop;
3835 end if;
3837 <<Continue>>
3838 Next (Choice);
3839 end loop Choice_Loop;
3841 Next (Elmt);
3842 end loop Component_Loop;
3844 -- If we get here the conversion is possible
3846 Vlist := New_List;
3847 for J in Vals'Range loop
3848 Append (Vals (J), Vlist);
3849 end loop;
3851 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3852 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3853 return True;
3854 end;
3855 end Flatten;
3857 -------------
3858 -- Is_Flat --
3859 -------------
3861 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3862 Elmt : Node_Id;
3864 begin
3865 if Dims = 0 then
3866 return True;
3868 elsif Nkind (N) = N_Aggregate then
3869 if Present (Component_Associations (N)) then
3870 return False;
3872 else
3873 Elmt := First (Expressions (N));
3874 while Present (Elmt) loop
3875 if not Is_Flat (Elmt, Dims - 1) then
3876 return False;
3877 end if;
3879 Next (Elmt);
3880 end loop;
3882 return True;
3883 end if;
3884 else
3885 return True;
3886 end if;
3887 end Is_Flat;
3889 -- Start of processing for Convert_To_Positional
3891 begin
3892 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3893 -- components because in this case will need to call the corresponding
3894 -- IP procedure.
3896 if Has_Default_Init_Comps (N) then
3897 return;
3898 end if;
3900 if Is_Flat (N, Number_Dimensions (Typ)) then
3901 return;
3902 end if;
3904 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
3905 return;
3906 end if;
3908 -- Do not convert to positional if controlled components are involved
3909 -- since these require special processing
3911 if Has_Controlled_Component (Typ) then
3912 return;
3913 end if;
3915 Check_Static_Components;
3917 -- If the size is known, or all the components are static, try to
3918 -- build a fully positional aggregate.
3920 -- The size of the type may not be known for an aggregate with
3921 -- discriminated array components, but if the components are static
3922 -- it is still possible to verify statically that the length is
3923 -- compatible with the upper bound of the type, and therefore it is
3924 -- worth flattening such aggregates as well.
3926 -- For now the back-end expands these aggregates into individual
3927 -- assignments to the target anyway, but it is conceivable that
3928 -- it will eventually be able to treat such aggregates statically???
3930 if Aggr_Size_OK (N, Typ)
3931 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3932 then
3933 if Static_Components then
3934 Set_Compile_Time_Known_Aggregate (N);
3935 Set_Expansion_Delayed (N, False);
3936 end if;
3938 Analyze_And_Resolve (N, Typ);
3939 end if;
3941 -- Is Static_Eaboration_Desired has been specified, diagnose aggregates
3942 -- that will still require initialization code.
3944 if (Ekind (Current_Scope) = E_Package
3945 and then Static_Elaboration_Desired (Current_Scope))
3946 and then Nkind (Parent (N)) = N_Object_Declaration
3947 then
3948 declare
3949 Expr : Node_Id;
3951 begin
3952 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
3953 Expr := First (Expressions (N));
3954 while Present (Expr) loop
3955 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
3956 or else
3957 (Is_Entity_Name (Expr)
3958 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
3959 then
3960 null;
3962 else
3963 Error_Msg_N
3964 ("non-static object requires elaboration code??", N);
3965 exit;
3966 end if;
3968 Next (Expr);
3969 end loop;
3971 if Present (Component_Associations (N)) then
3972 Error_Msg_N ("object requires elaboration code??", N);
3973 end if;
3974 end if;
3975 end;
3976 end if;
3977 end Convert_To_Positional;
3979 ----------------------------
3980 -- Expand_Array_Aggregate --
3981 ----------------------------
3983 -- Array aggregate expansion proceeds as follows:
3985 -- 1. If requested we generate code to perform all the array aggregate
3986 -- bound checks, specifically
3988 -- (a) Check that the index range defined by aggregate bounds is
3989 -- compatible with corresponding index subtype.
3991 -- (b) If an others choice is present check that no aggregate
3992 -- index is outside the bounds of the index constraint.
3994 -- (c) For multidimensional arrays make sure that all subaggregates
3995 -- corresponding to the same dimension have the same bounds.
3997 -- 2. Check for packed array aggregate which can be converted to a
3998 -- constant so that the aggregate disappears completely.
4000 -- 3. Check case of nested aggregate. Generally nested aggregates are
4001 -- handled during the processing of the parent aggregate.
4003 -- 4. Check if the aggregate can be statically processed. If this is the
4004 -- case pass it as is to Gigi. Note that a necessary condition for
4005 -- static processing is that the aggregate be fully positional.
4007 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4008 -- a temporary) then mark the aggregate as such and return. Otherwise
4009 -- create a new temporary and generate the appropriate initialization
4010 -- code.
4012 procedure Expand_Array_Aggregate (N : Node_Id) is
4013 Loc : constant Source_Ptr := Sloc (N);
4015 Typ : constant Entity_Id := Etype (N);
4016 Ctyp : constant Entity_Id := Component_Type (Typ);
4017 -- Typ is the correct constrained array subtype of the aggregate
4018 -- Ctyp is the corresponding component type.
4020 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4021 -- Number of aggregate index dimensions
4023 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4024 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4025 -- Low and High bounds of the constraint for each aggregate index
4027 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4028 -- The type of each index
4030 In_Place_Assign_OK_For_Declaration : Boolean := False;
4031 -- True if we are to generate an in place assignment for a declaration
4033 Maybe_In_Place_OK : Boolean;
4034 -- If the type is neither controlled nor packed and the aggregate
4035 -- is the expression in an assignment, assignment in place may be
4036 -- possible, provided other conditions are met on the LHS.
4038 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4039 (others => False);
4040 -- If Others_Present (J) is True, then there is an others choice
4041 -- in one of the sub-aggregates of N at dimension J.
4043 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4044 -- Returns true if an aggregate assignment can be done by the back end
4046 procedure Build_Constrained_Type (Positional : Boolean);
4047 -- If the subtype is not static or unconstrained, build a constrained
4048 -- type using the computable sizes of the aggregate and its sub-
4049 -- aggregates.
4051 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4052 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4053 -- by Index_Bounds.
4055 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4056 -- Checks that in a multi-dimensional array aggregate all subaggregates
4057 -- corresponding to the same dimension have the same bounds.
4058 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4059 -- corresponding to the sub-aggregate.
4061 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4062 -- Computes the values of array Others_Present. Sub_Aggr is the
4063 -- array sub-aggregate we start the computation from. Dim is the
4064 -- dimension corresponding to the sub-aggregate.
4066 function In_Place_Assign_OK return Boolean;
4067 -- Simple predicate to determine whether an aggregate assignment can
4068 -- be done in place, because none of the new values can depend on the
4069 -- components of the target of the assignment.
4071 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4072 -- Checks that if an others choice is present in any sub-aggregate no
4073 -- aggregate index is outside the bounds of the index constraint.
4074 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4075 -- corresponding to the sub-aggregate.
4077 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4078 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4079 -- built directly into the target of the assignment it must be free
4080 -- of side-effects.
4082 ------------------------------------
4083 -- Aggr_Assignment_OK_For_Backend --
4084 ------------------------------------
4086 -- Backend processing by Gigi/gcc is possible only if all the following
4087 -- conditions are met:
4089 -- 1. N consists of a single OTHERS choice, possibly recursively
4091 -- 2. The array type is not packed
4093 -- 3. The array type has no atomic components
4095 -- 4. The array type has no null ranges (the purpose of this is to
4096 -- avoid a bogus warning for an out-of-range value).
4098 -- 5. The component type is discrete
4100 -- 6. The component size is Storage_Unit or the value is of the form
4101 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4102 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4103 -- the 8-bit value M, concatenated together.
4105 -- The ultimate goal is to generate a call to a fast memset routine
4106 -- specifically optimized for the target.
4108 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4109 Ctyp : Entity_Id;
4110 Index : Entity_Id;
4111 Expr : Node_Id := N;
4112 Low : Node_Id;
4113 High : Node_Id;
4114 Remainder : Uint;
4115 Value : Uint;
4116 Nunits : Nat;
4118 begin
4119 -- Recurse as far as possible to find the innermost component type
4121 Ctyp := Etype (N);
4122 while Is_Array_Type (Ctyp) loop
4123 if Nkind (Expr) /= N_Aggregate
4124 or else not Is_Others_Aggregate (Expr)
4125 then
4126 return False;
4127 end if;
4129 if Present (Packed_Array_Impl_Type (Ctyp)) then
4130 return False;
4131 end if;
4133 if Has_Atomic_Components (Ctyp) then
4134 return False;
4135 end if;
4137 Index := First_Index (Ctyp);
4138 while Present (Index) loop
4139 Get_Index_Bounds (Index, Low, High);
4141 if Is_Null_Range (Low, High) then
4142 return False;
4143 end if;
4145 Next_Index (Index);
4146 end loop;
4148 Expr := Expression (First (Component_Associations (Expr)));
4150 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4151 if Nkind (Expr) /= N_Aggregate
4152 or else not Is_Others_Aggregate (Expr)
4153 then
4154 return False;
4155 end if;
4157 Expr := Expression (First (Component_Associations (Expr)));
4158 end loop;
4160 Ctyp := Component_Type (Ctyp);
4162 if Is_Atomic (Ctyp) then
4163 return False;
4164 end if;
4165 end loop;
4167 if not Is_Discrete_Type (Ctyp) then
4168 return False;
4169 end if;
4171 -- The expression needs to be analyzed if True is returned
4173 Analyze_And_Resolve (Expr, Ctyp);
4175 -- The back end uses the Esize as the precision of the type
4177 Nunits := UI_To_Int (Esize (Ctyp)) / System_Storage_Unit;
4179 if Nunits = 1 then
4180 return True;
4181 end if;
4183 if not Compile_Time_Known_Value (Expr) then
4184 return False;
4185 end if;
4187 Value := Expr_Value (Expr);
4189 if Has_Biased_Representation (Ctyp) then
4190 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
4191 end if;
4193 -- Values 0 and -1 immediately satisfy the last check
4195 if Value = Uint_0 or else Value = Uint_Minus_1 then
4196 return True;
4197 end if;
4199 -- We need to work with an unsigned value
4201 if Value < 0 then
4202 Value := Value + 2**(System_Storage_Unit * Nunits);
4203 end if;
4205 Remainder := Value rem 2**System_Storage_Unit;
4207 for J in 1 .. Nunits - 1 loop
4208 Value := Value / 2**System_Storage_Unit;
4210 if Value rem 2**System_Storage_Unit /= Remainder then
4211 return False;
4212 end if;
4213 end loop;
4215 return True;
4216 end Aggr_Assignment_OK_For_Backend;
4218 ----------------------------
4219 -- Build_Constrained_Type --
4220 ----------------------------
4222 procedure Build_Constrained_Type (Positional : Boolean) is
4223 Loc : constant Source_Ptr := Sloc (N);
4224 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4225 Comp : Node_Id;
4226 Decl : Node_Id;
4227 Typ : constant Entity_Id := Etype (N);
4228 Indexes : constant List_Id := New_List;
4229 Num : Int;
4230 Sub_Agg : Node_Id;
4232 begin
4233 -- If the aggregate is purely positional, all its subaggregates
4234 -- have the same size. We collect the dimensions from the first
4235 -- subaggregate at each level.
4237 if Positional then
4238 Sub_Agg := N;
4240 for D in 1 .. Number_Dimensions (Typ) loop
4241 Sub_Agg := First (Expressions (Sub_Agg));
4243 Comp := Sub_Agg;
4244 Num := 0;
4245 while Present (Comp) loop
4246 Num := Num + 1;
4247 Next (Comp);
4248 end loop;
4250 Append_To (Indexes,
4251 Make_Range (Loc,
4252 Low_Bound => Make_Integer_Literal (Loc, 1),
4253 High_Bound => Make_Integer_Literal (Loc, Num)));
4254 end loop;
4256 else
4257 -- We know the aggregate type is unconstrained and the aggregate
4258 -- is not processable by the back end, therefore not necessarily
4259 -- positional. Retrieve each dimension bounds (computed earlier).
4261 for D in 1 .. Number_Dimensions (Typ) loop
4262 Append_To (Indexes,
4263 Make_Range (Loc,
4264 Low_Bound => Aggr_Low (D),
4265 High_Bound => Aggr_High (D)));
4266 end loop;
4267 end if;
4269 Decl :=
4270 Make_Full_Type_Declaration (Loc,
4271 Defining_Identifier => Agg_Type,
4272 Type_Definition =>
4273 Make_Constrained_Array_Definition (Loc,
4274 Discrete_Subtype_Definitions => Indexes,
4275 Component_Definition =>
4276 Make_Component_Definition (Loc,
4277 Aliased_Present => False,
4278 Subtype_Indication =>
4279 New_Occurrence_Of (Component_Type (Typ), Loc))));
4281 Insert_Action (N, Decl);
4282 Analyze (Decl);
4283 Set_Etype (N, Agg_Type);
4284 Set_Is_Itype (Agg_Type);
4285 Freeze_Itype (Agg_Type, N);
4286 end Build_Constrained_Type;
4288 ------------------
4289 -- Check_Bounds --
4290 ------------------
4292 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4293 Aggr_Lo : Node_Id;
4294 Aggr_Hi : Node_Id;
4296 Ind_Lo : Node_Id;
4297 Ind_Hi : Node_Id;
4299 Cond : Node_Id := Empty;
4301 begin
4302 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4303 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4305 -- Generate the following test:
4307 -- [constraint_error when
4308 -- Aggr_Lo <= Aggr_Hi and then
4309 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4311 -- As an optimization try to see if some tests are trivially vacuous
4312 -- because we are comparing an expression against itself.
4314 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4315 Cond := Empty;
4317 elsif Aggr_Hi = Ind_Hi then
4318 Cond :=
4319 Make_Op_Lt (Loc,
4320 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4321 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4323 elsif Aggr_Lo = Ind_Lo then
4324 Cond :=
4325 Make_Op_Gt (Loc,
4326 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4327 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4329 else
4330 Cond :=
4331 Make_Or_Else (Loc,
4332 Left_Opnd =>
4333 Make_Op_Lt (Loc,
4334 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4335 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4337 Right_Opnd =>
4338 Make_Op_Gt (Loc,
4339 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4340 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4341 end if;
4343 if Present (Cond) then
4344 Cond :=
4345 Make_And_Then (Loc,
4346 Left_Opnd =>
4347 Make_Op_Le (Loc,
4348 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4349 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4351 Right_Opnd => Cond);
4353 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4354 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4355 Insert_Action (N,
4356 Make_Raise_Constraint_Error (Loc,
4357 Condition => Cond,
4358 Reason => CE_Range_Check_Failed));
4359 end if;
4360 end Check_Bounds;
4362 ----------------------------
4363 -- Check_Same_Aggr_Bounds --
4364 ----------------------------
4366 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4367 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4368 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4369 -- The bounds of this specific sub-aggregate
4371 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4372 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4373 -- The bounds of the aggregate for this dimension
4375 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4376 -- The index type for this dimension.xxx
4378 Cond : Node_Id := Empty;
4379 Assoc : Node_Id;
4380 Expr : Node_Id;
4382 begin
4383 -- If index checks are on generate the test
4385 -- [constraint_error when
4386 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4388 -- As an optimization try to see if some tests are trivially vacuos
4389 -- because we are comparing an expression against itself. Also for
4390 -- the first dimension the test is trivially vacuous because there
4391 -- is just one aggregate for dimension 1.
4393 if Index_Checks_Suppressed (Ind_Typ) then
4394 Cond := Empty;
4396 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4397 then
4398 Cond := Empty;
4400 elsif Aggr_Hi = Sub_Hi then
4401 Cond :=
4402 Make_Op_Ne (Loc,
4403 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4404 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4406 elsif Aggr_Lo = Sub_Lo then
4407 Cond :=
4408 Make_Op_Ne (Loc,
4409 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4410 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4412 else
4413 Cond :=
4414 Make_Or_Else (Loc,
4415 Left_Opnd =>
4416 Make_Op_Ne (Loc,
4417 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4418 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4420 Right_Opnd =>
4421 Make_Op_Ne (Loc,
4422 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4423 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4424 end if;
4426 if Present (Cond) then
4427 Insert_Action (N,
4428 Make_Raise_Constraint_Error (Loc,
4429 Condition => Cond,
4430 Reason => CE_Length_Check_Failed));
4431 end if;
4433 -- Now look inside the sub-aggregate to see if there is more work
4435 if Dim < Aggr_Dimension then
4437 -- Process positional components
4439 if Present (Expressions (Sub_Aggr)) then
4440 Expr := First (Expressions (Sub_Aggr));
4441 while Present (Expr) loop
4442 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4443 Next (Expr);
4444 end loop;
4445 end if;
4447 -- Process component associations
4449 if Present (Component_Associations (Sub_Aggr)) then
4450 Assoc := First (Component_Associations (Sub_Aggr));
4451 while Present (Assoc) loop
4452 Expr := Expression (Assoc);
4453 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4454 Next (Assoc);
4455 end loop;
4456 end if;
4457 end if;
4458 end Check_Same_Aggr_Bounds;
4460 ----------------------------
4461 -- Compute_Others_Present --
4462 ----------------------------
4464 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4465 Assoc : Node_Id;
4466 Expr : Node_Id;
4468 begin
4469 if Present (Component_Associations (Sub_Aggr)) then
4470 Assoc := Last (Component_Associations (Sub_Aggr));
4472 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4473 Others_Present (Dim) := True;
4474 end if;
4475 end if;
4477 -- Now look inside the sub-aggregate to see if there is more work
4479 if Dim < Aggr_Dimension then
4481 -- Process positional components
4483 if Present (Expressions (Sub_Aggr)) then
4484 Expr := First (Expressions (Sub_Aggr));
4485 while Present (Expr) loop
4486 Compute_Others_Present (Expr, Dim + 1);
4487 Next (Expr);
4488 end loop;
4489 end if;
4491 -- Process component associations
4493 if Present (Component_Associations (Sub_Aggr)) then
4494 Assoc := First (Component_Associations (Sub_Aggr));
4495 while Present (Assoc) loop
4496 Expr := Expression (Assoc);
4497 Compute_Others_Present (Expr, Dim + 1);
4498 Next (Assoc);
4499 end loop;
4500 end if;
4501 end if;
4502 end Compute_Others_Present;
4504 ------------------------
4505 -- In_Place_Assign_OK --
4506 ------------------------
4508 function In_Place_Assign_OK return Boolean is
4509 Aggr_In : Node_Id;
4510 Aggr_Lo : Node_Id;
4511 Aggr_Hi : Node_Id;
4512 Obj_In : Node_Id;
4513 Obj_Lo : Node_Id;
4514 Obj_Hi : Node_Id;
4516 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4517 -- Check recursively that each component of a (sub)aggregate does
4518 -- not depend on the variable being assigned to.
4520 function Safe_Component (Expr : Node_Id) return Boolean;
4521 -- Verify that an expression cannot depend on the variable being
4522 -- assigned to. Room for improvement here (but less than before).
4524 --------------------
4525 -- Safe_Aggregate --
4526 --------------------
4528 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4529 Expr : Node_Id;
4531 begin
4532 if Present (Expressions (Aggr)) then
4533 Expr := First (Expressions (Aggr));
4534 while Present (Expr) loop
4535 if Nkind (Expr) = N_Aggregate then
4536 if not Safe_Aggregate (Expr) then
4537 return False;
4538 end if;
4540 elsif not Safe_Component (Expr) then
4541 return False;
4542 end if;
4544 Next (Expr);
4545 end loop;
4546 end if;
4548 if Present (Component_Associations (Aggr)) then
4549 Expr := First (Component_Associations (Aggr));
4550 while Present (Expr) loop
4551 if Nkind (Expression (Expr)) = N_Aggregate then
4552 if not Safe_Aggregate (Expression (Expr)) then
4553 return False;
4554 end if;
4556 -- If association has a box, no way to determine yet
4557 -- whether default can be assigned in place.
4559 elsif Box_Present (Expr) then
4560 return False;
4562 elsif not Safe_Component (Expression (Expr)) then
4563 return False;
4564 end if;
4566 Next (Expr);
4567 end loop;
4568 end if;
4570 return True;
4571 end Safe_Aggregate;
4573 --------------------
4574 -- Safe_Component --
4575 --------------------
4577 function Safe_Component (Expr : Node_Id) return Boolean is
4578 Comp : Node_Id := Expr;
4580 function Check_Component (Comp : Node_Id) return Boolean;
4581 -- Do the recursive traversal, after copy
4583 ---------------------
4584 -- Check_Component --
4585 ---------------------
4587 function Check_Component (Comp : Node_Id) return Boolean is
4588 begin
4589 if Is_Overloaded (Comp) then
4590 return False;
4591 end if;
4593 return Compile_Time_Known_Value (Comp)
4595 or else (Is_Entity_Name (Comp)
4596 and then Present (Entity (Comp))
4597 and then No (Renamed_Object (Entity (Comp))))
4599 or else (Nkind (Comp) = N_Attribute_Reference
4600 and then Check_Component (Prefix (Comp)))
4602 or else (Nkind (Comp) in N_Binary_Op
4603 and then Check_Component (Left_Opnd (Comp))
4604 and then Check_Component (Right_Opnd (Comp)))
4606 or else (Nkind (Comp) in N_Unary_Op
4607 and then Check_Component (Right_Opnd (Comp)))
4609 or else (Nkind (Comp) = N_Selected_Component
4610 and then Check_Component (Prefix (Comp)))
4612 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4613 and then Check_Component (Expression (Comp)));
4614 end Check_Component;
4616 -- Start of processing for Safe_Component
4618 begin
4619 -- If the component appears in an association that may correspond
4620 -- to more than one element, it is not analyzed before expansion
4621 -- into assignments, to avoid side effects. We analyze, but do not
4622 -- resolve the copy, to obtain sufficient entity information for
4623 -- the checks that follow. If component is overloaded we assume
4624 -- an unsafe function call.
4626 if not Analyzed (Comp) then
4627 if Is_Overloaded (Expr) then
4628 return False;
4630 elsif Nkind (Expr) = N_Aggregate
4631 and then not Is_Others_Aggregate (Expr)
4632 then
4633 return False;
4635 elsif Nkind (Expr) = N_Allocator then
4637 -- For now, too complex to analyze
4639 return False;
4640 end if;
4642 Comp := New_Copy_Tree (Expr);
4643 Set_Parent (Comp, Parent (Expr));
4644 Analyze (Comp);
4645 end if;
4647 if Nkind (Comp) = N_Aggregate then
4648 return Safe_Aggregate (Comp);
4649 else
4650 return Check_Component (Comp);
4651 end if;
4652 end Safe_Component;
4654 -- Start of processing for In_Place_Assign_OK
4656 begin
4657 if Present (Component_Associations (N)) then
4659 -- On assignment, sliding can take place, so we cannot do the
4660 -- assignment in place unless the bounds of the aggregate are
4661 -- statically equal to those of the target.
4663 -- If the aggregate is given by an others choice, the bounds are
4664 -- derived from the left-hand side, and the assignment is safe if
4665 -- the expression is.
4667 if Is_Others_Aggregate (N) then
4668 return
4669 Safe_Component
4670 (Expression (First (Component_Associations (N))));
4671 end if;
4673 Aggr_In := First_Index (Etype (N));
4675 if Nkind (Parent (N)) = N_Assignment_Statement then
4676 Obj_In := First_Index (Etype (Name (Parent (N))));
4678 else
4679 -- Context is an allocator. Check bounds of aggregate against
4680 -- given type in qualified expression.
4682 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4683 Obj_In :=
4684 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4685 end if;
4687 while Present (Aggr_In) loop
4688 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4689 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4691 if not Compile_Time_Known_Value (Aggr_Lo)
4692 or else not Compile_Time_Known_Value (Aggr_Hi)
4693 or else not Compile_Time_Known_Value (Obj_Lo)
4694 or else not Compile_Time_Known_Value (Obj_Hi)
4695 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4696 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4697 then
4698 return False;
4699 end if;
4701 Next_Index (Aggr_In);
4702 Next_Index (Obj_In);
4703 end loop;
4704 end if;
4706 -- Now check the component values themselves
4708 return Safe_Aggregate (N);
4709 end In_Place_Assign_OK;
4711 ------------------
4712 -- Others_Check --
4713 ------------------
4715 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4716 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4717 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4718 -- The bounds of the aggregate for this dimension
4720 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4721 -- The index type for this dimension
4723 Need_To_Check : Boolean := False;
4725 Choices_Lo : Node_Id := Empty;
4726 Choices_Hi : Node_Id := Empty;
4727 -- The lowest and highest discrete choices for a named sub-aggregate
4729 Nb_Choices : Int := -1;
4730 -- The number of discrete non-others choices in this sub-aggregate
4732 Nb_Elements : Uint := Uint_0;
4733 -- The number of elements in a positional aggregate
4735 Cond : Node_Id := Empty;
4737 Assoc : Node_Id;
4738 Choice : Node_Id;
4739 Expr : Node_Id;
4741 begin
4742 -- Check if we have an others choice. If we do make sure that this
4743 -- sub-aggregate contains at least one element in addition to the
4744 -- others choice.
4746 if Range_Checks_Suppressed (Ind_Typ) then
4747 Need_To_Check := False;
4749 elsif Present (Expressions (Sub_Aggr))
4750 and then Present (Component_Associations (Sub_Aggr))
4751 then
4752 Need_To_Check := True;
4754 elsif Present (Component_Associations (Sub_Aggr)) then
4755 Assoc := Last (Component_Associations (Sub_Aggr));
4757 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4758 Need_To_Check := False;
4760 else
4761 -- Count the number of discrete choices. Start with -1 because
4762 -- the others choice does not count.
4764 -- Is there some reason we do not use List_Length here ???
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_Occurrence_Of (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_Occurrence_Of (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 => Duplicate_Subexpr_Move_Checks (Choices_Lo),
4888 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4890 Right_Opnd =>
4891 Make_Op_Gt (Loc,
4892 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
4893 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
4894 end if;
4896 if Present (Cond) then
4897 Insert_Action (N,
4898 Make_Raise_Constraint_Error (Loc,
4899 Condition => Cond,
4900 Reason => CE_Length_Check_Failed));
4901 -- Questionable reason code, shouldn't that be a
4902 -- CE_Range_Check_Failed ???
4903 end if;
4905 -- Now look inside the sub-aggregate to see if there is more work
4907 if Dim < Aggr_Dimension then
4909 -- Process positional components
4911 if Present (Expressions (Sub_Aggr)) then
4912 Expr := First (Expressions (Sub_Aggr));
4913 while Present (Expr) loop
4914 Others_Check (Expr, Dim + 1);
4915 Next (Expr);
4916 end loop;
4917 end if;
4919 -- Process component associations
4921 if Present (Component_Associations (Sub_Aggr)) then
4922 Assoc := First (Component_Associations (Sub_Aggr));
4923 while Present (Assoc) loop
4924 Expr := Expression (Assoc);
4925 Others_Check (Expr, Dim + 1);
4926 Next (Assoc);
4927 end loop;
4928 end if;
4929 end if;
4930 end Others_Check;
4932 -------------------------
4933 -- Safe_Left_Hand_Side --
4934 -------------------------
4936 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
4937 function Is_Safe_Index (Indx : Node_Id) return Boolean;
4938 -- If the left-hand side includes an indexed component, check that
4939 -- the indexes are free of side-effect.
4941 -------------------
4942 -- Is_Safe_Index --
4943 -------------------
4945 function Is_Safe_Index (Indx : Node_Id) return Boolean is
4946 begin
4947 if Is_Entity_Name (Indx) then
4948 return True;
4950 elsif Nkind (Indx) = N_Integer_Literal then
4951 return True;
4953 elsif Nkind (Indx) = N_Function_Call
4954 and then Is_Entity_Name (Name (Indx))
4955 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
4956 then
4957 return True;
4959 elsif Nkind (Indx) = N_Type_Conversion
4960 and then Is_Safe_Index (Expression (Indx))
4961 then
4962 return True;
4964 else
4965 return False;
4966 end if;
4967 end Is_Safe_Index;
4969 -- Start of processing for Safe_Left_Hand_Side
4971 begin
4972 if Is_Entity_Name (N) then
4973 return True;
4975 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
4976 and then Safe_Left_Hand_Side (Prefix (N))
4977 then
4978 return True;
4980 elsif Nkind (N) = N_Indexed_Component
4981 and then Safe_Left_Hand_Side (Prefix (N))
4982 and then Is_Safe_Index (First (Expressions (N)))
4983 then
4984 return True;
4986 elsif Nkind (N) = N_Unchecked_Type_Conversion then
4987 return Safe_Left_Hand_Side (Expression (N));
4989 else
4990 return False;
4991 end if;
4992 end Safe_Left_Hand_Side;
4994 -- Local variables
4996 Tmp : Entity_Id;
4997 -- Holds the temporary aggregate value
4999 Tmp_Decl : Node_Id;
5000 -- Holds the declaration of Tmp
5002 Aggr_Code : List_Id;
5003 Parent_Node : Node_Id;
5004 Parent_Kind : Node_Kind;
5006 -- Start of processing for Expand_Array_Aggregate
5008 begin
5009 -- Do not touch the special aggregates of attributes used for Asm calls
5011 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5012 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5013 then
5014 return;
5016 -- Do not expand an aggregate for an array type which contains tasks if
5017 -- the aggregate is associated with an unexpanded return statement of a
5018 -- build-in-place function. The aggregate is expanded when the related
5019 -- return statement (rewritten into an extended return) is processed.
5020 -- This delay ensures that any temporaries and initialization code
5021 -- generated for the aggregate appear in the proper return block and
5022 -- use the correct _chain and _master.
5024 elsif Has_Task (Base_Type (Etype (N)))
5025 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5026 and then Is_Build_In_Place_Function
5027 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5028 then
5029 return;
5031 -- Do not attempt expansion if error already detected. We may reach this
5032 -- point in spite of previous errors when compiling with -gnatq, to
5033 -- force all possible errors (this is the usual ACATS mode).
5035 elsif Error_Posted (N) then
5036 return;
5037 end if;
5039 -- If the semantic analyzer has determined that aggregate N will raise
5040 -- Constraint_Error at run time, then the aggregate node has been
5041 -- replaced with an N_Raise_Constraint_Error node and we should
5042 -- never get here.
5044 pragma Assert (not Raises_Constraint_Error (N));
5046 -- STEP 1a
5048 -- Check that the index range defined by aggregate bounds is
5049 -- compatible with corresponding index subtype.
5051 Index_Compatibility_Check : declare
5052 Aggr_Index_Range : Node_Id := First_Index (Typ);
5053 -- The current aggregate index range
5055 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5056 -- The corresponding index constraint against which we have to
5057 -- check the above aggregate index range.
5059 begin
5060 Compute_Others_Present (N, 1);
5062 for J in 1 .. Aggr_Dimension loop
5063 -- There is no need to emit a check if an others choice is present
5064 -- for this array aggregate dimension since in this case one of
5065 -- N's sub-aggregates has taken its bounds from the context and
5066 -- these bounds must have been checked already. In addition all
5067 -- sub-aggregates corresponding to the same dimension must all
5068 -- have the same bounds (checked in (c) below).
5070 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5071 and then not Others_Present (J)
5072 then
5073 -- We don't use Checks.Apply_Range_Check here because it emits
5074 -- a spurious check. Namely it checks that the range defined by
5075 -- the aggregate bounds is non empty. But we know this already
5076 -- if we get here.
5078 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5079 end if;
5081 -- Save the low and high bounds of the aggregate index as well as
5082 -- the index type for later use in checks (b) and (c) below.
5084 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5085 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5087 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5089 Next_Index (Aggr_Index_Range);
5090 Next_Index (Index_Constraint);
5091 end loop;
5092 end Index_Compatibility_Check;
5094 -- STEP 1b
5096 -- If an others choice is present check that no aggregate index is
5097 -- outside the bounds of the index constraint.
5099 Others_Check (N, 1);
5101 -- STEP 1c
5103 -- For multidimensional arrays make sure that all subaggregates
5104 -- corresponding to the same dimension have the same bounds.
5106 if Aggr_Dimension > 1 then
5107 Check_Same_Aggr_Bounds (N, 1);
5108 end if;
5110 -- STEP 1d
5112 -- If we have a default component value, or simple initialization is
5113 -- required for the component type, then we replace <> in component
5114 -- associations by the required default value.
5116 declare
5117 Default_Val : Node_Id;
5118 Assoc : Node_Id;
5120 begin
5121 if (Present (Default_Aspect_Component_Value (Typ))
5122 or else Needs_Simple_Initialization (Ctyp))
5123 and then Present (Component_Associations (N))
5124 then
5125 Assoc := First (Component_Associations (N));
5126 while Present (Assoc) loop
5127 if Nkind (Assoc) = N_Component_Association
5128 and then Box_Present (Assoc)
5129 then
5130 Set_Box_Present (Assoc, False);
5132 if Present (Default_Aspect_Component_Value (Typ)) then
5133 Default_Val := Default_Aspect_Component_Value (Typ);
5134 else
5135 Default_Val := Get_Simple_Init_Val (Ctyp, N);
5136 end if;
5138 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
5139 Analyze_And_Resolve (Expression (Assoc), Ctyp);
5140 end if;
5142 Next (Assoc);
5143 end loop;
5144 end if;
5145 end;
5147 -- STEP 2
5149 -- Here we test for is packed array aggregate that we can handle at
5150 -- compile time. If so, return with transformation done. Note that we do
5151 -- this even if the aggregate is nested, because once we have done this
5152 -- processing, there is no more nested aggregate.
5154 if Packed_Array_Aggregate_Handled (N) then
5155 return;
5156 end if;
5158 -- At this point we try to convert to positional form
5160 if Ekind (Current_Scope) = E_Package
5161 and then Static_Elaboration_Desired (Current_Scope)
5162 then
5163 Convert_To_Positional (N, Max_Others_Replicate => 100);
5164 else
5165 Convert_To_Positional (N);
5166 end if;
5168 -- if the result is no longer an aggregate (e.g. it may be a string
5169 -- literal, or a temporary which has the needed value), then we are
5170 -- done, since there is no longer a nested aggregate.
5172 if Nkind (N) /= N_Aggregate then
5173 return;
5175 -- We are also done if the result is an analyzed aggregate, indicating
5176 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5177 -- aggregate.
5179 elsif Analyzed (N) and then N /= Original_Node (N) then
5180 return;
5181 end if;
5183 -- If all aggregate components are compile-time known and the aggregate
5184 -- has been flattened, nothing left to do. The same occurs if the
5185 -- aggregate is used to initialize the components of a statically
5186 -- allocated dispatch table.
5188 if Compile_Time_Known_Aggregate (N)
5189 or else Is_Static_Dispatch_Table_Aggregate (N)
5190 then
5191 Set_Expansion_Delayed (N, False);
5192 return;
5193 end if;
5195 -- Now see if back end processing is possible
5197 if Backend_Processing_Possible (N) then
5199 -- If the aggregate is static but the constraints are not, build
5200 -- a static subtype for the aggregate, so that Gigi can place it
5201 -- in static memory. Perform an unchecked_conversion to the non-
5202 -- static type imposed by the context.
5204 declare
5205 Itype : constant Entity_Id := Etype (N);
5206 Index : Node_Id;
5207 Needs_Type : Boolean := False;
5209 begin
5210 Index := First_Index (Itype);
5211 while Present (Index) loop
5212 if not Is_OK_Static_Subtype (Etype (Index)) then
5213 Needs_Type := True;
5214 exit;
5215 else
5216 Next_Index (Index);
5217 end if;
5218 end loop;
5220 if Needs_Type then
5221 Build_Constrained_Type (Positional => True);
5222 Rewrite (N, Unchecked_Convert_To (Itype, N));
5223 Analyze (N);
5224 end if;
5225 end;
5227 return;
5228 end if;
5230 -- STEP 3
5232 -- Delay expansion for nested aggregates: it will be taken care of
5233 -- when the parent aggregate is expanded.
5235 Parent_Node := Parent (N);
5236 Parent_Kind := Nkind (Parent_Node);
5238 if Parent_Kind = N_Qualified_Expression then
5239 Parent_Node := Parent (Parent_Node);
5240 Parent_Kind := Nkind (Parent_Node);
5241 end if;
5243 if Parent_Kind = N_Aggregate
5244 or else Parent_Kind = N_Extension_Aggregate
5245 or else Parent_Kind = N_Component_Association
5246 or else (Parent_Kind = N_Object_Declaration
5247 and then Needs_Finalization (Typ))
5248 or else (Parent_Kind = N_Assignment_Statement
5249 and then Inside_Init_Proc)
5250 then
5251 if Static_Array_Aggregate (N)
5252 or else Compile_Time_Known_Aggregate (N)
5253 then
5254 Set_Expansion_Delayed (N, False);
5255 return;
5256 else
5257 Set_Expansion_Delayed (N);
5258 return;
5259 end if;
5260 end if;
5262 -- STEP 4
5264 -- Look if in place aggregate expansion is possible
5266 -- For object declarations we build the aggregate in place, unless
5267 -- the array is bit-packed or the component is controlled.
5269 -- For assignments we do the assignment in place if all the component
5270 -- associations have compile-time known values. For other cases we
5271 -- create a temporary. The analysis for safety of on-line assignment
5272 -- is delicate, i.e. we don't know how to do it fully yet ???
5274 -- For allocators we assign to the designated object in place if the
5275 -- aggregate meets the same conditions as other in-place assignments.
5276 -- In this case the aggregate may not come from source but was created
5277 -- for default initialization, e.g. with Initialize_Scalars.
5279 if Requires_Transient_Scope (Typ) then
5280 Establish_Transient_Scope
5281 (N, Sec_Stack => Has_Controlled_Component (Typ));
5282 end if;
5284 if Has_Default_Init_Comps (N) then
5285 Maybe_In_Place_OK := False;
5287 elsif Is_Bit_Packed_Array (Typ)
5288 or else Has_Controlled_Component (Typ)
5289 then
5290 Maybe_In_Place_OK := False;
5292 else
5293 Maybe_In_Place_OK :=
5294 (Nkind (Parent (N)) = N_Assignment_Statement
5295 and then In_Place_Assign_OK)
5297 or else
5298 (Nkind (Parent (Parent (N))) = N_Allocator
5299 and then In_Place_Assign_OK);
5300 end if;
5302 -- If this is an array of tasks, it will be expanded into build-in-place
5303 -- assignments. Build an activation chain for the tasks now.
5305 if Has_Task (Etype (N)) then
5306 Build_Activation_Chain_Entity (N);
5307 end if;
5309 -- Perform in-place expansion of aggregate in an object declaration.
5310 -- Note: actions generated for the aggregate will be captured in an
5311 -- expression-with-actions statement so that they can be transferred
5312 -- to freeze actions later if there is an address clause for the
5313 -- object. (Note: we don't use a block statement because this would
5314 -- cause generated freeze nodes to be elaborated in the wrong scope).
5316 -- Should document these individual tests ???
5318 if not Has_Default_Init_Comps (N)
5319 and then Comes_From_Source (Parent_Node)
5320 and then Parent_Kind = N_Object_Declaration
5321 and then not
5322 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
5323 and then N = Expression (Parent_Node)
5324 and then not Is_Bit_Packed_Array (Typ)
5325 and then not Has_Controlled_Component (Typ)
5326 then
5327 In_Place_Assign_OK_For_Declaration := True;
5328 Tmp := Defining_Identifier (Parent (N));
5329 Set_No_Initialization (Parent (N));
5330 Set_Expression (Parent (N), Empty);
5332 -- Set kind and type of the entity, for use in the analysis
5333 -- of the subsequent assignments. If the nominal type is not
5334 -- constrained, build a subtype from the known bounds of the
5335 -- aggregate. If the declaration has a subtype mark, use it,
5336 -- otherwise use the itype of the aggregate.
5338 Set_Ekind (Tmp, E_Variable);
5340 if not Is_Constrained (Typ) then
5341 Build_Constrained_Type (Positional => False);
5343 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5344 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5345 then
5346 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5348 else
5349 Set_Size_Known_At_Compile_Time (Typ, False);
5350 Set_Etype (Tmp, Typ);
5351 end if;
5353 elsif Maybe_In_Place_OK
5354 and then Nkind (Parent (N)) = N_Qualified_Expression
5355 and then Nkind (Parent (Parent (N))) = N_Allocator
5356 then
5357 Set_Expansion_Delayed (N);
5358 return;
5360 -- In the remaining cases the aggregate is the RHS of an assignment
5362 elsif Maybe_In_Place_OK
5363 and then Safe_Left_Hand_Side (Name (Parent (N)))
5364 then
5365 Tmp := Name (Parent (N));
5367 if Etype (Tmp) /= Etype (N) then
5368 Apply_Length_Check (N, Etype (Tmp));
5370 if Nkind (N) = N_Raise_Constraint_Error then
5372 -- Static error, nothing further to expand
5374 return;
5375 end if;
5376 end if;
5378 -- If a slice assignment has an aggregate with a single others_choice,
5379 -- the assignment can be done in place even if bounds are not static,
5380 -- by converting it into a loop over the discrete range of the slice.
5382 elsif Maybe_In_Place_OK
5383 and then Nkind (Name (Parent (N))) = N_Slice
5384 and then Is_Others_Aggregate (N)
5385 then
5386 Tmp := Name (Parent (N));
5388 -- Set type of aggregate to be type of lhs in assignment, in order
5389 -- to suppress redundant length checks.
5391 Set_Etype (N, Etype (Tmp));
5393 -- Step 5
5395 -- In place aggregate expansion is not possible
5397 else
5398 Maybe_In_Place_OK := False;
5399 Tmp := Make_Temporary (Loc, 'A', N);
5400 Tmp_Decl :=
5401 Make_Object_Declaration (Loc,
5402 Defining_Identifier => Tmp,
5403 Object_Definition => New_Occurrence_Of (Typ, Loc));
5404 Set_No_Initialization (Tmp_Decl, True);
5406 -- If we are within a loop, the temporary will be pushed on the
5407 -- stack at each iteration. If the aggregate is the expression for an
5408 -- allocator, it will be immediately copied to the heap and can
5409 -- be reclaimed at once. We create a transient scope around the
5410 -- aggregate for this purpose.
5412 if Ekind (Current_Scope) = E_Loop
5413 and then Nkind (Parent (Parent (N))) = N_Allocator
5414 then
5415 Establish_Transient_Scope (N, False);
5416 end if;
5418 Insert_Action (N, Tmp_Decl);
5419 end if;
5421 -- Construct and insert the aggregate code. We can safely suppress index
5422 -- checks because this code is guaranteed not to raise CE on index
5423 -- checks. However we should *not* suppress all checks.
5425 declare
5426 Target : Node_Id;
5428 begin
5429 if Nkind (Tmp) = N_Defining_Identifier then
5430 Target := New_Occurrence_Of (Tmp, Loc);
5432 else
5433 if Has_Default_Init_Comps (N) then
5435 -- Ada 2005 (AI-287): This case has not been analyzed???
5437 raise Program_Error;
5438 end if;
5440 -- Name in assignment is explicit dereference
5442 Target := New_Copy (Tmp);
5443 end if;
5445 -- If we are to generate an in place assignment for a declaration or
5446 -- an assignment statement, and the assignment can be done directly
5447 -- by the back end, then do not expand further.
5449 -- ??? We can also do that if in place expansion is not possible but
5450 -- then we could go into an infinite recursion.
5452 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
5453 and then VM_Target = No_VM
5454 and then not AAMP_On_Target
5455 and then not Generate_SCIL
5456 and then not Possible_Bit_Aligned_Component (Target)
5457 and then not Is_Possibly_Unaligned_Slice (Target)
5458 and then Aggr_Assignment_OK_For_Backend (N)
5459 then
5460 if Maybe_In_Place_OK then
5461 return;
5462 end if;
5464 Aggr_Code :=
5465 New_List (
5466 Make_Assignment_Statement (Loc,
5467 Name => Target,
5468 Expression => New_Copy (N)));
5470 else
5471 Aggr_Code :=
5472 Build_Array_Aggr_Code (N,
5473 Ctype => Ctyp,
5474 Index => First_Index (Typ),
5475 Into => Target,
5476 Scalar_Comp => Is_Scalar_Type (Ctyp));
5477 end if;
5479 -- Save the last assignment statement associated with the aggregate
5480 -- when building a controlled object. This reference is utilized by
5481 -- the finalization machinery when marking an object as successfully
5482 -- initialized.
5484 if Needs_Finalization (Typ)
5485 and then Is_Entity_Name (Target)
5486 and then Present (Entity (Target))
5487 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
5488 then
5489 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
5490 end if;
5491 end;
5493 -- If the aggregate is the expression in a declaration, the expanded
5494 -- code must be inserted after it. The defining entity might not come
5495 -- from source if this is part of an inlined body, but the declaration
5496 -- itself will.
5498 if Comes_From_Source (Tmp)
5499 or else
5500 (Nkind (Parent (N)) = N_Object_Declaration
5501 and then Comes_From_Source (Parent (N))
5502 and then Tmp = Defining_Entity (Parent (N)))
5503 then
5504 declare
5505 Node_After : constant Node_Id := Next (Parent_Node);
5507 begin
5508 Insert_Actions_After (Parent_Node, Aggr_Code);
5510 if Parent_Kind = N_Object_Declaration then
5511 Collect_Initialization_Statements
5512 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
5513 end if;
5514 end;
5516 else
5517 Insert_Actions (N, Aggr_Code);
5518 end if;
5520 -- If the aggregate has been assigned in place, remove the original
5521 -- assignment.
5523 if Nkind (Parent (N)) = N_Assignment_Statement
5524 and then Maybe_In_Place_OK
5525 then
5526 Rewrite (Parent (N), Make_Null_Statement (Loc));
5528 elsif Nkind (Parent (N)) /= N_Object_Declaration
5529 or else Tmp /= Defining_Identifier (Parent (N))
5530 then
5531 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5532 Analyze_And_Resolve (N, Typ);
5533 end if;
5534 end Expand_Array_Aggregate;
5536 ------------------------
5537 -- Expand_N_Aggregate --
5538 ------------------------
5540 procedure Expand_N_Aggregate (N : Node_Id) is
5541 begin
5542 -- Record aggregate case
5544 if Is_Record_Type (Etype (N)) then
5545 Expand_Record_Aggregate (N);
5547 -- Array aggregate case
5549 else
5550 -- A special case, if we have a string subtype with bounds 1 .. N,
5551 -- where N is known at compile time, and the aggregate is of the
5552 -- form (others => 'x'), with a single choice and no expressions,
5553 -- and N is less than 80 (an arbitrary limit for now), then replace
5554 -- the aggregate by the equivalent string literal (but do not mark
5555 -- it as static since it is not).
5557 -- Note: this entire circuit is redundant with respect to code in
5558 -- Expand_Array_Aggregate that collapses others choices to positional
5559 -- form, but there are two problems with that circuit:
5561 -- a) It is limited to very small cases due to ill-understood
5562 -- interactions with bootstrapping. That limit is removed by
5563 -- use of the No_Implicit_Loops restriction.
5565 -- b) It incorrectly ends up with the resulting expressions being
5566 -- considered static when they are not. For example, the
5567 -- following test should fail:
5569 -- pragma Restrictions (No_Implicit_Loops);
5570 -- package NonSOthers4 is
5571 -- B : constant String (1 .. 6) := (others => 'A');
5572 -- DH : constant String (1 .. 8) := B & "BB";
5573 -- X : Integer;
5574 -- pragma Export (C, X, Link_Name => DH);
5575 -- end;
5577 -- But it succeeds (DH looks static to pragma Export)
5579 -- To be sorted out ???
5581 if Present (Component_Associations (N)) then
5582 declare
5583 CA : constant Node_Id := First (Component_Associations (N));
5584 MX : constant := 80;
5586 begin
5587 if Nkind (First (Choices (CA))) = N_Others_Choice
5588 and then Nkind (Expression (CA)) = N_Character_Literal
5589 and then No (Expressions (N))
5590 then
5591 declare
5592 T : constant Entity_Id := Etype (N);
5593 X : constant Node_Id := First_Index (T);
5594 EC : constant Node_Id := Expression (CA);
5595 CV : constant Uint := Char_Literal_Value (EC);
5596 CC : constant Int := UI_To_Int (CV);
5598 begin
5599 if Nkind (X) = N_Range
5600 and then Compile_Time_Known_Value (Low_Bound (X))
5601 and then Expr_Value (Low_Bound (X)) = 1
5602 and then Compile_Time_Known_Value (High_Bound (X))
5603 then
5604 declare
5605 Hi : constant Uint := Expr_Value (High_Bound (X));
5607 begin
5608 if Hi <= MX then
5609 Start_String;
5611 for J in 1 .. UI_To_Int (Hi) loop
5612 Store_String_Char (Char_Code (CC));
5613 end loop;
5615 Rewrite (N,
5616 Make_String_Literal (Sloc (N),
5617 Strval => End_String));
5619 if CC >= Int (2 ** 16) then
5620 Set_Has_Wide_Wide_Character (N);
5621 elsif CC >= Int (2 ** 8) then
5622 Set_Has_Wide_Character (N);
5623 end if;
5625 Analyze_And_Resolve (N, T);
5626 Set_Is_Static_Expression (N, False);
5627 return;
5628 end if;
5629 end;
5630 end if;
5631 end;
5632 end if;
5633 end;
5634 end if;
5636 -- Not that special case, so normal expansion of array aggregate
5638 Expand_Array_Aggregate (N);
5639 end if;
5641 exception
5642 when RE_Not_Available =>
5643 return;
5644 end Expand_N_Aggregate;
5646 ----------------------------------
5647 -- Expand_N_Extension_Aggregate --
5648 ----------------------------------
5650 -- If the ancestor part is an expression, add a component association for
5651 -- the parent field. If the type of the ancestor part is not the direct
5652 -- parent of the expected type, build recursively the needed ancestors.
5653 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5654 -- ration for a temporary of the expected type, followed by individual
5655 -- assignments to the given components.
5657 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5658 Loc : constant Source_Ptr := Sloc (N);
5659 A : constant Node_Id := Ancestor_Part (N);
5660 Typ : constant Entity_Id := Etype (N);
5662 begin
5663 -- If the ancestor is a subtype mark, an init proc must be called
5664 -- on the resulting object which thus has to be materialized in
5665 -- the front-end
5667 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5668 Convert_To_Assignments (N, Typ);
5670 -- The extension aggregate is transformed into a record aggregate
5671 -- of the following form (c1 and c2 are inherited components)
5673 -- (Exp with c3 => a, c4 => b)
5674 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5676 else
5677 Set_Etype (N, Typ);
5679 if Tagged_Type_Expansion then
5680 Expand_Record_Aggregate (N,
5681 Orig_Tag =>
5682 New_Occurrence_Of
5683 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5684 Parent_Expr => A);
5686 -- No tag is needed in the case of a VM
5688 else
5689 Expand_Record_Aggregate (N, Parent_Expr => A);
5690 end if;
5691 end if;
5693 exception
5694 when RE_Not_Available =>
5695 return;
5696 end Expand_N_Extension_Aggregate;
5698 -----------------------------
5699 -- Expand_Record_Aggregate --
5700 -----------------------------
5702 procedure Expand_Record_Aggregate
5703 (N : Node_Id;
5704 Orig_Tag : Node_Id := Empty;
5705 Parent_Expr : Node_Id := Empty)
5707 Loc : constant Source_Ptr := Sloc (N);
5708 Comps : constant List_Id := Component_Associations (N);
5709 Typ : constant Entity_Id := Etype (N);
5710 Base_Typ : constant Entity_Id := Base_Type (Typ);
5712 Static_Components : Boolean := True;
5713 -- Flag to indicate whether all components are compile-time known,
5714 -- and the aggregate can be constructed statically and handled by
5715 -- the back-end.
5717 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
5718 -- Returns true if N is an expression of composite type which can be
5719 -- fully evaluated at compile time without raising constraint error.
5720 -- Such expressions can be passed as is to Gigi without any expansion.
5722 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
5723 -- set and constants whose expression is such an aggregate, recursively.
5725 function Component_Not_OK_For_Backend return Boolean;
5726 -- Check for presence of a component which makes it impossible for the
5727 -- backend to process the aggregate, thus requiring the use of a series
5728 -- of assignment statements. Cases checked for are a nested aggregate
5729 -- needing Late_Expansion, the presence of a tagged component which may
5730 -- need tag adjustment, and a bit unaligned component reference.
5732 -- We also force expansion into assignments if a component is of a
5733 -- mutable type (including a private type with discriminants) because
5734 -- in that case the size of the component to be copied may be smaller
5735 -- than the side of the target, and there is no simple way for gigi
5736 -- to compute the size of the object to be copied.
5738 -- NOTE: This is part of the ongoing work to define precisely the
5739 -- interface between front-end and back-end handling of aggregates.
5740 -- In general it is desirable to pass aggregates as they are to gigi,
5741 -- in order to minimize elaboration code. This is one case where the
5742 -- semantics of Ada complicate the analysis and lead to anomalies in
5743 -- the gcc back-end if the aggregate is not expanded into assignments.
5745 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
5746 -- If any ancestor of the current type is private, the aggregate
5747 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
5748 -- because it will not be set when type and its parent are in the
5749 -- same scope, and the parent component needs expansion.
5751 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
5752 -- For nested aggregates return the ultimate enclosing aggregate; for
5753 -- non-nested aggregates return N.
5755 ----------------------------------------
5756 -- Compile_Time_Known_Composite_Value --
5757 ----------------------------------------
5759 function Compile_Time_Known_Composite_Value
5760 (N : Node_Id) return Boolean
5762 begin
5763 -- If we have an entity name, then see if it is the name of a
5764 -- constant and if so, test the corresponding constant value.
5766 if Is_Entity_Name (N) then
5767 declare
5768 E : constant Entity_Id := Entity (N);
5769 V : Node_Id;
5770 begin
5771 if Ekind (E) /= E_Constant then
5772 return False;
5773 else
5774 V := Constant_Value (E);
5775 return Present (V)
5776 and then Compile_Time_Known_Composite_Value (V);
5777 end if;
5778 end;
5780 -- We have a value, see if it is compile time known
5782 else
5783 if Nkind (N) = N_Aggregate then
5784 return Compile_Time_Known_Aggregate (N);
5785 end if;
5787 -- All other types of values are not known at compile time
5789 return False;
5790 end if;
5792 end Compile_Time_Known_Composite_Value;
5794 ----------------------------------
5795 -- Component_Not_OK_For_Backend --
5796 ----------------------------------
5798 function Component_Not_OK_For_Backend return Boolean is
5799 C : Node_Id;
5800 Expr_Q : Node_Id;
5802 begin
5803 if No (Comps) then
5804 return False;
5805 end if;
5807 C := First (Comps);
5808 while Present (C) loop
5810 -- If the component has box initialization, expansion is needed
5811 -- and component is not ready for backend.
5813 if Box_Present (C) then
5814 return True;
5815 end if;
5817 if Nkind (Expression (C)) = N_Qualified_Expression then
5818 Expr_Q := Expression (Expression (C));
5819 else
5820 Expr_Q := Expression (C);
5821 end if;
5823 -- Return true if the aggregate has any associations for tagged
5824 -- components that may require tag adjustment.
5826 -- These are cases where the source expression may have a tag that
5827 -- could differ from the component tag (e.g., can occur for type
5828 -- conversions and formal parameters). (Tag adjustment not needed
5829 -- if VM_Target because object tags are implicit in the machine.)
5831 if Is_Tagged_Type (Etype (Expr_Q))
5832 and then (Nkind (Expr_Q) = N_Type_Conversion
5833 or else (Is_Entity_Name (Expr_Q)
5834 and then
5835 Ekind (Entity (Expr_Q)) in Formal_Kind))
5836 and then Tagged_Type_Expansion
5837 then
5838 Static_Components := False;
5839 return True;
5841 elsif Is_Delayed_Aggregate (Expr_Q) then
5842 Static_Components := False;
5843 return True;
5845 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5846 Static_Components := False;
5847 return True;
5848 end if;
5850 if Is_Elementary_Type (Etype (Expr_Q)) then
5851 if not Compile_Time_Known_Value (Expr_Q) then
5852 Static_Components := False;
5853 end if;
5855 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
5856 Static_Components := False;
5858 if Is_Private_Type (Etype (Expr_Q))
5859 and then Has_Discriminants (Etype (Expr_Q))
5860 then
5861 return True;
5862 end if;
5863 end if;
5865 Next (C);
5866 end loop;
5868 return False;
5869 end Component_Not_OK_For_Backend;
5871 -----------------------------------
5872 -- Has_Visible_Private_Ancestor --
5873 -----------------------------------
5875 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
5876 R : constant Entity_Id := Root_Type (Id);
5877 T1 : Entity_Id := Id;
5879 begin
5880 loop
5881 if Is_Private_Type (T1) then
5882 return True;
5884 elsif T1 = R then
5885 return False;
5887 else
5888 T1 := Etype (T1);
5889 end if;
5890 end loop;
5891 end Has_Visible_Private_Ancestor;
5893 -------------------------
5894 -- Top_Level_Aggregate --
5895 -------------------------
5897 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
5898 Aggr : Node_Id;
5900 begin
5901 Aggr := N;
5902 while Present (Parent (Aggr))
5903 and then Nkind_In (Parent (Aggr), N_Component_Association,
5904 N_Aggregate)
5905 loop
5906 Aggr := Parent (Aggr);
5907 end loop;
5909 return Aggr;
5910 end Top_Level_Aggregate;
5912 -- Local variables
5914 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
5915 Tag_Value : Node_Id;
5916 Comp : Entity_Id;
5917 New_Comp : Node_Id;
5919 -- Start of processing for Expand_Record_Aggregate
5921 begin
5922 -- If the aggregate is to be assigned to an atomic variable, we have
5923 -- to prevent a piecemeal assignment even if the aggregate is to be
5924 -- expanded. We create a temporary for the aggregate, and assign the
5925 -- temporary instead, so that the back end can generate an atomic move
5926 -- for it.
5928 if Is_Atomic (Typ)
5929 and then Comes_From_Source (Parent (N))
5930 and then Is_Atomic_Aggregate (N, Typ)
5931 then
5932 return;
5934 -- No special management required for aggregates used to initialize
5935 -- statically allocated dispatch tables
5937 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5938 return;
5939 end if;
5941 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5942 -- are build-in-place function calls. The assignments will each turn
5943 -- into a build-in-place function call. If components are all static,
5944 -- we can pass the aggregate to the backend regardless of limitedness.
5946 -- Extension aggregates, aggregates in extended return statements, and
5947 -- aggregates for C++ imported types must be expanded.
5949 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
5950 if not Nkind_In (Parent (N), N_Object_Declaration,
5951 N_Component_Association)
5952 then
5953 Convert_To_Assignments (N, Typ);
5955 elsif Nkind (N) = N_Extension_Aggregate
5956 or else Convention (Typ) = Convention_CPP
5957 then
5958 Convert_To_Assignments (N, Typ);
5960 elsif not Size_Known_At_Compile_Time (Typ)
5961 or else Component_Not_OK_For_Backend
5962 or else not Static_Components
5963 then
5964 Convert_To_Assignments (N, Typ);
5966 else
5967 Set_Compile_Time_Known_Aggregate (N);
5968 Set_Expansion_Delayed (N, False);
5969 end if;
5971 -- Gigi doesn't properly handle temporaries of variable size so we
5972 -- generate it in the front-end
5974 elsif not Size_Known_At_Compile_Time (Typ)
5975 and then Tagged_Type_Expansion
5976 then
5977 Convert_To_Assignments (N, Typ);
5979 -- An aggregate used to initialize a controlled object must be turned
5980 -- into component assignments as the components themselves may require
5981 -- finalization actions such as adjustment.
5983 elsif Needs_Finalization (Typ) then
5984 Convert_To_Assignments (N, Typ);
5986 -- Ada 2005 (AI-287): In case of default initialized components we
5987 -- convert the aggregate into assignments.
5989 elsif Has_Default_Init_Comps (N) then
5990 Convert_To_Assignments (N, Typ);
5992 -- Check components
5994 elsif Component_Not_OK_For_Backend then
5995 Convert_To_Assignments (N, Typ);
5997 -- If an ancestor is private, some components are not inherited and we
5998 -- cannot expand into a record aggregate.
6000 elsif Has_Visible_Private_Ancestor (Typ) then
6001 Convert_To_Assignments (N, Typ);
6003 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
6004 -- is not able to handle the aggregate for Late_Request.
6006 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
6007 Convert_To_Assignments (N, Typ);
6009 -- If the tagged types covers interface types we need to initialize all
6010 -- hidden components containing pointers to secondary dispatch tables.
6012 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
6013 Convert_To_Assignments (N, Typ);
6015 -- If some components are mutable, the size of the aggregate component
6016 -- may be distinct from the default size of the type component, so
6017 -- we need to expand to insure that the back-end copies the proper
6018 -- size of the data. However, if the aggregate is the initial value of
6019 -- a constant, the target is immutable and might be built statically
6020 -- if components are appropriate.
6022 elsif Has_Mutable_Components (Typ)
6023 and then
6024 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
6025 or else not Constant_Present (Parent (Top_Level_Aggr))
6026 or else not Static_Components)
6027 then
6028 Convert_To_Assignments (N, Typ);
6030 -- If the type involved has bit aligned components, then we are not sure
6031 -- that the back end can handle this case correctly.
6033 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
6034 Convert_To_Assignments (N, Typ);
6036 -- In all other cases, build a proper aggregate to be handled by gigi
6038 else
6039 if Nkind (N) = N_Aggregate then
6041 -- If the aggregate is static and can be handled by the back-end,
6042 -- nothing left to do.
6044 if Static_Components then
6045 Set_Compile_Time_Known_Aggregate (N);
6046 Set_Expansion_Delayed (N, False);
6047 end if;
6048 end if;
6050 -- If no discriminants, nothing special to do
6052 if not Has_Discriminants (Typ) then
6053 null;
6055 -- Case of discriminants present
6057 elsif Is_Derived_Type (Typ) then
6059 -- For untagged types, non-stored discriminants are replaced
6060 -- with stored discriminants, which are the ones that gigi uses
6061 -- to describe the type and its components.
6063 Generate_Aggregate_For_Derived_Type : declare
6064 Constraints : constant List_Id := New_List;
6065 First_Comp : Node_Id;
6066 Discriminant : Entity_Id;
6067 Decl : Node_Id;
6068 Num_Disc : Int := 0;
6069 Num_Gird : Int := 0;
6071 procedure Prepend_Stored_Values (T : Entity_Id);
6072 -- Scan the list of stored discriminants of the type, and add
6073 -- their values to the aggregate being built.
6075 ---------------------------
6076 -- Prepend_Stored_Values --
6077 ---------------------------
6079 procedure Prepend_Stored_Values (T : Entity_Id) is
6080 begin
6081 Discriminant := First_Stored_Discriminant (T);
6082 while Present (Discriminant) loop
6083 New_Comp :=
6084 Make_Component_Association (Loc,
6085 Choices =>
6086 New_List (New_Occurrence_Of (Discriminant, Loc)),
6088 Expression =>
6089 New_Copy_Tree
6090 (Get_Discriminant_Value
6091 (Discriminant,
6092 Typ,
6093 Discriminant_Constraint (Typ))));
6095 if No (First_Comp) then
6096 Prepend_To (Component_Associations (N), New_Comp);
6097 else
6098 Insert_After (First_Comp, New_Comp);
6099 end if;
6101 First_Comp := New_Comp;
6102 Next_Stored_Discriminant (Discriminant);
6103 end loop;
6104 end Prepend_Stored_Values;
6106 -- Start of processing for Generate_Aggregate_For_Derived_Type
6108 begin
6109 -- Remove the associations for the discriminant of derived type
6111 First_Comp := First (Component_Associations (N));
6112 while Present (First_Comp) loop
6113 Comp := First_Comp;
6114 Next (First_Comp);
6116 if Ekind (Entity (First (Choices (Comp)))) = E_Discriminant
6117 then
6118 Remove (Comp);
6119 Num_Disc := Num_Disc + 1;
6120 end if;
6121 end loop;
6123 -- Insert stored discriminant associations in the correct
6124 -- order. If there are more stored discriminants than new
6125 -- discriminants, there is at least one new discriminant that
6126 -- constrains more than one of the stored discriminants. In
6127 -- this case we need to construct a proper subtype of the
6128 -- parent type, in order to supply values to all the
6129 -- components. Otherwise there is one-one correspondence
6130 -- between the constraints and the stored discriminants.
6132 First_Comp := Empty;
6134 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6135 while Present (Discriminant) loop
6136 Num_Gird := Num_Gird + 1;
6137 Next_Stored_Discriminant (Discriminant);
6138 end loop;
6140 -- Case of more stored discriminants than new discriminants
6142 if Num_Gird > Num_Disc then
6144 -- Create a proper subtype of the parent type, which is the
6145 -- proper implementation type for the aggregate, and convert
6146 -- it to the intended target type.
6148 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6149 while Present (Discriminant) loop
6150 New_Comp :=
6151 New_Copy_Tree
6152 (Get_Discriminant_Value
6153 (Discriminant,
6154 Typ,
6155 Discriminant_Constraint (Typ)));
6156 Append (New_Comp, Constraints);
6157 Next_Stored_Discriminant (Discriminant);
6158 end loop;
6160 Decl :=
6161 Make_Subtype_Declaration (Loc,
6162 Defining_Identifier => Make_Temporary (Loc, 'T'),
6163 Subtype_Indication =>
6164 Make_Subtype_Indication (Loc,
6165 Subtype_Mark =>
6166 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
6167 Constraint =>
6168 Make_Index_Or_Discriminant_Constraint
6169 (Loc, Constraints)));
6171 Insert_Action (N, Decl);
6172 Prepend_Stored_Values (Base_Type (Typ));
6174 Set_Etype (N, Defining_Identifier (Decl));
6175 Set_Analyzed (N);
6177 Rewrite (N, Unchecked_Convert_To (Typ, N));
6178 Analyze (N);
6180 -- Case where we do not have fewer new discriminants than
6181 -- stored discriminants, so in this case we can simply use the
6182 -- stored discriminants of the subtype.
6184 else
6185 Prepend_Stored_Values (Typ);
6186 end if;
6187 end Generate_Aggregate_For_Derived_Type;
6188 end if;
6190 if Is_Tagged_Type (Typ) then
6192 -- In the tagged case, _parent and _tag component must be created
6194 -- Reset Null_Present unconditionally. Tagged records always have
6195 -- at least one field (the tag or the parent).
6197 Set_Null_Record_Present (N, False);
6199 -- When the current aggregate comes from the expansion of an
6200 -- extension aggregate, the parent expr is replaced by an
6201 -- aggregate formed by selected components of this expr.
6203 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
6204 Comp := First_Component_Or_Discriminant (Typ);
6205 while Present (Comp) loop
6207 -- Skip all expander-generated components
6209 if not Comes_From_Source (Original_Record_Component (Comp))
6210 then
6211 null;
6213 else
6214 New_Comp :=
6215 Make_Selected_Component (Loc,
6216 Prefix =>
6217 Unchecked_Convert_To (Typ,
6218 Duplicate_Subexpr (Parent_Expr, True)),
6219 Selector_Name => New_Occurrence_Of (Comp, Loc));
6221 Append_To (Comps,
6222 Make_Component_Association (Loc,
6223 Choices =>
6224 New_List (New_Occurrence_Of (Comp, Loc)),
6225 Expression => New_Comp));
6227 Analyze_And_Resolve (New_Comp, Etype (Comp));
6228 end if;
6230 Next_Component_Or_Discriminant (Comp);
6231 end loop;
6232 end if;
6234 -- Compute the value for the Tag now, if the type is a root it
6235 -- will be included in the aggregate right away, otherwise it will
6236 -- be propagated to the parent aggregate.
6238 if Present (Orig_Tag) then
6239 Tag_Value := Orig_Tag;
6240 elsif not Tagged_Type_Expansion then
6241 Tag_Value := Empty;
6242 else
6243 Tag_Value :=
6244 New_Occurrence_Of
6245 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6246 end if;
6248 -- For a derived type, an aggregate for the parent is formed with
6249 -- all the inherited components.
6251 if Is_Derived_Type (Typ) then
6253 declare
6254 First_Comp : Node_Id;
6255 Parent_Comps : List_Id;
6256 Parent_Aggr : Node_Id;
6257 Parent_Name : Node_Id;
6259 begin
6260 -- Remove the inherited component association from the
6261 -- aggregate and store them in the parent aggregate
6263 First_Comp := First (Component_Associations (N));
6264 Parent_Comps := New_List;
6265 while Present (First_Comp)
6266 and then
6267 Scope (Original_Record_Component
6268 (Entity (First (Choices (First_Comp))))) /=
6269 Base_Typ
6270 loop
6271 Comp := First_Comp;
6272 Next (First_Comp);
6273 Remove (Comp);
6274 Append (Comp, Parent_Comps);
6275 end loop;
6277 Parent_Aggr :=
6278 Make_Aggregate (Loc,
6279 Component_Associations => Parent_Comps);
6280 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
6282 -- Find the _parent component
6284 Comp := First_Component (Typ);
6285 while Chars (Comp) /= Name_uParent loop
6286 Comp := Next_Component (Comp);
6287 end loop;
6289 Parent_Name := New_Occurrence_Of (Comp, Loc);
6291 -- Insert the parent aggregate
6293 Prepend_To (Component_Associations (N),
6294 Make_Component_Association (Loc,
6295 Choices => New_List (Parent_Name),
6296 Expression => Parent_Aggr));
6298 -- Expand recursively the parent propagating the right Tag
6300 Expand_Record_Aggregate
6301 (Parent_Aggr, Tag_Value, Parent_Expr);
6303 -- The ancestor part may be a nested aggregate that has
6304 -- delayed expansion: recheck now.
6306 if Component_Not_OK_For_Backend then
6307 Convert_To_Assignments (N, Typ);
6308 end if;
6309 end;
6311 -- For a root type, the tag component is added (unless compiling
6312 -- for the VMs, where tags are implicit).
6314 elsif Tagged_Type_Expansion then
6315 declare
6316 Tag_Name : constant Node_Id :=
6317 New_Occurrence_Of (First_Tag_Component (Typ), Loc);
6318 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
6319 Conv_Node : constant Node_Id :=
6320 Unchecked_Convert_To (Typ_Tag, Tag_Value);
6322 begin
6323 Set_Etype (Conv_Node, Typ_Tag);
6324 Prepend_To (Component_Associations (N),
6325 Make_Component_Association (Loc,
6326 Choices => New_List (Tag_Name),
6327 Expression => Conv_Node));
6328 end;
6329 end if;
6330 end if;
6331 end if;
6333 end Expand_Record_Aggregate;
6335 ----------------------------
6336 -- Has_Default_Init_Comps --
6337 ----------------------------
6339 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
6340 Comps : constant List_Id := Component_Associations (N);
6341 C : Node_Id;
6342 Expr : Node_Id;
6344 begin
6345 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
6347 if No (Comps) then
6348 return False;
6349 end if;
6351 if Has_Self_Reference (N) then
6352 return True;
6353 end if;
6355 -- Check if any direct component has default initialized components
6357 C := First (Comps);
6358 while Present (C) loop
6359 if Box_Present (C) then
6360 return True;
6361 end if;
6363 Next (C);
6364 end loop;
6366 -- Recursive call in case of aggregate expression
6368 C := First (Comps);
6369 while Present (C) loop
6370 Expr := Expression (C);
6372 if Present (Expr)
6373 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
6374 and then Has_Default_Init_Comps (Expr)
6375 then
6376 return True;
6377 end if;
6379 Next (C);
6380 end loop;
6382 return False;
6383 end Has_Default_Init_Comps;
6385 --------------------------
6386 -- Is_Delayed_Aggregate --
6387 --------------------------
6389 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
6390 Node : Node_Id := N;
6391 Kind : Node_Kind := Nkind (Node);
6393 begin
6394 if Kind = N_Qualified_Expression then
6395 Node := Expression (Node);
6396 Kind := Nkind (Node);
6397 end if;
6399 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
6400 return False;
6401 else
6402 return Expansion_Delayed (Node);
6403 end if;
6404 end Is_Delayed_Aggregate;
6406 ----------------------------------------
6407 -- Is_Static_Dispatch_Table_Aggregate --
6408 ----------------------------------------
6410 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
6411 Typ : constant Entity_Id := Base_Type (Etype (N));
6413 begin
6414 return Static_Dispatch_Tables
6415 and then Tagged_Type_Expansion
6416 and then RTU_Loaded (Ada_Tags)
6418 -- Avoid circularity when rebuilding the compiler
6420 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
6421 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
6422 or else
6423 Typ = RTE (RE_Address_Array)
6424 or else
6425 Typ = RTE (RE_Type_Specific_Data)
6426 or else
6427 Typ = RTE (RE_Tag_Table)
6428 or else
6429 (RTE_Available (RE_Interface_Data)
6430 and then Typ = RTE (RE_Interface_Data))
6431 or else
6432 (RTE_Available (RE_Interfaces_Array)
6433 and then Typ = RTE (RE_Interfaces_Array))
6434 or else
6435 (RTE_Available (RE_Interface_Data_Element)
6436 and then Typ = RTE (RE_Interface_Data_Element)));
6437 end Is_Static_Dispatch_Table_Aggregate;
6439 -----------------------------
6440 -- Is_Two_Dim_Packed_Array --
6441 -----------------------------
6443 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
6444 C : constant Int := UI_To_Int (Component_Size (Typ));
6445 begin
6446 return Number_Dimensions (Typ) = 2
6447 and then Is_Bit_Packed_Array (Typ)
6448 and then (C = 1 or else C = 2 or else C = 4);
6449 end Is_Two_Dim_Packed_Array;
6451 --------------------
6452 -- Late_Expansion --
6453 --------------------
6455 function Late_Expansion
6456 (N : Node_Id;
6457 Typ : Entity_Id;
6458 Target : Node_Id) return List_Id
6460 Aggr_Code : List_Id;
6462 begin
6463 if Is_Record_Type (Etype (N)) then
6464 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
6466 else pragma Assert (Is_Array_Type (Etype (N)));
6467 Aggr_Code :=
6468 Build_Array_Aggr_Code
6469 (N => N,
6470 Ctype => Component_Type (Etype (N)),
6471 Index => First_Index (Typ),
6472 Into => Target,
6473 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
6474 Indexes => No_List);
6475 end if;
6477 -- Save the last assignment statement associated with the aggregate
6478 -- when building a controlled object. This reference is utilized by
6479 -- the finalization machinery when marking an object as successfully
6480 -- initialized.
6482 if Needs_Finalization (Typ)
6483 and then Is_Entity_Name (Target)
6484 and then Present (Entity (Target))
6485 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6486 then
6487 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6488 end if;
6490 return Aggr_Code;
6491 end Late_Expansion;
6493 ----------------------------------
6494 -- Make_OK_Assignment_Statement --
6495 ----------------------------------
6497 function Make_OK_Assignment_Statement
6498 (Sloc : Source_Ptr;
6499 Name : Node_Id;
6500 Expression : Node_Id) return Node_Id
6502 begin
6503 Set_Assignment_OK (Name);
6504 return Make_Assignment_Statement (Sloc, Name, Expression);
6505 end Make_OK_Assignment_Statement;
6507 -----------------------
6508 -- Number_Of_Choices --
6509 -----------------------
6511 function Number_Of_Choices (N : Node_Id) return Nat is
6512 Assoc : Node_Id;
6513 Choice : Node_Id;
6515 Nb_Choices : Nat := 0;
6517 begin
6518 if Present (Expressions (N)) then
6519 return 0;
6520 end if;
6522 Assoc := First (Component_Associations (N));
6523 while Present (Assoc) loop
6524 Choice := First (Choices (Assoc));
6525 while Present (Choice) loop
6526 if Nkind (Choice) /= N_Others_Choice then
6527 Nb_Choices := Nb_Choices + 1;
6528 end if;
6530 Next (Choice);
6531 end loop;
6533 Next (Assoc);
6534 end loop;
6536 return Nb_Choices;
6537 end Number_Of_Choices;
6539 ------------------------------------
6540 -- Packed_Array_Aggregate_Handled --
6541 ------------------------------------
6543 -- The current version of this procedure will handle at compile time
6544 -- any array aggregate that meets these conditions:
6546 -- One and two dimensional, bit packed
6547 -- Underlying packed type is modular type
6548 -- Bounds are within 32-bit Int range
6549 -- All bounds and values are static
6551 -- Note: for now, in the 2-D case, we only handle component sizes of
6552 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
6554 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6555 Loc : constant Source_Ptr := Sloc (N);
6556 Typ : constant Entity_Id := Etype (N);
6557 Ctyp : constant Entity_Id := Component_Type (Typ);
6559 Not_Handled : exception;
6560 -- Exception raised if this aggregate cannot be handled
6562 begin
6563 -- Handle one- or two dimensional bit packed array
6565 if not Is_Bit_Packed_Array (Typ)
6566 or else Number_Dimensions (Typ) > 2
6567 then
6568 return False;
6569 end if;
6571 -- If two-dimensional, check whether it can be folded, and transformed
6572 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
6573 -- the original type.
6575 if Number_Dimensions (Typ) = 2 then
6576 return Two_Dim_Packed_Array_Handled (N);
6577 end if;
6579 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
6580 return False;
6581 end if;
6583 if not Is_Scalar_Type (Component_Type (Typ))
6584 and then Has_Non_Standard_Rep (Component_Type (Typ))
6585 then
6586 return False;
6587 end if;
6589 declare
6590 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6592 Lo : Node_Id;
6593 Hi : Node_Id;
6594 -- Bounds of index type
6596 Lob : Uint;
6597 Hib : Uint;
6598 -- Values of bounds if compile time known
6600 function Get_Component_Val (N : Node_Id) return Uint;
6601 -- Given a expression value N of the component type Ctyp, returns a
6602 -- value of Csiz (component size) bits representing this value. If
6603 -- the value is non-static or any other reason exists why the value
6604 -- cannot be returned, then Not_Handled is raised.
6606 -----------------------
6607 -- Get_Component_Val --
6608 -----------------------
6610 function Get_Component_Val (N : Node_Id) return Uint is
6611 Val : Uint;
6613 begin
6614 -- We have to analyze the expression here before doing any further
6615 -- processing here. The analysis of such expressions is deferred
6616 -- till expansion to prevent some problems of premature analysis.
6618 Analyze_And_Resolve (N, Ctyp);
6620 -- Must have a compile time value. String literals have to be
6621 -- converted into temporaries as well, because they cannot easily
6622 -- be converted into their bit representation.
6624 if not Compile_Time_Known_Value (N)
6625 or else Nkind (N) = N_String_Literal
6626 then
6627 raise Not_Handled;
6628 end if;
6630 Val := Expr_Rep_Value (N);
6632 -- Adjust for bias, and strip proper number of bits
6634 if Has_Biased_Representation (Ctyp) then
6635 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6636 end if;
6638 return Val mod Uint_2 ** Csiz;
6639 end Get_Component_Val;
6641 -- Here we know we have a one dimensional bit packed array
6643 begin
6644 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6646 -- Cannot do anything if bounds are dynamic
6648 if not Compile_Time_Known_Value (Lo)
6649 or else
6650 not Compile_Time_Known_Value (Hi)
6651 then
6652 return False;
6653 end if;
6655 -- Or are silly out of range of int bounds
6657 Lob := Expr_Value (Lo);
6658 Hib := Expr_Value (Hi);
6660 if not UI_Is_In_Int_Range (Lob)
6661 or else
6662 not UI_Is_In_Int_Range (Hib)
6663 then
6664 return False;
6665 end if;
6667 -- At this stage we have a suitable aggregate for handling at compile
6668 -- time. The only remaining checks are that the values of expressions
6669 -- in the aggregate are compile-time known (checks are performed by
6670 -- Get_Component_Val), and that any subtypes or ranges are statically
6671 -- known.
6673 -- If the aggregate is not fully positional at this stage, then
6674 -- convert it to positional form. Either this will fail, in which
6675 -- case we can do nothing, or it will succeed, in which case we have
6676 -- succeeded in handling the aggregate and transforming it into a
6677 -- modular value, or it will stay an aggregate, in which case we
6678 -- have failed to create a packed value for it.
6680 if Present (Component_Associations (N)) then
6681 Convert_To_Positional
6682 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6683 return Nkind (N) /= N_Aggregate;
6684 end if;
6686 -- Otherwise we are all positional, so convert to proper value
6688 declare
6689 Lov : constant Int := UI_To_Int (Lob);
6690 Hiv : constant Int := UI_To_Int (Hib);
6692 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6693 -- The length of the array (number of elements)
6695 Aggregate_Val : Uint;
6696 -- Value of aggregate. The value is set in the low order bits of
6697 -- this value. For the little-endian case, the values are stored
6698 -- from low-order to high-order and for the big-endian case the
6699 -- values are stored from high-order to low-order. Note that gigi
6700 -- will take care of the conversions to left justify the value in
6701 -- the big endian case (because of left justified modular type
6702 -- processing), so we do not have to worry about that here.
6704 Lit : Node_Id;
6705 -- Integer literal for resulting constructed value
6707 Shift : Nat;
6708 -- Shift count from low order for next value
6710 Incr : Int;
6711 -- Shift increment for loop
6713 Expr : Node_Id;
6714 -- Next expression from positional parameters of aggregate
6716 Left_Justified : Boolean;
6717 -- Set True if we are filling the high order bits of the target
6718 -- value (i.e. the value is left justified).
6720 begin
6721 -- For little endian, we fill up the low order bits of the target
6722 -- value. For big endian we fill up the high order bits of the
6723 -- target value (which is a left justified modular value).
6725 Left_Justified := Bytes_Big_Endian;
6727 -- Switch justification if using -gnatd8
6729 if Debug_Flag_8 then
6730 Left_Justified := not Left_Justified;
6731 end if;
6733 -- Switch justfification if reverse storage order
6735 if Reverse_Storage_Order (Base_Type (Typ)) then
6736 Left_Justified := not Left_Justified;
6737 end if;
6739 if Left_Justified then
6740 Shift := Csiz * (Len - 1);
6741 Incr := -Csiz;
6742 else
6743 Shift := 0;
6744 Incr := +Csiz;
6745 end if;
6747 -- Loop to set the values
6749 if Len = 0 then
6750 Aggregate_Val := Uint_0;
6751 else
6752 Expr := First (Expressions (N));
6753 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6755 for J in 2 .. Len loop
6756 Shift := Shift + Incr;
6757 Next (Expr);
6758 Aggregate_Val :=
6759 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6760 end loop;
6761 end if;
6763 -- Now we can rewrite with the proper value
6765 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
6766 Set_Print_In_Hex (Lit);
6768 -- Construct the expression using this literal. Note that it is
6769 -- important to qualify the literal with its proper modular type
6770 -- since universal integer does not have the required range and
6771 -- also this is a left justified modular type, which is important
6772 -- in the big-endian case.
6774 Rewrite (N,
6775 Unchecked_Convert_To (Typ,
6776 Make_Qualified_Expression (Loc,
6777 Subtype_Mark =>
6778 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
6779 Expression => Lit)));
6781 Analyze_And_Resolve (N, Typ);
6782 return True;
6783 end;
6784 end;
6786 exception
6787 when Not_Handled =>
6788 return False;
6789 end Packed_Array_Aggregate_Handled;
6791 ----------------------------
6792 -- Has_Mutable_Components --
6793 ----------------------------
6795 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6796 Comp : Entity_Id;
6798 begin
6799 Comp := First_Component (Typ);
6800 while Present (Comp) loop
6801 if Is_Record_Type (Etype (Comp))
6802 and then Has_Discriminants (Etype (Comp))
6803 and then not Is_Constrained (Etype (Comp))
6804 then
6805 return True;
6806 end if;
6808 Next_Component (Comp);
6809 end loop;
6811 return False;
6812 end Has_Mutable_Components;
6814 ------------------------------
6815 -- Initialize_Discriminants --
6816 ------------------------------
6818 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6819 Loc : constant Source_Ptr := Sloc (N);
6820 Bas : constant Entity_Id := Base_Type (Typ);
6821 Par : constant Entity_Id := Etype (Bas);
6822 Decl : constant Node_Id := Parent (Par);
6823 Ref : Node_Id;
6825 begin
6826 if Is_Tagged_Type (Bas)
6827 and then Is_Derived_Type (Bas)
6828 and then Has_Discriminants (Par)
6829 and then Has_Discriminants (Bas)
6830 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6831 and then Nkind (Decl) = N_Full_Type_Declaration
6832 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6833 and then
6834 Present (Variant_Part (Component_List (Type_Definition (Decl))))
6835 and then Nkind (N) /= N_Extension_Aggregate
6836 then
6838 -- Call init proc to set discriminants.
6839 -- There should eventually be a special procedure for this ???
6841 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
6842 Insert_Actions_After (N,
6843 Build_Initialization_Call (Sloc (N), Ref, Typ));
6844 end if;
6845 end Initialize_Discriminants;
6847 ----------------
6848 -- Must_Slide --
6849 ----------------
6851 function Must_Slide
6852 (Obj_Type : Entity_Id;
6853 Typ : Entity_Id) return Boolean
6855 L1, L2, H1, H2 : Node_Id;
6857 begin
6858 -- No sliding if the type of the object is not established yet, if it is
6859 -- an unconstrained type whose actual subtype comes from the aggregate,
6860 -- or if the two types are identical.
6862 if not Is_Array_Type (Obj_Type) then
6863 return False;
6865 elsif not Is_Constrained (Obj_Type) then
6866 return False;
6868 elsif Typ = Obj_Type then
6869 return False;
6871 else
6872 -- Sliding can only occur along the first dimension
6874 Get_Index_Bounds (First_Index (Typ), L1, H1);
6875 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6877 if not Is_OK_Static_Expression (L1) or else
6878 not Is_OK_Static_Expression (L2) or else
6879 not Is_OK_Static_Expression (H1) or else
6880 not Is_OK_Static_Expression (H2)
6881 then
6882 return False;
6883 else
6884 return Expr_Value (L1) /= Expr_Value (L2)
6885 or else
6886 Expr_Value (H1) /= Expr_Value (H2);
6887 end if;
6888 end if;
6889 end Must_Slide;
6891 ----------------------------------
6892 -- Two_Dim_Packed_Array_Handled --
6893 ----------------------------------
6895 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
6896 Loc : constant Source_Ptr := Sloc (N);
6897 Typ : constant Entity_Id := Etype (N);
6898 Ctyp : constant Entity_Id := Component_Type (Typ);
6899 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
6900 Packed_Array : constant Entity_Id :=
6901 Packed_Array_Impl_Type (Base_Type (Typ));
6903 One_Comp : Node_Id;
6904 -- Expression in original aggregate
6906 One_Dim : Node_Id;
6907 -- One-dimensional subaggregate
6909 begin
6911 -- For now, only deal with cases where an integral number of elements
6912 -- fit in a single byte. This includes the most common boolean case.
6914 if not (Comp_Size = 1 or else
6915 Comp_Size = 2 or else
6916 Comp_Size = 4)
6917 then
6918 return False;
6919 end if;
6921 Convert_To_Positional
6922 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6924 -- Verify that all components are static
6926 if Nkind (N) = N_Aggregate
6927 and then Compile_Time_Known_Aggregate (N)
6928 then
6929 null;
6931 -- The aggregate may have been re-analyzed and converted already
6933 elsif Nkind (N) /= N_Aggregate then
6934 return True;
6936 -- If component associations remain, the aggregate is not static
6938 elsif Present (Component_Associations (N)) then
6939 return False;
6941 else
6942 One_Dim := First (Expressions (N));
6943 while Present (One_Dim) loop
6944 if Present (Component_Associations (One_Dim)) then
6945 return False;
6946 end if;
6948 One_Comp := First (Expressions (One_Dim));
6949 while Present (One_Comp) loop
6950 if not Is_OK_Static_Expression (One_Comp) then
6951 return False;
6952 end if;
6954 Next (One_Comp);
6955 end loop;
6957 Next (One_Dim);
6958 end loop;
6959 end if;
6961 -- Two-dimensional aggregate is now fully positional so pack one
6962 -- dimension to create a static one-dimensional array, and rewrite
6963 -- as an unchecked conversion to the original type.
6965 declare
6966 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
6967 -- The packed array type is a byte array
6969 Packed_Num : Int;
6970 -- Number of components accumulated in current byte
6972 Comps : List_Id;
6973 -- Assembled list of packed values for equivalent aggregate
6975 Comp_Val : Uint;
6976 -- integer value of component
6978 Incr : Int;
6979 -- Step size for packing
6981 Init_Shift : Int;
6982 -- Endian-dependent start position for packing
6984 Shift : Int;
6985 -- Current insertion position
6987 Val : Int;
6988 -- Component of packed array being assembled.
6990 begin
6991 Comps := New_List;
6992 Val := 0;
6993 Packed_Num := 0;
6995 -- Account for endianness. See corresponding comment in
6996 -- Packed_Array_Aggregate_Handled concerning the following.
6998 if Bytes_Big_Endian
6999 xor Debug_Flag_8
7000 xor Reverse_Storage_Order (Base_Type (Typ))
7001 then
7002 Init_Shift := Byte_Size - Comp_Size;
7003 Incr := -Comp_Size;
7004 else
7005 Init_Shift := 0;
7006 Incr := +Comp_Size;
7007 end if;
7009 -- Iterate over each subaggregate
7011 Shift := Init_Shift;
7012 One_Dim := First (Expressions (N));
7013 while Present (One_Dim) loop
7014 One_Comp := First (Expressions (One_Dim));
7015 while Present (One_Comp) loop
7016 if Packed_Num = Byte_Size / Comp_Size then
7018 -- Byte is complete, add to list of expressions
7020 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7021 Val := 0;
7022 Shift := Init_Shift;
7023 Packed_Num := 0;
7025 else
7026 Comp_Val := Expr_Rep_Value (One_Comp);
7028 -- Adjust for bias, and strip proper number of bits
7030 if Has_Biased_Representation (Ctyp) then
7031 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
7032 end if;
7034 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
7035 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
7036 Shift := Shift + Incr;
7037 One_Comp := Next (One_Comp);
7038 Packed_Num := Packed_Num + 1;
7039 end if;
7040 end loop;
7042 One_Dim := Next (One_Dim);
7043 end loop;
7045 if Packed_Num > 0 then
7047 -- Add final incomplete byte if present
7049 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7050 end if;
7052 Rewrite (N,
7053 Unchecked_Convert_To (Typ,
7054 Make_Qualified_Expression (Loc,
7055 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
7056 Expression => Make_Aggregate (Loc, Expressions => Comps))));
7057 Analyze_And_Resolve (N);
7058 return True;
7059 end;
7060 end Two_Dim_Packed_Array_Handled;
7062 ---------------------
7063 -- Sort_Case_Table --
7064 ---------------------
7066 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
7067 L : constant Int := Case_Table'First;
7068 U : constant Int := Case_Table'Last;
7069 K : Int;
7070 J : Int;
7071 T : Case_Bounds;
7073 begin
7074 K := L;
7075 while K /= U loop
7076 T := Case_Table (K + 1);
7078 J := K + 1;
7079 while J /= L
7080 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
7081 Expr_Value (T.Choice_Lo)
7082 loop
7083 Case_Table (J) := Case_Table (J - 1);
7084 J := J - 1;
7085 end loop;
7087 Case_Table (J) := T;
7088 K := K + 1;
7089 end loop;
7090 end Sort_Case_Table;
7092 ----------------------------
7093 -- Static_Array_Aggregate --
7094 ----------------------------
7096 function Static_Array_Aggregate (N : Node_Id) return Boolean is
7097 Bounds : constant Node_Id := Aggregate_Bounds (N);
7099 Typ : constant Entity_Id := Etype (N);
7100 Comp_Type : constant Entity_Id := Component_Type (Typ);
7101 Agg : Node_Id;
7102 Expr : Node_Id;
7103 Lo : Node_Id;
7104 Hi : Node_Id;
7106 begin
7107 if Is_Tagged_Type (Typ)
7108 or else Is_Controlled (Typ)
7109 or else Is_Packed (Typ)
7110 then
7111 return False;
7112 end if;
7114 if Present (Bounds)
7115 and then Nkind (Bounds) = N_Range
7116 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
7117 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
7118 then
7119 Lo := Low_Bound (Bounds);
7120 Hi := High_Bound (Bounds);
7122 if No (Component_Associations (N)) then
7124 -- Verify that all components are static integers
7126 Expr := First (Expressions (N));
7127 while Present (Expr) loop
7128 if Nkind (Expr) /= N_Integer_Literal then
7129 return False;
7130 end if;
7132 Next (Expr);
7133 end loop;
7135 return True;
7137 else
7138 -- We allow only a single named association, either a static
7139 -- range or an others_clause, with a static expression.
7141 Expr := First (Component_Associations (N));
7143 if Present (Expressions (N)) then
7144 return False;
7146 elsif Present (Next (Expr)) then
7147 return False;
7149 elsif Present (Next (First (Choices (Expr)))) then
7150 return False;
7152 else
7153 -- The aggregate is static if all components are literals,
7154 -- or else all its components are static aggregates for the
7155 -- component type. We also limit the size of a static aggregate
7156 -- to prevent runaway static expressions.
7158 if Is_Array_Type (Comp_Type)
7159 or else Is_Record_Type (Comp_Type)
7160 then
7161 if Nkind (Expression (Expr)) /= N_Aggregate
7162 or else
7163 not Compile_Time_Known_Aggregate (Expression (Expr))
7164 then
7165 return False;
7166 end if;
7168 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
7169 return False;
7170 end if;
7172 if not Aggr_Size_OK (N, Typ) then
7173 return False;
7174 end if;
7176 -- Create a positional aggregate with the right number of
7177 -- copies of the expression.
7179 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
7181 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
7182 loop
7183 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
7185 -- The copied expression must be analyzed and resolved.
7186 -- Besides setting the type, this ensures that static
7187 -- expressions are appropriately marked as such.
7189 Analyze_And_Resolve
7190 (Last (Expressions (Agg)), Component_Type (Typ));
7191 end loop;
7193 Set_Aggregate_Bounds (Agg, Bounds);
7194 Set_Etype (Agg, Typ);
7195 Set_Analyzed (Agg);
7196 Rewrite (N, Agg);
7197 Set_Compile_Time_Known_Aggregate (N);
7199 return True;
7200 end if;
7201 end if;
7203 else
7204 return False;
7205 end if;
7206 end Static_Array_Aggregate;
7208 end Exp_Aggr;