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