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