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