2016-04-21 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / ada / exp_aggr.adb
blob19ecdad97453dd99ab8276ee99b2c325b5fe4559
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-2016, 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 In_Object_Declaration (N : Node_Id) return Boolean;
92 -- Return True if N is part of an object declaration, False otherwise
94 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean;
95 -- Returns true if N is an aggregate used to initialize the components
96 -- of a statically allocated dispatch table.
98 function Must_Slide
99 (Obj_Type : Entity_Id;
100 Typ : Entity_Id) return Boolean;
101 -- A static array aggregate in an object declaration can in most cases be
102 -- expanded in place. The one exception is when the aggregate is given
103 -- with component associations that specify different bounds from those of
104 -- the type definition in the object declaration. In this pathological
105 -- case the aggregate must slide, and we must introduce an intermediate
106 -- temporary to hold it.
108 -- The same holds in an assignment to one-dimensional array of arrays,
109 -- when a component may be given with bounds that differ from those of the
110 -- component type.
112 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
113 -- Sort the Case Table using the Lower Bound of each Choice as the key.
114 -- A simple insertion sort is used since the number of choices in a case
115 -- statement of variant part will usually be small and probably in near
116 -- sorted order.
118 ------------------------------------------------------
119 -- Local subprograms for Record Aggregate Expansion --
120 ------------------------------------------------------
122 function Build_Record_Aggr_Code
123 (N : Node_Id;
124 Typ : Entity_Id;
125 Lhs : Node_Id) return List_Id;
126 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
127 -- aggregate. Target is an expression containing the location on which the
128 -- component by component assignments will take place. Returns the list of
129 -- assignments plus all other adjustments needed for tagged and controlled
130 -- types.
132 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
133 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
134 -- aggregate (which can only be a record type, this procedure is only used
135 -- for record types). Transform the given aggregate into a sequence of
136 -- assignments performed component by component.
138 procedure Expand_Record_Aggregate
139 (N : Node_Id;
140 Orig_Tag : Node_Id := Empty;
141 Parent_Expr : Node_Id := Empty);
142 -- This is the top level procedure for record aggregate expansion.
143 -- Expansion for record aggregates needs expand aggregates for tagged
144 -- record types. Specifically Expand_Record_Aggregate adds the Tag
145 -- field in front of the Component_Association list that was created
146 -- during resolution by Resolve_Record_Aggregate.
148 -- N is the record aggregate node.
149 -- Orig_Tag is the value of the Tag that has to be provided for this
150 -- specific aggregate. It carries the tag corresponding to the type
151 -- of the outermost aggregate during the recursive expansion
152 -- Parent_Expr is the ancestor part of the original extension
153 -- aggregate
155 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
156 -- Return true if one of the components is of a discriminated type with
157 -- defaults. An aggregate for a type with mutable components must be
158 -- expanded into individual assignments.
160 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
161 -- If the type of the aggregate is a type extension with renamed discrimi-
162 -- nants, we must initialize the hidden discriminants of the parent.
163 -- Otherwise, the target object must not be initialized. The discriminants
164 -- are initialized by calling the initialization procedure for the type.
165 -- This is incorrect if the initialization of other components has any
166 -- side effects. We restrict this call to the case where the parent type
167 -- has a variant part, because this is the only case where the hidden
168 -- discriminants are accessed, namely when calling discriminant checking
169 -- functions of the parent type, and when applying a stream attribute to
170 -- an object of the derived type.
172 -----------------------------------------------------
173 -- Local Subprograms for Array Aggregate Expansion --
174 -----------------------------------------------------
176 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
177 -- Very large static aggregates present problems to the back-end, and are
178 -- transformed into assignments and loops. This function verifies that the
179 -- total number of components of an aggregate is acceptable for rewriting
180 -- into a purely positional static form. Aggr_Size_OK must be called before
181 -- calling Flatten.
183 -- This function also detects and warns about one-component aggregates that
184 -- appear in a non-static context. Even if the component value is static,
185 -- such an aggregate must be expanded into an assignment.
187 function Backend_Processing_Possible (N : Node_Id) return Boolean;
188 -- This function checks if array aggregate N can be processed directly
189 -- by the backend. If this is the case, True is returned.
191 function Build_Array_Aggr_Code
192 (N : Node_Id;
193 Ctype : Entity_Id;
194 Index : Node_Id;
195 Into : Node_Id;
196 Scalar_Comp : Boolean;
197 Indexes : List_Id := No_List) return List_Id;
198 -- This recursive routine returns a list of statements containing the
199 -- loops and assignments that are needed for the expansion of the array
200 -- aggregate N.
202 -- N is the (sub-)aggregate node to be expanded into code. This node has
203 -- been fully analyzed, and its Etype is properly set.
205 -- Index is the index node corresponding to the array subaggregate N
207 -- Into is the target expression into which we are copying the aggregate.
208 -- Note that this node may not have been analyzed yet, and so the Etype
209 -- field may not be set.
211 -- Scalar_Comp is True if the component type of the aggregate is scalar
213 -- Indexes is the current list of expressions used to index the object we
214 -- are writing into.
216 procedure Convert_Array_Aggr_In_Allocator
217 (Decl : Node_Id;
218 Aggr : Node_Id;
219 Target : Node_Id);
220 -- If the aggregate appears within an allocator and can be expanded in
221 -- place, this routine generates the individual assignments to components
222 -- of the designated object. This is an optimization over the general
223 -- case, where a temporary is first created on the stack and then used to
224 -- construct the allocated object on the heap.
226 procedure Convert_To_Positional
227 (N : Node_Id;
228 Max_Others_Replicate : Nat := 5;
229 Handle_Bit_Packed : Boolean := False);
230 -- If possible, convert named notation to positional notation. This
231 -- conversion is possible only in some static cases. If the conversion is
232 -- possible, then N is rewritten with the analyzed converted aggregate.
233 -- The parameter Max_Others_Replicate controls the maximum number of
234 -- values corresponding to an others choice that will be converted to
235 -- positional notation (the default of 5 is the normal limit, and reflects
236 -- the fact that normally the loop is better than a lot of separate
237 -- assignments). Note that this limit gets overridden in any case if
238 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
239 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
240 -- not expect the back end to handle bit packed arrays, so the normal case
241 -- of conversion is pointless), but in the special case of a call from
242 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
243 -- these are cases we handle in there.
245 -- It would seem useful to have a higher default for Max_Others_Replicate,
246 -- but aggregates in the compiler make this impossible: the compiler
247 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
248 -- is unexpected ???
250 procedure Expand_Array_Aggregate (N : Node_Id);
251 -- This is the top-level routine to perform array aggregate expansion.
252 -- N is the N_Aggregate node to be expanded.
254 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
255 -- For two-dimensional packed aggregates with constant bounds and constant
256 -- components, it is preferable to pack the inner aggregates because the
257 -- whole matrix can then be presented to the back-end as a one-dimensional
258 -- list of literals. This is much more efficient than expanding into single
259 -- component assignments. This function determines if the type Typ is for
260 -- an array that is suitable for this optimization: it returns True if Typ
261 -- is a two dimensional bit packed array with component size 1, 2, or 4.
263 function Late_Expansion
264 (N : Node_Id;
265 Typ : Entity_Id;
266 Target : Node_Id) return List_Id;
267 -- This routine implements top-down expansion of nested aggregates. In
268 -- doing so, it avoids the generation of temporaries at each level. N is
269 -- a nested record or array aggregate with the Expansion_Delayed flag.
270 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
271 -- expression that will hold the result of the aggregate expansion.
273 function Make_OK_Assignment_Statement
274 (Sloc : Source_Ptr;
275 Name : Node_Id;
276 Expression : Node_Id) return Node_Id;
277 -- This is like Make_Assignment_Statement, except that Assignment_OK
278 -- is set in the left operand. All assignments built by this unit use
279 -- this routine. This is needed to deal with assignments to initialized
280 -- constants that are done in place.
282 function Number_Of_Choices (N : Node_Id) return Nat;
283 -- Returns the number of discrete choices (not including the others choice
284 -- if present) contained in (sub-)aggregate N.
286 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
287 -- Given an array aggregate, this function handles the case of a packed
288 -- array aggregate with all constant values, where the aggregate can be
289 -- evaluated at compile time. If this is possible, then N is rewritten
290 -- to be its proper compile time value with all the components properly
291 -- assembled. The expression is analyzed and resolved and True is returned.
292 -- If this transformation is not possible, N is unchanged and False is
293 -- returned.
295 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
296 -- If the type of the aggregate is a two-dimensional bit_packed array
297 -- it may be transformed into an array of bytes with constant values,
298 -- and presented to the back-end as a static value. The function returns
299 -- false if this transformation cannot be performed. THis is similar to,
300 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
302 ------------------
303 -- Aggr_Size_OK --
304 ------------------
306 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
307 Lo : Node_Id;
308 Hi : Node_Id;
309 Indx : Node_Id;
310 Siz : Int;
311 Lov : Uint;
312 Hiv : Uint;
314 Max_Aggr_Size : Nat;
315 -- Determines the maximum size of an array aggregate produced by
316 -- converting named to positional notation (e.g. from others clauses).
317 -- This avoids running away with attempts to convert huge aggregates,
318 -- which hit memory limits in the backend.
320 function Component_Count (T : Entity_Id) return Nat;
321 -- The limit is applied to the total number of components that the
322 -- aggregate will have, which is the number of static expressions
323 -- that will appear in the flattened array. This requires a recursive
324 -- computation of the number of scalar components of the structure.
326 ---------------------
327 -- Component_Count --
328 ---------------------
330 function Component_Count (T : Entity_Id) return Nat is
331 Res : Nat := 0;
332 Comp : Entity_Id;
334 begin
335 if Is_Scalar_Type (T) then
336 return 1;
338 elsif Is_Record_Type (T) then
339 Comp := First_Component (T);
340 while Present (Comp) loop
341 Res := Res + Component_Count (Etype (Comp));
342 Next_Component (Comp);
343 end loop;
345 return Res;
347 elsif Is_Array_Type (T) then
348 declare
349 Lo : constant Node_Id :=
350 Type_Low_Bound (Etype (First_Index (T)));
351 Hi : constant Node_Id :=
352 Type_High_Bound (Etype (First_Index (T)));
354 Siz : constant Nat := Component_Count (Component_Type (T));
356 begin
357 -- Check for superflat arrays, i.e. arrays with such bounds
358 -- as 4 .. 2, to insure that this function never returns a
359 -- meaningless negative value.
361 if not Compile_Time_Known_Value (Lo)
362 or else not Compile_Time_Known_Value (Hi)
363 or else Expr_Value (Hi) < Expr_Value (Lo)
364 then
365 return 0;
367 else
368 return
369 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
370 end if;
371 end;
373 else
374 -- Can only be a null for an access type
376 return 1;
377 end if;
378 end Component_Count;
380 -- Start of processing for Aggr_Size_OK
382 begin
383 -- The normal aggregate limit is 50000, but we increase this limit to
384 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
385 -- Restrictions (No_Implicit_Loops) is specified, since in either case
386 -- we are at risk of declaring the program illegal because of this
387 -- limit. We also increase the limit when Static_Elaboration_Desired,
388 -- given that this means that objects are intended to be placed in data
389 -- memory.
391 -- We also increase the limit if the aggregate is for a packed two-
392 -- dimensional array, because if components are static it is much more
393 -- efficient to construct a one-dimensional equivalent array with static
394 -- components.
396 -- Conversely, we decrease the maximum size if none of the above
397 -- requirements apply, and if the aggregate has a single component
398 -- association, which will be more efficient if implemented with a loop.
400 -- Finally, we use a small limit in CodePeer mode where we favor loops
401 -- instead of thousands of single assignments (from large aggregates).
403 Max_Aggr_Size := 50000;
405 if CodePeer_Mode then
406 Max_Aggr_Size := 100;
408 elsif Restriction_Active (No_Elaboration_Code)
409 or else Restriction_Active (No_Implicit_Loops)
410 or else Is_Two_Dim_Packed_Array (Typ)
411 or else (Ekind (Current_Scope) = E_Package
412 and then Static_Elaboration_Desired (Current_Scope))
413 then
414 Max_Aggr_Size := 2 ** 24;
416 elsif No (Expressions (N))
417 and then No (Next (First (Component_Associations (N))))
418 then
419 Max_Aggr_Size := 5000;
420 end if;
422 Siz := Component_Count (Component_Type (Typ));
424 Indx := First_Index (Typ);
425 while Present (Indx) loop
426 Lo := Type_Low_Bound (Etype (Indx));
427 Hi := Type_High_Bound (Etype (Indx));
429 -- Bounds need to be known at compile time
431 if not Compile_Time_Known_Value (Lo)
432 or else not Compile_Time_Known_Value (Hi)
433 then
434 return False;
435 end if;
437 Lov := Expr_Value (Lo);
438 Hiv := Expr_Value (Hi);
440 -- A flat array is always safe
442 if Hiv < Lov then
443 return True;
444 end if;
446 -- One-component aggregates are suspicious, and if the context type
447 -- is an object declaration with non-static bounds it will trip gcc;
448 -- such an aggregate must be expanded into a single assignment.
450 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
451 declare
452 Index_Type : constant Entity_Id :=
453 Etype
454 (First_Index (Etype (Defining_Identifier (Parent (N)))));
455 Indx : Node_Id;
457 begin
458 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
459 or else not Compile_Time_Known_Value
460 (Type_High_Bound (Index_Type))
461 then
462 if Present (Component_Associations (N)) then
463 Indx :=
464 First (Choices (First (Component_Associations (N))));
466 if Is_Entity_Name (Indx)
467 and then not Is_Type (Entity (Indx))
468 then
469 Error_Msg_N
470 ("single component aggregate in "
471 & "non-static context??", Indx);
472 Error_Msg_N ("\maybe subtype name was meant??", Indx);
473 end if;
474 end if;
476 return False;
477 end if;
478 end;
479 end if;
481 declare
482 Rng : constant Uint := Hiv - Lov + 1;
484 begin
485 -- Check if size is too large
487 if not UI_Is_In_Int_Range (Rng) then
488 return False;
489 end if;
491 Siz := Siz * UI_To_Int (Rng);
492 end;
494 if Siz <= 0
495 or else Siz > Max_Aggr_Size
496 then
497 return False;
498 end if;
500 -- Bounds must be in integer range, for later array construction
502 if not UI_Is_In_Int_Range (Lov)
503 or else
504 not UI_Is_In_Int_Range (Hiv)
505 then
506 return False;
507 end if;
509 Next_Index (Indx);
510 end loop;
512 return True;
513 end Aggr_Size_OK;
515 ---------------------------------
516 -- Backend_Processing_Possible --
517 ---------------------------------
519 -- Backend processing by Gigi/gcc is possible only if all the following
520 -- conditions are met:
522 -- 1. N is fully positional
524 -- 2. N is not a bit-packed array aggregate;
526 -- 3. The size of N's array type must be known at compile time. Note
527 -- that this implies that the component size is also known
529 -- 4. The array type of N does not follow the Fortran layout convention
530 -- or if it does it must be 1 dimensional.
532 -- 5. The array component type may not be tagged (which could necessitate
533 -- reassignment of proper tags).
535 -- 6. The array component type must not have unaligned bit components
537 -- 7. None of the components of the aggregate may be bit unaligned
538 -- components.
540 -- 8. There cannot be delayed components, since we do not know enough
541 -- at this stage to know if back end processing is possible.
543 -- 9. There cannot be any discriminated record components, since the
544 -- back end cannot handle this complex case.
546 -- 10. No controlled actions need to be generated for components
548 -- 11. When generating C code, N must be part of a N_Object_Declaration
550 -- 12. When generating C code, N must not include function calls
552 function Backend_Processing_Possible (N : Node_Id) return Boolean is
553 Typ : constant Entity_Id := Etype (N);
554 -- Typ is the correct constrained array subtype of the aggregate
556 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
557 -- This routine checks components of aggregate N, enforcing checks
558 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
559 -- are performed on subaggregates. The Index value is the current index
560 -- being checked in the multidimensional case.
562 ---------------------
563 -- Component_Check --
564 ---------------------
566 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
567 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
568 -- Given a type conversion or an unchecked type conversion N, return
569 -- its innermost original expression.
571 ----------------------------------
572 -- Ultimate_Original_Expression --
573 ----------------------------------
575 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
576 Expr : Node_Id := Original_Node (N);
578 begin
579 while Nkind_In (Expr, N_Type_Conversion,
580 N_Unchecked_Type_Conversion)
581 loop
582 Expr := Original_Node (Expression (Expr));
583 end loop;
585 return Expr;
586 end Ultimate_Original_Expression;
588 -- Local variables
590 Expr : Node_Id;
592 -- Start of processing for Component_Check
594 begin
595 -- Checks 1: (no component associations)
597 if Present (Component_Associations (N)) then
598 return False;
599 end if;
601 -- Checks 11: (part of an object declaration)
603 if Modify_Tree_For_C
604 and then Nkind (Parent (N)) /= N_Object_Declaration
605 and then
606 (Nkind (Parent (N)) /= N_Qualified_Expression
607 or else Nkind (Parent (Parent (N))) /= N_Object_Declaration)
608 then
609 return False;
610 end if;
612 -- Checks on components
614 -- Recurse to check subaggregates, which may appear in qualified
615 -- expressions. If delayed, the front-end will have to expand.
616 -- If the component is a discriminated record, treat as non-static,
617 -- as the back-end cannot handle this properly.
619 Expr := First (Expressions (N));
620 while Present (Expr) loop
622 -- Checks 8: (no delayed components)
624 if Is_Delayed_Aggregate (Expr) then
625 return False;
626 end if;
628 -- Checks 9: (no discriminated records)
630 if Present (Etype (Expr))
631 and then Is_Record_Type (Etype (Expr))
632 and then Has_Discriminants (Etype (Expr))
633 then
634 return False;
635 end if;
637 -- Checks 7. Component must not be bit aligned component
639 if Possible_Bit_Aligned_Component (Expr) then
640 return False;
641 end if;
643 -- Checks 12: (no function call)
645 if Modify_Tree_For_C
646 and then
647 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
648 then
649 return False;
650 end if;
652 -- Recursion to following indexes for multiple dimension case
654 if Present (Next_Index (Index))
655 and then not Component_Check (Expr, Next_Index (Index))
656 then
657 return False;
658 end if;
660 -- All checks for that component finished, on to next
662 Next (Expr);
663 end loop;
665 return True;
666 end Component_Check;
668 -- Start of processing for Backend_Processing_Possible
670 begin
671 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
673 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
674 return False;
675 end if;
677 -- If component is limited, aggregate must be expanded because each
678 -- component assignment must be built in place.
680 if Is_Limited_View (Component_Type (Typ)) then
681 return False;
682 end if;
684 -- Checks 4 (array must not be multidimensional Fortran case)
686 if Convention (Typ) = Convention_Fortran
687 and then Number_Dimensions (Typ) > 1
688 then
689 return False;
690 end if;
692 -- Checks 3 (size of array must be known at compile time)
694 if not Size_Known_At_Compile_Time (Typ) then
695 return False;
696 end if;
698 -- Checks on components
700 if not Component_Check (N, First_Index (Typ)) then
701 return False;
702 end if;
704 -- Checks 5 (if the component type is tagged, then we may need to do
705 -- tag adjustments. Perhaps this should be refined to check for any
706 -- component associations that actually need tag adjustment, similar
707 -- to the test in Component_Not_OK_For_Backend for record aggregates
708 -- with tagged components, but not clear whether it's worthwhile ???;
709 -- in the case of virtual machines (no Tagged_Type_Expansion), object
710 -- tags are handled implicitly).
712 if Is_Tagged_Type (Component_Type (Typ))
713 and then Tagged_Type_Expansion
714 then
715 return False;
716 end if;
718 -- Checks 6 (component type must not have bit aligned components)
720 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
721 return False;
722 end if;
724 -- Backend processing is possible
726 Set_Size_Known_At_Compile_Time (Etype (N), True);
727 return True;
728 end Backend_Processing_Possible;
730 ---------------------------
731 -- Build_Array_Aggr_Code --
732 ---------------------------
734 -- The code that we generate from a one dimensional aggregate is
736 -- 1. If the subaggregate contains discrete choices we
738 -- (a) Sort the discrete choices
740 -- (b) Otherwise for each discrete choice that specifies a range we
741 -- emit a loop. If a range specifies a maximum of three values, or
742 -- we are dealing with an expression we emit a sequence of
743 -- assignments instead of a loop.
745 -- (c) Generate the remaining loops to cover the others choice if any
747 -- 2. If the aggregate contains positional elements we
749 -- (a) translate the positional elements in a series of assignments
751 -- (b) Generate a final loop to cover the others choice if any.
752 -- Note that this final loop has to be a while loop since the case
754 -- L : Integer := Integer'Last;
755 -- H : Integer := Integer'Last;
756 -- A : array (L .. H) := (1, others =>0);
758 -- cannot be handled by a for loop. Thus for the following
760 -- array (L .. H) := (.. positional elements.., others =>E);
762 -- we always generate something like:
764 -- J : Index_Type := Index_Of_Last_Positional_Element;
765 -- while J < H loop
766 -- J := Index_Base'Succ (J)
767 -- Tmp (J) := E;
768 -- end loop;
770 function Build_Array_Aggr_Code
771 (N : Node_Id;
772 Ctype : Entity_Id;
773 Index : Node_Id;
774 Into : Node_Id;
775 Scalar_Comp : Boolean;
776 Indexes : List_Id := No_List) return List_Id
778 Loc : constant Source_Ptr := Sloc (N);
779 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
780 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
781 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
783 function Add (Val : Int; To : Node_Id) return Node_Id;
784 -- Returns an expression where Val is added to expression To, unless
785 -- To+Val is provably out of To's base type range. To must be an
786 -- already analyzed expression.
788 function Empty_Range (L, H : Node_Id) return Boolean;
789 -- Returns True if the range defined by L .. H is certainly empty
791 function Equal (L, H : Node_Id) return Boolean;
792 -- Returns True if L = H for sure
794 function Index_Base_Name return Node_Id;
795 -- Returns a new reference to the index type name
797 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id;
798 -- Ind must be a side-effect-free expression. If the input aggregate N
799 -- to Build_Loop contains no subaggregates, then this function returns
800 -- the assignment statement:
802 -- Into (Indexes, Ind) := Expr;
804 -- Otherwise we call Build_Code recursively
806 -- Ada 2005 (AI-287): In case of default initialized component, Expr
807 -- is empty and we generate a call to the corresponding IP subprogram.
809 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
810 -- Nodes L and H must be side-effect-free expressions. If the input
811 -- aggregate N to Build_Loop contains no subaggregates, this routine
812 -- returns the for loop statement:
814 -- for J in Index_Base'(L) .. Index_Base'(H) loop
815 -- Into (Indexes, J) := Expr;
816 -- end loop;
818 -- Otherwise we call Build_Code recursively.
819 -- As an optimization if the loop covers 3 or fewer scalar elements we
820 -- generate a sequence of assignments.
822 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
823 -- Nodes L and H must be side-effect-free expressions. If the input
824 -- aggregate N to Build_Loop contains no subaggregates, this routine
825 -- returns the while loop statement:
827 -- J : Index_Base := L;
828 -- while J < H loop
829 -- J := Index_Base'Succ (J);
830 -- Into (Indexes, J) := Expr;
831 -- end loop;
833 -- Otherwise we call Build_Code recursively
835 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
836 -- For an association with a box, use value given by aspect
837 -- Default_Component_Value of array type if specified, else use
838 -- value given by aspect Default_Value for component type itself
839 -- if specified, else return Empty.
841 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
842 function Local_Expr_Value (E : Node_Id) return Uint;
843 -- These two Local routines are used to replace the corresponding ones
844 -- in sem_eval because while processing the bounds of an aggregate with
845 -- discrete choices whose index type is an enumeration, we build static
846 -- expressions not recognized by Compile_Time_Known_Value as such since
847 -- they have not yet been analyzed and resolved. All the expressions in
848 -- question are things like Index_Base_Name'Val (Const) which we can
849 -- easily recognize as being constant.
851 ---------
852 -- Add --
853 ---------
855 function Add (Val : Int; To : Node_Id) return Node_Id is
856 Expr_Pos : Node_Id;
857 Expr : Node_Id;
858 To_Pos : Node_Id;
859 U_To : Uint;
860 U_Val : constant Uint := UI_From_Int (Val);
862 begin
863 -- Note: do not try to optimize the case of Val = 0, because
864 -- we need to build a new node with the proper Sloc value anyway.
866 -- First test if we can do constant folding
868 if Local_Compile_Time_Known_Value (To) then
869 U_To := Local_Expr_Value (To) + Val;
871 -- Determine if our constant is outside the range of the index.
872 -- If so return an Empty node. This empty node will be caught
873 -- by Empty_Range below.
875 if Compile_Time_Known_Value (Index_Base_L)
876 and then U_To < Expr_Value (Index_Base_L)
877 then
878 return Empty;
880 elsif Compile_Time_Known_Value (Index_Base_H)
881 and then U_To > Expr_Value (Index_Base_H)
882 then
883 return Empty;
884 end if;
886 Expr_Pos := Make_Integer_Literal (Loc, U_To);
887 Set_Is_Static_Expression (Expr_Pos);
889 if not Is_Enumeration_Type (Index_Base) then
890 Expr := Expr_Pos;
892 -- If we are dealing with enumeration return
893 -- Index_Base'Val (Expr_Pos)
895 else
896 Expr :=
897 Make_Attribute_Reference
898 (Loc,
899 Prefix => Index_Base_Name,
900 Attribute_Name => Name_Val,
901 Expressions => New_List (Expr_Pos));
902 end if;
904 return Expr;
905 end if;
907 -- If we are here no constant folding possible
909 if not Is_Enumeration_Type (Index_Base) then
910 Expr :=
911 Make_Op_Add (Loc,
912 Left_Opnd => Duplicate_Subexpr (To),
913 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
915 -- If we are dealing with enumeration return
916 -- Index_Base'Val (Index_Base'Pos (To) + Val)
918 else
919 To_Pos :=
920 Make_Attribute_Reference
921 (Loc,
922 Prefix => Index_Base_Name,
923 Attribute_Name => Name_Pos,
924 Expressions => New_List (Duplicate_Subexpr (To)));
926 Expr_Pos :=
927 Make_Op_Add (Loc,
928 Left_Opnd => To_Pos,
929 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
931 Expr :=
932 Make_Attribute_Reference
933 (Loc,
934 Prefix => Index_Base_Name,
935 Attribute_Name => Name_Val,
936 Expressions => New_List (Expr_Pos));
937 end if;
939 return Expr;
940 end Add;
942 -----------------
943 -- Empty_Range --
944 -----------------
946 function Empty_Range (L, H : Node_Id) return Boolean is
947 Is_Empty : Boolean := False;
948 Low : Node_Id;
949 High : Node_Id;
951 begin
952 -- First check if L or H were already detected as overflowing the
953 -- index base range type by function Add above. If this is so Add
954 -- returns the empty node.
956 if No (L) or else No (H) then
957 return True;
958 end if;
960 for J in 1 .. 3 loop
961 case J is
963 -- L > H range is empty
965 when 1 =>
966 Low := L;
967 High := H;
969 -- B_L > H range must be empty
971 when 2 =>
972 Low := Index_Base_L;
973 High := H;
975 -- L > B_H range must be empty
977 when 3 =>
978 Low := L;
979 High := Index_Base_H;
980 end case;
982 if Local_Compile_Time_Known_Value (Low)
983 and then
984 Local_Compile_Time_Known_Value (High)
985 then
986 Is_Empty :=
987 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
988 end if;
990 exit when Is_Empty;
991 end loop;
993 return Is_Empty;
994 end Empty_Range;
996 -----------
997 -- Equal --
998 -----------
1000 function Equal (L, H : Node_Id) return Boolean is
1001 begin
1002 if L = H then
1003 return True;
1005 elsif Local_Compile_Time_Known_Value (L)
1006 and then
1007 Local_Compile_Time_Known_Value (H)
1008 then
1009 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1010 end if;
1012 return False;
1013 end Equal;
1015 ----------------
1016 -- Gen_Assign --
1017 ----------------
1019 function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is
1020 L : constant List_Id := New_List;
1021 A : Node_Id;
1023 New_Indexes : List_Id;
1024 Indexed_Comp : Node_Id;
1025 Expr_Q : Node_Id;
1026 Comp_Type : Entity_Id := Empty;
1028 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1029 -- Collect insert_actions generated in the construction of a
1030 -- loop, and prepend them to the sequence of assignments to
1031 -- complete the eventual body of the loop.
1033 ----------------------
1034 -- Add_Loop_Actions --
1035 ----------------------
1037 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1038 Res : List_Id;
1040 begin
1041 -- Ada 2005 (AI-287): Do nothing else in case of default
1042 -- initialized component.
1044 if No (Expr) then
1045 return Lis;
1047 elsif Nkind (Parent (Expr)) = N_Component_Association
1048 and then Present (Loop_Actions (Parent (Expr)))
1049 then
1050 Append_List (Lis, Loop_Actions (Parent (Expr)));
1051 Res := Loop_Actions (Parent (Expr));
1052 Set_Loop_Actions (Parent (Expr), No_List);
1053 return Res;
1055 else
1056 return Lis;
1057 end if;
1058 end Add_Loop_Actions;
1060 -- Start of processing for Gen_Assign
1062 begin
1063 if No (Indexes) then
1064 New_Indexes := New_List;
1065 else
1066 New_Indexes := New_Copy_List_Tree (Indexes);
1067 end if;
1069 Append_To (New_Indexes, Ind);
1071 if Present (Next_Index (Index)) then
1072 return
1073 Add_Loop_Actions (
1074 Build_Array_Aggr_Code
1075 (N => Expr,
1076 Ctype => Ctype,
1077 Index => Next_Index (Index),
1078 Into => Into,
1079 Scalar_Comp => Scalar_Comp,
1080 Indexes => New_Indexes));
1081 end if;
1083 -- If we get here then we are at a bottom-level (sub-)aggregate
1085 Indexed_Comp :=
1086 Checks_Off
1087 (Make_Indexed_Component (Loc,
1088 Prefix => New_Copy_Tree (Into),
1089 Expressions => New_Indexes));
1091 Set_Assignment_OK (Indexed_Comp);
1093 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1094 -- is not present (and therefore we also initialize Expr_Q to empty).
1096 if No (Expr) then
1097 Expr_Q := Empty;
1098 elsif Nkind (Expr) = N_Qualified_Expression then
1099 Expr_Q := Expression (Expr);
1100 else
1101 Expr_Q := Expr;
1102 end if;
1104 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1105 Comp_Type := Component_Type (Etype (N));
1106 pragma Assert (Comp_Type = Ctype); -- AI-287
1108 elsif Present (Next (First (New_Indexes))) then
1110 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1111 -- component because we have received the component type in
1112 -- the formal parameter Ctype.
1114 -- ??? Some assert pragmas have been added to check if this new
1115 -- formal can be used to replace this code in all cases.
1117 if Present (Expr) then
1119 -- This is a multidimensional array. Recover the component type
1120 -- from the outermost aggregate, because subaggregates do not
1121 -- have an assigned type.
1123 declare
1124 P : Node_Id;
1126 begin
1127 P := Parent (Expr);
1128 while Present (P) loop
1129 if Nkind (P) = N_Aggregate
1130 and then Present (Etype (P))
1131 then
1132 Comp_Type := Component_Type (Etype (P));
1133 exit;
1135 else
1136 P := Parent (P);
1137 end if;
1138 end loop;
1140 pragma Assert (Comp_Type = Ctype); -- AI-287
1141 end;
1142 end if;
1143 end if;
1145 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1146 -- default initialized components (otherwise Expr_Q is not present).
1148 if Present (Expr_Q)
1149 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1150 then
1151 -- At this stage the Expression may not have been analyzed yet
1152 -- because the array aggregate code has not been updated to use
1153 -- the Expansion_Delayed flag and avoid analysis altogether to
1154 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1155 -- the analysis of non-array aggregates now in order to get the
1156 -- value of Expansion_Delayed flag for the inner aggregate ???
1158 if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then
1159 Analyze_And_Resolve (Expr_Q, Comp_Type);
1160 end if;
1162 if Is_Delayed_Aggregate (Expr_Q) then
1164 -- This is either a subaggregate of a multidimensional array,
1165 -- or a component of an array type whose component type is
1166 -- also an array. In the latter case, the expression may have
1167 -- component associations that provide different bounds from
1168 -- those of the component type, and sliding must occur. Instead
1169 -- of decomposing the current aggregate assignment, force the
1170 -- re-analysis of the assignment, so that a temporary will be
1171 -- generated in the usual fashion, and sliding will take place.
1173 if Nkind (Parent (N)) = N_Assignment_Statement
1174 and then Is_Array_Type (Comp_Type)
1175 and then Present (Component_Associations (Expr_Q))
1176 and then Must_Slide (Comp_Type, Etype (Expr_Q))
1177 then
1178 Set_Expansion_Delayed (Expr_Q, False);
1179 Set_Analyzed (Expr_Q, False);
1181 else
1182 return
1183 Add_Loop_Actions (
1184 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1185 end if;
1186 end if;
1187 end if;
1189 -- Ada 2005 (AI-287): In case of default initialized component, call
1190 -- the initialization subprogram associated with the component type.
1191 -- If the component type is an access type, add an explicit null
1192 -- assignment, because for the back-end there is an initialization
1193 -- present for the whole aggregate, and no default initialization
1194 -- will take place.
1196 -- In addition, if the component type is controlled, we must call
1197 -- its Initialize procedure explicitly, because there is no explicit
1198 -- object creation that will invoke it otherwise.
1200 if No (Expr) then
1201 if Present (Base_Init_Proc (Base_Type (Ctype)))
1202 or else Has_Task (Base_Type (Ctype))
1203 then
1204 Append_List_To (L,
1205 Build_Initialization_Call (Loc,
1206 Id_Ref => Indexed_Comp,
1207 Typ => Ctype,
1208 With_Default_Init => True));
1210 -- If the component type has invariants, add an invariant
1211 -- check after the component is default-initialized. It will
1212 -- be analyzed and resolved before the code for initialization
1213 -- of other components.
1215 if Has_Invariants (Ctype) then
1216 Set_Etype (Indexed_Comp, Ctype);
1217 Append_To (L, Make_Invariant_Call (Indexed_Comp));
1218 end if;
1220 elsif Is_Access_Type (Ctype) then
1221 Append_To (L,
1222 Make_Assignment_Statement (Loc,
1223 Name => Indexed_Comp,
1224 Expression => Make_Null (Loc)));
1225 end if;
1227 if Needs_Finalization (Ctype) then
1228 Append_To (L,
1229 Make_Init_Call
1230 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1231 Typ => Ctype));
1232 end if;
1234 else
1235 A :=
1236 Make_OK_Assignment_Statement (Loc,
1237 Name => Indexed_Comp,
1238 Expression => New_Copy_Tree (Expr));
1240 -- The target of the assignment may not have been initialized,
1241 -- so it is not possible to call Finalize as expected in normal
1242 -- controlled assignments. We must also avoid using the primitive
1243 -- _assign (which depends on a valid target, and may for example
1244 -- perform discriminant checks on it).
1246 -- Both Finalize and usage of _assign are disabled by setting
1247 -- No_Ctrl_Actions on the assignment. The rest of the controlled
1248 -- actions are done manually with the proper finalization list
1249 -- coming from the context.
1251 Set_No_Ctrl_Actions (A);
1253 -- If this is an aggregate for an array of arrays, each
1254 -- subaggregate will be expanded as well, and even with
1255 -- No_Ctrl_Actions the assignments of inner components will
1256 -- require attachment in their assignments to temporaries. These
1257 -- temporaries must be finalized for each subaggregate, to prevent
1258 -- multiple attachments of the same temporary location to same
1259 -- finalization chain (and consequently circular lists). To ensure
1260 -- that finalization takes place for each subaggregate we wrap the
1261 -- assignment in a block.
1263 if Present (Comp_Type)
1264 and then Needs_Finalization (Comp_Type)
1265 and then Is_Array_Type (Comp_Type)
1266 and then Present (Expr)
1267 then
1268 A :=
1269 Make_Block_Statement (Loc,
1270 Handled_Statement_Sequence =>
1271 Make_Handled_Sequence_Of_Statements (Loc,
1272 Statements => New_List (A)));
1273 end if;
1275 Append_To (L, A);
1277 -- Adjust the tag if tagged (because of possible view
1278 -- conversions), unless compiling for a VM where tags
1279 -- are implicit.
1281 if Present (Comp_Type)
1282 and then Is_Tagged_Type (Comp_Type)
1283 and then Tagged_Type_Expansion
1284 then
1285 declare
1286 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Type);
1288 begin
1289 A :=
1290 Make_OK_Assignment_Statement (Loc,
1291 Name =>
1292 Make_Selected_Component (Loc,
1293 Prefix => New_Copy_Tree (Indexed_Comp),
1294 Selector_Name =>
1295 New_Occurrence_Of
1296 (First_Tag_Component (Full_Typ), Loc)),
1298 Expression =>
1299 Unchecked_Convert_To (RTE (RE_Tag),
1300 New_Occurrence_Of
1301 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1302 Loc)));
1304 Append_To (L, A);
1305 end;
1306 end if;
1308 -- Adjust and attach the component to the proper final list, which
1309 -- can be the controller of the outer record object or the final
1310 -- list associated with the scope.
1312 -- If the component is itself an array of controlled types, whose
1313 -- value is given by a subaggregate, then the attach calls have
1314 -- been generated when individual subcomponent are assigned, and
1315 -- must not be done again to prevent malformed finalization chains
1316 -- (see comments above, concerning the creation of a block to hold
1317 -- inner finalization actions).
1319 if Present (Comp_Type)
1320 and then Needs_Finalization (Comp_Type)
1321 and then not Is_Limited_Type (Comp_Type)
1322 and then not
1323 (Is_Array_Type (Comp_Type)
1324 and then Is_Controlled (Component_Type (Comp_Type))
1325 and then Nkind (Expr) = N_Aggregate)
1326 then
1327 Append_To (L,
1328 Make_Adjust_Call
1329 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1330 Typ => Comp_Type));
1331 end if;
1332 end if;
1334 return Add_Loop_Actions (L);
1335 end Gen_Assign;
1337 --------------
1338 -- Gen_Loop --
1339 --------------
1341 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1342 L_J : Node_Id;
1344 L_L : Node_Id;
1345 -- Index_Base'(L)
1347 L_H : Node_Id;
1348 -- Index_Base'(H)
1350 L_Range : Node_Id;
1351 -- Index_Base'(L) .. Index_Base'(H)
1353 L_Iteration_Scheme : Node_Id;
1354 -- L_J in Index_Base'(L) .. Index_Base'(H)
1356 L_Body : List_Id;
1357 -- The statements to execute in the loop
1359 S : constant List_Id := New_List;
1360 -- List of statements
1362 Tcopy : Node_Id;
1363 -- Copy of expression tree, used for checking purposes
1365 begin
1366 -- If loop bounds define an empty range return the null statement
1368 if Empty_Range (L, H) then
1369 Append_To (S, Make_Null_Statement (Loc));
1371 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1372 -- default initialized component.
1374 if No (Expr) then
1375 null;
1377 else
1378 -- The expression must be type-checked even though no component
1379 -- of the aggregate will have this value. This is done only for
1380 -- actual components of the array, not for subaggregates. Do
1381 -- the check on a copy, because the expression may be shared
1382 -- among several choices, some of which might be non-null.
1384 if Present (Etype (N))
1385 and then Is_Array_Type (Etype (N))
1386 and then No (Next_Index (Index))
1387 then
1388 Expander_Mode_Save_And_Set (False);
1389 Tcopy := New_Copy_Tree (Expr);
1390 Set_Parent (Tcopy, N);
1391 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1392 Expander_Mode_Restore;
1393 end if;
1394 end if;
1396 return S;
1398 -- If loop bounds are the same then generate an assignment
1400 elsif Equal (L, H) then
1401 return Gen_Assign (New_Copy_Tree (L), Expr);
1403 -- If H - L <= 2 then generate a sequence of assignments when we are
1404 -- processing the bottom most aggregate and it contains scalar
1405 -- components.
1407 elsif No (Next_Index (Index))
1408 and then Scalar_Comp
1409 and then Local_Compile_Time_Known_Value (L)
1410 and then Local_Compile_Time_Known_Value (H)
1411 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1412 then
1414 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1415 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1417 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1418 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1419 end if;
1421 return S;
1422 end if;
1424 -- Otherwise construct the loop, starting with the loop index L_J
1426 L_J := Make_Temporary (Loc, 'J', L);
1428 -- Construct "L .. H" in Index_Base. We use a qualified expression
1429 -- for the bound to convert to the index base, but we don't need
1430 -- to do that if we already have the base type at hand.
1432 if Etype (L) = Index_Base then
1433 L_L := L;
1434 else
1435 L_L :=
1436 Make_Qualified_Expression (Loc,
1437 Subtype_Mark => Index_Base_Name,
1438 Expression => L);
1439 end if;
1441 if Etype (H) = Index_Base then
1442 L_H := H;
1443 else
1444 L_H :=
1445 Make_Qualified_Expression (Loc,
1446 Subtype_Mark => Index_Base_Name,
1447 Expression => H);
1448 end if;
1450 L_Range :=
1451 Make_Range (Loc,
1452 Low_Bound => L_L,
1453 High_Bound => L_H);
1455 -- Construct "for L_J in Index_Base range L .. H"
1457 L_Iteration_Scheme :=
1458 Make_Iteration_Scheme
1459 (Loc,
1460 Loop_Parameter_Specification =>
1461 Make_Loop_Parameter_Specification
1462 (Loc,
1463 Defining_Identifier => L_J,
1464 Discrete_Subtype_Definition => L_Range));
1466 -- Construct the statements to execute in the loop body
1468 L_Body := Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr);
1470 -- Construct the final loop
1472 Append_To (S,
1473 Make_Implicit_Loop_Statement
1474 (Node => N,
1475 Identifier => Empty,
1476 Iteration_Scheme => L_Iteration_Scheme,
1477 Statements => L_Body));
1479 -- A small optimization: if the aggregate is initialized with a box
1480 -- and the component type has no initialization procedure, remove the
1481 -- useless empty loop.
1483 if Nkind (First (S)) = N_Loop_Statement
1484 and then Is_Empty_List (Statements (First (S)))
1485 then
1486 return New_List (Make_Null_Statement (Loc));
1487 else
1488 return S;
1489 end if;
1490 end Gen_Loop;
1492 ---------------
1493 -- Gen_While --
1494 ---------------
1496 -- The code built is
1498 -- W_J : Index_Base := L;
1499 -- while W_J < H loop
1500 -- W_J := Index_Base'Succ (W);
1501 -- L_Body;
1502 -- end loop;
1504 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1505 W_J : Node_Id;
1507 W_Decl : Node_Id;
1508 -- W_J : Base_Type := L;
1510 W_Iteration_Scheme : Node_Id;
1511 -- while W_J < H
1513 W_Index_Succ : Node_Id;
1514 -- Index_Base'Succ (J)
1516 W_Increment : Node_Id;
1517 -- W_J := Index_Base'Succ (W)
1519 W_Body : constant List_Id := New_List;
1520 -- The statements to execute in the loop
1522 S : constant List_Id := New_List;
1523 -- list of statement
1525 begin
1526 -- If loop bounds define an empty range or are equal return null
1528 if Empty_Range (L, H) or else Equal (L, H) then
1529 Append_To (S, Make_Null_Statement (Loc));
1530 return S;
1531 end if;
1533 -- Build the decl of W_J
1535 W_J := Make_Temporary (Loc, 'J', L);
1536 W_Decl :=
1537 Make_Object_Declaration
1538 (Loc,
1539 Defining_Identifier => W_J,
1540 Object_Definition => Index_Base_Name,
1541 Expression => L);
1543 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1544 -- that in this particular case L is a fresh Expr generated by
1545 -- Add which we are the only ones to use.
1547 Append_To (S, W_Decl);
1549 -- Construct " while W_J < H"
1551 W_Iteration_Scheme :=
1552 Make_Iteration_Scheme
1553 (Loc,
1554 Condition => Make_Op_Lt
1555 (Loc,
1556 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1557 Right_Opnd => New_Copy_Tree (H)));
1559 -- Construct the statements to execute in the loop body
1561 W_Index_Succ :=
1562 Make_Attribute_Reference
1563 (Loc,
1564 Prefix => Index_Base_Name,
1565 Attribute_Name => Name_Succ,
1566 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1568 W_Increment :=
1569 Make_OK_Assignment_Statement
1570 (Loc,
1571 Name => New_Occurrence_Of (W_J, Loc),
1572 Expression => W_Index_Succ);
1574 Append_To (W_Body, W_Increment);
1575 Append_List_To (W_Body,
1576 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr));
1578 -- Construct the final loop
1580 Append_To (S,
1581 Make_Implicit_Loop_Statement
1582 (Node => N,
1583 Identifier => Empty,
1584 Iteration_Scheme => W_Iteration_Scheme,
1585 Statements => W_Body));
1587 return S;
1588 end Gen_While;
1590 --------------------
1591 -- Get_Assoc_Expr --
1592 --------------------
1594 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1595 Typ : constant Entity_Id := Base_Type (Etype (N));
1597 begin
1598 if Box_Present (Assoc) then
1599 if Is_Scalar_Type (Ctype) then
1600 if Present (Default_Aspect_Component_Value (Typ)) then
1601 return Default_Aspect_Component_Value (Typ);
1602 elsif Present (Default_Aspect_Value (Ctype)) then
1603 return Default_Aspect_Value (Ctype);
1604 else
1605 return Empty;
1606 end if;
1608 else
1609 return Empty;
1610 end if;
1612 else
1613 return Expression (Assoc);
1614 end if;
1615 end Get_Assoc_Expr;
1617 ---------------------
1618 -- Index_Base_Name --
1619 ---------------------
1621 function Index_Base_Name return Node_Id is
1622 begin
1623 return New_Occurrence_Of (Index_Base, Sloc (N));
1624 end Index_Base_Name;
1626 ------------------------------------
1627 -- Local_Compile_Time_Known_Value --
1628 ------------------------------------
1630 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1631 begin
1632 return Compile_Time_Known_Value (E)
1633 or else
1634 (Nkind (E) = N_Attribute_Reference
1635 and then Attribute_Name (E) = Name_Val
1636 and then Compile_Time_Known_Value (First (Expressions (E))));
1637 end Local_Compile_Time_Known_Value;
1639 ----------------------
1640 -- Local_Expr_Value --
1641 ----------------------
1643 function Local_Expr_Value (E : Node_Id) return Uint is
1644 begin
1645 if Compile_Time_Known_Value (E) then
1646 return Expr_Value (E);
1647 else
1648 return Expr_Value (First (Expressions (E)));
1649 end if;
1650 end Local_Expr_Value;
1652 -- Build_Array_Aggr_Code Variables
1654 Assoc : Node_Id;
1655 Choice : Node_Id;
1656 Expr : Node_Id;
1657 Typ : Entity_Id;
1659 Others_Assoc : Node_Id := Empty;
1661 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1662 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1663 -- The aggregate bounds of this specific subaggregate. Note that if the
1664 -- code generated by Build_Array_Aggr_Code is executed then these bounds
1665 -- are OK. Otherwise a Constraint_Error would have been raised.
1667 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1668 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1669 -- After Duplicate_Subexpr these are side-effect free
1671 Low : Node_Id;
1672 High : Node_Id;
1674 Nb_Choices : Nat := 0;
1675 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1676 -- Used to sort all the different choice values
1678 Nb_Elements : Int;
1679 -- Number of elements in the positional aggregate
1681 New_Code : constant List_Id := New_List;
1683 -- Start of processing for Build_Array_Aggr_Code
1685 begin
1686 -- First before we start, a special case. if we have a bit packed
1687 -- array represented as a modular type, then clear the value to
1688 -- zero first, to ensure that unused bits are properly cleared.
1690 Typ := Etype (N);
1692 if Present (Typ)
1693 and then Is_Bit_Packed_Array (Typ)
1694 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
1695 then
1696 Append_To (New_Code,
1697 Make_Assignment_Statement (Loc,
1698 Name => New_Copy_Tree (Into),
1699 Expression =>
1700 Unchecked_Convert_To (Typ,
1701 Make_Integer_Literal (Loc, Uint_0))));
1702 end if;
1704 -- If the component type contains tasks, we need to build a Master
1705 -- entity in the current scope, because it will be needed if build-
1706 -- in-place functions are called in the expanded code.
1708 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
1709 Build_Master_Entity (Defining_Identifier (Parent (N)));
1710 end if;
1712 -- STEP 1: Process component associations
1714 -- For those associations that may generate a loop, initialize
1715 -- Loop_Actions to collect inserted actions that may be crated.
1717 -- Skip this if no component associations
1719 if No (Expressions (N)) then
1721 -- STEP 1 (a): Sort the discrete choices
1723 Assoc := First (Component_Associations (N));
1724 while Present (Assoc) loop
1725 Choice := First (Choices (Assoc));
1726 while Present (Choice) loop
1727 if Nkind (Choice) = N_Others_Choice then
1728 Set_Loop_Actions (Assoc, New_List);
1729 Others_Assoc := Assoc;
1730 exit;
1731 end if;
1733 Get_Index_Bounds (Choice, Low, High);
1735 if Low /= High then
1736 Set_Loop_Actions (Assoc, New_List);
1737 end if;
1739 Nb_Choices := Nb_Choices + 1;
1741 Table (Nb_Choices) :=
1742 (Choice_Lo => Low,
1743 Choice_Hi => High,
1744 Choice_Node => Get_Assoc_Expr (Assoc));
1746 Next (Choice);
1747 end loop;
1749 Next (Assoc);
1750 end loop;
1752 -- If there is more than one set of choices these must be static
1753 -- and we can therefore sort them. Remember that Nb_Choices does not
1754 -- account for an others choice.
1756 if Nb_Choices > 1 then
1757 Sort_Case_Table (Table);
1758 end if;
1760 -- STEP 1 (b): take care of the whole set of discrete choices
1762 for J in 1 .. Nb_Choices loop
1763 Low := Table (J).Choice_Lo;
1764 High := Table (J).Choice_Hi;
1765 Expr := Table (J).Choice_Node;
1766 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
1767 end loop;
1769 -- STEP 1 (c): generate the remaining loops to cover others choice
1770 -- We don't need to generate loops over empty gaps, but if there is
1771 -- a single empty range we must analyze the expression for semantics
1773 if Present (Others_Assoc) then
1774 declare
1775 First : Boolean := True;
1777 begin
1778 for J in 0 .. Nb_Choices loop
1779 if J = 0 then
1780 Low := Aggr_Low;
1781 else
1782 Low := Add (1, To => Table (J).Choice_Hi);
1783 end if;
1785 if J = Nb_Choices then
1786 High := Aggr_High;
1787 else
1788 High := Add (-1, To => Table (J + 1).Choice_Lo);
1789 end if;
1791 -- If this is an expansion within an init proc, make
1792 -- sure that discriminant references are replaced by
1793 -- the corresponding discriminal.
1795 if Inside_Init_Proc then
1796 if Is_Entity_Name (Low)
1797 and then Ekind (Entity (Low)) = E_Discriminant
1798 then
1799 Set_Entity (Low, Discriminal (Entity (Low)));
1800 end if;
1802 if Is_Entity_Name (High)
1803 and then Ekind (Entity (High)) = E_Discriminant
1804 then
1805 Set_Entity (High, Discriminal (Entity (High)));
1806 end if;
1807 end if;
1809 if First
1810 or else not Empty_Range (Low, High)
1811 then
1812 First := False;
1813 Append_List
1814 (Gen_Loop (Low, High,
1815 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
1816 end if;
1817 end loop;
1818 end;
1819 end if;
1821 -- STEP 2: Process positional components
1823 else
1824 -- STEP 2 (a): Generate the assignments for each positional element
1825 -- Note that here we have to use Aggr_L rather than Aggr_Low because
1826 -- Aggr_L is analyzed and Add wants an analyzed expression.
1828 Expr := First (Expressions (N));
1829 Nb_Elements := -1;
1830 while Present (Expr) loop
1831 Nb_Elements := Nb_Elements + 1;
1832 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
1833 To => New_Code);
1834 Next (Expr);
1835 end loop;
1837 -- STEP 2 (b): Generate final loop if an others choice is present
1838 -- Here Nb_Elements gives the offset of the last positional element.
1840 if Present (Component_Associations (N)) then
1841 Assoc := Last (Component_Associations (N));
1843 -- Ada 2005 (AI-287)
1845 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
1846 Aggr_High,
1847 Get_Assoc_Expr (Assoc)), -- AI-287
1848 To => New_Code);
1849 end if;
1850 end if;
1852 return New_Code;
1853 end Build_Array_Aggr_Code;
1855 ----------------------------
1856 -- Build_Record_Aggr_Code --
1857 ----------------------------
1859 function Build_Record_Aggr_Code
1860 (N : Node_Id;
1861 Typ : Entity_Id;
1862 Lhs : Node_Id) return List_Id
1864 Loc : constant Source_Ptr := Sloc (N);
1865 L : constant List_Id := New_List;
1866 N_Typ : constant Entity_Id := Etype (N);
1868 Comp : Node_Id;
1869 Instr : Node_Id;
1870 Ref : Node_Id;
1871 Target : Entity_Id;
1872 Comp_Type : Entity_Id;
1873 Selector : Entity_Id;
1874 Comp_Expr : Node_Id;
1875 Expr_Q : Node_Id;
1877 -- If this is an internal aggregate, the External_Final_List is an
1878 -- expression for the controller record of the enclosing type.
1880 -- If the current aggregate has several controlled components, this
1881 -- expression will appear in several calls to attach to the finali-
1882 -- zation list, and it must not be shared.
1884 Ancestor_Is_Expression : Boolean := False;
1885 Ancestor_Is_Subtype_Mark : Boolean := False;
1887 Init_Typ : Entity_Id := Empty;
1889 Finalization_Done : Boolean := False;
1890 -- True if Generate_Finalization_Actions has already been called; calls
1891 -- after the first do nothing.
1893 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
1894 -- Returns the value that the given discriminant of an ancestor type
1895 -- should receive (in the absence of a conflict with the value provided
1896 -- by an ancestor part of an extension aggregate).
1898 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
1899 -- Check that each of the discriminant values defined by the ancestor
1900 -- part of an extension aggregate match the corresponding values
1901 -- provided by either an association of the aggregate or by the
1902 -- constraint imposed by a parent type (RM95-4.3.2(8)).
1904 function Compatible_Int_Bounds
1905 (Agg_Bounds : Node_Id;
1906 Typ_Bounds : Node_Id) return Boolean;
1907 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
1908 -- assumed that both bounds are integer ranges.
1910 procedure Generate_Finalization_Actions;
1911 -- Deal with the various controlled type data structure initializations
1912 -- (but only if it hasn't been done already).
1914 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
1915 -- Returns the first discriminant association in the constraint
1916 -- associated with T, if any, otherwise returns Empty.
1918 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
1919 -- If the ancestor part is an unconstrained type and further ancestors
1920 -- do not provide discriminants for it, check aggregate components for
1921 -- values of the discriminants.
1923 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
1924 -- If Typ is derived, and constrains discriminants of the parent type,
1925 -- these discriminants are not components of the aggregate, and must be
1926 -- initialized. The assignments are appended to List. The same is done
1927 -- if Typ derives fron an already constrained subtype of a discriminated
1928 -- parent type.
1930 procedure Init_Stored_Discriminants;
1931 -- If the type is derived and has inherited discriminants, generate
1932 -- explicit assignments for each, using the store constraint of the
1933 -- type. Note that both visible and stored discriminants must be
1934 -- initialized in case the derived type has some renamed and some
1935 -- constrained discriminants.
1937 procedure Init_Visible_Discriminants;
1938 -- If type has discriminants, retrieve their values from aggregate,
1939 -- and generate explicit assignments for each. This does not include
1940 -- discriminants inherited from ancestor, which are handled above.
1941 -- The type of the aggregate is a subtype created ealier using the
1942 -- given values of the discriminant components of the aggregate.
1944 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
1945 -- Check whether Bounds is a range node and its lower and higher bounds
1946 -- are integers literals.
1948 ---------------------------------
1949 -- Ancestor_Discriminant_Value --
1950 ---------------------------------
1952 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
1953 Assoc : Node_Id;
1954 Assoc_Elmt : Elmt_Id;
1955 Aggr_Comp : Entity_Id;
1956 Corresp_Disc : Entity_Id;
1957 Current_Typ : Entity_Id := Base_Type (Typ);
1958 Parent_Typ : Entity_Id;
1959 Parent_Disc : Entity_Id;
1960 Save_Assoc : Node_Id := Empty;
1962 begin
1963 -- First check any discriminant associations to see if any of them
1964 -- provide a value for the discriminant.
1966 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
1967 Assoc := First (Component_Associations (N));
1968 while Present (Assoc) loop
1969 Aggr_Comp := Entity (First (Choices (Assoc)));
1971 if Ekind (Aggr_Comp) = E_Discriminant then
1972 Save_Assoc := Expression (Assoc);
1974 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
1975 while Present (Corresp_Disc) loop
1977 -- If found a corresponding discriminant then return the
1978 -- value given in the aggregate. (Note: this is not
1979 -- correct in the presence of side effects. ???)
1981 if Disc = Corresp_Disc then
1982 return Duplicate_Subexpr (Expression (Assoc));
1983 end if;
1985 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
1986 end loop;
1987 end if;
1989 Next (Assoc);
1990 end loop;
1991 end if;
1993 -- No match found in aggregate, so chain up parent types to find
1994 -- a constraint that defines the value of the discriminant.
1996 Parent_Typ := Etype (Current_Typ);
1997 while Current_Typ /= Parent_Typ loop
1998 if Has_Discriminants (Parent_Typ)
1999 and then not Has_Unknown_Discriminants (Parent_Typ)
2000 then
2001 Parent_Disc := First_Discriminant (Parent_Typ);
2003 -- We either get the association from the subtype indication
2004 -- of the type definition itself, or from the discriminant
2005 -- constraint associated with the type entity (which is
2006 -- preferable, but it's not always present ???)
2008 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2009 then
2010 Assoc := Get_Constraint_Association (Current_Typ);
2011 Assoc_Elmt := No_Elmt;
2012 else
2013 Assoc_Elmt :=
2014 First_Elmt (Discriminant_Constraint (Current_Typ));
2015 Assoc := Node (Assoc_Elmt);
2016 end if;
2018 -- Traverse the discriminants of the parent type looking
2019 -- for one that corresponds.
2021 while Present (Parent_Disc) and then Present (Assoc) loop
2022 Corresp_Disc := Parent_Disc;
2023 while Present (Corresp_Disc)
2024 and then Disc /= Corresp_Disc
2025 loop
2026 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2027 end loop;
2029 if Disc = Corresp_Disc then
2030 if Nkind (Assoc) = N_Discriminant_Association then
2031 Assoc := Expression (Assoc);
2032 end if;
2034 -- If the located association directly denotes
2035 -- a discriminant, then use the value of a saved
2036 -- association of the aggregate. This is an approach
2037 -- used to handle certain cases involving multiple
2038 -- discriminants mapped to a single discriminant of
2039 -- a descendant. It's not clear how to locate the
2040 -- appropriate discriminant value for such cases. ???
2042 if Is_Entity_Name (Assoc)
2043 and then Ekind (Entity (Assoc)) = E_Discriminant
2044 then
2045 Assoc := Save_Assoc;
2046 end if;
2048 return Duplicate_Subexpr (Assoc);
2049 end if;
2051 Next_Discriminant (Parent_Disc);
2053 if No (Assoc_Elmt) then
2054 Next (Assoc);
2056 else
2057 Next_Elmt (Assoc_Elmt);
2059 if Present (Assoc_Elmt) then
2060 Assoc := Node (Assoc_Elmt);
2061 else
2062 Assoc := Empty;
2063 end if;
2064 end if;
2065 end loop;
2066 end if;
2068 Current_Typ := Parent_Typ;
2069 Parent_Typ := Etype (Current_Typ);
2070 end loop;
2072 -- In some cases there's no ancestor value to locate (such as
2073 -- when an ancestor part given by an expression defines the
2074 -- discriminant value).
2076 return Empty;
2077 end Ancestor_Discriminant_Value;
2079 ----------------------------------
2080 -- Check_Ancestor_Discriminants --
2081 ----------------------------------
2083 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2084 Discr : Entity_Id;
2085 Disc_Value : Node_Id;
2086 Cond : Node_Id;
2088 begin
2089 Discr := First_Discriminant (Base_Type (Anc_Typ));
2090 while Present (Discr) loop
2091 Disc_Value := Ancestor_Discriminant_Value (Discr);
2093 if Present (Disc_Value) then
2094 Cond := Make_Op_Ne (Loc,
2095 Left_Opnd =>
2096 Make_Selected_Component (Loc,
2097 Prefix => New_Copy_Tree (Target),
2098 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2099 Right_Opnd => Disc_Value);
2101 Append_To (L,
2102 Make_Raise_Constraint_Error (Loc,
2103 Condition => Cond,
2104 Reason => CE_Discriminant_Check_Failed));
2105 end if;
2107 Next_Discriminant (Discr);
2108 end loop;
2109 end Check_Ancestor_Discriminants;
2111 ---------------------------
2112 -- Compatible_Int_Bounds --
2113 ---------------------------
2115 function Compatible_Int_Bounds
2116 (Agg_Bounds : Node_Id;
2117 Typ_Bounds : Node_Id) return Boolean
2119 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2120 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2121 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2122 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2123 begin
2124 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2125 end Compatible_Int_Bounds;
2127 --------------------------------
2128 -- Get_Constraint_Association --
2129 --------------------------------
2131 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2132 Indic : Node_Id;
2133 Typ : Entity_Id;
2135 begin
2136 Typ := T;
2138 -- If type is private, get constraint from full view. This was
2139 -- previously done in an instance context, but is needed whenever
2140 -- the ancestor part has a discriminant, possibly inherited through
2141 -- multiple derivations.
2143 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2144 Typ := Full_View (Typ);
2145 end if;
2147 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2149 -- Verify that the subtype indication carries a constraint
2151 if Nkind (Indic) = N_Subtype_Indication
2152 and then Present (Constraint (Indic))
2153 then
2154 return First (Constraints (Constraint (Indic)));
2155 end if;
2157 return Empty;
2158 end Get_Constraint_Association;
2160 -------------------------------------
2161 -- Get_Explicit_Discriminant_Value --
2162 -------------------------------------
2164 function Get_Explicit_Discriminant_Value
2165 (D : Entity_Id) return Node_Id
2167 Assoc : Node_Id;
2168 Choice : Node_Id;
2169 Val : Node_Id;
2171 begin
2172 -- The aggregate has been normalized and all associations have a
2173 -- single choice.
2175 Assoc := First (Component_Associations (N));
2176 while Present (Assoc) loop
2177 Choice := First (Choices (Assoc));
2179 if Chars (Choice) = Chars (D) then
2180 Val := Expression (Assoc);
2181 Remove (Assoc);
2182 return Val;
2183 end if;
2185 Next (Assoc);
2186 end loop;
2188 return Empty;
2189 end Get_Explicit_Discriminant_Value;
2191 -------------------------------
2192 -- Init_Hidden_Discriminants --
2193 -------------------------------
2195 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2196 function Is_Completely_Hidden_Discriminant
2197 (Discr : Entity_Id) return Boolean;
2198 -- Determine whether Discr is a completely hidden discriminant of
2199 -- type Typ.
2201 ---------------------------------------
2202 -- Is_Completely_Hidden_Discriminant --
2203 ---------------------------------------
2205 function Is_Completely_Hidden_Discriminant
2206 (Discr : Entity_Id) return Boolean
2208 Item : Entity_Id;
2210 begin
2211 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2212 -- completely hidden discriminants.
2214 Item := First_Entity (Typ);
2215 while Present (Item) loop
2216 if Ekind (Item) = E_Discriminant
2217 and then Is_Completely_Hidden (Item)
2218 and then Chars (Original_Record_Component (Item)) =
2219 Chars (Discr)
2220 then
2221 return True;
2222 end if;
2224 Next_Entity (Item);
2225 end loop;
2227 return False;
2228 end Is_Completely_Hidden_Discriminant;
2230 -- Local variables
2232 Base_Typ : Entity_Id;
2233 Discr : Entity_Id;
2234 Discr_Constr : Elmt_Id;
2235 Discr_Init : Node_Id;
2236 Discr_Val : Node_Id;
2237 In_Aggr_Type : Boolean;
2238 Par_Typ : Entity_Id;
2240 -- Start of processing for Init_Hidden_Discriminants
2242 begin
2243 -- The constraints on the hidden discriminants, if present, are kept
2244 -- in the Stored_Constraint list of the type itself, or in that of
2245 -- the base type. If not in the constraints of the aggregate itself,
2246 -- we examine ancestors to find discriminants that are not renamed
2247 -- by other discriminants but constrained explicitly.
2249 In_Aggr_Type := True;
2251 Base_Typ := Base_Type (Typ);
2252 while Is_Derived_Type (Base_Typ)
2253 and then
2254 (Present (Stored_Constraint (Base_Typ))
2255 or else
2256 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2257 loop
2258 Par_Typ := Etype (Base_Typ);
2260 if not Has_Discriminants (Par_Typ) then
2261 return;
2262 end if;
2264 Discr := First_Discriminant (Par_Typ);
2266 -- We know that one of the stored-constraint lists is present
2268 if Present (Stored_Constraint (Base_Typ)) then
2269 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2271 -- For private extension, stored constraint may be on full view
2273 elsif Is_Private_Type (Base_Typ)
2274 and then Present (Full_View (Base_Typ))
2275 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2276 then
2277 Discr_Constr :=
2278 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2280 else
2281 Discr_Constr := First_Elmt (Stored_Constraint (Typ));
2282 end if;
2284 while Present (Discr) and then Present (Discr_Constr) loop
2285 Discr_Val := Node (Discr_Constr);
2287 -- The parent discriminant is renamed in the derived type,
2288 -- nothing to initialize.
2290 -- type Deriv_Typ (Discr : ...)
2291 -- is new Parent_Typ (Discr => Discr);
2293 if Is_Entity_Name (Discr_Val)
2294 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2295 then
2296 null;
2298 -- When the parent discriminant is constrained at the type
2299 -- extension level, it does not appear in the derived type.
2301 -- type Deriv_Typ (Discr : ...)
2302 -- is new Parent_Typ (Discr => Discr,
2303 -- Hidden_Discr => Expression);
2305 elsif Is_Completely_Hidden_Discriminant (Discr) then
2306 null;
2308 -- Otherwise initialize the discriminant
2310 else
2311 Discr_Init :=
2312 Make_OK_Assignment_Statement (Loc,
2313 Name =>
2314 Make_Selected_Component (Loc,
2315 Prefix => New_Copy_Tree (Target),
2316 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2317 Expression => New_Copy_Tree (Discr_Val));
2319 Set_No_Ctrl_Actions (Discr_Init);
2320 Append_To (List, Discr_Init);
2321 end if;
2323 Next_Elmt (Discr_Constr);
2324 Next_Discriminant (Discr);
2325 end loop;
2327 In_Aggr_Type := False;
2328 Base_Typ := Base_Type (Par_Typ);
2329 end loop;
2330 end Init_Hidden_Discriminants;
2332 --------------------------------
2333 -- Init_Visible_Discriminants --
2334 --------------------------------
2336 procedure Init_Visible_Discriminants is
2337 Discriminant : Entity_Id;
2338 Discriminant_Value : Node_Id;
2340 begin
2341 Discriminant := First_Discriminant (Typ);
2342 while Present (Discriminant) loop
2343 Comp_Expr :=
2344 Make_Selected_Component (Loc,
2345 Prefix => New_Copy_Tree (Target),
2346 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2348 Discriminant_Value :=
2349 Get_Discriminant_Value
2350 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2352 Instr :=
2353 Make_OK_Assignment_Statement (Loc,
2354 Name => Comp_Expr,
2355 Expression => New_Copy_Tree (Discriminant_Value));
2357 Set_No_Ctrl_Actions (Instr);
2358 Append_To (L, Instr);
2360 Next_Discriminant (Discriminant);
2361 end loop;
2362 end Init_Visible_Discriminants;
2364 -------------------------------
2365 -- Init_Stored_Discriminants --
2366 -------------------------------
2368 procedure Init_Stored_Discriminants is
2369 Discriminant : Entity_Id;
2370 Discriminant_Value : Node_Id;
2372 begin
2373 Discriminant := First_Stored_Discriminant (Typ);
2374 while Present (Discriminant) loop
2375 Comp_Expr :=
2376 Make_Selected_Component (Loc,
2377 Prefix => New_Copy_Tree (Target),
2378 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2380 Discriminant_Value :=
2381 Get_Discriminant_Value
2382 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2384 Instr :=
2385 Make_OK_Assignment_Statement (Loc,
2386 Name => Comp_Expr,
2387 Expression => New_Copy_Tree (Discriminant_Value));
2389 Set_No_Ctrl_Actions (Instr);
2390 Append_To (L, Instr);
2392 Next_Stored_Discriminant (Discriminant);
2393 end loop;
2394 end Init_Stored_Discriminants;
2396 -------------------------
2397 -- Is_Int_Range_Bounds --
2398 -------------------------
2400 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2401 begin
2402 return Nkind (Bounds) = N_Range
2403 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2404 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2405 end Is_Int_Range_Bounds;
2407 -----------------------------------
2408 -- Generate_Finalization_Actions --
2409 -----------------------------------
2411 procedure Generate_Finalization_Actions is
2412 begin
2413 -- Do the work only the first time this is called
2415 if Finalization_Done then
2416 return;
2417 end if;
2419 Finalization_Done := True;
2421 -- Determine the external finalization list. It is either the
2422 -- finalization list of the outer-scope or the one coming from an
2423 -- outer aggregate. When the target is not a temporary, the proper
2424 -- scope is the scope of the target rather than the potentially
2425 -- transient current scope.
2427 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2428 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2429 Set_Assignment_OK (Ref);
2431 Append_To (L,
2432 Make_Procedure_Call_Statement (Loc,
2433 Name =>
2434 New_Occurrence_Of
2435 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2436 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2437 end if;
2438 end Generate_Finalization_Actions;
2440 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2441 -- If default expression of a component mentions a discriminant of the
2442 -- type, it must be rewritten as the discriminant of the target object.
2444 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2445 -- If the aggregate contains a self-reference, traverse each expression
2446 -- to replace a possible self-reference with a reference to the proper
2447 -- component of the target of the assignment.
2449 --------------------------
2450 -- Rewrite_Discriminant --
2451 --------------------------
2453 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
2454 begin
2455 if Is_Entity_Name (Expr)
2456 and then Present (Entity (Expr))
2457 and then Ekind (Entity (Expr)) = E_In_Parameter
2458 and then Present (Discriminal_Link (Entity (Expr)))
2459 and then Scope (Discriminal_Link (Entity (Expr))) =
2460 Base_Type (Etype (N))
2461 then
2462 Rewrite (Expr,
2463 Make_Selected_Component (Loc,
2464 Prefix => New_Copy_Tree (Lhs),
2465 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
2466 end if;
2468 return OK;
2469 end Rewrite_Discriminant;
2471 ------------------
2472 -- Replace_Type --
2473 ------------------
2475 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2476 begin
2477 -- Note regarding the Root_Type test below: Aggregate components for
2478 -- self-referential types include attribute references to the current
2479 -- instance, of the form: Typ'access, etc.. These references are
2480 -- rewritten as references to the target of the aggregate: the
2481 -- left-hand side of an assignment, the entity in a declaration,
2482 -- or a temporary. Without this test, we would improperly extended
2483 -- this rewriting to attribute references whose prefix was not the
2484 -- type of the aggregate.
2486 if Nkind (Expr) = N_Attribute_Reference
2487 and then Is_Entity_Name (Prefix (Expr))
2488 and then Is_Type (Entity (Prefix (Expr)))
2489 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2490 then
2491 if Is_Entity_Name (Lhs) then
2492 Rewrite (Prefix (Expr),
2493 New_Occurrence_Of (Entity (Lhs), Loc));
2495 elsif Nkind (Lhs) = N_Selected_Component then
2496 Rewrite (Expr,
2497 Make_Attribute_Reference (Loc,
2498 Attribute_Name => Name_Unrestricted_Access,
2499 Prefix => New_Copy_Tree (Lhs)));
2500 Set_Analyzed (Parent (Expr), False);
2502 else
2503 Rewrite (Expr,
2504 Make_Attribute_Reference (Loc,
2505 Attribute_Name => Name_Unrestricted_Access,
2506 Prefix => New_Copy_Tree (Lhs)));
2507 Set_Analyzed (Parent (Expr), False);
2508 end if;
2509 end if;
2511 return OK;
2512 end Replace_Type;
2514 procedure Replace_Self_Reference is
2515 new Traverse_Proc (Replace_Type);
2517 procedure Replace_Discriminants is
2518 new Traverse_Proc (Rewrite_Discriminant);
2520 -- Start of processing for Build_Record_Aggr_Code
2522 begin
2523 if Has_Self_Reference (N) then
2524 Replace_Self_Reference (N);
2525 end if;
2527 -- If the target of the aggregate is class-wide, we must convert it
2528 -- to the actual type of the aggregate, so that the proper components
2529 -- are visible. We know already that the types are compatible.
2531 if Present (Etype (Lhs))
2532 and then Is_Class_Wide_Type (Etype (Lhs))
2533 then
2534 Target := Unchecked_Convert_To (Typ, Lhs);
2535 else
2536 Target := Lhs;
2537 end if;
2539 -- Deal with the ancestor part of extension aggregates or with the
2540 -- discriminants of the root type.
2542 if Nkind (N) = N_Extension_Aggregate then
2543 declare
2544 Ancestor : constant Node_Id := Ancestor_Part (N);
2545 Assign : List_Id;
2547 begin
2548 -- If the ancestor part is a subtype mark "T", we generate
2550 -- init-proc (T (tmp)); if T is constrained and
2551 -- init-proc (S (tmp)); where S applies an appropriate
2552 -- constraint if T is unconstrained
2554 if Is_Entity_Name (Ancestor)
2555 and then Is_Type (Entity (Ancestor))
2556 then
2557 Ancestor_Is_Subtype_Mark := True;
2559 if Is_Constrained (Entity (Ancestor)) then
2560 Init_Typ := Entity (Ancestor);
2562 -- For an ancestor part given by an unconstrained type mark,
2563 -- create a subtype constrained by appropriate corresponding
2564 -- discriminant values coming from either associations of the
2565 -- aggregate or a constraint on a parent type. The subtype will
2566 -- be used to generate the correct default value for the
2567 -- ancestor part.
2569 elsif Has_Discriminants (Entity (Ancestor)) then
2570 declare
2571 Anc_Typ : constant Entity_Id := Entity (Ancestor);
2572 Anc_Constr : constant List_Id := New_List;
2573 Discrim : Entity_Id;
2574 Disc_Value : Node_Id;
2575 New_Indic : Node_Id;
2576 Subt_Decl : Node_Id;
2578 begin
2579 Discrim := First_Discriminant (Anc_Typ);
2580 while Present (Discrim) loop
2581 Disc_Value := Ancestor_Discriminant_Value (Discrim);
2583 -- If no usable discriminant in ancestors, check
2584 -- whether aggregate has an explicit value for it.
2586 if No (Disc_Value) then
2587 Disc_Value :=
2588 Get_Explicit_Discriminant_Value (Discrim);
2589 end if;
2591 Append_To (Anc_Constr, Disc_Value);
2592 Next_Discriminant (Discrim);
2593 end loop;
2595 New_Indic :=
2596 Make_Subtype_Indication (Loc,
2597 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
2598 Constraint =>
2599 Make_Index_Or_Discriminant_Constraint (Loc,
2600 Constraints => Anc_Constr));
2602 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
2604 Subt_Decl :=
2605 Make_Subtype_Declaration (Loc,
2606 Defining_Identifier => Init_Typ,
2607 Subtype_Indication => New_Indic);
2609 -- Itypes must be analyzed with checks off Declaration
2610 -- must have a parent for proper handling of subsidiary
2611 -- actions.
2613 Set_Parent (Subt_Decl, N);
2614 Analyze (Subt_Decl, Suppress => All_Checks);
2615 end;
2616 end if;
2618 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2619 Set_Assignment_OK (Ref);
2621 if not Is_Interface (Init_Typ) then
2622 Append_List_To (L,
2623 Build_Initialization_Call (Loc,
2624 Id_Ref => Ref,
2625 Typ => Init_Typ,
2626 In_Init_Proc => Within_Init_Proc,
2627 With_Default_Init => Has_Default_Init_Comps (N)
2628 or else
2629 Has_Task (Base_Type (Init_Typ))));
2631 if Is_Constrained (Entity (Ancestor))
2632 and then Has_Discriminants (Entity (Ancestor))
2633 then
2634 Check_Ancestor_Discriminants (Entity (Ancestor));
2635 end if;
2636 end if;
2638 -- Handle calls to C++ constructors
2640 elsif Is_CPP_Constructor_Call (Ancestor) then
2641 Init_Typ := Etype (Ancestor);
2642 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2643 Set_Assignment_OK (Ref);
2645 Append_List_To (L,
2646 Build_Initialization_Call (Loc,
2647 Id_Ref => Ref,
2648 Typ => Init_Typ,
2649 In_Init_Proc => Within_Init_Proc,
2650 With_Default_Init => Has_Default_Init_Comps (N),
2651 Constructor_Ref => Ancestor));
2653 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
2654 -- limited type, a recursive call expands the ancestor. Note that
2655 -- in the limited case, the ancestor part must be either a
2656 -- function call (possibly qualified, or wrapped in an unchecked
2657 -- conversion) or aggregate (definitely qualified).
2659 -- The ancestor part can also be a function call (that may be
2660 -- transformed into an explicit dereference) or a qualification
2661 -- of one such.
2663 elsif Is_Limited_Type (Etype (Ancestor))
2664 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
2665 N_Extension_Aggregate)
2666 then
2667 Ancestor_Is_Expression := True;
2669 -- Set up finalization data for enclosing record, because
2670 -- controlled subcomponents of the ancestor part will be
2671 -- attached to it.
2673 Generate_Finalization_Actions;
2675 Append_List_To (L,
2676 Build_Record_Aggr_Code
2677 (N => Unqualify (Ancestor),
2678 Typ => Etype (Unqualify (Ancestor)),
2679 Lhs => Target));
2681 -- If the ancestor part is an expression "E", we generate
2683 -- T (tmp) := E;
2685 -- In Ada 2005, this includes the case of a (possibly qualified)
2686 -- limited function call. The assignment will turn into a
2687 -- build-in-place function call (for further details, see
2688 -- Make_Build_In_Place_Call_In_Assignment).
2690 else
2691 Ancestor_Is_Expression := True;
2692 Init_Typ := Etype (Ancestor);
2694 -- If the ancestor part is an aggregate, force its full
2695 -- expansion, which was delayed.
2697 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
2698 N_Extension_Aggregate)
2699 then
2700 Set_Analyzed (Ancestor, False);
2701 Set_Analyzed (Expression (Ancestor), False);
2702 end if;
2704 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2705 Set_Assignment_OK (Ref);
2707 -- Make the assignment without usual controlled actions, since
2708 -- we only want to Adjust afterwards, but not to Finalize
2709 -- beforehand. Add manual Adjust when necessary.
2711 Assign := New_List (
2712 Make_OK_Assignment_Statement (Loc,
2713 Name => Ref,
2714 Expression => Ancestor));
2715 Set_No_Ctrl_Actions (First (Assign));
2717 -- Assign the tag now to make sure that the dispatching call in
2718 -- the subsequent deep_adjust works properly (unless
2719 -- Tagged_Type_Expansion where tags are implicit).
2721 if Tagged_Type_Expansion then
2722 Instr :=
2723 Make_OK_Assignment_Statement (Loc,
2724 Name =>
2725 Make_Selected_Component (Loc,
2726 Prefix => New_Copy_Tree (Target),
2727 Selector_Name =>
2728 New_Occurrence_Of
2729 (First_Tag_Component (Base_Type (Typ)), Loc)),
2731 Expression =>
2732 Unchecked_Convert_To (RTE (RE_Tag),
2733 New_Occurrence_Of
2734 (Node (First_Elmt
2735 (Access_Disp_Table (Base_Type (Typ)))),
2736 Loc)));
2738 Set_Assignment_OK (Name (Instr));
2739 Append_To (Assign, Instr);
2741 -- Ada 2005 (AI-251): If tagged type has progenitors we must
2742 -- also initialize tags of the secondary dispatch tables.
2744 if Has_Interfaces (Base_Type (Typ)) then
2745 Init_Secondary_Tags
2746 (Typ => Base_Type (Typ),
2747 Target => Target,
2748 Stmts_List => Assign);
2749 end if;
2750 end if;
2752 -- Call Adjust manually
2754 if Needs_Finalization (Etype (Ancestor))
2755 and then not Is_Limited_Type (Etype (Ancestor))
2756 then
2757 Append_To (Assign,
2758 Make_Adjust_Call
2759 (Obj_Ref => New_Copy_Tree (Ref),
2760 Typ => Etype (Ancestor)));
2761 end if;
2763 Append_To (L,
2764 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
2766 if Has_Discriminants (Init_Typ) then
2767 Check_Ancestor_Discriminants (Init_Typ);
2768 end if;
2769 end if;
2770 end;
2772 -- Generate assignments of hidden discriminants. If the base type is
2773 -- an unchecked union, the discriminants are unknown to the back-end
2774 -- and absent from a value of the type, so assignments for them are
2775 -- not emitted.
2777 if Has_Discriminants (Typ)
2778 and then not Is_Unchecked_Union (Base_Type (Typ))
2779 then
2780 Init_Hidden_Discriminants (Typ, L);
2781 end if;
2783 -- Normal case (not an extension aggregate)
2785 else
2786 -- Generate the discriminant expressions, component by component.
2787 -- If the base type is an unchecked union, the discriminants are
2788 -- unknown to the back-end and absent from a value of the type, so
2789 -- assignments for them are not emitted.
2791 if Has_Discriminants (Typ)
2792 and then not Is_Unchecked_Union (Base_Type (Typ))
2793 then
2794 Init_Hidden_Discriminants (Typ, L);
2796 -- Generate discriminant init values for the visible discriminants
2798 Init_Visible_Discriminants;
2800 if Is_Derived_Type (N_Typ) then
2801 Init_Stored_Discriminants;
2802 end if;
2803 end if;
2804 end if;
2806 -- For CPP types we generate an implicit call to the C++ default
2807 -- constructor to ensure the proper initialization of the _Tag
2808 -- component.
2810 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
2811 Invoke_Constructor : declare
2812 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
2814 procedure Invoke_IC_Proc (T : Entity_Id);
2815 -- Recursive routine used to climb to parents. Required because
2816 -- parents must be initialized before descendants to ensure
2817 -- propagation of inherited C++ slots.
2819 --------------------
2820 -- Invoke_IC_Proc --
2821 --------------------
2823 procedure Invoke_IC_Proc (T : Entity_Id) is
2824 begin
2825 -- Avoid generating extra calls. Initialization required
2826 -- only for types defined from the level of derivation of
2827 -- type of the constructor and the type of the aggregate.
2829 if T = CPP_Parent then
2830 return;
2831 end if;
2833 Invoke_IC_Proc (Etype (T));
2835 -- Generate call to the IC routine
2837 if Present (CPP_Init_Proc (T)) then
2838 Append_To (L,
2839 Make_Procedure_Call_Statement (Loc,
2840 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
2841 end if;
2842 end Invoke_IC_Proc;
2844 -- Start of processing for Invoke_Constructor
2846 begin
2847 -- Implicit invocation of the C++ constructor
2849 if Nkind (N) = N_Aggregate then
2850 Append_To (L,
2851 Make_Procedure_Call_Statement (Loc,
2852 Name =>
2853 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
2854 Parameter_Associations => New_List (
2855 Unchecked_Convert_To (CPP_Parent,
2856 New_Copy_Tree (Lhs)))));
2857 end if;
2859 Invoke_IC_Proc (Typ);
2860 end Invoke_Constructor;
2861 end if;
2863 -- Generate the assignments, component by component
2865 -- tmp.comp1 := Expr1_From_Aggr;
2866 -- tmp.comp2 := Expr2_From_Aggr;
2867 -- ....
2869 Comp := First (Component_Associations (N));
2870 while Present (Comp) loop
2871 Selector := Entity (First (Choices (Comp)));
2873 -- C++ constructors
2875 if Is_CPP_Constructor_Call (Expression (Comp)) then
2876 Append_List_To (L,
2877 Build_Initialization_Call (Loc,
2878 Id_Ref =>
2879 Make_Selected_Component (Loc,
2880 Prefix => New_Copy_Tree (Target),
2881 Selector_Name => New_Occurrence_Of (Selector, Loc)),
2882 Typ => Etype (Selector),
2883 Enclos_Type => Typ,
2884 With_Default_Init => True,
2885 Constructor_Ref => Expression (Comp)));
2887 -- Ada 2005 (AI-287): For each default-initialized component generate
2888 -- a call to the corresponding IP subprogram if available.
2890 elsif Box_Present (Comp)
2891 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
2892 then
2893 if Ekind (Selector) /= E_Discriminant then
2894 Generate_Finalization_Actions;
2895 end if;
2897 -- Ada 2005 (AI-287): If the component type has tasks then
2898 -- generate the activation chain and master entities (except
2899 -- in case of an allocator because in that case these entities
2900 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
2902 declare
2903 Ctype : constant Entity_Id := Etype (Selector);
2904 Inside_Allocator : Boolean := False;
2905 P : Node_Id := Parent (N);
2907 begin
2908 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
2909 while Present (P) loop
2910 if Nkind (P) = N_Allocator then
2911 Inside_Allocator := True;
2912 exit;
2913 end if;
2915 P := Parent (P);
2916 end loop;
2918 if not Inside_Init_Proc and not Inside_Allocator then
2919 Build_Activation_Chain_Entity (N);
2920 end if;
2921 end if;
2922 end;
2924 Append_List_To (L,
2925 Build_Initialization_Call (Loc,
2926 Id_Ref => Make_Selected_Component (Loc,
2927 Prefix => New_Copy_Tree (Target),
2928 Selector_Name =>
2929 New_Occurrence_Of (Selector, Loc)),
2930 Typ => Etype (Selector),
2931 Enclos_Type => Typ,
2932 With_Default_Init => True));
2934 -- Prepare for component assignment
2936 elsif Ekind (Selector) /= E_Discriminant
2937 or else Nkind (N) = N_Extension_Aggregate
2938 then
2939 -- All the discriminants have now been assigned
2941 -- This is now a good moment to initialize and attach all the
2942 -- controllers. Their position may depend on the discriminants.
2944 if Ekind (Selector) /= E_Discriminant then
2945 Generate_Finalization_Actions;
2946 end if;
2948 Comp_Type := Underlying_Type (Etype (Selector));
2949 Comp_Expr :=
2950 Make_Selected_Component (Loc,
2951 Prefix => New_Copy_Tree (Target),
2952 Selector_Name => New_Occurrence_Of (Selector, Loc));
2954 if Nkind (Expression (Comp)) = N_Qualified_Expression then
2955 Expr_Q := Expression (Expression (Comp));
2956 else
2957 Expr_Q := Expression (Comp);
2958 end if;
2960 -- Now either create the assignment or generate the code for the
2961 -- inner aggregate top-down.
2963 if Is_Delayed_Aggregate (Expr_Q) then
2965 -- We have the following case of aggregate nesting inside
2966 -- an object declaration:
2968 -- type Arr_Typ is array (Integer range <>) of ...;
2970 -- type Rec_Typ (...) is record
2971 -- Obj_Arr_Typ : Arr_Typ (A .. B);
2972 -- end record;
2974 -- Obj_Rec_Typ : Rec_Typ := (...,
2975 -- Obj_Arr_Typ => (X => (...), Y => (...)));
2977 -- The length of the ranges of the aggregate and Obj_Add_Typ
2978 -- are equal (B - A = Y - X), but they do not coincide (X /=
2979 -- A and B /= Y). This case requires array sliding which is
2980 -- performed in the following manner:
2982 -- subtype Arr_Sub is Arr_Typ (X .. Y);
2983 -- Temp : Arr_Sub;
2984 -- Temp (X) := (...);
2985 -- ...
2986 -- Temp (Y) := (...);
2987 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
2989 if Ekind (Comp_Type) = E_Array_Subtype
2990 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
2991 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
2992 and then not
2993 Compatible_Int_Bounds
2994 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
2995 Typ_Bounds => First_Index (Comp_Type))
2996 then
2997 -- Create the array subtype with bounds equal to those of
2998 -- the corresponding aggregate.
3000 declare
3001 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3003 SubD : constant Node_Id :=
3004 Make_Subtype_Declaration (Loc,
3005 Defining_Identifier => SubE,
3006 Subtype_Indication =>
3007 Make_Subtype_Indication (Loc,
3008 Subtype_Mark =>
3009 New_Occurrence_Of (Etype (Comp_Type), Loc),
3010 Constraint =>
3011 Make_Index_Or_Discriminant_Constraint
3012 (Loc,
3013 Constraints => New_List (
3014 New_Copy_Tree
3015 (Aggregate_Bounds (Expr_Q))))));
3017 -- Create a temporary array of the above subtype which
3018 -- will be used to capture the aggregate assignments.
3020 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3022 TmpD : constant Node_Id :=
3023 Make_Object_Declaration (Loc,
3024 Defining_Identifier => TmpE,
3025 Object_Definition => New_Occurrence_Of (SubE, Loc));
3027 begin
3028 Set_No_Initialization (TmpD);
3029 Append_To (L, SubD);
3030 Append_To (L, TmpD);
3032 -- Expand aggregate into assignments to the temp array
3034 Append_List_To (L,
3035 Late_Expansion (Expr_Q, Comp_Type,
3036 New_Occurrence_Of (TmpE, Loc)));
3038 -- Slide
3040 Append_To (L,
3041 Make_Assignment_Statement (Loc,
3042 Name => New_Copy_Tree (Comp_Expr),
3043 Expression => New_Occurrence_Of (TmpE, Loc)));
3044 end;
3046 -- Normal case (sliding not required)
3048 else
3049 Append_List_To (L,
3050 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3051 end if;
3053 -- Expr_Q is not delayed aggregate
3055 else
3056 if Has_Discriminants (Typ) then
3057 Replace_Discriminants (Expr_Q);
3059 -- If the component is an array type that depends on
3060 -- discriminants, and the expression is a single Others
3061 -- clause, create an explicit subtype for it because the
3062 -- backend has troubles recovering the actual bounds.
3064 if Nkind (Expr_Q) = N_Aggregate
3065 and then Is_Array_Type (Comp_Type)
3066 and then Present (Component_Associations (Expr_Q))
3067 then
3068 declare
3069 Assoc : constant Node_Id :=
3070 First (Component_Associations (Expr_Q));
3071 Decl : Node_Id;
3073 begin
3074 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3075 then
3076 Decl :=
3077 Build_Actual_Subtype_Of_Component
3078 (Comp_Type, Comp_Expr);
3080 -- If the component type does not in fact depend on
3081 -- discriminants, the subtype declaration is empty.
3083 if Present (Decl) then
3084 Append_To (L, Decl);
3085 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3086 end if;
3087 end if;
3088 end;
3089 end if;
3090 end if;
3092 if Generate_C_Code
3093 and then Nkind (Expr_Q) = N_Aggregate
3094 and then Is_Array_Type (Etype (Expr_Q))
3095 and then Present (First_Index (Etype (Expr_Q)))
3096 then
3097 declare
3098 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3099 begin
3100 Append_List_To (L,
3101 Build_Array_Aggr_Code
3102 (N => Expr_Q,
3103 Ctype => Component_Type (Expr_Q_Type),
3104 Index => First_Index (Expr_Q_Type),
3105 Into => Comp_Expr,
3106 Scalar_Comp => Is_Scalar_Type
3107 (Component_Type (Expr_Q_Type))));
3108 end;
3110 else
3111 Instr :=
3112 Make_OK_Assignment_Statement (Loc,
3113 Name => Comp_Expr,
3114 Expression => Expr_Q);
3116 Set_No_Ctrl_Actions (Instr);
3117 Append_To (L, Instr);
3118 end if;
3120 -- Adjust the tag if tagged (because of possible view
3121 -- conversions), unless compiling for a VM where tags are
3122 -- implicit.
3124 -- tmp.comp._tag := comp_typ'tag;
3126 if Is_Tagged_Type (Comp_Type)
3127 and then Tagged_Type_Expansion
3128 then
3129 Instr :=
3130 Make_OK_Assignment_Statement (Loc,
3131 Name =>
3132 Make_Selected_Component (Loc,
3133 Prefix => New_Copy_Tree (Comp_Expr),
3134 Selector_Name =>
3135 New_Occurrence_Of
3136 (First_Tag_Component (Comp_Type), Loc)),
3138 Expression =>
3139 Unchecked_Convert_To (RTE (RE_Tag),
3140 New_Occurrence_Of
3141 (Node (First_Elmt (Access_Disp_Table (Comp_Type))),
3142 Loc)));
3144 Append_To (L, Instr);
3145 end if;
3147 -- Generate:
3148 -- Adjust (tmp.comp);
3150 if Needs_Finalization (Comp_Type)
3151 and then not Is_Limited_Type (Comp_Type)
3152 then
3153 Append_To (L,
3154 Make_Adjust_Call
3155 (Obj_Ref => New_Copy_Tree (Comp_Expr),
3156 Typ => Comp_Type));
3157 end if;
3158 end if;
3160 -- comment would be good here ???
3162 elsif Ekind (Selector) = E_Discriminant
3163 and then Nkind (N) /= N_Extension_Aggregate
3164 and then Nkind (Parent (N)) = N_Component_Association
3165 and then Is_Constrained (Typ)
3166 then
3167 -- We must check that the discriminant value imposed by the
3168 -- context is the same as the value given in the subaggregate,
3169 -- because after the expansion into assignments there is no
3170 -- record on which to perform a regular discriminant check.
3172 declare
3173 D_Val : Elmt_Id;
3174 Disc : Entity_Id;
3176 begin
3177 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3178 Disc := First_Discriminant (Typ);
3179 while Chars (Disc) /= Chars (Selector) loop
3180 Next_Discriminant (Disc);
3181 Next_Elmt (D_Val);
3182 end loop;
3184 pragma Assert (Present (D_Val));
3186 -- This check cannot performed for components that are
3187 -- constrained by a current instance, because this is not a
3188 -- value that can be compared with the actual constraint.
3190 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3191 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3192 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3193 then
3194 Append_To (L,
3195 Make_Raise_Constraint_Error (Loc,
3196 Condition =>
3197 Make_Op_Ne (Loc,
3198 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3199 Right_Opnd => Expression (Comp)),
3200 Reason => CE_Discriminant_Check_Failed));
3202 else
3203 -- Find self-reference in previous discriminant assignment,
3204 -- and replace with proper expression.
3206 declare
3207 Ass : Node_Id;
3209 begin
3210 Ass := First (L);
3211 while Present (Ass) loop
3212 if Nkind (Ass) = N_Assignment_Statement
3213 and then Nkind (Name (Ass)) = N_Selected_Component
3214 and then Chars (Selector_Name (Name (Ass))) =
3215 Chars (Disc)
3216 then
3217 Set_Expression
3218 (Ass, New_Copy_Tree (Expression (Comp)));
3219 exit;
3220 end if;
3221 Next (Ass);
3222 end loop;
3223 end;
3224 end if;
3225 end;
3226 end if;
3228 Next (Comp);
3229 end loop;
3231 -- If the type is tagged, the tag needs to be initialized (unless we
3232 -- are in VM-mode where tags are implicit). It is done late in the
3233 -- initialization process because in some cases, we call the init
3234 -- proc of an ancestor which will not leave out the right tag.
3236 if Ancestor_Is_Expression then
3237 null;
3239 -- For CPP types we generated a call to the C++ default constructor
3240 -- before the components have been initialized to ensure the proper
3241 -- initialization of the _Tag component (see above).
3243 elsif Is_CPP_Class (Typ) then
3244 null;
3246 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3247 Instr :=
3248 Make_OK_Assignment_Statement (Loc,
3249 Name =>
3250 Make_Selected_Component (Loc,
3251 Prefix => New_Copy_Tree (Target),
3252 Selector_Name =>
3253 New_Occurrence_Of
3254 (First_Tag_Component (Base_Type (Typ)), Loc)),
3256 Expression =>
3257 Unchecked_Convert_To (RTE (RE_Tag),
3258 New_Occurrence_Of
3259 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3260 Loc)));
3262 Append_To (L, Instr);
3264 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3265 -- abstract interfaces we must also initialize the tags of the
3266 -- secondary dispatch tables.
3268 if Has_Interfaces (Base_Type (Typ)) then
3269 Init_Secondary_Tags
3270 (Typ => Base_Type (Typ),
3271 Target => Target,
3272 Stmts_List => L);
3273 end if;
3274 end if;
3276 -- If the controllers have not been initialized yet (by lack of non-
3277 -- discriminant components), let's do it now.
3279 Generate_Finalization_Actions;
3281 return L;
3282 end Build_Record_Aggr_Code;
3284 ---------------------------------------
3285 -- Collect_Initialization_Statements --
3286 ---------------------------------------
3288 procedure Collect_Initialization_Statements
3289 (Obj : Entity_Id;
3290 N : Node_Id;
3291 Node_After : Node_Id)
3293 Loc : constant Source_Ptr := Sloc (N);
3294 Init_Actions : constant List_Id := New_List;
3295 Init_Node : Node_Id;
3296 Comp_Stmt : Node_Id;
3298 begin
3299 -- Nothing to do if Obj is already frozen, as in this case we known we
3300 -- won't need to move the initialization statements about later on.
3302 if Is_Frozen (Obj) then
3303 return;
3304 end if;
3306 Init_Node := N;
3307 while Next (Init_Node) /= Node_After loop
3308 Append_To (Init_Actions, Remove_Next (Init_Node));
3309 end loop;
3311 if not Is_Empty_List (Init_Actions) then
3312 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3313 Insert_Action_After (Init_Node, Comp_Stmt);
3314 Set_Initialization_Statements (Obj, Comp_Stmt);
3315 end if;
3316 end Collect_Initialization_Statements;
3318 -------------------------------
3319 -- Convert_Aggr_In_Allocator --
3320 -------------------------------
3322 procedure Convert_Aggr_In_Allocator
3323 (Alloc : Node_Id;
3324 Decl : Node_Id;
3325 Aggr : Node_Id)
3327 Loc : constant Source_Ptr := Sloc (Aggr);
3328 Typ : constant Entity_Id := Etype (Aggr);
3329 Temp : constant Entity_Id := Defining_Identifier (Decl);
3331 Occ : constant Node_Id :=
3332 Unchecked_Convert_To (Typ,
3333 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3335 begin
3336 if Is_Array_Type (Typ) then
3337 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3339 elsif Has_Default_Init_Comps (Aggr) then
3340 declare
3341 L : constant List_Id := New_List;
3342 Init_Stmts : List_Id;
3344 begin
3345 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3347 if Has_Task (Typ) then
3348 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3349 Insert_Actions (Alloc, L);
3350 else
3351 Insert_Actions (Alloc, Init_Stmts);
3352 end if;
3353 end;
3355 else
3356 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3357 end if;
3358 end Convert_Aggr_In_Allocator;
3360 --------------------------------
3361 -- Convert_Aggr_In_Assignment --
3362 --------------------------------
3364 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3365 Aggr : Node_Id := Expression (N);
3366 Typ : constant Entity_Id := Etype (Aggr);
3367 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3369 begin
3370 if Nkind (Aggr) = N_Qualified_Expression then
3371 Aggr := Expression (Aggr);
3372 end if;
3374 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3375 end Convert_Aggr_In_Assignment;
3377 ---------------------------------
3378 -- Convert_Aggr_In_Object_Decl --
3379 ---------------------------------
3381 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3382 Obj : constant Entity_Id := Defining_Identifier (N);
3383 Aggr : Node_Id := Expression (N);
3384 Loc : constant Source_Ptr := Sloc (Aggr);
3385 Typ : constant Entity_Id := Etype (Aggr);
3386 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3388 function Discriminants_Ok return Boolean;
3389 -- If the object type is constrained, the discriminants in the
3390 -- aggregate must be checked against the discriminants of the subtype.
3391 -- This cannot be done using Apply_Discriminant_Checks because after
3392 -- expansion there is no aggregate left to check.
3394 ----------------------
3395 -- Discriminants_Ok --
3396 ----------------------
3398 function Discriminants_Ok return Boolean is
3399 Cond : Node_Id := Empty;
3400 Check : Node_Id;
3401 D : Entity_Id;
3402 Disc1 : Elmt_Id;
3403 Disc2 : Elmt_Id;
3404 Val1 : Node_Id;
3405 Val2 : Node_Id;
3407 begin
3408 D := First_Discriminant (Typ);
3409 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3410 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3411 while Present (Disc1) and then Present (Disc2) loop
3412 Val1 := Node (Disc1);
3413 Val2 := Node (Disc2);
3415 if not Is_OK_Static_Expression (Val1)
3416 or else not Is_OK_Static_Expression (Val2)
3417 then
3418 Check := Make_Op_Ne (Loc,
3419 Left_Opnd => Duplicate_Subexpr (Val1),
3420 Right_Opnd => Duplicate_Subexpr (Val2));
3422 if No (Cond) then
3423 Cond := Check;
3425 else
3426 Cond := Make_Or_Else (Loc,
3427 Left_Opnd => Cond,
3428 Right_Opnd => Check);
3429 end if;
3431 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3432 Apply_Compile_Time_Constraint_Error (Aggr,
3433 Msg => "incorrect value for discriminant&??",
3434 Reason => CE_Discriminant_Check_Failed,
3435 Ent => D);
3436 return False;
3437 end if;
3439 Next_Discriminant (D);
3440 Next_Elmt (Disc1);
3441 Next_Elmt (Disc2);
3442 end loop;
3444 -- If any discriminant constraint is non-static, emit a check
3446 if Present (Cond) then
3447 Insert_Action (N,
3448 Make_Raise_Constraint_Error (Loc,
3449 Condition => Cond,
3450 Reason => CE_Discriminant_Check_Failed));
3451 end if;
3453 return True;
3454 end Discriminants_Ok;
3456 -- Start of processing for Convert_Aggr_In_Object_Decl
3458 begin
3459 Set_Assignment_OK (Occ);
3461 if Nkind (Aggr) = N_Qualified_Expression then
3462 Aggr := Expression (Aggr);
3463 end if;
3465 if Has_Discriminants (Typ)
3466 and then Typ /= Etype (Obj)
3467 and then Is_Constrained (Etype (Obj))
3468 and then not Discriminants_Ok
3469 then
3470 return;
3471 end if;
3473 -- If the context is an extended return statement, it has its own
3474 -- finalization machinery (i.e. works like a transient scope) and
3475 -- we do not want to create an additional one, because objects on
3476 -- the finalization list of the return must be moved to the caller's
3477 -- finalization list to complete the return.
3479 -- However, if the aggregate is limited, it is built in place, and the
3480 -- controlled components are not assigned to intermediate temporaries
3481 -- so there is no need for a transient scope in this case either.
3483 if Requires_Transient_Scope (Typ)
3484 and then Ekind (Current_Scope) /= E_Return_Statement
3485 and then not Is_Limited_Type (Typ)
3486 then
3487 Establish_Transient_Scope
3488 (Aggr,
3489 Sec_Stack =>
3490 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
3491 end if;
3493 declare
3494 Node_After : constant Node_Id := Next (N);
3495 begin
3496 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3497 Collect_Initialization_Statements (Obj, N, Node_After);
3498 end;
3499 Set_No_Initialization (N);
3500 Initialize_Discriminants (N, Typ);
3501 end Convert_Aggr_In_Object_Decl;
3503 -------------------------------------
3504 -- Convert_Array_Aggr_In_Allocator --
3505 -------------------------------------
3507 procedure Convert_Array_Aggr_In_Allocator
3508 (Decl : Node_Id;
3509 Aggr : Node_Id;
3510 Target : Node_Id)
3512 Aggr_Code : List_Id;
3513 Typ : constant Entity_Id := Etype (Aggr);
3514 Ctyp : constant Entity_Id := Component_Type (Typ);
3516 begin
3517 -- The target is an explicit dereference of the allocated object.
3518 -- Generate component assignments to it, as for an aggregate that
3519 -- appears on the right-hand side of an assignment statement.
3521 Aggr_Code :=
3522 Build_Array_Aggr_Code (Aggr,
3523 Ctype => Ctyp,
3524 Index => First_Index (Typ),
3525 Into => Target,
3526 Scalar_Comp => Is_Scalar_Type (Ctyp));
3528 Insert_Actions_After (Decl, Aggr_Code);
3529 end Convert_Array_Aggr_In_Allocator;
3531 ----------------------------
3532 -- Convert_To_Assignments --
3533 ----------------------------
3535 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
3536 Loc : constant Source_Ptr := Sloc (N);
3537 T : Entity_Id;
3538 Temp : Entity_Id;
3540 Aggr_Code : List_Id;
3541 Instr : Node_Id;
3542 Target_Expr : Node_Id;
3543 Parent_Kind : Node_Kind;
3544 Unc_Decl : Boolean := False;
3545 Parent_Node : Node_Id;
3547 begin
3548 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
3549 pragma Assert (Is_Record_Type (Typ));
3551 Parent_Node := Parent (N);
3552 Parent_Kind := Nkind (Parent_Node);
3554 if Parent_Kind = N_Qualified_Expression then
3556 -- Check if we are in a unconstrained declaration because in this
3557 -- case the current delayed expansion mechanism doesn't work when
3558 -- the declared object size depend on the initializing expr.
3560 begin
3561 Parent_Node := Parent (Parent_Node);
3562 Parent_Kind := Nkind (Parent_Node);
3564 if Parent_Kind = N_Object_Declaration then
3565 Unc_Decl :=
3566 not Is_Entity_Name (Object_Definition (Parent_Node))
3567 or else Has_Discriminants
3568 (Entity (Object_Definition (Parent_Node)))
3569 or else Is_Class_Wide_Type
3570 (Entity (Object_Definition (Parent_Node)));
3571 end if;
3572 end;
3573 end if;
3575 -- Just set the Delay flag in the cases where the transformation will be
3576 -- done top down from above.
3578 if False
3580 -- Internal aggregate (transformed when expanding the parent)
3582 or else Parent_Kind = N_Aggregate
3583 or else Parent_Kind = N_Extension_Aggregate
3584 or else Parent_Kind = N_Component_Association
3586 -- Allocator (see Convert_Aggr_In_Allocator)
3588 or else Parent_Kind = N_Allocator
3590 -- Object declaration (see Convert_Aggr_In_Object_Decl)
3592 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
3594 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
3595 -- assignments in init procs are taken into account.
3597 or else (Parent_Kind = N_Assignment_Statement
3598 and then Inside_Init_Proc)
3600 -- (Ada 2005) An inherently limited type in a return statement, which
3601 -- will be handled in a build-in-place fashion, and may be rewritten
3602 -- as an extended return and have its own finalization machinery.
3603 -- In the case of a simple return, the aggregate needs to be delayed
3604 -- until the scope for the return statement has been created, so
3605 -- that any finalization chain will be associated with that scope.
3606 -- For extended returns, we delay expansion to avoid the creation
3607 -- of an unwanted transient scope that could result in premature
3608 -- finalization of the return object (which is built in place
3609 -- within the caller's scope).
3611 or else
3612 (Is_Limited_View (Typ)
3613 and then
3614 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
3615 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
3616 then
3617 Set_Expansion_Delayed (N);
3618 return;
3619 end if;
3621 -- Otherwise, if a transient scope is required, create it now. If we
3622 -- are within an initialization procedure do not create such, because
3623 -- the target of the assignment must not be declared within a local
3624 -- block, and because cleanup will take place on return from the
3625 -- initialization procedure.
3626 -- Should the condition be more restrictive ???
3628 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
3629 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
3630 end if;
3632 -- If the aggregate is non-limited, create a temporary. If it is limited
3633 -- and context is an assignment, this is a subaggregate for an enclosing
3634 -- aggregate being expanded. It must be built in place, so use target of
3635 -- the current assignment.
3637 if Is_Limited_Type (Typ)
3638 and then Nkind (Parent (N)) = N_Assignment_Statement
3639 then
3640 Target_Expr := New_Copy_Tree (Name (Parent (N)));
3641 Insert_Actions (Parent (N),
3642 Build_Record_Aggr_Code (N, Typ, Target_Expr));
3643 Rewrite (Parent (N), Make_Null_Statement (Loc));
3645 else
3646 Temp := Make_Temporary (Loc, 'A', N);
3648 -- If the type inherits unknown discriminants, use the view with
3649 -- known discriminants if available.
3651 if Has_Unknown_Discriminants (Typ)
3652 and then Present (Underlying_Record_View (Typ))
3653 then
3654 T := Underlying_Record_View (Typ);
3655 else
3656 T := Typ;
3657 end if;
3659 Instr :=
3660 Make_Object_Declaration (Loc,
3661 Defining_Identifier => Temp,
3662 Object_Definition => New_Occurrence_Of (T, Loc));
3664 Set_No_Initialization (Instr);
3665 Insert_Action (N, Instr);
3666 Initialize_Discriminants (Instr, T);
3668 Target_Expr := New_Occurrence_Of (Temp, Loc);
3669 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
3671 -- Save the last assignment statement associated with the aggregate
3672 -- when building a controlled object. This reference is utilized by
3673 -- the finalization machinery when marking an object as successfully
3674 -- initialized.
3676 if Needs_Finalization (T) then
3677 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
3678 end if;
3680 Insert_Actions (N, Aggr_Code);
3681 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3682 Analyze_And_Resolve (N, T);
3683 end if;
3684 end Convert_To_Assignments;
3686 ---------------------------
3687 -- Convert_To_Positional --
3688 ---------------------------
3690 procedure Convert_To_Positional
3691 (N : Node_Id;
3692 Max_Others_Replicate : Nat := 5;
3693 Handle_Bit_Packed : Boolean := False)
3695 Typ : constant Entity_Id := Etype (N);
3697 Static_Components : Boolean := True;
3699 procedure Check_Static_Components;
3700 -- Check whether all components of the aggregate are compile-time known
3701 -- values, and can be passed as is to the back-end without further
3702 -- expansion.
3704 function Flatten
3705 (N : Node_Id;
3706 Ix : Node_Id;
3707 Ixb : Node_Id) return Boolean;
3708 -- Convert the aggregate into a purely positional form if possible. On
3709 -- entry the bounds of all dimensions are known to be static, and the
3710 -- total number of components is safe enough to expand.
3712 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
3713 -- Return True iff the array N is flat (which is not trivial in the case
3714 -- of multidimensional aggregates).
3716 -----------------------------
3717 -- Check_Static_Components --
3718 -----------------------------
3720 -- Could use some comments in this body ???
3722 procedure Check_Static_Components is
3723 Expr : Node_Id;
3725 begin
3726 Static_Components := True;
3728 if Nkind (N) = N_String_Literal then
3729 null;
3731 elsif Present (Expressions (N)) then
3732 Expr := First (Expressions (N));
3733 while Present (Expr) loop
3734 if Nkind (Expr) /= N_Aggregate
3735 or else not Compile_Time_Known_Aggregate (Expr)
3736 or else Expansion_Delayed (Expr)
3737 then
3738 Static_Components := False;
3739 exit;
3740 end if;
3742 Next (Expr);
3743 end loop;
3744 end if;
3746 if Nkind (N) = N_Aggregate
3747 and then Present (Component_Associations (N))
3748 then
3749 Expr := First (Component_Associations (N));
3750 while Present (Expr) loop
3751 if Nkind_In (Expression (Expr), N_Integer_Literal,
3752 N_Real_Literal)
3753 then
3754 null;
3756 elsif Is_Entity_Name (Expression (Expr))
3757 and then Present (Entity (Expression (Expr)))
3758 and then Ekind (Entity (Expression (Expr))) =
3759 E_Enumeration_Literal
3760 then
3761 null;
3763 elsif Nkind (Expression (Expr)) /= N_Aggregate
3764 or else not Compile_Time_Known_Aggregate (Expression (Expr))
3765 or else Expansion_Delayed (Expression (Expr))
3766 then
3767 Static_Components := False;
3768 exit;
3769 end if;
3771 Next (Expr);
3772 end loop;
3773 end if;
3774 end Check_Static_Components;
3776 -------------
3777 -- Flatten --
3778 -------------
3780 function Flatten
3781 (N : Node_Id;
3782 Ix : Node_Id;
3783 Ixb : Node_Id) return Boolean
3785 Loc : constant Source_Ptr := Sloc (N);
3786 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
3787 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
3788 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
3789 Lov : Uint;
3790 Hiv : Uint;
3792 Others_Present : Boolean := False;
3794 begin
3795 if Nkind (Original_Node (N)) = N_String_Literal then
3796 return True;
3797 end if;
3799 if not Compile_Time_Known_Value (Lo)
3800 or else not Compile_Time_Known_Value (Hi)
3801 then
3802 return False;
3803 end if;
3805 Lov := Expr_Value (Lo);
3806 Hiv := Expr_Value (Hi);
3808 -- Check if there is an others choice
3810 if Present (Component_Associations (N)) then
3811 declare
3812 Assoc : Node_Id;
3813 Choice : Node_Id;
3815 begin
3816 Assoc := First (Component_Associations (N));
3817 while Present (Assoc) loop
3819 -- If this is a box association, flattening is in general
3820 -- not possible because at this point we cannot tell if the
3821 -- default is static or even exists.
3823 if Box_Present (Assoc) then
3824 return False;
3825 end if;
3827 Choice := First (Choices (Assoc));
3829 while Present (Choice) loop
3830 if Nkind (Choice) = N_Others_Choice then
3831 Others_Present := True;
3832 end if;
3834 Next (Choice);
3835 end loop;
3837 Next (Assoc);
3838 end loop;
3839 end;
3840 end if;
3842 -- If the low bound is not known at compile time and others is not
3843 -- present we can proceed since the bounds can be obtained from the
3844 -- aggregate.
3846 if Hiv < Lov
3847 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
3848 then
3849 return False;
3850 end if;
3852 -- Determine if set of alternatives is suitable for conversion and
3853 -- build an array containing the values in sequence.
3855 declare
3856 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
3857 of Node_Id := (others => Empty);
3858 -- The values in the aggregate sorted appropriately
3860 Vlist : List_Id;
3861 -- Same data as Vals in list form
3863 Rep_Count : Nat;
3864 -- Used to validate Max_Others_Replicate limit
3866 Elmt : Node_Id;
3867 Num : Int := UI_To_Int (Lov);
3868 Choice_Index : Int;
3869 Choice : Node_Id;
3870 Lo, Hi : Node_Id;
3872 begin
3873 if Present (Expressions (N)) then
3874 Elmt := First (Expressions (N));
3875 while Present (Elmt) loop
3876 if Nkind (Elmt) = N_Aggregate
3877 and then Present (Next_Index (Ix))
3878 and then
3879 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
3880 then
3881 return False;
3882 end if;
3884 Vals (Num) := Relocate_Node (Elmt);
3885 Num := Num + 1;
3887 Next (Elmt);
3888 end loop;
3889 end if;
3891 if No (Component_Associations (N)) then
3892 return True;
3893 end if;
3895 Elmt := First (Component_Associations (N));
3897 if Nkind (Expression (Elmt)) = N_Aggregate then
3898 if Present (Next_Index (Ix))
3899 and then
3900 not Flatten
3901 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
3902 then
3903 return False;
3904 end if;
3905 end if;
3907 Component_Loop : while Present (Elmt) loop
3908 Choice := First (Choices (Elmt));
3909 Choice_Loop : while Present (Choice) loop
3911 -- If we have an others choice, fill in the missing elements
3912 -- subject to the limit established by Max_Others_Replicate.
3914 if Nkind (Choice) = N_Others_Choice then
3915 Rep_Count := 0;
3917 for J in Vals'Range loop
3918 if No (Vals (J)) then
3919 Vals (J) := New_Copy_Tree (Expression (Elmt));
3920 Rep_Count := Rep_Count + 1;
3922 -- Check for maximum others replication. Note that
3923 -- we skip this test if either of the restrictions
3924 -- No_Elaboration_Code or No_Implicit_Loops is
3925 -- active, if this is a preelaborable unit or
3926 -- a predefined unit, or if the unit must be
3927 -- placed in data memory. This also ensures that
3928 -- predefined units get the same level of constant
3929 -- folding in Ada 95 and Ada 2005, where their
3930 -- categorization has changed.
3932 declare
3933 P : constant Entity_Id :=
3934 Cunit_Entity (Current_Sem_Unit);
3936 begin
3937 -- Check if duplication OK and if so continue
3938 -- processing.
3940 if Restriction_Active (No_Elaboration_Code)
3941 or else Restriction_Active (No_Implicit_Loops)
3942 or else
3943 (Ekind (Current_Scope) = E_Package
3944 and then Static_Elaboration_Desired
3945 (Current_Scope))
3946 or else Is_Preelaborated (P)
3947 or else (Ekind (P) = E_Package_Body
3948 and then
3949 Is_Preelaborated (Spec_Entity (P)))
3950 or else
3951 Is_Predefined_File_Name
3952 (Unit_File_Name (Get_Source_Unit (P)))
3953 then
3954 null;
3956 -- If duplication not OK, then we return False
3957 -- if the replication count is too high
3959 elsif Rep_Count > Max_Others_Replicate then
3960 return False;
3962 -- Continue on if duplication not OK, but the
3963 -- replication count is not excessive.
3965 else
3966 null;
3967 end if;
3968 end;
3969 end if;
3970 end loop;
3972 exit Component_Loop;
3974 -- Case of a subtype mark, identifier or expanded name
3976 elsif Is_Entity_Name (Choice)
3977 and then Is_Type (Entity (Choice))
3978 then
3979 Lo := Type_Low_Bound (Etype (Choice));
3980 Hi := Type_High_Bound (Etype (Choice));
3982 -- Case of subtype indication
3984 elsif Nkind (Choice) = N_Subtype_Indication then
3985 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
3986 Hi := High_Bound (Range_Expression (Constraint (Choice)));
3988 -- Case of a range
3990 elsif Nkind (Choice) = N_Range then
3991 Lo := Low_Bound (Choice);
3992 Hi := High_Bound (Choice);
3994 -- Normal subexpression case
3996 else pragma Assert (Nkind (Choice) in N_Subexpr);
3997 if not Compile_Time_Known_Value (Choice) then
3998 return False;
4000 else
4001 Choice_Index := UI_To_Int (Expr_Value (Choice));
4003 if Choice_Index in Vals'Range then
4004 Vals (Choice_Index) :=
4005 New_Copy_Tree (Expression (Elmt));
4006 goto Continue;
4008 -- Choice is statically out-of-range, will be
4009 -- rewritten to raise Constraint_Error.
4011 else
4012 return False;
4013 end if;
4014 end if;
4015 end if;
4017 -- Range cases merge with Lo,Hi set
4019 if not Compile_Time_Known_Value (Lo)
4020 or else
4021 not Compile_Time_Known_Value (Hi)
4022 then
4023 return False;
4025 else
4026 for J in UI_To_Int (Expr_Value (Lo)) ..
4027 UI_To_Int (Expr_Value (Hi))
4028 loop
4029 Vals (J) := New_Copy_Tree (Expression (Elmt));
4030 end loop;
4031 end if;
4033 <<Continue>>
4034 Next (Choice);
4035 end loop Choice_Loop;
4037 Next (Elmt);
4038 end loop Component_Loop;
4040 -- If we get here the conversion is possible
4042 Vlist := New_List;
4043 for J in Vals'Range loop
4044 Append (Vals (J), Vlist);
4045 end loop;
4047 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4048 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4049 return True;
4050 end;
4051 end Flatten;
4053 -------------
4054 -- Is_Flat --
4055 -------------
4057 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4058 Elmt : Node_Id;
4060 begin
4061 if Dims = 0 then
4062 return True;
4064 elsif Nkind (N) = N_Aggregate then
4065 if Present (Component_Associations (N)) then
4066 return False;
4068 else
4069 Elmt := First (Expressions (N));
4070 while Present (Elmt) loop
4071 if not Is_Flat (Elmt, Dims - 1) then
4072 return False;
4073 end if;
4075 Next (Elmt);
4076 end loop;
4078 return True;
4079 end if;
4080 else
4081 return True;
4082 end if;
4083 end Is_Flat;
4085 -- Start of processing for Convert_To_Positional
4087 begin
4088 -- Only convert to positional when generating C in case of an
4089 -- object declaration, this is the only case where aggregates are
4090 -- supported in C.
4092 if Modify_Tree_For_C and then not In_Object_Declaration (N) then
4093 return;
4094 end if;
4096 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4097 -- components because in this case will need to call the corresponding
4098 -- IP procedure.
4100 if Has_Default_Init_Comps (N) then
4101 return;
4102 end if;
4104 if Is_Flat (N, Number_Dimensions (Typ)) then
4105 return;
4106 end if;
4108 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4109 return;
4110 end if;
4112 -- Do not convert to positional if controlled components are involved
4113 -- since these require special processing
4115 if Has_Controlled_Component (Typ) then
4116 return;
4117 end if;
4119 Check_Static_Components;
4121 -- If the size is known, or all the components are static, try to
4122 -- build a fully positional aggregate.
4124 -- The size of the type may not be known for an aggregate with
4125 -- discriminated array components, but if the components are static
4126 -- it is still possible to verify statically that the length is
4127 -- compatible with the upper bound of the type, and therefore it is
4128 -- worth flattening such aggregates as well.
4130 -- For now the back-end expands these aggregates into individual
4131 -- assignments to the target anyway, but it is conceivable that
4132 -- it will eventually be able to treat such aggregates statically???
4134 if Aggr_Size_OK (N, Typ)
4135 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4136 then
4137 if Static_Components then
4138 Set_Compile_Time_Known_Aggregate (N);
4139 Set_Expansion_Delayed (N, False);
4140 end if;
4142 Analyze_And_Resolve (N, Typ);
4143 end if;
4145 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4146 -- that will still require initialization code.
4148 if (Ekind (Current_Scope) = E_Package
4149 and then Static_Elaboration_Desired (Current_Scope))
4150 and then Nkind (Parent (N)) = N_Object_Declaration
4151 then
4152 declare
4153 Expr : Node_Id;
4155 begin
4156 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4157 Expr := First (Expressions (N));
4158 while Present (Expr) loop
4159 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
4160 or else
4161 (Is_Entity_Name (Expr)
4162 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
4163 then
4164 null;
4166 else
4167 Error_Msg_N
4168 ("non-static object requires elaboration code??", N);
4169 exit;
4170 end if;
4172 Next (Expr);
4173 end loop;
4175 if Present (Component_Associations (N)) then
4176 Error_Msg_N ("object requires elaboration code??", N);
4177 end if;
4178 end if;
4179 end;
4180 end if;
4181 end Convert_To_Positional;
4183 ----------------------------
4184 -- Expand_Array_Aggregate --
4185 ----------------------------
4187 -- Array aggregate expansion proceeds as follows:
4189 -- 1. If requested we generate code to perform all the array aggregate
4190 -- bound checks, specifically
4192 -- (a) Check that the index range defined by aggregate bounds is
4193 -- compatible with corresponding index subtype.
4195 -- (b) If an others choice is present check that no aggregate
4196 -- index is outside the bounds of the index constraint.
4198 -- (c) For multidimensional arrays make sure that all subaggregates
4199 -- corresponding to the same dimension have the same bounds.
4201 -- 2. Check for packed array aggregate which can be converted to a
4202 -- constant so that the aggregate disappears completely.
4204 -- 3. Check case of nested aggregate. Generally nested aggregates are
4205 -- handled during the processing of the parent aggregate.
4207 -- 4. Check if the aggregate can be statically processed. If this is the
4208 -- case pass it as is to Gigi. Note that a necessary condition for
4209 -- static processing is that the aggregate be fully positional.
4211 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4212 -- a temporary) then mark the aggregate as such and return. Otherwise
4213 -- create a new temporary and generate the appropriate initialization
4214 -- code.
4216 procedure Expand_Array_Aggregate (N : Node_Id) is
4217 Loc : constant Source_Ptr := Sloc (N);
4219 Typ : constant Entity_Id := Etype (N);
4220 Ctyp : constant Entity_Id := Component_Type (Typ);
4221 -- Typ is the correct constrained array subtype of the aggregate
4222 -- Ctyp is the corresponding component type.
4224 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4225 -- Number of aggregate index dimensions
4227 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4228 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4229 -- Low and High bounds of the constraint for each aggregate index
4231 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4232 -- The type of each index
4234 In_Place_Assign_OK_For_Declaration : Boolean := False;
4235 -- True if we are to generate an in place assignment for a declaration
4237 Maybe_In_Place_OK : Boolean;
4238 -- If the type is neither controlled nor packed and the aggregate
4239 -- is the expression in an assignment, assignment in place may be
4240 -- possible, provided other conditions are met on the LHS.
4242 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4243 (others => False);
4244 -- If Others_Present (J) is True, then there is an others choice in one
4245 -- of the subaggregates of N at dimension J.
4247 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4248 -- Returns true if an aggregate assignment can be done by the back end
4250 procedure Build_Constrained_Type (Positional : Boolean);
4251 -- If the subtype is not static or unconstrained, build a constrained
4252 -- type using the computable sizes of the aggregate and its sub-
4253 -- aggregates.
4255 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4256 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4257 -- by Index_Bounds.
4259 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4260 -- Checks that in a multidimensional array aggregate all subaggregates
4261 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4262 -- an array subaggregate. Dim is the dimension corresponding to the
4263 -- subaggregate.
4265 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4266 -- Computes the values of array Others_Present. Sub_Aggr is the array
4267 -- subaggregate we start the computation from. Dim is the dimension
4268 -- corresponding to the subaggregate.
4270 function In_Place_Assign_OK return Boolean;
4271 -- Simple predicate to determine whether an aggregate assignment can
4272 -- be done in place, because none of the new values can depend on the
4273 -- components of the target of the assignment.
4275 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4276 -- Checks that if an others choice is present in any subaggregate, no
4277 -- aggregate index is outside the bounds of the index constraint.
4278 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4279 -- to the subaggregate.
4281 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4282 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4283 -- built directly into the target of the assignment it must be free
4284 -- of side effects.
4286 ------------------------------------
4287 -- Aggr_Assignment_OK_For_Backend --
4288 ------------------------------------
4290 -- Backend processing by Gigi/gcc is possible only if all the following
4291 -- conditions are met:
4293 -- 1. N consists of a single OTHERS choice, possibly recursively
4295 -- 2. The array type is not packed
4297 -- 3. The array type has no atomic components
4299 -- 4. The array type has no null ranges (the purpose of this is to
4300 -- avoid a bogus warning for an out-of-range value).
4302 -- 5. The component type is discrete
4304 -- 6. The component size is Storage_Unit or the value is of the form
4305 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4306 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4307 -- the 8-bit value M, concatenated together.
4309 -- The ultimate goal is to generate a call to a fast memset routine
4310 -- specifically optimized for the target.
4312 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4313 Ctyp : Entity_Id;
4314 Index : Entity_Id;
4315 Expr : Node_Id := N;
4316 Low : Node_Id;
4317 High : Node_Id;
4318 Remainder : Uint;
4319 Value : Uint;
4320 Nunits : Nat;
4322 begin
4323 -- Recurse as far as possible to find the innermost component type
4325 Ctyp := Etype (N);
4326 while Is_Array_Type (Ctyp) loop
4327 if Nkind (Expr) /= N_Aggregate
4328 or else not Is_Others_Aggregate (Expr)
4329 then
4330 return False;
4331 end if;
4333 if Present (Packed_Array_Impl_Type (Ctyp)) then
4334 return False;
4335 end if;
4337 if Has_Atomic_Components (Ctyp) then
4338 return False;
4339 end if;
4341 Index := First_Index (Ctyp);
4342 while Present (Index) loop
4343 Get_Index_Bounds (Index, Low, High);
4345 if Is_Null_Range (Low, High) then
4346 return False;
4347 end if;
4349 Next_Index (Index);
4350 end loop;
4352 Expr := Expression (First (Component_Associations (Expr)));
4354 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4355 if Nkind (Expr) /= N_Aggregate
4356 or else not Is_Others_Aggregate (Expr)
4357 then
4358 return False;
4359 end if;
4361 Expr := Expression (First (Component_Associations (Expr)));
4362 end loop;
4364 Ctyp := Component_Type (Ctyp);
4366 if Is_Atomic_Or_VFA (Ctyp) then
4367 return False;
4368 end if;
4369 end loop;
4371 if not Is_Discrete_Type (Ctyp) then
4372 return False;
4373 end if;
4375 -- The expression needs to be analyzed if True is returned
4377 Analyze_And_Resolve (Expr, Ctyp);
4379 -- The back end uses the Esize as the precision of the type
4381 Nunits := UI_To_Int (Esize (Ctyp)) / System_Storage_Unit;
4383 if Nunits = 1 then
4384 return True;
4385 end if;
4387 if not Compile_Time_Known_Value (Expr) then
4388 return False;
4389 end if;
4391 Value := Expr_Value (Expr);
4393 if Has_Biased_Representation (Ctyp) then
4394 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
4395 end if;
4397 -- Values 0 and -1 immediately satisfy the last check
4399 if Value = Uint_0 or else Value = Uint_Minus_1 then
4400 return True;
4401 end if;
4403 -- We need to work with an unsigned value
4405 if Value < 0 then
4406 Value := Value + 2**(System_Storage_Unit * Nunits);
4407 end if;
4409 Remainder := Value rem 2**System_Storage_Unit;
4411 for J in 1 .. Nunits - 1 loop
4412 Value := Value / 2**System_Storage_Unit;
4414 if Value rem 2**System_Storage_Unit /= Remainder then
4415 return False;
4416 end if;
4417 end loop;
4419 return True;
4420 end Aggr_Assignment_OK_For_Backend;
4422 ----------------------------
4423 -- Build_Constrained_Type --
4424 ----------------------------
4426 procedure Build_Constrained_Type (Positional : Boolean) is
4427 Loc : constant Source_Ptr := Sloc (N);
4428 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4429 Comp : Node_Id;
4430 Decl : Node_Id;
4431 Typ : constant Entity_Id := Etype (N);
4432 Indexes : constant List_Id := New_List;
4433 Num : Nat;
4434 Sub_Agg : Node_Id;
4436 begin
4437 -- If the aggregate is purely positional, all its subaggregates
4438 -- have the same size. We collect the dimensions from the first
4439 -- subaggregate at each level.
4441 if Positional then
4442 Sub_Agg := N;
4444 for D in 1 .. Number_Dimensions (Typ) loop
4445 Sub_Agg := First (Expressions (Sub_Agg));
4447 Comp := Sub_Agg;
4448 Num := 0;
4449 while Present (Comp) loop
4450 Num := Num + 1;
4451 Next (Comp);
4452 end loop;
4454 Append_To (Indexes,
4455 Make_Range (Loc,
4456 Low_Bound => Make_Integer_Literal (Loc, 1),
4457 High_Bound => Make_Integer_Literal (Loc, Num)));
4458 end loop;
4460 else
4461 -- We know the aggregate type is unconstrained and the aggregate
4462 -- is not processable by the back end, therefore not necessarily
4463 -- positional. Retrieve each dimension bounds (computed earlier).
4465 for D in 1 .. Number_Dimensions (Typ) loop
4466 Append_To (Indexes,
4467 Make_Range (Loc,
4468 Low_Bound => Aggr_Low (D),
4469 High_Bound => Aggr_High (D)));
4470 end loop;
4471 end if;
4473 Decl :=
4474 Make_Full_Type_Declaration (Loc,
4475 Defining_Identifier => Agg_Type,
4476 Type_Definition =>
4477 Make_Constrained_Array_Definition (Loc,
4478 Discrete_Subtype_Definitions => Indexes,
4479 Component_Definition =>
4480 Make_Component_Definition (Loc,
4481 Aliased_Present => False,
4482 Subtype_Indication =>
4483 New_Occurrence_Of (Component_Type (Typ), Loc))));
4485 Insert_Action (N, Decl);
4486 Analyze (Decl);
4487 Set_Etype (N, Agg_Type);
4488 Set_Is_Itype (Agg_Type);
4489 Freeze_Itype (Agg_Type, N);
4490 end Build_Constrained_Type;
4492 ------------------
4493 -- Check_Bounds --
4494 ------------------
4496 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
4497 Aggr_Lo : Node_Id;
4498 Aggr_Hi : Node_Id;
4500 Ind_Lo : Node_Id;
4501 Ind_Hi : Node_Id;
4503 Cond : Node_Id := Empty;
4505 begin
4506 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
4507 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
4509 -- Generate the following test:
4511 -- [constraint_error when
4512 -- Aggr_Lo <= Aggr_Hi and then
4513 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
4515 -- As an optimization try to see if some tests are trivially vacuous
4516 -- because we are comparing an expression against itself.
4518 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
4519 Cond := Empty;
4521 elsif Aggr_Hi = Ind_Hi then
4522 Cond :=
4523 Make_Op_Lt (Loc,
4524 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4525 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
4527 elsif Aggr_Lo = Ind_Lo then
4528 Cond :=
4529 Make_Op_Gt (Loc,
4530 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4531 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
4533 else
4534 Cond :=
4535 Make_Or_Else (Loc,
4536 Left_Opnd =>
4537 Make_Op_Lt (Loc,
4538 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4539 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
4541 Right_Opnd =>
4542 Make_Op_Gt (Loc,
4543 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4544 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
4545 end if;
4547 if Present (Cond) then
4548 Cond :=
4549 Make_And_Then (Loc,
4550 Left_Opnd =>
4551 Make_Op_Le (Loc,
4552 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4553 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
4555 Right_Opnd => Cond);
4557 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
4558 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
4559 Insert_Action (N,
4560 Make_Raise_Constraint_Error (Loc,
4561 Condition => Cond,
4562 Reason => CE_Range_Check_Failed));
4563 end if;
4564 end Check_Bounds;
4566 ----------------------------
4567 -- Check_Same_Aggr_Bounds --
4568 ----------------------------
4570 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
4571 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
4572 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
4573 -- The bounds of this specific subaggregate
4575 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4576 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4577 -- The bounds of the aggregate for this dimension
4579 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4580 -- The index type for this dimension.xxx
4582 Cond : Node_Id := Empty;
4583 Assoc : Node_Id;
4584 Expr : Node_Id;
4586 begin
4587 -- If index checks are on generate the test
4589 -- [constraint_error when
4590 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
4592 -- As an optimization try to see if some tests are trivially vacuos
4593 -- because we are comparing an expression against itself. Also for
4594 -- the first dimension the test is trivially vacuous because there
4595 -- is just one aggregate for dimension 1.
4597 if Index_Checks_Suppressed (Ind_Typ) then
4598 Cond := Empty;
4600 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
4601 then
4602 Cond := Empty;
4604 elsif Aggr_Hi = Sub_Hi then
4605 Cond :=
4606 Make_Op_Ne (Loc,
4607 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4608 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
4610 elsif Aggr_Lo = Sub_Lo then
4611 Cond :=
4612 Make_Op_Ne (Loc,
4613 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
4614 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
4616 else
4617 Cond :=
4618 Make_Or_Else (Loc,
4619 Left_Opnd =>
4620 Make_Op_Ne (Loc,
4621 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
4622 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
4624 Right_Opnd =>
4625 Make_Op_Ne (Loc,
4626 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
4627 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
4628 end if;
4630 if Present (Cond) then
4631 Insert_Action (N,
4632 Make_Raise_Constraint_Error (Loc,
4633 Condition => Cond,
4634 Reason => CE_Length_Check_Failed));
4635 end if;
4637 -- Now look inside the subaggregate to see if there is more work
4639 if Dim < Aggr_Dimension then
4641 -- Process positional components
4643 if Present (Expressions (Sub_Aggr)) then
4644 Expr := First (Expressions (Sub_Aggr));
4645 while Present (Expr) loop
4646 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4647 Next (Expr);
4648 end loop;
4649 end if;
4651 -- Process component associations
4653 if Present (Component_Associations (Sub_Aggr)) then
4654 Assoc := First (Component_Associations (Sub_Aggr));
4655 while Present (Assoc) loop
4656 Expr := Expression (Assoc);
4657 Check_Same_Aggr_Bounds (Expr, Dim + 1);
4658 Next (Assoc);
4659 end loop;
4660 end if;
4661 end if;
4662 end Check_Same_Aggr_Bounds;
4664 ----------------------------
4665 -- Compute_Others_Present --
4666 ----------------------------
4668 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
4669 Assoc : Node_Id;
4670 Expr : Node_Id;
4672 begin
4673 if Present (Component_Associations (Sub_Aggr)) then
4674 Assoc := Last (Component_Associations (Sub_Aggr));
4676 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
4677 Others_Present (Dim) := True;
4678 end if;
4679 end if;
4681 -- Now look inside the subaggregate to see if there is more work
4683 if Dim < Aggr_Dimension then
4685 -- Process positional components
4687 if Present (Expressions (Sub_Aggr)) then
4688 Expr := First (Expressions (Sub_Aggr));
4689 while Present (Expr) loop
4690 Compute_Others_Present (Expr, Dim + 1);
4691 Next (Expr);
4692 end loop;
4693 end if;
4695 -- Process component associations
4697 if Present (Component_Associations (Sub_Aggr)) then
4698 Assoc := First (Component_Associations (Sub_Aggr));
4699 while Present (Assoc) loop
4700 Expr := Expression (Assoc);
4701 Compute_Others_Present (Expr, Dim + 1);
4702 Next (Assoc);
4703 end loop;
4704 end if;
4705 end if;
4706 end Compute_Others_Present;
4708 ------------------------
4709 -- In_Place_Assign_OK --
4710 ------------------------
4712 function In_Place_Assign_OK return Boolean is
4713 Aggr_In : Node_Id;
4714 Aggr_Lo : Node_Id;
4715 Aggr_Hi : Node_Id;
4716 Obj_In : Node_Id;
4717 Obj_Lo : Node_Id;
4718 Obj_Hi : Node_Id;
4720 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
4721 -- Check recursively that each component of a (sub)aggregate does not
4722 -- depend on the variable being assigned to.
4724 function Safe_Component (Expr : Node_Id) return Boolean;
4725 -- Verify that an expression cannot depend on the variable being
4726 -- assigned to. Room for improvement here (but less than before).
4728 --------------------
4729 -- Safe_Aggregate --
4730 --------------------
4732 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
4733 Expr : Node_Id;
4735 begin
4736 if Present (Expressions (Aggr)) then
4737 Expr := First (Expressions (Aggr));
4738 while Present (Expr) loop
4739 if Nkind (Expr) = N_Aggregate then
4740 if not Safe_Aggregate (Expr) then
4741 return False;
4742 end if;
4744 elsif not Safe_Component (Expr) then
4745 return False;
4746 end if;
4748 Next (Expr);
4749 end loop;
4750 end if;
4752 if Present (Component_Associations (Aggr)) then
4753 Expr := First (Component_Associations (Aggr));
4754 while Present (Expr) loop
4755 if Nkind (Expression (Expr)) = N_Aggregate then
4756 if not Safe_Aggregate (Expression (Expr)) then
4757 return False;
4758 end if;
4760 -- If association has a box, no way to determine yet
4761 -- whether default can be assigned in place.
4763 elsif Box_Present (Expr) then
4764 return False;
4766 elsif not Safe_Component (Expression (Expr)) then
4767 return False;
4768 end if;
4770 Next (Expr);
4771 end loop;
4772 end if;
4774 return True;
4775 end Safe_Aggregate;
4777 --------------------
4778 -- Safe_Component --
4779 --------------------
4781 function Safe_Component (Expr : Node_Id) return Boolean is
4782 Comp : Node_Id := Expr;
4784 function Check_Component (Comp : Node_Id) return Boolean;
4785 -- Do the recursive traversal, after copy
4787 ---------------------
4788 -- Check_Component --
4789 ---------------------
4791 function Check_Component (Comp : Node_Id) return Boolean is
4792 begin
4793 if Is_Overloaded (Comp) then
4794 return False;
4795 end if;
4797 return Compile_Time_Known_Value (Comp)
4799 or else (Is_Entity_Name (Comp)
4800 and then Present (Entity (Comp))
4801 and then No (Renamed_Object (Entity (Comp))))
4803 or else (Nkind (Comp) = N_Attribute_Reference
4804 and then Check_Component (Prefix (Comp)))
4806 or else (Nkind (Comp) in N_Binary_Op
4807 and then Check_Component (Left_Opnd (Comp))
4808 and then Check_Component (Right_Opnd (Comp)))
4810 or else (Nkind (Comp) in N_Unary_Op
4811 and then Check_Component (Right_Opnd (Comp)))
4813 or else (Nkind (Comp) = N_Selected_Component
4814 and then Check_Component (Prefix (Comp)))
4816 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
4817 and then Check_Component (Expression (Comp)));
4818 end Check_Component;
4820 -- Start of processing for Safe_Component
4822 begin
4823 -- If the component appears in an association that may correspond
4824 -- to more than one element, it is not analyzed before expansion
4825 -- into assignments, to avoid side effects. We analyze, but do not
4826 -- resolve the copy, to obtain sufficient entity information for
4827 -- the checks that follow. If component is overloaded we assume
4828 -- an unsafe function call.
4830 if not Analyzed (Comp) then
4831 if Is_Overloaded (Expr) then
4832 return False;
4834 elsif Nkind (Expr) = N_Aggregate
4835 and then not Is_Others_Aggregate (Expr)
4836 then
4837 return False;
4839 elsif Nkind (Expr) = N_Allocator then
4841 -- For now, too complex to analyze
4843 return False;
4844 end if;
4846 Comp := New_Copy_Tree (Expr);
4847 Set_Parent (Comp, Parent (Expr));
4848 Analyze (Comp);
4849 end if;
4851 if Nkind (Comp) = N_Aggregate then
4852 return Safe_Aggregate (Comp);
4853 else
4854 return Check_Component (Comp);
4855 end if;
4856 end Safe_Component;
4858 -- Start of processing for In_Place_Assign_OK
4860 begin
4861 if Present (Component_Associations (N)) then
4863 -- On assignment, sliding can take place, so we cannot do the
4864 -- assignment in place unless the bounds of the aggregate are
4865 -- statically equal to those of the target.
4867 -- If the aggregate is given by an others choice, the bounds are
4868 -- derived from the left-hand side, and the assignment is safe if
4869 -- the expression is.
4871 if Is_Others_Aggregate (N) then
4872 return
4873 Safe_Component
4874 (Expression (First (Component_Associations (N))));
4875 end if;
4877 Aggr_In := First_Index (Etype (N));
4879 if Nkind (Parent (N)) = N_Assignment_Statement then
4880 Obj_In := First_Index (Etype (Name (Parent (N))));
4882 else
4883 -- Context is an allocator. Check bounds of aggregate against
4884 -- given type in qualified expression.
4886 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
4887 Obj_In :=
4888 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
4889 end if;
4891 while Present (Aggr_In) loop
4892 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
4893 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
4895 if not Compile_Time_Known_Value (Aggr_Lo)
4896 or else not Compile_Time_Known_Value (Aggr_Hi)
4897 or else not Compile_Time_Known_Value (Obj_Lo)
4898 or else not Compile_Time_Known_Value (Obj_Hi)
4899 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
4900 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
4901 then
4902 return False;
4903 end if;
4905 Next_Index (Aggr_In);
4906 Next_Index (Obj_In);
4907 end loop;
4908 end if;
4910 -- Now check the component values themselves
4912 return Safe_Aggregate (N);
4913 end In_Place_Assign_OK;
4915 ------------------
4916 -- Others_Check --
4917 ------------------
4919 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
4920 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
4921 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
4922 -- The bounds of the aggregate for this dimension
4924 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
4925 -- The index type for this dimension
4927 Need_To_Check : Boolean := False;
4929 Choices_Lo : Node_Id := Empty;
4930 Choices_Hi : Node_Id := Empty;
4931 -- The lowest and highest discrete choices for a named subaggregate
4933 Nb_Choices : Int := -1;
4934 -- The number of discrete non-others choices in this subaggregate
4936 Nb_Elements : Uint := Uint_0;
4937 -- The number of elements in a positional aggregate
4939 Cond : Node_Id := Empty;
4941 Assoc : Node_Id;
4942 Choice : Node_Id;
4943 Expr : Node_Id;
4945 begin
4946 -- Check if we have an others choice. If we do make sure that this
4947 -- subaggregate contains at least one element in addition to the
4948 -- others choice.
4950 if Range_Checks_Suppressed (Ind_Typ) then
4951 Need_To_Check := False;
4953 elsif Present (Expressions (Sub_Aggr))
4954 and then Present (Component_Associations (Sub_Aggr))
4955 then
4956 Need_To_Check := True;
4958 elsif Present (Component_Associations (Sub_Aggr)) then
4959 Assoc := Last (Component_Associations (Sub_Aggr));
4961 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
4962 Need_To_Check := False;
4964 else
4965 -- Count the number of discrete choices. Start with -1 because
4966 -- the others choice does not count.
4968 -- Is there some reason we do not use List_Length here ???
4970 Nb_Choices := -1;
4971 Assoc := First (Component_Associations (Sub_Aggr));
4972 while Present (Assoc) loop
4973 Choice := First (Choices (Assoc));
4974 while Present (Choice) loop
4975 Nb_Choices := Nb_Choices + 1;
4976 Next (Choice);
4977 end loop;
4979 Next (Assoc);
4980 end loop;
4982 -- If there is only an others choice nothing to do
4984 Need_To_Check := (Nb_Choices > 0);
4985 end if;
4987 else
4988 Need_To_Check := False;
4989 end if;
4991 -- If we are dealing with a positional subaggregate with an others
4992 -- choice then compute the number or positional elements.
4994 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
4995 Expr := First (Expressions (Sub_Aggr));
4996 Nb_Elements := Uint_0;
4997 while Present (Expr) loop
4998 Nb_Elements := Nb_Elements + 1;
4999 Next (Expr);
5000 end loop;
5002 -- If the aggregate contains discrete choices and an others choice
5003 -- compute the smallest and largest discrete choice values.
5005 elsif Need_To_Check then
5006 Compute_Choices_Lo_And_Choices_Hi : declare
5008 Table : Case_Table_Type (1 .. Nb_Choices);
5009 -- Used to sort all the different choice values
5011 J : Pos := 1;
5012 Low : Node_Id;
5013 High : Node_Id;
5015 begin
5016 Assoc := First (Component_Associations (Sub_Aggr));
5017 while Present (Assoc) loop
5018 Choice := First (Choices (Assoc));
5019 while Present (Choice) loop
5020 if Nkind (Choice) = N_Others_Choice then
5021 exit;
5022 end if;
5024 Get_Index_Bounds (Choice, Low, High);
5025 Table (J).Choice_Lo := Low;
5026 Table (J).Choice_Hi := High;
5028 J := J + 1;
5029 Next (Choice);
5030 end loop;
5032 Next (Assoc);
5033 end loop;
5035 -- Sort the discrete choices
5037 Sort_Case_Table (Table);
5039 Choices_Lo := Table (1).Choice_Lo;
5040 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5041 end Compute_Choices_Lo_And_Choices_Hi;
5042 end if;
5044 -- If no others choice in this subaggregate, or the aggregate
5045 -- comprises only an others choice, nothing to do.
5047 if not Need_To_Check then
5048 Cond := Empty;
5050 -- If we are dealing with an aggregate containing an others choice
5051 -- and positional components, we generate the following test:
5053 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5054 -- Ind_Typ'Pos (Aggr_Hi)
5055 -- then
5056 -- raise Constraint_Error;
5057 -- end if;
5059 elsif Nb_Elements > Uint_0 then
5060 Cond :=
5061 Make_Op_Gt (Loc,
5062 Left_Opnd =>
5063 Make_Op_Add (Loc,
5064 Left_Opnd =>
5065 Make_Attribute_Reference (Loc,
5066 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5067 Attribute_Name => Name_Pos,
5068 Expressions =>
5069 New_List
5070 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5071 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5073 Right_Opnd =>
5074 Make_Attribute_Reference (Loc,
5075 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5076 Attribute_Name => Name_Pos,
5077 Expressions => New_List (
5078 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5080 -- If we are dealing with an aggregate containing an others choice
5081 -- and discrete choices we generate the following test:
5083 -- [constraint_error when
5084 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5086 else
5087 Cond :=
5088 Make_Or_Else (Loc,
5089 Left_Opnd =>
5090 Make_Op_Lt (Loc,
5091 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5092 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5094 Right_Opnd =>
5095 Make_Op_Gt (Loc,
5096 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5097 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5098 end if;
5100 if Present (Cond) then
5101 Insert_Action (N,
5102 Make_Raise_Constraint_Error (Loc,
5103 Condition => Cond,
5104 Reason => CE_Length_Check_Failed));
5105 -- Questionable reason code, shouldn't that be a
5106 -- CE_Range_Check_Failed ???
5107 end if;
5109 -- Now look inside the subaggregate to see if there is more work
5111 if Dim < Aggr_Dimension then
5113 -- Process positional components
5115 if Present (Expressions (Sub_Aggr)) then
5116 Expr := First (Expressions (Sub_Aggr));
5117 while Present (Expr) loop
5118 Others_Check (Expr, Dim + 1);
5119 Next (Expr);
5120 end loop;
5121 end if;
5123 -- Process component associations
5125 if Present (Component_Associations (Sub_Aggr)) then
5126 Assoc := First (Component_Associations (Sub_Aggr));
5127 while Present (Assoc) loop
5128 Expr := Expression (Assoc);
5129 Others_Check (Expr, Dim + 1);
5130 Next (Assoc);
5131 end loop;
5132 end if;
5133 end if;
5134 end Others_Check;
5136 -------------------------
5137 -- Safe_Left_Hand_Side --
5138 -------------------------
5140 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5141 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5142 -- If the left-hand side includes an indexed component, check that
5143 -- the indexes are free of side effects.
5145 -------------------
5146 -- Is_Safe_Index --
5147 -------------------
5149 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5150 begin
5151 if Is_Entity_Name (Indx) then
5152 return True;
5154 elsif Nkind (Indx) = N_Integer_Literal then
5155 return True;
5157 elsif Nkind (Indx) = N_Function_Call
5158 and then Is_Entity_Name (Name (Indx))
5159 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5160 then
5161 return True;
5163 elsif Nkind (Indx) = N_Type_Conversion
5164 and then Is_Safe_Index (Expression (Indx))
5165 then
5166 return True;
5168 else
5169 return False;
5170 end if;
5171 end Is_Safe_Index;
5173 -- Start of processing for Safe_Left_Hand_Side
5175 begin
5176 if Is_Entity_Name (N) then
5177 return True;
5179 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5180 and then Safe_Left_Hand_Side (Prefix (N))
5181 then
5182 return True;
5184 elsif Nkind (N) = N_Indexed_Component
5185 and then Safe_Left_Hand_Side (Prefix (N))
5186 and then Is_Safe_Index (First (Expressions (N)))
5187 then
5188 return True;
5190 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5191 return Safe_Left_Hand_Side (Expression (N));
5193 else
5194 return False;
5195 end if;
5196 end Safe_Left_Hand_Side;
5198 -- Local variables
5200 Tmp : Entity_Id;
5201 -- Holds the temporary aggregate value
5203 Tmp_Decl : Node_Id;
5204 -- Holds the declaration of Tmp
5206 Aggr_Code : List_Id;
5207 Parent_Node : Node_Id;
5208 Parent_Kind : Node_Kind;
5210 -- Start of processing for Expand_Array_Aggregate
5212 begin
5213 -- Do not touch the special aggregates of attributes used for Asm calls
5215 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5216 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5217 then
5218 return;
5220 -- Do not expand an aggregate for an array type which contains tasks if
5221 -- the aggregate is associated with an unexpanded return statement of a
5222 -- build-in-place function. The aggregate is expanded when the related
5223 -- return statement (rewritten into an extended return) is processed.
5224 -- This delay ensures that any temporaries and initialization code
5225 -- generated for the aggregate appear in the proper return block and
5226 -- use the correct _chain and _master.
5228 elsif Has_Task (Base_Type (Etype (N)))
5229 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5230 and then Is_Build_In_Place_Function
5231 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5232 then
5233 return;
5235 -- Do not attempt expansion if error already detected. We may reach this
5236 -- point in spite of previous errors when compiling with -gnatq, to
5237 -- force all possible errors (this is the usual ACATS mode).
5239 elsif Error_Posted (N) then
5240 return;
5241 end if;
5243 -- If the semantic analyzer has determined that aggregate N will raise
5244 -- Constraint_Error at run time, then the aggregate node has been
5245 -- replaced with an N_Raise_Constraint_Error node and we should
5246 -- never get here.
5248 pragma Assert (not Raises_Constraint_Error (N));
5250 -- STEP 1a
5252 -- Check that the index range defined by aggregate bounds is
5253 -- compatible with corresponding index subtype.
5255 Index_Compatibility_Check : declare
5256 Aggr_Index_Range : Node_Id := First_Index (Typ);
5257 -- The current aggregate index range
5259 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5260 -- The corresponding index constraint against which we have to
5261 -- check the above aggregate index range.
5263 begin
5264 Compute_Others_Present (N, 1);
5266 for J in 1 .. Aggr_Dimension loop
5267 -- There is no need to emit a check if an others choice is present
5268 -- for this array aggregate dimension since in this case one of
5269 -- N's subaggregates has taken its bounds from the context and
5270 -- these bounds must have been checked already. In addition all
5271 -- subaggregates corresponding to the same dimension must all have
5272 -- the same bounds (checked in (c) below).
5274 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5275 and then not Others_Present (J)
5276 then
5277 -- We don't use Checks.Apply_Range_Check here because it emits
5278 -- a spurious check. Namely it checks that the range defined by
5279 -- the aggregate bounds is nonempty. But we know this already
5280 -- if we get here.
5282 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5283 end if;
5285 -- Save the low and high bounds of the aggregate index as well as
5286 -- the index type for later use in checks (b) and (c) below.
5288 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5289 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5291 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5293 Next_Index (Aggr_Index_Range);
5294 Next_Index (Index_Constraint);
5295 end loop;
5296 end Index_Compatibility_Check;
5298 -- STEP 1b
5300 -- If an others choice is present check that no aggregate index is
5301 -- outside the bounds of the index constraint.
5303 Others_Check (N, 1);
5305 -- STEP 1c
5307 -- For multidimensional arrays make sure that all subaggregates
5308 -- corresponding to the same dimension have the same bounds.
5310 if Aggr_Dimension > 1 then
5311 Check_Same_Aggr_Bounds (N, 1);
5312 end if;
5314 -- STEP 1d
5316 -- If we have a default component value, or simple initialization is
5317 -- required for the component type, then we replace <> in component
5318 -- associations by the required default value.
5320 declare
5321 Default_Val : Node_Id;
5322 Assoc : Node_Id;
5324 begin
5325 if (Present (Default_Aspect_Component_Value (Typ))
5326 or else Needs_Simple_Initialization (Ctyp))
5327 and then Present (Component_Associations (N))
5328 then
5329 Assoc := First (Component_Associations (N));
5330 while Present (Assoc) loop
5331 if Nkind (Assoc) = N_Component_Association
5332 and then Box_Present (Assoc)
5333 then
5334 Set_Box_Present (Assoc, False);
5336 if Present (Default_Aspect_Component_Value (Typ)) then
5337 Default_Val := Default_Aspect_Component_Value (Typ);
5338 else
5339 Default_Val := Get_Simple_Init_Val (Ctyp, N);
5340 end if;
5342 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
5343 Analyze_And_Resolve (Expression (Assoc), Ctyp);
5344 end if;
5346 Next (Assoc);
5347 end loop;
5348 end if;
5349 end;
5351 -- STEP 2
5353 -- Here we test for is packed array aggregate that we can handle at
5354 -- compile time. If so, return with transformation done. Note that we do
5355 -- this even if the aggregate is nested, because once we have done this
5356 -- processing, there is no more nested aggregate.
5358 if Packed_Array_Aggregate_Handled (N) then
5359 return;
5360 end if;
5362 -- At this point we try to convert to positional form
5364 if Ekind (Current_Scope) = E_Package
5365 and then Static_Elaboration_Desired (Current_Scope)
5366 then
5367 Convert_To_Positional (N, Max_Others_Replicate => 100);
5368 else
5369 Convert_To_Positional (N);
5370 end if;
5372 -- if the result is no longer an aggregate (e.g. it may be a string
5373 -- literal, or a temporary which has the needed value), then we are
5374 -- done, since there is no longer a nested aggregate.
5376 if Nkind (N) /= N_Aggregate then
5377 return;
5379 -- We are also done if the result is an analyzed aggregate, indicating
5380 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5381 -- aggregate.
5383 elsif Analyzed (N) and then N /= Original_Node (N) then
5384 return;
5385 end if;
5387 -- If all aggregate components are compile-time known and the aggregate
5388 -- has been flattened, nothing left to do. The same occurs if the
5389 -- aggregate is used to initialize the components of a statically
5390 -- allocated dispatch table.
5392 if Compile_Time_Known_Aggregate (N)
5393 or else Is_Static_Dispatch_Table_Aggregate (N)
5394 then
5395 Set_Expansion_Delayed (N, False);
5396 return;
5397 end if;
5399 -- Now see if back end processing is possible
5401 if Backend_Processing_Possible (N) then
5403 -- If the aggregate is static but the constraints are not, build
5404 -- a static subtype for the aggregate, so that Gigi can place it
5405 -- in static memory. Perform an unchecked_conversion to the non-
5406 -- static type imposed by the context.
5408 declare
5409 Itype : constant Entity_Id := Etype (N);
5410 Index : Node_Id;
5411 Needs_Type : Boolean := False;
5413 begin
5414 Index := First_Index (Itype);
5415 while Present (Index) loop
5416 if not Is_OK_Static_Subtype (Etype (Index)) then
5417 Needs_Type := True;
5418 exit;
5419 else
5420 Next_Index (Index);
5421 end if;
5422 end loop;
5424 if Needs_Type then
5425 Build_Constrained_Type (Positional => True);
5426 Rewrite (N, Unchecked_Convert_To (Itype, N));
5427 Analyze (N);
5428 end if;
5429 end;
5431 return;
5432 end if;
5434 -- STEP 3
5436 -- Delay expansion for nested aggregates: it will be taken care of
5437 -- when the parent aggregate is expanded.
5439 Parent_Node := Parent (N);
5440 Parent_Kind := Nkind (Parent_Node);
5442 if Parent_Kind = N_Qualified_Expression then
5443 Parent_Node := Parent (Parent_Node);
5444 Parent_Kind := Nkind (Parent_Node);
5445 end if;
5447 if Parent_Kind = N_Aggregate
5448 or else Parent_Kind = N_Extension_Aggregate
5449 or else Parent_Kind = N_Component_Association
5450 or else (Parent_Kind = N_Object_Declaration
5451 and then Needs_Finalization (Typ))
5452 or else (Parent_Kind = N_Assignment_Statement
5453 and then Inside_Init_Proc)
5454 then
5455 if Static_Array_Aggregate (N)
5456 or else Compile_Time_Known_Aggregate (N)
5457 then
5458 Set_Expansion_Delayed (N, False);
5459 return;
5460 else
5461 Set_Expansion_Delayed (N);
5462 return;
5463 end if;
5464 end if;
5466 -- STEP 4
5468 -- Look if in place aggregate expansion is possible
5470 -- For object declarations we build the aggregate in place, unless
5471 -- the array is bit-packed or the component is controlled.
5473 -- For assignments we do the assignment in place if all the component
5474 -- associations have compile-time known values. For other cases we
5475 -- create a temporary. The analysis for safety of on-line assignment
5476 -- is delicate, i.e. we don't know how to do it fully yet ???
5478 -- For allocators we assign to the designated object in place if the
5479 -- aggregate meets the same conditions as other in-place assignments.
5480 -- In this case the aggregate may not come from source but was created
5481 -- for default initialization, e.g. with Initialize_Scalars.
5483 if Requires_Transient_Scope (Typ) then
5484 Establish_Transient_Scope
5485 (N, Sec_Stack => Has_Controlled_Component (Typ));
5486 end if;
5488 if Has_Default_Init_Comps (N) then
5489 Maybe_In_Place_OK := False;
5491 elsif Is_Bit_Packed_Array (Typ)
5492 or else Has_Controlled_Component (Typ)
5493 then
5494 Maybe_In_Place_OK := False;
5496 else
5497 Maybe_In_Place_OK :=
5498 (Nkind (Parent (N)) = N_Assignment_Statement
5499 and then In_Place_Assign_OK)
5501 or else
5502 (Nkind (Parent (Parent (N))) = N_Allocator
5503 and then In_Place_Assign_OK);
5504 end if;
5506 -- If this is an array of tasks, it will be expanded into build-in-place
5507 -- assignments. Build an activation chain for the tasks now.
5509 if Has_Task (Etype (N)) then
5510 Build_Activation_Chain_Entity (N);
5511 end if;
5513 -- Perform in-place expansion of aggregate in an object declaration.
5514 -- Note: actions generated for the aggregate will be captured in an
5515 -- expression-with-actions statement so that they can be transferred
5516 -- to freeze actions later if there is an address clause for the
5517 -- object. (Note: we don't use a block statement because this would
5518 -- cause generated freeze nodes to be elaborated in the wrong scope).
5520 -- Should document these individual tests ???
5522 if not Has_Default_Init_Comps (N)
5523 and then Comes_From_Source (Parent_Node)
5524 and then Parent_Kind = N_Object_Declaration
5525 and then not
5526 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
5527 and then N = Expression (Parent_Node)
5528 and then not Is_Bit_Packed_Array (Typ)
5529 and then not Has_Controlled_Component (Typ)
5530 then
5531 In_Place_Assign_OK_For_Declaration := True;
5532 Tmp := Defining_Identifier (Parent (N));
5533 Set_No_Initialization (Parent (N));
5534 Set_Expression (Parent (N), Empty);
5536 -- Set kind and type of the entity, for use in the analysis
5537 -- of the subsequent assignments. If the nominal type is not
5538 -- constrained, build a subtype from the known bounds of the
5539 -- aggregate. If the declaration has a subtype mark, use it,
5540 -- otherwise use the itype of the aggregate.
5542 Set_Ekind (Tmp, E_Variable);
5544 if not Is_Constrained (Typ) then
5545 Build_Constrained_Type (Positional => False);
5547 elsif Is_Entity_Name (Object_Definition (Parent (N)))
5548 and then Is_Constrained (Entity (Object_Definition (Parent (N))))
5549 then
5550 Set_Etype (Tmp, Entity (Object_Definition (Parent (N))));
5552 else
5553 Set_Size_Known_At_Compile_Time (Typ, False);
5554 Set_Etype (Tmp, Typ);
5555 end if;
5557 elsif Maybe_In_Place_OK
5558 and then Nkind (Parent (N)) = N_Qualified_Expression
5559 and then Nkind (Parent (Parent (N))) = N_Allocator
5560 then
5561 Set_Expansion_Delayed (N);
5562 return;
5564 -- In the remaining cases the aggregate is the RHS of an assignment
5566 elsif Maybe_In_Place_OK
5567 and then Safe_Left_Hand_Side (Name (Parent (N)))
5568 then
5569 Tmp := Name (Parent (N));
5571 if Etype (Tmp) /= Etype (N) then
5572 Apply_Length_Check (N, Etype (Tmp));
5574 if Nkind (N) = N_Raise_Constraint_Error then
5576 -- Static error, nothing further to expand
5578 return;
5579 end if;
5580 end if;
5582 -- If a slice assignment has an aggregate with a single others_choice,
5583 -- the assignment can be done in place even if bounds are not static,
5584 -- by converting it into a loop over the discrete range of the slice.
5586 elsif Maybe_In_Place_OK
5587 and then Nkind (Name (Parent (N))) = N_Slice
5588 and then Is_Others_Aggregate (N)
5589 then
5590 Tmp := Name (Parent (N));
5592 -- Set type of aggregate to be type of lhs in assignment, in order
5593 -- to suppress redundant length checks.
5595 Set_Etype (N, Etype (Tmp));
5597 -- Step 5
5599 -- In place aggregate expansion is not possible
5601 else
5602 Maybe_In_Place_OK := False;
5603 Tmp := Make_Temporary (Loc, 'A', N);
5604 Tmp_Decl :=
5605 Make_Object_Declaration (Loc,
5606 Defining_Identifier => Tmp,
5607 Object_Definition => New_Occurrence_Of (Typ, Loc));
5608 Set_No_Initialization (Tmp_Decl, True);
5610 -- If we are within a loop, the temporary will be pushed on the
5611 -- stack at each iteration. If the aggregate is the expression for an
5612 -- allocator, it will be immediately copied to the heap and can
5613 -- be reclaimed at once. We create a transient scope around the
5614 -- aggregate for this purpose.
5616 if Ekind (Current_Scope) = E_Loop
5617 and then Nkind (Parent (Parent (N))) = N_Allocator
5618 then
5619 Establish_Transient_Scope (N, False);
5620 end if;
5622 Insert_Action (N, Tmp_Decl);
5623 end if;
5625 -- Construct and insert the aggregate code. We can safely suppress index
5626 -- checks because this code is guaranteed not to raise CE on index
5627 -- checks. However we should *not* suppress all checks.
5629 declare
5630 Target : Node_Id;
5632 begin
5633 if Nkind (Tmp) = N_Defining_Identifier then
5634 Target := New_Occurrence_Of (Tmp, Loc);
5636 else
5637 if Has_Default_Init_Comps (N) then
5639 -- Ada 2005 (AI-287): This case has not been analyzed???
5641 raise Program_Error;
5642 end if;
5644 -- Name in assignment is explicit dereference
5646 Target := New_Copy (Tmp);
5647 end if;
5649 -- If we are to generate an in place assignment for a declaration or
5650 -- an assignment statement, and the assignment can be done directly
5651 -- by the back end, then do not expand further.
5653 -- ??? We can also do that if in place expansion is not possible but
5654 -- then we could go into an infinite recursion.
5656 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
5657 and then not AAMP_On_Target
5658 and then not CodePeer_Mode
5659 and then not Generate_C_Code
5660 and then not Possible_Bit_Aligned_Component (Target)
5661 and then not Is_Possibly_Unaligned_Slice (Target)
5662 and then Aggr_Assignment_OK_For_Backend (N)
5663 then
5664 if Maybe_In_Place_OK then
5665 return;
5666 end if;
5668 Aggr_Code :=
5669 New_List (
5670 Make_Assignment_Statement (Loc,
5671 Name => Target,
5672 Expression => New_Copy (N)));
5674 else
5675 Aggr_Code :=
5676 Build_Array_Aggr_Code (N,
5677 Ctype => Ctyp,
5678 Index => First_Index (Typ),
5679 Into => Target,
5680 Scalar_Comp => Is_Scalar_Type (Ctyp));
5681 end if;
5683 -- Save the last assignment statement associated with the aggregate
5684 -- when building a controlled object. This reference is utilized by
5685 -- the finalization machinery when marking an object as successfully
5686 -- initialized.
5688 if Needs_Finalization (Typ)
5689 and then Is_Entity_Name (Target)
5690 and then Present (Entity (Target))
5691 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
5692 then
5693 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
5694 end if;
5695 end;
5697 -- If the aggregate is the expression in a declaration, the expanded
5698 -- code must be inserted after it. The defining entity might not come
5699 -- from source if this is part of an inlined body, but the declaration
5700 -- itself will.
5702 if Comes_From_Source (Tmp)
5703 or else
5704 (Nkind (Parent (N)) = N_Object_Declaration
5705 and then Comes_From_Source (Parent (N))
5706 and then Tmp = Defining_Entity (Parent (N)))
5707 then
5708 declare
5709 Node_After : constant Node_Id := Next (Parent_Node);
5711 begin
5712 Insert_Actions_After (Parent_Node, Aggr_Code);
5714 if Parent_Kind = N_Object_Declaration then
5715 Collect_Initialization_Statements
5716 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
5717 end if;
5718 end;
5720 else
5721 Insert_Actions (N, Aggr_Code);
5722 end if;
5724 -- If the aggregate has been assigned in place, remove the original
5725 -- assignment.
5727 if Nkind (Parent (N)) = N_Assignment_Statement
5728 and then Maybe_In_Place_OK
5729 then
5730 Rewrite (Parent (N), Make_Null_Statement (Loc));
5732 elsif Nkind (Parent (N)) /= N_Object_Declaration
5733 or else Tmp /= Defining_Identifier (Parent (N))
5734 then
5735 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
5736 Analyze_And_Resolve (N, Typ);
5737 end if;
5738 end Expand_Array_Aggregate;
5740 ------------------------
5741 -- Expand_N_Aggregate --
5742 ------------------------
5744 procedure Expand_N_Aggregate (N : Node_Id) is
5745 begin
5746 -- Record aggregate case
5748 if Is_Record_Type (Etype (N)) then
5749 Expand_Record_Aggregate (N);
5751 -- Array aggregate case
5753 else
5754 -- A special case, if we have a string subtype with bounds 1 .. N,
5755 -- where N is known at compile time, and the aggregate is of the
5756 -- form (others => 'x'), with a single choice and no expressions,
5757 -- and N is less than 80 (an arbitrary limit for now), then replace
5758 -- the aggregate by the equivalent string literal (but do not mark
5759 -- it as static since it is not).
5761 -- Note: this entire circuit is redundant with respect to code in
5762 -- Expand_Array_Aggregate that collapses others choices to positional
5763 -- form, but there are two problems with that circuit:
5765 -- a) It is limited to very small cases due to ill-understood
5766 -- interactions with bootstrapping. That limit is removed by
5767 -- use of the No_Implicit_Loops restriction.
5769 -- b) It incorrectly ends up with the resulting expressions being
5770 -- considered static when they are not. For example, the
5771 -- following test should fail:
5773 -- pragma Restrictions (No_Implicit_Loops);
5774 -- package NonSOthers4 is
5775 -- B : constant String (1 .. 6) := (others => 'A');
5776 -- DH : constant String (1 .. 8) := B & "BB";
5777 -- X : Integer;
5778 -- pragma Export (C, X, Link_Name => DH);
5779 -- end;
5781 -- But it succeeds (DH looks static to pragma Export)
5783 -- To be sorted out ???
5785 if Present (Component_Associations (N)) then
5786 declare
5787 CA : constant Node_Id := First (Component_Associations (N));
5788 MX : constant := 80;
5790 begin
5791 if Nkind (First (Choices (CA))) = N_Others_Choice
5792 and then Nkind (Expression (CA)) = N_Character_Literal
5793 and then No (Expressions (N))
5794 then
5795 declare
5796 T : constant Entity_Id := Etype (N);
5797 X : constant Node_Id := First_Index (T);
5798 EC : constant Node_Id := Expression (CA);
5799 CV : constant Uint := Char_Literal_Value (EC);
5800 CC : constant Int := UI_To_Int (CV);
5802 begin
5803 if Nkind (X) = N_Range
5804 and then Compile_Time_Known_Value (Low_Bound (X))
5805 and then Expr_Value (Low_Bound (X)) = 1
5806 and then Compile_Time_Known_Value (High_Bound (X))
5807 then
5808 declare
5809 Hi : constant Uint := Expr_Value (High_Bound (X));
5811 begin
5812 if Hi <= MX then
5813 Start_String;
5815 for J in 1 .. UI_To_Int (Hi) loop
5816 Store_String_Char (Char_Code (CC));
5817 end loop;
5819 Rewrite (N,
5820 Make_String_Literal (Sloc (N),
5821 Strval => End_String));
5823 if CC >= Int (2 ** 16) then
5824 Set_Has_Wide_Wide_Character (N);
5825 elsif CC >= Int (2 ** 8) then
5826 Set_Has_Wide_Character (N);
5827 end if;
5829 Analyze_And_Resolve (N, T);
5830 Set_Is_Static_Expression (N, False);
5831 return;
5832 end if;
5833 end;
5834 end if;
5835 end;
5836 end if;
5837 end;
5838 end if;
5840 -- Not that special case, so normal expansion of array aggregate
5842 Expand_Array_Aggregate (N);
5843 end if;
5845 exception
5846 when RE_Not_Available =>
5847 return;
5848 end Expand_N_Aggregate;
5850 ----------------------------------
5851 -- Expand_N_Extension_Aggregate --
5852 ----------------------------------
5854 -- If the ancestor part is an expression, add a component association for
5855 -- the parent field. If the type of the ancestor part is not the direct
5856 -- parent of the expected type, build recursively the needed ancestors.
5857 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
5858 -- ration for a temporary of the expected type, followed by individual
5859 -- assignments to the given components.
5861 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
5862 Loc : constant Source_Ptr := Sloc (N);
5863 A : constant Node_Id := Ancestor_Part (N);
5864 Typ : constant Entity_Id := Etype (N);
5866 begin
5867 -- If the ancestor is a subtype mark, an init proc must be called
5868 -- on the resulting object which thus has to be materialized in
5869 -- the front-end
5871 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
5872 Convert_To_Assignments (N, Typ);
5874 -- The extension aggregate is transformed into a record aggregate
5875 -- of the following form (c1 and c2 are inherited components)
5877 -- (Exp with c3 => a, c4 => b)
5878 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
5880 else
5881 Set_Etype (N, Typ);
5883 if Tagged_Type_Expansion then
5884 Expand_Record_Aggregate (N,
5885 Orig_Tag =>
5886 New_Occurrence_Of
5887 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
5888 Parent_Expr => A);
5890 -- No tag is needed in the case of a VM
5892 else
5893 Expand_Record_Aggregate (N, Parent_Expr => A);
5894 end if;
5895 end if;
5897 exception
5898 when RE_Not_Available =>
5899 return;
5900 end Expand_N_Extension_Aggregate;
5902 -----------------------------
5903 -- Expand_Record_Aggregate --
5904 -----------------------------
5906 procedure Expand_Record_Aggregate
5907 (N : Node_Id;
5908 Orig_Tag : Node_Id := Empty;
5909 Parent_Expr : Node_Id := Empty)
5911 Loc : constant Source_Ptr := Sloc (N);
5912 Comps : constant List_Id := Component_Associations (N);
5913 Typ : constant Entity_Id := Etype (N);
5914 Base_Typ : constant Entity_Id := Base_Type (Typ);
5916 Static_Components : Boolean := True;
5917 -- Flag to indicate whether all components are compile-time known,
5918 -- and the aggregate can be constructed statically and handled by
5919 -- the back-end.
5921 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
5922 -- Returns true if N is an expression of composite type which can be
5923 -- fully evaluated at compile time without raising constraint error.
5924 -- Such expressions can be passed as is to Gigi without any expansion.
5926 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
5927 -- set and constants whose expression is such an aggregate, recursively.
5929 function Component_Not_OK_For_Backend return Boolean;
5930 -- Check for presence of a component which makes it impossible for the
5931 -- backend to process the aggregate, thus requiring the use of a series
5932 -- of assignment statements. Cases checked for are a nested aggregate
5933 -- needing Late_Expansion, the presence of a tagged component which may
5934 -- need tag adjustment, and a bit unaligned component reference.
5936 -- We also force expansion into assignments if a component is of a
5937 -- mutable type (including a private type with discriminants) because
5938 -- in that case the size of the component to be copied may be smaller
5939 -- than the side of the target, and there is no simple way for gigi
5940 -- to compute the size of the object to be copied.
5942 -- NOTE: This is part of the ongoing work to define precisely the
5943 -- interface between front-end and back-end handling of aggregates.
5944 -- In general it is desirable to pass aggregates as they are to gigi,
5945 -- in order to minimize elaboration code. This is one case where the
5946 -- semantics of Ada complicate the analysis and lead to anomalies in
5947 -- the gcc back-end if the aggregate is not expanded into assignments.
5949 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
5950 -- Return True if any element of L has Has_Per_Object_Constraint set.
5951 -- L should be the Choices component of an N_Component_Association.
5953 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
5954 -- If any ancestor of the current type is private, the aggregate
5955 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
5956 -- because it will not be set when type and its parent are in the
5957 -- same scope, and the parent component needs expansion.
5959 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
5960 -- For nested aggregates return the ultimate enclosing aggregate; for
5961 -- non-nested aggregates return N.
5963 ----------------------------------------
5964 -- Compile_Time_Known_Composite_Value --
5965 ----------------------------------------
5967 function Compile_Time_Known_Composite_Value
5968 (N : Node_Id) return Boolean
5970 begin
5971 -- If we have an entity name, then see if it is the name of a
5972 -- constant and if so, test the corresponding constant value.
5974 if Is_Entity_Name (N) then
5975 declare
5976 E : constant Entity_Id := Entity (N);
5977 V : Node_Id;
5978 begin
5979 if Ekind (E) /= E_Constant then
5980 return False;
5981 else
5982 V := Constant_Value (E);
5983 return Present (V)
5984 and then Compile_Time_Known_Composite_Value (V);
5985 end if;
5986 end;
5988 -- We have a value, see if it is compile time known
5990 else
5991 if Nkind (N) = N_Aggregate then
5992 return Compile_Time_Known_Aggregate (N);
5993 end if;
5995 -- All other types of values are not known at compile time
5997 return False;
5998 end if;
6000 end Compile_Time_Known_Composite_Value;
6002 ----------------------------------
6003 -- Component_Not_OK_For_Backend --
6004 ----------------------------------
6006 function Component_Not_OK_For_Backend return Boolean is
6007 C : Node_Id;
6008 Expr_Q : Node_Id;
6010 begin
6011 if No (Comps) then
6012 return False;
6013 end if;
6015 C := First (Comps);
6016 while Present (C) loop
6018 -- If the component has box initialization, expansion is needed
6019 -- and component is not ready for backend.
6021 if Box_Present (C) then
6022 return True;
6023 end if;
6025 if Nkind (Expression (C)) = N_Qualified_Expression then
6026 Expr_Q := Expression (Expression (C));
6027 else
6028 Expr_Q := Expression (C);
6029 end if;
6031 -- Return true if the aggregate has any associations for tagged
6032 -- components that may require tag adjustment.
6034 -- These are cases where the source expression may have a tag that
6035 -- could differ from the component tag (e.g., can occur for type
6036 -- conversions and formal parameters). (Tag adjustment not needed
6037 -- if Tagged_Type_Expansion because object tags are implicit in
6038 -- the machine.)
6040 if Is_Tagged_Type (Etype (Expr_Q))
6041 and then (Nkind (Expr_Q) = N_Type_Conversion
6042 or else (Is_Entity_Name (Expr_Q)
6043 and then
6044 Ekind (Entity (Expr_Q)) in Formal_Kind))
6045 and then Tagged_Type_Expansion
6046 then
6047 Static_Components := False;
6048 return True;
6050 elsif Is_Delayed_Aggregate (Expr_Q) then
6051 Static_Components := False;
6052 return True;
6054 elsif Possible_Bit_Aligned_Component (Expr_Q) then
6055 Static_Components := False;
6056 return True;
6058 elsif Modify_Tree_For_C
6059 and then Nkind (C) = N_Component_Association
6060 and then Has_Per_Object_Constraint (Choices (C))
6061 then
6062 Static_Components := False;
6063 return True;
6065 elsif Modify_Tree_For_C
6066 and then Nkind (Expr_Q) = N_Identifier
6067 and then Is_Array_Type (Etype (Expr_Q))
6068 then
6069 Static_Components := False;
6070 return True;
6071 end if;
6073 if Is_Elementary_Type (Etype (Expr_Q)) then
6074 if not Compile_Time_Known_Value (Expr_Q) then
6075 Static_Components := False;
6076 end if;
6078 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
6079 Static_Components := False;
6081 if Is_Private_Type (Etype (Expr_Q))
6082 and then Has_Discriminants (Etype (Expr_Q))
6083 then
6084 return True;
6085 end if;
6086 end if;
6088 Next (C);
6089 end loop;
6091 return False;
6092 end Component_Not_OK_For_Backend;
6094 -------------------------------
6095 -- Has_Per_Object_Constraint --
6096 -------------------------------
6098 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
6099 N : Node_Id := First (L);
6100 begin
6101 while Present (N) loop
6102 if Is_Entity_Name (N)
6103 and then Present (Entity (N))
6104 and then Has_Per_Object_Constraint (Entity (N))
6105 then
6106 return True;
6107 end if;
6109 Next (N);
6110 end loop;
6112 return False;
6113 end Has_Per_Object_Constraint;
6115 -----------------------------------
6116 -- Has_Visible_Private_Ancestor --
6117 -----------------------------------
6119 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
6120 R : constant Entity_Id := Root_Type (Id);
6121 T1 : Entity_Id := Id;
6123 begin
6124 loop
6125 if Is_Private_Type (T1) then
6126 return True;
6128 elsif T1 = R then
6129 return False;
6131 else
6132 T1 := Etype (T1);
6133 end if;
6134 end loop;
6135 end Has_Visible_Private_Ancestor;
6137 -------------------------
6138 -- Top_Level_Aggregate --
6139 -------------------------
6141 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
6142 Aggr : Node_Id;
6144 begin
6145 Aggr := N;
6146 while Present (Parent (Aggr))
6147 and then Nkind_In (Parent (Aggr), N_Component_Association,
6148 N_Aggregate)
6149 loop
6150 Aggr := Parent (Aggr);
6151 end loop;
6153 return Aggr;
6154 end Top_Level_Aggregate;
6156 -- Local variables
6158 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
6159 Tag_Value : Node_Id;
6160 Comp : Entity_Id;
6161 New_Comp : Node_Id;
6163 -- Start of processing for Expand_Record_Aggregate
6165 begin
6166 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
6167 -- to prevent a piecemeal assignment even if the aggregate is to be
6168 -- expanded. We create a temporary for the aggregate, and assign the
6169 -- temporary instead, so that the back end can generate an atomic move
6170 -- for it.
6172 if Is_Atomic_VFA_Aggregate (N) then
6173 return;
6175 -- No special management required for aggregates used to initialize
6176 -- statically allocated dispatch tables
6178 elsif Is_Static_Dispatch_Table_Aggregate (N) then
6179 return;
6180 end if;
6182 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
6183 -- are build-in-place function calls. The assignments will each turn
6184 -- into a build-in-place function call. If components are all static,
6185 -- we can pass the aggregate to the backend regardless of limitedness.
6187 -- Extension aggregates, aggregates in extended return statements, and
6188 -- aggregates for C++ imported types must be expanded.
6190 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
6191 if not Nkind_In (Parent (N), N_Object_Declaration,
6192 N_Component_Association)
6193 then
6194 Convert_To_Assignments (N, Typ);
6196 elsif Nkind (N) = N_Extension_Aggregate
6197 or else Convention (Typ) = Convention_CPP
6198 then
6199 Convert_To_Assignments (N, Typ);
6201 elsif not Size_Known_At_Compile_Time (Typ)
6202 or else Component_Not_OK_For_Backend
6203 or else not Static_Components
6204 then
6205 Convert_To_Assignments (N, Typ);
6207 else
6208 Set_Compile_Time_Known_Aggregate (N);
6209 Set_Expansion_Delayed (N, False);
6210 end if;
6212 -- Gigi doesn't properly handle temporaries of variable size so we
6213 -- generate it in the front-end
6215 elsif not Size_Known_At_Compile_Time (Typ)
6216 and then Tagged_Type_Expansion
6217 then
6218 Convert_To_Assignments (N, Typ);
6220 -- An aggregate used to initialize a controlled object must be turned
6221 -- into component assignments as the components themselves may require
6222 -- finalization actions such as adjustment.
6224 elsif Needs_Finalization (Typ) then
6225 Convert_To_Assignments (N, Typ);
6227 -- Ada 2005 (AI-287): In case of default initialized components we
6228 -- convert the aggregate into assignments.
6230 elsif Has_Default_Init_Comps (N) then
6231 Convert_To_Assignments (N, Typ);
6233 -- Check components
6235 elsif Component_Not_OK_For_Backend then
6236 Convert_To_Assignments (N, Typ);
6238 -- If an ancestor is private, some components are not inherited and we
6239 -- cannot expand into a record aggregate.
6241 elsif Has_Visible_Private_Ancestor (Typ) then
6242 Convert_To_Assignments (N, Typ);
6244 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
6245 -- is not able to handle the aggregate for Late_Request.
6247 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
6248 Convert_To_Assignments (N, Typ);
6250 -- If the tagged types covers interface types we need to initialize all
6251 -- hidden components containing pointers to secondary dispatch tables.
6253 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
6254 Convert_To_Assignments (N, Typ);
6256 -- If some components are mutable, the size of the aggregate component
6257 -- may be distinct from the default size of the type component, so
6258 -- we need to expand to insure that the back-end copies the proper
6259 -- size of the data. However, if the aggregate is the initial value of
6260 -- a constant, the target is immutable and might be built statically
6261 -- if components are appropriate.
6263 elsif Has_Mutable_Components (Typ)
6264 and then
6265 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
6266 or else not Constant_Present (Parent (Top_Level_Aggr))
6267 or else not Static_Components)
6268 then
6269 Convert_To_Assignments (N, Typ);
6271 -- If the type involved has bit aligned components, then we are not sure
6272 -- that the back end can handle this case correctly.
6274 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
6275 Convert_To_Assignments (N, Typ);
6277 -- When generating C, only generate an aggregate when declaring objects
6278 -- since C does not support aggregates in e.g. assignment statements.
6280 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
6281 Convert_To_Assignments (N, Typ);
6283 -- In all other cases, build a proper aggregate to be handled by gigi
6285 else
6286 if Nkind (N) = N_Aggregate then
6288 -- If the aggregate is static and can be handled by the back-end,
6289 -- nothing left to do.
6291 if Static_Components then
6292 Set_Compile_Time_Known_Aggregate (N);
6293 Set_Expansion_Delayed (N, False);
6294 end if;
6295 end if;
6297 -- If no discriminants, nothing special to do
6299 if not Has_Discriminants (Typ) then
6300 null;
6302 -- Case of discriminants present
6304 elsif Is_Derived_Type (Typ) then
6306 -- For untagged types, non-stored discriminants are replaced
6307 -- with stored discriminants, which are the ones that gigi uses
6308 -- to describe the type and its components.
6310 Generate_Aggregate_For_Derived_Type : declare
6311 Constraints : constant List_Id := New_List;
6312 First_Comp : Node_Id;
6313 Discriminant : Entity_Id;
6314 Decl : Node_Id;
6315 Num_Disc : Nat := 0;
6316 Num_Gird : Nat := 0;
6318 procedure Prepend_Stored_Values (T : Entity_Id);
6319 -- Scan the list of stored discriminants of the type, and add
6320 -- their values to the aggregate being built.
6322 ---------------------------
6323 -- Prepend_Stored_Values --
6324 ---------------------------
6326 procedure Prepend_Stored_Values (T : Entity_Id) is
6327 begin
6328 Discriminant := First_Stored_Discriminant (T);
6329 while Present (Discriminant) loop
6330 New_Comp :=
6331 Make_Component_Association (Loc,
6332 Choices =>
6333 New_List (New_Occurrence_Of (Discriminant, Loc)),
6335 Expression =>
6336 New_Copy_Tree
6337 (Get_Discriminant_Value
6338 (Discriminant,
6339 Typ,
6340 Discriminant_Constraint (Typ))));
6342 if No (First_Comp) then
6343 Prepend_To (Component_Associations (N), New_Comp);
6344 else
6345 Insert_After (First_Comp, New_Comp);
6346 end if;
6348 First_Comp := New_Comp;
6349 Next_Stored_Discriminant (Discriminant);
6350 end loop;
6351 end Prepend_Stored_Values;
6353 -- Start of processing for Generate_Aggregate_For_Derived_Type
6355 begin
6356 -- Remove the associations for the discriminant of derived type
6358 First_Comp := First (Component_Associations (N));
6359 while Present (First_Comp) loop
6360 Comp := First_Comp;
6361 Next (First_Comp);
6363 if Ekind (Entity (First (Choices (Comp)))) = E_Discriminant
6364 then
6365 Remove (Comp);
6366 Num_Disc := Num_Disc + 1;
6367 end if;
6368 end loop;
6370 -- Insert stored discriminant associations in the correct
6371 -- order. If there are more stored discriminants than new
6372 -- discriminants, there is at least one new discriminant that
6373 -- constrains more than one of the stored discriminants. In
6374 -- this case we need to construct a proper subtype of the
6375 -- parent type, in order to supply values to all the
6376 -- components. Otherwise there is one-one correspondence
6377 -- between the constraints and the stored discriminants.
6379 First_Comp := Empty;
6381 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6382 while Present (Discriminant) loop
6383 Num_Gird := Num_Gird + 1;
6384 Next_Stored_Discriminant (Discriminant);
6385 end loop;
6387 -- Case of more stored discriminants than new discriminants
6389 if Num_Gird > Num_Disc then
6391 -- Create a proper subtype of the parent type, which is the
6392 -- proper implementation type for the aggregate, and convert
6393 -- it to the intended target type.
6395 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6396 while Present (Discriminant) loop
6397 New_Comp :=
6398 New_Copy_Tree
6399 (Get_Discriminant_Value
6400 (Discriminant,
6401 Typ,
6402 Discriminant_Constraint (Typ)));
6403 Append (New_Comp, Constraints);
6404 Next_Stored_Discriminant (Discriminant);
6405 end loop;
6407 Decl :=
6408 Make_Subtype_Declaration (Loc,
6409 Defining_Identifier => Make_Temporary (Loc, 'T'),
6410 Subtype_Indication =>
6411 Make_Subtype_Indication (Loc,
6412 Subtype_Mark =>
6413 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
6414 Constraint =>
6415 Make_Index_Or_Discriminant_Constraint
6416 (Loc, Constraints)));
6418 Insert_Action (N, Decl);
6419 Prepend_Stored_Values (Base_Type (Typ));
6421 Set_Etype (N, Defining_Identifier (Decl));
6422 Set_Analyzed (N);
6424 Rewrite (N, Unchecked_Convert_To (Typ, N));
6425 Analyze (N);
6427 -- Case where we do not have fewer new discriminants than
6428 -- stored discriminants, so in this case we can simply use the
6429 -- stored discriminants of the subtype.
6431 else
6432 Prepend_Stored_Values (Typ);
6433 end if;
6434 end Generate_Aggregate_For_Derived_Type;
6435 end if;
6437 if Is_Tagged_Type (Typ) then
6439 -- In the tagged case, _parent and _tag component must be created
6441 -- Reset Null_Present unconditionally. Tagged records always have
6442 -- at least one field (the tag or the parent).
6444 Set_Null_Record_Present (N, False);
6446 -- When the current aggregate comes from the expansion of an
6447 -- extension aggregate, the parent expr is replaced by an
6448 -- aggregate formed by selected components of this expr.
6450 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
6451 Comp := First_Component_Or_Discriminant (Typ);
6452 while Present (Comp) loop
6454 -- Skip all expander-generated components
6456 if not Comes_From_Source (Original_Record_Component (Comp))
6457 then
6458 null;
6460 else
6461 New_Comp :=
6462 Make_Selected_Component (Loc,
6463 Prefix =>
6464 Unchecked_Convert_To (Typ,
6465 Duplicate_Subexpr (Parent_Expr, True)),
6466 Selector_Name => New_Occurrence_Of (Comp, Loc));
6468 Append_To (Comps,
6469 Make_Component_Association (Loc,
6470 Choices =>
6471 New_List (New_Occurrence_Of (Comp, Loc)),
6472 Expression => New_Comp));
6474 Analyze_And_Resolve (New_Comp, Etype (Comp));
6475 end if;
6477 Next_Component_Or_Discriminant (Comp);
6478 end loop;
6479 end if;
6481 -- Compute the value for the Tag now, if the type is a root it
6482 -- will be included in the aggregate right away, otherwise it will
6483 -- be propagated to the parent aggregate.
6485 if Present (Orig_Tag) then
6486 Tag_Value := Orig_Tag;
6487 elsif not Tagged_Type_Expansion then
6488 Tag_Value := Empty;
6489 else
6490 Tag_Value :=
6491 New_Occurrence_Of
6492 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
6493 end if;
6495 -- For a derived type, an aggregate for the parent is formed with
6496 -- all the inherited components.
6498 if Is_Derived_Type (Typ) then
6500 declare
6501 First_Comp : Node_Id;
6502 Parent_Comps : List_Id;
6503 Parent_Aggr : Node_Id;
6504 Parent_Name : Node_Id;
6506 begin
6507 -- Remove the inherited component association from the
6508 -- aggregate and store them in the parent aggregate
6510 First_Comp := First (Component_Associations (N));
6511 Parent_Comps := New_List;
6512 while Present (First_Comp)
6513 and then
6514 Scope (Original_Record_Component
6515 (Entity (First (Choices (First_Comp))))) /=
6516 Base_Typ
6517 loop
6518 Comp := First_Comp;
6519 Next (First_Comp);
6520 Remove (Comp);
6521 Append (Comp, Parent_Comps);
6522 end loop;
6524 Parent_Aggr :=
6525 Make_Aggregate (Loc,
6526 Component_Associations => Parent_Comps);
6527 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
6529 -- Find the _parent component
6531 Comp := First_Component (Typ);
6532 while Chars (Comp) /= Name_uParent loop
6533 Comp := Next_Component (Comp);
6534 end loop;
6536 Parent_Name := New_Occurrence_Of (Comp, Loc);
6538 -- Insert the parent aggregate
6540 Prepend_To (Component_Associations (N),
6541 Make_Component_Association (Loc,
6542 Choices => New_List (Parent_Name),
6543 Expression => Parent_Aggr));
6545 -- Expand recursively the parent propagating the right Tag
6547 Expand_Record_Aggregate
6548 (Parent_Aggr, Tag_Value, Parent_Expr);
6550 -- The ancestor part may be a nested aggregate that has
6551 -- delayed expansion: recheck now.
6553 if Component_Not_OK_For_Backend then
6554 Convert_To_Assignments (N, Typ);
6555 end if;
6556 end;
6558 -- For a root type, the tag component is added (unless compiling
6559 -- for the VMs, where tags are implicit).
6561 elsif Tagged_Type_Expansion then
6562 declare
6563 Tag_Name : constant Node_Id :=
6564 New_Occurrence_Of (First_Tag_Component (Typ), Loc);
6565 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
6566 Conv_Node : constant Node_Id :=
6567 Unchecked_Convert_To (Typ_Tag, Tag_Value);
6569 begin
6570 Set_Etype (Conv_Node, Typ_Tag);
6571 Prepend_To (Component_Associations (N),
6572 Make_Component_Association (Loc,
6573 Choices => New_List (Tag_Name),
6574 Expression => Conv_Node));
6575 end;
6576 end if;
6577 end if;
6578 end if;
6580 end Expand_Record_Aggregate;
6582 ----------------------------
6583 -- Has_Default_Init_Comps --
6584 ----------------------------
6586 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
6587 Comps : constant List_Id := Component_Associations (N);
6588 C : Node_Id;
6589 Expr : Node_Id;
6591 begin
6592 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
6594 if No (Comps) then
6595 return False;
6596 end if;
6598 if Has_Self_Reference (N) then
6599 return True;
6600 end if;
6602 -- Check if any direct component has default initialized components
6604 C := First (Comps);
6605 while Present (C) loop
6606 if Box_Present (C) then
6607 return True;
6608 end if;
6610 Next (C);
6611 end loop;
6613 -- Recursive call in case of aggregate expression
6615 C := First (Comps);
6616 while Present (C) loop
6617 Expr := Expression (C);
6619 if Present (Expr)
6620 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
6621 and then Has_Default_Init_Comps (Expr)
6622 then
6623 return True;
6624 end if;
6626 Next (C);
6627 end loop;
6629 return False;
6630 end Has_Default_Init_Comps;
6632 --------------------------
6633 -- Is_Delayed_Aggregate --
6634 --------------------------
6636 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
6637 Node : Node_Id := N;
6638 Kind : Node_Kind := Nkind (Node);
6640 begin
6641 if Kind = N_Qualified_Expression then
6642 Node := Expression (Node);
6643 Kind := Nkind (Node);
6644 end if;
6646 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
6647 return False;
6648 else
6649 return Expansion_Delayed (Node);
6650 end if;
6651 end Is_Delayed_Aggregate;
6653 ---------------------------
6654 -- In_Object_Declaration --
6655 ---------------------------
6657 function In_Object_Declaration (N : Node_Id) return Boolean is
6658 P : Node_Id := Parent (N);
6659 begin
6660 while Present (P) loop
6661 if Nkind (P) = N_Object_Declaration then
6662 return True;
6663 end if;
6665 P := Parent (P);
6666 end loop;
6668 return False;
6669 end In_Object_Declaration;
6671 ----------------------------------------
6672 -- Is_Static_Dispatch_Table_Aggregate --
6673 ----------------------------------------
6675 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
6676 Typ : constant Entity_Id := Base_Type (Etype (N));
6678 begin
6679 return Static_Dispatch_Tables
6680 and then Tagged_Type_Expansion
6681 and then RTU_Loaded (Ada_Tags)
6683 -- Avoid circularity when rebuilding the compiler
6685 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
6686 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
6687 or else
6688 Typ = RTE (RE_Address_Array)
6689 or else
6690 Typ = RTE (RE_Type_Specific_Data)
6691 or else
6692 Typ = RTE (RE_Tag_Table)
6693 or else
6694 (RTE_Available (RE_Interface_Data)
6695 and then Typ = RTE (RE_Interface_Data))
6696 or else
6697 (RTE_Available (RE_Interfaces_Array)
6698 and then Typ = RTE (RE_Interfaces_Array))
6699 or else
6700 (RTE_Available (RE_Interface_Data_Element)
6701 and then Typ = RTE (RE_Interface_Data_Element)));
6702 end Is_Static_Dispatch_Table_Aggregate;
6704 -----------------------------
6705 -- Is_Two_Dim_Packed_Array --
6706 -----------------------------
6708 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
6709 C : constant Int := UI_To_Int (Component_Size (Typ));
6710 begin
6711 return Number_Dimensions (Typ) = 2
6712 and then Is_Bit_Packed_Array (Typ)
6713 and then (C = 1 or else C = 2 or else C = 4);
6714 end Is_Two_Dim_Packed_Array;
6716 --------------------
6717 -- Late_Expansion --
6718 --------------------
6720 function Late_Expansion
6721 (N : Node_Id;
6722 Typ : Entity_Id;
6723 Target : Node_Id) return List_Id
6725 Aggr_Code : List_Id;
6727 begin
6728 if Is_Array_Type (Etype (N)) then
6729 Aggr_Code :=
6730 Build_Array_Aggr_Code
6731 (N => N,
6732 Ctype => Component_Type (Etype (N)),
6733 Index => First_Index (Typ),
6734 Into => Target,
6735 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
6736 Indexes => No_List);
6738 -- Directly or indirectly (e.g. access protected procedure) a record
6740 else
6741 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
6742 end if;
6744 -- Save the last assignment statement associated with the aggregate
6745 -- when building a controlled object. This reference is utilized by
6746 -- the finalization machinery when marking an object as successfully
6747 -- initialized.
6749 if Needs_Finalization (Typ)
6750 and then Is_Entity_Name (Target)
6751 and then Present (Entity (Target))
6752 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6753 then
6754 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6755 end if;
6757 return Aggr_Code;
6758 end Late_Expansion;
6760 ----------------------------------
6761 -- Make_OK_Assignment_Statement --
6762 ----------------------------------
6764 function Make_OK_Assignment_Statement
6765 (Sloc : Source_Ptr;
6766 Name : Node_Id;
6767 Expression : Node_Id) return Node_Id
6769 begin
6770 Set_Assignment_OK (Name);
6771 return Make_Assignment_Statement (Sloc, Name, Expression);
6772 end Make_OK_Assignment_Statement;
6774 -----------------------
6775 -- Number_Of_Choices --
6776 -----------------------
6778 function Number_Of_Choices (N : Node_Id) return Nat is
6779 Assoc : Node_Id;
6780 Choice : Node_Id;
6782 Nb_Choices : Nat := 0;
6784 begin
6785 if Present (Expressions (N)) then
6786 return 0;
6787 end if;
6789 Assoc := First (Component_Associations (N));
6790 while Present (Assoc) loop
6791 Choice := First (Choices (Assoc));
6792 while Present (Choice) loop
6793 if Nkind (Choice) /= N_Others_Choice then
6794 Nb_Choices := Nb_Choices + 1;
6795 end if;
6797 Next (Choice);
6798 end loop;
6800 Next (Assoc);
6801 end loop;
6803 return Nb_Choices;
6804 end Number_Of_Choices;
6806 ------------------------------------
6807 -- Packed_Array_Aggregate_Handled --
6808 ------------------------------------
6810 -- The current version of this procedure will handle at compile time
6811 -- any array aggregate that meets these conditions:
6813 -- One and two dimensional, bit packed
6814 -- Underlying packed type is modular type
6815 -- Bounds are within 32-bit Int range
6816 -- All bounds and values are static
6818 -- Note: for now, in the 2-D case, we only handle component sizes of
6819 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
6821 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
6822 Loc : constant Source_Ptr := Sloc (N);
6823 Typ : constant Entity_Id := Etype (N);
6824 Ctyp : constant Entity_Id := Component_Type (Typ);
6826 Not_Handled : exception;
6827 -- Exception raised if this aggregate cannot be handled
6829 begin
6830 -- Handle one- or two dimensional bit packed array
6832 if not Is_Bit_Packed_Array (Typ)
6833 or else Number_Dimensions (Typ) > 2
6834 then
6835 return False;
6836 end if;
6838 -- If two-dimensional, check whether it can be folded, and transformed
6839 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
6840 -- the original type.
6842 if Number_Dimensions (Typ) = 2 then
6843 return Two_Dim_Packed_Array_Handled (N);
6844 end if;
6846 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
6847 return False;
6848 end if;
6850 if not Is_Scalar_Type (Component_Type (Typ))
6851 and then Has_Non_Standard_Rep (Component_Type (Typ))
6852 then
6853 return False;
6854 end if;
6856 declare
6857 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
6859 Lo : Node_Id;
6860 Hi : Node_Id;
6861 -- Bounds of index type
6863 Lob : Uint;
6864 Hib : Uint;
6865 -- Values of bounds if compile time known
6867 function Get_Component_Val (N : Node_Id) return Uint;
6868 -- Given a expression value N of the component type Ctyp, returns a
6869 -- value of Csiz (component size) bits representing this value. If
6870 -- the value is non-static or any other reason exists why the value
6871 -- cannot be returned, then Not_Handled is raised.
6873 -----------------------
6874 -- Get_Component_Val --
6875 -----------------------
6877 function Get_Component_Val (N : Node_Id) return Uint is
6878 Val : Uint;
6880 begin
6881 -- We have to analyze the expression here before doing any further
6882 -- processing here. The analysis of such expressions is deferred
6883 -- till expansion to prevent some problems of premature analysis.
6885 Analyze_And_Resolve (N, Ctyp);
6887 -- Must have a compile time value. String literals have to be
6888 -- converted into temporaries as well, because they cannot easily
6889 -- be converted into their bit representation.
6891 if not Compile_Time_Known_Value (N)
6892 or else Nkind (N) = N_String_Literal
6893 then
6894 raise Not_Handled;
6895 end if;
6897 Val := Expr_Rep_Value (N);
6899 -- Adjust for bias, and strip proper number of bits
6901 if Has_Biased_Representation (Ctyp) then
6902 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
6903 end if;
6905 return Val mod Uint_2 ** Csiz;
6906 end Get_Component_Val;
6908 -- Here we know we have a one dimensional bit packed array
6910 begin
6911 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
6913 -- Cannot do anything if bounds are dynamic
6915 if not Compile_Time_Known_Value (Lo)
6916 or else
6917 not Compile_Time_Known_Value (Hi)
6918 then
6919 return False;
6920 end if;
6922 -- Or are silly out of range of int bounds
6924 Lob := Expr_Value (Lo);
6925 Hib := Expr_Value (Hi);
6927 if not UI_Is_In_Int_Range (Lob)
6928 or else
6929 not UI_Is_In_Int_Range (Hib)
6930 then
6931 return False;
6932 end if;
6934 -- At this stage we have a suitable aggregate for handling at compile
6935 -- time. The only remaining checks are that the values of expressions
6936 -- in the aggregate are compile-time known (checks are performed by
6937 -- Get_Component_Val), and that any subtypes or ranges are statically
6938 -- known.
6940 -- If the aggregate is not fully positional at this stage, then
6941 -- convert it to positional form. Either this will fail, in which
6942 -- case we can do nothing, or it will succeed, in which case we have
6943 -- succeeded in handling the aggregate and transforming it into a
6944 -- modular value, or it will stay an aggregate, in which case we
6945 -- have failed to create a packed value for it.
6947 if Present (Component_Associations (N)) then
6948 Convert_To_Positional
6949 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
6950 return Nkind (N) /= N_Aggregate;
6951 end if;
6953 -- Otherwise we are all positional, so convert to proper value
6955 declare
6956 Lov : constant Int := UI_To_Int (Lob);
6957 Hiv : constant Int := UI_To_Int (Hib);
6959 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
6960 -- The length of the array (number of elements)
6962 Aggregate_Val : Uint;
6963 -- Value of aggregate. The value is set in the low order bits of
6964 -- this value. For the little-endian case, the values are stored
6965 -- from low-order to high-order and for the big-endian case the
6966 -- values are stored from high-order to low-order. Note that gigi
6967 -- will take care of the conversions to left justify the value in
6968 -- the big endian case (because of left justified modular type
6969 -- processing), so we do not have to worry about that here.
6971 Lit : Node_Id;
6972 -- Integer literal for resulting constructed value
6974 Shift : Nat;
6975 -- Shift count from low order for next value
6977 Incr : Int;
6978 -- Shift increment for loop
6980 Expr : Node_Id;
6981 -- Next expression from positional parameters of aggregate
6983 Left_Justified : Boolean;
6984 -- Set True if we are filling the high order bits of the target
6985 -- value (i.e. the value is left justified).
6987 begin
6988 -- For little endian, we fill up the low order bits of the target
6989 -- value. For big endian we fill up the high order bits of the
6990 -- target value (which is a left justified modular value).
6992 Left_Justified := Bytes_Big_Endian;
6994 -- Switch justification if using -gnatd8
6996 if Debug_Flag_8 then
6997 Left_Justified := not Left_Justified;
6998 end if;
7000 -- Switch justfification if reverse storage order
7002 if Reverse_Storage_Order (Base_Type (Typ)) then
7003 Left_Justified := not Left_Justified;
7004 end if;
7006 if Left_Justified then
7007 Shift := Csiz * (Len - 1);
7008 Incr := -Csiz;
7009 else
7010 Shift := 0;
7011 Incr := +Csiz;
7012 end if;
7014 -- Loop to set the values
7016 if Len = 0 then
7017 Aggregate_Val := Uint_0;
7018 else
7019 Expr := First (Expressions (N));
7020 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
7022 for J in 2 .. Len loop
7023 Shift := Shift + Incr;
7024 Next (Expr);
7025 Aggregate_Val :=
7026 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
7027 end loop;
7028 end if;
7030 -- Now we can rewrite with the proper value
7032 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
7033 Set_Print_In_Hex (Lit);
7035 -- Construct the expression using this literal. Note that it is
7036 -- important to qualify the literal with its proper modular type
7037 -- since universal integer does not have the required range and
7038 -- also this is a left justified modular type, which is important
7039 -- in the big-endian case.
7041 Rewrite (N,
7042 Unchecked_Convert_To (Typ,
7043 Make_Qualified_Expression (Loc,
7044 Subtype_Mark =>
7045 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
7046 Expression => Lit)));
7048 Analyze_And_Resolve (N, Typ);
7049 return True;
7050 end;
7051 end;
7053 exception
7054 when Not_Handled =>
7055 return False;
7056 end Packed_Array_Aggregate_Handled;
7058 ----------------------------
7059 -- Has_Mutable_Components --
7060 ----------------------------
7062 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
7063 Comp : Entity_Id;
7065 begin
7066 Comp := First_Component (Typ);
7067 while Present (Comp) loop
7068 if Is_Record_Type (Etype (Comp))
7069 and then Has_Discriminants (Etype (Comp))
7070 and then not Is_Constrained (Etype (Comp))
7071 then
7072 return True;
7073 end if;
7075 Next_Component (Comp);
7076 end loop;
7078 return False;
7079 end Has_Mutable_Components;
7081 ------------------------------
7082 -- Initialize_Discriminants --
7083 ------------------------------
7085 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
7086 Loc : constant Source_Ptr := Sloc (N);
7087 Bas : constant Entity_Id := Base_Type (Typ);
7088 Par : constant Entity_Id := Etype (Bas);
7089 Decl : constant Node_Id := Parent (Par);
7090 Ref : Node_Id;
7092 begin
7093 if Is_Tagged_Type (Bas)
7094 and then Is_Derived_Type (Bas)
7095 and then Has_Discriminants (Par)
7096 and then Has_Discriminants (Bas)
7097 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
7098 and then Nkind (Decl) = N_Full_Type_Declaration
7099 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7100 and then
7101 Present (Variant_Part (Component_List (Type_Definition (Decl))))
7102 and then Nkind (N) /= N_Extension_Aggregate
7103 then
7105 -- Call init proc to set discriminants.
7106 -- There should eventually be a special procedure for this ???
7108 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
7109 Insert_Actions_After (N,
7110 Build_Initialization_Call (Sloc (N), Ref, Typ));
7111 end if;
7112 end Initialize_Discriminants;
7114 ----------------
7115 -- Must_Slide --
7116 ----------------
7118 function Must_Slide
7119 (Obj_Type : Entity_Id;
7120 Typ : Entity_Id) return Boolean
7122 L1, L2, H1, H2 : Node_Id;
7124 begin
7125 -- No sliding if the type of the object is not established yet, if it is
7126 -- an unconstrained type whose actual subtype comes from the aggregate,
7127 -- or if the two types are identical.
7129 if not Is_Array_Type (Obj_Type) then
7130 return False;
7132 elsif not Is_Constrained (Obj_Type) then
7133 return False;
7135 elsif Typ = Obj_Type then
7136 return False;
7138 else
7139 -- Sliding can only occur along the first dimension
7141 Get_Index_Bounds (First_Index (Typ), L1, H1);
7142 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
7144 if not Is_OK_Static_Expression (L1) or else
7145 not Is_OK_Static_Expression (L2) or else
7146 not Is_OK_Static_Expression (H1) or else
7147 not Is_OK_Static_Expression (H2)
7148 then
7149 return False;
7150 else
7151 return Expr_Value (L1) /= Expr_Value (L2)
7152 or else
7153 Expr_Value (H1) /= Expr_Value (H2);
7154 end if;
7155 end if;
7156 end Must_Slide;
7158 ----------------------------------
7159 -- Two_Dim_Packed_Array_Handled --
7160 ----------------------------------
7162 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
7163 Loc : constant Source_Ptr := Sloc (N);
7164 Typ : constant Entity_Id := Etype (N);
7165 Ctyp : constant Entity_Id := Component_Type (Typ);
7166 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
7167 Packed_Array : constant Entity_Id :=
7168 Packed_Array_Impl_Type (Base_Type (Typ));
7170 One_Comp : Node_Id;
7171 -- Expression in original aggregate
7173 One_Dim : Node_Id;
7174 -- One-dimensional subaggregate
7176 begin
7178 -- For now, only deal with cases where an integral number of elements
7179 -- fit in a single byte. This includes the most common boolean case.
7181 if not (Comp_Size = 1 or else
7182 Comp_Size = 2 or else
7183 Comp_Size = 4)
7184 then
7185 return False;
7186 end if;
7188 Convert_To_Positional
7189 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7191 -- Verify that all components are static
7193 if Nkind (N) = N_Aggregate
7194 and then Compile_Time_Known_Aggregate (N)
7195 then
7196 null;
7198 -- The aggregate may have been re-analyzed and converted already
7200 elsif Nkind (N) /= N_Aggregate then
7201 return True;
7203 -- If component associations remain, the aggregate is not static
7205 elsif Present (Component_Associations (N)) then
7206 return False;
7208 else
7209 One_Dim := First (Expressions (N));
7210 while Present (One_Dim) loop
7211 if Present (Component_Associations (One_Dim)) then
7212 return False;
7213 end if;
7215 One_Comp := First (Expressions (One_Dim));
7216 while Present (One_Comp) loop
7217 if not Is_OK_Static_Expression (One_Comp) then
7218 return False;
7219 end if;
7221 Next (One_Comp);
7222 end loop;
7224 Next (One_Dim);
7225 end loop;
7226 end if;
7228 -- Two-dimensional aggregate is now fully positional so pack one
7229 -- dimension to create a static one-dimensional array, and rewrite
7230 -- as an unchecked conversion to the original type.
7232 declare
7233 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
7234 -- The packed array type is a byte array
7236 Packed_Num : Nat;
7237 -- Number of components accumulated in current byte
7239 Comps : List_Id;
7240 -- Assembled list of packed values for equivalent aggregate
7242 Comp_Val : Uint;
7243 -- integer value of component
7245 Incr : Int;
7246 -- Step size for packing
7248 Init_Shift : Int;
7249 -- Endian-dependent start position for packing
7251 Shift : Int;
7252 -- Current insertion position
7254 Val : Int;
7255 -- Component of packed array being assembled.
7257 begin
7258 Comps := New_List;
7259 Val := 0;
7260 Packed_Num := 0;
7262 -- Account for endianness. See corresponding comment in
7263 -- Packed_Array_Aggregate_Handled concerning the following.
7265 if Bytes_Big_Endian
7266 xor Debug_Flag_8
7267 xor Reverse_Storage_Order (Base_Type (Typ))
7268 then
7269 Init_Shift := Byte_Size - Comp_Size;
7270 Incr := -Comp_Size;
7271 else
7272 Init_Shift := 0;
7273 Incr := +Comp_Size;
7274 end if;
7276 -- Iterate over each subaggregate
7278 Shift := Init_Shift;
7279 One_Dim := First (Expressions (N));
7280 while Present (One_Dim) loop
7281 One_Comp := First (Expressions (One_Dim));
7282 while Present (One_Comp) loop
7283 if Packed_Num = Byte_Size / Comp_Size then
7285 -- Byte is complete, add to list of expressions
7287 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7288 Val := 0;
7289 Shift := Init_Shift;
7290 Packed_Num := 0;
7292 else
7293 Comp_Val := Expr_Rep_Value (One_Comp);
7295 -- Adjust for bias, and strip proper number of bits
7297 if Has_Biased_Representation (Ctyp) then
7298 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
7299 end if;
7301 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
7302 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
7303 Shift := Shift + Incr;
7304 One_Comp := Next (One_Comp);
7305 Packed_Num := Packed_Num + 1;
7306 end if;
7307 end loop;
7309 One_Dim := Next (One_Dim);
7310 end loop;
7312 if Packed_Num > 0 then
7314 -- Add final incomplete byte if present
7316 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
7317 end if;
7319 Rewrite (N,
7320 Unchecked_Convert_To (Typ,
7321 Make_Qualified_Expression (Loc,
7322 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
7323 Expression => Make_Aggregate (Loc, Expressions => Comps))));
7324 Analyze_And_Resolve (N);
7325 return True;
7326 end;
7327 end Two_Dim_Packed_Array_Handled;
7329 ---------------------
7330 -- Sort_Case_Table --
7331 ---------------------
7333 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
7334 L : constant Int := Case_Table'First;
7335 U : constant Int := Case_Table'Last;
7336 K : Int;
7337 J : Int;
7338 T : Case_Bounds;
7340 begin
7341 K := L;
7342 while K /= U loop
7343 T := Case_Table (K + 1);
7345 J := K + 1;
7346 while J /= L
7347 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
7348 Expr_Value (T.Choice_Lo)
7349 loop
7350 Case_Table (J) := Case_Table (J - 1);
7351 J := J - 1;
7352 end loop;
7354 Case_Table (J) := T;
7355 K := K + 1;
7356 end loop;
7357 end Sort_Case_Table;
7359 ----------------------------
7360 -- Static_Array_Aggregate --
7361 ----------------------------
7363 function Static_Array_Aggregate (N : Node_Id) return Boolean is
7364 Bounds : constant Node_Id := Aggregate_Bounds (N);
7366 Typ : constant Entity_Id := Etype (N);
7367 Comp_Type : constant Entity_Id := Component_Type (Typ);
7368 Agg : Node_Id;
7369 Expr : Node_Id;
7370 Lo : Node_Id;
7371 Hi : Node_Id;
7373 begin
7374 if Is_Tagged_Type (Typ)
7375 or else Is_Controlled (Typ)
7376 or else Is_Packed (Typ)
7377 then
7378 return False;
7379 end if;
7381 if Present (Bounds)
7382 and then Nkind (Bounds) = N_Range
7383 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
7384 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
7385 then
7386 Lo := Low_Bound (Bounds);
7387 Hi := High_Bound (Bounds);
7389 if No (Component_Associations (N)) then
7391 -- Verify that all components are static integers
7393 Expr := First (Expressions (N));
7394 while Present (Expr) loop
7395 if Nkind (Expr) /= N_Integer_Literal then
7396 return False;
7397 end if;
7399 Next (Expr);
7400 end loop;
7402 return True;
7404 else
7405 -- We allow only a single named association, either a static
7406 -- range or an others_clause, with a static expression.
7408 Expr := First (Component_Associations (N));
7410 if Present (Expressions (N)) then
7411 return False;
7413 elsif Present (Next (Expr)) then
7414 return False;
7416 elsif Present (Next (First (Choices (Expr)))) then
7417 return False;
7419 else
7420 -- The aggregate is static if all components are literals,
7421 -- or else all its components are static aggregates for the
7422 -- component type. We also limit the size of a static aggregate
7423 -- to prevent runaway static expressions.
7425 if Is_Array_Type (Comp_Type)
7426 or else Is_Record_Type (Comp_Type)
7427 then
7428 if Nkind (Expression (Expr)) /= N_Aggregate
7429 or else
7430 not Compile_Time_Known_Aggregate (Expression (Expr))
7431 then
7432 return False;
7433 end if;
7435 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
7436 return False;
7437 end if;
7439 if not Aggr_Size_OK (N, Typ) then
7440 return False;
7441 end if;
7443 -- Create a positional aggregate with the right number of
7444 -- copies of the expression.
7446 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
7448 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
7449 loop
7450 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
7452 -- The copied expression must be analyzed and resolved.
7453 -- Besides setting the type, this ensures that static
7454 -- expressions are appropriately marked as such.
7456 Analyze_And_Resolve
7457 (Last (Expressions (Agg)), Component_Type (Typ));
7458 end loop;
7460 Set_Aggregate_Bounds (Agg, Bounds);
7461 Set_Etype (Agg, Typ);
7462 Set_Analyzed (Agg);
7463 Rewrite (N, Agg);
7464 Set_Compile_Time_Known_Aggregate (N);
7466 return True;
7467 end if;
7468 end if;
7470 else
7471 return False;
7472 end if;
7473 end Static_Array_Aggregate;
7475 end Exp_Aggr;