2015-01-06 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / exp_aggr.adb
blobf958c152b6e820a400c9cf086da19e4e3968ba36
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 useful to have a higher default for Max_Others_Replicate,
243 -- but aggregates in the compiler make this impossible: the compiler
244 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
245 -- 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 Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
789 -- For an association with a box, use default aspect of component type
790 -- if present, to initialize one or more components.
792 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
793 function Local_Expr_Value (E : Node_Id) return Uint;
794 -- These two Local routines are used to replace the corresponding ones
795 -- in sem_eval because while processing the bounds of an aggregate with
796 -- discrete choices whose index type is an enumeration, we build static
797 -- expressions not recognized by Compile_Time_Known_Value as such since
798 -- they have not yet been analyzed and resolved. All the expressions in
799 -- question are things like Index_Base_Name'Val (Const) which we can
800 -- easily recognize as being constant.
802 ---------
803 -- Add --
804 ---------
806 function Add (Val : Int; To : Node_Id) return Node_Id is
807 Expr_Pos : Node_Id;
808 Expr : Node_Id;
809 To_Pos : Node_Id;
810 U_To : Uint;
811 U_Val : constant Uint := UI_From_Int (Val);
813 begin
814 -- Note: do not try to optimize the case of Val = 0, because
815 -- we need to build a new node with the proper Sloc value anyway.
817 -- First test if we can do constant folding
819 if Local_Compile_Time_Known_Value (To) then
820 U_To := Local_Expr_Value (To) + Val;
822 -- Determine if our constant is outside the range of the index.
823 -- If so return an Empty node. This empty node will be caught
824 -- by Empty_Range below.
826 if Compile_Time_Known_Value (Index_Base_L)
827 and then U_To < Expr_Value (Index_Base_L)
828 then
829 return Empty;
831 elsif Compile_Time_Known_Value (Index_Base_H)
832 and then U_To > Expr_Value (Index_Base_H)
833 then
834 return Empty;
835 end if;
837 Expr_Pos := Make_Integer_Literal (Loc, U_To);
838 Set_Is_Static_Expression (Expr_Pos);
840 if not Is_Enumeration_Type (Index_Base) then
841 Expr := Expr_Pos;
843 -- If we are dealing with enumeration return
844 -- Index_Base'Val (Expr_Pos)
846 else
847 Expr :=
848 Make_Attribute_Reference
849 (Loc,
850 Prefix => Index_Base_Name,
851 Attribute_Name => Name_Val,
852 Expressions => New_List (Expr_Pos));
853 end if;
855 return Expr;
856 end if;
858 -- If we are here no constant folding possible
860 if not Is_Enumeration_Type (Index_Base) then
861 Expr :=
862 Make_Op_Add (Loc,
863 Left_Opnd => Duplicate_Subexpr (To),
864 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
866 -- If we are dealing with enumeration return
867 -- Index_Base'Val (Index_Base'Pos (To) + Val)
869 else
870 To_Pos :=
871 Make_Attribute_Reference
872 (Loc,
873 Prefix => Index_Base_Name,
874 Attribute_Name => Name_Pos,
875 Expressions => New_List (Duplicate_Subexpr (To)));
877 Expr_Pos :=
878 Make_Op_Add (Loc,
879 Left_Opnd => To_Pos,
880 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
882 Expr :=
883 Make_Attribute_Reference
884 (Loc,
885 Prefix => Index_Base_Name,
886 Attribute_Name => Name_Val,
887 Expressions => New_List (Expr_Pos));
888 end if;
890 return Expr;
891 end Add;
893 -----------------
894 -- Empty_Range --
895 -----------------
897 function Empty_Range (L, H : Node_Id) return Boolean is
898 Is_Empty : Boolean := False;
899 Low : Node_Id;
900 High : Node_Id;
902 begin
903 -- First check if L or H were already detected as overflowing the
904 -- index base range type by function Add above. If this is so Add
905 -- returns the empty node.
907 if No (L) or else No (H) then
908 return True;
909 end if;
911 for J in 1 .. 3 loop
912 case J is
914 -- L > H range is empty
916 when 1 =>
917 Low := L;
918 High := H;
920 -- B_L > H range must be empty
922 when 2 =>
923 Low := Index_Base_L;
924 High := H;
926 -- L > B_H range must be empty
928 when 3 =>
929 Low := L;
930 High := Index_Base_H;
931 end case;
933 if Local_Compile_Time_Known_Value (Low)
934 and then
935 Local_Compile_Time_Known_Value (High)
936 then
937 Is_Empty :=
938 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
939 end if;
941 exit when Is_Empty;
942 end loop;
944 return Is_Empty;
945 end Empty_Range;
947 -----------
948 -- Equal --
949 -----------
951 function Equal (L, H : Node_Id) return Boolean is
952 begin
953 if L = H then
954 return True;
956 elsif Local_Compile_Time_Known_Value (L)
957 and then
958 Local_Compile_Time_Known_Value (H)
959 then
960 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
961 end if;
963 return False;
964 end Equal;
966 ----------------
967 -- Gen_Assign --
968 ----------------
970 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
971 L : constant List_Id := New_List;
972 A : Node_Id;
974 New_Indexes : List_Id;
975 Indexed_Comp : Node_Id;
976 Expr_Q : Node_Id;
977 Comp_Type : Entity_Id := Empty;
979 function Add_Loop_Actions (Lis : List_Id) return List_Id;
980 -- Collect insert_actions generated in the construction of a
981 -- loop, and prepend them to the sequence of assignments to
982 -- complete the eventual body of the loop.
984 ----------------------
985 -- Add_Loop_Actions --
986 ----------------------
988 function Add_Loop_Actions (Lis : List_Id) return List_Id is
989 Res : List_Id;
991 begin
992 -- Ada 2005 (AI-287): Do nothing else in case of default
993 -- initialized component.
995 if No (Expr) then
996 return Lis;
998 elsif Nkind (Parent (Expr)) = N_Component_Association
999 and then Present (Loop_Actions (Parent (Expr)))
1000 then
1001 Append_List (Lis, Loop_Actions (Parent (Expr)));
1002 Res := Loop_Actions (Parent (Expr));
1003 Set_Loop_Actions (Parent (Expr), No_List);
1004 return Res;
1006 else
1007 return Lis;
1008 end if;
1009 end Add_Loop_Actions;
1011 -- Start of processing for Gen_Assign
1013 begin
1014 if No (Indexes) then
1015 New_Indexes := New_List;
1016 else
1017 New_Indexes := New_Copy_List_Tree (Indexes);
1018 end if;
1020 Append_To (New_Indexes, Ind);
1022 if Present (Next_Index (Index)) then
1023 return
1024 Add_Loop_Actions (
1025 Build_Array_Aggr_Code
1026 (N => Expr,
1027 Ctype => Ctype,
1028 Index => Next_Index (Index),
1029 Into => Into,
1030 Scalar_Comp => Scalar_Comp,
1031 Indexes => New_Indexes));
1032 end if;
1034 -- If we get here then we are at a bottom-level (sub-)aggregate
1036 Indexed_Comp :=
1037 Checks_Off
1038 (Make_Indexed_Component (Loc,
1039 Prefix => New_Copy_Tree (Into),
1040 Expressions => New_Indexes));
1042 Set_Assignment_OK (Indexed_Comp);
1044 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1045 -- is not present (and therefore we also initialize Expr_Q to empty).
1047 if No (Expr) then
1048 Expr_Q := Empty;
1049 elsif Nkind (Expr) = N_Qualified_Expression then
1050 Expr_Q := Expression (Expr);
1051 else
1052 Expr_Q := Expr;
1053 end if;
1055 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1056 Comp_Type := Component_Type (Etype (N));
1057 pragma Assert (Comp_Type = Ctype); -- AI-287
1059 elsif Present (Next (First (New_Indexes))) then
1061 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1062 -- component because we have received the component type in
1063 -- the formal parameter Ctype.
1065 -- ??? Some assert pragmas have been added to check if this new
1066 -- formal can be used to replace this code in all cases.
1068 if Present (Expr) then
1070 -- This is a multidimensional array. Recover the component type
1071 -- from the outermost aggregate, because subaggregates do not
1072 -- have an assigned type.
1074 declare
1075 P : Node_Id;
1077 begin
1078 P := Parent (Expr);
1079 while Present (P) loop
1080 if Nkind (P) = N_Aggregate
1081 and then Present (Etype (P))
1082 then
1083 Comp_Type := Component_Type (Etype (P));
1084 exit;
1086 else
1087 P := Parent (P);
1088 end if;
1089 end loop;
1091 pragma Assert (Comp_Type = Ctype); -- AI-287
1092 end;
1093 end if;
1094 end if;
1096 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1097 -- default initialized components (otherwise Expr_Q is not present).
1099 if Present (Expr_Q)
1100 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1101 then
1102 -- At this stage the Expression may not have been analyzed yet
1103 -- because the array aggregate code has not been updated to use
1104 -- the Expansion_Delayed flag and avoid analysis altogether to
1105 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1106 -- the analysis of non-array aggregates now in order to get the
1107 -- value of Expansion_Delayed flag for the inner aggregate ???
1109 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1110 Analyze_And_Resolve (Expr_Q, Comp_Type);
1111 end if;
1113 if Is_Delayed_Aggregate (Expr_Q) then
1115 -- This is either a subaggregate of a multidimensional array,
1116 -- or a component of an array type whose component type is
1117 -- also an array. In the latter case, the expression may have
1118 -- component associations that provide different bounds from
1119 -- those of the component type, and sliding must occur. Instead
1120 -- of decomposing the current aggregate assignment, force the
1121 -- re-analysis of the assignment, so that a temporary will be
1122 -- generated in the usual fashion, and sliding will take place.
1124 if Nkind (Parent (N)) = N_Assignment_Statement
1125 and then Is_Array_Type (Comp_Type)
1126 and then Present (Component_Associations (Expr_Q))
1127 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1128 then
1129 Set_Expansion_Delayed (Expr_Q, False);
1130 Set_Analyzed (Expr_Q, False);
1132 else
1133 return
1134 Add_Loop_Actions (
1135 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1136 end if;
1137 end if;
1138 end if;
1140 -- Ada 2005 (AI-287): In case of default initialized component, call
1141 -- the initialization subprogram associated with the component type.
1142 -- If the component type is an access type, add an explicit null
1143 -- assignment, because for the back-end there is an initialization
1144 -- present for the whole aggregate, and no default initialization
1145 -- will take place.
1147 -- In addition, if the component type is controlled, we must call
1148 -- its Initialize procedure explicitly, because there is no explicit
1149 -- object creation that will invoke it otherwise.
1151 if No (Expr) then
1152 if Present (Base_Init_Proc (Base_Type (Ctype)))
1153 or else Has_Task (Base_Type (Ctype))
1154 then
1155 Append_List_To (L,
1156 Build_Initialization_Call (Loc,
1157 Id_Ref => Indexed_Comp,
1158 Typ => Ctype,
1159 With_Default_Init => True));
1161 elsif Is_Access_Type (Ctype) then
1162 Append_To (L,
1163 Make_Assignment_Statement (Loc,
1164 Name => Indexed_Comp,
1165 Expression => Make_Null (Loc)));
1166 end if;
1168 if Needs_Finalization (Ctype) then
1169 Append_To (L,
1170 Make_Init_Call
1171 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1172 Typ => Ctype));
1173 end if;
1175 else
1176 A :=
1177 Make_OK_Assignment_Statement (Loc,
1178 Name => Indexed_Comp,
1179 Expression => New_Copy_Tree (Expr));
1181 -- The target of the assignment may not have been initialized,
1182 -- so it is not possible to call Finalize as expected in normal
1183 -- controlled assignments. We must also avoid using the primitive
1184 -- _assign (which depends on a valid target, and may for example
1185 -- perform discriminant checks on it).
1187 -- Both Finalize and usage of _assign are disabled by setting
1188 -- No_Ctrl_Actions on the assignment. The rest of the controlled
1189 -- actions are done manually with the proper finalization list
1190 -- coming from the context.
1192 Set_No_Ctrl_Actions (A);
1194 -- If this is an aggregate for an array of arrays, each
1195 -- sub-aggregate will be expanded as well, and even with
1196 -- No_Ctrl_Actions the assignments of inner components will
1197 -- require attachment in their assignments to temporaries. These
1198 -- temporaries must be finalized for each subaggregate, to prevent
1199 -- multiple attachments of the same temporary location to same
1200 -- finalization chain (and consequently circular lists). To ensure
1201 -- that finalization takes place for each subaggregate we wrap the
1202 -- assignment in a block.
1204 if Present (Comp_Type)
1205 and then Needs_Finalization (Comp_Type)
1206 and then Is_Array_Type (Comp_Type)
1207 and then Present (Expr)
1208 then
1209 A :=
1210 Make_Block_Statement (Loc,
1211 Handled_Statement_Sequence =>
1212 Make_Handled_Sequence_Of_Statements (Loc,
1213 Statements => New_List (A)));
1214 end if;
1216 Append_To (L, A);
1218 -- Adjust the tag if tagged (because of possible view
1219 -- conversions), unless compiling for a VM where tags
1220 -- are implicit.
1222 if Present (Comp_Type)
1223 and then Is_Tagged_Type (Comp_Type)
1224 and then Tagged_Type_Expansion
1225 then
1226 declare
1227 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Type);
1229 begin
1230 A :=
1231 Make_OK_Assignment_Statement (Loc,
1232 Name =>
1233 Make_Selected_Component (Loc,
1234 Prefix => New_Copy_Tree (Indexed_Comp),
1235 Selector_Name =>
1236 New_Occurrence_Of
1237 (First_Tag_Component (Full_Typ), Loc)),
1239 Expression =>
1240 Unchecked_Convert_To (RTE (RE_Tag),
1241 New_Occurrence_Of
1242 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1243 Loc)));
1245 Append_To (L, A);
1246 end;
1247 end if;
1249 -- Adjust and attach the component to the proper final list, which
1250 -- can be the controller of the outer record object or the final
1251 -- list associated with the scope.
1253 -- If the component is itself an array of controlled types, whose
1254 -- value is given by a sub-aggregate, then the attach calls have
1255 -- been generated when individual subcomponent are assigned, and
1256 -- must not be done again to prevent malformed finalization chains
1257 -- (see comments above, concerning the creation of a block to hold
1258 -- inner finalization actions).
1260 if Present (Comp_Type)
1261 and then Needs_Finalization (Comp_Type)
1262 and then not Is_Limited_Type (Comp_Type)
1263 and then not
1264 (Is_Array_Type (Comp_Type)
1265 and then Is_Controlled (Component_Type (Comp_Type))
1266 and then Nkind (Expr) = N_Aggregate)
1267 then
1268 Append_To (L,
1269 Make_Adjust_Call
1270 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1271 Typ => Comp_Type));
1272 end if;
1273 end if;
1275 return Add_Loop_Actions (L);
1276 end Gen_Assign;
1278 --------------
1279 -- Gen_Loop --
1280 --------------
1282 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1283 L_J : Node_Id;
1285 L_L : Node_Id;
1286 -- Index_Base'(L)
1288 L_H : Node_Id;
1289 -- Index_Base'(H)
1291 L_Range : Node_Id;
1292 -- Index_Base'(L) .. Index_Base'(H)
1294 L_Iteration_Scheme : Node_Id;
1295 -- L_J in Index_Base'(L) .. Index_Base'(H)
1297 L_Body : List_Id;
1298 -- The statements to execute in the loop
1300 S : constant List_Id := New_List;
1301 -- List of statements
1303 Tcopy : Node_Id;
1304 -- Copy of expression tree, used for checking purposes
1306 begin
1307 -- If loop bounds define an empty range return the null statement
1309 if Empty_Range (L, H) then
1310 Append_To (S, Make_Null_Statement (Loc));
1312 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1313 -- default initialized component.
1315 if No (Expr) then
1316 null;
1318 else
1319 -- The expression must be type-checked even though no component
1320 -- of the aggregate will have this value. This is done only for
1321 -- actual components of the array, not for subaggregates. Do
1322 -- the check on a copy, because the expression may be shared
1323 -- among several choices, some of which might be non-null.
1325 if Present (Etype (N))
1326 and then Is_Array_Type (Etype (N))
1327 and then No (Next_Index (Index))
1328 then
1329 Expander_Mode_Save_And_Set (False);
1330 Tcopy := New_Copy_Tree (Expr);
1331 Set_Parent (Tcopy, N);
1332 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1333 Expander_Mode_Restore;
1334 end if;
1335 end if;
1337 return S;
1339 -- If loop bounds are the same then generate an assignment
1341 elsif Equal (L, H) then
1342 return Gen_Assign (New_Copy_Tree (L), Expr);
1344 -- If H - L <= 2 then generate a sequence of assignments when we are
1345 -- processing the bottom most aggregate and it contains scalar
1346 -- components.
1348 elsif No (Next_Index (Index))
1349 and then Scalar_Comp
1350 and then Local_Compile_Time_Known_Value (L)
1351 and then Local_Compile_Time_Known_Value (H)
1352 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1353 then
1355 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1356 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1358 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1359 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1360 end if;
1362 return S;
1363 end if;
1365 -- Otherwise construct the loop, starting with the loop index L_J
1367 L_J := Make_Temporary (Loc, 'J', L);
1369 -- Construct "L .. H" in Index_Base. We use a qualified expression
1370 -- for the bound to convert to the index base, but we don't need
1371 -- to do that if we already have the base type at hand.
1373 if Etype (L) = Index_Base then
1374 L_L := L;
1375 else
1376 L_L :=
1377 Make_Qualified_Expression (Loc,
1378 Subtype_Mark => Index_Base_Name,
1379 Expression => L);
1380 end if;
1382 if Etype (H) = Index_Base then
1383 L_H := H;
1384 else
1385 L_H :=
1386 Make_Qualified_Expression (Loc,
1387 Subtype_Mark => Index_Base_Name,
1388 Expression => H);
1389 end if;
1391 L_Range :=
1392 Make_Range (Loc,
1393 Low_Bound => L_L,
1394 High_Bound => L_H);
1396 -- Construct "for L_J in Index_Base range L .. H"
1398 L_Iteration_Scheme :=
1399 Make_Iteration_Scheme
1400 (Loc,
1401 Loop_Parameter_Specification =>
1402 Make_Loop_Parameter_Specification
1403 (Loc,
1404 Defining_Identifier => L_J,
1405 Discrete_Subtype_Definition => L_Range));
1407 -- Construct the statements to execute in the loop body
1409 L_Body := Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr);
1411 -- Construct the final loop
1413 Append_To (S,
1414 Make_Implicit_Loop_Statement
1415 (Node => N,
1416 Identifier => Empty,
1417 Iteration_Scheme => L_Iteration_Scheme,
1418 Statements => L_Body));
1420 -- A small optimization: if the aggregate is initialized with a box
1421 -- and the component type has no initialization procedure, remove the
1422 -- useless empty loop.
1424 if Nkind (First (S)) = N_Loop_Statement
1425 and then Is_Empty_List (Statements (First (S)))
1426 then
1427 return New_List (Make_Null_Statement (Loc));
1428 else
1429 return S;
1430 end if;
1431 end Gen_Loop;
1433 ---------------
1434 -- Gen_While --
1435 ---------------
1437 -- The code built is
1439 -- W_J : Index_Base := L;
1440 -- while W_J < H loop
1441 -- W_J := Index_Base'Succ (W);
1442 -- L_Body;
1443 -- end loop;
1445 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1446 W_J : Node_Id;
1448 W_Decl : Node_Id;
1449 -- W_J : Base_Type := L;
1451 W_Iteration_Scheme : Node_Id;
1452 -- while W_J < H
1454 W_Index_Succ : Node_Id;
1455 -- Index_Base'Succ (J)
1457 W_Increment : Node_Id;
1458 -- W_J := Index_Base'Succ (W)
1460 W_Body : constant List_Id := New_List;
1461 -- The statements to execute in the loop
1463 S : constant List_Id := New_List;
1464 -- list of statement
1466 begin
1467 -- If loop bounds define an empty range or are equal return null
1469 if Empty_Range (L, H) or else Equal (L, H) then
1470 Append_To (S, Make_Null_Statement (Loc));
1471 return S;
1472 end if;
1474 -- Build the decl of W_J
1476 W_J := Make_Temporary (Loc, 'J', L);
1477 W_Decl :=
1478 Make_Object_Declaration
1479 (Loc,
1480 Defining_Identifier => W_J,
1481 Object_Definition => Index_Base_Name,
1482 Expression => L);
1484 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1485 -- that in this particular case L is a fresh Expr generated by
1486 -- Add which we are the only ones to use.
1488 Append_To (S, W_Decl);
1490 -- Construct " while W_J < H"
1492 W_Iteration_Scheme :=
1493 Make_Iteration_Scheme
1494 (Loc,
1495 Condition => Make_Op_Lt
1496 (Loc,
1497 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1498 Right_Opnd => New_Copy_Tree (H)));
1500 -- Construct the statements to execute in the loop body
1502 W_Index_Succ :=
1503 Make_Attribute_Reference
1504 (Loc,
1505 Prefix => Index_Base_Name,
1506 Attribute_Name => Name_Succ,
1507 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1509 W_Increment :=
1510 Make_OK_Assignment_Statement
1511 (Loc,
1512 Name => New_Occurrence_Of (W_J, Loc),
1513 Expression => W_Index_Succ);
1515 Append_To (W_Body, W_Increment);
1516 Append_List_To (W_Body,
1517 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr));
1519 -- Construct the final loop
1521 Append_To (S,
1522 Make_Implicit_Loop_Statement
1523 (Node => N,
1524 Identifier => Empty,
1525 Iteration_Scheme => W_Iteration_Scheme,
1526 Statements => W_Body));
1528 return S;
1529 end Gen_While;
1531 --------------------
1532 -- Get_Assoc_Expr --
1533 --------------------
1535 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1536 begin
1537 if Box_Present (Assoc) then
1538 if Is_Scalar_Type (Ctype)
1539 and then Present (Default_Aspect_Value (Ctype))
1540 then
1541 return Default_Aspect_Value (Ctype);
1542 else
1543 return Empty;
1544 end if;
1546 else
1547 return Expression (Assoc);
1548 end if;
1549 end Get_Assoc_Expr;
1551 ---------------------
1552 -- Index_Base_Name --
1553 ---------------------
1555 function Index_Base_Name return Node_Id is
1556 begin
1557 return New_Occurrence_Of (Index_Base, Sloc (N));
1558 end Index_Base_Name;
1560 ------------------------------------
1561 -- Local_Compile_Time_Known_Value --
1562 ------------------------------------
1564 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1565 begin
1566 return Compile_Time_Known_Value (E)
1567 or else
1568 (Nkind (E) = N_Attribute_Reference
1569 and then Attribute_Name (E) = Name_Val
1570 and then Compile_Time_Known_Value (First (Expressions (E))));
1571 end Local_Compile_Time_Known_Value;
1573 ----------------------
1574 -- Local_Expr_Value --
1575 ----------------------
1577 function Local_Expr_Value (E : Node_Id) return Uint is
1578 begin
1579 if Compile_Time_Known_Value (E) then
1580 return Expr_Value (E);
1581 else
1582 return Expr_Value (First (Expressions (E)));
1583 end if;
1584 end Local_Expr_Value;
1586 -- Build_Array_Aggr_Code Variables
1588 Assoc : Node_Id;
1589 Choice : Node_Id;
1590 Expr : Node_Id;
1591 Typ : Entity_Id;
1593 Others_Assoc : Node_Id := Empty;
1595 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1596 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1597 -- The aggregate bounds of this specific sub-aggregate. Note that if
1598 -- the code generated by Build_Array_Aggr_Code is executed then these
1599 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1601 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1602 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1603 -- After Duplicate_Subexpr these are side-effect free
1605 Low : Node_Id;
1606 High : Node_Id;
1608 Nb_Choices : Nat := 0;
1609 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1610 -- Used to sort all the different choice values
1612 Nb_Elements : Int;
1613 -- Number of elements in the positional aggregate
1615 New_Code : constant List_Id := New_List;
1617 -- Start of processing for Build_Array_Aggr_Code
1619 begin
1620 -- First before we start, a special case. if we have a bit packed
1621 -- array represented as a modular type, then clear the value to
1622 -- zero first, to ensure that unused bits are properly cleared.
1624 Typ := Etype (N);
1626 if Present (Typ)
1627 and then Is_Bit_Packed_Array (Typ)
1628 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
1629 then
1630 Append_To (New_Code,
1631 Make_Assignment_Statement (Loc,
1632 Name => New_Copy_Tree (Into),
1633 Expression =>
1634 Unchecked_Convert_To (Typ,
1635 Make_Integer_Literal (Loc, Uint_0))));
1636 end if;
1638 -- If the component type contains tasks, we need to build a Master
1639 -- entity in the current scope, because it will be needed if build-
1640 -- in-place functions are called in the expanded code.
1642 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
1643 Build_Master_Entity (Defining_Identifier (Parent (N)));
1644 end if;
1646 -- STEP 1: Process component associations
1648 -- For those associations that may generate a loop, initialize
1649 -- Loop_Actions to collect inserted actions that may be crated.
1651 -- Skip this if no component associations
1653 if No (Expressions (N)) then
1655 -- STEP 1 (a): Sort the discrete choices
1657 Assoc := First (Component_Associations (N));
1658 while Present (Assoc) loop
1659 Choice := First (Choices (Assoc));
1660 while Present (Choice) loop
1661 if Nkind (Choice) = N_Others_Choice then
1662 Set_Loop_Actions (Assoc, New_List);
1663 Others_Assoc := Assoc;
1664 exit;
1665 end if;
1667 Get_Index_Bounds (Choice, Low, High);
1669 if Low /= High then
1670 Set_Loop_Actions (Assoc, New_List);
1671 end if;
1673 Nb_Choices := Nb_Choices + 1;
1675 Table (Nb_Choices) :=
1676 (Choice_Lo => Low,
1677 Choice_Hi => High,
1678 Choice_Node => Get_Assoc_Expr (Assoc));
1680 Next (Choice);
1681 end loop;
1683 Next (Assoc);
1684 end loop;
1686 -- If there is more than one set of choices these must be static
1687 -- and we can therefore sort them. Remember that Nb_Choices does not
1688 -- account for an others choice.
1690 if Nb_Choices > 1 then
1691 Sort_Case_Table (Table);
1692 end if;
1694 -- STEP 1 (b): take care of the whole set of discrete choices
1696 for J in 1 .. Nb_Choices loop
1697 Low := Table (J).Choice_Lo;
1698 High := Table (J).Choice_Hi;
1699 Expr := Table (J).Choice_Node;
1700 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1701 end loop;
1703 -- STEP 1 (c): generate the remaining loops to cover others choice
1704 -- We don't need to generate loops over empty gaps, but if there is
1705 -- a single empty range we must analyze the expression for semantics
1707 if Present (Others_Assoc) then
1708 declare
1709 First : Boolean := True;
1711 begin
1712 for J in 0 .. Nb_Choices loop
1713 if J = 0 then
1714 Low := Aggr_Low;
1715 else
1716 Low := Add (1, To => Table (J).Choice_Hi);
1717 end if;
1719 if J = Nb_Choices then
1720 High := Aggr_High;
1721 else
1722 High := Add (-1, To => Table (J + 1).Choice_Lo);
1723 end if;
1725 -- If this is an expansion within an init proc, make
1726 -- sure that discriminant references are replaced by
1727 -- the corresponding discriminal.
1729 if Inside_Init_Proc then
1730 if Is_Entity_Name (Low)
1731 and then Ekind (Entity (Low)) = E_Discriminant
1732 then
1733 Set_Entity (Low, Discriminal (Entity (Low)));
1734 end if;
1736 if Is_Entity_Name (High)
1737 and then Ekind (Entity (High)) = E_Discriminant
1738 then
1739 Set_Entity (High, Discriminal (Entity (High)));
1740 end if;
1741 end if;
1743 if First
1744 or else not Empty_Range (Low, High)
1745 then
1746 First := False;
1747 Append_List
1748 (Gen_Loop (Low, High,
1749 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
1750 end if;
1751 end loop;
1752 end;
1753 end if;
1755 -- STEP 2: Process positional components
1757 else
1758 -- STEP 2 (a): Generate the assignments for each positional element
1759 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1760 -- Aggr_L is analyzed and Add wants an analyzed expression.
1762 Expr := First (Expressions (N));
1763 Nb_Elements := -1;
1764 while Present (Expr) loop
1765 Nb_Elements := Nb_Elements + 1;
1766 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1767 To => New_Code);
1768 Next (Expr);
1769 end loop;
1771 -- STEP 2 (b): Generate final loop if an others choice is present
1772 -- Here Nb_Elements gives the offset of the last positional element.
1774 if Present (Component_Associations (N)) then
1775 Assoc := Last (Component_Associations (N));
1777 -- Ada 2005 (AI-287)
1779 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1780 Aggr_High,
1781 Get_Assoc_Expr (Assoc)), -- AI-287
1782 To => New_Code);
1783 end if;
1784 end if;
1786 return New_Code;
1787 end Build_Array_Aggr_Code;
1789 ----------------------------
1790 -- Build_Record_Aggr_Code --
1791 ----------------------------
1793 function Build_Record_Aggr_Code
1794 (N : Node_Id;
1795 Typ : Entity_Id;
1796 Lhs : Node_Id) return List_Id
1798 Loc : constant Source_Ptr := Sloc (N);
1799 L : constant List_Id := New_List;
1800 N_Typ : constant Entity_Id := Etype (N);
1802 Comp : Node_Id;
1803 Instr : Node_Id;
1804 Ref : Node_Id;
1805 Target : Entity_Id;
1806 Comp_Type : Entity_Id;
1807 Selector : Entity_Id;
1808 Comp_Expr : Node_Id;
1809 Expr_Q : Node_Id;
1811 -- If this is an internal aggregate, the External_Final_List is an
1812 -- expression for the controller record of the enclosing type.
1814 -- If the current aggregate has several controlled components, this
1815 -- expression will appear in several calls to attach to the finali-
1816 -- zation list, and it must not be shared.
1818 Ancestor_Is_Expression : Boolean := False;
1819 Ancestor_Is_Subtype_Mark : Boolean := False;
1821 Init_Typ : Entity_Id := Empty;
1823 Finalization_Done : Boolean := False;
1824 -- True if Generate_Finalization_Actions has already been called; calls
1825 -- after the first do nothing.
1827 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1828 -- Returns the value that the given discriminant of an ancestor type
1829 -- should receive (in the absence of a conflict with the value provided
1830 -- by an ancestor part of an extension aggregate).
1832 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1833 -- Check that each of the discriminant values defined by the ancestor
1834 -- part of an extension aggregate match the corresponding values
1835 -- provided by either an association of the aggregate or by the
1836 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1838 function Compatible_Int_Bounds
1839 (Agg_Bounds : Node_Id;
1840 Typ_Bounds : Node_Id) return Boolean;
1841 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1842 -- assumed that both bounds are integer ranges.
1844 procedure Generate_Finalization_Actions;
1845 -- Deal with the various controlled type data structure initializations
1846 -- (but only if it hasn't been done already).
1848 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1849 -- Returns the first discriminant association in the constraint
1850 -- associated with T, if any, otherwise returns Empty.
1852 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
1853 -- If Typ is derived, and constrains discriminants of the parent type,
1854 -- these discriminants are not components of the aggregate, and must be
1855 -- initialized. The assignments are appended to List. The same is done
1856 -- if Typ derives fron an already constrained subtype of a discriminated
1857 -- parent type.
1859 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
1860 -- If the ancestor part is an unconstrained type and further ancestors
1861 -- do not provide discriminants for it, check aggregate components for
1862 -- values of the discriminants.
1864 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1865 -- Check whether Bounds is a range node and its lower and higher bounds
1866 -- are integers literals.
1868 ---------------------------------
1869 -- Ancestor_Discriminant_Value --
1870 ---------------------------------
1872 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1873 Assoc : Node_Id;
1874 Assoc_Elmt : Elmt_Id;
1875 Aggr_Comp : Entity_Id;
1876 Corresp_Disc : Entity_Id;
1877 Current_Typ : Entity_Id := Base_Type (Typ);
1878 Parent_Typ : Entity_Id;
1879 Parent_Disc : Entity_Id;
1880 Save_Assoc : Node_Id := Empty;
1882 begin
1883 -- First check any discriminant associations to see if any of them
1884 -- provide a value for the discriminant.
1886 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1887 Assoc := First (Component_Associations (N));
1888 while Present (Assoc) loop
1889 Aggr_Comp := Entity (First (Choices (Assoc)));
1891 if Ekind (Aggr_Comp) = E_Discriminant then
1892 Save_Assoc := Expression (Assoc);
1894 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1895 while Present (Corresp_Disc) loop
1897 -- If found a corresponding discriminant then return the
1898 -- value given in the aggregate. (Note: this is not
1899 -- correct in the presence of side effects. ???)
1901 if Disc = Corresp_Disc then
1902 return Duplicate_Subexpr (Expression (Assoc));
1903 end if;
1905 Corresp_Disc :=
1906 Corresponding_Discriminant (Corresp_Disc);
1907 end loop;
1908 end if;
1910 Next (Assoc);
1911 end loop;
1912 end if;
1914 -- No match found in aggregate, so chain up parent types to find
1915 -- a constraint that defines the value of the discriminant.
1917 Parent_Typ := Etype (Current_Typ);
1918 while Current_Typ /= Parent_Typ loop
1919 if Has_Discriminants (Parent_Typ)
1920 and then not Has_Unknown_Discriminants (Parent_Typ)
1921 then
1922 Parent_Disc := First_Discriminant (Parent_Typ);
1924 -- We either get the association from the subtype indication
1925 -- of the type definition itself, or from the discriminant
1926 -- constraint associated with the type entity (which is
1927 -- preferable, but it's not always present ???)
1929 if Is_Empty_Elmt_List (
1930 Discriminant_Constraint (Current_Typ))
1931 then
1932 Assoc := Get_Constraint_Association (Current_Typ);
1933 Assoc_Elmt := No_Elmt;
1934 else
1935 Assoc_Elmt :=
1936 First_Elmt (Discriminant_Constraint (Current_Typ));
1937 Assoc := Node (Assoc_Elmt);
1938 end if;
1940 -- Traverse the discriminants of the parent type looking
1941 -- for one that corresponds.
1943 while Present (Parent_Disc) and then Present (Assoc) loop
1944 Corresp_Disc := Parent_Disc;
1945 while Present (Corresp_Disc)
1946 and then Disc /= Corresp_Disc
1947 loop
1948 Corresp_Disc :=
1949 Corresponding_Discriminant (Corresp_Disc);
1950 end loop;
1952 if Disc = Corresp_Disc then
1953 if Nkind (Assoc) = N_Discriminant_Association then
1954 Assoc := Expression (Assoc);
1955 end if;
1957 -- If the located association directly denotes
1958 -- a discriminant, then use the value of a saved
1959 -- association of the aggregate. This is an approach
1960 -- used to handle certain cases involving multiple
1961 -- discriminants mapped to a single discriminant of
1962 -- a descendant. It's not clear how to locate the
1963 -- appropriate discriminant value for such cases. ???
1965 if Is_Entity_Name (Assoc)
1966 and then Ekind (Entity (Assoc)) = E_Discriminant
1967 then
1968 Assoc := Save_Assoc;
1969 end if;
1971 return Duplicate_Subexpr (Assoc);
1972 end if;
1974 Next_Discriminant (Parent_Disc);
1976 if No (Assoc_Elmt) then
1977 Next (Assoc);
1978 else
1979 Next_Elmt (Assoc_Elmt);
1980 if Present (Assoc_Elmt) then
1981 Assoc := Node (Assoc_Elmt);
1982 else
1983 Assoc := Empty;
1984 end if;
1985 end if;
1986 end loop;
1987 end if;
1989 Current_Typ := Parent_Typ;
1990 Parent_Typ := Etype (Current_Typ);
1991 end loop;
1993 -- In some cases there's no ancestor value to locate (such as
1994 -- when an ancestor part given by an expression defines the
1995 -- discriminant value).
1997 return Empty;
1998 end Ancestor_Discriminant_Value;
2000 ----------------------------------
2001 -- Check_Ancestor_Discriminants --
2002 ----------------------------------
2004 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2005 Discr : Entity_Id;
2006 Disc_Value : Node_Id;
2007 Cond : Node_Id;
2009 begin
2010 Discr := First_Discriminant (Base_Type (Anc_Typ));
2011 while Present (Discr) loop
2012 Disc_Value := Ancestor_Discriminant_Value (Discr);
2014 if Present (Disc_Value) then
2015 Cond := Make_Op_Ne (Loc,
2016 Left_Opnd =>
2017 Make_Selected_Component (Loc,
2018 Prefix => New_Copy_Tree (Target),
2019 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2020 Right_Opnd => Disc_Value);
2022 Append_To (L,
2023 Make_Raise_Constraint_Error (Loc,
2024 Condition => Cond,
2025 Reason => CE_Discriminant_Check_Failed));
2026 end if;
2028 Next_Discriminant (Discr);
2029 end loop;
2030 end Check_Ancestor_Discriminants;
2032 ---------------------------
2033 -- Compatible_Int_Bounds --
2034 ---------------------------
2036 function Compatible_Int_Bounds
2037 (Agg_Bounds : Node_Id;
2038 Typ_Bounds : Node_Id) return Boolean
2040 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2041 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2042 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2043 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2044 begin
2045 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2046 end Compatible_Int_Bounds;
2048 --------------------------------
2049 -- Get_Constraint_Association --
2050 --------------------------------
2052 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2053 Indic : Node_Id;
2054 Typ : Entity_Id;
2056 begin
2057 Typ := T;
2059 -- Handle private types in instances
2061 if In_Instance
2062 and then Is_Private_Type (Typ)
2063 and then Present (Full_View (Typ))
2064 then
2065 Typ := Full_View (Typ);
2066 end if;
2068 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2070 -- ??? Also need to cover case of a type mark denoting a subtype
2071 -- with constraint.
2073 if Nkind (Indic) = N_Subtype_Indication
2074 and then Present (Constraint (Indic))
2075 then
2076 return First (Constraints (Constraint (Indic)));
2077 end if;
2079 return Empty;
2080 end Get_Constraint_Association;
2082 -------------------------------------
2083 -- Get_Explicit_Discriminant_Value --
2084 -------------------------------------
2086 function Get_Explicit_Discriminant_Value
2087 (D : Entity_Id) return Node_Id
2089 Assoc : Node_Id;
2090 Choice : Node_Id;
2091 Val : Node_Id;
2093 begin
2094 -- The aggregate has been normalized and all associations have a
2095 -- single choice.
2097 Assoc := First (Component_Associations (N));
2098 while Present (Assoc) loop
2099 Choice := First (Choices (Assoc));
2101 if Chars (Choice) = Chars (D) then
2102 Val := Expression (Assoc);
2103 Remove (Assoc);
2104 return Val;
2105 end if;
2107 Next (Assoc);
2108 end loop;
2110 return Empty;
2111 end Get_Explicit_Discriminant_Value;
2113 -------------------------------
2114 -- Init_Hidden_Discriminants --
2115 -------------------------------
2117 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2118 Btype : Entity_Id;
2119 Parent_Type : Entity_Id;
2120 Disc : Entity_Id;
2121 Discr_Val : Elmt_Id;
2122 In_Aggr_Type : Boolean;
2124 begin
2125 -- The constraints on the hidden discriminants, if present, are kept
2126 -- in the Stored_Constraint list of the type itself, or in that of
2127 -- the base type. If not in the constraints of the aggregate itself,
2128 -- we examine ancestors to find discriminants that are not renamed
2129 -- by other discriminants but constrained explicitly.
2131 In_Aggr_Type := True;
2133 Btype := Base_Type (Typ);
2134 while Is_Derived_Type (Btype)
2135 and then
2136 (Present (Stored_Constraint (Btype))
2137 or else
2138 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2139 loop
2140 Parent_Type := Etype (Btype);
2142 if not Has_Discriminants (Parent_Type) then
2143 return;
2144 end if;
2146 Disc := First_Discriminant (Parent_Type);
2148 -- We know that one of the stored-constraint lists is present
2150 if Present (Stored_Constraint (Btype)) then
2151 Discr_Val := First_Elmt (Stored_Constraint (Btype));
2153 -- For private extension, stored constraint may be on full view
2155 elsif Is_Private_Type (Btype)
2156 and then Present (Full_View (Btype))
2157 and then Present (Stored_Constraint (Full_View (Btype)))
2158 then
2159 Discr_Val := First_Elmt (Stored_Constraint (Full_View (Btype)));
2161 else
2162 Discr_Val := First_Elmt (Stored_Constraint (Typ));
2163 end if;
2165 while Present (Discr_Val) and then Present (Disc) loop
2167 -- Only those discriminants of the parent that are not
2168 -- renamed by discriminants of the derived type need to
2169 -- be added explicitly.
2171 if not Is_Entity_Name (Node (Discr_Val))
2172 or else Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2173 then
2174 Comp_Expr :=
2175 Make_Selected_Component (Loc,
2176 Prefix => New_Copy_Tree (Target),
2177 Selector_Name => New_Occurrence_Of (Disc, Loc));
2179 Instr :=
2180 Make_OK_Assignment_Statement (Loc,
2181 Name => Comp_Expr,
2182 Expression => New_Copy_Tree (Node (Discr_Val)));
2184 Set_No_Ctrl_Actions (Instr);
2185 Append_To (List, Instr);
2186 end if;
2188 Next_Discriminant (Disc);
2189 Next_Elmt (Discr_Val);
2190 end loop;
2192 In_Aggr_Type := False;
2193 Btype := Base_Type (Parent_Type);
2194 end loop;
2195 end Init_Hidden_Discriminants;
2197 -------------------------
2198 -- Is_Int_Range_Bounds --
2199 -------------------------
2201 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2202 begin
2203 return Nkind (Bounds) = N_Range
2204 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2205 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2206 end Is_Int_Range_Bounds;
2208 -----------------------------------
2209 -- Generate_Finalization_Actions --
2210 -----------------------------------
2212 procedure Generate_Finalization_Actions is
2213 begin
2214 -- Do the work only the first time this is called
2216 if Finalization_Done then
2217 return;
2218 end if;
2220 Finalization_Done := True;
2222 -- Determine the external finalization list. It is either the
2223 -- finalization list of the outer-scope or the one coming from an
2224 -- outer aggregate. When the target is not a temporary, the proper
2225 -- scope is the scope of the target rather than the potentially
2226 -- transient current scope.
2228 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2229 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2230 Set_Assignment_OK (Ref);
2232 Append_To (L,
2233 Make_Procedure_Call_Statement (Loc,
2234 Name =>
2235 New_Occurrence_Of
2236 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2237 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2238 end if;
2239 end Generate_Finalization_Actions;
2241 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2242 -- If default expression of a component mentions a discriminant of the
2243 -- type, it must be rewritten as the discriminant of the target object.
2245 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2246 -- If the aggregate contains a self-reference, traverse each expression
2247 -- to replace a possible self-reference with a reference to the proper
2248 -- component of the target of the assignment.
2250 --------------------------
2251 -- Rewrite_Discriminant --
2252 --------------------------
2254 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2255 begin
2256 if Is_Entity_Name (Expr)
2257 and then Present (Entity (Expr))
2258 and then Ekind (Entity (Expr)) = E_In_Parameter
2259 and then Present (Discriminal_Link (Entity (Expr)))
2260 and then Scope (Discriminal_Link (Entity (Expr))) =
2261 Base_Type (Etype (N))
2262 then
2263 Rewrite (Expr,
2264 Make_Selected_Component (Loc,
2265 Prefix => New_Copy_Tree (Lhs),
2266 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2267 end if;
2269 return OK;
2270 end Rewrite_Discriminant;
2272 ------------------
2273 -- Replace_Type --
2274 ------------------
2276 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2277 begin
2278 -- Note regarding the Root_Type test below: Aggregate components for
2279 -- self-referential types include attribute references to the current
2280 -- instance, of the form: Typ'access, etc.. These references are
2281 -- rewritten as references to the target of the aggregate: the
2282 -- left-hand side of an assignment, the entity in a declaration,
2283 -- or a temporary. Without this test, we would improperly extended
2284 -- this rewriting to attribute references whose prefix was not the
2285 -- type of the aggregate.
2287 if Nkind (Expr) = N_Attribute_Reference
2288 and then Is_Entity_Name (Prefix (Expr))
2289 and then Is_Type (Entity (Prefix (Expr)))
2290 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2291 then
2292 if Is_Entity_Name (Lhs) then
2293 Rewrite (Prefix (Expr),
2294 New_Occurrence_Of (Entity (Lhs), Loc));
2296 elsif Nkind (Lhs) = N_Selected_Component then
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);
2303 else
2304 Rewrite (Expr,
2305 Make_Attribute_Reference (Loc,
2306 Attribute_Name => Name_Unrestricted_Access,
2307 Prefix => New_Copy_Tree (Lhs)));
2308 Set_Analyzed (Parent (Expr), False);
2309 end if;
2310 end if;
2312 return OK;
2313 end Replace_Type;
2315 procedure Replace_Self_Reference is
2316 new Traverse_Proc (Replace_Type);
2318 procedure Replace_Discriminants is
2319 new Traverse_Proc (Rewrite_Discriminant);
2321 -- Start of processing for Build_Record_Aggr_Code
2323 begin
2324 if Has_Self_Reference (N) then
2325 Replace_Self_Reference (N);
2326 end if;
2328 -- If the target of the aggregate is class-wide, we must convert it
2329 -- to the actual type of the aggregate, so that the proper components
2330 -- are visible. We know already that the types are compatible.
2332 if Present (Etype (Lhs))
2333 and then Is_Class_Wide_Type (Etype (Lhs))
2334 then
2335 Target := Unchecked_Convert_To (Typ, Lhs);
2336 else
2337 Target := Lhs;
2338 end if;
2340 -- Deal with the ancestor part of extension aggregates or with the
2341 -- discriminants of the root type.
2343 if Nkind (N) = N_Extension_Aggregate then
2344 declare
2345 Ancestor : constant Node_Id := Ancestor_Part (N);
2346 Assign : List_Id;
2348 begin
2349 -- If the ancestor part is a subtype mark "T", we generate
2351 -- init-proc (T (tmp)); if T is constrained and
2352 -- init-proc (S (tmp)); where S applies an appropriate
2353 -- constraint if T is unconstrained
2355 if Is_Entity_Name (Ancestor)
2356 and then Is_Type (Entity (Ancestor))
2357 then
2358 Ancestor_Is_Subtype_Mark := True;
2360 if Is_Constrained (Entity (Ancestor)) then
2361 Init_Typ := Entity (Ancestor);
2363 -- For an ancestor part given by an unconstrained type mark,
2364 -- create a subtype constrained by appropriate corresponding
2365 -- discriminant values coming from either associations of the
2366 -- aggregate or a constraint on a parent type. The subtype will
2367 -- be used to generate the correct default value for the
2368 -- ancestor part.
2370 elsif Has_Discriminants (Entity (Ancestor)) then
2371 declare
2372 Anc_Typ : constant Entity_Id := Entity (Ancestor);
2373 Anc_Constr : constant List_Id := New_List;
2374 Discrim : Entity_Id;
2375 Disc_Value : Node_Id;
2376 New_Indic : Node_Id;
2377 Subt_Decl : Node_Id;
2379 begin
2380 Discrim := First_Discriminant (Anc_Typ);
2381 while Present (Discrim) loop
2382 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2384 -- If no usable discriminant in ancestors, check
2385 -- whether aggregate has an explicit value for it.
2387 if No (Disc_Value) then
2388 Disc_Value :=
2389 Get_Explicit_Discriminant_Value (Discrim);
2390 end if;
2392 Append_To (Anc_Constr, Disc_Value);
2393 Next_Discriminant (Discrim);
2394 end loop;
2396 New_Indic :=
2397 Make_Subtype_Indication (Loc,
2398 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2399 Constraint =>
2400 Make_Index_Or_Discriminant_Constraint (Loc,
2401 Constraints => Anc_Constr));
2403 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2405 Subt_Decl :=
2406 Make_Subtype_Declaration (Loc,
2407 Defining_Identifier => Init_Typ,
2408 Subtype_Indication => New_Indic);
2410 -- Itypes must be analyzed with checks off Declaration
2411 -- must have a parent for proper handling of subsidiary
2412 -- actions.
2414 Set_Parent (Subt_Decl, N);
2415 Analyze (Subt_Decl, Suppress => All_Checks);
2416 end;
2417 end if;
2419 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2420 Set_Assignment_OK (Ref);
2422 if not Is_Interface (Init_Typ) then
2423 Append_List_To (L,
2424 Build_Initialization_Call (Loc,
2425 Id_Ref => Ref,
2426 Typ => Init_Typ,
2427 In_Init_Proc => Within_Init_Proc,
2428 With_Default_Init => Has_Default_Init_Comps (N)
2429 or else
2430 Has_Task (Base_Type (Init_Typ))));
2432 if Is_Constrained (Entity (Ancestor))
2433 and then Has_Discriminants (Entity (Ancestor))
2434 then
2435 Check_Ancestor_Discriminants (Entity (Ancestor));
2436 end if;
2437 end if;
2439 -- Handle calls to C++ constructors
2441 elsif Is_CPP_Constructor_Call (Ancestor) then
2442 Init_Typ := Etype (Ancestor);
2443 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2444 Set_Assignment_OK (Ref);
2446 Append_List_To (L,
2447 Build_Initialization_Call (Loc,
2448 Id_Ref => Ref,
2449 Typ => Init_Typ,
2450 In_Init_Proc => Within_Init_Proc,
2451 With_Default_Init => Has_Default_Init_Comps (N),
2452 Constructor_Ref => Ancestor));
2454 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2455 -- limited type, a recursive call expands the ancestor. Note that
2456 -- in the limited case, the ancestor part must be either a
2457 -- function call (possibly qualified, or wrapped in an unchecked
2458 -- conversion) or aggregate (definitely qualified).
2460 -- The ancestor part can also be a function call (that may be
2461 -- transformed into an explicit dereference) or a qualification
2462 -- of one such.
2464 elsif Is_Limited_Type (Etype (Ancestor))
2465 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
2466 N_Extension_Aggregate)
2467 then
2468 Ancestor_Is_Expression := True;
2470 -- Set up finalization data for enclosing record, because
2471 -- controlled subcomponents of the ancestor part will be
2472 -- attached to it.
2474 Generate_Finalization_Actions;
2476 Append_List_To (L,
2477 Build_Record_Aggr_Code
2478 (N => Unqualify (Ancestor),
2479 Typ => Etype (Unqualify (Ancestor)),
2480 Lhs => Target));
2482 -- If the ancestor part is an expression "E", we generate
2484 -- T (tmp) := E;
2486 -- In Ada 2005, this includes the case of a (possibly qualified)
2487 -- limited function call. The assignment will turn into a
2488 -- build-in-place function call (for further details, see
2489 -- Make_Build_In_Place_Call_In_Assignment).
2491 else
2492 Ancestor_Is_Expression := True;
2493 Init_Typ := Etype (Ancestor);
2495 -- If the ancestor part is an aggregate, force its full
2496 -- expansion, which was delayed.
2498 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
2499 N_Extension_Aggregate)
2500 then
2501 Set_Analyzed (Ancestor, False);
2502 Set_Analyzed (Expression (Ancestor), False);
2503 end if;
2505 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2506 Set_Assignment_OK (Ref);
2508 -- Make the assignment without usual controlled actions, since
2509 -- we only want to Adjust afterwards, but not to Finalize
2510 -- beforehand. Add manual Adjust when necessary.
2512 Assign := New_List (
2513 Make_OK_Assignment_Statement (Loc,
2514 Name => Ref,
2515 Expression => Ancestor));
2516 Set_No_Ctrl_Actions (First (Assign));
2518 -- Assign the tag now to make sure that the dispatching call in
2519 -- the subsequent deep_adjust works properly (unless VM_Target,
2520 -- where tags are implicit).
2522 if Tagged_Type_Expansion then
2523 Instr :=
2524 Make_OK_Assignment_Statement (Loc,
2525 Name =>
2526 Make_Selected_Component (Loc,
2527 Prefix => New_Copy_Tree (Target),
2528 Selector_Name =>
2529 New_Occurrence_Of
2530 (First_Tag_Component (Base_Type (Typ)), Loc)),
2532 Expression =>
2533 Unchecked_Convert_To (RTE (RE_Tag),
2534 New_Occurrence_Of
2535 (Node (First_Elmt
2536 (Access_Disp_Table (Base_Type (Typ)))),
2537 Loc)));
2539 Set_Assignment_OK (Name (Instr));
2540 Append_To (Assign, Instr);
2542 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2543 -- also initialize tags of the secondary dispatch tables.
2545 if Has_Interfaces (Base_Type (Typ)) then
2546 Init_Secondary_Tags
2547 (Typ => Base_Type (Typ),
2548 Target => Target,
2549 Stmts_List => Assign);
2550 end if;
2551 end if;
2553 -- Call Adjust manually
2555 if Needs_Finalization (Etype (Ancestor))
2556 and then not Is_Limited_Type (Etype (Ancestor))
2557 then
2558 Append_To (Assign,
2559 Make_Adjust_Call
2560 (Obj_Ref => New_Copy_Tree (Ref),
2561 Typ => Etype (Ancestor)));
2562 end if;
2564 Append_To (L,
2565 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2567 if Has_Discriminants (Init_Typ) then
2568 Check_Ancestor_Discriminants (Init_Typ);
2569 end if;
2570 end if;
2571 end;
2573 -- Generate assignments of hidden discriminants. If the base type is
2574 -- an unchecked union, the discriminants are unknown to the back-end
2575 -- and absent from a value of the type, so assignments for them are
2576 -- not emitted.
2578 if Has_Discriminants (Typ)
2579 and then not Is_Unchecked_Union (Base_Type (Typ))
2580 then
2581 Init_Hidden_Discriminants (Typ, L);
2582 end if;
2584 -- Normal case (not an extension aggregate)
2586 else
2587 -- Generate the discriminant expressions, component by component.
2588 -- If the base type is an unchecked union, the discriminants are
2589 -- unknown to the back-end and absent from a value of the type, so
2590 -- assignments for them are not emitted.
2592 if Has_Discriminants (Typ)
2593 and then not Is_Unchecked_Union (Base_Type (Typ))
2594 then
2595 Init_Hidden_Discriminants (Typ, L);
2597 -- Generate discriminant init values for the visible discriminants
2599 declare
2600 Discriminant : Entity_Id;
2601 Discriminant_Value : Node_Id;
2603 begin
2604 Discriminant := First_Stored_Discriminant (Typ);
2605 while Present (Discriminant) loop
2606 Comp_Expr :=
2607 Make_Selected_Component (Loc,
2608 Prefix => New_Copy_Tree (Target),
2609 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2611 Discriminant_Value :=
2612 Get_Discriminant_Value (
2613 Discriminant,
2614 N_Typ,
2615 Discriminant_Constraint (N_Typ));
2617 Instr :=
2618 Make_OK_Assignment_Statement (Loc,
2619 Name => Comp_Expr,
2620 Expression => New_Copy_Tree (Discriminant_Value));
2622 Set_No_Ctrl_Actions (Instr);
2623 Append_To (L, Instr);
2625 Next_Stored_Discriminant (Discriminant);
2626 end loop;
2627 end;
2628 end if;
2629 end if;
2631 -- For CPP types we generate an implicit call to the C++ default
2632 -- constructor to ensure the proper initialization of the _Tag
2633 -- component.
2635 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
2636 Invoke_Constructor : declare
2637 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
2639 procedure Invoke_IC_Proc (T : Entity_Id);
2640 -- Recursive routine used to climb to parents. Required because
2641 -- parents must be initialized before descendants to ensure
2642 -- propagation of inherited C++ slots.
2644 --------------------
2645 -- Invoke_IC_Proc --
2646 --------------------
2648 procedure Invoke_IC_Proc (T : Entity_Id) is
2649 begin
2650 -- Avoid generating extra calls. Initialization required
2651 -- only for types defined from the level of derivation of
2652 -- type of the constructor and the type of the aggregate.
2654 if T = CPP_Parent then
2655 return;
2656 end if;
2658 Invoke_IC_Proc (Etype (T));
2660 -- Generate call to the IC routine
2662 if Present (CPP_Init_Proc (T)) then
2663 Append_To (L,
2664 Make_Procedure_Call_Statement (Loc,
2665 New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
2666 end if;
2667 end Invoke_IC_Proc;
2669 -- Start of processing for Invoke_Constructor
2671 begin
2672 -- Implicit invocation of the C++ constructor
2674 if Nkind (N) = N_Aggregate then
2675 Append_To (L,
2676 Make_Procedure_Call_Statement (Loc,
2677 Name =>
2678 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
2679 Parameter_Associations => New_List (
2680 Unchecked_Convert_To (CPP_Parent,
2681 New_Copy_Tree (Lhs)))));
2682 end if;
2684 Invoke_IC_Proc (Typ);
2685 end Invoke_Constructor;
2686 end if;
2688 -- Generate the assignments, component by component
2690 -- tmp.comp1 := Expr1_From_Aggr;
2691 -- tmp.comp2 := Expr2_From_Aggr;
2692 -- ....
2694 Comp := First (Component_Associations (N));
2695 while Present (Comp) loop
2696 Selector := Entity (First (Choices (Comp)));
2698 -- C++ constructors
2700 if Is_CPP_Constructor_Call (Expression (Comp)) then
2701 Append_List_To (L,
2702 Build_Initialization_Call (Loc,
2703 Id_Ref =>
2704 Make_Selected_Component (Loc,
2705 Prefix => New_Copy_Tree (Target),
2706 Selector_Name => New_Occurrence_Of (Selector, Loc)),
2707 Typ => Etype (Selector),
2708 Enclos_Type => Typ,
2709 With_Default_Init => True,
2710 Constructor_Ref => Expression (Comp)));
2712 -- Ada 2005 (AI-287): For each default-initialized component generate
2713 -- a call to the corresponding IP subprogram if available.
2715 elsif Box_Present (Comp)
2716 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2717 then
2718 if Ekind (Selector) /= E_Discriminant then
2719 Generate_Finalization_Actions;
2720 end if;
2722 -- Ada 2005 (AI-287): If the component type has tasks then
2723 -- generate the activation chain and master entities (except
2724 -- in case of an allocator because in that case these entities
2725 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2727 declare
2728 Ctype : constant Entity_Id := Etype (Selector);
2729 Inside_Allocator : Boolean := False;
2730 P : Node_Id := Parent (N);
2732 begin
2733 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2734 while Present (P) loop
2735 if Nkind (P) = N_Allocator then
2736 Inside_Allocator := True;
2737 exit;
2738 end if;
2740 P := Parent (P);
2741 end loop;
2743 if not Inside_Init_Proc and not Inside_Allocator then
2744 Build_Activation_Chain_Entity (N);
2745 end if;
2746 end if;
2747 end;
2749 Append_List_To (L,
2750 Build_Initialization_Call (Loc,
2751 Id_Ref => Make_Selected_Component (Loc,
2752 Prefix => New_Copy_Tree (Target),
2753 Selector_Name =>
2754 New_Occurrence_Of (Selector, Loc)),
2755 Typ => Etype (Selector),
2756 Enclos_Type => Typ,
2757 With_Default_Init => True));
2759 -- Prepare for component assignment
2761 elsif Ekind (Selector) /= E_Discriminant
2762 or else Nkind (N) = N_Extension_Aggregate
2763 then
2764 -- All the discriminants have now been assigned
2766 -- This is now a good moment to initialize and attach all the
2767 -- controllers. Their position may depend on the discriminants.
2769 if Ekind (Selector) /= E_Discriminant then
2770 Generate_Finalization_Actions;
2771 end if;
2773 Comp_Type := Underlying_Type (Etype (Selector));
2774 Comp_Expr :=
2775 Make_Selected_Component (Loc,
2776 Prefix => New_Copy_Tree (Target),
2777 Selector_Name => New_Occurrence_Of (Selector, Loc));
2779 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2780 Expr_Q := Expression (Expression (Comp));
2781 else
2782 Expr_Q := Expression (Comp);
2783 end if;
2785 -- Now either create the assignment or generate the code for the
2786 -- inner aggregate top-down.
2788 if Is_Delayed_Aggregate (Expr_Q) then
2790 -- We have the following case of aggregate nesting inside
2791 -- an object declaration:
2793 -- type Arr_Typ is array (Integer range <>) of ...;
2795 -- type Rec_Typ (...) is record
2796 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2797 -- end record;
2799 -- Obj_Rec_Typ : Rec_Typ := (...,
2800 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2802 -- The length of the ranges of the aggregate and Obj_Add_Typ
2803 -- are equal (B - A = Y - X), but they do not coincide (X /=
2804 -- A and B /= Y). This case requires array sliding which is
2805 -- performed in the following manner:
2807 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2808 -- Temp : Arr_Sub;
2809 -- Temp (X) := (...);
2810 -- ...
2811 -- Temp (Y) := (...);
2812 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2814 if Ekind (Comp_Type) = E_Array_Subtype
2815 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2816 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2817 and then not
2818 Compatible_Int_Bounds
2819 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2820 Typ_Bounds => First_Index (Comp_Type))
2821 then
2822 -- Create the array subtype with bounds equal to those of
2823 -- the corresponding aggregate.
2825 declare
2826 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
2828 SubD : constant Node_Id :=
2829 Make_Subtype_Declaration (Loc,
2830 Defining_Identifier => SubE,
2831 Subtype_Indication =>
2832 Make_Subtype_Indication (Loc,
2833 Subtype_Mark =>
2834 New_Occurrence_Of (Etype (Comp_Type), Loc),
2835 Constraint =>
2836 Make_Index_Or_Discriminant_Constraint
2837 (Loc,
2838 Constraints => New_List (
2839 New_Copy_Tree
2840 (Aggregate_Bounds (Expr_Q))))));
2842 -- Create a temporary array of the above subtype which
2843 -- will be used to capture the aggregate assignments.
2845 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
2847 TmpD : constant Node_Id :=
2848 Make_Object_Declaration (Loc,
2849 Defining_Identifier => TmpE,
2850 Object_Definition => New_Occurrence_Of (SubE, Loc));
2852 begin
2853 Set_No_Initialization (TmpD);
2854 Append_To (L, SubD);
2855 Append_To (L, TmpD);
2857 -- Expand aggregate into assignments to the temp array
2859 Append_List_To (L,
2860 Late_Expansion (Expr_Q, Comp_Type,
2861 New_Occurrence_Of (TmpE, Loc)));
2863 -- Slide
2865 Append_To (L,
2866 Make_Assignment_Statement (Loc,
2867 Name => New_Copy_Tree (Comp_Expr),
2868 Expression => New_Occurrence_Of (TmpE, Loc)));
2869 end;
2871 -- Normal case (sliding not required)
2873 else
2874 Append_List_To (L,
2875 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
2876 end if;
2878 -- Expr_Q is not delayed aggregate
2880 else
2881 if Has_Discriminants (Typ) then
2882 Replace_Discriminants (Expr_Q);
2884 -- If the component is an array type that depends on
2885 -- discriminants, and the expression is a single Others
2886 -- clause, create an explicit subtype for it because the
2887 -- backend has troubles recovering the actual bounds.
2889 if Nkind (Expr_Q) = N_Aggregate
2890 and then Is_Array_Type (Comp_Type)
2891 and then Present (Component_Associations (Expr_Q))
2892 then
2893 declare
2894 Assoc : constant Node_Id :=
2895 First (Component_Associations (Expr_Q));
2896 Decl : Node_Id;
2898 begin
2899 if Nkind (First (Choices (Assoc))) = N_Others_Choice
2900 then
2901 Decl :=
2902 Build_Actual_Subtype_Of_Component
2903 (Comp_Type, Comp_Expr);
2905 -- If the component type does not in fact depend on
2906 -- discriminants, the subtype declaration is empty.
2908 if Present (Decl) then
2909 Append_To (L, Decl);
2910 Set_Etype (Comp_Expr, Defining_Entity (Decl));
2911 end if;
2912 end if;
2913 end;
2914 end if;
2915 end if;
2917 Instr :=
2918 Make_OK_Assignment_Statement (Loc,
2919 Name => Comp_Expr,
2920 Expression => Expr_Q);
2922 Set_No_Ctrl_Actions (Instr);
2923 Append_To (L, Instr);
2925 -- Adjust the tag if tagged (because of possible view
2926 -- conversions), unless compiling for a VM where tags are
2927 -- implicit.
2929 -- tmp.comp._tag := comp_typ'tag;
2931 if Is_Tagged_Type (Comp_Type)
2932 and then Tagged_Type_Expansion
2933 then
2934 Instr :=
2935 Make_OK_Assignment_Statement (Loc,
2936 Name =>
2937 Make_Selected_Component (Loc,
2938 Prefix => New_Copy_Tree (Comp_Expr),
2939 Selector_Name =>
2940 New_Occurrence_Of
2941 (First_Tag_Component (Comp_Type), Loc)),
2943 Expression =>
2944 Unchecked_Convert_To (RTE (RE_Tag),
2945 New_Occurrence_Of
2946 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
2947 Loc)));
2949 Append_To (L, Instr);
2950 end if;
2952 -- Generate:
2953 -- Adjust (tmp.comp);
2955 if Needs_Finalization (Comp_Type)
2956 and then not Is_Limited_Type (Comp_Type)
2957 then
2958 Append_To (L,
2959 Make_Adjust_Call
2960 (Obj_Ref => New_Copy_Tree (Comp_Expr),
2961 Typ => Comp_Type));
2962 end if;
2963 end if;
2965 -- comment would be good here ???
2967 elsif Ekind (Selector) = E_Discriminant
2968 and then Nkind (N) /= N_Extension_Aggregate
2969 and then Nkind (Parent (N)) = N_Component_Association
2970 and then Is_Constrained (Typ)
2971 then
2972 -- We must check that the discriminant value imposed by the
2973 -- context is the same as the value given in the subaggregate,
2974 -- because after the expansion into assignments there is no
2975 -- record on which to perform a regular discriminant check.
2977 declare
2978 D_Val : Elmt_Id;
2979 Disc : Entity_Id;
2981 begin
2982 D_Val := First_Elmt (Discriminant_Constraint (Typ));
2983 Disc := First_Discriminant (Typ);
2984 while Chars (Disc) /= Chars (Selector) loop
2985 Next_Discriminant (Disc);
2986 Next_Elmt (D_Val);
2987 end loop;
2989 pragma Assert (Present (D_Val));
2991 -- This check cannot performed for components that are
2992 -- constrained by a current instance, because this is not a
2993 -- value that can be compared with the actual constraint.
2995 if Nkind (Node (D_Val)) /= N_Attribute_Reference
2996 or else not Is_Entity_Name (Prefix (Node (D_Val)))
2997 or else not Is_Type (Entity (Prefix (Node (D_Val))))
2998 then
2999 Append_To (L,
3000 Make_Raise_Constraint_Error (Loc,
3001 Condition =>
3002 Make_Op_Ne (Loc,
3003 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3004 Right_Opnd => Expression (Comp)),
3005 Reason => CE_Discriminant_Check_Failed));
3007 else
3008 -- Find self-reference in previous discriminant assignment,
3009 -- and replace with proper expression.
3011 declare
3012 Ass : Node_Id;
3014 begin
3015 Ass := First (L);
3016 while Present (Ass) loop
3017 if Nkind (Ass) = N_Assignment_Statement
3018 and then Nkind (Name (Ass)) = N_Selected_Component
3019 and then Chars (Selector_Name (Name (Ass))) =
3020 Chars (Disc)
3021 then
3022 Set_Expression
3023 (Ass, New_Copy_Tree (Expression (Comp)));
3024 exit;
3025 end if;
3026 Next (Ass);
3027 end loop;
3028 end;
3029 end if;
3030 end;
3031 end if;
3033 Next (Comp);
3034 end loop;
3036 -- If the type is tagged, the tag needs to be initialized (unless we
3037 -- are in VM-mode where tags are implicit). It is done late in the
3038 -- initialization process because in some cases, we call the init
3039 -- proc of an ancestor which will not leave out the right tag.
3041 if Ancestor_Is_Expression then
3042 null;
3044 -- For CPP types we generated a call to the C++ default constructor
3045 -- before the components have been initialized to ensure the proper
3046 -- initialization of the _Tag component (see above).
3048 elsif Is_CPP_Class (Typ) then
3049 null;
3051 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3052 Instr :=
3053 Make_OK_Assignment_Statement (Loc,
3054 Name =>
3055 Make_Selected_Component (Loc,
3056 Prefix => New_Copy_Tree (Target),
3057 Selector_Name =>
3058 New_Occurrence_Of
3059 (First_Tag_Component (Base_Type (Typ)), Loc)),
3061 Expression =>
3062 Unchecked_Convert_To (RTE (RE_Tag),
3063 New_Occurrence_Of
3064 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3065 Loc)));
3067 Append_To (L, Instr);
3069 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3070 -- abstract interfaces we must also initialize the tags of the
3071 -- secondary dispatch tables.
3073 if Has_Interfaces (Base_Type (Typ)) then
3074 Init_Secondary_Tags
3075 (Typ => Base_Type (Typ),
3076 Target => Target,
3077 Stmts_List => L);
3078 end if;
3079 end if;
3081 -- If the controllers have not been initialized yet (by lack of non-
3082 -- discriminant components), let's do it now.
3084 Generate_Finalization_Actions;
3086 return L;
3087 end Build_Record_Aggr_Code;
3089 ---------------------------------------
3090 -- Collect_Initialization_Statements --
3091 ---------------------------------------
3093 procedure Collect_Initialization_Statements
3094 (Obj : Entity_Id;
3095 N : Node_Id;
3096 Node_After : Node_Id)
3098 Loc : constant Source_Ptr := Sloc (N);
3099 Init_Actions : constant List_Id := New_List;
3100 Init_Node : Node_Id;
3101 Comp_Stmt : Node_Id;
3103 begin
3104 -- Nothing to do if Obj is already frozen, as in this case we known we
3105 -- won't need to move the initialization statements about later on.
3107 if Is_Frozen (Obj) then
3108 return;
3109 end if;
3111 Init_Node := N;
3112 while Next (Init_Node) /= Node_After loop
3113 Append_To (Init_Actions, Remove_Next (Init_Node));
3114 end loop;
3116 if not Is_Empty_List (Init_Actions) then
3117 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3118 Insert_Action_After (Init_Node, Comp_Stmt);
3119 Set_Initialization_Statements (Obj, Comp_Stmt);
3120 end if;
3121 end Collect_Initialization_Statements;
3123 -------------------------------
3124 -- Convert_Aggr_In_Allocator --
3125 -------------------------------
3127 procedure Convert_Aggr_In_Allocator
3128 (Alloc : Node_Id;
3129 Decl : Node_Id;
3130 Aggr : Node_Id)
3132 Loc : constant Source_Ptr := Sloc (Aggr);
3133 Typ : constant Entity_Id := Etype (Aggr);
3134 Temp : constant Entity_Id := Defining_Identifier (Decl);
3136 Occ : constant Node_Id :=
3137 Unchecked_Convert_To (Typ,
3138 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3140 begin
3141 if Is_Array_Type (Typ) then
3142 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3144 elsif Has_Default_Init_Comps (Aggr) then
3145 declare
3146 L : constant List_Id := New_List;
3147 Init_Stmts : List_Id;
3149 begin
3150 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3152 if Has_Task (Typ) then
3153 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3154 Insert_Actions (Alloc, L);
3155 else
3156 Insert_Actions (Alloc, Init_Stmts);
3157 end if;
3158 end;
3160 else
3161 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3162 end if;
3163 end Convert_Aggr_In_Allocator;
3165 --------------------------------
3166 -- Convert_Aggr_In_Assignment --
3167 --------------------------------
3169 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3170 Aggr : Node_Id := Expression (N);
3171 Typ : constant Entity_Id := Etype (Aggr);
3172 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3174 begin
3175 if Nkind (Aggr) = N_Qualified_Expression then
3176 Aggr := Expression (Aggr);
3177 end if;
3179 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3180 end Convert_Aggr_In_Assignment;
3182 ---------------------------------
3183 -- Convert_Aggr_In_Object_Decl --
3184 ---------------------------------
3186 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3187 Obj : constant Entity_Id := Defining_Identifier (N);
3188 Aggr : Node_Id := Expression (N);
3189 Loc : constant Source_Ptr := Sloc (Aggr);
3190 Typ : constant Entity_Id := Etype (Aggr);
3191 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3193 function Discriminants_Ok return Boolean;
3194 -- If the object type is constrained, the discriminants in the
3195 -- aggregate must be checked against the discriminants of the subtype.
3196 -- This cannot be done using Apply_Discriminant_Checks because after
3197 -- expansion there is no aggregate left to check.
3199 ----------------------
3200 -- Discriminants_Ok --
3201 ----------------------
3203 function Discriminants_Ok return Boolean is
3204 Cond : Node_Id := Empty;
3205 Check : Node_Id;
3206 D : Entity_Id;
3207 Disc1 : Elmt_Id;
3208 Disc2 : Elmt_Id;
3209 Val1 : Node_Id;
3210 Val2 : Node_Id;
3212 begin
3213 D := First_Discriminant (Typ);
3214 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3215 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3216 while Present (Disc1) and then Present (Disc2) loop
3217 Val1 := Node (Disc1);
3218 Val2 := Node (Disc2);
3220 if not Is_OK_Static_Expression (Val1)
3221 or else not Is_OK_Static_Expression (Val2)
3222 then
3223 Check := Make_Op_Ne (Loc,
3224 Left_Opnd => Duplicate_Subexpr (Val1),
3225 Right_Opnd => Duplicate_Subexpr (Val2));
3227 if No (Cond) then
3228 Cond := Check;
3230 else
3231 Cond := Make_Or_Else (Loc,
3232 Left_Opnd => Cond,
3233 Right_Opnd => Check);
3234 end if;
3236 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3237 Apply_Compile_Time_Constraint_Error (Aggr,
3238 Msg => "incorrect value for discriminant&??",
3239 Reason => CE_Discriminant_Check_Failed,
3240 Ent => D);
3241 return False;
3242 end if;
3244 Next_Discriminant (D);
3245 Next_Elmt (Disc1);
3246 Next_Elmt (Disc2);
3247 end loop;
3249 -- If any discriminant constraint is non-static, emit a check
3251 if Present (Cond) then
3252 Insert_Action (N,
3253 Make_Raise_Constraint_Error (Loc,
3254 Condition => Cond,
3255 Reason => CE_Discriminant_Check_Failed));
3256 end if;
3258 return True;
3259 end Discriminants_Ok;
3261 -- Start of processing for Convert_Aggr_In_Object_Decl
3263 begin
3264 Set_Assignment_OK (Occ);
3266 if Nkind (Aggr) = N_Qualified_Expression then
3267 Aggr := Expression (Aggr);
3268 end if;
3270 if Has_Discriminants (Typ)
3271 and then Typ /= Etype (Obj)
3272 and then Is_Constrained (Etype (Obj))
3273 and then not Discriminants_Ok
3274 then
3275 return;
3276 end if;
3278 -- If the context is an extended return statement, it has its own
3279 -- finalization machinery (i.e. works like a transient scope) and
3280 -- we do not want to create an additional one, because objects on
3281 -- the finalization list of the return must be moved to the caller's
3282 -- finalization list to complete the return.
3284 -- However, if the aggregate is limited, it is built in place, and the
3285 -- controlled components are not assigned to intermediate temporaries
3286 -- so there is no need for a transient scope in this case either.
3288 if Requires_Transient_Scope (Typ)
3289 and then Ekind (Current_Scope) /= E_Return_Statement
3290 and then not Is_Limited_Type (Typ)
3291 then
3292 Establish_Transient_Scope
3293 (Aggr,
3294 Sec_Stack =>
3295 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3296 end if;
3298 declare
3299 Node_After : constant Node_Id := Next (N);
3300 begin
3301 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3302 Collect_Initialization_Statements (Obj, N, Node_After);
3303 end;
3304 Set_No_Initialization (N);
3305 Initialize_Discriminants (N, Typ);
3306 end Convert_Aggr_In_Object_Decl;
3308 -------------------------------------
3309 -- Convert_Array_Aggr_In_Allocator --
3310 -------------------------------------
3312 procedure Convert_Array_Aggr_In_Allocator
3313 (Decl : Node_Id;
3314 Aggr : Node_Id;
3315 Target : Node_Id)
3317 Aggr_Code : List_Id;
3318 Typ : constant Entity_Id := Etype (Aggr);
3319 Ctyp : constant Entity_Id := Component_Type (Typ);
3321 begin
3322 -- The target is an explicit dereference of the allocated object.
3323 -- Generate component assignments to it, as for an aggregate that
3324 -- appears on the right-hand side of an assignment statement.
3326 Aggr_Code :=
3327 Build_Array_Aggr_Code (Aggr,
3328 Ctype => Ctyp,
3329 Index => First_Index (Typ),
3330 Into => Target,
3331 Scalar_Comp => Is_Scalar_Type (Ctyp));
3333 Insert_Actions_After (Decl, Aggr_Code);
3334 end Convert_Array_Aggr_In_Allocator;
3336 ----------------------------
3337 -- Convert_To_Assignments --
3338 ----------------------------
3340 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3341 Loc : constant Source_Ptr := Sloc (N);
3342 T : Entity_Id;
3343 Temp : Entity_Id;
3345 Aggr_Code : List_Id;
3346 Instr : Node_Id;
3347 Target_Expr : Node_Id;
3348 Parent_Kind : Node_Kind;
3349 Unc_Decl : Boolean := False;
3350 Parent_Node : Node_Id;
3352 begin
3353 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3354 pragma Assert (Is_Record_Type (Typ));
3356 Parent_Node := Parent (N);
3357 Parent_Kind := Nkind (Parent_Node);
3359 if Parent_Kind = N_Qualified_Expression then
3361 -- Check if we are in a unconstrained declaration because in this
3362 -- case the current delayed expansion mechanism doesn't work when
3363 -- the declared object size depend on the initializing expr.
3365 begin
3366 Parent_Node := Parent (Parent_Node);
3367 Parent_Kind := Nkind (Parent_Node);
3369 if Parent_Kind = N_Object_Declaration then
3370 Unc_Decl :=
3371 not Is_Entity_Name (Object_Definition (Parent_Node))
3372 or else Has_Discriminants
3373 (Entity (Object_Definition (Parent_Node)))
3374 or else Is_Class_Wide_Type
3375 (Entity (Object_Definition (Parent_Node)));
3376 end if;
3377 end;
3378 end if;
3380 -- Just set the Delay flag in the cases where the transformation will be
3381 -- done top down from above.
3383 if False
3385 -- Internal aggregate (transformed when expanding the parent)
3387 or else Parent_Kind = N_Aggregate
3388 or else Parent_Kind = N_Extension_Aggregate
3389 or else Parent_Kind = N_Component_Association
3391 -- Allocator (see Convert_Aggr_In_Allocator)
3393 or else Parent_Kind = N_Allocator
3395 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3397 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3399 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3400 -- assignments in init procs are taken into account.
3402 or else (Parent_Kind = N_Assignment_Statement
3403 and then Inside_Init_Proc)
3405 -- (Ada 2005) An inherently limited type in a return statement, which
3406 -- will be handled in a build-in-place fashion, and may be rewritten
3407 -- as an extended return and have its own finalization machinery.
3408 -- In the case of a simple return, the aggregate needs to be delayed
3409 -- until the scope for the return statement has been created, so
3410 -- that any finalization chain will be associated with that scope.
3411 -- For extended returns, we delay expansion to avoid the creation
3412 -- of an unwanted transient scope that could result in premature
3413 -- finalization of the return object (which is built in place
3414 -- within the caller's scope).
3416 or else
3417 (Is_Limited_View (Typ)
3418 and then
3419 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3420 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3421 then
3422 Set_Expansion_Delayed (N);
3423 return;
3424 end if;
3426 -- Otherwise, if a transient scope is required, create it now. If we
3427 -- are within an initialization procedure do not create such, because
3428 -- the target of the assignment must not be declared within a local
3429 -- block, and because cleanup will take place on return from the
3430 -- initialization procedure.
3431 -- Should the condition be more restrictive ???
3433 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
3434 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
3435 end if;
3437 -- If the aggregate is non-limited, create a temporary. If it is limited
3438 -- and context is an assignment, this is a subaggregate for an enclosing
3439 -- aggregate being expanded. It must be built in place, so use target of
3440 -- the current assignment.
3442 if Is_Limited_Type (Typ)
3443 and then Nkind (Parent (N)) = N_Assignment_Statement
3444 then
3445 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3446 Insert_Actions (Parent (N),
3447 Build_Record_Aggr_Code (N, Typ, Target_Expr));
3448 Rewrite (Parent (N), Make_Null_Statement (Loc));
3450 else
3451 Temp := Make_Temporary (Loc, 'A', N);
3453 -- If the type inherits unknown discriminants, use the view with
3454 -- known discriminants if available.
3456 if Has_Unknown_Discriminants (Typ)
3457 and then Present (Underlying_Record_View (Typ))
3458 then
3459 T := Underlying_Record_View (Typ);
3460 else
3461 T := Typ;
3462 end if;
3464 Instr :=
3465 Make_Object_Declaration (Loc,
3466 Defining_Identifier => Temp,
3467 Object_Definition => New_Occurrence_Of (T, Loc));
3469 Set_No_Initialization (Instr);
3470 Insert_Action (N, Instr);
3471 Initialize_Discriminants (Instr, T);
3473 Target_Expr := New_Occurrence_Of (Temp, Loc);
3474 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
3476 -- Save the last assignment statement associated with the aggregate
3477 -- when building a controlled object. This reference is utilized by
3478 -- the finalization machinery when marking an object as successfully
3479 -- initialized.
3481 if Needs_Finalization (T) then
3482 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
3483 end if;
3485 Insert_Actions (N, Aggr_Code);
3486 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3487 Analyze_And_Resolve (N, T);
3488 end if;
3489 end Convert_To_Assignments;
3491 ---------------------------
3492 -- Convert_To_Positional --
3493 ---------------------------
3495 procedure Convert_To_Positional
3496 (N : Node_Id;
3497 Max_Others_Replicate : Nat := 5;
3498 Handle_Bit_Packed : Boolean := False)
3500 Typ : constant Entity_Id := Etype (N);
3502 Static_Components : Boolean := True;
3504 procedure Check_Static_Components;
3505 -- Check whether all components of the aggregate are compile-time known
3506 -- values, and can be passed as is to the back-end without further
3507 -- expansion.
3509 function Flatten
3510 (N : Node_Id;
3511 Ix : Node_Id;
3512 Ixb : Node_Id) return Boolean;
3513 -- Convert the aggregate into a purely positional form if possible. On
3514 -- entry the bounds of all dimensions are known to be static, and the
3515 -- total number of components is safe enough to expand.
3517 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3518 -- Return True iff the array N is flat (which is not trivial in the case
3519 -- of multidimensional aggregates).
3521 -----------------------------
3522 -- Check_Static_Components --
3523 -----------------------------
3525 -- Could use some comments in this body ???
3527 procedure Check_Static_Components is
3528 Expr : Node_Id;
3530 begin
3531 Static_Components := True;
3533 if Nkind (N) = N_String_Literal then
3534 null;
3536 elsif Present (Expressions (N)) then
3537 Expr := First (Expressions (N));
3538 while Present (Expr) loop
3539 if Nkind (Expr) /= N_Aggregate
3540 or else not Compile_Time_Known_Aggregate (Expr)
3541 or else Expansion_Delayed (Expr)
3542 then
3543 Static_Components := False;
3544 exit;
3545 end if;
3547 Next (Expr);
3548 end loop;
3549 end if;
3551 if Nkind (N) = N_Aggregate
3552 and then Present (Component_Associations (N))
3553 then
3554 Expr := First (Component_Associations (N));
3555 while Present (Expr) loop
3556 if Nkind_In (Expression (Expr), N_Integer_Literal,
3557 N_Real_Literal)
3558 then
3559 null;
3561 elsif Is_Entity_Name (Expression (Expr))
3562 and then Present (Entity (Expression (Expr)))
3563 and then Ekind (Entity (Expression (Expr))) =
3564 E_Enumeration_Literal
3565 then
3566 null;
3568 elsif Nkind (Expression (Expr)) /= N_Aggregate
3569 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3570 or else Expansion_Delayed (Expression (Expr))
3571 then
3572 Static_Components := False;
3573 exit;
3574 end if;
3576 Next (Expr);
3577 end loop;
3578 end if;
3579 end Check_Static_Components;
3581 -------------
3582 -- Flatten --
3583 -------------
3585 function Flatten
3586 (N : Node_Id;
3587 Ix : Node_Id;
3588 Ixb : Node_Id) return Boolean
3590 Loc : constant Source_Ptr := Sloc (N);
3591 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3592 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3593 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3594 Lov : Uint;
3595 Hiv : Uint;
3597 Others_Present : Boolean := False;
3599 begin
3600 if Nkind (Original_Node (N)) = N_String_Literal then
3601 return True;
3602 end if;
3604 if not Compile_Time_Known_Value (Lo)
3605 or else not Compile_Time_Known_Value (Hi)
3606 then
3607 return False;
3608 end if;
3610 Lov := Expr_Value (Lo);
3611 Hiv := Expr_Value (Hi);
3613 -- Check if there is an others choice
3615 if Present (Component_Associations (N)) then
3616 declare
3617 Assoc : Node_Id;
3618 Choice : Node_Id;
3620 begin
3621 Assoc := First (Component_Associations (N));
3622 while Present (Assoc) loop
3624 -- If this is a box association, flattening is in general
3625 -- not possible because at this point we cannot tell if the
3626 -- default is static or even exists.
3628 if Box_Present (Assoc) then
3629 return False;
3630 end if;
3632 Choice := First (Choices (Assoc));
3634 while Present (Choice) loop
3635 if Nkind (Choice) = N_Others_Choice then
3636 Others_Present := True;
3637 end if;
3639 Next (Choice);
3640 end loop;
3642 Next (Assoc);
3643 end loop;
3644 end;
3645 end if;
3647 -- If the low bound is not known at compile time and others is not
3648 -- present we can proceed since the bounds can be obtained from the
3649 -- aggregate.
3651 -- Note: This case is required in VM platforms since their backends
3652 -- normalize array indexes in the range 0 .. N-1. Hence, if we do
3653 -- not flat an array whose bounds cannot be obtained from the type
3654 -- of the index the backend has no way to properly generate the code.
3655 -- See ACATS c460010 for an example.
3657 if Hiv < Lov
3658 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
3659 then
3660 return False;
3661 end if;
3663 -- Determine if set of alternatives is suitable for conversion and
3664 -- build an array containing the values in sequence.
3666 declare
3667 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3668 of Node_Id := (others => Empty);
3669 -- The values in the aggregate sorted appropriately
3671 Vlist : List_Id;
3672 -- Same data as Vals in list form
3674 Rep_Count : Nat;
3675 -- Used to validate Max_Others_Replicate limit
3677 Elmt : Node_Id;
3678 Num : Int := UI_To_Int (Lov);
3679 Choice_Index : Int;
3680 Choice : Node_Id;
3681 Lo, Hi : Node_Id;
3683 begin
3684 if Present (Expressions (N)) then
3685 Elmt := First (Expressions (N));
3686 while Present (Elmt) loop
3687 if Nkind (Elmt) = N_Aggregate
3688 and then Present (Next_Index (Ix))
3689 and then
3690 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3691 then
3692 return False;
3693 end if;
3695 Vals (Num) := Relocate_Node (Elmt);
3696 Num := Num + 1;
3698 Next (Elmt);
3699 end loop;
3700 end if;
3702 if No (Component_Associations (N)) then
3703 return True;
3704 end if;
3706 Elmt := First (Component_Associations (N));
3708 if Nkind (Expression (Elmt)) = N_Aggregate then
3709 if Present (Next_Index (Ix))
3710 and then
3711 not Flatten
3712 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3713 then
3714 return False;
3715 end if;
3716 end if;
3718 Component_Loop : while Present (Elmt) loop
3719 Choice := First (Choices (Elmt));
3720 Choice_Loop : while Present (Choice) loop
3722 -- If we have an others choice, fill in the missing elements
3723 -- subject to the limit established by Max_Others_Replicate.
3725 if Nkind (Choice) = N_Others_Choice then
3726 Rep_Count := 0;
3728 for J in Vals'Range loop
3729 if No (Vals (J)) then
3730 Vals (J) := New_Copy_Tree (Expression (Elmt));
3731 Rep_Count := Rep_Count + 1;
3733 -- Check for maximum others replication. Note that
3734 -- we skip this test if either of the restrictions
3735 -- No_Elaboration_Code or No_Implicit_Loops is
3736 -- active, if this is a preelaborable unit or
3737 -- a predefined unit, or if the unit must be
3738 -- placed in data memory. This also ensures that
3739 -- predefined units get the same level of constant
3740 -- folding in Ada 95 and Ada 2005, where their
3741 -- categorization has changed.
3743 declare
3744 P : constant Entity_Id :=
3745 Cunit_Entity (Current_Sem_Unit);
3747 begin
3748 -- Check if duplication OK and if so continue
3749 -- processing.
3751 if Restriction_Active (No_Elaboration_Code)
3752 or else Restriction_Active (No_Implicit_Loops)
3753 or else
3754 (Ekind (Current_Scope) = E_Package
3755 and then Static_Elaboration_Desired
3756 (Current_Scope))
3757 or else Is_Preelaborated (P)
3758 or else (Ekind (P) = E_Package_Body
3759 and then
3760 Is_Preelaborated (Spec_Entity (P)))
3761 or else
3762 Is_Predefined_File_Name
3763 (Unit_File_Name (Get_Source_Unit (P)))
3764 then
3765 null;
3767 -- If duplication not OK, then we return False
3768 -- if the replication count is too high
3770 elsif Rep_Count > Max_Others_Replicate then
3771 return False;
3773 -- Continue on if duplication not OK, but the
3774 -- replication count is not excessive.
3776 else
3777 null;
3778 end if;
3779 end;
3780 end if;
3781 end loop;
3783 exit Component_Loop;
3785 -- Case of a subtype mark, identifier or expanded name
3787 elsif Is_Entity_Name (Choice)
3788 and then Is_Type (Entity (Choice))
3789 then
3790 Lo := Type_Low_Bound (Etype (Choice));
3791 Hi := Type_High_Bound (Etype (Choice));
3793 -- Case of subtype indication
3795 elsif Nkind (Choice) = N_Subtype_Indication then
3796 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3797 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3799 -- Case of a range
3801 elsif Nkind (Choice) = N_Range then
3802 Lo := Low_Bound (Choice);
3803 Hi := High_Bound (Choice);
3805 -- Normal subexpression case
3807 else pragma Assert (Nkind (Choice) in N_Subexpr);
3808 if not Compile_Time_Known_Value (Choice) then
3809 return False;
3811 else
3812 Choice_Index := UI_To_Int (Expr_Value (Choice));
3814 if Choice_Index in Vals'Range then
3815 Vals (Choice_Index) :=
3816 New_Copy_Tree (Expression (Elmt));
3817 goto Continue;
3819 -- Choice is statically out-of-range, will be
3820 -- rewritten to raise Constraint_Error.
3822 else
3823 return False;
3824 end if;
3825 end if;
3826 end if;
3828 -- Range cases merge with Lo,Hi set
3830 if not Compile_Time_Known_Value (Lo)
3831 or else
3832 not Compile_Time_Known_Value (Hi)
3833 then
3834 return False;
3836 else
3837 for J in UI_To_Int (Expr_Value (Lo)) ..
3838 UI_To_Int (Expr_Value (Hi))
3839 loop
3840 Vals (J) := New_Copy_Tree (Expression (Elmt));
3841 end loop;
3842 end if;
3844 <<Continue>>
3845 Next (Choice);
3846 end loop Choice_Loop;
3848 Next (Elmt);
3849 end loop Component_Loop;
3851 -- If we get here the conversion is possible
3853 Vlist := New_List;
3854 for J in Vals'Range loop
3855 Append (Vals (J), Vlist);
3856 end loop;
3858 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3859 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3860 return True;
3861 end;
3862 end Flatten;
3864 -------------
3865 -- Is_Flat --
3866 -------------
3868 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3869 Elmt : Node_Id;
3871 begin
3872 if Dims = 0 then
3873 return True;
3875 elsif Nkind (N) = N_Aggregate then
3876 if Present (Component_Associations (N)) then
3877 return False;
3879 else
3880 Elmt := First (Expressions (N));
3881 while Present (Elmt) loop
3882 if not Is_Flat (Elmt, Dims - 1) then
3883 return False;
3884 end if;
3886 Next (Elmt);
3887 end loop;
3889 return True;
3890 end if;
3891 else
3892 return True;
3893 end if;
3894 end Is_Flat;
3896 -- Start of processing for Convert_To_Positional
3898 begin
3899 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3900 -- components because in this case will need to call the corresponding
3901 -- IP procedure.
3903 if Has_Default_Init_Comps (N) then
3904 return;
3905 end if;
3907 if Is_Flat (N, Number_Dimensions (Typ)) then
3908 return;
3909 end if;
3911 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
3912 return;
3913 end if;
3915 -- Do not convert to positional if controlled components are involved
3916 -- since these require special processing
3918 if Has_Controlled_Component (Typ) then
3919 return;
3920 end if;
3922 Check_Static_Components;
3924 -- If the size is known, or all the components are static, try to
3925 -- build a fully positional aggregate.
3927 -- The size of the type may not be known for an aggregate with
3928 -- discriminated array components, but if the components are static
3929 -- it is still possible to verify statically that the length is
3930 -- compatible with the upper bound of the type, and therefore it is
3931 -- worth flattening such aggregates as well.
3933 -- For now the back-end expands these aggregates into individual
3934 -- assignments to the target anyway, but it is conceivable that
3935 -- it will eventually be able to treat such aggregates statically???
3937 if Aggr_Size_OK (N, Typ)
3938 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3939 then
3940 if Static_Components then
3941 Set_Compile_Time_Known_Aggregate (N);
3942 Set_Expansion_Delayed (N, False);
3943 end if;
3945 Analyze_And_Resolve (N, Typ);
3946 end if;
3948 -- Is Static_Eaboration_Desired has been specified, diagnose aggregates
3949 -- that will still require initialization code.
3951 if (Ekind (Current_Scope) = E_Package
3952 and then Static_Elaboration_Desired (Current_Scope))
3953 and then Nkind (Parent (N)) = N_Object_Declaration
3954 then
3955 declare
3956 Expr : Node_Id;
3958 begin
3959 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
3960 Expr := First (Expressions (N));
3961 while Present (Expr) loop
3962 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
3963 or else
3964 (Is_Entity_Name (Expr)
3965 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
3966 then
3967 null;
3969 else
3970 Error_Msg_N
3971 ("non-static object requires elaboration code??", N);
3972 exit;
3973 end if;
3975 Next (Expr);
3976 end loop;
3978 if Present (Component_Associations (N)) then
3979 Error_Msg_N ("object requires elaboration code??", N);
3980 end if;
3981 end if;
3982 end;
3983 end if;
3984 end Convert_To_Positional;
3986 ----------------------------
3987 -- Expand_Array_Aggregate --
3988 ----------------------------
3990 -- Array aggregate expansion proceeds as follows:
3992 -- 1. If requested we generate code to perform all the array aggregate
3993 -- bound checks, specifically
3995 -- (a) Check that the index range defined by aggregate bounds is
3996 -- compatible with corresponding index subtype.
3998 -- (b) If an others choice is present check that no aggregate
3999 -- index is outside the bounds of the index constraint.
4001 -- (c) For multidimensional arrays make sure that all subaggregates
4002 -- corresponding to the same dimension have the same bounds.
4004 -- 2. Check for packed array aggregate which can be converted to a
4005 -- constant so that the aggregate disappears completely.
4007 -- 3. Check case of nested aggregate. Generally nested aggregates are
4008 -- handled during the processing of the parent aggregate.
4010 -- 4. Check if the aggregate can be statically processed. If this is the
4011 -- case pass it as is to Gigi. Note that a necessary condition for
4012 -- static processing is that the aggregate be fully positional.
4014 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4015 -- a temporary) then mark the aggregate as such and return. Otherwise
4016 -- create a new temporary and generate the appropriate initialization
4017 -- code.
4019 procedure Expand_Array_Aggregate (N : Node_Id) is
4020 Loc : constant Source_Ptr := Sloc (N);
4022 Typ : constant Entity_Id := Etype (N);
4023 Ctyp : constant Entity_Id := Component_Type (Typ);
4024 -- Typ is the correct constrained array subtype of the aggregate
4025 -- Ctyp is the corresponding component type.
4027 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4028 -- Number of aggregate index dimensions
4030 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4031 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4032 -- Low and High bounds of the constraint for each aggregate index
4034 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4035 -- The type of each index
4037 In_Place_Assign_OK_For_Declaration : Boolean := False;
4038 -- True if we are to generate an in place assignment for a declaration
4040 Maybe_In_Place_OK : Boolean;
4041 -- If the type is neither controlled nor packed and the aggregate
4042 -- is the expression in an assignment, assignment in place may be
4043 -- possible, provided other conditions are met on the LHS.
4045 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4046 (others => False);
4047 -- If Others_Present (J) is True, then there is an others choice
4048 -- in one of the sub-aggregates of N at dimension J.
4050 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4051 -- Returns true if an aggregate assignment can be done by the back end
4053 procedure Build_Constrained_Type (Positional : Boolean);
4054 -- If the subtype is not static or unconstrained, build a constrained
4055 -- type using the computable sizes of the aggregate and its sub-
4056 -- aggregates.
4058 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4059 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4060 -- by Index_Bounds.
4062 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4063 -- Checks that in a multi-dimensional array aggregate all subaggregates
4064 -- corresponding to the same dimension have the same bounds.
4065 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4066 -- corresponding to the sub-aggregate.
4068 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4069 -- Computes the values of array Others_Present. Sub_Aggr is the
4070 -- array sub-aggregate we start the computation from. Dim is the
4071 -- dimension corresponding to the sub-aggregate.
4073 function In_Place_Assign_OK return Boolean;
4074 -- Simple predicate to determine whether an aggregate assignment can
4075 -- be done in place, because none of the new values can depend on the
4076 -- components of the target of the assignment.
4078 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4079 -- Checks that if an others choice is present in any sub-aggregate no
4080 -- aggregate index is outside the bounds of the index constraint.
4081 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4082 -- corresponding to the sub-aggregate.
4084 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4085 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4086 -- built directly into the target of the assignment it must be free
4087 -- of side-effects.
4089 ------------------------------------
4090 -- Aggr_Assignment_OK_For_Backend --
4091 ------------------------------------
4093 -- Backend processing by Gigi/gcc is possible only if all the following
4094 -- conditions are met:
4096 -- 1. N consists of a single OTHERS choice, possibly recursively
4098 -- 2. The array type is not packed
4100 -- 3. The array type has no atomic components
4102 -- 4. The array type has no null ranges (the purpose of this is to
4103 -- avoid a bogus warning for an out-of-range value).
4105 -- 5. The component type is discrete
4107 -- 6. The component size is Storage_Unit or the value is of the form
4108 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4109 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4110 -- the 8-bit value M, concatenated together.
4112 -- The ultimate goal is to generate a call to a fast memset routine
4113 -- specifically optimized for the target.
4115 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4116 Ctyp : Entity_Id;
4117 Index : Entity_Id;
4118 Expr : Node_Id := N;
4119 Low : Node_Id;
4120 High : Node_Id;
4121 Remainder : Uint;
4122 Value : Uint;
4123 Nunits : Nat;
4125 begin
4126 -- Recurse as far as possible to find the innermost component type
4128 Ctyp := Etype (N);
4129 while Is_Array_Type (Ctyp) loop
4130 if Nkind (Expr) /= N_Aggregate
4131 or else not Is_Others_Aggregate (Expr)
4132 then
4133 return False;
4134 end if;
4136 if Present (Packed_Array_Impl_Type (Ctyp)) then
4137 return False;
4138 end if;
4140 if Has_Atomic_Components (Ctyp) then
4141 return False;
4142 end if;
4144 Index := First_Index (Ctyp);
4145 while Present (Index) loop
4146 Get_Index_Bounds (Index, Low, High);
4148 if Is_Null_Range (Low, High) then
4149 return False;
4150 end if;
4152 Next_Index (Index);
4153 end loop;
4155 Expr := Expression (First (Component_Associations (Expr)));
4157 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4158 if Nkind (Expr) /= N_Aggregate
4159 or else not Is_Others_Aggregate (Expr)
4160 then
4161 return False;
4162 end if;
4164 Expr := Expression (First (Component_Associations (Expr)));
4165 end loop;
4167 Ctyp := Component_Type (Ctyp);
4169 if Is_Atomic (Ctyp) then
4170 return False;
4171 end if;
4172 end loop;
4174 if not Is_Discrete_Type (Ctyp) then
4175 return False;
4176 end if;
4178 -- The expression needs to be analyzed if True is returned
4180 Analyze_And_Resolve (Expr, Ctyp);
4182 -- The back end uses the Esize as the precision of the type
4184 Nunits := UI_To_Int (Esize (Ctyp)) / System_Storage_Unit;
4186 if Nunits = 1 then
4187 return True;
4188 end if;
4190 if not Compile_Time_Known_Value (Expr) then
4191 return False;
4192 end if;
4194 Value := Expr_Value (Expr);
4196 if Has_Biased_Representation (Ctyp) then
4197 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
4198 end if;
4200 -- Values 0 and -1 immediately satisfy the last check
4202 if Value = Uint_0 or else Value = Uint_Minus_1 then
4203 return True;
4204 end if;
4206 -- We need to work with an unsigned value
4208 if Value < 0 then
4209 Value := Value + 2**(System_Storage_Unit * Nunits);
4210 end if;
4212 Remainder := Value rem 2**System_Storage_Unit;
4214 for J in 1 .. Nunits - 1 loop
4215 Value := Value / 2**System_Storage_Unit;
4217 if Value rem 2**System_Storage_Unit /= Remainder then
4218 return False;
4219 end if;
4220 end loop;
4222 return True;
4223 end Aggr_Assignment_OK_For_Backend;
4225 ----------------------------
4226 -- Build_Constrained_Type --
4227 ----------------------------
4229 procedure Build_Constrained_Type (Positional : Boolean) is
4230 Loc : constant Source_Ptr := Sloc (N);
4231 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4232 Comp : Node_Id;
4233 Decl : Node_Id;
4234 Typ : constant Entity_Id := Etype (N);
4235 Indexes : constant List_Id := New_List;
4236 Num : Int;
4237 Sub_Agg : Node_Id;
4239 begin
4240 -- If the aggregate is purely positional, all its subaggregates
4241 -- have the same size. We collect the dimensions from the first
4242 -- subaggregate at each level.
4244 if Positional then
4245 Sub_Agg := N;
4247 for D in 1 .. Number_Dimensions (Typ) loop
4248 Sub_Agg := First (Expressions (Sub_Agg));
4250 Comp := Sub_Agg;
4251 Num := 0;
4252 while Present (Comp) loop
4253 Num := Num + 1;
4254 Next (Comp);
4255 end loop;
4257 Append_To (Indexes,
4258 Make_Range (Loc,
4259 Low_Bound => Make_Integer_Literal (Loc, 1),
4260 High_Bound => Make_Integer_Literal (Loc, Num)));
4261 end loop;
4263 else
4264 -- We know the aggregate type is unconstrained and the aggregate
4265 -- is not processable by the back end, therefore not necessarily
4266 -- positional. Retrieve each dimension bounds (computed earlier).
4268 for D in 1 .. Number_Dimensions (Typ) loop
4269 Append_To (Indexes,
4270 Make_Range (Loc,
4271 Low_Bound => Aggr_Low (D),
4272 High_Bound => Aggr_High (D)));
4273 end loop;
4274 end if;
4276 Decl :=
4277 Make_Full_Type_Declaration (Loc,
4278 Defining_Identifier => Agg_Type,
4279 Type_Definition =>
4280 Make_Constrained_Array_Definition (Loc,
4281 Discrete_Subtype_Definitions => Indexes,
4282 Component_Definition =>
4283 Make_Component_Definition (Loc,
4284 Aliased_Present => False,
4285 Subtype_Indication =>
4286 New_Occurrence_Of (Component_Type (Typ), Loc))));
4288 Insert_Action (N, Decl);
4289 Analyze (Decl);
4290 Set_Etype (N, Agg_Type);
4291 Set_Is_Itype (Agg_Type);
4292 Freeze_Itype (Agg_Type, N);
4293 end Build_Constrained_Type;
4295 ------------------
4296 -- Check_Bounds --
4297 ------------------
4299 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4300 Aggr_Lo : Node_Id;
4301 Aggr_Hi : Node_Id;
4303 Ind_Lo : Node_Id;
4304 Ind_Hi : Node_Id;
4306 Cond : Node_Id := Empty;
4308 begin
4309 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4310 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4312 -- Generate the following test:
4314 -- [constraint_error when
4315 -- Aggr_Lo <= Aggr_Hi and then
4316 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4318 -- As an optimization try to see if some tests are trivially vacuous
4319 -- because we are comparing an expression against itself.
4321 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4322 Cond := Empty;
4324 elsif Aggr_Hi = Ind_Hi then
4325 Cond :=
4326 Make_Op_Lt (Loc,
4327 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4328 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4330 elsif Aggr_Lo = Ind_Lo then
4331 Cond :=
4332 Make_Op_Gt (Loc,
4333 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4334 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4336 else
4337 Cond :=
4338 Make_Or_Else (Loc,
4339 Left_Opnd =>
4340 Make_Op_Lt (Loc,
4341 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4342 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4344 Right_Opnd =>
4345 Make_Op_Gt (Loc,
4346 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4347 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4348 end if;
4350 if Present (Cond) then
4351 Cond :=
4352 Make_And_Then (Loc,
4353 Left_Opnd =>
4354 Make_Op_Le (Loc,
4355 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4356 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4358 Right_Opnd => Cond);
4360 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4361 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4362 Insert_Action (N,
4363 Make_Raise_Constraint_Error (Loc,
4364 Condition => Cond,
4365 Reason => CE_Range_Check_Failed));
4366 end if;
4367 end Check_Bounds;
4369 ----------------------------
4370 -- Check_Same_Aggr_Bounds --
4371 ----------------------------
4373 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4374 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4375 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4376 -- The bounds of this specific sub-aggregate
4378 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4379 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4380 -- The bounds of the aggregate for this dimension
4382 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4383 -- The index type for this dimension.xxx
4385 Cond : Node_Id := Empty;
4386 Assoc : Node_Id;
4387 Expr : Node_Id;
4389 begin
4390 -- If index checks are on generate the test
4392 -- [constraint_error when
4393 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4395 -- As an optimization try to see if some tests are trivially vacuos
4396 -- because we are comparing an expression against itself. Also for
4397 -- the first dimension the test is trivially vacuous because there
4398 -- is just one aggregate for dimension 1.
4400 if Index_Checks_Suppressed (Ind_Typ) then
4401 Cond := Empty;
4403 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4404 then
4405 Cond := Empty;
4407 elsif Aggr_Hi = Sub_Hi then
4408 Cond :=
4409 Make_Op_Ne (Loc,
4410 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4411 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4413 elsif Aggr_Lo = Sub_Lo then
4414 Cond :=
4415 Make_Op_Ne (Loc,
4416 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4417 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4419 else
4420 Cond :=
4421 Make_Or_Else (Loc,
4422 Left_Opnd =>
4423 Make_Op_Ne (Loc,
4424 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4425 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4427 Right_Opnd =>
4428 Make_Op_Ne (Loc,
4429 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4430 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4431 end if;
4433 if Present (Cond) then
4434 Insert_Action (N,
4435 Make_Raise_Constraint_Error (Loc,
4436 Condition => Cond,
4437 Reason => CE_Length_Check_Failed));
4438 end if;
4440 -- Now look inside the sub-aggregate to see if there is more work
4442 if Dim < Aggr_Dimension then
4444 -- Process positional components
4446 if Present (Expressions (Sub_Aggr)) then
4447 Expr := First (Expressions (Sub_Aggr));
4448 while Present (Expr) loop
4449 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4450 Next (Expr);
4451 end loop;
4452 end if;
4454 -- Process component associations
4456 if Present (Component_Associations (Sub_Aggr)) then
4457 Assoc := First (Component_Associations (Sub_Aggr));
4458 while Present (Assoc) loop
4459 Expr := Expression (Assoc);
4460 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4461 Next (Assoc);
4462 end loop;
4463 end if;
4464 end if;
4465 end Check_Same_Aggr_Bounds;
4467 ----------------------------
4468 -- Compute_Others_Present --
4469 ----------------------------
4471 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4472 Assoc : Node_Id;
4473 Expr : Node_Id;
4475 begin
4476 if Present (Component_Associations (Sub_Aggr)) then
4477 Assoc := Last (Component_Associations (Sub_Aggr));
4479 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4480 Others_Present (Dim) := True;
4481 end if;
4482 end if;
4484 -- Now look inside the sub-aggregate to see if there is more work
4486 if Dim < Aggr_Dimension then
4488 -- Process positional components
4490 if Present (Expressions (Sub_Aggr)) then
4491 Expr := First (Expressions (Sub_Aggr));
4492 while Present (Expr) loop
4493 Compute_Others_Present (Expr, Dim + 1);
4494 Next (Expr);
4495 end loop;
4496 end if;
4498 -- Process component associations
4500 if Present (Component_Associations (Sub_Aggr)) then
4501 Assoc := First (Component_Associations (Sub_Aggr));
4502 while Present (Assoc) loop
4503 Expr := Expression (Assoc);
4504 Compute_Others_Present (Expr, Dim + 1);
4505 Next (Assoc);
4506 end loop;
4507 end if;
4508 end if;
4509 end Compute_Others_Present;
4511 ------------------------
4512 -- In_Place_Assign_OK --
4513 ------------------------
4515 function In_Place_Assign_OK return Boolean is
4516 Aggr_In : Node_Id;
4517 Aggr_Lo : Node_Id;
4518 Aggr_Hi : Node_Id;
4519 Obj_In : Node_Id;
4520 Obj_Lo : Node_Id;
4521 Obj_Hi : Node_Id;
4523 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4524 -- Check recursively that each component of a (sub)aggregate does
4525 -- not depend on the variable being assigned to.
4527 function Safe_Component (Expr : Node_Id) return Boolean;
4528 -- Verify that an expression cannot depend on the variable being
4529 -- assigned to. Room for improvement here (but less than before).
4531 --------------------
4532 -- Safe_Aggregate --
4533 --------------------
4535 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4536 Expr : Node_Id;
4538 begin
4539 if Present (Expressions (Aggr)) then
4540 Expr := First (Expressions (Aggr));
4541 while Present (Expr) loop
4542 if Nkind (Expr) = N_Aggregate then
4543 if not Safe_Aggregate (Expr) then
4544 return False;
4545 end if;
4547 elsif not Safe_Component (Expr) then
4548 return False;
4549 end if;
4551 Next (Expr);
4552 end loop;
4553 end if;
4555 if Present (Component_Associations (Aggr)) then
4556 Expr := First (Component_Associations (Aggr));
4557 while Present (Expr) loop
4558 if Nkind (Expression (Expr)) = N_Aggregate then
4559 if not Safe_Aggregate (Expression (Expr)) then
4560 return False;
4561 end if;
4563 -- If association has a box, no way to determine yet
4564 -- whether default can be assigned in place.
4566 elsif Box_Present (Expr) then
4567 return False;
4569 elsif not Safe_Component (Expression (Expr)) then
4570 return False;
4571 end if;
4573 Next (Expr);
4574 end loop;
4575 end if;
4577 return True;
4578 end Safe_Aggregate;
4580 --------------------
4581 -- Safe_Component --
4582 --------------------
4584 function Safe_Component (Expr : Node_Id) return Boolean is
4585 Comp : Node_Id := Expr;
4587 function Check_Component (Comp : Node_Id) return Boolean;
4588 -- Do the recursive traversal, after copy
4590 ---------------------
4591 -- Check_Component --
4592 ---------------------
4594 function Check_Component (Comp : Node_Id) return Boolean is
4595 begin
4596 if Is_Overloaded (Comp) then
4597 return False;
4598 end if;
4600 return Compile_Time_Known_Value (Comp)
4602 or else (Is_Entity_Name (Comp)
4603 and then Present (Entity (Comp))
4604 and then No (Renamed_Object (Entity (Comp))))
4606 or else (Nkind (Comp) = N_Attribute_Reference
4607 and then Check_Component (Prefix (Comp)))
4609 or else (Nkind (Comp) in N_Binary_Op
4610 and then Check_Component (Left_Opnd (Comp))
4611 and then Check_Component (Right_Opnd (Comp)))
4613 or else (Nkind (Comp) in N_Unary_Op
4614 and then Check_Component (Right_Opnd (Comp)))
4616 or else (Nkind (Comp) = N_Selected_Component
4617 and then Check_Component (Prefix (Comp)))
4619 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4620 and then Check_Component (Expression (Comp)));
4621 end Check_Component;
4623 -- Start of processing for Safe_Component
4625 begin
4626 -- If the component appears in an association that may correspond
4627 -- to more than one element, it is not analyzed before expansion
4628 -- into assignments, to avoid side effects. We analyze, but do not
4629 -- resolve the copy, to obtain sufficient entity information for
4630 -- the checks that follow. If component is overloaded we assume
4631 -- an unsafe function call.
4633 if not Analyzed (Comp) then
4634 if Is_Overloaded (Expr) then
4635 return False;
4637 elsif Nkind (Expr) = N_Aggregate
4638 and then not Is_Others_Aggregate (Expr)
4639 then
4640 return False;
4642 elsif Nkind (Expr) = N_Allocator then
4644 -- For now, too complex to analyze
4646 return False;
4647 end if;
4649 Comp := New_Copy_Tree (Expr);
4650 Set_Parent (Comp, Parent (Expr));
4651 Analyze (Comp);
4652 end if;
4654 if Nkind (Comp) = N_Aggregate then
4655 return Safe_Aggregate (Comp);
4656 else
4657 return Check_Component (Comp);
4658 end if;
4659 end Safe_Component;
4661 -- Start of processing for In_Place_Assign_OK
4663 begin
4664 if Present (Component_Associations (N)) then
4666 -- On assignment, sliding can take place, so we cannot do the
4667 -- assignment in place unless the bounds of the aggregate are
4668 -- statically equal to those of the target.
4670 -- If the aggregate is given by an others choice, the bounds are
4671 -- derived from the left-hand side, and the assignment is safe if
4672 -- the expression is.
4674 if Is_Others_Aggregate (N) then
4675 return
4676 Safe_Component
4677 (Expression (First (Component_Associations (N))));
4678 end if;
4680 Aggr_In := First_Index (Etype (N));
4682 if Nkind (Parent (N)) = N_Assignment_Statement then
4683 Obj_In := First_Index (Etype (Name (Parent (N))));
4685 else
4686 -- Context is an allocator. Check bounds of aggregate against
4687 -- given type in qualified expression.
4689 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4690 Obj_In :=
4691 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4692 end if;
4694 while Present (Aggr_In) loop
4695 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4696 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4698 if not Compile_Time_Known_Value (Aggr_Lo)
4699 or else not Compile_Time_Known_Value (Aggr_Hi)
4700 or else not Compile_Time_Known_Value (Obj_Lo)
4701 or else not Compile_Time_Known_Value (Obj_Hi)
4702 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4703 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4704 then
4705 return False;
4706 end if;
4708 Next_Index (Aggr_In);
4709 Next_Index (Obj_In);
4710 end loop;
4711 end if;
4713 -- Now check the component values themselves
4715 return Safe_Aggregate (N);
4716 end In_Place_Assign_OK;
4718 ------------------
4719 -- Others_Check --
4720 ------------------
4722 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4723 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4724 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4725 -- The bounds of the aggregate for this dimension
4727 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4728 -- The index type for this dimension
4730 Need_To_Check : Boolean := False;
4732 Choices_Lo : Node_Id := Empty;
4733 Choices_Hi : Node_Id := Empty;
4734 -- The lowest and highest discrete choices for a named sub-aggregate
4736 Nb_Choices : Int := -1;
4737 -- The number of discrete non-others choices in this sub-aggregate
4739 Nb_Elements : Uint := Uint_0;
4740 -- The number of elements in a positional aggregate
4742 Cond : Node_Id := Empty;
4744 Assoc : Node_Id;
4745 Choice : Node_Id;
4746 Expr : Node_Id;
4748 begin
4749 -- Check if we have an others choice. If we do make sure that this
4750 -- sub-aggregate contains at least one element in addition to the
4751 -- others choice.
4753 if Range_Checks_Suppressed (Ind_Typ) then
4754 Need_To_Check := False;
4756 elsif Present (Expressions (Sub_Aggr))
4757 and then Present (Component_Associations (Sub_Aggr))
4758 then
4759 Need_To_Check := True;
4761 elsif Present (Component_Associations (Sub_Aggr)) then
4762 Assoc := Last (Component_Associations (Sub_Aggr));
4764 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4765 Need_To_Check := False;
4767 else
4768 -- Count the number of discrete choices. Start with -1 because
4769 -- the others choice does not count.
4771 -- Is there some reason we do not use List_Length here ???
4773 Nb_Choices := -1;
4774 Assoc := First (Component_Associations (Sub_Aggr));
4775 while Present (Assoc) loop
4776 Choice := First (Choices (Assoc));
4777 while Present (Choice) loop
4778 Nb_Choices := Nb_Choices + 1;
4779 Next (Choice);
4780 end loop;
4782 Next (Assoc);
4783 end loop;
4785 -- If there is only an others choice nothing to do
4787 Need_To_Check := (Nb_Choices > 0);
4788 end if;
4790 else
4791 Need_To_Check := False;
4792 end if;
4794 -- If we are dealing with a positional sub-aggregate with an others
4795 -- choice then compute the number or positional elements.
4797 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4798 Expr := First (Expressions (Sub_Aggr));
4799 Nb_Elements := Uint_0;
4800 while Present (Expr) loop
4801 Nb_Elements := Nb_Elements + 1;
4802 Next (Expr);
4803 end loop;
4805 -- If the aggregate contains discrete choices and an others choice
4806 -- compute the smallest and largest discrete choice values.
4808 elsif Need_To_Check then
4809 Compute_Choices_Lo_And_Choices_Hi : declare
4811 Table : Case_Table_Type (1 .. Nb_Choices);
4812 -- Used to sort all the different choice values
4814 J : Pos := 1;
4815 Low : Node_Id;
4816 High : Node_Id;
4818 begin
4819 Assoc := First (Component_Associations (Sub_Aggr));
4820 while Present (Assoc) loop
4821 Choice := First (Choices (Assoc));
4822 while Present (Choice) loop
4823 if Nkind (Choice) = N_Others_Choice then
4824 exit;
4825 end if;
4827 Get_Index_Bounds (Choice, Low, High);
4828 Table (J).Choice_Lo := Low;
4829 Table (J).Choice_Hi := High;
4831 J := J + 1;
4832 Next (Choice);
4833 end loop;
4835 Next (Assoc);
4836 end loop;
4838 -- Sort the discrete choices
4840 Sort_Case_Table (Table);
4842 Choices_Lo := Table (1).Choice_Lo;
4843 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4844 end Compute_Choices_Lo_And_Choices_Hi;
4845 end if;
4847 -- If no others choice in this sub-aggregate, or the aggregate
4848 -- comprises only an others choice, nothing to do.
4850 if not Need_To_Check then
4851 Cond := Empty;
4853 -- If we are dealing with an aggregate containing an others choice
4854 -- and positional components, we generate the following test:
4856 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4857 -- Ind_Typ'Pos (Aggr_Hi)
4858 -- then
4859 -- raise Constraint_Error;
4860 -- end if;
4862 elsif Nb_Elements > Uint_0 then
4863 Cond :=
4864 Make_Op_Gt (Loc,
4865 Left_Opnd =>
4866 Make_Op_Add (Loc,
4867 Left_Opnd =>
4868 Make_Attribute_Reference (Loc,
4869 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
4870 Attribute_Name => Name_Pos,
4871 Expressions =>
4872 New_List
4873 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4874 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4876 Right_Opnd =>
4877 Make_Attribute_Reference (Loc,
4878 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
4879 Attribute_Name => Name_Pos,
4880 Expressions => New_List (
4881 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4883 -- If we are dealing with an aggregate containing an others choice
4884 -- and discrete choices we generate the following test:
4886 -- [constraint_error when
4887 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4889 else
4890 Cond :=
4891 Make_Or_Else (Loc,
4892 Left_Opnd =>
4893 Make_Op_Lt (Loc,
4894 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
4895 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4897 Right_Opnd =>
4898 Make_Op_Gt (Loc,
4899 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
4900 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
4901 end if;
4903 if Present (Cond) then
4904 Insert_Action (N,
4905 Make_Raise_Constraint_Error (Loc,
4906 Condition => Cond,
4907 Reason => CE_Length_Check_Failed));
4908 -- Questionable reason code, shouldn't that be a
4909 -- CE_Range_Check_Failed ???
4910 end if;
4912 -- Now look inside the sub-aggregate to see if there is more work
4914 if Dim < Aggr_Dimension then
4916 -- Process positional components
4918 if Present (Expressions (Sub_Aggr)) then
4919 Expr := First (Expressions (Sub_Aggr));
4920 while Present (Expr) loop
4921 Others_Check (Expr, Dim + 1);
4922 Next (Expr);
4923 end loop;
4924 end if;
4926 -- Process component associations
4928 if Present (Component_Associations (Sub_Aggr)) then
4929 Assoc := First (Component_Associations (Sub_Aggr));
4930 while Present (Assoc) loop
4931 Expr := Expression (Assoc);
4932 Others_Check (Expr, Dim + 1);
4933 Next (Assoc);
4934 end loop;
4935 end if;
4936 end if;
4937 end Others_Check;
4939 -------------------------
4940 -- Safe_Left_Hand_Side --
4941 -------------------------
4943 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
4944 function Is_Safe_Index (Indx : Node_Id) return Boolean;
4945 -- If the left-hand side includes an indexed component, check that
4946 -- the indexes are free of side-effect.
4948 -------------------
4949 -- Is_Safe_Index --
4950 -------------------
4952 function Is_Safe_Index (Indx : Node_Id) return Boolean is
4953 begin
4954 if Is_Entity_Name (Indx) then
4955 return True;
4957 elsif Nkind (Indx) = N_Integer_Literal then
4958 return True;
4960 elsif Nkind (Indx) = N_Function_Call
4961 and then Is_Entity_Name (Name (Indx))
4962 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
4963 then
4964 return True;
4966 elsif Nkind (Indx) = N_Type_Conversion
4967 and then Is_Safe_Index (Expression (Indx))
4968 then
4969 return True;
4971 else
4972 return False;
4973 end if;
4974 end Is_Safe_Index;
4976 -- Start of processing for Safe_Left_Hand_Side
4978 begin
4979 if Is_Entity_Name (N) then
4980 return True;
4982 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
4983 and then Safe_Left_Hand_Side (Prefix (N))
4984 then
4985 return True;
4987 elsif Nkind (N) = N_Indexed_Component
4988 and then Safe_Left_Hand_Side (Prefix (N))
4989 and then Is_Safe_Index (First (Expressions (N)))
4990 then
4991 return True;
4993 elsif Nkind (N) = N_Unchecked_Type_Conversion then
4994 return Safe_Left_Hand_Side (Expression (N));
4996 else
4997 return False;
4998 end if;
4999 end Safe_Left_Hand_Side;
5001 -- Local variables
5003 Tmp : Entity_Id;
5004 -- Holds the temporary aggregate value
5006 Tmp_Decl : Node_Id;
5007 -- Holds the declaration of Tmp
5009 Aggr_Code : List_Id;
5010 Parent_Node : Node_Id;
5011 Parent_Kind : Node_Kind;
5013 -- Start of processing for Expand_Array_Aggregate
5015 begin
5016 -- Do not touch the special aggregates of attributes used for Asm calls
5018 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5019 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5020 then
5021 return;
5023 -- Do not expand an aggregate for an array type which contains tasks if
5024 -- the aggregate is associated with an unexpanded return statement of a
5025 -- build-in-place function. The aggregate is expanded when the related
5026 -- return statement (rewritten into an extended return) is processed.
5027 -- This delay ensures that any temporaries and initialization code
5028 -- generated for the aggregate appear in the proper return block and
5029 -- use the correct _chain and _master.
5031 elsif Has_Task (Base_Type (Etype (N)))
5032 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5033 and then Is_Build_In_Place_Function
5034 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5035 then
5036 return;
5038 -- Do not attempt expansion if error already detected. We may reach this
5039 -- point in spite of previous errors when compiling with -gnatq, to
5040 -- force all possible errors (this is the usual ACATS mode).
5042 elsif Error_Posted (N) then
5043 return;
5044 end if;
5046 -- If the semantic analyzer has determined that aggregate N will raise
5047 -- Constraint_Error at run time, then the aggregate node has been
5048 -- replaced with an N_Raise_Constraint_Error node and we should
5049 -- never get here.
5051 pragma Assert (not Raises_Constraint_Error (N));
5053 -- STEP 1a
5055 -- Check that the index range defined by aggregate bounds is
5056 -- compatible with corresponding index subtype.
5058 Index_Compatibility_Check : declare
5059 Aggr_Index_Range : Node_Id := First_Index (Typ);
5060 -- The current aggregate index range
5062 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5063 -- The corresponding index constraint against which we have to
5064 -- check the above aggregate index range.
5066 begin
5067 Compute_Others_Present (N, 1);
5069 for J in 1 .. Aggr_Dimension loop
5070 -- There is no need to emit a check if an others choice is present
5071 -- for this array aggregate dimension since in this case one of
5072 -- N's sub-aggregates has taken its bounds from the context and
5073 -- these bounds must have been checked already. In addition all
5074 -- sub-aggregates corresponding to the same dimension must all
5075 -- have the same bounds (checked in (c) below).
5077 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5078 and then not Others_Present (J)
5079 then
5080 -- We don't use Checks.Apply_Range_Check here because it emits
5081 -- a spurious check. Namely it checks that the range defined by
5082 -- the aggregate bounds is non empty. But we know this already
5083 -- if we get here.
5085 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5086 end if;
5088 -- Save the low and high bounds of the aggregate index as well as
5089 -- the index type for later use in checks (b) and (c) below.
5091 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5092 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5094 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5096 Next_Index (Aggr_Index_Range);
5097 Next_Index (Index_Constraint);
5098 end loop;
5099 end Index_Compatibility_Check;
5101 -- STEP 1b
5103 -- If an others choice is present check that no aggregate index is
5104 -- outside the bounds of the index constraint.
5106 Others_Check (N, 1);
5108 -- STEP 1c
5110 -- For multidimensional arrays make sure that all subaggregates
5111 -- corresponding to the same dimension have the same bounds.
5113 if Aggr_Dimension > 1 then
5114 Check_Same_Aggr_Bounds (N, 1);
5115 end if;
5117 -- STEP 1d
5119 -- If we have a default component value, or simple initialization is
5120 -- required for the component type, then we replace <> in component
5121 -- associations by the required default value.
5123 declare
5124 Default_Val : Node_Id;
5125 Assoc : Node_Id;
5127 begin
5128 if (Present (Default_Aspect_Component_Value (Typ))
5129 or else Needs_Simple_Initialization (Ctyp))
5130 and then Present (Component_Associations (N))
5131 then
5132 Assoc := First (Component_Associations (N));
5133 while Present (Assoc) loop
5134 if Nkind (Assoc) = N_Component_Association
5135 and then Box_Present (Assoc)
5136 then
5137 Set_Box_Present (Assoc, False);
5139 if Present (Default_Aspect_Component_Value (Typ)) then
5140 Default_Val := Default_Aspect_Component_Value (Typ);
5141 else
5142 Default_Val := Get_Simple_Init_Val (Ctyp, N);
5143 end if;
5145 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
5146 Analyze_And_Resolve (Expression (Assoc), Ctyp);
5147 end if;
5149 Next (Assoc);
5150 end loop;
5151 end if;
5152 end;
5154 -- STEP 2
5156 -- Here we test for is packed array aggregate that we can handle at
5157 -- compile time. If so, return with transformation done. Note that we do
5158 -- this even if the aggregate is nested, because once we have done this
5159 -- processing, there is no more nested aggregate.
5161 if Packed_Array_Aggregate_Handled (N) then
5162 return;
5163 end if;
5165 -- At this point we try to convert to positional form
5167 if Ekind (Current_Scope) = E_Package
5168 and then Static_Elaboration_Desired (Current_Scope)
5169 then
5170 Convert_To_Positional (N, Max_Others_Replicate => 100);
5171 else
5172 Convert_To_Positional (N);
5173 end if;
5175 -- if the result is no longer an aggregate (e.g. it may be a string
5176 -- literal, or a temporary which has the needed value), then we are
5177 -- done, since there is no longer a nested aggregate.
5179 if Nkind (N) /= N_Aggregate then
5180 return;
5182 -- We are also done if the result is an analyzed aggregate, indicating
5183 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5184 -- aggregate.
5186 elsif Analyzed (N) and then N /= Original_Node (N) then
5187 return;
5188 end if;
5190 -- If all aggregate components are compile-time known and the aggregate
5191 -- has been flattened, nothing left to do. The same occurs if the
5192 -- aggregate is used to initialize the components of a statically
5193 -- allocated dispatch table.
5195 if Compile_Time_Known_Aggregate (N)
5196 or else Is_Static_Dispatch_Table_Aggregate (N)
5197 then
5198 Set_Expansion_Delayed (N, False);
5199 return;
5200 end if;
5202 -- Now see if back end processing is possible
5204 if Backend_Processing_Possible (N) then
5206 -- If the aggregate is static but the constraints are not, build
5207 -- a static subtype for the aggregate, so that Gigi can place it
5208 -- in static memory. Perform an unchecked_conversion to the non-
5209 -- static type imposed by the context.
5211 declare
5212 Itype : constant Entity_Id := Etype (N);
5213 Index : Node_Id;
5214 Needs_Type : Boolean := False;
5216 begin
5217 Index := First_Index (Itype);
5218 while Present (Index) loop
5219 if not Is_OK_Static_Subtype (Etype (Index)) then
5220 Needs_Type := True;
5221 exit;
5222 else
5223 Next_Index (Index);
5224 end if;
5225 end loop;
5227 if Needs_Type then
5228 Build_Constrained_Type (Positional => True);
5229 Rewrite (N, Unchecked_Convert_To (Itype, N));
5230 Analyze (N);
5231 end if;
5232 end;
5234 return;
5235 end if;
5237 -- STEP 3
5239 -- Delay expansion for nested aggregates: it will be taken care of
5240 -- when the parent aggregate is expanded.
5242 Parent_Node := Parent (N);
5243 Parent_Kind := Nkind (Parent_Node);
5245 if Parent_Kind = N_Qualified_Expression then
5246 Parent_Node := Parent (Parent_Node);
5247 Parent_Kind := Nkind (Parent_Node);
5248 end if;
5250 if Parent_Kind = N_Aggregate
5251 or else Parent_Kind = N_Extension_Aggregate
5252 or else Parent_Kind = N_Component_Association
5253 or else (Parent_Kind = N_Object_Declaration
5254 and then Needs_Finalization (Typ))
5255 or else (Parent_Kind = N_Assignment_Statement
5256 and then Inside_Init_Proc)
5257 then
5258 if Static_Array_Aggregate (N)
5259 or else Compile_Time_Known_Aggregate (N)
5260 then
5261 Set_Expansion_Delayed (N, False);
5262 return;
5263 else
5264 Set_Expansion_Delayed (N);
5265 return;
5266 end if;
5267 end if;
5269 -- STEP 4
5271 -- Look if in place aggregate expansion is possible
5273 -- For object declarations we build the aggregate in place, unless
5274 -- the array is bit-packed or the component is controlled.
5276 -- For assignments we do the assignment in place if all the component
5277 -- associations have compile-time known values. For other cases we
5278 -- create a temporary. The analysis for safety of on-line assignment
5279 -- is delicate, i.e. we don't know how to do it fully yet ???
5281 -- For allocators we assign to the designated object in place if the
5282 -- aggregate meets the same conditions as other in-place assignments.
5283 -- In this case the aggregate may not come from source but was created
5284 -- for default initialization, e.g. with Initialize_Scalars.
5286 if Requires_Transient_Scope (Typ) then
5287 Establish_Transient_Scope
5288 (N, Sec_Stack => Has_Controlled_Component (Typ));
5289 end if;
5291 if Has_Default_Init_Comps (N) then
5292 Maybe_In_Place_OK := False;
5294 elsif Is_Bit_Packed_Array (Typ)
5295 or else Has_Controlled_Component (Typ)
5296 then
5297 Maybe_In_Place_OK := False;
5299 else
5300 Maybe_In_Place_OK :=
5301 (Nkind (Parent (N)) = N_Assignment_Statement
5302 and then In_Place_Assign_OK)
5304 or else
5305 (Nkind (Parent (Parent (N))) = N_Allocator
5306 and then In_Place_Assign_OK);
5307 end if;
5309 -- If this is an array of tasks, it will be expanded into build-in-place
5310 -- assignments. Build an activation chain for the tasks now.
5312 if Has_Task (Etype (N)) then
5313 Build_Activation_Chain_Entity (N);
5314 end if;
5316 -- Perform in-place expansion of aggregate in an object declaration.
5317 -- Note: actions generated for the aggregate will be captured in an
5318 -- expression-with-actions statement so that they can be transferred
5319 -- to freeze actions later if there is an address clause for the
5320 -- object. (Note: we don't use a block statement because this would
5321 -- cause generated freeze nodes to be elaborated in the wrong scope).
5323 -- Should document these individual tests ???
5325 if not Has_Default_Init_Comps (N)
5326 and then Comes_From_Source (Parent_Node)
5327 and then Parent_Kind = N_Object_Declaration
5328 and then not
5329 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
5330 and then N = Expression (Parent_Node)
5331 and then not Is_Bit_Packed_Array (Typ)
5332 and then not Has_Controlled_Component (Typ)
5333 then
5334 In_Place_Assign_OK_For_Declaration := True;
5335 Tmp := Defining_Identifier (Parent (N));
5336 Set_No_Initialization (Parent (N));
5337 Set_Expression (Parent (N), Empty);
5339 -- Set kind and type of the entity, for use in the analysis
5340 -- of the subsequent assignments. If the nominal type is not
5341 -- constrained, build a subtype from the known bounds of the
5342 -- aggregate. If the declaration has a subtype mark, use it,
5343 -- otherwise use the itype of the aggregate.
5345 Set_Ekind (Tmp, E_Variable);
5347 if not Is_Constrained (Typ) then
5348 Build_Constrained_Type (Positional => False);
5350 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5351 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5352 then
5353 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5355 else
5356 Set_Size_Known_At_Compile_Time (Typ, False);
5357 Set_Etype (Tmp, Typ);
5358 end if;
5360 elsif Maybe_In_Place_OK
5361 and then Nkind (Parent (N)) = N_Qualified_Expression
5362 and then Nkind (Parent (Parent (N))) = N_Allocator
5363 then
5364 Set_Expansion_Delayed (N);
5365 return;
5367 -- In the remaining cases the aggregate is the RHS of an assignment
5369 elsif Maybe_In_Place_OK
5370 and then Safe_Left_Hand_Side (Name (Parent (N)))
5371 then
5372 Tmp := Name (Parent (N));
5374 if Etype (Tmp) /= Etype (N) then
5375 Apply_Length_Check (N, Etype (Tmp));
5377 if Nkind (N) = N_Raise_Constraint_Error then
5379 -- Static error, nothing further to expand
5381 return;
5382 end if;
5383 end if;
5385 -- If a slice assignment has an aggregate with a single others_choice,
5386 -- the assignment can be done in place even if bounds are not static,
5387 -- by converting it into a loop over the discrete range of the slice.
5389 elsif Maybe_In_Place_OK
5390 and then Nkind (Name (Parent (N))) = N_Slice
5391 and then Is_Others_Aggregate (N)
5392 then
5393 Tmp := Name (Parent (N));
5395 -- Set type of aggregate to be type of lhs in assignment, in order
5396 -- to suppress redundant length checks.
5398 Set_Etype (N, Etype (Tmp));
5400 -- Step 5
5402 -- In place aggregate expansion is not possible
5404 else
5405 Maybe_In_Place_OK := False;
5406 Tmp := Make_Temporary (Loc, 'A', N);
5407 Tmp_Decl :=
5408 Make_Object_Declaration (Loc,
5409 Defining_Identifier => Tmp,
5410 Object_Definition => New_Occurrence_Of (Typ, Loc));
5411 Set_No_Initialization (Tmp_Decl, True);
5413 -- If we are within a loop, the temporary will be pushed on the
5414 -- stack at each iteration. If the aggregate is the expression for an
5415 -- allocator, it will be immediately copied to the heap and can
5416 -- be reclaimed at once. We create a transient scope around the
5417 -- aggregate for this purpose.
5419 if Ekind (Current_Scope) = E_Loop
5420 and then Nkind (Parent (Parent (N))) = N_Allocator
5421 then
5422 Establish_Transient_Scope (N, False);
5423 end if;
5425 Insert_Action (N, Tmp_Decl);
5426 end if;
5428 -- Construct and insert the aggregate code. We can safely suppress index
5429 -- checks because this code is guaranteed not to raise CE on index
5430 -- checks. However we should *not* suppress all checks.
5432 declare
5433 Target : Node_Id;
5435 begin
5436 if Nkind (Tmp) = N_Defining_Identifier then
5437 Target := New_Occurrence_Of (Tmp, Loc);
5439 else
5440 if Has_Default_Init_Comps (N) then
5442 -- Ada 2005 (AI-287): This case has not been analyzed???
5444 raise Program_Error;
5445 end if;
5447 -- Name in assignment is explicit dereference
5449 Target := New_Copy (Tmp);
5450 end if;
5452 -- If we are to generate an in place assignment for a declaration or
5453 -- an assignment statement, and the assignment can be done directly
5454 -- by the back end, then do not expand further.
5456 -- ??? We can also do that if in place expansion is not possible but
5457 -- then we could go into an infinite recursion.
5459 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
5460 and then VM_Target = No_VM
5461 and then not AAMP_On_Target
5462 and then not Generate_SCIL
5463 and then not Possible_Bit_Aligned_Component (Target)
5464 and then not Is_Possibly_Unaligned_Slice (Target)
5465 and then Aggr_Assignment_OK_For_Backend (N)
5466 then
5467 if Maybe_In_Place_OK then
5468 return;
5469 end if;
5471 Aggr_Code :=
5472 New_List (
5473 Make_Assignment_Statement (Loc,
5474 Name => Target,
5475 Expression => New_Copy (N)));
5477 else
5478 Aggr_Code :=
5479 Build_Array_Aggr_Code (N,
5480 Ctype => Ctyp,
5481 Index => First_Index (Typ),
5482 Into => Target,
5483 Scalar_Comp => Is_Scalar_Type (Ctyp));
5484 end if;
5486 -- Save the last assignment statement associated with the aggregate
5487 -- when building a controlled object. This reference is utilized by
5488 -- the finalization machinery when marking an object as successfully
5489 -- initialized.
5491 if Needs_Finalization (Typ)
5492 and then Is_Entity_Name (Target)
5493 and then Present (Entity (Target))
5494 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
5495 then
5496 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
5497 end if;
5498 end;
5500 -- If the aggregate is the expression in a declaration, the expanded
5501 -- code must be inserted after it. The defining entity might not come
5502 -- from source if this is part of an inlined body, but the declaration
5503 -- itself will.
5505 if Comes_From_Source (Tmp)
5506 or else
5507 (Nkind (Parent (N)) = N_Object_Declaration
5508 and then Comes_From_Source (Parent (N))
5509 and then Tmp = Defining_Entity (Parent (N)))
5510 then
5511 declare
5512 Node_After : constant Node_Id := Next (Parent_Node);
5514 begin
5515 Insert_Actions_After (Parent_Node, Aggr_Code);
5517 if Parent_Kind = N_Object_Declaration then
5518 Collect_Initialization_Statements
5519 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
5520 end if;
5521 end;
5523 else
5524 Insert_Actions (N, Aggr_Code);
5525 end if;
5527 -- If the aggregate has been assigned in place, remove the original
5528 -- assignment.
5530 if Nkind (Parent (N)) = N_Assignment_Statement
5531 and then Maybe_In_Place_OK
5532 then
5533 Rewrite (Parent (N), Make_Null_Statement (Loc));
5535 elsif Nkind (Parent (N)) /= N_Object_Declaration
5536 or else Tmp /= Defining_Identifier (Parent (N))
5537 then
5538 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5539 Analyze_And_Resolve (N, Typ);
5540 end if;
5541 end Expand_Array_Aggregate;
5543 ------------------------
5544 -- Expand_N_Aggregate --
5545 ------------------------
5547 procedure Expand_N_Aggregate (N : Node_Id) is
5548 begin
5549 -- Record aggregate case
5551 if Is_Record_Type (Etype (N)) then
5552 Expand_Record_Aggregate (N);
5554 -- Array aggregate case
5556 else
5557 -- A special case, if we have a string subtype with bounds 1 .. N,
5558 -- where N is known at compile time, and the aggregate is of the
5559 -- form (others => 'x'), with a single choice and no expressions,
5560 -- and N is less than 80 (an arbitrary limit for now), then replace
5561 -- the aggregate by the equivalent string literal (but do not mark
5562 -- it as static since it is not).
5564 -- Note: this entire circuit is redundant with respect to code in
5565 -- Expand_Array_Aggregate that collapses others choices to positional
5566 -- form, but there are two problems with that circuit:
5568 -- a) It is limited to very small cases due to ill-understood
5569 -- interactions with bootstrapping. That limit is removed by
5570 -- use of the No_Implicit_Loops restriction.
5572 -- b) It incorrectly ends up with the resulting expressions being
5573 -- considered static when they are not. For example, the
5574 -- following test should fail:
5576 -- pragma Restrictions (No_Implicit_Loops);
5577 -- package NonSOthers4 is
5578 -- B : constant String (1 .. 6) := (others => 'A');
5579 -- DH : constant String (1 .. 8) := B & "BB";
5580 -- X : Integer;
5581 -- pragma Export (C, X, Link_Name => DH);
5582 -- end;
5584 -- But it succeeds (DH looks static to pragma Export)
5586 -- To be sorted out ???
5588 if Present (Component_Associations (N)) then
5589 declare
5590 CA : constant Node_Id := First (Component_Associations (N));
5591 MX : constant := 80;
5593 begin
5594 if Nkind (First (Choices (CA))) = N_Others_Choice
5595 and then Nkind (Expression (CA)) = N_Character_Literal
5596 and then No (Expressions (N))
5597 then
5598 declare
5599 T : constant Entity_Id := Etype (N);
5600 X : constant Node_Id := First_Index (T);
5601 EC : constant Node_Id := Expression (CA);
5602 CV : constant Uint := Char_Literal_Value (EC);
5603 CC : constant Int := UI_To_Int (CV);
5605 begin
5606 if Nkind (X) = N_Range
5607 and then Compile_Time_Known_Value (Low_Bound (X))
5608 and then Expr_Value (Low_Bound (X)) = 1
5609 and then Compile_Time_Known_Value (High_Bound (X))
5610 then
5611 declare
5612 Hi : constant Uint := Expr_Value (High_Bound (X));
5614 begin
5615 if Hi <= MX then
5616 Start_String;
5618 for J in 1 .. UI_To_Int (Hi) loop
5619 Store_String_Char (Char_Code (CC));
5620 end loop;
5622 Rewrite (N,
5623 Make_String_Literal (Sloc (N),
5624 Strval => End_String));
5626 if CC >= Int (2 ** 16) then
5627 Set_Has_Wide_Wide_Character (N);
5628 elsif CC >= Int (2 ** 8) then
5629 Set_Has_Wide_Character (N);
5630 end if;
5632 Analyze_And_Resolve (N, T);
5633 Set_Is_Static_Expression (N, False);
5634 return;
5635 end if;
5636 end;
5637 end if;
5638 end;
5639 end if;
5640 end;
5641 end if;
5643 -- Not that special case, so normal expansion of array aggregate
5645 Expand_Array_Aggregate (N);
5646 end if;
5648 exception
5649 when RE_Not_Available =>
5650 return;
5651 end Expand_N_Aggregate;
5653 ----------------------------------
5654 -- Expand_N_Extension_Aggregate --
5655 ----------------------------------
5657 -- If the ancestor part is an expression, add a component association for
5658 -- the parent field. If the type of the ancestor part is not the direct
5659 -- parent of the expected type, build recursively the needed ancestors.
5660 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5661 -- ration for a temporary of the expected type, followed by individual
5662 -- assignments to the given components.
5664 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5665 Loc : constant Source_Ptr := Sloc (N);
5666 A : constant Node_Id := Ancestor_Part (N);
5667 Typ : constant Entity_Id := Etype (N);
5669 begin
5670 -- If the ancestor is a subtype mark, an init proc must be called
5671 -- on the resulting object which thus has to be materialized in
5672 -- the front-end
5674 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5675 Convert_To_Assignments (N, Typ);
5677 -- The extension aggregate is transformed into a record aggregate
5678 -- of the following form (c1 and c2 are inherited components)
5680 -- (Exp with c3 => a, c4 => b)
5681 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5683 else
5684 Set_Etype (N, Typ);
5686 if Tagged_Type_Expansion then
5687 Expand_Record_Aggregate (N,
5688 Orig_Tag =>
5689 New_Occurrence_Of
5690 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5691 Parent_Expr => A);
5693 -- No tag is needed in the case of a VM
5695 else
5696 Expand_Record_Aggregate (N, Parent_Expr => A);
5697 end if;
5698 end if;
5700 exception
5701 when RE_Not_Available =>
5702 return;
5703 end Expand_N_Extension_Aggregate;
5705 -----------------------------
5706 -- Expand_Record_Aggregate --
5707 -----------------------------
5709 procedure Expand_Record_Aggregate
5710 (N : Node_Id;
5711 Orig_Tag : Node_Id := Empty;
5712 Parent_Expr : Node_Id := Empty)
5714 Loc : constant Source_Ptr := Sloc (N);
5715 Comps : constant List_Id := Component_Associations (N);
5716 Typ : constant Entity_Id := Etype (N);
5717 Base_Typ : constant Entity_Id := Base_Type (Typ);
5719 Static_Components : Boolean := True;
5720 -- Flag to indicate whether all components are compile-time known,
5721 -- and the aggregate can be constructed statically and handled by
5722 -- the back-end.
5724 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
5725 -- Returns true if N is an expression of composite type which can be
5726 -- fully evaluated at compile time without raising constraint error.
5727 -- Such expressions can be passed as is to Gigi without any expansion.
5729 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
5730 -- set and constants whose expression is such an aggregate, recursively.
5732 function Component_Not_OK_For_Backend return Boolean;
5733 -- Check for presence of a component which makes it impossible for the
5734 -- backend to process the aggregate, thus requiring the use of a series
5735 -- of assignment statements. Cases checked for are a nested aggregate
5736 -- needing Late_Expansion, the presence of a tagged component which may
5737 -- need tag adjustment, and a bit unaligned component reference.
5739 -- We also force expansion into assignments if a component is of a
5740 -- mutable type (including a private type with discriminants) because
5741 -- in that case the size of the component to be copied may be smaller
5742 -- than the side of the target, and there is no simple way for gigi
5743 -- to compute the size of the object to be copied.
5745 -- NOTE: This is part of the ongoing work to define precisely the
5746 -- interface between front-end and back-end handling of aggregates.
5747 -- In general it is desirable to pass aggregates as they are to gigi,
5748 -- in order to minimize elaboration code. This is one case where the
5749 -- semantics of Ada complicate the analysis and lead to anomalies in
5750 -- the gcc back-end if the aggregate is not expanded into assignments.
5752 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
5753 -- If any ancestor of the current type is private, the aggregate
5754 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
5755 -- because it will not be set when type and its parent are in the
5756 -- same scope, and the parent component needs expansion.
5758 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
5759 -- For nested aggregates return the ultimate enclosing aggregate; for
5760 -- non-nested aggregates return N.
5762 ----------------------------------------
5763 -- Compile_Time_Known_Composite_Value --
5764 ----------------------------------------
5766 function Compile_Time_Known_Composite_Value
5767 (N : Node_Id) return Boolean
5769 begin
5770 -- If we have an entity name, then see if it is the name of a
5771 -- constant and if so, test the corresponding constant value.
5773 if Is_Entity_Name (N) then
5774 declare
5775 E : constant Entity_Id := Entity (N);
5776 V : Node_Id;
5777 begin
5778 if Ekind (E) /= E_Constant then
5779 return False;
5780 else
5781 V := Constant_Value (E);
5782 return Present (V)
5783 and then Compile_Time_Known_Composite_Value (V);
5784 end if;
5785 end;
5787 -- We have a value, see if it is compile time known
5789 else
5790 if Nkind (N) = N_Aggregate then
5791 return Compile_Time_Known_Aggregate (N);
5792 end if;
5794 -- All other types of values are not known at compile time
5796 return False;
5797 end if;
5799 end Compile_Time_Known_Composite_Value;
5801 ----------------------------------
5802 -- Component_Not_OK_For_Backend --
5803 ----------------------------------
5805 function Component_Not_OK_For_Backend return Boolean is
5806 C : Node_Id;
5807 Expr_Q : Node_Id;
5809 begin
5810 if No (Comps) then
5811 return False;
5812 end if;
5814 C := First (Comps);
5815 while Present (C) loop
5817 -- If the component has box initialization, expansion is needed
5818 -- and component is not ready for backend.
5820 if Box_Present (C) then
5821 return True;
5822 end if;
5824 if Nkind (Expression (C)) = N_Qualified_Expression then
5825 Expr_Q := Expression (Expression (C));
5826 else
5827 Expr_Q := Expression (C);
5828 end if;
5830 -- Return true if the aggregate has any associations for tagged
5831 -- components that may require tag adjustment.
5833 -- These are cases where the source expression may have a tag that
5834 -- could differ from the component tag (e.g., can occur for type
5835 -- conversions and formal parameters). (Tag adjustment not needed
5836 -- if VM_Target because object tags are implicit in the machine.)
5838 if Is_Tagged_Type (Etype (Expr_Q))
5839 and then (Nkind (Expr_Q) = N_Type_Conversion
5840 or else (Is_Entity_Name (Expr_Q)
5841 and then
5842 Ekind (Entity (Expr_Q)) in Formal_Kind))
5843 and then Tagged_Type_Expansion
5844 then
5845 Static_Components := False;
5846 return True;
5848 elsif Is_Delayed_Aggregate (Expr_Q) then
5849 Static_Components := False;
5850 return True;
5852 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5853 Static_Components := False;
5854 return True;
5855 end if;
5857 if Is_Elementary_Type (Etype (Expr_Q)) then
5858 if not Compile_Time_Known_Value (Expr_Q) then
5859 Static_Components := False;
5860 end if;
5862 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
5863 Static_Components := False;
5865 if Is_Private_Type (Etype (Expr_Q))
5866 and then Has_Discriminants (Etype (Expr_Q))
5867 then
5868 return True;
5869 end if;
5870 end if;
5872 Next (C);
5873 end loop;
5875 return False;
5876 end Component_Not_OK_For_Backend;
5878 -----------------------------------
5879 -- Has_Visible_Private_Ancestor --
5880 -----------------------------------
5882 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
5883 R : constant Entity_Id := Root_Type (Id);
5884 T1 : Entity_Id := Id;
5886 begin
5887 loop
5888 if Is_Private_Type (T1) then
5889 return True;
5891 elsif T1 = R then
5892 return False;
5894 else
5895 T1 := Etype (T1);
5896 end if;
5897 end loop;
5898 end Has_Visible_Private_Ancestor;
5900 -------------------------
5901 -- Top_Level_Aggregate --
5902 -------------------------
5904 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
5905 Aggr : Node_Id;
5907 begin
5908 Aggr := N;
5909 while Present (Parent (Aggr))
5910 and then Nkind_In (Parent (Aggr), N_Component_Association,
5911 N_Aggregate)
5912 loop
5913 Aggr := Parent (Aggr);
5914 end loop;
5916 return Aggr;
5917 end Top_Level_Aggregate;
5919 -- Local variables
5921 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
5922 Tag_Value : Node_Id;
5923 Comp : Entity_Id;
5924 New_Comp : Node_Id;
5926 -- Start of processing for Expand_Record_Aggregate
5928 begin
5929 -- If the aggregate is to be assigned to an atomic variable, we have
5930 -- to prevent a piecemeal assignment even if the aggregate is to be
5931 -- expanded. We create a temporary for the aggregate, and assign the
5932 -- temporary instead, so that the back end can generate an atomic move
5933 -- for it.
5935 if Is_Atomic (Typ)
5936 and then Comes_From_Source (Parent (N))
5937 and then Is_Atomic_Aggregate (N, Typ)
5938 then
5939 return;
5941 -- No special management required for aggregates used to initialize
5942 -- statically allocated dispatch tables
5944 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5945 return;
5946 end if;
5948 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5949 -- are build-in-place function calls. The assignments will each turn
5950 -- into a build-in-place function call. If components are all static,
5951 -- we can pass the aggregate to the backend regardless of limitedness.
5953 -- Extension aggregates, aggregates in extended return statements, and
5954 -- aggregates for C++ imported types must be expanded.
5956 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
5957 if not Nkind_In (Parent (N), N_Object_Declaration,
5958 N_Component_Association)
5959 then
5960 Convert_To_Assignments (N, Typ);
5962 elsif Nkind (N) = N_Extension_Aggregate
5963 or else Convention (Typ) = Convention_CPP
5964 then
5965 Convert_To_Assignments (N, Typ);
5967 elsif not Size_Known_At_Compile_Time (Typ)
5968 or else Component_Not_OK_For_Backend
5969 or else not Static_Components
5970 then
5971 Convert_To_Assignments (N, Typ);
5973 else
5974 Set_Compile_Time_Known_Aggregate (N);
5975 Set_Expansion_Delayed (N, False);
5976 end if;
5978 -- Gigi doesn't properly handle temporaries of variable size so we
5979 -- generate it in the front-end
5981 elsif not Size_Known_At_Compile_Time (Typ)
5982 and then Tagged_Type_Expansion
5983 then
5984 Convert_To_Assignments (N, Typ);
5986 -- An aggregate used to initialize a controlled object must be turned
5987 -- into component assignments as the components themselves may require
5988 -- finalization actions such as adjustment.
5990 elsif Needs_Finalization (Typ) then
5991 Convert_To_Assignments (N, Typ);
5993 -- Ada 2005 (AI-287): In case of default initialized components we
5994 -- convert the aggregate into assignments.
5996 elsif Has_Default_Init_Comps (N) then
5997 Convert_To_Assignments (N, Typ);
5999 -- Check components
6001 elsif Component_Not_OK_For_Backend then
6002 Convert_To_Assignments (N, Typ);
6004 -- If an ancestor is private, some components are not inherited and we
6005 -- cannot expand into a record aggregate.
6007 elsif Has_Visible_Private_Ancestor (Typ) then
6008 Convert_To_Assignments (N, Typ);
6010 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
6011 -- is not able to handle the aggregate for Late_Request.
6013 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
6014 Convert_To_Assignments (N, Typ);
6016 -- If the tagged types covers interface types we need to initialize all
6017 -- hidden components containing pointers to secondary dispatch tables.
6019 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
6020 Convert_To_Assignments (N, Typ);
6022 -- If some components are mutable, the size of the aggregate component
6023 -- may be distinct from the default size of the type component, so
6024 -- we need to expand to insure that the back-end copies the proper
6025 -- size of the data. However, if the aggregate is the initial value of
6026 -- a constant, the target is immutable and might be built statically
6027 -- if components are appropriate.
6029 elsif Has_Mutable_Components (Typ)
6030 and then
6031 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
6032 or else not Constant_Present (Parent (Top_Level_Aggr))
6033 or else not Static_Components)
6034 then
6035 Convert_To_Assignments (N, Typ);
6037 -- If the type involved has bit aligned components, then we are not sure
6038 -- that the back end can handle this case correctly.
6040 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
6041 Convert_To_Assignments (N, Typ);
6043 -- In all other cases, build a proper aggregate to be handled by gigi
6045 else
6046 if Nkind (N) = N_Aggregate then
6048 -- If the aggregate is static and can be handled by the back-end,
6049 -- nothing left to do.
6051 if Static_Components then
6052 Set_Compile_Time_Known_Aggregate (N);
6053 Set_Expansion_Delayed (N, False);
6054 end if;
6055 end if;
6057 -- If no discriminants, nothing special to do
6059 if not Has_Discriminants (Typ) then
6060 null;
6062 -- Case of discriminants present
6064 elsif Is_Derived_Type (Typ) then
6066 -- For untagged types, non-stored discriminants are replaced
6067 -- with stored discriminants, which are the ones that gigi uses
6068 -- to describe the type and its components.
6070 Generate_Aggregate_For_Derived_Type : declare
6071 Constraints : constant List_Id := New_List;
6072 First_Comp : Node_Id;
6073 Discriminant : Entity_Id;
6074 Decl : Node_Id;
6075 Num_Disc : Int := 0;
6076 Num_Gird : Int := 0;
6078 procedure Prepend_Stored_Values (T : Entity_Id);
6079 -- Scan the list of stored discriminants of the type, and add
6080 -- their values to the aggregate being built.
6082 ---------------------------
6083 -- Prepend_Stored_Values --
6084 ---------------------------
6086 procedure Prepend_Stored_Values (T : Entity_Id) is
6087 begin
6088 Discriminant := First_Stored_Discriminant (T);
6089 while Present (Discriminant) loop
6090 New_Comp :=
6091 Make_Component_Association (Loc,
6092 Choices =>
6093 New_List (New_Occurrence_Of (Discriminant, Loc)),
6095 Expression =>
6096 New_Copy_Tree
6097 (Get_Discriminant_Value
6098 (Discriminant,
6099 Typ,
6100 Discriminant_Constraint (Typ))));
6102 if No (First_Comp) then
6103 Prepend_To (Component_Associations (N), New_Comp);
6104 else
6105 Insert_After (First_Comp, New_Comp);
6106 end if;
6108 First_Comp := New_Comp;
6109 Next_Stored_Discriminant (Discriminant);
6110 end loop;
6111 end Prepend_Stored_Values;
6113 -- Start of processing for Generate_Aggregate_For_Derived_Type
6115 begin
6116 -- Remove the associations for the discriminant of derived type
6118 First_Comp := First (Component_Associations (N));
6119 while Present (First_Comp) loop
6120 Comp := First_Comp;
6121 Next (First_Comp);
6123 if Ekind (Entity (First (Choices (Comp)))) = E_Discriminant
6124 then
6125 Remove (Comp);
6126 Num_Disc := Num_Disc + 1;
6127 end if;
6128 end loop;
6130 -- Insert stored discriminant associations in the correct
6131 -- order. If there are more stored discriminants than new
6132 -- discriminants, there is at least one new discriminant that
6133 -- constrains more than one of the stored discriminants. In
6134 -- this case we need to construct a proper subtype of the
6135 -- parent type, in order to supply values to all the
6136 -- components. Otherwise there is one-one correspondence
6137 -- between the constraints and the stored discriminants.
6139 First_Comp := Empty;
6141 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6142 while Present (Discriminant) loop
6143 Num_Gird := Num_Gird + 1;
6144 Next_Stored_Discriminant (Discriminant);
6145 end loop;
6147 -- Case of more stored discriminants than new discriminants
6149 if Num_Gird > Num_Disc then
6151 -- Create a proper subtype of the parent type, which is the
6152 -- proper implementation type for the aggregate, and convert
6153 -- it to the intended target type.
6155 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6156 while Present (Discriminant) loop
6157 New_Comp :=
6158 New_Copy_Tree
6159 (Get_Discriminant_Value
6160 (Discriminant,
6161 Typ,
6162 Discriminant_Constraint (Typ)));
6163 Append (New_Comp, Constraints);
6164 Next_Stored_Discriminant (Discriminant);
6165 end loop;
6167 Decl :=
6168 Make_Subtype_Declaration (Loc,
6169 Defining_Identifier => Make_Temporary (Loc, 'T'),
6170 Subtype_Indication =>
6171 Make_Subtype_Indication (Loc,
6172 Subtype_Mark =>
6173 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
6174 Constraint =>
6175 Make_Index_Or_Discriminant_Constraint
6176 (Loc, Constraints)));
6178 Insert_Action (N, Decl);
6179 Prepend_Stored_Values (Base_Type (Typ));
6181 Set_Etype (N, Defining_Identifier (Decl));
6182 Set_Analyzed (N);
6184 Rewrite (N, Unchecked_Convert_To (Typ, N));
6185 Analyze (N);
6187 -- Case where we do not have fewer new discriminants than
6188 -- stored discriminants, so in this case we can simply use the
6189 -- stored discriminants of the subtype.
6191 else
6192 Prepend_Stored_Values (Typ);
6193 end if;
6194 end Generate_Aggregate_For_Derived_Type;
6195 end if;
6197 if Is_Tagged_Type (Typ) then
6199 -- In the tagged case, _parent and _tag component must be created
6201 -- Reset Null_Present unconditionally. Tagged records always have
6202 -- at least one field (the tag or the parent).
6204 Set_Null_Record_Present (N, False);
6206 -- When the current aggregate comes from the expansion of an
6207 -- extension aggregate, the parent expr is replaced by an
6208 -- aggregate formed by selected components of this expr.
6210 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
6211 Comp := First_Component_Or_Discriminant (Typ);
6212 while Present (Comp) loop
6214 -- Skip all expander-generated components
6216 if not Comes_From_Source (Original_Record_Component (Comp))
6217 then
6218 null;
6220 else
6221 New_Comp :=
6222 Make_Selected_Component (Loc,
6223 Prefix =>
6224 Unchecked_Convert_To (Typ,
6225 Duplicate_Subexpr (Parent_Expr, True)),
6226 Selector_Name => New_Occurrence_Of (Comp, Loc));
6228 Append_To (Comps,
6229 Make_Component_Association (Loc,
6230 Choices =>
6231 New_List (New_Occurrence_Of (Comp, Loc)),
6232 Expression => New_Comp));
6234 Analyze_And_Resolve (New_Comp, Etype (Comp));
6235 end if;
6237 Next_Component_Or_Discriminant (Comp);
6238 end loop;
6239 end if;
6241 -- Compute the value for the Tag now, if the type is a root it
6242 -- will be included in the aggregate right away, otherwise it will
6243 -- be propagated to the parent aggregate.
6245 if Present (Orig_Tag) then
6246 Tag_Value := Orig_Tag;
6247 elsif not Tagged_Type_Expansion then
6248 Tag_Value := Empty;
6249 else
6250 Tag_Value :=
6251 New_Occurrence_Of
6252 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6253 end if;
6255 -- For a derived type, an aggregate for the parent is formed with
6256 -- all the inherited components.
6258 if Is_Derived_Type (Typ) then
6260 declare
6261 First_Comp : Node_Id;
6262 Parent_Comps : List_Id;
6263 Parent_Aggr : Node_Id;
6264 Parent_Name : Node_Id;
6266 begin
6267 -- Remove the inherited component association from the
6268 -- aggregate and store them in the parent aggregate
6270 First_Comp := First (Component_Associations (N));
6271 Parent_Comps := New_List;
6272 while Present (First_Comp)
6273 and then
6274 Scope (Original_Record_Component
6275 (Entity (First (Choices (First_Comp))))) /=
6276 Base_Typ
6277 loop
6278 Comp := First_Comp;
6279 Next (First_Comp);
6280 Remove (Comp);
6281 Append (Comp, Parent_Comps);
6282 end loop;
6284 Parent_Aggr :=
6285 Make_Aggregate (Loc,
6286 Component_Associations => Parent_Comps);
6287 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
6289 -- Find the _parent component
6291 Comp := First_Component (Typ);
6292 while Chars (Comp) /= Name_uParent loop
6293 Comp := Next_Component (Comp);
6294 end loop;
6296 Parent_Name := New_Occurrence_Of (Comp, Loc);
6298 -- Insert the parent aggregate
6300 Prepend_To (Component_Associations (N),
6301 Make_Component_Association (Loc,
6302 Choices => New_List (Parent_Name),
6303 Expression => Parent_Aggr));
6305 -- Expand recursively the parent propagating the right Tag
6307 Expand_Record_Aggregate
6308 (Parent_Aggr, Tag_Value, Parent_Expr);
6310 -- The ancestor part may be a nested aggregate that has
6311 -- delayed expansion: recheck now.
6313 if Component_Not_OK_For_Backend then
6314 Convert_To_Assignments (N, Typ);
6315 end if;
6316 end;
6318 -- For a root type, the tag component is added (unless compiling
6319 -- for the VMs, where tags are implicit).
6321 elsif Tagged_Type_Expansion then
6322 declare
6323 Tag_Name : constant Node_Id :=
6324 New_Occurrence_Of (First_Tag_Component (Typ), Loc);
6325 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
6326 Conv_Node : constant Node_Id :=
6327 Unchecked_Convert_To (Typ_Tag, Tag_Value);
6329 begin
6330 Set_Etype (Conv_Node, Typ_Tag);
6331 Prepend_To (Component_Associations (N),
6332 Make_Component_Association (Loc,
6333 Choices => New_List (Tag_Name),
6334 Expression => Conv_Node));
6335 end;
6336 end if;
6337 end if;
6338 end if;
6340 end Expand_Record_Aggregate;
6342 ----------------------------
6343 -- Has_Default_Init_Comps --
6344 ----------------------------
6346 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
6347 Comps : constant List_Id := Component_Associations (N);
6348 C : Node_Id;
6349 Expr : Node_Id;
6351 begin
6352 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
6354 if No (Comps) then
6355 return False;
6356 end if;
6358 if Has_Self_Reference (N) then
6359 return True;
6360 end if;
6362 -- Check if any direct component has default initialized components
6364 C := First (Comps);
6365 while Present (C) loop
6366 if Box_Present (C) then
6367 return True;
6368 end if;
6370 Next (C);
6371 end loop;
6373 -- Recursive call in case of aggregate expression
6375 C := First (Comps);
6376 while Present (C) loop
6377 Expr := Expression (C);
6379 if Present (Expr)
6380 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
6381 and then Has_Default_Init_Comps (Expr)
6382 then
6383 return True;
6384 end if;
6386 Next (C);
6387 end loop;
6389 return False;
6390 end Has_Default_Init_Comps;
6392 --------------------------
6393 -- Is_Delayed_Aggregate --
6394 --------------------------
6396 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
6397 Node : Node_Id := N;
6398 Kind : Node_Kind := Nkind (Node);
6400 begin
6401 if Kind = N_Qualified_Expression then
6402 Node := Expression (Node);
6403 Kind := Nkind (Node);
6404 end if;
6406 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
6407 return False;
6408 else
6409 return Expansion_Delayed (Node);
6410 end if;
6411 end Is_Delayed_Aggregate;
6413 ----------------------------------------
6414 -- Is_Static_Dispatch_Table_Aggregate --
6415 ----------------------------------------
6417 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
6418 Typ : constant Entity_Id := Base_Type (Etype (N));
6420 begin
6421 return Static_Dispatch_Tables
6422 and then Tagged_Type_Expansion
6423 and then RTU_Loaded (Ada_Tags)
6425 -- Avoid circularity when rebuilding the compiler
6427 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
6428 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
6429 or else
6430 Typ = RTE (RE_Address_Array)
6431 or else
6432 Typ = RTE (RE_Type_Specific_Data)
6433 or else
6434 Typ = RTE (RE_Tag_Table)
6435 or else
6436 (RTE_Available (RE_Interface_Data)
6437 and then Typ = RTE (RE_Interface_Data))
6438 or else
6439 (RTE_Available (RE_Interfaces_Array)
6440 and then Typ = RTE (RE_Interfaces_Array))
6441 or else
6442 (RTE_Available (RE_Interface_Data_Element)
6443 and then Typ = RTE (RE_Interface_Data_Element)));
6444 end Is_Static_Dispatch_Table_Aggregate;
6446 -----------------------------
6447 -- Is_Two_Dim_Packed_Array --
6448 -----------------------------
6450 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
6451 C : constant Int := UI_To_Int (Component_Size (Typ));
6452 begin
6453 return Number_Dimensions (Typ) = 2
6454 and then Is_Bit_Packed_Array (Typ)
6455 and then (C = 1 or else C = 2 or else C = 4);
6456 end Is_Two_Dim_Packed_Array;
6458 --------------------
6459 -- Late_Expansion --
6460 --------------------
6462 function Late_Expansion
6463 (N : Node_Id;
6464 Typ : Entity_Id;
6465 Target : Node_Id) return List_Id
6467 Aggr_Code : List_Id;
6469 begin
6470 if Is_Record_Type (Etype (N)) then
6471 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
6473 else pragma Assert (Is_Array_Type (Etype (N)));
6474 Aggr_Code :=
6475 Build_Array_Aggr_Code
6476 (N => N,
6477 Ctype => Component_Type (Etype (N)),
6478 Index => First_Index (Typ),
6479 Into => Target,
6480 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
6481 Indexes => No_List);
6482 end if;
6484 -- Save the last assignment statement associated with the aggregate
6485 -- when building a controlled object. This reference is utilized by
6486 -- the finalization machinery when marking an object as successfully
6487 -- initialized.
6489 if Needs_Finalization (Typ)
6490 and then Is_Entity_Name (Target)
6491 and then Present (Entity (Target))
6492 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6493 then
6494 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6495 end if;
6497 return Aggr_Code;
6498 end Late_Expansion;
6500 ----------------------------------
6501 -- Make_OK_Assignment_Statement --
6502 ----------------------------------
6504 function Make_OK_Assignment_Statement
6505 (Sloc : Source_Ptr;
6506 Name : Node_Id;
6507 Expression : Node_Id) return Node_Id
6509 begin
6510 Set_Assignment_OK (Name);
6511 return Make_Assignment_Statement (Sloc, Name, Expression);
6512 end Make_OK_Assignment_Statement;
6514 -----------------------
6515 -- Number_Of_Choices --
6516 -----------------------
6518 function Number_Of_Choices (N : Node_Id) return Nat is
6519 Assoc : Node_Id;
6520 Choice : Node_Id;
6522 Nb_Choices : Nat := 0;
6524 begin
6525 if Present (Expressions (N)) then
6526 return 0;
6527 end if;
6529 Assoc := First (Component_Associations (N));
6530 while Present (Assoc) loop
6531 Choice := First (Choices (Assoc));
6532 while Present (Choice) loop
6533 if Nkind (Choice) /= N_Others_Choice then
6534 Nb_Choices := Nb_Choices + 1;
6535 end if;
6537 Next (Choice);
6538 end loop;
6540 Next (Assoc);
6541 end loop;
6543 return Nb_Choices;
6544 end Number_Of_Choices;
6546 ------------------------------------
6547 -- Packed_Array_Aggregate_Handled --
6548 ------------------------------------
6550 -- The current version of this procedure will handle at compile time
6551 -- any array aggregate that meets these conditions:
6553 -- One and two dimensional, bit packed
6554 -- Underlying packed type is modular type
6555 -- Bounds are within 32-bit Int range
6556 -- All bounds and values are static
6558 -- Note: for now, in the 2-D case, we only handle component sizes of
6559 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
6561 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6562 Loc : constant Source_Ptr := Sloc (N);
6563 Typ : constant Entity_Id := Etype (N);
6564 Ctyp : constant Entity_Id := Component_Type (Typ);
6566 Not_Handled : exception;
6567 -- Exception raised if this aggregate cannot be handled
6569 begin
6570 -- Handle one- or two dimensional bit packed array
6572 if not Is_Bit_Packed_Array (Typ)
6573 or else Number_Dimensions (Typ) > 2
6574 then
6575 return False;
6576 end if;
6578 -- If two-dimensional, check whether it can be folded, and transformed
6579 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
6580 -- the original type.
6582 if Number_Dimensions (Typ) = 2 then
6583 return Two_Dim_Packed_Array_Handled (N);
6584 end if;
6586 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
6587 return False;
6588 end if;
6590 if not Is_Scalar_Type (Component_Type (Typ))
6591 and then Has_Non_Standard_Rep (Component_Type (Typ))
6592 then
6593 return False;
6594 end if;
6596 declare
6597 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6599 Lo : Node_Id;
6600 Hi : Node_Id;
6601 -- Bounds of index type
6603 Lob : Uint;
6604 Hib : Uint;
6605 -- Values of bounds if compile time known
6607 function Get_Component_Val (N : Node_Id) return Uint;
6608 -- Given a expression value N of the component type Ctyp, returns a
6609 -- value of Csiz (component size) bits representing this value. If
6610 -- the value is non-static or any other reason exists why the value
6611 -- cannot be returned, then Not_Handled is raised.
6613 -----------------------
6614 -- Get_Component_Val --
6615 -----------------------
6617 function Get_Component_Val (N : Node_Id) return Uint is
6618 Val : Uint;
6620 begin
6621 -- We have to analyze the expression here before doing any further
6622 -- processing here. The analysis of such expressions is deferred
6623 -- till expansion to prevent some problems of premature analysis.
6625 Analyze_And_Resolve (N, Ctyp);
6627 -- Must have a compile time value. String literals have to be
6628 -- converted into temporaries as well, because they cannot easily
6629 -- be converted into their bit representation.
6631 if not Compile_Time_Known_Value (N)
6632 or else Nkind (N) = N_String_Literal
6633 then
6634 raise Not_Handled;
6635 end if;
6637 Val := Expr_Rep_Value (N);
6639 -- Adjust for bias, and strip proper number of bits
6641 if Has_Biased_Representation (Ctyp) then
6642 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6643 end if;
6645 return Val mod Uint_2 ** Csiz;
6646 end Get_Component_Val;
6648 -- Here we know we have a one dimensional bit packed array
6650 begin
6651 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6653 -- Cannot do anything if bounds are dynamic
6655 if not Compile_Time_Known_Value (Lo)
6656 or else
6657 not Compile_Time_Known_Value (Hi)
6658 then
6659 return False;
6660 end if;
6662 -- Or are silly out of range of int bounds
6664 Lob := Expr_Value (Lo);
6665 Hib := Expr_Value (Hi);
6667 if not UI_Is_In_Int_Range (Lob)
6668 or else
6669 not UI_Is_In_Int_Range (Hib)
6670 then
6671 return False;
6672 end if;
6674 -- At this stage we have a suitable aggregate for handling at compile
6675 -- time. The only remaining checks are that the values of expressions
6676 -- in the aggregate are compile-time known (checks are performed by
6677 -- Get_Component_Val), and that any subtypes or ranges are statically
6678 -- known.
6680 -- If the aggregate is not fully positional at this stage, then
6681 -- convert it to positional form. Either this will fail, in which
6682 -- case we can do nothing, or it will succeed, in which case we have
6683 -- succeeded in handling the aggregate and transforming it into a
6684 -- modular value, or it will stay an aggregate, in which case we
6685 -- have failed to create a packed value for it.
6687 if Present (Component_Associations (N)) then
6688 Convert_To_Positional
6689 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6690 return Nkind (N) /= N_Aggregate;
6691 end if;
6693 -- Otherwise we are all positional, so convert to proper value
6695 declare
6696 Lov : constant Int := UI_To_Int (Lob);
6697 Hiv : constant Int := UI_To_Int (Hib);
6699 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6700 -- The length of the array (number of elements)
6702 Aggregate_Val : Uint;
6703 -- Value of aggregate. The value is set in the low order bits of
6704 -- this value. For the little-endian case, the values are stored
6705 -- from low-order to high-order and for the big-endian case the
6706 -- values are stored from high-order to low-order. Note that gigi
6707 -- will take care of the conversions to left justify the value in
6708 -- the big endian case (because of left justified modular type
6709 -- processing), so we do not have to worry about that here.
6711 Lit : Node_Id;
6712 -- Integer literal for resulting constructed value
6714 Shift : Nat;
6715 -- Shift count from low order for next value
6717 Incr : Int;
6718 -- Shift increment for loop
6720 Expr : Node_Id;
6721 -- Next expression from positional parameters of aggregate
6723 Left_Justified : Boolean;
6724 -- Set True if we are filling the high order bits of the target
6725 -- value (i.e. the value is left justified).
6727 begin
6728 -- For little endian, we fill up the low order bits of the target
6729 -- value. For big endian we fill up the high order bits of the
6730 -- target value (which is a left justified modular value).
6732 Left_Justified := Bytes_Big_Endian;
6734 -- Switch justification if using -gnatd8
6736 if Debug_Flag_8 then
6737 Left_Justified := not Left_Justified;
6738 end if;
6740 -- Switch justfification if reverse storage order
6742 if Reverse_Storage_Order (Base_Type (Typ)) then
6743 Left_Justified := not Left_Justified;
6744 end if;
6746 if Left_Justified then
6747 Shift := Csiz * (Len - 1);
6748 Incr := -Csiz;
6749 else
6750 Shift := 0;
6751 Incr := +Csiz;
6752 end if;
6754 -- Loop to set the values
6756 if Len = 0 then
6757 Aggregate_Val := Uint_0;
6758 else
6759 Expr := First (Expressions (N));
6760 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6762 for J in 2 .. Len loop
6763 Shift := Shift + Incr;
6764 Next (Expr);
6765 Aggregate_Val :=
6766 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6767 end loop;
6768 end if;
6770 -- Now we can rewrite with the proper value
6772 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
6773 Set_Print_In_Hex (Lit);
6775 -- Construct the expression using this literal. Note that it is
6776 -- important to qualify the literal with its proper modular type
6777 -- since universal integer does not have the required range and
6778 -- also this is a left justified modular type, which is important
6779 -- in the big-endian case.
6781 Rewrite (N,
6782 Unchecked_Convert_To (Typ,
6783 Make_Qualified_Expression (Loc,
6784 Subtype_Mark =>
6785 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
6786 Expression => Lit)));
6788 Analyze_And_Resolve (N, Typ);
6789 return True;
6790 end;
6791 end;
6793 exception
6794 when Not_Handled =>
6795 return False;
6796 end Packed_Array_Aggregate_Handled;
6798 ----------------------------
6799 -- Has_Mutable_Components --
6800 ----------------------------
6802 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6803 Comp : Entity_Id;
6805 begin
6806 Comp := First_Component (Typ);
6807 while Present (Comp) loop
6808 if Is_Record_Type (Etype (Comp))
6809 and then Has_Discriminants (Etype (Comp))
6810 and then not Is_Constrained (Etype (Comp))
6811 then
6812 return True;
6813 end if;
6815 Next_Component (Comp);
6816 end loop;
6818 return False;
6819 end Has_Mutable_Components;
6821 ------------------------------
6822 -- Initialize_Discriminants --
6823 ------------------------------
6825 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6826 Loc : constant Source_Ptr := Sloc (N);
6827 Bas : constant Entity_Id := Base_Type (Typ);
6828 Par : constant Entity_Id := Etype (Bas);
6829 Decl : constant Node_Id := Parent (Par);
6830 Ref : Node_Id;
6832 begin
6833 if Is_Tagged_Type (Bas)
6834 and then Is_Derived_Type (Bas)
6835 and then Has_Discriminants (Par)
6836 and then Has_Discriminants (Bas)
6837 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6838 and then Nkind (Decl) = N_Full_Type_Declaration
6839 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6840 and then
6841 Present (Variant_Part (Component_List (Type_Definition (Decl))))
6842 and then Nkind (N) /= N_Extension_Aggregate
6843 then
6845 -- Call init proc to set discriminants.
6846 -- There should eventually be a special procedure for this ???
6848 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
6849 Insert_Actions_After (N,
6850 Build_Initialization_Call (Sloc (N), Ref, Typ));
6851 end if;
6852 end Initialize_Discriminants;
6854 ----------------
6855 -- Must_Slide --
6856 ----------------
6858 function Must_Slide
6859 (Obj_Type : Entity_Id;
6860 Typ : Entity_Id) return Boolean
6862 L1, L2, H1, H2 : Node_Id;
6864 begin
6865 -- No sliding if the type of the object is not established yet, if it is
6866 -- an unconstrained type whose actual subtype comes from the aggregate,
6867 -- or if the two types are identical.
6869 if not Is_Array_Type (Obj_Type) then
6870 return False;
6872 elsif not Is_Constrained (Obj_Type) then
6873 return False;
6875 elsif Typ = Obj_Type then
6876 return False;
6878 else
6879 -- Sliding can only occur along the first dimension
6881 Get_Index_Bounds (First_Index (Typ), L1, H1);
6882 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6884 if not Is_OK_Static_Expression (L1) or else
6885 not Is_OK_Static_Expression (L2) or else
6886 not Is_OK_Static_Expression (H1) or else
6887 not Is_OK_Static_Expression (H2)
6888 then
6889 return False;
6890 else
6891 return Expr_Value (L1) /= Expr_Value (L2)
6892 or else
6893 Expr_Value (H1) /= Expr_Value (H2);
6894 end if;
6895 end if;
6896 end Must_Slide;
6898 ----------------------------------
6899 -- Two_Dim_Packed_Array_Handled --
6900 ----------------------------------
6902 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
6903 Loc : constant Source_Ptr := Sloc (N);
6904 Typ : constant Entity_Id := Etype (N);
6905 Ctyp : constant Entity_Id := Component_Type (Typ);
6906 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
6907 Packed_Array : constant Entity_Id :=
6908 Packed_Array_Impl_Type (Base_Type (Typ));
6910 One_Comp : Node_Id;
6911 -- Expression in original aggregate
6913 One_Dim : Node_Id;
6914 -- One-dimensional subaggregate
6916 begin
6918 -- For now, only deal with cases where an integral number of elements
6919 -- fit in a single byte. This includes the most common boolean case.
6921 if not (Comp_Size = 1 or else
6922 Comp_Size = 2 or else
6923 Comp_Size = 4)
6924 then
6925 return False;
6926 end if;
6928 Convert_To_Positional
6929 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6931 -- Verify that all components are static
6933 if Nkind (N) = N_Aggregate
6934 and then Compile_Time_Known_Aggregate (N)
6935 then
6936 null;
6938 -- The aggregate may have been re-analyzed and converted already
6940 elsif Nkind (N) /= N_Aggregate then
6941 return True;
6943 -- If component associations remain, the aggregate is not static
6945 elsif Present (Component_Associations (N)) then
6946 return False;
6948 else
6949 One_Dim := First (Expressions (N));
6950 while Present (One_Dim) loop
6951 if Present (Component_Associations (One_Dim)) then
6952 return False;
6953 end if;
6955 One_Comp := First (Expressions (One_Dim));
6956 while Present (One_Comp) loop
6957 if not Is_OK_Static_Expression (One_Comp) then
6958 return False;
6959 end if;
6961 Next (One_Comp);
6962 end loop;
6964 Next (One_Dim);
6965 end loop;
6966 end if;
6968 -- Two-dimensional aggregate is now fully positional so pack one
6969 -- dimension to create a static one-dimensional array, and rewrite
6970 -- as an unchecked conversion to the original type.
6972 declare
6973 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
6974 -- The packed array type is a byte array
6976 Packed_Num : Int;
6977 -- Number of components accumulated in current byte
6979 Comps : List_Id;
6980 -- Assembled list of packed values for equivalent aggregate
6982 Comp_Val : Uint;
6983 -- integer value of component
6985 Incr : Int;
6986 -- Step size for packing
6988 Init_Shift : Int;
6989 -- Endian-dependent start position for packing
6991 Shift : Int;
6992 -- Current insertion position
6994 Val : Int;
6995 -- Component of packed array being assembled.
6997 begin
6998 Comps := New_List;
6999 Val := 0;
7000 Packed_Num := 0;
7002 -- Account for endianness. See corresponding comment in
7003 -- Packed_Array_Aggregate_Handled concerning the following.
7005 if Bytes_Big_Endian
7006 xor Debug_Flag_8
7007 xor Reverse_Storage_Order (Base_Type (Typ))
7008 then
7009 Init_Shift := Byte_Size - Comp_Size;
7010 Incr := -Comp_Size;
7011 else
7012 Init_Shift := 0;
7013 Incr := +Comp_Size;
7014 end if;
7016 -- Iterate over each subaggregate
7018 Shift := Init_Shift;
7019 One_Dim := First (Expressions (N));
7020 while Present (One_Dim) loop
7021 One_Comp := First (Expressions (One_Dim));
7022 while Present (One_Comp) loop
7023 if Packed_Num = Byte_Size / Comp_Size then
7025 -- Byte is complete, add to list of expressions
7027 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7028 Val := 0;
7029 Shift := Init_Shift;
7030 Packed_Num := 0;
7032 else
7033 Comp_Val := Expr_Rep_Value (One_Comp);
7035 -- Adjust for bias, and strip proper number of bits
7037 if Has_Biased_Representation (Ctyp) then
7038 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
7039 end if;
7041 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
7042 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
7043 Shift := Shift + Incr;
7044 One_Comp := Next (One_Comp);
7045 Packed_Num := Packed_Num + 1;
7046 end if;
7047 end loop;
7049 One_Dim := Next (One_Dim);
7050 end loop;
7052 if Packed_Num > 0 then
7054 -- Add final incomplete byte if present
7056 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7057 end if;
7059 Rewrite (N,
7060 Unchecked_Convert_To (Typ,
7061 Make_Qualified_Expression (Loc,
7062 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
7063 Expression => Make_Aggregate (Loc, Expressions => Comps))));
7064 Analyze_And_Resolve (N);
7065 return True;
7066 end;
7067 end Two_Dim_Packed_Array_Handled;
7069 ---------------------
7070 -- Sort_Case_Table --
7071 ---------------------
7073 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
7074 L : constant Int := Case_Table'First;
7075 U : constant Int := Case_Table'Last;
7076 K : Int;
7077 J : Int;
7078 T : Case_Bounds;
7080 begin
7081 K := L;
7082 while K /= U loop
7083 T := Case_Table (K + 1);
7085 J := K + 1;
7086 while J /= L
7087 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
7088 Expr_Value (T.Choice_Lo)
7089 loop
7090 Case_Table (J) := Case_Table (J - 1);
7091 J := J - 1;
7092 end loop;
7094 Case_Table (J) := T;
7095 K := K + 1;
7096 end loop;
7097 end Sort_Case_Table;
7099 ----------------------------
7100 -- Static_Array_Aggregate --
7101 ----------------------------
7103 function Static_Array_Aggregate (N : Node_Id) return Boolean is
7104 Bounds : constant Node_Id := Aggregate_Bounds (N);
7106 Typ : constant Entity_Id := Etype (N);
7107 Comp_Type : constant Entity_Id := Component_Type (Typ);
7108 Agg : Node_Id;
7109 Expr : Node_Id;
7110 Lo : Node_Id;
7111 Hi : Node_Id;
7113 begin
7114 if Is_Tagged_Type (Typ)
7115 or else Is_Controlled (Typ)
7116 or else Is_Packed (Typ)
7117 then
7118 return False;
7119 end if;
7121 if Present (Bounds)
7122 and then Nkind (Bounds) = N_Range
7123 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
7124 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
7125 then
7126 Lo := Low_Bound (Bounds);
7127 Hi := High_Bound (Bounds);
7129 if No (Component_Associations (N)) then
7131 -- Verify that all components are static integers
7133 Expr := First (Expressions (N));
7134 while Present (Expr) loop
7135 if Nkind (Expr) /= N_Integer_Literal then
7136 return False;
7137 end if;
7139 Next (Expr);
7140 end loop;
7142 return True;
7144 else
7145 -- We allow only a single named association, either a static
7146 -- range or an others_clause, with a static expression.
7148 Expr := First (Component_Associations (N));
7150 if Present (Expressions (N)) then
7151 return False;
7153 elsif Present (Next (Expr)) then
7154 return False;
7156 elsif Present (Next (First (Choices (Expr)))) then
7157 return False;
7159 else
7160 -- The aggregate is static if all components are literals,
7161 -- or else all its components are static aggregates for the
7162 -- component type. We also limit the size of a static aggregate
7163 -- to prevent runaway static expressions.
7165 if Is_Array_Type (Comp_Type)
7166 or else Is_Record_Type (Comp_Type)
7167 then
7168 if Nkind (Expression (Expr)) /= N_Aggregate
7169 or else
7170 not Compile_Time_Known_Aggregate (Expression (Expr))
7171 then
7172 return False;
7173 end if;
7175 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
7176 return False;
7177 end if;
7179 if not Aggr_Size_OK (N, Typ) then
7180 return False;
7181 end if;
7183 -- Create a positional aggregate with the right number of
7184 -- copies of the expression.
7186 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
7188 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
7189 loop
7190 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
7192 -- The copied expression must be analyzed and resolved.
7193 -- Besides setting the type, this ensures that static
7194 -- expressions are appropriately marked as such.
7196 Analyze_And_Resolve
7197 (Last (Expressions (Agg)), Component_Type (Typ));
7198 end loop;
7200 Set_Aggregate_Bounds (Agg, Bounds);
7201 Set_Etype (Agg, Typ);
7202 Set_Analyzed (Agg);
7203 Rewrite (N, Agg);
7204 Set_Compile_Time_Known_Aggregate (N);
7206 return True;
7207 end if;
7208 end if;
7210 else
7211 return False;
7212 end if;
7213 end Static_Array_Aggregate;
7215 end Exp_Aggr;