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