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