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