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