* gcc.dg/Wtrampolines.c: XFAIL AIX.
[official-gcc.git] / gcc / ada / exp_aggr.adb
blobe83b07affdd16921ba5c483216dc8162af9d9cc2
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 Late_Expansion
99 (N : Node_Id;
100 Typ : Entity_Id;
101 Target : Node_Id) return List_Id;
102 -- This routine implements top-down expansion of nested aggregates. In
103 -- doing so, it avoids the generation of temporaries at each level. N is
104 -- a nested record or array aggregate with the Expansion_Delayed flag.
105 -- Typ is the expected type of the aggregate. Target is a (duplicatable)
106 -- expression that will hold the result of the aggregate expansion.
108 function Make_OK_Assignment_Statement
109 (Sloc : Source_Ptr;
110 Name : Node_Id;
111 Expression : Node_Id) return Node_Id;
112 -- This is like Make_Assignment_Statement, except that Assignment_OK
113 -- is set in the left operand. All assignments built by this unit use
114 -- this routine. This is needed to deal with assignments to initialized
115 -- constants that are done in place.
117 function Must_Slide
118 (Obj_Type : Entity_Id;
119 Typ : Entity_Id) return Boolean;
120 -- A static array aggregate in an object declaration can in most cases be
121 -- expanded in place. The one exception is when the aggregate is given
122 -- with component associations that specify different bounds from those of
123 -- the type definition in the object declaration. In this pathological
124 -- case the aggregate must slide, and we must introduce an intermediate
125 -- temporary to hold it.
127 -- The same holds in an assignment to one-dimensional array of arrays,
128 -- when a component may be given with bounds that differ from those of the
129 -- component type.
131 function Number_Of_Choices (N : Node_Id) return Nat;
132 -- Returns the number of discrete choices (not including the others choice
133 -- if present) contained in (sub-)aggregate N.
135 procedure Process_Transient_Component
136 (Loc : Source_Ptr;
137 Comp_Typ : Entity_Id;
138 Init_Expr : Node_Id;
139 Fin_Call : out Node_Id;
140 Hook_Clear : out Node_Id;
141 Aggr : Node_Id := Empty;
142 Stmts : List_Id := No_List);
143 -- Subsidiary to the expansion of array and record aggregates. Generate
144 -- part of the necessary code to finalize a transient component. Comp_Typ
145 -- is the component type. Init_Expr is the initialization expression of the
146 -- component which is always a function call. Fin_Call is the finalization
147 -- call used to clean up the transient function result. Hook_Clear is the
148 -- hook reset statement. Aggr and Stmts both control the placement of the
149 -- generated code. Aggr is the related aggregate. If present, all code is
150 -- inserted prior to Aggr using Insert_Action. Stmts is the initialization
151 -- statements of the component. If present, all code is added to Stmts.
153 procedure Process_Transient_Component_Completion
154 (Loc : Source_Ptr;
155 Aggr : Node_Id;
156 Fin_Call : Node_Id;
157 Hook_Clear : Node_Id;
158 Stmts : List_Id);
159 -- Subsidiary to the expansion of array and record aggregates. Generate
160 -- part of the necessary code to finalize a transient component. Aggr is
161 -- the related aggregate. Fin_Clear is the finalization call used to clean
162 -- up the transient component. Hook_Clear is the hook reset statment. Stmts
163 -- is the initialization statement list for the component. All generated
164 -- code is added to Stmts.
166 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type);
167 -- Sort the Case Table using the Lower Bound of each Choice as the key.
168 -- A simple insertion sort is used since the number of choices in a case
169 -- statement of variant part will usually be small and probably in near
170 -- sorted order.
172 ------------------------------------------------------
173 -- Local subprograms for Record Aggregate Expansion --
174 ------------------------------------------------------
176 function Build_Record_Aggr_Code
177 (N : Node_Id;
178 Typ : Entity_Id;
179 Lhs : Node_Id) return List_Id;
180 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
181 -- aggregate. Target is an expression containing the location on which the
182 -- component by component assignments will take place. Returns the list of
183 -- assignments plus all other adjustments needed for tagged and controlled
184 -- types.
186 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id);
187 -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the
188 -- aggregate (which can only be a record type, this procedure is only used
189 -- for record types). Transform the given aggregate into a sequence of
190 -- assignments performed component by component.
192 procedure Expand_Record_Aggregate
193 (N : Node_Id;
194 Orig_Tag : Node_Id := Empty;
195 Parent_Expr : Node_Id := Empty);
196 -- This is the top level procedure for record aggregate expansion.
197 -- Expansion for record aggregates needs expand aggregates for tagged
198 -- record types. Specifically Expand_Record_Aggregate adds the Tag
199 -- field in front of the Component_Association list that was created
200 -- during resolution by Resolve_Record_Aggregate.
202 -- N is the record aggregate node.
203 -- Orig_Tag is the value of the Tag that has to be provided for this
204 -- specific aggregate. It carries the tag corresponding to the type
205 -- of the outermost aggregate during the recursive expansion
206 -- Parent_Expr is the ancestor part of the original extension
207 -- aggregate
209 function Has_Mutable_Components (Typ : Entity_Id) return Boolean;
210 -- Return true if one of the components is of a discriminated type with
211 -- defaults. An aggregate for a type with mutable components must be
212 -- expanded into individual assignments.
214 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id);
215 -- If the type of the aggregate is a type extension with renamed discrimi-
216 -- nants, we must initialize the hidden discriminants of the parent.
217 -- Otherwise, the target object must not be initialized. The discriminants
218 -- are initialized by calling the initialization procedure for the type.
219 -- This is incorrect if the initialization of other components has any
220 -- side effects. We restrict this call to the case where the parent type
221 -- has a variant part, because this is the only case where the hidden
222 -- discriminants are accessed, namely when calling discriminant checking
223 -- functions of the parent type, and when applying a stream attribute to
224 -- an object of the derived type.
226 -----------------------------------------------------
227 -- Local Subprograms for Array Aggregate Expansion --
228 -----------------------------------------------------
230 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean;
231 -- Very large static aggregates present problems to the back-end, and are
232 -- transformed into assignments and loops. This function verifies that the
233 -- total number of components of an aggregate is acceptable for rewriting
234 -- into a purely positional static form. Aggr_Size_OK must be called before
235 -- calling Flatten.
237 -- This function also detects and warns about one-component aggregates that
238 -- appear in a non-static context. Even if the component value is static,
239 -- such an aggregate must be expanded into an assignment.
241 function Backend_Processing_Possible (N : Node_Id) return Boolean;
242 -- This function checks if array aggregate N can be processed directly
243 -- by the backend. If this is the case, True is returned.
245 function Build_Array_Aggr_Code
246 (N : Node_Id;
247 Ctype : Entity_Id;
248 Index : Node_Id;
249 Into : Node_Id;
250 Scalar_Comp : Boolean;
251 Indexes : List_Id := No_List) return List_Id;
252 -- This recursive routine returns a list of statements containing the
253 -- loops and assignments that are needed for the expansion of the array
254 -- aggregate N.
256 -- N is the (sub-)aggregate node to be expanded into code. This node has
257 -- been fully analyzed, and its Etype is properly set.
259 -- Index is the index node corresponding to the array subaggregate N
261 -- Into is the target expression into which we are copying the aggregate.
262 -- Note that this node may not have been analyzed yet, and so the Etype
263 -- field may not be set.
265 -- Scalar_Comp is True if the component type of the aggregate is scalar
267 -- Indexes is the current list of expressions used to index the object we
268 -- are writing into.
270 procedure Convert_Array_Aggr_In_Allocator
271 (Decl : Node_Id;
272 Aggr : Node_Id;
273 Target : Node_Id);
274 -- If the aggregate appears within an allocator and can be expanded in
275 -- place, this routine generates the individual assignments to components
276 -- of the designated object. This is an optimization over the general
277 -- case, where a temporary is first created on the stack and then used to
278 -- construct the allocated object on the heap.
280 procedure Convert_To_Positional
281 (N : Node_Id;
282 Max_Others_Replicate : Nat := 5;
283 Handle_Bit_Packed : Boolean := False);
284 -- If possible, convert named notation to positional notation. This
285 -- conversion is possible only in some static cases. If the conversion is
286 -- possible, then N is rewritten with the analyzed converted aggregate.
287 -- The parameter Max_Others_Replicate controls the maximum number of
288 -- values corresponding to an others choice that will be converted to
289 -- positional notation (the default of 5 is the normal limit, and reflects
290 -- the fact that normally the loop is better than a lot of separate
291 -- assignments). Note that this limit gets overridden in any case if
292 -- either of the restrictions No_Elaboration_Code or No_Implicit_Loops is
293 -- set. The parameter Handle_Bit_Packed is usually set False (since we do
294 -- not expect the back end to handle bit packed arrays, so the normal case
295 -- of conversion is pointless), but in the special case of a call from
296 -- Packed_Array_Aggregate_Handled, we set this parameter to True, since
297 -- these are cases we handle in there.
299 -- It would seem useful to have a higher default for Max_Others_Replicate,
300 -- but aggregates in the compiler make this impossible: the compiler
301 -- bootstrap fails if Max_Others_Replicate is greater than 25. This
302 -- is unexpected ???
304 procedure Expand_Array_Aggregate (N : Node_Id);
305 -- This is the top-level routine to perform array aggregate expansion.
306 -- N is the N_Aggregate node to be expanded.
308 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean;
309 -- For two-dimensional packed aggregates with constant bounds and constant
310 -- components, it is preferable to pack the inner aggregates because the
311 -- whole matrix can then be presented to the back-end as a one-dimensional
312 -- list of literals. This is much more efficient than expanding into single
313 -- component assignments. This function determines if the type Typ is for
314 -- an array that is suitable for this optimization: it returns True if Typ
315 -- is a two dimensional bit packed array with component size 1, 2, or 4.
317 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean;
318 -- Given an array aggregate, this function handles the case of a packed
319 -- array aggregate with all constant values, where the aggregate can be
320 -- evaluated at compile time. If this is possible, then N is rewritten
321 -- to be its proper compile time value with all the components properly
322 -- assembled. The expression is analyzed and resolved and True is returned.
323 -- If this transformation is not possible, N is unchanged and False is
324 -- returned.
326 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean;
327 -- If the type of the aggregate is a two-dimensional bit_packed array
328 -- it may be transformed into an array of bytes with constant values,
329 -- and presented to the back-end as a static value. The function returns
330 -- false if this transformation cannot be performed. THis is similar to,
331 -- and reuses part of the machinery in Packed_Array_Aggregate_Handled.
333 ------------------
334 -- Aggr_Size_OK --
335 ------------------
337 function Aggr_Size_OK (N : Node_Id; Typ : Entity_Id) return Boolean is
338 Lo : Node_Id;
339 Hi : Node_Id;
340 Indx : Node_Id;
341 Siz : Int;
342 Lov : Uint;
343 Hiv : Uint;
345 Max_Aggr_Size : Nat;
346 -- Determines the maximum size of an array aggregate produced by
347 -- converting named to positional notation (e.g. from others clauses).
348 -- This avoids running away with attempts to convert huge aggregates,
349 -- which hit memory limits in the backend.
351 function Component_Count (T : Entity_Id) return Nat;
352 -- The limit is applied to the total number of components that the
353 -- aggregate will have, which is the number of static expressions
354 -- that will appear in the flattened array. This requires a recursive
355 -- computation of the number of scalar components of the structure.
357 ---------------------
358 -- Component_Count --
359 ---------------------
361 function Component_Count (T : Entity_Id) return Nat is
362 Res : Nat := 0;
363 Comp : Entity_Id;
365 begin
366 if Is_Scalar_Type (T) then
367 return 1;
369 elsif Is_Record_Type (T) then
370 Comp := First_Component (T);
371 while Present (Comp) loop
372 Res := Res + Component_Count (Etype (Comp));
373 Next_Component (Comp);
374 end loop;
376 return Res;
378 elsif Is_Array_Type (T) then
379 declare
380 Lo : constant Node_Id :=
381 Type_Low_Bound (Etype (First_Index (T)));
382 Hi : constant Node_Id :=
383 Type_High_Bound (Etype (First_Index (T)));
385 Siz : constant Nat := Component_Count (Component_Type (T));
387 begin
388 -- Check for superflat arrays, i.e. arrays with such bounds
389 -- as 4 .. 2, to insure that this function never returns a
390 -- meaningless negative value.
392 if not Compile_Time_Known_Value (Lo)
393 or else not Compile_Time_Known_Value (Hi)
394 or else Expr_Value (Hi) < Expr_Value (Lo)
395 then
396 return 0;
398 else
399 return
400 Siz * UI_To_Int (Expr_Value (Hi) - Expr_Value (Lo) + 1);
401 end if;
402 end;
404 else
405 -- Can only be a null for an access type
407 return 1;
408 end if;
409 end Component_Count;
411 -- Start of processing for Aggr_Size_OK
413 begin
414 -- The normal aggregate limit is 50000, but we increase this limit to
415 -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) or
416 -- Restrictions (No_Implicit_Loops) is specified, since in either case
417 -- we are at risk of declaring the program illegal because of this
418 -- limit. We also increase the limit when Static_Elaboration_Desired,
419 -- given that this means that objects are intended to be placed in data
420 -- memory.
422 -- We also increase the limit if the aggregate is for a packed two-
423 -- dimensional array, because if components are static it is much more
424 -- efficient to construct a one-dimensional equivalent array with static
425 -- components.
427 -- Conversely, we decrease the maximum size if none of the above
428 -- requirements apply, and if the aggregate has a single component
429 -- association, which will be more efficient if implemented with a loop.
431 -- Finally, we use a small limit in CodePeer mode where we favor loops
432 -- instead of thousands of single assignments (from large aggregates).
434 Max_Aggr_Size := 50000;
436 if CodePeer_Mode then
437 Max_Aggr_Size := 100;
439 elsif Restriction_Active (No_Elaboration_Code)
440 or else Restriction_Active (No_Implicit_Loops)
441 or else Is_Two_Dim_Packed_Array (Typ)
442 or else (Ekind (Current_Scope) = E_Package
443 and then Static_Elaboration_Desired (Current_Scope))
444 then
445 Max_Aggr_Size := 2 ** 24;
447 elsif No (Expressions (N))
448 and then No (Next (First (Component_Associations (N))))
449 then
450 Max_Aggr_Size := 5000;
451 end if;
453 Siz := Component_Count (Component_Type (Typ));
455 Indx := First_Index (Typ);
456 while Present (Indx) loop
457 Lo := Type_Low_Bound (Etype (Indx));
458 Hi := Type_High_Bound (Etype (Indx));
460 -- Bounds need to be known at compile time
462 if not Compile_Time_Known_Value (Lo)
463 or else not Compile_Time_Known_Value (Hi)
464 then
465 return False;
466 end if;
468 Lov := Expr_Value (Lo);
469 Hiv := Expr_Value (Hi);
471 -- A flat array is always safe
473 if Hiv < Lov then
474 return True;
475 end if;
477 -- One-component aggregates are suspicious, and if the context type
478 -- is an object declaration with non-static bounds it will trip gcc;
479 -- such an aggregate must be expanded into a single assignment.
481 if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then
482 declare
483 Index_Type : constant Entity_Id :=
484 Etype
485 (First_Index (Etype (Defining_Identifier (Parent (N)))));
486 Indx : Node_Id;
488 begin
489 if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type))
490 or else not Compile_Time_Known_Value
491 (Type_High_Bound (Index_Type))
492 then
493 if Present (Component_Associations (N)) then
494 Indx :=
495 First (Choices (First (Component_Associations (N))));
497 if Is_Entity_Name (Indx)
498 and then not Is_Type (Entity (Indx))
499 then
500 Error_Msg_N
501 ("single component aggregate in "
502 & "non-static context??", Indx);
503 Error_Msg_N ("\maybe subtype name was meant??", Indx);
504 end if;
505 end if;
507 return False;
508 end if;
509 end;
510 end if;
512 declare
513 Rng : constant Uint := Hiv - Lov + 1;
515 begin
516 -- Check if size is too large
518 if not UI_Is_In_Int_Range (Rng) then
519 return False;
520 end if;
522 Siz := Siz * UI_To_Int (Rng);
523 end;
525 if Siz <= 0
526 or else Siz > Max_Aggr_Size
527 then
528 return False;
529 end if;
531 -- Bounds must be in integer range, for later array construction
533 if not UI_Is_In_Int_Range (Lov)
534 or else
535 not UI_Is_In_Int_Range (Hiv)
536 then
537 return False;
538 end if;
540 Next_Index (Indx);
541 end loop;
543 return True;
544 end Aggr_Size_OK;
546 ---------------------------------
547 -- Backend_Processing_Possible --
548 ---------------------------------
550 -- Backend processing by Gigi/gcc is possible only if all the following
551 -- conditions are met:
553 -- 1. N is fully positional
555 -- 2. N is not a bit-packed array aggregate;
557 -- 3. The size of N's array type must be known at compile time. Note
558 -- that this implies that the component size is also known
560 -- 4. The array type of N does not follow the Fortran layout convention
561 -- or if it does it must be 1 dimensional.
563 -- 5. The array component type may not be tagged (which could necessitate
564 -- reassignment of proper tags).
566 -- 6. The array component type must not have unaligned bit components
568 -- 7. None of the components of the aggregate may be bit unaligned
569 -- components.
571 -- 8. There cannot be delayed components, since we do not know enough
572 -- at this stage to know if back end processing is possible.
574 -- 9. There cannot be any discriminated record components, since the
575 -- back end cannot handle this complex case.
577 -- 10. No controlled actions need to be generated for components
579 -- 11. When generating C code, N must be part of a N_Object_Declaration
581 -- 12. When generating C code, N must not include function calls
583 function Backend_Processing_Possible (N : Node_Id) return Boolean is
584 Typ : constant Entity_Id := Etype (N);
585 -- Typ is the correct constrained array subtype of the aggregate
587 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean;
588 -- This routine checks components of aggregate N, enforcing checks
589 -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks
590 -- are performed on subaggregates. The Index value is the current index
591 -- being checked in the multidimensional case.
593 ---------------------
594 -- Component_Check --
595 ---------------------
597 function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is
598 function Ultimate_Original_Expression (N : Node_Id) return Node_Id;
599 -- Given a type conversion or an unchecked type conversion N, return
600 -- its innermost original expression.
602 ----------------------------------
603 -- Ultimate_Original_Expression --
604 ----------------------------------
606 function Ultimate_Original_Expression (N : Node_Id) return Node_Id is
607 Expr : Node_Id := Original_Node (N);
609 begin
610 while Nkind_In (Expr, N_Type_Conversion,
611 N_Unchecked_Type_Conversion)
612 loop
613 Expr := Original_Node (Expression (Expr));
614 end loop;
616 return Expr;
617 end Ultimate_Original_Expression;
619 -- Local variables
621 Expr : Node_Id;
623 -- Start of processing for Component_Check
625 begin
626 -- Checks 1: (no component associations)
628 if Present (Component_Associations (N)) then
629 return False;
630 end if;
632 -- Checks 11: (part of an object declaration)
634 if Modify_Tree_For_C
635 and then Nkind (Parent (N)) /= N_Object_Declaration
636 and then
637 (Nkind (Parent (N)) /= N_Qualified_Expression
638 or else Nkind (Parent (Parent (N))) /= N_Object_Declaration)
639 then
640 return False;
641 end if;
643 -- Checks on components
645 -- Recurse to check subaggregates, which may appear in qualified
646 -- expressions. If delayed, the front-end will have to expand.
647 -- If the component is a discriminated record, treat as non-static,
648 -- as the back-end cannot handle this properly.
650 Expr := First (Expressions (N));
651 while Present (Expr) loop
653 -- Checks 8: (no delayed components)
655 if Is_Delayed_Aggregate (Expr) then
656 return False;
657 end if;
659 -- Checks 9: (no discriminated records)
661 if Present (Etype (Expr))
662 and then Is_Record_Type (Etype (Expr))
663 and then Has_Discriminants (Etype (Expr))
664 then
665 return False;
666 end if;
668 -- Checks 7. Component must not be bit aligned component
670 if Possible_Bit_Aligned_Component (Expr) then
671 return False;
672 end if;
674 -- Checks 12: (no function call)
676 if Modify_Tree_For_C
677 and then
678 Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call
679 then
680 return False;
681 end if;
683 -- Recursion to following indexes for multiple dimension case
685 if Present (Next_Index (Index))
686 and then not Component_Check (Expr, Next_Index (Index))
687 then
688 return False;
689 end if;
691 -- All checks for that component finished, on to next
693 Next (Expr);
694 end loop;
696 return True;
697 end Component_Check;
699 -- Start of processing for Backend_Processing_Possible
701 begin
702 -- Checks 2 (array not bit packed) and 10 (no controlled actions)
704 if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then
705 return False;
706 end if;
708 -- If component is limited, aggregate must be expanded because each
709 -- component assignment must be built in place.
711 if Is_Limited_View (Component_Type (Typ)) then
712 return False;
713 end if;
715 -- Checks 4 (array must not be multidimensional Fortran case)
717 if Convention (Typ) = Convention_Fortran
718 and then Number_Dimensions (Typ) > 1
719 then
720 return False;
721 end if;
723 -- Checks 3 (size of array must be known at compile time)
725 if not Size_Known_At_Compile_Time (Typ) then
726 return False;
727 end if;
729 -- Checks on components
731 if not Component_Check (N, First_Index (Typ)) then
732 return False;
733 end if;
735 -- Checks 5 (if the component type is tagged, then we may need to do
736 -- tag adjustments. Perhaps this should be refined to check for any
737 -- component associations that actually need tag adjustment, similar
738 -- to the test in Component_Not_OK_For_Backend for record aggregates
739 -- with tagged components, but not clear whether it's worthwhile ???;
740 -- in the case of virtual machines (no Tagged_Type_Expansion), object
741 -- tags are handled implicitly).
743 if Is_Tagged_Type (Component_Type (Typ))
744 and then Tagged_Type_Expansion
745 then
746 return False;
747 end if;
749 -- Checks 6 (component type must not have bit aligned components)
751 if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then
752 return False;
753 end if;
755 -- Backend processing is possible
757 Set_Size_Known_At_Compile_Time (Etype (N), True);
758 return True;
759 end Backend_Processing_Possible;
761 ---------------------------
762 -- Build_Array_Aggr_Code --
763 ---------------------------
765 -- The code that we generate from a one dimensional aggregate is
767 -- 1. If the subaggregate contains discrete choices we
769 -- (a) Sort the discrete choices
771 -- (b) Otherwise for each discrete choice that specifies a range we
772 -- emit a loop. If a range specifies a maximum of three values, or
773 -- we are dealing with an expression we emit a sequence of
774 -- assignments instead of a loop.
776 -- (c) Generate the remaining loops to cover the others choice if any
778 -- 2. If the aggregate contains positional elements we
780 -- (a) translate the positional elements in a series of assignments
782 -- (b) Generate a final loop to cover the others choice if any.
783 -- Note that this final loop has to be a while loop since the case
785 -- L : Integer := Integer'Last;
786 -- H : Integer := Integer'Last;
787 -- A : array (L .. H) := (1, others =>0);
789 -- cannot be handled by a for loop. Thus for the following
791 -- array (L .. H) := (.. positional elements.., others =>E);
793 -- we always generate something like:
795 -- J : Index_Type := Index_Of_Last_Positional_Element;
796 -- while J < H loop
797 -- J := Index_Base'Succ (J)
798 -- Tmp (J) := E;
799 -- end loop;
801 function Build_Array_Aggr_Code
802 (N : Node_Id;
803 Ctype : Entity_Id;
804 Index : Node_Id;
805 Into : Node_Id;
806 Scalar_Comp : Boolean;
807 Indexes : List_Id := No_List) return List_Id
809 Loc : constant Source_Ptr := Sloc (N);
810 Index_Base : constant Entity_Id := Base_Type (Etype (Index));
811 Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base);
812 Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base);
814 function Add (Val : Int; To : Node_Id) return Node_Id;
815 -- Returns an expression where Val is added to expression To, unless
816 -- To+Val is provably out of To's base type range. To must be an
817 -- already analyzed expression.
819 function Empty_Range (L, H : Node_Id) return Boolean;
820 -- Returns True if the range defined by L .. H is certainly empty
822 function Equal (L, H : Node_Id) return Boolean;
823 -- Returns True if L = H for sure
825 function Index_Base_Name return Node_Id;
826 -- Returns a new reference to the index type name
828 function Gen_Assign
829 (Ind : Node_Id;
830 Expr : Node_Id;
831 In_Loop : Boolean := False) return List_Id;
832 -- Ind must be a side-effect-free expression. If the input aggregate N
833 -- to Build_Loop contains no subaggregates, then this function returns
834 -- the assignment statement:
836 -- Into (Indexes, Ind) := Expr;
838 -- Otherwise we call Build_Code recursively. Flag In_Loop should be set
839 -- when the assignment appears within a generated loop.
841 -- Ada 2005 (AI-287): In case of default initialized component, Expr
842 -- is empty and we generate a call to the corresponding IP subprogram.
844 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id;
845 -- Nodes L and H must be side-effect-free expressions. If the input
846 -- aggregate N to Build_Loop contains no subaggregates, this routine
847 -- returns the for loop statement:
849 -- for J in Index_Base'(L) .. Index_Base'(H) loop
850 -- Into (Indexes, J) := Expr;
851 -- end loop;
853 -- Otherwise we call Build_Code recursively. As an optimization if the
854 -- loop covers 3 or fewer scalar elements we generate a sequence of
855 -- assignments.
857 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id;
858 -- Nodes L and H must be side-effect-free expressions. If the input
859 -- aggregate N to Build_Loop contains no subaggregates, this routine
860 -- returns the while loop statement:
862 -- J : Index_Base := L;
863 -- while J < H loop
864 -- J := Index_Base'Succ (J);
865 -- Into (Indexes, J) := Expr;
866 -- end loop;
868 -- Otherwise we call Build_Code recursively
870 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id;
871 -- For an association with a box, use value given by aspect
872 -- Default_Component_Value of array type if specified, else use
873 -- value given by aspect Default_Value for component type itself
874 -- if specified, else return Empty.
876 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean;
877 function Local_Expr_Value (E : Node_Id) return Uint;
878 -- These two Local routines are used to replace the corresponding ones
879 -- in sem_eval because while processing the bounds of an aggregate with
880 -- discrete choices whose index type is an enumeration, we build static
881 -- expressions not recognized by Compile_Time_Known_Value as such since
882 -- they have not yet been analyzed and resolved. All the expressions in
883 -- question are things like Index_Base_Name'Val (Const) which we can
884 -- easily recognize as being constant.
886 ---------
887 -- Add --
888 ---------
890 function Add (Val : Int; To : Node_Id) return Node_Id is
891 Expr_Pos : Node_Id;
892 Expr : Node_Id;
893 To_Pos : Node_Id;
894 U_To : Uint;
895 U_Val : constant Uint := UI_From_Int (Val);
897 begin
898 -- Note: do not try to optimize the case of Val = 0, because
899 -- we need to build a new node with the proper Sloc value anyway.
901 -- First test if we can do constant folding
903 if Local_Compile_Time_Known_Value (To) then
904 U_To := Local_Expr_Value (To) + Val;
906 -- Determine if our constant is outside the range of the index.
907 -- If so return an Empty node. This empty node will be caught
908 -- by Empty_Range below.
910 if Compile_Time_Known_Value (Index_Base_L)
911 and then U_To < Expr_Value (Index_Base_L)
912 then
913 return Empty;
915 elsif Compile_Time_Known_Value (Index_Base_H)
916 and then U_To > Expr_Value (Index_Base_H)
917 then
918 return Empty;
919 end if;
921 Expr_Pos := Make_Integer_Literal (Loc, U_To);
922 Set_Is_Static_Expression (Expr_Pos);
924 if not Is_Enumeration_Type (Index_Base) then
925 Expr := Expr_Pos;
927 -- If we are dealing with enumeration return
928 -- Index_Base'Val (Expr_Pos)
930 else
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 if;
942 -- If we are here no constant folding possible
944 if not Is_Enumeration_Type (Index_Base) then
945 Expr :=
946 Make_Op_Add (Loc,
947 Left_Opnd => Duplicate_Subexpr (To),
948 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
950 -- If we are dealing with enumeration return
951 -- Index_Base'Val (Index_Base'Pos (To) + Val)
953 else
954 To_Pos :=
955 Make_Attribute_Reference
956 (Loc,
957 Prefix => Index_Base_Name,
958 Attribute_Name => Name_Pos,
959 Expressions => New_List (Duplicate_Subexpr (To)));
961 Expr_Pos :=
962 Make_Op_Add (Loc,
963 Left_Opnd => To_Pos,
964 Right_Opnd => Make_Integer_Literal (Loc, U_Val));
966 Expr :=
967 Make_Attribute_Reference
968 (Loc,
969 Prefix => Index_Base_Name,
970 Attribute_Name => Name_Val,
971 Expressions => New_List (Expr_Pos));
972 end if;
974 return Expr;
975 end Add;
977 -----------------
978 -- Empty_Range --
979 -----------------
981 function Empty_Range (L, H : Node_Id) return Boolean is
982 Is_Empty : Boolean := False;
983 Low : Node_Id;
984 High : Node_Id;
986 begin
987 -- First check if L or H were already detected as overflowing the
988 -- index base range type by function Add above. If this is so Add
989 -- returns the empty node.
991 if No (L) or else No (H) then
992 return True;
993 end if;
995 for J in 1 .. 3 loop
996 case J is
998 -- L > H range is empty
1000 when 1 =>
1001 Low := L;
1002 High := H;
1004 -- B_L > H range must be empty
1006 when 2 =>
1007 Low := Index_Base_L;
1008 High := H;
1010 -- L > B_H range must be empty
1012 when 3 =>
1013 Low := L;
1014 High := Index_Base_H;
1015 end case;
1017 if Local_Compile_Time_Known_Value (Low)
1018 and then
1019 Local_Compile_Time_Known_Value (High)
1020 then
1021 Is_Empty :=
1022 UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High));
1023 end if;
1025 exit when Is_Empty;
1026 end loop;
1028 return Is_Empty;
1029 end Empty_Range;
1031 -----------
1032 -- Equal --
1033 -----------
1035 function Equal (L, H : Node_Id) return Boolean is
1036 begin
1037 if L = H then
1038 return True;
1040 elsif Local_Compile_Time_Known_Value (L)
1041 and then
1042 Local_Compile_Time_Known_Value (H)
1043 then
1044 return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H));
1045 end if;
1047 return False;
1048 end Equal;
1050 ----------------
1051 -- Gen_Assign --
1052 ----------------
1054 function Gen_Assign
1055 (Ind : Node_Id;
1056 Expr : Node_Id;
1057 In_Loop : Boolean := False) return List_Id
1059 function Add_Loop_Actions (Lis : List_Id) return List_Id;
1060 -- Collect insert_actions generated in the construction of a loop,
1061 -- and prepend them to the sequence of assignments to complete the
1062 -- eventual body of the loop.
1064 procedure Initialize_Array_Component
1065 (Arr_Comp : Node_Id;
1066 Comp_Typ : Node_Id;
1067 Init_Expr : Node_Id;
1068 Stmts : List_Id);
1069 -- Perform the initialization of array component Arr_Comp with
1070 -- expected type Comp_Typ. Init_Expr denotes the initialization
1071 -- expression of the array component. All generated code is added
1072 -- to list Stmts.
1074 procedure Initialize_Ctrl_Array_Component
1075 (Arr_Comp : Node_Id;
1076 Comp_Typ : Entity_Id;
1077 Init_Expr : Node_Id;
1078 Stmts : List_Id);
1079 -- Perform the initialization of array component Arr_Comp when its
1080 -- expected type Comp_Typ needs finalization actions. Init_Expr is
1081 -- the initialization expression of the array component. All hook-
1082 -- related declarations are inserted prior to aggregate N. Remaining
1083 -- code is added to list Stmts.
1085 ----------------------
1086 -- Add_Loop_Actions --
1087 ----------------------
1089 function Add_Loop_Actions (Lis : List_Id) return List_Id is
1090 Res : List_Id;
1092 begin
1093 -- Ada 2005 (AI-287): Do nothing else in case of default
1094 -- initialized component.
1096 if No (Expr) then
1097 return Lis;
1099 elsif Nkind (Parent (Expr)) = N_Component_Association
1100 and then Present (Loop_Actions (Parent (Expr)))
1101 then
1102 Append_List (Lis, Loop_Actions (Parent (Expr)));
1103 Res := Loop_Actions (Parent (Expr));
1104 Set_Loop_Actions (Parent (Expr), No_List);
1105 return Res;
1107 else
1108 return Lis;
1109 end if;
1110 end Add_Loop_Actions;
1112 --------------------------------
1113 -- Initialize_Array_Component --
1114 --------------------------------
1116 procedure Initialize_Array_Component
1117 (Arr_Comp : Node_Id;
1118 Comp_Typ : Node_Id;
1119 Init_Expr : Node_Id;
1120 Stmts : List_Id)
1122 Exceptions_OK : constant Boolean :=
1123 not Restriction_Active
1124 (No_Exception_Propagation);
1126 Finalization_OK : constant Boolean :=
1127 Present (Comp_Typ)
1128 and then Needs_Finalization (Comp_Typ);
1130 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
1131 Blk_Stmts : List_Id;
1132 Init_Stmt : Node_Id;
1134 begin
1135 -- Protect the initialization statements from aborts. Generate:
1137 -- Abort_Defer;
1139 if Finalization_OK and Abort_Allowed then
1140 if Exceptions_OK then
1141 Blk_Stmts := New_List;
1142 else
1143 Blk_Stmts := Stmts;
1144 end if;
1146 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
1148 -- Otherwise aborts are not allowed. All generated code is added
1149 -- directly to the input list.
1151 else
1152 Blk_Stmts := Stmts;
1153 end if;
1155 -- Initialize the array element. Generate:
1157 -- Arr_Comp := Init_Expr;
1159 -- Note that the initialization expression is replicated because
1160 -- it has to be reevaluated within a generated loop.
1162 Init_Stmt :=
1163 Make_OK_Assignment_Statement (Loc,
1164 Name => New_Copy_Tree (Arr_Comp),
1165 Expression => New_Copy_Tree (Init_Expr));
1166 Set_No_Ctrl_Actions (Init_Stmt);
1168 -- If this is an aggregate for an array of arrays, each
1169 -- subaggregate will be expanded as well, and even with
1170 -- No_Ctrl_Actions the assignments of inner components will
1171 -- require attachment in their assignments to temporaries. These
1172 -- temporaries must be finalized for each subaggregate. Generate:
1174 -- begin
1175 -- Arr_Comp := Init_Expr;
1176 -- end;
1178 if Finalization_OK and then Is_Array_Type (Comp_Typ) then
1179 Init_Stmt :=
1180 Make_Block_Statement (Loc,
1181 Handled_Statement_Sequence =>
1182 Make_Handled_Sequence_Of_Statements (Loc,
1183 Statements => New_List (Init_Stmt)));
1184 end if;
1186 Append_To (Blk_Stmts, Init_Stmt);
1188 -- Adjust the tag due to a possible view conversion. Generate:
1190 -- Arr_Comp._tag := Full_TypP;
1192 if Tagged_Type_Expansion
1193 and then Present (Comp_Typ)
1194 and then Is_Tagged_Type (Comp_Typ)
1195 then
1196 Append_To (Blk_Stmts,
1197 Make_OK_Assignment_Statement (Loc,
1198 Name =>
1199 Make_Selected_Component (Loc,
1200 Prefix => New_Copy_Tree (Arr_Comp),
1201 Selector_Name =>
1202 New_Occurrence_Of
1203 (First_Tag_Component (Full_Typ), Loc)),
1205 Expression =>
1206 Unchecked_Convert_To (RTE (RE_Tag),
1207 New_Occurrence_Of
1208 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
1209 Loc))));
1210 end if;
1212 -- Adjust the array component. Controlled subaggregates are not
1213 -- considered because each of their individual elements will
1214 -- receive an adjustment of its own. Generate:
1216 -- [Deep_]Adjust (Arr_Comp);
1218 if Finalization_OK
1219 and then not Is_Limited_Type (Comp_Typ)
1220 and then not
1221 (Is_Array_Type (Comp_Typ)
1222 and then Is_Controlled (Component_Type (Comp_Typ))
1223 and then Nkind (Expr) = N_Aggregate)
1224 then
1225 Append_To (Blk_Stmts,
1226 Make_Adjust_Call
1227 (Obj_Ref => New_Copy_Tree (Arr_Comp),
1228 Typ => Comp_Typ));
1229 end if;
1231 -- Complete the protection of the initialization statements
1233 if Finalization_OK and Abort_Allowed then
1235 -- Wrap the initialization statements in a block to catch a
1236 -- potential exception. Generate:
1238 -- begin
1239 -- Abort_Defer;
1240 -- Arr_Comp := Init_Expr;
1241 -- Arr_Comp._tag := Full_TypP;
1242 -- [Deep_]Adjust (Arr_Comp);
1243 -- at end
1244 -- Abort_Undefer_Direct;
1245 -- end;
1247 if Exceptions_OK then
1248 Append_To (Stmts,
1249 Build_Abort_Undefer_Block (Loc,
1250 Stmts => Blk_Stmts,
1251 Context => N));
1253 -- Otherwise exceptions are not propagated. Generate:
1255 -- Abort_Defer;
1256 -- Arr_Comp := Init_Expr;
1257 -- Arr_Comp._tag := Full_TypP;
1258 -- [Deep_]Adjust (Arr_Comp);
1259 -- Abort_Undefer;
1261 else
1262 Append_To (Blk_Stmts,
1263 Build_Runtime_Call (Loc, RE_Abort_Undefer));
1264 end if;
1265 end if;
1266 end Initialize_Array_Component;
1268 -------------------------------------
1269 -- Initialize_Ctrl_Array_Component --
1270 -------------------------------------
1272 procedure Initialize_Ctrl_Array_Component
1273 (Arr_Comp : Node_Id;
1274 Comp_Typ : Entity_Id;
1275 Init_Expr : Node_Id;
1276 Stmts : List_Id)
1278 Act_Aggr : Node_Id;
1279 Act_Stmts : List_Id;
1280 Expr : Node_Id;
1281 Fin_Call : Node_Id;
1282 Hook_Clear : Node_Id;
1284 In_Place_Expansion : Boolean;
1285 -- Flag set when a nonlimited controlled function call requires
1286 -- in-place expansion.
1288 begin
1289 -- Duplicate the initialization expression in case the context is
1290 -- a multi choice list or an "others" choice which plugs various
1291 -- holes in the aggregate. As a result the expression is no longer
1292 -- shared between the various components and is reevaluated for
1293 -- each such component.
1295 Expr := New_Copy_Tree (Init_Expr);
1296 Set_Parent (Expr, Parent (Init_Expr));
1298 -- Perform a preliminary analysis and resolution to determine what
1299 -- the initialization expression denotes. An unanalyzed function
1300 -- call may appear as an identifier or an indexed component.
1302 if Nkind_In (Expr, N_Function_Call,
1303 N_Identifier,
1304 N_Indexed_Component)
1305 and then not Analyzed (Expr)
1306 then
1307 Preanalyze_And_Resolve (Expr, Comp_Typ);
1308 end if;
1310 In_Place_Expansion :=
1311 Nkind (Expr) = N_Function_Call
1312 and then not Is_Limited_Type (Comp_Typ);
1314 -- The initialization expression is a controlled function call.
1315 -- Perform in-place removal of side effects to avoid creating a
1316 -- transient scope, which leads to premature finalization.
1318 -- This in-place expansion is not performed for limited transient
1319 -- objects because the initialization is already done in-place.
1321 if In_Place_Expansion then
1323 -- Suppress the removal of side effects by general analysis
1324 -- because this behavior is emulated here. This avoids the
1325 -- generation of a transient scope, which leads to out-of-order
1326 -- adjustment and finalization.
1328 Set_No_Side_Effect_Removal (Expr);
1330 -- When the transient component initialization is related to a
1331 -- range or an "others", keep all generated statements within
1332 -- the enclosing loop. This way the controlled function call
1333 -- will be evaluated at each iteration, and its result will be
1334 -- finalized at the end of each iteration.
1336 if In_Loop then
1337 Act_Aggr := Empty;
1338 Act_Stmts := Stmts;
1340 -- Otherwise this is a single component initialization. Hook-
1341 -- related statements are inserted prior to the aggregate.
1343 else
1344 Act_Aggr := N;
1345 Act_Stmts := No_List;
1346 end if;
1348 -- Install all hook-related declarations and prepare the clean
1349 -- up statements.
1351 Process_Transient_Component
1352 (Loc => Loc,
1353 Comp_Typ => Comp_Typ,
1354 Init_Expr => Expr,
1355 Fin_Call => Fin_Call,
1356 Hook_Clear => Hook_Clear,
1357 Aggr => Act_Aggr,
1358 Stmts => Act_Stmts);
1359 end if;
1361 -- Use the noncontrolled component initialization circuitry to
1362 -- assign the result of the function call to the array element.
1363 -- This also performs subaggregate wrapping, tag adjustment, and
1364 -- [deep] adjustment of the array element.
1366 Initialize_Array_Component
1367 (Arr_Comp => Arr_Comp,
1368 Comp_Typ => Comp_Typ,
1369 Init_Expr => Expr,
1370 Stmts => Stmts);
1372 -- At this point the array element is fully initialized. Complete
1373 -- the processing of the controlled array component by finalizing
1374 -- the transient function result.
1376 if In_Place_Expansion then
1377 Process_Transient_Component_Completion
1378 (Loc => Loc,
1379 Aggr => N,
1380 Fin_Call => Fin_Call,
1381 Hook_Clear => Hook_Clear,
1382 Stmts => Stmts);
1383 end if;
1384 end Initialize_Ctrl_Array_Component;
1386 -- Local variables
1388 Stmts : constant List_Id := New_List;
1390 Comp_Typ : Entity_Id := Empty;
1391 Expr_Q : Node_Id;
1392 Indexed_Comp : Node_Id;
1393 New_Indexes : List_Id;
1395 -- Start of processing for Gen_Assign
1397 begin
1398 if No (Indexes) then
1399 New_Indexes := New_List;
1400 else
1401 New_Indexes := New_Copy_List_Tree (Indexes);
1402 end if;
1404 Append_To (New_Indexes, Ind);
1406 if Present (Next_Index (Index)) then
1407 return
1408 Add_Loop_Actions (
1409 Build_Array_Aggr_Code
1410 (N => Expr,
1411 Ctype => Ctype,
1412 Index => Next_Index (Index),
1413 Into => Into,
1414 Scalar_Comp => Scalar_Comp,
1415 Indexes => New_Indexes));
1416 end if;
1418 -- If we get here then we are at a bottom-level (sub-)aggregate
1420 Indexed_Comp :=
1421 Checks_Off
1422 (Make_Indexed_Component (Loc,
1423 Prefix => New_Copy_Tree (Into),
1424 Expressions => New_Indexes));
1426 Set_Assignment_OK (Indexed_Comp);
1428 -- Ada 2005 (AI-287): In case of default initialized component, Expr
1429 -- is not present (and therefore we also initialize Expr_Q to empty).
1431 if No (Expr) then
1432 Expr_Q := Empty;
1433 elsif Nkind (Expr) = N_Qualified_Expression then
1434 Expr_Q := Expression (Expr);
1435 else
1436 Expr_Q := Expr;
1437 end if;
1439 if Present (Etype (N)) and then Etype (N) /= Any_Composite then
1440 Comp_Typ := Component_Type (Etype (N));
1441 pragma Assert (Comp_Typ = Ctype); -- AI-287
1443 elsif Present (Next (First (New_Indexes))) then
1445 -- Ada 2005 (AI-287): Do nothing in case of default initialized
1446 -- component because we have received the component type in
1447 -- the formal parameter Ctype.
1449 -- ??? Some assert pragmas have been added to check if this new
1450 -- formal can be used to replace this code in all cases.
1452 if Present (Expr) then
1454 -- This is a multidimensional array. Recover the component type
1455 -- from the outermost aggregate, because subaggregates do not
1456 -- have an assigned type.
1458 declare
1459 P : Node_Id;
1461 begin
1462 P := Parent (Expr);
1463 while Present (P) loop
1464 if Nkind (P) = N_Aggregate
1465 and then Present (Etype (P))
1466 then
1467 Comp_Typ := Component_Type (Etype (P));
1468 exit;
1470 else
1471 P := Parent (P);
1472 end if;
1473 end loop;
1475 pragma Assert (Comp_Typ = Ctype); -- AI-287
1476 end;
1477 end if;
1478 end if;
1480 -- Ada 2005 (AI-287): We only analyze the expression in case of non-
1481 -- default initialized components (otherwise Expr_Q is not present).
1483 if Present (Expr_Q)
1484 and then Nkind_In (Expr_Q, N_Aggregate, N_Extension_Aggregate)
1485 then
1486 -- At this stage the Expression may not have been analyzed yet
1487 -- because the array aggregate code has not been updated to use
1488 -- the Expansion_Delayed flag and avoid analysis altogether to
1489 -- solve the same problem (see Resolve_Aggr_Expr). So let us do
1490 -- the analysis of non-array aggregates now in order to get the
1491 -- value of Expansion_Delayed flag for the inner aggregate ???
1493 if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then
1494 Analyze_And_Resolve (Expr_Q, Comp_Typ);
1495 end if;
1497 if Is_Delayed_Aggregate (Expr_Q) then
1499 -- This is either a subaggregate of a multidimensional array,
1500 -- or a component of an array type whose component type is
1501 -- also an array. In the latter case, the expression may have
1502 -- component associations that provide different bounds from
1503 -- those of the component type, and sliding must occur. Instead
1504 -- of decomposing the current aggregate assignment, force the
1505 -- reanalysis of the assignment, so that a temporary will be
1506 -- generated in the usual fashion, and sliding will take place.
1508 if Nkind (Parent (N)) = N_Assignment_Statement
1509 and then Is_Array_Type (Comp_Typ)
1510 and then Present (Component_Associations (Expr_Q))
1511 and then Must_Slide (Comp_Typ, Etype (Expr_Q))
1512 then
1513 Set_Expansion_Delayed (Expr_Q, False);
1514 Set_Analyzed (Expr_Q, False);
1516 else
1517 return
1518 Add_Loop_Actions (
1519 Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp));
1520 end if;
1521 end if;
1522 end if;
1524 if Present (Expr) then
1526 -- Handle an initialization expression of a controlled type in
1527 -- case it denotes a function call. In general such a scenario
1528 -- will produce a transient scope, but this will lead to wrong
1529 -- order of initialization, adjustment, and finalization in the
1530 -- context of aggregates.
1532 -- Target (1) := Ctrl_Func_Call;
1534 -- begin -- scope
1535 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
1536 -- Target (1) := Trans_Obj;
1537 -- Finalize (Trans_Obj);
1538 -- end;
1539 -- Target (1)._tag := ...;
1540 -- Adjust (Target (1));
1542 -- In the example above, the call to Finalize occurs too early
1543 -- and as a result it may leave the array component in a bad
1544 -- state. Finalization of the transient object should really
1545 -- happen after adjustment.
1547 -- To avoid this scenario, perform in-place side-effect removal
1548 -- of the function call. This eliminates the transient property
1549 -- of the function result and ensures correct order of actions.
1551 -- Res : ... := Ctrl_Func_Call;
1552 -- Target (1) := Res;
1553 -- Target (1)._tag := ...;
1554 -- Adjust (Target (1));
1555 -- Finalize (Res);
1557 if Present (Comp_Typ)
1558 and then Needs_Finalization (Comp_Typ)
1559 and then Nkind (Expr) /= N_Aggregate
1560 then
1561 Initialize_Ctrl_Array_Component
1562 (Arr_Comp => Indexed_Comp,
1563 Comp_Typ => Comp_Typ,
1564 Init_Expr => Expr,
1565 Stmts => Stmts);
1567 -- Otherwise perform simple component initialization
1569 else
1570 Initialize_Array_Component
1571 (Arr_Comp => Indexed_Comp,
1572 Comp_Typ => Comp_Typ,
1573 Init_Expr => Expr,
1574 Stmts => Stmts);
1575 end if;
1577 -- Ada 2005 (AI-287): In case of default initialized component, call
1578 -- the initialization subprogram associated with the component type.
1579 -- If the component type is an access type, add an explicit null
1580 -- assignment, because for the back-end there is an initialization
1581 -- present for the whole aggregate, and no default initialization
1582 -- will take place.
1584 -- In addition, if the component type is controlled, we must call
1585 -- its Initialize procedure explicitly, because there is no explicit
1586 -- object creation that will invoke it otherwise.
1588 else
1589 if Present (Base_Init_Proc (Base_Type (Ctype)))
1590 or else Has_Task (Base_Type (Ctype))
1591 then
1592 Append_List_To (Stmts,
1593 Build_Initialization_Call (Loc,
1594 Id_Ref => Indexed_Comp,
1595 Typ => Ctype,
1596 With_Default_Init => True));
1598 -- If the component type has invariants, add an invariant
1599 -- check after the component is default-initialized. It will
1600 -- be analyzed and resolved before the code for initialization
1601 -- of other components.
1603 if Has_Invariants (Ctype) then
1604 Set_Etype (Indexed_Comp, Ctype);
1605 Append_To (Stmts, Make_Invariant_Call (Indexed_Comp));
1606 end if;
1608 elsif Is_Access_Type (Ctype) then
1609 Append_To (Stmts,
1610 Make_Assignment_Statement (Loc,
1611 Name => New_Copy_Tree (Indexed_Comp),
1612 Expression => Make_Null (Loc)));
1613 end if;
1615 if Needs_Finalization (Ctype) then
1616 Append_To (Stmts,
1617 Make_Init_Call
1618 (Obj_Ref => New_Copy_Tree (Indexed_Comp),
1619 Typ => Ctype));
1620 end if;
1621 end if;
1623 return Add_Loop_Actions (Stmts);
1624 end Gen_Assign;
1626 --------------
1627 -- Gen_Loop --
1628 --------------
1630 function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is
1631 L_J : Node_Id;
1633 L_L : Node_Id;
1634 -- Index_Base'(L)
1636 L_H : Node_Id;
1637 -- Index_Base'(H)
1639 L_Range : Node_Id;
1640 -- Index_Base'(L) .. Index_Base'(H)
1642 L_Iteration_Scheme : Node_Id;
1643 -- L_J in Index_Base'(L) .. Index_Base'(H)
1645 L_Body : List_Id;
1646 -- The statements to execute in the loop
1648 S : constant List_Id := New_List;
1649 -- List of statements
1651 Tcopy : Node_Id;
1652 -- Copy of expression tree, used for checking purposes
1654 begin
1655 -- If loop bounds define an empty range return the null statement
1657 if Empty_Range (L, H) then
1658 Append_To (S, Make_Null_Statement (Loc));
1660 -- Ada 2005 (AI-287): Nothing else need to be done in case of
1661 -- default initialized component.
1663 if No (Expr) then
1664 null;
1666 else
1667 -- The expression must be type-checked even though no component
1668 -- of the aggregate will have this value. This is done only for
1669 -- actual components of the array, not for subaggregates. Do
1670 -- the check on a copy, because the expression may be shared
1671 -- among several choices, some of which might be non-null.
1673 if Present (Etype (N))
1674 and then Is_Array_Type (Etype (N))
1675 and then No (Next_Index (Index))
1676 then
1677 Expander_Mode_Save_And_Set (False);
1678 Tcopy := New_Copy_Tree (Expr);
1679 Set_Parent (Tcopy, N);
1680 Analyze_And_Resolve (Tcopy, Component_Type (Etype (N)));
1681 Expander_Mode_Restore;
1682 end if;
1683 end if;
1685 return S;
1687 -- If loop bounds are the same then generate an assignment
1689 elsif Equal (L, H) then
1690 return Gen_Assign (New_Copy_Tree (L), Expr);
1692 -- If H - L <= 2 then generate a sequence of assignments when we are
1693 -- processing the bottom most aggregate and it contains scalar
1694 -- components.
1696 elsif No (Next_Index (Index))
1697 and then Scalar_Comp
1698 and then Local_Compile_Time_Known_Value (L)
1699 and then Local_Compile_Time_Known_Value (H)
1700 and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2
1701 then
1702 Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr));
1703 Append_List_To (S, Gen_Assign (Add (1, To => L), Expr));
1705 if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then
1706 Append_List_To (S, Gen_Assign (Add (2, To => L), Expr));
1707 end if;
1709 return S;
1710 end if;
1712 -- Otherwise construct the loop, starting with the loop index L_J
1714 L_J := Make_Temporary (Loc, 'J', L);
1716 -- Construct "L .. H" in Index_Base. We use a qualified expression
1717 -- for the bound to convert to the index base, but we don't need
1718 -- to do that if we already have the base type at hand.
1720 if Etype (L) = Index_Base then
1721 L_L := L;
1722 else
1723 L_L :=
1724 Make_Qualified_Expression (Loc,
1725 Subtype_Mark => Index_Base_Name,
1726 Expression => L);
1727 end if;
1729 if Etype (H) = Index_Base then
1730 L_H := H;
1731 else
1732 L_H :=
1733 Make_Qualified_Expression (Loc,
1734 Subtype_Mark => Index_Base_Name,
1735 Expression => H);
1736 end if;
1738 L_Range :=
1739 Make_Range (Loc,
1740 Low_Bound => L_L,
1741 High_Bound => L_H);
1743 -- Construct "for L_J in Index_Base range L .. H"
1745 L_Iteration_Scheme :=
1746 Make_Iteration_Scheme
1747 (Loc,
1748 Loop_Parameter_Specification =>
1749 Make_Loop_Parameter_Specification
1750 (Loc,
1751 Defining_Identifier => L_J,
1752 Discrete_Subtype_Definition => L_Range));
1754 -- Construct the statements to execute in the loop body
1756 L_Body :=
1757 Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr, In_Loop => True);
1759 -- Construct the final loop
1761 Append_To (S,
1762 Make_Implicit_Loop_Statement
1763 (Node => N,
1764 Identifier => Empty,
1765 Iteration_Scheme => L_Iteration_Scheme,
1766 Statements => L_Body));
1768 -- A small optimization: if the aggregate is initialized with a box
1769 -- and the component type has no initialization procedure, remove the
1770 -- useless empty loop.
1772 if Nkind (First (S)) = N_Loop_Statement
1773 and then Is_Empty_List (Statements (First (S)))
1774 then
1775 return New_List (Make_Null_Statement (Loc));
1776 else
1777 return S;
1778 end if;
1779 end Gen_Loop;
1781 ---------------
1782 -- Gen_While --
1783 ---------------
1785 -- The code built is
1787 -- W_J : Index_Base := L;
1788 -- while W_J < H loop
1789 -- W_J := Index_Base'Succ (W);
1790 -- L_Body;
1791 -- end loop;
1793 function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is
1794 W_J : Node_Id;
1796 W_Decl : Node_Id;
1797 -- W_J : Base_Type := L;
1799 W_Iteration_Scheme : Node_Id;
1800 -- while W_J < H
1802 W_Index_Succ : Node_Id;
1803 -- Index_Base'Succ (J)
1805 W_Increment : Node_Id;
1806 -- W_J := Index_Base'Succ (W)
1808 W_Body : constant List_Id := New_List;
1809 -- The statements to execute in the loop
1811 S : constant List_Id := New_List;
1812 -- list of statement
1814 begin
1815 -- If loop bounds define an empty range or are equal return null
1817 if Empty_Range (L, H) or else Equal (L, H) then
1818 Append_To (S, Make_Null_Statement (Loc));
1819 return S;
1820 end if;
1822 -- Build the decl of W_J
1824 W_J := Make_Temporary (Loc, 'J', L);
1825 W_Decl :=
1826 Make_Object_Declaration
1827 (Loc,
1828 Defining_Identifier => W_J,
1829 Object_Definition => Index_Base_Name,
1830 Expression => L);
1832 -- Theoretically we should do a New_Copy_Tree (L) here, but we know
1833 -- that in this particular case L is a fresh Expr generated by
1834 -- Add which we are the only ones to use.
1836 Append_To (S, W_Decl);
1838 -- Construct " while W_J < H"
1840 W_Iteration_Scheme :=
1841 Make_Iteration_Scheme
1842 (Loc,
1843 Condition => Make_Op_Lt
1844 (Loc,
1845 Left_Opnd => New_Occurrence_Of (W_J, Loc),
1846 Right_Opnd => New_Copy_Tree (H)));
1848 -- Construct the statements to execute in the loop body
1850 W_Index_Succ :=
1851 Make_Attribute_Reference
1852 (Loc,
1853 Prefix => Index_Base_Name,
1854 Attribute_Name => Name_Succ,
1855 Expressions => New_List (New_Occurrence_Of (W_J, Loc)));
1857 W_Increment :=
1858 Make_OK_Assignment_Statement
1859 (Loc,
1860 Name => New_Occurrence_Of (W_J, Loc),
1861 Expression => W_Index_Succ);
1863 Append_To (W_Body, W_Increment);
1865 Append_List_To (W_Body,
1866 Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr, In_Loop => True));
1868 -- Construct the final loop
1870 Append_To (S,
1871 Make_Implicit_Loop_Statement
1872 (Node => N,
1873 Identifier => Empty,
1874 Iteration_Scheme => W_Iteration_Scheme,
1875 Statements => W_Body));
1877 return S;
1878 end Gen_While;
1880 --------------------
1881 -- Get_Assoc_Expr --
1882 --------------------
1884 function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is
1885 Typ : constant Entity_Id := Base_Type (Etype (N));
1887 begin
1888 if Box_Present (Assoc) then
1889 if Is_Scalar_Type (Ctype) then
1890 if Present (Default_Aspect_Component_Value (Typ)) then
1891 return Default_Aspect_Component_Value (Typ);
1892 elsif Present (Default_Aspect_Value (Ctype)) then
1893 return Default_Aspect_Value (Ctype);
1894 else
1895 return Empty;
1896 end if;
1898 else
1899 return Empty;
1900 end if;
1902 else
1903 return Expression (Assoc);
1904 end if;
1905 end Get_Assoc_Expr;
1907 ---------------------
1908 -- Index_Base_Name --
1909 ---------------------
1911 function Index_Base_Name return Node_Id is
1912 begin
1913 return New_Occurrence_Of (Index_Base, Sloc (N));
1914 end Index_Base_Name;
1916 ------------------------------------
1917 -- Local_Compile_Time_Known_Value --
1918 ------------------------------------
1920 function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is
1921 begin
1922 return Compile_Time_Known_Value (E)
1923 or else
1924 (Nkind (E) = N_Attribute_Reference
1925 and then Attribute_Name (E) = Name_Val
1926 and then Compile_Time_Known_Value (First (Expressions (E))));
1927 end Local_Compile_Time_Known_Value;
1929 ----------------------
1930 -- Local_Expr_Value --
1931 ----------------------
1933 function Local_Expr_Value (E : Node_Id) return Uint is
1934 begin
1935 if Compile_Time_Known_Value (E) then
1936 return Expr_Value (E);
1937 else
1938 return Expr_Value (First (Expressions (E)));
1939 end if;
1940 end Local_Expr_Value;
1942 -- Local variables
1944 New_Code : constant List_Id := New_List;
1946 Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N));
1947 Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N));
1948 -- The aggregate bounds of this specific subaggregate. Note that if the
1949 -- code generated by Build_Array_Aggr_Code is executed then these bounds
1950 -- are OK. Otherwise a Constraint_Error would have been raised.
1952 Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L);
1953 Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H);
1954 -- After Duplicate_Subexpr these are side-effect free
1956 Assoc : Node_Id;
1957 Choice : Node_Id;
1958 Expr : Node_Id;
1959 High : Node_Id;
1960 Low : Node_Id;
1961 Typ : Entity_Id;
1963 Nb_Choices : Nat := 0;
1964 Table : Case_Table_Type (1 .. Number_Of_Choices (N));
1965 -- Used to sort all the different choice values
1967 Nb_Elements : Int;
1968 -- Number of elements in the positional aggregate
1970 Others_Assoc : Node_Id := Empty;
1972 -- Start of processing for Build_Array_Aggr_Code
1974 begin
1975 -- First before we start, a special case. if we have a bit packed
1976 -- array represented as a modular type, then clear the value to
1977 -- zero first, to ensure that unused bits are properly cleared.
1979 Typ := Etype (N);
1981 if Present (Typ)
1982 and then Is_Bit_Packed_Array (Typ)
1983 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ))
1984 then
1985 Append_To (New_Code,
1986 Make_Assignment_Statement (Loc,
1987 Name => New_Copy_Tree (Into),
1988 Expression =>
1989 Unchecked_Convert_To (Typ,
1990 Make_Integer_Literal (Loc, Uint_0))));
1991 end if;
1993 -- If the component type contains tasks, we need to build a Master
1994 -- entity in the current scope, because it will be needed if build-
1995 -- in-place functions are called in the expanded code.
1997 if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then
1998 Build_Master_Entity (Defining_Identifier (Parent (N)));
1999 end if;
2001 -- STEP 1: Process component associations
2003 -- For those associations that may generate a loop, initialize
2004 -- Loop_Actions to collect inserted actions that may be crated.
2006 -- Skip this if no component associations
2008 if No (Expressions (N)) then
2010 -- STEP 1 (a): Sort the discrete choices
2012 Assoc := First (Component_Associations (N));
2013 while Present (Assoc) loop
2014 Choice := First (Choices (Assoc));
2015 while Present (Choice) loop
2016 if Nkind (Choice) = N_Others_Choice then
2017 Set_Loop_Actions (Assoc, New_List);
2018 Others_Assoc := Assoc;
2019 exit;
2020 end if;
2022 Get_Index_Bounds (Choice, Low, High);
2024 if Low /= High then
2025 Set_Loop_Actions (Assoc, New_List);
2026 end if;
2028 Nb_Choices := Nb_Choices + 1;
2030 Table (Nb_Choices) :=
2031 (Choice_Lo => Low,
2032 Choice_Hi => High,
2033 Choice_Node => Get_Assoc_Expr (Assoc));
2035 Next (Choice);
2036 end loop;
2038 Next (Assoc);
2039 end loop;
2041 -- If there is more than one set of choices these must be static
2042 -- and we can therefore sort them. Remember that Nb_Choices does not
2043 -- account for an others choice.
2045 if Nb_Choices > 1 then
2046 Sort_Case_Table (Table);
2047 end if;
2049 -- STEP 1 (b): take care of the whole set of discrete choices
2051 for J in 1 .. Nb_Choices loop
2052 Low := Table (J).Choice_Lo;
2053 High := Table (J).Choice_Hi;
2054 Expr := Table (J).Choice_Node;
2055 Append_List (Gen_Loop (Low, High, Expr), To => New_Code);
2056 end loop;
2058 -- STEP 1 (c): generate the remaining loops to cover others choice
2059 -- We don't need to generate loops over empty gaps, but if there is
2060 -- a single empty range we must analyze the expression for semantics
2062 if Present (Others_Assoc) then
2063 declare
2064 First : Boolean := True;
2066 begin
2067 for J in 0 .. Nb_Choices loop
2068 if J = 0 then
2069 Low := Aggr_Low;
2070 else
2071 Low := Add (1, To => Table (J).Choice_Hi);
2072 end if;
2074 if J = Nb_Choices then
2075 High := Aggr_High;
2076 else
2077 High := Add (-1, To => Table (J + 1).Choice_Lo);
2078 end if;
2080 -- If this is an expansion within an init proc, make
2081 -- sure that discriminant references are replaced by
2082 -- the corresponding discriminal.
2084 if Inside_Init_Proc then
2085 if Is_Entity_Name (Low)
2086 and then Ekind (Entity (Low)) = E_Discriminant
2087 then
2088 Set_Entity (Low, Discriminal (Entity (Low)));
2089 end if;
2091 if Is_Entity_Name (High)
2092 and then Ekind (Entity (High)) = E_Discriminant
2093 then
2094 Set_Entity (High, Discriminal (Entity (High)));
2095 end if;
2096 end if;
2098 if First
2099 or else not Empty_Range (Low, High)
2100 then
2101 First := False;
2102 Append_List
2103 (Gen_Loop (Low, High,
2104 Get_Assoc_Expr (Others_Assoc)), To => New_Code);
2105 end if;
2106 end loop;
2107 end;
2108 end if;
2110 -- STEP 2: Process positional components
2112 else
2113 -- STEP 2 (a): Generate the assignments for each positional element
2114 -- Note that here we have to use Aggr_L rather than Aggr_Low because
2115 -- Aggr_L is analyzed and Add wants an analyzed expression.
2117 Expr := First (Expressions (N));
2118 Nb_Elements := -1;
2119 while Present (Expr) loop
2120 Nb_Elements := Nb_Elements + 1;
2121 Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr),
2122 To => New_Code);
2123 Next (Expr);
2124 end loop;
2126 -- STEP 2 (b): Generate final loop if an others choice is present
2127 -- Here Nb_Elements gives the offset of the last positional element.
2129 if Present (Component_Associations (N)) then
2130 Assoc := Last (Component_Associations (N));
2132 -- Ada 2005 (AI-287)
2134 Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L),
2135 Aggr_High,
2136 Get_Assoc_Expr (Assoc)), -- AI-287
2137 To => New_Code);
2138 end if;
2139 end if;
2141 return New_Code;
2142 end Build_Array_Aggr_Code;
2144 ----------------------------
2145 -- Build_Record_Aggr_Code --
2146 ----------------------------
2148 function Build_Record_Aggr_Code
2149 (N : Node_Id;
2150 Typ : Entity_Id;
2151 Lhs : Node_Id) return List_Id
2153 Loc : constant Source_Ptr := Sloc (N);
2154 L : constant List_Id := New_List;
2155 N_Typ : constant Entity_Id := Etype (N);
2157 Comp : Node_Id;
2158 Instr : Node_Id;
2159 Ref : Node_Id;
2160 Target : Entity_Id;
2161 Comp_Type : Entity_Id;
2162 Selector : Entity_Id;
2163 Comp_Expr : Node_Id;
2164 Expr_Q : Node_Id;
2166 -- If this is an internal aggregate, the External_Final_List is an
2167 -- expression for the controller record of the enclosing type.
2169 -- If the current aggregate has several controlled components, this
2170 -- expression will appear in several calls to attach to the finali-
2171 -- zation list, and it must not be shared.
2173 Ancestor_Is_Expression : Boolean := False;
2174 Ancestor_Is_Subtype_Mark : Boolean := False;
2176 Init_Typ : Entity_Id := Empty;
2178 Finalization_Done : Boolean := False;
2179 -- True if Generate_Finalization_Actions has already been called; calls
2180 -- after the first do nothing.
2182 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id;
2183 -- Returns the value that the given discriminant of an ancestor type
2184 -- should receive (in the absence of a conflict with the value provided
2185 -- by an ancestor part of an extension aggregate).
2187 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id);
2188 -- Check that each of the discriminant values defined by the ancestor
2189 -- part of an extension aggregate match the corresponding values
2190 -- provided by either an association of the aggregate or by the
2191 -- constraint imposed by a parent type (RM95-4.3.2(8)).
2193 function Compatible_Int_Bounds
2194 (Agg_Bounds : Node_Id;
2195 Typ_Bounds : Node_Id) return Boolean;
2196 -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is
2197 -- assumed that both bounds are integer ranges.
2199 procedure Generate_Finalization_Actions;
2200 -- Deal with the various controlled type data structure initializations
2201 -- (but only if it hasn't been done already).
2203 function Get_Constraint_Association (T : Entity_Id) return Node_Id;
2204 -- Returns the first discriminant association in the constraint
2205 -- associated with T, if any, otherwise returns Empty.
2207 function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id;
2208 -- If the ancestor part is an unconstrained type and further ancestors
2209 -- do not provide discriminants for it, check aggregate components for
2210 -- values of the discriminants.
2212 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id);
2213 -- If Typ is derived, and constrains discriminants of the parent type,
2214 -- these discriminants are not components of the aggregate, and must be
2215 -- initialized. The assignments are appended to List. The same is done
2216 -- if Typ derives fron an already constrained subtype of a discriminated
2217 -- parent type.
2219 procedure Init_Stored_Discriminants;
2220 -- If the type is derived and has inherited discriminants, generate
2221 -- explicit assignments for each, using the store constraint of the
2222 -- type. Note that both visible and stored discriminants must be
2223 -- initialized in case the derived type has some renamed and some
2224 -- constrained discriminants.
2226 procedure Init_Visible_Discriminants;
2227 -- If type has discriminants, retrieve their values from aggregate,
2228 -- and generate explicit assignments for each. This does not include
2229 -- discriminants inherited from ancestor, which are handled above.
2230 -- The type of the aggregate is a subtype created ealier using the
2231 -- given values of the discriminant components of the aggregate.
2233 procedure Initialize_Ctrl_Record_Component
2234 (Rec_Comp : Node_Id;
2235 Comp_Typ : Entity_Id;
2236 Init_Expr : Node_Id;
2237 Stmts : List_Id);
2238 -- Perform the initialization of controlled record component Rec_Comp.
2239 -- Comp_Typ is the component type. Init_Expr is the initialization
2240 -- expression for the record component. Hook-related declarations are
2241 -- inserted prior to aggregate N using Insert_Action. All remaining
2242 -- generated code is added to list Stmts.
2244 procedure Initialize_Record_Component
2245 (Rec_Comp : Node_Id;
2246 Comp_Typ : Entity_Id;
2247 Init_Expr : Node_Id;
2248 Stmts : List_Id);
2249 -- Perform the initialization of record component Rec_Comp. Comp_Typ
2250 -- is the component type. Init_Expr is the initialization expression
2251 -- of the record component. All generated code is added to list Stmts.
2253 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean;
2254 -- Check whether Bounds is a range node and its lower and higher bounds
2255 -- are integers literals.
2257 function Replace_Type (Expr : Node_Id) return Traverse_Result;
2258 -- If the aggregate contains a self-reference, traverse each expression
2259 -- to replace a possible self-reference with a reference to the proper
2260 -- component of the target of the assignment.
2262 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result;
2263 -- If default expression of a component mentions a discriminant of the
2264 -- type, it must be rewritten as the discriminant of the target object.
2266 ---------------------------------
2267 -- Ancestor_Discriminant_Value --
2268 ---------------------------------
2270 function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is
2271 Assoc : Node_Id;
2272 Assoc_Elmt : Elmt_Id;
2273 Aggr_Comp : Entity_Id;
2274 Corresp_Disc : Entity_Id;
2275 Current_Typ : Entity_Id := Base_Type (Typ);
2276 Parent_Typ : Entity_Id;
2277 Parent_Disc : Entity_Id;
2278 Save_Assoc : Node_Id := Empty;
2280 begin
2281 -- First check any discriminant associations to see if any of them
2282 -- provide a value for the discriminant.
2284 if Present (Discriminant_Specifications (Parent (Current_Typ))) then
2285 Assoc := First (Component_Associations (N));
2286 while Present (Assoc) loop
2287 Aggr_Comp := Entity (First (Choices (Assoc)));
2289 if Ekind (Aggr_Comp) = E_Discriminant then
2290 Save_Assoc := Expression (Assoc);
2292 Corresp_Disc := Corresponding_Discriminant (Aggr_Comp);
2293 while Present (Corresp_Disc) loop
2295 -- If found a corresponding discriminant then return the
2296 -- value given in the aggregate. (Note: this is not
2297 -- correct in the presence of side effects. ???)
2299 if Disc = Corresp_Disc then
2300 return Duplicate_Subexpr (Expression (Assoc));
2301 end if;
2303 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2304 end loop;
2305 end if;
2307 Next (Assoc);
2308 end loop;
2309 end if;
2311 -- No match found in aggregate, so chain up parent types to find
2312 -- a constraint that defines the value of the discriminant.
2314 Parent_Typ := Etype (Current_Typ);
2315 while Current_Typ /= Parent_Typ loop
2316 if Has_Discriminants (Parent_Typ)
2317 and then not Has_Unknown_Discriminants (Parent_Typ)
2318 then
2319 Parent_Disc := First_Discriminant (Parent_Typ);
2321 -- We either get the association from the subtype indication
2322 -- of the type definition itself, or from the discriminant
2323 -- constraint associated with the type entity (which is
2324 -- preferable, but it's not always present ???)
2326 if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ))
2327 then
2328 Assoc := Get_Constraint_Association (Current_Typ);
2329 Assoc_Elmt := No_Elmt;
2330 else
2331 Assoc_Elmt :=
2332 First_Elmt (Discriminant_Constraint (Current_Typ));
2333 Assoc := Node (Assoc_Elmt);
2334 end if;
2336 -- Traverse the discriminants of the parent type looking
2337 -- for one that corresponds.
2339 while Present (Parent_Disc) and then Present (Assoc) loop
2340 Corresp_Disc := Parent_Disc;
2341 while Present (Corresp_Disc)
2342 and then Disc /= Corresp_Disc
2343 loop
2344 Corresp_Disc := Corresponding_Discriminant (Corresp_Disc);
2345 end loop;
2347 if Disc = Corresp_Disc then
2348 if Nkind (Assoc) = N_Discriminant_Association then
2349 Assoc := Expression (Assoc);
2350 end if;
2352 -- If the located association directly denotes
2353 -- a discriminant, then use the value of a saved
2354 -- association of the aggregate. This is an approach
2355 -- used to handle certain cases involving multiple
2356 -- discriminants mapped to a single discriminant of
2357 -- a descendant. It's not clear how to locate the
2358 -- appropriate discriminant value for such cases. ???
2360 if Is_Entity_Name (Assoc)
2361 and then Ekind (Entity (Assoc)) = E_Discriminant
2362 then
2363 Assoc := Save_Assoc;
2364 end if;
2366 return Duplicate_Subexpr (Assoc);
2367 end if;
2369 Next_Discriminant (Parent_Disc);
2371 if No (Assoc_Elmt) then
2372 Next (Assoc);
2374 else
2375 Next_Elmt (Assoc_Elmt);
2377 if Present (Assoc_Elmt) then
2378 Assoc := Node (Assoc_Elmt);
2379 else
2380 Assoc := Empty;
2381 end if;
2382 end if;
2383 end loop;
2384 end if;
2386 Current_Typ := Parent_Typ;
2387 Parent_Typ := Etype (Current_Typ);
2388 end loop;
2390 -- In some cases there's no ancestor value to locate (such as
2391 -- when an ancestor part given by an expression defines the
2392 -- discriminant value).
2394 return Empty;
2395 end Ancestor_Discriminant_Value;
2397 ----------------------------------
2398 -- Check_Ancestor_Discriminants --
2399 ----------------------------------
2401 procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is
2402 Discr : Entity_Id;
2403 Disc_Value : Node_Id;
2404 Cond : Node_Id;
2406 begin
2407 Discr := First_Discriminant (Base_Type (Anc_Typ));
2408 while Present (Discr) loop
2409 Disc_Value := Ancestor_Discriminant_Value (Discr);
2411 if Present (Disc_Value) then
2412 Cond := Make_Op_Ne (Loc,
2413 Left_Opnd =>
2414 Make_Selected_Component (Loc,
2415 Prefix => New_Copy_Tree (Target),
2416 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2417 Right_Opnd => Disc_Value);
2419 Append_To (L,
2420 Make_Raise_Constraint_Error (Loc,
2421 Condition => Cond,
2422 Reason => CE_Discriminant_Check_Failed));
2423 end if;
2425 Next_Discriminant (Discr);
2426 end loop;
2427 end Check_Ancestor_Discriminants;
2429 ---------------------------
2430 -- Compatible_Int_Bounds --
2431 ---------------------------
2433 function Compatible_Int_Bounds
2434 (Agg_Bounds : Node_Id;
2435 Typ_Bounds : Node_Id) return Boolean
2437 Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds));
2438 Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds));
2439 Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds));
2440 Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds));
2441 begin
2442 return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi;
2443 end Compatible_Int_Bounds;
2445 -----------------------------------
2446 -- Generate_Finalization_Actions --
2447 -----------------------------------
2449 procedure Generate_Finalization_Actions is
2450 begin
2451 -- Do the work only the first time this is called
2453 if Finalization_Done then
2454 return;
2455 end if;
2457 Finalization_Done := True;
2459 -- Determine the external finalization list. It is either the
2460 -- finalization list of the outer scope or the one coming from an
2461 -- outer aggregate. When the target is not a temporary, the proper
2462 -- scope is the scope of the target rather than the potentially
2463 -- transient current scope.
2465 if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then
2466 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
2467 Set_Assignment_OK (Ref);
2469 Append_To (L,
2470 Make_Procedure_Call_Statement (Loc,
2471 Name =>
2472 New_Occurrence_Of
2473 (Find_Prim_Op (Init_Typ, Name_Initialize), Loc),
2474 Parameter_Associations => New_List (New_Copy_Tree (Ref))));
2475 end if;
2476 end Generate_Finalization_Actions;
2478 --------------------------------
2479 -- Get_Constraint_Association --
2480 --------------------------------
2482 function Get_Constraint_Association (T : Entity_Id) return Node_Id is
2483 Indic : Node_Id;
2484 Typ : Entity_Id;
2486 begin
2487 Typ := T;
2489 -- If type is private, get constraint from full view. This was
2490 -- previously done in an instance context, but is needed whenever
2491 -- the ancestor part has a discriminant, possibly inherited through
2492 -- multiple derivations.
2494 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
2495 Typ := Full_View (Typ);
2496 end if;
2498 Indic := Subtype_Indication (Type_Definition (Parent (Typ)));
2500 -- Verify that the subtype indication carries a constraint
2502 if Nkind (Indic) = N_Subtype_Indication
2503 and then Present (Constraint (Indic))
2504 then
2505 return First (Constraints (Constraint (Indic)));
2506 end if;
2508 return Empty;
2509 end Get_Constraint_Association;
2511 -------------------------------------
2512 -- Get_Explicit_Discriminant_Value --
2513 -------------------------------------
2515 function Get_Explicit_Discriminant_Value
2516 (D : Entity_Id) return Node_Id
2518 Assoc : Node_Id;
2519 Choice : Node_Id;
2520 Val : Node_Id;
2522 begin
2523 -- The aggregate has been normalized and all associations have a
2524 -- single choice.
2526 Assoc := First (Component_Associations (N));
2527 while Present (Assoc) loop
2528 Choice := First (Choices (Assoc));
2530 if Chars (Choice) = Chars (D) then
2531 Val := Expression (Assoc);
2532 Remove (Assoc);
2533 return Val;
2534 end if;
2536 Next (Assoc);
2537 end loop;
2539 return Empty;
2540 end Get_Explicit_Discriminant_Value;
2542 -------------------------------
2543 -- Init_Hidden_Discriminants --
2544 -------------------------------
2546 procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is
2547 function Is_Completely_Hidden_Discriminant
2548 (Discr : Entity_Id) return Boolean;
2549 -- Determine whether Discr is a completely hidden discriminant of
2550 -- type Typ.
2552 ---------------------------------------
2553 -- Is_Completely_Hidden_Discriminant --
2554 ---------------------------------------
2556 function Is_Completely_Hidden_Discriminant
2557 (Discr : Entity_Id) return Boolean
2559 Item : Entity_Id;
2561 begin
2562 -- Use First/Next_Entity as First/Next_Discriminant do not yield
2563 -- completely hidden discriminants.
2565 Item := First_Entity (Typ);
2566 while Present (Item) loop
2567 if Ekind (Item) = E_Discriminant
2568 and then Is_Completely_Hidden (Item)
2569 and then Chars (Original_Record_Component (Item)) =
2570 Chars (Discr)
2571 then
2572 return True;
2573 end if;
2575 Next_Entity (Item);
2576 end loop;
2578 return False;
2579 end Is_Completely_Hidden_Discriminant;
2581 -- Local variables
2583 Base_Typ : Entity_Id;
2584 Discr : Entity_Id;
2585 Discr_Constr : Elmt_Id;
2586 Discr_Init : Node_Id;
2587 Discr_Val : Node_Id;
2588 In_Aggr_Type : Boolean;
2589 Par_Typ : Entity_Id;
2591 -- Start of processing for Init_Hidden_Discriminants
2593 begin
2594 -- The constraints on the hidden discriminants, if present, are kept
2595 -- in the Stored_Constraint list of the type itself, or in that of
2596 -- the base type. If not in the constraints of the aggregate itself,
2597 -- we examine ancestors to find discriminants that are not renamed
2598 -- by other discriminants but constrained explicitly.
2600 In_Aggr_Type := True;
2602 Base_Typ := Base_Type (Typ);
2603 while Is_Derived_Type (Base_Typ)
2604 and then
2605 (Present (Stored_Constraint (Base_Typ))
2606 or else
2607 (In_Aggr_Type and then Present (Stored_Constraint (Typ))))
2608 loop
2609 Par_Typ := Etype (Base_Typ);
2611 if not Has_Discriminants (Par_Typ) then
2612 return;
2613 end if;
2615 Discr := First_Discriminant (Par_Typ);
2617 -- We know that one of the stored-constraint lists is present
2619 if Present (Stored_Constraint (Base_Typ)) then
2620 Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ));
2622 -- For private extension, stored constraint may be on full view
2624 elsif Is_Private_Type (Base_Typ)
2625 and then Present (Full_View (Base_Typ))
2626 and then Present (Stored_Constraint (Full_View (Base_Typ)))
2627 then
2628 Discr_Constr :=
2629 First_Elmt (Stored_Constraint (Full_View (Base_Typ)));
2631 else
2632 Discr_Constr := First_Elmt (Stored_Constraint (Typ));
2633 end if;
2635 while Present (Discr) and then Present (Discr_Constr) loop
2636 Discr_Val := Node (Discr_Constr);
2638 -- The parent discriminant is renamed in the derived type,
2639 -- nothing to initialize.
2641 -- type Deriv_Typ (Discr : ...)
2642 -- is new Parent_Typ (Discr => Discr);
2644 if Is_Entity_Name (Discr_Val)
2645 and then Ekind (Entity (Discr_Val)) = E_Discriminant
2646 then
2647 null;
2649 -- When the parent discriminant is constrained at the type
2650 -- extension level, it does not appear in the derived type.
2652 -- type Deriv_Typ (Discr : ...)
2653 -- is new Parent_Typ (Discr => Discr,
2654 -- Hidden_Discr => Expression);
2656 elsif Is_Completely_Hidden_Discriminant (Discr) then
2657 null;
2659 -- Otherwise initialize the discriminant
2661 else
2662 Discr_Init :=
2663 Make_OK_Assignment_Statement (Loc,
2664 Name =>
2665 Make_Selected_Component (Loc,
2666 Prefix => New_Copy_Tree (Target),
2667 Selector_Name => New_Occurrence_Of (Discr, Loc)),
2668 Expression => New_Copy_Tree (Discr_Val));
2670 Set_No_Ctrl_Actions (Discr_Init);
2671 Append_To (List, Discr_Init);
2672 end if;
2674 Next_Elmt (Discr_Constr);
2675 Next_Discriminant (Discr);
2676 end loop;
2678 In_Aggr_Type := False;
2679 Base_Typ := Base_Type (Par_Typ);
2680 end loop;
2681 end Init_Hidden_Discriminants;
2683 --------------------------------
2684 -- Init_Visible_Discriminants --
2685 --------------------------------
2687 procedure Init_Visible_Discriminants is
2688 Discriminant : Entity_Id;
2689 Discriminant_Value : Node_Id;
2691 begin
2692 Discriminant := First_Discriminant (Typ);
2693 while Present (Discriminant) loop
2694 Comp_Expr :=
2695 Make_Selected_Component (Loc,
2696 Prefix => New_Copy_Tree (Target),
2697 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2699 Discriminant_Value :=
2700 Get_Discriminant_Value
2701 (Discriminant, Typ, Discriminant_Constraint (N_Typ));
2703 Instr :=
2704 Make_OK_Assignment_Statement (Loc,
2705 Name => Comp_Expr,
2706 Expression => New_Copy_Tree (Discriminant_Value));
2708 Set_No_Ctrl_Actions (Instr);
2709 Append_To (L, Instr);
2711 Next_Discriminant (Discriminant);
2712 end loop;
2713 end Init_Visible_Discriminants;
2715 -------------------------------
2716 -- Init_Stored_Discriminants --
2717 -------------------------------
2719 procedure Init_Stored_Discriminants is
2720 Discriminant : Entity_Id;
2721 Discriminant_Value : Node_Id;
2723 begin
2724 Discriminant := First_Stored_Discriminant (Typ);
2725 while Present (Discriminant) loop
2726 Comp_Expr :=
2727 Make_Selected_Component (Loc,
2728 Prefix => New_Copy_Tree (Target),
2729 Selector_Name => New_Occurrence_Of (Discriminant, Loc));
2731 Discriminant_Value :=
2732 Get_Discriminant_Value
2733 (Discriminant, N_Typ, Discriminant_Constraint (N_Typ));
2735 Instr :=
2736 Make_OK_Assignment_Statement (Loc,
2737 Name => Comp_Expr,
2738 Expression => New_Copy_Tree (Discriminant_Value));
2740 Set_No_Ctrl_Actions (Instr);
2741 Append_To (L, Instr);
2743 Next_Stored_Discriminant (Discriminant);
2744 end loop;
2745 end Init_Stored_Discriminants;
2747 --------------------------------------
2748 -- Initialize_Ctrl_Record_Component --
2749 --------------------------------------
2751 procedure Initialize_Ctrl_Record_Component
2752 (Rec_Comp : Node_Id;
2753 Comp_Typ : Entity_Id;
2754 Init_Expr : Node_Id;
2755 Stmts : List_Id)
2757 Fin_Call : Node_Id;
2758 Hook_Clear : Node_Id;
2760 In_Place_Expansion : Boolean;
2761 -- Flag set when a nonlimited controlled function call requires
2762 -- in-place expansion.
2764 begin
2765 -- Perform a preliminary analysis and resolution to determine what
2766 -- the initialization expression denotes. Unanalyzed function calls
2767 -- may appear as identifiers or indexed components.
2769 if Nkind_In (Init_Expr, N_Function_Call,
2770 N_Identifier,
2771 N_Indexed_Component)
2772 and then not Analyzed (Init_Expr)
2773 then
2774 Preanalyze_And_Resolve (Init_Expr, Comp_Typ);
2775 end if;
2777 In_Place_Expansion :=
2778 Nkind (Init_Expr) = N_Function_Call
2779 and then not Is_Limited_Type (Comp_Typ);
2781 -- The initialization expression is a controlled function call.
2782 -- Perform in-place removal of side effects to avoid creating a
2783 -- transient scope.
2785 -- This in-place expansion is not performed for limited transient
2786 -- objects because the initialization is already done in place.
2788 if In_Place_Expansion then
2790 -- Suppress the removal of side effects by general analysis
2791 -- because this behavior is emulated here. This avoids the
2792 -- generation of a transient scope, which leads to out-of-order
2793 -- adjustment and finalization.
2795 Set_No_Side_Effect_Removal (Init_Expr);
2797 -- Install all hook-related declarations and prepare the clean up
2798 -- statements.
2800 Process_Transient_Component
2801 (Loc => Loc,
2802 Comp_Typ => Comp_Typ,
2803 Init_Expr => Init_Expr,
2804 Fin_Call => Fin_Call,
2805 Hook_Clear => Hook_Clear,
2806 Aggr => N);
2807 end if;
2809 -- Use the noncontrolled component initialization circuitry to
2810 -- assign the result of the function call to the record component.
2811 -- This also performs tag adjustment and [deep] adjustment of the
2812 -- record component.
2814 Initialize_Record_Component
2815 (Rec_Comp => Rec_Comp,
2816 Comp_Typ => Comp_Typ,
2817 Init_Expr => Init_Expr,
2818 Stmts => Stmts);
2820 -- At this point the record component is fully initialized. Complete
2821 -- the processing of the controlled record component by finalizing
2822 -- the transient function result.
2824 if In_Place_Expansion then
2825 Process_Transient_Component_Completion
2826 (Loc => Loc,
2827 Aggr => N,
2828 Fin_Call => Fin_Call,
2829 Hook_Clear => Hook_Clear,
2830 Stmts => Stmts);
2831 end if;
2832 end Initialize_Ctrl_Record_Component;
2834 ---------------------------------
2835 -- Initialize_Record_Component --
2836 ---------------------------------
2838 procedure Initialize_Record_Component
2839 (Rec_Comp : Node_Id;
2840 Comp_Typ : Entity_Id;
2841 Init_Expr : Node_Id;
2842 Stmts : List_Id)
2844 Exceptions_OK : constant Boolean :=
2845 not Restriction_Active (No_Exception_Propagation);
2847 Finalization_OK : constant Boolean := Needs_Finalization (Comp_Typ);
2849 Full_Typ : constant Entity_Id := Underlying_Type (Comp_Typ);
2850 Blk_Stmts : List_Id;
2851 Init_Stmt : Node_Id;
2853 begin
2854 -- Protect the initialization statements from aborts. Generate:
2856 -- Abort_Defer;
2858 if Finalization_OK and Abort_Allowed then
2859 if Exceptions_OK then
2860 Blk_Stmts := New_List;
2861 else
2862 Blk_Stmts := Stmts;
2863 end if;
2865 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
2867 -- Otherwise aborts are not allowed. All generated code is added
2868 -- directly to the input list.
2870 else
2871 Blk_Stmts := Stmts;
2872 end if;
2874 -- Initialize the record component. Generate:
2876 -- Rec_Comp := Init_Expr;
2878 -- Note that the initialization expression is NOT replicated because
2879 -- only a single component may be initialized by it.
2881 Init_Stmt :=
2882 Make_OK_Assignment_Statement (Loc,
2883 Name => New_Copy_Tree (Rec_Comp),
2884 Expression => Init_Expr);
2885 Set_No_Ctrl_Actions (Init_Stmt);
2887 Append_To (Blk_Stmts, Init_Stmt);
2889 -- Adjust the tag due to a possible view conversion. Generate:
2891 -- Rec_Comp._tag := Full_TypeP;
2893 if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then
2894 Append_To (Blk_Stmts,
2895 Make_OK_Assignment_Statement (Loc,
2896 Name =>
2897 Make_Selected_Component (Loc,
2898 Prefix => New_Copy_Tree (Rec_Comp),
2899 Selector_Name =>
2900 New_Occurrence_Of
2901 (First_Tag_Component (Full_Typ), Loc)),
2903 Expression =>
2904 Unchecked_Convert_To (RTE (RE_Tag),
2905 New_Occurrence_Of
2906 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
2907 Loc))));
2908 end if;
2910 -- Adjust the component. Generate:
2912 -- [Deep_]Adjust (Rec_Comp);
2914 if Finalization_OK and then not Is_Limited_Type (Comp_Typ) then
2915 Append_To (Blk_Stmts,
2916 Make_Adjust_Call
2917 (Obj_Ref => New_Copy_Tree (Rec_Comp),
2918 Typ => Comp_Typ));
2919 end if;
2921 -- Complete the protection of the initialization statements
2923 if Finalization_OK and Abort_Allowed then
2925 -- Wrap the initialization statements in a block to catch a
2926 -- potential exception. Generate:
2928 -- begin
2929 -- Abort_Defer;
2930 -- Rec_Comp := Init_Expr;
2931 -- Rec_Comp._tag := Full_TypP;
2932 -- [Deep_]Adjust (Rec_Comp);
2933 -- at end
2934 -- Abort_Undefer_Direct;
2935 -- end;
2937 if Exceptions_OK then
2938 Append_To (Stmts,
2939 Build_Abort_Undefer_Block (Loc,
2940 Stmts => Blk_Stmts,
2941 Context => N));
2943 -- Otherwise exceptions are not propagated. Generate:
2945 -- Abort_Defer;
2946 -- Rec_Comp := Init_Expr;
2947 -- Rec_Comp._tag := Full_TypP;
2948 -- [Deep_]Adjust (Rec_Comp);
2949 -- Abort_Undefer;
2951 else
2952 Append_To (Blk_Stmts,
2953 Build_Runtime_Call (Loc, RE_Abort_Undefer));
2954 end if;
2955 end if;
2956 end Initialize_Record_Component;
2958 -------------------------
2959 -- Is_Int_Range_Bounds --
2960 -------------------------
2962 function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is
2963 begin
2964 return Nkind (Bounds) = N_Range
2965 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
2966 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal;
2967 end Is_Int_Range_Bounds;
2969 ------------------
2970 -- Replace_Type --
2971 ------------------
2973 function Replace_Type (Expr : Node_Id) return Traverse_Result is
2974 begin
2975 -- Note regarding the Root_Type test below: Aggregate components for
2976 -- self-referential types include attribute references to the current
2977 -- instance, of the form: Typ'access, etc.. These references are
2978 -- rewritten as references to the target of the aggregate: the
2979 -- left-hand side of an assignment, the entity in a declaration,
2980 -- or a temporary. Without this test, we would improperly extended
2981 -- this rewriting to attribute references whose prefix was not the
2982 -- type of the aggregate.
2984 if Nkind (Expr) = N_Attribute_Reference
2985 and then Is_Entity_Name (Prefix (Expr))
2986 and then Is_Type (Entity (Prefix (Expr)))
2987 and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr)))
2988 then
2989 if Is_Entity_Name (Lhs) then
2990 Rewrite (Prefix (Expr),
2991 New_Occurrence_Of (Entity (Lhs), Loc));
2993 elsif Nkind (Lhs) = N_Selected_Component then
2994 Rewrite (Expr,
2995 Make_Attribute_Reference (Loc,
2996 Attribute_Name => Name_Unrestricted_Access,
2997 Prefix => New_Copy_Tree (Lhs)));
2998 Set_Analyzed (Parent (Expr), False);
3000 else
3001 Rewrite (Expr,
3002 Make_Attribute_Reference (Loc,
3003 Attribute_Name => Name_Unrestricted_Access,
3004 Prefix => New_Copy_Tree (Lhs)));
3005 Set_Analyzed (Parent (Expr), False);
3006 end if;
3007 end if;
3009 return OK;
3010 end Replace_Type;
3012 --------------------------
3013 -- Rewrite_Discriminant --
3014 --------------------------
3016 function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is
3017 begin
3018 if Is_Entity_Name (Expr)
3019 and then Present (Entity (Expr))
3020 and then Ekind (Entity (Expr)) = E_In_Parameter
3021 and then Present (Discriminal_Link (Entity (Expr)))
3022 and then Scope (Discriminal_Link (Entity (Expr))) =
3023 Base_Type (Etype (N))
3024 then
3025 Rewrite (Expr,
3026 Make_Selected_Component (Loc,
3027 Prefix => New_Copy_Tree (Lhs),
3028 Selector_Name => Make_Identifier (Loc, Chars (Expr))));
3029 end if;
3031 return OK;
3032 end Rewrite_Discriminant;
3034 procedure Replace_Discriminants is
3035 new Traverse_Proc (Rewrite_Discriminant);
3037 procedure Replace_Self_Reference is
3038 new Traverse_Proc (Replace_Type);
3040 -- Start of processing for Build_Record_Aggr_Code
3042 begin
3043 if Has_Self_Reference (N) then
3044 Replace_Self_Reference (N);
3045 end if;
3047 -- If the target of the aggregate is class-wide, we must convert it
3048 -- to the actual type of the aggregate, so that the proper components
3049 -- are visible. We know already that the types are compatible.
3051 if Present (Etype (Lhs))
3052 and then Is_Class_Wide_Type (Etype (Lhs))
3053 then
3054 Target := Unchecked_Convert_To (Typ, Lhs);
3055 else
3056 Target := Lhs;
3057 end if;
3059 -- Deal with the ancestor part of extension aggregates or with the
3060 -- discriminants of the root type.
3062 if Nkind (N) = N_Extension_Aggregate then
3063 declare
3064 Ancestor : constant Node_Id := Ancestor_Part (N);
3065 Assign : List_Id;
3067 begin
3068 -- If the ancestor part is a subtype mark "T", we generate
3070 -- init-proc (T (tmp)); if T is constrained and
3071 -- init-proc (S (tmp)); where S applies an appropriate
3072 -- constraint if T is unconstrained
3074 if Is_Entity_Name (Ancestor)
3075 and then Is_Type (Entity (Ancestor))
3076 then
3077 Ancestor_Is_Subtype_Mark := True;
3079 if Is_Constrained (Entity (Ancestor)) then
3080 Init_Typ := Entity (Ancestor);
3082 -- For an ancestor part given by an unconstrained type mark,
3083 -- create a subtype constrained by appropriate corresponding
3084 -- discriminant values coming from either associations of the
3085 -- aggregate or a constraint on a parent type. The subtype will
3086 -- be used to generate the correct default value for the
3087 -- ancestor part.
3089 elsif Has_Discriminants (Entity (Ancestor)) then
3090 declare
3091 Anc_Typ : constant Entity_Id := Entity (Ancestor);
3092 Anc_Constr : constant List_Id := New_List;
3093 Discrim : Entity_Id;
3094 Disc_Value : Node_Id;
3095 New_Indic : Node_Id;
3096 Subt_Decl : Node_Id;
3098 begin
3099 Discrim := First_Discriminant (Anc_Typ);
3100 while Present (Discrim) loop
3101 Disc_Value := Ancestor_Discriminant_Value (Discrim);
3103 -- If no usable discriminant in ancestors, check
3104 -- whether aggregate has an explicit value for it.
3106 if No (Disc_Value) then
3107 Disc_Value :=
3108 Get_Explicit_Discriminant_Value (Discrim);
3109 end if;
3111 Append_To (Anc_Constr, Disc_Value);
3112 Next_Discriminant (Discrim);
3113 end loop;
3115 New_Indic :=
3116 Make_Subtype_Indication (Loc,
3117 Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc),
3118 Constraint =>
3119 Make_Index_Or_Discriminant_Constraint (Loc,
3120 Constraints => Anc_Constr));
3122 Init_Typ := Create_Itype (Ekind (Anc_Typ), N);
3124 Subt_Decl :=
3125 Make_Subtype_Declaration (Loc,
3126 Defining_Identifier => Init_Typ,
3127 Subtype_Indication => New_Indic);
3129 -- Itypes must be analyzed with checks off Declaration
3130 -- must have a parent for proper handling of subsidiary
3131 -- actions.
3133 Set_Parent (Subt_Decl, N);
3134 Analyze (Subt_Decl, Suppress => All_Checks);
3135 end;
3136 end if;
3138 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3139 Set_Assignment_OK (Ref);
3141 if not Is_Interface (Init_Typ) then
3142 Append_List_To (L,
3143 Build_Initialization_Call (Loc,
3144 Id_Ref => Ref,
3145 Typ => Init_Typ,
3146 In_Init_Proc => Within_Init_Proc,
3147 With_Default_Init => Has_Default_Init_Comps (N)
3148 or else
3149 Has_Task (Base_Type (Init_Typ))));
3151 if Is_Constrained (Entity (Ancestor))
3152 and then Has_Discriminants (Entity (Ancestor))
3153 then
3154 Check_Ancestor_Discriminants (Entity (Ancestor));
3155 end if;
3156 end if;
3158 -- Handle calls to C++ constructors
3160 elsif Is_CPP_Constructor_Call (Ancestor) then
3161 Init_Typ := Etype (Ancestor);
3162 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3163 Set_Assignment_OK (Ref);
3165 Append_List_To (L,
3166 Build_Initialization_Call (Loc,
3167 Id_Ref => Ref,
3168 Typ => Init_Typ,
3169 In_Init_Proc => Within_Init_Proc,
3170 With_Default_Init => Has_Default_Init_Comps (N),
3171 Constructor_Ref => Ancestor));
3173 -- Ada 2005 (AI-287): If the ancestor part is an aggregate of
3174 -- limited type, a recursive call expands the ancestor. Note that
3175 -- in the limited case, the ancestor part must be either a
3176 -- function call (possibly qualified, or wrapped in an unchecked
3177 -- conversion) or aggregate (definitely qualified).
3179 -- The ancestor part can also be a function call (that may be
3180 -- transformed into an explicit dereference) or a qualification
3181 -- of one such.
3183 elsif Is_Limited_Type (Etype (Ancestor))
3184 and then Nkind_In (Unqualify (Ancestor), N_Aggregate,
3185 N_Extension_Aggregate)
3186 then
3187 Ancestor_Is_Expression := True;
3189 -- Set up finalization data for enclosing record, because
3190 -- controlled subcomponents of the ancestor part will be
3191 -- attached to it.
3193 Generate_Finalization_Actions;
3195 Append_List_To (L,
3196 Build_Record_Aggr_Code
3197 (N => Unqualify (Ancestor),
3198 Typ => Etype (Unqualify (Ancestor)),
3199 Lhs => Target));
3201 -- If the ancestor part is an expression "E", we generate
3203 -- T (tmp) := E;
3205 -- In Ada 2005, this includes the case of a (possibly qualified)
3206 -- limited function call. The assignment will turn into a
3207 -- build-in-place function call (for further details, see
3208 -- Make_Build_In_Place_Call_In_Assignment).
3210 else
3211 Ancestor_Is_Expression := True;
3212 Init_Typ := Etype (Ancestor);
3214 -- If the ancestor part is an aggregate, force its full
3215 -- expansion, which was delayed.
3217 if Nkind_In (Unqualify (Ancestor), N_Aggregate,
3218 N_Extension_Aggregate)
3219 then
3220 Set_Analyzed (Ancestor, False);
3221 Set_Analyzed (Expression (Ancestor), False);
3222 end if;
3224 Ref := Convert_To (Init_Typ, New_Copy_Tree (Target));
3225 Set_Assignment_OK (Ref);
3227 -- Make the assignment without usual controlled actions, since
3228 -- we only want to Adjust afterwards, but not to Finalize
3229 -- beforehand. Add manual Adjust when necessary.
3231 Assign := New_List (
3232 Make_OK_Assignment_Statement (Loc,
3233 Name => Ref,
3234 Expression => Ancestor));
3235 Set_No_Ctrl_Actions (First (Assign));
3237 -- Assign the tag now to make sure that the dispatching call in
3238 -- the subsequent deep_adjust works properly (unless
3239 -- Tagged_Type_Expansion where tags are implicit).
3241 if Tagged_Type_Expansion then
3242 Instr :=
3243 Make_OK_Assignment_Statement (Loc,
3244 Name =>
3245 Make_Selected_Component (Loc,
3246 Prefix => New_Copy_Tree (Target),
3247 Selector_Name =>
3248 New_Occurrence_Of
3249 (First_Tag_Component (Base_Type (Typ)), Loc)),
3251 Expression =>
3252 Unchecked_Convert_To (RTE (RE_Tag),
3253 New_Occurrence_Of
3254 (Node (First_Elmt
3255 (Access_Disp_Table (Base_Type (Typ)))),
3256 Loc)));
3258 Set_Assignment_OK (Name (Instr));
3259 Append_To (Assign, Instr);
3261 -- Ada 2005 (AI-251): If tagged type has progenitors we must
3262 -- also initialize tags of the secondary dispatch tables.
3264 if Has_Interfaces (Base_Type (Typ)) then
3265 Init_Secondary_Tags
3266 (Typ => Base_Type (Typ),
3267 Target => Target,
3268 Stmts_List => Assign);
3269 end if;
3270 end if;
3272 -- Call Adjust manually
3274 if Needs_Finalization (Etype (Ancestor))
3275 and then not Is_Limited_Type (Etype (Ancestor))
3276 then
3277 Append_To (Assign,
3278 Make_Adjust_Call
3279 (Obj_Ref => New_Copy_Tree (Ref),
3280 Typ => Etype (Ancestor)));
3281 end if;
3283 Append_To (L,
3284 Make_Unsuppress_Block (Loc, Name_Discriminant_Check, Assign));
3286 if Has_Discriminants (Init_Typ) then
3287 Check_Ancestor_Discriminants (Init_Typ);
3288 end if;
3289 end if;
3290 end;
3292 -- Generate assignments of hidden discriminants. If the base type is
3293 -- an unchecked union, the discriminants are unknown to the back-end
3294 -- and absent from a value of the type, so assignments for them are
3295 -- not emitted.
3297 if Has_Discriminants (Typ)
3298 and then not Is_Unchecked_Union (Base_Type (Typ))
3299 then
3300 Init_Hidden_Discriminants (Typ, L);
3301 end if;
3303 -- Normal case (not an extension aggregate)
3305 else
3306 -- Generate the discriminant expressions, component by component.
3307 -- If the base type is an unchecked union, the discriminants are
3308 -- unknown to the back-end and absent from a value of the type, so
3309 -- assignments for them are not emitted.
3311 if Has_Discriminants (Typ)
3312 and then not Is_Unchecked_Union (Base_Type (Typ))
3313 then
3314 Init_Hidden_Discriminants (Typ, L);
3316 -- Generate discriminant init values for the visible discriminants
3318 Init_Visible_Discriminants;
3320 if Is_Derived_Type (N_Typ) then
3321 Init_Stored_Discriminants;
3322 end if;
3323 end if;
3324 end if;
3326 -- For CPP types we generate an implicit call to the C++ default
3327 -- constructor to ensure the proper initialization of the _Tag
3328 -- component.
3330 if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then
3331 Invoke_Constructor : declare
3332 CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ);
3334 procedure Invoke_IC_Proc (T : Entity_Id);
3335 -- Recursive routine used to climb to parents. Required because
3336 -- parents must be initialized before descendants to ensure
3337 -- propagation of inherited C++ slots.
3339 --------------------
3340 -- Invoke_IC_Proc --
3341 --------------------
3343 procedure Invoke_IC_Proc (T : Entity_Id) is
3344 begin
3345 -- Avoid generating extra calls. Initialization required
3346 -- only for types defined from the level of derivation of
3347 -- type of the constructor and the type of the aggregate.
3349 if T = CPP_Parent then
3350 return;
3351 end if;
3353 Invoke_IC_Proc (Etype (T));
3355 -- Generate call to the IC routine
3357 if Present (CPP_Init_Proc (T)) then
3358 Append_To (L,
3359 Make_Procedure_Call_Statement (Loc,
3360 Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc)));
3361 end if;
3362 end Invoke_IC_Proc;
3364 -- Start of processing for Invoke_Constructor
3366 begin
3367 -- Implicit invocation of the C++ constructor
3369 if Nkind (N) = N_Aggregate then
3370 Append_To (L,
3371 Make_Procedure_Call_Statement (Loc,
3372 Name =>
3373 New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc),
3374 Parameter_Associations => New_List (
3375 Unchecked_Convert_To (CPP_Parent,
3376 New_Copy_Tree (Lhs)))));
3377 end if;
3379 Invoke_IC_Proc (Typ);
3380 end Invoke_Constructor;
3381 end if;
3383 -- Generate the assignments, component by component
3385 -- tmp.comp1 := Expr1_From_Aggr;
3386 -- tmp.comp2 := Expr2_From_Aggr;
3387 -- ....
3389 Comp := First (Component_Associations (N));
3390 while Present (Comp) loop
3391 Selector := Entity (First (Choices (Comp)));
3393 -- C++ constructors
3395 if Is_CPP_Constructor_Call (Expression (Comp)) then
3396 Append_List_To (L,
3397 Build_Initialization_Call (Loc,
3398 Id_Ref =>
3399 Make_Selected_Component (Loc,
3400 Prefix => New_Copy_Tree (Target),
3401 Selector_Name => New_Occurrence_Of (Selector, Loc)),
3402 Typ => Etype (Selector),
3403 Enclos_Type => Typ,
3404 With_Default_Init => True,
3405 Constructor_Ref => Expression (Comp)));
3407 -- Ada 2005 (AI-287): For each default-initialized component generate
3408 -- a call to the corresponding IP subprogram if available.
3410 elsif Box_Present (Comp)
3411 and then Has_Non_Null_Base_Init_Proc (Etype (Selector))
3412 then
3413 if Ekind (Selector) /= E_Discriminant then
3414 Generate_Finalization_Actions;
3415 end if;
3417 -- Ada 2005 (AI-287): If the component type has tasks then
3418 -- generate the activation chain and master entities (except
3419 -- in case of an allocator because in that case these entities
3420 -- are generated by Build_Task_Allocate_Block_With_Init_Stmts).
3422 declare
3423 Ctype : constant Entity_Id := Etype (Selector);
3424 Inside_Allocator : Boolean := False;
3425 P : Node_Id := Parent (N);
3427 begin
3428 if Is_Task_Type (Ctype) or else Has_Task (Ctype) then
3429 while Present (P) loop
3430 if Nkind (P) = N_Allocator then
3431 Inside_Allocator := True;
3432 exit;
3433 end if;
3435 P := Parent (P);
3436 end loop;
3438 if not Inside_Init_Proc and not Inside_Allocator then
3439 Build_Activation_Chain_Entity (N);
3440 end if;
3441 end if;
3442 end;
3444 Append_List_To (L,
3445 Build_Initialization_Call (Loc,
3446 Id_Ref => Make_Selected_Component (Loc,
3447 Prefix => New_Copy_Tree (Target),
3448 Selector_Name =>
3449 New_Occurrence_Of (Selector, Loc)),
3450 Typ => Etype (Selector),
3451 Enclos_Type => Typ,
3452 With_Default_Init => True));
3454 -- Prepare for component assignment
3456 elsif Ekind (Selector) /= E_Discriminant
3457 or else Nkind (N) = N_Extension_Aggregate
3458 then
3459 -- All the discriminants have now been assigned
3461 -- This is now a good moment to initialize and attach all the
3462 -- controllers. Their position may depend on the discriminants.
3464 if Ekind (Selector) /= E_Discriminant then
3465 Generate_Finalization_Actions;
3466 end if;
3468 Comp_Type := Underlying_Type (Etype (Selector));
3469 Comp_Expr :=
3470 Make_Selected_Component (Loc,
3471 Prefix => New_Copy_Tree (Target),
3472 Selector_Name => New_Occurrence_Of (Selector, Loc));
3474 if Nkind (Expression (Comp)) = N_Qualified_Expression then
3475 Expr_Q := Expression (Expression (Comp));
3476 else
3477 Expr_Q := Expression (Comp);
3478 end if;
3480 -- Now either create the assignment or generate the code for the
3481 -- inner aggregate top-down.
3483 if Is_Delayed_Aggregate (Expr_Q) then
3485 -- We have the following case of aggregate nesting inside
3486 -- an object declaration:
3488 -- type Arr_Typ is array (Integer range <>) of ...;
3490 -- type Rec_Typ (...) is record
3491 -- Obj_Arr_Typ : Arr_Typ (A .. B);
3492 -- end record;
3494 -- Obj_Rec_Typ : Rec_Typ := (...,
3495 -- Obj_Arr_Typ => (X => (...), Y => (...)));
3497 -- The length of the ranges of the aggregate and Obj_Add_Typ
3498 -- are equal (B - A = Y - X), but they do not coincide (X /=
3499 -- A and B /= Y). This case requires array sliding which is
3500 -- performed in the following manner:
3502 -- subtype Arr_Sub is Arr_Typ (X .. Y);
3503 -- Temp : Arr_Sub;
3504 -- Temp (X) := (...);
3505 -- ...
3506 -- Temp (Y) := (...);
3507 -- Obj_Rec_Typ.Obj_Arr_Typ := Temp;
3509 if Ekind (Comp_Type) = E_Array_Subtype
3510 and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q))
3511 and then Is_Int_Range_Bounds (First_Index (Comp_Type))
3512 and then not
3513 Compatible_Int_Bounds
3514 (Agg_Bounds => Aggregate_Bounds (Expr_Q),
3515 Typ_Bounds => First_Index (Comp_Type))
3516 then
3517 -- Create the array subtype with bounds equal to those of
3518 -- the corresponding aggregate.
3520 declare
3521 SubE : constant Entity_Id := Make_Temporary (Loc, 'T');
3523 SubD : constant Node_Id :=
3524 Make_Subtype_Declaration (Loc,
3525 Defining_Identifier => SubE,
3526 Subtype_Indication =>
3527 Make_Subtype_Indication (Loc,
3528 Subtype_Mark =>
3529 New_Occurrence_Of (Etype (Comp_Type), Loc),
3530 Constraint =>
3531 Make_Index_Or_Discriminant_Constraint
3532 (Loc,
3533 Constraints => New_List (
3534 New_Copy_Tree
3535 (Aggregate_Bounds (Expr_Q))))));
3537 -- Create a temporary array of the above subtype which
3538 -- will be used to capture the aggregate assignments.
3540 TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N);
3542 TmpD : constant Node_Id :=
3543 Make_Object_Declaration (Loc,
3544 Defining_Identifier => TmpE,
3545 Object_Definition => New_Occurrence_Of (SubE, Loc));
3547 begin
3548 Set_No_Initialization (TmpD);
3549 Append_To (L, SubD);
3550 Append_To (L, TmpD);
3552 -- Expand aggregate into assignments to the temp array
3554 Append_List_To (L,
3555 Late_Expansion (Expr_Q, Comp_Type,
3556 New_Occurrence_Of (TmpE, Loc)));
3558 -- Slide
3560 Append_To (L,
3561 Make_Assignment_Statement (Loc,
3562 Name => New_Copy_Tree (Comp_Expr),
3563 Expression => New_Occurrence_Of (TmpE, Loc)));
3564 end;
3566 -- Normal case (sliding not required)
3568 else
3569 Append_List_To (L,
3570 Late_Expansion (Expr_Q, Comp_Type, Comp_Expr));
3571 end if;
3573 -- Expr_Q is not delayed aggregate
3575 else
3576 if Has_Discriminants (Typ) then
3577 Replace_Discriminants (Expr_Q);
3579 -- If the component is an array type that depends on
3580 -- discriminants, and the expression is a single Others
3581 -- clause, create an explicit subtype for it because the
3582 -- backend has troubles recovering the actual bounds.
3584 if Nkind (Expr_Q) = N_Aggregate
3585 and then Is_Array_Type (Comp_Type)
3586 and then Present (Component_Associations (Expr_Q))
3587 then
3588 declare
3589 Assoc : constant Node_Id :=
3590 First (Component_Associations (Expr_Q));
3591 Decl : Node_Id;
3593 begin
3594 if Nkind (First (Choices (Assoc))) = N_Others_Choice
3595 then
3596 Decl :=
3597 Build_Actual_Subtype_Of_Component
3598 (Comp_Type, Comp_Expr);
3600 -- If the component type does not in fact depend on
3601 -- discriminants, the subtype declaration is empty.
3603 if Present (Decl) then
3604 Append_To (L, Decl);
3605 Set_Etype (Comp_Expr, Defining_Entity (Decl));
3606 end if;
3607 end if;
3608 end;
3609 end if;
3610 end if;
3612 if Generate_C_Code
3613 and then Nkind (Expr_Q) = N_Aggregate
3614 and then Is_Array_Type (Etype (Expr_Q))
3615 and then Present (First_Index (Etype (Expr_Q)))
3616 then
3617 declare
3618 Expr_Q_Type : constant Node_Id := Etype (Expr_Q);
3619 begin
3620 Append_List_To (L,
3621 Build_Array_Aggr_Code
3622 (N => Expr_Q,
3623 Ctype => Component_Type (Expr_Q_Type),
3624 Index => First_Index (Expr_Q_Type),
3625 Into => Comp_Expr,
3626 Scalar_Comp =>
3627 Is_Scalar_Type (Component_Type (Expr_Q_Type))));
3628 end;
3630 else
3631 -- Handle an initialization expression of a controlled type
3632 -- in case it denotes a function call. In general such a
3633 -- scenario will produce a transient scope, but this will
3634 -- lead to wrong order of initialization, adjustment, and
3635 -- finalization in the context of aggregates.
3637 -- Target.Comp := Ctrl_Func_Call;
3639 -- begin -- scope
3640 -- Trans_Obj : ... := Ctrl_Func_Call; -- object
3641 -- Target.Comp := Trans_Obj;
3642 -- Finalize (Trans_Obj);
3643 -- end
3644 -- Target.Comp._tag := ...;
3645 -- Adjust (Target.Comp);
3647 -- In the example above, the call to Finalize occurs too
3648 -- early and as a result it may leave the record component
3649 -- in a bad state. Finalization of the transient object
3650 -- should really happen after adjustment.
3652 -- To avoid this scenario, perform in-place side-effect
3653 -- removal of the function call. This eliminates the
3654 -- transient property of the function result and ensures
3655 -- correct order of actions.
3657 -- Res : ... := Ctrl_Func_Call;
3658 -- Target.Comp := Res;
3659 -- Target.Comp._tag := ...;
3660 -- Adjust (Target.Comp);
3661 -- Finalize (Res);
3663 if Needs_Finalization (Comp_Type)
3664 and then Nkind (Expr_Q) /= N_Aggregate
3665 then
3666 Initialize_Ctrl_Record_Component
3667 (Rec_Comp => Comp_Expr,
3668 Comp_Typ => Etype (Selector),
3669 Init_Expr => Expr_Q,
3670 Stmts => L);
3672 -- Otherwise perform single component initialization
3674 else
3675 Initialize_Record_Component
3676 (Rec_Comp => Comp_Expr,
3677 Comp_Typ => Etype (Selector),
3678 Init_Expr => Expr_Q,
3679 Stmts => L);
3680 end if;
3681 end if;
3682 end if;
3684 -- comment would be good here ???
3686 elsif Ekind (Selector) = E_Discriminant
3687 and then Nkind (N) /= N_Extension_Aggregate
3688 and then Nkind (Parent (N)) = N_Component_Association
3689 and then Is_Constrained (Typ)
3690 then
3691 -- We must check that the discriminant value imposed by the
3692 -- context is the same as the value given in the subaggregate,
3693 -- because after the expansion into assignments there is no
3694 -- record on which to perform a regular discriminant check.
3696 declare
3697 D_Val : Elmt_Id;
3698 Disc : Entity_Id;
3700 begin
3701 D_Val := First_Elmt (Discriminant_Constraint (Typ));
3702 Disc := First_Discriminant (Typ);
3703 while Chars (Disc) /= Chars (Selector) loop
3704 Next_Discriminant (Disc);
3705 Next_Elmt (D_Val);
3706 end loop;
3708 pragma Assert (Present (D_Val));
3710 -- This check cannot performed for components that are
3711 -- constrained by a current instance, because this is not a
3712 -- value that can be compared with the actual constraint.
3714 if Nkind (Node (D_Val)) /= N_Attribute_Reference
3715 or else not Is_Entity_Name (Prefix (Node (D_Val)))
3716 or else not Is_Type (Entity (Prefix (Node (D_Val))))
3717 then
3718 Append_To (L,
3719 Make_Raise_Constraint_Error (Loc,
3720 Condition =>
3721 Make_Op_Ne (Loc,
3722 Left_Opnd => New_Copy_Tree (Node (D_Val)),
3723 Right_Opnd => Expression (Comp)),
3724 Reason => CE_Discriminant_Check_Failed));
3726 else
3727 -- Find self-reference in previous discriminant assignment,
3728 -- and replace with proper expression.
3730 declare
3731 Ass : Node_Id;
3733 begin
3734 Ass := First (L);
3735 while Present (Ass) loop
3736 if Nkind (Ass) = N_Assignment_Statement
3737 and then Nkind (Name (Ass)) = N_Selected_Component
3738 and then Chars (Selector_Name (Name (Ass))) =
3739 Chars (Disc)
3740 then
3741 Set_Expression
3742 (Ass, New_Copy_Tree (Expression (Comp)));
3743 exit;
3744 end if;
3745 Next (Ass);
3746 end loop;
3747 end;
3748 end if;
3749 end;
3750 end if;
3752 Next (Comp);
3753 end loop;
3755 -- If the type is tagged, the tag needs to be initialized (unless we
3756 -- are in VM-mode where tags are implicit). It is done late in the
3757 -- initialization process because in some cases, we call the init
3758 -- proc of an ancestor which will not leave out the right tag.
3760 if Ancestor_Is_Expression then
3761 null;
3763 -- For CPP types we generated a call to the C++ default constructor
3764 -- before the components have been initialized to ensure the proper
3765 -- initialization of the _Tag component (see above).
3767 elsif Is_CPP_Class (Typ) then
3768 null;
3770 elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
3771 Instr :=
3772 Make_OK_Assignment_Statement (Loc,
3773 Name =>
3774 Make_Selected_Component (Loc,
3775 Prefix => New_Copy_Tree (Target),
3776 Selector_Name =>
3777 New_Occurrence_Of
3778 (First_Tag_Component (Base_Type (Typ)), Loc)),
3780 Expression =>
3781 Unchecked_Convert_To (RTE (RE_Tag),
3782 New_Occurrence_Of
3783 (Node (First_Elmt (Access_Disp_Table (Base_Type (Typ)))),
3784 Loc)));
3786 Append_To (L, Instr);
3788 -- Ada 2005 (AI-251): If the tagged type has been derived from an
3789 -- abstract interfaces we must also initialize the tags of the
3790 -- secondary dispatch tables.
3792 if Has_Interfaces (Base_Type (Typ)) then
3793 Init_Secondary_Tags
3794 (Typ => Base_Type (Typ),
3795 Target => Target,
3796 Stmts_List => L);
3797 end if;
3798 end if;
3800 -- If the controllers have not been initialized yet (by lack of non-
3801 -- discriminant components), let's do it now.
3803 Generate_Finalization_Actions;
3805 return L;
3806 end Build_Record_Aggr_Code;
3808 ---------------------------------------
3809 -- Collect_Initialization_Statements --
3810 ---------------------------------------
3812 procedure Collect_Initialization_Statements
3813 (Obj : Entity_Id;
3814 N : Node_Id;
3815 Node_After : Node_Id)
3817 Loc : constant Source_Ptr := Sloc (N);
3818 Init_Actions : constant List_Id := New_List;
3819 Init_Node : Node_Id;
3820 Comp_Stmt : Node_Id;
3822 begin
3823 -- Nothing to do if Obj is already frozen, as in this case we known we
3824 -- won't need to move the initialization statements about later on.
3826 if Is_Frozen (Obj) then
3827 return;
3828 end if;
3830 Init_Node := N;
3831 while Next (Init_Node) /= Node_After loop
3832 Append_To (Init_Actions, Remove_Next (Init_Node));
3833 end loop;
3835 if not Is_Empty_List (Init_Actions) then
3836 Comp_Stmt := Make_Compound_Statement (Loc, Actions => Init_Actions);
3837 Insert_Action_After (Init_Node, Comp_Stmt);
3838 Set_Initialization_Statements (Obj, Comp_Stmt);
3839 end if;
3840 end Collect_Initialization_Statements;
3842 -------------------------------
3843 -- Convert_Aggr_In_Allocator --
3844 -------------------------------
3846 procedure Convert_Aggr_In_Allocator
3847 (Alloc : Node_Id;
3848 Decl : Node_Id;
3849 Aggr : Node_Id)
3851 Loc : constant Source_Ptr := Sloc (Aggr);
3852 Typ : constant Entity_Id := Etype (Aggr);
3853 Temp : constant Entity_Id := Defining_Identifier (Decl);
3855 Occ : constant Node_Id :=
3856 Unchecked_Convert_To (Typ,
3857 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc)));
3859 begin
3860 if Is_Array_Type (Typ) then
3861 Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ);
3863 elsif Has_Default_Init_Comps (Aggr) then
3864 declare
3865 L : constant List_Id := New_List;
3866 Init_Stmts : List_Id;
3868 begin
3869 Init_Stmts := Late_Expansion (Aggr, Typ, Occ);
3871 if Has_Task (Typ) then
3872 Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts);
3873 Insert_Actions (Alloc, L);
3874 else
3875 Insert_Actions (Alloc, Init_Stmts);
3876 end if;
3877 end;
3879 else
3880 Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ));
3881 end if;
3882 end Convert_Aggr_In_Allocator;
3884 --------------------------------
3885 -- Convert_Aggr_In_Assignment --
3886 --------------------------------
3888 procedure Convert_Aggr_In_Assignment (N : Node_Id) is
3889 Aggr : Node_Id := Expression (N);
3890 Typ : constant Entity_Id := Etype (Aggr);
3891 Occ : constant Node_Id := New_Copy_Tree (Name (N));
3893 begin
3894 if Nkind (Aggr) = N_Qualified_Expression then
3895 Aggr := Expression (Aggr);
3896 end if;
3898 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
3899 end Convert_Aggr_In_Assignment;
3901 ---------------------------------
3902 -- Convert_Aggr_In_Object_Decl --
3903 ---------------------------------
3905 procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is
3906 Obj : constant Entity_Id := Defining_Identifier (N);
3907 Aggr : Node_Id := Expression (N);
3908 Loc : constant Source_Ptr := Sloc (Aggr);
3909 Typ : constant Entity_Id := Etype (Aggr);
3910 Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc);
3912 function Discriminants_Ok return Boolean;
3913 -- If the object type is constrained, the discriminants in the
3914 -- aggregate must be checked against the discriminants of the subtype.
3915 -- This cannot be done using Apply_Discriminant_Checks because after
3916 -- expansion there is no aggregate left to check.
3918 ----------------------
3919 -- Discriminants_Ok --
3920 ----------------------
3922 function Discriminants_Ok return Boolean is
3923 Cond : Node_Id := Empty;
3924 Check : Node_Id;
3925 D : Entity_Id;
3926 Disc1 : Elmt_Id;
3927 Disc2 : Elmt_Id;
3928 Val1 : Node_Id;
3929 Val2 : Node_Id;
3931 begin
3932 D := First_Discriminant (Typ);
3933 Disc1 := First_Elmt (Discriminant_Constraint (Typ));
3934 Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj)));
3935 while Present (Disc1) and then Present (Disc2) loop
3936 Val1 := Node (Disc1);
3937 Val2 := Node (Disc2);
3939 if not Is_OK_Static_Expression (Val1)
3940 or else not Is_OK_Static_Expression (Val2)
3941 then
3942 Check := Make_Op_Ne (Loc,
3943 Left_Opnd => Duplicate_Subexpr (Val1),
3944 Right_Opnd => Duplicate_Subexpr (Val2));
3946 if No (Cond) then
3947 Cond := Check;
3949 else
3950 Cond := Make_Or_Else (Loc,
3951 Left_Opnd => Cond,
3952 Right_Opnd => Check);
3953 end if;
3955 elsif Expr_Value (Val1) /= Expr_Value (Val2) then
3956 Apply_Compile_Time_Constraint_Error (Aggr,
3957 Msg => "incorrect value for discriminant&??",
3958 Reason => CE_Discriminant_Check_Failed,
3959 Ent => D);
3960 return False;
3961 end if;
3963 Next_Discriminant (D);
3964 Next_Elmt (Disc1);
3965 Next_Elmt (Disc2);
3966 end loop;
3968 -- If any discriminant constraint is non-static, emit a check
3970 if Present (Cond) then
3971 Insert_Action (N,
3972 Make_Raise_Constraint_Error (Loc,
3973 Condition => Cond,
3974 Reason => CE_Discriminant_Check_Failed));
3975 end if;
3977 return True;
3978 end Discriminants_Ok;
3980 -- Start of processing for Convert_Aggr_In_Object_Decl
3982 begin
3983 Set_Assignment_OK (Occ);
3985 if Nkind (Aggr) = N_Qualified_Expression then
3986 Aggr := Expression (Aggr);
3987 end if;
3989 if Has_Discriminants (Typ)
3990 and then Typ /= Etype (Obj)
3991 and then Is_Constrained (Etype (Obj))
3992 and then not Discriminants_Ok
3993 then
3994 return;
3995 end if;
3997 -- If the context is an extended return statement, it has its own
3998 -- finalization machinery (i.e. works like a transient scope) and
3999 -- we do not want to create an additional one, because objects on
4000 -- the finalization list of the return must be moved to the caller's
4001 -- finalization list to complete the return.
4003 -- However, if the aggregate is limited, it is built in place, and the
4004 -- controlled components are not assigned to intermediate temporaries
4005 -- so there is no need for a transient scope in this case either.
4007 if Requires_Transient_Scope (Typ)
4008 and then Ekind (Current_Scope) /= E_Return_Statement
4009 and then not Is_Limited_Type (Typ)
4010 then
4011 Establish_Transient_Scope
4012 (Aggr,
4013 Sec_Stack =>
4014 Is_Controlled (Typ) or else Has_Controlled_Component (Typ));
4015 end if;
4017 declare
4018 Node_After : constant Node_Id := Next (N);
4019 begin
4020 Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ));
4021 Collect_Initialization_Statements (Obj, N, Node_After);
4022 end;
4023 Set_No_Initialization (N);
4024 Initialize_Discriminants (N, Typ);
4025 end Convert_Aggr_In_Object_Decl;
4027 -------------------------------------
4028 -- Convert_Array_Aggr_In_Allocator --
4029 -------------------------------------
4031 procedure Convert_Array_Aggr_In_Allocator
4032 (Decl : Node_Id;
4033 Aggr : Node_Id;
4034 Target : Node_Id)
4036 Aggr_Code : List_Id;
4037 Typ : constant Entity_Id := Etype (Aggr);
4038 Ctyp : constant Entity_Id := Component_Type (Typ);
4040 begin
4041 -- The target is an explicit dereference of the allocated object.
4042 -- Generate component assignments to it, as for an aggregate that
4043 -- appears on the right-hand side of an assignment statement.
4045 Aggr_Code :=
4046 Build_Array_Aggr_Code (Aggr,
4047 Ctype => Ctyp,
4048 Index => First_Index (Typ),
4049 Into => Target,
4050 Scalar_Comp => Is_Scalar_Type (Ctyp));
4052 Insert_Actions_After (Decl, Aggr_Code);
4053 end Convert_Array_Aggr_In_Allocator;
4055 ----------------------------
4056 -- Convert_To_Assignments --
4057 ----------------------------
4059 procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is
4060 Loc : constant Source_Ptr := Sloc (N);
4061 T : Entity_Id;
4062 Temp : Entity_Id;
4064 Aggr_Code : List_Id;
4065 Instr : Node_Id;
4066 Target_Expr : Node_Id;
4067 Parent_Kind : Node_Kind;
4068 Unc_Decl : Boolean := False;
4069 Parent_Node : Node_Id;
4071 begin
4072 pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N));
4073 pragma Assert (Is_Record_Type (Typ));
4075 Parent_Node := Parent (N);
4076 Parent_Kind := Nkind (Parent_Node);
4078 if Parent_Kind = N_Qualified_Expression then
4080 -- Check if we are in a unconstrained declaration because in this
4081 -- case the current delayed expansion mechanism doesn't work when
4082 -- the declared object size depend on the initializing expr.
4084 Parent_Node := Parent (Parent_Node);
4085 Parent_Kind := Nkind (Parent_Node);
4087 if Parent_Kind = N_Object_Declaration then
4088 Unc_Decl :=
4089 not Is_Entity_Name (Object_Definition (Parent_Node))
4090 or else Has_Discriminants
4091 (Entity (Object_Definition (Parent_Node)))
4092 or else Is_Class_Wide_Type
4093 (Entity (Object_Definition (Parent_Node)));
4094 end if;
4095 end if;
4097 -- Just set the Delay flag in the cases where the transformation will be
4098 -- done top down from above.
4100 if False
4102 -- Internal aggregate (transformed when expanding the parent)
4104 or else Parent_Kind = N_Aggregate
4105 or else Parent_Kind = N_Extension_Aggregate
4106 or else Parent_Kind = N_Component_Association
4108 -- Allocator (see Convert_Aggr_In_Allocator)
4110 or else Parent_Kind = N_Allocator
4112 -- Object declaration (see Convert_Aggr_In_Object_Decl)
4114 or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl)
4116 -- Safe assignment (see Convert_Aggr_Assignments). So far only the
4117 -- assignments in init procs are taken into account.
4119 or else (Parent_Kind = N_Assignment_Statement
4120 and then Inside_Init_Proc)
4122 -- (Ada 2005) An inherently limited type in a return statement, which
4123 -- will be handled in a build-in-place fashion, and may be rewritten
4124 -- as an extended return and have its own finalization machinery.
4125 -- In the case of a simple return, the aggregate needs to be delayed
4126 -- until the scope for the return statement has been created, so
4127 -- that any finalization chain will be associated with that scope.
4128 -- For extended returns, we delay expansion to avoid the creation
4129 -- of an unwanted transient scope that could result in premature
4130 -- finalization of the return object (which is built in place
4131 -- within the caller's scope).
4133 or else
4134 (Is_Limited_View (Typ)
4135 and then
4136 (Nkind (Parent (Parent_Node)) = N_Extended_Return_Statement
4137 or else Nkind (Parent_Node) = N_Simple_Return_Statement))
4138 then
4139 Set_Expansion_Delayed (N);
4140 return;
4141 end if;
4143 -- Otherwise, if a transient scope is required, create it now. If we
4144 -- are within an initialization procedure do not create such, because
4145 -- the target of the assignment must not be declared within a local
4146 -- block, and because cleanup will take place on return from the
4147 -- initialization procedure.
4149 -- Should the condition be more restrictive ???
4151 if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then
4152 Establish_Transient_Scope (N, Sec_Stack => Needs_Finalization (Typ));
4153 end if;
4155 -- If the aggregate is nonlimited, create a temporary. If it is limited
4156 -- and context is an assignment, this is a subaggregate for an enclosing
4157 -- aggregate being expanded. It must be built in place, so use target of
4158 -- the current assignment.
4160 if Is_Limited_Type (Typ)
4161 and then Nkind (Parent (N)) = N_Assignment_Statement
4162 then
4163 Target_Expr := New_Copy_Tree (Name (Parent (N)));
4164 Insert_Actions (Parent (N),
4165 Build_Record_Aggr_Code (N, Typ, Target_Expr));
4166 Rewrite (Parent (N), Make_Null_Statement (Loc));
4168 else
4169 Temp := Make_Temporary (Loc, 'A', N);
4171 -- If the type inherits unknown discriminants, use the view with
4172 -- known discriminants if available.
4174 if Has_Unknown_Discriminants (Typ)
4175 and then Present (Underlying_Record_View (Typ))
4176 then
4177 T := Underlying_Record_View (Typ);
4178 else
4179 T := Typ;
4180 end if;
4182 Instr :=
4183 Make_Object_Declaration (Loc,
4184 Defining_Identifier => Temp,
4185 Object_Definition => New_Occurrence_Of (T, Loc));
4187 Set_No_Initialization (Instr);
4188 Insert_Action (N, Instr);
4189 Initialize_Discriminants (Instr, T);
4191 Target_Expr := New_Occurrence_Of (Temp, Loc);
4192 Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr);
4194 -- Save the last assignment statement associated with the aggregate
4195 -- when building a controlled object. This reference is utilized by
4196 -- the finalization machinery when marking an object as successfully
4197 -- initialized.
4199 if Needs_Finalization (T) then
4200 Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code));
4201 end if;
4203 Insert_Actions (N, Aggr_Code);
4204 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4205 Analyze_And_Resolve (N, T);
4206 end if;
4207 end Convert_To_Assignments;
4209 ---------------------------
4210 -- Convert_To_Positional --
4211 ---------------------------
4213 procedure Convert_To_Positional
4214 (N : Node_Id;
4215 Max_Others_Replicate : Nat := 5;
4216 Handle_Bit_Packed : Boolean := False)
4218 Typ : constant Entity_Id := Etype (N);
4220 Static_Components : Boolean := True;
4222 procedure Check_Static_Components;
4223 -- Check whether all components of the aggregate are compile-time known
4224 -- values, and can be passed as is to the back-end without further
4225 -- expansion.
4227 function Flatten
4228 (N : Node_Id;
4229 Ix : Node_Id;
4230 Ixb : Node_Id) return Boolean;
4231 -- Convert the aggregate into a purely positional form if possible. On
4232 -- entry the bounds of all dimensions are known to be static, and the
4233 -- total number of components is safe enough to expand.
4235 function Is_Flat (N : Node_Id; Dims : Int) return Boolean;
4236 -- Return True iff the array N is flat (which is not trivial in the case
4237 -- of multidimensional aggregates).
4239 -----------------------------
4240 -- Check_Static_Components --
4241 -----------------------------
4243 -- Could use some comments in this body ???
4245 procedure Check_Static_Components is
4246 Expr : Node_Id;
4248 begin
4249 Static_Components := True;
4251 if Nkind (N) = N_String_Literal then
4252 null;
4254 elsif Present (Expressions (N)) then
4255 Expr := First (Expressions (N));
4256 while Present (Expr) loop
4257 if Nkind (Expr) /= N_Aggregate
4258 or else not Compile_Time_Known_Aggregate (Expr)
4259 or else Expansion_Delayed (Expr)
4260 then
4261 Static_Components := False;
4262 exit;
4263 end if;
4265 Next (Expr);
4266 end loop;
4267 end if;
4269 if Nkind (N) = N_Aggregate
4270 and then Present (Component_Associations (N))
4271 then
4272 Expr := First (Component_Associations (N));
4273 while Present (Expr) loop
4274 if Nkind_In (Expression (Expr), N_Integer_Literal,
4275 N_Real_Literal)
4276 then
4277 null;
4279 elsif Is_Entity_Name (Expression (Expr))
4280 and then Present (Entity (Expression (Expr)))
4281 and then Ekind (Entity (Expression (Expr))) =
4282 E_Enumeration_Literal
4283 then
4284 null;
4286 elsif Nkind (Expression (Expr)) /= N_Aggregate
4287 or else not Compile_Time_Known_Aggregate (Expression (Expr))
4288 or else Expansion_Delayed (Expression (Expr))
4289 then
4290 Static_Components := False;
4291 exit;
4292 end if;
4294 Next (Expr);
4295 end loop;
4296 end if;
4297 end Check_Static_Components;
4299 -------------
4300 -- Flatten --
4301 -------------
4303 function Flatten
4304 (N : Node_Id;
4305 Ix : Node_Id;
4306 Ixb : Node_Id) return Boolean
4308 Loc : constant Source_Ptr := Sloc (N);
4309 Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb));
4310 Lo : constant Node_Id := Type_Low_Bound (Etype (Ix));
4311 Hi : constant Node_Id := Type_High_Bound (Etype (Ix));
4312 Lov : Uint;
4313 Hiv : Uint;
4315 Others_Present : Boolean := False;
4317 begin
4318 if Nkind (Original_Node (N)) = N_String_Literal then
4319 return True;
4320 end if;
4322 if not Compile_Time_Known_Value (Lo)
4323 or else not Compile_Time_Known_Value (Hi)
4324 then
4325 return False;
4326 end if;
4328 Lov := Expr_Value (Lo);
4329 Hiv := Expr_Value (Hi);
4331 -- Check if there is an others choice
4333 if Present (Component_Associations (N)) then
4334 declare
4335 Assoc : Node_Id;
4336 Choice : Node_Id;
4338 begin
4339 Assoc := First (Component_Associations (N));
4340 while Present (Assoc) loop
4342 -- If this is a box association, flattening is in general
4343 -- not possible because at this point we cannot tell if the
4344 -- default is static or even exists.
4346 if Box_Present (Assoc) then
4347 return False;
4348 end if;
4350 Choice := First (Choices (Assoc));
4352 while Present (Choice) loop
4353 if Nkind (Choice) = N_Others_Choice then
4354 Others_Present := True;
4355 end if;
4357 Next (Choice);
4358 end loop;
4360 Next (Assoc);
4361 end loop;
4362 end;
4363 end if;
4365 -- If the low bound is not known at compile time and others is not
4366 -- present we can proceed since the bounds can be obtained from the
4367 -- aggregate.
4369 if Hiv < Lov
4370 or else (not Compile_Time_Known_Value (Blo) and then Others_Present)
4371 then
4372 return False;
4373 end if;
4375 -- Determine if set of alternatives is suitable for conversion and
4376 -- build an array containing the values in sequence.
4378 declare
4379 Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv))
4380 of Node_Id := (others => Empty);
4381 -- The values in the aggregate sorted appropriately
4383 Vlist : List_Id;
4384 -- Same data as Vals in list form
4386 Rep_Count : Nat;
4387 -- Used to validate Max_Others_Replicate limit
4389 Elmt : Node_Id;
4390 Num : Int := UI_To_Int (Lov);
4391 Choice_Index : Int;
4392 Choice : Node_Id;
4393 Lo, Hi : Node_Id;
4395 begin
4396 if Present (Expressions (N)) then
4397 Elmt := First (Expressions (N));
4398 while Present (Elmt) loop
4399 if Nkind (Elmt) = N_Aggregate
4400 and then Present (Next_Index (Ix))
4401 and then
4402 not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb))
4403 then
4404 return False;
4405 end if;
4407 Vals (Num) := Relocate_Node (Elmt);
4408 Num := Num + 1;
4410 Next (Elmt);
4411 end loop;
4412 end if;
4414 if No (Component_Associations (N)) then
4415 return True;
4416 end if;
4418 Elmt := First (Component_Associations (N));
4420 if Nkind (Expression (Elmt)) = N_Aggregate then
4421 if Present (Next_Index (Ix))
4422 and then
4423 not Flatten
4424 (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb))
4425 then
4426 return False;
4427 end if;
4428 end if;
4430 Component_Loop : while Present (Elmt) loop
4431 Choice := First (Choices (Elmt));
4432 Choice_Loop : while Present (Choice) loop
4434 -- If we have an others choice, fill in the missing elements
4435 -- subject to the limit established by Max_Others_Replicate.
4437 if Nkind (Choice) = N_Others_Choice then
4438 Rep_Count := 0;
4440 for J in Vals'Range loop
4441 if No (Vals (J)) then
4442 Vals (J) := New_Copy_Tree (Expression (Elmt));
4443 Rep_Count := Rep_Count + 1;
4445 -- Check for maximum others replication. Note that
4446 -- we skip this test if either of the restrictions
4447 -- No_Elaboration_Code or No_Implicit_Loops is
4448 -- active, if this is a preelaborable unit or
4449 -- a predefined unit, or if the unit must be
4450 -- placed in data memory. This also ensures that
4451 -- predefined units get the same level of constant
4452 -- folding in Ada 95 and Ada 2005, where their
4453 -- categorization has changed.
4455 declare
4456 P : constant Entity_Id :=
4457 Cunit_Entity (Current_Sem_Unit);
4459 begin
4460 -- Check if duplication OK and if so continue
4461 -- processing.
4463 if Restriction_Active (No_Elaboration_Code)
4464 or else Restriction_Active (No_Implicit_Loops)
4465 or else
4466 (Ekind (Current_Scope) = E_Package
4467 and then Static_Elaboration_Desired
4468 (Current_Scope))
4469 or else Is_Preelaborated (P)
4470 or else (Ekind (P) = E_Package_Body
4471 and then
4472 Is_Preelaborated (Spec_Entity (P)))
4473 or else
4474 Is_Predefined_File_Name
4475 (Unit_File_Name (Get_Source_Unit (P)))
4476 then
4477 null;
4479 -- If duplication not OK, then we return False
4480 -- if the replication count is too high
4482 elsif Rep_Count > Max_Others_Replicate then
4483 return False;
4485 -- Continue on if duplication not OK, but the
4486 -- replication count is not excessive.
4488 else
4489 null;
4490 end if;
4491 end;
4492 end if;
4493 end loop;
4495 exit Component_Loop;
4497 -- Case of a subtype mark, identifier or expanded name
4499 elsif Is_Entity_Name (Choice)
4500 and then Is_Type (Entity (Choice))
4501 then
4502 Lo := Type_Low_Bound (Etype (Choice));
4503 Hi := Type_High_Bound (Etype (Choice));
4505 -- Case of subtype indication
4507 elsif Nkind (Choice) = N_Subtype_Indication then
4508 Lo := Low_Bound (Range_Expression (Constraint (Choice)));
4509 Hi := High_Bound (Range_Expression (Constraint (Choice)));
4511 -- Case of a range
4513 elsif Nkind (Choice) = N_Range then
4514 Lo := Low_Bound (Choice);
4515 Hi := High_Bound (Choice);
4517 -- Normal subexpression case
4519 else pragma Assert (Nkind (Choice) in N_Subexpr);
4520 if not Compile_Time_Known_Value (Choice) then
4521 return False;
4523 else
4524 Choice_Index := UI_To_Int (Expr_Value (Choice));
4526 if Choice_Index in Vals'Range then
4527 Vals (Choice_Index) :=
4528 New_Copy_Tree (Expression (Elmt));
4529 goto Continue;
4531 -- Choice is statically out-of-range, will be
4532 -- rewritten to raise Constraint_Error.
4534 else
4535 return False;
4536 end if;
4537 end if;
4538 end if;
4540 -- Range cases merge with Lo,Hi set
4542 if not Compile_Time_Known_Value (Lo)
4543 or else
4544 not Compile_Time_Known_Value (Hi)
4545 then
4546 return False;
4548 else
4549 for J in UI_To_Int (Expr_Value (Lo)) ..
4550 UI_To_Int (Expr_Value (Hi))
4551 loop
4552 Vals (J) := New_Copy_Tree (Expression (Elmt));
4553 end loop;
4554 end if;
4556 <<Continue>>
4557 Next (Choice);
4558 end loop Choice_Loop;
4560 Next (Elmt);
4561 end loop Component_Loop;
4563 -- If we get here the conversion is possible
4565 Vlist := New_List;
4566 for J in Vals'Range loop
4567 Append (Vals (J), Vlist);
4568 end loop;
4570 Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist));
4571 Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N)));
4572 return True;
4573 end;
4574 end Flatten;
4576 -------------
4577 -- Is_Flat --
4578 -------------
4580 function Is_Flat (N : Node_Id; Dims : Int) return Boolean is
4581 Elmt : Node_Id;
4583 begin
4584 if Dims = 0 then
4585 return True;
4587 elsif Nkind (N) = N_Aggregate then
4588 if Present (Component_Associations (N)) then
4589 return False;
4591 else
4592 Elmt := First (Expressions (N));
4593 while Present (Elmt) loop
4594 if not Is_Flat (Elmt, Dims - 1) then
4595 return False;
4596 end if;
4598 Next (Elmt);
4599 end loop;
4601 return True;
4602 end if;
4603 else
4604 return True;
4605 end if;
4606 end Is_Flat;
4608 -- Start of processing for Convert_To_Positional
4610 begin
4611 -- Only convert to positional when generating C in case of an
4612 -- object declaration, this is the only case where aggregates are
4613 -- supported in C.
4615 if Modify_Tree_For_C and then not In_Object_Declaration (N) then
4616 return;
4617 end if;
4619 -- Ada 2005 (AI-287): Do not convert in case of default initialized
4620 -- components because in this case will need to call the corresponding
4621 -- IP procedure.
4623 if Has_Default_Init_Comps (N) then
4624 return;
4625 end if;
4627 if Is_Flat (N, Number_Dimensions (Typ)) then
4628 return;
4629 end if;
4631 if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then
4632 return;
4633 end if;
4635 -- Do not convert to positional if controlled components are involved
4636 -- since these require special processing
4638 if Has_Controlled_Component (Typ) then
4639 return;
4640 end if;
4642 Check_Static_Components;
4644 -- If the size is known, or all the components are static, try to
4645 -- build a fully positional aggregate.
4647 -- The size of the type may not be known for an aggregate with
4648 -- discriminated array components, but if the components are static
4649 -- it is still possible to verify statically that the length is
4650 -- compatible with the upper bound of the type, and therefore it is
4651 -- worth flattening such aggregates as well.
4653 -- For now the back-end expands these aggregates into individual
4654 -- assignments to the target anyway, but it is conceivable that
4655 -- it will eventually be able to treat such aggregates statically???
4657 if Aggr_Size_OK (N, Typ)
4658 and then Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ)))
4659 then
4660 if Static_Components then
4661 Set_Compile_Time_Known_Aggregate (N);
4662 Set_Expansion_Delayed (N, False);
4663 end if;
4665 Analyze_And_Resolve (N, Typ);
4666 end if;
4668 -- If Static_Elaboration_Desired has been specified, diagnose aggregates
4669 -- that will still require initialization code.
4671 if (Ekind (Current_Scope) = E_Package
4672 and then Static_Elaboration_Desired (Current_Scope))
4673 and then Nkind (Parent (N)) = N_Object_Declaration
4674 then
4675 declare
4676 Expr : Node_Id;
4678 begin
4679 if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then
4680 Expr := First (Expressions (N));
4681 while Present (Expr) loop
4682 if Nkind_In (Expr, N_Integer_Literal, N_Real_Literal)
4683 or else
4684 (Is_Entity_Name (Expr)
4685 and then Ekind (Entity (Expr)) = E_Enumeration_Literal)
4686 then
4687 null;
4689 else
4690 Error_Msg_N
4691 ("non-static object requires elaboration code??", N);
4692 exit;
4693 end if;
4695 Next (Expr);
4696 end loop;
4698 if Present (Component_Associations (N)) then
4699 Error_Msg_N ("object requires elaboration code??", N);
4700 end if;
4701 end if;
4702 end;
4703 end if;
4704 end Convert_To_Positional;
4706 ----------------------------
4707 -- Expand_Array_Aggregate --
4708 ----------------------------
4710 -- Array aggregate expansion proceeds as follows:
4712 -- 1. If requested we generate code to perform all the array aggregate
4713 -- bound checks, specifically
4715 -- (a) Check that the index range defined by aggregate bounds is
4716 -- compatible with corresponding index subtype.
4718 -- (b) If an others choice is present check that no aggregate
4719 -- index is outside the bounds of the index constraint.
4721 -- (c) For multidimensional arrays make sure that all subaggregates
4722 -- corresponding to the same dimension have the same bounds.
4724 -- 2. Check for packed array aggregate which can be converted to a
4725 -- constant so that the aggregate disappears completely.
4727 -- 3. Check case of nested aggregate. Generally nested aggregates are
4728 -- handled during the processing of the parent aggregate.
4730 -- 4. Check if the aggregate can be statically processed. If this is the
4731 -- case pass it as is to Gigi. Note that a necessary condition for
4732 -- static processing is that the aggregate be fully positional.
4734 -- 5. If in place aggregate expansion is possible (i.e. no need to create
4735 -- a temporary) then mark the aggregate as such and return. Otherwise
4736 -- create a new temporary and generate the appropriate initialization
4737 -- code.
4739 procedure Expand_Array_Aggregate (N : Node_Id) is
4740 Loc : constant Source_Ptr := Sloc (N);
4742 Typ : constant Entity_Id := Etype (N);
4743 Ctyp : constant Entity_Id := Component_Type (Typ);
4744 -- Typ is the correct constrained array subtype of the aggregate
4745 -- Ctyp is the corresponding component type.
4747 Aggr_Dimension : constant Pos := Number_Dimensions (Typ);
4748 -- Number of aggregate index dimensions
4750 Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id;
4751 Aggr_High : array (1 .. Aggr_Dimension) of Node_Id;
4752 -- Low and High bounds of the constraint for each aggregate index
4754 Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id;
4755 -- The type of each index
4757 In_Place_Assign_OK_For_Declaration : Boolean := False;
4758 -- True if we are to generate an in place assignment for a declaration
4760 Maybe_In_Place_OK : Boolean;
4761 -- If the type is neither controlled nor packed and the aggregate
4762 -- is the expression in an assignment, assignment in place may be
4763 -- possible, provided other conditions are met on the LHS.
4765 Others_Present : array (1 .. Aggr_Dimension) of Boolean :=
4766 (others => False);
4767 -- If Others_Present (J) is True, then there is an others choice in one
4768 -- of the subaggregates of N at dimension J.
4770 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean;
4771 -- Returns true if an aggregate assignment can be done by the back end
4773 procedure Build_Constrained_Type (Positional : Boolean);
4774 -- If the subtype is not static or unconstrained, build a constrained
4775 -- type using the computable sizes of the aggregate and its sub-
4776 -- aggregates.
4778 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id);
4779 -- Checks that the bounds of Aggr_Bounds are within the bounds defined
4780 -- by Index_Bounds.
4782 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos);
4783 -- Checks that in a multidimensional array aggregate all subaggregates
4784 -- corresponding to the same dimension have the same bounds. Sub_Aggr is
4785 -- an array subaggregate. Dim is the dimension corresponding to the
4786 -- subaggregate.
4788 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos);
4789 -- Computes the values of array Others_Present. Sub_Aggr is the array
4790 -- subaggregate we start the computation from. Dim is the dimension
4791 -- corresponding to the subaggregate.
4793 function In_Place_Assign_OK return Boolean;
4794 -- Simple predicate to determine whether an aggregate assignment can
4795 -- be done in place, because none of the new values can depend on the
4796 -- components of the target of the assignment.
4798 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos);
4799 -- Checks that if an others choice is present in any subaggregate, no
4800 -- aggregate index is outside the bounds of the index constraint.
4801 -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding
4802 -- to the subaggregate.
4804 function Safe_Left_Hand_Side (N : Node_Id) return Boolean;
4805 -- In addition to Maybe_In_Place_OK, in order for an aggregate to be
4806 -- built directly into the target of the assignment it must be free
4807 -- of side effects.
4809 ------------------------------------
4810 -- Aggr_Assignment_OK_For_Backend --
4811 ------------------------------------
4813 -- Backend processing by Gigi/gcc is possible only if all the following
4814 -- conditions are met:
4816 -- 1. N consists of a single OTHERS choice, possibly recursively
4818 -- 2. The array type is not packed
4820 -- 3. The array type has no atomic components
4822 -- 4. The array type has no null ranges (the purpose of this is to
4823 -- avoid a bogus warning for an out-of-range value).
4825 -- 5. The component type is discrete
4827 -- 6. The component size is Storage_Unit or the value is of the form
4828 -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit)
4829 -- and M in 1 .. A-1. This can also be viewed as K occurrences of
4830 -- the 8-bit value M, concatenated together.
4832 -- The ultimate goal is to generate a call to a fast memset routine
4833 -- specifically optimized for the target.
4835 function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is
4836 Ctyp : Entity_Id;
4837 Index : Entity_Id;
4838 Expr : Node_Id := N;
4839 Low : Node_Id;
4840 High : Node_Id;
4841 Remainder : Uint;
4842 Value : Uint;
4843 Nunits : Nat;
4845 begin
4846 -- Recurse as far as possible to find the innermost component type
4848 Ctyp := Etype (N);
4849 while Is_Array_Type (Ctyp) loop
4850 if Nkind (Expr) /= N_Aggregate
4851 or else not Is_Others_Aggregate (Expr)
4852 then
4853 return False;
4854 end if;
4856 if Present (Packed_Array_Impl_Type (Ctyp)) then
4857 return False;
4858 end if;
4860 if Has_Atomic_Components (Ctyp) then
4861 return False;
4862 end if;
4864 Index := First_Index (Ctyp);
4865 while Present (Index) loop
4866 Get_Index_Bounds (Index, Low, High);
4868 if Is_Null_Range (Low, High) then
4869 return False;
4870 end if;
4872 Next_Index (Index);
4873 end loop;
4875 Expr := Expression (First (Component_Associations (Expr)));
4877 for J in 1 .. Number_Dimensions (Ctyp) - 1 loop
4878 if Nkind (Expr) /= N_Aggregate
4879 or else not Is_Others_Aggregate (Expr)
4880 then
4881 return False;
4882 end if;
4884 Expr := Expression (First (Component_Associations (Expr)));
4885 end loop;
4887 Ctyp := Component_Type (Ctyp);
4889 if Is_Atomic_Or_VFA (Ctyp) then
4890 return False;
4891 end if;
4892 end loop;
4894 if not Is_Discrete_Type (Ctyp) then
4895 return False;
4896 end if;
4898 -- The expression needs to be analyzed if True is returned
4900 Analyze_And_Resolve (Expr, Ctyp);
4902 -- The back end uses the Esize as the precision of the type
4904 Nunits := UI_To_Int (Esize (Ctyp)) / System_Storage_Unit;
4906 if Nunits = 1 then
4907 return True;
4908 end if;
4910 if not Compile_Time_Known_Value (Expr) then
4911 return False;
4912 end if;
4914 Value := Expr_Value (Expr);
4916 if Has_Biased_Representation (Ctyp) then
4917 Value := Value - Expr_Value (Type_Low_Bound (Ctyp));
4918 end if;
4920 -- Values 0 and -1 immediately satisfy the last check
4922 if Value = Uint_0 or else Value = Uint_Minus_1 then
4923 return True;
4924 end if;
4926 -- We need to work with an unsigned value
4928 if Value < 0 then
4929 Value := Value + 2**(System_Storage_Unit * Nunits);
4930 end if;
4932 Remainder := Value rem 2**System_Storage_Unit;
4934 for J in 1 .. Nunits - 1 loop
4935 Value := Value / 2**System_Storage_Unit;
4937 if Value rem 2**System_Storage_Unit /= Remainder then
4938 return False;
4939 end if;
4940 end loop;
4942 return True;
4943 end Aggr_Assignment_OK_For_Backend;
4945 ----------------------------
4946 -- Build_Constrained_Type --
4947 ----------------------------
4949 procedure Build_Constrained_Type (Positional : Boolean) is
4950 Loc : constant Source_Ptr := Sloc (N);
4951 Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A');
4952 Comp : Node_Id;
4953 Decl : Node_Id;
4954 Typ : constant Entity_Id := Etype (N);
4955 Indexes : constant List_Id := New_List;
4956 Num : Nat;
4957 Sub_Agg : Node_Id;
4959 begin
4960 -- If the aggregate is purely positional, all its subaggregates
4961 -- have the same size. We collect the dimensions from the first
4962 -- subaggregate at each level.
4964 if Positional then
4965 Sub_Agg := N;
4967 for D in 1 .. Number_Dimensions (Typ) loop
4968 Sub_Agg := First (Expressions (Sub_Agg));
4970 Comp := Sub_Agg;
4971 Num := 0;
4972 while Present (Comp) loop
4973 Num := Num + 1;
4974 Next (Comp);
4975 end loop;
4977 Append_To (Indexes,
4978 Make_Range (Loc,
4979 Low_Bound => Make_Integer_Literal (Loc, 1),
4980 High_Bound => Make_Integer_Literal (Loc, Num)));
4981 end loop;
4983 else
4984 -- We know the aggregate type is unconstrained and the aggregate
4985 -- is not processable by the back end, therefore not necessarily
4986 -- positional. Retrieve each dimension bounds (computed earlier).
4988 for D in 1 .. Number_Dimensions (Typ) loop
4989 Append_To (Indexes,
4990 Make_Range (Loc,
4991 Low_Bound => Aggr_Low (D),
4992 High_Bound => Aggr_High (D)));
4993 end loop;
4994 end if;
4996 Decl :=
4997 Make_Full_Type_Declaration (Loc,
4998 Defining_Identifier => Agg_Type,
4999 Type_Definition =>
5000 Make_Constrained_Array_Definition (Loc,
5001 Discrete_Subtype_Definitions => Indexes,
5002 Component_Definition =>
5003 Make_Component_Definition (Loc,
5004 Aliased_Present => False,
5005 Subtype_Indication =>
5006 New_Occurrence_Of (Component_Type (Typ), Loc))));
5008 Insert_Action (N, Decl);
5009 Analyze (Decl);
5010 Set_Etype (N, Agg_Type);
5011 Set_Is_Itype (Agg_Type);
5012 Freeze_Itype (Agg_Type, N);
5013 end Build_Constrained_Type;
5015 ------------------
5016 -- Check_Bounds --
5017 ------------------
5019 procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is
5020 Aggr_Lo : Node_Id;
5021 Aggr_Hi : Node_Id;
5023 Ind_Lo : Node_Id;
5024 Ind_Hi : Node_Id;
5026 Cond : Node_Id := Empty;
5028 begin
5029 Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi);
5030 Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi);
5032 -- Generate the following test:
5034 -- [constraint_error when
5035 -- Aggr_Lo <= Aggr_Hi and then
5036 -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)]
5038 -- As an optimization try to see if some tests are trivially vacuous
5039 -- because we are comparing an expression against itself.
5041 if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then
5042 Cond := Empty;
5044 elsif Aggr_Hi = Ind_Hi then
5045 Cond :=
5046 Make_Op_Lt (Loc,
5047 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5048 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo));
5050 elsif Aggr_Lo = Ind_Lo then
5051 Cond :=
5052 Make_Op_Gt (Loc,
5053 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5054 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi));
5056 else
5057 Cond :=
5058 Make_Or_Else (Loc,
5059 Left_Opnd =>
5060 Make_Op_Lt (Loc,
5061 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5062 Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)),
5064 Right_Opnd =>
5065 Make_Op_Gt (Loc,
5066 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5067 Right_Opnd => Duplicate_Subexpr (Ind_Hi)));
5068 end if;
5070 if Present (Cond) then
5071 Cond :=
5072 Make_And_Then (Loc,
5073 Left_Opnd =>
5074 Make_Op_Le (Loc,
5075 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5076 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)),
5078 Right_Opnd => Cond);
5080 Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False);
5081 Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False);
5082 Insert_Action (N,
5083 Make_Raise_Constraint_Error (Loc,
5084 Condition => Cond,
5085 Reason => CE_Range_Check_Failed));
5086 end if;
5087 end Check_Bounds;
5089 ----------------------------
5090 -- Check_Same_Aggr_Bounds --
5091 ----------------------------
5093 procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is
5094 Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr));
5095 Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr));
5096 -- The bounds of this specific subaggregate
5098 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5099 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5100 -- The bounds of the aggregate for this dimension
5102 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5103 -- The index type for this dimension.xxx
5105 Cond : Node_Id := Empty;
5106 Assoc : Node_Id;
5107 Expr : Node_Id;
5109 begin
5110 -- If index checks are on generate the test
5112 -- [constraint_error when
5113 -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi]
5115 -- As an optimization try to see if some tests are trivially vacuos
5116 -- because we are comparing an expression against itself. Also for
5117 -- the first dimension the test is trivially vacuous because there
5118 -- is just one aggregate for dimension 1.
5120 if Index_Checks_Suppressed (Ind_Typ) then
5121 Cond := Empty;
5123 elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi)
5124 then
5125 Cond := Empty;
5127 elsif Aggr_Hi = Sub_Hi then
5128 Cond :=
5129 Make_Op_Ne (Loc,
5130 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5131 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo));
5133 elsif Aggr_Lo = Sub_Lo then
5134 Cond :=
5135 Make_Op_Ne (Loc,
5136 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi),
5137 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi));
5139 else
5140 Cond :=
5141 Make_Or_Else (Loc,
5142 Left_Opnd =>
5143 Make_Op_Ne (Loc,
5144 Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo),
5145 Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)),
5147 Right_Opnd =>
5148 Make_Op_Ne (Loc,
5149 Left_Opnd => Duplicate_Subexpr (Aggr_Hi),
5150 Right_Opnd => Duplicate_Subexpr (Sub_Hi)));
5151 end if;
5153 if Present (Cond) then
5154 Insert_Action (N,
5155 Make_Raise_Constraint_Error (Loc,
5156 Condition => Cond,
5157 Reason => CE_Length_Check_Failed));
5158 end if;
5160 -- Now look inside the subaggregate to see if there is more work
5162 if Dim < Aggr_Dimension then
5164 -- Process positional components
5166 if Present (Expressions (Sub_Aggr)) then
5167 Expr := First (Expressions (Sub_Aggr));
5168 while Present (Expr) loop
5169 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5170 Next (Expr);
5171 end loop;
5172 end if;
5174 -- Process component associations
5176 if Present (Component_Associations (Sub_Aggr)) then
5177 Assoc := First (Component_Associations (Sub_Aggr));
5178 while Present (Assoc) loop
5179 Expr := Expression (Assoc);
5180 Check_Same_Aggr_Bounds (Expr, Dim + 1);
5181 Next (Assoc);
5182 end loop;
5183 end if;
5184 end if;
5185 end Check_Same_Aggr_Bounds;
5187 ----------------------------
5188 -- Compute_Others_Present --
5189 ----------------------------
5191 procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is
5192 Assoc : Node_Id;
5193 Expr : Node_Id;
5195 begin
5196 if Present (Component_Associations (Sub_Aggr)) then
5197 Assoc := Last (Component_Associations (Sub_Aggr));
5199 if Nkind (First (Choices (Assoc))) = N_Others_Choice then
5200 Others_Present (Dim) := True;
5201 end if;
5202 end if;
5204 -- Now look inside the subaggregate to see if there is more work
5206 if Dim < Aggr_Dimension then
5208 -- Process positional components
5210 if Present (Expressions (Sub_Aggr)) then
5211 Expr := First (Expressions (Sub_Aggr));
5212 while Present (Expr) loop
5213 Compute_Others_Present (Expr, Dim + 1);
5214 Next (Expr);
5215 end loop;
5216 end if;
5218 -- Process component associations
5220 if Present (Component_Associations (Sub_Aggr)) then
5221 Assoc := First (Component_Associations (Sub_Aggr));
5222 while Present (Assoc) loop
5223 Expr := Expression (Assoc);
5224 Compute_Others_Present (Expr, Dim + 1);
5225 Next (Assoc);
5226 end loop;
5227 end if;
5228 end if;
5229 end Compute_Others_Present;
5231 ------------------------
5232 -- In_Place_Assign_OK --
5233 ------------------------
5235 function In_Place_Assign_OK return Boolean is
5236 Aggr_In : Node_Id;
5237 Aggr_Lo : Node_Id;
5238 Aggr_Hi : Node_Id;
5239 Obj_In : Node_Id;
5240 Obj_Lo : Node_Id;
5241 Obj_Hi : Node_Id;
5243 function Safe_Aggregate (Aggr : Node_Id) return Boolean;
5244 -- Check recursively that each component of a (sub)aggregate does not
5245 -- depend on the variable being assigned to.
5247 function Safe_Component (Expr : Node_Id) return Boolean;
5248 -- Verify that an expression cannot depend on the variable being
5249 -- assigned to. Room for improvement here (but less than before).
5251 --------------------
5252 -- Safe_Aggregate --
5253 --------------------
5255 function Safe_Aggregate (Aggr : Node_Id) return Boolean is
5256 Expr : Node_Id;
5258 begin
5259 if Present (Expressions (Aggr)) then
5260 Expr := First (Expressions (Aggr));
5261 while Present (Expr) loop
5262 if Nkind (Expr) = N_Aggregate then
5263 if not Safe_Aggregate (Expr) then
5264 return False;
5265 end if;
5267 elsif not Safe_Component (Expr) then
5268 return False;
5269 end if;
5271 Next (Expr);
5272 end loop;
5273 end if;
5275 if Present (Component_Associations (Aggr)) then
5276 Expr := First (Component_Associations (Aggr));
5277 while Present (Expr) loop
5278 if Nkind (Expression (Expr)) = N_Aggregate then
5279 if not Safe_Aggregate (Expression (Expr)) then
5280 return False;
5281 end if;
5283 -- If association has a box, no way to determine yet
5284 -- whether default can be assigned in place.
5286 elsif Box_Present (Expr) then
5287 return False;
5289 elsif not Safe_Component (Expression (Expr)) then
5290 return False;
5291 end if;
5293 Next (Expr);
5294 end loop;
5295 end if;
5297 return True;
5298 end Safe_Aggregate;
5300 --------------------
5301 -- Safe_Component --
5302 --------------------
5304 function Safe_Component (Expr : Node_Id) return Boolean is
5305 Comp : Node_Id := Expr;
5307 function Check_Component (Comp : Node_Id) return Boolean;
5308 -- Do the recursive traversal, after copy
5310 ---------------------
5311 -- Check_Component --
5312 ---------------------
5314 function Check_Component (Comp : Node_Id) return Boolean is
5315 begin
5316 if Is_Overloaded (Comp) then
5317 return False;
5318 end if;
5320 return Compile_Time_Known_Value (Comp)
5322 or else (Is_Entity_Name (Comp)
5323 and then Present (Entity (Comp))
5324 and then No (Renamed_Object (Entity (Comp))))
5326 or else (Nkind (Comp) = N_Attribute_Reference
5327 and then Check_Component (Prefix (Comp)))
5329 or else (Nkind (Comp) in N_Binary_Op
5330 and then Check_Component (Left_Opnd (Comp))
5331 and then Check_Component (Right_Opnd (Comp)))
5333 or else (Nkind (Comp) in N_Unary_Op
5334 and then Check_Component (Right_Opnd (Comp)))
5336 or else (Nkind (Comp) = N_Selected_Component
5337 and then Check_Component (Prefix (Comp)))
5339 or else (Nkind (Comp) = N_Unchecked_Type_Conversion
5340 and then Check_Component (Expression (Comp)));
5341 end Check_Component;
5343 -- Start of processing for Safe_Component
5345 begin
5346 -- If the component appears in an association that may correspond
5347 -- to more than one element, it is not analyzed before expansion
5348 -- into assignments, to avoid side effects. We analyze, but do not
5349 -- resolve the copy, to obtain sufficient entity information for
5350 -- the checks that follow. If component is overloaded we assume
5351 -- an unsafe function call.
5353 if not Analyzed (Comp) then
5354 if Is_Overloaded (Expr) then
5355 return False;
5357 elsif Nkind (Expr) = N_Aggregate
5358 and then not Is_Others_Aggregate (Expr)
5359 then
5360 return False;
5362 elsif Nkind (Expr) = N_Allocator then
5364 -- For now, too complex to analyze
5366 return False;
5367 end if;
5369 Comp := New_Copy_Tree (Expr);
5370 Set_Parent (Comp, Parent (Expr));
5371 Analyze (Comp);
5372 end if;
5374 if Nkind (Comp) = N_Aggregate then
5375 return Safe_Aggregate (Comp);
5376 else
5377 return Check_Component (Comp);
5378 end if;
5379 end Safe_Component;
5381 -- Start of processing for In_Place_Assign_OK
5383 begin
5384 if Present (Component_Associations (N)) then
5386 -- On assignment, sliding can take place, so we cannot do the
5387 -- assignment in place unless the bounds of the aggregate are
5388 -- statically equal to those of the target.
5390 -- If the aggregate is given by an others choice, the bounds are
5391 -- derived from the left-hand side, and the assignment is safe if
5392 -- the expression is.
5394 if Is_Others_Aggregate (N) then
5395 return
5396 Safe_Component
5397 (Expression (First (Component_Associations (N))));
5398 end if;
5400 Aggr_In := First_Index (Etype (N));
5402 if Nkind (Parent (N)) = N_Assignment_Statement then
5403 Obj_In := First_Index (Etype (Name (Parent (N))));
5405 else
5406 -- Context is an allocator. Check bounds of aggregate against
5407 -- given type in qualified expression.
5409 pragma Assert (Nkind (Parent (Parent (N))) = N_Allocator);
5410 Obj_In :=
5411 First_Index (Etype (Entity (Subtype_Mark (Parent (N)))));
5412 end if;
5414 while Present (Aggr_In) loop
5415 Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi);
5416 Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi);
5418 if not Compile_Time_Known_Value (Aggr_Lo)
5419 or else not Compile_Time_Known_Value (Aggr_Hi)
5420 or else not Compile_Time_Known_Value (Obj_Lo)
5421 or else not Compile_Time_Known_Value (Obj_Hi)
5422 or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo)
5423 or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi)
5424 then
5425 return False;
5426 end if;
5428 Next_Index (Aggr_In);
5429 Next_Index (Obj_In);
5430 end loop;
5431 end if;
5433 -- Now check the component values themselves
5435 return Safe_Aggregate (N);
5436 end In_Place_Assign_OK;
5438 ------------------
5439 -- Others_Check --
5440 ------------------
5442 procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is
5443 Aggr_Lo : constant Node_Id := Aggr_Low (Dim);
5444 Aggr_Hi : constant Node_Id := Aggr_High (Dim);
5445 -- The bounds of the aggregate for this dimension
5447 Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim);
5448 -- The index type for this dimension
5450 Need_To_Check : Boolean := False;
5452 Choices_Lo : Node_Id := Empty;
5453 Choices_Hi : Node_Id := Empty;
5454 -- The lowest and highest discrete choices for a named subaggregate
5456 Nb_Choices : Int := -1;
5457 -- The number of discrete non-others choices in this subaggregate
5459 Nb_Elements : Uint := Uint_0;
5460 -- The number of elements in a positional aggregate
5462 Cond : Node_Id := Empty;
5464 Assoc : Node_Id;
5465 Choice : Node_Id;
5466 Expr : Node_Id;
5468 begin
5469 -- Check if we have an others choice. If we do make sure that this
5470 -- subaggregate contains at least one element in addition to the
5471 -- others choice.
5473 if Range_Checks_Suppressed (Ind_Typ) then
5474 Need_To_Check := False;
5476 elsif Present (Expressions (Sub_Aggr))
5477 and then Present (Component_Associations (Sub_Aggr))
5478 then
5479 Need_To_Check := True;
5481 elsif Present (Component_Associations (Sub_Aggr)) then
5482 Assoc := Last (Component_Associations (Sub_Aggr));
5484 if Nkind (First (Choices (Assoc))) /= N_Others_Choice then
5485 Need_To_Check := False;
5487 else
5488 -- Count the number of discrete choices. Start with -1 because
5489 -- the others choice does not count.
5491 -- Is there some reason we do not use List_Length here ???
5493 Nb_Choices := -1;
5494 Assoc := First (Component_Associations (Sub_Aggr));
5495 while Present (Assoc) loop
5496 Choice := First (Choices (Assoc));
5497 while Present (Choice) loop
5498 Nb_Choices := Nb_Choices + 1;
5499 Next (Choice);
5500 end loop;
5502 Next (Assoc);
5503 end loop;
5505 -- If there is only an others choice nothing to do
5507 Need_To_Check := (Nb_Choices > 0);
5508 end if;
5510 else
5511 Need_To_Check := False;
5512 end if;
5514 -- If we are dealing with a positional subaggregate with an others
5515 -- choice then compute the number or positional elements.
5517 if Need_To_Check and then Present (Expressions (Sub_Aggr)) then
5518 Expr := First (Expressions (Sub_Aggr));
5519 Nb_Elements := Uint_0;
5520 while Present (Expr) loop
5521 Nb_Elements := Nb_Elements + 1;
5522 Next (Expr);
5523 end loop;
5525 -- If the aggregate contains discrete choices and an others choice
5526 -- compute the smallest and largest discrete choice values.
5528 elsif Need_To_Check then
5529 Compute_Choices_Lo_And_Choices_Hi : declare
5531 Table : Case_Table_Type (1 .. Nb_Choices);
5532 -- Used to sort all the different choice values
5534 J : Pos := 1;
5535 Low : Node_Id;
5536 High : Node_Id;
5538 begin
5539 Assoc := First (Component_Associations (Sub_Aggr));
5540 while Present (Assoc) loop
5541 Choice := First (Choices (Assoc));
5542 while Present (Choice) loop
5543 if Nkind (Choice) = N_Others_Choice then
5544 exit;
5545 end if;
5547 Get_Index_Bounds (Choice, Low, High);
5548 Table (J).Choice_Lo := Low;
5549 Table (J).Choice_Hi := High;
5551 J := J + 1;
5552 Next (Choice);
5553 end loop;
5555 Next (Assoc);
5556 end loop;
5558 -- Sort the discrete choices
5560 Sort_Case_Table (Table);
5562 Choices_Lo := Table (1).Choice_Lo;
5563 Choices_Hi := Table (Nb_Choices).Choice_Hi;
5564 end Compute_Choices_Lo_And_Choices_Hi;
5565 end if;
5567 -- If no others choice in this subaggregate, or the aggregate
5568 -- comprises only an others choice, nothing to do.
5570 if not Need_To_Check then
5571 Cond := Empty;
5573 -- If we are dealing with an aggregate containing an others choice
5574 -- and positional components, we generate the following test:
5576 -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) >
5577 -- Ind_Typ'Pos (Aggr_Hi)
5578 -- then
5579 -- raise Constraint_Error;
5580 -- end if;
5582 elsif Nb_Elements > Uint_0 then
5583 Cond :=
5584 Make_Op_Gt (Loc,
5585 Left_Opnd =>
5586 Make_Op_Add (Loc,
5587 Left_Opnd =>
5588 Make_Attribute_Reference (Loc,
5589 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5590 Attribute_Name => Name_Pos,
5591 Expressions =>
5592 New_List
5593 (Duplicate_Subexpr_Move_Checks (Aggr_Lo))),
5594 Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)),
5596 Right_Opnd =>
5597 Make_Attribute_Reference (Loc,
5598 Prefix => New_Occurrence_Of (Ind_Typ, Loc),
5599 Attribute_Name => Name_Pos,
5600 Expressions => New_List (
5601 Duplicate_Subexpr_Move_Checks (Aggr_Hi))));
5603 -- If we are dealing with an aggregate containing an others choice
5604 -- and discrete choices we generate the following test:
5606 -- [constraint_error when
5607 -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi];
5609 else
5610 Cond :=
5611 Make_Or_Else (Loc,
5612 Left_Opnd =>
5613 Make_Op_Lt (Loc,
5614 Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo),
5615 Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)),
5617 Right_Opnd =>
5618 Make_Op_Gt (Loc,
5619 Left_Opnd => Duplicate_Subexpr (Choices_Hi),
5620 Right_Opnd => Duplicate_Subexpr (Aggr_Hi)));
5621 end if;
5623 if Present (Cond) then
5624 Insert_Action (N,
5625 Make_Raise_Constraint_Error (Loc,
5626 Condition => Cond,
5627 Reason => CE_Length_Check_Failed));
5628 -- Questionable reason code, shouldn't that be a
5629 -- CE_Range_Check_Failed ???
5630 end if;
5632 -- Now look inside the subaggregate to see if there is more work
5634 if Dim < Aggr_Dimension then
5636 -- Process positional components
5638 if Present (Expressions (Sub_Aggr)) then
5639 Expr := First (Expressions (Sub_Aggr));
5640 while Present (Expr) loop
5641 Others_Check (Expr, Dim + 1);
5642 Next (Expr);
5643 end loop;
5644 end if;
5646 -- Process component associations
5648 if Present (Component_Associations (Sub_Aggr)) then
5649 Assoc := First (Component_Associations (Sub_Aggr));
5650 while Present (Assoc) loop
5651 Expr := Expression (Assoc);
5652 Others_Check (Expr, Dim + 1);
5653 Next (Assoc);
5654 end loop;
5655 end if;
5656 end if;
5657 end Others_Check;
5659 -------------------------
5660 -- Safe_Left_Hand_Side --
5661 -------------------------
5663 function Safe_Left_Hand_Side (N : Node_Id) return Boolean is
5664 function Is_Safe_Index (Indx : Node_Id) return Boolean;
5665 -- If the left-hand side includes an indexed component, check that
5666 -- the indexes are free of side effects.
5668 -------------------
5669 -- Is_Safe_Index --
5670 -------------------
5672 function Is_Safe_Index (Indx : Node_Id) return Boolean is
5673 begin
5674 if Is_Entity_Name (Indx) then
5675 return True;
5677 elsif Nkind (Indx) = N_Integer_Literal then
5678 return True;
5680 elsif Nkind (Indx) = N_Function_Call
5681 and then Is_Entity_Name (Name (Indx))
5682 and then Has_Pragma_Pure_Function (Entity (Name (Indx)))
5683 then
5684 return True;
5686 elsif Nkind (Indx) = N_Type_Conversion
5687 and then Is_Safe_Index (Expression (Indx))
5688 then
5689 return True;
5691 else
5692 return False;
5693 end if;
5694 end Is_Safe_Index;
5696 -- Start of processing for Safe_Left_Hand_Side
5698 begin
5699 if Is_Entity_Name (N) then
5700 return True;
5702 elsif Nkind_In (N, N_Explicit_Dereference, N_Selected_Component)
5703 and then Safe_Left_Hand_Side (Prefix (N))
5704 then
5705 return True;
5707 elsif Nkind (N) = N_Indexed_Component
5708 and then Safe_Left_Hand_Side (Prefix (N))
5709 and then Is_Safe_Index (First (Expressions (N)))
5710 then
5711 return True;
5713 elsif Nkind (N) = N_Unchecked_Type_Conversion then
5714 return Safe_Left_Hand_Side (Expression (N));
5716 else
5717 return False;
5718 end if;
5719 end Safe_Left_Hand_Side;
5721 -- Local variables
5723 Tmp : Entity_Id;
5724 -- Holds the temporary aggregate value
5726 Tmp_Decl : Node_Id;
5727 -- Holds the declaration of Tmp
5729 Aggr_Code : List_Id;
5730 Parent_Node : Node_Id;
5731 Parent_Kind : Node_Kind;
5733 -- Start of processing for Expand_Array_Aggregate
5735 begin
5736 -- Do not touch the special aggregates of attributes used for Asm calls
5738 if Is_RTE (Ctyp, RE_Asm_Input_Operand)
5739 or else Is_RTE (Ctyp, RE_Asm_Output_Operand)
5740 then
5741 return;
5743 -- Do not expand an aggregate for an array type which contains tasks if
5744 -- the aggregate is associated with an unexpanded return statement of a
5745 -- build-in-place function. The aggregate is expanded when the related
5746 -- return statement (rewritten into an extended return) is processed.
5747 -- This delay ensures that any temporaries and initialization code
5748 -- generated for the aggregate appear in the proper return block and
5749 -- use the correct _chain and _master.
5751 elsif Has_Task (Base_Type (Etype (N)))
5752 and then Nkind (Parent (N)) = N_Simple_Return_Statement
5753 and then Is_Build_In_Place_Function
5754 (Return_Applies_To (Return_Statement_Entity (Parent (N))))
5755 then
5756 return;
5758 -- Do not attempt expansion if error already detected. We may reach this
5759 -- point in spite of previous errors when compiling with -gnatq, to
5760 -- force all possible errors (this is the usual ACATS mode).
5762 elsif Error_Posted (N) then
5763 return;
5764 end if;
5766 -- If the semantic analyzer has determined that aggregate N will raise
5767 -- Constraint_Error at run time, then the aggregate node has been
5768 -- replaced with an N_Raise_Constraint_Error node and we should
5769 -- never get here.
5771 pragma Assert (not Raises_Constraint_Error (N));
5773 -- STEP 1a
5775 -- Check that the index range defined by aggregate bounds is
5776 -- compatible with corresponding index subtype.
5778 Index_Compatibility_Check : declare
5779 Aggr_Index_Range : Node_Id := First_Index (Typ);
5780 -- The current aggregate index range
5782 Index_Constraint : Node_Id := First_Index (Etype (Typ));
5783 -- The corresponding index constraint against which we have to
5784 -- check the above aggregate index range.
5786 begin
5787 Compute_Others_Present (N, 1);
5789 for J in 1 .. Aggr_Dimension loop
5790 -- There is no need to emit a check if an others choice is present
5791 -- for this array aggregate dimension since in this case one of
5792 -- N's subaggregates has taken its bounds from the context and
5793 -- these bounds must have been checked already. In addition all
5794 -- subaggregates corresponding to the same dimension must all have
5795 -- the same bounds (checked in (c) below).
5797 if not Range_Checks_Suppressed (Etype (Index_Constraint))
5798 and then not Others_Present (J)
5799 then
5800 -- We don't use Checks.Apply_Range_Check here because it emits
5801 -- a spurious check. Namely it checks that the range defined by
5802 -- the aggregate bounds is nonempty. But we know this already
5803 -- if we get here.
5805 Check_Bounds (Aggr_Index_Range, Index_Constraint);
5806 end if;
5808 -- Save the low and high bounds of the aggregate index as well as
5809 -- the index type for later use in checks (b) and (c) below.
5811 Aggr_Low (J) := Low_Bound (Aggr_Index_Range);
5812 Aggr_High (J) := High_Bound (Aggr_Index_Range);
5814 Aggr_Index_Typ (J) := Etype (Index_Constraint);
5816 Next_Index (Aggr_Index_Range);
5817 Next_Index (Index_Constraint);
5818 end loop;
5819 end Index_Compatibility_Check;
5821 -- STEP 1b
5823 -- If an others choice is present check that no aggregate index is
5824 -- outside the bounds of the index constraint.
5826 Others_Check (N, 1);
5828 -- STEP 1c
5830 -- For multidimensional arrays make sure that all subaggregates
5831 -- corresponding to the same dimension have the same bounds.
5833 if Aggr_Dimension > 1 then
5834 Check_Same_Aggr_Bounds (N, 1);
5835 end if;
5837 -- STEP 1d
5839 -- If we have a default component value, or simple initialization is
5840 -- required for the component type, then we replace <> in component
5841 -- associations by the required default value.
5843 declare
5844 Default_Val : Node_Id;
5845 Assoc : Node_Id;
5847 begin
5848 if (Present (Default_Aspect_Component_Value (Typ))
5849 or else Needs_Simple_Initialization (Ctyp))
5850 and then Present (Component_Associations (N))
5851 then
5852 Assoc := First (Component_Associations (N));
5853 while Present (Assoc) loop
5854 if Nkind (Assoc) = N_Component_Association
5855 and then Box_Present (Assoc)
5856 then
5857 Set_Box_Present (Assoc, False);
5859 if Present (Default_Aspect_Component_Value (Typ)) then
5860 Default_Val := Default_Aspect_Component_Value (Typ);
5861 else
5862 Default_Val := Get_Simple_Init_Val (Ctyp, N);
5863 end if;
5865 Set_Expression (Assoc, New_Copy_Tree (Default_Val));
5866 Analyze_And_Resolve (Expression (Assoc), Ctyp);
5867 end if;
5869 Next (Assoc);
5870 end loop;
5871 end if;
5872 end;
5874 -- STEP 2
5876 -- Here we test for is packed array aggregate that we can handle at
5877 -- compile time. If so, return with transformation done. Note that we do
5878 -- this even if the aggregate is nested, because once we have done this
5879 -- processing, there is no more nested aggregate.
5881 if Packed_Array_Aggregate_Handled (N) then
5882 return;
5883 end if;
5885 -- At this point we try to convert to positional form
5887 if Ekind (Current_Scope) = E_Package
5888 and then Static_Elaboration_Desired (Current_Scope)
5889 then
5890 Convert_To_Positional (N, Max_Others_Replicate => 100);
5891 else
5892 Convert_To_Positional (N);
5893 end if;
5895 -- if the result is no longer an aggregate (e.g. it may be a string
5896 -- literal, or a temporary which has the needed value), then we are
5897 -- done, since there is no longer a nested aggregate.
5899 if Nkind (N) /= N_Aggregate then
5900 return;
5902 -- We are also done if the result is an analyzed aggregate, indicating
5903 -- that Convert_To_Positional succeeded and reanalyzed the rewritten
5904 -- aggregate.
5906 elsif Analyzed (N) and then N /= Original_Node (N) then
5907 return;
5908 end if;
5910 -- If all aggregate components are compile-time known and the aggregate
5911 -- has been flattened, nothing left to do. The same occurs if the
5912 -- aggregate is used to initialize the components of a statically
5913 -- allocated dispatch table.
5915 if Compile_Time_Known_Aggregate (N)
5916 or else Is_Static_Dispatch_Table_Aggregate (N)
5917 then
5918 Set_Expansion_Delayed (N, False);
5919 return;
5920 end if;
5922 -- Now see if back end processing is possible
5924 if Backend_Processing_Possible (N) then
5926 -- If the aggregate is static but the constraints are not, build
5927 -- a static subtype for the aggregate, so that Gigi can place it
5928 -- in static memory. Perform an unchecked_conversion to the non-
5929 -- static type imposed by the context.
5931 declare
5932 Itype : constant Entity_Id := Etype (N);
5933 Index : Node_Id;
5934 Needs_Type : Boolean := False;
5936 begin
5937 Index := First_Index (Itype);
5938 while Present (Index) loop
5939 if not Is_OK_Static_Subtype (Etype (Index)) then
5940 Needs_Type := True;
5941 exit;
5942 else
5943 Next_Index (Index);
5944 end if;
5945 end loop;
5947 if Needs_Type then
5948 Build_Constrained_Type (Positional => True);
5949 Rewrite (N, Unchecked_Convert_To (Itype, N));
5950 Analyze (N);
5951 end if;
5952 end;
5954 return;
5955 end if;
5957 -- STEP 3
5959 -- Delay expansion for nested aggregates: it will be taken care of when
5960 -- the parent aggregate is expanded.
5962 Parent_Node := Parent (N);
5963 Parent_Kind := Nkind (Parent_Node);
5965 if Parent_Kind = N_Qualified_Expression then
5966 Parent_Node := Parent (Parent_Node);
5967 Parent_Kind := Nkind (Parent_Node);
5968 end if;
5970 if Parent_Kind = N_Aggregate
5971 or else Parent_Kind = N_Extension_Aggregate
5972 or else Parent_Kind = N_Component_Association
5973 or else (Parent_Kind = N_Object_Declaration
5974 and then Needs_Finalization (Typ))
5975 or else (Parent_Kind = N_Assignment_Statement
5976 and then Inside_Init_Proc)
5977 then
5978 if Static_Array_Aggregate (N)
5979 or else Compile_Time_Known_Aggregate (N)
5980 then
5981 Set_Expansion_Delayed (N, False);
5982 return;
5983 else
5984 Set_Expansion_Delayed (N);
5985 return;
5986 end if;
5987 end if;
5989 -- STEP 4
5991 -- Look if in place aggregate expansion is possible
5993 -- For object declarations we build the aggregate in place, unless
5994 -- the array is bit-packed or the component is controlled.
5996 -- For assignments we do the assignment in place if all the component
5997 -- associations have compile-time known values. For other cases we
5998 -- create a temporary. The analysis for safety of on-line assignment
5999 -- is delicate, i.e. we don't know how to do it fully yet ???
6001 -- For allocators we assign to the designated object in place if the
6002 -- aggregate meets the same conditions as other in-place assignments.
6003 -- In this case the aggregate may not come from source but was created
6004 -- for default initialization, e.g. with Initialize_Scalars.
6006 if Requires_Transient_Scope (Typ) then
6007 Establish_Transient_Scope
6008 (N, Sec_Stack => Has_Controlled_Component (Typ));
6009 end if;
6011 if Has_Default_Init_Comps (N) then
6012 Maybe_In_Place_OK := False;
6014 elsif Is_Bit_Packed_Array (Typ)
6015 or else Has_Controlled_Component (Typ)
6016 then
6017 Maybe_In_Place_OK := False;
6019 else
6020 Maybe_In_Place_OK :=
6021 (Nkind (Parent (N)) = N_Assignment_Statement
6022 and then In_Place_Assign_OK)
6024 or else
6025 (Nkind (Parent (Parent (N))) = N_Allocator
6026 and then In_Place_Assign_OK);
6027 end if;
6029 -- If this is an array of tasks, it will be expanded into build-in-place
6030 -- assignments. Build an activation chain for the tasks now.
6032 if Has_Task (Etype (N)) then
6033 Build_Activation_Chain_Entity (N);
6034 end if;
6036 -- Perform in-place expansion of aggregate in an object declaration.
6037 -- Note: actions generated for the aggregate will be captured in an
6038 -- expression-with-actions statement so that they can be transferred
6039 -- to freeze actions later if there is an address clause for the
6040 -- object. (Note: we don't use a block statement because this would
6041 -- cause generated freeze nodes to be elaborated in the wrong scope).
6043 -- Do not perform in-place expansion for SPARK 05 because aggregates are
6044 -- expected to appear in qualified form. In-place expansion eliminates
6045 -- the qualification and eventually violates this SPARK 05 restiction.
6047 -- Should document the rest of the guards ???
6049 if not Has_Default_Init_Comps (N)
6050 and then Comes_From_Source (Parent_Node)
6051 and then Parent_Kind = N_Object_Declaration
6052 and then Present (Expression (Parent_Node))
6053 and then not
6054 Must_Slide (Etype (Defining_Identifier (Parent_Node)), Typ)
6055 and then not Has_Controlled_Component (Typ)
6056 and then not Is_Bit_Packed_Array (Typ)
6057 and then not Restriction_Check_Required (SPARK_05)
6058 then
6059 In_Place_Assign_OK_For_Declaration := True;
6060 Tmp := Defining_Identifier (Parent_Node);
6061 Set_No_Initialization (Parent_Node);
6062 Set_Expression (Parent_Node, Empty);
6064 -- Set kind and type of the entity, for use in the analysis
6065 -- of the subsequent assignments. If the nominal type is not
6066 -- constrained, build a subtype from the known bounds of the
6067 -- aggregate. If the declaration has a subtype mark, use it,
6068 -- otherwise use the itype of the aggregate.
6070 Set_Ekind (Tmp, E_Variable);
6072 if not Is_Constrained (Typ) then
6073 Build_Constrained_Type (Positional => False);
6075 elsif Is_Entity_Name (Object_Definition (Parent_Node))
6076 and then Is_Constrained (Entity (Object_Definition (Parent_Node)))
6077 then
6078 Set_Etype (Tmp, Entity (Object_Definition (Parent_Node)));
6080 else
6081 Set_Size_Known_At_Compile_Time (Typ, False);
6082 Set_Etype (Tmp, Typ);
6083 end if;
6085 elsif Maybe_In_Place_OK
6086 and then Nkind (Parent (N)) = N_Qualified_Expression
6087 and then Nkind (Parent (Parent (N))) = N_Allocator
6088 then
6089 Set_Expansion_Delayed (N);
6090 return;
6092 -- In the remaining cases the aggregate is the RHS of an assignment
6094 elsif Maybe_In_Place_OK
6095 and then Safe_Left_Hand_Side (Name (Parent (N)))
6096 then
6097 Tmp := Name (Parent (N));
6099 if Etype (Tmp) /= Etype (N) then
6100 Apply_Length_Check (N, Etype (Tmp));
6102 if Nkind (N) = N_Raise_Constraint_Error then
6104 -- Static error, nothing further to expand
6106 return;
6107 end if;
6108 end if;
6110 -- If a slice assignment has an aggregate with a single others_choice,
6111 -- the assignment can be done in place even if bounds are not static,
6112 -- by converting it into a loop over the discrete range of the slice.
6114 elsif Maybe_In_Place_OK
6115 and then Nkind (Name (Parent (N))) = N_Slice
6116 and then Is_Others_Aggregate (N)
6117 then
6118 Tmp := Name (Parent (N));
6120 -- Set type of aggregate to be type of lhs in assignment, in order
6121 -- to suppress redundant length checks.
6123 Set_Etype (N, Etype (Tmp));
6125 -- Step 5
6127 -- In place aggregate expansion is not possible
6129 else
6130 Maybe_In_Place_OK := False;
6131 Tmp := Make_Temporary (Loc, 'A', N);
6132 Tmp_Decl :=
6133 Make_Object_Declaration (Loc,
6134 Defining_Identifier => Tmp,
6135 Object_Definition => New_Occurrence_Of (Typ, Loc));
6136 Set_No_Initialization (Tmp_Decl, True);
6138 -- If we are within a loop, the temporary will be pushed on the
6139 -- stack at each iteration. If the aggregate is the expression for an
6140 -- allocator, it will be immediately copied to the heap and can
6141 -- be reclaimed at once. We create a transient scope around the
6142 -- aggregate for this purpose.
6144 if Ekind (Current_Scope) = E_Loop
6145 and then Nkind (Parent (Parent (N))) = N_Allocator
6146 then
6147 Establish_Transient_Scope (N, False);
6148 end if;
6150 Insert_Action (N, Tmp_Decl);
6151 end if;
6153 -- Construct and insert the aggregate code. We can safely suppress index
6154 -- checks because this code is guaranteed not to raise CE on index
6155 -- checks. However we should *not* suppress all checks.
6157 declare
6158 Target : Node_Id;
6160 begin
6161 if Nkind (Tmp) = N_Defining_Identifier then
6162 Target := New_Occurrence_Of (Tmp, Loc);
6164 else
6165 if Has_Default_Init_Comps (N) then
6167 -- Ada 2005 (AI-287): This case has not been analyzed???
6169 raise Program_Error;
6170 end if;
6172 -- Name in assignment is explicit dereference
6174 Target := New_Copy (Tmp);
6175 end if;
6177 -- If we are to generate an in place assignment for a declaration or
6178 -- an assignment statement, and the assignment can be done directly
6179 -- by the back end, then do not expand further.
6181 -- ??? We can also do that if in place expansion is not possible but
6182 -- then we could go into an infinite recursion.
6184 if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK)
6185 and then not AAMP_On_Target
6186 and then not CodePeer_Mode
6187 and then not Generate_C_Code
6188 and then not Possible_Bit_Aligned_Component (Target)
6189 and then not Is_Possibly_Unaligned_Slice (Target)
6190 and then Aggr_Assignment_OK_For_Backend (N)
6191 then
6192 if Maybe_In_Place_OK then
6193 return;
6194 end if;
6196 Aggr_Code :=
6197 New_List (
6198 Make_Assignment_Statement (Loc,
6199 Name => Target,
6200 Expression => New_Copy (N)));
6202 else
6203 Aggr_Code :=
6204 Build_Array_Aggr_Code (N,
6205 Ctype => Ctyp,
6206 Index => First_Index (Typ),
6207 Into => Target,
6208 Scalar_Comp => Is_Scalar_Type (Ctyp));
6209 end if;
6211 -- Save the last assignment statement associated with the aggregate
6212 -- when building a controlled object. This reference is utilized by
6213 -- the finalization machinery when marking an object as successfully
6214 -- initialized.
6216 if Needs_Finalization (Typ)
6217 and then Is_Entity_Name (Target)
6218 and then Present (Entity (Target))
6219 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
6220 then
6221 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
6222 end if;
6223 end;
6225 -- If the aggregate is the expression in a declaration, the expanded
6226 -- code must be inserted after it. The defining entity might not come
6227 -- from source if this is part of an inlined body, but the declaration
6228 -- itself will.
6230 if Comes_From_Source (Tmp)
6231 or else
6232 (Nkind (Parent (N)) = N_Object_Declaration
6233 and then Comes_From_Source (Parent (N))
6234 and then Tmp = Defining_Entity (Parent (N)))
6235 then
6236 declare
6237 Node_After : constant Node_Id := Next (Parent_Node);
6239 begin
6240 Insert_Actions_After (Parent_Node, Aggr_Code);
6242 if Parent_Kind = N_Object_Declaration then
6243 Collect_Initialization_Statements
6244 (Obj => Tmp, N => Parent_Node, Node_After => Node_After);
6245 end if;
6246 end;
6248 else
6249 Insert_Actions (N, Aggr_Code);
6250 end if;
6252 -- If the aggregate has been assigned in place, remove the original
6253 -- assignment.
6255 if Nkind (Parent (N)) = N_Assignment_Statement
6256 and then Maybe_In_Place_OK
6257 then
6258 Rewrite (Parent (N), Make_Null_Statement (Loc));
6260 elsif Nkind (Parent (N)) /= N_Object_Declaration
6261 or else Tmp /= Defining_Identifier (Parent (N))
6262 then
6263 Rewrite (N, New_Occurrence_Of (Tmp, Loc));
6264 Analyze_And_Resolve (N, Typ);
6265 end if;
6266 end Expand_Array_Aggregate;
6268 ------------------------
6269 -- Expand_N_Aggregate --
6270 ------------------------
6272 procedure Expand_N_Aggregate (N : Node_Id) is
6273 begin
6274 -- Record aggregate case
6276 if Is_Record_Type (Etype (N)) then
6277 Expand_Record_Aggregate (N);
6279 -- Array aggregate case
6281 else
6282 -- A special case, if we have a string subtype with bounds 1 .. N,
6283 -- where N is known at compile time, and the aggregate is of the
6284 -- form (others => 'x'), with a single choice and no expressions,
6285 -- and N is less than 80 (an arbitrary limit for now), then replace
6286 -- the aggregate by the equivalent string literal (but do not mark
6287 -- it as static since it is not).
6289 -- Note: this entire circuit is redundant with respect to code in
6290 -- Expand_Array_Aggregate that collapses others choices to positional
6291 -- form, but there are two problems with that circuit:
6293 -- a) It is limited to very small cases due to ill-understood
6294 -- interactions with bootstrapping. That limit is removed by
6295 -- use of the No_Implicit_Loops restriction.
6297 -- b) It incorrectly ends up with the resulting expressions being
6298 -- considered static when they are not. For example, the
6299 -- following test should fail:
6301 -- pragma Restrictions (No_Implicit_Loops);
6302 -- package NonSOthers4 is
6303 -- B : constant String (1 .. 6) := (others => 'A');
6304 -- DH : constant String (1 .. 8) := B & "BB";
6305 -- X : Integer;
6306 -- pragma Export (C, X, Link_Name => DH);
6307 -- end;
6309 -- But it succeeds (DH looks static to pragma Export)
6311 -- To be sorted out ???
6313 if Present (Component_Associations (N)) then
6314 declare
6315 CA : constant Node_Id := First (Component_Associations (N));
6316 MX : constant := 80;
6318 begin
6319 if Nkind (First (Choices (CA))) = N_Others_Choice
6320 and then Nkind (Expression (CA)) = N_Character_Literal
6321 and then No (Expressions (N))
6322 then
6323 declare
6324 T : constant Entity_Id := Etype (N);
6325 X : constant Node_Id := First_Index (T);
6326 EC : constant Node_Id := Expression (CA);
6327 CV : constant Uint := Char_Literal_Value (EC);
6328 CC : constant Int := UI_To_Int (CV);
6330 begin
6331 if Nkind (X) = N_Range
6332 and then Compile_Time_Known_Value (Low_Bound (X))
6333 and then Expr_Value (Low_Bound (X)) = 1
6334 and then Compile_Time_Known_Value (High_Bound (X))
6335 then
6336 declare
6337 Hi : constant Uint := Expr_Value (High_Bound (X));
6339 begin
6340 if Hi <= MX then
6341 Start_String;
6343 for J in 1 .. UI_To_Int (Hi) loop
6344 Store_String_Char (Char_Code (CC));
6345 end loop;
6347 Rewrite (N,
6348 Make_String_Literal (Sloc (N),
6349 Strval => End_String));
6351 if CC >= Int (2 ** 16) then
6352 Set_Has_Wide_Wide_Character (N);
6353 elsif CC >= Int (2 ** 8) then
6354 Set_Has_Wide_Character (N);
6355 end if;
6357 Analyze_And_Resolve (N, T);
6358 Set_Is_Static_Expression (N, False);
6359 return;
6360 end if;
6361 end;
6362 end if;
6363 end;
6364 end if;
6365 end;
6366 end if;
6368 -- Not that special case, so normal expansion of array aggregate
6370 Expand_Array_Aggregate (N);
6371 end if;
6373 exception
6374 when RE_Not_Available =>
6375 return;
6376 end Expand_N_Aggregate;
6378 ----------------------------------
6379 -- Expand_N_Extension_Aggregate --
6380 ----------------------------------
6382 -- If the ancestor part is an expression, add a component association for
6383 -- the parent field. If the type of the ancestor part is not the direct
6384 -- parent of the expected type, build recursively the needed ancestors.
6385 -- If the ancestor part is a subtype_mark, replace aggregate with a decla-
6386 -- ration for a temporary of the expected type, followed by individual
6387 -- assignments to the given components.
6389 procedure Expand_N_Extension_Aggregate (N : Node_Id) is
6390 Loc : constant Source_Ptr := Sloc (N);
6391 A : constant Node_Id := Ancestor_Part (N);
6392 Typ : constant Entity_Id := Etype (N);
6394 begin
6395 -- If the ancestor is a subtype mark, an init proc must be called
6396 -- on the resulting object which thus has to be materialized in
6397 -- the front-end
6399 if Is_Entity_Name (A) and then Is_Type (Entity (A)) then
6400 Convert_To_Assignments (N, Typ);
6402 -- The extension aggregate is transformed into a record aggregate
6403 -- of the following form (c1 and c2 are inherited components)
6405 -- (Exp with c3 => a, c4 => b)
6406 -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b)
6408 else
6409 Set_Etype (N, Typ);
6411 if Tagged_Type_Expansion then
6412 Expand_Record_Aggregate (N,
6413 Orig_Tag =>
6414 New_Occurrence_Of
6415 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc),
6416 Parent_Expr => A);
6418 -- No tag is needed in the case of a VM
6420 else
6421 Expand_Record_Aggregate (N, Parent_Expr => A);
6422 end if;
6423 end if;
6425 exception
6426 when RE_Not_Available =>
6427 return;
6428 end Expand_N_Extension_Aggregate;
6430 -----------------------------
6431 -- Expand_Record_Aggregate --
6432 -----------------------------
6434 procedure Expand_Record_Aggregate
6435 (N : Node_Id;
6436 Orig_Tag : Node_Id := Empty;
6437 Parent_Expr : Node_Id := Empty)
6439 Loc : constant Source_Ptr := Sloc (N);
6440 Comps : constant List_Id := Component_Associations (N);
6441 Typ : constant Entity_Id := Etype (N);
6442 Base_Typ : constant Entity_Id := Base_Type (Typ);
6444 Static_Components : Boolean := True;
6445 -- Flag to indicate whether all components are compile-time known,
6446 -- and the aggregate can be constructed statically and handled by
6447 -- the back-end.
6449 function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean;
6450 -- Returns true if N is an expression of composite type which can be
6451 -- fully evaluated at compile time without raising constraint error.
6452 -- Such expressions can be passed as is to Gigi without any expansion.
6454 -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate
6455 -- set and constants whose expression is such an aggregate, recursively.
6457 function Component_Not_OK_For_Backend return Boolean;
6458 -- Check for presence of a component which makes it impossible for the
6459 -- backend to process the aggregate, thus requiring the use of a series
6460 -- of assignment statements. Cases checked for are a nested aggregate
6461 -- needing Late_Expansion, the presence of a tagged component which may
6462 -- need tag adjustment, and a bit unaligned component reference.
6464 -- We also force expansion into assignments if a component is of a
6465 -- mutable type (including a private type with discriminants) because
6466 -- in that case the size of the component to be copied may be smaller
6467 -- than the side of the target, and there is no simple way for gigi
6468 -- to compute the size of the object to be copied.
6470 -- NOTE: This is part of the ongoing work to define precisely the
6471 -- interface between front-end and back-end handling of aggregates.
6472 -- In general it is desirable to pass aggregates as they are to gigi,
6473 -- in order to minimize elaboration code. This is one case where the
6474 -- semantics of Ada complicate the analysis and lead to anomalies in
6475 -- the gcc back-end if the aggregate is not expanded into assignments.
6477 function Has_Per_Object_Constraint (L : List_Id) return Boolean;
6478 -- Return True if any element of L has Has_Per_Object_Constraint set.
6479 -- L should be the Choices component of an N_Component_Association.
6481 function Has_Visible_Private_Ancestor (Id : E) return Boolean;
6482 -- If any ancestor of the current type is private, the aggregate
6483 -- cannot be built in place. We cannot rely on Has_Private_Ancestor,
6484 -- because it will not be set when type and its parent are in the
6485 -- same scope, and the parent component needs expansion.
6487 function Top_Level_Aggregate (N : Node_Id) return Node_Id;
6488 -- For nested aggregates return the ultimate enclosing aggregate; for
6489 -- non-nested aggregates return N.
6491 ----------------------------------------
6492 -- Compile_Time_Known_Composite_Value --
6493 ----------------------------------------
6495 function Compile_Time_Known_Composite_Value
6496 (N : Node_Id) return Boolean
6498 begin
6499 -- If we have an entity name, then see if it is the name of a
6500 -- constant and if so, test the corresponding constant value.
6502 if Is_Entity_Name (N) then
6503 declare
6504 E : constant Entity_Id := Entity (N);
6505 V : Node_Id;
6506 begin
6507 if Ekind (E) /= E_Constant then
6508 return False;
6509 else
6510 V := Constant_Value (E);
6511 return Present (V)
6512 and then Compile_Time_Known_Composite_Value (V);
6513 end if;
6514 end;
6516 -- We have a value, see if it is compile time known
6518 else
6519 if Nkind (N) = N_Aggregate then
6520 return Compile_Time_Known_Aggregate (N);
6521 end if;
6523 -- All other types of values are not known at compile time
6525 return False;
6526 end if;
6528 end Compile_Time_Known_Composite_Value;
6530 ----------------------------------
6531 -- Component_Not_OK_For_Backend --
6532 ----------------------------------
6534 function Component_Not_OK_For_Backend return Boolean is
6535 C : Node_Id;
6536 Expr_Q : Node_Id;
6538 begin
6539 if No (Comps) then
6540 return False;
6541 end if;
6543 C := First (Comps);
6544 while Present (C) loop
6546 -- If the component has box initialization, expansion is needed
6547 -- and component is not ready for backend.
6549 if Box_Present (C) then
6550 return True;
6551 end if;
6553 if Nkind (Expression (C)) = N_Qualified_Expression then
6554 Expr_Q := Expression (Expression (C));
6555 else
6556 Expr_Q := Expression (C);
6557 end if;
6559 -- Return true if the aggregate has any associations for tagged
6560 -- components that may require tag adjustment.
6562 -- These are cases where the source expression may have a tag that
6563 -- could differ from the component tag (e.g., can occur for type
6564 -- conversions and formal parameters). (Tag adjustment not needed
6565 -- if Tagged_Type_Expansion because object tags are implicit in
6566 -- the machine.)
6568 if Is_Tagged_Type (Etype (Expr_Q))
6569 and then (Nkind (Expr_Q) = N_Type_Conversion
6570 or else (Is_Entity_Name (Expr_Q)
6571 and then
6572 Ekind (Entity (Expr_Q)) in Formal_Kind))
6573 and then Tagged_Type_Expansion
6574 then
6575 Static_Components := False;
6576 return True;
6578 elsif Is_Delayed_Aggregate (Expr_Q) then
6579 Static_Components := False;
6580 return True;
6582 elsif Possible_Bit_Aligned_Component (Expr_Q) then
6583 Static_Components := False;
6584 return True;
6586 elsif Modify_Tree_For_C
6587 and then Nkind (C) = N_Component_Association
6588 and then Has_Per_Object_Constraint (Choices (C))
6589 then
6590 Static_Components := False;
6591 return True;
6593 elsif Modify_Tree_For_C
6594 and then Nkind (Expr_Q) = N_Identifier
6595 and then Is_Array_Type (Etype (Expr_Q))
6596 then
6597 Static_Components := False;
6598 return True;
6599 end if;
6601 if Is_Elementary_Type (Etype (Expr_Q)) then
6602 if not Compile_Time_Known_Value (Expr_Q) then
6603 Static_Components := False;
6604 end if;
6606 elsif not Compile_Time_Known_Composite_Value (Expr_Q) then
6607 Static_Components := False;
6609 if Is_Private_Type (Etype (Expr_Q))
6610 and then Has_Discriminants (Etype (Expr_Q))
6611 then
6612 return True;
6613 end if;
6614 end if;
6616 Next (C);
6617 end loop;
6619 return False;
6620 end Component_Not_OK_For_Backend;
6622 -------------------------------
6623 -- Has_Per_Object_Constraint --
6624 -------------------------------
6626 function Has_Per_Object_Constraint (L : List_Id) return Boolean is
6627 N : Node_Id := First (L);
6628 begin
6629 while Present (N) loop
6630 if Is_Entity_Name (N)
6631 and then Present (Entity (N))
6632 and then Has_Per_Object_Constraint (Entity (N))
6633 then
6634 return True;
6635 end if;
6637 Next (N);
6638 end loop;
6640 return False;
6641 end Has_Per_Object_Constraint;
6643 -----------------------------------
6644 -- Has_Visible_Private_Ancestor --
6645 -----------------------------------
6647 function Has_Visible_Private_Ancestor (Id : E) return Boolean is
6648 R : constant Entity_Id := Root_Type (Id);
6649 T1 : Entity_Id := Id;
6651 begin
6652 loop
6653 if Is_Private_Type (T1) then
6654 return True;
6656 elsif T1 = R then
6657 return False;
6659 else
6660 T1 := Etype (T1);
6661 end if;
6662 end loop;
6663 end Has_Visible_Private_Ancestor;
6665 -------------------------
6666 -- Top_Level_Aggregate --
6667 -------------------------
6669 function Top_Level_Aggregate (N : Node_Id) return Node_Id is
6670 Aggr : Node_Id;
6672 begin
6673 Aggr := N;
6674 while Present (Parent (Aggr))
6675 and then Nkind_In (Parent (Aggr), N_Component_Association,
6676 N_Aggregate)
6677 loop
6678 Aggr := Parent (Aggr);
6679 end loop;
6681 return Aggr;
6682 end Top_Level_Aggregate;
6684 -- Local variables
6686 Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N);
6687 Tag_Value : Node_Id;
6688 Comp : Entity_Id;
6689 New_Comp : Node_Id;
6691 -- Start of processing for Expand_Record_Aggregate
6693 begin
6694 -- If the aggregate is to be assigned to an atomic/VFA variable, we have
6695 -- to prevent a piecemeal assignment even if the aggregate is to be
6696 -- expanded. We create a temporary for the aggregate, and assign the
6697 -- temporary instead, so that the back end can generate an atomic move
6698 -- for it.
6700 if Is_Atomic_VFA_Aggregate (N) then
6701 return;
6703 -- No special management required for aggregates used to initialize
6704 -- statically allocated dispatch tables
6706 elsif Is_Static_Dispatch_Table_Aggregate (N) then
6707 return;
6708 end if;
6710 -- Ada 2005 (AI-318-2): We need to convert to assignments if components
6711 -- are build-in-place function calls. The assignments will each turn
6712 -- into a build-in-place function call. If components are all static,
6713 -- we can pass the aggregate to the backend regardless of limitedness.
6715 -- Extension aggregates, aggregates in extended return statements, and
6716 -- aggregates for C++ imported types must be expanded.
6718 if Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then
6719 if not Nkind_In (Parent (N), N_Object_Declaration,
6720 N_Component_Association)
6721 then
6722 Convert_To_Assignments (N, Typ);
6724 elsif Nkind (N) = N_Extension_Aggregate
6725 or else Convention (Typ) = Convention_CPP
6726 then
6727 Convert_To_Assignments (N, Typ);
6729 elsif not Size_Known_At_Compile_Time (Typ)
6730 or else Component_Not_OK_For_Backend
6731 or else not Static_Components
6732 then
6733 Convert_To_Assignments (N, Typ);
6735 else
6736 Set_Compile_Time_Known_Aggregate (N);
6737 Set_Expansion_Delayed (N, False);
6738 end if;
6740 -- Gigi doesn't properly handle temporaries of variable size so we
6741 -- generate it in the front-end
6743 elsif not Size_Known_At_Compile_Time (Typ)
6744 and then Tagged_Type_Expansion
6745 then
6746 Convert_To_Assignments (N, Typ);
6748 -- An aggregate used to initialize a controlled object must be turned
6749 -- into component assignments as the components themselves may require
6750 -- finalization actions such as adjustment.
6752 elsif Needs_Finalization (Typ) then
6753 Convert_To_Assignments (N, Typ);
6755 -- Ada 2005 (AI-287): In case of default initialized components we
6756 -- convert the aggregate into assignments.
6758 elsif Has_Default_Init_Comps (N) then
6759 Convert_To_Assignments (N, Typ);
6761 -- Check components
6763 elsif Component_Not_OK_For_Backend then
6764 Convert_To_Assignments (N, Typ);
6766 -- If an ancestor is private, some components are not inherited and we
6767 -- cannot expand into a record aggregate.
6769 elsif Has_Visible_Private_Ancestor (Typ) then
6770 Convert_To_Assignments (N, Typ);
6772 -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi
6773 -- is not able to handle the aggregate for Late_Request.
6775 elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then
6776 Convert_To_Assignments (N, Typ);
6778 -- If the tagged types covers interface types we need to initialize all
6779 -- hidden components containing pointers to secondary dispatch tables.
6781 elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then
6782 Convert_To_Assignments (N, Typ);
6784 -- If some components are mutable, the size of the aggregate component
6785 -- may be distinct from the default size of the type component, so
6786 -- we need to expand to insure that the back-end copies the proper
6787 -- size of the data. However, if the aggregate is the initial value of
6788 -- a constant, the target is immutable and might be built statically
6789 -- if components are appropriate.
6791 elsif Has_Mutable_Components (Typ)
6792 and then
6793 (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration
6794 or else not Constant_Present (Parent (Top_Level_Aggr))
6795 or else not Static_Components)
6796 then
6797 Convert_To_Assignments (N, Typ);
6799 -- If the type involved has bit aligned components, then we are not sure
6800 -- that the back end can handle this case correctly.
6802 elsif Type_May_Have_Bit_Aligned_Components (Typ) then
6803 Convert_To_Assignments (N, Typ);
6805 -- When generating C, only generate an aggregate when declaring objects
6806 -- since C does not support aggregates in e.g. assignment statements.
6808 elsif Modify_Tree_For_C and then not In_Object_Declaration (N) then
6809 Convert_To_Assignments (N, Typ);
6811 -- In all other cases, build a proper aggregate to be handled by gigi
6813 else
6814 if Nkind (N) = N_Aggregate then
6816 -- If the aggregate is static and can be handled by the back-end,
6817 -- nothing left to do.
6819 if Static_Components then
6820 Set_Compile_Time_Known_Aggregate (N);
6821 Set_Expansion_Delayed (N, False);
6822 end if;
6823 end if;
6825 -- If no discriminants, nothing special to do
6827 if not Has_Discriminants (Typ) then
6828 null;
6830 -- Case of discriminants present
6832 elsif Is_Derived_Type (Typ) then
6834 -- For untagged types, non-stored discriminants are replaced
6835 -- with stored discriminants, which are the ones that gigi uses
6836 -- to describe the type and its components.
6838 Generate_Aggregate_For_Derived_Type : declare
6839 Constraints : constant List_Id := New_List;
6840 First_Comp : Node_Id;
6841 Discriminant : Entity_Id;
6842 Decl : Node_Id;
6843 Num_Disc : Nat := 0;
6844 Num_Gird : Nat := 0;
6846 procedure Prepend_Stored_Values (T : Entity_Id);
6847 -- Scan the list of stored discriminants of the type, and add
6848 -- their values to the aggregate being built.
6850 ---------------------------
6851 -- Prepend_Stored_Values --
6852 ---------------------------
6854 procedure Prepend_Stored_Values (T : Entity_Id) is
6855 begin
6856 Discriminant := First_Stored_Discriminant (T);
6857 while Present (Discriminant) loop
6858 New_Comp :=
6859 Make_Component_Association (Loc,
6860 Choices =>
6861 New_List (New_Occurrence_Of (Discriminant, Loc)),
6863 Expression =>
6864 New_Copy_Tree
6865 (Get_Discriminant_Value
6866 (Discriminant,
6867 Typ,
6868 Discriminant_Constraint (Typ))));
6870 if No (First_Comp) then
6871 Prepend_To (Component_Associations (N), New_Comp);
6872 else
6873 Insert_After (First_Comp, New_Comp);
6874 end if;
6876 First_Comp := New_Comp;
6877 Next_Stored_Discriminant (Discriminant);
6878 end loop;
6879 end Prepend_Stored_Values;
6881 -- Start of processing for Generate_Aggregate_For_Derived_Type
6883 begin
6884 -- Remove the associations for the discriminant of derived type
6886 First_Comp := First (Component_Associations (N));
6887 while Present (First_Comp) loop
6888 Comp := First_Comp;
6889 Next (First_Comp);
6891 if Ekind (Entity (First (Choices (Comp)))) = E_Discriminant
6892 then
6893 Remove (Comp);
6894 Num_Disc := Num_Disc + 1;
6895 end if;
6896 end loop;
6898 -- Insert stored discriminant associations in the correct
6899 -- order. If there are more stored discriminants than new
6900 -- discriminants, there is at least one new discriminant that
6901 -- constrains more than one of the stored discriminants. In
6902 -- this case we need to construct a proper subtype of the
6903 -- parent type, in order to supply values to all the
6904 -- components. Otherwise there is one-one correspondence
6905 -- between the constraints and the stored discriminants.
6907 First_Comp := Empty;
6909 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6910 while Present (Discriminant) loop
6911 Num_Gird := Num_Gird + 1;
6912 Next_Stored_Discriminant (Discriminant);
6913 end loop;
6915 -- Case of more stored discriminants than new discriminants
6917 if Num_Gird > Num_Disc then
6919 -- Create a proper subtype of the parent type, which is the
6920 -- proper implementation type for the aggregate, and convert
6921 -- it to the intended target type.
6923 Discriminant := First_Stored_Discriminant (Base_Type (Typ));
6924 while Present (Discriminant) loop
6925 New_Comp :=
6926 New_Copy_Tree
6927 (Get_Discriminant_Value
6928 (Discriminant,
6929 Typ,
6930 Discriminant_Constraint (Typ)));
6931 Append (New_Comp, Constraints);
6932 Next_Stored_Discriminant (Discriminant);
6933 end loop;
6935 Decl :=
6936 Make_Subtype_Declaration (Loc,
6937 Defining_Identifier => Make_Temporary (Loc, 'T'),
6938 Subtype_Indication =>
6939 Make_Subtype_Indication (Loc,
6940 Subtype_Mark =>
6941 New_Occurrence_Of (Etype (Base_Type (Typ)), Loc),
6942 Constraint =>
6943 Make_Index_Or_Discriminant_Constraint
6944 (Loc, Constraints)));
6946 Insert_Action (N, Decl);
6947 Prepend_Stored_Values (Base_Type (Typ));
6949 Set_Etype (N, Defining_Identifier (Decl));
6950 Set_Analyzed (N);
6952 Rewrite (N, Unchecked_Convert_To (Typ, N));
6953 Analyze (N);
6955 -- Case where we do not have fewer new discriminants than
6956 -- stored discriminants, so in this case we can simply use the
6957 -- stored discriminants of the subtype.
6959 else
6960 Prepend_Stored_Values (Typ);
6961 end if;
6962 end Generate_Aggregate_For_Derived_Type;
6963 end if;
6965 if Is_Tagged_Type (Typ) then
6967 -- In the tagged case, _parent and _tag component must be created
6969 -- Reset Null_Present unconditionally. Tagged records always have
6970 -- at least one field (the tag or the parent).
6972 Set_Null_Record_Present (N, False);
6974 -- When the current aggregate comes from the expansion of an
6975 -- extension aggregate, the parent expr is replaced by an
6976 -- aggregate formed by selected components of this expr.
6978 if Present (Parent_Expr) and then Is_Empty_List (Comps) then
6979 Comp := First_Component_Or_Discriminant (Typ);
6980 while Present (Comp) loop
6982 -- Skip all expander-generated components
6984 if not Comes_From_Source (Original_Record_Component (Comp))
6985 then
6986 null;
6988 else
6989 New_Comp :=
6990 Make_Selected_Component (Loc,
6991 Prefix =>
6992 Unchecked_Convert_To (Typ,
6993 Duplicate_Subexpr (Parent_Expr, True)),
6994 Selector_Name => New_Occurrence_Of (Comp, Loc));
6996 Append_To (Comps,
6997 Make_Component_Association (Loc,
6998 Choices =>
6999 New_List (New_Occurrence_Of (Comp, Loc)),
7000 Expression => New_Comp));
7002 Analyze_And_Resolve (New_Comp, Etype (Comp));
7003 end if;
7005 Next_Component_Or_Discriminant (Comp);
7006 end loop;
7007 end if;
7009 -- Compute the value for the Tag now, if the type is a root it
7010 -- will be included in the aggregate right away, otherwise it will
7011 -- be propagated to the parent aggregate.
7013 if Present (Orig_Tag) then
7014 Tag_Value := Orig_Tag;
7015 elsif not Tagged_Type_Expansion then
7016 Tag_Value := Empty;
7017 else
7018 Tag_Value :=
7019 New_Occurrence_Of
7020 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc);
7021 end if;
7023 -- For a derived type, an aggregate for the parent is formed with
7024 -- all the inherited components.
7026 if Is_Derived_Type (Typ) then
7028 declare
7029 First_Comp : Node_Id;
7030 Parent_Comps : List_Id;
7031 Parent_Aggr : Node_Id;
7032 Parent_Name : Node_Id;
7034 begin
7035 -- Remove the inherited component association from the
7036 -- aggregate and store them in the parent aggregate
7038 First_Comp := First (Component_Associations (N));
7039 Parent_Comps := New_List;
7040 while Present (First_Comp)
7041 and then
7042 Scope (Original_Record_Component
7043 (Entity (First (Choices (First_Comp))))) /=
7044 Base_Typ
7045 loop
7046 Comp := First_Comp;
7047 Next (First_Comp);
7048 Remove (Comp);
7049 Append (Comp, Parent_Comps);
7050 end loop;
7052 Parent_Aggr :=
7053 Make_Aggregate (Loc,
7054 Component_Associations => Parent_Comps);
7055 Set_Etype (Parent_Aggr, Etype (Base_Type (Typ)));
7057 -- Find the _parent component
7059 Comp := First_Component (Typ);
7060 while Chars (Comp) /= Name_uParent loop
7061 Comp := Next_Component (Comp);
7062 end loop;
7064 Parent_Name := New_Occurrence_Of (Comp, Loc);
7066 -- Insert the parent aggregate
7068 Prepend_To (Component_Associations (N),
7069 Make_Component_Association (Loc,
7070 Choices => New_List (Parent_Name),
7071 Expression => Parent_Aggr));
7073 -- Expand recursively the parent propagating the right Tag
7075 Expand_Record_Aggregate
7076 (Parent_Aggr, Tag_Value, Parent_Expr);
7078 -- The ancestor part may be a nested aggregate that has
7079 -- delayed expansion: recheck now.
7081 if Component_Not_OK_For_Backend then
7082 Convert_To_Assignments (N, Typ);
7083 end if;
7084 end;
7086 -- For a root type, the tag component is added (unless compiling
7087 -- for the VMs, where tags are implicit).
7089 elsif Tagged_Type_Expansion then
7090 declare
7091 Tag_Name : constant Node_Id :=
7092 New_Occurrence_Of (First_Tag_Component (Typ), Loc);
7093 Typ_Tag : constant Entity_Id := RTE (RE_Tag);
7094 Conv_Node : constant Node_Id :=
7095 Unchecked_Convert_To (Typ_Tag, Tag_Value);
7097 begin
7098 Set_Etype (Conv_Node, Typ_Tag);
7099 Prepend_To (Component_Associations (N),
7100 Make_Component_Association (Loc,
7101 Choices => New_List (Tag_Name),
7102 Expression => Conv_Node));
7103 end;
7104 end if;
7105 end if;
7106 end if;
7108 end Expand_Record_Aggregate;
7110 ----------------------------
7111 -- Has_Default_Init_Comps --
7112 ----------------------------
7114 function Has_Default_Init_Comps (N : Node_Id) return Boolean is
7115 Comps : constant List_Id := Component_Associations (N);
7116 C : Node_Id;
7117 Expr : Node_Id;
7119 begin
7120 pragma Assert (Nkind_In (N, N_Aggregate, N_Extension_Aggregate));
7122 if No (Comps) then
7123 return False;
7124 end if;
7126 if Has_Self_Reference (N) then
7127 return True;
7128 end if;
7130 -- Check if any direct component has default initialized components
7132 C := First (Comps);
7133 while Present (C) loop
7134 if Box_Present (C) then
7135 return True;
7136 end if;
7138 Next (C);
7139 end loop;
7141 -- Recursive call in case of aggregate expression
7143 C := First (Comps);
7144 while Present (C) loop
7145 Expr := Expression (C);
7147 if Present (Expr)
7148 and then Nkind_In (Expr, N_Aggregate, N_Extension_Aggregate)
7149 and then Has_Default_Init_Comps (Expr)
7150 then
7151 return True;
7152 end if;
7154 Next (C);
7155 end loop;
7157 return False;
7158 end Has_Default_Init_Comps;
7160 --------------------------
7161 -- Is_Delayed_Aggregate --
7162 --------------------------
7164 function Is_Delayed_Aggregate (N : Node_Id) return Boolean is
7165 Node : Node_Id := N;
7166 Kind : Node_Kind := Nkind (Node);
7168 begin
7169 if Kind = N_Qualified_Expression then
7170 Node := Expression (Node);
7171 Kind := Nkind (Node);
7172 end if;
7174 if not Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate) then
7175 return False;
7176 else
7177 return Expansion_Delayed (Node);
7178 end if;
7179 end Is_Delayed_Aggregate;
7181 ---------------------------
7182 -- In_Object_Declaration --
7183 ---------------------------
7185 function In_Object_Declaration (N : Node_Id) return Boolean is
7186 P : Node_Id := Parent (N);
7187 begin
7188 while Present (P) loop
7189 if Nkind (P) = N_Object_Declaration then
7190 return True;
7191 end if;
7193 P := Parent (P);
7194 end loop;
7196 return False;
7197 end In_Object_Declaration;
7199 ----------------------------------------
7200 -- Is_Static_Dispatch_Table_Aggregate --
7201 ----------------------------------------
7203 function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is
7204 Typ : constant Entity_Id := Base_Type (Etype (N));
7206 begin
7207 return Static_Dispatch_Tables
7208 and then Tagged_Type_Expansion
7209 and then RTU_Loaded (Ada_Tags)
7211 -- Avoid circularity when rebuilding the compiler
7213 and then Cunit_Entity (Get_Source_Unit (N)) /= RTU_Entity (Ada_Tags)
7214 and then (Typ = RTE (RE_Dispatch_Table_Wrapper)
7215 or else
7216 Typ = RTE (RE_Address_Array)
7217 or else
7218 Typ = RTE (RE_Type_Specific_Data)
7219 or else
7220 Typ = RTE (RE_Tag_Table)
7221 or else
7222 (RTE_Available (RE_Interface_Data)
7223 and then Typ = RTE (RE_Interface_Data))
7224 or else
7225 (RTE_Available (RE_Interfaces_Array)
7226 and then Typ = RTE (RE_Interfaces_Array))
7227 or else
7228 (RTE_Available (RE_Interface_Data_Element)
7229 and then Typ = RTE (RE_Interface_Data_Element)));
7230 end Is_Static_Dispatch_Table_Aggregate;
7232 -----------------------------
7233 -- Is_Two_Dim_Packed_Array --
7234 -----------------------------
7236 function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is
7237 C : constant Int := UI_To_Int (Component_Size (Typ));
7238 begin
7239 return Number_Dimensions (Typ) = 2
7240 and then Is_Bit_Packed_Array (Typ)
7241 and then (C = 1 or else C = 2 or else C = 4);
7242 end Is_Two_Dim_Packed_Array;
7244 --------------------
7245 -- Late_Expansion --
7246 --------------------
7248 function Late_Expansion
7249 (N : Node_Id;
7250 Typ : Entity_Id;
7251 Target : Node_Id) return List_Id
7253 Aggr_Code : List_Id;
7255 begin
7256 if Is_Array_Type (Etype (N)) then
7257 Aggr_Code :=
7258 Build_Array_Aggr_Code
7259 (N => N,
7260 Ctype => Component_Type (Etype (N)),
7261 Index => First_Index (Typ),
7262 Into => Target,
7263 Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)),
7264 Indexes => No_List);
7266 -- Directly or indirectly (e.g. access protected procedure) a record
7268 else
7269 Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target);
7270 end if;
7272 -- Save the last assignment statement associated with the aggregate
7273 -- when building a controlled object. This reference is utilized by
7274 -- the finalization machinery when marking an object as successfully
7275 -- initialized.
7277 if Needs_Finalization (Typ)
7278 and then Is_Entity_Name (Target)
7279 and then Present (Entity (Target))
7280 and then Ekind_In (Entity (Target), E_Constant, E_Variable)
7281 then
7282 Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code));
7283 end if;
7285 return Aggr_Code;
7286 end Late_Expansion;
7288 ----------------------------------
7289 -- Make_OK_Assignment_Statement --
7290 ----------------------------------
7292 function Make_OK_Assignment_Statement
7293 (Sloc : Source_Ptr;
7294 Name : Node_Id;
7295 Expression : Node_Id) return Node_Id
7297 begin
7298 Set_Assignment_OK (Name);
7299 return Make_Assignment_Statement (Sloc, Name, Expression);
7300 end Make_OK_Assignment_Statement;
7302 -----------------------
7303 -- Number_Of_Choices --
7304 -----------------------
7306 function Number_Of_Choices (N : Node_Id) return Nat is
7307 Assoc : Node_Id;
7308 Choice : Node_Id;
7310 Nb_Choices : Nat := 0;
7312 begin
7313 if Present (Expressions (N)) then
7314 return 0;
7315 end if;
7317 Assoc := First (Component_Associations (N));
7318 while Present (Assoc) loop
7319 Choice := First (Choices (Assoc));
7320 while Present (Choice) loop
7321 if Nkind (Choice) /= N_Others_Choice then
7322 Nb_Choices := Nb_Choices + 1;
7323 end if;
7325 Next (Choice);
7326 end loop;
7328 Next (Assoc);
7329 end loop;
7331 return Nb_Choices;
7332 end Number_Of_Choices;
7334 ------------------------------------
7335 -- Packed_Array_Aggregate_Handled --
7336 ------------------------------------
7338 -- The current version of this procedure will handle at compile time
7339 -- any array aggregate that meets these conditions:
7341 -- One and two dimensional, bit packed
7342 -- Underlying packed type is modular type
7343 -- Bounds are within 32-bit Int range
7344 -- All bounds and values are static
7346 -- Note: for now, in the 2-D case, we only handle component sizes of
7347 -- 1, 2, 4 (cases where an integral number of elements occupies a byte).
7349 function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is
7350 Loc : constant Source_Ptr := Sloc (N);
7351 Typ : constant Entity_Id := Etype (N);
7352 Ctyp : constant Entity_Id := Component_Type (Typ);
7354 Not_Handled : exception;
7355 -- Exception raised if this aggregate cannot be handled
7357 begin
7358 -- Handle one- or two dimensional bit packed array
7360 if not Is_Bit_Packed_Array (Typ)
7361 or else Number_Dimensions (Typ) > 2
7362 then
7363 return False;
7364 end if;
7366 -- If two-dimensional, check whether it can be folded, and transformed
7367 -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of
7368 -- the original type.
7370 if Number_Dimensions (Typ) = 2 then
7371 return Two_Dim_Packed_Array_Handled (N);
7372 end if;
7374 if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then
7375 return False;
7376 end if;
7378 if not Is_Scalar_Type (Component_Type (Typ))
7379 and then Has_Non_Standard_Rep (Component_Type (Typ))
7380 then
7381 return False;
7382 end if;
7384 declare
7385 Csiz : constant Nat := UI_To_Int (Component_Size (Typ));
7387 Lo : Node_Id;
7388 Hi : Node_Id;
7389 -- Bounds of index type
7391 Lob : Uint;
7392 Hib : Uint;
7393 -- Values of bounds if compile time known
7395 function Get_Component_Val (N : Node_Id) return Uint;
7396 -- Given a expression value N of the component type Ctyp, returns a
7397 -- value of Csiz (component size) bits representing this value. If
7398 -- the value is non-static or any other reason exists why the value
7399 -- cannot be returned, then Not_Handled is raised.
7401 -----------------------
7402 -- Get_Component_Val --
7403 -----------------------
7405 function Get_Component_Val (N : Node_Id) return Uint is
7406 Val : Uint;
7408 begin
7409 -- We have to analyze the expression here before doing any further
7410 -- processing here. The analysis of such expressions is deferred
7411 -- till expansion to prevent some problems of premature analysis.
7413 Analyze_And_Resolve (N, Ctyp);
7415 -- Must have a compile time value. String literals have to be
7416 -- converted into temporaries as well, because they cannot easily
7417 -- be converted into their bit representation.
7419 if not Compile_Time_Known_Value (N)
7420 or else Nkind (N) = N_String_Literal
7421 then
7422 raise Not_Handled;
7423 end if;
7425 Val := Expr_Rep_Value (N);
7427 -- Adjust for bias, and strip proper number of bits
7429 if Has_Biased_Representation (Ctyp) then
7430 Val := Val - Expr_Value (Type_Low_Bound (Ctyp));
7431 end if;
7433 return Val mod Uint_2 ** Csiz;
7434 end Get_Component_Val;
7436 -- Here we know we have a one dimensional bit packed array
7438 begin
7439 Get_Index_Bounds (First_Index (Typ), Lo, Hi);
7441 -- Cannot do anything if bounds are dynamic
7443 if not Compile_Time_Known_Value (Lo)
7444 or else
7445 not Compile_Time_Known_Value (Hi)
7446 then
7447 return False;
7448 end if;
7450 -- Or are silly out of range of int bounds
7452 Lob := Expr_Value (Lo);
7453 Hib := Expr_Value (Hi);
7455 if not UI_Is_In_Int_Range (Lob)
7456 or else
7457 not UI_Is_In_Int_Range (Hib)
7458 then
7459 return False;
7460 end if;
7462 -- At this stage we have a suitable aggregate for handling at compile
7463 -- time. The only remaining checks are that the values of expressions
7464 -- in the aggregate are compile-time known (checks are performed by
7465 -- Get_Component_Val), and that any subtypes or ranges are statically
7466 -- known.
7468 -- If the aggregate is not fully positional at this stage, then
7469 -- convert it to positional form. Either this will fail, in which
7470 -- case we can do nothing, or it will succeed, in which case we have
7471 -- succeeded in handling the aggregate and transforming it into a
7472 -- modular value, or it will stay an aggregate, in which case we
7473 -- have failed to create a packed value for it.
7475 if Present (Component_Associations (N)) then
7476 Convert_To_Positional
7477 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
7478 return Nkind (N) /= N_Aggregate;
7479 end if;
7481 -- Otherwise we are all positional, so convert to proper value
7483 declare
7484 Lov : constant Int := UI_To_Int (Lob);
7485 Hiv : constant Int := UI_To_Int (Hib);
7487 Len : constant Nat := Int'Max (0, Hiv - Lov + 1);
7488 -- The length of the array (number of elements)
7490 Aggregate_Val : Uint;
7491 -- Value of aggregate. The value is set in the low order bits of
7492 -- this value. For the little-endian case, the values are stored
7493 -- from low-order to high-order and for the big-endian case the
7494 -- values are stored from high-order to low-order. Note that gigi
7495 -- will take care of the conversions to left justify the value in
7496 -- the big endian case (because of left justified modular type
7497 -- processing), so we do not have to worry about that here.
7499 Lit : Node_Id;
7500 -- Integer literal for resulting constructed value
7502 Shift : Nat;
7503 -- Shift count from low order for next value
7505 Incr : Int;
7506 -- Shift increment for loop
7508 Expr : Node_Id;
7509 -- Next expression from positional parameters of aggregate
7511 Left_Justified : Boolean;
7512 -- Set True if we are filling the high order bits of the target
7513 -- value (i.e. the value is left justified).
7515 begin
7516 -- For little endian, we fill up the low order bits of the target
7517 -- value. For big endian we fill up the high order bits of the
7518 -- target value (which is a left justified modular value).
7520 Left_Justified := Bytes_Big_Endian;
7522 -- Switch justification if using -gnatd8
7524 if Debug_Flag_8 then
7525 Left_Justified := not Left_Justified;
7526 end if;
7528 -- Switch justfification if reverse storage order
7530 if Reverse_Storage_Order (Base_Type (Typ)) then
7531 Left_Justified := not Left_Justified;
7532 end if;
7534 if Left_Justified then
7535 Shift := Csiz * (Len - 1);
7536 Incr := -Csiz;
7537 else
7538 Shift := 0;
7539 Incr := +Csiz;
7540 end if;
7542 -- Loop to set the values
7544 if Len = 0 then
7545 Aggregate_Val := Uint_0;
7546 else
7547 Expr := First (Expressions (N));
7548 Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift;
7550 for J in 2 .. Len loop
7551 Shift := Shift + Incr;
7552 Next (Expr);
7553 Aggregate_Val :=
7554 Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift;
7555 end loop;
7556 end if;
7558 -- Now we can rewrite with the proper value
7560 Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val);
7561 Set_Print_In_Hex (Lit);
7563 -- Construct the expression using this literal. Note that it is
7564 -- important to qualify the literal with its proper modular type
7565 -- since universal integer does not have the required range and
7566 -- also this is a left justified modular type, which is important
7567 -- in the big-endian case.
7569 Rewrite (N,
7570 Unchecked_Convert_To (Typ,
7571 Make_Qualified_Expression (Loc,
7572 Subtype_Mark =>
7573 New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc),
7574 Expression => Lit)));
7576 Analyze_And_Resolve (N, Typ);
7577 return True;
7578 end;
7579 end;
7581 exception
7582 when Not_Handled =>
7583 return False;
7584 end Packed_Array_Aggregate_Handled;
7586 ----------------------------
7587 -- Has_Mutable_Components --
7588 ----------------------------
7590 function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
7591 Comp : Entity_Id;
7593 begin
7594 Comp := First_Component (Typ);
7595 while Present (Comp) loop
7596 if Is_Record_Type (Etype (Comp))
7597 and then Has_Discriminants (Etype (Comp))
7598 and then not Is_Constrained (Etype (Comp))
7599 then
7600 return True;
7601 end if;
7603 Next_Component (Comp);
7604 end loop;
7606 return False;
7607 end Has_Mutable_Components;
7609 ------------------------------
7610 -- Initialize_Discriminants --
7611 ------------------------------
7613 procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is
7614 Loc : constant Source_Ptr := Sloc (N);
7615 Bas : constant Entity_Id := Base_Type (Typ);
7616 Par : constant Entity_Id := Etype (Bas);
7617 Decl : constant Node_Id := Parent (Par);
7618 Ref : Node_Id;
7620 begin
7621 if Is_Tagged_Type (Bas)
7622 and then Is_Derived_Type (Bas)
7623 and then Has_Discriminants (Par)
7624 and then Has_Discriminants (Bas)
7625 and then Number_Discriminants (Bas) /= Number_Discriminants (Par)
7626 and then Nkind (Decl) = N_Full_Type_Declaration
7627 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7628 and then
7629 Present (Variant_Part (Component_List (Type_Definition (Decl))))
7630 and then Nkind (N) /= N_Extension_Aggregate
7631 then
7633 -- Call init proc to set discriminants.
7634 -- There should eventually be a special procedure for this ???
7636 Ref := New_Occurrence_Of (Defining_Identifier (N), Loc);
7637 Insert_Actions_After (N,
7638 Build_Initialization_Call (Sloc (N), Ref, Typ));
7639 end if;
7640 end Initialize_Discriminants;
7642 ----------------
7643 -- Must_Slide --
7644 ----------------
7646 function Must_Slide
7647 (Obj_Type : Entity_Id;
7648 Typ : Entity_Id) return Boolean
7650 L1, L2, H1, H2 : Node_Id;
7652 begin
7653 -- No sliding if the type of the object is not established yet, if it is
7654 -- an unconstrained type whose actual subtype comes from the aggregate,
7655 -- or if the two types are identical.
7657 if not Is_Array_Type (Obj_Type) then
7658 return False;
7660 elsif not Is_Constrained (Obj_Type) then
7661 return False;
7663 elsif Typ = Obj_Type then
7664 return False;
7666 else
7667 -- Sliding can only occur along the first dimension
7669 Get_Index_Bounds (First_Index (Typ), L1, H1);
7670 Get_Index_Bounds (First_Index (Obj_Type), L2, H2);
7672 if not Is_OK_Static_Expression (L1) or else
7673 not Is_OK_Static_Expression (L2) or else
7674 not Is_OK_Static_Expression (H1) or else
7675 not Is_OK_Static_Expression (H2)
7676 then
7677 return False;
7678 else
7679 return Expr_Value (L1) /= Expr_Value (L2)
7680 or else
7681 Expr_Value (H1) /= Expr_Value (H2);
7682 end if;
7683 end if;
7684 end Must_Slide;
7686 ---------------------------------
7687 -- Process_Transient_Component --
7688 ---------------------------------
7690 procedure Process_Transient_Component
7691 (Loc : Source_Ptr;
7692 Comp_Typ : Entity_Id;
7693 Init_Expr : Node_Id;
7694 Fin_Call : out Node_Id;
7695 Hook_Clear : out Node_Id;
7696 Aggr : Node_Id := Empty;
7697 Stmts : List_Id := No_List)
7699 procedure Add_Item (Item : Node_Id);
7700 -- Insert arbitrary node Item into the tree depending on the values of
7701 -- Aggr and Stmts.
7703 --------------
7704 -- Add_Item --
7705 --------------
7707 procedure Add_Item (Item : Node_Id) is
7708 begin
7709 if Present (Aggr) then
7710 Insert_Action (Aggr, Item);
7711 else
7712 pragma Assert (Present (Stmts));
7713 Append_To (Stmts, Item);
7714 end if;
7715 end Add_Item;
7717 -- Local variables
7719 Hook_Assign : Node_Id;
7720 Hook_Decl : Node_Id;
7721 Ptr_Decl : Node_Id;
7722 Res_Decl : Node_Id;
7723 Res_Id : Entity_Id;
7724 Res_Typ : Entity_Id;
7726 -- Start of processing for Process_Transient_Component
7728 begin
7729 -- Add the access type, which provides a reference to the function
7730 -- result. Generate:
7732 -- type Res_Typ is access all Comp_Typ;
7734 Res_Typ := Make_Temporary (Loc, 'A');
7735 Set_Ekind (Res_Typ, E_General_Access_Type);
7736 Set_Directly_Designated_Type (Res_Typ, Comp_Typ);
7738 Add_Item
7739 (Make_Full_Type_Declaration (Loc,
7740 Defining_Identifier => Res_Typ,
7741 Type_Definition =>
7742 Make_Access_To_Object_Definition (Loc,
7743 All_Present => True,
7744 Subtype_Indication => New_Occurrence_Of (Comp_Typ, Loc))));
7746 -- Add the temporary which captures the result of the function call.
7747 -- Generate:
7749 -- Res : constant Res_Typ := Init_Expr'Reference;
7751 -- Note that this temporary is effectively a transient object because
7752 -- its lifetime is bounded by the current array or record component.
7754 Res_Id := Make_Temporary (Loc, 'R');
7755 Set_Ekind (Res_Id, E_Constant);
7756 Set_Etype (Res_Id, Res_Typ);
7758 -- Mark the transient object as successfully processed to avoid double
7759 -- finalization.
7761 Set_Is_Finalized_Transient (Res_Id);
7763 -- Signal the general finalization machinery that this transient object
7764 -- should not be considered for finalization actions because its cleanup
7765 -- will be performed by Process_Transient_Component_Completion.
7767 Set_Is_Ignored_Transient (Res_Id);
7769 Res_Decl :=
7770 Make_Object_Declaration (Loc,
7771 Defining_Identifier => Res_Id,
7772 Constant_Present => True,
7773 Object_Definition => New_Occurrence_Of (Res_Typ, Loc),
7774 Expression =>
7775 Make_Reference (Loc, New_Copy_Tree (Init_Expr)));
7777 Add_Item (Res_Decl);
7779 -- Construct all pieces necessary to hook and finalize the transient
7780 -- result.
7782 Build_Transient_Object_Statements
7783 (Obj_Decl => Res_Decl,
7784 Fin_Call => Fin_Call,
7785 Hook_Assign => Hook_Assign,
7786 Hook_Clear => Hook_Clear,
7787 Hook_Decl => Hook_Decl,
7788 Ptr_Decl => Ptr_Decl);
7790 -- Add the access type which provides a reference to the transient
7791 -- result. Generate:
7793 -- type Ptr_Typ is access all Comp_Typ;
7795 Add_Item (Ptr_Decl);
7797 -- Add the temporary which acts as a hook to the transient result.
7798 -- Generate:
7800 -- Hook : Ptr_Typ := null;
7802 Add_Item (Hook_Decl);
7804 -- Attach the transient result to the hook. Generate:
7806 -- Hook := Ptr_Typ (Res);
7808 Add_Item (Hook_Assign);
7810 -- The original initialization expression now references the value of
7811 -- the temporary function result. Generate:
7813 -- Res.all
7815 Rewrite (Init_Expr,
7816 Make_Explicit_Dereference (Loc,
7817 Prefix => New_Occurrence_Of (Res_Id, Loc)));
7818 end Process_Transient_Component;
7820 --------------------------------------------
7821 -- Process_Transient_Component_Completion --
7822 --------------------------------------------
7824 procedure Process_Transient_Component_Completion
7825 (Loc : Source_Ptr;
7826 Aggr : Node_Id;
7827 Fin_Call : Node_Id;
7828 Hook_Clear : Node_Id;
7829 Stmts : List_Id)
7831 Exceptions_OK : constant Boolean :=
7832 not Restriction_Active (No_Exception_Propagation);
7834 begin
7835 pragma Assert (Present (Fin_Call));
7836 pragma Assert (Present (Hook_Clear));
7838 -- Generate the following code if exception propagation is allowed:
7840 -- declare
7841 -- Abort : constant Boolean := Triggered_By_Abort;
7842 -- <or>
7843 -- Abort : constant Boolean := False; -- no abort
7845 -- E : Exception_Occurrence;
7846 -- Raised : Boolean := False;
7848 -- begin
7849 -- [Abort_Defer;]
7851 -- begin
7852 -- Hook := null;
7853 -- [Deep_]Finalize (Res.all);
7855 -- exception
7856 -- when others =>
7857 -- if not Raised then
7858 -- Raised := True;
7859 -- Save_Occurrence (E,
7860 -- Get_Curent_Excep.all.all);
7861 -- end if;
7862 -- end;
7864 -- [Abort_Undefer;]
7866 -- if Raised and then not Abort then
7867 -- Raise_From_Controlled_Operation (E);
7868 -- end if;
7869 -- end;
7871 if Exceptions_OK then
7872 Abort_And_Exception : declare
7873 Blk_Decls : constant List_Id := New_List;
7874 Blk_Stmts : constant List_Id := New_List;
7876 Fin_Data : Finalization_Exception_Data;
7878 begin
7879 -- Create the declarations of the two flags and the exception
7880 -- occurrence.
7882 Build_Object_Declarations (Fin_Data, Blk_Decls, Loc);
7884 -- Generate:
7885 -- Abort_Defer;
7887 if Abort_Allowed then
7888 Append_To (Blk_Stmts,
7889 Build_Runtime_Call (Loc, RE_Abort_Defer));
7890 end if;
7892 -- Wrap the hook clear and the finalization call in order to trap
7893 -- a potential exception.
7895 Append_To (Blk_Stmts,
7896 Make_Block_Statement (Loc,
7897 Handled_Statement_Sequence =>
7898 Make_Handled_Sequence_Of_Statements (Loc,
7899 Statements => New_List (
7900 Hook_Clear,
7901 Fin_Call),
7902 Exception_Handlers => New_List (
7903 Build_Exception_Handler (Fin_Data)))));
7905 -- Generate:
7906 -- Abort_Undefer;
7908 if Abort_Allowed then
7909 Append_To (Blk_Stmts,
7910 Build_Runtime_Call (Loc, RE_Abort_Undefer));
7911 end if;
7913 -- Reraise the potential exception with a proper "upgrade" to
7914 -- Program_Error if needed.
7916 Append_To (Blk_Stmts, Build_Raise_Statement (Fin_Data));
7918 -- Wrap everything in a block
7920 Append_To (Stmts,
7921 Make_Block_Statement (Loc,
7922 Declarations => Blk_Decls,
7923 Handled_Statement_Sequence =>
7924 Make_Handled_Sequence_Of_Statements (Loc,
7925 Statements => Blk_Stmts)));
7926 end Abort_And_Exception;
7928 -- Generate the following code if exception propagation is not allowed
7929 -- and aborts are allowed:
7931 -- begin
7932 -- Abort_Defer;
7933 -- Hook := null;
7934 -- [Deep_]Finalize (Res.all);
7935 -- at end
7936 -- Abort_Undefer_Direct;
7937 -- end;
7939 elsif Abort_Allowed then
7940 Abort_Only : declare
7941 Blk_Stmts : constant List_Id := New_List;
7943 begin
7944 Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7945 Append_To (Blk_Stmts, Hook_Clear);
7946 Append_To (Blk_Stmts, Fin_Call);
7948 Append_To (Stmts,
7949 Build_Abort_Undefer_Block (Loc,
7950 Stmts => Blk_Stmts,
7951 Context => Aggr));
7952 end Abort_Only;
7954 -- Otherwise generate:
7956 -- Hook := null;
7957 -- [Deep_]Finalize (Res.all);
7959 else
7960 Append_To (Stmts, Hook_Clear);
7961 Append_To (Stmts, Fin_Call);
7962 end if;
7963 end Process_Transient_Component_Completion;
7965 ---------------------
7966 -- Sort_Case_Table --
7967 ---------------------
7969 procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is
7970 L : constant Int := Case_Table'First;
7971 U : constant Int := Case_Table'Last;
7972 K : Int;
7973 J : Int;
7974 T : Case_Bounds;
7976 begin
7977 K := L;
7978 while K /= U loop
7979 T := Case_Table (K + 1);
7981 J := K + 1;
7982 while J /= L
7983 and then Expr_Value (Case_Table (J - 1).Choice_Lo) >
7984 Expr_Value (T.Choice_Lo)
7985 loop
7986 Case_Table (J) := Case_Table (J - 1);
7987 J := J - 1;
7988 end loop;
7990 Case_Table (J) := T;
7991 K := K + 1;
7992 end loop;
7993 end Sort_Case_Table;
7995 ----------------------------
7996 -- Static_Array_Aggregate --
7997 ----------------------------
7999 function Static_Array_Aggregate (N : Node_Id) return Boolean is
8000 Bounds : constant Node_Id := Aggregate_Bounds (N);
8002 Typ : constant Entity_Id := Etype (N);
8003 Comp_Type : constant Entity_Id := Component_Type (Typ);
8004 Agg : Node_Id;
8005 Expr : Node_Id;
8006 Lo : Node_Id;
8007 Hi : Node_Id;
8009 begin
8010 if Is_Tagged_Type (Typ)
8011 or else Is_Controlled (Typ)
8012 or else Is_Packed (Typ)
8013 then
8014 return False;
8015 end if;
8017 if Present (Bounds)
8018 and then Nkind (Bounds) = N_Range
8019 and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal
8020 and then Nkind (High_Bound (Bounds)) = N_Integer_Literal
8021 then
8022 Lo := Low_Bound (Bounds);
8023 Hi := High_Bound (Bounds);
8025 if No (Component_Associations (N)) then
8027 -- Verify that all components are static integers
8029 Expr := First (Expressions (N));
8030 while Present (Expr) loop
8031 if Nkind (Expr) /= N_Integer_Literal then
8032 return False;
8033 end if;
8035 Next (Expr);
8036 end loop;
8038 return True;
8040 else
8041 -- We allow only a single named association, either a static
8042 -- range or an others_clause, with a static expression.
8044 Expr := First (Component_Associations (N));
8046 if Present (Expressions (N)) then
8047 return False;
8049 elsif Present (Next (Expr)) then
8050 return False;
8052 elsif Present (Next (First (Choices (Expr)))) then
8053 return False;
8055 else
8056 -- The aggregate is static if all components are literals,
8057 -- or else all its components are static aggregates for the
8058 -- component type. We also limit the size of a static aggregate
8059 -- to prevent runaway static expressions.
8061 if Is_Array_Type (Comp_Type)
8062 or else Is_Record_Type (Comp_Type)
8063 then
8064 if Nkind (Expression (Expr)) /= N_Aggregate
8065 or else
8066 not Compile_Time_Known_Aggregate (Expression (Expr))
8067 then
8068 return False;
8069 end if;
8071 elsif Nkind (Expression (Expr)) /= N_Integer_Literal then
8072 return False;
8073 end if;
8075 if not Aggr_Size_OK (N, Typ) then
8076 return False;
8077 end if;
8079 -- Create a positional aggregate with the right number of
8080 -- copies of the expression.
8082 Agg := Make_Aggregate (Sloc (N), New_List, No_List);
8084 for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi))
8085 loop
8086 Append_To (Expressions (Agg), New_Copy (Expression (Expr)));
8088 -- The copied expression must be analyzed and resolved.
8089 -- Besides setting the type, this ensures that static
8090 -- expressions are appropriately marked as such.
8092 Analyze_And_Resolve
8093 (Last (Expressions (Agg)), Component_Type (Typ));
8094 end loop;
8096 Set_Aggregate_Bounds (Agg, Bounds);
8097 Set_Etype (Agg, Typ);
8098 Set_Analyzed (Agg);
8099 Rewrite (N, Agg);
8100 Set_Compile_Time_Known_Aggregate (N);
8102 return True;
8103 end if;
8104 end if;
8106 else
8107 return False;
8108 end if;
8109 end Static_Array_Aggregate;
8111 ----------------------------------
8112 -- Two_Dim_Packed_Array_Handled --
8113 ----------------------------------
8115 function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is
8116 Loc : constant Source_Ptr := Sloc (N);
8117 Typ : constant Entity_Id := Etype (N);
8118 Ctyp : constant Entity_Id := Component_Type (Typ);
8119 Comp_Size : constant Int := UI_To_Int (Component_Size (Typ));
8120 Packed_Array : constant Entity_Id :=
8121 Packed_Array_Impl_Type (Base_Type (Typ));
8123 One_Comp : Node_Id;
8124 -- Expression in original aggregate
8126 One_Dim : Node_Id;
8127 -- One-dimensional subaggregate
8129 begin
8131 -- For now, only deal with cases where an integral number of elements
8132 -- fit in a single byte. This includes the most common boolean case.
8134 if not (Comp_Size = 1 or else
8135 Comp_Size = 2 or else
8136 Comp_Size = 4)
8137 then
8138 return False;
8139 end if;
8141 Convert_To_Positional
8142 (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True);
8144 -- Verify that all components are static
8146 if Nkind (N) = N_Aggregate
8147 and then Compile_Time_Known_Aggregate (N)
8148 then
8149 null;
8151 -- The aggregate may have been reanalyzed and converted already
8153 elsif Nkind (N) /= N_Aggregate then
8154 return True;
8156 -- If component associations remain, the aggregate is not static
8158 elsif Present (Component_Associations (N)) then
8159 return False;
8161 else
8162 One_Dim := First (Expressions (N));
8163 while Present (One_Dim) loop
8164 if Present (Component_Associations (One_Dim)) then
8165 return False;
8166 end if;
8168 One_Comp := First (Expressions (One_Dim));
8169 while Present (One_Comp) loop
8170 if not Is_OK_Static_Expression (One_Comp) then
8171 return False;
8172 end if;
8174 Next (One_Comp);
8175 end loop;
8177 Next (One_Dim);
8178 end loop;
8179 end if;
8181 -- Two-dimensional aggregate is now fully positional so pack one
8182 -- dimension to create a static one-dimensional array, and rewrite
8183 -- as an unchecked conversion to the original type.
8185 declare
8186 Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array));
8187 -- The packed array type is a byte array
8189 Packed_Num : Nat;
8190 -- Number of components accumulated in current byte
8192 Comps : List_Id;
8193 -- Assembled list of packed values for equivalent aggregate
8195 Comp_Val : Uint;
8196 -- Integer value of component
8198 Incr : Int;
8199 -- Step size for packing
8201 Init_Shift : Int;
8202 -- Endian-dependent start position for packing
8204 Shift : Int;
8205 -- Current insertion position
8207 Val : Int;
8208 -- Component of packed array being assembled
8210 begin
8211 Comps := New_List;
8212 Val := 0;
8213 Packed_Num := 0;
8215 -- Account for endianness. See corresponding comment in
8216 -- Packed_Array_Aggregate_Handled concerning the following.
8218 if Bytes_Big_Endian
8219 xor Debug_Flag_8
8220 xor Reverse_Storage_Order (Base_Type (Typ))
8221 then
8222 Init_Shift := Byte_Size - Comp_Size;
8223 Incr := -Comp_Size;
8224 else
8225 Init_Shift := 0;
8226 Incr := +Comp_Size;
8227 end if;
8229 -- Iterate over each subaggregate
8231 Shift := Init_Shift;
8232 One_Dim := First (Expressions (N));
8233 while Present (One_Dim) loop
8234 One_Comp := First (Expressions (One_Dim));
8235 while Present (One_Comp) loop
8236 if Packed_Num = Byte_Size / Comp_Size then
8238 -- Byte is complete, add to list of expressions
8240 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8241 Val := 0;
8242 Shift := Init_Shift;
8243 Packed_Num := 0;
8245 else
8246 Comp_Val := Expr_Rep_Value (One_Comp);
8248 -- Adjust for bias, and strip proper number of bits
8250 if Has_Biased_Representation (Ctyp) then
8251 Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp));
8252 end if;
8254 Comp_Val := Comp_Val mod Uint_2 ** Comp_Size;
8255 Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift);
8256 Shift := Shift + Incr;
8257 One_Comp := Next (One_Comp);
8258 Packed_Num := Packed_Num + 1;
8259 end if;
8260 end loop;
8262 One_Dim := Next (One_Dim);
8263 end loop;
8265 if Packed_Num > 0 then
8267 -- Add final incomplete byte if present
8269 Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps);
8270 end if;
8272 Rewrite (N,
8273 Unchecked_Convert_To (Typ,
8274 Make_Qualified_Expression (Loc,
8275 Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc),
8276 Expression => Make_Aggregate (Loc, Expressions => Comps))));
8277 Analyze_And_Resolve (N);
8278 return True;
8279 end;
8280 end Two_Dim_Packed_Array_Handled;
8282 end Exp_Aggr;