2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / exp_aggr.adb
blob6cdd290bd9ee837e11f29932ea05f7fe55aefebf
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-2015, 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 value given by aspect
790 -- Default_Component_Value of array type if specified, else use
791 -- value given by aspect Default_Value for component type itself
792 -- if specified, else return Empty.
794 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
795 function Local_Expr_Value (E : Node_Id) return Uint;
796 -- These two Local routines are used to replace the corresponding ones
797 -- in sem_eval because while processing the bounds of an aggregate with
798 -- discrete choices whose index type is an enumeration, we build static
799 -- expressions not recognized by Compile_Time_Known_Value as such since
800 -- they have not yet been analyzed and resolved. All the expressions in
801 -- question are things like Index_Base_Name'Val (Const) which we can
802 -- easily recognize as being constant.
804 ---------
805 -- Add --
806 ---------
808 function Add (Val : Int; To : Node_Id) return Node_Id is
809 Expr_Pos : Node_Id;
810 Expr : Node_Id;
811 To_Pos : Node_Id;
812 U_To : Uint;
813 U_Val : constant Uint := UI_From_Int (Val);
815 begin
816 -- Note: do not try to optimize the case of Val = 0, because
817 -- we need to build a new node with the proper Sloc value anyway.
819 -- First test if we can do constant folding
821 if Local_Compile_Time_Known_Value (To) then
822 U_To := Local_Expr_Value (To) + Val;
824 -- Determine if our constant is outside the range of the index.
825 -- If so return an Empty node. This empty node will be caught
826 -- by Empty_Range below.
828 if Compile_Time_Known_Value (Index_Base_L)
829 and then U_To < Expr_Value (Index_Base_L)
830 then
831 return Empty;
833 elsif Compile_Time_Known_Value (Index_Base_H)
834 and then U_To > Expr_Value (Index_Base_H)
835 then
836 return Empty;
837 end if;
839 Expr_Pos := Make_Integer_Literal (Loc, U_To);
840 Set_Is_Static_Expression (Expr_Pos);
842 if not Is_Enumeration_Type (Index_Base) then
843 Expr := Expr_Pos;
845 -- If we are dealing with enumeration return
846 -- Index_Base'Val (Expr_Pos)
848 else
849 Expr :=
850 Make_Attribute_Reference
851 (Loc,
852 Prefix => Index_Base_Name,
853 Attribute_Name => Name_Val,
854 Expressions => New_List (Expr_Pos));
855 end if;
857 return Expr;
858 end if;
860 -- If we are here no constant folding possible
862 if not Is_Enumeration_Type (Index_Base) then
863 Expr :=
864 Make_Op_Add (Loc,
865 Left_Opnd => Duplicate_Subexpr (To),
866 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
868 -- If we are dealing with enumeration return
869 -- Index_Base'Val (Index_Base'Pos (To) + Val)
871 else
872 To_Pos :=
873 Make_Attribute_Reference
874 (Loc,
875 Prefix => Index_Base_Name,
876 Attribute_Name => Name_Pos,
877 Expressions => New_List (Duplicate_Subexpr (To)));
879 Expr_Pos :=
880 Make_Op_Add (Loc,
881 Left_Opnd => To_Pos,
882 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
884 Expr :=
885 Make_Attribute_Reference
886 (Loc,
887 Prefix => Index_Base_Name,
888 Attribute_Name => Name_Val,
889 Expressions => New_List (Expr_Pos));
890 end if;
892 return Expr;
893 end Add;
895 -----------------
896 -- Empty_Range --
897 -----------------
899 function Empty_Range (L, H : Node_Id) return Boolean is
900 Is_Empty : Boolean := False;
901 Low : Node_Id;
902 High : Node_Id;
904 begin
905 -- First check if L or H were already detected as overflowing the
906 -- index base range type by function Add above. If this is so Add
907 -- returns the empty node.
909 if No (L) or else No (H) then
910 return True;
911 end if;
913 for J in 1 .. 3 loop
914 case J is
916 -- L > H range is empty
918 when 1 =>
919 Low := L;
920 High := H;
922 -- B_L > H range must be empty
924 when 2 =>
925 Low := Index_Base_L;
926 High := H;
928 -- L > B_H range must be empty
930 when 3 =>
931 Low := L;
932 High := Index_Base_H;
933 end case;
935 if Local_Compile_Time_Known_Value (Low)
936 and then
937 Local_Compile_Time_Known_Value (High)
938 then
939 Is_Empty :=
940 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
941 end if;
943 exit when Is_Empty;
944 end loop;
946 return Is_Empty;
947 end Empty_Range;
949 -----------
950 -- Equal --
951 -----------
953 function Equal (L, H : Node_Id) return Boolean is
954 begin
955 if L = H then
956 return True;
958 elsif Local_Compile_Time_Known_Value (L)
959 and then
960 Local_Compile_Time_Known_Value (H)
961 then
962 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
963 end if;
965 return False;
966 end Equal;
968 ----------------
969 -- Gen_Assign --
970 ----------------
972 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
973 L : constant List_Id := New_List;
974 A : Node_Id;
976 New_Indexes : List_Id;
977 Indexed_Comp : Node_Id;
978 Expr_Q : Node_Id;
979 Comp_Type : Entity_Id := Empty;
981 function Add_Loop_Actions (Lis : List_Id) return List_Id;
982 -- Collect insert_actions generated in the construction of a
983 -- loop, and prepend them to the sequence of assignments to
984 -- complete the eventual body of the loop.
986 ----------------------
987 -- Add_Loop_Actions --
988 ----------------------
990 function Add_Loop_Actions (Lis : List_Id) return List_Id is
991 Res : List_Id;
993 begin
994 -- Ada 2005 (AI-287): Do nothing else in case of default
995 -- initialized component.
997 if No (Expr) then
998 return Lis;
1000 elsif Nkind (Parent (Expr)) = N_Component_Association
1001 and then Present (Loop_Actions (Parent (Expr)))
1002 then
1003 Append_List (Lis, Loop_Actions (Parent (Expr)));
1004 Res := Loop_Actions (Parent (Expr));
1005 Set_Loop_Actions (Parent (Expr), No_List);
1006 return Res;
1008 else
1009 return Lis;
1010 end if;
1011 end Add_Loop_Actions;
1013 -- Start of processing for Gen_Assign
1015 begin
1016 if No (Indexes) then
1017 New_Indexes := New_List;
1018 else
1019 New_Indexes := New_Copy_List_Tree (Indexes);
1020 end if;
1022 Append_To (New_Indexes, Ind);
1024 if Present (Next_Index (Index)) then
1025 return
1026 Add_Loop_Actions (
1027 Build_Array_Aggr_Code
1028 (N => Expr,
1029 Ctype => Ctype,
1030 Index => Next_Index (Index),
1031 Into => Into,
1032 Scalar_Comp => Scalar_Comp,
1033 Indexes => New_Indexes));
1034 end if;
1036 -- If we get here then we are at a bottom-level (sub-)aggregate
1038 Indexed_Comp :=
1039 Checks_Off
1040 (Make_Indexed_Component (Loc,
1041 Prefix => New_Copy_Tree (Into),
1042 Expressions => New_Indexes));
1044 Set_Assignment_OK (Indexed_Comp);
1046 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1047 -- is not present (and therefore we also initialize Expr_Q to empty).
1049 if No (Expr) then
1050 Expr_Q := Empty;
1051 elsif Nkind (Expr) = N_Qualified_Expression then
1052 Expr_Q := Expression (Expr);
1053 else
1054 Expr_Q := Expr;
1055 end if;
1057 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1058 Comp_Type := Component_Type (Etype (N));
1059 pragma Assert (Comp_Type = Ctype); -- AI-287
1061 elsif Present (Next (First (New_Indexes))) then
1063 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1064 -- component because we have received the component type in
1065 -- the formal parameter Ctype.
1067 -- ??? Some assert pragmas have been added to check if this new
1068 -- formal can be used to replace this code in all cases.
1070 if Present (Expr) then
1072 -- This is a multidimensional array. Recover the component type
1073 -- from the outermost aggregate, because subaggregates do not
1074 -- have an assigned type.
1076 declare
1077 P : Node_Id;
1079 begin
1080 P := Parent (Expr);
1081 while Present (P) loop
1082 if Nkind (P) = N_Aggregate
1083 and then Present (Etype (P))
1084 then
1085 Comp_Type := Component_Type (Etype (P));
1086 exit;
1088 else
1089 P := Parent (P);
1090 end if;
1091 end loop;
1093 pragma Assert (Comp_Type = Ctype); -- AI-287
1094 end;
1095 end if;
1096 end if;
1098 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1099 -- default initialized components (otherwise Expr_Q is not present).
1101 if Present (Expr_Q)
1102 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1103 then
1104 -- At this stage the Expression may not have been analyzed yet
1105 -- because the array aggregate code has not been updated to use
1106 -- the Expansion_Delayed flag and avoid analysis altogether to
1107 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1108 -- the analysis of non-array aggregates now in order to get the
1109 -- value of Expansion_Delayed flag for the inner aggregate ???
1111 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1112 Analyze_And_Resolve (Expr_Q, Comp_Type);
1113 end if;
1115 if Is_Delayed_Aggregate (Expr_Q) then
1117 -- This is either a subaggregate of a multidimensional array,
1118 -- or a component of an array type whose component type is
1119 -- also an array. In the latter case, the expression may have
1120 -- component associations that provide different bounds from
1121 -- those of the component type, and sliding must occur. Instead
1122 -- of decomposing the current aggregate assignment, force the
1123 -- re-analysis of the assignment, so that a temporary will be
1124 -- generated in the usual fashion, and sliding will take place.
1126 if Nkind (Parent (N)) = N_Assignment_Statement
1127 and then Is_Array_Type (Comp_Type)
1128 and then Present (Component_Associations (Expr_Q))
1129 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1130 then
1131 Set_Expansion_Delayed (Expr_Q, False);
1132 Set_Analyzed (Expr_Q, False);
1134 else
1135 return
1136 Add_Loop_Actions (
1137 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1138 end if;
1139 end if;
1140 end if;
1142 -- Ada 2005 (AI-287): In case of default initialized component, call
1143 -- the initialization subprogram associated with the component type.
1144 -- If the component type is an access type, add an explicit null
1145 -- assignment, because for the back-end there is an initialization
1146 -- present for the whole aggregate, and no default initialization
1147 -- will take place.
1149 -- In addition, if the component type is controlled, we must call
1150 -- its Initialize procedure explicitly, because there is no explicit
1151 -- object creation that will invoke it otherwise.
1153 if No (Expr) then
1154 if Present (Base_Init_Proc (Base_Type (Ctype)))
1155 or else Has_Task (Base_Type (Ctype))
1156 then
1157 Append_List_To (L,
1158 Build_Initialization_Call (Loc,
1159 Id_Ref => Indexed_Comp,
1160 Typ => Ctype,
1161 With_Default_Init => True));
1163 -- If the component type has invariants, add an invariant
1164 -- check after the component is default-initialized. It will
1165 -- be analyzed and resolved before the code for initialization
1166 -- of other components.
1168 if Has_Invariants (Ctype) then
1169 Set_Etype (Indexed_Comp, Ctype);
1170 Append_To (L, Make_Invariant_Call (Indexed_Comp));
1171 end if;
1173 elsif Is_Access_Type (Ctype) then
1174 Append_To (L,
1175 Make_Assignment_Statement (Loc,
1176 Name => Indexed_Comp,
1177 Expression => Make_Null (Loc)));
1178 end if;
1180 if Needs_Finalization (Ctype) then
1181 Append_To (L,
1182 Make_Init_Call
1183 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1184 Typ => Ctype));
1185 end if;
1187 else
1188 A :=
1189 Make_OK_Assignment_Statement (Loc,
1190 Name => Indexed_Comp,
1191 Expression => New_Copy_Tree (Expr));
1193 -- The target of the assignment may not have been initialized,
1194 -- so it is not possible to call Finalize as expected in normal
1195 -- controlled assignments. We must also avoid using the primitive
1196 -- _assign (which depends on a valid target, and may for example
1197 -- perform discriminant checks on it).
1199 -- Both Finalize and usage of _assign are disabled by setting
1200 -- No_Ctrl_Actions on the assignment. The rest of the controlled
1201 -- actions are done manually with the proper finalization list
1202 -- coming from the context.
1204 Set_No_Ctrl_Actions (A);
1206 -- If this is an aggregate for an array of arrays, each
1207 -- sub-aggregate will be expanded as well, and even with
1208 -- No_Ctrl_Actions the assignments of inner components will
1209 -- require attachment in their assignments to temporaries. These
1210 -- temporaries must be finalized for each subaggregate, to prevent
1211 -- multiple attachments of the same temporary location to same
1212 -- finalization chain (and consequently circular lists). To ensure
1213 -- that finalization takes place for each subaggregate we wrap the
1214 -- assignment in a block.
1216 if Present (Comp_Type)
1217 and then Needs_Finalization (Comp_Type)
1218 and then Is_Array_Type (Comp_Type)
1219 and then Present (Expr)
1220 then
1221 A :=
1222 Make_Block_Statement (Loc,
1223 Handled_Statement_Sequence =>
1224 Make_Handled_Sequence_Of_Statements (Loc,
1225 Statements => New_List (A)));
1226 end if;
1228 Append_To (L, A);
1230 -- Adjust the tag if tagged (because of possible view
1231 -- conversions), unless compiling for a VM where tags
1232 -- are implicit.
1234 if Present (Comp_Type)
1235 and then Is_Tagged_Type (Comp_Type)
1236 and then Tagged_Type_Expansion
1237 then
1238 declare
1239 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Type);
1241 begin
1242 A :=
1243 Make_OK_Assignment_Statement (Loc,
1244 Name =>
1245 Make_Selected_Component (Loc,
1246 Prefix => New_Copy_Tree (Indexed_Comp),
1247 Selector_Name =>
1248 New_Occurrence_Of
1249 (First_Tag_Component (Full_Typ), Loc)),
1251 Expression =>
1252 Unchecked_Convert_To (RTE (RE_Tag),
1253 New_Occurrence_Of
1254 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1255 Loc)));
1257 Append_To (L, A);
1258 end;
1259 end if;
1261 -- Adjust and attach the component to the proper final list, which
1262 -- can be the controller of the outer record object or the final
1263 -- list associated with the scope.
1265 -- If the component is itself an array of controlled types, whose
1266 -- value is given by a sub-aggregate, then the attach calls have
1267 -- been generated when individual subcomponent are assigned, and
1268 -- must not be done again to prevent malformed finalization chains
1269 -- (see comments above, concerning the creation of a block to hold
1270 -- inner finalization actions).
1272 if Present (Comp_Type)
1273 and then Needs_Finalization (Comp_Type)
1274 and then not Is_Limited_Type (Comp_Type)
1275 and then not
1276 (Is_Array_Type (Comp_Type)
1277 and then Is_Controlled (Component_Type (Comp_Type))
1278 and then Nkind (Expr) = N_Aggregate)
1279 then
1280 Append_To (L,
1281 Make_Adjust_Call
1282 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1283 Typ => Comp_Type));
1284 end if;
1285 end if;
1287 return Add_Loop_Actions (L);
1288 end Gen_Assign;
1290 --------------
1291 -- Gen_Loop --
1292 --------------
1294 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1295 L_J : Node_Id;
1297 L_L : Node_Id;
1298 -- Index_Base'(L)
1300 L_H : Node_Id;
1301 -- Index_Base'(H)
1303 L_Range : Node_Id;
1304 -- Index_Base'(L) .. Index_Base'(H)
1306 L_Iteration_Scheme : Node_Id;
1307 -- L_J in Index_Base'(L) .. Index_Base'(H)
1309 L_Body : List_Id;
1310 -- The statements to execute in the loop
1312 S : constant List_Id := New_List;
1313 -- List of statements
1315 Tcopy : Node_Id;
1316 -- Copy of expression tree, used for checking purposes
1318 begin
1319 -- If loop bounds define an empty range return the null statement
1321 if Empty_Range (L, H) then
1322 Append_To (S, Make_Null_Statement (Loc));
1324 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1325 -- default initialized component.
1327 if No (Expr) then
1328 null;
1330 else
1331 -- The expression must be type-checked even though no component
1332 -- of the aggregate will have this value. This is done only for
1333 -- actual components of the array, not for subaggregates. Do
1334 -- the check on a copy, because the expression may be shared
1335 -- among several choices, some of which might be non-null.
1337 if Present (Etype (N))
1338 and then Is_Array_Type (Etype (N))
1339 and then No (Next_Index (Index))
1340 then
1341 Expander_Mode_Save_And_Set (False);
1342 Tcopy := New_Copy_Tree (Expr);
1343 Set_Parent (Tcopy, N);
1344 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1345 Expander_Mode_Restore;
1346 end if;
1347 end if;
1349 return S;
1351 -- If loop bounds are the same then generate an assignment
1353 elsif Equal (L, H) then
1354 return Gen_Assign (New_Copy_Tree (L), Expr);
1356 -- If H - L <= 2 then generate a sequence of assignments when we are
1357 -- processing the bottom most aggregate and it contains scalar
1358 -- components.
1360 elsif No (Next_Index (Index))
1361 and then Scalar_Comp
1362 and then Local_Compile_Time_Known_Value (L)
1363 and then Local_Compile_Time_Known_Value (H)
1364 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1365 then
1367 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1368 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1370 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1371 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1372 end if;
1374 return S;
1375 end if;
1377 -- Otherwise construct the loop, starting with the loop index L_J
1379 L_J := Make_Temporary (Loc, 'J', L);
1381 -- Construct "L .. H" in Index_Base. We use a qualified expression
1382 -- for the bound to convert to the index base, but we don't need
1383 -- to do that if we already have the base type at hand.
1385 if Etype (L) = Index_Base then
1386 L_L := L;
1387 else
1388 L_L :=
1389 Make_Qualified_Expression (Loc,
1390 Subtype_Mark => Index_Base_Name,
1391 Expression => L);
1392 end if;
1394 if Etype (H) = Index_Base then
1395 L_H := H;
1396 else
1397 L_H :=
1398 Make_Qualified_Expression (Loc,
1399 Subtype_Mark => Index_Base_Name,
1400 Expression => H);
1401 end if;
1403 L_Range :=
1404 Make_Range (Loc,
1405 Low_Bound => L_L,
1406 High_Bound => L_H);
1408 -- Construct "for L_J in Index_Base range L .. H"
1410 L_Iteration_Scheme :=
1411 Make_Iteration_Scheme
1412 (Loc,
1413 Loop_Parameter_Specification =>
1414 Make_Loop_Parameter_Specification
1415 (Loc,
1416 Defining_Identifier => L_J,
1417 Discrete_Subtype_Definition => L_Range));
1419 -- Construct the statements to execute in the loop body
1421 L_Body := Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr);
1423 -- Construct the final loop
1425 Append_To (S,
1426 Make_Implicit_Loop_Statement
1427 (Node => N,
1428 Identifier => Empty,
1429 Iteration_Scheme => L_Iteration_Scheme,
1430 Statements => L_Body));
1432 -- A small optimization: if the aggregate is initialized with a box
1433 -- and the component type has no initialization procedure, remove the
1434 -- useless empty loop.
1436 if Nkind (First (S)) = N_Loop_Statement
1437 and then Is_Empty_List (Statements (First (S)))
1438 then
1439 return New_List (Make_Null_Statement (Loc));
1440 else
1441 return S;
1442 end if;
1443 end Gen_Loop;
1445 ---------------
1446 -- Gen_While --
1447 ---------------
1449 -- The code built is
1451 -- W_J : Index_Base := L;
1452 -- while W_J < H loop
1453 -- W_J := Index_Base'Succ (W);
1454 -- L_Body;
1455 -- end loop;
1457 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1458 W_J : Node_Id;
1460 W_Decl : Node_Id;
1461 -- W_J : Base_Type := L;
1463 W_Iteration_Scheme : Node_Id;
1464 -- while W_J < H
1466 W_Index_Succ : Node_Id;
1467 -- Index_Base'Succ (J)
1469 W_Increment : Node_Id;
1470 -- W_J := Index_Base'Succ (W)
1472 W_Body : constant List_Id := New_List;
1473 -- The statements to execute in the loop
1475 S : constant List_Id := New_List;
1476 -- list of statement
1478 begin
1479 -- If loop bounds define an empty range or are equal return null
1481 if Empty_Range (L, H) or else Equal (L, H) then
1482 Append_To (S, Make_Null_Statement (Loc));
1483 return S;
1484 end if;
1486 -- Build the decl of W_J
1488 W_J := Make_Temporary (Loc, 'J', L);
1489 W_Decl :=
1490 Make_Object_Declaration
1491 (Loc,
1492 Defining_Identifier => W_J,
1493 Object_Definition => Index_Base_Name,
1494 Expression => L);
1496 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1497 -- that in this particular case L is a fresh Expr generated by
1498 -- Add which we are the only ones to use.
1500 Append_To (S, W_Decl);
1502 -- Construct " while W_J < H"
1504 W_Iteration_Scheme :=
1505 Make_Iteration_Scheme
1506 (Loc,
1507 Condition => Make_Op_Lt
1508 (Loc,
1509 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1510 Right_Opnd => New_Copy_Tree (H)));
1512 -- Construct the statements to execute in the loop body
1514 W_Index_Succ :=
1515 Make_Attribute_Reference
1516 (Loc,
1517 Prefix => Index_Base_Name,
1518 Attribute_Name => Name_Succ,
1519 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1521 W_Increment :=
1522 Make_OK_Assignment_Statement
1523 (Loc,
1524 Name => New_Occurrence_Of (W_J, Loc),
1525 Expression => W_Index_Succ);
1527 Append_To (W_Body, W_Increment);
1528 Append_List_To (W_Body,
1529 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr));
1531 -- Construct the final loop
1533 Append_To (S,
1534 Make_Implicit_Loop_Statement
1535 (Node => N,
1536 Identifier => Empty,
1537 Iteration_Scheme => W_Iteration_Scheme,
1538 Statements => W_Body));
1540 return S;
1541 end Gen_While;
1543 --------------------
1544 -- Get_Assoc_Expr --
1545 --------------------
1547 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1548 Typ : constant Entity_Id := Base_Type (Etype (N));
1550 begin
1551 if Box_Present (Assoc) then
1552 if Is_Scalar_Type (Ctype) then
1553 if Present (Default_Aspect_Component_Value (Typ)) then
1554 return Default_Aspect_Component_Value (Typ);
1555 elsif Present (Default_Aspect_Value (Ctype)) then
1556 return Default_Aspect_Value (Ctype);
1557 else
1558 return Empty;
1559 end if;
1561 else
1562 return Empty;
1563 end if;
1565 else
1566 return Expression (Assoc);
1567 end if;
1568 end Get_Assoc_Expr;
1570 ---------------------
1571 -- Index_Base_Name --
1572 ---------------------
1574 function Index_Base_Name return Node_Id is
1575 begin
1576 return New_Occurrence_Of (Index_Base, Sloc (N));
1577 end Index_Base_Name;
1579 ------------------------------------
1580 -- Local_Compile_Time_Known_Value --
1581 ------------------------------------
1583 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1584 begin
1585 return Compile_Time_Known_Value (E)
1586 or else
1587 (Nkind (E) = N_Attribute_Reference
1588 and then Attribute_Name (E) = Name_Val
1589 and then Compile_Time_Known_Value (First (Expressions (E))));
1590 end Local_Compile_Time_Known_Value;
1592 ----------------------
1593 -- Local_Expr_Value --
1594 ----------------------
1596 function Local_Expr_Value (E : Node_Id) return Uint is
1597 begin
1598 if Compile_Time_Known_Value (E) then
1599 return Expr_Value (E);
1600 else
1601 return Expr_Value (First (Expressions (E)));
1602 end if;
1603 end Local_Expr_Value;
1605 -- Build_Array_Aggr_Code Variables
1607 Assoc : Node_Id;
1608 Choice : Node_Id;
1609 Expr : Node_Id;
1610 Typ : Entity_Id;
1612 Others_Assoc : Node_Id := Empty;
1614 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1615 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1616 -- The aggregate bounds of this specific sub-aggregate. Note that if
1617 -- the code generated by Build_Array_Aggr_Code is executed then these
1618 -- bounds are OK. Otherwise a Constraint_Error would have been raised.
1620 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1621 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1622 -- After Duplicate_Subexpr these are side-effect free
1624 Low : Node_Id;
1625 High : Node_Id;
1627 Nb_Choices : Nat := 0;
1628 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1629 -- Used to sort all the different choice values
1631 Nb_Elements : Int;
1632 -- Number of elements in the positional aggregate
1634 New_Code : constant List_Id := New_List;
1636 -- Start of processing for Build_Array_Aggr_Code
1638 begin
1639 -- First before we start, a special case. if we have a bit packed
1640 -- array represented as a modular type, then clear the value to
1641 -- zero first, to ensure that unused bits are properly cleared.
1643 Typ := Etype (N);
1645 if Present (Typ)
1646 and then Is_Bit_Packed_Array (Typ)
1647 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
1648 then
1649 Append_To (New_Code,
1650 Make_Assignment_Statement (Loc,
1651 Name => New_Copy_Tree (Into),
1652 Expression =>
1653 Unchecked_Convert_To (Typ,
1654 Make_Integer_Literal (Loc, Uint_0))));
1655 end if;
1657 -- If the component type contains tasks, we need to build a Master
1658 -- entity in the current scope, because it will be needed if build-
1659 -- in-place functions are called in the expanded code.
1661 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
1662 Build_Master_Entity (Defining_Identifier (Parent (N)));
1663 end if;
1665 -- STEP 1: Process component associations
1667 -- For those associations that may generate a loop, initialize
1668 -- Loop_Actions to collect inserted actions that may be crated.
1670 -- Skip this if no component associations
1672 if No (Expressions (N)) then
1674 -- STEP 1 (a): Sort the discrete choices
1676 Assoc := First (Component_Associations (N));
1677 while Present (Assoc) loop
1678 Choice := First (Choices (Assoc));
1679 while Present (Choice) loop
1680 if Nkind (Choice) = N_Others_Choice then
1681 Set_Loop_Actions (Assoc, New_List);
1682 Others_Assoc := Assoc;
1683 exit;
1684 end if;
1686 Get_Index_Bounds (Choice, Low, High);
1688 if Low /= High then
1689 Set_Loop_Actions (Assoc, New_List);
1690 end if;
1692 Nb_Choices := Nb_Choices + 1;
1694 Table (Nb_Choices) :=
1695 (Choice_Lo => Low,
1696 Choice_Hi => High,
1697 Choice_Node => Get_Assoc_Expr (Assoc));
1699 Next (Choice);
1700 end loop;
1702 Next (Assoc);
1703 end loop;
1705 -- If there is more than one set of choices these must be static
1706 -- and we can therefore sort them. Remember that Nb_Choices does not
1707 -- account for an others choice.
1709 if Nb_Choices > 1 then
1710 Sort_Case_Table (Table);
1711 end if;
1713 -- STEP 1 (b): take care of the whole set of discrete choices
1715 for J in 1 .. Nb_Choices loop
1716 Low := Table (J).Choice_Lo;
1717 High := Table (J).Choice_Hi;
1718 Expr := Table (J).Choice_Node;
1719 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1720 end loop;
1722 -- STEP 1 (c): generate the remaining loops to cover others choice
1723 -- We don't need to generate loops over empty gaps, but if there is
1724 -- a single empty range we must analyze the expression for semantics
1726 if Present (Others_Assoc) then
1727 declare
1728 First : Boolean := True;
1730 begin
1731 for J in 0 .. Nb_Choices loop
1732 if J = 0 then
1733 Low := Aggr_Low;
1734 else
1735 Low := Add (1, To => Table (J).Choice_Hi);
1736 end if;
1738 if J = Nb_Choices then
1739 High := Aggr_High;
1740 else
1741 High := Add (-1, To => Table (J + 1).Choice_Lo);
1742 end if;
1744 -- If this is an expansion within an init proc, make
1745 -- sure that discriminant references are replaced by
1746 -- the corresponding discriminal.
1748 if Inside_Init_Proc then
1749 if Is_Entity_Name (Low)
1750 and then Ekind (Entity (Low)) = E_Discriminant
1751 then
1752 Set_Entity (Low, Discriminal (Entity (Low)));
1753 end if;
1755 if Is_Entity_Name (High)
1756 and then Ekind (Entity (High)) = E_Discriminant
1757 then
1758 Set_Entity (High, Discriminal (Entity (High)));
1759 end if;
1760 end if;
1762 if First
1763 or else not Empty_Range (Low, High)
1764 then
1765 First := False;
1766 Append_List
1767 (Gen_Loop (Low, High,
1768 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
1769 end if;
1770 end loop;
1771 end;
1772 end if;
1774 -- STEP 2: Process positional components
1776 else
1777 -- STEP 2 (a): Generate the assignments for each positional element
1778 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1779 -- Aggr_L is analyzed and Add wants an analyzed expression.
1781 Expr := First (Expressions (N));
1782 Nb_Elements := -1;
1783 while Present (Expr) loop
1784 Nb_Elements := Nb_Elements + 1;
1785 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1786 To => New_Code);
1787 Next (Expr);
1788 end loop;
1790 -- STEP 2 (b): Generate final loop if an others choice is present
1791 -- Here Nb_Elements gives the offset of the last positional element.
1793 if Present (Component_Associations (N)) then
1794 Assoc := Last (Component_Associations (N));
1796 -- Ada 2005 (AI-287)
1798 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1799 Aggr_High,
1800 Get_Assoc_Expr (Assoc)), -- AI-287
1801 To => New_Code);
1802 end if;
1803 end if;
1805 return New_Code;
1806 end Build_Array_Aggr_Code;
1808 ----------------------------
1809 -- Build_Record_Aggr_Code --
1810 ----------------------------
1812 function Build_Record_Aggr_Code
1813 (N : Node_Id;
1814 Typ : Entity_Id;
1815 Lhs : Node_Id) return List_Id
1817 Loc : constant Source_Ptr := Sloc (N);
1818 L : constant List_Id := New_List;
1819 N_Typ : constant Entity_Id := Etype (N);
1821 Comp : Node_Id;
1822 Instr : Node_Id;
1823 Ref : Node_Id;
1824 Target : Entity_Id;
1825 Comp_Type : Entity_Id;
1826 Selector : Entity_Id;
1827 Comp_Expr : Node_Id;
1828 Expr_Q : Node_Id;
1830 -- If this is an internal aggregate, the External_Final_List is an
1831 -- expression for the controller record of the enclosing type.
1833 -- If the current aggregate has several controlled components, this
1834 -- expression will appear in several calls to attach to the finali-
1835 -- zation list, and it must not be shared.
1837 Ancestor_Is_Expression : Boolean := False;
1838 Ancestor_Is_Subtype_Mark : Boolean := False;
1840 Init_Typ : Entity_Id := Empty;
1842 Finalization_Done : Boolean := False;
1843 -- True if Generate_Finalization_Actions has already been called; calls
1844 -- after the first do nothing.
1846 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1847 -- Returns the value that the given discriminant of an ancestor type
1848 -- should receive (in the absence of a conflict with the value provided
1849 -- by an ancestor part of an extension aggregate).
1851 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1852 -- Check that each of the discriminant values defined by the ancestor
1853 -- part of an extension aggregate match the corresponding values
1854 -- provided by either an association of the aggregate or by the
1855 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1857 function Compatible_Int_Bounds
1858 (Agg_Bounds : Node_Id;
1859 Typ_Bounds : Node_Id) return Boolean;
1860 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1861 -- assumed that both bounds are integer ranges.
1863 procedure Generate_Finalization_Actions;
1864 -- Deal with the various controlled type data structure initializations
1865 -- (but only if it hasn't been done already).
1867 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1868 -- Returns the first discriminant association in the constraint
1869 -- associated with T, if any, otherwise returns Empty.
1871 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
1872 -- If Typ is derived, and constrains discriminants of the parent type,
1873 -- these discriminants are not components of the aggregate, and must be
1874 -- initialized. The assignments are appended to List. The same is done
1875 -- if Typ derives fron an already constrained subtype of a discriminated
1876 -- parent type.
1878 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
1879 -- If the ancestor part is an unconstrained type and further ancestors
1880 -- do not provide discriminants for it, check aggregate components for
1881 -- values of the discriminants.
1883 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1884 -- Check whether Bounds is a range node and its lower and higher bounds
1885 -- are integers literals.
1887 ---------------------------------
1888 -- Ancestor_Discriminant_Value --
1889 ---------------------------------
1891 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1892 Assoc : Node_Id;
1893 Assoc_Elmt : Elmt_Id;
1894 Aggr_Comp : Entity_Id;
1895 Corresp_Disc : Entity_Id;
1896 Current_Typ : Entity_Id := Base_Type (Typ);
1897 Parent_Typ : Entity_Id;
1898 Parent_Disc : Entity_Id;
1899 Save_Assoc : Node_Id := Empty;
1901 begin
1902 -- First check any discriminant associations to see if any of them
1903 -- provide a value for the discriminant.
1905 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1906 Assoc := First (Component_Associations (N));
1907 while Present (Assoc) loop
1908 Aggr_Comp := Entity (First (Choices (Assoc)));
1910 if Ekind (Aggr_Comp) = E_Discriminant then
1911 Save_Assoc := Expression (Assoc);
1913 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1914 while Present (Corresp_Disc) loop
1916 -- If found a corresponding discriminant then return the
1917 -- value given in the aggregate. (Note: this is not
1918 -- correct in the presence of side effects. ???)
1920 if Disc = Corresp_Disc then
1921 return Duplicate_Subexpr (Expression (Assoc));
1922 end if;
1924 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
1925 end loop;
1926 end if;
1928 Next (Assoc);
1929 end loop;
1930 end if;
1932 -- No match found in aggregate, so chain up parent types to find
1933 -- a constraint that defines the value of the discriminant.
1935 Parent_Typ := Etype (Current_Typ);
1936 while Current_Typ /= Parent_Typ loop
1937 if Has_Discriminants (Parent_Typ)
1938 and then not Has_Unknown_Discriminants (Parent_Typ)
1939 then
1940 Parent_Disc := First_Discriminant (Parent_Typ);
1942 -- We either get the association from the subtype indication
1943 -- of the type definition itself, or from the discriminant
1944 -- constraint associated with the type entity (which is
1945 -- preferable, but it's not always present ???)
1947 if Is_Empty_Elmt_List (
1948 Discriminant_Constraint (Current_Typ))
1949 then
1950 Assoc := Get_Constraint_Association (Current_Typ);
1951 Assoc_Elmt := No_Elmt;
1952 else
1953 Assoc_Elmt :=
1954 First_Elmt (Discriminant_Constraint (Current_Typ));
1955 Assoc := Node (Assoc_Elmt);
1956 end if;
1958 -- Traverse the discriminants of the parent type looking
1959 -- for one that corresponds.
1961 while Present (Parent_Disc) and then Present (Assoc) loop
1962 Corresp_Disc := Parent_Disc;
1963 while Present (Corresp_Disc)
1964 and then Disc /= Corresp_Disc
1965 loop
1966 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
1967 end loop;
1969 if Disc = Corresp_Disc then
1970 if Nkind (Assoc) = N_Discriminant_Association then
1971 Assoc := Expression (Assoc);
1972 end if;
1974 -- If the located association directly denotes
1975 -- a discriminant, then use the value of a saved
1976 -- association of the aggregate. This is an approach
1977 -- used to handle certain cases involving multiple
1978 -- discriminants mapped to a single discriminant of
1979 -- a descendant. It's not clear how to locate the
1980 -- appropriate discriminant value for such cases. ???
1982 if Is_Entity_Name (Assoc)
1983 and then Ekind (Entity (Assoc)) = E_Discriminant
1984 then
1985 Assoc := Save_Assoc;
1986 end if;
1988 return Duplicate_Subexpr (Assoc);
1989 end if;
1991 Next_Discriminant (Parent_Disc);
1993 if No (Assoc_Elmt) then
1994 Next (Assoc);
1996 else
1997 Next_Elmt (Assoc_Elmt);
1999 if Present (Assoc_Elmt) then
2000 Assoc := Node (Assoc_Elmt);
2001 else
2002 Assoc := Empty;
2003 end if;
2004 end if;
2005 end loop;
2006 end if;
2008 Current_Typ := Parent_Typ;
2009 Parent_Typ := Etype (Current_Typ);
2010 end loop;
2012 -- In some cases there's no ancestor value to locate (such as
2013 -- when an ancestor part given by an expression defines the
2014 -- discriminant value).
2016 return Empty;
2017 end Ancestor_Discriminant_Value;
2019 ----------------------------------
2020 -- Check_Ancestor_Discriminants --
2021 ----------------------------------
2023 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2024 Discr : Entity_Id;
2025 Disc_Value : Node_Id;
2026 Cond : Node_Id;
2028 begin
2029 Discr := First_Discriminant (Base_Type (Anc_Typ));
2030 while Present (Discr) loop
2031 Disc_Value := Ancestor_Discriminant_Value (Discr);
2033 if Present (Disc_Value) then
2034 Cond := Make_Op_Ne (Loc,
2035 Left_Opnd =>
2036 Make_Selected_Component (Loc,
2037 Prefix => New_Copy_Tree (Target),
2038 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2039 Right_Opnd => Disc_Value);
2041 Append_To (L,
2042 Make_Raise_Constraint_Error (Loc,
2043 Condition => Cond,
2044 Reason => CE_Discriminant_Check_Failed));
2045 end if;
2047 Next_Discriminant (Discr);
2048 end loop;
2049 end Check_Ancestor_Discriminants;
2051 ---------------------------
2052 -- Compatible_Int_Bounds --
2053 ---------------------------
2055 function Compatible_Int_Bounds
2056 (Agg_Bounds : Node_Id;
2057 Typ_Bounds : Node_Id) return Boolean
2059 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2060 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2061 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2062 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2063 begin
2064 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2065 end Compatible_Int_Bounds;
2067 --------------------------------
2068 -- Get_Constraint_Association --
2069 --------------------------------
2071 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2072 Indic : Node_Id;
2073 Typ : Entity_Id;
2075 begin
2076 Typ := T;
2078 -- If type is private, get constraint from full view. This was
2079 -- previously done in an instance context, but is needed whenever
2080 -- the ancestor part has a discriminant, possibly inherited through
2081 -- multiple derivations.
2083 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2084 Typ := Full_View (Typ);
2085 end if;
2087 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2089 -- Verify that the subtype indication carries a constraint
2091 if Nkind (Indic) = N_Subtype_Indication
2092 and then Present (Constraint (Indic))
2093 then
2094 return First (Constraints (Constraint (Indic)));
2095 end if;
2097 return Empty;
2098 end Get_Constraint_Association;
2100 -------------------------------------
2101 -- Get_Explicit_Discriminant_Value --
2102 -------------------------------------
2104 function Get_Explicit_Discriminant_Value
2105 (D : Entity_Id) return Node_Id
2107 Assoc : Node_Id;
2108 Choice : Node_Id;
2109 Val : Node_Id;
2111 begin
2112 -- The aggregate has been normalized and all associations have a
2113 -- single choice.
2115 Assoc := First (Component_Associations (N));
2116 while Present (Assoc) loop
2117 Choice := First (Choices (Assoc));
2119 if Chars (Choice) = Chars (D) then
2120 Val := Expression (Assoc);
2121 Remove (Assoc);
2122 return Val;
2123 end if;
2125 Next (Assoc);
2126 end loop;
2128 return Empty;
2129 end Get_Explicit_Discriminant_Value;
2131 -------------------------------
2132 -- Init_Hidden_Discriminants --
2133 -------------------------------
2135 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2136 Btype : Entity_Id;
2137 Parent_Type : Entity_Id;
2138 Disc : Entity_Id;
2139 Discr_Val : Elmt_Id;
2140 In_Aggr_Type : Boolean;
2142 begin
2143 -- The constraints on the hidden discriminants, if present, are kept
2144 -- in the Stored_Constraint list of the type itself, or in that of
2145 -- the base type. If not in the constraints of the aggregate itself,
2146 -- we examine ancestors to find discriminants that are not renamed
2147 -- by other discriminants but constrained explicitly.
2149 In_Aggr_Type := True;
2151 Btype := Base_Type (Typ);
2152 while Is_Derived_Type (Btype)
2153 and then
2154 (Present (Stored_Constraint (Btype))
2155 or else
2156 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2157 loop
2158 Parent_Type := Etype (Btype);
2160 if not Has_Discriminants (Parent_Type) then
2161 return;
2162 end if;
2164 Disc := First_Discriminant (Parent_Type);
2166 -- We know that one of the stored-constraint lists is present
2168 if Present (Stored_Constraint (Btype)) then
2169 Discr_Val := First_Elmt (Stored_Constraint (Btype));
2171 -- For private extension, stored constraint may be on full view
2173 elsif Is_Private_Type (Btype)
2174 and then Present (Full_View (Btype))
2175 and then Present (Stored_Constraint (Full_View (Btype)))
2176 then
2177 Discr_Val := First_Elmt (Stored_Constraint (Full_View (Btype)));
2179 else
2180 Discr_Val := First_Elmt (Stored_Constraint (Typ));
2181 end if;
2183 while Present (Discr_Val) and then Present (Disc) loop
2185 -- Only those discriminants of the parent that are not
2186 -- renamed by discriminants of the derived type need to
2187 -- be added explicitly.
2189 if not Is_Entity_Name (Node (Discr_Val))
2190 or else Ekind (Entity (Node (Discr_Val))) /= E_Discriminant
2191 then
2192 Comp_Expr :=
2193 Make_Selected_Component (Loc,
2194 Prefix => New_Copy_Tree (Target),
2195 Selector_Name => New_Occurrence_Of (Disc, Loc));
2197 Instr :=
2198 Make_OK_Assignment_Statement (Loc,
2199 Name => Comp_Expr,
2200 Expression => New_Copy_Tree (Node (Discr_Val)));
2202 Set_No_Ctrl_Actions (Instr);
2203 Append_To (List, Instr);
2204 end if;
2206 Next_Discriminant (Disc);
2207 Next_Elmt (Discr_Val);
2208 end loop;
2210 In_Aggr_Type := False;
2211 Btype := Base_Type (Parent_Type);
2212 end loop;
2213 end Init_Hidden_Discriminants;
2215 -------------------------
2216 -- Is_Int_Range_Bounds --
2217 -------------------------
2219 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2220 begin
2221 return Nkind (Bounds) = N_Range
2222 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2223 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2224 end Is_Int_Range_Bounds;
2226 -----------------------------------
2227 -- Generate_Finalization_Actions --
2228 -----------------------------------
2230 procedure Generate_Finalization_Actions is
2231 begin
2232 -- Do the work only the first time this is called
2234 if Finalization_Done then
2235 return;
2236 end if;
2238 Finalization_Done := True;
2240 -- Determine the external finalization list. It is either the
2241 -- finalization list of the outer-scope or the one coming from an
2242 -- outer aggregate. When the target is not a temporary, the proper
2243 -- scope is the scope of the target rather than the potentially
2244 -- transient current scope.
2246 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2247 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2248 Set_Assignment_OK (Ref);
2250 Append_To (L,
2251 Make_Procedure_Call_Statement (Loc,
2252 Name =>
2253 New_Occurrence_Of
2254 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2255 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2256 end if;
2257 end Generate_Finalization_Actions;
2259 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2260 -- If default expression of a component mentions a discriminant of the
2261 -- type, it must be rewritten as the discriminant of the target object.
2263 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2264 -- If the aggregate contains a self-reference, traverse each expression
2265 -- to replace a possible self-reference with a reference to the proper
2266 -- component of the target of the assignment.
2268 --------------------------
2269 -- Rewrite_Discriminant --
2270 --------------------------
2272 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2273 begin
2274 if Is_Entity_Name (Expr)
2275 and then Present (Entity (Expr))
2276 and then Ekind (Entity (Expr)) = E_In_Parameter
2277 and then Present (Discriminal_Link (Entity (Expr)))
2278 and then Scope (Discriminal_Link (Entity (Expr))) =
2279 Base_Type (Etype (N))
2280 then
2281 Rewrite (Expr,
2282 Make_Selected_Component (Loc,
2283 Prefix => New_Copy_Tree (Lhs),
2284 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2285 end if;
2287 return OK;
2288 end Rewrite_Discriminant;
2290 ------------------
2291 -- Replace_Type --
2292 ------------------
2294 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2295 begin
2296 -- Note regarding the Root_Type test below: Aggregate components for
2297 -- self-referential types include attribute references to the current
2298 -- instance, of the form: Typ'access, etc.. These references are
2299 -- rewritten as references to the target of the aggregate: the
2300 -- left-hand side of an assignment, the entity in a declaration,
2301 -- or a temporary. Without this test, we would improperly extended
2302 -- this rewriting to attribute references whose prefix was not the
2303 -- type of the aggregate.
2305 if Nkind (Expr) = N_Attribute_Reference
2306 and then Is_Entity_Name (Prefix (Expr))
2307 and then Is_Type (Entity (Prefix (Expr)))
2308 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2309 then
2310 if Is_Entity_Name (Lhs) then
2311 Rewrite (Prefix (Expr),
2312 New_Occurrence_Of (Entity (Lhs), Loc));
2314 elsif Nkind (Lhs) = N_Selected_Component then
2315 Rewrite (Expr,
2316 Make_Attribute_Reference (Loc,
2317 Attribute_Name => Name_Unrestricted_Access,
2318 Prefix => New_Copy_Tree (Lhs)));
2319 Set_Analyzed (Parent (Expr), False);
2321 else
2322 Rewrite (Expr,
2323 Make_Attribute_Reference (Loc,
2324 Attribute_Name => Name_Unrestricted_Access,
2325 Prefix => New_Copy_Tree (Lhs)));
2326 Set_Analyzed (Parent (Expr), False);
2327 end if;
2328 end if;
2330 return OK;
2331 end Replace_Type;
2333 procedure Replace_Self_Reference is
2334 new Traverse_Proc (Replace_Type);
2336 procedure Replace_Discriminants is
2337 new Traverse_Proc (Rewrite_Discriminant);
2339 -- Start of processing for Build_Record_Aggr_Code
2341 begin
2342 if Has_Self_Reference (N) then
2343 Replace_Self_Reference (N);
2344 end if;
2346 -- If the target of the aggregate is class-wide, we must convert it
2347 -- to the actual type of the aggregate, so that the proper components
2348 -- are visible. We know already that the types are compatible.
2350 if Present (Etype (Lhs))
2351 and then Is_Class_Wide_Type (Etype (Lhs))
2352 then
2353 Target := Unchecked_Convert_To (Typ, Lhs);
2354 else
2355 Target := Lhs;
2356 end if;
2358 -- Deal with the ancestor part of extension aggregates or with the
2359 -- discriminants of the root type.
2361 if Nkind (N) = N_Extension_Aggregate then
2362 declare
2363 Ancestor : constant Node_Id := Ancestor_Part (N);
2364 Assign : List_Id;
2366 begin
2367 -- If the ancestor part is a subtype mark "T", we generate
2369 -- init-proc (T (tmp)); if T is constrained and
2370 -- init-proc (S (tmp)); where S applies an appropriate
2371 -- constraint if T is unconstrained
2373 if Is_Entity_Name (Ancestor)
2374 and then Is_Type (Entity (Ancestor))
2375 then
2376 Ancestor_Is_Subtype_Mark := True;
2378 if Is_Constrained (Entity (Ancestor)) then
2379 Init_Typ := Entity (Ancestor);
2381 -- For an ancestor part given by an unconstrained type mark,
2382 -- create a subtype constrained by appropriate corresponding
2383 -- discriminant values coming from either associations of the
2384 -- aggregate or a constraint on a parent type. The subtype will
2385 -- be used to generate the correct default value for the
2386 -- ancestor part.
2388 elsif Has_Discriminants (Entity (Ancestor)) then
2389 declare
2390 Anc_Typ : constant Entity_Id := Entity (Ancestor);
2391 Anc_Constr : constant List_Id := New_List;
2392 Discrim : Entity_Id;
2393 Disc_Value : Node_Id;
2394 New_Indic : Node_Id;
2395 Subt_Decl : Node_Id;
2397 begin
2398 Discrim := First_Discriminant (Anc_Typ);
2399 while Present (Discrim) loop
2400 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2402 -- If no usable discriminant in ancestors, check
2403 -- whether aggregate has an explicit value for it.
2405 if No (Disc_Value) then
2406 Disc_Value :=
2407 Get_Explicit_Discriminant_Value (Discrim);
2408 end if;
2410 Append_To (Anc_Constr, Disc_Value);
2411 Next_Discriminant (Discrim);
2412 end loop;
2414 New_Indic :=
2415 Make_Subtype_Indication (Loc,
2416 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2417 Constraint =>
2418 Make_Index_Or_Discriminant_Constraint (Loc,
2419 Constraints => Anc_Constr));
2421 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2423 Subt_Decl :=
2424 Make_Subtype_Declaration (Loc,
2425 Defining_Identifier => Init_Typ,
2426 Subtype_Indication => New_Indic);
2428 -- Itypes must be analyzed with checks off Declaration
2429 -- must have a parent for proper handling of subsidiary
2430 -- actions.
2432 Set_Parent (Subt_Decl, N);
2433 Analyze (Subt_Decl, Suppress => All_Checks);
2434 end;
2435 end if;
2437 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2438 Set_Assignment_OK (Ref);
2440 if not Is_Interface (Init_Typ) then
2441 Append_List_To (L,
2442 Build_Initialization_Call (Loc,
2443 Id_Ref => Ref,
2444 Typ => Init_Typ,
2445 In_Init_Proc => Within_Init_Proc,
2446 With_Default_Init => Has_Default_Init_Comps (N)
2447 or else
2448 Has_Task (Base_Type (Init_Typ))));
2450 if Is_Constrained (Entity (Ancestor))
2451 and then Has_Discriminants (Entity (Ancestor))
2452 then
2453 Check_Ancestor_Discriminants (Entity (Ancestor));
2454 end if;
2455 end if;
2457 -- Handle calls to C++ constructors
2459 elsif Is_CPP_Constructor_Call (Ancestor) then
2460 Init_Typ := Etype (Ancestor);
2461 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2462 Set_Assignment_OK (Ref);
2464 Append_List_To (L,
2465 Build_Initialization_Call (Loc,
2466 Id_Ref => Ref,
2467 Typ => Init_Typ,
2468 In_Init_Proc => Within_Init_Proc,
2469 With_Default_Init => Has_Default_Init_Comps (N),
2470 Constructor_Ref => Ancestor));
2472 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2473 -- limited type, a recursive call expands the ancestor. Note that
2474 -- in the limited case, the ancestor part must be either a
2475 -- function call (possibly qualified, or wrapped in an unchecked
2476 -- conversion) or aggregate (definitely qualified).
2478 -- The ancestor part can also be a function call (that may be
2479 -- transformed into an explicit dereference) or a qualification
2480 -- of one such.
2482 elsif Is_Limited_Type (Etype (Ancestor))
2483 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
2484 N_Extension_Aggregate)
2485 then
2486 Ancestor_Is_Expression := True;
2488 -- Set up finalization data for enclosing record, because
2489 -- controlled subcomponents of the ancestor part will be
2490 -- attached to it.
2492 Generate_Finalization_Actions;
2494 Append_List_To (L,
2495 Build_Record_Aggr_Code
2496 (N => Unqualify (Ancestor),
2497 Typ => Etype (Unqualify (Ancestor)),
2498 Lhs => Target));
2500 -- If the ancestor part is an expression "E", we generate
2502 -- T (tmp) := E;
2504 -- In Ada 2005, this includes the case of a (possibly qualified)
2505 -- limited function call. The assignment will turn into a
2506 -- build-in-place function call (for further details, see
2507 -- Make_Build_In_Place_Call_In_Assignment).
2509 else
2510 Ancestor_Is_Expression := True;
2511 Init_Typ := Etype (Ancestor);
2513 -- If the ancestor part is an aggregate, force its full
2514 -- expansion, which was delayed.
2516 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
2517 N_Extension_Aggregate)
2518 then
2519 Set_Analyzed (Ancestor, False);
2520 Set_Analyzed (Expression (Ancestor), False);
2521 end if;
2523 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2524 Set_Assignment_OK (Ref);
2526 -- Make the assignment without usual controlled actions, since
2527 -- we only want to Adjust afterwards, but not to Finalize
2528 -- beforehand. Add manual Adjust when necessary.
2530 Assign := New_List (
2531 Make_OK_Assignment_Statement (Loc,
2532 Name => Ref,
2533 Expression => Ancestor));
2534 Set_No_Ctrl_Actions (First (Assign));
2536 -- Assign the tag now to make sure that the dispatching call in
2537 -- the subsequent deep_adjust works properly (unless VM_Target,
2538 -- where tags are implicit).
2540 if Tagged_Type_Expansion then
2541 Instr :=
2542 Make_OK_Assignment_Statement (Loc,
2543 Name =>
2544 Make_Selected_Component (Loc,
2545 Prefix => New_Copy_Tree (Target),
2546 Selector_Name =>
2547 New_Occurrence_Of
2548 (First_Tag_Component (Base_Type (Typ)), Loc)),
2550 Expression =>
2551 Unchecked_Convert_To (RTE (RE_Tag),
2552 New_Occurrence_Of
2553 (Node (First_Elmt
2554 (Access_Disp_Table (Base_Type (Typ)))),
2555 Loc)));
2557 Set_Assignment_OK (Name (Instr));
2558 Append_To (Assign, Instr);
2560 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2561 -- also initialize tags of the secondary dispatch tables.
2563 if Has_Interfaces (Base_Type (Typ)) then
2564 Init_Secondary_Tags
2565 (Typ => Base_Type (Typ),
2566 Target => Target,
2567 Stmts_List => Assign);
2568 end if;
2569 end if;
2571 -- Call Adjust manually
2573 if Needs_Finalization (Etype (Ancestor))
2574 and then not Is_Limited_Type (Etype (Ancestor))
2575 then
2576 Append_To (Assign,
2577 Make_Adjust_Call
2578 (Obj_Ref => New_Copy_Tree (Ref),
2579 Typ => Etype (Ancestor)));
2580 end if;
2582 Append_To (L,
2583 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2585 if Has_Discriminants (Init_Typ) then
2586 Check_Ancestor_Discriminants (Init_Typ);
2587 end if;
2588 end if;
2589 end;
2591 -- Generate assignments of hidden discriminants. If the base type is
2592 -- an unchecked union, the discriminants are unknown to the back-end
2593 -- and absent from a value of the type, so assignments for them are
2594 -- not emitted.
2596 if Has_Discriminants (Typ)
2597 and then not Is_Unchecked_Union (Base_Type (Typ))
2598 then
2599 Init_Hidden_Discriminants (Typ, L);
2600 end if;
2602 -- Normal case (not an extension aggregate)
2604 else
2605 -- Generate the discriminant expressions, component by component.
2606 -- If the base type is an unchecked union, the discriminants are
2607 -- unknown to the back-end and absent from a value of the type, so
2608 -- assignments for them are not emitted.
2610 if Has_Discriminants (Typ)
2611 and then not Is_Unchecked_Union (Base_Type (Typ))
2612 then
2613 Init_Hidden_Discriminants (Typ, L);
2615 -- Generate discriminant init values for the visible discriminants
2617 declare
2618 Discriminant : Entity_Id;
2619 Discriminant_Value : Node_Id;
2621 begin
2622 Discriminant := First_Stored_Discriminant (Typ);
2623 while Present (Discriminant) loop
2624 Comp_Expr :=
2625 Make_Selected_Component (Loc,
2626 Prefix => New_Copy_Tree (Target),
2627 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2629 Discriminant_Value :=
2630 Get_Discriminant_Value
2631 (Discriminant,
2632 N_Typ,
2633 Discriminant_Constraint (N_Typ));
2635 Instr :=
2636 Make_OK_Assignment_Statement (Loc,
2637 Name => Comp_Expr,
2638 Expression => New_Copy_Tree (Discriminant_Value));
2640 Set_No_Ctrl_Actions (Instr);
2641 Append_To (L, Instr);
2643 Next_Stored_Discriminant (Discriminant);
2644 end loop;
2645 end;
2646 end if;
2647 end if;
2649 -- For CPP types we generate an implicit call to the C++ default
2650 -- constructor to ensure the proper initialization of the _Tag
2651 -- component.
2653 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
2654 Invoke_Constructor : declare
2655 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
2657 procedure Invoke_IC_Proc (T : Entity_Id);
2658 -- Recursive routine used to climb to parents. Required because
2659 -- parents must be initialized before descendants to ensure
2660 -- propagation of inherited C++ slots.
2662 --------------------
2663 -- Invoke_IC_Proc --
2664 --------------------
2666 procedure Invoke_IC_Proc (T : Entity_Id) is
2667 begin
2668 -- Avoid generating extra calls. Initialization required
2669 -- only for types defined from the level of derivation of
2670 -- type of the constructor and the type of the aggregate.
2672 if T = CPP_Parent then
2673 return;
2674 end if;
2676 Invoke_IC_Proc (Etype (T));
2678 -- Generate call to the IC routine
2680 if Present (CPP_Init_Proc (T)) then
2681 Append_To (L,
2682 Make_Procedure_Call_Statement (Loc,
2683 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
2684 end if;
2685 end Invoke_IC_Proc;
2687 -- Start of processing for Invoke_Constructor
2689 begin
2690 -- Implicit invocation of the C++ constructor
2692 if Nkind (N) = N_Aggregate then
2693 Append_To (L,
2694 Make_Procedure_Call_Statement (Loc,
2695 Name =>
2696 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
2697 Parameter_Associations => New_List (
2698 Unchecked_Convert_To (CPP_Parent,
2699 New_Copy_Tree (Lhs)))));
2700 end if;
2702 Invoke_IC_Proc (Typ);
2703 end Invoke_Constructor;
2704 end if;
2706 -- Generate the assignments, component by component
2708 -- tmp.comp1 := Expr1_From_Aggr;
2709 -- tmp.comp2 := Expr2_From_Aggr;
2710 -- ....
2712 Comp := First (Component_Associations (N));
2713 while Present (Comp) loop
2714 Selector := Entity (First (Choices (Comp)));
2716 -- C++ constructors
2718 if Is_CPP_Constructor_Call (Expression (Comp)) then
2719 Append_List_To (L,
2720 Build_Initialization_Call (Loc,
2721 Id_Ref =>
2722 Make_Selected_Component (Loc,
2723 Prefix => New_Copy_Tree (Target),
2724 Selector_Name => New_Occurrence_Of (Selector, Loc)),
2725 Typ => Etype (Selector),
2726 Enclos_Type => Typ,
2727 With_Default_Init => True,
2728 Constructor_Ref => Expression (Comp)));
2730 -- Ada 2005 (AI-287): For each default-initialized component generate
2731 -- a call to the corresponding IP subprogram if available.
2733 elsif Box_Present (Comp)
2734 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2735 then
2736 if Ekind (Selector) /= E_Discriminant then
2737 Generate_Finalization_Actions;
2738 end if;
2740 -- Ada 2005 (AI-287): If the component type has tasks then
2741 -- generate the activation chain and master entities (except
2742 -- in case of an allocator because in that case these entities
2743 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2745 declare
2746 Ctype : constant Entity_Id := Etype (Selector);
2747 Inside_Allocator : Boolean := False;
2748 P : Node_Id := Parent (N);
2750 begin
2751 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2752 while Present (P) loop
2753 if Nkind (P) = N_Allocator then
2754 Inside_Allocator := True;
2755 exit;
2756 end if;
2758 P := Parent (P);
2759 end loop;
2761 if not Inside_Init_Proc and not Inside_Allocator then
2762 Build_Activation_Chain_Entity (N);
2763 end if;
2764 end if;
2765 end;
2767 Append_List_To (L,
2768 Build_Initialization_Call (Loc,
2769 Id_Ref => Make_Selected_Component (Loc,
2770 Prefix => New_Copy_Tree (Target),
2771 Selector_Name =>
2772 New_Occurrence_Of (Selector, Loc)),
2773 Typ => Etype (Selector),
2774 Enclos_Type => Typ,
2775 With_Default_Init => True));
2777 -- Prepare for component assignment
2779 elsif Ekind (Selector) /= E_Discriminant
2780 or else Nkind (N) = N_Extension_Aggregate
2781 then
2782 -- All the discriminants have now been assigned
2784 -- This is now a good moment to initialize and attach all the
2785 -- controllers. Their position may depend on the discriminants.
2787 if Ekind (Selector) /= E_Discriminant then
2788 Generate_Finalization_Actions;
2789 end if;
2791 Comp_Type := Underlying_Type (Etype (Selector));
2792 Comp_Expr :=
2793 Make_Selected_Component (Loc,
2794 Prefix => New_Copy_Tree (Target),
2795 Selector_Name => New_Occurrence_Of (Selector, Loc));
2797 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2798 Expr_Q := Expression (Expression (Comp));
2799 else
2800 Expr_Q := Expression (Comp);
2801 end if;
2803 -- Now either create the assignment or generate the code for the
2804 -- inner aggregate top-down.
2806 if Is_Delayed_Aggregate (Expr_Q) then
2808 -- We have the following case of aggregate nesting inside
2809 -- an object declaration:
2811 -- type Arr_Typ is array (Integer range <>) of ...;
2813 -- type Rec_Typ (...) is record
2814 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2815 -- end record;
2817 -- Obj_Rec_Typ : Rec_Typ := (...,
2818 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2820 -- The length of the ranges of the aggregate and Obj_Add_Typ
2821 -- are equal (B - A = Y - X), but they do not coincide (X /=
2822 -- A and B /= Y). This case requires array sliding which is
2823 -- performed in the following manner:
2825 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2826 -- Temp : Arr_Sub;
2827 -- Temp (X) := (...);
2828 -- ...
2829 -- Temp (Y) := (...);
2830 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2832 if Ekind (Comp_Type) = E_Array_Subtype
2833 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2834 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2835 and then not
2836 Compatible_Int_Bounds
2837 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2838 Typ_Bounds => First_Index (Comp_Type))
2839 then
2840 -- Create the array subtype with bounds equal to those of
2841 -- the corresponding aggregate.
2843 declare
2844 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
2846 SubD : constant Node_Id :=
2847 Make_Subtype_Declaration (Loc,
2848 Defining_Identifier => SubE,
2849 Subtype_Indication =>
2850 Make_Subtype_Indication (Loc,
2851 Subtype_Mark =>
2852 New_Occurrence_Of (Etype (Comp_Type), Loc),
2853 Constraint =>
2854 Make_Index_Or_Discriminant_Constraint
2855 (Loc,
2856 Constraints => New_List (
2857 New_Copy_Tree
2858 (Aggregate_Bounds (Expr_Q))))));
2860 -- Create a temporary array of the above subtype which
2861 -- will be used to capture the aggregate assignments.
2863 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
2865 TmpD : constant Node_Id :=
2866 Make_Object_Declaration (Loc,
2867 Defining_Identifier => TmpE,
2868 Object_Definition => New_Occurrence_Of (SubE, Loc));
2870 begin
2871 Set_No_Initialization (TmpD);
2872 Append_To (L, SubD);
2873 Append_To (L, TmpD);
2875 -- Expand aggregate into assignments to the temp array
2877 Append_List_To (L,
2878 Late_Expansion (Expr_Q, Comp_Type,
2879 New_Occurrence_Of (TmpE, Loc)));
2881 -- Slide
2883 Append_To (L,
2884 Make_Assignment_Statement (Loc,
2885 Name => New_Copy_Tree (Comp_Expr),
2886 Expression => New_Occurrence_Of (TmpE, Loc)));
2887 end;
2889 -- Normal case (sliding not required)
2891 else
2892 Append_List_To (L,
2893 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
2894 end if;
2896 -- Expr_Q is not delayed aggregate
2898 else
2899 if Has_Discriminants (Typ) then
2900 Replace_Discriminants (Expr_Q);
2902 -- If the component is an array type that depends on
2903 -- discriminants, and the expression is a single Others
2904 -- clause, create an explicit subtype for it because the
2905 -- backend has troubles recovering the actual bounds.
2907 if Nkind (Expr_Q) = N_Aggregate
2908 and then Is_Array_Type (Comp_Type)
2909 and then Present (Component_Associations (Expr_Q))
2910 then
2911 declare
2912 Assoc : constant Node_Id :=
2913 First (Component_Associations (Expr_Q));
2914 Decl : Node_Id;
2916 begin
2917 if Nkind (First (Choices (Assoc))) = N_Others_Choice
2918 then
2919 Decl :=
2920 Build_Actual_Subtype_Of_Component
2921 (Comp_Type, Comp_Expr);
2923 -- If the component type does not in fact depend on
2924 -- discriminants, the subtype declaration is empty.
2926 if Present (Decl) then
2927 Append_To (L, Decl);
2928 Set_Etype (Comp_Expr, Defining_Entity (Decl));
2929 end if;
2930 end if;
2931 end;
2932 end if;
2933 end if;
2935 Instr :=
2936 Make_OK_Assignment_Statement (Loc,
2937 Name => Comp_Expr,
2938 Expression => Expr_Q);
2940 Set_No_Ctrl_Actions (Instr);
2941 Append_To (L, Instr);
2943 -- Adjust the tag if tagged (because of possible view
2944 -- conversions), unless compiling for a VM where tags are
2945 -- implicit.
2947 -- tmp.comp._tag := comp_typ'tag;
2949 if Is_Tagged_Type (Comp_Type)
2950 and then Tagged_Type_Expansion
2951 then
2952 Instr :=
2953 Make_OK_Assignment_Statement (Loc,
2954 Name =>
2955 Make_Selected_Component (Loc,
2956 Prefix => New_Copy_Tree (Comp_Expr),
2957 Selector_Name =>
2958 New_Occurrence_Of
2959 (First_Tag_Component (Comp_Type), Loc)),
2961 Expression =>
2962 Unchecked_Convert_To (RTE (RE_Tag),
2963 New_Occurrence_Of
2964 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
2965 Loc)));
2967 Append_To (L, Instr);
2968 end if;
2970 -- Generate:
2971 -- Adjust (tmp.comp);
2973 if Needs_Finalization (Comp_Type)
2974 and then not Is_Limited_Type (Comp_Type)
2975 then
2976 Append_To (L,
2977 Make_Adjust_Call
2978 (Obj_Ref => New_Copy_Tree (Comp_Expr),
2979 Typ => Comp_Type));
2980 end if;
2981 end if;
2983 -- comment would be good here ???
2985 elsif Ekind (Selector) = E_Discriminant
2986 and then Nkind (N) /= N_Extension_Aggregate
2987 and then Nkind (Parent (N)) = N_Component_Association
2988 and then Is_Constrained (Typ)
2989 then
2990 -- We must check that the discriminant value imposed by the
2991 -- context is the same as the value given in the subaggregate,
2992 -- because after the expansion into assignments there is no
2993 -- record on which to perform a regular discriminant check.
2995 declare
2996 D_Val : Elmt_Id;
2997 Disc : Entity_Id;
2999 begin
3000 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3001 Disc := First_Discriminant (Typ);
3002 while Chars (Disc) /= Chars (Selector) loop
3003 Next_Discriminant (Disc);
3004 Next_Elmt (D_Val);
3005 end loop;
3007 pragma Assert (Present (D_Val));
3009 -- This check cannot performed for components that are
3010 -- constrained by a current instance, because this is not a
3011 -- value that can be compared with the actual constraint.
3013 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3014 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3015 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3016 then
3017 Append_To (L,
3018 Make_Raise_Constraint_Error (Loc,
3019 Condition =>
3020 Make_Op_Ne (Loc,
3021 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3022 Right_Opnd => Expression (Comp)),
3023 Reason => CE_Discriminant_Check_Failed));
3025 else
3026 -- Find self-reference in previous discriminant assignment,
3027 -- and replace with proper expression.
3029 declare
3030 Ass : Node_Id;
3032 begin
3033 Ass := First (L);
3034 while Present (Ass) loop
3035 if Nkind (Ass) = N_Assignment_Statement
3036 and then Nkind (Name (Ass)) = N_Selected_Component
3037 and then Chars (Selector_Name (Name (Ass))) =
3038 Chars (Disc)
3039 then
3040 Set_Expression
3041 (Ass, New_Copy_Tree (Expression (Comp)));
3042 exit;
3043 end if;
3044 Next (Ass);
3045 end loop;
3046 end;
3047 end if;
3048 end;
3049 end if;
3051 Next (Comp);
3052 end loop;
3054 -- If the type is tagged, the tag needs to be initialized (unless we
3055 -- are in VM-mode where tags are implicit). It is done late in the
3056 -- initialization process because in some cases, we call the init
3057 -- proc of an ancestor which will not leave out the right tag.
3059 if Ancestor_Is_Expression then
3060 null;
3062 -- For CPP types we generated a call to the C++ default constructor
3063 -- before the components have been initialized to ensure the proper
3064 -- initialization of the _Tag component (see above).
3066 elsif Is_CPP_Class (Typ) then
3067 null;
3069 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3070 Instr :=
3071 Make_OK_Assignment_Statement (Loc,
3072 Name =>
3073 Make_Selected_Component (Loc,
3074 Prefix => New_Copy_Tree (Target),
3075 Selector_Name =>
3076 New_Occurrence_Of
3077 (First_Tag_Component (Base_Type (Typ)), Loc)),
3079 Expression =>
3080 Unchecked_Convert_To (RTE (RE_Tag),
3081 New_Occurrence_Of
3082 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3083 Loc)));
3085 Append_To (L, Instr);
3087 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3088 -- abstract interfaces we must also initialize the tags of the
3089 -- secondary dispatch tables.
3091 if Has_Interfaces (Base_Type (Typ)) then
3092 Init_Secondary_Tags
3093 (Typ => Base_Type (Typ),
3094 Target => Target,
3095 Stmts_List => L);
3096 end if;
3097 end if;
3099 -- If the controllers have not been initialized yet (by lack of non-
3100 -- discriminant components), let's do it now.
3102 Generate_Finalization_Actions;
3104 return L;
3105 end Build_Record_Aggr_Code;
3107 ---------------------------------------
3108 -- Collect_Initialization_Statements --
3109 ---------------------------------------
3111 procedure Collect_Initialization_Statements
3112 (Obj : Entity_Id;
3113 N : Node_Id;
3114 Node_After : Node_Id)
3116 Loc : constant Source_Ptr := Sloc (N);
3117 Init_Actions : constant List_Id := New_List;
3118 Init_Node : Node_Id;
3119 Comp_Stmt : Node_Id;
3121 begin
3122 -- Nothing to do if Obj is already frozen, as in this case we known we
3123 -- won't need to move the initialization statements about later on.
3125 if Is_Frozen (Obj) then
3126 return;
3127 end if;
3129 Init_Node := N;
3130 while Next (Init_Node) /= Node_After loop
3131 Append_To (Init_Actions, Remove_Next (Init_Node));
3132 end loop;
3134 if not Is_Empty_List (Init_Actions) then
3135 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3136 Insert_Action_After (Init_Node, Comp_Stmt);
3137 Set_Initialization_Statements (Obj, Comp_Stmt);
3138 end if;
3139 end Collect_Initialization_Statements;
3141 -------------------------------
3142 -- Convert_Aggr_In_Allocator --
3143 -------------------------------
3145 procedure Convert_Aggr_In_Allocator
3146 (Alloc : Node_Id;
3147 Decl : Node_Id;
3148 Aggr : Node_Id)
3150 Loc : constant Source_Ptr := Sloc (Aggr);
3151 Typ : constant Entity_Id := Etype (Aggr);
3152 Temp : constant Entity_Id := Defining_Identifier (Decl);
3154 Occ : constant Node_Id :=
3155 Unchecked_Convert_To (Typ,
3156 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3158 begin
3159 if Is_Array_Type (Typ) then
3160 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3162 elsif Has_Default_Init_Comps (Aggr) then
3163 declare
3164 L : constant List_Id := New_List;
3165 Init_Stmts : List_Id;
3167 begin
3168 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3170 if Has_Task (Typ) then
3171 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3172 Insert_Actions (Alloc, L);
3173 else
3174 Insert_Actions (Alloc, Init_Stmts);
3175 end if;
3176 end;
3178 else
3179 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3180 end if;
3181 end Convert_Aggr_In_Allocator;
3183 --------------------------------
3184 -- Convert_Aggr_In_Assignment --
3185 --------------------------------
3187 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3188 Aggr : Node_Id := Expression (N);
3189 Typ : constant Entity_Id := Etype (Aggr);
3190 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3192 begin
3193 if Nkind (Aggr) = N_Qualified_Expression then
3194 Aggr := Expression (Aggr);
3195 end if;
3197 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3198 end Convert_Aggr_In_Assignment;
3200 ---------------------------------
3201 -- Convert_Aggr_In_Object_Decl --
3202 ---------------------------------
3204 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3205 Obj : constant Entity_Id := Defining_Identifier (N);
3206 Aggr : Node_Id := Expression (N);
3207 Loc : constant Source_Ptr := Sloc (Aggr);
3208 Typ : constant Entity_Id := Etype (Aggr);
3209 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3211 function Discriminants_Ok return Boolean;
3212 -- If the object type is constrained, the discriminants in the
3213 -- aggregate must be checked against the discriminants of the subtype.
3214 -- This cannot be done using Apply_Discriminant_Checks because after
3215 -- expansion there is no aggregate left to check.
3217 ----------------------
3218 -- Discriminants_Ok --
3219 ----------------------
3221 function Discriminants_Ok return Boolean is
3222 Cond : Node_Id := Empty;
3223 Check : Node_Id;
3224 D : Entity_Id;
3225 Disc1 : Elmt_Id;
3226 Disc2 : Elmt_Id;
3227 Val1 : Node_Id;
3228 Val2 : Node_Id;
3230 begin
3231 D := First_Discriminant (Typ);
3232 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3233 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3234 while Present (Disc1) and then Present (Disc2) loop
3235 Val1 := Node (Disc1);
3236 Val2 := Node (Disc2);
3238 if not Is_OK_Static_Expression (Val1)
3239 or else not Is_OK_Static_Expression (Val2)
3240 then
3241 Check := Make_Op_Ne (Loc,
3242 Left_Opnd => Duplicate_Subexpr (Val1),
3243 Right_Opnd => Duplicate_Subexpr (Val2));
3245 if No (Cond) then
3246 Cond := Check;
3248 else
3249 Cond := Make_Or_Else (Loc,
3250 Left_Opnd => Cond,
3251 Right_Opnd => Check);
3252 end if;
3254 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3255 Apply_Compile_Time_Constraint_Error (Aggr,
3256 Msg => "incorrect value for discriminant&??",
3257 Reason => CE_Discriminant_Check_Failed,
3258 Ent => D);
3259 return False;
3260 end if;
3262 Next_Discriminant (D);
3263 Next_Elmt (Disc1);
3264 Next_Elmt (Disc2);
3265 end loop;
3267 -- If any discriminant constraint is non-static, emit a check
3269 if Present (Cond) then
3270 Insert_Action (N,
3271 Make_Raise_Constraint_Error (Loc,
3272 Condition => Cond,
3273 Reason => CE_Discriminant_Check_Failed));
3274 end if;
3276 return True;
3277 end Discriminants_Ok;
3279 -- Start of processing for Convert_Aggr_In_Object_Decl
3281 begin
3282 Set_Assignment_OK (Occ);
3284 if Nkind (Aggr) = N_Qualified_Expression then
3285 Aggr := Expression (Aggr);
3286 end if;
3288 if Has_Discriminants (Typ)
3289 and then Typ /= Etype (Obj)
3290 and then Is_Constrained (Etype (Obj))
3291 and then not Discriminants_Ok
3292 then
3293 return;
3294 end if;
3296 -- If the context is an extended return statement, it has its own
3297 -- finalization machinery (i.e. works like a transient scope) and
3298 -- we do not want to create an additional one, because objects on
3299 -- the finalization list of the return must be moved to the caller's
3300 -- finalization list to complete the return.
3302 -- However, if the aggregate is limited, it is built in place, and the
3303 -- controlled components are not assigned to intermediate temporaries
3304 -- so there is no need for a transient scope in this case either.
3306 if Requires_Transient_Scope (Typ)
3307 and then Ekind (Current_Scope) /= E_Return_Statement
3308 and then not Is_Limited_Type (Typ)
3309 then
3310 Establish_Transient_Scope
3311 (Aggr,
3312 Sec_Stack =>
3313 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3314 end if;
3316 declare
3317 Node_After : constant Node_Id := Next (N);
3318 begin
3319 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3320 Collect_Initialization_Statements (Obj, N, Node_After);
3321 end;
3322 Set_No_Initialization (N);
3323 Initialize_Discriminants (N, Typ);
3324 end Convert_Aggr_In_Object_Decl;
3326 -------------------------------------
3327 -- Convert_Array_Aggr_In_Allocator --
3328 -------------------------------------
3330 procedure Convert_Array_Aggr_In_Allocator
3331 (Decl : Node_Id;
3332 Aggr : Node_Id;
3333 Target : Node_Id)
3335 Aggr_Code : List_Id;
3336 Typ : constant Entity_Id := Etype (Aggr);
3337 Ctyp : constant Entity_Id := Component_Type (Typ);
3339 begin
3340 -- The target is an explicit dereference of the allocated object.
3341 -- Generate component assignments to it, as for an aggregate that
3342 -- appears on the right-hand side of an assignment statement.
3344 Aggr_Code :=
3345 Build_Array_Aggr_Code (Aggr,
3346 Ctype => Ctyp,
3347 Index => First_Index (Typ),
3348 Into => Target,
3349 Scalar_Comp => Is_Scalar_Type (Ctyp));
3351 Insert_Actions_After (Decl, Aggr_Code);
3352 end Convert_Array_Aggr_In_Allocator;
3354 ----------------------------
3355 -- Convert_To_Assignments --
3356 ----------------------------
3358 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3359 Loc : constant Source_Ptr := Sloc (N);
3360 T : Entity_Id;
3361 Temp : Entity_Id;
3363 Aggr_Code : List_Id;
3364 Instr : Node_Id;
3365 Target_Expr : Node_Id;
3366 Parent_Kind : Node_Kind;
3367 Unc_Decl : Boolean := False;
3368 Parent_Node : Node_Id;
3370 begin
3371 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3372 pragma Assert (Is_Record_Type (Typ));
3374 Parent_Node := Parent (N);
3375 Parent_Kind := Nkind (Parent_Node);
3377 if Parent_Kind = N_Qualified_Expression then
3379 -- Check if we are in a unconstrained declaration because in this
3380 -- case the current delayed expansion mechanism doesn't work when
3381 -- the declared object size depend on the initializing expr.
3383 begin
3384 Parent_Node := Parent (Parent_Node);
3385 Parent_Kind := Nkind (Parent_Node);
3387 if Parent_Kind = N_Object_Declaration then
3388 Unc_Decl :=
3389 not Is_Entity_Name (Object_Definition (Parent_Node))
3390 or else Has_Discriminants
3391 (Entity (Object_Definition (Parent_Node)))
3392 or else Is_Class_Wide_Type
3393 (Entity (Object_Definition (Parent_Node)));
3394 end if;
3395 end;
3396 end if;
3398 -- Just set the Delay flag in the cases where the transformation will be
3399 -- done top down from above.
3401 if False
3403 -- Internal aggregate (transformed when expanding the parent)
3405 or else Parent_Kind = N_Aggregate
3406 or else Parent_Kind = N_Extension_Aggregate
3407 or else Parent_Kind = N_Component_Association
3409 -- Allocator (see Convert_Aggr_In_Allocator)
3411 or else Parent_Kind = N_Allocator
3413 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3415 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3417 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3418 -- assignments in init procs are taken into account.
3420 or else (Parent_Kind = N_Assignment_Statement
3421 and then Inside_Init_Proc)
3423 -- (Ada 2005) An inherently limited type in a return statement, which
3424 -- will be handled in a build-in-place fashion, and may be rewritten
3425 -- as an extended return and have its own finalization machinery.
3426 -- In the case of a simple return, the aggregate needs to be delayed
3427 -- until the scope for the return statement has been created, so
3428 -- that any finalization chain will be associated with that scope.
3429 -- For extended returns, we delay expansion to avoid the creation
3430 -- of an unwanted transient scope that could result in premature
3431 -- finalization of the return object (which is built in place
3432 -- within the caller's scope).
3434 or else
3435 (Is_Limited_View (Typ)
3436 and then
3437 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3438 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3439 then
3440 Set_Expansion_Delayed (N);
3441 return;
3442 end if;
3444 -- Otherwise, if a transient scope is required, create it now. If we
3445 -- are within an initialization procedure do not create such, because
3446 -- the target of the assignment must not be declared within a local
3447 -- block, and because cleanup will take place on return from the
3448 -- initialization procedure.
3449 -- Should the condition be more restrictive ???
3451 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
3452 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
3453 end if;
3455 -- If the aggregate is non-limited, create a temporary. If it is limited
3456 -- and context is an assignment, this is a subaggregate for an enclosing
3457 -- aggregate being expanded. It must be built in place, so use target of
3458 -- the current assignment.
3460 if Is_Limited_Type (Typ)
3461 and then Nkind (Parent (N)) = N_Assignment_Statement
3462 then
3463 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3464 Insert_Actions (Parent (N),
3465 Build_Record_Aggr_Code (N, Typ, Target_Expr));
3466 Rewrite (Parent (N), Make_Null_Statement (Loc));
3468 else
3469 Temp := Make_Temporary (Loc, 'A', N);
3471 -- If the type inherits unknown discriminants, use the view with
3472 -- known discriminants if available.
3474 if Has_Unknown_Discriminants (Typ)
3475 and then Present (Underlying_Record_View (Typ))
3476 then
3477 T := Underlying_Record_View (Typ);
3478 else
3479 T := Typ;
3480 end if;
3482 Instr :=
3483 Make_Object_Declaration (Loc,
3484 Defining_Identifier => Temp,
3485 Object_Definition => New_Occurrence_Of (T, Loc));
3487 Set_No_Initialization (Instr);
3488 Insert_Action (N, Instr);
3489 Initialize_Discriminants (Instr, T);
3491 Target_Expr := New_Occurrence_Of (Temp, Loc);
3492 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
3494 -- Save the last assignment statement associated with the aggregate
3495 -- when building a controlled object. This reference is utilized by
3496 -- the finalization machinery when marking an object as successfully
3497 -- initialized.
3499 if Needs_Finalization (T) then
3500 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
3501 end if;
3503 Insert_Actions (N, Aggr_Code);
3504 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3505 Analyze_And_Resolve (N, T);
3506 end if;
3507 end Convert_To_Assignments;
3509 ---------------------------
3510 -- Convert_To_Positional --
3511 ---------------------------
3513 procedure Convert_To_Positional
3514 (N : Node_Id;
3515 Max_Others_Replicate : Nat := 5;
3516 Handle_Bit_Packed : Boolean := False)
3518 Typ : constant Entity_Id := Etype (N);
3520 Static_Components : Boolean := True;
3522 procedure Check_Static_Components;
3523 -- Check whether all components of the aggregate are compile-time known
3524 -- values, and can be passed as is to the back-end without further
3525 -- expansion.
3527 function Flatten
3528 (N : Node_Id;
3529 Ix : Node_Id;
3530 Ixb : Node_Id) return Boolean;
3531 -- Convert the aggregate into a purely positional form if possible. On
3532 -- entry the bounds of all dimensions are known to be static, and the
3533 -- total number of components is safe enough to expand.
3535 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3536 -- Return True iff the array N is flat (which is not trivial in the case
3537 -- of multidimensional aggregates).
3539 -----------------------------
3540 -- Check_Static_Components --
3541 -----------------------------
3543 -- Could use some comments in this body ???
3545 procedure Check_Static_Components is
3546 Expr : Node_Id;
3548 begin
3549 Static_Components := True;
3551 if Nkind (N) = N_String_Literal then
3552 null;
3554 elsif Present (Expressions (N)) then
3555 Expr := First (Expressions (N));
3556 while Present (Expr) loop
3557 if Nkind (Expr) /= N_Aggregate
3558 or else not Compile_Time_Known_Aggregate (Expr)
3559 or else Expansion_Delayed (Expr)
3560 then
3561 Static_Components := False;
3562 exit;
3563 end if;
3565 Next (Expr);
3566 end loop;
3567 end if;
3569 if Nkind (N) = N_Aggregate
3570 and then Present (Component_Associations (N))
3571 then
3572 Expr := First (Component_Associations (N));
3573 while Present (Expr) loop
3574 if Nkind_In (Expression (Expr), N_Integer_Literal,
3575 N_Real_Literal)
3576 then
3577 null;
3579 elsif Is_Entity_Name (Expression (Expr))
3580 and then Present (Entity (Expression (Expr)))
3581 and then Ekind (Entity (Expression (Expr))) =
3582 E_Enumeration_Literal
3583 then
3584 null;
3586 elsif Nkind (Expression (Expr)) /= N_Aggregate
3587 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3588 or else Expansion_Delayed (Expression (Expr))
3589 then
3590 Static_Components := False;
3591 exit;
3592 end if;
3594 Next (Expr);
3595 end loop;
3596 end if;
3597 end Check_Static_Components;
3599 -------------
3600 -- Flatten --
3601 -------------
3603 function Flatten
3604 (N : Node_Id;
3605 Ix : Node_Id;
3606 Ixb : Node_Id) return Boolean
3608 Loc : constant Source_Ptr := Sloc (N);
3609 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3610 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3611 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3612 Lov : Uint;
3613 Hiv : Uint;
3615 Others_Present : Boolean := False;
3617 begin
3618 if Nkind (Original_Node (N)) = N_String_Literal then
3619 return True;
3620 end if;
3622 if not Compile_Time_Known_Value (Lo)
3623 or else not Compile_Time_Known_Value (Hi)
3624 then
3625 return False;
3626 end if;
3628 Lov := Expr_Value (Lo);
3629 Hiv := Expr_Value (Hi);
3631 -- Check if there is an others choice
3633 if Present (Component_Associations (N)) then
3634 declare
3635 Assoc : Node_Id;
3636 Choice : Node_Id;
3638 begin
3639 Assoc := First (Component_Associations (N));
3640 while Present (Assoc) loop
3642 -- If this is a box association, flattening is in general
3643 -- not possible because at this point we cannot tell if the
3644 -- default is static or even exists.
3646 if Box_Present (Assoc) then
3647 return False;
3648 end if;
3650 Choice := First (Choices (Assoc));
3652 while Present (Choice) loop
3653 if Nkind (Choice) = N_Others_Choice then
3654 Others_Present := True;
3655 end if;
3657 Next (Choice);
3658 end loop;
3660 Next (Assoc);
3661 end loop;
3662 end;
3663 end if;
3665 -- If the low bound is not known at compile time and others is not
3666 -- present we can proceed since the bounds can be obtained from the
3667 -- aggregate.
3669 -- Note: This case is required in VM platforms since their backends
3670 -- normalize array indexes in the range 0 .. N-1. Hence, if we do
3671 -- not flat an array whose bounds cannot be obtained from the type
3672 -- of the index the backend has no way to properly generate the code.
3673 -- See ACATS c460010 for an example.
3675 if Hiv < Lov
3676 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
3677 then
3678 return False;
3679 end if;
3681 -- Determine if set of alternatives is suitable for conversion and
3682 -- build an array containing the values in sequence.
3684 declare
3685 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3686 of Node_Id := (others => Empty);
3687 -- The values in the aggregate sorted appropriately
3689 Vlist : List_Id;
3690 -- Same data as Vals in list form
3692 Rep_Count : Nat;
3693 -- Used to validate Max_Others_Replicate limit
3695 Elmt : Node_Id;
3696 Num : Int := UI_To_Int (Lov);
3697 Choice_Index : Int;
3698 Choice : Node_Id;
3699 Lo, Hi : Node_Id;
3701 begin
3702 if Present (Expressions (N)) then
3703 Elmt := First (Expressions (N));
3704 while Present (Elmt) loop
3705 if Nkind (Elmt) = N_Aggregate
3706 and then Present (Next_Index (Ix))
3707 and then
3708 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3709 then
3710 return False;
3711 end if;
3713 Vals (Num) := Relocate_Node (Elmt);
3714 Num := Num + 1;
3716 Next (Elmt);
3717 end loop;
3718 end if;
3720 if No (Component_Associations (N)) then
3721 return True;
3722 end if;
3724 Elmt := First (Component_Associations (N));
3726 if Nkind (Expression (Elmt)) = N_Aggregate then
3727 if Present (Next_Index (Ix))
3728 and then
3729 not Flatten
3730 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3731 then
3732 return False;
3733 end if;
3734 end if;
3736 Component_Loop : while Present (Elmt) loop
3737 Choice := First (Choices (Elmt));
3738 Choice_Loop : while Present (Choice) loop
3740 -- If we have an others choice, fill in the missing elements
3741 -- subject to the limit established by Max_Others_Replicate.
3743 if Nkind (Choice) = N_Others_Choice then
3744 Rep_Count := 0;
3746 for J in Vals'Range loop
3747 if No (Vals (J)) then
3748 Vals (J) := New_Copy_Tree (Expression (Elmt));
3749 Rep_Count := Rep_Count + 1;
3751 -- Check for maximum others replication. Note that
3752 -- we skip this test if either of the restrictions
3753 -- No_Elaboration_Code or No_Implicit_Loops is
3754 -- active, if this is a preelaborable unit or
3755 -- a predefined unit, or if the unit must be
3756 -- placed in data memory. This also ensures that
3757 -- predefined units get the same level of constant
3758 -- folding in Ada 95 and Ada 2005, where their
3759 -- categorization has changed.
3761 declare
3762 P : constant Entity_Id :=
3763 Cunit_Entity (Current_Sem_Unit);
3765 begin
3766 -- Check if duplication OK and if so continue
3767 -- processing.
3769 if Restriction_Active (No_Elaboration_Code)
3770 or else Restriction_Active (No_Implicit_Loops)
3771 or else
3772 (Ekind (Current_Scope) = E_Package
3773 and then Static_Elaboration_Desired
3774 (Current_Scope))
3775 or else Is_Preelaborated (P)
3776 or else (Ekind (P) = E_Package_Body
3777 and then
3778 Is_Preelaborated (Spec_Entity (P)))
3779 or else
3780 Is_Predefined_File_Name
3781 (Unit_File_Name (Get_Source_Unit (P)))
3782 then
3783 null;
3785 -- If duplication not OK, then we return False
3786 -- if the replication count is too high
3788 elsif Rep_Count > Max_Others_Replicate then
3789 return False;
3791 -- Continue on if duplication not OK, but the
3792 -- replication count is not excessive.
3794 else
3795 null;
3796 end if;
3797 end;
3798 end if;
3799 end loop;
3801 exit Component_Loop;
3803 -- Case of a subtype mark, identifier or expanded name
3805 elsif Is_Entity_Name (Choice)
3806 and then Is_Type (Entity (Choice))
3807 then
3808 Lo := Type_Low_Bound (Etype (Choice));
3809 Hi := Type_High_Bound (Etype (Choice));
3811 -- Case of subtype indication
3813 elsif Nkind (Choice) = N_Subtype_Indication then
3814 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3815 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3817 -- Case of a range
3819 elsif Nkind (Choice) = N_Range then
3820 Lo := Low_Bound (Choice);
3821 Hi := High_Bound (Choice);
3823 -- Normal subexpression case
3825 else pragma Assert (Nkind (Choice) in N_Subexpr);
3826 if not Compile_Time_Known_Value (Choice) then
3827 return False;
3829 else
3830 Choice_Index := UI_To_Int (Expr_Value (Choice));
3832 if Choice_Index in Vals'Range then
3833 Vals (Choice_Index) :=
3834 New_Copy_Tree (Expression (Elmt));
3835 goto Continue;
3837 -- Choice is statically out-of-range, will be
3838 -- rewritten to raise Constraint_Error.
3840 else
3841 return False;
3842 end if;
3843 end if;
3844 end if;
3846 -- Range cases merge with Lo,Hi set
3848 if not Compile_Time_Known_Value (Lo)
3849 or else
3850 not Compile_Time_Known_Value (Hi)
3851 then
3852 return False;
3854 else
3855 for J in UI_To_Int (Expr_Value (Lo)) ..
3856 UI_To_Int (Expr_Value (Hi))
3857 loop
3858 Vals (J) := New_Copy_Tree (Expression (Elmt));
3859 end loop;
3860 end if;
3862 <<Continue>>
3863 Next (Choice);
3864 end loop Choice_Loop;
3866 Next (Elmt);
3867 end loop Component_Loop;
3869 -- If we get here the conversion is possible
3871 Vlist := New_List;
3872 for J in Vals'Range loop
3873 Append (Vals (J), Vlist);
3874 end loop;
3876 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
3877 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
3878 return True;
3879 end;
3880 end Flatten;
3882 -------------
3883 -- Is_Flat --
3884 -------------
3886 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
3887 Elmt : Node_Id;
3889 begin
3890 if Dims = 0 then
3891 return True;
3893 elsif Nkind (N) = N_Aggregate then
3894 if Present (Component_Associations (N)) then
3895 return False;
3897 else
3898 Elmt := First (Expressions (N));
3899 while Present (Elmt) loop
3900 if not Is_Flat (Elmt, Dims - 1) then
3901 return False;
3902 end if;
3904 Next (Elmt);
3905 end loop;
3907 return True;
3908 end if;
3909 else
3910 return True;
3911 end if;
3912 end Is_Flat;
3914 -- Start of processing for Convert_To_Positional
3916 begin
3917 -- Ada 2005 (AI-287): Do not convert in case of default initialized
3918 -- components because in this case will need to call the corresponding
3919 -- IP procedure.
3921 if Has_Default_Init_Comps (N) then
3922 return;
3923 end if;
3925 if Is_Flat (N, Number_Dimensions (Typ)) then
3926 return;
3927 end if;
3929 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
3930 return;
3931 end if;
3933 -- Do not convert to positional if controlled components are involved
3934 -- since these require special processing
3936 if Has_Controlled_Component (Typ) then
3937 return;
3938 end if;
3940 Check_Static_Components;
3942 -- If the size is known, or all the components are static, try to
3943 -- build a fully positional aggregate.
3945 -- The size of the type may not be known for an aggregate with
3946 -- discriminated array components, but if the components are static
3947 -- it is still possible to verify statically that the length is
3948 -- compatible with the upper bound of the type, and therefore it is
3949 -- worth flattening such aggregates as well.
3951 -- For now the back-end expands these aggregates into individual
3952 -- assignments to the target anyway, but it is conceivable that
3953 -- it will eventually be able to treat such aggregates statically???
3955 if Aggr_Size_OK (N, Typ)
3956 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
3957 then
3958 if Static_Components then
3959 Set_Compile_Time_Known_Aggregate (N);
3960 Set_Expansion_Delayed (N, False);
3961 end if;
3963 Analyze_And_Resolve (N, Typ);
3964 end if;
3966 -- Is Static_Eaboration_Desired has been specified, diagnose aggregates
3967 -- that will still require initialization code.
3969 if (Ekind (Current_Scope) = E_Package
3970 and then Static_Elaboration_Desired (Current_Scope))
3971 and then Nkind (Parent (N)) = N_Object_Declaration
3972 then
3973 declare
3974 Expr : Node_Id;
3976 begin
3977 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
3978 Expr := First (Expressions (N));
3979 while Present (Expr) loop
3980 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
3981 or else
3982 (Is_Entity_Name (Expr)
3983 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
3984 then
3985 null;
3987 else
3988 Error_Msg_N
3989 ("non-static object requires elaboration code??", N);
3990 exit;
3991 end if;
3993 Next (Expr);
3994 end loop;
3996 if Present (Component_Associations (N)) then
3997 Error_Msg_N ("object requires elaboration code??", N);
3998 end if;
3999 end if;
4000 end;
4001 end if;
4002 end Convert_To_Positional;
4004 ----------------------------
4005 -- Expand_Array_Aggregate --
4006 ----------------------------
4008 -- Array aggregate expansion proceeds as follows:
4010 -- 1. If requested we generate code to perform all the array aggregate
4011 -- bound checks, specifically
4013 -- (a) Check that the index range defined by aggregate bounds is
4014 -- compatible with corresponding index subtype.
4016 -- (b) If an others choice is present check that no aggregate
4017 -- index is outside the bounds of the index constraint.
4019 -- (c) For multidimensional arrays make sure that all subaggregates
4020 -- corresponding to the same dimension have the same bounds.
4022 -- 2. Check for packed array aggregate which can be converted to a
4023 -- constant so that the aggregate disappears completely.
4025 -- 3. Check case of nested aggregate. Generally nested aggregates are
4026 -- handled during the processing of the parent aggregate.
4028 -- 4. Check if the aggregate can be statically processed. If this is the
4029 -- case pass it as is to Gigi. Note that a necessary condition for
4030 -- static processing is that the aggregate be fully positional.
4032 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4033 -- a temporary) then mark the aggregate as such and return. Otherwise
4034 -- create a new temporary and generate the appropriate initialization
4035 -- code.
4037 procedure Expand_Array_Aggregate (N : Node_Id) is
4038 Loc : constant Source_Ptr := Sloc (N);
4040 Typ : constant Entity_Id := Etype (N);
4041 Ctyp : constant Entity_Id := Component_Type (Typ);
4042 -- Typ is the correct constrained array subtype of the aggregate
4043 -- Ctyp is the corresponding component type.
4045 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4046 -- Number of aggregate index dimensions
4048 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4049 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4050 -- Low and High bounds of the constraint for each aggregate index
4052 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4053 -- The type of each index
4055 In_Place_Assign_OK_For_Declaration : Boolean := False;
4056 -- True if we are to generate an in place assignment for a declaration
4058 Maybe_In_Place_OK : Boolean;
4059 -- If the type is neither controlled nor packed and the aggregate
4060 -- is the expression in an assignment, assignment in place may be
4061 -- possible, provided other conditions are met on the LHS.
4063 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4064 (others => False);
4065 -- If Others_Present (J) is True, then there is an others choice
4066 -- in one of the sub-aggregates of N at dimension J.
4068 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4069 -- Returns true if an aggregate assignment can be done by the back end
4071 procedure Build_Constrained_Type (Positional : Boolean);
4072 -- If the subtype is not static or unconstrained, build a constrained
4073 -- type using the computable sizes of the aggregate and its sub-
4074 -- aggregates.
4076 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4077 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4078 -- by Index_Bounds.
4080 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4081 -- Checks that in a multi-dimensional array aggregate all subaggregates
4082 -- corresponding to the same dimension have the same bounds.
4083 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4084 -- corresponding to the sub-aggregate.
4086 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4087 -- Computes the values of array Others_Present. Sub_Aggr is the
4088 -- array sub-aggregate we start the computation from. Dim is the
4089 -- dimension corresponding to the sub-aggregate.
4091 function In_Place_Assign_OK return Boolean;
4092 -- Simple predicate to determine whether an aggregate assignment can
4093 -- be done in place, because none of the new values can depend on the
4094 -- components of the target of the assignment.
4096 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4097 -- Checks that if an others choice is present in any sub-aggregate no
4098 -- aggregate index is outside the bounds of the index constraint.
4099 -- Sub_Aggr is an array sub-aggregate. Dim is the dimension
4100 -- corresponding to the sub-aggregate.
4102 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4103 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4104 -- built directly into the target of the assignment it must be free
4105 -- of side-effects.
4107 ------------------------------------
4108 -- Aggr_Assignment_OK_For_Backend --
4109 ------------------------------------
4111 -- Backend processing by Gigi/gcc is possible only if all the following
4112 -- conditions are met:
4114 -- 1. N consists of a single OTHERS choice, possibly recursively
4116 -- 2. The array type is not packed
4118 -- 3. The array type has no atomic components
4120 -- 4. The array type has no null ranges (the purpose of this is to
4121 -- avoid a bogus warning for an out-of-range value).
4123 -- 5. The component type is discrete
4125 -- 6. The component size is Storage_Unit or the value is of the form
4126 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4127 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4128 -- the 8-bit value M, concatenated together.
4130 -- The ultimate goal is to generate a call to a fast memset routine
4131 -- specifically optimized for the target.
4133 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4134 Ctyp : Entity_Id;
4135 Index : Entity_Id;
4136 Expr : Node_Id := N;
4137 Low : Node_Id;
4138 High : Node_Id;
4139 Remainder : Uint;
4140 Value : Uint;
4141 Nunits : Nat;
4143 begin
4144 -- Recurse as far as possible to find the innermost component type
4146 Ctyp := Etype (N);
4147 while Is_Array_Type (Ctyp) loop
4148 if Nkind (Expr) /= N_Aggregate
4149 or else not Is_Others_Aggregate (Expr)
4150 then
4151 return False;
4152 end if;
4154 if Present (Packed_Array_Impl_Type (Ctyp)) then
4155 return False;
4156 end if;
4158 if Has_Atomic_Components (Ctyp) then
4159 return False;
4160 end if;
4162 Index := First_Index (Ctyp);
4163 while Present (Index) loop
4164 Get_Index_Bounds (Index, Low, High);
4166 if Is_Null_Range (Low, High) then
4167 return False;
4168 end if;
4170 Next_Index (Index);
4171 end loop;
4173 Expr := Expression (First (Component_Associations (Expr)));
4175 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4176 if Nkind (Expr) /= N_Aggregate
4177 or else not Is_Others_Aggregate (Expr)
4178 then
4179 return False;
4180 end if;
4182 Expr := Expression (First (Component_Associations (Expr)));
4183 end loop;
4185 Ctyp := Component_Type (Ctyp);
4187 if Is_Atomic_Or_VFA (Ctyp) then
4188 return False;
4189 end if;
4190 end loop;
4192 if not Is_Discrete_Type (Ctyp) then
4193 return False;
4194 end if;
4196 -- The expression needs to be analyzed if True is returned
4198 Analyze_And_Resolve (Expr, Ctyp);
4200 -- The back end uses the Esize as the precision of the type
4202 Nunits := UI_To_Int (Esize (Ctyp)) / System_Storage_Unit;
4204 if Nunits = 1 then
4205 return True;
4206 end if;
4208 if not Compile_Time_Known_Value (Expr) then
4209 return False;
4210 end if;
4212 Value := Expr_Value (Expr);
4214 if Has_Biased_Representation (Ctyp) then
4215 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
4216 end if;
4218 -- Values 0 and -1 immediately satisfy the last check
4220 if Value = Uint_0 or else Value = Uint_Minus_1 then
4221 return True;
4222 end if;
4224 -- We need to work with an unsigned value
4226 if Value < 0 then
4227 Value := Value + 2**(System_Storage_Unit * Nunits);
4228 end if;
4230 Remainder := Value rem 2**System_Storage_Unit;
4232 for J in 1 .. Nunits - 1 loop
4233 Value := Value / 2**System_Storage_Unit;
4235 if Value rem 2**System_Storage_Unit /= Remainder then
4236 return False;
4237 end if;
4238 end loop;
4240 return True;
4241 end Aggr_Assignment_OK_For_Backend;
4243 ----------------------------
4244 -- Build_Constrained_Type --
4245 ----------------------------
4247 procedure Build_Constrained_Type (Positional : Boolean) is
4248 Loc : constant Source_Ptr := Sloc (N);
4249 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4250 Comp : Node_Id;
4251 Decl : Node_Id;
4252 Typ : constant Entity_Id := Etype (N);
4253 Indexes : constant List_Id := New_List;
4254 Num : Int;
4255 Sub_Agg : Node_Id;
4257 begin
4258 -- If the aggregate is purely positional, all its subaggregates
4259 -- have the same size. We collect the dimensions from the first
4260 -- subaggregate at each level.
4262 if Positional then
4263 Sub_Agg := N;
4265 for D in 1 .. Number_Dimensions (Typ) loop
4266 Sub_Agg := First (Expressions (Sub_Agg));
4268 Comp := Sub_Agg;
4269 Num := 0;
4270 while Present (Comp) loop
4271 Num := Num + 1;
4272 Next (Comp);
4273 end loop;
4275 Append_To (Indexes,
4276 Make_Range (Loc,
4277 Low_Bound => Make_Integer_Literal (Loc, 1),
4278 High_Bound => Make_Integer_Literal (Loc, Num)));
4279 end loop;
4281 else
4282 -- We know the aggregate type is unconstrained and the aggregate
4283 -- is not processable by the back end, therefore not necessarily
4284 -- positional. Retrieve each dimension bounds (computed earlier).
4286 for D in 1 .. Number_Dimensions (Typ) loop
4287 Append_To (Indexes,
4288 Make_Range (Loc,
4289 Low_Bound => Aggr_Low (D),
4290 High_Bound => Aggr_High (D)));
4291 end loop;
4292 end if;
4294 Decl :=
4295 Make_Full_Type_Declaration (Loc,
4296 Defining_Identifier => Agg_Type,
4297 Type_Definition =>
4298 Make_Constrained_Array_Definition (Loc,
4299 Discrete_Subtype_Definitions => Indexes,
4300 Component_Definition =>
4301 Make_Component_Definition (Loc,
4302 Aliased_Present => False,
4303 Subtype_Indication =>
4304 New_Occurrence_Of (Component_Type (Typ), Loc))));
4306 Insert_Action (N, Decl);
4307 Analyze (Decl);
4308 Set_Etype (N, Agg_Type);
4309 Set_Is_Itype (Agg_Type);
4310 Freeze_Itype (Agg_Type, N);
4311 end Build_Constrained_Type;
4313 ------------------
4314 -- Check_Bounds --
4315 ------------------
4317 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4318 Aggr_Lo : Node_Id;
4319 Aggr_Hi : Node_Id;
4321 Ind_Lo : Node_Id;
4322 Ind_Hi : Node_Id;
4324 Cond : Node_Id := Empty;
4326 begin
4327 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4328 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4330 -- Generate the following test:
4332 -- [constraint_error when
4333 -- Aggr_Lo <= Aggr_Hi and then
4334 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4336 -- As an optimization try to see if some tests are trivially vacuous
4337 -- because we are comparing an expression against itself.
4339 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4340 Cond := Empty;
4342 elsif Aggr_Hi = Ind_Hi then
4343 Cond :=
4344 Make_Op_Lt (Loc,
4345 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4346 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4348 elsif Aggr_Lo = Ind_Lo then
4349 Cond :=
4350 Make_Op_Gt (Loc,
4351 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4352 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4354 else
4355 Cond :=
4356 Make_Or_Else (Loc,
4357 Left_Opnd =>
4358 Make_Op_Lt (Loc,
4359 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4360 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4362 Right_Opnd =>
4363 Make_Op_Gt (Loc,
4364 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4365 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4366 end if;
4368 if Present (Cond) then
4369 Cond :=
4370 Make_And_Then (Loc,
4371 Left_Opnd =>
4372 Make_Op_Le (Loc,
4373 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4374 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4376 Right_Opnd => Cond);
4378 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4379 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4380 Insert_Action (N,
4381 Make_Raise_Constraint_Error (Loc,
4382 Condition => Cond,
4383 Reason => CE_Range_Check_Failed));
4384 end if;
4385 end Check_Bounds;
4387 ----------------------------
4388 -- Check_Same_Aggr_Bounds --
4389 ----------------------------
4391 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4392 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4393 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4394 -- The bounds of this specific sub-aggregate
4396 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4397 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4398 -- The bounds of the aggregate for this dimension
4400 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4401 -- The index type for this dimension.xxx
4403 Cond : Node_Id := Empty;
4404 Assoc : Node_Id;
4405 Expr : Node_Id;
4407 begin
4408 -- If index checks are on generate the test
4410 -- [constraint_error when
4411 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4413 -- As an optimization try to see if some tests are trivially vacuos
4414 -- because we are comparing an expression against itself. Also for
4415 -- the first dimension the test is trivially vacuous because there
4416 -- is just one aggregate for dimension 1.
4418 if Index_Checks_Suppressed (Ind_Typ) then
4419 Cond := Empty;
4421 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4422 then
4423 Cond := Empty;
4425 elsif Aggr_Hi = Sub_Hi then
4426 Cond :=
4427 Make_Op_Ne (Loc,
4428 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4429 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4431 elsif Aggr_Lo = Sub_Lo then
4432 Cond :=
4433 Make_Op_Ne (Loc,
4434 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4435 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4437 else
4438 Cond :=
4439 Make_Or_Else (Loc,
4440 Left_Opnd =>
4441 Make_Op_Ne (Loc,
4442 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4443 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4445 Right_Opnd =>
4446 Make_Op_Ne (Loc,
4447 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4448 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4449 end if;
4451 if Present (Cond) then
4452 Insert_Action (N,
4453 Make_Raise_Constraint_Error (Loc,
4454 Condition => Cond,
4455 Reason => CE_Length_Check_Failed));
4456 end if;
4458 -- Now look inside the sub-aggregate to see if there is more work
4460 if Dim < Aggr_Dimension then
4462 -- Process positional components
4464 if Present (Expressions (Sub_Aggr)) then
4465 Expr := First (Expressions (Sub_Aggr));
4466 while Present (Expr) loop
4467 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4468 Next (Expr);
4469 end loop;
4470 end if;
4472 -- Process component associations
4474 if Present (Component_Associations (Sub_Aggr)) then
4475 Assoc := First (Component_Associations (Sub_Aggr));
4476 while Present (Assoc) loop
4477 Expr := Expression (Assoc);
4478 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4479 Next (Assoc);
4480 end loop;
4481 end if;
4482 end if;
4483 end Check_Same_Aggr_Bounds;
4485 ----------------------------
4486 -- Compute_Others_Present --
4487 ----------------------------
4489 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4490 Assoc : Node_Id;
4491 Expr : Node_Id;
4493 begin
4494 if Present (Component_Associations (Sub_Aggr)) then
4495 Assoc := Last (Component_Associations (Sub_Aggr));
4497 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4498 Others_Present (Dim) := True;
4499 end if;
4500 end if;
4502 -- Now look inside the sub-aggregate to see if there is more work
4504 if Dim < Aggr_Dimension then
4506 -- Process positional components
4508 if Present (Expressions (Sub_Aggr)) then
4509 Expr := First (Expressions (Sub_Aggr));
4510 while Present (Expr) loop
4511 Compute_Others_Present (Expr, Dim + 1);
4512 Next (Expr);
4513 end loop;
4514 end if;
4516 -- Process component associations
4518 if Present (Component_Associations (Sub_Aggr)) then
4519 Assoc := First (Component_Associations (Sub_Aggr));
4520 while Present (Assoc) loop
4521 Expr := Expression (Assoc);
4522 Compute_Others_Present (Expr, Dim + 1);
4523 Next (Assoc);
4524 end loop;
4525 end if;
4526 end if;
4527 end Compute_Others_Present;
4529 ------------------------
4530 -- In_Place_Assign_OK --
4531 ------------------------
4533 function In_Place_Assign_OK return Boolean is
4534 Aggr_In : Node_Id;
4535 Aggr_Lo : Node_Id;
4536 Aggr_Hi : Node_Id;
4537 Obj_In : Node_Id;
4538 Obj_Lo : Node_Id;
4539 Obj_Hi : Node_Id;
4541 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4542 -- Check recursively that each component of a (sub)aggregate does
4543 -- not depend on the variable being assigned to.
4545 function Safe_Component (Expr : Node_Id) return Boolean;
4546 -- Verify that an expression cannot depend on the variable being
4547 -- assigned to. Room for improvement here (but less than before).
4549 --------------------
4550 -- Safe_Aggregate --
4551 --------------------
4553 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4554 Expr : Node_Id;
4556 begin
4557 if Present (Expressions (Aggr)) then
4558 Expr := First (Expressions (Aggr));
4559 while Present (Expr) loop
4560 if Nkind (Expr) = N_Aggregate then
4561 if not Safe_Aggregate (Expr) then
4562 return False;
4563 end if;
4565 elsif not Safe_Component (Expr) then
4566 return False;
4567 end if;
4569 Next (Expr);
4570 end loop;
4571 end if;
4573 if Present (Component_Associations (Aggr)) then
4574 Expr := First (Component_Associations (Aggr));
4575 while Present (Expr) loop
4576 if Nkind (Expression (Expr)) = N_Aggregate then
4577 if not Safe_Aggregate (Expression (Expr)) then
4578 return False;
4579 end if;
4581 -- If association has a box, no way to determine yet
4582 -- whether default can be assigned in place.
4584 elsif Box_Present (Expr) then
4585 return False;
4587 elsif not Safe_Component (Expression (Expr)) then
4588 return False;
4589 end if;
4591 Next (Expr);
4592 end loop;
4593 end if;
4595 return True;
4596 end Safe_Aggregate;
4598 --------------------
4599 -- Safe_Component --
4600 --------------------
4602 function Safe_Component (Expr : Node_Id) return Boolean is
4603 Comp : Node_Id := Expr;
4605 function Check_Component (Comp : Node_Id) return Boolean;
4606 -- Do the recursive traversal, after copy
4608 ---------------------
4609 -- Check_Component --
4610 ---------------------
4612 function Check_Component (Comp : Node_Id) return Boolean is
4613 begin
4614 if Is_Overloaded (Comp) then
4615 return False;
4616 end if;
4618 return Compile_Time_Known_Value (Comp)
4620 or else (Is_Entity_Name (Comp)
4621 and then Present (Entity (Comp))
4622 and then No (Renamed_Object (Entity (Comp))))
4624 or else (Nkind (Comp) = N_Attribute_Reference
4625 and then Check_Component (Prefix (Comp)))
4627 or else (Nkind (Comp) in N_Binary_Op
4628 and then Check_Component (Left_Opnd (Comp))
4629 and then Check_Component (Right_Opnd (Comp)))
4631 or else (Nkind (Comp) in N_Unary_Op
4632 and then Check_Component (Right_Opnd (Comp)))
4634 or else (Nkind (Comp) = N_Selected_Component
4635 and then Check_Component (Prefix (Comp)))
4637 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4638 and then Check_Component (Expression (Comp)));
4639 end Check_Component;
4641 -- Start of processing for Safe_Component
4643 begin
4644 -- If the component appears in an association that may correspond
4645 -- to more than one element, it is not analyzed before expansion
4646 -- into assignments, to avoid side effects. We analyze, but do not
4647 -- resolve the copy, to obtain sufficient entity information for
4648 -- the checks that follow. If component is overloaded we assume
4649 -- an unsafe function call.
4651 if not Analyzed (Comp) then
4652 if Is_Overloaded (Expr) then
4653 return False;
4655 elsif Nkind (Expr) = N_Aggregate
4656 and then not Is_Others_Aggregate (Expr)
4657 then
4658 return False;
4660 elsif Nkind (Expr) = N_Allocator then
4662 -- For now, too complex to analyze
4664 return False;
4665 end if;
4667 Comp := New_Copy_Tree (Expr);
4668 Set_Parent (Comp, Parent (Expr));
4669 Analyze (Comp);
4670 end if;
4672 if Nkind (Comp) = N_Aggregate then
4673 return Safe_Aggregate (Comp);
4674 else
4675 return Check_Component (Comp);
4676 end if;
4677 end Safe_Component;
4679 -- Start of processing for In_Place_Assign_OK
4681 begin
4682 if Present (Component_Associations (N)) then
4684 -- On assignment, sliding can take place, so we cannot do the
4685 -- assignment in place unless the bounds of the aggregate are
4686 -- statically equal to those of the target.
4688 -- If the aggregate is given by an others choice, the bounds are
4689 -- derived from the left-hand side, and the assignment is safe if
4690 -- the expression is.
4692 if Is_Others_Aggregate (N) then
4693 return
4694 Safe_Component
4695 (Expression (First (Component_Associations (N))));
4696 end if;
4698 Aggr_In := First_Index (Etype (N));
4700 if Nkind (Parent (N)) = N_Assignment_Statement then
4701 Obj_In := First_Index (Etype (Name (Parent (N))));
4703 else
4704 -- Context is an allocator. Check bounds of aggregate against
4705 -- given type in qualified expression.
4707 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4708 Obj_In :=
4709 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4710 end if;
4712 while Present (Aggr_In) loop
4713 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4714 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4716 if not Compile_Time_Known_Value (Aggr_Lo)
4717 or else not Compile_Time_Known_Value (Aggr_Hi)
4718 or else not Compile_Time_Known_Value (Obj_Lo)
4719 or else not Compile_Time_Known_Value (Obj_Hi)
4720 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4721 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4722 then
4723 return False;
4724 end if;
4726 Next_Index (Aggr_In);
4727 Next_Index (Obj_In);
4728 end loop;
4729 end if;
4731 -- Now check the component values themselves
4733 return Safe_Aggregate (N);
4734 end In_Place_Assign_OK;
4736 ------------------
4737 -- Others_Check --
4738 ------------------
4740 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4741 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4742 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4743 -- The bounds of the aggregate for this dimension
4745 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4746 -- The index type for this dimension
4748 Need_To_Check : Boolean := False;
4750 Choices_Lo : Node_Id := Empty;
4751 Choices_Hi : Node_Id := Empty;
4752 -- The lowest and highest discrete choices for a named sub-aggregate
4754 Nb_Choices : Int := -1;
4755 -- The number of discrete non-others choices in this sub-aggregate
4757 Nb_Elements : Uint := Uint_0;
4758 -- The number of elements in a positional aggregate
4760 Cond : Node_Id := Empty;
4762 Assoc : Node_Id;
4763 Choice : Node_Id;
4764 Expr : Node_Id;
4766 begin
4767 -- Check if we have an others choice. If we do make sure that this
4768 -- sub-aggregate contains at least one element in addition to the
4769 -- others choice.
4771 if Range_Checks_Suppressed (Ind_Typ) then
4772 Need_To_Check := False;
4774 elsif Present (Expressions (Sub_Aggr))
4775 and then Present (Component_Associations (Sub_Aggr))
4776 then
4777 Need_To_Check := True;
4779 elsif Present (Component_Associations (Sub_Aggr)) then
4780 Assoc := Last (Component_Associations (Sub_Aggr));
4782 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4783 Need_To_Check := False;
4785 else
4786 -- Count the number of discrete choices. Start with -1 because
4787 -- the others choice does not count.
4789 -- Is there some reason we do not use List_Length here ???
4791 Nb_Choices := -1;
4792 Assoc := First (Component_Associations (Sub_Aggr));
4793 while Present (Assoc) loop
4794 Choice := First (Choices (Assoc));
4795 while Present (Choice) loop
4796 Nb_Choices := Nb_Choices + 1;
4797 Next (Choice);
4798 end loop;
4800 Next (Assoc);
4801 end loop;
4803 -- If there is only an others choice nothing to do
4805 Need_To_Check := (Nb_Choices > 0);
4806 end if;
4808 else
4809 Need_To_Check := False;
4810 end if;
4812 -- If we are dealing with a positional sub-aggregate with an others
4813 -- choice then compute the number or positional elements.
4815 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4816 Expr := First (Expressions (Sub_Aggr));
4817 Nb_Elements := Uint_0;
4818 while Present (Expr) loop
4819 Nb_Elements := Nb_Elements + 1;
4820 Next (Expr);
4821 end loop;
4823 -- If the aggregate contains discrete choices and an others choice
4824 -- compute the smallest and largest discrete choice values.
4826 elsif Need_To_Check then
4827 Compute_Choices_Lo_And_Choices_Hi : declare
4829 Table : Case_Table_Type (1 .. Nb_Choices);
4830 -- Used to sort all the different choice values
4832 J : Pos := 1;
4833 Low : Node_Id;
4834 High : Node_Id;
4836 begin
4837 Assoc := First (Component_Associations (Sub_Aggr));
4838 while Present (Assoc) loop
4839 Choice := First (Choices (Assoc));
4840 while Present (Choice) loop
4841 if Nkind (Choice) = N_Others_Choice then
4842 exit;
4843 end if;
4845 Get_Index_Bounds (Choice, Low, High);
4846 Table (J).Choice_Lo := Low;
4847 Table (J).Choice_Hi := High;
4849 J := J + 1;
4850 Next (Choice);
4851 end loop;
4853 Next (Assoc);
4854 end loop;
4856 -- Sort the discrete choices
4858 Sort_Case_Table (Table);
4860 Choices_Lo := Table (1).Choice_Lo;
4861 Choices_Hi := Table (Nb_Choices).Choice_Hi;
4862 end Compute_Choices_Lo_And_Choices_Hi;
4863 end if;
4865 -- If no others choice in this sub-aggregate, or the aggregate
4866 -- comprises only an others choice, nothing to do.
4868 if not Need_To_Check then
4869 Cond := Empty;
4871 -- If we are dealing with an aggregate containing an others choice
4872 -- and positional components, we generate the following test:
4874 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
4875 -- Ind_Typ'Pos (Aggr_Hi)
4876 -- then
4877 -- raise Constraint_Error;
4878 -- end if;
4880 elsif Nb_Elements > Uint_0 then
4881 Cond :=
4882 Make_Op_Gt (Loc,
4883 Left_Opnd =>
4884 Make_Op_Add (Loc,
4885 Left_Opnd =>
4886 Make_Attribute_Reference (Loc,
4887 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
4888 Attribute_Name => Name_Pos,
4889 Expressions =>
4890 New_List
4891 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
4892 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
4894 Right_Opnd =>
4895 Make_Attribute_Reference (Loc,
4896 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
4897 Attribute_Name => Name_Pos,
4898 Expressions => New_List (
4899 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
4901 -- If we are dealing with an aggregate containing an others choice
4902 -- and discrete choices we generate the following test:
4904 -- [constraint_error when
4905 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
4907 else
4908 Cond :=
4909 Make_Or_Else (Loc,
4910 Left_Opnd =>
4911 Make_Op_Lt (Loc,
4912 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
4913 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
4915 Right_Opnd =>
4916 Make_Op_Gt (Loc,
4917 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
4918 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
4919 end if;
4921 if Present (Cond) then
4922 Insert_Action (N,
4923 Make_Raise_Constraint_Error (Loc,
4924 Condition => Cond,
4925 Reason => CE_Length_Check_Failed));
4926 -- Questionable reason code, shouldn't that be a
4927 -- CE_Range_Check_Failed ???
4928 end if;
4930 -- Now look inside the sub-aggregate to see if there is more work
4932 if Dim < Aggr_Dimension then
4934 -- Process positional components
4936 if Present (Expressions (Sub_Aggr)) then
4937 Expr := First (Expressions (Sub_Aggr));
4938 while Present (Expr) loop
4939 Others_Check (Expr, Dim + 1);
4940 Next (Expr);
4941 end loop;
4942 end if;
4944 -- Process component associations
4946 if Present (Component_Associations (Sub_Aggr)) then
4947 Assoc := First (Component_Associations (Sub_Aggr));
4948 while Present (Assoc) loop
4949 Expr := Expression (Assoc);
4950 Others_Check (Expr, Dim + 1);
4951 Next (Assoc);
4952 end loop;
4953 end if;
4954 end if;
4955 end Others_Check;
4957 -------------------------
4958 -- Safe_Left_Hand_Side --
4959 -------------------------
4961 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
4962 function Is_Safe_Index (Indx : Node_Id) return Boolean;
4963 -- If the left-hand side includes an indexed component, check that
4964 -- the indexes are free of side-effect.
4966 -------------------
4967 -- Is_Safe_Index --
4968 -------------------
4970 function Is_Safe_Index (Indx : Node_Id) return Boolean is
4971 begin
4972 if Is_Entity_Name (Indx) then
4973 return True;
4975 elsif Nkind (Indx) = N_Integer_Literal then
4976 return True;
4978 elsif Nkind (Indx) = N_Function_Call
4979 and then Is_Entity_Name (Name (Indx))
4980 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
4981 then
4982 return True;
4984 elsif Nkind (Indx) = N_Type_Conversion
4985 and then Is_Safe_Index (Expression (Indx))
4986 then
4987 return True;
4989 else
4990 return False;
4991 end if;
4992 end Is_Safe_Index;
4994 -- Start of processing for Safe_Left_Hand_Side
4996 begin
4997 if Is_Entity_Name (N) then
4998 return True;
5000 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5001 and then Safe_Left_Hand_Side (Prefix (N))
5002 then
5003 return True;
5005 elsif Nkind (N) = N_Indexed_Component
5006 and then Safe_Left_Hand_Side (Prefix (N))
5007 and then Is_Safe_Index (First (Expressions (N)))
5008 then
5009 return True;
5011 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5012 return Safe_Left_Hand_Side (Expression (N));
5014 else
5015 return False;
5016 end if;
5017 end Safe_Left_Hand_Side;
5019 -- Local variables
5021 Tmp : Entity_Id;
5022 -- Holds the temporary aggregate value
5024 Tmp_Decl : Node_Id;
5025 -- Holds the declaration of Tmp
5027 Aggr_Code : List_Id;
5028 Parent_Node : Node_Id;
5029 Parent_Kind : Node_Kind;
5031 -- Start of processing for Expand_Array_Aggregate
5033 begin
5034 -- Do not touch the special aggregates of attributes used for Asm calls
5036 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5037 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5038 then
5039 return;
5041 -- Do not expand an aggregate for an array type which contains tasks if
5042 -- the aggregate is associated with an unexpanded return statement of a
5043 -- build-in-place function. The aggregate is expanded when the related
5044 -- return statement (rewritten into an extended return) is processed.
5045 -- This delay ensures that any temporaries and initialization code
5046 -- generated for the aggregate appear in the proper return block and
5047 -- use the correct _chain and _master.
5049 elsif Has_Task (Base_Type (Etype (N)))
5050 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5051 and then Is_Build_In_Place_Function
5052 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5053 then
5054 return;
5056 -- Do not attempt expansion if error already detected. We may reach this
5057 -- point in spite of previous errors when compiling with -gnatq, to
5058 -- force all possible errors (this is the usual ACATS mode).
5060 elsif Error_Posted (N) then
5061 return;
5062 end if;
5064 -- If the semantic analyzer has determined that aggregate N will raise
5065 -- Constraint_Error at run time, then the aggregate node has been
5066 -- replaced with an N_Raise_Constraint_Error node and we should
5067 -- never get here.
5069 pragma Assert (not Raises_Constraint_Error (N));
5071 -- STEP 1a
5073 -- Check that the index range defined by aggregate bounds is
5074 -- compatible with corresponding index subtype.
5076 Index_Compatibility_Check : declare
5077 Aggr_Index_Range : Node_Id := First_Index (Typ);
5078 -- The current aggregate index range
5080 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5081 -- The corresponding index constraint against which we have to
5082 -- check the above aggregate index range.
5084 begin
5085 Compute_Others_Present (N, 1);
5087 for J in 1 .. Aggr_Dimension loop
5088 -- There is no need to emit a check if an others choice is present
5089 -- for this array aggregate dimension since in this case one of
5090 -- N's sub-aggregates has taken its bounds from the context and
5091 -- these bounds must have been checked already. In addition all
5092 -- sub-aggregates corresponding to the same dimension must all
5093 -- have the same bounds (checked in (c) below).
5095 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5096 and then not Others_Present (J)
5097 then
5098 -- We don't use Checks.Apply_Range_Check here because it emits
5099 -- a spurious check. Namely it checks that the range defined by
5100 -- the aggregate bounds is non empty. But we know this already
5101 -- if we get here.
5103 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5104 end if;
5106 -- Save the low and high bounds of the aggregate index as well as
5107 -- the index type for later use in checks (b) and (c) below.
5109 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5110 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5112 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5114 Next_Index (Aggr_Index_Range);
5115 Next_Index (Index_Constraint);
5116 end loop;
5117 end Index_Compatibility_Check;
5119 -- STEP 1b
5121 -- If an others choice is present check that no aggregate index is
5122 -- outside the bounds of the index constraint.
5124 Others_Check (N, 1);
5126 -- STEP 1c
5128 -- For multidimensional arrays make sure that all subaggregates
5129 -- corresponding to the same dimension have the same bounds.
5131 if Aggr_Dimension > 1 then
5132 Check_Same_Aggr_Bounds (N, 1);
5133 end if;
5135 -- STEP 1d
5137 -- If we have a default component value, or simple initialization is
5138 -- required for the component type, then we replace <> in component
5139 -- associations by the required default value.
5141 declare
5142 Default_Val : Node_Id;
5143 Assoc : Node_Id;
5145 begin
5146 if (Present (Default_Aspect_Component_Value (Typ))
5147 or else Needs_Simple_Initialization (Ctyp))
5148 and then Present (Component_Associations (N))
5149 then
5150 Assoc := First (Component_Associations (N));
5151 while Present (Assoc) loop
5152 if Nkind (Assoc) = N_Component_Association
5153 and then Box_Present (Assoc)
5154 then
5155 Set_Box_Present (Assoc, False);
5157 if Present (Default_Aspect_Component_Value (Typ)) then
5158 Default_Val := Default_Aspect_Component_Value (Typ);
5159 else
5160 Default_Val := Get_Simple_Init_Val (Ctyp, N);
5161 end if;
5163 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
5164 Analyze_And_Resolve (Expression (Assoc), Ctyp);
5165 end if;
5167 Next (Assoc);
5168 end loop;
5169 end if;
5170 end;
5172 -- STEP 2
5174 -- Here we test for is packed array aggregate that we can handle at
5175 -- compile time. If so, return with transformation done. Note that we do
5176 -- this even if the aggregate is nested, because once we have done this
5177 -- processing, there is no more nested aggregate.
5179 if Packed_Array_Aggregate_Handled (N) then
5180 return;
5181 end if;
5183 -- At this point we try to convert to positional form
5185 if Ekind (Current_Scope) = E_Package
5186 and then Static_Elaboration_Desired (Current_Scope)
5187 then
5188 Convert_To_Positional (N, Max_Others_Replicate => 100);
5189 else
5190 Convert_To_Positional (N);
5191 end if;
5193 -- if the result is no longer an aggregate (e.g. it may be a string
5194 -- literal, or a temporary which has the needed value), then we are
5195 -- done, since there is no longer a nested aggregate.
5197 if Nkind (N) /= N_Aggregate then
5198 return;
5200 -- We are also done if the result is an analyzed aggregate, indicating
5201 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5202 -- aggregate.
5204 elsif Analyzed (N) and then N /= Original_Node (N) then
5205 return;
5206 end if;
5208 -- If all aggregate components are compile-time known and the aggregate
5209 -- has been flattened, nothing left to do. The same occurs if the
5210 -- aggregate is used to initialize the components of a statically
5211 -- allocated dispatch table.
5213 if Compile_Time_Known_Aggregate (N)
5214 or else Is_Static_Dispatch_Table_Aggregate (N)
5215 then
5216 Set_Expansion_Delayed (N, False);
5217 return;
5218 end if;
5220 -- Now see if back end processing is possible
5222 if Backend_Processing_Possible (N) then
5224 -- If the aggregate is static but the constraints are not, build
5225 -- a static subtype for the aggregate, so that Gigi can place it
5226 -- in static memory. Perform an unchecked_conversion to the non-
5227 -- static type imposed by the context.
5229 declare
5230 Itype : constant Entity_Id := Etype (N);
5231 Index : Node_Id;
5232 Needs_Type : Boolean := False;
5234 begin
5235 Index := First_Index (Itype);
5236 while Present (Index) loop
5237 if not Is_OK_Static_Subtype (Etype (Index)) then
5238 Needs_Type := True;
5239 exit;
5240 else
5241 Next_Index (Index);
5242 end if;
5243 end loop;
5245 if Needs_Type then
5246 Build_Constrained_Type (Positional => True);
5247 Rewrite (N, Unchecked_Convert_To (Itype, N));
5248 Analyze (N);
5249 end if;
5250 end;
5252 return;
5253 end if;
5255 -- STEP 3
5257 -- Delay expansion for nested aggregates: it will be taken care of
5258 -- when the parent aggregate is expanded.
5260 Parent_Node := Parent (N);
5261 Parent_Kind := Nkind (Parent_Node);
5263 if Parent_Kind = N_Qualified_Expression then
5264 Parent_Node := Parent (Parent_Node);
5265 Parent_Kind := Nkind (Parent_Node);
5266 end if;
5268 if Parent_Kind = N_Aggregate
5269 or else Parent_Kind = N_Extension_Aggregate
5270 or else Parent_Kind = N_Component_Association
5271 or else (Parent_Kind = N_Object_Declaration
5272 and then Needs_Finalization (Typ))
5273 or else (Parent_Kind = N_Assignment_Statement
5274 and then Inside_Init_Proc)
5275 then
5276 if Static_Array_Aggregate (N)
5277 or else Compile_Time_Known_Aggregate (N)
5278 then
5279 Set_Expansion_Delayed (N, False);
5280 return;
5281 else
5282 Set_Expansion_Delayed (N);
5283 return;
5284 end if;
5285 end if;
5287 -- STEP 4
5289 -- Look if in place aggregate expansion is possible
5291 -- For object declarations we build the aggregate in place, unless
5292 -- the array is bit-packed or the component is controlled.
5294 -- For assignments we do the assignment in place if all the component
5295 -- associations have compile-time known values. For other cases we
5296 -- create a temporary. The analysis for safety of on-line assignment
5297 -- is delicate, i.e. we don't know how to do it fully yet ???
5299 -- For allocators we assign to the designated object in place if the
5300 -- aggregate meets the same conditions as other in-place assignments.
5301 -- In this case the aggregate may not come from source but was created
5302 -- for default initialization, e.g. with Initialize_Scalars.
5304 if Requires_Transient_Scope (Typ) then
5305 Establish_Transient_Scope
5306 (N, Sec_Stack => Has_Controlled_Component (Typ));
5307 end if;
5309 if Has_Default_Init_Comps (N) then
5310 Maybe_In_Place_OK := False;
5312 elsif Is_Bit_Packed_Array (Typ)
5313 or else Has_Controlled_Component (Typ)
5314 then
5315 Maybe_In_Place_OK := False;
5317 else
5318 Maybe_In_Place_OK :=
5319 (Nkind (Parent (N)) = N_Assignment_Statement
5320 and then In_Place_Assign_OK)
5322 or else
5323 (Nkind (Parent (Parent (N))) = N_Allocator
5324 and then In_Place_Assign_OK);
5325 end if;
5327 -- If this is an array of tasks, it will be expanded into build-in-place
5328 -- assignments. Build an activation chain for the tasks now.
5330 if Has_Task (Etype (N)) then
5331 Build_Activation_Chain_Entity (N);
5332 end if;
5334 -- Perform in-place expansion of aggregate in an object declaration.
5335 -- Note: actions generated for the aggregate will be captured in an
5336 -- expression-with-actions statement so that they can be transferred
5337 -- to freeze actions later if there is an address clause for the
5338 -- object. (Note: we don't use a block statement because this would
5339 -- cause generated freeze nodes to be elaborated in the wrong scope).
5341 -- Should document these individual tests ???
5343 if not Has_Default_Init_Comps (N)
5344 and then Comes_From_Source (Parent_Node)
5345 and then Parent_Kind = N_Object_Declaration
5346 and then not
5347 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
5348 and then N = Expression (Parent_Node)
5349 and then not Is_Bit_Packed_Array (Typ)
5350 and then not Has_Controlled_Component (Typ)
5351 then
5352 In_Place_Assign_OK_For_Declaration := True;
5353 Tmp := Defining_Identifier (Parent (N));
5354 Set_No_Initialization (Parent (N));
5355 Set_Expression (Parent (N), Empty);
5357 -- Set kind and type of the entity, for use in the analysis
5358 -- of the subsequent assignments. If the nominal type is not
5359 -- constrained, build a subtype from the known bounds of the
5360 -- aggregate. If the declaration has a subtype mark, use it,
5361 -- otherwise use the itype of the aggregate.
5363 Set_Ekind (Tmp, E_Variable);
5365 if not Is_Constrained (Typ) then
5366 Build_Constrained_Type (Positional => False);
5368 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5369 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5370 then
5371 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5373 else
5374 Set_Size_Known_At_Compile_Time (Typ, False);
5375 Set_Etype (Tmp, Typ);
5376 end if;
5378 elsif Maybe_In_Place_OK
5379 and then Nkind (Parent (N)) = N_Qualified_Expression
5380 and then Nkind (Parent (Parent (N))) = N_Allocator
5381 then
5382 Set_Expansion_Delayed (N);
5383 return;
5385 -- In the remaining cases the aggregate is the RHS of an assignment
5387 elsif Maybe_In_Place_OK
5388 and then Safe_Left_Hand_Side (Name (Parent (N)))
5389 then
5390 Tmp := Name (Parent (N));
5392 if Etype (Tmp) /= Etype (N) then
5393 Apply_Length_Check (N, Etype (Tmp));
5395 if Nkind (N) = N_Raise_Constraint_Error then
5397 -- Static error, nothing further to expand
5399 return;
5400 end if;
5401 end if;
5403 -- If a slice assignment has an aggregate with a single others_choice,
5404 -- the assignment can be done in place even if bounds are not static,
5405 -- by converting it into a loop over the discrete range of the slice.
5407 elsif Maybe_In_Place_OK
5408 and then Nkind (Name (Parent (N))) = N_Slice
5409 and then Is_Others_Aggregate (N)
5410 then
5411 Tmp := Name (Parent (N));
5413 -- Set type of aggregate to be type of lhs in assignment, in order
5414 -- to suppress redundant length checks.
5416 Set_Etype (N, Etype (Tmp));
5418 -- Step 5
5420 -- In place aggregate expansion is not possible
5422 else
5423 Maybe_In_Place_OK := False;
5424 Tmp := Make_Temporary (Loc, 'A', N);
5425 Tmp_Decl :=
5426 Make_Object_Declaration (Loc,
5427 Defining_Identifier => Tmp,
5428 Object_Definition => New_Occurrence_Of (Typ, Loc));
5429 Set_No_Initialization (Tmp_Decl, True);
5431 -- If we are within a loop, the temporary will be pushed on the
5432 -- stack at each iteration. If the aggregate is the expression for an
5433 -- allocator, it will be immediately copied to the heap and can
5434 -- be reclaimed at once. We create a transient scope around the
5435 -- aggregate for this purpose.
5437 if Ekind (Current_Scope) = E_Loop
5438 and then Nkind (Parent (Parent (N))) = N_Allocator
5439 then
5440 Establish_Transient_Scope (N, False);
5441 end if;
5443 Insert_Action (N, Tmp_Decl);
5444 end if;
5446 -- Construct and insert the aggregate code. We can safely suppress index
5447 -- checks because this code is guaranteed not to raise CE on index
5448 -- checks. However we should *not* suppress all checks.
5450 declare
5451 Target : Node_Id;
5453 begin
5454 if Nkind (Tmp) = N_Defining_Identifier then
5455 Target := New_Occurrence_Of (Tmp, Loc);
5457 else
5458 if Has_Default_Init_Comps (N) then
5460 -- Ada 2005 (AI-287): This case has not been analyzed???
5462 raise Program_Error;
5463 end if;
5465 -- Name in assignment is explicit dereference
5467 Target := New_Copy (Tmp);
5468 end if;
5470 -- If we are to generate an in place assignment for a declaration or
5471 -- an assignment statement, and the assignment can be done directly
5472 -- by the back end, then do not expand further.
5474 -- ??? We can also do that if in place expansion is not possible but
5475 -- then we could go into an infinite recursion.
5477 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
5478 and then VM_Target = No_VM
5479 and then not AAMP_On_Target
5480 and then not Generate_SCIL
5481 and then not Possible_Bit_Aligned_Component (Target)
5482 and then not Is_Possibly_Unaligned_Slice (Target)
5483 and then Aggr_Assignment_OK_For_Backend (N)
5484 then
5485 if Maybe_In_Place_OK then
5486 return;
5487 end if;
5489 Aggr_Code :=
5490 New_List (
5491 Make_Assignment_Statement (Loc,
5492 Name => Target,
5493 Expression => New_Copy (N)));
5495 else
5496 Aggr_Code :=
5497 Build_Array_Aggr_Code (N,
5498 Ctype => Ctyp,
5499 Index => First_Index (Typ),
5500 Into => Target,
5501 Scalar_Comp => Is_Scalar_Type (Ctyp));
5502 end if;
5504 -- Save the last assignment statement associated with the aggregate
5505 -- when building a controlled object. This reference is utilized by
5506 -- the finalization machinery when marking an object as successfully
5507 -- initialized.
5509 if Needs_Finalization (Typ)
5510 and then Is_Entity_Name (Target)
5511 and then Present (Entity (Target))
5512 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
5513 then
5514 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
5515 end if;
5516 end;
5518 -- If the aggregate is the expression in a declaration, the expanded
5519 -- code must be inserted after it. The defining entity might not come
5520 -- from source if this is part of an inlined body, but the declaration
5521 -- itself will.
5523 if Comes_From_Source (Tmp)
5524 or else
5525 (Nkind (Parent (N)) = N_Object_Declaration
5526 and then Comes_From_Source (Parent (N))
5527 and then Tmp = Defining_Entity (Parent (N)))
5528 then
5529 declare
5530 Node_After : constant Node_Id := Next (Parent_Node);
5532 begin
5533 Insert_Actions_After (Parent_Node, Aggr_Code);
5535 if Parent_Kind = N_Object_Declaration then
5536 Collect_Initialization_Statements
5537 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
5538 end if;
5539 end;
5541 else
5542 Insert_Actions (N, Aggr_Code);
5543 end if;
5545 -- If the aggregate has been assigned in place, remove the original
5546 -- assignment.
5548 if Nkind (Parent (N)) = N_Assignment_Statement
5549 and then Maybe_In_Place_OK
5550 then
5551 Rewrite (Parent (N), Make_Null_Statement (Loc));
5553 elsif Nkind (Parent (N)) /= N_Object_Declaration
5554 or else Tmp /= Defining_Identifier (Parent (N))
5555 then
5556 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5557 Analyze_And_Resolve (N, Typ);
5558 end if;
5559 end Expand_Array_Aggregate;
5561 ------------------------
5562 -- Expand_N_Aggregate --
5563 ------------------------
5565 procedure Expand_N_Aggregate (N : Node_Id) is
5566 begin
5567 -- Record aggregate case
5569 if Is_Record_Type (Etype (N)) then
5570 Expand_Record_Aggregate (N);
5572 -- Array aggregate case
5574 else
5575 -- A special case, if we have a string subtype with bounds 1 .. N,
5576 -- where N is known at compile time, and the aggregate is of the
5577 -- form (others => 'x'), with a single choice and no expressions,
5578 -- and N is less than 80 (an arbitrary limit for now), then replace
5579 -- the aggregate by the equivalent string literal (but do not mark
5580 -- it as static since it is not).
5582 -- Note: this entire circuit is redundant with respect to code in
5583 -- Expand_Array_Aggregate that collapses others choices to positional
5584 -- form, but there are two problems with that circuit:
5586 -- a) It is limited to very small cases due to ill-understood
5587 -- interactions with bootstrapping. That limit is removed by
5588 -- use of the No_Implicit_Loops restriction.
5590 -- b) It incorrectly ends up with the resulting expressions being
5591 -- considered static when they are not. For example, the
5592 -- following test should fail:
5594 -- pragma Restrictions (No_Implicit_Loops);
5595 -- package NonSOthers4 is
5596 -- B : constant String (1 .. 6) := (others => 'A');
5597 -- DH : constant String (1 .. 8) := B & "BB";
5598 -- X : Integer;
5599 -- pragma Export (C, X, Link_Name => DH);
5600 -- end;
5602 -- But it succeeds (DH looks static to pragma Export)
5604 -- To be sorted out ???
5606 if Present (Component_Associations (N)) then
5607 declare
5608 CA : constant Node_Id := First (Component_Associations (N));
5609 MX : constant := 80;
5611 begin
5612 if Nkind (First (Choices (CA))) = N_Others_Choice
5613 and then Nkind (Expression (CA)) = N_Character_Literal
5614 and then No (Expressions (N))
5615 then
5616 declare
5617 T : constant Entity_Id := Etype (N);
5618 X : constant Node_Id := First_Index (T);
5619 EC : constant Node_Id := Expression (CA);
5620 CV : constant Uint := Char_Literal_Value (EC);
5621 CC : constant Int := UI_To_Int (CV);
5623 begin
5624 if Nkind (X) = N_Range
5625 and then Compile_Time_Known_Value (Low_Bound (X))
5626 and then Expr_Value (Low_Bound (X)) = 1
5627 and then Compile_Time_Known_Value (High_Bound (X))
5628 then
5629 declare
5630 Hi : constant Uint := Expr_Value (High_Bound (X));
5632 begin
5633 if Hi <= MX then
5634 Start_String;
5636 for J in 1 .. UI_To_Int (Hi) loop
5637 Store_String_Char (Char_Code (CC));
5638 end loop;
5640 Rewrite (N,
5641 Make_String_Literal (Sloc (N),
5642 Strval => End_String));
5644 if CC >= Int (2 ** 16) then
5645 Set_Has_Wide_Wide_Character (N);
5646 elsif CC >= Int (2 ** 8) then
5647 Set_Has_Wide_Character (N);
5648 end if;
5650 Analyze_And_Resolve (N, T);
5651 Set_Is_Static_Expression (N, False);
5652 return;
5653 end if;
5654 end;
5655 end if;
5656 end;
5657 end if;
5658 end;
5659 end if;
5661 -- Not that special case, so normal expansion of array aggregate
5663 Expand_Array_Aggregate (N);
5664 end if;
5666 exception
5667 when RE_Not_Available =>
5668 return;
5669 end Expand_N_Aggregate;
5671 ----------------------------------
5672 -- Expand_N_Extension_Aggregate --
5673 ----------------------------------
5675 -- If the ancestor part is an expression, add a component association for
5676 -- the parent field. If the type of the ancestor part is not the direct
5677 -- parent of the expected type, build recursively the needed ancestors.
5678 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5679 -- ration for a temporary of the expected type, followed by individual
5680 -- assignments to the given components.
5682 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5683 Loc : constant Source_Ptr := Sloc (N);
5684 A : constant Node_Id := Ancestor_Part (N);
5685 Typ : constant Entity_Id := Etype (N);
5687 begin
5688 -- If the ancestor is a subtype mark, an init proc must be called
5689 -- on the resulting object which thus has to be materialized in
5690 -- the front-end
5692 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5693 Convert_To_Assignments (N, Typ);
5695 -- The extension aggregate is transformed into a record aggregate
5696 -- of the following form (c1 and c2 are inherited components)
5698 -- (Exp with c3 => a, c4 => b)
5699 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5701 else
5702 Set_Etype (N, Typ);
5704 if Tagged_Type_Expansion then
5705 Expand_Record_Aggregate (N,
5706 Orig_Tag =>
5707 New_Occurrence_Of
5708 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5709 Parent_Expr => A);
5711 -- No tag is needed in the case of a VM
5713 else
5714 Expand_Record_Aggregate (N, Parent_Expr => A);
5715 end if;
5716 end if;
5718 exception
5719 when RE_Not_Available =>
5720 return;
5721 end Expand_N_Extension_Aggregate;
5723 -----------------------------
5724 -- Expand_Record_Aggregate --
5725 -----------------------------
5727 procedure Expand_Record_Aggregate
5728 (N : Node_Id;
5729 Orig_Tag : Node_Id := Empty;
5730 Parent_Expr : Node_Id := Empty)
5732 Loc : constant Source_Ptr := Sloc (N);
5733 Comps : constant List_Id := Component_Associations (N);
5734 Typ : constant Entity_Id := Etype (N);
5735 Base_Typ : constant Entity_Id := Base_Type (Typ);
5737 Static_Components : Boolean := True;
5738 -- Flag to indicate whether all components are compile-time known,
5739 -- and the aggregate can be constructed statically and handled by
5740 -- the back-end.
5742 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
5743 -- Returns true if N is an expression of composite type which can be
5744 -- fully evaluated at compile time without raising constraint error.
5745 -- Such expressions can be passed as is to Gigi without any expansion.
5747 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
5748 -- set and constants whose expression is such an aggregate, recursively.
5750 function Component_Not_OK_For_Backend return Boolean;
5751 -- Check for presence of a component which makes it impossible for the
5752 -- backend to process the aggregate, thus requiring the use of a series
5753 -- of assignment statements. Cases checked for are a nested aggregate
5754 -- needing Late_Expansion, the presence of a tagged component which may
5755 -- need tag adjustment, and a bit unaligned component reference.
5757 -- We also force expansion into assignments if a component is of a
5758 -- mutable type (including a private type with discriminants) because
5759 -- in that case the size of the component to be copied may be smaller
5760 -- than the side of the target, and there is no simple way for gigi
5761 -- to compute the size of the object to be copied.
5763 -- NOTE: This is part of the ongoing work to define precisely the
5764 -- interface between front-end and back-end handling of aggregates.
5765 -- In general it is desirable to pass aggregates as they are to gigi,
5766 -- in order to minimize elaboration code. This is one case where the
5767 -- semantics of Ada complicate the analysis and lead to anomalies in
5768 -- the gcc back-end if the aggregate is not expanded into assignments.
5770 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
5771 -- If any ancestor of the current type is private, the aggregate
5772 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
5773 -- because it will not be set when type and its parent are in the
5774 -- same scope, and the parent component needs expansion.
5776 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
5777 -- For nested aggregates return the ultimate enclosing aggregate; for
5778 -- non-nested aggregates return N.
5780 ----------------------------------------
5781 -- Compile_Time_Known_Composite_Value --
5782 ----------------------------------------
5784 function Compile_Time_Known_Composite_Value
5785 (N : Node_Id) return Boolean
5787 begin
5788 -- If we have an entity name, then see if it is the name of a
5789 -- constant and if so, test the corresponding constant value.
5791 if Is_Entity_Name (N) then
5792 declare
5793 E : constant Entity_Id := Entity (N);
5794 V : Node_Id;
5795 begin
5796 if Ekind (E) /= E_Constant then
5797 return False;
5798 else
5799 V := Constant_Value (E);
5800 return Present (V)
5801 and then Compile_Time_Known_Composite_Value (V);
5802 end if;
5803 end;
5805 -- We have a value, see if it is compile time known
5807 else
5808 if Nkind (N) = N_Aggregate then
5809 return Compile_Time_Known_Aggregate (N);
5810 end if;
5812 -- All other types of values are not known at compile time
5814 return False;
5815 end if;
5817 end Compile_Time_Known_Composite_Value;
5819 ----------------------------------
5820 -- Component_Not_OK_For_Backend --
5821 ----------------------------------
5823 function Component_Not_OK_For_Backend return Boolean is
5824 C : Node_Id;
5825 Expr_Q : Node_Id;
5827 begin
5828 if No (Comps) then
5829 return False;
5830 end if;
5832 C := First (Comps);
5833 while Present (C) loop
5835 -- If the component has box initialization, expansion is needed
5836 -- and component is not ready for backend.
5838 if Box_Present (C) then
5839 return True;
5840 end if;
5842 if Nkind (Expression (C)) = N_Qualified_Expression then
5843 Expr_Q := Expression (Expression (C));
5844 else
5845 Expr_Q := Expression (C);
5846 end if;
5848 -- Return true if the aggregate has any associations for tagged
5849 -- components that may require tag adjustment.
5851 -- These are cases where the source expression may have a tag that
5852 -- could differ from the component tag (e.g., can occur for type
5853 -- conversions and formal parameters). (Tag adjustment not needed
5854 -- if VM_Target because object tags are implicit in the machine.)
5856 if Is_Tagged_Type (Etype (Expr_Q))
5857 and then (Nkind (Expr_Q) = N_Type_Conversion
5858 or else (Is_Entity_Name (Expr_Q)
5859 and then
5860 Ekind (Entity (Expr_Q)) in Formal_Kind))
5861 and then Tagged_Type_Expansion
5862 then
5863 Static_Components := False;
5864 return True;
5866 elsif Is_Delayed_Aggregate (Expr_Q) then
5867 Static_Components := False;
5868 return True;
5870 elsif Possible_Bit_Aligned_Component (Expr_Q) then
5871 Static_Components := False;
5872 return True;
5873 end if;
5875 if Is_Elementary_Type (Etype (Expr_Q)) then
5876 if not Compile_Time_Known_Value (Expr_Q) then
5877 Static_Components := False;
5878 end if;
5880 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
5881 Static_Components := False;
5883 if Is_Private_Type (Etype (Expr_Q))
5884 and then Has_Discriminants (Etype (Expr_Q))
5885 then
5886 return True;
5887 end if;
5888 end if;
5890 Next (C);
5891 end loop;
5893 return False;
5894 end Component_Not_OK_For_Backend;
5896 -----------------------------------
5897 -- Has_Visible_Private_Ancestor --
5898 -----------------------------------
5900 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
5901 R : constant Entity_Id := Root_Type (Id);
5902 T1 : Entity_Id := Id;
5904 begin
5905 loop
5906 if Is_Private_Type (T1) then
5907 return True;
5909 elsif T1 = R then
5910 return False;
5912 else
5913 T1 := Etype (T1);
5914 end if;
5915 end loop;
5916 end Has_Visible_Private_Ancestor;
5918 -------------------------
5919 -- Top_Level_Aggregate --
5920 -------------------------
5922 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
5923 Aggr : Node_Id;
5925 begin
5926 Aggr := N;
5927 while Present (Parent (Aggr))
5928 and then Nkind_In (Parent (Aggr), N_Component_Association,
5929 N_Aggregate)
5930 loop
5931 Aggr := Parent (Aggr);
5932 end loop;
5934 return Aggr;
5935 end Top_Level_Aggregate;
5937 -- Local variables
5939 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
5940 Tag_Value : Node_Id;
5941 Comp : Entity_Id;
5942 New_Comp : Node_Id;
5944 -- Start of processing for Expand_Record_Aggregate
5946 begin
5947 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
5948 -- to prevent a piecemeal assignment even if the aggregate is to be
5949 -- expanded. We create a temporary for the aggregate, and assign the
5950 -- temporary instead, so that the back end can generate an atomic move
5951 -- for it.
5953 if Is_Atomic_VFA_Aggregate (N) then
5954 return;
5956 -- No special management required for aggregates used to initialize
5957 -- statically allocated dispatch tables
5959 elsif Is_Static_Dispatch_Table_Aggregate (N) then
5960 return;
5961 end if;
5963 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
5964 -- are build-in-place function calls. The assignments will each turn
5965 -- into a build-in-place function call. If components are all static,
5966 -- we can pass the aggregate to the backend regardless of limitedness.
5968 -- Extension aggregates, aggregates in extended return statements, and
5969 -- aggregates for C++ imported types must be expanded.
5971 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
5972 if not Nkind_In (Parent (N), N_Object_Declaration,
5973 N_Component_Association)
5974 then
5975 Convert_To_Assignments (N, Typ);
5977 elsif Nkind (N) = N_Extension_Aggregate
5978 or else Convention (Typ) = Convention_CPP
5979 then
5980 Convert_To_Assignments (N, Typ);
5982 elsif not Size_Known_At_Compile_Time (Typ)
5983 or else Component_Not_OK_For_Backend
5984 or else not Static_Components
5985 then
5986 Convert_To_Assignments (N, Typ);
5988 else
5989 Set_Compile_Time_Known_Aggregate (N);
5990 Set_Expansion_Delayed (N, False);
5991 end if;
5993 -- Gigi doesn't properly handle temporaries of variable size so we
5994 -- generate it in the front-end
5996 elsif not Size_Known_At_Compile_Time (Typ)
5997 and then Tagged_Type_Expansion
5998 then
5999 Convert_To_Assignments (N, Typ);
6001 -- An aggregate used to initialize a controlled object must be turned
6002 -- into component assignments as the components themselves may require
6003 -- finalization actions such as adjustment.
6005 elsif Needs_Finalization (Typ) then
6006 Convert_To_Assignments (N, Typ);
6008 -- Ada 2005 (AI-287): In case of default initialized components we
6009 -- convert the aggregate into assignments.
6011 elsif Has_Default_Init_Comps (N) then
6012 Convert_To_Assignments (N, Typ);
6014 -- Check components
6016 elsif Component_Not_OK_For_Backend then
6017 Convert_To_Assignments (N, Typ);
6019 -- If an ancestor is private, some components are not inherited and we
6020 -- cannot expand into a record aggregate.
6022 elsif Has_Visible_Private_Ancestor (Typ) then
6023 Convert_To_Assignments (N, Typ);
6025 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
6026 -- is not able to handle the aggregate for Late_Request.
6028 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
6029 Convert_To_Assignments (N, Typ);
6031 -- If the tagged types covers interface types we need to initialize all
6032 -- hidden components containing pointers to secondary dispatch tables.
6034 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
6035 Convert_To_Assignments (N, Typ);
6037 -- If some components are mutable, the size of the aggregate component
6038 -- may be distinct from the default size of the type component, so
6039 -- we need to expand to insure that the back-end copies the proper
6040 -- size of the data. However, if the aggregate is the initial value of
6041 -- a constant, the target is immutable and might be built statically
6042 -- if components are appropriate.
6044 elsif Has_Mutable_Components (Typ)
6045 and then
6046 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
6047 or else not Constant_Present (Parent (Top_Level_Aggr))
6048 or else not Static_Components)
6049 then
6050 Convert_To_Assignments (N, Typ);
6052 -- If the type involved has bit aligned components, then we are not sure
6053 -- that the back end can handle this case correctly.
6055 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
6056 Convert_To_Assignments (N, Typ);
6058 -- In all other cases, build a proper aggregate to be handled by gigi
6060 else
6061 if Nkind (N) = N_Aggregate then
6063 -- If the aggregate is static and can be handled by the back-end,
6064 -- nothing left to do.
6066 if Static_Components then
6067 Set_Compile_Time_Known_Aggregate (N);
6068 Set_Expansion_Delayed (N, False);
6069 end if;
6070 end if;
6072 -- If no discriminants, nothing special to do
6074 if not Has_Discriminants (Typ) then
6075 null;
6077 -- Case of discriminants present
6079 elsif Is_Derived_Type (Typ) then
6081 -- For untagged types, non-stored discriminants are replaced
6082 -- with stored discriminants, which are the ones that gigi uses
6083 -- to describe the type and its components.
6085 Generate_Aggregate_For_Derived_Type : declare
6086 Constraints : constant List_Id := New_List;
6087 First_Comp : Node_Id;
6088 Discriminant : Entity_Id;
6089 Decl : Node_Id;
6090 Num_Disc : Int := 0;
6091 Num_Gird : Int := 0;
6093 procedure Prepend_Stored_Values (T : Entity_Id);
6094 -- Scan the list of stored discriminants of the type, and add
6095 -- their values to the aggregate being built.
6097 ---------------------------
6098 -- Prepend_Stored_Values --
6099 ---------------------------
6101 procedure Prepend_Stored_Values (T : Entity_Id) is
6102 begin
6103 Discriminant := First_Stored_Discriminant (T);
6104 while Present (Discriminant) loop
6105 New_Comp :=
6106 Make_Component_Association (Loc,
6107 Choices =>
6108 New_List (New_Occurrence_Of (Discriminant, Loc)),
6110 Expression =>
6111 New_Copy_Tree
6112 (Get_Discriminant_Value
6113 (Discriminant,
6114 Typ,
6115 Discriminant_Constraint (Typ))));
6117 if No (First_Comp) then
6118 Prepend_To (Component_Associations (N), New_Comp);
6119 else
6120 Insert_After (First_Comp, New_Comp);
6121 end if;
6123 First_Comp := New_Comp;
6124 Next_Stored_Discriminant (Discriminant);
6125 end loop;
6126 end Prepend_Stored_Values;
6128 -- Start of processing for Generate_Aggregate_For_Derived_Type
6130 begin
6131 -- Remove the associations for the discriminant of derived type
6133 First_Comp := First (Component_Associations (N));
6134 while Present (First_Comp) loop
6135 Comp := First_Comp;
6136 Next (First_Comp);
6138 if Ekind (Entity (First (Choices (Comp)))) = E_Discriminant
6139 then
6140 Remove (Comp);
6141 Num_Disc := Num_Disc + 1;
6142 end if;
6143 end loop;
6145 -- Insert stored discriminant associations in the correct
6146 -- order. If there are more stored discriminants than new
6147 -- discriminants, there is at least one new discriminant that
6148 -- constrains more than one of the stored discriminants. In
6149 -- this case we need to construct a proper subtype of the
6150 -- parent type, in order to supply values to all the
6151 -- components. Otherwise there is one-one correspondence
6152 -- between the constraints and the stored discriminants.
6154 First_Comp := Empty;
6156 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6157 while Present (Discriminant) loop
6158 Num_Gird := Num_Gird + 1;
6159 Next_Stored_Discriminant (Discriminant);
6160 end loop;
6162 -- Case of more stored discriminants than new discriminants
6164 if Num_Gird > Num_Disc then
6166 -- Create a proper subtype of the parent type, which is the
6167 -- proper implementation type for the aggregate, and convert
6168 -- it to the intended target type.
6170 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6171 while Present (Discriminant) loop
6172 New_Comp :=
6173 New_Copy_Tree
6174 (Get_Discriminant_Value
6175 (Discriminant,
6176 Typ,
6177 Discriminant_Constraint (Typ)));
6178 Append (New_Comp, Constraints);
6179 Next_Stored_Discriminant (Discriminant);
6180 end loop;
6182 Decl :=
6183 Make_Subtype_Declaration (Loc,
6184 Defining_Identifier => Make_Temporary (Loc, 'T'),
6185 Subtype_Indication =>
6186 Make_Subtype_Indication (Loc,
6187 Subtype_Mark =>
6188 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
6189 Constraint =>
6190 Make_Index_Or_Discriminant_Constraint
6191 (Loc, Constraints)));
6193 Insert_Action (N, Decl);
6194 Prepend_Stored_Values (Base_Type (Typ));
6196 Set_Etype (N, Defining_Identifier (Decl));
6197 Set_Analyzed (N);
6199 Rewrite (N, Unchecked_Convert_To (Typ, N));
6200 Analyze (N);
6202 -- Case where we do not have fewer new discriminants than
6203 -- stored discriminants, so in this case we can simply use the
6204 -- stored discriminants of the subtype.
6206 else
6207 Prepend_Stored_Values (Typ);
6208 end if;
6209 end Generate_Aggregate_For_Derived_Type;
6210 end if;
6212 if Is_Tagged_Type (Typ) then
6214 -- In the tagged case, _parent and _tag component must be created
6216 -- Reset Null_Present unconditionally. Tagged records always have
6217 -- at least one field (the tag or the parent).
6219 Set_Null_Record_Present (N, False);
6221 -- When the current aggregate comes from the expansion of an
6222 -- extension aggregate, the parent expr is replaced by an
6223 -- aggregate formed by selected components of this expr.
6225 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
6226 Comp := First_Component_Or_Discriminant (Typ);
6227 while Present (Comp) loop
6229 -- Skip all expander-generated components
6231 if not Comes_From_Source (Original_Record_Component (Comp))
6232 then
6233 null;
6235 else
6236 New_Comp :=
6237 Make_Selected_Component (Loc,
6238 Prefix =>
6239 Unchecked_Convert_To (Typ,
6240 Duplicate_Subexpr (Parent_Expr, True)),
6241 Selector_Name => New_Occurrence_Of (Comp, Loc));
6243 Append_To (Comps,
6244 Make_Component_Association (Loc,
6245 Choices =>
6246 New_List (New_Occurrence_Of (Comp, Loc)),
6247 Expression => New_Comp));
6249 Analyze_And_Resolve (New_Comp, Etype (Comp));
6250 end if;
6252 Next_Component_Or_Discriminant (Comp);
6253 end loop;
6254 end if;
6256 -- Compute the value for the Tag now, if the type is a root it
6257 -- will be included in the aggregate right away, otherwise it will
6258 -- be propagated to the parent aggregate.
6260 if Present (Orig_Tag) then
6261 Tag_Value := Orig_Tag;
6262 elsif not Tagged_Type_Expansion then
6263 Tag_Value := Empty;
6264 else
6265 Tag_Value :=
6266 New_Occurrence_Of
6267 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6268 end if;
6270 -- For a derived type, an aggregate for the parent is formed with
6271 -- all the inherited components.
6273 if Is_Derived_Type (Typ) then
6275 declare
6276 First_Comp : Node_Id;
6277 Parent_Comps : List_Id;
6278 Parent_Aggr : Node_Id;
6279 Parent_Name : Node_Id;
6281 begin
6282 -- Remove the inherited component association from the
6283 -- aggregate and store them in the parent aggregate
6285 First_Comp := First (Component_Associations (N));
6286 Parent_Comps := New_List;
6287 while Present (First_Comp)
6288 and then
6289 Scope (Original_Record_Component
6290 (Entity (First (Choices (First_Comp))))) /=
6291 Base_Typ
6292 loop
6293 Comp := First_Comp;
6294 Next (First_Comp);
6295 Remove (Comp);
6296 Append (Comp, Parent_Comps);
6297 end loop;
6299 Parent_Aggr :=
6300 Make_Aggregate (Loc,
6301 Component_Associations => Parent_Comps);
6302 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
6304 -- Find the _parent component
6306 Comp := First_Component (Typ);
6307 while Chars (Comp) /= Name_uParent loop
6308 Comp := Next_Component (Comp);
6309 end loop;
6311 Parent_Name := New_Occurrence_Of (Comp, Loc);
6313 -- Insert the parent aggregate
6315 Prepend_To (Component_Associations (N),
6316 Make_Component_Association (Loc,
6317 Choices => New_List (Parent_Name),
6318 Expression => Parent_Aggr));
6320 -- Expand recursively the parent propagating the right Tag
6322 Expand_Record_Aggregate
6323 (Parent_Aggr, Tag_Value, Parent_Expr);
6325 -- The ancestor part may be a nested aggregate that has
6326 -- delayed expansion: recheck now.
6328 if Component_Not_OK_For_Backend then
6329 Convert_To_Assignments (N, Typ);
6330 end if;
6331 end;
6333 -- For a root type, the tag component is added (unless compiling
6334 -- for the VMs, where tags are implicit).
6336 elsif Tagged_Type_Expansion then
6337 declare
6338 Tag_Name : constant Node_Id :=
6339 New_Occurrence_Of (First_Tag_Component (Typ), Loc);
6340 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
6341 Conv_Node : constant Node_Id :=
6342 Unchecked_Convert_To (Typ_Tag, Tag_Value);
6344 begin
6345 Set_Etype (Conv_Node, Typ_Tag);
6346 Prepend_To (Component_Associations (N),
6347 Make_Component_Association (Loc,
6348 Choices => New_List (Tag_Name),
6349 Expression => Conv_Node));
6350 end;
6351 end if;
6352 end if;
6353 end if;
6355 end Expand_Record_Aggregate;
6357 ----------------------------
6358 -- Has_Default_Init_Comps --
6359 ----------------------------
6361 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
6362 Comps : constant List_Id := Component_Associations (N);
6363 C : Node_Id;
6364 Expr : Node_Id;
6366 begin
6367 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
6369 if No (Comps) then
6370 return False;
6371 end if;
6373 if Has_Self_Reference (N) then
6374 return True;
6375 end if;
6377 -- Check if any direct component has default initialized components
6379 C := First (Comps);
6380 while Present (C) loop
6381 if Box_Present (C) then
6382 return True;
6383 end if;
6385 Next (C);
6386 end loop;
6388 -- Recursive call in case of aggregate expression
6390 C := First (Comps);
6391 while Present (C) loop
6392 Expr := Expression (C);
6394 if Present (Expr)
6395 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
6396 and then Has_Default_Init_Comps (Expr)
6397 then
6398 return True;
6399 end if;
6401 Next (C);
6402 end loop;
6404 return False;
6405 end Has_Default_Init_Comps;
6407 --------------------------
6408 -- Is_Delayed_Aggregate --
6409 --------------------------
6411 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
6412 Node : Node_Id := N;
6413 Kind : Node_Kind := Nkind (Node);
6415 begin
6416 if Kind = N_Qualified_Expression then
6417 Node := Expression (Node);
6418 Kind := Nkind (Node);
6419 end if;
6421 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
6422 return False;
6423 else
6424 return Expansion_Delayed (Node);
6425 end if;
6426 end Is_Delayed_Aggregate;
6428 ----------------------------------------
6429 -- Is_Static_Dispatch_Table_Aggregate --
6430 ----------------------------------------
6432 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
6433 Typ : constant Entity_Id := Base_Type (Etype (N));
6435 begin
6436 return Static_Dispatch_Tables
6437 and then Tagged_Type_Expansion
6438 and then RTU_Loaded (Ada_Tags)
6440 -- Avoid circularity when rebuilding the compiler
6442 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
6443 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
6444 or else
6445 Typ = RTE (RE_Address_Array)
6446 or else
6447 Typ = RTE (RE_Type_Specific_Data)
6448 or else
6449 Typ = RTE (RE_Tag_Table)
6450 or else
6451 (RTE_Available (RE_Interface_Data)
6452 and then Typ = RTE (RE_Interface_Data))
6453 or else
6454 (RTE_Available (RE_Interfaces_Array)
6455 and then Typ = RTE (RE_Interfaces_Array))
6456 or else
6457 (RTE_Available (RE_Interface_Data_Element)
6458 and then Typ = RTE (RE_Interface_Data_Element)));
6459 end Is_Static_Dispatch_Table_Aggregate;
6461 -----------------------------
6462 -- Is_Two_Dim_Packed_Array --
6463 -----------------------------
6465 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
6466 C : constant Int := UI_To_Int (Component_Size (Typ));
6467 begin
6468 return Number_Dimensions (Typ) = 2
6469 and then Is_Bit_Packed_Array (Typ)
6470 and then (C = 1 or else C = 2 or else C = 4);
6471 end Is_Two_Dim_Packed_Array;
6473 --------------------
6474 -- Late_Expansion --
6475 --------------------
6477 function Late_Expansion
6478 (N : Node_Id;
6479 Typ : Entity_Id;
6480 Target : Node_Id) return List_Id
6482 Aggr_Code : List_Id;
6484 begin
6485 if Is_Record_Type (Etype (N)) then
6486 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
6488 else pragma Assert (Is_Array_Type (Etype (N)));
6489 Aggr_Code :=
6490 Build_Array_Aggr_Code
6491 (N => N,
6492 Ctype => Component_Type (Etype (N)),
6493 Index => First_Index (Typ),
6494 Into => Target,
6495 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
6496 Indexes => No_List);
6497 end if;
6499 -- Save the last assignment statement associated with the aggregate
6500 -- when building a controlled object. This reference is utilized by
6501 -- the finalization machinery when marking an object as successfully
6502 -- initialized.
6504 if Needs_Finalization (Typ)
6505 and then Is_Entity_Name (Target)
6506 and then Present (Entity (Target))
6507 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6508 then
6509 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6510 end if;
6512 return Aggr_Code;
6513 end Late_Expansion;
6515 ----------------------------------
6516 -- Make_OK_Assignment_Statement --
6517 ----------------------------------
6519 function Make_OK_Assignment_Statement
6520 (Sloc : Source_Ptr;
6521 Name : Node_Id;
6522 Expression : Node_Id) return Node_Id
6524 begin
6525 Set_Assignment_OK (Name);
6526 return Make_Assignment_Statement (Sloc, Name, Expression);
6527 end Make_OK_Assignment_Statement;
6529 -----------------------
6530 -- Number_Of_Choices --
6531 -----------------------
6533 function Number_Of_Choices (N : Node_Id) return Nat is
6534 Assoc : Node_Id;
6535 Choice : Node_Id;
6537 Nb_Choices : Nat := 0;
6539 begin
6540 if Present (Expressions (N)) then
6541 return 0;
6542 end if;
6544 Assoc := First (Component_Associations (N));
6545 while Present (Assoc) loop
6546 Choice := First (Choices (Assoc));
6547 while Present (Choice) loop
6548 if Nkind (Choice) /= N_Others_Choice then
6549 Nb_Choices := Nb_Choices + 1;
6550 end if;
6552 Next (Choice);
6553 end loop;
6555 Next (Assoc);
6556 end loop;
6558 return Nb_Choices;
6559 end Number_Of_Choices;
6561 ------------------------------------
6562 -- Packed_Array_Aggregate_Handled --
6563 ------------------------------------
6565 -- The current version of this procedure will handle at compile time
6566 -- any array aggregate that meets these conditions:
6568 -- One and two dimensional, bit packed
6569 -- Underlying packed type is modular type
6570 -- Bounds are within 32-bit Int range
6571 -- All bounds and values are static
6573 -- Note: for now, in the 2-D case, we only handle component sizes of
6574 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
6576 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6577 Loc : constant Source_Ptr := Sloc (N);
6578 Typ : constant Entity_Id := Etype (N);
6579 Ctyp : constant Entity_Id := Component_Type (Typ);
6581 Not_Handled : exception;
6582 -- Exception raised if this aggregate cannot be handled
6584 begin
6585 -- Handle one- or two dimensional bit packed array
6587 if not Is_Bit_Packed_Array (Typ)
6588 or else Number_Dimensions (Typ) > 2
6589 then
6590 return False;
6591 end if;
6593 -- If two-dimensional, check whether it can be folded, and transformed
6594 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
6595 -- the original type.
6597 if Number_Dimensions (Typ) = 2 then
6598 return Two_Dim_Packed_Array_Handled (N);
6599 end if;
6601 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
6602 return False;
6603 end if;
6605 if not Is_Scalar_Type (Component_Type (Typ))
6606 and then Has_Non_Standard_Rep (Component_Type (Typ))
6607 then
6608 return False;
6609 end if;
6611 declare
6612 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6614 Lo : Node_Id;
6615 Hi : Node_Id;
6616 -- Bounds of index type
6618 Lob : Uint;
6619 Hib : Uint;
6620 -- Values of bounds if compile time known
6622 function Get_Component_Val (N : Node_Id) return Uint;
6623 -- Given a expression value N of the component type Ctyp, returns a
6624 -- value of Csiz (component size) bits representing this value. If
6625 -- the value is non-static or any other reason exists why the value
6626 -- cannot be returned, then Not_Handled is raised.
6628 -----------------------
6629 -- Get_Component_Val --
6630 -----------------------
6632 function Get_Component_Val (N : Node_Id) return Uint is
6633 Val : Uint;
6635 begin
6636 -- We have to analyze the expression here before doing any further
6637 -- processing here. The analysis of such expressions is deferred
6638 -- till expansion to prevent some problems of premature analysis.
6640 Analyze_And_Resolve (N, Ctyp);
6642 -- Must have a compile time value. String literals have to be
6643 -- converted into temporaries as well, because they cannot easily
6644 -- be converted into their bit representation.
6646 if not Compile_Time_Known_Value (N)
6647 or else Nkind (N) = N_String_Literal
6648 then
6649 raise Not_Handled;
6650 end if;
6652 Val := Expr_Rep_Value (N);
6654 -- Adjust for bias, and strip proper number of bits
6656 if Has_Biased_Representation (Ctyp) then
6657 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6658 end if;
6660 return Val mod Uint_2 ** Csiz;
6661 end Get_Component_Val;
6663 -- Here we know we have a one dimensional bit packed array
6665 begin
6666 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6668 -- Cannot do anything if bounds are dynamic
6670 if not Compile_Time_Known_Value (Lo)
6671 or else
6672 not Compile_Time_Known_Value (Hi)
6673 then
6674 return False;
6675 end if;
6677 -- Or are silly out of range of int bounds
6679 Lob := Expr_Value (Lo);
6680 Hib := Expr_Value (Hi);
6682 if not UI_Is_In_Int_Range (Lob)
6683 or else
6684 not UI_Is_In_Int_Range (Hib)
6685 then
6686 return False;
6687 end if;
6689 -- At this stage we have a suitable aggregate for handling at compile
6690 -- time. The only remaining checks are that the values of expressions
6691 -- in the aggregate are compile-time known (checks are performed by
6692 -- Get_Component_Val), and that any subtypes or ranges are statically
6693 -- known.
6695 -- If the aggregate is not fully positional at this stage, then
6696 -- convert it to positional form. Either this will fail, in which
6697 -- case we can do nothing, or it will succeed, in which case we have
6698 -- succeeded in handling the aggregate and transforming it into a
6699 -- modular value, or it will stay an aggregate, in which case we
6700 -- have failed to create a packed value for it.
6702 if Present (Component_Associations (N)) then
6703 Convert_To_Positional
6704 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6705 return Nkind (N) /= N_Aggregate;
6706 end if;
6708 -- Otherwise we are all positional, so convert to proper value
6710 declare
6711 Lov : constant Int := UI_To_Int (Lob);
6712 Hiv : constant Int := UI_To_Int (Hib);
6714 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6715 -- The length of the array (number of elements)
6717 Aggregate_Val : Uint;
6718 -- Value of aggregate. The value is set in the low order bits of
6719 -- this value. For the little-endian case, the values are stored
6720 -- from low-order to high-order and for the big-endian case the
6721 -- values are stored from high-order to low-order. Note that gigi
6722 -- will take care of the conversions to left justify the value in
6723 -- the big endian case (because of left justified modular type
6724 -- processing), so we do not have to worry about that here.
6726 Lit : Node_Id;
6727 -- Integer literal for resulting constructed value
6729 Shift : Nat;
6730 -- Shift count from low order for next value
6732 Incr : Int;
6733 -- Shift increment for loop
6735 Expr : Node_Id;
6736 -- Next expression from positional parameters of aggregate
6738 Left_Justified : Boolean;
6739 -- Set True if we are filling the high order bits of the target
6740 -- value (i.e. the value is left justified).
6742 begin
6743 -- For little endian, we fill up the low order bits of the target
6744 -- value. For big endian we fill up the high order bits of the
6745 -- target value (which is a left justified modular value).
6747 Left_Justified := Bytes_Big_Endian;
6749 -- Switch justification if using -gnatd8
6751 if Debug_Flag_8 then
6752 Left_Justified := not Left_Justified;
6753 end if;
6755 -- Switch justfification if reverse storage order
6757 if Reverse_Storage_Order (Base_Type (Typ)) then
6758 Left_Justified := not Left_Justified;
6759 end if;
6761 if Left_Justified then
6762 Shift := Csiz * (Len - 1);
6763 Incr := -Csiz;
6764 else
6765 Shift := 0;
6766 Incr := +Csiz;
6767 end if;
6769 -- Loop to set the values
6771 if Len = 0 then
6772 Aggregate_Val := Uint_0;
6773 else
6774 Expr := First (Expressions (N));
6775 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
6777 for J in 2 .. Len loop
6778 Shift := Shift + Incr;
6779 Next (Expr);
6780 Aggregate_Val :=
6781 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
6782 end loop;
6783 end if;
6785 -- Now we can rewrite with the proper value
6787 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
6788 Set_Print_In_Hex (Lit);
6790 -- Construct the expression using this literal. Note that it is
6791 -- important to qualify the literal with its proper modular type
6792 -- since universal integer does not have the required range and
6793 -- also this is a left justified modular type, which is important
6794 -- in the big-endian case.
6796 Rewrite (N,
6797 Unchecked_Convert_To (Typ,
6798 Make_Qualified_Expression (Loc,
6799 Subtype_Mark =>
6800 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
6801 Expression => Lit)));
6803 Analyze_And_Resolve (N, Typ);
6804 return True;
6805 end;
6806 end;
6808 exception
6809 when Not_Handled =>
6810 return False;
6811 end Packed_Array_Aggregate_Handled;
6813 ----------------------------
6814 -- Has_Mutable_Components --
6815 ----------------------------
6817 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
6818 Comp : Entity_Id;
6820 begin
6821 Comp := First_Component (Typ);
6822 while Present (Comp) loop
6823 if Is_Record_Type (Etype (Comp))
6824 and then Has_Discriminants (Etype (Comp))
6825 and then not Is_Constrained (Etype (Comp))
6826 then
6827 return True;
6828 end if;
6830 Next_Component (Comp);
6831 end loop;
6833 return False;
6834 end Has_Mutable_Components;
6836 ------------------------------
6837 -- Initialize_Discriminants --
6838 ------------------------------
6840 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
6841 Loc : constant Source_Ptr := Sloc (N);
6842 Bas : constant Entity_Id := Base_Type (Typ);
6843 Par : constant Entity_Id := Etype (Bas);
6844 Decl : constant Node_Id := Parent (Par);
6845 Ref : Node_Id;
6847 begin
6848 if Is_Tagged_Type (Bas)
6849 and then Is_Derived_Type (Bas)
6850 and then Has_Discriminants (Par)
6851 and then Has_Discriminants (Bas)
6852 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
6853 and then Nkind (Decl) = N_Full_Type_Declaration
6854 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6855 and then
6856 Present (Variant_Part (Component_List (Type_Definition (Decl))))
6857 and then Nkind (N) /= N_Extension_Aggregate
6858 then
6860 -- Call init proc to set discriminants.
6861 -- There should eventually be a special procedure for this ???
6863 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
6864 Insert_Actions_After (N,
6865 Build_Initialization_Call (Sloc (N), Ref, Typ));
6866 end if;
6867 end Initialize_Discriminants;
6869 ----------------
6870 -- Must_Slide --
6871 ----------------
6873 function Must_Slide
6874 (Obj_Type : Entity_Id;
6875 Typ : Entity_Id) return Boolean
6877 L1, L2, H1, H2 : Node_Id;
6879 begin
6880 -- No sliding if the type of the object is not established yet, if it is
6881 -- an unconstrained type whose actual subtype comes from the aggregate,
6882 -- or if the two types are identical.
6884 if not Is_Array_Type (Obj_Type) then
6885 return False;
6887 elsif not Is_Constrained (Obj_Type) then
6888 return False;
6890 elsif Typ = Obj_Type then
6891 return False;
6893 else
6894 -- Sliding can only occur along the first dimension
6896 Get_Index_Bounds (First_Index (Typ), L1, H1);
6897 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
6899 if not Is_OK_Static_Expression (L1) or else
6900 not Is_OK_Static_Expression (L2) or else
6901 not Is_OK_Static_Expression (H1) or else
6902 not Is_OK_Static_Expression (H2)
6903 then
6904 return False;
6905 else
6906 return Expr_Value (L1) /= Expr_Value (L2)
6907 or else
6908 Expr_Value (H1) /= Expr_Value (H2);
6909 end if;
6910 end if;
6911 end Must_Slide;
6913 ----------------------------------
6914 -- Two_Dim_Packed_Array_Handled --
6915 ----------------------------------
6917 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
6918 Loc : constant Source_Ptr := Sloc (N);
6919 Typ : constant Entity_Id := Etype (N);
6920 Ctyp : constant Entity_Id := Component_Type (Typ);
6921 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
6922 Packed_Array : constant Entity_Id :=
6923 Packed_Array_Impl_Type (Base_Type (Typ));
6925 One_Comp : Node_Id;
6926 -- Expression in original aggregate
6928 One_Dim : Node_Id;
6929 -- One-dimensional subaggregate
6931 begin
6933 -- For now, only deal with cases where an integral number of elements
6934 -- fit in a single byte. This includes the most common boolean case.
6936 if not (Comp_Size = 1 or else
6937 Comp_Size = 2 or else
6938 Comp_Size = 4)
6939 then
6940 return False;
6941 end if;
6943 Convert_To_Positional
6944 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6946 -- Verify that all components are static
6948 if Nkind (N) = N_Aggregate
6949 and then Compile_Time_Known_Aggregate (N)
6950 then
6951 null;
6953 -- The aggregate may have been re-analyzed and converted already
6955 elsif Nkind (N) /= N_Aggregate then
6956 return True;
6958 -- If component associations remain, the aggregate is not static
6960 elsif Present (Component_Associations (N)) then
6961 return False;
6963 else
6964 One_Dim := First (Expressions (N));
6965 while Present (One_Dim) loop
6966 if Present (Component_Associations (One_Dim)) then
6967 return False;
6968 end if;
6970 One_Comp := First (Expressions (One_Dim));
6971 while Present (One_Comp) loop
6972 if not Is_OK_Static_Expression (One_Comp) then
6973 return False;
6974 end if;
6976 Next (One_Comp);
6977 end loop;
6979 Next (One_Dim);
6980 end loop;
6981 end if;
6983 -- Two-dimensional aggregate is now fully positional so pack one
6984 -- dimension to create a static one-dimensional array, and rewrite
6985 -- as an unchecked conversion to the original type.
6987 declare
6988 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
6989 -- The packed array type is a byte array
6991 Packed_Num : Int;
6992 -- Number of components accumulated in current byte
6994 Comps : List_Id;
6995 -- Assembled list of packed values for equivalent aggregate
6997 Comp_Val : Uint;
6998 -- integer value of component
7000 Incr : Int;
7001 -- Step size for packing
7003 Init_Shift : Int;
7004 -- Endian-dependent start position for packing
7006 Shift : Int;
7007 -- Current insertion position
7009 Val : Int;
7010 -- Component of packed array being assembled.
7012 begin
7013 Comps := New_List;
7014 Val := 0;
7015 Packed_Num := 0;
7017 -- Account for endianness. See corresponding comment in
7018 -- Packed_Array_Aggregate_Handled concerning the following.
7020 if Bytes_Big_Endian
7021 xor Debug_Flag_8
7022 xor Reverse_Storage_Order (Base_Type (Typ))
7023 then
7024 Init_Shift := Byte_Size - Comp_Size;
7025 Incr := -Comp_Size;
7026 else
7027 Init_Shift := 0;
7028 Incr := +Comp_Size;
7029 end if;
7031 -- Iterate over each subaggregate
7033 Shift := Init_Shift;
7034 One_Dim := First (Expressions (N));
7035 while Present (One_Dim) loop
7036 One_Comp := First (Expressions (One_Dim));
7037 while Present (One_Comp) loop
7038 if Packed_Num = Byte_Size / Comp_Size then
7040 -- Byte is complete, add to list of expressions
7042 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7043 Val := 0;
7044 Shift := Init_Shift;
7045 Packed_Num := 0;
7047 else
7048 Comp_Val := Expr_Rep_Value (One_Comp);
7050 -- Adjust for bias, and strip proper number of bits
7052 if Has_Biased_Representation (Ctyp) then
7053 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
7054 end if;
7056 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
7057 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
7058 Shift := Shift + Incr;
7059 One_Comp := Next (One_Comp);
7060 Packed_Num := Packed_Num + 1;
7061 end if;
7062 end loop;
7064 One_Dim := Next (One_Dim);
7065 end loop;
7067 if Packed_Num > 0 then
7069 -- Add final incomplete byte if present
7071 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7072 end if;
7074 Rewrite (N,
7075 Unchecked_Convert_To (Typ,
7076 Make_Qualified_Expression (Loc,
7077 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
7078 Expression => Make_Aggregate (Loc, Expressions => Comps))));
7079 Analyze_And_Resolve (N);
7080 return True;
7081 end;
7082 end Two_Dim_Packed_Array_Handled;
7084 ---------------------
7085 -- Sort_Case_Table --
7086 ---------------------
7088 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
7089 L : constant Int := Case_Table'First;
7090 U : constant Int := Case_Table'Last;
7091 K : Int;
7092 J : Int;
7093 T : Case_Bounds;
7095 begin
7096 K := L;
7097 while K /= U loop
7098 T := Case_Table (K + 1);
7100 J := K + 1;
7101 while J /= L
7102 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
7103 Expr_Value (T.Choice_Lo)
7104 loop
7105 Case_Table (J) := Case_Table (J - 1);
7106 J := J - 1;
7107 end loop;
7109 Case_Table (J) := T;
7110 K := K + 1;
7111 end loop;
7112 end Sort_Case_Table;
7114 ----------------------------
7115 -- Static_Array_Aggregate --
7116 ----------------------------
7118 function Static_Array_Aggregate (N : Node_Id) return Boolean is
7119 Bounds : constant Node_Id := Aggregate_Bounds (N);
7121 Typ : constant Entity_Id := Etype (N);
7122 Comp_Type : constant Entity_Id := Component_Type (Typ);
7123 Agg : Node_Id;
7124 Expr : Node_Id;
7125 Lo : Node_Id;
7126 Hi : Node_Id;
7128 begin
7129 if Is_Tagged_Type (Typ)
7130 or else Is_Controlled (Typ)
7131 or else Is_Packed (Typ)
7132 then
7133 return False;
7134 end if;
7136 if Present (Bounds)
7137 and then Nkind (Bounds) = N_Range
7138 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
7139 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
7140 then
7141 Lo := Low_Bound (Bounds);
7142 Hi := High_Bound (Bounds);
7144 if No (Component_Associations (N)) then
7146 -- Verify that all components are static integers
7148 Expr := First (Expressions (N));
7149 while Present (Expr) loop
7150 if Nkind (Expr) /= N_Integer_Literal then
7151 return False;
7152 end if;
7154 Next (Expr);
7155 end loop;
7157 return True;
7159 else
7160 -- We allow only a single named association, either a static
7161 -- range or an others_clause, with a static expression.
7163 Expr := First (Component_Associations (N));
7165 if Present (Expressions (N)) then
7166 return False;
7168 elsif Present (Next (Expr)) then
7169 return False;
7171 elsif Present (Next (First (Choices (Expr)))) then
7172 return False;
7174 else
7175 -- The aggregate is static if all components are literals,
7176 -- or else all its components are static aggregates for the
7177 -- component type. We also limit the size of a static aggregate
7178 -- to prevent runaway static expressions.
7180 if Is_Array_Type (Comp_Type)
7181 or else Is_Record_Type (Comp_Type)
7182 then
7183 if Nkind (Expression (Expr)) /= N_Aggregate
7184 or else
7185 not Compile_Time_Known_Aggregate (Expression (Expr))
7186 then
7187 return False;
7188 end if;
7190 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
7191 return False;
7192 end if;
7194 if not Aggr_Size_OK (N, Typ) then
7195 return False;
7196 end if;
7198 -- Create a positional aggregate with the right number of
7199 -- copies of the expression.
7201 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
7203 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
7204 loop
7205 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
7207 -- The copied expression must be analyzed and resolved.
7208 -- Besides setting the type, this ensures that static
7209 -- expressions are appropriately marked as such.
7211 Analyze_And_Resolve
7212 (Last (Expressions (Agg)), Component_Type (Typ));
7213 end loop;
7215 Set_Aggregate_Bounds (Agg, Bounds);
7216 Set_Etype (Agg, Typ);
7217 Set_Analyzed (Agg);
7218 Rewrite (N, Agg);
7219 Set_Compile_Time_Known_Aggregate (N);
7221 return True;
7222 end if;
7223 end if;
7225 else
7226 return False;
7227 end if;
7228 end Static_Array_Aggregate;
7230 end Exp_Aggr;