]>
Commit | Line | Data |
---|---|---|
70482933 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- E X P _ A G G R -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
cccef051 | 9 | -- Copyright (C) 1992-2023, 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 | ||
104f58db BD |
26 | with Aspects; use Aspects; |
27 | with Atree; use Atree; | |
28 | with Checks; use Checks; | |
29 | with Debug; use Debug; | |
30 | with Einfo; use Einfo; | |
76f9c7f4 | 31 | with Einfo.Entities; use Einfo.Entities; |
104f58db BD |
32 | with Einfo.Utils; use Einfo.Utils; |
33 | with Elists; use Elists; | |
34 | with Errout; use Errout; | |
35 | with Expander; use Expander; | |
36 | with Exp_Util; use Exp_Util; | |
37 | with Exp_Ch3; use Exp_Ch3; | |
38 | with Exp_Ch6; use Exp_Ch6; | |
39 | with Exp_Ch7; use Exp_Ch7; | |
40 | with Exp_Ch9; use Exp_Ch9; | |
41 | with Exp_Disp; use Exp_Disp; | |
42 | with Exp_Tss; use Exp_Tss; | |
43 | with Freeze; use Freeze; | |
44 | with Itypes; use Itypes; | |
45 | with Lib; use Lib; | |
46 | with Namet; use Namet; | |
47 | with Nmake; use Nmake; | |
48 | with Nlists; use Nlists; | |
49 | with Opt; use Opt; | |
50 | with Restrict; use Restrict; | |
51 | with Rident; use Rident; | |
52 | with Rtsfind; use Rtsfind; | |
53 | with Ttypes; use Ttypes; | |
54 | with Sem; use Sem; | |
55 | with Sem_Aggr; use Sem_Aggr; | |
56 | with Sem_Aux; use Sem_Aux; | |
e1dfbb03 | 57 | with Sem_Case; use Sem_Case; |
104f58db BD |
58 | with Sem_Ch3; use Sem_Ch3; |
59 | with Sem_Ch8; use Sem_Ch8; | |
60 | with Sem_Ch13; use Sem_Ch13; | |
61 | with Sem_Eval; use Sem_Eval; | |
62 | with Sem_Mech; use Sem_Mech; | |
63 | with Sem_Res; use Sem_Res; | |
ae250f9e | 64 | with Sem_Type; use Sem_Type; |
104f58db | 65 | with Sem_Util; use Sem_Util; |
ca4bff3a | 66 | use Sem_Util.Storage_Model_Support; |
104f58db BD |
67 | with Sinfo; use Sinfo; |
68 | with Sinfo.Nodes; use Sinfo.Nodes; | |
69 | with Sinfo.Utils; use Sinfo.Utils; | |
70 | with Snames; use Snames; | |
71 | with Stand; use Stand; | |
72 | with Stringt; use Stringt; | |
73 | with Tbuild; use Tbuild; | |
74 | with Uintp; use Uintp; | |
75 | with Urealp; use Urealp; | |
bc50ac71 | 76 | with Warnsw; use Warnsw; |
70482933 RK |
77 | |
78 | package body Exp_Aggr is | |
79 | ||
742084ad MP |
80 | function Build_Assignment_With_Temporary |
81 | (Target : Node_Id; | |
ca4bff3a | 82 | Typ : Entity_Id; |
742084ad MP |
83 | Source : Node_Id) return List_Id; |
84 | -- Returns a list of actions to assign Source to Target of type Typ using | |
ca4bff3a | 85 | -- an extra temporary, which can potentially be large. |
742084ad | 86 | |
70482933 RK |
87 | type Case_Bounds is record |
88 | Choice_Lo : Node_Id; | |
89 | Choice_Hi : Node_Id; | |
90 | Choice_Node : Node_Id; | |
91 | end record; | |
92 | ||
93 | type Case_Table_Type is array (Nat range <>) of Case_Bounds; | |
94 | -- Table type used by Check_Case_Choices procedure | |
95 | ||
9eb8d5b4 AC |
96 | procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id); |
97 | procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id); | |
745f5698 | 98 | procedure Expand_Container_Aggregate (N : Node_Id); |
9eb8d5b4 | 99 | |
a80b1eb7 EB |
100 | function Get_Base_Object (N : Node_Id) return Entity_Id; |
101 | -- Return the base object, i.e. the outermost prefix object, that N refers | |
102 | -- to statically, or Empty if it cannot be determined. The assumption is | |
103 | -- that all dereferences are explicit in the tree rooted at N. | |
104 | ||
df3e68b1 HK |
105 | function Has_Default_Init_Comps (N : Node_Id) return Boolean; |
106 | -- N is an aggregate (record or array). Checks the presence of default | |
107 | -- initialization (<>) in any component (Ada 2005: AI-287). | |
108 | ||
fc84947c | 109 | procedure Initialize_Component |
fc84947c EB |
110 | (N : Node_Id; |
111 | Comp : Node_Id; | |
112 | Comp_Typ : Node_Id; | |
113 | Init_Expr : Node_Id; | |
114 | Stmts : List_Id); | |
c0ceba6c EB |
115 | -- Perform the initialization of component Comp with expected type |
116 | -- Comp_Typ of aggregate N. Init_Expr denotes the initialization | |
fc84947c EB |
117 | -- expression of the component. All generated code is added to Stmts. |
118 | ||
9f51b855 JM |
119 | function Is_CCG_Supported_Aggregate (N : Node_Id) return Boolean; |
120 | -- Return True if aggregate N is located in a context supported by the | |
121 | -- CCG backend; False otherwise. | |
6031f544 | 122 | |
df3e68b1 HK |
123 | function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean; |
124 | -- Returns true if N is an aggregate used to initialize the components | |
b465ef6f | 125 | -- of a statically allocated dispatch table. |
df3e68b1 | 126 | |
937e9676 AC |
127 | function Late_Expansion |
128 | (N : Node_Id; | |
129 | Typ : Entity_Id; | |
130 | Target : Node_Id) return List_Id; | |
131 | -- This routine implements top-down expansion of nested aggregates. In | |
132 | -- doing so, it avoids the generation of temporaries at each level. N is | |
133 | -- a nested record or array aggregate with the Expansion_Delayed flag. | |
134 | -- Typ is the expected type of the aggregate. Target is a (duplicatable) | |
135 | -- expression that will hold the result of the aggregate expansion. | |
136 | ||
137 | function Make_OK_Assignment_Statement | |
138 | (Sloc : Source_Ptr; | |
139 | Name : Node_Id; | |
140 | Expression : Node_Id) return Node_Id; | |
141 | -- This is like Make_Assignment_Statement, except that Assignment_OK | |
142 | -- is set in the left operand. All assignments built by this unit use | |
143 | -- this routine. This is needed to deal with assignments to initialized | |
144 | -- constants that are done in place. | |
145 | ||
3cf3e5c6 | 146 | function Must_Slide |
6732c403 ES |
147 | (Aggr : Node_Id; |
148 | Obj_Type : Entity_Id; | |
3cf3e5c6 AC |
149 | Typ : Entity_Id) return Boolean; |
150 | -- A static array aggregate in an object declaration can in most cases be | |
151 | -- expanded in place. The one exception is when the aggregate is given | |
152 | -- with component associations that specify different bounds from those of | |
153 | -- the type definition in the object declaration. In this pathological | |
154 | -- case the aggregate must slide, and we must introduce an intermediate | |
155 | -- temporary to hold it. | |
156 | -- | |
157 | -- The same holds in an assignment to one-dimensional array of arrays, | |
158 | -- when a component may be given with bounds that differ from those of the | |
159 | -- component type. | |
160 | ||
937e9676 AC |
161 | function Number_Of_Choices (N : Node_Id) return Nat; |
162 | -- Returns the number of discrete choices (not including the others choice | |
163 | -- if present) contained in (sub-)aggregate N. | |
164 | ||
70482933 RK |
165 | procedure Sort_Case_Table (Case_Table : in out Case_Table_Type); |
166 | -- Sort the Case Table using the Lower Bound of each Choice as the key. | |
167 | -- A simple insertion sort is used since the number of choices in a case | |
168 | -- statement of variant part will usually be small and probably in near | |
169 | -- sorted order. | |
170 | ||
171 | ------------------------------------------------------ | |
172 | -- Local subprograms for Record Aggregate Expansion -- | |
173 | ------------------------------------------------------ | |
174 | ||
d4dfb005 | 175 | function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean; |
e05e5d6b EB |
176 | -- True if N is an aggregate (possibly qualified or a dependent expression |
177 | -- of a conditional expression, and possibly recursively so) that is being | |
178 | -- returned from a build-in-place function. Such qualified and conditional | |
179 | -- expressions are transparent for this purpose because an enclosing return | |
180 | -- is propagated resp. distributed into these expressions by the expander. | |
d4dfb005 | 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 | 193 | -- Transform a record aggregate into a sequence of assignments performed |
64ac53f4 | 194 | -- component by component. N is an N_Aggregate or N_Extension_Aggregate. |
d4dfb005 | 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 | ||
a80b1eb7 EB |
219 | function In_Place_Assign_OK |
220 | (N : Node_Id; | |
221 | Target_Object : Entity_Id := Empty) return Boolean; | |
4ff5aa0c AC |
222 | -- Predicate to determine whether an aggregate assignment can be done in |
223 | -- place, because none of the new values can depend on the components of | |
224 | -- the target of the assignment. | |
225 | ||
07fc65c4 GB |
226 | procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id); |
227 | -- If the type of the aggregate is a type extension with renamed discrimi- | |
228 | -- nants, we must initialize the hidden discriminants of the parent. | |
229 | -- Otherwise, the target object must not be initialized. The discriminants | |
230 | -- are initialized by calling the initialization procedure for the type. | |
231 | -- This is incorrect if the initialization of other components has any | |
232 | -- side effects. We restrict this call to the case where the parent type | |
233 | -- has a variant part, because this is the only case where the hidden | |
234 | -- discriminants are accessed, namely when calling discriminant checking | |
235 | -- functions of the parent type, and when applying a stream attribute to | |
236 | -- an object of the derived type. | |
237 | ||
70482933 | 238 | ----------------------------------------------------- |
07fc65c4 | 239 | -- Local Subprograms for Array Aggregate Expansion -- |
70482933 RK |
240 | ----------------------------------------------------- |
241 | ||
2fedcc18 EB |
242 | function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean; |
243 | -- Returns true if an aggregate assignment can be done by the back end | |
244 | ||
eaf6e63a | 245 | function Aggr_Size_OK (N : Node_Id) return Boolean; |
841dd0f5 AC |
246 | -- Very large static aggregates present problems to the back-end, and are |
247 | -- transformed into assignments and loops. This function verifies that the | |
248 | -- total number of components of an aggregate is acceptable for rewriting | |
74e7891f RD |
249 | -- into a purely positional static form. Aggr_Size_OK must be called before |
250 | -- calling Flatten. | |
251 | -- | |
841dd0f5 | 252 | -- This function also detects and warns about one-component aggregates that |
d940c627 | 253 | -- appear in a nonstatic context. Even if the component value is static, |
841dd0f5 | 254 | -- such an aggregate must be expanded into an assignment. |
643a0839 | 255 | |
df3e68b1 HK |
256 | function Backend_Processing_Possible (N : Node_Id) return Boolean; |
257 | -- This function checks if array aggregate N can be processed directly | |
b465ef6f | 258 | -- by the backend. If this is the case, True is returned. |
df3e68b1 HK |
259 | |
260 | function Build_Array_Aggr_Code | |
261 | (N : Node_Id; | |
262 | Ctype : Entity_Id; | |
263 | Index : Node_Id; | |
264 | Into : Node_Id; | |
265 | Scalar_Comp : Boolean; | |
266 | Indexes : List_Id := No_List) return List_Id; | |
267 | -- This recursive routine returns a list of statements containing the | |
268 | -- loops and assignments that are needed for the expansion of the array | |
269 | -- aggregate N. | |
270 | -- | |
271 | -- N is the (sub-)aggregate node to be expanded into code. This node has | |
272 | -- been fully analyzed, and its Etype is properly set. | |
273 | -- | |
d74716b3 | 274 | -- Index is the index node corresponding to the array subaggregate N |
df3e68b1 HK |
275 | -- |
276 | -- Into is the target expression into which we are copying the aggregate. | |
277 | -- Note that this node may not have been analyzed yet, and so the Etype | |
278 | -- field may not be set. | |
279 | -- | |
280 | -- Scalar_Comp is True if the component type of the aggregate is scalar | |
281 | -- | |
282 | -- Indexes is the current list of expressions used to index the object we | |
283 | -- are writing into. | |
284 | ||
6f639c98 ES |
285 | procedure Convert_Array_Aggr_In_Allocator |
286 | (Decl : Node_Id; | |
287 | Aggr : Node_Id; | |
288 | Target : Node_Id); | |
289 | -- If the aggregate appears within an allocator and can be expanded in | |
290 | -- place, this routine generates the individual assignments to components | |
291 | -- of the designated object. This is an optimization over the general | |
292 | -- case, where a temporary is first created on the stack and then used to | |
293 | -- construct the allocated object on the heap. | |
294 | ||
07fc65c4 | 295 | procedure Convert_To_Positional |
c42006e9 AC |
296 | (N : Node_Id; |
297 | Handle_Bit_Packed : Boolean := False); | |
07fc65c4 | 298 | -- If possible, convert named notation to positional notation. This |
3cf3e5c6 AC |
299 | -- conversion is possible only in some static cases. If the conversion is |
300 | -- possible, then N is rewritten with the analyzed converted aggregate. | |
c42006e9 | 301 | -- The parameter Handle_Bit_Packed is usually set False (since we do |
3cf3e5c6 AC |
302 | -- not expect the back end to handle bit packed arrays, so the normal case |
303 | -- of conversion is pointless), but in the special case of a call from | |
304 | -- Packed_Array_Aggregate_Handled, we set this parameter to True, since | |
305 | -- these are cases we handle in there. | |
07fc65c4 | 306 | |
70482933 RK |
307 | procedure Expand_Array_Aggregate (N : Node_Id); |
308 | -- This is the top-level routine to perform array aggregate expansion. | |
309 | -- N is the N_Aggregate node to be expanded. | |
310 | ||
dc3af7e2 | 311 | function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean; |
42fcc750 | 312 | -- For 2D packed array aggregates with constant bounds and constant scalar |
dc3af7e2 AC |
313 | -- components, it is preferable to pack the inner aggregates because the |
314 | -- whole matrix can then be presented to the back-end as a one-dimensional | |
315 | -- list of literals. This is much more efficient than expanding into single | |
2791be24 AC |
316 | -- component assignments. This function determines if the type Typ is for |
317 | -- an array that is suitable for this optimization: it returns True if Typ | |
318 | -- is a two dimensional bit packed array with component size 1, 2, or 4. | |
dc3af7e2 | 319 | |
c42006e9 | 320 | function Max_Aggregate_Size |
eaf6e63a | 321 | (N : Node_Id; |
c42006e9 | 322 | Default_Size : Nat := 5000) return Nat; |
eaf6e63a BD |
323 | -- Return the max size for a static aggregate N. Return Default_Size if no |
324 | -- other special criteria trigger. | |
c42006e9 | 325 | |
07fc65c4 GB |
326 | function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean; |
327 | -- Given an array aggregate, this function handles the case of a packed | |
328 | -- array aggregate with all constant values, where the aggregate can be | |
329 | -- evaluated at compile time. If this is possible, then N is rewritten | |
330 | -- to be its proper compile time value with all the components properly | |
50decc81 RD |
331 | -- assembled. The expression is analyzed and resolved and True is returned. |
332 | -- If this transformation is not possible, N is unchanged and False is | |
333 | -- returned. | |
07fc65c4 | 334 | |
5eeeed5e AC |
335 | function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean; |
336 | -- If the type of the aggregate is a two-dimensional bit_packed array | |
337 | -- it may be transformed into an array of bytes with constant values, | |
338 | -- and presented to the back-end as a static value. The function returns | |
339 | -- false if this transformation cannot be performed. THis is similar to, | |
340 | -- and reuses part of the machinery in Packed_Array_Aggregate_Handled. | |
341 | ||
2fedcc18 EB |
342 | ------------------------------------ |
343 | -- Aggr_Assignment_OK_For_Backend -- | |
344 | ------------------------------------ | |
345 | ||
346 | -- Back-end processing by Gigi/gcc is possible only if all the following | |
347 | -- conditions are met: | |
348 | ||
349 | -- 1. N consists of a single OTHERS choice, possibly recursively, or | |
350 | -- of a single choice, possibly recursively, if it is surrounded by | |
351 | -- a qualified expression whose subtype mark is unconstrained. | |
352 | ||
353 | -- 2. The array type has no null ranges (the purpose of this is to | |
354 | -- avoid a bogus warning for an out-of-range value). | |
355 | ||
356 | -- 3. The array type has no atomic components | |
357 | ||
358 | -- 4. The component type is elementary | |
359 | ||
360 | -- 5. The component size is a multiple of Storage_Unit | |
361 | ||
362 | -- 6. The component size is Storage_Unit or the value is of the form | |
363 | -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit) | |
364 | -- and M in 0 .. A-1. This can also be viewed as K occurrences of | |
365 | -- the Storage_Unit value M, concatenated together. | |
366 | ||
367 | -- The ultimate goal is to generate a call to a fast memset routine | |
368 | -- specifically optimized for the target. | |
369 | ||
370 | function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is | |
2fedcc18 EB |
371 | |
372 | function Is_OK_Aggregate (Aggr : Node_Id) return Boolean; | |
373 | -- Return true if Aggr is suitable for back-end assignment | |
374 | ||
375 | --------------------- | |
376 | -- Is_OK_Aggregate -- | |
377 | --------------------- | |
378 | ||
379 | function Is_OK_Aggregate (Aggr : Node_Id) return Boolean is | |
380 | Assoc : constant List_Id := Component_Associations (Aggr); | |
381 | ||
382 | begin | |
383 | -- An "others" aggregate is most likely OK, but see below | |
384 | ||
385 | if Is_Others_Aggregate (Aggr) then | |
386 | null; | |
387 | ||
388 | -- An aggregate with a single choice requires a qualified expression | |
389 | -- whose subtype mark is an unconstrained type because we need it to | |
390 | -- have the semantics of an "others" aggregate. | |
391 | ||
392 | elsif Nkind (Parent (N)) = N_Qualified_Expression | |
393 | and then not Is_Constrained (Entity (Subtype_Mark (Parent (N)))) | |
394 | and then Is_Single_Aggregate (Aggr) | |
395 | then | |
396 | null; | |
397 | ||
398 | -- The other cases are not OK | |
399 | ||
400 | else | |
401 | return False; | |
402 | end if; | |
403 | ||
404 | -- In any case we do not support an iterated association | |
405 | ||
406 | return Nkind (First (Assoc)) /= N_Iterated_Component_Association; | |
407 | end Is_OK_Aggregate; | |
408 | ||
7c4f3267 BD |
409 | Bounds : Range_Nodes; |
410 | Csiz : Uint := No_Uint; | |
411 | Ctyp : Entity_Id; | |
412 | Expr : Node_Id; | |
413 | Index : Entity_Id; | |
414 | Nunits : Int; | |
415 | Remainder : Uint; | |
416 | Value : Uint; | |
417 | ||
b120ca61 EB |
418 | -- Start of processing for Aggr_Assignment_OK_For_Backend |
419 | ||
2fedcc18 EB |
420 | begin |
421 | -- Back end doesn't know about <> | |
422 | ||
423 | if Has_Default_Init_Comps (N) then | |
424 | return False; | |
425 | end if; | |
426 | ||
427 | -- Recurse as far as possible to find the innermost component type | |
428 | ||
429 | Ctyp := Etype (N); | |
430 | Expr := N; | |
431 | while Is_Array_Type (Ctyp) loop | |
432 | if Nkind (Expr) /= N_Aggregate | |
433 | or else not Is_OK_Aggregate (Expr) | |
434 | then | |
435 | return False; | |
436 | end if; | |
437 | ||
438 | Index := First_Index (Ctyp); | |
439 | while Present (Index) loop | |
7c4f3267 | 440 | Bounds := Get_Index_Bounds (Index); |
2fedcc18 | 441 | |
7c4f3267 | 442 | if Is_Null_Range (Bounds.First, Bounds.Last) then |
2fedcc18 EB |
443 | return False; |
444 | end if; | |
445 | ||
446 | Next_Index (Index); | |
447 | end loop; | |
448 | ||
449 | Expr := Expression (First (Component_Associations (Expr))); | |
450 | ||
451 | for J in 1 .. Number_Dimensions (Ctyp) - 1 loop | |
452 | if Nkind (Expr) /= N_Aggregate | |
453 | or else not Is_OK_Aggregate (Expr) | |
454 | then | |
455 | return False; | |
456 | end if; | |
457 | ||
458 | Expr := Expression (First (Component_Associations (Expr))); | |
459 | end loop; | |
460 | ||
461 | if Has_Atomic_Components (Ctyp) then | |
462 | return False; | |
463 | end if; | |
464 | ||
465 | Csiz := Component_Size (Ctyp); | |
466 | Ctyp := Component_Type (Ctyp); | |
467 | ||
b120ca61 | 468 | if Is_Full_Access (Ctyp) then |
2fedcc18 EB |
469 | return False; |
470 | end if; | |
471 | end loop; | |
472 | ||
473 | -- Access types need to be dealt with specially | |
474 | ||
475 | if Is_Access_Type (Ctyp) then | |
476 | ||
477 | -- Component_Size is not set by Layout_Type if the component | |
478 | -- type is an access type ??? | |
479 | ||
480 | Csiz := Esize (Ctyp); | |
481 | ||
482 | -- Fat pointers are rejected as they are not really elementary | |
483 | -- for the backend. | |
484 | ||
36e38022 | 485 | if No (Csiz) or else Csiz /= System_Address_Size then |
2fedcc18 EB |
486 | return False; |
487 | end if; | |
488 | ||
489 | -- The supported expressions are NULL and constants, others are | |
490 | -- rejected upfront to avoid being analyzed below, which can be | |
491 | -- problematic for some of them, for example allocators. | |
492 | ||
493 | if Nkind (Expr) /= N_Null and then not Is_Entity_Name (Expr) then | |
494 | return False; | |
495 | end if; | |
496 | ||
497 | -- Scalar types are OK if their size is a multiple of Storage_Unit | |
498 | ||
36e38022 | 499 | elsif Is_Scalar_Type (Ctyp) and then Present (Csiz) then |
2fedcc18 EB |
500 | |
501 | if Csiz mod System_Storage_Unit /= 0 then | |
502 | return False; | |
503 | end if; | |
504 | ||
505 | -- Composite types are rejected | |
506 | ||
507 | else | |
508 | return False; | |
509 | end if; | |
510 | ||
511 | -- If the expression has side effects (e.g. contains calls with | |
512 | -- potential side effects) reject as well. We only preanalyze the | |
513 | -- expression to prevent the removal of intended side effects. | |
514 | ||
515 | Preanalyze_And_Resolve (Expr, Ctyp); | |
516 | ||
517 | if not Side_Effect_Free (Expr) then | |
518 | return False; | |
519 | end if; | |
520 | ||
521 | -- The expression needs to be analyzed if True is returned | |
522 | ||
523 | Analyze_And_Resolve (Expr, Ctyp); | |
524 | ||
525 | -- Strip away any conversions from the expression as they simply | |
526 | -- qualify the real expression. | |
527 | ||
4a08c95c | 528 | while Nkind (Expr) in N_Unchecked_Type_Conversion | N_Type_Conversion |
2fedcc18 EB |
529 | loop |
530 | Expr := Expression (Expr); | |
531 | end loop; | |
532 | ||
533 | Nunits := UI_To_Int (Csiz) / System_Storage_Unit; | |
534 | ||
535 | if Nunits = 1 then | |
536 | return True; | |
537 | end if; | |
538 | ||
539 | if not Compile_Time_Known_Value (Expr) then | |
540 | return False; | |
541 | end if; | |
542 | ||
543 | -- The only supported value for floating point is 0.0 | |
544 | ||
545 | if Is_Floating_Point_Type (Ctyp) then | |
546 | return Expr_Value_R (Expr) = Ureal_0; | |
547 | end if; | |
548 | ||
549 | -- For other types, we can look into the value as an integer, which | |
550 | -- means the representation value for enumeration literals. | |
551 | ||
552 | Value := Expr_Rep_Value (Expr); | |
553 | ||
554 | if Has_Biased_Representation (Ctyp) then | |
555 | Value := Value - Expr_Value (Type_Low_Bound (Ctyp)); | |
556 | end if; | |
557 | ||
558 | -- Values 0 and -1 immediately satisfy the last check | |
559 | ||
560 | if Value = Uint_0 or else Value = Uint_Minus_1 then | |
561 | return True; | |
562 | end if; | |
563 | ||
564 | -- We need to work with an unsigned value | |
565 | ||
566 | if Value < 0 then | |
567 | Value := Value + 2**(System_Storage_Unit * Nunits); | |
568 | end if; | |
569 | ||
570 | Remainder := Value rem 2**System_Storage_Unit; | |
571 | ||
572 | for J in 1 .. Nunits - 1 loop | |
573 | Value := Value / 2**System_Storage_Unit; | |
574 | ||
575 | if Value rem 2**System_Storage_Unit /= Remainder then | |
576 | return False; | |
577 | end if; | |
578 | end loop; | |
579 | ||
580 | return True; | |
581 | end Aggr_Assignment_OK_For_Backend; | |
582 | ||
643a0839 ES |
583 | ------------------ |
584 | -- Aggr_Size_OK -- | |
585 | ------------------ | |
586 | ||
eaf6e63a BD |
587 | function Aggr_Size_OK (N : Node_Id) return Boolean is |
588 | Typ : constant Entity_Id := Etype (N); | |
643a0839 ES |
589 | Lo : Node_Id; |
590 | Hi : Node_Id; | |
591 | Indx : Node_Id; | |
4167b075 | 592 | Size : Uint; |
643a0839 ES |
593 | Lov : Uint; |
594 | Hiv : Uint; | |
595 | ||
303fbb20 AC |
596 | Max_Aggr_Size : Nat; |
597 | -- Determines the maximum size of an array aggregate produced by | |
598 | -- converting named to positional notation (e.g. from others clauses). | |
599 | -- This avoids running away with attempts to convert huge aggregates, | |
600 | -- which hit memory limits in the backend. | |
643a0839 | 601 | |
16e764a7 | 602 | function Component_Count (T : Entity_Id) return Nat; |
457cee0b | 603 | -- The limit is applied to the total number of subcomponents that the |
643a0839 ES |
604 | -- aggregate will have, which is the number of static expressions |
605 | -- that will appear in the flattened array. This requires a recursive | |
16b05213 | 606 | -- computation of the number of scalar components of the structure. |
643a0839 ES |
607 | |
608 | --------------------- | |
609 | -- Component_Count -- | |
610 | --------------------- | |
611 | ||
16e764a7 AC |
612 | function Component_Count (T : Entity_Id) return Nat is |
613 | Res : Nat := 0; | |
643a0839 ES |
614 | Comp : Entity_Id; |
615 | ||
616 | begin | |
617 | if Is_Scalar_Type (T) then | |
618 | return 1; | |
619 | ||
620 | elsif Is_Record_Type (T) then | |
621 | Comp := First_Component (T); | |
622 | while Present (Comp) loop | |
623 | Res := Res + Component_Count (Etype (Comp)); | |
624 | Next_Component (Comp); | |
625 | end loop; | |
626 | ||
627 | return Res; | |
628 | ||
629 | elsif Is_Array_Type (T) then | |
630 | declare | |
631 | Lo : constant Node_Id := | |
15f0f591 | 632 | Type_Low_Bound (Etype (First_Index (T))); |
643a0839 | 633 | Hi : constant Node_Id := |
15f0f591 | 634 | Type_High_Bound (Etype (First_Index (T))); |
643a0839 | 635 | |
16e764a7 | 636 | Siz : constant Nat := Component_Count (Component_Type (T)); |
643a0839 ES |
637 | |
638 | begin | |
b4213ffd AC |
639 | -- Check for superflat arrays, i.e. arrays with such bounds |
640 | -- as 4 .. 2, to insure that this function never returns a | |
641 | -- meaningless negative value. | |
642 | ||
643a0839 ES |
643 | if not Compile_Time_Known_Value (Lo) |
644 | or else not Compile_Time_Known_Value (Hi) | |
b4213ffd | 645 | or else Expr_Value (Hi) < Expr_Value (Lo) |
643a0839 ES |
646 | then |
647 | return 0; | |
b4213ffd | 648 | |
643a0839 | 649 | else |
457cee0b AC |
650 | -- If the number of components is greater than Int'Last, |
651 | -- then return Int'Last, so caller will return False (Aggr | |
652 | -- size is not OK). Otherwise, UI_To_Int will crash. | |
653 | ||
654 | declare | |
655 | UI : constant Uint := | |
16b8ba10 | 656 | (Expr_Value (Hi) - Expr_Value (Lo) + 1) * Siz; |
457cee0b AC |
657 | begin |
658 | if UI_Is_In_Int_Range (UI) then | |
16b8ba10 | 659 | return UI_To_Int (UI); |
457cee0b AC |
660 | else |
661 | return Int'Last; | |
662 | end if; | |
663 | end; | |
643a0839 ES |
664 | end if; |
665 | end; | |
666 | ||
667 | else | |
668 | -- Can only be a null for an access type | |
669 | ||
670 | return 1; | |
671 | end if; | |
672 | end Component_Count; | |
673 | ||
674 | -- Start of processing for Aggr_Size_OK | |
675 | ||
676 | begin | |
c42006e9 | 677 | -- We bump the maximum size unless the aggregate has a single component |
b9ec8463 | 678 | -- association, which will be more efficient if implemented with a loop. |
73b670e3 | 679 | -- The -gnatd_g switch disables this bumping. |
b9ec8463 | 680 | |
73b670e3 BD |
681 | if (No (Expressions (N)) |
682 | and then No (Next (First (Component_Associations (N))))) | |
683 | or else Debug_Flag_Underscore_G | |
b9ec8463 | 684 | then |
eaf6e63a | 685 | Max_Aggr_Size := Max_Aggregate_Size (N); |
c42006e9 | 686 | else |
eaf6e63a | 687 | Max_Aggr_Size := Max_Aggregate_Size (N, 500_000); |
303fbb20 AC |
688 | end if; |
689 | ||
4167b075 | 690 | Size := UI_From_Int (Component_Count (Component_Type (Typ))); |
643a0839 | 691 | |
5277cab6 | 692 | Indx := First_Index (Typ); |
643a0839 ES |
693 | while Present (Indx) loop |
694 | Lo := Type_Low_Bound (Etype (Indx)); | |
695 | Hi := Type_High_Bound (Etype (Indx)); | |
696 | ||
697 | -- Bounds need to be known at compile time | |
698 | ||
699 | if not Compile_Time_Known_Value (Lo) | |
700 | or else not Compile_Time_Known_Value (Hi) | |
701 | then | |
702 | return False; | |
703 | end if; | |
704 | ||
705 | Lov := Expr_Value (Lo); | |
706 | Hiv := Expr_Value (Hi); | |
707 | ||
708 | -- A flat array is always safe | |
709 | ||
710 | if Hiv < Lov then | |
711 | return True; | |
712 | end if; | |
713 | ||
86038a88 | 714 | -- One-component aggregates are suspicious, and if the context type |
d940c627 | 715 | -- is an object declaration with nonstatic bounds it will trip gcc; |
86038a88 | 716 | -- such an aggregate must be expanded into a single assignment. |
58fda84d | 717 | |
36a66365 | 718 | if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then |
58fda84d ES |
719 | declare |
720 | Index_Type : constant Entity_Id := | |
15f0f591 AC |
721 | Etype |
722 | (First_Index (Etype (Defining_Identifier (Parent (N))))); | |
86038a88 RD |
723 | Indx : Node_Id; |
724 | ||
58fda84d ES |
725 | begin |
726 | if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type)) | |
36a66365 AC |
727 | or else not Compile_Time_Known_Value |
728 | (Type_High_Bound (Index_Type)) | |
58fda84d ES |
729 | then |
730 | if Present (Component_Associations (N)) then | |
731 | Indx := | |
00f45f30 AC |
732 | First |
733 | (Choice_List (First (Component_Associations (N)))); | |
324ac540 | 734 | |
58fda84d ES |
735 | if Is_Entity_Name (Indx) |
736 | and then not Is_Type (Entity (Indx)) | |
737 | then | |
738 | Error_Msg_N | |
324ac540 AC |
739 | ("single component aggregate in " |
740 | & "non-static context??", Indx); | |
741 | Error_Msg_N ("\maybe subtype name was meant??", Indx); | |
58fda84d ES |
742 | end if; |
743 | end if; | |
744 | ||
745 | return False; | |
746 | end if; | |
747 | end; | |
748 | end if; | |
749 | ||
643a0839 ES |
750 | declare |
751 | Rng : constant Uint := Hiv - Lov + 1; | |
752 | ||
753 | begin | |
754 | -- Check if size is too large | |
755 | ||
756 | if not UI_Is_In_Int_Range (Rng) then | |
757 | return False; | |
758 | end if; | |
759 | ||
4167b075 GD |
760 | -- Compute the size using universal arithmetic to avoid the |
761 | -- possibility of overflow on very large aggregates. | |
643a0839 | 762 | |
4167b075 GD |
763 | Size := Size * Rng; |
764 | ||
765 | if Size <= 0 | |
766 | or else Size > Max_Aggr_Size | |
767 | then | |
768 | return False; | |
769 | end if; | |
770 | end; | |
643a0839 ES |
771 | |
772 | -- Bounds must be in integer range, for later array construction | |
773 | ||
774 | if not UI_Is_In_Int_Range (Lov) | |
775 | or else | |
776 | not UI_Is_In_Int_Range (Hiv) | |
777 | then | |
778 | return False; | |
779 | end if; | |
780 | ||
781 | Next_Index (Indx); | |
782 | end loop; | |
783 | ||
784 | return True; | |
785 | end Aggr_Size_OK; | |
786 | ||
70482933 RK |
787 | --------------------------------- |
788 | -- Backend_Processing_Possible -- | |
789 | --------------------------------- | |
790 | ||
791 | -- Backend processing by Gigi/gcc is possible only if all the following | |
792 | -- conditions are met: | |
793 | ||
794 | -- 1. N is fully positional | |
795 | ||
796 | -- 2. N is not a bit-packed array aggregate; | |
797 | ||
798 | -- 3. The size of N's array type must be known at compile time. Note | |
799 | -- that this implies that the component size is also known | |
800 | ||
801 | -- 4. The array type of N does not follow the Fortran layout convention | |
802 | -- or if it does it must be 1 dimensional. | |
803 | ||
0f95b178 JM |
804 | -- 5. The array component type may not be tagged (which could necessitate |
805 | -- reassignment of proper tags). | |
70482933 | 806 | |
0f95b178 JM |
807 | -- 6. The array component type must not have unaligned bit components |
808 | ||
809 | -- 7. None of the components of the aggregate may be bit unaligned | |
810 | -- components. | |
811 | ||
812 | -- 8. There cannot be delayed components, since we do not know enough | |
813 | -- at this stage to know if back end processing is possible. | |
814 | ||
815 | -- 9. There cannot be any discriminated record components, since the | |
816 | -- back end cannot handle this complex case. | |
91b1417d | 817 | |
7f4c1903 | 818 | -- 10. No controlled actions need to be generated for components |
a8f59a33 | 819 | |
7e22a38c AC |
820 | -- 11. When generating C code, N must be part of a N_Object_Declaration |
821 | ||
2d6aa715 AC |
822 | -- 12. When generating C code, N must not include function calls |
823 | ||
70482933 RK |
824 | function Backend_Processing_Possible (N : Node_Id) return Boolean is |
825 | Typ : constant Entity_Id := Etype (N); | |
3cf3e5c6 | 826 | -- Typ is the correct constrained array subtype of the aggregate |
70482933 | 827 | |
0f95b178 JM |
828 | function Component_Check (N : Node_Id; Index : Node_Id) return Boolean; |
829 | -- This routine checks components of aggregate N, enforcing checks | |
d74716b3 | 830 | -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks |
2d6aa715 | 831 | -- are performed on subaggregates. The Index value is the current index |
d74716b3 | 832 | -- being checked in the multidimensional case. |
70482933 | 833 | |
0f95b178 JM |
834 | --------------------- |
835 | -- Component_Check -- | |
836 | --------------------- | |
70482933 | 837 | |
0f95b178 | 838 | function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is |
35f4f238 JM |
839 | function Ultimate_Original_Expression (N : Node_Id) return Node_Id; |
840 | -- Given a type conversion or an unchecked type conversion N, return | |
841 | -- its innermost original expression. | |
842 | ||
843 | ---------------------------------- | |
844 | -- Ultimate_Original_Expression -- | |
845 | ---------------------------------- | |
846 | ||
847 | function Ultimate_Original_Expression (N : Node_Id) return Node_Id is | |
848 | Expr : Node_Id := Original_Node (N); | |
849 | ||
850 | begin | |
4a08c95c AC |
851 | while Nkind (Expr) in |
852 | N_Type_Conversion | N_Unchecked_Type_Conversion | |
35f4f238 JM |
853 | loop |
854 | Expr := Original_Node (Expression (Expr)); | |
855 | end loop; | |
856 | ||
857 | return Expr; | |
858 | end Ultimate_Original_Expression; | |
859 | ||
860 | -- Local variables | |
861 | ||
70482933 RK |
862 | Expr : Node_Id; |
863 | ||
bbe008b6 HK |
864 | -- Start of processing for Component_Check |
865 | ||
70482933 | 866 | begin |
0f95b178 | 867 | -- Checks 1: (no component associations) |
70482933 RK |
868 | |
869 | if Present (Component_Associations (N)) then | |
870 | return False; | |
871 | end if; | |
872 | ||
7ec25b2b AC |
873 | -- Checks 11: The C code generator cannot handle aggregates that are |
874 | -- not part of an object declaration. | |
7e22a38c | 875 | |
4ff5aa0c AC |
876 | if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then |
877 | return False; | |
7e22a38c AC |
878 | end if; |
879 | ||
0f95b178 JM |
880 | -- Checks on components |
881 | ||
70482933 RK |
882 | -- Recurse to check subaggregates, which may appear in qualified |
883 | -- expressions. If delayed, the front-end will have to expand. | |
d940c627 | 884 | -- If the component is a discriminated record, treat as nonstatic, |
5277cab6 | 885 | -- as the back-end cannot handle this properly. |
70482933 RK |
886 | |
887 | Expr := First (Expressions (N)); | |
70482933 | 888 | while Present (Expr) loop |
0f95b178 JM |
889 | |
890 | -- Checks 8: (no delayed components) | |
891 | ||
70482933 RK |
892 | if Is_Delayed_Aggregate (Expr) then |
893 | return False; | |
894 | end if; | |
895 | ||
0f95b178 JM |
896 | -- Checks 9: (no discriminated records) |
897 | ||
5277cab6 ES |
898 | if Present (Etype (Expr)) |
899 | and then Is_Record_Type (Etype (Expr)) | |
900 | and then Has_Discriminants (Etype (Expr)) | |
901 | then | |
902 | return False; | |
903 | end if; | |
904 | ||
0f95b178 JM |
905 | -- Checks 7. Component must not be bit aligned component |
906 | ||
907 | if Possible_Bit_Aligned_Component (Expr) then | |
908 | return False; | |
909 | end if; | |
910 | ||
2d6aa715 AC |
911 | -- Checks 12: (no function call) |
912 | ||
35f4f238 JM |
913 | if Modify_Tree_For_C |
914 | and then | |
915 | Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call | |
916 | then | |
2d6aa715 AC |
917 | return False; |
918 | end if; | |
919 | ||
0f95b178 JM |
920 | -- Recursion to following indexes for multiple dimension case |
921 | ||
70482933 | 922 | if Present (Next_Index (Index)) |
36a66365 | 923 | and then not Component_Check (Expr, Next_Index (Index)) |
70482933 RK |
924 | then |
925 | return False; | |
926 | end if; | |
927 | ||
0f95b178 JM |
928 | -- All checks for that component finished, on to next |
929 | ||
70482933 RK |
930 | Next (Expr); |
931 | end loop; | |
932 | ||
933 | return True; | |
0f95b178 | 934 | end Component_Check; |
70482933 RK |
935 | |
936 | -- Start of processing for Backend_Processing_Possible | |
937 | ||
938 | begin | |
a8f59a33 | 939 | -- Checks 2 (array not bit packed) and 10 (no controlled actions) |
70482933 | 940 | |
a8f59a33 | 941 | if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then |
70482933 RK |
942 | return False; |
943 | end if; | |
944 | ||
a38ff9b1 ES |
945 | -- If component is limited, aggregate must be expanded because each |
946 | -- component assignment must be built in place. | |
947 | ||
51245e2d | 948 | if Is_Limited_View (Component_Type (Typ)) then |
a38ff9b1 ES |
949 | return False; |
950 | end if; | |
951 | ||
d74716b3 | 952 | -- Checks 4 (array must not be multidimensional Fortran case) |
70482933 RK |
953 | |
954 | if Convention (Typ) = Convention_Fortran | |
955 | and then Number_Dimensions (Typ) > 1 | |
956 | then | |
957 | return False; | |
958 | end if; | |
959 | ||
960 | -- Checks 3 (size of array must be known at compile time) | |
961 | ||
962 | if not Size_Known_At_Compile_Time (Typ) then | |
963 | return False; | |
964 | end if; | |
965 | ||
0f95b178 | 966 | -- Checks on components |
70482933 | 967 | |
0f95b178 | 968 | if not Component_Check (N, First_Index (Typ)) then |
70482933 RK |
969 | return False; |
970 | end if; | |
971 | ||
0f95b178 | 972 | -- Checks 5 (if the component type is tagged, then we may need to do |
36a66365 AC |
973 | -- tag adjustments. Perhaps this should be refined to check for any |
974 | -- component associations that actually need tag adjustment, similar | |
d4dfb005 BD |
975 | -- to the test in Component_OK_For_Backend for record aggregates with |
976 | -- tagged components, but not clear whether it's worthwhile ???; in the | |
977 | -- case of virtual machines (no Tagged_Type_Expansion), object tags are | |
978 | -- handled implicitly). | |
70482933 | 979 | |
1f110335 AC |
980 | if Is_Tagged_Type (Component_Type (Typ)) |
981 | and then Tagged_Type_Expansion | |
982 | then | |
70482933 RK |
983 | return False; |
984 | end if; | |
985 | ||
91b1417d AC |
986 | -- Checks 6 (component type must not have bit aligned components) |
987 | ||
988 | if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then | |
989 | return False; | |
990 | end if; | |
991 | ||
70482933 RK |
992 | -- Backend processing is possible |
993 | ||
70482933 RK |
994 | return True; |
995 | end Backend_Processing_Possible; | |
996 | ||
997 | --------------------------- | |
998 | -- Build_Array_Aggr_Code -- | |
999 | --------------------------- | |
1000 | ||
1001 | -- The code that we generate from a one dimensional aggregate is | |
1002 | ||
d74716b3 | 1003 | -- 1. If the subaggregate contains discrete choices we |
70482933 RK |
1004 | |
1005 | -- (a) Sort the discrete choices | |
1006 | ||
1007 | -- (b) Otherwise for each discrete choice that specifies a range we | |
1008 | -- emit a loop. If a range specifies a maximum of three values, or | |
1009 | -- we are dealing with an expression we emit a sequence of | |
1010 | -- assignments instead of a loop. | |
1011 | ||
3cf3e5c6 | 1012 | -- (c) Generate the remaining loops to cover the others choice if any |
70482933 RK |
1013 | |
1014 | -- 2. If the aggregate contains positional elements we | |
1015 | ||
8313d0ee | 1016 | -- (a) Translate the positional elements in a series of assignments |
70482933 RK |
1017 | |
1018 | -- (b) Generate a final loop to cover the others choice if any. | |
1019 | -- Note that this final loop has to be a while loop since the case | |
1020 | ||
1021 | -- L : Integer := Integer'Last; | |
1022 | -- H : Integer := Integer'Last; | |
1023 | -- A : array (L .. H) := (1, others =>0); | |
1024 | ||
1025 | -- cannot be handled by a for loop. Thus for the following | |
1026 | ||
8313d0ee | 1027 | -- array (L .. H) := (.. positional elements.., others => E); |
70482933 RK |
1028 | |
1029 | -- we always generate something like: | |
1030 | ||
07fc65c4 GB |
1031 | -- J : Index_Type := Index_Of_Last_Positional_Element; |
1032 | -- while J < H loop | |
1033 | -- J := Index_Base'Succ (J) | |
1034 | -- Tmp (J) := E; | |
70482933 RK |
1035 | -- end loop; |
1036 | ||
1037 | function Build_Array_Aggr_Code | |
1038 | (N : Node_Id; | |
c45b6ae0 | 1039 | Ctype : Entity_Id; |
70482933 RK |
1040 | Index : Node_Id; |
1041 | Into : Node_Id; | |
1042 | Scalar_Comp : Boolean; | |
df3e68b1 | 1043 | Indexes : List_Id := No_List) return List_Id |
70482933 RK |
1044 | is |
1045 | Loc : constant Source_Ptr := Sloc (N); | |
4f061cf2 | 1046 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
1047 | Index_Base : constant Entity_Id := Base_Type (Etype (Index)); |
1048 | Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base); | |
1049 | Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base); | |
1050 | ||
1051 | function Add (Val : Int; To : Node_Id) return Node_Id; | |
3cf3e5c6 AC |
1052 | -- Returns an expression where Val is added to expression To, unless |
1053 | -- To+Val is provably out of To's base type range. To must be an | |
1054 | -- already analyzed expression. | |
70482933 RK |
1055 | |
1056 | function Empty_Range (L, H : Node_Id) return Boolean; | |
3cf3e5c6 | 1057 | -- Returns True if the range defined by L .. H is certainly empty |
70482933 RK |
1058 | |
1059 | function Equal (L, H : Node_Id) return Boolean; | |
3cf3e5c6 | 1060 | -- Returns True if L = H for sure |
70482933 RK |
1061 | |
1062 | function Index_Base_Name return Node_Id; | |
3cf3e5c6 | 1063 | -- Returns a new reference to the index type name |
70482933 | 1064 | |
937e9676 AC |
1065 | function Gen_Assign |
1066 | (Ind : Node_Id; | |
fc84947c | 1067 | Expr : Node_Id) return List_Id; |
d74716b3 AC |
1068 | -- Ind must be a side-effect-free expression. If the input aggregate N |
1069 | -- to Build_Loop contains no subaggregates, then this function returns | |
1070 | -- the assignment statement: | |
70482933 | 1071 | -- |
deeb1604 | 1072 | -- Into (Indexes, Ind) := Expr; |
70482933 | 1073 | -- |
fc84947c | 1074 | -- Otherwise we call Build_Code recursively. |
c45b6ae0 | 1075 | -- |
0ab80019 AC |
1076 | -- Ada 2005 (AI-287): In case of default initialized component, Expr |
1077 | -- is empty and we generate a call to the corresponding IP subprogram. | |
70482933 RK |
1078 | |
1079 | function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id; | |
d74716b3 AC |
1080 | -- Nodes L and H must be side-effect-free expressions. If the input |
1081 | -- aggregate N to Build_Loop contains no subaggregates, this routine | |
1082 | -- returns the for loop statement: | |
70482933 RK |
1083 | -- |
1084 | -- for J in Index_Base'(L) .. Index_Base'(H) loop | |
deeb1604 | 1085 | -- Into (Indexes, J) := Expr; |
70482933 RK |
1086 | -- end loop; |
1087 | -- | |
937e9676 AC |
1088 | -- Otherwise we call Build_Code recursively. As an optimization if the |
1089 | -- loop covers 3 or fewer scalar elements we generate a sequence of | |
1090 | -- assignments. | |
00f45f30 AC |
1091 | -- If the component association that generates the loop comes from an |
1092 | -- Iterated_Component_Association, the loop parameter has the name of | |
1093 | -- the corresponding parameter in the original construct. | |
70482933 RK |
1094 | |
1095 | function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id; | |
d74716b3 AC |
1096 | -- Nodes L and H must be side-effect-free expressions. If the input |
1097 | -- aggregate N to Build_Loop contains no subaggregates, this routine | |
1098 | -- returns the while loop statement: | |
70482933 | 1099 | -- |
07fc65c4 GB |
1100 | -- J : Index_Base := L; |
1101 | -- while J < H loop | |
1102 | -- J := Index_Base'Succ (J); | |
deeb1604 | 1103 | -- Into (Indexes, J) := Expr; |
70482933 RK |
1104 | -- end loop; |
1105 | -- | |
fbf5a39b | 1106 | -- Otherwise we call Build_Code recursively |
70482933 | 1107 | |
59e9bc0b | 1108 | function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id; |
e9999161 AC |
1109 | -- For an association with a box, use value given by aspect |
1110 | -- Default_Component_Value of array type if specified, else use | |
1111 | -- value given by aspect Default_Value for component type itself | |
1112 | -- if specified, else return Empty. | |
59e9bc0b | 1113 | |
70482933 RK |
1114 | function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean; |
1115 | function Local_Expr_Value (E : Node_Id) return Uint; | |
1116 | -- These two Local routines are used to replace the corresponding ones | |
1117 | -- in sem_eval because while processing the bounds of an aggregate with | |
1118 | -- discrete choices whose index type is an enumeration, we build static | |
1119 | -- expressions not recognized by Compile_Time_Known_Value as such since | |
1120 | -- they have not yet been analyzed and resolved. All the expressions in | |
1121 | -- question are things like Index_Base_Name'Val (Const) which we can | |
1122 | -- easily recognize as being constant. | |
1123 | ||
1124 | --------- | |
1125 | -- Add -- | |
1126 | --------- | |
1127 | ||
1128 | function Add (Val : Int; To : Node_Id) return Node_Id is | |
1129 | Expr_Pos : Node_Id; | |
1130 | Expr : Node_Id; | |
1131 | To_Pos : Node_Id; | |
fbf5a39b AC |
1132 | U_To : Uint; |
1133 | U_Val : constant Uint := UI_From_Int (Val); | |
70482933 RK |
1134 | |
1135 | begin | |
1136 | -- Note: do not try to optimize the case of Val = 0, because | |
1137 | -- we need to build a new node with the proper Sloc value anyway. | |
1138 | ||
1139 | -- First test if we can do constant folding | |
1140 | ||
1141 | if Local_Compile_Time_Known_Value (To) then | |
1142 | U_To := Local_Expr_Value (To) + Val; | |
1143 | ||
1144 | -- Determine if our constant is outside the range of the index. | |
1145 | -- If so return an Empty node. This empty node will be caught | |
1146 | -- by Empty_Range below. | |
1147 | ||
1148 | if Compile_Time_Known_Value (Index_Base_L) | |
1149 | and then U_To < Expr_Value (Index_Base_L) | |
1150 | then | |
1151 | return Empty; | |
1152 | ||
1153 | elsif Compile_Time_Known_Value (Index_Base_H) | |
1154 | and then U_To > Expr_Value (Index_Base_H) | |
1155 | then | |
1156 | return Empty; | |
1157 | end if; | |
1158 | ||
1159 | Expr_Pos := Make_Integer_Literal (Loc, U_To); | |
1160 | Set_Is_Static_Expression (Expr_Pos); | |
1161 | ||
1162 | if not Is_Enumeration_Type (Index_Base) then | |
1163 | Expr := Expr_Pos; | |
1164 | ||
1165 | -- If we are dealing with enumeration return | |
1166 | -- Index_Base'Val (Expr_Pos) | |
1167 | ||
1168 | else | |
1169 | Expr := | |
1170 | Make_Attribute_Reference | |
1171 | (Loc, | |
1172 | Prefix => Index_Base_Name, | |
1173 | Attribute_Name => Name_Val, | |
1174 | Expressions => New_List (Expr_Pos)); | |
1175 | end if; | |
1176 | ||
1177 | return Expr; | |
1178 | end if; | |
1179 | ||
1180 | -- If we are here no constant folding possible | |
1181 | ||
1182 | if not Is_Enumeration_Type (Index_Base) then | |
1183 | Expr := | |
1184 | Make_Op_Add (Loc, | |
47c14114 AC |
1185 | Left_Opnd => Duplicate_Subexpr (To), |
1186 | Right_Opnd => Make_Integer_Literal (Loc, U_Val)); | |
70482933 RK |
1187 | |
1188 | -- If we are dealing with enumeration return | |
1189 | -- Index_Base'Val (Index_Base'Pos (To) + Val) | |
1190 | ||
1191 | else | |
1192 | To_Pos := | |
1193 | Make_Attribute_Reference | |
1194 | (Loc, | |
1195 | Prefix => Index_Base_Name, | |
1196 | Attribute_Name => Name_Pos, | |
1197 | Expressions => New_List (Duplicate_Subexpr (To))); | |
1198 | ||
1199 | Expr_Pos := | |
1200 | Make_Op_Add (Loc, | |
47c14114 AC |
1201 | Left_Opnd => To_Pos, |
1202 | Right_Opnd => Make_Integer_Literal (Loc, U_Val)); | |
70482933 RK |
1203 | |
1204 | Expr := | |
1205 | Make_Attribute_Reference | |
1206 | (Loc, | |
1207 | Prefix => Index_Base_Name, | |
1208 | Attribute_Name => Name_Val, | |
1209 | Expressions => New_List (Expr_Pos)); | |
1210 | end if; | |
1211 | ||
1212 | return Expr; | |
1213 | end Add; | |
1214 | ||
1215 | ----------------- | |
1216 | -- Empty_Range -- | |
1217 | ----------------- | |
1218 | ||
1219 | function Empty_Range (L, H : Node_Id) return Boolean is | |
1220 | Is_Empty : Boolean := False; | |
1221 | Low : Node_Id; | |
1222 | High : Node_Id; | |
1223 | ||
1224 | begin | |
1225 | -- First check if L or H were already detected as overflowing the | |
1226 | -- index base range type by function Add above. If this is so Add | |
1227 | -- returns the empty node. | |
1228 | ||
1229 | if No (L) or else No (H) then | |
1230 | return True; | |
1231 | end if; | |
1232 | ||
1233 | for J in 1 .. 3 loop | |
1234 | case J is | |
1235 | ||
1236 | -- L > H range is empty | |
1237 | ||
1238 | when 1 => | |
1239 | Low := L; | |
1240 | High := H; | |
1241 | ||
1242 | -- B_L > H range must be empty | |
1243 | ||
1244 | when 2 => | |
1245 | Low := Index_Base_L; | |
1246 | High := H; | |
1247 | ||
1248 | -- L > B_H range must be empty | |
1249 | ||
1250 | when 3 => | |
1251 | Low := L; | |
1252 | High := Index_Base_H; | |
1253 | end case; | |
1254 | ||
1255 | if Local_Compile_Time_Known_Value (Low) | |
36a66365 AC |
1256 | and then |
1257 | Local_Compile_Time_Known_Value (High) | |
70482933 RK |
1258 | then |
1259 | Is_Empty := | |
1260 | UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High)); | |
1261 | end if; | |
1262 | ||
1263 | exit when Is_Empty; | |
1264 | end loop; | |
1265 | ||
1266 | return Is_Empty; | |
1267 | end Empty_Range; | |
1268 | ||
1269 | ----------- | |
1270 | -- Equal -- | |
1271 | ----------- | |
1272 | ||
1273 | function Equal (L, H : Node_Id) return Boolean is | |
1274 | begin | |
1275 | if L = H then | |
1276 | return True; | |
1277 | ||
1278 | elsif Local_Compile_Time_Known_Value (L) | |
36a66365 AC |
1279 | and then |
1280 | Local_Compile_Time_Known_Value (H) | |
70482933 RK |
1281 | then |
1282 | return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H)); | |
1283 | end if; | |
1284 | ||
1285 | return False; | |
1286 | end Equal; | |
1287 | ||
1288 | ---------------- | |
1289 | -- Gen_Assign -- | |
1290 | ---------------- | |
1291 | ||
937e9676 AC |
1292 | function Gen_Assign |
1293 | (Ind : Node_Id; | |
fc84947c | 1294 | Expr : Node_Id) return List_Id |
937e9676 | 1295 | is |
70482933 | 1296 | function Add_Loop_Actions (Lis : List_Id) return List_Id; |
937e9676 AC |
1297 | -- Collect insert_actions generated in the construction of a loop, |
1298 | -- and prepend them to the sequence of assignments to complete the | |
1299 | -- eventual body of the loop. | |
1300 | ||
70482933 RK |
1301 | ---------------------- |
1302 | -- Add_Loop_Actions -- | |
1303 | ---------------------- | |
1304 | ||
1305 | function Add_Loop_Actions (Lis : List_Id) return List_Id is | |
1306 | Res : List_Id; | |
1307 | ||
1308 | begin | |
0ab80019 | 1309 | -- Ada 2005 (AI-287): Do nothing else in case of default |
6e937c1c | 1310 | -- initialized component. |
c45b6ae0 | 1311 | |
d8f7b976 | 1312 | if No (Expr) then |
c45b6ae0 AC |
1313 | return Lis; |
1314 | ||
1315 | elsif Nkind (Parent (Expr)) = N_Component_Association | |
70482933 RK |
1316 | and then Present (Loop_Actions (Parent (Expr))) |
1317 | then | |
1318 | Append_List (Lis, Loop_Actions (Parent (Expr))); | |
1319 | Res := Loop_Actions (Parent (Expr)); | |
1320 | Set_Loop_Actions (Parent (Expr), No_List); | |
1321 | return Res; | |
1322 | ||
1323 | else | |
1324 | return Lis; | |
1325 | end if; | |
1326 | end Add_Loop_Actions; | |
1327 | ||
10edebe7 AC |
1328 | -- Local variables |
1329 | ||
1330 | Stmts : constant List_Id := New_List; | |
1331 | ||
1332 | Comp_Typ : Entity_Id := Empty; | |
1333 | Expr_Q : Node_Id; | |
1334 | Indexed_Comp : Node_Id; | |
2168d7cc | 1335 | Init_Call : Node_Id; |
10edebe7 | 1336 | New_Indexes : List_Id; |
10edebe7 | 1337 | |
70482933 RK |
1338 | -- Start of processing for Gen_Assign |
1339 | ||
1340 | begin | |
deeb1604 AC |
1341 | if No (Indexes) then |
1342 | New_Indexes := New_List; | |
70482933 | 1343 | else |
deeb1604 | 1344 | New_Indexes := New_Copy_List_Tree (Indexes); |
70482933 RK |
1345 | end if; |
1346 | ||
deeb1604 | 1347 | Append_To (New_Indexes, Ind); |
70482933 | 1348 | |
70482933 RK |
1349 | if Present (Next_Index (Index)) then |
1350 | return | |
1351 | Add_Loop_Actions ( | |
1352 | Build_Array_Aggr_Code | |
c45b6ae0 AC |
1353 | (N => Expr, |
1354 | Ctype => Ctype, | |
1355 | Index => Next_Index (Index), | |
1356 | Into => Into, | |
1357 | Scalar_Comp => Scalar_Comp, | |
df3e68b1 | 1358 | Indexes => New_Indexes)); |
70482933 RK |
1359 | end if; |
1360 | ||
1361 | -- If we get here then we are at a bottom-level (sub-)aggregate | |
1362 | ||
fbf5a39b AC |
1363 | Indexed_Comp := |
1364 | Checks_Off | |
1365 | (Make_Indexed_Component (Loc, | |
1366 | Prefix => New_Copy_Tree (Into), | |
deeb1604 | 1367 | Expressions => New_Indexes)); |
70482933 RK |
1368 | |
1369 | Set_Assignment_OK (Indexed_Comp); | |
1370 | ||
0ab80019 | 1371 | -- Ada 2005 (AI-287): In case of default initialized component, Expr |
6e937c1c | 1372 | -- is not present (and therefore we also initialize Expr_Q to empty). |
c45b6ae0 | 1373 | |
4f061cf2 | 1374 | Expr_Q := Unqualify (Expr); |
70482933 | 1375 | |
36a66365 | 1376 | if Present (Etype (N)) and then Etype (N) /= Any_Composite then |
10edebe7 AC |
1377 | Comp_Typ := Component_Type (Etype (N)); |
1378 | pragma Assert (Comp_Typ = Ctype); -- AI-287 | |
70482933 | 1379 | |
deeb1604 | 1380 | elsif Present (Next (First (New_Indexes))) then |
70482933 | 1381 | |
0ab80019 | 1382 | -- Ada 2005 (AI-287): Do nothing in case of default initialized |
c45b6ae0 AC |
1383 | -- component because we have received the component type in |
1384 | -- the formal parameter Ctype. | |
6e937c1c AC |
1385 | |
1386 | -- ??? Some assert pragmas have been added to check if this new | |
36a66365 | 1387 | -- formal can be used to replace this code in all cases. |
70482933 | 1388 | |
c45b6ae0 | 1389 | if Present (Expr) then |
70482933 | 1390 | |
36a66365 AC |
1391 | -- This is a multidimensional array. Recover the component type |
1392 | -- from the outermost aggregate, because subaggregates do not | |
1393 | -- have an assigned type. | |
70482933 | 1394 | |
c45b6ae0 | 1395 | declare |
5277cab6 | 1396 | P : Node_Id; |
70482933 | 1397 | |
c45b6ae0 | 1398 | begin |
5277cab6 | 1399 | P := Parent (Expr); |
c45b6ae0 | 1400 | while Present (P) loop |
c45b6ae0 AC |
1401 | if Nkind (P) = N_Aggregate |
1402 | and then Present (Etype (P)) | |
1403 | then | |
10edebe7 | 1404 | Comp_Typ := Component_Type (Etype (P)); |
c45b6ae0 AC |
1405 | exit; |
1406 | ||
1407 | else | |
1408 | P := Parent (P); | |
1409 | end if; | |
1410 | end loop; | |
6e937c1c | 1411 | |
10edebe7 | 1412 | pragma Assert (Comp_Typ = Ctype); -- AI-287 |
c45b6ae0 AC |
1413 | end; |
1414 | end if; | |
70482933 RK |
1415 | end if; |
1416 | ||
0ab80019 | 1417 | -- Ada 2005 (AI-287): We only analyze the expression in case of non- |
6e937c1c | 1418 | -- default initialized components (otherwise Expr_Q is not present). |
c45b6ae0 AC |
1419 | |
1420 | if Present (Expr_Q) | |
4a08c95c | 1421 | and then Nkind (Expr_Q) in N_Aggregate | N_Extension_Aggregate |
70482933 | 1422 | then |
d7f94401 AC |
1423 | -- At this stage the Expression may not have been analyzed yet |
1424 | -- because the array aggregate code has not been updated to use | |
1425 | -- the Expansion_Delayed flag and avoid analysis altogether to | |
1426 | -- solve the same problem (see Resolve_Aggr_Expr). So let us do | |
1427 | -- the analysis of non-array aggregates now in order to get the | |
1428 | -- value of Expansion_Delayed flag for the inner aggregate ??? | |
70482933 | 1429 | |
6cbd45e4 PMR |
1430 | -- In the case of an iterated component association, the analysis |
1431 | -- of the generated loop will analyze the expression in the | |
1432 | -- proper context, in which the loop parameter is visible. | |
1433 | ||
d940c627 | 1434 | if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then |
10fdda1c HK |
1435 | if Nkind (Parent (Expr_Q)) = N_Iterated_Component_Association |
1436 | or else Nkind (Parent (Parent ((Expr_Q)))) = | |
1437 | N_Iterated_Component_Association | |
d940c627 ES |
1438 | then |
1439 | null; | |
1440 | else | |
1441 | Analyze_And_Resolve (Expr_Q, Comp_Typ); | |
1442 | end if; | |
70482933 RK |
1443 | end if; |
1444 | ||
1445 | if Is_Delayed_Aggregate (Expr_Q) then | |
3cf3e5c6 | 1446 | |
308e6f3a | 1447 | -- This is either a subaggregate of a multidimensional array, |
3cf3e5c6 AC |
1448 | -- or a component of an array type whose component type is |
1449 | -- also an array. In the latter case, the expression may have | |
1450 | -- component associations that provide different bounds from | |
1451 | -- those of the component type, and sliding must occur. Instead | |
1452 | -- of decomposing the current aggregate assignment, force the | |
937e9676 | 1453 | -- reanalysis of the assignment, so that a temporary will be |
3cf3e5c6 AC |
1454 | -- generated in the usual fashion, and sliding will take place. |
1455 | ||
1456 | if Nkind (Parent (N)) = N_Assignment_Statement | |
10edebe7 | 1457 | and then Is_Array_Type (Comp_Typ) |
3cf3e5c6 | 1458 | and then Present (Component_Associations (Expr_Q)) |
6732c403 | 1459 | and then Must_Slide (N, Comp_Typ, Etype (Expr_Q)) |
3cf3e5c6 AC |
1460 | then |
1461 | Set_Expansion_Delayed (Expr_Q, False); | |
1462 | Set_Analyzed (Expr_Q, False); | |
1463 | ||
1464 | else | |
1465 | return | |
1466 | Add_Loop_Actions ( | |
df3e68b1 | 1467 | Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp)); |
3cf3e5c6 | 1468 | end if; |
70482933 RK |
1469 | end if; |
1470 | end if; | |
1471 | ||
937e9676 | 1472 | if Present (Expr) then |
fc84947c EB |
1473 | Initialize_Component |
1474 | (N => N, | |
1475 | Comp => Indexed_Comp, | |
1476 | Comp_Typ => Comp_Typ, | |
1477 | Init_Expr => Expr, | |
1478 | Stmts => Stmts); | |
937e9676 | 1479 | |
0ab80019 | 1480 | -- Ada 2005 (AI-287): In case of default initialized component, call |
6e937c1c | 1481 | -- the initialization subprogram associated with the component type. |
3b9fa2df ES |
1482 | -- If the component type is an access type, add an explicit null |
1483 | -- assignment, because for the back-end there is an initialization | |
1484 | -- present for the whole aggregate, and no default initialization | |
1485 | -- will take place. | |
1486 | ||
1487 | -- In addition, if the component type is controlled, we must call | |
1488 | -- its Initialize procedure explicitly, because there is no explicit | |
1489 | -- object creation that will invoke it otherwise. | |
70482933 | 1490 | |
937e9676 | 1491 | else |
3b9fa2df | 1492 | if Present (Base_Init_Proc (Base_Type (Ctype))) |
615cbd95 AC |
1493 | or else Has_Task (Base_Type (Ctype)) |
1494 | then | |
10edebe7 | 1495 | Append_List_To (Stmts, |
c45b6ae0 AC |
1496 | Build_Initialization_Call (Loc, |
1497 | Id_Ref => Indexed_Comp, | |
1498 | Typ => Ctype, | |
1499 | With_Default_Init => True)); | |
3b9fa2df | 1500 | |
ffcfb997 ES |
1501 | -- If the component type has invariants, add an invariant |
1502 | -- check after the component is default-initialized. It will | |
1503 | -- be analyzed and resolved before the code for initialization | |
1504 | -- of other components. | |
1505 | ||
1506 | if Has_Invariants (Ctype) then | |
1507 | Set_Etype (Indexed_Comp, Ctype); | |
10edebe7 | 1508 | Append_To (Stmts, Make_Invariant_Call (Indexed_Comp)); |
ffcfb997 | 1509 | end if; |
3b9fa2df ES |
1510 | end if; |
1511 | ||
048e5cef | 1512 | if Needs_Finalization (Ctype) then |
2168d7cc | 1513 | Init_Call := |
37368818 RD |
1514 | Make_Init_Call |
1515 | (Obj_Ref => New_Copy_Tree (Indexed_Comp), | |
2168d7cc AC |
1516 | Typ => Ctype); |
1517 | ||
1518 | -- Guard against a missing [Deep_]Initialize when the component | |
1519 | -- type was not properly frozen. | |
1520 | ||
1521 | if Present (Init_Call) then | |
1522 | Append_To (Stmts, Init_Call); | |
1523 | end if; | |
615cbd95 | 1524 | end if; |
097826df GD |
1525 | |
1526 | -- If Default_Initial_Condition applies to the component type, | |
1527 | -- add a DIC check after the component is default-initialized, | |
1528 | -- as well as after an Initialize procedure is called, in the | |
1529 | -- case of components of a controlled type. It will be analyzed | |
1530 | -- and resolved before the code for initialization of other | |
1531 | -- components. | |
1532 | ||
1533 | -- Theoretically this might also be needed for cases where Expr | |
1534 | -- is not empty, but a default init still applies, such as for | |
1535 | -- Default_Value cases, in which case we won't get here. ??? | |
1536 | ||
1537 | if Has_DIC (Ctype) and then Present (DIC_Procedure (Ctype)) then | |
1538 | Append_To (Stmts, | |
1539 | Build_DIC_Call (Loc, New_Copy_Tree (Indexed_Comp), Ctype)); | |
1540 | end if; | |
70482933 RK |
1541 | end if; |
1542 | ||
10edebe7 | 1543 | return Add_Loop_Actions (Stmts); |
70482933 RK |
1544 | end Gen_Assign; |
1545 | ||
1546 | -------------- | |
1547 | -- Gen_Loop -- | |
1548 | -------------- | |
1549 | ||
1550 | function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is | |
00f45f30 | 1551 | Is_Iterated_Component : constant Boolean := |
898edf75 | 1552 | Parent_Kind (Expr) = N_Iterated_Component_Association; |
00f45f30 | 1553 | |
e3584606 MP |
1554 | Ent : Entity_Id; |
1555 | ||
07fc65c4 | 1556 | L_J : Node_Id; |
70482933 | 1557 | |
240fe2a4 AC |
1558 | L_L : Node_Id; |
1559 | -- Index_Base'(L) | |
1560 | ||
1561 | L_H : Node_Id; | |
1562 | -- Index_Base'(H) | |
1563 | ||
70482933 RK |
1564 | L_Range : Node_Id; |
1565 | -- Index_Base'(L) .. Index_Base'(H) | |
1566 | ||
1567 | L_Iteration_Scheme : Node_Id; | |
07fc65c4 | 1568 | -- L_J in Index_Base'(L) .. Index_Base'(H) |
70482933 RK |
1569 | |
1570 | L_Body : List_Id; | |
1571 | -- The statements to execute in the loop | |
1572 | ||
fbf5a39b AC |
1573 | S : constant List_Id := New_List; |
1574 | -- List of statements | |
70482933 RK |
1575 | |
1576 | Tcopy : Node_Id; | |
1577 | -- Copy of expression tree, used for checking purposes | |
1578 | ||
1579 | begin | |
1580 | -- If loop bounds define an empty range return the null statement | |
1581 | ||
1582 | if Empty_Range (L, H) then | |
1583 | Append_To (S, Make_Null_Statement (Loc)); | |
1584 | ||
0ab80019 | 1585 | -- Ada 2005 (AI-287): Nothing else need to be done in case of |
6e937c1c | 1586 | -- default initialized component. |
70482933 | 1587 | |
d8f7b976 | 1588 | if No (Expr) then |
c45b6ae0 AC |
1589 | null; |
1590 | ||
1591 | else | |
1592 | -- The expression must be type-checked even though no component | |
1593 | -- of the aggregate will have this value. This is done only for | |
1594 | -- actual components of the array, not for subaggregates. Do | |
1595 | -- the check on a copy, because the expression may be shared | |
1596 | -- among several choices, some of which might be non-null. | |
1597 | ||
1598 | if Present (Etype (N)) | |
1599 | and then Is_Array_Type (Etype (N)) | |
1600 | and then No (Next_Index (Index)) | |
1601 | then | |
1602 | Expander_Mode_Save_And_Set (False); | |
1603 | Tcopy := New_Copy_Tree (Expr); | |
1604 | Set_Parent (Tcopy, N); | |
4270e945 PT |
1605 | |
1606 | -- For iterated_component_association analyze and resolve | |
1607 | -- the expression with name of the index parameter visible. | |
1608 | -- To manipulate scopes, we use entity of the implicit loop. | |
1609 | ||
1610 | if Is_Iterated_Component then | |
1611 | declare | |
1612 | Index_Parameter : constant Entity_Id := | |
1613 | Defining_Identifier (Parent (Expr)); | |
1614 | begin | |
1615 | Push_Scope (Scope (Index_Parameter)); | |
1616 | Enter_Name (Index_Parameter); | |
1617 | Analyze_And_Resolve | |
1618 | (Tcopy, Component_Type (Etype (N))); | |
1619 | End_Scope; | |
1620 | end; | |
1621 | ||
1622 | -- For ordinary component association, just analyze and | |
1623 | -- resolve the expression. | |
1624 | ||
1625 | else | |
1626 | Analyze_And_Resolve (Tcopy, Component_Type (Etype (N))); | |
1627 | end if; | |
1628 | ||
c45b6ae0 AC |
1629 | Expander_Mode_Restore; |
1630 | end if; | |
70482933 RK |
1631 | end if; |
1632 | ||
1633 | return S; | |
1634 | ||
00f45f30 AC |
1635 | -- If loop bounds are the same then generate an assignment, unless |
1636 | -- the parent construct is an Iterated_Component_Association. | |
70482933 | 1637 | |
00f45f30 | 1638 | elsif Equal (L, H) and then not Is_Iterated_Component then |
70482933 RK |
1639 | return Gen_Assign (New_Copy_Tree (L), Expr); |
1640 | ||
3b9fa2df ES |
1641 | -- If H - L <= 2 then generate a sequence of assignments when we are |
1642 | -- processing the bottom most aggregate and it contains scalar | |
1643 | -- components. | |
70482933 RK |
1644 | |
1645 | elsif No (Next_Index (Index)) | |
1646 | and then Scalar_Comp | |
1647 | and then Local_Compile_Time_Known_Value (L) | |
1648 | and then Local_Compile_Time_Known_Value (H) | |
1649 | and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2 | |
00f45f30 | 1650 | and then not Is_Iterated_Component |
70482933 RK |
1651 | then |
1652 | Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr)); | |
1653 | Append_List_To (S, Gen_Assign (Add (1, To => L), Expr)); | |
1654 | ||
1655 | if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then | |
1656 | Append_List_To (S, Gen_Assign (Add (2, To => L), Expr)); | |
1657 | end if; | |
1658 | ||
1659 | return S; | |
1660 | end if; | |
1661 | ||
07fc65c4 | 1662 | -- Otherwise construct the loop, starting with the loop index L_J |
70482933 | 1663 | |
00f45f30 | 1664 | if Is_Iterated_Component then |
e3584606 MP |
1665 | |
1666 | -- Create a new scope for the loop variable so that the | |
1667 | -- following Gen_Assign (that ends up calling | |
1668 | -- Preanalyze_And_Resolve) can correctly find it. | |
1669 | ||
1670 | Ent := New_Internal_Entity (E_Loop, | |
1671 | Current_Scope, Loc, 'L'); | |
1672 | Set_Etype (Ent, Standard_Void_Type); | |
1673 | Set_Parent (Ent, Parent (Parent (Expr))); | |
1674 | Push_Scope (Ent); | |
1675 | ||
72cdccfa HK |
1676 | L_J := |
1677 | Make_Defining_Identifier (Loc, | |
1678 | Chars => (Chars (Defining_Identifier (Parent (Expr))))); | |
00f45f30 | 1679 | |
e3584606 MP |
1680 | Enter_Name (L_J); |
1681 | ||
1682 | -- The Etype will be set by a later Analyze call. | |
1683 | Set_Etype (L_J, Any_Type); | |
1684 | ||
1685 | Mutate_Ekind (L_J, E_Variable); | |
25e4024c | 1686 | Set_Is_Not_Self_Hidden (L_J); |
e3584606 | 1687 | Set_Scope (L_J, Ent); |
00f45f30 AC |
1688 | else |
1689 | L_J := Make_Temporary (Loc, 'J', L); | |
1690 | end if; | |
70482933 | 1691 | |
240fe2a4 AC |
1692 | -- Construct "L .. H" in Index_Base. We use a qualified expression |
1693 | -- for the bound to convert to the index base, but we don't need | |
1694 | -- to do that if we already have the base type at hand. | |
1695 | ||
1696 | if Etype (L) = Index_Base then | |
4eac8834 | 1697 | L_L := New_Copy_Tree (L); |
240fe2a4 AC |
1698 | else |
1699 | L_L := | |
1700 | Make_Qualified_Expression (Loc, | |
1701 | Subtype_Mark => Index_Base_Name, | |
00f45f30 | 1702 | Expression => New_Copy_Tree (L)); |
240fe2a4 AC |
1703 | end if; |
1704 | ||
1705 | if Etype (H) = Index_Base then | |
4eac8834 | 1706 | L_H := New_Copy_Tree (H); |
240fe2a4 AC |
1707 | else |
1708 | L_H := | |
1709 | Make_Qualified_Expression (Loc, | |
1710 | Subtype_Mark => Index_Base_Name, | |
00f45f30 | 1711 | Expression => New_Copy_Tree (H)); |
240fe2a4 | 1712 | end if; |
70482933 RK |
1713 | |
1714 | L_Range := | |
240fe2a4 | 1715 | Make_Range (Loc, |
ffcfb997 | 1716 | Low_Bound => L_L, |
240fe2a4 | 1717 | High_Bound => L_H); |
70482933 | 1718 | |
07fc65c4 | 1719 | -- Construct "for L_J in Index_Base range L .. H" |
70482933 RK |
1720 | |
1721 | L_Iteration_Scheme := | |
8313d0ee | 1722 | Make_Iteration_Scheme (Loc, |
70482933 | 1723 | Loop_Parameter_Specification => |
8313d0ee | 1724 | Make_Loop_Parameter_Specification (Loc, |
07fc65c4 | 1725 | Defining_Identifier => L_J, |
70482933 RK |
1726 | Discrete_Subtype_Definition => L_Range)); |
1727 | ||
1728 | -- Construct the statements to execute in the loop body | |
1729 | ||
fc84947c | 1730 | L_Body := Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr); |
70482933 RK |
1731 | |
1732 | -- Construct the final loop | |
1733 | ||
37368818 RD |
1734 | Append_To (S, |
1735 | Make_Implicit_Loop_Statement | |
1736 | (Node => N, | |
1737 | Identifier => Empty, | |
1738 | Iteration_Scheme => L_Iteration_Scheme, | |
1739 | Statements => L_Body)); | |
70482933 | 1740 | |
e3584606 MP |
1741 | if Is_Iterated_Component then |
1742 | End_Scope; | |
1743 | end if; | |
1744 | ||
3b9fa2df ES |
1745 | -- A small optimization: if the aggregate is initialized with a box |
1746 | -- and the component type has no initialization procedure, remove the | |
1747 | -- useless empty loop. | |
0f95b178 JM |
1748 | |
1749 | if Nkind (First (S)) = N_Loop_Statement | |
1750 | and then Is_Empty_List (Statements (First (S))) | |
1751 | then | |
1752 | return New_List (Make_Null_Statement (Loc)); | |
1753 | else | |
1754 | return S; | |
1755 | end if; | |
70482933 RK |
1756 | end Gen_Loop; |
1757 | ||
1758 | --------------- | |
1759 | -- Gen_While -- | |
1760 | --------------- | |
1761 | ||
1762 | -- The code built is | |
1763 | ||
07fc65c4 GB |
1764 | -- W_J : Index_Base := L; |
1765 | -- while W_J < H loop | |
1766 | -- W_J := Index_Base'Succ (W); | |
70482933 RK |
1767 | -- L_Body; |
1768 | -- end loop; | |
1769 | ||
1770 | function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is | |
07fc65c4 | 1771 | W_J : Node_Id; |
70482933 RK |
1772 | |
1773 | W_Decl : Node_Id; | |
07fc65c4 | 1774 | -- W_J : Base_Type := L; |
70482933 RK |
1775 | |
1776 | W_Iteration_Scheme : Node_Id; | |
07fc65c4 | 1777 | -- while W_J < H |
70482933 RK |
1778 | |
1779 | W_Index_Succ : Node_Id; | |
07fc65c4 | 1780 | -- Index_Base'Succ (J) |
70482933 | 1781 | |
fbf5a39b | 1782 | W_Increment : Node_Id; |
07fc65c4 | 1783 | -- W_J := Index_Base'Succ (W) |
70482933 | 1784 | |
fbf5a39b | 1785 | W_Body : constant List_Id := New_List; |
70482933 RK |
1786 | -- The statements to execute in the loop |
1787 | ||
fbf5a39b | 1788 | S : constant List_Id := New_List; |
70482933 RK |
1789 | -- list of statement |
1790 | ||
1791 | begin | |
1792 | -- If loop bounds define an empty range or are equal return null | |
1793 | ||
1794 | if Empty_Range (L, H) or else Equal (L, H) then | |
1795 | Append_To (S, Make_Null_Statement (Loc)); | |
1796 | return S; | |
1797 | end if; | |
1798 | ||
07fc65c4 | 1799 | -- Build the decl of W_J |
70482933 | 1800 | |
191fcb3a | 1801 | W_J := Make_Temporary (Loc, 'J', L); |
70482933 RK |
1802 | W_Decl := |
1803 | Make_Object_Declaration | |
1804 | (Loc, | |
07fc65c4 | 1805 | Defining_Identifier => W_J, |
70482933 RK |
1806 | Object_Definition => Index_Base_Name, |
1807 | Expression => L); | |
1808 | ||
1809 | -- Theoretically we should do a New_Copy_Tree (L) here, but we know | |
1810 | -- that in this particular case L is a fresh Expr generated by | |
1811 | -- Add which we are the only ones to use. | |
1812 | ||
1813 | Append_To (S, W_Decl); | |
1814 | ||
fbf5a39b | 1815 | -- Construct " while W_J < H" |
70482933 RK |
1816 | |
1817 | W_Iteration_Scheme := | |
1818 | Make_Iteration_Scheme | |
1819 | (Loc, | |
1820 | Condition => Make_Op_Lt | |
1821 | (Loc, | |
e4494292 | 1822 | Left_Opnd => New_Occurrence_Of (W_J, Loc), |
70482933 RK |
1823 | Right_Opnd => New_Copy_Tree (H))); |
1824 | ||
1825 | -- Construct the statements to execute in the loop body | |
1826 | ||
1827 | W_Index_Succ := | |
1828 | Make_Attribute_Reference | |
1829 | (Loc, | |
1830 | Prefix => Index_Base_Name, | |
1831 | Attribute_Name => Name_Succ, | |
e4494292 | 1832 | Expressions => New_List (New_Occurrence_Of (W_J, Loc))); |
70482933 RK |
1833 | |
1834 | W_Increment := | |
1835 | Make_OK_Assignment_Statement | |
1836 | (Loc, | |
e4494292 | 1837 | Name => New_Occurrence_Of (W_J, Loc), |
70482933 RK |
1838 | Expression => W_Index_Succ); |
1839 | ||
1840 | Append_To (W_Body, W_Increment); | |
937e9676 | 1841 | |
70482933 | 1842 | Append_List_To (W_Body, |
fc84947c | 1843 | Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr)); |
70482933 RK |
1844 | |
1845 | -- Construct the final loop | |
1846 | ||
37368818 RD |
1847 | Append_To (S, |
1848 | Make_Implicit_Loop_Statement | |
1849 | (Node => N, | |
1850 | Identifier => Empty, | |
1851 | Iteration_Scheme => W_Iteration_Scheme, | |
1852 | Statements => W_Body)); | |
70482933 RK |
1853 | |
1854 | return S; | |
1855 | end Gen_While; | |
1856 | ||
59e9bc0b AC |
1857 | -------------------- |
1858 | -- Get_Assoc_Expr -- | |
1859 | -------------------- | |
1860 | ||
c8df2335 EB |
1861 | -- Duplicate the expression in case we will be generating several loops. |
1862 | -- As a result the expression is no longer shared between the loops and | |
1863 | -- is reevaluated for each such loop. | |
1864 | ||
59e9bc0b | 1865 | function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is |
e9999161 AC |
1866 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
1867 | ||
59e9bc0b AC |
1868 | begin |
1869 | if Box_Present (Assoc) then | |
d7b2fad2 | 1870 | if Present (Default_Aspect_Component_Value (Typ)) then |
c8df2335 | 1871 | return New_Copy_Tree (Default_Aspect_Component_Value (Typ)); |
d7b2fad2 | 1872 | elsif Needs_Simple_Initialization (Ctype) then |
c8df2335 | 1873 | return New_Copy_Tree (Get_Simple_Init_Val (Ctype, N)); |
59e9bc0b AC |
1874 | else |
1875 | return Empty; | |
1876 | end if; | |
1877 | ||
1878 | else | |
c8df2335 EB |
1879 | -- The expression will be passed to Gen_Loop, which immediately |
1880 | -- calls Parent_Kind on it, so we set Parent when it matters. | |
1881 | ||
1882 | return | |
1883 | Expr : constant Node_Id := New_Copy_Tree (Expression (Assoc)) | |
1884 | do | |
1885 | Copy_Parent (To => Expr, From => Expression (Assoc)); | |
1886 | end return; | |
59e9bc0b AC |
1887 | end if; |
1888 | end Get_Assoc_Expr; | |
1889 | ||
70482933 RK |
1890 | --------------------- |
1891 | -- Index_Base_Name -- | |
1892 | --------------------- | |
1893 | ||
1894 | function Index_Base_Name return Node_Id is | |
1895 | begin | |
e4494292 | 1896 | return New_Occurrence_Of (Index_Base, Sloc (N)); |
70482933 RK |
1897 | end Index_Base_Name; |
1898 | ||
1899 | ------------------------------------ | |
1900 | -- Local_Compile_Time_Known_Value -- | |
1901 | ------------------------------------ | |
1902 | ||
1903 | function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is | |
1904 | begin | |
1905 | return Compile_Time_Known_Value (E) | |
1906 | or else | |
1907 | (Nkind (E) = N_Attribute_Reference | |
fbf5a39b AC |
1908 | and then Attribute_Name (E) = Name_Val |
1909 | and then Compile_Time_Known_Value (First (Expressions (E)))); | |
70482933 RK |
1910 | end Local_Compile_Time_Known_Value; |
1911 | ||
1912 | ---------------------- | |
1913 | -- Local_Expr_Value -- | |
1914 | ---------------------- | |
1915 | ||
1916 | function Local_Expr_Value (E : Node_Id) return Uint is | |
1917 | begin | |
1918 | if Compile_Time_Known_Value (E) then | |
1919 | return Expr_Value (E); | |
1920 | else | |
1921 | return Expr_Value (First (Expressions (E))); | |
1922 | end if; | |
1923 | end Local_Expr_Value; | |
1924 | ||
937e9676 | 1925 | -- Local variables |
70482933 | 1926 | |
937e9676 | 1927 | New_Code : constant List_Id := New_List; |
70482933 | 1928 | |
10c257af ES |
1929 | Aggr_Bounds : constant Range_Nodes := |
1930 | Get_Index_Bounds (Aggregate_Bounds (N)); | |
1931 | Aggr_L : Node_Id renames Aggr_Bounds.First; | |
1932 | Aggr_H : Node_Id renames Aggr_Bounds.Last; | |
d74716b3 AC |
1933 | -- The aggregate bounds of this specific subaggregate. Note that if the |
1934 | -- code generated by Build_Array_Aggr_Code is executed then these bounds | |
1935 | -- are OK. Otherwise a Constraint_Error would have been raised. | |
70482933 | 1936 | |
fbf5a39b AC |
1937 | Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L); |
1938 | Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H); | |
7324bf49 | 1939 | -- After Duplicate_Subexpr these are side-effect free |
70482933 | 1940 | |
937e9676 AC |
1941 | Assoc : Node_Id; |
1942 | Choice : Node_Id; | |
1943 | Expr : Node_Id; | |
70482933 | 1944 | |
7c4f3267 BD |
1945 | Bounds : Range_Nodes; |
1946 | Low : Node_Id renames Bounds.First; | |
1947 | High : Node_Id renames Bounds.Last; | |
1948 | ||
70482933 RK |
1949 | Nb_Choices : Nat := 0; |
1950 | Table : Case_Table_Type (1 .. Number_Of_Choices (N)); | |
1951 | -- Used to sort all the different choice values | |
1952 | ||
1953 | Nb_Elements : Int; | |
1954 | -- Number of elements in the positional aggregate | |
1955 | ||
937e9676 | 1956 | Others_Assoc : Node_Id := Empty; |
70482933 RK |
1957 | |
1958 | -- Start of processing for Build_Array_Aggr_Code | |
1959 | ||
1960 | begin | |
8ba0b4cf | 1961 | -- First before we start, a special case. If we have a bit packed |
fbf5a39b AC |
1962 | -- array represented as a modular type, then clear the value to |
1963 | -- zero first, to ensure that unused bits are properly cleared. | |
1964 | ||
fbf5a39b AC |
1965 | if Present (Typ) |
1966 | and then Is_Bit_Packed_Array (Typ) | |
8ca597af | 1967 | and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) |
fbf5a39b | 1968 | then |
445514c0 EB |
1969 | declare |
1970 | Zero : constant Node_Id := Make_Integer_Literal (Loc, Uint_0); | |
1971 | begin | |
1972 | Analyze_And_Resolve (Zero, Packed_Array_Impl_Type (Typ)); | |
1973 | Append_To (New_Code, | |
1974 | Make_Assignment_Statement (Loc, | |
1975 | Name => New_Copy_Tree (Into), | |
1976 | Expression => Unchecked_Convert_To (Typ, Zero))); | |
1977 | end; | |
fbf5a39b AC |
1978 | end if; |
1979 | ||
0e08f7ab ES |
1980 | -- If the component type contains tasks, we need to build a Master |
1981 | -- entity in the current scope, because it will be needed if build- | |
1982 | -- in-place functions are called in the expanded code. | |
1983 | ||
36a66365 | 1984 | if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then |
0e08f7ab ES |
1985 | Build_Master_Entity (Defining_Identifier (Parent (N))); |
1986 | end if; | |
1987 | ||
70482933 | 1988 | -- STEP 1: Process component associations |
3b9fa2df | 1989 | |
fbf5a39b AC |
1990 | -- For those associations that may generate a loop, initialize |
1991 | -- Loop_Actions to collect inserted actions that may be crated. | |
70482933 | 1992 | |
3b9fa2df ES |
1993 | -- Skip this if no component associations |
1994 | ||
d9275e87 PT |
1995 | if Is_Null_Aggregate (N) then |
1996 | null; | |
1997 | ||
1998 | elsif No (Expressions (N)) then | |
70482933 RK |
1999 | |
2000 | -- STEP 1 (a): Sort the discrete choices | |
2001 | ||
2002 | Assoc := First (Component_Associations (N)); | |
2003 | while Present (Assoc) loop | |
00f45f30 | 2004 | Choice := First (Choice_List (Assoc)); |
70482933 | 2005 | while Present (Choice) loop |
70482933 | 2006 | if Nkind (Choice) = N_Others_Choice then |
59e9bc0b | 2007 | Others_Assoc := Assoc; |
70482933 RK |
2008 | exit; |
2009 | end if; | |
2010 | ||
7c4f3267 | 2011 | Bounds := Get_Index_Bounds (Choice); |
70482933 | 2012 | |
fbf5a39b AC |
2013 | if Low /= High then |
2014 | Set_Loop_Actions (Assoc, New_List); | |
2015 | end if; | |
2016 | ||
70482933 | 2017 | Nb_Choices := Nb_Choices + 1; |
59e9bc0b AC |
2018 | |
2019 | Table (Nb_Choices) := | |
2020 | (Choice_Lo => Low, | |
2021 | Choice_Hi => High, | |
2022 | Choice_Node => Get_Assoc_Expr (Assoc)); | |
2023 | ||
70482933 RK |
2024 | Next (Choice); |
2025 | end loop; | |
2026 | ||
2027 | Next (Assoc); | |
2028 | end loop; | |
2029 | ||
2030 | -- If there is more than one set of choices these must be static | |
2031 | -- and we can therefore sort them. Remember that Nb_Choices does not | |
2032 | -- account for an others choice. | |
2033 | ||
2034 | if Nb_Choices > 1 then | |
2035 | Sort_Case_Table (Table); | |
2036 | end if; | |
2037 | ||
74580e1b | 2038 | -- STEP 1 (b): take care of the whole set of discrete choices |
70482933 RK |
2039 | |
2040 | for J in 1 .. Nb_Choices loop | |
2041 | Low := Table (J).Choice_Lo; | |
2042 | High := Table (J).Choice_Hi; | |
2043 | Expr := Table (J).Choice_Node; | |
70482933 RK |
2044 | Append_List (Gen_Loop (Low, High, Expr), To => New_Code); |
2045 | end loop; | |
2046 | ||
2047 | -- STEP 1 (c): generate the remaining loops to cover others choice | |
2048 | -- We don't need to generate loops over empty gaps, but if there is | |
2049 | -- a single empty range we must analyze the expression for semantics | |
2050 | ||
59e9bc0b | 2051 | if Present (Others_Assoc) then |
70482933 | 2052 | declare |
c8df2335 | 2053 | First : Boolean := True; |
70482933 RK |
2054 | |
2055 | begin | |
2056 | for J in 0 .. Nb_Choices loop | |
70482933 RK |
2057 | if J = 0 then |
2058 | Low := Aggr_Low; | |
2059 | else | |
2060 | Low := Add (1, To => Table (J).Choice_Hi); | |
2061 | end if; | |
2062 | ||
2063 | if J = Nb_Choices then | |
2064 | High := Aggr_High; | |
2065 | else | |
2066 | High := Add (-1, To => Table (J + 1).Choice_Lo); | |
2067 | end if; | |
2068 | ||
fbf5a39b | 2069 | -- If this is an expansion within an init proc, make |
c84700e7 ES |
2070 | -- sure that discriminant references are replaced by |
2071 | -- the corresponding discriminal. | |
2072 | ||
2073 | if Inside_Init_Proc then | |
2074 | if Is_Entity_Name (Low) | |
2075 | and then Ekind (Entity (Low)) = E_Discriminant | |
2076 | then | |
2077 | Set_Entity (Low, Discriminal (Entity (Low))); | |
2078 | end if; | |
2079 | ||
2080 | if Is_Entity_Name (High) | |
2081 | and then Ekind (Entity (High)) = E_Discriminant | |
2082 | then | |
2083 | Set_Entity (High, Discriminal (Entity (High))); | |
2084 | end if; | |
2085 | end if; | |
2086 | ||
c8df2335 | 2087 | if First or else not Empty_Range (Low, High) then |
70482933 | 2088 | First := False; |
6951cbc9 | 2089 | Set_Loop_Actions (Others_Assoc, New_List); |
c8df2335 EB |
2090 | Expr := Get_Assoc_Expr (Others_Assoc); |
2091 | Append_List (Gen_Loop (Low, High, Expr), To => New_Code); | |
70482933 RK |
2092 | end if; |
2093 | end loop; | |
2094 | end; | |
2095 | end if; | |
2096 | ||
2097 | -- STEP 2: Process positional components | |
2098 | ||
2099 | else | |
2100 | -- STEP 2 (a): Generate the assignments for each positional element | |
2101 | -- Note that here we have to use Aggr_L rather than Aggr_Low because | |
2102 | -- Aggr_L is analyzed and Add wants an analyzed expression. | |
2103 | ||
2104 | Expr := First (Expressions (N)); | |
2105 | Nb_Elements := -1; | |
70482933 RK |
2106 | while Present (Expr) loop |
2107 | Nb_Elements := Nb_Elements + 1; | |
2108 | Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr), | |
2109 | To => New_Code); | |
2110 | Next (Expr); | |
2111 | end loop; | |
2112 | ||
02a82539 | 2113 | -- STEP 2 (b): Generate final loop if an others choice is present. |
70482933 RK |
2114 | -- Here Nb_Elements gives the offset of the last positional element. |
2115 | ||
2116 | if Present (Component_Associations (N)) then | |
2117 | Assoc := Last (Component_Associations (N)); | |
70482933 | 2118 | |
02a82539 | 2119 | if Nkind (Assoc) = N_Iterated_Component_Association then |
81e68a19 | 2120 | -- Ada 2022: generate a loop to have a proper scope for |
02a82539 ES |
2121 | -- the identifier that typically appears in the expression. |
2122 | -- The lower bound of the loop is the position after all | |
2123 | -- previous positional components. | |
6e937c1c | 2124 | |
02a82539 ES |
2125 | Append_List (Gen_Loop (Add (Nb_Elements + 1, To => Aggr_L), |
2126 | Aggr_High, | |
2127 | Expression (Assoc)), | |
2128 | To => New_Code); | |
2129 | else | |
2130 | -- Ada 2005 (AI-287) | |
2131 | ||
2132 | Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L), | |
2133 | Aggr_High, | |
2134 | Get_Assoc_Expr (Assoc)), | |
2135 | To => New_Code); | |
2136 | end if; | |
70482933 RK |
2137 | end if; |
2138 | end if; | |
2139 | ||
2140 | return New_Code; | |
2141 | end Build_Array_Aggr_Code; | |
2142 | ||
742084ad MP |
2143 | ------------------------------------- |
2144 | -- Build_Assignment_With_Temporary -- | |
2145 | ------------------------------------- | |
2146 | ||
2147 | function Build_Assignment_With_Temporary | |
2148 | (Target : Node_Id; | |
ca4bff3a | 2149 | Typ : Entity_Id; |
742084ad MP |
2150 | Source : Node_Id) return List_Id |
2151 | is | |
2152 | Loc : constant Source_Ptr := Sloc (Source); | |
2153 | ||
2154 | Aggr_Code : List_Id; | |
2155 | Tmp : Entity_Id; | |
742084ad MP |
2156 | |
2157 | begin | |
ca4bff3a EB |
2158 | Aggr_Code := New_List; |
2159 | ||
2160 | Tmp := Build_Temporary_On_Secondary_Stack (Loc, Typ, Aggr_Code); | |
2161 | ||
742084ad MP |
2162 | Append_To (Aggr_Code, |
2163 | Make_OK_Assignment_Statement (Loc, | |
ca4bff3a EB |
2164 | Name => |
2165 | Make_Explicit_Dereference (Loc, | |
2166 | Prefix => New_Occurrence_Of (Tmp, Loc)), | |
742084ad MP |
2167 | Expression => Source)); |
2168 | ||
2169 | Append_To (Aggr_Code, | |
2170 | Make_OK_Assignment_Statement (Loc, | |
2171 | Name => Target, | |
ca4bff3a EB |
2172 | Expression => |
2173 | Make_Explicit_Dereference (Loc, | |
2174 | Prefix => New_Occurrence_Of (Tmp, Loc)))); | |
2175 | ||
742084ad MP |
2176 | return Aggr_Code; |
2177 | end Build_Assignment_With_Temporary; | |
2178 | ||
70482933 RK |
2179 | ---------------------------- |
2180 | -- Build_Record_Aggr_Code -- | |
2181 | ---------------------------- | |
2182 | ||
2183 | function Build_Record_Aggr_Code | |
f7e6fc47 RD |
2184 | (N : Node_Id; |
2185 | Typ : Entity_Id; | |
2186 | Lhs : Node_Id) return List_Id | |
70482933 RK |
2187 | is |
2188 | Loc : constant Source_Ptr := Sloc (N); | |
2189 | L : constant List_Id := New_List; | |
70482933 RK |
2190 | N_Typ : constant Entity_Id := Etype (N); |
2191 | ||
2192 | Comp : Node_Id; | |
2193 | Instr : Node_Id; | |
2194 | Ref : Node_Id; | |
0f95b178 | 2195 | Target : Entity_Id; |
70482933 RK |
2196 | Comp_Type : Entity_Id; |
2197 | Selector : Entity_Id; | |
2198 | Comp_Expr : Node_Id; | |
70482933 RK |
2199 | Expr_Q : Node_Id; |
2200 | ||
70482933 RK |
2201 | Ancestor_Is_Subtype_Mark : Boolean := False; |
2202 | ||
2203 | Init_Typ : Entity_Id := Empty; | |
5277cab6 | 2204 | |
df3e68b1 HK |
2205 | Finalization_Done : Boolean := False; |
2206 | -- True if Generate_Finalization_Actions has already been called; calls | |
0f95b178 | 2207 | -- after the first do nothing. |
70482933 | 2208 | |
70482933 | 2209 | function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id; |
3b9fa2df ES |
2210 | -- Returns the value that the given discriminant of an ancestor type |
2211 | -- should receive (in the absence of a conflict with the value provided | |
2212 | -- by an ancestor part of an extension aggregate). | |
70482933 RK |
2213 | |
2214 | procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id); | |
3b9fa2df ES |
2215 | -- Check that each of the discriminant values defined by the ancestor |
2216 | -- part of an extension aggregate match the corresponding values | |
2217 | -- provided by either an association of the aggregate or by the | |
2218 | -- constraint imposed by a parent type (RM95-4.3.2(8)). | |
70482933 | 2219 | |
d8f7b976 ES |
2220 | function Compatible_Int_Bounds |
2221 | (Agg_Bounds : Node_Id; | |
2222 | Typ_Bounds : Node_Id) return Boolean; | |
2223 | -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is | |
2224 | -- assumed that both bounds are integer ranges. | |
2225 | ||
df3e68b1 | 2226 | procedure Generate_Finalization_Actions; |
0f95b178 JM |
2227 | -- Deal with the various controlled type data structure initializations |
2228 | -- (but only if it hasn't been done already). | |
d8f7b976 ES |
2229 | |
2230 | function Get_Constraint_Association (T : Entity_Id) return Node_Id; | |
2231 | -- Returns the first discriminant association in the constraint | |
2232 | -- associated with T, if any, otherwise returns Empty. | |
2233 | ||
71129dde AC |
2234 | function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id; |
2235 | -- If the ancestor part is an unconstrained type and further ancestors | |
2236 | -- do not provide discriminants for it, check aggregate components for | |
2237 | -- values of the discriminants. | |
2238 | ||
3e582869 AC |
2239 | procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id); |
2240 | -- If Typ is derived, and constrains discriminants of the parent type, | |
2241 | -- these discriminants are not components of the aggregate, and must be | |
2feb1f84 | 2242 | -- initialized. The assignments are appended to List. The same is done |
10c257af | 2243 | -- if Typ derives from an already constrained subtype of a discriminated |
2feb1f84 | 2244 | -- parent type. |
3e582869 | 2245 | |
71129dde AC |
2246 | procedure Init_Stored_Discriminants; |
2247 | -- If the type is derived and has inherited discriminants, generate | |
2248 | -- explicit assignments for each, using the store constraint of the | |
2249 | -- type. Note that both visible and stored discriminants must be | |
2250 | -- initialized in case the derived type has some renamed and some | |
2251 | -- constrained discriminants. | |
2252 | ||
2253 | procedure Init_Visible_Discriminants; | |
2254 | -- If type has discriminants, retrieve their values from aggregate, | |
2255 | -- and generate explicit assignments for each. This does not include | |
2256 | -- discriminants inherited from ancestor, which are handled above. | |
2257 | -- The type of the aggregate is a subtype created ealier using the | |
2258 | -- given values of the discriminant components of the aggregate. | |
aab45d22 | 2259 | |
d8f7b976 ES |
2260 | function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean; |
2261 | -- Check whether Bounds is a range node and its lower and higher bounds | |
2262 | -- are integers literals. | |
7b9d0d69 | 2263 | |
937e9676 AC |
2264 | function Replace_Type (Expr : Node_Id) return Traverse_Result; |
2265 | -- If the aggregate contains a self-reference, traverse each expression | |
2266 | -- to replace a possible self-reference with a reference to the proper | |
2267 | -- component of the target of the assignment. | |
2268 | ||
2269 | function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result; | |
2270 | -- If default expression of a component mentions a discriminant of the | |
2271 | -- type, it must be rewritten as the discriminant of the target object. | |
2272 | ||
2273 | --------------------------------- | |
2274 | -- Ancestor_Discriminant_Value -- | |
2275 | --------------------------------- | |
70482933 RK |
2276 | |
2277 | function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is | |
2278 | Assoc : Node_Id; | |
2279 | Assoc_Elmt : Elmt_Id; | |
2280 | Aggr_Comp : Entity_Id; | |
2281 | Corresp_Disc : Entity_Id; | |
2282 | Current_Typ : Entity_Id := Base_Type (Typ); | |
2283 | Parent_Typ : Entity_Id; | |
2284 | Parent_Disc : Entity_Id; | |
2285 | Save_Assoc : Node_Id := Empty; | |
2286 | ||
2287 | begin | |
3b9fa2df ES |
2288 | -- First check any discriminant associations to see if any of them |
2289 | -- provide a value for the discriminant. | |
70482933 RK |
2290 | |
2291 | if Present (Discriminant_Specifications (Parent (Current_Typ))) then | |
2292 | Assoc := First (Component_Associations (N)); | |
2293 | while Present (Assoc) loop | |
2294 | Aggr_Comp := Entity (First (Choices (Assoc))); | |
2295 | ||
2296 | if Ekind (Aggr_Comp) = E_Discriminant then | |
2297 | Save_Assoc := Expression (Assoc); | |
2298 | ||
2299 | Corresp_Disc := Corresponding_Discriminant (Aggr_Comp); | |
2300 | while Present (Corresp_Disc) loop | |
3b9fa2df ES |
2301 | |
2302 | -- If found a corresponding discriminant then return the | |
2303 | -- value given in the aggregate. (Note: this is not | |
2304 | -- correct in the presence of side effects. ???) | |
70482933 RK |
2305 | |
2306 | if Disc = Corresp_Disc then | |
2307 | return Duplicate_Subexpr (Expression (Assoc)); | |
2308 | end if; | |
fbf5a39b | 2309 | |
ffcfb997 | 2310 | Corresp_Disc := Corresponding_Discriminant (Corresp_Disc); |
70482933 RK |
2311 | end loop; |
2312 | end if; | |
2313 | ||
2314 | Next (Assoc); | |
2315 | end loop; | |
2316 | end if; | |
2317 | ||
2318 | -- No match found in aggregate, so chain up parent types to find | |
2319 | -- a constraint that defines the value of the discriminant. | |
2320 | ||
2321 | Parent_Typ := Etype (Current_Typ); | |
2322 | while Current_Typ /= Parent_Typ loop | |
9013065b AC |
2323 | if Has_Discriminants (Parent_Typ) |
2324 | and then not Has_Unknown_Discriminants (Parent_Typ) | |
2325 | then | |
70482933 RK |
2326 | Parent_Disc := First_Discriminant (Parent_Typ); |
2327 | ||
2328 | -- We either get the association from the subtype indication | |
2329 | -- of the type definition itself, or from the discriminant | |
2330 | -- constraint associated with the type entity (which is | |
2331 | -- preferable, but it's not always present ???) | |
2332 | ||
aff557c7 | 2333 | if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ)) |
70482933 RK |
2334 | then |
2335 | Assoc := Get_Constraint_Association (Current_Typ); | |
2336 | Assoc_Elmt := No_Elmt; | |
2337 | else | |
2338 | Assoc_Elmt := | |
2339 | First_Elmt (Discriminant_Constraint (Current_Typ)); | |
2340 | Assoc := Node (Assoc_Elmt); | |
2341 | end if; | |
2342 | ||
2343 | -- Traverse the discriminants of the parent type looking | |
2344 | -- for one that corresponds. | |
2345 | ||
2346 | while Present (Parent_Disc) and then Present (Assoc) loop | |
2347 | Corresp_Disc := Parent_Disc; | |
2348 | while Present (Corresp_Disc) | |
2349 | and then Disc /= Corresp_Disc | |
2350 | loop | |
ffcfb997 | 2351 | Corresp_Disc := Corresponding_Discriminant (Corresp_Disc); |
70482933 RK |
2352 | end loop; |
2353 | ||
2354 | if Disc = Corresp_Disc then | |
2355 | if Nkind (Assoc) = N_Discriminant_Association then | |
2356 | Assoc := Expression (Assoc); | |
2357 | end if; | |
2358 | ||
e80f0cb0 RD |
2359 | -- If the located association directly denotes |
2360 | -- a discriminant, then use the value of a saved | |
2361 | -- association of the aggregate. This is an approach | |
2362 | -- used to handle certain cases involving multiple | |
2363 | -- discriminants mapped to a single discriminant of | |
2364 | -- a descendant. It's not clear how to locate the | |
2365 | -- appropriate discriminant value for such cases. ??? | |
70482933 RK |
2366 | |
2367 | if Is_Entity_Name (Assoc) | |
2368 | and then Ekind (Entity (Assoc)) = E_Discriminant | |
2369 | then | |
2370 | Assoc := Save_Assoc; | |
2371 | end if; | |
2372 | ||
2373 | return Duplicate_Subexpr (Assoc); | |
2374 | end if; | |
2375 | ||
2376 | Next_Discriminant (Parent_Disc); | |
2377 | ||
2378 | if No (Assoc_Elmt) then | |
2379 | Next (Assoc); | |
ffcfb997 | 2380 | |
70482933 RK |
2381 | else |
2382 | Next_Elmt (Assoc_Elmt); | |
ffcfb997 | 2383 | |
70482933 RK |
2384 | if Present (Assoc_Elmt) then |
2385 | Assoc := Node (Assoc_Elmt); | |
2386 | else | |
2387 | Assoc := Empty; | |
2388 | end if; | |
2389 | end if; | |
2390 | end loop; | |
2391 | end if; | |
2392 | ||
2393 | Current_Typ := Parent_Typ; | |
2394 | Parent_Typ := Etype (Current_Typ); | |
2395 | end loop; | |
2396 | ||
2397 | -- In some cases there's no ancestor value to locate (such as | |
2398 | -- when an ancestor part given by an expression defines the | |
2399 | -- discriminant value). | |
2400 | ||
2401 | return Empty; | |
2402 | end Ancestor_Discriminant_Value; | |
2403 | ||
2404 | ---------------------------------- | |
2405 | -- Check_Ancestor_Discriminants -- | |
2406 | ---------------------------------- | |
2407 | ||
2408 | procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is | |
5277cab6 | 2409 | Discr : Entity_Id; |
70482933 RK |
2410 | Disc_Value : Node_Id; |
2411 | Cond : Node_Id; | |
2412 | ||
2413 | begin | |
5277cab6 | 2414 | Discr := First_Discriminant (Base_Type (Anc_Typ)); |
70482933 RK |
2415 | while Present (Discr) loop |
2416 | Disc_Value := Ancestor_Discriminant_Value (Discr); | |
2417 | ||
2418 | if Present (Disc_Value) then | |
2419 | Cond := Make_Op_Ne (Loc, | |
ffcfb997 | 2420 | Left_Opnd => |
70482933 RK |
2421 | Make_Selected_Component (Loc, |
2422 | Prefix => New_Copy_Tree (Target), | |
2423 | Selector_Name => New_Occurrence_Of (Discr, Loc)), | |
2424 | Right_Opnd => Disc_Value); | |
2425 | ||
07fc65c4 GB |
2426 | Append_To (L, |
2427 | Make_Raise_Constraint_Error (Loc, | |
2428 | Condition => Cond, | |
2429 | Reason => CE_Discriminant_Check_Failed)); | |
70482933 RK |
2430 | end if; |
2431 | ||
2432 | Next_Discriminant (Discr); | |
2433 | end loop; | |
2434 | end Check_Ancestor_Discriminants; | |
2435 | ||
d8f7b976 ES |
2436 | --------------------------- |
2437 | -- Compatible_Int_Bounds -- | |
2438 | --------------------------- | |
2439 | ||
2440 | function Compatible_Int_Bounds | |
2441 | (Agg_Bounds : Node_Id; | |
2442 | Typ_Bounds : Node_Id) return Boolean | |
2443 | is | |
2444 | Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds)); | |
2445 | Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds)); | |
2446 | Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds)); | |
2447 | Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds)); | |
2448 | begin | |
2449 | return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi; | |
2450 | end Compatible_Int_Bounds; | |
2451 | ||
937e9676 AC |
2452 | ----------------------------------- |
2453 | -- Generate_Finalization_Actions -- | |
2454 | ----------------------------------- | |
2455 | ||
2456 | procedure Generate_Finalization_Actions is | |
2457 | begin | |
2458 | -- Do the work only the first time this is called | |
2459 | ||
2460 | if Finalization_Done then | |
2461 | return; | |
2462 | end if; | |
2463 | ||
2464 | Finalization_Done := True; | |
2465 | ||
2466 | -- Determine the external finalization list. It is either the | |
2467 | -- finalization list of the outer scope or the one coming from an | |
2468 | -- outer aggregate. When the target is not a temporary, the proper | |
2469 | -- scope is the scope of the target rather than the potentially | |
2470 | -- transient current scope. | |
2471 | ||
2472 | if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then | |
2473 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); | |
2474 | Set_Assignment_OK (Ref); | |
2475 | ||
2476 | Append_To (L, | |
2477 | Make_Procedure_Call_Statement (Loc, | |
2478 | Name => | |
2479 | New_Occurrence_Of | |
2480 | (Find_Prim_Op (Init_Typ, Name_Initialize), Loc), | |
2481 | Parameter_Associations => New_List (New_Copy_Tree (Ref)))); | |
2482 | end if; | |
2483 | end Generate_Finalization_Actions; | |
2484 | ||
70482933 RK |
2485 | -------------------------------- |
2486 | -- Get_Constraint_Association -- | |
2487 | -------------------------------- | |
2488 | ||
2489 | function Get_Constraint_Association (T : Entity_Id) return Node_Id is | |
2c17ca0a AC |
2490 | Indic : Node_Id; |
2491 | Typ : Entity_Id; | |
70482933 RK |
2492 | |
2493 | begin | |
2c17ca0a AC |
2494 | Typ := T; |
2495 | ||
598a56c0 ES |
2496 | -- If type is private, get constraint from full view. This was |
2497 | -- previously done in an instance context, but is needed whenever | |
2498 | -- the ancestor part has a discriminant, possibly inherited through | |
2499 | -- multiple derivations. | |
2c17ca0a | 2500 | |
598a56c0 | 2501 | if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then |
2c17ca0a AC |
2502 | Typ := Full_View (Typ); |
2503 | end if; | |
2504 | ||
2505 | Indic := Subtype_Indication (Type_Definition (Parent (Typ))); | |
2506 | ||
598a56c0 | 2507 | -- Verify that the subtype indication carries a constraint |
70482933 RK |
2508 | |
2509 | if Nkind (Indic) = N_Subtype_Indication | |
2510 | and then Present (Constraint (Indic)) | |
2511 | then | |
2512 | return First (Constraints (Constraint (Indic))); | |
2513 | end if; | |
2514 | ||
2515 | return Empty; | |
2516 | end Get_Constraint_Association; | |
2517 | ||
aab45d22 AC |
2518 | ------------------------------------- |
2519 | -- Get_Explicit_Discriminant_Value -- | |
2520 | ------------------------------------- | |
2521 | ||
7893514c RD |
2522 | function Get_Explicit_Discriminant_Value |
2523 | (D : Entity_Id) return Node_Id | |
aab45d22 AC |
2524 | is |
2525 | Assoc : Node_Id; | |
2526 | Choice : Node_Id; | |
2527 | Val : Node_Id; | |
2528 | ||
2529 | begin | |
2530 | -- The aggregate has been normalized and all associations have a | |
2531 | -- single choice. | |
2532 | ||
2533 | Assoc := First (Component_Associations (N)); | |
2534 | while Present (Assoc) loop | |
2535 | Choice := First (Choices (Assoc)); | |
7893514c | 2536 | |
aab45d22 AC |
2537 | if Chars (Choice) = Chars (D) then |
2538 | Val := Expression (Assoc); | |
2539 | Remove (Assoc); | |
2540 | return Val; | |
2541 | end if; | |
2542 | ||
2543 | Next (Assoc); | |
2544 | end loop; | |
2545 | ||
2546 | return Empty; | |
2547 | end Get_Explicit_Discriminant_Value; | |
2548 | ||
3e582869 AC |
2549 | ------------------------------- |
2550 | -- Init_Hidden_Discriminants -- | |
2551 | ------------------------------- | |
2552 | ||
2553 | procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is | |
a25e72b5 AC |
2554 | function Is_Completely_Hidden_Discriminant |
2555 | (Discr : Entity_Id) return Boolean; | |
2556 | -- Determine whether Discr is a completely hidden discriminant of | |
2557 | -- type Typ. | |
2558 | ||
2559 | --------------------------------------- | |
2560 | -- Is_Completely_Hidden_Discriminant -- | |
2561 | --------------------------------------- | |
2562 | ||
2563 | function Is_Completely_Hidden_Discriminant | |
2564 | (Discr : Entity_Id) return Boolean | |
2565 | is | |
2566 | Item : Entity_Id; | |
2567 | ||
2568 | begin | |
2569 | -- Use First/Next_Entity as First/Next_Discriminant do not yield | |
2570 | -- completely hidden discriminants. | |
2571 | ||
2572 | Item := First_Entity (Typ); | |
2573 | while Present (Item) loop | |
2574 | if Ekind (Item) = E_Discriminant | |
2575 | and then Is_Completely_Hidden (Item) | |
2576 | and then Chars (Original_Record_Component (Item)) = | |
2577 | Chars (Discr) | |
2578 | then | |
2579 | return True; | |
2580 | end if; | |
2581 | ||
2582 | Next_Entity (Item); | |
2583 | end loop; | |
2584 | ||
2585 | return False; | |
2586 | end Is_Completely_Hidden_Discriminant; | |
2587 | ||
2588 | -- Local variables | |
2589 | ||
2590 | Base_Typ : Entity_Id; | |
2591 | Discr : Entity_Id; | |
2592 | Discr_Constr : Elmt_Id; | |
2593 | Discr_Init : Node_Id; | |
2594 | Discr_Val : Node_Id; | |
ddce04b8 | 2595 | In_Aggr_Type : Boolean; |
a25e72b5 AC |
2596 | Par_Typ : Entity_Id; |
2597 | ||
2598 | -- Start of processing for Init_Hidden_Discriminants | |
3e582869 AC |
2599 | |
2600 | begin | |
7b536495 AC |
2601 | -- The constraints on the hidden discriminants, if present, are kept |
2602 | -- in the Stored_Constraint list of the type itself, or in that of | |
ddce04b8 AC |
2603 | -- the base type. If not in the constraints of the aggregate itself, |
2604 | -- we examine ancestors to find discriminants that are not renamed | |
2605 | -- by other discriminants but constrained explicitly. | |
2606 | ||
2607 | In_Aggr_Type := True; | |
2feb1f84 | 2608 | |
a25e72b5 AC |
2609 | Base_Typ := Base_Type (Typ); |
2610 | while Is_Derived_Type (Base_Typ) | |
596f7139 | 2611 | and then |
a25e72b5 | 2612 | (Present (Stored_Constraint (Base_Typ)) |
596f7139 AC |
2613 | or else |
2614 | (In_Aggr_Type and then Present (Stored_Constraint (Typ)))) | |
3e582869 | 2615 | loop |
a25e72b5 | 2616 | Par_Typ := Etype (Base_Typ); |
7b536495 | 2617 | |
a25e72b5 | 2618 | if not Has_Discriminants (Par_Typ) then |
2feb1f84 AC |
2619 | return; |
2620 | end if; | |
3e582869 | 2621 | |
a25e72b5 | 2622 | Discr := First_Discriminant (Par_Typ); |
2feb1f84 | 2623 | |
bdc193ba | 2624 | -- We know that one of the stored-constraint lists is present |
2feb1f84 | 2625 | |
a25e72b5 AC |
2626 | if Present (Stored_Constraint (Base_Typ)) then |
2627 | Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ)); | |
bdc193ba AC |
2628 | |
2629 | -- For private extension, stored constraint may be on full view | |
2630 | ||
a25e72b5 AC |
2631 | elsif Is_Private_Type (Base_Typ) |
2632 | and then Present (Full_View (Base_Typ)) | |
2633 | and then Present (Stored_Constraint (Full_View (Base_Typ))) | |
bdc193ba | 2634 | then |
a25e72b5 AC |
2635 | Discr_Constr := |
2636 | First_Elmt (Stored_Constraint (Full_View (Base_Typ))); | |
bdc193ba | 2637 | |
f056076f BD |
2638 | -- Otherwise, no discriminant to process |
2639 | ||
2feb1f84 | 2640 | else |
f056076f | 2641 | Discr_Constr := No_Elmt; |
2feb1f84 AC |
2642 | end if; |
2643 | ||
a25e72b5 AC |
2644 | while Present (Discr) and then Present (Discr_Constr) loop |
2645 | Discr_Val := Node (Discr_Constr); | |
2646 | ||
2647 | -- The parent discriminant is renamed in the derived type, | |
2648 | -- nothing to initialize. | |
3e582869 | 2649 | |
a25e72b5 AC |
2650 | -- type Deriv_Typ (Discr : ...) |
2651 | -- is new Parent_Typ (Discr => Discr); | |
3e582869 | 2652 | |
a25e72b5 AC |
2653 | if Is_Entity_Name (Discr_Val) |
2654 | and then Ekind (Entity (Discr_Val)) = E_Discriminant | |
3e582869 | 2655 | then |
a25e72b5 AC |
2656 | null; |
2657 | ||
2658 | -- When the parent discriminant is constrained at the type | |
2659 | -- extension level, it does not appear in the derived type. | |
2660 | ||
2661 | -- type Deriv_Typ (Discr : ...) | |
2662 | -- is new Parent_Typ (Discr => Discr, | |
2663 | -- Hidden_Discr => Expression); | |
3e582869 | 2664 | |
a25e72b5 AC |
2665 | elsif Is_Completely_Hidden_Discriminant (Discr) then |
2666 | null; | |
2667 | ||
2668 | -- Otherwise initialize the discriminant | |
2669 | ||
2670 | else | |
2671 | Discr_Init := | |
3e582869 | 2672 | Make_OK_Assignment_Statement (Loc, |
a25e72b5 AC |
2673 | Name => |
2674 | Make_Selected_Component (Loc, | |
2675 | Prefix => New_Copy_Tree (Target), | |
2676 | Selector_Name => New_Occurrence_Of (Discr, Loc)), | |
2677 | Expression => New_Copy_Tree (Discr_Val)); | |
3e582869 | 2678 | |
a25e72b5 | 2679 | Append_To (List, Discr_Init); |
3e582869 AC |
2680 | end if; |
2681 | ||
a25e72b5 AC |
2682 | Next_Elmt (Discr_Constr); |
2683 | Next_Discriminant (Discr); | |
3e582869 AC |
2684 | end loop; |
2685 | ||
ddce04b8 | 2686 | In_Aggr_Type := False; |
a25e72b5 | 2687 | Base_Typ := Base_Type (Par_Typ); |
3e582869 AC |
2688 | end loop; |
2689 | end Init_Hidden_Discriminants; | |
2690 | ||
71129dde AC |
2691 | -------------------------------- |
2692 | -- Init_Visible_Discriminants -- | |
2693 | -------------------------------- | |
2694 | ||
2695 | procedure Init_Visible_Discriminants is | |
2696 | Discriminant : Entity_Id; | |
2697 | Discriminant_Value : Node_Id; | |
2698 | ||
2699 | begin | |
2700 | Discriminant := First_Discriminant (Typ); | |
2701 | while Present (Discriminant) loop | |
2702 | Comp_Expr := | |
2703 | Make_Selected_Component (Loc, | |
2704 | Prefix => New_Copy_Tree (Target), | |
2705 | Selector_Name => New_Occurrence_Of (Discriminant, Loc)); | |
2706 | ||
2707 | Discriminant_Value := | |
2708 | Get_Discriminant_Value | |
2709 | (Discriminant, Typ, Discriminant_Constraint (N_Typ)); | |
2710 | ||
2711 | Instr := | |
2712 | Make_OK_Assignment_Statement (Loc, | |
2713 | Name => Comp_Expr, | |
2714 | Expression => New_Copy_Tree (Discriminant_Value)); | |
2715 | ||
71129dde AC |
2716 | Append_To (L, Instr); |
2717 | ||
2718 | Next_Discriminant (Discriminant); | |
2719 | end loop; | |
2720 | end Init_Visible_Discriminants; | |
2721 | ||
2722 | ------------------------------- | |
2723 | -- Init_Stored_Discriminants -- | |
2724 | ------------------------------- | |
2725 | ||
2726 | procedure Init_Stored_Discriminants is | |
2727 | Discriminant : Entity_Id; | |
2728 | Discriminant_Value : Node_Id; | |
2729 | ||
2730 | begin | |
2731 | Discriminant := First_Stored_Discriminant (Typ); | |
2732 | while Present (Discriminant) loop | |
2733 | Comp_Expr := | |
2734 | Make_Selected_Component (Loc, | |
2735 | Prefix => New_Copy_Tree (Target), | |
2736 | Selector_Name => New_Occurrence_Of (Discriminant, Loc)); | |
2737 | ||
2738 | Discriminant_Value := | |
2739 | Get_Discriminant_Value | |
2740 | (Discriminant, N_Typ, Discriminant_Constraint (N_Typ)); | |
2741 | ||
2742 | Instr := | |
2743 | Make_OK_Assignment_Statement (Loc, | |
2744 | Name => Comp_Expr, | |
2745 | Expression => New_Copy_Tree (Discriminant_Value)); | |
2746 | ||
71129dde AC |
2747 | Append_To (L, Instr); |
2748 | ||
2749 | Next_Stored_Discriminant (Discriminant); | |
2750 | end loop; | |
2751 | end Init_Stored_Discriminants; | |
2752 | ||
937e9676 AC |
2753 | ------------------------- |
2754 | -- Is_Int_Range_Bounds -- | |
2755 | ------------------------- | |
2756 | ||
2757 | function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is | |
2758 | begin | |
2759 | return Nkind (Bounds) = N_Range | |
2760 | and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal | |
2761 | and then Nkind (High_Bound (Bounds)) = N_Integer_Literal; | |
2762 | end Is_Int_Range_Bounds; | |
f2abc637 | 2763 | |
0f95b178 JM |
2764 | ------------------ |
2765 | -- Replace_Type -- | |
2766 | ------------------ | |
2767 | ||
2768 | function Replace_Type (Expr : Node_Id) return Traverse_Result is | |
2769 | begin | |
ae250f9e | 2770 | -- Note about the Is_Ancestor test below: aggregate components for |
acf63f8c | 2771 | -- self-referential types include attribute references to the current |
ae250f9e | 2772 | -- instance, of the form: Typ'access, etc. These references are |
acf63f8c ES |
2773 | -- rewritten as references to the target of the aggregate: the |
2774 | -- left-hand side of an assignment, the entity in a declaration, | |
ae250f9e EB |
2775 | -- or a temporary. Without this test, we would improperly extend |
2776 | -- this rewriting to attribute references whose prefix is not the | |
acf63f8c ES |
2777 | -- type of the aggregate. |
2778 | ||
0f95b178 | 2779 | if Nkind (Expr) = N_Attribute_Reference |
acf63f8c | 2780 | and then Is_Entity_Name (Prefix (Expr)) |
0f95b178 | 2781 | and then Is_Type (Entity (Prefix (Expr))) |
ae250f9e EB |
2782 | and then |
2783 | Is_Ancestor | |
2784 | (Entity (Prefix (Expr)), Etype (N), Use_Full_View => True) | |
0f95b178 JM |
2785 | then |
2786 | if Is_Entity_Name (Lhs) then | |
304757d2 | 2787 | Rewrite (Prefix (Expr), New_Occurrence_Of (Entity (Lhs), Loc)); |
0f95b178 JM |
2788 | |
2789 | else | |
2790 | Rewrite (Expr, | |
2791 | Make_Attribute_Reference (Loc, | |
2792 | Attribute_Name => Name_Unrestricted_Access, | |
2793 | Prefix => New_Copy_Tree (Lhs))); | |
2794 | Set_Analyzed (Parent (Expr), False); | |
2795 | end if; | |
2796 | end if; | |
2797 | ||
2798 | return OK; | |
2799 | end Replace_Type; | |
2800 | ||
937e9676 AC |
2801 | -------------------------- |
2802 | -- Rewrite_Discriminant -- | |
2803 | -------------------------- | |
2804 | ||
2805 | function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is | |
2806 | begin | |
2807 | if Is_Entity_Name (Expr) | |
2808 | and then Present (Entity (Expr)) | |
2809 | and then Ekind (Entity (Expr)) = E_In_Parameter | |
2810 | and then Present (Discriminal_Link (Entity (Expr))) | |
2811 | and then Scope (Discriminal_Link (Entity (Expr))) = | |
2812 | Base_Type (Etype (N)) | |
2813 | then | |
2814 | Rewrite (Expr, | |
2815 | Make_Selected_Component (Loc, | |
2816 | Prefix => New_Copy_Tree (Lhs), | |
2817 | Selector_Name => Make_Identifier (Loc, Chars (Expr)))); | |
b8411279 ES |
2818 | |
2819 | -- The generated code will be reanalyzed, but if the reference | |
2820 | -- to the discriminant appears within an already analyzed | |
2821 | -- expression (e.g. a conditional) we must set its proper entity | |
2822 | -- now. Context is an initialization procedure. | |
2823 | ||
2824 | Analyze (Expr); | |
937e9676 AC |
2825 | end if; |
2826 | ||
2827 | return OK; | |
2828 | end Rewrite_Discriminant; | |
0f95b178 | 2829 | |
f2abc637 AC |
2830 | procedure Replace_Discriminants is |
2831 | new Traverse_Proc (Rewrite_Discriminant); | |
2832 | ||
937e9676 AC |
2833 | procedure Replace_Self_Reference is |
2834 | new Traverse_Proc (Replace_Type); | |
2835 | ||
70482933 RK |
2836 | -- Start of processing for Build_Record_Aggr_Code |
2837 | ||
2838 | begin | |
0f95b178 JM |
2839 | if Has_Self_Reference (N) then |
2840 | Replace_Self_Reference (N); | |
2841 | end if; | |
2842 | ||
2843 | -- If the target of the aggregate is class-wide, we must convert it | |
2844 | -- to the actual type of the aggregate, so that the proper components | |
2845 | -- are visible. We know already that the types are compatible. | |
2846 | ||
c0ceba6c | 2847 | if Present (Etype (Lhs)) and then Is_Class_Wide_Type (Etype (Lhs)) then |
0f95b178 JM |
2848 | Target := Unchecked_Convert_To (Typ, Lhs); |
2849 | else | |
2850 | Target := Lhs; | |
2851 | end if; | |
2852 | ||
3b9fa2df ES |
2853 | -- Deal with the ancestor part of extension aggregates or with the |
2854 | -- discriminants of the root type. | |
70482933 RK |
2855 | |
2856 | if Nkind (N) = N_Extension_Aggregate then | |
2857 | declare | |
d18dd43c EB |
2858 | Ancestor : constant Node_Id := Ancestor_Part (N); |
2859 | Ancestor_Q : constant Node_Id := Unqualify (Ancestor); | |
2860 | ||
df3e68b1 | 2861 | Assign : List_Id; |
70482933 RK |
2862 | |
2863 | begin | |
d18dd43c | 2864 | -- If the ancestor part is a subtype mark T, we generate |
fbf5a39b | 2865 | |
df3e68b1 HK |
2866 | -- init-proc (T (tmp)); if T is constrained and |
2867 | -- init-proc (S (tmp)); where S applies an appropriate | |
2868 | -- constraint if T is unconstrained | |
70482933 | 2869 | |
df3e68b1 HK |
2870 | if Is_Entity_Name (Ancestor) |
2871 | and then Is_Type (Entity (Ancestor)) | |
2872 | then | |
70482933 RK |
2873 | Ancestor_Is_Subtype_Mark := True; |
2874 | ||
df3e68b1 HK |
2875 | if Is_Constrained (Entity (Ancestor)) then |
2876 | Init_Typ := Entity (Ancestor); | |
70482933 | 2877 | |
3b9fa2df ES |
2878 | -- For an ancestor part given by an unconstrained type mark, |
2879 | -- create a subtype constrained by appropriate corresponding | |
2880 | -- discriminant values coming from either associations of the | |
2881 | -- aggregate or a constraint on a parent type. The subtype will | |
2882 | -- be used to generate the correct default value for the | |
2883 | -- ancestor part. | |
70482933 | 2884 | |
df3e68b1 | 2885 | elsif Has_Discriminants (Entity (Ancestor)) then |
70482933 | 2886 | declare |
df3e68b1 | 2887 | Anc_Typ : constant Entity_Id := Entity (Ancestor); |
fbf5a39b AC |
2888 | Anc_Constr : constant List_Id := New_List; |
2889 | Discrim : Entity_Id; | |
70482933 RK |
2890 | Disc_Value : Node_Id; |
2891 | New_Indic : Node_Id; | |
2892 | Subt_Decl : Node_Id; | |
fbf5a39b | 2893 | |
70482933 | 2894 | begin |
fbf5a39b | 2895 | Discrim := First_Discriminant (Anc_Typ); |
70482933 RK |
2896 | while Present (Discrim) loop |
2897 | Disc_Value := Ancestor_Discriminant_Value (Discrim); | |
aab45d22 AC |
2898 | |
2899 | -- If no usable discriminant in ancestors, check | |
2900 | -- whether aggregate has an explicit value for it. | |
2901 | ||
2902 | if No (Disc_Value) then | |
2903 | Disc_Value := | |
2904 | Get_Explicit_Discriminant_Value (Discrim); | |
2905 | end if; | |
2906 | ||
70482933 RK |
2907 | Append_To (Anc_Constr, Disc_Value); |
2908 | Next_Discriminant (Discrim); | |
2909 | end loop; | |
2910 | ||
2911 | New_Indic := | |
2912 | Make_Subtype_Indication (Loc, | |
2913 | Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc), | |
2914 | Constraint => | |
2915 | Make_Index_Or_Discriminant_Constraint (Loc, | |
2916 | Constraints => Anc_Constr)); | |
2917 | ||
2918 | Init_Typ := Create_Itype (Ekind (Anc_Typ), N); | |
2919 | ||
2920 | Subt_Decl := | |
2921 | Make_Subtype_Declaration (Loc, | |
2922 | Defining_Identifier => Init_Typ, | |
2923 | Subtype_Indication => New_Indic); | |
2924 | ||
3b9fa2df ES |
2925 | -- Itypes must be analyzed with checks off Declaration |
2926 | -- must have a parent for proper handling of subsidiary | |
2927 | -- actions. | |
70482933 | 2928 | |
07fc65c4 | 2929 | Set_Parent (Subt_Decl, N); |
70482933 RK |
2930 | Analyze (Subt_Decl, Suppress => All_Checks); |
2931 | end; | |
2932 | end if; | |
2933 | ||
2934 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); | |
2935 | Set_Assignment_OK (Ref); | |
2936 | ||
64425dff | 2937 | if not Is_Interface (Init_Typ) then |
3bb3f6d6 AC |
2938 | Append_List_To (L, |
2939 | Build_Initialization_Call (Loc, | |
2940 | Id_Ref => Ref, | |
2941 | Typ => Init_Typ, | |
2942 | In_Init_Proc => Within_Init_Proc, | |
2943 | With_Default_Init => Has_Default_Init_Comps (N) | |
2944 | or else | |
2945 | Has_Task (Base_Type (Init_Typ)))); | |
2946 | ||
df3e68b1 HK |
2947 | if Is_Constrained (Entity (Ancestor)) |
2948 | and then Has_Discriminants (Entity (Ancestor)) | |
3bb3f6d6 | 2949 | then |
df3e68b1 | 2950 | Check_Ancestor_Discriminants (Entity (Ancestor)); |
3bb3f6d6 | 2951 | end if; |
f7937111 GD |
2952 | |
2953 | -- If ancestor type has Default_Initialization_Condition, | |
2954 | -- add a DIC check after the ancestor object is initialized | |
2955 | -- by default. | |
2956 | ||
2957 | if Has_DIC (Entity (Ancestor)) | |
2958 | and then Present (DIC_Procedure (Entity (Ancestor))) | |
2959 | then | |
2960 | Append_To (L, | |
2961 | Build_DIC_Call | |
2962 | (Loc, New_Copy_Tree (Ref), Entity (Ancestor))); | |
2963 | end if; | |
70482933 RK |
2964 | end if; |
2965 | ||
11795185 JM |
2966 | -- Handle calls to C++ constructors |
2967 | ||
df3e68b1 HK |
2968 | elsif Is_CPP_Constructor_Call (Ancestor) then |
2969 | Init_Typ := Etype (Ancestor); | |
11795185 JM |
2970 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); |
2971 | Set_Assignment_OK (Ref); | |
2972 | ||
2973 | Append_List_To (L, | |
2974 | Build_Initialization_Call (Loc, | |
2975 | Id_Ref => Ref, | |
2976 | Typ => Init_Typ, | |
2977 | In_Init_Proc => Within_Init_Proc, | |
2978 | With_Default_Init => Has_Default_Init_Comps (N), | |
df3e68b1 | 2979 | Constructor_Ref => Ancestor)); |
11795185 | 2980 | |
c5ee5ad2 BD |
2981 | -- Ada 2005 (AI-287): If the ancestor part is an aggregate of |
2982 | -- limited type, a recursive call expands the ancestor. Note that | |
2983 | -- in the limited case, the ancestor part must be either a | |
d4dfb005 BD |
2984 | -- function call (possibly qualified) or aggregate (definitely |
2985 | -- qualified). | |
65356e64 | 2986 | |
df3e68b1 | 2987 | elsif Is_Limited_Type (Etype (Ancestor)) |
d18dd43c EB |
2988 | and then Nkind (Ancestor_Q) in N_Aggregate |
2989 | | N_Extension_Aggregate | |
c5ee5ad2 | 2990 | then |
7b9d0d69 | 2991 | Append_List_To (L, |
f7e6fc47 | 2992 | Build_Record_Aggr_Code |
d18dd43c EB |
2993 | (N => Ancestor_Q, |
2994 | Typ => Etype (Ancestor_Q), | |
f7e6fc47 | 2995 | Lhs => Target)); |
65356e64 | 2996 | |
d18dd43c | 2997 | -- If the ancestor part is an expression E of type T, we generate |
3b9fa2df | 2998 | |
df3e68b1 | 2999 | -- T (tmp) := E; |
3b9fa2df | 3000 | |
c5ee5ad2 | 3001 | -- In Ada 2005, this includes the case of a (possibly qualified) |
d18dd43c EB |
3002 | -- limited function call. The assignment will later be turned into |
3003 | -- a build-in-place function call (for further details, see | |
c5ee5ad2 | 3004 | -- Make_Build_In_Place_Call_In_Assignment). |
70482933 RK |
3005 | |
3006 | else | |
df3e68b1 | 3007 | Init_Typ := Etype (Ancestor); |
70482933 | 3008 | |
7b9d0d69 ES |
3009 | -- If the ancestor part is an aggregate, force its full |
3010 | -- expansion, which was delayed. | |
3011 | ||
d18dd43c | 3012 | if Nkind (Ancestor_Q) in N_Aggregate | N_Extension_Aggregate |
7b9d0d69 | 3013 | then |
df3e68b1 HK |
3014 | Set_Analyzed (Ancestor, False); |
3015 | Set_Analyzed (Expression (Ancestor), False); | |
7b9d0d69 ES |
3016 | end if; |
3017 | ||
3018 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); | |
7b9d0d69 | 3019 | |
7b9d0d69 ES |
3020 | Assign := New_List ( |
3021 | Make_OK_Assignment_Statement (Loc, | |
3022 | Name => Ref, | |
df3e68b1 | 3023 | Expression => Ancestor)); |
70482933 | 3024 | |
c0ceba6c EB |
3025 | -- Arrange for the component to be adjusted if need be (the |
3026 | -- call will be generated by Make_Tag_Ctrl_Assignment). | |
70482933 | 3027 | |
d18dd43c | 3028 | if Needs_Finalization (Init_Typ) |
c0ceba6c | 3029 | and then not Is_Limited_View (Init_Typ) |
3b9fa2df | 3030 | then |
c0ceba6c EB |
3031 | Set_No_Finalize_Actions (First (Assign)); |
3032 | else | |
3033 | Set_No_Ctrl_Actions (First (Assign)); | |
70482933 RK |
3034 | end if; |
3035 | ||
70482933 | 3036 | Append_To (L, |
c0ceba6c | 3037 | Make_Suppress_Block (Loc, Name_Discriminant_Check, Assign)); |
70482933 RK |
3038 | |
3039 | if Has_Discriminants (Init_Typ) then | |
3040 | Check_Ancestor_Discriminants (Init_Typ); | |
3041 | end if; | |
3042 | end if; | |
3043 | end; | |
3044 | ||
376e7d14 AC |
3045 | -- Generate assignments of hidden discriminants. If the base type is |
3046 | -- an unchecked union, the discriminants are unknown to the back-end | |
3047 | -- and absent from a value of the type, so assignments for them are | |
3048 | -- not emitted. | |
3e582869 AC |
3049 | |
3050 | if Has_Discriminants (Typ) | |
3051 | and then not Is_Unchecked_Union (Base_Type (Typ)) | |
3052 | then | |
3053 | Init_Hidden_Discriminants (Typ, L); | |
3054 | end if; | |
3055 | ||
fbf5a39b AC |
3056 | -- Normal case (not an extension aggregate) |
3057 | ||
70482933 RK |
3058 | else |
3059 | -- Generate the discriminant expressions, component by component. | |
3060 | -- If the base type is an unchecked union, the discriminants are | |
3061 | -- unknown to the back-end and absent from a value of the type, so | |
3062 | -- assignments for them are not emitted. | |
3063 | ||
3064 | if Has_Discriminants (Typ) | |
3065 | and then not Is_Unchecked_Union (Base_Type (Typ)) | |
3066 | then | |
3e582869 | 3067 | Init_Hidden_Discriminants (Typ, L); |
d8f7b976 ES |
3068 | |
3069 | -- Generate discriminant init values for the visible discriminants | |
70482933 | 3070 | |
71129dde | 3071 | Init_Visible_Discriminants; |
70482933 | 3072 | |
71129dde AC |
3073 | if Is_Derived_Type (N_Typ) then |
3074 | Init_Stored_Discriminants; | |
3075 | end if; | |
70482933 RK |
3076 | end if; |
3077 | end if; | |
3078 | ||
28541488 JM |
3079 | -- For CPP types we generate an implicit call to the C++ default |
3080 | -- constructor to ensure the proper initialization of the _Tag | |
3081 | -- component. | |
3082 | ||
36a66365 | 3083 | if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then |
cefce34c | 3084 | Invoke_Constructor : declare |
15f0f591 | 3085 | CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ); |
cefce34c JM |
3086 | |
3087 | procedure Invoke_IC_Proc (T : Entity_Id); | |
3088 | -- Recursive routine used to climb to parents. Required because | |
3089 | -- parents must be initialized before descendants to ensure | |
3090 | -- propagation of inherited C++ slots. | |
3091 | ||
3092 | -------------------- | |
3093 | -- Invoke_IC_Proc -- | |
3094 | -------------------- | |
3095 | ||
3096 | procedure Invoke_IC_Proc (T : Entity_Id) is | |
3097 | begin | |
3098 | -- Avoid generating extra calls. Initialization required | |
3099 | -- only for types defined from the level of derivation of | |
3100 | -- type of the constructor and the type of the aggregate. | |
3101 | ||
3102 | if T = CPP_Parent then | |
3103 | return; | |
3104 | end if; | |
3105 | ||
3106 | Invoke_IC_Proc (Etype (T)); | |
3107 | ||
3108 | -- Generate call to the IC routine | |
3109 | ||
3110 | if Present (CPP_Init_Proc (T)) then | |
3111 | Append_To (L, | |
3112 | Make_Procedure_Call_Statement (Loc, | |
ffcfb997 | 3113 | Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc))); |
cefce34c JM |
3114 | end if; |
3115 | end Invoke_IC_Proc; | |
3116 | ||
3117 | -- Start of processing for Invoke_Constructor | |
3118 | ||
3119 | begin | |
3120 | -- Implicit invocation of the C++ constructor | |
3121 | ||
3122 | if Nkind (N) = N_Aggregate then | |
3123 | Append_To (L, | |
3124 | Make_Procedure_Call_Statement (Loc, | |
37368818 RD |
3125 | Name => |
3126 | New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc), | |
cefce34c JM |
3127 | Parameter_Associations => New_List ( |
3128 | Unchecked_Convert_To (CPP_Parent, | |
3129 | New_Copy_Tree (Lhs))))); | |
3130 | end if; | |
3131 | ||
3132 | Invoke_IC_Proc (Typ); | |
3133 | end Invoke_Constructor; | |
28541488 JM |
3134 | end if; |
3135 | ||
70482933 RK |
3136 | -- Generate the assignments, component by component |
3137 | ||
3138 | -- tmp.comp1 := Expr1_From_Aggr; | |
3139 | -- tmp.comp2 := Expr2_From_Aggr; | |
3140 | -- .... | |
3141 | ||
3142 | Comp := First (Component_Associations (N)); | |
3143 | while Present (Comp) loop | |
b7e429ab | 3144 | Selector := Entity (First (Choices (Comp))); |
df0ac6e1 | 3145 | pragma Assert (Present (Selector)); |
70482933 | 3146 | |
236fecbf JM |
3147 | -- C++ constructors |
3148 | ||
3149 | if Is_CPP_Constructor_Call (Expression (Comp)) then | |
3150 | Append_List_To (L, | |
3151 | Build_Initialization_Call (Loc, | |
37368818 RD |
3152 | Id_Ref => |
3153 | Make_Selected_Component (Loc, | |
3154 | Prefix => New_Copy_Tree (Target), | |
3155 | Selector_Name => New_Occurrence_Of (Selector, Loc)), | |
1c612f29 RD |
3156 | Typ => Etype (Selector), |
3157 | Enclos_Type => Typ, | |
236fecbf | 3158 | With_Default_Init => True, |
1c612f29 | 3159 | Constructor_Ref => Expression (Comp))); |
236fecbf | 3160 | |
736f9bed PT |
3161 | elsif Box_Present (Comp) |
3162 | and then Needs_Simple_Initialization (Etype (Selector)) | |
3163 | then | |
3164 | Comp_Expr := | |
3165 | Make_Selected_Component (Loc, | |
3166 | Prefix => New_Copy_Tree (Target), | |
3167 | Selector_Name => New_Occurrence_Of (Selector, Loc)); | |
3168 | ||
c0ceba6c | 3169 | Initialize_Component |
fc84947c EB |
3170 | (N => N, |
3171 | Comp => Comp_Expr, | |
736f9bed PT |
3172 | Comp_Typ => Etype (Selector), |
3173 | Init_Expr => Get_Simple_Init_Val | |
3174 | (Typ => Etype (Selector), | |
3175 | N => Comp, | |
3176 | Size => | |
3177 | (if Known_Esize (Selector) | |
3178 | then Esize (Selector) | |
3179 | else Uint_0)), | |
3180 | Stmts => L); | |
3181 | ||
3b9fa2df | 3182 | -- Ada 2005 (AI-287): For each default-initialized component generate |
52739835 | 3183 | -- a call to the corresponding IP subprogram if available. |
65356e64 | 3184 | |
236fecbf | 3185 | elsif Box_Present (Comp) |
52739835 | 3186 | and then Has_Non_Null_Base_Init_Proc (Etype (Selector)) |
65356e64 | 3187 | then |
5277cab6 | 3188 | if Ekind (Selector) /= E_Discriminant then |
df3e68b1 | 3189 | Generate_Finalization_Actions; |
5277cab6 ES |
3190 | end if; |
3191 | ||
0ab80019 AC |
3192 | -- Ada 2005 (AI-287): If the component type has tasks then |
3193 | -- generate the activation chain and master entities (except | |
3194 | -- in case of an allocator because in that case these entities | |
3195 | -- are generated by Build_Task_Allocate_Block_With_Init_Stmts). | |
c45b6ae0 AC |
3196 | |
3197 | declare | |
91b1417d | 3198 | Ctype : constant Entity_Id := Etype (Selector); |
1c612f29 RD |
3199 | Inside_Allocator : Boolean := False; |
3200 | P : Node_Id := Parent (N); | |
c45b6ae0 AC |
3201 | |
3202 | begin | |
3203 | if Is_Task_Type (Ctype) or else Has_Task (Ctype) then | |
3204 | while Present (P) loop | |
3205 | if Nkind (P) = N_Allocator then | |
3206 | Inside_Allocator := True; | |
3207 | exit; | |
3208 | end if; | |
3209 | ||
3210 | P := Parent (P); | |
3211 | end loop; | |
3212 | ||
3213 | if not Inside_Init_Proc and not Inside_Allocator then | |
3214 | Build_Activation_Chain_Entity (N); | |
c45b6ae0 AC |
3215 | end if; |
3216 | end if; | |
3217 | end; | |
3218 | ||
65356e64 AC |
3219 | Append_List_To (L, |
3220 | Build_Initialization_Call (Loc, | |
1c612f29 RD |
3221 | Id_Ref => Make_Selected_Component (Loc, |
3222 | Prefix => New_Copy_Tree (Target), | |
3223 | Selector_Name => | |
3224 | New_Occurrence_Of (Selector, Loc)), | |
3225 | Typ => Etype (Selector), | |
3226 | Enclos_Type => Typ, | |
c45b6ae0 | 3227 | With_Default_Init => True)); |
65356e64 | 3228 | |
7b9d0d69 | 3229 | -- Prepare for component assignment |
fbf5a39b | 3230 | |
236fecbf | 3231 | elsif Ekind (Selector) /= E_Discriminant |
70482933 RK |
3232 | or else Nkind (N) = N_Extension_Aggregate |
3233 | then | |
7b9d0d69 | 3234 | -- All the discriminants have now been assigned |
3b9fa2df | 3235 | |
7b9d0d69 ES |
3236 | -- This is now a good moment to initialize and attach all the |
3237 | -- controllers. Their position may depend on the discriminants. | |
3238 | ||
5277cab6 | 3239 | if Ekind (Selector) /= E_Discriminant then |
df3e68b1 | 3240 | Generate_Finalization_Actions; |
7b9d0d69 ES |
3241 | end if; |
3242 | ||
38171f43 | 3243 | Comp_Type := Underlying_Type (Etype (Selector)); |
70482933 RK |
3244 | Comp_Expr := |
3245 | Make_Selected_Component (Loc, | |
3246 | Prefix => New_Copy_Tree (Target), | |
3247 | Selector_Name => New_Occurrence_Of (Selector, Loc)); | |
3248 | ||
4f061cf2 | 3249 | Expr_Q := Unqualify (Expression (Comp)); |
70482933 | 3250 | |
7b9d0d69 ES |
3251 | -- Now either create the assignment or generate the code for the |
3252 | -- inner aggregate top-down. | |
fbf5a39b | 3253 | |
70482933 | 3254 | if Is_Delayed_Aggregate (Expr_Q) then |
d8f7b976 ES |
3255 | |
3256 | -- We have the following case of aggregate nesting inside | |
3257 | -- an object declaration: | |
3258 | ||
3259 | -- type Arr_Typ is array (Integer range <>) of ...; | |
3b9fa2df | 3260 | |
d8f7b976 ES |
3261 | -- type Rec_Typ (...) is record |
3262 | -- Obj_Arr_Typ : Arr_Typ (A .. B); | |
3263 | -- end record; | |
3b9fa2df | 3264 | |
d8f7b976 ES |
3265 | -- Obj_Rec_Typ : Rec_Typ := (..., |
3266 | -- Obj_Arr_Typ => (X => (...), Y => (...))); | |
3267 | ||
3268 | -- The length of the ranges of the aggregate and Obj_Add_Typ | |
3269 | -- are equal (B - A = Y - X), but they do not coincide (X /= | |
3270 | -- A and B /= Y). This case requires array sliding which is | |
3271 | -- performed in the following manner: | |
3272 | ||
3273 | -- subtype Arr_Sub is Arr_Typ (X .. Y); | |
3274 | -- Temp : Arr_Sub; | |
3275 | -- Temp (X) := (...); | |
3276 | -- ... | |
3277 | -- Temp (Y) := (...); | |
3278 | -- Obj_Rec_Typ.Obj_Arr_Typ := Temp; | |
3279 | ||
5277cab6 | 3280 | if Ekind (Comp_Type) = E_Array_Subtype |
d8f7b976 ES |
3281 | and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q)) |
3282 | and then Is_Int_Range_Bounds (First_Index (Comp_Type)) | |
3283 | and then not | |
5277cab6 ES |
3284 | Compatible_Int_Bounds |
3285 | (Agg_Bounds => Aggregate_Bounds (Expr_Q), | |
3286 | Typ_Bounds => First_Index (Comp_Type)) | |
d8f7b976 | 3287 | then |
5277cab6 ES |
3288 | -- Create the array subtype with bounds equal to those of |
3289 | -- the corresponding aggregate. | |
d8f7b976 | 3290 | |
5277cab6 | 3291 | declare |
191fcb3a | 3292 | SubE : constant Entity_Id := Make_Temporary (Loc, 'T'); |
d8f7b976 ES |
3293 | |
3294 | SubD : constant Node_Id := | |
15f0f591 AC |
3295 | Make_Subtype_Declaration (Loc, |
3296 | Defining_Identifier => SubE, | |
3297 | Subtype_Indication => | |
3298 | Make_Subtype_Indication (Loc, | |
3299 | Subtype_Mark => | |
e4494292 | 3300 | New_Occurrence_Of (Etype (Comp_Type), Loc), |
15f0f591 AC |
3301 | Constraint => |
3302 | Make_Index_Or_Discriminant_Constraint | |
3303 | (Loc, | |
3304 | Constraints => New_List ( | |
3305 | New_Copy_Tree | |
3306 | (Aggregate_Bounds (Expr_Q)))))); | |
d8f7b976 ES |
3307 | |
3308 | -- Create a temporary array of the above subtype which | |
3309 | -- will be used to capture the aggregate assignments. | |
3310 | ||
faf387e1 | 3311 | TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N); |
d8f7b976 ES |
3312 | |
3313 | TmpD : constant Node_Id := | |
15f0f591 AC |
3314 | Make_Object_Declaration (Loc, |
3315 | Defining_Identifier => TmpE, | |
e4494292 | 3316 | Object_Definition => New_Occurrence_Of (SubE, Loc)); |
d8f7b976 ES |
3317 | |
3318 | begin | |
3319 | Set_No_Initialization (TmpD); | |
3320 | Append_To (L, SubD); | |
3321 | Append_To (L, TmpD); | |
3322 | ||
5277cab6 | 3323 | -- Expand aggregate into assignments to the temp array |
d8f7b976 ES |
3324 | |
3325 | Append_List_To (L, | |
3326 | Late_Expansion (Expr_Q, Comp_Type, | |
e4494292 | 3327 | New_Occurrence_Of (TmpE, Loc))); |
d8f7b976 ES |
3328 | |
3329 | -- Slide | |
3330 | ||
3331 | Append_To (L, | |
3332 | Make_Assignment_Statement (Loc, | |
3333 | Name => New_Copy_Tree (Comp_Expr), | |
e4494292 | 3334 | Expression => New_Occurrence_Of (TmpE, Loc))); |
d8f7b976 ES |
3335 | end; |
3336 | ||
3337 | -- Normal case (sliding not required) | |
3338 | ||
3339 | else | |
3340 | Append_List_To (L, | |
df3e68b1 | 3341 | Late_Expansion (Expr_Q, Comp_Type, Comp_Expr)); |
d8f7b976 | 3342 | end if; |
fbf5a39b | 3343 | |
5277cab6 ES |
3344 | -- Expr_Q is not delayed aggregate |
3345 | ||
70482933 | 3346 | else |
f2abc637 AC |
3347 | if Has_Discriminants (Typ) then |
3348 | Replace_Discriminants (Expr_Q); | |
b3f5eef0 AC |
3349 | |
3350 | -- If the component is an array type that depends on | |
3351 | -- discriminants, and the expression is a single Others | |
3352 | -- clause, create an explicit subtype for it because the | |
3353 | -- backend has troubles recovering the actual bounds. | |
3354 | ||
3355 | if Nkind (Expr_Q) = N_Aggregate | |
3356 | and then Is_Array_Type (Comp_Type) | |
3357 | and then Present (Component_Associations (Expr_Q)) | |
3358 | then | |
3359 | declare | |
3360 | Assoc : constant Node_Id := | |
45ec05e1 | 3361 | First (Component_Associations (Expr_Q)); |
b3f5eef0 AC |
3362 | Decl : Node_Id; |
3363 | ||
3364 | begin | |
f7f8e290 JM |
3365 | if Present (Assoc) |
3366 | and then | |
3367 | Nkind (First (Choices (Assoc))) = N_Others_Choice | |
b3f5eef0 AC |
3368 | then |
3369 | Decl := | |
3370 | Build_Actual_Subtype_Of_Component | |
3371 | (Comp_Type, Comp_Expr); | |
3372 | ||
3373 | -- If the component type does not in fact depend on | |
3374 | -- discriminants, the subtype declaration is empty. | |
3375 | ||
3376 | if Present (Decl) then | |
3377 | Append_To (L, Decl); | |
3378 | Set_Etype (Comp_Expr, Defining_Entity (Decl)); | |
3379 | end if; | |
3380 | end if; | |
3381 | end; | |
3382 | end if; | |
f2abc637 AC |
3383 | end if; |
3384 | ||
c63a2ad6 | 3385 | if Modify_Tree_For_C |
a1e1820b AC |
3386 | and then Nkind (Expr_Q) = N_Aggregate |
3387 | and then Is_Array_Type (Etype (Expr_Q)) | |
3388 | and then Present (First_Index (Etype (Expr_Q))) | |
3389 | then | |
3390 | declare | |
0c5c46a9 | 3391 | Expr_Q_Type : constant Entity_Id := Etype (Expr_Q); |
a1e1820b AC |
3392 | begin |
3393 | Append_List_To (L, | |
3394 | Build_Array_Aggr_Code | |
3395 | (N => Expr_Q, | |
3396 | Ctype => Component_Type (Expr_Q_Type), | |
3397 | Index => First_Index (Expr_Q_Type), | |
3398 | Into => Comp_Expr, | |
937e9676 AC |
3399 | Scalar_Comp => |
3400 | Is_Scalar_Type (Component_Type (Expr_Q_Type)))); | |
a1e1820b AC |
3401 | end; |
3402 | ||
3403 | else | |
fc84947c EB |
3404 | Initialize_Component |
3405 | (N => N, | |
3406 | Comp => Comp_Expr, | |
3407 | Comp_Typ => Etype (Selector), | |
3408 | Init_Expr => Expr_Q, | |
3409 | Stmts => L); | |
70482933 RK |
3410 | end if; |
3411 | end if; | |
fbf5a39b | 3412 | |
37368818 | 3413 | -- comment would be good here ??? |
fbf5a39b AC |
3414 | |
3415 | elsif Ekind (Selector) = E_Discriminant | |
3416 | and then Nkind (N) /= N_Extension_Aggregate | |
3417 | and then Nkind (Parent (N)) = N_Component_Association | |
3418 | and then Is_Constrained (Typ) | |
3419 | then | |
3420 | -- We must check that the discriminant value imposed by the | |
3421 | -- context is the same as the value given in the subaggregate, | |
3422 | -- because after the expansion into assignments there is no | |
3423 | -- record on which to perform a regular discriminant check. | |
3424 | ||
3425 | declare | |
3426 | D_Val : Elmt_Id; | |
3427 | Disc : Entity_Id; | |
3428 | ||
3429 | begin | |
3430 | D_Val := First_Elmt (Discriminant_Constraint (Typ)); | |
3431 | Disc := First_Discriminant (Typ); | |
fbf5a39b AC |
3432 | while Chars (Disc) /= Chars (Selector) loop |
3433 | Next_Discriminant (Disc); | |
3434 | Next_Elmt (D_Val); | |
3435 | end loop; | |
3436 | ||
3437 | pragma Assert (Present (D_Val)); | |
3438 | ||
0f95b178 JM |
3439 | -- This check cannot performed for components that are |
3440 | -- constrained by a current instance, because this is not a | |
3441 | -- value that can be compared with the actual constraint. | |
3442 | ||
3443 | if Nkind (Node (D_Val)) /= N_Attribute_Reference | |
3444 | or else not Is_Entity_Name (Prefix (Node (D_Val))) | |
3445 | or else not Is_Type (Entity (Prefix (Node (D_Val)))) | |
3446 | then | |
3447 | Append_To (L, | |
3448 | Make_Raise_Constraint_Error (Loc, | |
3449 | Condition => | |
3450 | Make_Op_Ne (Loc, | |
37368818 | 3451 | Left_Opnd => New_Copy_Tree (Node (D_Val)), |
0f95b178 | 3452 | Right_Opnd => Expression (Comp)), |
37368818 | 3453 | Reason => CE_Discriminant_Check_Failed)); |
0f95b178 JM |
3454 | |
3455 | else | |
3b9fa2df ES |
3456 | -- Find self-reference in previous discriminant assignment, |
3457 | -- and replace with proper expression. | |
0f95b178 JM |
3458 | |
3459 | declare | |
3460 | Ass : Node_Id; | |
3461 | ||
3462 | begin | |
3463 | Ass := First (L); | |
3464 | while Present (Ass) loop | |
3465 | if Nkind (Ass) = N_Assignment_Statement | |
3466 | and then Nkind (Name (Ass)) = N_Selected_Component | |
3467 | and then Chars (Selector_Name (Name (Ass))) = | |
36a66365 | 3468 | Chars (Disc) |
0f95b178 JM |
3469 | then |
3470 | Set_Expression | |
3471 | (Ass, New_Copy_Tree (Expression (Comp))); | |
3472 | exit; | |
3473 | end if; | |
3474 | Next (Ass); | |
3475 | end loop; | |
3476 | end; | |
3477 | end if; | |
fbf5a39b | 3478 | end; |
70482933 RK |
3479 | end if; |
3480 | ||
f7937111 GD |
3481 | -- If the component association was specified with a box and the |
3482 | -- component type has a Default_Initial_Condition, then generate | |
3483 | -- a call to the DIC procedure. | |
3484 | ||
3485 | if Has_DIC (Etype (Selector)) | |
3486 | and then Was_Default_Init_Box_Association (Comp) | |
3487 | and then Present (DIC_Procedure (Etype (Selector))) | |
3488 | then | |
3489 | Append_To (L, | |
3490 | Build_DIC_Call (Loc, | |
3491 | Make_Selected_Component (Loc, | |
3492 | Prefix => New_Copy_Tree (Target), | |
3493 | Selector_Name => New_Occurrence_Of (Selector, Loc)), | |
3494 | Etype (Selector))); | |
3495 | end if; | |
3496 | ||
70482933 RK |
3497 | Next (Comp); |
3498 | end loop; | |
3499 | ||
28541488 JM |
3500 | -- For CPP types we generated a call to the C++ default constructor |
3501 | -- before the components have been initialized to ensure the proper | |
3502 | -- initialization of the _Tag component (see above). | |
3503 | ||
c0ceba6c | 3504 | if Is_CPP_Class (Typ) then |
28541488 JM |
3505 | null; |
3506 | ||
c0ceba6c EB |
3507 | -- If the type is tagged, the tag needs to be initialized (unless we |
3508 | -- are in VM-mode where tags are implicit). It is done late in the | |
3509 | -- initialization process because in some cases, we call the init | |
3510 | -- proc of an ancestor which will not leave out the right tag. | |
3511 | ||
1f110335 | 3512 | elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then |
70482933 | 3513 | Instr := |
af10c962 EB |
3514 | Make_Tag_Assignment_From_Type |
3515 | (Loc, New_Copy_Tree (Target), Base_Type (Typ)); | |
70482933 RK |
3516 | |
3517 | Append_To (L, Instr); | |
c5ee5ad2 | 3518 | |
bdc193ba | 3519 | -- Ada 2005 (AI-251): If the tagged type has been derived from an |
c5ee5ad2 BD |
3520 | -- abstract interfaces we must also initialize the tags of the |
3521 | -- secondary dispatch tables. | |
3522 | ||
ce2b6ba5 | 3523 | if Has_Interfaces (Base_Type (Typ)) then |
c5ee5ad2 | 3524 | Init_Secondary_Tags |
ed323421 AC |
3525 | (Typ => Base_Type (Typ), |
3526 | Target => Target, | |
3527 | Stmts_List => L, | |
fe683ef6 | 3528 | Init_Tags_List => L); |
c5ee5ad2 | 3529 | end if; |
70482933 RK |
3530 | end if; |
3531 | ||
7b9d0d69 ES |
3532 | -- If the controllers have not been initialized yet (by lack of non- |
3533 | -- discriminant components), let's do it now. | |
70482933 | 3534 | |
df3e68b1 | 3535 | Generate_Finalization_Actions; |
70482933 | 3536 | |
7b9d0d69 | 3537 | return L; |
70482933 RK |
3538 | end Build_Record_Aggr_Code; |
3539 | ||
3540 | ------------------------------- | |
3541 | -- Convert_Aggr_In_Allocator -- | |
3542 | ------------------------------- | |
3543 | ||
fa57ac97 ES |
3544 | procedure Convert_Aggr_In_Allocator |
3545 | (Alloc : Node_Id; | |
3546 | Decl : Node_Id; | |
3547 | Aggr : Node_Id) | |
3548 | is | |
70482933 RK |
3549 | Loc : constant Source_Ptr := Sloc (Aggr); |
3550 | Typ : constant Entity_Id := Etype (Aggr); | |
3551 | Temp : constant Entity_Id := Defining_Identifier (Decl); | |
fbf5a39b AC |
3552 | |
3553 | Occ : constant Node_Id := | |
15f0f591 | 3554 | Unchecked_Convert_To (Typ, |
e4494292 | 3555 | Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc))); |
70482933 | 3556 | |
70482933 | 3557 | begin |
6f639c98 ES |
3558 | if Is_Array_Type (Typ) then |
3559 | Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ); | |
3560 | ||
3561 | elsif Has_Default_Init_Comps (Aggr) then | |
c45b6ae0 AC |
3562 | declare |
3563 | L : constant List_Id := New_List; | |
3564 | Init_Stmts : List_Id; | |
3565 | ||
3566 | begin | |
df3e68b1 | 3567 | Init_Stmts := Late_Expansion (Aggr, Typ, Occ); |
c45b6ae0 | 3568 | |
0f95b178 JM |
3569 | if Has_Task (Typ) then |
3570 | Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts); | |
fa57ac97 | 3571 | Insert_Actions (Alloc, L); |
0f95b178 | 3572 | else |
fa57ac97 | 3573 | Insert_Actions (Alloc, Init_Stmts); |
0f95b178 | 3574 | end if; |
c45b6ae0 AC |
3575 | end; |
3576 | ||
3577 | else | |
df3e68b1 | 3578 | Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ)); |
c45b6ae0 | 3579 | end if; |
70482933 RK |
3580 | end Convert_Aggr_In_Allocator; |
3581 | ||
3582 | -------------------------------- | |
3583 | -- Convert_Aggr_In_Assignment -- | |
3584 | -------------------------------- | |
3585 | ||
3586 | procedure Convert_Aggr_In_Assignment (N : Node_Id) is | |
4f061cf2 | 3587 | Aggr : constant Node_Id := Unqualify (Expression (N)); |
3b9fa2df ES |
3588 | Typ : constant Entity_Id := Etype (Aggr); |
3589 | Occ : constant Node_Id := New_Copy_Tree (Name (N)); | |
70482933 RK |
3590 | |
3591 | begin | |
df3e68b1 | 3592 | Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ)); |
70482933 RK |
3593 | end Convert_Aggr_In_Assignment; |
3594 | ||
3595 | --------------------------------- | |
3596 | -- Convert_Aggr_In_Object_Decl -- | |
3597 | --------------------------------- | |
3598 | ||
3599 | procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is | |
3600 | Obj : constant Entity_Id := Defining_Identifier (N); | |
4f061cf2 | 3601 | Aggr : constant Node_Id := Unqualify (Expression (N)); |
70482933 RK |
3602 | Loc : constant Source_Ptr := Sloc (Aggr); |
3603 | Typ : constant Entity_Id := Etype (Aggr); | |
3604 | Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc); | |
3605 | ||
fff7a6d9 AC |
3606 | Has_Transient_Scope : Boolean := False; |
3607 | ||
fbf5a39b AC |
3608 | function Discriminants_Ok return Boolean; |
3609 | -- If the object type is constrained, the discriminants in the | |
3610 | -- aggregate must be checked against the discriminants of the subtype. | |
3611 | -- This cannot be done using Apply_Discriminant_Checks because after | |
3612 | -- expansion there is no aggregate left to check. | |
3613 | ||
3614 | ---------------------- | |
3615 | -- Discriminants_Ok -- | |
3616 | ---------------------- | |
3617 | ||
3618 | function Discriminants_Ok return Boolean is | |
3619 | Cond : Node_Id := Empty; | |
3620 | Check : Node_Id; | |
3621 | D : Entity_Id; | |
3622 | Disc1 : Elmt_Id; | |
3623 | Disc2 : Elmt_Id; | |
3624 | Val1 : Node_Id; | |
3625 | Val2 : Node_Id; | |
3626 | ||
3627 | begin | |
3628 | D := First_Discriminant (Typ); | |
3629 | Disc1 := First_Elmt (Discriminant_Constraint (Typ)); | |
3630 | Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj))); | |
fbf5a39b AC |
3631 | while Present (Disc1) and then Present (Disc2) loop |
3632 | Val1 := Node (Disc1); | |
3633 | Val2 := Node (Disc2); | |
3634 | ||
3635 | if not Is_OK_Static_Expression (Val1) | |
3636 | or else not Is_OK_Static_Expression (Val2) | |
3637 | then | |
3638 | Check := Make_Op_Ne (Loc, | |
3639 | Left_Opnd => Duplicate_Subexpr (Val1), | |
3640 | Right_Opnd => Duplicate_Subexpr (Val2)); | |
3641 | ||
3642 | if No (Cond) then | |
3643 | Cond := Check; | |
3644 | ||
3645 | else | |
3646 | Cond := Make_Or_Else (Loc, | |
3647 | Left_Opnd => Cond, | |
3648 | Right_Opnd => Check); | |
3649 | end if; | |
3650 | ||
3651 | elsif Expr_Value (Val1) /= Expr_Value (Val2) then | |
3652 | Apply_Compile_Time_Constraint_Error (Aggr, | |
324ac540 | 3653 | Msg => "incorrect value for discriminant&??", |
fbf5a39b AC |
3654 | Reason => CE_Discriminant_Check_Failed, |
3655 | Ent => D); | |
3656 | return False; | |
3657 | end if; | |
3658 | ||
3659 | Next_Discriminant (D); | |
3660 | Next_Elmt (Disc1); | |
3661 | Next_Elmt (Disc2); | |
3662 | end loop; | |
3663 | ||
d940c627 | 3664 | -- If any discriminant constraint is nonstatic, emit a check |
fbf5a39b AC |
3665 | |
3666 | if Present (Cond) then | |
3667 | Insert_Action (N, | |
3668 | Make_Raise_Constraint_Error (Loc, | |
3669 | Condition => Cond, | |
ef1c0511 | 3670 | Reason => CE_Discriminant_Check_Failed)); |
fbf5a39b AC |
3671 | end if; |
3672 | ||
3673 | return True; | |
3674 | end Discriminants_Ok; | |
3675 | ||
3676 | -- Start of processing for Convert_Aggr_In_Object_Decl | |
3677 | ||
70482933 RK |
3678 | begin |
3679 | Set_Assignment_OK (Occ); | |
3680 | ||
fbf5a39b AC |
3681 | if Has_Discriminants (Typ) |
3682 | and then Typ /= Etype (Obj) | |
3683 | and then Is_Constrained (Etype (Obj)) | |
3684 | and then not Discriminants_Ok | |
3685 | then | |
3686 | return; | |
3687 | end if; | |
3688 | ||
0f95b178 JM |
3689 | -- If the context is an extended return statement, it has its own |
3690 | -- finalization machinery (i.e. works like a transient scope) and | |
3691 | -- we do not want to create an additional one, because objects on | |
3692 | -- the finalization list of the return must be moved to the caller's | |
3693 | -- finalization list to complete the return. | |
3694 | ||
fff7a6d9 | 3695 | -- Similarly if the aggregate is limited, it is built in place, and the |
3b9fa2df ES |
3696 | -- controlled components are not assigned to intermediate temporaries |
3697 | -- so there is no need for a transient scope in this case either. | |
3698 | ||
0f95b178 JM |
3699 | if Requires_Transient_Scope (Typ) |
3700 | and then Ekind (Current_Scope) /= E_Return_Statement | |
3b9fa2df | 3701 | and then not Is_Limited_Type (Typ) |
0f95b178 | 3702 | then |
6560f851 | 3703 | Establish_Transient_Scope (Aggr, Manage_Sec_Stack => False); |
fff7a6d9 | 3704 | Has_Transient_Scope := True; |
6f5c2c4b | 3705 | end if; |
02217452 | 3706 | |
6f5c2c4b | 3707 | declare |
fff7a6d9 AC |
3708 | Stmts : constant List_Id := Late_Expansion (Aggr, Typ, Occ); |
3709 | Stmt : Node_Id; | |
3710 | Param : Node_Id; | |
3711 | ||
6f5c2c4b | 3712 | begin |
fff7a6d9 AC |
3713 | -- If Obj is already frozen or if N is wrapped in a transient scope, |
3714 | -- Stmts do not need to be saved in Initialization_Statements since | |
3715 | -- there is no freezing issue. | |
3716 | ||
3717 | if Is_Frozen (Obj) or else Has_Transient_Scope then | |
3718 | Insert_Actions_After (N, Stmts); | |
3719 | else | |
3720 | Stmt := Make_Compound_Statement (Sloc (N), Actions => Stmts); | |
3721 | Insert_Action_After (N, Stmt); | |
3722 | ||
3723 | -- Insert_Action_After may freeze Obj in which case we should | |
3724 | -- remove the compound statement just created and simply insert | |
3725 | -- Stmts after N. | |
3726 | ||
3727 | if Is_Frozen (Obj) then | |
3728 | Remove (Stmt); | |
3729 | Insert_Actions_After (N, Stmts); | |
3730 | else | |
3731 | Set_Initialization_Statements (Obj, Stmt); | |
3732 | end if; | |
3733 | end if; | |
3734 | ||
3735 | -- If Typ has controlled components and a call to a Slice_Assign | |
3736 | -- procedure is part of the initialization statements, then we | |
3737 | -- need to initialize the array component since Slice_Assign will | |
3738 | -- need to adjust it. | |
3739 | ||
3740 | if Has_Controlled_Component (Typ) then | |
3741 | Stmt := First (Stmts); | |
3742 | ||
3743 | while Present (Stmt) loop | |
3744 | if Nkind (Stmt) = N_Procedure_Call_Statement | |
f82fb002 | 3745 | and then Is_TSS (Entity (Name (Stmt)), TSS_Slice_Assign) |
fff7a6d9 AC |
3746 | then |
3747 | Param := First (Parameter_Associations (Stmt)); | |
3748 | Insert_Actions | |
3749 | (Stmt, | |
3750 | Build_Initialization_Call | |
3751 | (Sloc (N), New_Copy_Tree (Param), Etype (Param))); | |
3752 | end if; | |
3753 | ||
3754 | Next (Stmt); | |
3755 | end loop; | |
3756 | end if; | |
6f5c2c4b | 3757 | end; |
6560f851 | 3758 | |
70482933 | 3759 | Set_No_Initialization (N); |
a671959b ES |
3760 | |
3761 | -- After expansion the expression can be removed from the declaration | |
3762 | -- except if the object is class-wide, in which case the aggregate | |
3763 | -- provides the actual type. | |
3764 | ||
3765 | if not Is_Class_Wide_Type (Etype (Obj)) then | |
3766 | Set_Expression (N, Empty); | |
3767 | end if; | |
3768 | ||
07fc65c4 | 3769 | Initialize_Discriminants (N, Typ); |
70482933 RK |
3770 | end Convert_Aggr_In_Object_Decl; |
3771 | ||
6f639c98 | 3772 | ------------------------------------- |
3b9fa2df | 3773 | -- Convert_Array_Aggr_In_Allocator -- |
6f639c98 ES |
3774 | ------------------------------------- |
3775 | ||
3776 | procedure Convert_Array_Aggr_In_Allocator | |
3777 | (Decl : Node_Id; | |
3778 | Aggr : Node_Id; | |
3779 | Target : Node_Id) | |
3780 | is | |
6f639c98 ES |
3781 | Typ : constant Entity_Id := Etype (Aggr); |
3782 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
2fedcc18 EB |
3783 | Aggr_Code : List_Id; |
3784 | New_Aggr : Node_Id; | |
6f639c98 ES |
3785 | |
3786 | begin | |
2fedcc18 EB |
3787 | -- The target is an explicit dereference of the allocated object |
3788 | ||
3789 | -- If the assignment can be done directly by the back end, then | |
3790 | -- reset Set_Expansion_Delayed and do not expand further. | |
3791 | ||
3792 | if not CodePeer_Mode | |
3793 | and then not Modify_Tree_For_C | |
3794 | and then Aggr_Assignment_OK_For_Backend (Aggr) | |
3795 | then | |
3796 | New_Aggr := New_Copy_Tree (Aggr); | |
3797 | Set_Expansion_Delayed (New_Aggr, False); | |
3798 | ||
742084ad MP |
3799 | -- In the case of Target's type using the Designated_Storage_Model |
3800 | -- aspect with a Copy_To procedure, insert a temporary and have the | |
3801 | -- back end handle the assignment to it. Copy the result to the | |
3802 | -- original target. | |
3803 | ||
3804 | if Has_Designated_Storage_Model_Aspect | |
3805 | (Etype (Prefix (Expression (Target)))) | |
3806 | and then Present (Storage_Model_Copy_To | |
3807 | (Storage_Model_Object | |
3808 | (Etype (Prefix (Expression (Target)))))) | |
3809 | then | |
ca4bff3a EB |
3810 | Aggr_Code := |
3811 | Build_Assignment_With_Temporary (Target, Typ, New_Aggr); | |
3812 | ||
742084ad MP |
3813 | else |
3814 | Aggr_Code := | |
3815 | New_List ( | |
3816 | Make_OK_Assignment_Statement (Sloc (New_Aggr), | |
3817 | Name => Target, | |
3818 | Expression => New_Aggr)); | |
3819 | end if; | |
6f639c98 | 3820 | |
2fedcc18 EB |
3821 | -- Or else, generate component assignments to it, as for an aggregate |
3822 | -- that appears on the right-hand side of an assignment statement. | |
2fedcc18 EB |
3823 | else |
3824 | Aggr_Code := | |
3825 | Build_Array_Aggr_Code (Aggr, | |
3826 | Ctype => Ctyp, | |
3827 | Index => First_Index (Typ), | |
3828 | Into => Target, | |
3829 | Scalar_Comp => Is_Scalar_Type (Ctyp)); | |
3830 | end if; | |
6f639c98 ES |
3831 | |
3832 | Insert_Actions_After (Decl, Aggr_Code); | |
3833 | end Convert_Array_Aggr_In_Allocator; | |
3834 | ||
4ff5aa0c AC |
3835 | ------------------------ |
3836 | -- In_Place_Assign_OK -- | |
3837 | ------------------------ | |
3838 | ||
a80b1eb7 EB |
3839 | function In_Place_Assign_OK |
3840 | (N : Node_Id; | |
3841 | Target_Object : Entity_Id := Empty) return Boolean | |
3842 | is | |
4ff5aa0c AC |
3843 | Is_Array : constant Boolean := Is_Array_Type (Etype (N)); |
3844 | ||
a80b1eb7 | 3845 | Aggr_In : Node_Id; |
7c4f3267 | 3846 | Aggr_Bounds : Range_Nodes; |
a80b1eb7 | 3847 | Obj_In : Node_Id; |
7c4f3267 | 3848 | Obj_Bounds : Range_Nodes; |
a80b1eb7 EB |
3849 | Parent_Kind : Node_Kind; |
3850 | Parent_Node : Node_Id; | |
4ff5aa0c AC |
3851 | |
3852 | function Safe_Aggregate (Aggr : Node_Id) return Boolean; | |
3853 | -- Check recursively that each component of a (sub)aggregate does not | |
3854 | -- depend on the variable being assigned to. | |
3855 | ||
3856 | function Safe_Component (Expr : Node_Id) return Boolean; | |
a80b1eb7 EB |
3857 | -- Verify that an expression cannot depend on the target being assigned |
3858 | -- to. Return true for compile-time known values, stand-alone objects, | |
3859 | -- parameters passed by copy, calls to functions that return by copy, | |
3860 | -- selected components thereof only if the aggregate's type is an array, | |
3861 | -- indexed components and slices thereof only if the aggregate's type is | |
3862 | -- a record, and simple expressions involving only these as operands. | |
3863 | -- This is OK whatever the target because, for a component to overlap | |
3864 | -- with the target, it must be either a direct reference to a component | |
3865 | -- of the target, in which case there must be a matching selection or | |
3866 | -- indexation or slicing, or an indirect reference to such a component, | |
3867 | -- which is excluded by the above condition. Additionally, if the target | |
3868 | -- is statically known, return true for arbitrarily nested selections, | |
3869 | -- indexations or slicings, provided that their ultimate prefix is not | |
3870 | -- the target itself. | |
4ff5aa0c AC |
3871 | |
3872 | -------------------- | |
3873 | -- Safe_Aggregate -- | |
3874 | -------------------- | |
3875 | ||
3876 | function Safe_Aggregate (Aggr : Node_Id) return Boolean is | |
3877 | Expr : Node_Id; | |
3878 | ||
3879 | begin | |
3880 | if Nkind (Parent (Aggr)) = N_Iterated_Component_Association then | |
3881 | return False; | |
3882 | end if; | |
3883 | ||
3884 | if Present (Expressions (Aggr)) then | |
3885 | Expr := First (Expressions (Aggr)); | |
3886 | while Present (Expr) loop | |
3887 | if Nkind (Expr) = N_Aggregate then | |
3888 | if not Safe_Aggregate (Expr) then | |
3889 | return False; | |
3890 | end if; | |
3891 | ||
3892 | elsif not Safe_Component (Expr) then | |
3893 | return False; | |
3894 | end if; | |
3895 | ||
3896 | Next (Expr); | |
3897 | end loop; | |
3898 | end if; | |
3899 | ||
3900 | if Present (Component_Associations (Aggr)) then | |
3901 | Expr := First (Component_Associations (Aggr)); | |
3902 | while Present (Expr) loop | |
3903 | if Nkind (Expression (Expr)) = N_Aggregate then | |
3904 | if not Safe_Aggregate (Expression (Expr)) then | |
3905 | return False; | |
3906 | end if; | |
3907 | ||
bc1146e5 HK |
3908 | -- If association has a box, no way to determine yet whether |
3909 | -- default can be assigned in place. | |
4ff5aa0c AC |
3910 | |
3911 | elsif Box_Present (Expr) then | |
3912 | return False; | |
3913 | ||
3914 | elsif not Safe_Component (Expression (Expr)) then | |
3915 | return False; | |
3916 | end if; | |
3917 | ||
3918 | Next (Expr); | |
3919 | end loop; | |
3920 | end if; | |
3921 | ||
3922 | return True; | |
3923 | end Safe_Aggregate; | |
3924 | ||
3925 | -------------------- | |
3926 | -- Safe_Component -- | |
3927 | -------------------- | |
3928 | ||
3929 | function Safe_Component (Expr : Node_Id) return Boolean is | |
3930 | Comp : Node_Id := Expr; | |
3931 | ||
a80b1eb7 EB |
3932 | function Check_Component (C : Node_Id; T_OK : Boolean) return Boolean; |
3933 | -- Do the recursive traversal, after copy. If T_OK is True, return | |
3934 | -- True for a stand-alone object only if the target is statically | |
3935 | -- known and distinct from the object. At the top level, we start | |
3936 | -- with T_OK set to False and set it to True at a deeper level only | |
3937 | -- if we cannot disambiguate the component here without statically | |
3938 | -- knowing the target. Note that this is not optimal, we should do | |
3939 | -- something along the lines of Denotes_Same_Prefix for that. | |
4ff5aa0c AC |
3940 | |
3941 | --------------------- | |
3942 | -- Check_Component -- | |
3943 | --------------------- | |
3944 | ||
a80b1eb7 EB |
3945 | function Check_Component (C : Node_Id; T_OK : Boolean) return Boolean |
3946 | is | |
3947 | ||
3948 | function SDO (E : Entity_Id) return Uint; | |
3949 | -- Return the Scope Depth Of the enclosing dynamic scope of E | |
3950 | ||
3951 | --------- | |
3952 | -- SDO -- | |
3953 | --------- | |
3954 | ||
3955 | function SDO (E : Entity_Id) return Uint is | |
3956 | begin | |
3957 | return Scope_Depth (Enclosing_Dynamic_Scope (E)); | |
3958 | end SDO; | |
3959 | ||
3960 | -- Start of processing for Check_Component | |
3961 | ||
4ff5aa0c | 3962 | begin |
a80b1eb7 | 3963 | if Is_Overloaded (C) then |
4ff5aa0c | 3964 | return False; |
a80b1eb7 EB |
3965 | |
3966 | elsif Compile_Time_Known_Value (C) then | |
3967 | return True; | |
4ff5aa0c AC |
3968 | end if; |
3969 | ||
a80b1eb7 EB |
3970 | case Nkind (C) is |
3971 | when N_Attribute_Reference => | |
3972 | return Check_Component (Prefix (C), T_OK); | |
3973 | ||
3974 | when N_Function_Call => | |
3975 | if Nkind (Name (C)) = N_Explicit_Dereference then | |
3976 | return not Returns_By_Ref (Etype (Name (C))); | |
3977 | else | |
3978 | return not Returns_By_Ref (Entity (Name (C))); | |
3979 | end if; | |
3980 | ||
3981 | when N_Indexed_Component | N_Slice => | |
3982 | -- In a target record, these operations cannot determine | |
3983 | -- alone a component so we can recurse whatever the target. | |
3984 | return Check_Component (Prefix (C), T_OK or else Is_Array); | |
3985 | ||
3986 | when N_Selected_Component => | |
3987 | -- In a target array, this operation cannot determine alone | |
3988 | -- a component so we can recurse whatever the target. | |
3989 | return | |
3990 | Check_Component (Prefix (C), T_OK or else not Is_Array); | |
3991 | ||
3992 | when N_Type_Conversion | N_Unchecked_Type_Conversion => | |
3993 | return Check_Component (Expression (C), T_OK); | |
4ff5aa0c | 3994 | |
a80b1eb7 EB |
3995 | when N_Binary_Op => |
3996 | return Check_Component (Left_Opnd (C), T_OK) | |
3997 | and then Check_Component (Right_Opnd (C), T_OK); | |
4ff5aa0c | 3998 | |
a80b1eb7 EB |
3999 | when N_Unary_Op => |
4000 | return Check_Component (Right_Opnd (C), T_OK); | |
4ff5aa0c | 4001 | |
a80b1eb7 EB |
4002 | when others => |
4003 | if Is_Entity_Name (C) and then Is_Object (Entity (C)) then | |
4004 | -- Case of a formal parameter component. It's either | |
4005 | -- trivial if passed by copy or very annoying if not, | |
4006 | -- because in the latter case it's almost equivalent | |
4007 | -- to a dereference, so the path-based disambiguation | |
4008 | -- logic is totally off and we always need the target. | |
4ff5aa0c | 4009 | |
a80b1eb7 EB |
4010 | if Is_Formal (Entity (C)) then |
4011 | ||
4012 | -- If it is passed by copy, then this is safe | |
4013 | ||
4014 | if Mechanism (Entity (C)) = By_Copy then | |
4015 | return True; | |
4016 | ||
4017 | -- Otherwise, this is safe if the target is present | |
4018 | -- and is at least as deeply nested as the component. | |
4019 | ||
4020 | else | |
4021 | return Present (Target_Object) | |
4022 | and then not Is_Formal (Target_Object) | |
4023 | and then SDO (Target_Object) >= SDO (Entity (C)); | |
4024 | end if; | |
4025 | ||
4026 | -- For a renamed object, recurse | |
4027 | ||
4028 | elsif Present (Renamed_Object (Entity (C))) then | |
4029 | return | |
4030 | Check_Component (Renamed_Object (Entity (C)), T_OK); | |
4031 | ||
4032 | -- If this is safe whatever the target, we are done | |
4033 | ||
4034 | elsif not T_OK then | |
4035 | return True; | |
4036 | ||
4037 | -- If there is no target or the component is the target, | |
4038 | -- this is not safe. | |
4039 | ||
4040 | elsif No (Target_Object) | |
4041 | or else Entity (C) = Target_Object | |
4042 | then | |
4043 | return False; | |
4044 | ||
4045 | -- Case of a formal parameter target. This is safe if it | |
4046 | -- is at most as deeply nested as the component. | |
4047 | ||
4048 | elsif Is_Formal (Target_Object) then | |
4049 | return SDO (Target_Object) <= SDO (Entity (C)); | |
4050 | ||
4051 | -- For distinct stand-alone objects, this is safe | |
4052 | ||
4053 | else | |
4054 | return True; | |
4055 | end if; | |
4ff5aa0c | 4056 | |
a80b1eb7 | 4057 | -- For anything else than an object, this is not safe |
4ff5aa0c | 4058 | |
a80b1eb7 EB |
4059 | else |
4060 | return False; | |
4061 | end if; | |
4062 | end case; | |
4ff5aa0c AC |
4063 | end Check_Component; |
4064 | ||
4065 | -- Start of processing for Safe_Component | |
4066 | ||
4067 | begin | |
4068 | -- If the component appears in an association that may correspond | |
4069 | -- to more than one element, it is not analyzed before expansion | |
4070 | -- into assignments, to avoid side effects. We analyze, but do not | |
4071 | -- resolve the copy, to obtain sufficient entity information for | |
4072 | -- the checks that follow. If component is overloaded we assume | |
4073 | -- an unsafe function call. | |
4074 | ||
4075 | if not Analyzed (Comp) then | |
4076 | if Is_Overloaded (Expr) then | |
4077 | return False; | |
4ff5aa0c AC |
4078 | |
4079 | elsif Nkind (Expr) = N_Allocator then | |
4080 | ||
4081 | -- For now, too complex to analyze | |
4082 | ||
4083 | return False; | |
4084 | ||
bc1146e5 HK |
4085 | elsif Nkind (Parent (Expr)) = N_Iterated_Component_Association then |
4086 | ||
4087 | -- Ditto for iterated component associations, which in general | |
4088 | -- require an enclosing loop and involve nonstatic expressions. | |
4ff5aa0c AC |
4089 | |
4090 | return False; | |
4091 | end if; | |
4092 | ||
4093 | Comp := New_Copy_Tree (Expr); | |
4094 | Set_Parent (Comp, Parent (Expr)); | |
4095 | Analyze (Comp); | |
4096 | end if; | |
4097 | ||
4098 | if Nkind (Comp) = N_Aggregate then | |
4099 | return Safe_Aggregate (Comp); | |
4100 | else | |
a80b1eb7 | 4101 | return Check_Component (Comp, False); |
4ff5aa0c AC |
4102 | end if; |
4103 | end Safe_Component; | |
4104 | ||
4105 | -- Start of processing for In_Place_Assign_OK | |
4106 | ||
4107 | begin | |
e67df677 | 4108 | -- By-copy semantic cannot be guaranteed for controlled objects |
4ff5aa0c | 4109 | |
e67df677 | 4110 | if Needs_Finalization (Etype (N)) then |
4ff5aa0c | 4111 | return False; |
a80b1eb7 | 4112 | end if; |
4ff5aa0c | 4113 | |
a80b1eb7 EB |
4114 | Parent_Node := Parent (N); |
4115 | Parent_Kind := Nkind (Parent_Node); | |
4ff5aa0c | 4116 | |
a80b1eb7 EB |
4117 | if Parent_Kind = N_Qualified_Expression then |
4118 | Parent_Node := Parent (Parent_Node); | |
4119 | Parent_Kind := Nkind (Parent_Node); | |
4120 | end if; | |
4ff5aa0c | 4121 | |
a80b1eb7 EB |
4122 | -- On assignment, sliding can take place, so we cannot do the |
4123 | -- assignment in place unless the bounds of the aggregate are | |
4124 | -- statically equal to those of the target. | |
4ff5aa0c | 4125 | |
a80b1eb7 EB |
4126 | -- If the aggregate is given by an others choice, the bounds are |
4127 | -- derived from the left-hand side, and the assignment is safe if | |
4128 | -- the expression is. | |
4ff5aa0c | 4129 | |
a80b1eb7 EB |
4130 | if Is_Array |
4131 | and then Present (Component_Associations (N)) | |
4132 | and then not Is_Others_Aggregate (N) | |
4133 | then | |
4ff5aa0c AC |
4134 | Aggr_In := First_Index (Etype (N)); |
4135 | ||
d0e9248d EB |
4136 | -- Context is an assignment |
4137 | ||
a80b1eb7 EB |
4138 | if Parent_Kind = N_Assignment_Statement then |
4139 | Obj_In := First_Index (Etype (Name (Parent_Node))); | |
4ff5aa0c | 4140 | |
d0e9248d EB |
4141 | -- Context is an allocator. Check the bounds of the aggregate against |
4142 | -- those of the designated type, except in the case where the type is | |
4143 | -- unconstrained (and then we can directly return true, see below). | |
4144 | ||
4145 | else pragma Assert (Parent_Kind = N_Allocator); | |
4146 | declare | |
4147 | Desig_Typ : constant Entity_Id := | |
4148 | Designated_Type (Etype (Parent_Node)); | |
4149 | begin | |
4150 | if not Is_Constrained (Desig_Typ) then | |
4151 | return True; | |
4152 | end if; | |
4ff5aa0c | 4153 | |
d0e9248d EB |
4154 | Obj_In := First_Index (Desig_Typ); |
4155 | end; | |
4ff5aa0c AC |
4156 | end if; |
4157 | ||
4158 | while Present (Aggr_In) loop | |
7c4f3267 BD |
4159 | Aggr_Bounds := Get_Index_Bounds (Aggr_In); |
4160 | Obj_Bounds := Get_Index_Bounds (Obj_In); | |
4ff5aa0c | 4161 | |
a80b1eb7 EB |
4162 | -- We require static bounds for the target and a static matching |
4163 | -- of low bound for the aggregate. | |
4164 | ||
7c4f3267 BD |
4165 | if not Compile_Time_Known_Value (Obj_Bounds.First) |
4166 | or else not Compile_Time_Known_Value (Obj_Bounds.Last) | |
4167 | or else not Compile_Time_Known_Value (Aggr_Bounds.First) | |
4168 | or else Expr_Value (Aggr_Bounds.First) /= | |
4169 | Expr_Value (Obj_Bounds.First) | |
4ff5aa0c AC |
4170 | then |
4171 | return False; | |
4172 | ||
4173 | -- For an assignment statement we require static matching of | |
4174 | -- bounds. Ditto for an allocator whose qualified expression | |
4175 | -- is a constrained type. If the expression in the allocator | |
4176 | -- is an unconstrained array, we accept an upper bound that | |
4177 | -- is not static, to allow for nonstatic expressions of the | |
4178 | -- base type. Clearly there are further possibilities (with | |
4179 | -- diminishing returns) for safely building arrays in place | |
4180 | -- here. | |
4181 | ||
a80b1eb7 EB |
4182 | elsif Parent_Kind = N_Assignment_Statement |
4183 | or else Is_Constrained (Etype (Parent_Node)) | |
4ff5aa0c | 4184 | then |
7c4f3267 BD |
4185 | if not Compile_Time_Known_Value (Aggr_Bounds.Last) |
4186 | or else Expr_Value (Aggr_Bounds.Last) /= | |
4187 | Expr_Value (Obj_Bounds.Last) | |
4ff5aa0c AC |
4188 | then |
4189 | return False; | |
4190 | end if; | |
4191 | end if; | |
4192 | ||
4193 | Next_Index (Aggr_In); | |
4194 | Next_Index (Obj_In); | |
4195 | end loop; | |
4196 | end if; | |
4197 | ||
a80b1eb7 EB |
4198 | -- Now check the component values themselves, except for an allocator |
4199 | -- for which the target is newly allocated memory. | |
4ff5aa0c | 4200 | |
a80b1eb7 EB |
4201 | if Parent_Kind = N_Allocator then |
4202 | return True; | |
4203 | else | |
4204 | return Safe_Aggregate (N); | |
4205 | end if; | |
4ff5aa0c AC |
4206 | end In_Place_Assign_OK; |
4207 | ||
70482933 RK |
4208 | ---------------------------- |
4209 | -- Convert_To_Assignments -- | |
4210 | ---------------------------- | |
4211 | ||
4212 | procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is | |
4213 | Loc : constant Source_Ptr := Sloc (N); | |
39f346aa | 4214 | T : Entity_Id; |
70482933 RK |
4215 | Temp : Entity_Id; |
4216 | ||
f29afe5f | 4217 | Aggr_Code : List_Id; |
fbf5a39b AC |
4218 | Instr : Node_Id; |
4219 | Target_Expr : Node_Id; | |
4220 | Parent_Kind : Node_Kind; | |
4221 | Unc_Decl : Boolean := False; | |
4222 | Parent_Node : Node_Id; | |
70482933 RK |
4223 | |
4224 | begin | |
4a08c95c | 4225 | pragma Assert (Nkind (N) in N_Aggregate | N_Extension_Aggregate); |
fa57ac97 ES |
4226 | pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N)); |
4227 | pragma Assert (Is_Record_Type (Typ)); | |
4228 | ||
70482933 RK |
4229 | Parent_Node := Parent (N); |
4230 | Parent_Kind := Nkind (Parent_Node); | |
4231 | ||
4232 | if Parent_Kind = N_Qualified_Expression then | |
d4dfb005 | 4233 | -- Check if we are in an unconstrained declaration because in this |
70482933 | 4234 | -- case the current delayed expansion mechanism doesn't work when |
d4dfb005 | 4235 | -- the declared object size depends on the initializing expr. |
70482933 | 4236 | |
937e9676 AC |
4237 | Parent_Node := Parent (Parent_Node); |
4238 | Parent_Kind := Nkind (Parent_Node); | |
fbf5a39b | 4239 | |
937e9676 AC |
4240 | if Parent_Kind = N_Object_Declaration then |
4241 | Unc_Decl := | |
4242 | not Is_Entity_Name (Object_Definition (Parent_Node)) | |
d4dfb005 | 4243 | or else (Nkind (N) = N_Aggregate |
3fc40cd7 PMR |
4244 | and then |
4245 | Has_Discriminants | |
4246 | (Entity (Object_Definition (Parent_Node)))) | |
937e9676 AC |
4247 | or else Is_Class_Wide_Type |
4248 | (Entity (Object_Definition (Parent_Node))); | |
4249 | end if; | |
70482933 RK |
4250 | end if; |
4251 | ||
3b9fa2df ES |
4252 | -- Just set the Delay flag in the cases where the transformation will be |
4253 | -- done top down from above. | |
fbf5a39b | 4254 | |
f037632e | 4255 | if |
d22792bc EB |
4256 | -- Internal aggregates (transformed when expanding the parent), |
4257 | -- excluding container aggregates as these are transformed into | |
4258 | -- subprogram calls later. | |
0f95b178 | 4259 | |
ab246c16 EB |
4260 | (Parent_Kind = N_Component_Association |
4261 | and then not Is_Container_Aggregate (Parent (Parent_Node))) | |
4262 | ||
4263 | or else (Parent_Kind in N_Aggregate | N_Extension_Aggregate | |
4264 | and then not Is_Container_Aggregate (Parent_Node)) | |
0f95b178 | 4265 | |
fa57ac97 | 4266 | -- Allocator (see Convert_Aggr_In_Allocator) |
70482933 | 4267 | |
fa57ac97 | 4268 | or else Parent_Kind = N_Allocator |
0f95b178 | 4269 | |
fa57ac97 ES |
4270 | -- Object declaration (see Convert_Aggr_In_Object_Decl) |
4271 | ||
4272 | or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl) | |
4273 | ||
4274 | -- Safe assignment (see Convert_Aggr_Assignments). So far only the | |
4275 | -- assignments in init procs are taken into account. | |
4276 | ||
4277 | or else (Parent_Kind = N_Assignment_Statement | |
4278 | and then Inside_Init_Proc) | |
4279 | ||
bdc193ba AC |
4280 | -- (Ada 2005) An inherently limited type in a return statement, which |
4281 | -- will be handled in a build-in-place fashion, and may be rewritten | |
4282 | -- as an extended return and have its own finalization machinery. | |
4283 | -- In the case of a simple return, the aggregate needs to be delayed | |
4284 | -- until the scope for the return statement has been created, so | |
4285 | -- that any finalization chain will be associated with that scope. | |
4286 | -- For extended returns, we delay expansion to avoid the creation | |
4287 | -- of an unwanted transient scope that could result in premature | |
a9bbfbd0 | 4288 | -- finalization of the return object (which is built in place |
bdc193ba | 4289 | -- within the caller's scope). |
fa57ac97 | 4290 | |
d4dfb005 | 4291 | or else Is_Build_In_Place_Aggregate_Return (N) |
70482933 RK |
4292 | then |
4293 | Set_Expansion_Delayed (N); | |
4294 | return; | |
4295 | end if; | |
4296 | ||
a9bbfbd0 AC |
4297 | -- Otherwise, if a transient scope is required, create it now. If we |
4298 | -- are within an initialization procedure do not create such, because | |
4299 | -- the target of the assignment must not be declared within a local | |
4300 | -- block, and because cleanup will take place on return from the | |
4301 | -- initialization procedure. | |
937e9676 | 4302 | |
a9bbfbd0 AC |
4303 | -- Should the condition be more restrictive ??? |
4304 | ||
4305 | if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then | |
6560f851 | 4306 | Establish_Transient_Scope (N, Manage_Sec_Stack => False); |
70482933 RK |
4307 | end if; |
4308 | ||
bc1146e5 HK |
4309 | -- If the aggregate is nonlimited, create a temporary, since aggregates |
4310 | -- have "by copy" semantics. If it is limited and context is an | |
4ff5aa0c AC |
4311 | -- assignment, this is a subaggregate for an enclosing aggregate being |
4312 | -- expanded. It must be built in place, so use target of the current | |
4313 | -- assignment. | |
70482933 | 4314 | |
3b9fa2df | 4315 | if Is_Limited_Type (Typ) |
e67df677 | 4316 | and then Parent_Kind = N_Assignment_Statement |
3b9fa2df | 4317 | then |
e67df677 EB |
4318 | Target_Expr := New_Copy_Tree (Name (Parent_Node)); |
4319 | Insert_Actions (Parent_Node, | |
df3e68b1 | 4320 | Build_Record_Aggr_Code (N, Typ, Target_Expr)); |
e67df677 | 4321 | Rewrite (Parent_Node, Make_Null_Statement (Loc)); |
70482933 | 4322 | |
a80b1eb7 EB |
4323 | -- Do not declare a temporary to initialize an aggregate assigned to |
4324 | -- a target when in-place assignment is possible, i.e. preserving the | |
4ff5aa0c AC |
4325 | -- by-copy semantic of aggregates. This avoids large stack usage and |
4326 | -- generates more efficient code. | |
e64ac631 | 4327 | |
e67df677 | 4328 | elsif Parent_Kind = N_Assignment_Statement |
a80b1eb7 | 4329 | and then In_Place_Assign_OK (N, Get_Base_Object (Name (Parent_Node))) |
e64ac631 | 4330 | then |
e67df677 EB |
4331 | declare |
4332 | Lhs : constant Node_Id := Name (Parent_Node); | |
4333 | begin | |
4334 | -- Apply discriminant check if required | |
4335 | ||
4336 | if Has_Discriminants (Etype (N)) then | |
4337 | Apply_Discriminant_Check (N, Etype (Lhs), Lhs); | |
4338 | end if; | |
4339 | ||
4340 | -- The check just above may have replaced the aggregate with a CE | |
4341 | ||
4a08c95c | 4342 | if Nkind (N) in N_Aggregate | N_Extension_Aggregate then |
e67df677 EB |
4343 | Target_Expr := New_Copy_Tree (Lhs); |
4344 | Insert_Actions (Parent_Node, | |
4345 | Build_Record_Aggr_Code (N, Typ, Target_Expr)); | |
4346 | Rewrite (Parent_Node, Make_Null_Statement (Loc)); | |
4347 | end if; | |
4348 | end; | |
e64ac631 | 4349 | |
3b9fa2df | 4350 | else |
faf387e1 | 4351 | Temp := Make_Temporary (Loc, 'A', N); |
70482933 | 4352 | |
39f346aa ES |
4353 | -- If the type inherits unknown discriminants, use the view with |
4354 | -- known discriminants if available. | |
4355 | ||
4356 | if Has_Unknown_Discriminants (Typ) | |
36a66365 | 4357 | and then Present (Underlying_Record_View (Typ)) |
39f346aa ES |
4358 | then |
4359 | T := Underlying_Record_View (Typ); | |
4360 | else | |
4361 | T := Typ; | |
4362 | end if; | |
4363 | ||
3b9fa2df ES |
4364 | Instr := |
4365 | Make_Object_Declaration (Loc, | |
4366 | Defining_Identifier => Temp, | |
39f346aa | 4367 | Object_Definition => New_Occurrence_Of (T, Loc)); |
3b9fa2df ES |
4368 | |
4369 | Set_No_Initialization (Instr); | |
4370 | Insert_Action (N, Instr); | |
39f346aa | 4371 | Initialize_Discriminants (Instr, T); |
f29afe5f | 4372 | |
3b9fa2df | 4373 | Target_Expr := New_Occurrence_Of (Temp, Loc); |
f29afe5f AC |
4374 | Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr); |
4375 | ||
4376 | -- Save the last assignment statement associated with the aggregate | |
4377 | -- when building a controlled object. This reference is utilized by | |
4378 | -- the finalization machinery when marking an object as successfully | |
4379 | -- initialized. | |
4380 | ||
4381 | if Needs_Finalization (T) then | |
4382 | Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code)); | |
4383 | end if; | |
4384 | ||
4385 | Insert_Actions (N, Aggr_Code); | |
3b9fa2df | 4386 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
39f346aa | 4387 | Analyze_And_Resolve (N, T); |
3b9fa2df | 4388 | end if; |
70482933 RK |
4389 | end Convert_To_Assignments; |
4390 | ||
07fc65c4 GB |
4391 | --------------------------- |
4392 | -- Convert_To_Positional -- | |
4393 | --------------------------- | |
4394 | ||
4395 | procedure Convert_To_Positional | |
c42006e9 AC |
4396 | (N : Node_Id; |
4397 | Handle_Bit_Packed : Boolean := False) | |
07fc65c4 | 4398 | is |
c42006e9 | 4399 | Typ : constant Entity_Id := Etype (N); |
b748c3d1 | 4400 | Dims : constant Nat := Number_Dimensions (Typ); |
eaf6e63a | 4401 | Max_Others_Replicate : constant Nat := Max_Aggregate_Size (N); |
07fc65c4 | 4402 | |
0f95b178 JM |
4403 | Static_Components : Boolean := True; |
4404 | ||
4405 | procedure Check_Static_Components; | |
3b9fa2df ES |
4406 | -- Check whether all components of the aggregate are compile-time known |
4407 | -- values, and can be passed as is to the back-end without further | |
4408 | -- expansion. | |
0f95b178 | 4409 | |
fbf5a39b | 4410 | function Flatten |
b748c3d1 EB |
4411 | (N : Node_Id; |
4412 | Dims : Nat; | |
4413 | Ix : Node_Id; | |
4414 | Ixb : Node_Id) return Boolean; | |
c2ba82ad EB |
4415 | -- Convert the aggregate into a purely positional form if possible after |
4416 | -- checking that the bounds of all dimensions are known to be static. | |
fbf5a39b | 4417 | |
b748c3d1 EB |
4418 | function Is_Flat (N : Node_Id; Dims : Nat) return Boolean; |
4419 | -- Return True if the aggregate N is flat (which is not trivial in the | |
4420 | -- case of multidimensional aggregates). | |
fbf5a39b | 4421 | |
b748c3d1 | 4422 | function Is_Static_Element (N : Node_Id; Dims : Nat) return Boolean; |
f1e2bf65 EB |
4423 | -- Return True if N, an element of a component association list, i.e. |
4424 | -- N_Component_Association or N_Iterated_Component_Association, has a | |
4425 | -- compile-time known value and can be passed as is to the back-end | |
4426 | -- without further expansion. | |
4427 | -- An Iterated_Component_Association is treated as nonstatic in most | |
4428 | -- cases for now, so there are possibilities for optimization. | |
4429 | ||
0f95b178 JM |
4430 | ----------------------------- |
4431 | -- Check_Static_Components -- | |
4432 | ----------------------------- | |
4433 | ||
bdc193ba AC |
4434 | -- Could use some comments in this body ??? |
4435 | ||
0f95b178 | 4436 | procedure Check_Static_Components is |
f1e2bf65 EB |
4437 | Assoc : Node_Id; |
4438 | Expr : Node_Id; | |
0f95b178 JM |
4439 | |
4440 | begin | |
4441 | Static_Components := True; | |
4442 | ||
4443 | if Nkind (N) = N_String_Literal then | |
4444 | null; | |
4445 | ||
4446 | elsif Present (Expressions (N)) then | |
4447 | Expr := First (Expressions (N)); | |
4448 | while Present (Expr) loop | |
4449 | if Nkind (Expr) /= N_Aggregate | |
4450 | or else not Compile_Time_Known_Aggregate (Expr) | |
4451 | or else Expansion_Delayed (Expr) | |
4452 | then | |
4453 | Static_Components := False; | |
4454 | exit; | |
4455 | end if; | |
4456 | ||
4457 | Next (Expr); | |
4458 | end loop; | |
4459 | end if; | |
4460 | ||
4461 | if Nkind (N) = N_Aggregate | |
21d7ef70 | 4462 | and then Present (Component_Associations (N)) |
0f95b178 | 4463 | then |
f1e2bf65 EB |
4464 | Assoc := First (Component_Associations (N)); |
4465 | while Present (Assoc) loop | |
b748c3d1 | 4466 | if not Is_Static_Element (Assoc, Dims) then |
0f95b178 JM |
4467 | Static_Components := False; |
4468 | exit; | |
4469 | end if; | |
4470 | ||
f1e2bf65 | 4471 | Next (Assoc); |
0f95b178 JM |
4472 | end loop; |
4473 | end if; | |
4474 | end Check_Static_Components; | |
4475 | ||
fbf5a39b AC |
4476 | ------------- |
4477 | -- Flatten -- | |
4478 | ------------- | |
4479 | ||
4480 | function Flatten | |
b748c3d1 EB |
4481 | (N : Node_Id; |
4482 | Dims : Nat; | |
4483 | Ix : Node_Id; | |
4484 | Ixb : Node_Id) return Boolean | |
fbf5a39b AC |
4485 | is |
4486 | Loc : constant Source_Ptr := Sloc (N); | |
4487 | Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb)); | |
4488 | Lo : constant Node_Id := Type_Low_Bound (Etype (Ix)); | |
4489 | Hi : constant Node_Id := Type_High_Bound (Etype (Ix)); | |
fbf5a39b | 4490 | |
b748c3d1 EB |
4491 | function Cannot_Flatten_Next_Aggr (Expr : Node_Id) return Boolean; |
4492 | -- Return true if Expr is an aggregate for the next dimension that | |
4493 | -- cannot be recursively flattened. | |
4494 | ||
4495 | ------------------------------ | |
4496 | -- Cannot_Flatten_Next_Aggr -- | |
4497 | ------------------------------ | |
4498 | ||
4499 | function Cannot_Flatten_Next_Aggr (Expr : Node_Id) return Boolean is | |
4500 | begin | |
4501 | return Nkind (Expr) = N_Aggregate | |
4502 | and then Present (Next_Index (Ix)) | |
4503 | and then not | |
4504 | Flatten (Expr, Dims - 1, Next_Index (Ix), Next_Index (Ixb)); | |
4505 | end Cannot_Flatten_Next_Aggr; | |
4506 | ||
4507 | -- Local variables | |
4508 | ||
4509 | Lov : Uint; | |
4510 | Hiv : Uint; | |
4511 | Others_Present : Boolean; | |
4512 | ||
4513 | -- Start of processing for Flatten | |
3f5a8fee | 4514 | |
6e937c1c | 4515 | begin |
fbf5a39b AC |
4516 | if Nkind (Original_Node (N)) = N_String_Literal then |
4517 | return True; | |
4518 | end if; | |
07fc65c4 | 4519 | |
0f95b178 JM |
4520 | if not Compile_Time_Known_Value (Lo) |
4521 | or else not Compile_Time_Known_Value (Hi) | |
4522 | then | |
4523 | return False; | |
4524 | end if; | |
07fc65c4 | 4525 | |
fbf5a39b AC |
4526 | Lov := Expr_Value (Lo); |
4527 | Hiv := Expr_Value (Hi); | |
07fc65c4 | 4528 | |
3f5a8fee AC |
4529 | -- Check if there is an others choice |
4530 | ||
b748c3d1 EB |
4531 | Others_Present := False; |
4532 | ||
3f5a8fee | 4533 | if Present (Component_Associations (N)) then |
10c257af ES |
4534 | if Is_Empty_List (Component_Associations (N)) then |
4535 | -- an expanded null array aggregate | |
4536 | return False; | |
4537 | end if; | |
4538 | ||
3f5a8fee AC |
4539 | declare |
4540 | Assoc : Node_Id; | |
4541 | Choice : Node_Id; | |
4542 | ||
4543 | begin | |
4544 | Assoc := First (Component_Associations (N)); | |
4545 | while Present (Assoc) loop | |
9f8d1e5c AC |
4546 | |
4547 | -- If this is a box association, flattening is in general | |
4548 | -- not possible because at this point we cannot tell if the | |
4549 | -- default is static or even exists. | |
4550 | ||
4551 | if Box_Present (Assoc) then | |
4552 | return False; | |
00f45f30 AC |
4553 | |
4554 | elsif Nkind (Assoc) = N_Iterated_Component_Association then | |
4555 | return False; | |
9f8d1e5c AC |
4556 | end if; |
4557 | ||
00f45f30 | 4558 | Choice := First (Choice_List (Assoc)); |
3f5a8fee AC |
4559 | |
4560 | while Present (Choice) loop | |
4561 | if Nkind (Choice) = N_Others_Choice then | |
4562 | Others_Present := True; | |
4563 | end if; | |
4564 | ||
4565 | Next (Choice); | |
4566 | end loop; | |
4567 | ||
4568 | Next (Assoc); | |
4569 | end loop; | |
4570 | end; | |
4571 | end if; | |
4572 | ||
4573 | -- If the low bound is not known at compile time and others is not | |
4574 | -- present we can proceed since the bounds can be obtained from the | |
4575 | -- aggregate. | |
4576 | ||
fbf5a39b | 4577 | if Hiv < Lov |
36a66365 | 4578 | or else (not Compile_Time_Known_Value (Blo) and then Others_Present) |
fbf5a39b AC |
4579 | then |
4580 | return False; | |
4581 | end if; | |
07fc65c4 | 4582 | |
3b9fa2df ES |
4583 | -- Determine if set of alternatives is suitable for conversion and |
4584 | -- build an array containing the values in sequence. | |
07fc65c4 | 4585 | |
fbf5a39b AC |
4586 | declare |
4587 | Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv)) | |
4588 | of Node_Id := (others => Empty); | |
4589 | -- The values in the aggregate sorted appropriately | |
07fc65c4 | 4590 | |
fbf5a39b AC |
4591 | Vlist : List_Id; |
4592 | -- Same data as Vals in list form | |
07fc65c4 | 4593 | |
fbf5a39b AC |
4594 | Rep_Count : Nat; |
4595 | -- Used to validate Max_Others_Replicate limit | |
07fc65c4 | 4596 | |
841dd0f5 | 4597 | Elmt : Node_Id; |
b748c3d1 | 4598 | Expr : Node_Id; |
841dd0f5 AC |
4599 | Num : Int := UI_To_Int (Lov); |
4600 | Choice_Index : Int; | |
4601 | Choice : Node_Id; | |
4602 | Lo, Hi : Node_Id; | |
07fc65c4 | 4603 | |
fbf5a39b AC |
4604 | begin |
4605 | if Present (Expressions (N)) then | |
4606 | Elmt := First (Expressions (N)); | |
fbf5a39b | 4607 | while Present (Elmt) loop |
b748c3d1 EB |
4608 | -- In the case of a multidimensional array, check that the |
4609 | -- aggregate can be recursively flattened. | |
4610 | ||
4611 | if Cannot_Flatten_Next_Aggr (Elmt) then | |
fbf5a39b AC |
4612 | return False; |
4613 | end if; | |
07fc65c4 | 4614 | |
f537fc00 | 4615 | -- Duplicate expression for each index it covers |
1541ede1 ES |
4616 | |
4617 | Vals (Num) := New_Copy_Tree (Elmt); | |
fbf5a39b | 4618 | Num := Num + 1; |
07fc65c4 | 4619 | |
fbf5a39b AC |
4620 | Next (Elmt); |
4621 | end loop; | |
4622 | end if; | |
07fc65c4 | 4623 | |
fbf5a39b AC |
4624 | if No (Component_Associations (N)) then |
4625 | return True; | |
4626 | end if; | |
07fc65c4 | 4627 | |
fbf5a39b | 4628 | Elmt := First (Component_Associations (N)); |
07fc65c4 | 4629 | |
b748c3d1 EB |
4630 | Component_Loop : while Present (Elmt) loop |
4631 | Expr := Expression (Elmt); | |
4632 | ||
4633 | -- In the case of a multidimensional array, check that the | |
4634 | -- aggregate can be recursively flattened. | |
4635 | ||
4636 | if Cannot_Flatten_Next_Aggr (Expr) then | |
fbf5a39b AC |
4637 | return False; |
4638 | end if; | |
07fc65c4 | 4639 | |
00f45f30 | 4640 | Choice := First (Choice_List (Elmt)); |
fbf5a39b AC |
4641 | Choice_Loop : while Present (Choice) loop |
4642 | ||
4643 | -- If we have an others choice, fill in the missing elements | |
4644 | -- subject to the limit established by Max_Others_Replicate. | |
4645 | ||
4646 | if Nkind (Choice) = N_Others_Choice then | |
4647 | Rep_Count := 0; | |
4648 | ||
64a87aa5 EB |
4649 | -- If the expression involves a construct that generates |
4650 | -- a loop, we must generate individual assignments and | |
4651 | -- no flattening is possible. | |
4652 | ||
b748c3d1 | 4653 | if Nkind (Expr) = N_Quantified_Expression then |
1f6237e3 ES |
4654 | return False; |
4655 | end if; | |
4656 | ||
fbf5a39b AC |
4657 | for J in Vals'Range loop |
4658 | if No (Vals (J)) then | |
b748c3d1 | 4659 | Vals (J) := New_Copy_Tree (Expr); |
fbf5a39b AC |
4660 | Rep_Count := Rep_Count + 1; |
4661 | ||
4662 | -- Check for maximum others replication. Note that | |
4663 | -- we skip this test if either of the restrictions | |
b748c3d1 | 4664 | -- No_Implicit_Loops or No_Elaboration_Code is |
8926d369 AC |
4665 | -- active, if this is a preelaborable unit or |
4666 | -- a predefined unit, or if the unit must be | |
4667 | -- placed in data memory. This also ensures that | |
d9819bbd AC |
4668 | -- predefined units get the same level of constant |
4669 | -- folding in Ada 95 and Ada 2005, where their | |
4670 | -- categorization has changed. | |
fbf5a39b AC |
4671 | |
4672 | declare | |
4673 | P : constant Entity_Id := | |
89beb653 | 4674 | Cunit_Entity (Current_Sem_Unit); |
fbf5a39b AC |
4675 | |
4676 | begin | |
f1e2bf65 EB |
4677 | -- Check if duplication is always OK and, if so, |
4678 | -- continue processing. | |
7f4c1903 | 4679 | |
b748c3d1 EB |
4680 | if Restriction_Active (No_Implicit_Loops) then |
4681 | null; | |
4682 | ||
4683 | -- If duplication is not always OK, continue | |
4684 | -- only if either the element is static or is | |
4685 | -- an aggregate (we already know it is OK). | |
4686 | ||
4687 | elsif not Is_Static_Element (Elmt, Dims) | |
4688 | and then Nkind (Expr) /= N_Aggregate | |
4689 | then | |
4690 | return False; | |
4691 | ||
4692 | -- Check if duplication is OK for elaboration | |
4693 | -- purposes and, if so, continue processing. | |
4694 | ||
4695 | elsif Restriction_Active (No_Elaboration_Code) | |
d9819bbd AC |
4696 | or else |
4697 | (Ekind (Current_Scope) = E_Package | |
b748c3d1 EB |
4698 | and then |
4699 | Static_Elaboration_Desired (Current_Scope)) | |
fbf5a39b AC |
4700 | or else Is_Preelaborated (P) |
4701 | or else (Ekind (P) = E_Package_Body | |
4702 | and then | |
b748c3d1 | 4703 | Is_Preelaborated (Spec_Entity (P))) |
7f4c1903 | 4704 | or else |
8ab31c0c | 4705 | Is_Predefined_Unit (Get_Source_Unit (P)) |
fbf5a39b AC |
4706 | then |
4707 | null; | |
6e937c1c | 4708 | |
b748c3d1 EB |
4709 | -- Otherwise, check that the replication count |
4710 | -- is not too high. | |
7f4c1903 | 4711 | |
b748c3d1 | 4712 | elsif Rep_Count > Max_Others_Replicate then |
f1e2bf65 | 4713 | return False; |
fbf5a39b AC |
4714 | end if; |
4715 | end; | |
4716 | end if; | |
4717 | end loop; | |
07fc65c4 | 4718 | |
861e589e ES |
4719 | if Rep_Count = 0 |
4720 | and then Warn_On_Redundant_Constructs | |
4721 | then | |
4722 | Error_Msg_N ("there are no others?r?", Elmt); | |
4723 | end if; | |
4724 | ||
fbf5a39b | 4725 | exit Component_Loop; |
07fc65c4 | 4726 | |
deeb1604 | 4727 | -- Case of a subtype mark, identifier or expanded name |
07fc65c4 | 4728 | |
deeb1604 | 4729 | elsif Is_Entity_Name (Choice) |
fbf5a39b AC |
4730 | and then Is_Type (Entity (Choice)) |
4731 | then | |
4732 | Lo := Type_Low_Bound (Etype (Choice)); | |
4733 | Hi := Type_High_Bound (Etype (Choice)); | |
07fc65c4 | 4734 | |
fbf5a39b | 4735 | -- Case of subtype indication |
07fc65c4 | 4736 | |
fbf5a39b AC |
4737 | elsif Nkind (Choice) = N_Subtype_Indication then |
4738 | Lo := Low_Bound (Range_Expression (Constraint (Choice))); | |
4739 | Hi := High_Bound (Range_Expression (Constraint (Choice))); | |
4740 | ||
4741 | -- Case of a range | |
4742 | ||
4743 | elsif Nkind (Choice) = N_Range then | |
4744 | Lo := Low_Bound (Choice); | |
4745 | Hi := High_Bound (Choice); | |
4746 | ||
4747 | -- Normal subexpression case | |
4748 | ||
4749 | else pragma Assert (Nkind (Choice) in N_Subexpr); | |
4750 | if not Compile_Time_Known_Value (Choice) then | |
4751 | return False; | |
4752 | ||
4753 | else | |
841dd0f5 | 4754 | Choice_Index := UI_To_Int (Expr_Value (Choice)); |
bdc193ba | 4755 | |
841dd0f5 | 4756 | if Choice_Index in Vals'Range then |
b748c3d1 | 4757 | Vals (Choice_Index) := New_Copy_Tree (Expr); |
841dd0f5 AC |
4758 | goto Continue; |
4759 | ||
bdc193ba AC |
4760 | -- Choice is statically out-of-range, will be |
4761 | -- rewritten to raise Constraint_Error. | |
841dd0f5 | 4762 | |
bdc193ba | 4763 | else |
841dd0f5 AC |
4764 | return False; |
4765 | end if; | |
07fc65c4 | 4766 | end if; |
fbf5a39b AC |
4767 | end if; |
4768 | ||
64425dff | 4769 | -- Range cases merge with Lo,Hi set |
fbf5a39b AC |
4770 | |
4771 | if not Compile_Time_Known_Value (Lo) | |
4772 | or else | |
4773 | not Compile_Time_Known_Value (Hi) | |
4774 | then | |
4775 | return False; | |
bdc193ba | 4776 | |
fbf5a39b AC |
4777 | else |
4778 | for J in UI_To_Int (Expr_Value (Lo)) .. | |
4779 | UI_To_Int (Expr_Value (Hi)) | |
4780 | loop | |
b748c3d1 | 4781 | Vals (J) := New_Copy_Tree (Expr); |
fbf5a39b AC |
4782 | end loop; |
4783 | end if; | |
07fc65c4 | 4784 | |
fbf5a39b AC |
4785 | <<Continue>> |
4786 | Next (Choice); | |
4787 | end loop Choice_Loop; | |
07fc65c4 | 4788 | |
fbf5a39b AC |
4789 | Next (Elmt); |
4790 | end loop Component_Loop; | |
07fc65c4 | 4791 | |
fbf5a39b | 4792 | -- If we get here the conversion is possible |
07fc65c4 | 4793 | |
fbf5a39b AC |
4794 | Vlist := New_List; |
4795 | for J in Vals'Range loop | |
4796 | Append (Vals (J), Vlist); | |
4797 | end loop; | |
07fc65c4 | 4798 | |
fbf5a39b AC |
4799 | Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist)); |
4800 | Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N))); | |
4801 | return True; | |
4802 | end; | |
4803 | end Flatten; | |
07fc65c4 | 4804 | |
fbf5a39b AC |
4805 | ------------- |
4806 | -- Is_Flat -- | |
4807 | ------------- | |
07fc65c4 | 4808 | |
b748c3d1 | 4809 | function Is_Flat (N : Node_Id; Dims : Nat) return Boolean is |
fbf5a39b | 4810 | Elmt : Node_Id; |
07fc65c4 | 4811 | |
fbf5a39b AC |
4812 | begin |
4813 | if Dims = 0 then | |
4814 | return True; | |
07fc65c4 | 4815 | |
fbf5a39b AC |
4816 | elsif Nkind (N) = N_Aggregate then |
4817 | if Present (Component_Associations (N)) then | |
4818 | return False; | |
07fc65c4 | 4819 | |
fbf5a39b AC |
4820 | else |
4821 | Elmt := First (Expressions (N)); | |
fbf5a39b AC |
4822 | while Present (Elmt) loop |
4823 | if not Is_Flat (Elmt, Dims - 1) then | |
4824 | return False; | |
07fc65c4 | 4825 | end if; |
07fc65c4 | 4826 | |
fbf5a39b AC |
4827 | Next (Elmt); |
4828 | end loop; | |
07fc65c4 | 4829 | |
fbf5a39b AC |
4830 | return True; |
4831 | end if; | |
4832 | else | |
4833 | return True; | |
4834 | end if; | |
4835 | end Is_Flat; | |
07fc65c4 | 4836 | |
f1e2bf65 EB |
4837 | ------------------------- |
4838 | -- Is_Static_Element -- | |
4839 | ------------------------- | |
4840 | ||
b748c3d1 | 4841 | function Is_Static_Element (N : Node_Id; Dims : Nat) return Boolean is |
f1e2bf65 EB |
4842 | Expr : constant Node_Id := Expression (N); |
4843 | ||
4844 | begin | |
b748c3d1 | 4845 | -- In most cases the interesting expressions are unambiguously static |
f1e2bf65 | 4846 | |
b748c3d1 | 4847 | if Compile_Time_Known_Value (Expr) then |
f1e2bf65 EB |
4848 | return True; |
4849 | ||
4850 | elsif Nkind (N) = N_Iterated_Component_Association then | |
4851 | return False; | |
4852 | ||
4853 | elsif Nkind (Expr) = N_Aggregate | |
4854 | and then Compile_Time_Known_Aggregate (Expr) | |
4855 | and then not Expansion_Delayed (Expr) | |
4856 | then | |
4857 | return True; | |
4858 | ||
b748c3d1 EB |
4859 | -- However, one may write static expressions that are syntactically |
4860 | -- ambiguous, so preanalyze the expression before checking it again, | |
4861 | -- but only at the innermost level for a multidimensional array. | |
4862 | ||
4863 | elsif Dims = 1 then | |
4864 | Preanalyze_And_Resolve (Expr, Component_Type (Typ)); | |
4865 | return Compile_Time_Known_Value (Expr); | |
4866 | ||
f1e2bf65 EB |
4867 | else |
4868 | return False; | |
4869 | end if; | |
4870 | end Is_Static_Element; | |
4871 | ||
fbf5a39b | 4872 | -- Start of processing for Convert_To_Positional |
07fc65c4 | 4873 | |
fbf5a39b | 4874 | begin |
6031f544 AC |
4875 | -- Only convert to positional when generating C in case of an |
4876 | -- object declaration, this is the only case where aggregates are | |
4877 | -- supported in C. | |
4878 | ||
9f51b855 | 4879 | if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then |
6031f544 AC |
4880 | return; |
4881 | end if; | |
4882 | ||
0ab80019 | 4883 | -- Ada 2005 (AI-287): Do not convert in case of default initialized |
c45b6ae0 AC |
4884 | -- components because in this case will need to call the corresponding |
4885 | -- IP procedure. | |
4886 | ||
4887 | if Has_Default_Init_Comps (N) then | |
4888 | return; | |
4889 | end if; | |
4890 | ||
d7db3f4f ES |
4891 | -- A subaggregate may have been flattened but is not known to be |
4892 | -- Compile_Time_Known. Set that flag in cases that cannot require | |
4893 | -- elaboration code, so that the aggregate can be used as the | |
4894 | -- initial value of a thread-local variable. | |
4895 | ||
b748c3d1 | 4896 | if Is_Flat (N, Dims) then |
2a1838cd EB |
4897 | if Static_Array_Aggregate (N) then |
4898 | Set_Compile_Time_Known_Aggregate (N); | |
d7db3f4f ES |
4899 | end if; |
4900 | ||
fbf5a39b AC |
4901 | return; |
4902 | end if; | |
4903 | ||
36a66365 | 4904 | if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then |
fbf5a39b AC |
4905 | return; |
4906 | end if; | |
07fc65c4 | 4907 | |
3b9fa2df ES |
4908 | -- Do not convert to positional if controlled components are involved |
4909 | -- since these require special processing | |
07fc65c4 | 4910 | |
fbf5a39b AC |
4911 | if Has_Controlled_Component (Typ) then |
4912 | return; | |
4913 | end if; | |
07fc65c4 | 4914 | |
0f95b178 JM |
4915 | Check_Static_Components; |
4916 | ||
4917 | -- If the size is known, or all the components are static, try to | |
4918 | -- build a fully positional aggregate. | |
4919 | ||
21d7ef70 | 4920 | -- The size of the type may not be known for an aggregate with |
0f95b178 JM |
4921 | -- discriminated array components, but if the components are static |
4922 | -- it is still possible to verify statically that the length is | |
4923 | -- compatible with the upper bound of the type, and therefore it is | |
4924 | -- worth flattening such aggregates as well. | |
4925 | ||
eaf6e63a | 4926 | if Aggr_Size_OK (N) |
b748c3d1 EB |
4927 | and then |
4928 | Flatten (N, Dims, First_Index (Typ), First_Index (Base_Type (Typ))) | |
643a0839 | 4929 | then |
0f95b178 JM |
4930 | if Static_Components then |
4931 | Set_Compile_Time_Known_Aggregate (N); | |
4932 | Set_Expansion_Delayed (N, False); | |
4933 | end if; | |
4934 | ||
07fc65c4 | 4935 | Analyze_And_Resolve (N, Typ); |
fbf5a39b | 4936 | end if; |
d9819bbd | 4937 | |
d74716b3 | 4938 | -- If Static_Elaboration_Desired has been specified, diagnose aggregates |
e6807723 AC |
4939 | -- that will still require initialization code. |
4940 | ||
d9819bbd AC |
4941 | if (Ekind (Current_Scope) = E_Package |
4942 | and then Static_Elaboration_Desired (Current_Scope)) | |
4943 | and then Nkind (Parent (N)) = N_Object_Declaration | |
4944 | then | |
4945 | declare | |
4946 | Expr : Node_Id; | |
4947 | ||
4948 | begin | |
e6807723 | 4949 | if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then |
d9819bbd AC |
4950 | Expr := First (Expressions (N)); |
4951 | while Present (Expr) loop | |
c2ba82ad | 4952 | if not Compile_Time_Known_Value (Expr) then |
8926d369 | 4953 | Error_Msg_N |
21d7ef70 | 4954 | ("non-static object requires elaboration code??", N); |
d9819bbd AC |
4955 | exit; |
4956 | end if; | |
8926d369 | 4957 | |
d9819bbd AC |
4958 | Next (Expr); |
4959 | end loop; | |
4960 | ||
4961 | if Present (Component_Associations (N)) then | |
324ac540 | 4962 | Error_Msg_N ("object requires elaboration code??", N); |
d9819bbd AC |
4963 | end if; |
4964 | end if; | |
4965 | end; | |
4966 | end if; | |
07fc65c4 GB |
4967 | end Convert_To_Positional; |
4968 | ||
70482933 RK |
4969 | ---------------------------- |
4970 | -- Expand_Array_Aggregate -- | |
4971 | ---------------------------- | |
4972 | ||
4973 | -- Array aggregate expansion proceeds as follows: | |
4974 | ||
4975 | -- 1. If requested we generate code to perform all the array aggregate | |
4976 | -- bound checks, specifically | |
4977 | ||
4978 | -- (a) Check that the index range defined by aggregate bounds is | |
4979 | -- compatible with corresponding index subtype. | |
4980 | ||
4981 | -- (b) If an others choice is present check that no aggregate | |
4982 | -- index is outside the bounds of the index constraint. | |
4983 | ||
4984 | -- (c) For multidimensional arrays make sure that all subaggregates | |
4985 | -- corresponding to the same dimension have the same bounds. | |
4986 | ||
fbf5a39b | 4987 | -- 2. Check for packed array aggregate which can be converted to a |
b465ef6f | 4988 | -- constant so that the aggregate disappears completely. |
fbf5a39b AC |
4989 | |
4990 | -- 3. Check case of nested aggregate. Generally nested aggregates are | |
4991 | -- handled during the processing of the parent aggregate. | |
4992 | ||
4993 | -- 4. Check if the aggregate can be statically processed. If this is the | |
70482933 RK |
4994 | -- case pass it as is to Gigi. Note that a necessary condition for |
4995 | -- static processing is that the aggregate be fully positional. | |
4996 | ||
bc1146e5 | 4997 | -- 5. If in-place aggregate expansion is possible (i.e. no need to create |
70482933 RK |
4998 | -- a temporary) then mark the aggregate as such and return. Otherwise |
4999 | -- create a new temporary and generate the appropriate initialization | |
5000 | -- code. | |
5001 | ||
5002 | procedure Expand_Array_Aggregate (N : Node_Id) is | |
5003 | Loc : constant Source_Ptr := Sloc (N); | |
5004 | ||
5005 | Typ : constant Entity_Id := Etype (N); | |
5006 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
07fc65c4 | 5007 | -- Typ is the correct constrained array subtype of the aggregate |
70482933 RK |
5008 | -- Ctyp is the corresponding component type. |
5009 | ||
5010 | Aggr_Dimension : constant Pos := Number_Dimensions (Typ); | |
3cf3e5c6 | 5011 | -- Number of aggregate index dimensions |
70482933 RK |
5012 | |
5013 | Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id; | |
5014 | Aggr_High : array (1 .. Aggr_Dimension) of Node_Id; | |
3cf3e5c6 | 5015 | -- Low and High bounds of the constraint for each aggregate index |
70482933 RK |
5016 | |
5017 | Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id; | |
3cf3e5c6 | 5018 | -- The type of each index |
70482933 | 5019 | |
ac43e11e | 5020 | In_Place_Assign_OK_For_Declaration : Boolean := False; |
bc1146e5 | 5021 | -- True if we are to generate an in-place assignment for a declaration |
ac43e11e | 5022 | |
70482933 RK |
5023 | Maybe_In_Place_OK : Boolean; |
5024 | -- If the type is neither controlled nor packed and the aggregate | |
5025 | -- is the expression in an assignment, assignment in place may be | |
5026 | -- possible, provided other conditions are met on the LHS. | |
5027 | ||
07fc65c4 | 5028 | Others_Present : array (1 .. Aggr_Dimension) of Boolean := |
15f0f591 | 5029 | (others => False); |
d74716b3 AC |
5030 | -- If Others_Present (J) is True, then there is an others choice in one |
5031 | -- of the subaggregates of N at dimension J. | |
70482933 RK |
5032 | |
5033 | procedure Build_Constrained_Type (Positional : Boolean); | |
5034 | -- If the subtype is not static or unconstrained, build a constrained | |
5035 | -- type using the computable sizes of the aggregate and its sub- | |
5036 | -- aggregates. | |
5037 | ||
7c4f3267 | 5038 | procedure Check_Bounds (Aggr_Bounds_Node, Index_Bounds_Node : Node_Id); |
70482933 | 5039 | -- Checks that the bounds of Aggr_Bounds are within the bounds defined |
3424f4c3 PT |
5040 | -- by Index_Bounds. For null array aggregate (Ada 2022) check that the |
5041 | -- aggregate bounds define a null range. | |
70482933 RK |
5042 | |
5043 | procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos); | |
d74716b3 AC |
5044 | -- Checks that in a multidimensional array aggregate all subaggregates |
5045 | -- corresponding to the same dimension have the same bounds. Sub_Aggr is | |
5046 | -- an array subaggregate. Dim is the dimension corresponding to the | |
5047 | -- subaggregate. | |
70482933 RK |
5048 | |
5049 | procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos); | |
d74716b3 AC |
5050 | -- Computes the values of array Others_Present. Sub_Aggr is the array |
5051 | -- subaggregate we start the computation from. Dim is the dimension | |
5052 | -- corresponding to the subaggregate. | |
70482933 | 5053 | |
70482933 | 5054 | procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos); |
d74716b3 | 5055 | -- Checks that if an others choice is present in any subaggregate, no |
70482933 | 5056 | -- aggregate index is outside the bounds of the index constraint. |
d74716b3 AC |
5057 | -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding |
5058 | -- to the subaggregate. | |
70482933 | 5059 | |
8da337c5 AC |
5060 | function Safe_Left_Hand_Side (N : Node_Id) return Boolean; |
5061 | -- In addition to Maybe_In_Place_OK, in order for an aggregate to be | |
5062 | -- built directly into the target of the assignment it must be free | |
6537318f | 5063 | -- of side effects. N is the LHS of an assignment. |
8da337c5 | 5064 | |
fe43084c ES |
5065 | procedure Two_Pass_Aggregate_Expansion (N : Node_Id); |
5066 | -- If the aggregate consists only of iterated associations then the | |
5067 | -- aggregate is constructed in two steps: | |
5068 | -- a) Build an expression to compute the number of elements | |
5069 | -- generated by each iterator, and use the expression to allocate | |
5070 | -- the destination aggregate. | |
5071 | -- b) Generate the loops corresponding to each iterator to insert | |
5072 | -- the elements in their proper positions. | |
5073 | ||
70482933 RK |
5074 | ---------------------------- |
5075 | -- Build_Constrained_Type -- | |
5076 | ---------------------------- | |
5077 | ||
5078 | procedure Build_Constrained_Type (Positional : Boolean) is | |
c8def50f | 5079 | Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A'); |
fbf5a39b | 5080 | Decl : Node_Id; |
c8def50f | 5081 | Indexes : constant List_Id := New_List; |
b3143037 | 5082 | Num : Nat; |
fbf5a39b | 5083 | Sub_Agg : Node_Id; |
70482933 RK |
5084 | |
5085 | begin | |
70482933 RK |
5086 | -- If the aggregate is purely positional, all its subaggregates |
5087 | -- have the same size. We collect the dimensions from the first | |
5088 | -- subaggregate at each level. | |
5089 | ||
5090 | if Positional then | |
5091 | Sub_Agg := N; | |
5092 | ||
c8def50f | 5093 | for D in 1 .. Aggr_Dimension loop |
c8c37d2b | 5094 | Num := List_Length (Expressions (Sub_Agg)); |
70482933 | 5095 | |
deeb1604 | 5096 | Append_To (Indexes, |
70482933 | 5097 | Make_Range (Loc, |
c8def50f | 5098 | Low_Bound => Make_Integer_Literal (Loc, Uint_1), |
191fcb3a | 5099 | High_Bound => Make_Integer_Literal (Loc, Num))); |
c8c37d2b PT |
5100 | |
5101 | Sub_Agg := First (Expressions (Sub_Agg)); | |
70482933 RK |
5102 | end loop; |
5103 | ||
5104 | else | |
3b9fa2df ES |
5105 | -- We know the aggregate type is unconstrained and the aggregate |
5106 | -- is not processable by the back end, therefore not necessarily | |
5107 | -- positional. Retrieve each dimension bounds (computed earlier). | |
70482933 | 5108 | |
c8def50f | 5109 | for D in 1 .. Aggr_Dimension loop |
37368818 | 5110 | Append_To (Indexes, |
70482933 | 5111 | Make_Range (Loc, |
37368818 RD |
5112 | Low_Bound => Aggr_Low (D), |
5113 | High_Bound => Aggr_High (D))); | |
70482933 RK |
5114 | end loop; |
5115 | end if; | |
5116 | ||
5117 | Decl := | |
5118 | Make_Full_Type_Declaration (Loc, | |
5119 | Defining_Identifier => Agg_Type, | |
bdc193ba | 5120 | Type_Definition => |
70482933 | 5121 | Make_Constrained_Array_Definition (Loc, |
deeb1604 AC |
5122 | Discrete_Subtype_Definitions => Indexes, |
5123 | Component_Definition => | |
a397db96 | 5124 | Make_Component_Definition (Loc, |
a397db96 AC |
5125 | Subtype_Indication => |
5126 | New_Occurrence_Of (Component_Type (Typ), Loc)))); | |
70482933 RK |
5127 | |
5128 | Insert_Action (N, Decl); | |
5129 | Analyze (Decl); | |
5130 | Set_Etype (N, Agg_Type); | |
5131 | Set_Is_Itype (Agg_Type); | |
5132 | Freeze_Itype (Agg_Type, N); | |
5133 | end Build_Constrained_Type; | |
5134 | ||
5135 | ------------------ | |
5136 | -- Check_Bounds -- | |
5137 | ------------------ | |
5138 | ||
7c4f3267 BD |
5139 | procedure Check_Bounds (Aggr_Bounds_Node, Index_Bounds_Node : Node_Id) is |
5140 | Aggr_Bounds : constant Range_Nodes := | |
5141 | Get_Index_Bounds (Aggr_Bounds_Node); | |
5142 | Ind_Bounds : constant Range_Nodes := | |
5143 | Get_Index_Bounds (Index_Bounds_Node); | |
70482933 | 5144 | |
c8def50f | 5145 | Cond : Node_Id; |
70482933 RK |
5146 | |
5147 | begin | |
3424f4c3 PT |
5148 | -- For a null array aggregate check that high bound (i.e., low |
5149 | -- bound predecessor) exists. Fail if low bound is low bound of | |
5150 | -- base subtype (in all cases, including modular). | |
5151 | ||
5152 | if Is_Null_Aggregate (N) then | |
5153 | Insert_Action (N, | |
5154 | Make_Raise_Constraint_Error (Loc, | |
5155 | Condition => | |
5156 | Make_Op_Eq (Loc, | |
5157 | New_Copy_Tree (Aggr_Bounds.First), | |
5158 | New_Copy_Tree | |
5159 | (Type_Low_Bound (Base_Type (Etype (Ind_Bounds.First))))), | |
5160 | Reason => CE_Range_Check_Failed)); | |
5161 | return; | |
5162 | end if; | |
5163 | ||
70482933 | 5164 | -- Generate the following test: |
bdc193ba | 5165 | |
70482933 | 5166 | -- [constraint_error when |
7c4f3267 BD |
5167 | -- Aggr_Bounds.First <= Aggr_Bounds.Last and then |
5168 | -- (Aggr_Bounds.First < Ind_Bounds.First | |
5169 | -- or else Aggr_Bounds.Last > Ind_Bounds.Last)] | |
3b9fa2df | 5170 | |
641d3093 | 5171 | -- As an optimization try to see if some tests are trivially vacuous |
70482933 RK |
5172 | -- because we are comparing an expression against itself. |
5173 | ||
7c4f3267 BD |
5174 | if Aggr_Bounds.First = Ind_Bounds.First |
5175 | and then Aggr_Bounds.Last = Ind_Bounds.Last | |
5176 | then | |
70482933 RK |
5177 | Cond := Empty; |
5178 | ||
7c4f3267 | 5179 | elsif Aggr_Bounds.Last = Ind_Bounds.Last then |
70482933 RK |
5180 | Cond := |
5181 | Make_Op_Lt (Loc, | |
7c4f3267 BD |
5182 | Left_Opnd => |
5183 | Duplicate_Subexpr_Move_Checks (Aggr_Bounds.First), | |
5184 | Right_Opnd => | |
5185 | Duplicate_Subexpr_Move_Checks (Ind_Bounds.First)); | |
70482933 | 5186 | |
7c4f3267 | 5187 | elsif Aggr_Bounds.First = Ind_Bounds.First then |
70482933 RK |
5188 | Cond := |
5189 | Make_Op_Gt (Loc, | |
7c4f3267 BD |
5190 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Bounds.Last), |
5191 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Bounds.Last)); | |
70482933 RK |
5192 | |
5193 | else | |
5194 | Cond := | |
5195 | Make_Or_Else (Loc, | |
5196 | Left_Opnd => | |
5197 | Make_Op_Lt (Loc, | |
7c4f3267 BD |
5198 | Left_Opnd => |
5199 | Duplicate_Subexpr_Move_Checks (Aggr_Bounds.First), | |
5200 | Right_Opnd => | |
5201 | Duplicate_Subexpr_Move_Checks (Ind_Bounds.First)), | |
70482933 RK |
5202 | |
5203 | Right_Opnd => | |
5204 | Make_Op_Gt (Loc, | |
7c4f3267 BD |
5205 | Left_Opnd => Duplicate_Subexpr (Aggr_Bounds.Last), |
5206 | Right_Opnd => Duplicate_Subexpr (Ind_Bounds.Last))); | |
70482933 RK |
5207 | end if; |
5208 | ||
5209 | if Present (Cond) then | |
5210 | Cond := | |
5211 | Make_And_Then (Loc, | |
5212 | Left_Opnd => | |
5213 | Make_Op_Le (Loc, | |
7c4f3267 BD |
5214 | Left_Opnd => |
5215 | Duplicate_Subexpr_Move_Checks (Aggr_Bounds.First), | |
5216 | Right_Opnd => | |
5217 | Duplicate_Subexpr_Move_Checks (Aggr_Bounds.Last)), | |
70482933 RK |
5218 | |
5219 | Right_Opnd => Cond); | |
5220 | ||
5221 | Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False); | |
5222 | Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False); | |
5223 | Insert_Action (N, | |
07fc65c4 GB |
5224 | Make_Raise_Constraint_Error (Loc, |
5225 | Condition => Cond, | |
8fdafe44 | 5226 | Reason => CE_Range_Check_Failed)); |
70482933 RK |
5227 | end if; |
5228 | end Check_Bounds; | |
5229 | ||
5230 | ---------------------------- | |
5231 | -- Check_Same_Aggr_Bounds -- | |
5232 | ---------------------------- | |
5233 | ||
5234 | procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is | |
8ba0b4cf PT |
5235 | Sub_Bounds : constant Range_Nodes := |
5236 | Get_Index_Bounds (Aggregate_Bounds (Sub_Aggr)); | |
10c257af ES |
5237 | Sub_Lo : Node_Id renames Sub_Bounds.First; |
5238 | Sub_Hi : Node_Id renames Sub_Bounds.Last; | |
d74716b3 | 5239 | -- The bounds of this specific subaggregate |
70482933 RK |
5240 | |
5241 | Aggr_Lo : constant Node_Id := Aggr_Low (Dim); | |
5242 | Aggr_Hi : constant Node_Id := Aggr_High (Dim); | |
5243 | -- The bounds of the aggregate for this dimension | |
5244 | ||
5245 | Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim); | |
3cf3e5c6 | 5246 | -- The index type for this dimension.xxx |
70482933 | 5247 | |
8ba0b4cf | 5248 | Cond : Node_Id; |
fbf5a39b AC |
5249 | Assoc : Node_Id; |
5250 | Expr : Node_Id; | |
70482933 RK |
5251 | |
5252 | begin | |
5253 | -- If index checks are on generate the test | |
3b9fa2df | 5254 | |
70482933 RK |
5255 | -- [constraint_error when |
5256 | -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi] | |
3b9fa2df | 5257 | |
70482933 RK |
5258 | -- As an optimization try to see if some tests are trivially vacuos |
5259 | -- because we are comparing an expression against itself. Also for | |
5260 | -- the first dimension the test is trivially vacuous because there | |
5261 | -- is just one aggregate for dimension 1. | |
5262 | ||
5263 | if Index_Checks_Suppressed (Ind_Typ) then | |
5264 | Cond := Empty; | |
5265 | ||
bdc193ba | 5266 | elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi) |
70482933 RK |
5267 | then |
5268 | Cond := Empty; | |
5269 | ||
5270 | elsif Aggr_Hi = Sub_Hi then | |
5271 | Cond := | |
5272 | Make_Op_Ne (Loc, | |
fbf5a39b AC |
5273 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), |
5274 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)); | |
70482933 RK |
5275 | |
5276 | elsif Aggr_Lo = Sub_Lo then | |
5277 | Cond := | |
5278 | Make_Op_Ne (Loc, | |
fbf5a39b AC |
5279 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi), |
5280 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi)); | |
70482933 RK |
5281 | |
5282 | else | |
5283 | Cond := | |
5284 | Make_Or_Else (Loc, | |
5285 | Left_Opnd => | |
5286 | Make_Op_Ne (Loc, | |
fbf5a39b AC |
5287 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), |
5288 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)), | |
70482933 RK |
5289 | |
5290 | Right_Opnd => | |
5291 | Make_Op_Ne (Loc, | |
5292 | Left_Opnd => Duplicate_Subexpr (Aggr_Hi), | |
5293 | Right_Opnd => Duplicate_Subexpr (Sub_Hi))); | |
5294 | end if; | |
5295 | ||
5296 | if Present (Cond) then | |
5297 | Insert_Action (N, | |
07fc65c4 GB |
5298 | Make_Raise_Constraint_Error (Loc, |
5299 | Condition => Cond, | |
5300 | Reason => CE_Length_Check_Failed)); | |
70482933 RK |
5301 | end if; |
5302 | ||
d74716b3 | 5303 | -- Now look inside the subaggregate to see if there is more work |
70482933 RK |
5304 | |
5305 | if Dim < Aggr_Dimension then | |
5306 | ||
5307 | -- Process positional components | |
5308 | ||
5309 | if Present (Expressions (Sub_Aggr)) then | |
5310 | Expr := First (Expressions (Sub_Aggr)); | |
5311 | while Present (Expr) loop | |
5312 | Check_Same_Aggr_Bounds (Expr, Dim + 1); | |
5313 | Next (Expr); | |
5314 | end loop; | |
5315 | end if; | |
5316 | ||
5317 | -- Process component associations | |
5318 | ||
5319 | if Present (Component_Associations (Sub_Aggr)) then | |
5320 | Assoc := First (Component_Associations (Sub_Aggr)); | |
5321 | while Present (Assoc) loop | |
5322 | Expr := Expression (Assoc); | |
5323 | Check_Same_Aggr_Bounds (Expr, Dim + 1); | |
5324 | Next (Assoc); | |
5325 | end loop; | |
5326 | end if; | |
5327 | end if; | |
5328 | end Check_Same_Aggr_Bounds; | |
5329 | ||
5330 | ---------------------------- | |
5331 | -- Compute_Others_Present -- | |
5332 | ---------------------------- | |
5333 | ||
5334 | procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is | |
fbf5a39b AC |
5335 | Assoc : Node_Id; |
5336 | Expr : Node_Id; | |
70482933 RK |
5337 | |
5338 | begin | |
5339 | if Present (Component_Associations (Sub_Aggr)) then | |
5340 | Assoc := Last (Component_Associations (Sub_Aggr)); | |
07fc65c4 | 5341 | |
10c257af ES |
5342 | if Present (Assoc) |
5343 | and then Nkind (First (Choice_List (Assoc))) = N_Others_Choice | |
5344 | then | |
70482933 | 5345 | Others_Present (Dim) := True; |
e84d25c9 ES |
5346 | |
5347 | -- An others_clause may be superfluous if previous components | |
5348 | -- cover the full given range of a constrained array. In such | |
5349 | -- a case an others_clause does not contribute any additional | |
5350 | -- components and has not been analyzed. We analyze it now to | |
5351 | -- detect type errors in the expression, even though no code | |
5352 | -- will be generated for it. | |
5353 | ||
5354 | if Dim = Aggr_Dimension | |
5355 | and then Nkind (Assoc) /= N_Iterated_Component_Association | |
5356 | and then not Analyzed (Expression (Assoc)) | |
5357 | and then not Box_Present (Assoc) | |
5358 | then | |
5359 | Preanalyze_And_Resolve (Expression (Assoc), Ctyp); | |
5360 | end if; | |
70482933 RK |
5361 | end if; |
5362 | end if; | |
5363 | ||
d74716b3 | 5364 | -- Now look inside the subaggregate to see if there is more work |
70482933 RK |
5365 | |
5366 | if Dim < Aggr_Dimension then | |
5367 | ||
5368 | -- Process positional components | |
5369 | ||
5370 | if Present (Expressions (Sub_Aggr)) then | |
5371 | Expr := First (Expressions (Sub_Aggr)); | |
5372 | while Present (Expr) loop | |
5373 | Compute_Others_Present (Expr, Dim + 1); | |
5374 | Next (Expr); | |
5375 | end loop; | |
5376 | end if; | |
5377 | ||
5378 | -- Process component associations | |
5379 | ||
5380 | if Present (Component_Associations (Sub_Aggr)) then | |
5381 | Assoc := First (Component_Associations (Sub_Aggr)); | |
5382 | while Present (Assoc) loop | |
5383 | Expr := Expression (Assoc); | |
5384 | Compute_Others_Present (Expr, Dim + 1); | |
5385 | Next (Assoc); | |
5386 | end loop; | |
5387 | end if; | |
5388 | end if; | |
5389 | end Compute_Others_Present; | |
5390 | ||
70482933 RK |
5391 | ------------------ |
5392 | -- Others_Check -- | |
5393 | ------------------ | |
5394 | ||
5395 | procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is | |
5396 | Aggr_Lo : constant Node_Id := Aggr_Low (Dim); | |
5397 | Aggr_Hi : constant Node_Id := Aggr_High (Dim); | |
3cf3e5c6 | 5398 | -- The bounds of the aggregate for this dimension |
70482933 RK |
5399 | |
5400 | Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim); | |
3cf3e5c6 | 5401 | -- The index type for this dimension |
70482933 RK |
5402 | |
5403 | Need_To_Check : Boolean := False; | |
5404 | ||
5405 | Choices_Lo : Node_Id := Empty; | |
5406 | Choices_Hi : Node_Id := Empty; | |
d74716b3 | 5407 | -- The lowest and highest discrete choices for a named subaggregate |
70482933 RK |
5408 | |
5409 | Nb_Choices : Int := -1; | |
d74716b3 | 5410 | -- The number of discrete non-others choices in this subaggregate |
70482933 RK |
5411 | |
5412 | Nb_Elements : Uint := Uint_0; | |
5413 | -- The number of elements in a positional aggregate | |
5414 | ||
5415 | Cond : Node_Id := Empty; | |
5416 | ||
5417 | Assoc : Node_Id; | |
5418 | Choice : Node_Id; | |
5419 | Expr : Node_Id; | |
5420 | ||
5421 | begin | |
5422 | -- Check if we have an others choice. If we do make sure that this | |
d74716b3 | 5423 | -- subaggregate contains at least one element in addition to the |
70482933 RK |
5424 | -- others choice. |
5425 | ||
5426 | if Range_Checks_Suppressed (Ind_Typ) then | |
5427 | Need_To_Check := False; | |
5428 | ||
5429 | elsif Present (Expressions (Sub_Aggr)) | |
5430 | and then Present (Component_Associations (Sub_Aggr)) | |
5431 | then | |
10c257af ES |
5432 | Need_To_Check := |
5433 | not (Is_Empty_List (Expressions (Sub_Aggr)) | |
5434 | and then Is_Empty_List | |
5435 | (Component_Associations (Sub_Aggr))); | |
70482933 RK |
5436 | |
5437 | elsif Present (Component_Associations (Sub_Aggr)) then | |
5438 | Assoc := Last (Component_Associations (Sub_Aggr)); | |
5439 | ||
00f45f30 | 5440 | if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then |
70482933 RK |
5441 | Need_To_Check := False; |
5442 | ||
5443 | else | |
3b9fa2df ES |
5444 | -- Count the number of discrete choices. Start with -1 because |
5445 | -- the others choice does not count. | |
70482933 | 5446 | |
bdc193ba AC |
5447 | -- Is there some reason we do not use List_Length here ??? |
5448 | ||
70482933 RK |
5449 | Nb_Choices := -1; |
5450 | Assoc := First (Component_Associations (Sub_Aggr)); | |
5451 | while Present (Assoc) loop | |
00f45f30 | 5452 | Choice := First (Choice_List (Assoc)); |
70482933 RK |
5453 | while Present (Choice) loop |
5454 | Nb_Choices := Nb_Choices + 1; | |
5455 | Next (Choice); | |
5456 | end loop; | |
5457 | ||
5458 | Next (Assoc); | |
5459 | end loop; | |
5460 | ||
5461 | -- If there is only an others choice nothing to do | |
5462 | ||
5463 | Need_To_Check := (Nb_Choices > 0); | |
5464 | end if; | |
5465 | ||
5466 | else | |
5467 | Need_To_Check := False; | |
5468 | end if; | |
5469 | ||
d74716b3 | 5470 | -- If we are dealing with a positional subaggregate with an others |
3b9fa2df | 5471 | -- choice then compute the number or positional elements. |
70482933 RK |
5472 | |
5473 | if Need_To_Check and then Present (Expressions (Sub_Aggr)) then | |
5474 | Expr := First (Expressions (Sub_Aggr)); | |
5475 | Nb_Elements := Uint_0; | |
5476 | while Present (Expr) loop | |
5477 | Nb_Elements := Nb_Elements + 1; | |
5478 | Next (Expr); | |
5479 | end loop; | |
5480 | ||
5481 | -- If the aggregate contains discrete choices and an others choice | |
5482 | -- compute the smallest and largest discrete choice values. | |
5483 | ||
5484 | elsif Need_To_Check then | |
5485 | Compute_Choices_Lo_And_Choices_Hi : declare | |
07fc65c4 | 5486 | |
70482933 RK |
5487 | Table : Case_Table_Type (1 .. Nb_Choices); |
5488 | -- Used to sort all the different choice values | |
5489 | ||
07fc65c4 | 5490 | J : Pos := 1; |
70482933 RK |
5491 | |
5492 | begin | |
5493 | Assoc := First (Component_Associations (Sub_Aggr)); | |
5494 | while Present (Assoc) loop | |
00f45f30 | 5495 | Choice := First (Choice_List (Assoc)); |
70482933 RK |
5496 | while Present (Choice) loop |
5497 | if Nkind (Choice) = N_Others_Choice then | |
5498 | exit; | |
5499 | end if; | |
5500 | ||
7c4f3267 BD |
5501 | declare |
5502 | Bounds : constant Range_Nodes := | |
5503 | Get_Index_Bounds (Choice); | |
5504 | begin | |
5505 | Table (J).Choice_Lo := Bounds.First; | |
5506 | Table (J).Choice_Hi := Bounds.Last; | |
5507 | end; | |
70482933 | 5508 | |
07fc65c4 | 5509 | J := J + 1; |
70482933 RK |
5510 | Next (Choice); |
5511 | end loop; | |
5512 | ||
5513 | Next (Assoc); | |
5514 | end loop; | |
5515 | ||
5516 | -- Sort the discrete choices | |
5517 | ||
5518 | Sort_Case_Table (Table); | |
5519 | ||
5520 | Choices_Lo := Table (1).Choice_Lo; | |
5521 | Choices_Hi := Table (Nb_Choices).Choice_Hi; | |
5522 | end Compute_Choices_Lo_And_Choices_Hi; | |
5523 | end if; | |
5524 | ||
d74716b3 | 5525 | -- If no others choice in this subaggregate, or the aggregate |
70482933 RK |
5526 | -- comprises only an others choice, nothing to do. |
5527 | ||
5528 | if not Need_To_Check then | |
5529 | Cond := Empty; | |
5530 | ||
3b9fa2df ES |
5531 | -- If we are dealing with an aggregate containing an others choice |
5532 | -- and positional components, we generate the following test: | |
5533 | ||
70482933 RK |
5534 | -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) > |
5535 | -- Ind_Typ'Pos (Aggr_Hi) | |
5536 | -- then | |
5537 | -- raise Constraint_Error; | |
5538 | -- end if; | |
5539 | ||
6a987d78 EB |
5540 | -- in the general case, but the following simpler test: |
5541 | ||
5542 | -- [constraint_error when | |
5543 | -- Aggr_Lo + (Nb_Elements - 1) > Aggr_Hi]; | |
5544 | ||
5545 | -- instead if the index type is a signed integer. | |
5546 | ||
70482933 | 5547 | elsif Nb_Elements > Uint_0 then |
6a987d78 EB |
5548 | if Nb_Elements = Uint_1 then |
5549 | Cond := | |
5550 | Make_Op_Gt (Loc, | |
5551 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), | |
5552 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)); | |
5553 | ||
5554 | elsif Is_Signed_Integer_Type (Ind_Typ) then | |
5555 | Cond := | |
5556 | Make_Op_Gt (Loc, | |
5557 | Left_Opnd => | |
5558 | Make_Op_Add (Loc, | |
5559 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), | |
5560 | Right_Opnd => | |
5561 | Make_Integer_Literal (Loc, Nb_Elements - 1)), | |
5562 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)); | |
70482933 | 5563 | |
6a987d78 EB |
5564 | else |
5565 | Cond := | |
5566 | Make_Op_Gt (Loc, | |
5567 | Left_Opnd => | |
5568 | Make_Op_Add (Loc, | |
5569 | Left_Opnd => | |
5570 | Make_Attribute_Reference (Loc, | |
5571 | Prefix => New_Occurrence_Of (Ind_Typ, Loc), | |
5572 | Attribute_Name => Name_Pos, | |
5573 | Expressions => | |
5574 | New_List | |
5575 | (Duplicate_Subexpr_Move_Checks (Aggr_Lo))), | |
5576 | Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)), | |
5577 | ||
5578 | Right_Opnd => | |
5579 | Make_Attribute_Reference (Loc, | |
5580 | Prefix => New_Occurrence_Of (Ind_Typ, Loc), | |
5581 | Attribute_Name => Name_Pos, | |
5582 | Expressions => New_List ( | |
5583 | Duplicate_Subexpr_Move_Checks (Aggr_Hi)))); | |
5584 | end if; | |
70482933 | 5585 | |
3b9fa2df ES |
5586 | -- If we are dealing with an aggregate containing an others choice |
5587 | -- and discrete choices we generate the following test: | |
5588 | ||
70482933 RK |
5589 | -- [constraint_error when |
5590 | -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi]; | |
5591 | ||
5592 | else | |
5593 | Cond := | |
5594 | Make_Or_Else (Loc, | |
5595 | Left_Opnd => | |
5596 | Make_Op_Lt (Loc, | |
bdc193ba AC |
5597 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo), |
5598 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)), | |
70482933 RK |
5599 | |
5600 | Right_Opnd => | |
5601 | Make_Op_Gt (Loc, | |
bdc193ba AC |
5602 | Left_Opnd => Duplicate_Subexpr (Choices_Hi), |
5603 | Right_Opnd => Duplicate_Subexpr (Aggr_Hi))); | |
70482933 RK |
5604 | end if; |
5605 | ||
5606 | if Present (Cond) then | |
5607 | Insert_Action (N, | |
07fc65c4 GB |
5608 | Make_Raise_Constraint_Error (Loc, |
5609 | Condition => Cond, | |
5610 | Reason => CE_Length_Check_Failed)); | |
641d3093 TQ |
5611 | -- Questionable reason code, shouldn't that be a |
5612 | -- CE_Range_Check_Failed ??? | |
70482933 RK |
5613 | end if; |
5614 | ||
d74716b3 | 5615 | -- Now look inside the subaggregate to see if there is more work |
70482933 RK |
5616 | |
5617 | if Dim < Aggr_Dimension then | |
5618 | ||
5619 | -- Process positional components | |
5620 | ||
5621 | if Present (Expressions (Sub_Aggr)) then | |
5622 | Expr := First (Expressions (Sub_Aggr)); | |
5623 | while Present (Expr) loop | |
5624 | Others_Check (Expr, Dim + 1); | |
5625 | Next (Expr); | |
5626 | end loop; | |
5627 | end if; | |
5628 | ||
5629 | -- Process component associations | |
5630 | ||
5631 | if Present (Component_Associations (Sub_Aggr)) then | |
5632 | Assoc := First (Component_Associations (Sub_Aggr)); | |
5633 | while Present (Assoc) loop | |
5634 | Expr := Expression (Assoc); | |
5635 | Others_Check (Expr, Dim + 1); | |
5636 | Next (Assoc); | |
5637 | end loop; | |
5638 | end if; | |
5639 | end if; | |
5640 | end Others_Check; | |
5641 | ||
8da337c5 AC |
5642 | ------------------------- |
5643 | -- Safe_Left_Hand_Side -- | |
5644 | ------------------------- | |
5645 | ||
5646 | function Safe_Left_Hand_Side (N : Node_Id) return Boolean is | |
deeb1604 AC |
5647 | function Is_Safe_Index (Indx : Node_Id) return Boolean; |
5648 | -- If the left-hand side includes an indexed component, check that | |
d74716b3 | 5649 | -- the indexes are free of side effects. |
deeb1604 AC |
5650 | |
5651 | ------------------- | |
5652 | -- Is_Safe_Index -- | |
5653 | ------------------- | |
5654 | ||
5655 | function Is_Safe_Index (Indx : Node_Id) return Boolean is | |
5656 | begin | |
5657 | if Is_Entity_Name (Indx) then | |
5658 | return True; | |
5659 | ||
5660 | elsif Nkind (Indx) = N_Integer_Literal then | |
5661 | return True; | |
5662 | ||
5663 | elsif Nkind (Indx) = N_Function_Call | |
5664 | and then Is_Entity_Name (Name (Indx)) | |
36a66365 | 5665 | and then Has_Pragma_Pure_Function (Entity (Name (Indx))) |
deeb1604 AC |
5666 | then |
5667 | return True; | |
5668 | ||
5669 | elsif Nkind (Indx) = N_Type_Conversion | |
5670 | and then Is_Safe_Index (Expression (Indx)) | |
5671 | then | |
5672 | return True; | |
5673 | ||
5674 | else | |
5675 | return False; | |
5676 | end if; | |
5677 | end Is_Safe_Index; | |
5678 | ||
5679 | -- Start of processing for Safe_Left_Hand_Side | |
5680 | ||
8da337c5 AC |
5681 | begin |
5682 | if Is_Entity_Name (N) then | |
5683 | return True; | |
5684 | ||
4a08c95c | 5685 | elsif Nkind (N) in N_Explicit_Dereference | N_Selected_Component |
8da337c5 AC |
5686 | and then Safe_Left_Hand_Side (Prefix (N)) |
5687 | then | |
5688 | return True; | |
5689 | ||
5690 | elsif Nkind (N) = N_Indexed_Component | |
5691 | and then Safe_Left_Hand_Side (Prefix (N)) | |
36a66365 | 5692 | and then Is_Safe_Index (First (Expressions (N))) |
8da337c5 AC |
5693 | then |
5694 | return True; | |
deeb1604 AC |
5695 | |
5696 | elsif Nkind (N) = N_Unchecked_Type_Conversion then | |
5697 | return Safe_Left_Hand_Side (Expression (N)); | |
5698 | ||
8da337c5 AC |
5699 | else |
5700 | return False; | |
5701 | end if; | |
5702 | end Safe_Left_Hand_Side; | |
5703 | ||
fe43084c ES |
5704 | ---------------------------------- |
5705 | -- Two_Pass_Aggregate_Expansion -- | |
5706 | ---------------------------------- | |
5707 | ||
5708 | procedure Two_Pass_Aggregate_Expansion (N : Node_Id) is | |
5709 | Loc : constant Source_Ptr := Sloc (N); | |
5710 | Comp_Type : constant Entity_Id := Etype (N); | |
5711 | Index_Id : constant Entity_Id := Make_Temporary (Loc, 'I', N); | |
5712 | Index_Type : constant Entity_Id := Etype (First_Index (Etype (N))); | |
5713 | Size_Id : constant Entity_Id := Make_Temporary (Loc, 'I', N); | |
5714 | TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N); | |
5715 | ||
5716 | Assoc : Node_Id := First (Component_Associations (N)); | |
5717 | Incr : Node_Id; | |
5718 | Iter : Node_Id; | |
5719 | New_Comp : Node_Id; | |
5720 | One_Loop : Node_Id; | |
5721 | ||
5722 | Size_Expr_Code : List_Id; | |
5723 | Insertion_Code : List_Id := New_List; | |
5724 | ||
5725 | begin | |
5726 | Size_Expr_Code := New_List ( | |
5727 | Make_Object_Declaration (Loc, | |
5728 | Defining_Identifier => Size_Id, | |
5729 | Object_Definition => New_Occurrence_Of (Standard_Integer, Loc), | |
5730 | Expression => Make_Integer_Literal (Loc, 0))); | |
5731 | ||
5732 | -- First pass: execute the iterators to count the number of elements | |
5733 | -- that will be generated. | |
5734 | ||
5735 | while Present (Assoc) loop | |
5736 | Iter := Iterator_Specification (Assoc); | |
5737 | Incr := Make_Assignment_Statement (Loc, | |
5738 | Name => New_Occurrence_Of (Size_Id, Loc), | |
5739 | Expression => | |
5740 | Make_Op_Add (Loc, | |
5741 | Left_Opnd => New_Occurrence_Of (Size_Id, Loc), | |
5742 | Right_Opnd => Make_Integer_Literal (Loc, 1))); | |
5743 | ||
f3561c06 | 5744 | One_Loop := Make_Implicit_Loop_Statement (N, |
fe43084c ES |
5745 | Iteration_Scheme => |
5746 | Make_Iteration_Scheme (Loc, | |
08613129 | 5747 | Iterator_Specification => New_Copy_Tree (Iter)), |
fe43084c ES |
5748 | Statements => New_List (Incr)); |
5749 | ||
5750 | Append (One_Loop, Size_Expr_Code); | |
5751 | Next (Assoc); | |
5752 | end loop; | |
5753 | ||
5754 | Insert_Actions (N, Size_Expr_Code); | |
5755 | ||
5756 | -- Build a constrained subtype with the calculated length | |
5757 | -- and declare the proper bounded aggregate object. | |
5758 | -- The index type is some discrete type, so the bounds of the | |
5759 | -- constructed array are computed as T'Val (T'Pos (ineger bound)); | |
5760 | ||
5761 | declare | |
5762 | Pos_Lo : constant Node_Id := | |
5763 | Make_Attribute_Reference (Loc, | |
5764 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5765 | Attribute_Name => Name_Pos, | |
5766 | Expressions => New_List ( | |
5767 | Make_Attribute_Reference (Loc, | |
5768 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5769 | Attribute_Name => Name_First))); | |
5770 | ||
5771 | Aggr_Lo : constant Node_Id := | |
5772 | Make_Attribute_Reference (Loc, | |
5773 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5774 | Attribute_Name => Name_Val, | |
5775 | Expressions => New_List (New_Copy_Tree (Pos_Lo))); | |
5776 | ||
5777 | -- Hi = Index_type'Pos (Lo + Size -1). | |
5778 | ||
5779 | Pos_Hi : constant Node_Id := | |
5780 | Make_Op_Add (Loc, | |
5781 | Left_Opnd => New_Copy_Tree (Pos_Lo), | |
5782 | Right_Opnd => | |
5783 | Make_Op_Subtract (Loc, | |
5784 | Left_Opnd => New_Occurrence_Of (Size_Id, Loc), | |
5785 | Right_Opnd => Make_Integer_Literal (Loc, 1))); | |
5786 | ||
5787 | -- Corresponding index value | |
5788 | ||
5789 | Aggr_Hi : constant Node_Id := | |
5790 | Make_Attribute_Reference (Loc, | |
5791 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5792 | Attribute_Name => Name_Val, | |
5793 | Expressions => New_List (New_Copy_Tree (Pos_Hi))); | |
5794 | ||
5795 | SubE : constant Entity_Id := Make_Temporary (Loc, 'T'); | |
5796 | SubD : constant Node_Id := | |
5797 | Make_Subtype_Declaration (Loc, | |
5798 | Defining_Identifier => SubE, | |
5799 | Subtype_Indication => | |
5800 | Make_Subtype_Indication (Loc, | |
5801 | Subtype_Mark => | |
5802 | New_Occurrence_Of (Etype (Comp_Type), Loc), | |
5803 | Constraint => | |
5804 | Make_Index_Or_Discriminant_Constraint | |
5805 | (Loc, | |
5806 | Constraints => | |
5807 | New_List (Make_Range (Loc, Aggr_Lo, Aggr_Hi))))); | |
5808 | ||
5809 | -- Create a temporary array of the above subtype which | |
5810 | -- will be used to capture the aggregate assignments. | |
5811 | ||
5812 | TmpD : constant Node_Id := | |
5813 | Make_Object_Declaration (Loc, | |
5814 | Defining_Identifier => TmpE, | |
5815 | Object_Definition => New_Occurrence_Of (SubE, Loc)); | |
5816 | begin | |
5817 | Insert_Actions (N, New_List (SubD, TmpD)); | |
5818 | end; | |
5819 | ||
5820 | -- Second pass: use the iterators to generate the elements of the | |
5821 | -- aggregate. Insertion index starts at Index_Type'First. We | |
5822 | -- assume that the second evaluation of each iterator generates | |
5823 | -- the same number of elements as the first pass, and consider | |
5824 | -- that the execution is erroneous (even if the RM does not state | |
5825 | -- this explicitly) if the number of elements generated differs | |
5826 | -- between first and second pass. | |
5827 | ||
5828 | Assoc := First (Component_Associations (N)); | |
5829 | ||
5830 | -- Initialize insertion position to first array component. | |
5831 | ||
5832 | Insertion_Code := New_List ( | |
5833 | Make_Object_Declaration (Loc, | |
5834 | Defining_Identifier => Index_Id, | |
5835 | Object_Definition => | |
5836 | New_Occurrence_Of (Index_Type, Loc), | |
5837 | Expression => | |
5838 | Make_Attribute_Reference (Loc, | |
5839 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5840 | Attribute_Name => Name_First))); | |
5841 | ||
5842 | while Present (Assoc) loop | |
5843 | Iter := Iterator_Specification (Assoc); | |
5844 | New_Comp := Make_Assignment_Statement (Loc, | |
5845 | Name => | |
5846 | Make_Indexed_Component (Loc, | |
5847 | Prefix => New_Occurrence_Of (TmpE, Loc), | |
5848 | Expressions => | |
5849 | New_List (New_Occurrence_Of (Index_Id, Loc))), | |
d983abeb | 5850 | Expression => Copy_Separate_Tree (Expression (Assoc))); |
fe43084c ES |
5851 | |
5852 | -- Advance index position for insertion. | |
5853 | ||
5854 | Incr := Make_Assignment_Statement (Loc, | |
5855 | Name => New_Occurrence_Of (Index_Id, Loc), | |
5856 | Expression => | |
5857 | Make_Attribute_Reference (Loc, | |
5858 | Prefix => | |
5859 | New_Occurrence_Of (Index_Type, Loc), | |
5860 | Attribute_Name => Name_Succ, | |
5861 | Expressions => | |
5862 | New_List (New_Occurrence_Of (Index_Id, Loc)))); | |
5863 | ||
8b1a5da3 ES |
5864 | -- Add guard to skip last increment when upper bound is reached. |
5865 | ||
5866 | Incr := Make_If_Statement (Loc, | |
5867 | Condition => | |
5868 | Make_Op_Ne (Loc, | |
5869 | Left_Opnd => New_Occurrence_Of (Index_Id, Loc), | |
5870 | Right_Opnd => | |
5871 | Make_Attribute_Reference (Loc, | |
5872 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5873 | Attribute_Name => Name_Last)), | |
5874 | Then_Statements => New_List (Incr)); | |
5875 | ||
f3561c06 | 5876 | One_Loop := Make_Implicit_Loop_Statement (N, |
fe43084c ES |
5877 | Iteration_Scheme => |
5878 | Make_Iteration_Scheme (Loc, | |
08613129 | 5879 | Iterator_Specification => Copy_Separate_Tree (Iter)), |
fe43084c ES |
5880 | Statements => New_List (New_Comp, Incr)); |
5881 | ||
5882 | Append (One_Loop, Insertion_Code); | |
5883 | Next (Assoc); | |
5884 | end loop; | |
5885 | ||
5886 | Insert_Actions (N, Insertion_Code); | |
5887 | ||
5888 | -- Depending on context this may not work for build-in-place | |
5889 | -- arrays ??? | |
5890 | ||
5891 | Rewrite (N, New_Occurrence_Of (TmpE, Loc)); | |
5892 | ||
5893 | end Two_Pass_Aggregate_Expansion; | |
5894 | ||
8da337c5 | 5895 | -- Local variables |
70482933 RK |
5896 | |
5897 | Tmp : Entity_Id; | |
fbf5a39b | 5898 | -- Holds the temporary aggregate value |
70482933 RK |
5899 | |
5900 | Tmp_Decl : Node_Id; | |
fbf5a39b | 5901 | -- Holds the declaration of Tmp |
70482933 RK |
5902 | |
5903 | Aggr_Code : List_Id; | |
5904 | Parent_Node : Node_Id; | |
5905 | Parent_Kind : Node_Kind; | |
5906 | ||
5907 | -- Start of processing for Expand_Array_Aggregate | |
5908 | ||
5909 | begin | |
5910 | -- Do not touch the special aggregates of attributes used for Asm calls | |
5911 | ||
5912 | if Is_RTE (Ctyp, RE_Asm_Input_Operand) | |
5913 | or else Is_RTE (Ctyp, RE_Asm_Output_Operand) | |
5914 | then | |
5915 | return; | |
4a1bfefb | 5916 | |
fe43084c | 5917 | elsif Present (Component_Associations (N)) |
8b1a5da3 ES |
5918 | and then Nkind (First (Component_Associations (N))) = |
5919 | N_Iterated_Component_Association | |
5920 | and then | |
5921 | Present (Iterator_Specification (First (Component_Associations (N)))) | |
fe43084c ES |
5922 | then |
5923 | Two_Pass_Aggregate_Expansion (N); | |
5924 | return; | |
5925 | ||
f5655e4a AC |
5926 | -- Do not attempt expansion if error already detected. We may reach this |
5927 | -- point in spite of previous errors when compiling with -gnatq, to | |
5928 | -- force all possible errors (this is the usual ACATS mode). | |
5929 | ||
5930 | elsif Error_Posted (N) then | |
5931 | return; | |
70482933 RK |
5932 | end if; |
5933 | ||
07fc65c4 | 5934 | -- If the semantic analyzer has determined that aggregate N will raise |
e7c0dd39 | 5935 | -- Constraint_Error at run time, then the aggregate node has been |
07fc65c4 GB |
5936 | -- replaced with an N_Raise_Constraint_Error node and we should |
5937 | -- never get here. | |
70482933 RK |
5938 | |
5939 | pragma Assert (not Raises_Constraint_Error (N)); | |
5940 | ||
3cf3e5c6 | 5941 | -- STEP 1a |
fbf5a39b AC |
5942 | |
5943 | -- Check that the index range defined by aggregate bounds is | |
5944 | -- compatible with corresponding index subtype. | |
70482933 RK |
5945 | |
5946 | Index_Compatibility_Check : declare | |
5947 | Aggr_Index_Range : Node_Id := First_Index (Typ); | |
5948 | -- The current aggregate index range | |
5949 | ||
5950 | Index_Constraint : Node_Id := First_Index (Etype (Typ)); | |
5951 | -- The corresponding index constraint against which we have to | |
5952 | -- check the above aggregate index range. | |
5953 | ||
5954 | begin | |
5955 | Compute_Others_Present (N, 1); | |
5956 | ||
5957 | for J in 1 .. Aggr_Dimension loop | |
bdc193ba AC |
5958 | -- There is no need to emit a check if an others choice is present |
5959 | -- for this array aggregate dimension since in this case one of | |
d74716b3 | 5960 | -- N's subaggregates has taken its bounds from the context and |
bdc193ba | 5961 | -- these bounds must have been checked already. In addition all |
d74716b3 AC |
5962 | -- subaggregates corresponding to the same dimension must all have |
5963 | -- the same bounds (checked in (c) below). | |
70482933 RK |
5964 | |
5965 | if not Range_Checks_Suppressed (Etype (Index_Constraint)) | |
5966 | and then not Others_Present (J) | |
5967 | then | |
3b9fa2df ES |
5968 | -- We don't use Checks.Apply_Range_Check here because it emits |
5969 | -- a spurious check. Namely it checks that the range defined by | |
d74716b3 | 5970 | -- the aggregate bounds is nonempty. But we know this already |
3b9fa2df | 5971 | -- if we get here. |
70482933 RK |
5972 | |
5973 | Check_Bounds (Aggr_Index_Range, Index_Constraint); | |
5974 | end if; | |
5975 | ||
3b9fa2df ES |
5976 | -- Save the low and high bounds of the aggregate index as well as |
5977 | -- the index type for later use in checks (b) and (c) below. | |
70482933 | 5978 | |
10c257af ES |
5979 | Get_Index_Bounds |
5980 | (Aggr_Index_Range, L => Aggr_Low (J), H => Aggr_High (J)); | |
70482933 RK |
5981 | |
5982 | Aggr_Index_Typ (J) := Etype (Index_Constraint); | |
5983 | ||
5984 | Next_Index (Aggr_Index_Range); | |
5985 | Next_Index (Index_Constraint); | |
5986 | end loop; | |
5987 | end Index_Compatibility_Check; | |
5988 | ||
3cf3e5c6 | 5989 | -- STEP 1b |
fbf5a39b | 5990 | |
3b9fa2df ES |
5991 | -- If an others choice is present check that no aggregate index is |
5992 | -- outside the bounds of the index constraint. | |
70482933 RK |
5993 | |
5994 | Others_Check (N, 1); | |
5995 | ||
3cf3e5c6 | 5996 | -- STEP 1c |
fbf5a39b AC |
5997 | |
5998 | -- For multidimensional arrays make sure that all subaggregates | |
5999 | -- corresponding to the same dimension have the same bounds. | |
70482933 RK |
6000 | |
6001 | if Aggr_Dimension > 1 then | |
6002 | Check_Same_Aggr_Bounds (N, 1); | |
6003 | end if; | |
6004 | ||
688a9b51 RD |
6005 | -- STEP 1d |
6006 | ||
6007 | -- If we have a default component value, or simple initialization is | |
6008 | -- required for the component type, then we replace <> in component | |
6009 | -- associations by the required default value. | |
6010 | ||
6011 | declare | |
6012 | Default_Val : Node_Id; | |
6013 | Assoc : Node_Id; | |
6014 | ||
6015 | begin | |
6016 | if (Present (Default_Aspect_Component_Value (Typ)) | |
6017 | or else Needs_Simple_Initialization (Ctyp)) | |
6018 | and then Present (Component_Associations (N)) | |
6019 | then | |
6020 | Assoc := First (Component_Associations (N)); | |
6021 | while Present (Assoc) loop | |
6022 | if Nkind (Assoc) = N_Component_Association | |
6023 | and then Box_Present (Assoc) | |
6024 | then | |
6025 | Set_Box_Present (Assoc, False); | |
6026 | ||
6027 | if Present (Default_Aspect_Component_Value (Typ)) then | |
6028 | Default_Val := Default_Aspect_Component_Value (Typ); | |
6029 | else | |
6030 | Default_Val := Get_Simple_Init_Val (Ctyp, N); | |
6031 | end if; | |
6032 | ||
6033 | Set_Expression (Assoc, New_Copy_Tree (Default_Val)); | |
6034 | Analyze_And_Resolve (Expression (Assoc), Ctyp); | |
6035 | end if; | |
6036 | ||
6037 | Next (Assoc); | |
6038 | end loop; | |
6039 | end if; | |
6040 | end; | |
6041 | ||
3cf3e5c6 | 6042 | -- STEP 2 |
70482933 | 6043 | |
3b9fa2df ES |
6044 | -- Here we test for is packed array aggregate that we can handle at |
6045 | -- compile time. If so, return with transformation done. Note that we do | |
6046 | -- this even if the aggregate is nested, because once we have done this | |
a90bd866 | 6047 | -- processing, there is no more nested aggregate. |
fbf5a39b AC |
6048 | |
6049 | if Packed_Array_Aggregate_Handled (N) then | |
6050 | return; | |
6051 | end if; | |
6052 | ||
6053 | -- At this point we try to convert to positional form | |
70482933 | 6054 | |
c42006e9 | 6055 | Convert_To_Positional (N); |
70482933 | 6056 | |
23a9215f | 6057 | -- If the result is no longer an aggregate (e.g. it may be a string |
fbf5a39b AC |
6058 | -- literal, or a temporary which has the needed value), then we are |
6059 | -- done, since there is no longer a nested aggregate. | |
6060 | ||
70482933 RK |
6061 | if Nkind (N) /= N_Aggregate then |
6062 | return; | |
6063 | ||
5eeeed5e AC |
6064 | -- We are also done if the result is an analyzed aggregate, indicating |
6065 | -- that Convert_To_Positional succeeded and reanalyzed the rewritten | |
6066 | -- aggregate. | |
fbf5a39b | 6067 | |
dc67cfea | 6068 | elsif Analyzed (N) and then Is_Rewrite_Substitution (N) then |
70482933 RK |
6069 | return; |
6070 | end if; | |
6071 | ||
fa57ac97 ES |
6072 | -- If all aggregate components are compile-time known and the aggregate |
6073 | -- has been flattened, nothing left to do. The same occurs if the | |
b465ef6f | 6074 | -- aggregate is used to initialize the components of a statically |
fa57ac97 | 6075 | -- allocated dispatch table. |
0f95b178 | 6076 | |
fa57ac97 ES |
6077 | if Compile_Time_Known_Aggregate (N) |
6078 | or else Is_Static_Dispatch_Table_Aggregate (N) | |
6079 | then | |
0f95b178 JM |
6080 | Set_Expansion_Delayed (N, False); |
6081 | return; | |
6082 | end if; | |
6083 | ||
fbf5a39b AC |
6084 | -- Now see if back end processing is possible |
6085 | ||
70482933 RK |
6086 | if Backend_Processing_Possible (N) then |
6087 | ||
6088 | -- If the aggregate is static but the constraints are not, build | |
6089 | -- a static subtype for the aggregate, so that Gigi can place it | |
6090 | -- in static memory. Perform an unchecked_conversion to the non- | |
6091 | -- static type imposed by the context. | |
6092 | ||
6093 | declare | |
6094 | Itype : constant Entity_Id := Etype (N); | |
6095 | Index : Node_Id; | |
6096 | Needs_Type : Boolean := False; | |
6097 | ||
6098 | begin | |
6099 | Index := First_Index (Itype); | |
70482933 | 6100 | while Present (Index) loop |
edab6088 | 6101 | if not Is_OK_Static_Subtype (Etype (Index)) then |
70482933 RK |
6102 | Needs_Type := True; |
6103 | exit; | |
6104 | else | |
6105 | Next_Index (Index); | |
6106 | end if; | |
6107 | end loop; | |
6108 | ||
6109 | if Needs_Type then | |
6110 | Build_Constrained_Type (Positional => True); | |
6111 | Rewrite (N, Unchecked_Convert_To (Itype, N)); | |
6112 | Analyze (N); | |
6113 | end if; | |
6114 | end; | |
6115 | ||
6116 | return; | |
6117 | end if; | |
6118 | ||
3cf3e5c6 | 6119 | -- STEP 3 |
fbf5a39b | 6120 | |
5ed4ba15 | 6121 | -- Delay expansion for nested aggregates: it will be taken care of when |
d22792bc EB |
6122 | -- the parent aggregate is expanded, excluding container aggregates as |
6123 | -- these are transformed into subprogram calls later. | |
70482933 RK |
6124 | |
6125 | Parent_Node := Parent (N); | |
6126 | Parent_Kind := Nkind (Parent_Node); | |
6127 | ||
6128 | if Parent_Kind = N_Qualified_Expression then | |
6129 | Parent_Node := Parent (Parent_Node); | |
6130 | Parent_Kind := Nkind (Parent_Node); | |
6131 | end if; | |
6132 | ||
ab246c16 EB |
6133 | if (Parent_Kind = N_Component_Association |
6134 | and then not Is_Container_Aggregate (Parent (Parent_Node))) | |
6135 | or else (Parent_Kind in N_Aggregate | N_Extension_Aggregate | |
6136 | and then not Is_Container_Aggregate (Parent_Node)) | |
70482933 | 6137 | or else (Parent_Kind = N_Object_Declaration |
4844a259 | 6138 | and then (Needs_Finalization (Typ) |
ea588d41 | 6139 | or else Is_Special_Return_Object |
4844a259 | 6140 | (Defining_Identifier (Parent_Node)))) |
70482933 RK |
6141 | or else (Parent_Kind = N_Assignment_Statement |
6142 | and then Inside_Init_Proc) | |
6143 | then | |
2a1838cd EB |
6144 | Set_Expansion_Delayed (N, not Static_Array_Aggregate (N)); |
6145 | return; | |
70482933 RK |
6146 | end if; |
6147 | ||
3cf3e5c6 | 6148 | -- STEP 4 |
70482933 | 6149 | |
bc1146e5 | 6150 | -- Check whether in-place aggregate expansion is possible |
70482933 RK |
6151 | |
6152 | -- For object declarations we build the aggregate in place, unless | |
d2a60e59 | 6153 | -- the array is bit-packed. |
70482933 RK |
6154 | |
6155 | -- For assignments we do the assignment in place if all the component | |
d2a60e59 ES |
6156 | -- associations have compile-time known values, or are default- |
6157 | -- initialized limited components, e.g. tasks. For other cases we | |
f037632e BD |
6158 | -- create a temporary. A full analysis for safety of in-place assignment |
6159 | -- is delicate. | |
70482933 | 6160 | |
6f639c98 ES |
6161 | -- For allocators we assign to the designated object in place if the |
6162 | -- aggregate meets the same conditions as other in-place assignments. | |
6163 | -- In this case the aggregate may not come from source but was created | |
6164 | -- for default initialization, e.g. with Initialize_Scalars. | |
6165 | ||
70482933 | 6166 | if Requires_Transient_Scope (Typ) then |
6560f851 | 6167 | Establish_Transient_Scope (N, Manage_Sec_Stack => False); |
70482933 RK |
6168 | end if; |
6169 | ||
92a68a04 | 6170 | -- An array of limited components is built in place |
d2a60e59 ES |
6171 | |
6172 | if Is_Limited_Type (Typ) then | |
6173 | Maybe_In_Place_OK := True; | |
6174 | ||
6175 | elsif Has_Default_Init_Comps (N) then | |
c45b6ae0 | 6176 | Maybe_In_Place_OK := False; |
6f639c98 ES |
6177 | |
6178 | elsif Is_Bit_Packed_Array (Typ) | |
6179 | or else Has_Controlled_Component (Typ) | |
6180 | then | |
6181 | Maybe_In_Place_OK := False; | |
6182 | ||
a80b1eb7 | 6183 | elsif Parent_Kind = N_Assignment_Statement then |
c45b6ae0 | 6184 | Maybe_In_Place_OK := |
a80b1eb7 | 6185 | In_Place_Assign_OK (N, Get_Base_Object (Name (Parent_Node))); |
6f639c98 | 6186 | |
a80b1eb7 EB |
6187 | elsif Parent_Kind = N_Allocator then |
6188 | Maybe_In_Place_OK := In_Place_Assign_OK (N); | |
6189 | ||
6190 | else | |
6191 | Maybe_In_Place_OK := False; | |
c45b6ae0 | 6192 | end if; |
70482933 | 6193 | |
36c73552 AC |
6194 | -- If this is an array of tasks, it will be expanded into build-in-place |
6195 | -- assignments. Build an activation chain for the tasks now. | |
a38ff9b1 | 6196 | |
c8def50f | 6197 | if Has_Task (Typ) then |
a38ff9b1 ES |
6198 | Build_Activation_Chain_Entity (N); |
6199 | end if; | |
6200 | ||
cf6956bb | 6201 | -- Perform in-place expansion of aggregate in an object declaration. |
f3d42000 AC |
6202 | -- Note: actions generated for the aggregate will be captured in an |
6203 | -- expression-with-actions statement so that they can be transferred | |
6204 | -- to freeze actions later if there is an address clause for the | |
6205 | -- object. (Note: we don't use a block statement because this would | |
6206 | -- cause generated freeze nodes to be elaborated in the wrong scope). | |
cf6956bb | 6207 | |
d2a60e59 ES |
6208 | -- Arrays of limited components must be built in place. The code |
6209 | -- previously excluded controlled components but this is an old | |
6210 | -- oversight: the rules in 7.6 (17) are clear. | |
5ed4ba15 | 6211 | |
d4e4e88a | 6212 | if Comes_From_Source (Parent_Node) |
3386e3ae AC |
6213 | and then Parent_Kind = N_Object_Declaration |
6214 | and then Present (Expression (Parent_Node)) | |
6215 | and then not | |
6732c403 | 6216 | Must_Slide (N, Etype (Defining_Identifier (Parent_Node)), Typ) |
3386e3ae | 6217 | and then not Is_Bit_Packed_Array (Typ) |
70482933 | 6218 | then |
ac43e11e | 6219 | In_Place_Assign_OK_For_Declaration := True; |
5ed4ba15 AC |
6220 | Tmp := Defining_Identifier (Parent_Node); |
6221 | Set_No_Initialization (Parent_Node); | |
6222 | Set_Expression (Parent_Node, Empty); | |
70482933 | 6223 | |
ac43e11e AC |
6224 | -- Set kind and type of the entity, for use in the analysis |
6225 | -- of the subsequent assignments. If the nominal type is not | |
70482933 RK |
6226 | -- constrained, build a subtype from the known bounds of the |
6227 | -- aggregate. If the declaration has a subtype mark, use it, | |
6228 | -- otherwise use the itype of the aggregate. | |
6229 | ||
2e02ab86 | 6230 | Mutate_Ekind (Tmp, E_Variable); |
ac43e11e | 6231 | |
70482933 RK |
6232 | if not Is_Constrained (Typ) then |
6233 | Build_Constrained_Type (Positional => False); | |
ac43e11e | 6234 | |
5ed4ba15 AC |
6235 | elsif Is_Entity_Name (Object_Definition (Parent_Node)) |
6236 | and then Is_Constrained (Entity (Object_Definition (Parent_Node))) | |
70482933 | 6237 | then |
5ed4ba15 | 6238 | Set_Etype (Tmp, Entity (Object_Definition (Parent_Node))); |
ac43e11e | 6239 | |
70482933 RK |
6240 | else |
6241 | Set_Size_Known_At_Compile_Time (Typ, False); | |
6242 | Set_Etype (Tmp, Typ); | |
6243 | end if; | |
6244 | ||
a80b1eb7 | 6245 | elsif Maybe_In_Place_OK and then Parent_Kind = N_Allocator then |
6f639c98 ES |
6246 | Set_Expansion_Delayed (N); |
6247 | return; | |
6248 | ||
d2a60e59 ES |
6249 | -- Limited arrays in return statements are expanded when |
6250 | -- enclosing construct is expanded. | |
6251 | ||
6252 | elsif Maybe_In_Place_OK | |
a80b1eb7 | 6253 | and then Parent_Kind = N_Simple_Return_Statement |
d2a60e59 ES |
6254 | then |
6255 | Set_Expansion_Delayed (N); | |
6256 | return; | |
6257 | ||
6537318f ES |
6258 | -- In the remaining cases the aggregate appears in the RHS of an |
6259 | -- assignment, which may be part of the expansion of an object | |
41a7b948 | 6260 | -- declaration. If the aggregate is an actual in a call, itself |
6537318f | 6261 | -- possibly in a RHS, building it in the target is not possible. |
6f639c98 | 6262 | |
70482933 | 6263 | elsif Maybe_In_Place_OK |
6537318f | 6264 | and then Nkind (Parent_Node) not in N_Subprogram_Call |
a80b1eb7 | 6265 | and then Safe_Left_Hand_Side (Name (Parent_Node)) |
70482933 | 6266 | then |
a80b1eb7 | 6267 | Tmp := Name (Parent_Node); |
70482933 RK |
6268 | |
6269 | if Etype (Tmp) /= Etype (N) then | |
6270 | Apply_Length_Check (N, Etype (Tmp)); | |
fbf5a39b AC |
6271 | |
6272 | if Nkind (N) = N_Raise_Constraint_Error then | |
6273 | ||
6274 | -- Static error, nothing further to expand | |
6275 | ||
6276 | return; | |
6277 | end if; | |
70482933 RK |
6278 | end if; |
6279 | ||
36a66365 AC |
6280 | -- If a slice assignment has an aggregate with a single others_choice, |
6281 | -- the assignment can be done in place even if bounds are not static, | |
6282 | -- by converting it into a loop over the discrete range of the slice. | |
6283 | ||
70482933 | 6284 | elsif Maybe_In_Place_OK |
a80b1eb7 | 6285 | and then Nkind (Name (Parent_Node)) = N_Slice |
36a66365 | 6286 | and then Is_Others_Aggregate (N) |
70482933 | 6287 | then |
a80b1eb7 | 6288 | Tmp := Name (Parent_Node); |
70482933 | 6289 | |
36a66365 AC |
6290 | -- Set type of aggregate to be type of lhs in assignment, in order |
6291 | -- to suppress redundant length checks. | |
6292 | ||
6293 | Set_Etype (N, Etype (Tmp)); | |
70482933 | 6294 | |
fbf5a39b AC |
6295 | -- Step 5 |
6296 | ||
bc1146e5 | 6297 | -- In-place aggregate expansion is not possible |
fbf5a39b | 6298 | |
70482933 | 6299 | else |
07fc65c4 | 6300 | Maybe_In_Place_OK := False; |
faf387e1 | 6301 | Tmp := Make_Temporary (Loc, 'A', N); |
70482933 | 6302 | Tmp_Decl := |
bdc193ba AC |
6303 | Make_Object_Declaration (Loc, |
6304 | Defining_Identifier => Tmp, | |
6305 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
70482933 RK |
6306 | Set_No_Initialization (Tmp_Decl, True); |
6307 | ||
6308 | -- If we are within a loop, the temporary will be pushed on the | |
6560f851 HK |
6309 | -- stack at each iteration. If the aggregate is the expression |
6310 | -- for an allocator, it will be immediately copied to the heap | |
6311 | -- and can be reclaimed at once. We create a transient scope | |
6312 | -- around the aggregate for this purpose. | |
70482933 RK |
6313 | |
6314 | if Ekind (Current_Scope) = E_Loop | |
a80b1eb7 | 6315 | and then Parent_Kind = N_Allocator |
70482933 | 6316 | then |
6560f851 | 6317 | Establish_Transient_Scope (N, Manage_Sec_Stack => False); |
9f6cee82 EB |
6318 | |
6319 | -- If the parent is an assignment for which no controlled actions | |
6320 | -- should take place, prevent the temporary from being finalized. | |
6321 | ||
6322 | elsif Parent_Kind = N_Assignment_Statement | |
6323 | and then No_Ctrl_Actions (Parent_Node) | |
6324 | then | |
6325 | Mutate_Ekind (Tmp, E_Variable); | |
6326 | Set_Is_Ignored_Transient (Tmp); | |
70482933 RK |
6327 | end if; |
6328 | ||
6329 | Insert_Action (N, Tmp_Decl); | |
6330 | end if; | |
6331 | ||
36c73552 AC |
6332 | -- Construct and insert the aggregate code. We can safely suppress index |
6333 | -- checks because this code is guaranteed not to raise CE on index | |
6334 | -- checks. However we should *not* suppress all checks. | |
70482933 | 6335 | |
07fc65c4 GB |
6336 | declare |
6337 | Target : Node_Id; | |
6338 | ||
6339 | begin | |
6340 | if Nkind (Tmp) = N_Defining_Identifier then | |
e4494292 | 6341 | Target := New_Occurrence_Of (Tmp, Loc); |
07fc65c4 GB |
6342 | |
6343 | else | |
d2a60e59 ES |
6344 | if Has_Default_Init_Comps (N) |
6345 | and then not Maybe_In_Place_OK | |
6346 | then | |
0ab80019 | 6347 | -- Ada 2005 (AI-287): This case has not been analyzed??? |
c45b6ae0 | 6348 | |
9bc856dd | 6349 | raise Program_Error; |
c45b6ae0 AC |
6350 | end if; |
6351 | ||
0da2c8ac | 6352 | -- Name in assignment is explicit dereference |
07fc65c4 GB |
6353 | |
6354 | Target := New_Copy (Tmp); | |
6355 | end if; | |
6356 | ||
bc1146e5 | 6357 | -- If we are to generate an in-place assignment for a declaration or |
ac43e11e AC |
6358 | -- an assignment statement, and the assignment can be done directly |
6359 | -- by the back end, then do not expand further. | |
6360 | ||
bc1146e5 | 6361 | -- ??? We can also do that if in-place expansion is not possible but |
ac43e11e AC |
6362 | -- then we could go into an infinite recursion. |
6363 | ||
6364 | if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK) | |
a1e1820b | 6365 | and then not CodePeer_Mode |
c63a2ad6 | 6366 | and then not Modify_Tree_For_C |
ac43e11e | 6367 | and then not Possible_Bit_Aligned_Component (Target) |
6b6bce61 | 6368 | and then not Is_Possibly_Unaligned_Slice (Target) |
ac43e11e AC |
6369 | and then Aggr_Assignment_OK_For_Backend (N) |
6370 | then | |
ac43e11e | 6371 | |
742084ad MP |
6372 | -- In the case of an assignment using an access with the |
6373 | -- Designated_Storage_Model aspect with a Copy_To procedure, | |
6374 | -- insert a temporary and have the back end handle the assignment | |
6375 | -- to it. Copy the result to the original target. | |
6376 | ||
6377 | if Parent_Kind = N_Assignment_Statement | |
6378 | and then Nkind (Name (Parent_Node)) = N_Explicit_Dereference | |
6379 | and then Has_Designated_Storage_Model_Aspect | |
6380 | (Etype (Prefix (Name (Parent_Node)))) | |
6381 | and then Present (Storage_Model_Copy_To | |
6382 | (Storage_Model_Object | |
6383 | (Etype (Prefix (Name (Parent_Node)))))) | |
6384 | then | |
ca4bff3a EB |
6385 | Aggr_Code := Build_Assignment_With_Temporary |
6386 | (Target, Typ, New_Copy_Tree (N)); | |
6387 | ||
742084ad MP |
6388 | else |
6389 | if Maybe_In_Place_OK then | |
6390 | return; | |
6391 | end if; | |
6392 | ||
ca4bff3a EB |
6393 | Aggr_Code := New_List ( |
6394 | Make_Assignment_Statement (Loc, | |
6395 | Name => Target, | |
6396 | Expression => New_Copy_Tree (N))); | |
742084ad | 6397 | end if; |
ca4bff3a | 6398 | |
d6e8719d | 6399 | else |
ac43e11e AC |
6400 | Aggr_Code := |
6401 | Build_Array_Aggr_Code (N, | |
6402 | Ctype => Ctyp, | |
6403 | Index => First_Index (Typ), | |
6404 | Into => Target, | |
6405 | Scalar_Comp => Is_Scalar_Type (Ctyp)); | |
6406 | end if; | |
4ac2bbbd AC |
6407 | |
6408 | -- Save the last assignment statement associated with the aggregate | |
6409 | -- when building a controlled object. This reference is utilized by | |
6410 | -- the finalization machinery when marking an object as successfully | |
6411 | -- initialized. | |
6412 | ||
6413 | if Needs_Finalization (Typ) | |
6414 | and then Is_Entity_Name (Target) | |
6415 | and then Present (Entity (Target)) | |
4a08c95c | 6416 | and then Ekind (Entity (Target)) in E_Constant | E_Variable |
4ac2bbbd AC |
6417 | then |
6418 | Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code)); | |
6419 | end if; | |
07fc65c4 | 6420 | end; |
70482933 | 6421 | |
6782b1ef AC |
6422 | -- If the aggregate is the expression in a declaration, the expanded |
6423 | -- code must be inserted after it. The defining entity might not come | |
6424 | -- from source if this is part of an inlined body, but the declaration | |
6425 | -- itself will. | |
fff7a6d9 | 6426 | -- The test below looks very specialized and kludgy??? |
6782b1ef AC |
6427 | |
6428 | if Comes_From_Source (Tmp) | |
6429 | or else | |
6430 | (Nkind (Parent (N)) = N_Object_Declaration | |
6431 | and then Comes_From_Source (Parent (N)) | |
6432 | and then Tmp = Defining_Entity (Parent (N))) | |
6433 | then | |
fff7a6d9 | 6434 | if Parent_Kind /= N_Object_Declaration or else Is_Frozen (Tmp) then |
cf6956bb | 6435 | Insert_Actions_After (Parent_Node, Aggr_Code); |
fff7a6d9 AC |
6436 | else |
6437 | declare | |
6438 | Comp_Stmt : constant Node_Id := | |
6439 | Make_Compound_Statement | |
6440 | (Sloc (Parent_Node), Actions => Aggr_Code); | |
6441 | begin | |
6442 | Insert_Action_After (Parent_Node, Comp_Stmt); | |
6443 | Set_Initialization_Statements (Tmp, Comp_Stmt); | |
6444 | end; | |
6445 | end if; | |
70482933 RK |
6446 | else |
6447 | Insert_Actions (N, Aggr_Code); | |
6448 | end if; | |
6449 | ||
07fc65c4 GB |
6450 | -- If the aggregate has been assigned in place, remove the original |
6451 | -- assignment. | |
6452 | ||
a80b1eb7 EB |
6453 | if Parent_Kind = N_Assignment_Statement and then Maybe_In_Place_OK then |
6454 | Rewrite (Parent_Node, Make_Null_Statement (Loc)); | |
70482933 | 6455 | |
a80b1eb7 EB |
6456 | -- Or else, if a temporary was created, replace the aggregate with it |
6457 | ||
6458 | elsif Parent_Kind /= N_Object_Declaration | |
6459 | or else Tmp /= Defining_Identifier (Parent_Node) | |
70482933 RK |
6460 | then |
6461 | Rewrite (N, New_Occurrence_Of (Tmp, Loc)); | |
6462 | Analyze_And_Resolve (N, Typ); | |
6463 | end if; | |
6464 | end Expand_Array_Aggregate; | |
6465 | ||
6466 | ------------------------ | |
6467 | -- Expand_N_Aggregate -- | |
6468 | ------------------------ | |
6469 | ||
6470 | procedure Expand_N_Aggregate (N : Node_Id) is | |
74580e1b | 6471 | T : constant Entity_Id := Etype (N); |
70482933 | 6472 | begin |
354c3840 AC |
6473 | -- Record aggregate case |
6474 | ||
74580e1b PT |
6475 | if Is_Record_Type (T) |
6476 | and then not Is_Private_Type (T) | |
765fc22c | 6477 | and then not Is_Homogeneous_Aggregate (N) |
fe3463cc | 6478 | then |
70482933 | 6479 | Expand_Record_Aggregate (N); |
354c3840 | 6480 | |
74580e1b | 6481 | elsif Has_Aspect (T, Aspect_Aggregate) then |
745f5698 ES |
6482 | Expand_Container_Aggregate (N); |
6483 | ||
354c3840 AC |
6484 | -- Array aggregate case |
6485 | ||
70482933 | 6486 | else |
354c3840 AC |
6487 | -- A special case, if we have a string subtype with bounds 1 .. N, |
6488 | -- where N is known at compile time, and the aggregate is of the | |
49eef89f AC |
6489 | -- form (others => 'x'), with a single choice and no expressions, |
6490 | -- and N is less than 80 (an arbitrary limit for now), then replace | |
6491 | -- the aggregate by the equivalent string literal (but do not mark | |
a90bd866 | 6492 | -- it as static since it is not). |
354c3840 AC |
6493 | |
6494 | -- Note: this entire circuit is redundant with respect to code in | |
6495 | -- Expand_Array_Aggregate that collapses others choices to positional | |
6496 | -- form, but there are two problems with that circuit: | |
6497 | ||
6498 | -- a) It is limited to very small cases due to ill-understood | |
b465ef6f | 6499 | -- interactions with bootstrapping. That limit is removed by |
354c3840 AC |
6500 | -- use of the No_Implicit_Loops restriction. |
6501 | ||
77a40ec1 | 6502 | -- b) It incorrectly ends up with the resulting expressions being |
354c3840 AC |
6503 | -- considered static when they are not. For example, the |
6504 | -- following test should fail: | |
6505 | ||
6506 | -- pragma Restrictions (No_Implicit_Loops); | |
6507 | -- package NonSOthers4 is | |
6508 | -- B : constant String (1 .. 6) := (others => 'A'); | |
6509 | -- DH : constant String (1 .. 8) := B & "BB"; | |
6510 | -- X : Integer; | |
6511 | -- pragma Export (C, X, Link_Name => DH); | |
6512 | -- end; | |
6513 | ||
6514 | -- But it succeeds (DH looks static to pragma Export) | |
6515 | ||
a90bd866 | 6516 | -- To be sorted out ??? |
354c3840 AC |
6517 | |
6518 | if Present (Component_Associations (N)) then | |
6519 | declare | |
6520 | CA : constant Node_Id := First (Component_Associations (N)); | |
6521 | MX : constant := 80; | |
6522 | ||
6523 | begin | |
10c257af ES |
6524 | if Present (CA) |
6525 | and then Nkind (First (Choice_List (CA))) = N_Others_Choice | |
354c3840 | 6526 | and then Nkind (Expression (CA)) = N_Character_Literal |
49eef89f | 6527 | and then No (Expressions (N)) |
354c3840 AC |
6528 | then |
6529 | declare | |
53f2aaf0 PT |
6530 | X : constant Node_Id := First_Index (T); |
6531 | EC : constant Node_Id := Expression (CA); | |
6532 | CV : constant Uint := Char_Literal_Value (EC); | |
6533 | CC : constant Char_Code := UI_To_CC (CV); | |
354c3840 AC |
6534 | |
6535 | begin | |
6536 | if Nkind (X) = N_Range | |
6537 | and then Compile_Time_Known_Value (Low_Bound (X)) | |
6538 | and then Expr_Value (Low_Bound (X)) = 1 | |
6539 | and then Compile_Time_Known_Value (High_Bound (X)) | |
6540 | then | |
6541 | declare | |
6542 | Hi : constant Uint := Expr_Value (High_Bound (X)); | |
6543 | ||
6544 | begin | |
6545 | if Hi <= MX then | |
6546 | Start_String; | |
6547 | ||
6548 | for J in 1 .. UI_To_Int (Hi) loop | |
53f2aaf0 | 6549 | Store_String_Char (CC); |
354c3840 AC |
6550 | end loop; |
6551 | ||
6552 | Rewrite (N, | |
6553 | Make_String_Literal (Sloc (N), | |
6554 | Strval => End_String)); | |
6555 | ||
53f2aaf0 PT |
6556 | if In_Character_Range (CC) then |
6557 | null; | |
6558 | elsif In_Wide_Character_Range (CC) then | |
354c3840 | 6559 | Set_Has_Wide_Character (N); |
53f2aaf0 PT |
6560 | else |
6561 | Set_Has_Wide_Wide_Character (N); | |
354c3840 AC |
6562 | end if; |
6563 | ||
6564 | Analyze_And_Resolve (N, T); | |
6565 | Set_Is_Static_Expression (N, False); | |
6566 | return; | |
6567 | end if; | |
6568 | end; | |
6569 | end if; | |
6570 | end; | |
6571 | end if; | |
6572 | end; | |
6573 | end if; | |
6574 | ||
6575 | -- Not that special case, so normal expansion of array aggregate | |
6576 | ||
70482933 RK |
6577 | Expand_Array_Aggregate (N); |
6578 | end if; | |
bdc193ba | 6579 | |
fbf5a39b AC |
6580 | exception |
6581 | when RE_Not_Available => | |
6582 | return; | |
70482933 RK |
6583 | end Expand_N_Aggregate; |
6584 | ||
745f5698 ES |
6585 | -------------------------------- |
6586 | -- Expand_Container_Aggregate -- | |
6587 | -------------------------------- | |
6588 | ||
6589 | procedure Expand_Container_Aggregate (N : Node_Id) is | |
ce59f39f GD |
6590 | Loc : constant Source_Ptr := Sloc (N); |
6591 | Typ : constant Entity_Id := Etype (N); | |
6592 | Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate); | |
745f5698 ES |
6593 | |
6594 | Empty_Subp : Node_Id := Empty; | |
6595 | Add_Named_Subp : Node_Id := Empty; | |
6596 | Add_Unnamed_Subp : Node_Id := Empty; | |
6597 | New_Indexed_Subp : Node_Id := Empty; | |
6598 | Assign_Indexed_Subp : Node_Id := Empty; | |
6599 | ||
ce59f39f GD |
6600 | Aggr_Code : constant List_Id := New_List; |
6601 | Temp : constant Entity_Id := Make_Temporary (Loc, 'C', N); | |
745f5698 | 6602 | |
4f6ebe2a | 6603 | Comp : Node_Id; |
745f5698 | 6604 | Decl : Node_Id; |
08c8883f | 6605 | Default : Node_Id; |
ce59f39f | 6606 | Init_Stat : Node_Id; |
08c8883f ES |
6607 | Siz : Int; |
6608 | ||
13112239 ES |
6609 | -- The following are used when the size of the aggregate is not |
6610 | -- static and requires a dynamic evaluation. | |
6611 | Siz_Decl : Node_Id; | |
6612 | Siz_Exp : Node_Id := Empty; | |
6613 | Count_Type : Entity_Id; | |
6614 | ||
08c8883f ES |
6615 | function Aggregate_Size return Int; |
6616 | -- Compute number of entries in aggregate, including choices | |
13112239 | 6617 | -- that cover a range or subtype, as well as iterated constructs. |
08c8883f | 6618 | -- Return -1 if the size is not known statically, in which case |
13112239 ES |
6619 | -- allocate a default size for the aggregate, or build an expression |
6620 | -- to estimate the size dynamically. | |
6621 | ||
6622 | function Build_Siz_Exp (Comp : Node_Id) return Int; | |
6623 | -- When the aggregate contains a single Iterated_Component_Association | |
6624 | -- or Element_Association with non-static bounds, build an expression | |
6625 | -- to be used as the allocated size of the container. This may be an | |
6626 | -- overestimate if a filter is present, but is a safe approximation. | |
fe43084c ES |
6627 | -- If bounds are dynamic the aggregate is created in two passes, and |
6628 | -- the first generates a loop for the sole purpose of computing the | |
41a7b948 | 6629 | -- number of elements that will be generated on the second pass. |
ce59f39f | 6630 | |
7a21651f ES |
6631 | procedure Expand_Iterated_Component (Comp : Node_Id); |
6632 | -- Handle iterated_component_association and iterated_Element | |
6633 | -- association by generating a loop over the specified range, | |
6634 | -- given either by a loop parameter specification or an iterator | |
6635 | -- specification. | |
6636 | ||
08c8883f ES |
6637 | -------------------- |
6638 | -- Aggregate_Size -- | |
6639 | -------------------- | |
6640 | ||
6641 | function Aggregate_Size return Int is | |
6642 | Comp : Node_Id; | |
6643 | Choice : Node_Id; | |
6644 | Lo, Hi : Node_Id; | |
ef7a3876 | 6645 | Siz : Int; |
08c8883f ES |
6646 | |
6647 | procedure Add_Range_Size; | |
13112239 ES |
6648 | -- Compute number of components specified by a component association |
6649 | -- given by a range or subtype name. | |
6650 | ||
6651 | -------------------- | |
6652 | -- Add_Range_Size -- | |
6653 | -------------------- | |
08c8883f ES |
6654 | |
6655 | procedure Add_Range_Size is | |
6656 | begin | |
13112239 ES |
6657 | -- The bounds of the discrete range are integers or enumeration |
6658 | -- literals | |
6659 | ||
08c8883f ES |
6660 | if Nkind (Lo) = N_Integer_Literal then |
6661 | Siz := Siz + UI_To_Int (Intval (Hi)) | |
13112239 ES |
6662 | - UI_To_Int (Intval (Lo)) + 1; |
6663 | else | |
6664 | Siz := Siz + UI_To_Int (Enumeration_Pos (Hi)) | |
6665 | - UI_To_Int (Enumeration_Pos (Lo)) + 1; | |
08c8883f ES |
6666 | end if; |
6667 | end Add_Range_Size; | |
6668 | ||
6669 | begin | |
ef7a3876 | 6670 | -- Aggregate is either all positional or all named |
13112239 | 6671 | |
ef7a3876 | 6672 | Siz := List_Length (Expressions (N)); |
08c8883f ES |
6673 | |
6674 | if Present (Component_Associations (N)) then | |
6675 | Comp := First (Component_Associations (N)); | |
13112239 ES |
6676 | -- If there is a single component association it can be |
6677 | -- an iterated component with dynamic bounds or an element | |
6678 | -- iterator over an iterable object. If it is an array | |
6679 | -- we can use the attribute Length to get its size; | |
6680 | -- for a predefined container the function Length plays | |
6681 | -- the same role. There is no available mechanism for | |
6682 | -- user-defined containers. For now we treat all of these | |
6683 | -- as dynamic. | |
6684 | ||
6685 | if List_Length (Component_Associations (N)) = 1 | |
6686 | and then Nkind (Comp) in N_Iterated_Component_Association | | |
6687 | N_Iterated_Element_Association | |
6688 | then | |
6689 | return Build_Siz_Exp (Comp); | |
daaf0179 ES |
6690 | end if; |
6691 | ||
13112239 ES |
6692 | -- Otherwise all associations must specify static sizes. |
6693 | ||
08c8883f ES |
6694 | while Present (Comp) loop |
6695 | Choice := First (Choice_List (Comp)); | |
6696 | ||
6697 | while Present (Choice) loop | |
6698 | Analyze (Choice); | |
6699 | ||
6700 | if Nkind (Choice) = N_Range then | |
6701 | Lo := Low_Bound (Choice); | |
6702 | Hi := High_Bound (Choice); | |
13112239 | 6703 | Add_Range_Size; |
08c8883f ES |
6704 | |
6705 | elsif Is_Entity_Name (Choice) | |
6706 | and then Is_Type (Entity (Choice)) | |
6707 | then | |
6708 | Lo := Type_Low_Bound (Entity (Choice)); | |
6709 | Hi := Type_High_Bound (Entity (Choice)); | |
13112239 | 6710 | Add_Range_Size; |
08c8883f ES |
6711 | |
6712 | Rewrite (Choice, | |
6713 | Make_Range (Loc, | |
6714 | New_Copy_Tree (Lo), | |
6715 | New_Copy_Tree (Hi))); | |
6716 | ||
6717 | else | |
6718 | -- Single choice (syntax excludes a subtype | |
6719 | -- indication). | |
6720 | ||
6721 | Siz := Siz + 1; | |
6722 | end if; | |
6723 | ||
6724 | Next (Choice); | |
6725 | end loop; | |
6726 | Next (Comp); | |
6727 | end loop; | |
6728 | end if; | |
6729 | ||
6730 | return Siz; | |
6731 | end Aggregate_Size; | |
6732 | ||
13112239 ES |
6733 | ------------------- |
6734 | -- Build_Siz_Exp -- | |
6735 | ------------------- | |
6736 | ||
6737 | function Build_Siz_Exp (Comp : Node_Id) return Int is | |
6738 | Lo, Hi : Node_Id; | |
6739 | begin | |
6740 | if Nkind (Comp) = N_Range then | |
6741 | Lo := Low_Bound (Comp); | |
6742 | Hi := High_Bound (Comp); | |
6743 | Analyze (Lo); | |
6744 | Analyze (Hi); | |
6745 | ||
6746 | -- Compute static size when possible. | |
6747 | ||
6748 | if Is_Static_Expression (Lo) | |
6749 | and then Is_Static_Expression (Hi) | |
6750 | then | |
6751 | if Nkind (Lo) = N_Integer_Literal then | |
6752 | Siz := UI_To_Int (Intval (Hi)) - UI_To_Int (Intval (Lo)) + 1; | |
6753 | else | |
6754 | Siz := UI_To_Int (Enumeration_Pos (Hi)) | |
6755 | - UI_To_Int (Enumeration_Pos (Lo)) + 1; | |
6756 | end if; | |
6757 | return Siz; | |
6758 | ||
6759 | else | |
6760 | Siz_Exp := | |
6761 | Make_Op_Add (Sloc (Comp), | |
6762 | Left_Opnd => | |
6763 | Make_Op_Subtract (Sloc (Comp), | |
6764 | Left_Opnd => New_Copy_Tree (Hi), | |
6765 | Right_Opnd => New_Copy_Tree (Lo)), | |
6766 | Right_Opnd => | |
6767 | Make_Integer_Literal (Loc, 1)); | |
6768 | return -1; | |
6769 | end if; | |
6770 | ||
6771 | elsif Nkind (Comp) = N_Iterated_Component_Association then | |
6772 | return Build_Siz_Exp (First (Discrete_Choices (Comp))); | |
6773 | ||
6774 | elsif Nkind (Comp) = N_Iterated_Element_Association then | |
fe43084c ES |
6775 | return -1; |
6776 | ||
8b1a5da3 | 6777 | -- ??? Need to create code for a loop and add to generated code, |
fe43084c ES |
6778 | -- as is done for array aggregates with iterated element |
6779 | -- associations, instead of using Append operations. | |
13112239 ES |
6780 | |
6781 | else | |
6782 | return -1; | |
6783 | end if; | |
6784 | end Build_Siz_Exp; | |
6785 | ||
4f6ebe2a ES |
6786 | ------------------------------- |
6787 | -- Expand_Iterated_Component -- | |
6788 | ------------------------------- | |
6789 | ||
6790 | procedure Expand_Iterated_Component (Comp : Node_Id) is | |
6791 | Expr : constant Node_Id := Expression (Comp); | |
4f6ebe2a | 6792 | |
c0bab60b ES |
6793 | Key_Expr : Node_Id := Empty; |
6794 | Loop_Id : Entity_Id; | |
4f6ebe2a ES |
6795 | L_Range : Node_Id; |
6796 | L_Iteration_Scheme : Node_Id; | |
6797 | Loop_Stat : Node_Id; | |
f3f1debe | 6798 | Params : List_Id; |
4f6ebe2a ES |
6799 | Stats : List_Id; |
6800 | ||
6801 | begin | |
c0bab60b ES |
6802 | if Nkind (Comp) = N_Iterated_Element_Association then |
6803 | Key_Expr := Key_Expression (Comp); | |
6804 | ||
6805 | -- We create a new entity as loop identifier in all cases, | |
6806 | -- as is done for generated loops elsewhere, as the loop | |
6807 | -- structure has been previously analyzed. | |
6808 | ||
6809 | if Present (Iterator_Specification (Comp)) then | |
6810 | ||
fe43084c | 6811 | -- Either an Iterator_Specification or a Loop_Parameter_ |
c0bab60b ES |
6812 | -- Specification is present. |
6813 | ||
6814 | L_Iteration_Scheme := | |
6815 | Make_Iteration_Scheme (Loc, | |
6816 | Iterator_Specification => Iterator_Specification (Comp)); | |
6817 | Loop_Id := | |
6818 | Make_Defining_Identifier (Loc, | |
6819 | Chars => Chars (Defining_Identifier | |
6820 | (Iterator_Specification (Comp)))); | |
6821 | Set_Defining_Identifier | |
6822 | (Iterator_Specification (L_Iteration_Scheme), Loop_Id); | |
6823 | ||
6824 | else | |
6825 | L_Iteration_Scheme := | |
6826 | Make_Iteration_Scheme (Loc, | |
6827 | Loop_Parameter_Specification => | |
6828 | Loop_Parameter_Specification (Comp)); | |
6829 | Loop_Id := | |
f3f1debe ES |
6830 | Make_Defining_Identifier (Loc, |
6831 | Chars => Chars (Defining_Identifier | |
6832 | (Loop_Parameter_Specification (Comp)))); | |
c0bab60b | 6833 | Set_Defining_Identifier |
f3f1debe ES |
6834 | (Loop_Parameter_Specification |
6835 | (L_Iteration_Scheme), Loop_Id); | |
c0bab60b | 6836 | end if; |
f3f1debe | 6837 | else |
c0bab60b | 6838 | |
f3f1debe | 6839 | -- Iterated_Component_Association. |
8092c199 | 6840 | |
f3f1debe | 6841 | if Present (Iterator_Specification (Comp)) then |
d983abeb PT |
6842 | Loop_Id := |
6843 | Make_Defining_Identifier (Loc, | |
6844 | Chars => Chars (Defining_Identifier | |
6845 | (Iterator_Specification (Comp)))); | |
f3f1debe ES |
6846 | L_Iteration_Scheme := |
6847 | Make_Iteration_Scheme (Loc, | |
6848 | Iterator_Specification => Iterator_Specification (Comp)); | |
6849 | ||
6850 | else | |
41a7b948 | 6851 | -- Loop_Parameter_Specification is parsed with a choice list. |
f3f1debe ES |
6852 | -- where the range is the first (and only) choice. |
6853 | ||
d983abeb PT |
6854 | Loop_Id := |
6855 | Make_Defining_Identifier (Loc, | |
6856 | Chars => Chars (Defining_Identifier (Comp))); | |
f3f1debe ES |
6857 | L_Range := Relocate_Node (First (Discrete_Choices (Comp))); |
6858 | ||
6859 | L_Iteration_Scheme := | |
6860 | Make_Iteration_Scheme (Loc, | |
6861 | Loop_Parameter_Specification => | |
6862 | Make_Loop_Parameter_Specification (Loc, | |
6863 | Defining_Identifier => Loop_Id, | |
6864 | Discrete_Subtype_Definition => L_Range)); | |
6865 | end if; | |
8092c199 | 6866 | end if; |
4f6ebe2a | 6867 | |
0b4034c0 GD |
6868 | -- Build insertion statement. For a positional aggregate, only the |
6869 | -- expression is needed. For a named aggregate, the loop variable, | |
6870 | -- whose type is that of the key, is an additional parameter for | |
6871 | -- the insertion operation. | |
c0bab60b ES |
6872 | -- If a Key_Expression is present, it serves as the additional |
6873 | -- parameter. Otherwise the key is given by the loop parameter | |
6874 | -- itself. | |
4f6ebe2a | 6875 | |
13112239 ES |
6876 | if Present (Add_Unnamed_Subp) |
6877 | and then No (Add_Named_Subp) | |
6878 | then | |
4f6ebe2a ES |
6879 | Stats := New_List |
6880 | (Make_Procedure_Call_Statement (Loc, | |
0b4034c0 GD |
6881 | Name => New_Occurrence_Of (Entity (Add_Unnamed_Subp), Loc), |
6882 | Parameter_Associations => | |
6883 | New_List (New_Occurrence_Of (Temp, Loc), | |
4f6ebe2a ES |
6884 | New_Copy_Tree (Expr)))); |
6885 | else | |
c0bab60b ES |
6886 | -- Named or indexed aggregate, for which a key is present, |
6887 | -- possibly with a specified key_expression. | |
6888 | ||
6889 | if Present (Key_Expr) then | |
f3f1debe ES |
6890 | Params := New_List (New_Occurrence_Of (Temp, Loc), |
6891 | New_Copy_Tree (Key_Expr), | |
6892 | New_Copy_Tree (Expr)); | |
c0bab60b | 6893 | else |
f3f1debe ES |
6894 | Params := New_List (New_Occurrence_Of (Temp, Loc), |
6895 | New_Occurrence_Of (Loop_Id, Loc), | |
6896 | New_Copy_Tree (Expr)); | |
c0bab60b | 6897 | end if; |
f3f1debe ES |
6898 | |
6899 | Stats := New_List | |
6900 | (Make_Procedure_Call_Statement (Loc, | |
6901 | Name => New_Occurrence_Of (Entity (Add_Named_Subp), Loc), | |
6902 | Parameter_Associations => Params)); | |
4f6ebe2a ES |
6903 | end if; |
6904 | ||
d43fbe01 PT |
6905 | Loop_Stat := Make_Implicit_Loop_Statement |
6906 | (Node => N, | |
6907 | Identifier => Empty, | |
6908 | Iteration_Scheme => L_Iteration_Scheme, | |
6909 | Statements => Stats); | |
4f6ebe2a | 6910 | Append (Loop_Stat, Aggr_Code); |
7a21651f | 6911 | |
4f6ebe2a ES |
6912 | end Expand_Iterated_Component; |
6913 | ||
00b21aa9 | 6914 | -- Start of processing for Expand_Container_Aggregate |
08c8883f | 6915 | |
745f5698 ES |
6916 | begin |
6917 | Parse_Aspect_Aggregate (Asp, | |
6918 | Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp, | |
6919 | New_Indexed_Subp, Assign_Indexed_Subp); | |
08c8883f ES |
6920 | |
6921 | -- The constructor for bounded containers is a function with | |
6922 | -- a parameter that sets the size of the container. If the | |
13112239 ES |
6923 | -- size cannot be determined statically we use a default value |
6924 | -- or a dynamic expression. | |
08c8883f ES |
6925 | |
6926 | Siz := Aggregate_Size; | |
7a21651f | 6927 | |
67138e09 JM |
6928 | --------------------- |
6929 | -- Empty function -- | |
6930 | --------------------- | |
6931 | ||
08c8883f ES |
6932 | if Ekind (Entity (Empty_Subp)) = E_Function |
6933 | and then Present (First_Formal (Entity (Empty_Subp))) | |
6934 | then | |
6935 | Default := Default_Value (First_Formal (Entity (Empty_Subp))); | |
13112239 ES |
6936 | |
6937 | -- If aggregate size is not static, we can use default value | |
6938 | -- of formal parameter for allocation. We assume that this | |
08c8883f | 6939 | -- (implementation-dependent) value is static, even though |
13112239 ES |
6940 | -- the AI does not require it. |
6941 | ||
6942 | -- Create declaration for size: a constant literal in the simple | |
6943 | -- case, an expression if iterated component associations may be | |
6944 | -- involved, the default otherwise. | |
08c8883f | 6945 | |
13112239 ES |
6946 | Count_Type := Etype (First_Formal (Entity (Empty_Subp))); |
6947 | if Siz = -1 then | |
6948 | if No (Siz_Exp) then | |
6949 | Siz := UI_To_Int (Intval (Default)); | |
6950 | Siz_Exp := Make_Integer_Literal (Loc, Siz); | |
6951 | ||
6952 | else | |
6953 | Siz_Exp := Make_Type_Conversion (Loc, | |
6954 | Subtype_Mark => | |
6955 | New_Occurrence_Of (Count_Type, Loc), | |
6956 | Expression => Siz_Exp); | |
6957 | end if; | |
6958 | ||
6959 | else | |
6960 | Siz_Exp := Make_Integer_Literal (Loc, Siz); | |
08c8883f ES |
6961 | end if; |
6962 | ||
13112239 ES |
6963 | Siz_Decl := Make_Object_Declaration (Loc, |
6964 | Defining_Identifier => Make_Temporary (Loc, 'S', N), | |
6965 | Object_Definition => | |
6966 | New_Occurrence_Of (Count_Type, Loc), | |
6967 | Expression => Siz_Exp); | |
6968 | Append (Siz_Decl, Aggr_Code); | |
6969 | ||
6970 | if Nkind (Siz_Exp) = N_Integer_Literal then | |
6971 | Init_Stat := | |
6972 | Make_Object_Declaration (Loc, | |
6973 | Defining_Identifier => Temp, | |
6974 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
6975 | Expression => Make_Function_Call (Loc, | |
6976 | Name => New_Occurrence_Of (Entity (Empty_Subp), Loc), | |
6977 | Parameter_Associations => | |
6978 | New_List | |
6979 | (New_Occurrence_Of | |
6980 | (Defining_Identifier (Siz_Decl), Loc)))); | |
6981 | ||
6982 | else | |
6983 | Init_Stat := | |
6984 | Make_Object_Declaration (Loc, | |
6985 | Defining_Identifier => Temp, | |
6986 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
6987 | Expression => Make_Function_Call (Loc, | |
6988 | Name => | |
6989 | New_Occurrence_Of (Entity (New_Indexed_Subp), Loc), | |
6990 | Parameter_Associations => | |
6991 | New_List ( | |
6992 | Make_Integer_Literal (Loc, 1), | |
6993 | New_Occurrence_Of | |
6994 | (Defining_Identifier (Siz_Decl), Loc)))); | |
6995 | end if; | |
08c8883f ES |
6996 | |
6997 | Append (Init_Stat, Aggr_Code); | |
6998 | ||
67138e09 | 6999 | -- Size is dynamic: Create declaration for object, and initialize |
13112239 | 7000 | -- with a call to the null container, or an assignment to it. |
08c8883f ES |
7001 | |
7002 | else | |
7003 | Decl := | |
7004 | Make_Object_Declaration (Loc, | |
7005 | Defining_Identifier => Temp, | |
7006 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
7007 | ||
7008 | Insert_Action (N, Decl); | |
13112239 ES |
7009 | |
7010 | -- The Empty entity is either a parameterless function, or | |
7011 | -- a constant. | |
7012 | ||
08c8883f ES |
7013 | if Ekind (Entity (Empty_Subp)) = E_Function then |
7014 | Init_Stat := Make_Assignment_Statement (Loc, | |
7015 | Name => New_Occurrence_Of (Temp, Loc), | |
7016 | Expression => Make_Function_Call (Loc, | |
7017 | Name => New_Occurrence_Of (Entity (Empty_Subp), Loc))); | |
13112239 | 7018 | |
08c8883f ES |
7019 | else |
7020 | Init_Stat := Make_Assignment_Statement (Loc, | |
7021 | Name => New_Occurrence_Of (Temp, Loc), | |
7022 | Expression => New_Occurrence_Of (Entity (Empty_Subp), Loc)); | |
7023 | end if; | |
7024 | ||
7025 | Append (Init_Stat, Aggr_Code); | |
7026 | end if; | |
745f5698 | 7027 | |
67138e09 JM |
7028 | -- Report warning on infinite recursion if an empty container aggregate |
7029 | -- appears in the return statement of its Empty function. | |
7030 | ||
7031 | if Ekind (Entity (Empty_Subp)) = E_Function | |
7032 | and then Nkind (Parent (N)) = N_Simple_Return_Statement | |
7033 | and then Is_Empty_List (Expressions (N)) | |
7034 | and then Is_Empty_List (Component_Associations (N)) | |
7035 | and then Entity (Empty_Subp) = Current_Scope | |
7036 | then | |
7037 | Error_Msg_Warn := SPARK_Mode /= On; | |
7038 | Error_Msg_N | |
7039 | ("!empty aggregate returned by the empty function of a container" | |
7040 | & " aggregate<<<", Parent (N)); | |
7041 | Error_Msg_N | |
7042 | ("\this will result in infinite recursion??", Parent (N)); | |
7043 | end if; | |
7044 | ||
7a21651f ES |
7045 | --------------------------- |
7046 | -- Positional aggregate -- | |
7047 | --------------------------- | |
745f5698 | 7048 | |
08c8883f ES |
7049 | -- If the aggregate is positional the aspect must include |
7050 | -- an Add_Unnamed subprogram. | |
7051 | ||
13112239 | 7052 | if Present (Add_Unnamed_Subp) then |
4f6ebe2a ES |
7053 | if Present (Expressions (N)) then |
7054 | declare | |
7055 | Insert : constant Entity_Id := Entity (Add_Unnamed_Subp); | |
7056 | Comp : Node_Id; | |
7057 | Stat : Node_Id; | |
7058 | ||
7059 | begin | |
7060 | Comp := First (Expressions (N)); | |
7061 | while Present (Comp) loop | |
7062 | Stat := Make_Procedure_Call_Statement (Loc, | |
7063 | Name => New_Occurrence_Of (Insert, Loc), | |
7064 | Parameter_Associations => | |
7065 | New_List (New_Occurrence_Of (Temp, Loc), | |
0b4034c0 | 7066 | New_Copy_Tree (Comp))); |
4f6ebe2a ES |
7067 | Append (Stat, Aggr_Code); |
7068 | Next (Comp); | |
7069 | end loop; | |
7070 | end; | |
7071 | end if; | |
7072 | ||
13112239 ES |
7073 | -- Indexed aggregates are handled below. Unnamed aggregates |
7074 | -- such as sets may include iterated component associations. | |
4f6ebe2a | 7075 | |
13112239 ES |
7076 | if No (New_Indexed_Subp) then |
7077 | Comp := First (Component_Associations (N)); | |
7078 | while Present (Comp) loop | |
7079 | if Nkind (Comp) = N_Iterated_Component_Association then | |
7080 | Expand_Iterated_Component (Comp); | |
7081 | end if; | |
7082 | Next (Comp); | |
7083 | end loop; | |
7084 | end if; | |
4f6ebe2a | 7085 | |
7a21651f ES |
7086 | --------------------- |
7087 | -- Named_Aggregate -- | |
7088 | --------------------- | |
7089 | ||
4f6ebe2a | 7090 | elsif Present (Add_Named_Subp) then |
ce59f39f | 7091 | declare |
4f6ebe2a | 7092 | Insert : constant Entity_Id := Entity (Add_Named_Subp); |
ce59f39f | 7093 | Stat : Node_Id; |
4f6ebe2a | 7094 | Key : Node_Id; |
ce59f39f | 7095 | begin |
4f6ebe2a ES |
7096 | Comp := First (Component_Associations (N)); |
7097 | ||
0b4034c0 | 7098 | -- Each component association may contain several choices; |
4f6ebe2a ES |
7099 | -- generate an insertion statement for each. |
7100 | ||
ce59f39f | 7101 | while Present (Comp) loop |
c0bab60b ES |
7102 | if Nkind (Comp) in N_Iterated_Component_Association |
7103 | | N_Iterated_Element_Association | |
7104 | then | |
4f6ebe2a ES |
7105 | Expand_Iterated_Component (Comp); |
7106 | else | |
7107 | Key := First (Choices (Comp)); | |
7108 | ||
7109 | while Present (Key) loop | |
7110 | Stat := Make_Procedure_Call_Statement (Loc, | |
7111 | Name => New_Occurrence_Of (Insert, Loc), | |
7112 | Parameter_Associations => | |
7113 | New_List (New_Occurrence_Of (Temp, Loc), | |
0b4034c0 GD |
7114 | New_Copy_Tree (Key), |
7115 | New_Copy_Tree (Expression (Comp)))); | |
4f6ebe2a ES |
7116 | Append (Stat, Aggr_Code); |
7117 | ||
7118 | Next (Key); | |
7119 | end loop; | |
7120 | end if; | |
7121 | ||
ce59f39f GD |
7122 | Next (Comp); |
7123 | end loop; | |
7124 | end; | |
08c8883f | 7125 | end if; |
7a21651f ES |
7126 | |
7127 | ----------------------- | |
7128 | -- Indexed_Aggregate -- | |
7129 | ----------------------- | |
7130 | ||
08c8883f ES |
7131 | -- For an indexed aggregate there must be an Assigned_Indexeed |
7132 | -- subprogram. Note that unlike array aggregates, a container | |
7133 | -- aggregate must be fully positional or fully indexed. In the | |
7134 | -- first case the expansion has already taken place. | |
13112239 ES |
7135 | -- TBA: the keys for an indexed aggregate must provide a dense |
7136 | -- range with no repetitions. | |
08c8883f ES |
7137 | |
7138 | if Present (Assign_Indexed_Subp) | |
7139 | and then Present (Component_Associations (N)) | |
7140 | then | |
7a21651f ES |
7141 | declare |
7142 | Insert : constant Entity_Id := Entity (Assign_Indexed_Subp); | |
7143 | Index_Type : constant Entity_Id := | |
7144 | Etype (Next_Formal (First_Formal (Insert))); | |
7145 | ||
7a21651f ES |
7146 | function Expand_Range_Component |
7147 | (Rng : Node_Id; | |
7148 | Expr : Node_Id) return Node_Id; | |
7149 | -- Transform a component assoication with a range into an | |
7150 | -- explicit loop. If the choice is a subtype name, it is | |
7151 | -- rewritten as a range with the corresponding bounds, which | |
7152 | -- are known to be static. | |
7153 | ||
7154 | Comp : Node_Id; | |
7155 | Index : Node_Id; | |
7156 | Pos : Int := 0; | |
7157 | Stat : Node_Id; | |
7158 | Key : Node_Id; | |
7a21651f ES |
7159 | |
7160 | ----------------------------- | |
7161 | -- Expand_Raange_Component -- | |
7162 | ----------------------------- | |
7163 | ||
7164 | function Expand_Range_Component | |
7165 | (Rng : Node_Id; | |
7166 | Expr : Node_Id) return Node_Id | |
7167 | is | |
7168 | Loop_Id : constant Entity_Id := | |
7169 | Make_Temporary (Loc, 'T'); | |
7170 | ||
7171 | L_Iteration_Scheme : Node_Id; | |
7172 | Stats : List_Id; | |
7173 | ||
7174 | begin | |
7175 | L_Iteration_Scheme := | |
7176 | Make_Iteration_Scheme (Loc, | |
7177 | Loop_Parameter_Specification => | |
7178 | Make_Loop_Parameter_Specification (Loc, | |
7179 | Defining_Identifier => Loop_Id, | |
7180 | Discrete_Subtype_Definition => New_Copy_Tree (Rng))); | |
7181 | ||
7182 | Stats := New_List | |
7183 | (Make_Procedure_Call_Statement (Loc, | |
7184 | Name => | |
7185 | New_Occurrence_Of (Entity (Assign_Indexed_Subp), Loc), | |
7186 | Parameter_Associations => | |
7187 | New_List (New_Occurrence_Of (Temp, Loc), | |
7188 | New_Occurrence_Of (Loop_Id, Loc), | |
7189 | New_Copy_Tree (Expr)))); | |
7190 | ||
7191 | return Make_Implicit_Loop_Statement | |
7192 | (Node => N, | |
7193 | Identifier => Empty, | |
7194 | Iteration_Scheme => L_Iteration_Scheme, | |
7195 | Statements => Stats); | |
7196 | end Expand_Range_Component; | |
7197 | ||
7a21651f | 7198 | begin |
08c8883f | 7199 | if Siz > 0 then |
7a21651f ES |
7200 | |
7201 | -- Modify the call to the constructor to allocate the | |
7202 | -- required size for the aggregwte : call the provided | |
7203 | -- constructor rather than the Empty aggregate. | |
7204 | ||
d43fbe01 | 7205 | Index := Make_Op_Add (Loc, |
7a21651f | 7206 | Left_Opnd => New_Copy_Tree (Type_Low_Bound (Index_Type)), |
08c8883f | 7207 | Right_Opnd => Make_Integer_Literal (Loc, Siz - 1)); |
7a21651f ES |
7208 | |
7209 | Set_Expression (Init_Stat, | |
7210 | Make_Function_Call (Loc, | |
7211 | Name => | |
7212 | New_Occurrence_Of (Entity (New_Indexed_Subp), Loc), | |
7213 | Parameter_Associations => | |
7214 | New_List ( | |
7215 | New_Copy_Tree (Type_Low_Bound (Index_Type)), | |
7216 | Index))); | |
7217 | end if; | |
7218 | ||
7219 | if Present (Expressions (N)) then | |
7220 | Comp := First (Expressions (N)); | |
7221 | ||
7222 | while Present (Comp) loop | |
7223 | ||
7224 | -- Compute index position for successive components | |
7225 | -- in the list of expressions, and use the indexed | |
7226 | -- assignment procedure for each. | |
7227 | ||
7228 | Index := Make_Op_Add (Loc, | |
7229 | Left_Opnd => Type_Low_Bound (Index_Type), | |
7230 | Right_Opnd => Make_Integer_Literal (Loc, Pos)); | |
7231 | ||
7232 | Stat := Make_Procedure_Call_Statement (Loc, | |
7233 | Name => New_Occurrence_Of (Insert, Loc), | |
7234 | Parameter_Associations => | |
7235 | New_List (New_Occurrence_Of (Temp, Loc), | |
7236 | Index, | |
7237 | New_Copy_Tree (Comp))); | |
7238 | ||
7239 | Pos := Pos + 1; | |
7240 | ||
7241 | Append (Stat, Aggr_Code); | |
7242 | Next (Comp); | |
7243 | end loop; | |
7244 | end if; | |
7245 | ||
7246 | if Present (Component_Associations (N)) then | |
7247 | Comp := First (Component_Associations (N)); | |
7248 | ||
7249 | -- The choice may be a static value, or a range with | |
7250 | -- static bounds. | |
7251 | ||
7252 | while Present (Comp) loop | |
7253 | if Nkind (Comp) = N_Component_Association then | |
7254 | Key := First (Choices (Comp)); | |
7255 | while Present (Key) loop | |
7256 | ||
7257 | -- If the expression is a box, the corresponding | |
7258 | -- component (s) is left uninitialized. | |
7259 | ||
7260 | if Box_Present (Comp) then | |
7261 | goto Next_Key; | |
7262 | ||
7263 | elsif Nkind (Key) = N_Range then | |
7264 | ||
7265 | -- Create loop for tne specified range, | |
7266 | -- with copies of the expression. | |
7267 | ||
7268 | Stat := | |
7269 | Expand_Range_Component (Key, Expression (Comp)); | |
7270 | ||
7271 | else | |
7272 | Stat := Make_Procedure_Call_Statement (Loc, | |
7273 | Name => New_Occurrence_Of | |
7274 | (Entity (Assign_Indexed_Subp), Loc), | |
7275 | Parameter_Associations => | |
7276 | New_List (New_Occurrence_Of (Temp, Loc), | |
7277 | New_Copy_Tree (Key), | |
7278 | New_Copy_Tree (Expression (Comp)))); | |
7279 | end if; | |
7280 | ||
7281 | Append (Stat, Aggr_Code); | |
7282 | ||
7283 | <<Next_Key>> | |
7284 | Next (Key); | |
7285 | end loop; | |
08c8883f | 7286 | |
7a21651f | 7287 | else |
08c8883f ES |
7288 | -- Iterated component association. Discard |
7289 | -- positional insertion procedure. | |
7290 | ||
a6ca70c6 VI |
7291 | if not Present (Iterator_Specification (Comp)) then |
7292 | Add_Named_Subp := Assign_Indexed_Subp; | |
7293 | Add_Unnamed_Subp := Empty; | |
7294 | end if; | |
7295 | ||
08c8883f | 7296 | Expand_Iterated_Component (Comp); |
7a21651f | 7297 | end if; |
08c8883f | 7298 | |
7a21651f ES |
7299 | Next (Comp); |
7300 | end loop; | |
7301 | end if; | |
7302 | end; | |
ce59f39f | 7303 | end if; |
4f6ebe2a | 7304 | |
ce59f39f GD |
7305 | Insert_Actions (N, Aggr_Code); |
7306 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
7307 | Analyze_And_Resolve (N, Typ); | |
745f5698 ES |
7308 | end Expand_Container_Aggregate; |
7309 | ||
9eb8d5b4 AC |
7310 | ------------------------------ |
7311 | -- Expand_N_Delta_Aggregate -- | |
7312 | ------------------------------ | |
7313 | ||
7314 | procedure Expand_N_Delta_Aggregate (N : Node_Id) is | |
9313a26a | 7315 | Loc : constant Source_Ptr := Sloc (N); |
c78efe92 | 7316 | Typ : constant Entity_Id := Etype (Expression (N)); |
9eb8d5b4 AC |
7317 | Decl : Node_Id; |
7318 | ||
7319 | begin | |
9313a26a AC |
7320 | Decl := |
7321 | Make_Object_Declaration (Loc, | |
7322 | Defining_Identifier => Make_Temporary (Loc, 'T'), | |
7323 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
7324 | Expression => New_Copy_Tree (Expression (N))); | |
9eb8d5b4 AC |
7325 | |
7326 | if Is_Array_Type (Etype (N)) then | |
7327 | Expand_Delta_Array_Aggregate (N, New_List (Decl)); | |
7328 | else | |
7329 | Expand_Delta_Record_Aggregate (N, New_List (Decl)); | |
7330 | end if; | |
7331 | end Expand_N_Delta_Aggregate; | |
7332 | ||
7333 | ---------------------------------- | |
7334 | -- Expand_Delta_Array_Aggregate -- | |
7335 | ---------------------------------- | |
7336 | ||
7337 | procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is | |
9313a26a AC |
7338 | Loc : constant Source_Ptr := Sloc (N); |
7339 | Temp : constant Entity_Id := Defining_Identifier (First (Deltas)); | |
7340 | Assoc : Node_Id; | |
7341 | ||
9eb8d5b4 AC |
7342 | function Generate_Loop (C : Node_Id) return Node_Id; |
7343 | -- Generate a loop containing individual component assignments for | |
7344 | -- choices that are ranges, subtype indications, subtype names, and | |
7345 | -- iterated component associations. | |
7346 | ||
9313a26a AC |
7347 | ------------------- |
7348 | -- Generate_Loop -- | |
7349 | ------------------- | |
7350 | ||
9eb8d5b4 AC |
7351 | function Generate_Loop (C : Node_Id) return Node_Id is |
7352 | Sl : constant Source_Ptr := Sloc (C); | |
7353 | Ix : Entity_Id; | |
7354 | ||
7355 | begin | |
7356 | if Nkind (Parent (C)) = N_Iterated_Component_Association then | |
7357 | Ix := | |
7358 | Make_Defining_Identifier (Loc, | |
7359 | Chars => (Chars (Defining_Identifier (Parent (C))))); | |
7360 | else | |
7361 | Ix := Make_Temporary (Sl, 'I'); | |
7362 | end if; | |
7363 | ||
7364 | return | |
f3561c06 | 7365 | Make_Implicit_Loop_Statement (C, |
9313a26a AC |
7366 | Iteration_Scheme => |
7367 | Make_Iteration_Scheme (Sl, | |
7368 | Loop_Parameter_Specification => | |
7369 | Make_Loop_Parameter_Specification (Sl, | |
7370 | Defining_Identifier => Ix, | |
7371 | Discrete_Subtype_Definition => New_Copy_Tree (C))), | |
7372 | ||
7373 | Statements => New_List ( | |
7374 | Make_Assignment_Statement (Sl, | |
7375 | Name => | |
7376 | Make_Indexed_Component (Sl, | |
9eb8d5b4 AC |
7377 | Prefix => New_Occurrence_Of (Temp, Sl), |
7378 | Expressions => New_List (New_Occurrence_Of (Ix, Sl))), | |
9313a26a AC |
7379 | Expression => New_Copy_Tree (Expression (Assoc)))), |
7380 | End_Label => Empty); | |
9eb8d5b4 AC |
7381 | end Generate_Loop; |
7382 | ||
9313a26a AC |
7383 | -- Local variables |
7384 | ||
7385 | Choice : Node_Id; | |
7386 | ||
7387 | -- Start of processing for Expand_Delta_Array_Aggregate | |
7388 | ||
9eb8d5b4 AC |
7389 | begin |
7390 | Assoc := First (Component_Associations (N)); | |
7391 | while Present (Assoc) loop | |
7392 | Choice := First (Choice_List (Assoc)); | |
7393 | if Nkind (Assoc) = N_Iterated_Component_Association then | |
7394 | while Present (Choice) loop | |
7395 | Append_To (Deltas, Generate_Loop (Choice)); | |
7396 | Next (Choice); | |
7397 | end loop; | |
7398 | ||
7399 | else | |
7400 | while Present (Choice) loop | |
7401 | ||
7402 | -- Choice can be given by a range, a subtype indication, a | |
7403 | -- subtype name, a scalar value, or an entity. | |
7404 | ||
7405 | if Nkind (Choice) = N_Range | |
7406 | or else (Is_Entity_Name (Choice) | |
9313a26a | 7407 | and then Is_Type (Entity (Choice))) |
9eb8d5b4 AC |
7408 | then |
7409 | Append_To (Deltas, Generate_Loop (Choice)); | |
7410 | ||
7411 | elsif Nkind (Choice) = N_Subtype_Indication then | |
7412 | Append_To (Deltas, | |
7413 | Generate_Loop (Range_Expression (Constraint (Choice)))); | |
7414 | ||
7415 | else | |
7416 | Append_To (Deltas, | |
9313a26a AC |
7417 | Make_Assignment_Statement (Sloc (Choice), |
7418 | Name => | |
7419 | Make_Indexed_Component (Sloc (Choice), | |
7420 | Prefix => New_Occurrence_Of (Temp, Loc), | |
7421 | Expressions => New_List (New_Copy_Tree (Choice))), | |
7422 | Expression => New_Copy_Tree (Expression (Assoc)))); | |
9eb8d5b4 AC |
7423 | end if; |
7424 | ||
7425 | Next (Choice); | |
7426 | end loop; | |
7427 | end if; | |
7428 | ||
7429 | Next (Assoc); | |
7430 | end loop; | |
7431 | ||
7432 | Insert_Actions (N, Deltas); | |
7433 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
7434 | end Expand_Delta_Array_Aggregate; | |
7435 | ||
7436 | ----------------------------------- | |
7437 | -- Expand_Delta_Record_Aggregate -- | |
7438 | ----------------------------------- | |
7439 | ||
7440 | procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is | |
7441 | Loc : constant Source_Ptr := Sloc (N); | |
7442 | Temp : constant Entity_Id := Defining_Identifier (First (Deltas)); | |
7443 | Assoc : Node_Id; | |
7444 | Choice : Node_Id; | |
7445 | ||
7446 | begin | |
7447 | Assoc := First (Component_Associations (N)); | |
7448 | ||
7449 | while Present (Assoc) loop | |
7450 | Choice := First (Choice_List (Assoc)); | |
7451 | while Present (Choice) loop | |
7452 | Append_To (Deltas, | |
9313a26a AC |
7453 | Make_Assignment_Statement (Sloc (Choice), |
7454 | Name => | |
7455 | Make_Selected_Component (Sloc (Choice), | |
7456 | Prefix => New_Occurrence_Of (Temp, Loc), | |
7457 | Selector_Name => Make_Identifier (Loc, Chars (Choice))), | |
7458 | Expression => New_Copy_Tree (Expression (Assoc)))); | |
9eb8d5b4 AC |
7459 | Next (Choice); |
7460 | end loop; | |
7461 | ||
7462 | Next (Assoc); | |
7463 | end loop; | |
7464 | ||
7465 | Insert_Actions (N, Deltas); | |
7466 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
7467 | end Expand_Delta_Record_Aggregate; | |
7468 | ||
70482933 RK |
7469 | ---------------------------------- |
7470 | -- Expand_N_Extension_Aggregate -- | |
7471 | ---------------------------------- | |
7472 | ||
7473 | -- If the ancestor part is an expression, add a component association for | |
7474 | -- the parent field. If the type of the ancestor part is not the direct | |
d4dfb005 BD |
7475 | -- parent of the expected type, build recursively the needed ancestors. |
7476 | -- If the ancestor part is a subtype_mark, replace aggregate with a | |
7477 | -- declaration for a temporary of the expected type, followed by | |
7478 | -- individual assignments to the given components. | |
70482933 RK |
7479 | |
7480 | procedure Expand_N_Extension_Aggregate (N : Node_Id) is | |
70482933 | 7481 | A : constant Node_Id := Ancestor_Part (N); |
3fc40cd7 | 7482 | Loc : constant Source_Ptr := Sloc (N); |
70482933 RK |
7483 | Typ : constant Entity_Id := Etype (N); |
7484 | ||
7485 | begin | |
fbf5a39b | 7486 | -- If the ancestor is a subtype mark, an init proc must be called |
70482933 RK |
7487 | -- on the resulting object which thus has to be materialized in |
7488 | -- the front-end | |
7489 | ||
7490 | if Is_Entity_Name (A) and then Is_Type (Entity (A)) then | |
7491 | Convert_To_Assignments (N, Typ); | |
7492 | ||
7493 | -- The extension aggregate is transformed into a record aggregate | |
7494 | -- of the following form (c1 and c2 are inherited components) | |
7495 | ||
7496 | -- (Exp with c3 => a, c4 => b) | |
0877856b | 7497 | -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b) |
70482933 RK |
7498 | |
7499 | else | |
7500 | Set_Etype (N, Typ); | |
7501 | ||
1f110335 | 7502 | if Tagged_Type_Expansion then |
70482933 | 7503 | Expand_Record_Aggregate (N, |
a9d8907c JM |
7504 | Orig_Tag => |
7505 | New_Occurrence_Of | |
7506 | (Node (First_Elmt (Access_Disp_Table (Typ))), Loc), | |
70482933 | 7507 | Parent_Expr => A); |
5c34e9cd AC |
7508 | |
7509 | -- No tag is needed in the case of a VM | |
7510 | ||
0f95b178 | 7511 | else |
5c34e9cd | 7512 | Expand_Record_Aggregate (N, Parent_Expr => A); |
70482933 RK |
7513 | end if; |
7514 | end if; | |
fbf5a39b AC |
7515 | |
7516 | exception | |
7517 | when RE_Not_Available => | |
7518 | return; | |
70482933 RK |
7519 | end Expand_N_Extension_Aggregate; |
7520 | ||
7521 | ----------------------------- | |
7522 | -- Expand_Record_Aggregate -- | |
7523 | ----------------------------- | |
7524 | ||
7525 | procedure Expand_Record_Aggregate | |
7526 | (N : Node_Id; | |
7527 | Orig_Tag : Node_Id := Empty; | |
7528 | Parent_Expr : Node_Id := Empty) | |
7529 | is | |
fbf5a39b AC |
7530 | Loc : constant Source_Ptr := Sloc (N); |
7531 | Comps : constant List_Id := Component_Associations (N); | |
7532 | Typ : constant Entity_Id := Etype (N); | |
7533 | Base_Typ : constant Entity_Id := Base_Type (Typ); | |
70482933 | 7534 | |
0f95b178 JM |
7535 | Static_Components : Boolean := True; |
7536 | -- Flag to indicate whether all components are compile-time known, | |
7537 | -- and the aggregate can be constructed statically and handled by | |
d4dfb005 | 7538 | -- the back-end. Set to False by Component_OK_For_Backend. |
70482933 | 7539 | |
54740d7d AC |
7540 | procedure Build_Back_End_Aggregate; |
7541 | -- Build a proper aggregate to be handled by the back-end | |
7542 | ||
f6205414 AC |
7543 | function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean; |
7544 | -- Returns true if N is an expression of composite type which can be | |
7545 | -- fully evaluated at compile time without raising constraint error. | |
7546 | -- Such expressions can be passed as is to Gigi without any expansion. | |
7547 | -- | |
7548 | -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate | |
7549 | -- set and constants whose expression is such an aggregate, recursively. | |
7550 | ||
d4dfb005 | 7551 | function Component_OK_For_Backend return Boolean; |
b465ef6f | 7552 | -- Check for presence of a component which makes it impossible for the |
0f95b178 JM |
7553 | -- backend to process the aggregate, thus requiring the use of a series |
7554 | -- of assignment statements. Cases checked for are a nested aggregate | |
7555 | -- needing Late_Expansion, the presence of a tagged component which may | |
7556 | -- need tag adjustment, and a bit unaligned component reference. | |
4a76b687 ES |
7557 | -- |
7558 | -- We also force expansion into assignments if a component is of a | |
7559 | -- mutable type (including a private type with discriminants) because | |
7560 | -- in that case the size of the component to be copied may be smaller | |
7561 | -- than the side of the target, and there is no simple way for gigi | |
7562 | -- to compute the size of the object to be copied. | |
7563 | -- | |
7564 | -- NOTE: This is part of the ongoing work to define precisely the | |
7565 | -- interface between front-end and back-end handling of aggregates. | |
7566 | -- In general it is desirable to pass aggregates as they are to gigi, | |
7567 | -- in order to minimize elaboration code. This is one case where the | |
7568 | -- semantics of Ada complicate the analysis and lead to anomalies in | |
7569 | -- the gcc back-end if the aggregate is not expanded into assignments. | |
d4dfb005 BD |
7570 | -- |
7571 | -- NOTE: This sets the global Static_Components to False in most, but | |
7572 | -- not all, cases when it returns False. | |
70482933 | 7573 | |
9b7924dd AC |
7574 | function Has_Per_Object_Constraint (L : List_Id) return Boolean; |
7575 | -- Return True if any element of L has Has_Per_Object_Constraint set. | |
7576 | -- L should be the Choices component of an N_Component_Association. | |
7577 | ||
57a8057a AC |
7578 | function Has_Visible_Private_Ancestor (Id : E) return Boolean; |
7579 | -- If any ancestor of the current type is private, the aggregate | |
b465ef6f | 7580 | -- cannot be built in place. We cannot rely on Has_Private_Ancestor, |
57a8057a AC |
7581 | -- because it will not be set when type and its parent are in the |
7582 | -- same scope, and the parent component needs expansion. | |
7583 | ||
7584 | function Top_Level_Aggregate (N : Node_Id) return Node_Id; | |
7585 | -- For nested aggregates return the ultimate enclosing aggregate; for | |
7586 | -- non-nested aggregates return N. | |
7587 | ||
54740d7d AC |
7588 | ------------------------------ |
7589 | -- Build_Back_End_Aggregate -- | |
7590 | ------------------------------ | |
f6205414 | 7591 | |
54740d7d | 7592 | procedure Build_Back_End_Aggregate is |
4f94fa11 AC |
7593 | Comp : Entity_Id; |
7594 | New_Comp : Node_Id; | |
7595 | Tag_Value : Node_Id; | |
57a8057a AC |
7596 | |
7597 | begin | |
0f95b178 JM |
7598 | if Nkind (N) = N_Aggregate then |
7599 | ||
3b9fa2df ES |
7600 | -- If the aggregate is static and can be handled by the back-end, |
7601 | -- nothing left to do. | |
0f95b178 JM |
7602 | |
7603 | if Static_Components then | |
7604 | Set_Compile_Time_Known_Aggregate (N); | |
7605 | Set_Expansion_Delayed (N, False); | |
7606 | end if; | |
7607 | end if; | |
7608 | ||
07fc65c4 | 7609 | -- If no discriminants, nothing special to do |
70482933 | 7610 | |
07fc65c4 | 7611 | if not Has_Discriminants (Typ) then |
70482933 RK |
7612 | null; |
7613 | ||
07fc65c4 GB |
7614 | -- Case of discriminants present |
7615 | ||
70482933 RK |
7616 | elsif Is_Derived_Type (Typ) then |
7617 | ||
138fc6f1 HK |
7618 | -- For untagged types, non-stored discriminants are replaced with |
7619 | -- stored discriminants, which are the ones that gigi uses to | |
7620 | -- describe the type and its components. | |
70482933 | 7621 | |
07fc65c4 | 7622 | Generate_Aggregate_For_Derived_Type : declare |
fbf5a39b | 7623 | procedure Prepend_Stored_Values (T : Entity_Id); |
3b9fa2df ES |
7624 | -- Scan the list of stored discriminants of the type, and add |
7625 | -- their values to the aggregate being built. | |
07fc65c4 GB |
7626 | |
7627 | --------------------------- | |
fbf5a39b | 7628 | -- Prepend_Stored_Values -- |
07fc65c4 GB |
7629 | --------------------------- |
7630 | ||
fbf5a39b | 7631 | procedure Prepend_Stored_Values (T : Entity_Id) is |
54740d7d AC |
7632 | Discr : Entity_Id; |
7633 | First_Comp : Node_Id := Empty; | |
7634 | ||
07fc65c4 | 7635 | begin |
54740d7d AC |
7636 | Discr := First_Stored_Discriminant (T); |
7637 | while Present (Discr) loop | |
07fc65c4 GB |
7638 | New_Comp := |
7639 | Make_Component_Association (Loc, | |
138fc6f1 | 7640 | Choices => New_List ( |
54740d7d | 7641 | New_Occurrence_Of (Discr, Loc)), |
07fc65c4 | 7642 | Expression => |
bdc193ba AC |
7643 | New_Copy_Tree |
7644 | (Get_Discriminant_Value | |
54740d7d | 7645 | (Discr, |
07fc65c4 GB |
7646 | Typ, |
7647 | Discriminant_Constraint (Typ)))); | |
7648 | ||
7649 | if No (First_Comp) then | |
7650 | Prepend_To (Component_Associations (N), New_Comp); | |
7651 | else | |
7652 | Insert_After (First_Comp, New_Comp); | |
7653 | end if; | |
7654 | ||
7655 | First_Comp := New_Comp; | |
54740d7d | 7656 | Next_Stored_Discriminant (Discr); |
07fc65c4 | 7657 | end loop; |
fbf5a39b | 7658 | end Prepend_Stored_Values; |
07fc65c4 | 7659 | |
54740d7d AC |
7660 | -- Local variables |
7661 | ||
7662 | Constraints : constant List_Id := New_List; | |
7663 | ||
7664 | Discr : Entity_Id; | |
7665 | Decl : Node_Id; | |
7666 | Num_Disc : Nat := 0; | |
81a0f4a3 | 7667 | Num_Stor : Nat := 0; |
54740d7d | 7668 | |
07fc65c4 | 7669 | -- Start of processing for Generate_Aggregate_For_Derived_Type |
70482933 RK |
7670 | |
7671 | begin | |
3b9fa2df | 7672 | -- Remove the associations for the discriminant of derived type |
70482933 | 7673 | |
54740d7d AC |
7674 | declare |
7675 | First_Comp : Node_Id; | |
70482933 | 7676 | |
54740d7d AC |
7677 | begin |
7678 | First_Comp := First (Component_Associations (N)); | |
7679 | while Present (First_Comp) loop | |
7680 | Comp := First_Comp; | |
7681 | Next (First_Comp); | |
7682 | ||
7683 | if Ekind (Entity (First (Choices (Comp)))) = | |
7684 | E_Discriminant | |
7685 | then | |
7686 | Remove (Comp); | |
7687 | Num_Disc := Num_Disc + 1; | |
7688 | end if; | |
7689 | end loop; | |
7690 | end; | |
70482933 | 7691 | |
fbf5a39b AC |
7692 | -- Insert stored discriminant associations in the correct |
7693 | -- order. If there are more stored discriminants than new | |
3b9fa2df ES |
7694 | -- discriminants, there is at least one new discriminant that |
7695 | -- constrains more than one of the stored discriminants. In | |
7696 | -- this case we need to construct a proper subtype of the | |
7697 | -- parent type, in order to supply values to all the | |
fbf5a39b AC |
7698 | -- components. Otherwise there is one-one correspondence |
7699 | -- between the constraints and the stored discriminants. | |
70482933 | 7700 | |
54740d7d AC |
7701 | Discr := First_Stored_Discriminant (Base_Type (Typ)); |
7702 | while Present (Discr) loop | |
81a0f4a3 | 7703 | Num_Stor := Num_Stor + 1; |
54740d7d | 7704 | Next_Stored_Discriminant (Discr); |
70482933 | 7705 | end loop; |
07fc65c4 | 7706 | |
fbf5a39b | 7707 | -- Case of more stored discriminants than new discriminants |
07fc65c4 | 7708 | |
81a0f4a3 | 7709 | if Num_Stor > Num_Disc then |
07fc65c4 | 7710 | |
3b9fa2df ES |
7711 | -- Create a proper subtype of the parent type, which is the |
7712 | -- proper implementation type for the aggregate, and convert | |
7713 | -- it to the intended target type. | |
07fc65c4 | 7714 | |
54740d7d AC |
7715 | Discr := First_Stored_Discriminant (Base_Type (Typ)); |
7716 | while Present (Discr) loop | |
07fc65c4 | 7717 | New_Comp := |
37368818 RD |
7718 | New_Copy_Tree |
7719 | (Get_Discriminant_Value | |
54740d7d | 7720 | (Discr, |
bdc193ba AC |
7721 | Typ, |
7722 | Discriminant_Constraint (Typ))); | |
138fc6f1 | 7723 | |
07fc65c4 | 7724 | Append (New_Comp, Constraints); |
54740d7d | 7725 | Next_Stored_Discriminant (Discr); |
07fc65c4 GB |
7726 | end loop; |
7727 | ||
7728 | Decl := | |
7729 | Make_Subtype_Declaration (Loc, | |
191fcb3a | 7730 | Defining_Identifier => Make_Temporary (Loc, 'T'), |
bdc193ba | 7731 | Subtype_Indication => |
07fc65c4 GB |
7732 | Make_Subtype_Indication (Loc, |
7733 | Subtype_Mark => | |
7734 | New_Occurrence_Of (Etype (Base_Type (Typ)), Loc), | |
bdc193ba | 7735 | Constraint => |
07fc65c4 GB |
7736 | Make_Index_Or_Discriminant_Constraint |
7737 | (Loc, Constraints))); | |
7738 | ||
7739 | Insert_Action (N, Decl); | |
fbf5a39b | 7740 | Prepend_Stored_Values (Base_Type (Typ)); |
07fc65c4 GB |
7741 | |
7742 | Set_Etype (N, Defining_Identifier (Decl)); | |
7743 | Set_Analyzed (N); | |
7744 | ||
7745 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
7746 | Analyze (N); | |
7747 | ||
7748 | -- Case where we do not have fewer new discriminants than | |
3b9fa2df ES |
7749 | -- stored discriminants, so in this case we can simply use the |
7750 | -- stored discriminants of the subtype. | |
07fc65c4 GB |
7751 | |
7752 | else | |
fbf5a39b | 7753 | Prepend_Stored_Values (Typ); |
07fc65c4 GB |
7754 | end if; |
7755 | end Generate_Aggregate_For_Derived_Type; | |
70482933 RK |
7756 | end if; |
7757 | ||
7758 | if Is_Tagged_Type (Typ) then | |
7759 | ||
22243c12 | 7760 | -- In the tagged case, _parent and _tag component must be created |
70482933 | 7761 | |
22243c12 RD |
7762 | -- Reset Null_Present unconditionally. Tagged records always have |
7763 | -- at least one field (the tag or the parent). | |
70482933 RK |
7764 | |
7765 | Set_Null_Record_Present (N, False); | |
7766 | ||
7767 | -- When the current aggregate comes from the expansion of an | |
7768 | -- extension aggregate, the parent expr is replaced by an | |
22243c12 | 7769 | -- aggregate formed by selected components of this expr. |
70482933 | 7770 | |
36a66365 | 7771 | if Present (Parent_Expr) and then Is_Empty_List (Comps) then |
5277cab6 | 7772 | Comp := First_Component_Or_Discriminant (Typ); |
70482933 RK |
7773 | while Present (Comp) loop |
7774 | ||
70482933 RK |
7775 | -- Skip all expander-generated components |
7776 | ||
bdc193ba | 7777 | if not Comes_From_Source (Original_Record_Component (Comp)) |
70482933 RK |
7778 | then |
7779 | null; | |
7780 | ||
7781 | else | |
7782 | New_Comp := | |
7783 | Make_Selected_Component (Loc, | |
bdc193ba | 7784 | Prefix => |
70482933 RK |
7785 | Unchecked_Convert_To (Typ, |
7786 | Duplicate_Subexpr (Parent_Expr, True)), | |
70482933 RK |
7787 | Selector_Name => New_Occurrence_Of (Comp, Loc)); |
7788 | ||
7789 | Append_To (Comps, | |
7790 | Make_Component_Association (Loc, | |
54740d7d AC |
7791 | Choices => New_List ( |
7792 | New_Occurrence_Of (Comp, Loc)), | |
37368818 | 7793 | Expression => New_Comp)); |
70482933 RK |
7794 | |
7795 | Analyze_And_Resolve (New_Comp, Etype (Comp)); | |
7796 | end if; | |
7797 | ||
5277cab6 | 7798 | Next_Component_Or_Discriminant (Comp); |
70482933 RK |
7799 | end loop; |
7800 | end if; | |
7801 | ||
7802 | -- Compute the value for the Tag now, if the type is a root it | |
7803 | -- will be included in the aggregate right away, otherwise it will | |
22243c12 | 7804 | -- be propagated to the parent aggregate. |
70482933 RK |
7805 | |
7806 | if Present (Orig_Tag) then | |
7807 | Tag_Value := Orig_Tag; | |
54740d7d | 7808 | |
1f110335 | 7809 | elsif not Tagged_Type_Expansion then |
70482933 | 7810 | Tag_Value := Empty; |
54740d7d | 7811 | |
70482933 | 7812 | else |
a9d8907c JM |
7813 | Tag_Value := |
7814 | New_Occurrence_Of | |
7815 | (Node (First_Elmt (Access_Disp_Table (Typ))), Loc); | |
70482933 RK |
7816 | end if; |
7817 | ||
7818 | -- For a derived type, an aggregate for the parent is formed with | |
7819 | -- all the inherited components. | |
7820 | ||
7821 | if Is_Derived_Type (Typ) then | |
70482933 RK |
7822 | declare |
7823 | First_Comp : Node_Id; | |
7824 | Parent_Comps : List_Id; | |
7825 | Parent_Aggr : Node_Id; | |
7826 | Parent_Name : Node_Id; | |
7827 | ||
7828 | begin | |
54740d7d | 7829 | First_Comp := First (Component_Associations (N)); |
70482933 | 7830 | Parent_Comps := New_List; |
567bf449 EB |
7831 | |
7832 | -- First skip the discriminants | |
7833 | ||
7834 | while Present (First_Comp) | |
7835 | and then Ekind (Entity (First (Choices (First_Comp)))) | |
7836 | = E_Discriminant | |
7837 | loop | |
7838 | Next (First_Comp); | |
7839 | end loop; | |
7840 | ||
7841 | -- Then remove the inherited component association from the | |
7842 | -- aggregate and store them in the parent aggregate | |
7843 | ||
70482933 | 7844 | while Present (First_Comp) |
36a66365 AC |
7845 | and then |
7846 | Scope (Original_Record_Component | |
7847 | (Entity (First (Choices (First_Comp))))) /= | |
7848 | Base_Typ | |
70482933 RK |
7849 | loop |
7850 | Comp := First_Comp; | |
7851 | Next (First_Comp); | |
7852 | Remove (Comp); | |
7853 | Append (Comp, Parent_Comps); | |
7854 | end loop; | |
7855 | ||
36a66365 AC |
7856 | Parent_Aggr := |
7857 | Make_Aggregate (Loc, | |
7858 | Component_Associations => Parent_Comps); | |
70482933 RK |
7859 | Set_Etype (Parent_Aggr, Etype (Base_Type (Typ))); |
7860 | ||
7861 | -- Find the _parent component | |
7862 | ||
7863 | Comp := First_Component (Typ); | |
7864 | while Chars (Comp) /= Name_uParent loop | |
99859ea7 | 7865 | Next_Component (Comp); |
70482933 RK |
7866 | end loop; |
7867 | ||
7868 | Parent_Name := New_Occurrence_Of (Comp, Loc); | |
7869 | ||
7870 | -- Insert the parent aggregate | |
7871 | ||
7872 | Prepend_To (Component_Associations (N), | |
7873 | Make_Component_Association (Loc, | |
7874 | Choices => New_List (Parent_Name), | |
7875 | Expression => Parent_Aggr)); | |
7876 | ||
7877 | -- Expand recursively the parent propagating the right Tag | |
7878 | ||
22243c12 RD |
7879 | Expand_Record_Aggregate |
7880 | (Parent_Aggr, Tag_Value, Parent_Expr); | |
1b6897ce AC |
7881 | |
7882 | -- The ancestor part may be a nested aggregate that has | |
7883 | -- delayed expansion: recheck now. | |
7884 | ||
d4dfb005 | 7885 | if not Component_OK_For_Backend then |
1b6897ce AC |
7886 | Convert_To_Assignments (N, Typ); |
7887 | end if; | |
70482933 RK |
7888 | end; |
7889 | ||
7890 | -- For a root type, the tag component is added (unless compiling | |
0f95b178 | 7891 | -- for the VMs, where tags are implicit). |
70482933 | 7892 | |
1f110335 | 7893 | elsif Tagged_Type_Expansion then |
70482933 RK |
7894 | declare |
7895 | Tag_Name : constant Node_Id := | |
138fc6f1 HK |
7896 | New_Occurrence_Of |
7897 | (First_Tag_Component (Typ), Loc); | |
70482933 RK |
7898 | Typ_Tag : constant Entity_Id := RTE (RE_Tag); |
7899 | Conv_Node : constant Node_Id := | |
138fc6f1 | 7900 | Unchecked_Convert_To (Typ_Tag, Tag_Value); |
70482933 RK |
7901 | |
7902 | begin | |
7903 | Set_Etype (Conv_Node, Typ_Tag); | |
7904 | Prepend_To (Component_Associations (N), | |
7905 | Make_Component_Association (Loc, | |
7906 | Choices => New_List (Tag_Name), | |
7907 | Expression => Conv_Node)); | |
7908 | end; | |
7909 | end if; | |
7910 | end if; | |
54740d7d AC |
7911 | end Build_Back_End_Aggregate; |
7912 | ||
7913 | ---------------------------------------- | |
7914 | -- Compile_Time_Known_Composite_Value -- | |
7915 | ---------------------------------------- | |
7916 | ||
7917 | function Compile_Time_Known_Composite_Value | |
7918 | (N : Node_Id) return Boolean | |
7919 | is | |
7920 | begin | |
7921 | -- If we have an entity name, then see if it is the name of a | |
7922 | -- constant and if so, test the corresponding constant value. | |
7923 | ||
7924 | if Is_Entity_Name (N) then | |
7925 | declare | |
7926 | E : constant Entity_Id := Entity (N); | |
7927 | V : Node_Id; | |
7928 | begin | |
7929 | if Ekind (E) /= E_Constant then | |
7930 | return False; | |
7931 | else | |
7932 | V := Constant_Value (E); | |
7933 | return Present (V) | |
7934 | and then Compile_Time_Known_Composite_Value (V); | |
7935 | end if; | |
7936 | end; | |
7937 | ||
7938 | -- We have a value, see if it is compile time known | |
7939 | ||
7940 | else | |
7941 | if Nkind (N) = N_Aggregate then | |
7942 | return Compile_Time_Known_Aggregate (N); | |
7943 | end if; | |
7944 | ||
7945 | -- All other types of values are not known at compile time | |
7946 | ||
7947 | return False; | |
7948 | end if; | |
7949 | ||
7950 | end Compile_Time_Known_Composite_Value; | |
7951 | ||
d4dfb005 BD |
7952 | ------------------------------ |
7953 | -- Component_OK_For_Backend -- | |
7954 | ------------------------------ | |
54740d7d | 7955 | |
d4dfb005 | 7956 | function Component_OK_For_Backend return Boolean is |
54740d7d AC |
7957 | C : Node_Id; |
7958 | Expr_Q : Node_Id; | |
7959 | ||
7960 | begin | |
54740d7d AC |
7961 | C := First (Comps); |
7962 | while Present (C) loop | |
7963 | ||
7964 | -- If the component has box initialization, expansion is needed | |
7965 | -- and component is not ready for backend. | |
7966 | ||
7967 | if Box_Present (C) then | |
d4dfb005 | 7968 | return False; |
54740d7d AC |
7969 | end if; |
7970 | ||
4f061cf2 | 7971 | Expr_Q := Unqualify (Expression (C)); |
54740d7d | 7972 | |
f4c26077 ES |
7973 | -- Return False for array components whose bounds raise |
7974 | -- constraint error. | |
7975 | ||
7976 | declare | |
61770974 | 7977 | Comp : constant Entity_Id := First (Choices (C)); |
f4c26077 ES |
7978 | Indx : Node_Id; |
7979 | ||
7980 | begin | |
f4c26077 ES |
7981 | if Present (Etype (Comp)) |
7982 | and then Is_Array_Type (Etype (Comp)) | |
7983 | then | |
7984 | Indx := First_Index (Etype (Comp)); | |
f4c26077 | 7985 | while Present (Indx) loop |
61770974 HK |
7986 | if Nkind (Type_Low_Bound (Etype (Indx))) = |
7987 | N_Raise_Constraint_Error | |
7988 | or else Nkind (Type_High_Bound (Etype (Indx))) = | |
7989 | N_Raise_Constraint_Error | |
f4c26077 ES |
7990 | then |
7991 | return False; | |
7992 | end if; | |
7993 | ||
99859ea7 | 7994 | Next_Index (Indx); |
f4c26077 ES |
7995 | end loop; |
7996 | end if; | |
7997 | end; | |
7998 | ||
d4dfb005 | 7999 | -- Return False if the aggregate has any associations for tagged |
54740d7d AC |
8000 | -- components that may require tag adjustment. |
8001 | ||
8002 | -- These are cases where the source expression may have a tag that | |
8003 | -- could differ from the component tag (e.g., can occur for type | |
8004 | -- conversions and formal parameters). (Tag adjustment not needed | |
8005 | -- if Tagged_Type_Expansion because object tags are implicit in | |
8006 | -- the machine.) | |
8007 | ||
8008 | if Is_Tagged_Type (Etype (Expr_Q)) | |
61770974 HK |
8009 | and then |
8010 | (Nkind (Expr_Q) = N_Type_Conversion | |
8011 | or else | |
8012 | (Is_Entity_Name (Expr_Q) | |
bb6a856b | 8013 | and then Is_Formal (Entity (Expr_Q)))) |
54740d7d AC |
8014 | and then Tagged_Type_Expansion |
8015 | then | |
8016 | Static_Components := False; | |
d4dfb005 | 8017 | return False; |
54740d7d AC |
8018 | |
8019 | elsif Is_Delayed_Aggregate (Expr_Q) then | |
8020 | Static_Components := False; | |
d4dfb005 | 8021 | return False; |
54740d7d | 8022 | |
1f6237e3 ES |
8023 | elsif Nkind (Expr_Q) = N_Quantified_Expression then |
8024 | Static_Components := False; | |
8025 | return False; | |
8026 | ||
54740d7d AC |
8027 | elsif Possible_Bit_Aligned_Component (Expr_Q) then |
8028 | Static_Components := False; | |
d4dfb005 | 8029 | return False; |
54740d7d AC |
8030 | |
8031 | elsif Modify_Tree_For_C | |
8032 | and then Nkind (C) = N_Component_Association | |
8033 | and then Has_Per_Object_Constraint (Choices (C)) | |
8034 | then | |
8035 | Static_Components := False; | |
d4dfb005 | 8036 | return False; |
54740d7d AC |
8037 | |
8038 | elsif Modify_Tree_For_C | |
8039 | and then Nkind (Expr_Q) = N_Identifier | |
8040 | and then Is_Array_Type (Etype (Expr_Q)) | |
8041 | then | |
8042 | Static_Components := False; | |
d4dfb005 | 8043 | return False; |
b276ab7a AC |
8044 | |
8045 | elsif Modify_Tree_For_C | |
8046 | and then Nkind (Expr_Q) = N_Type_Conversion | |
8047 | and then Is_Array_Type (Etype (Expr_Q)) | |
8048 | then | |
8049 | Static_Components := False; | |
d4dfb005 | 8050 | return False; |
54740d7d AC |
8051 | end if; |
8052 | ||
8053 | if Is_Elementary_Type (Etype (Expr_Q)) then | |
8054 | if not Compile_Time_Known_Value (Expr_Q) then | |
8055 | Static_Components := False; | |
8056 | end if; | |
8057 | ||
8058 | elsif not Compile_Time_Known_Composite_Value (Expr_Q) then | |
8059 | Static_Components := False; | |
8060 | ||
8061 | if Is_Private_Type (Etype (Expr_Q)) | |
8062 | and then Has_Discriminants (Etype (Expr_Q)) | |
8063 | then | |
d4dfb005 | 8064 | return False; |
54740d7d AC |
8065 | end if; |
8066 | end if; | |
8067 | ||
8068 | Next (C); | |
8069 | end loop; | |
8070 | ||
d4dfb005 BD |
8071 | return True; |
8072 | end Component_OK_For_Backend; | |
54740d7d AC |
8073 | |
8074 | ------------------------------- | |
8075 | -- Has_Per_Object_Constraint -- | |
8076 | ------------------------------- | |
8077 | ||
8078 | function Has_Per_Object_Constraint (L : List_Id) return Boolean is | |
8079 | N : Node_Id := First (L); | |
8080 | begin | |
8081 | while Present (N) loop | |
8082 | if Is_Entity_Name (N) | |
8083 | and then Present (Entity (N)) | |
8084 | and then Has_Per_Object_Constraint (Entity (N)) | |
8085 | then | |
8086 | return True; | |
8087 | end if; | |
8088 | ||
8089 | Next (N); | |
8090 | end loop; | |
8091 | ||
8092 | return False; | |
8093 | end Has_Per_Object_Constraint; | |
8094 | ||
8095 | ----------------------------------- | |
8096 | -- Has_Visible_Private_Ancestor -- | |
8097 | ----------------------------------- | |
8098 | ||
8099 | function Has_Visible_Private_Ancestor (Id : E) return Boolean is | |
8100 | R : constant Entity_Id := Root_Type (Id); | |
8101 | T1 : Entity_Id := Id; | |
8102 | ||
8103 | begin | |
8104 | loop | |
8105 | if Is_Private_Type (T1) then | |
8106 | return True; | |
8107 | ||
8108 | elsif T1 = R then | |
8109 | return False; | |
8110 | ||
8111 | else | |
8112 | T1 := Etype (T1); | |
8113 | end if; | |
8114 | end loop; | |
8115 | end Has_Visible_Private_Ancestor; | |
4f94fa11 AC |
8116 | |
8117 | ------------------------- | |
8118 | -- Top_Level_Aggregate -- | |
8119 | ------------------------- | |
8120 | ||
8121 | function Top_Level_Aggregate (N : Node_Id) return Node_Id is | |
8122 | Aggr : Node_Id; | |
8123 | ||
8124 | begin | |
8125 | Aggr := N; | |
8126 | while Present (Parent (Aggr)) | |
4a08c95c AC |
8127 | and then Nkind (Parent (Aggr)) in |
8128 | N_Aggregate | N_Component_Association | |
4f94fa11 AC |
8129 | loop |
8130 | Aggr := Parent (Aggr); | |
8131 | end loop; | |
8132 | ||
8133 | return Aggr; | |
8134 | end Top_Level_Aggregate; | |
8135 | ||
8136 | -- Local variables | |
8137 | ||
8138 | Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N); | |
8139 | ||
8140 | -- Start of processing for Expand_Record_Aggregate | |
8141 | ||
8142 | begin | |
4f94fa11 AC |
8143 | -- No special management required for aggregates used to initialize |
8144 | -- statically allocated dispatch tables | |
8145 | ||
c061e99b | 8146 | if Is_Static_Dispatch_Table_Aggregate (N) then |
4f94fa11 | 8147 | return; |
e1dfbb03 SB |
8148 | |
8149 | -- Case pattern aggregates need to remain as aggregates | |
8150 | ||
8151 | elsif Is_Case_Choice_Pattern (N) then | |
8152 | return; | |
70482933 | 8153 | end if; |
0f95b178 | 8154 | |
8973b987 | 8155 | -- If the pragma Aggregate_Individually_Assign is set, always convert to |
efc00a88 PB |
8156 | -- assignments. |
8157 | ||
8158 | if Aggregate_Individually_Assign then | |
8159 | Convert_To_Assignments (N, Typ); | |
8160 | ||
4f94fa11 AC |
8161 | -- Ada 2005 (AI-318-2): We need to convert to assignments if components |
8162 | -- are build-in-place function calls. The assignments will each turn | |
8163 | -- into a build-in-place function call. If components are all static, | |
d4dfb005 | 8164 | -- we can pass the aggregate to the back end regardless of limitedness. |
4f94fa11 AC |
8165 | |
8166 | -- Extension aggregates, aggregates in extended return statements, and | |
8167 | -- aggregates for C++ imported types must be expanded. | |
8168 | ||
efc00a88 | 8169 | elsif Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then |
4a08c95c AC |
8170 | if Nkind (Parent (N)) not in |
8171 | N_Component_Association | N_Object_Declaration | |
4f94fa11 AC |
8172 | then |
8173 | Convert_To_Assignments (N, Typ); | |
8174 | ||
8175 | elsif Nkind (N) = N_Extension_Aggregate | |
8176 | or else Convention (Typ) = Convention_CPP | |
8177 | then | |
8178 | Convert_To_Assignments (N, Typ); | |
8179 | ||
8180 | elsif not Size_Known_At_Compile_Time (Typ) | |
d4dfb005 | 8181 | or else not Component_OK_For_Backend |
4f94fa11 AC |
8182 | or else not Static_Components |
8183 | then | |
8184 | Convert_To_Assignments (N, Typ); | |
8185 | ||
8186 | -- In all other cases, build a proper aggregate to be handled by | |
23a9215f | 8187 | -- the back-end. |
4f94fa11 AC |
8188 | |
8189 | else | |
54740d7d | 8190 | Build_Back_End_Aggregate; |
4f94fa11 AC |
8191 | end if; |
8192 | ||
8193 | -- Gigi doesn't properly handle temporaries of variable size so we | |
8194 | -- generate it in the front-end | |
8195 | ||
8196 | elsif not Size_Known_At_Compile_Time (Typ) | |
8197 | and then Tagged_Type_Expansion | |
8198 | then | |
8199 | Convert_To_Assignments (N, Typ); | |
8200 | ||
8201 | -- An aggregate used to initialize a controlled object must be turned | |
8202 | -- into component assignments as the components themselves may require | |
8203 | -- finalization actions such as adjustment. | |
8204 | ||
8205 | elsif Needs_Finalization (Typ) then | |
8206 | Convert_To_Assignments (N, Typ); | |
8207 | ||
8208 | -- Ada 2005 (AI-287): In case of default initialized components we | |
8209 | -- convert the aggregate into assignments. | |
8210 | ||
8211 | elsif Has_Default_Init_Comps (N) then | |
8212 | Convert_To_Assignments (N, Typ); | |
8213 | ||
8214 | -- Check components | |
8215 | ||
d4dfb005 | 8216 | elsif not Component_OK_For_Backend then |
4f94fa11 AC |
8217 | Convert_To_Assignments (N, Typ); |
8218 | ||
8219 | -- If an ancestor is private, some components are not inherited and we | |
8220 | -- cannot expand into a record aggregate. | |
8221 | ||
8222 | elsif Has_Visible_Private_Ancestor (Typ) then | |
8223 | Convert_To_Assignments (N, Typ); | |
8224 | ||
8225 | -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi | |
8226 | -- is not able to handle the aggregate for Late_Request. | |
8227 | ||
8228 | elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then | |
8229 | Convert_To_Assignments (N, Typ); | |
8230 | ||
8231 | -- If the tagged types covers interface types we need to initialize all | |
8232 | -- hidden components containing pointers to secondary dispatch tables. | |
8233 | ||
8234 | elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then | |
8235 | Convert_To_Assignments (N, Typ); | |
8236 | ||
8237 | -- If some components are mutable, the size of the aggregate component | |
8238 | -- may be distinct from the default size of the type component, so | |
8239 | -- we need to expand to insure that the back-end copies the proper | |
8240 | -- size of the data. However, if the aggregate is the initial value of | |
8241 | -- a constant, the target is immutable and might be built statically | |
8242 | -- if components are appropriate. | |
8243 | ||
8244 | elsif Has_Mutable_Components (Typ) | |
8245 | and then | |
8246 | (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration | |
8247 | or else not Constant_Present (Parent (Top_Level_Aggr)) | |
8248 | or else not Static_Components) | |
8249 | then | |
8250 | Convert_To_Assignments (N, Typ); | |
8251 | ||
8252 | -- If the type involved has bit aligned components, then we are not sure | |
8253 | -- that the back end can handle this case correctly. | |
8254 | ||
8255 | elsif Type_May_Have_Bit_Aligned_Components (Typ) then | |
8256 | Convert_To_Assignments (N, Typ); | |
8257 | ||
8258 | -- When generating C, only generate an aggregate when declaring objects | |
8259 | -- since C does not support aggregates in e.g. assignment statements. | |
8260 | ||
9f51b855 | 8261 | elsif Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then |
4f94fa11 AC |
8262 | Convert_To_Assignments (N, Typ); |
8263 | ||
8264 | -- In all other cases, build a proper aggregate to be handled by gigi | |
8265 | ||
8266 | else | |
54740d7d | 8267 | Build_Back_End_Aggregate; |
4f94fa11 | 8268 | end if; |
70482933 RK |
8269 | end Expand_Record_Aggregate; |
8270 | ||
a80b1eb7 EB |
8271 | --------------------- |
8272 | -- Get_Base_Object -- | |
8273 | --------------------- | |
8274 | ||
8275 | function Get_Base_Object (N : Node_Id) return Entity_Id is | |
8276 | R : Node_Id; | |
8277 | ||
8278 | begin | |
8279 | R := Get_Referenced_Object (N); | |
8280 | ||
4a08c95c | 8281 | while Nkind (R) in N_Indexed_Component | N_Selected_Component | N_Slice |
a80b1eb7 EB |
8282 | loop |
8283 | R := Get_Referenced_Object (Prefix (R)); | |
8284 | end loop; | |
8285 | ||
8286 | if Is_Entity_Name (R) and then Is_Object (Entity (R)) then | |
8287 | return Entity (R); | |
8288 | else | |
8289 | return Empty; | |
8290 | end if; | |
8291 | end Get_Base_Object; | |
8292 | ||
65356e64 AC |
8293 | ---------------------------- |
8294 | -- Has_Default_Init_Comps -- | |
8295 | ---------------------------- | |
8296 | ||
8297 | function Has_Default_Init_Comps (N : Node_Id) return Boolean is | |
b52e1520 | 8298 | Assoc : Node_Id; |
c45b6ae0 | 8299 | Expr : Node_Id; |
b52e1520 | 8300 | -- Component association and expression, respectively |
bdc193ba | 8301 | |
65356e64 | 8302 | begin |
4a08c95c | 8303 | pragma Assert (Nkind (N) in N_Aggregate | N_Extension_Aggregate); |
c45b6ae0 | 8304 | |
c5ee5ad2 BD |
8305 | if Has_Self_Reference (N) then |
8306 | return True; | |
8307 | end if; | |
8308 | ||
b52e1520 PT |
8309 | Assoc := First (Component_Associations (N)); |
8310 | while Present (Assoc) loop | |
8311 | -- Each component association has either a box or an expression | |
c45b6ae0 | 8312 | |
b52e1520 | 8313 | pragma Assert (Box_Present (Assoc) xor Present (Expression (Assoc))); |
65356e64 | 8314 | |
b52e1520 | 8315 | -- Check if any direct component has default initialized components |
c45b6ae0 | 8316 | |
b52e1520 PT |
8317 | if Box_Present (Assoc) then |
8318 | return True; | |
c45b6ae0 | 8319 | |
b52e1520 | 8320 | -- Recursive call in case of aggregate expression |
c45b6ae0 | 8321 | |
b52e1520 PT |
8322 | else |
8323 | Expr := Expression (Assoc); | |
8324 | ||
8325 | if Nkind (Expr) in N_Aggregate | N_Extension_Aggregate | |
8326 | and then Has_Default_Init_Comps (Expr) | |
8327 | then | |
8328 | return True; | |
8329 | end if; | |
c45b6ae0 AC |
8330 | end if; |
8331 | ||
b52e1520 | 8332 | Next (Assoc); |
c45b6ae0 AC |
8333 | end loop; |
8334 | ||
65356e64 AC |
8335 | return False; |
8336 | end Has_Default_Init_Comps; | |
8337 | ||
fc84947c EB |
8338 | -------------------------- |
8339 | -- Initialize_Component -- | |
8340 | -------------------------- | |
8341 | ||
8342 | procedure Initialize_Component | |
fc84947c EB |
8343 | (N : Node_Id; |
8344 | Comp : Node_Id; | |
8345 | Comp_Typ : Node_Id; | |
8346 | Init_Expr : Node_Id; | |
8347 | Stmts : List_Id) | |
8348 | is | |
8349 | Exceptions_OK : constant Boolean := | |
8350 | not Restriction_Active (No_Exception_Propagation); | |
8351 | Finalization_OK : constant Boolean := | |
8352 | Present (Comp_Typ) | |
8353 | and then Needs_Finalization (Comp_Typ); | |
fc84947c EB |
8354 | Loc : constant Source_Ptr := Sloc (N); |
8355 | ||
fc84947c EB |
8356 | Blk_Stmts : List_Id; |
8357 | Init_Stmt : Node_Id; | |
8358 | ||
8359 | begin | |
8360 | pragma Assert (Nkind (Init_Expr) in N_Subexpr); | |
8361 | ||
8362 | -- Protect the initialization statements from aborts. Generate: | |
8363 | ||
8364 | -- Abort_Defer; | |
8365 | ||
8366 | if Finalization_OK and Abort_Allowed then | |
8367 | if Exceptions_OK then | |
8368 | Blk_Stmts := New_List; | |
8369 | else | |
8370 | Blk_Stmts := Stmts; | |
8371 | end if; | |
8372 | ||
8373 | Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer)); | |
8374 | ||
8375 | -- Otherwise aborts are not allowed. All generated code is added | |
8376 | -- directly to the input list. | |
8377 | ||
8378 | else | |
8379 | Blk_Stmts := Stmts; | |
8380 | end if; | |
8381 | ||
8382 | -- Initialize the component. Generate: | |
8383 | ||
8384 | -- Comp := Init_Expr; | |
8385 | ||
8386 | -- Note that the initialization expression is not duplicated because | |
8387 | -- either only a single component may be initialized by it (record) | |
8388 | -- or it has already been duplicated if need be (array). | |
8389 | ||
8390 | Init_Stmt := | |
8391 | Make_OK_Assignment_Statement (Loc, | |
8392 | Name => New_Copy_Tree (Comp), | |
8393 | Expression => Relocate_Node (Init_Expr)); | |
fc84947c EB |
8394 | |
8395 | Append_To (Blk_Stmts, Init_Stmt); | |
8396 | ||
c0ceba6c EB |
8397 | -- Arrange for the component to be adjusted if need be (the call will be |
8398 | -- generated by Make_Tag_Ctrl_Assignment). But, in the case of an array | |
8399 | -- aggregate, controlled subaggregates are not considered because each | |
8400 | -- of their individual elements will receive an adjustment of its own. | |
fc84947c EB |
8401 | |
8402 | if Finalization_OK | |
c0ceba6c | 8403 | and then not Is_Limited_View (Comp_Typ) |
fc84947c EB |
8404 | and then not |
8405 | (Is_Array_Type (Etype (N)) | |
8406 | and then Is_Array_Type (Comp_Typ) | |
8407 | and then Needs_Finalization (Component_Type (Comp_Typ)) | |
8408 | and then Nkind (Unqualify (Init_Expr)) = N_Aggregate) | |
8409 | then | |
c0ceba6c | 8410 | Set_No_Finalize_Actions (Init_Stmt); |
fc84947c | 8411 | |
c0ceba6c EB |
8412 | -- Or else, only adjust the tag due to a possible view conversion |
8413 | ||
8414 | else | |
8415 | Set_No_Ctrl_Actions (Init_Stmt); | |
fc84947c | 8416 | |
c0ceba6c EB |
8417 | if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then |
8418 | Append_To (Blk_Stmts, | |
8419 | Make_Tag_Assignment_From_Type | |
8420 | (Loc, New_Copy_Tree (Comp), Underlying_Type (Comp_Typ))); | |
fc84947c EB |
8421 | end if; |
8422 | end if; | |
8423 | ||
8424 | -- Complete the protection of the initialization statements | |
8425 | ||
8426 | if Finalization_OK and Abort_Allowed then | |
8427 | ||
8428 | -- Wrap the initialization statements in a block to catch a | |
8429 | -- potential exception. Generate: | |
8430 | ||
8431 | -- begin | |
8432 | -- Abort_Defer; | |
8433 | -- Comp := Init_Expr; | |
8434 | -- Comp._tag := Full_TypP; | |
8435 | -- [Deep_]Adjust (Comp); | |
8436 | -- at end | |
8437 | -- Abort_Undefer_Direct; | |
8438 | -- end; | |
8439 | ||
8440 | if Exceptions_OK then | |
8441 | Append_To (Stmts, | |
8442 | Build_Abort_Undefer_Block (Loc, | |
8443 | Stmts => Blk_Stmts, | |
8444 | Context => N)); | |
8445 | ||
8446 | -- Otherwise exceptions are not propagated. Generate: | |
8447 | ||
8448 | -- Abort_Defer; | |
8449 | -- Comp := Init_Expr; | |
8450 | -- Comp._tag := Full_TypP; | |
8451 | -- [Deep_]Adjust (Comp); | |
8452 | -- Abort_Undefer; | |
8453 | ||
8454 | else | |
8455 | Append_To (Blk_Stmts, | |
8456 | Build_Runtime_Call (Loc, RE_Abort_Undefer)); | |
8457 | end if; | |
8458 | end if; | |
c0ceba6c | 8459 | end Initialize_Component; |
fc84947c | 8460 | |
3fc40cd7 PMR |
8461 | ---------------------------------------- |
8462 | -- Is_Build_In_Place_Aggregate_Return -- | |
8463 | ---------------------------------------- | |
8464 | ||
8465 | function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean is | |
8466 | P : Node_Id := Parent (N); | |
8467 | ||
8468 | begin | |
e05e5d6b EB |
8469 | while Nkind (P) in N_Case_Expression |
8470 | | N_Case_Expression_Alternative | |
8471 | | N_If_Expression | |
8472 | | N_Qualified_Expression | |
8473 | loop | |
3fc40cd7 PMR |
8474 | P := Parent (P); |
8475 | end loop; | |
8476 | ||
8477 | if Nkind (P) = N_Simple_Return_Statement then | |
8478 | null; | |
8479 | ||
8480 | elsif Nkind (Parent (P)) = N_Extended_Return_Statement then | |
8481 | P := Parent (P); | |
8482 | ||
8483 | else | |
8484 | return False; | |
8485 | end if; | |
8486 | ||
8487 | return | |
8488 | Is_Build_In_Place_Function | |
8489 | (Return_Applies_To (Return_Statement_Entity (P))); | |
8490 | end Is_Build_In_Place_Aggregate_Return; | |
8491 | ||
70482933 RK |
8492 | -------------------------- |
8493 | -- Is_Delayed_Aggregate -- | |
8494 | -------------------------- | |
8495 | ||
8496 | function Is_Delayed_Aggregate (N : Node_Id) return Boolean is | |
4f061cf2 | 8497 | Unqual_N : constant Node_Id := Unqualify (N); |
fbf5a39b | 8498 | |
70482933 | 8499 | begin |
4f061cf2 EB |
8500 | return Nkind (Unqual_N) in N_Aggregate | N_Extension_Aggregate |
8501 | and then Expansion_Delayed (Unqual_N); | |
70482933 RK |
8502 | end Is_Delayed_Aggregate; |
8503 | ||
9f51b855 JM |
8504 | -------------------------------- |
8505 | -- Is_CCG_Supported_Aggregate -- | |
8506 | -------------------------------- | |
6031f544 | 8507 | |
9f51b855 JM |
8508 | function Is_CCG_Supported_Aggregate |
8509 | (N : Node_Id) return Boolean | |
8510 | is | |
4ff5aa0c | 8511 | P : Node_Id := Parent (N); |
2401c98f | 8512 | |
6031f544 | 8513 | begin |
bc1146e5 HK |
8514 | -- Aggregates are not supported for nonstandard rep clauses, since they |
8515 | -- may lead to extra padding fields in CCG. | |
4ff5aa0c | 8516 | |
3bcf8298 | 8517 | if Is_Record_Type (Etype (N)) |
4ff5aa0c AC |
8518 | and then Has_Non_Standard_Rep (Etype (N)) |
8519 | then | |
8520 | return False; | |
8521 | end if; | |
6031f544 | 8522 | |
4ff5aa0c | 8523 | while Present (P) and then Nkind (P) = N_Aggregate loop |
6031f544 AC |
8524 | P := Parent (P); |
8525 | end loop; | |
8526 | ||
d2d56bba | 8527 | -- Check cases where aggregates are supported by the CCG backend |
9f51b855 | 8528 | |
4ff5aa0c | 8529 | if Nkind (P) = N_Object_Declaration then |
d2d56bba JM |
8530 | declare |
8531 | P_Typ : constant Entity_Id := Etype (Defining_Identifier (P)); | |
9f51b855 | 8532 | |
d2d56bba JM |
8533 | begin |
8534 | if Is_Record_Type (P_Typ) then | |
8535 | return True; | |
8536 | else | |
8537 | return Compile_Time_Known_Bounds (P_Typ); | |
8538 | end if; | |
8539 | end; | |
8540 | ||
8541 | elsif Nkind (P) = N_Qualified_Expression then | |
8542 | if Nkind (Parent (P)) = N_Object_Declaration then | |
8543 | declare | |
8544 | P_Typ : constant Entity_Id := | |
8545 | Etype (Defining_Identifier (Parent (P))); | |
8546 | begin | |
8547 | if Is_Record_Type (P_Typ) then | |
8548 | return True; | |
8549 | else | |
8550 | return Compile_Time_Known_Bounds (P_Typ); | |
8551 | end if; | |
8552 | end; | |
8553 | ||
8554 | elsif Nkind (Parent (P)) = N_Allocator then | |
8555 | return True; | |
8556 | end if; | |
9f51b855 JM |
8557 | end if; |
8558 | ||
6031f544 | 8559 | return False; |
9f51b855 | 8560 | end Is_CCG_Supported_Aggregate; |
6031f544 | 8561 | |
fa57ac97 ES |
8562 | ---------------------------------------- |
8563 | -- Is_Static_Dispatch_Table_Aggregate -- | |
8564 | ---------------------------------------- | |
8565 | ||
8566 | function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is | |
8567 | Typ : constant Entity_Id := Base_Type (Etype (N)); | |
8568 | ||
8569 | begin | |
6214b83b | 8570 | return Building_Static_Dispatch_Tables |
1f110335 | 8571 | and then Tagged_Type_Expansion |
fa57ac97 ES |
8572 | |
8573 | -- Avoid circularity when rebuilding the compiler | |
8574 | ||
3477e0b2 PT |
8575 | and then not Is_RTU (Cunit_Entity (Get_Source_Unit (N)), Ada_Tags) |
8576 | and then (Is_RTE (Typ, RE_Dispatch_Table_Wrapper) | |
fa57ac97 | 8577 | or else |
3477e0b2 | 8578 | Is_RTE (Typ, RE_Address_Array) |
fa57ac97 | 8579 | or else |
3477e0b2 | 8580 | Is_RTE (Typ, RE_Type_Specific_Data) |
fa57ac97 | 8581 | or else |
3477e0b2 | 8582 | Is_RTE (Typ, RE_Tag_Table) |
fa57ac97 | 8583 | or else |
3477e0b2 | 8584 | Is_RTE (Typ, RE_Object_Specific_Data) |
c7cb99f8 | 8585 | or else |
3477e0b2 | 8586 | Is_RTE (Typ, RE_Interface_Data) |
fa57ac97 | 8587 | or else |
3477e0b2 | 8588 | Is_RTE (Typ, RE_Interfaces_Array) |
fa57ac97 | 8589 | or else |
3477e0b2 | 8590 | Is_RTE (Typ, RE_Interface_Data_Element)); |
fa57ac97 ES |
8591 | end Is_Static_Dispatch_Table_Aggregate; |
8592 | ||
dc3af7e2 AC |
8593 | ----------------------------- |
8594 | -- Is_Two_Dim_Packed_Array -- | |
8595 | ----------------------------- | |
8596 | ||
8597 | function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is | |
36e38022 | 8598 | C : constant Uint := Component_Size (Typ); |
42fcc750 | 8599 | |
dc3af7e2 AC |
8600 | begin |
8601 | return Number_Dimensions (Typ) = 2 | |
8602 | and then Is_Bit_Packed_Array (Typ) | |
42fcc750 | 8603 | and then Is_Scalar_Type (Component_Type (Typ)) |
36e38022 | 8604 | and then C in Uint_1 | Uint_2 | Uint_4; -- False if No_Uint |
dc3af7e2 AC |
8605 | end Is_Two_Dim_Packed_Array; |
8606 | ||
70482933 RK |
8607 | -------------------- |
8608 | -- Late_Expansion -- | |
8609 | -------------------- | |
8610 | ||
8611 | function Late_Expansion | |
8612 | (N : Node_Id; | |
8613 | Typ : Entity_Id; | |
df3e68b1 | 8614 | Target : Node_Id) return List_Id |
9bc856dd | 8615 | is |
f29afe5f | 8616 | Aggr_Code : List_Id; |
b748c3d1 | 8617 | New_Aggr : Node_Id; |
f29afe5f | 8618 | |
70482933 | 8619 | begin |
b748c3d1 EB |
8620 | if Is_Array_Type (Typ) then |
8621 | -- If the assignment can be done directly by the back end, then | |
8622 | -- reset Set_Expansion_Delayed and do not expand further. | |
8623 | ||
8624 | if not CodePeer_Mode | |
8625 | and then not Modify_Tree_For_C | |
8626 | and then not Possible_Bit_Aligned_Component (Target) | |
8627 | and then not Is_Possibly_Unaligned_Slice (Target) | |
8628 | and then Aggr_Assignment_OK_For_Backend (N) | |
8629 | then | |
8630 | New_Aggr := New_Copy_Tree (N); | |
8631 | Set_Expansion_Delayed (New_Aggr, False); | |
8632 | ||
8633 | Aggr_Code := | |
8634 | New_List ( | |
8635 | Make_OK_Assignment_Statement (Sloc (New_Aggr), | |
8636 | Name => Target, | |
8637 | Expression => New_Aggr)); | |
8638 | ||
8639 | -- Or else, generate component assignments to it | |
8640 | ||
8641 | else | |
8642 | Aggr_Code := | |
8643 | Build_Array_Aggr_Code | |
8644 | (N => N, | |
8645 | Ctype => Component_Type (Typ), | |
8646 | Index => First_Index (Typ), | |
8647 | Into => Target, | |
8648 | Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)), | |
8649 | Indexes => No_List); | |
8650 | end if; | |
6031f544 | 8651 | |
f3bf0d9a HK |
8652 | -- Directly or indirectly (e.g. access protected procedure) a record |
8653 | ||
8654 | else | |
6031f544 | 8655 | Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target); |
70482933 | 8656 | end if; |
4ac2bbbd AC |
8657 | |
8658 | -- Save the last assignment statement associated with the aggregate | |
8659 | -- when building a controlled object. This reference is utilized by | |
8660 | -- the finalization machinery when marking an object as successfully | |
8661 | -- initialized. | |
8662 | ||
8663 | if Needs_Finalization (Typ) | |
8664 | and then Is_Entity_Name (Target) | |
8665 | and then Present (Entity (Target)) | |
4a08c95c | 8666 | and then Ekind (Entity (Target)) in E_Constant | E_Variable |
4ac2bbbd AC |
8667 | then |
8668 | Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code)); | |
8669 | end if; | |
8670 | ||
8671 | return Aggr_Code; | |
70482933 RK |
8672 | end Late_Expansion; |
8673 | ||
8674 | ---------------------------------- | |
8675 | -- Make_OK_Assignment_Statement -- | |
8676 | ---------------------------------- | |
8677 | ||
8678 | function Make_OK_Assignment_Statement | |
8679 | (Sloc : Source_Ptr; | |
8680 | Name : Node_Id; | |
0f95b178 | 8681 | Expression : Node_Id) return Node_Id |
70482933 RK |
8682 | is |
8683 | begin | |
8684 | Set_Assignment_OK (Name); | |
8685 | return Make_Assignment_Statement (Sloc, Name, Expression); | |
8686 | end Make_OK_Assignment_Statement; | |
8687 | ||
c42006e9 AC |
8688 | ------------------------ |
8689 | -- Max_Aggregate_Size -- | |
8690 | ------------------------ | |
8691 | ||
8692 | function Max_Aggregate_Size | |
eaf6e63a BD |
8693 | (N : Node_Id; |
8694 | Default_Size : Nat := 5000) return Nat | |
8695 | is | |
eaf6e63a BD |
8696 | function Use_Small_Size (N : Node_Id) return Boolean; |
8697 | -- True if we should return a very small size, which means large | |
8698 | -- aggregates will be implemented as a loop when possible (potentially | |
8699 | -- transformed to memset calls). | |
8700 | ||
8701 | function Aggr_Context (N : Node_Id) return Node_Id; | |
8702 | -- Return the context in which the aggregate appears, not counting | |
8703 | -- qualified expressions and similar. | |
8704 | ||
bcc15039 PT |
8705 | ------------------ |
8706 | -- Aggr_Context -- | |
8707 | ------------------ | |
8708 | ||
eaf6e63a BD |
8709 | function Aggr_Context (N : Node_Id) return Node_Id is |
8710 | Result : Node_Id := Parent (N); | |
8711 | begin | |
4a08c95c AC |
8712 | if Nkind (Result) in N_Qualified_Expression |
8713 | | N_Type_Conversion | |
8714 | | N_Unchecked_Type_Conversion | |
8715 | | N_If_Expression | |
8716 | | N_Case_Expression | |
8717 | | N_Component_Association | |
8718 | | N_Aggregate | |
eaf6e63a BD |
8719 | then |
8720 | Result := Aggr_Context (Result); | |
8721 | end if; | |
8722 | ||
8723 | return Result; | |
8724 | end Aggr_Context; | |
8725 | ||
bcc15039 PT |
8726 | -------------------- |
8727 | -- Use_Small_Size -- | |
8728 | -------------------- | |
8729 | ||
eaf6e63a BD |
8730 | function Use_Small_Size (N : Node_Id) return Boolean is |
8731 | C : constant Node_Id := Aggr_Context (N); | |
8732 | -- The decision depends on the context in which the aggregate occurs, | |
8733 | -- and for variable declarations, whether we are nested inside a | |
8734 | -- subprogram. | |
8735 | begin | |
8736 | case Nkind (C) is | |
8737 | -- True for assignment statements and similar | |
8738 | ||
8739 | when N_Assignment_Statement | |
8740 | | N_Simple_Return_Statement | |
8741 | | N_Allocator | |
8742 | | N_Attribute_Reference | |
8743 | => | |
8744 | return True; | |
8745 | ||
8746 | -- True for nested variable declarations. False for library level | |
8747 | -- variables, and for constants (whether or not nested). | |
8748 | ||
8749 | when N_Object_Declaration => | |
8750 | return not Constant_Present (C) | |
0f3dfe41 | 8751 | and then Is_Subprogram (Current_Scope); |
eaf6e63a BD |
8752 | |
8753 | -- False for all other contexts | |
8754 | ||
8755 | when others => | |
8756 | return False; | |
8757 | end case; | |
8758 | end Use_Small_Size; | |
8759 | ||
bcc15039 PT |
8760 | -- Local variables |
8761 | ||
8762 | Typ : constant Entity_Id := Etype (N); | |
8763 | ||
eaf6e63a BD |
8764 | -- Start of processing for Max_Aggregate_Size |
8765 | ||
c42006e9 | 8766 | begin |
bcc15039 PT |
8767 | -- We use a small limit in CodePeer mode where we favor loops instead of |
8768 | -- thousands of single assignments (from large aggregates). | |
c42006e9 AC |
8769 | |
8770 | -- We also increase the limit to 2**24 (about 16 million) if | |
8771 | -- Restrictions (No_Elaboration_Code) or Restrictions | |
8772 | -- (No_Implicit_Loops) is specified, since in either case we are at risk | |
8773 | -- of declaring the program illegal because of this limit. We also | |
8774 | -- increase the limit when Static_Elaboration_Desired, given that this | |
8775 | -- means that objects are intended to be placed in data memory. | |
8776 | ||
8777 | -- Same if the aggregate is for a packed two-dimensional array, because | |
8778 | -- if components are static it is much more efficient to construct a | |
8779 | -- one-dimensional equivalent array with static components. | |
8780 | ||
8781 | if CodePeer_Mode then | |
8782 | return 100; | |
8783 | elsif Restriction_Active (No_Elaboration_Code) | |
8784 | or else Restriction_Active (No_Implicit_Loops) | |
8785 | or else Is_Two_Dim_Packed_Array (Typ) | |
8786 | or else (Ekind (Current_Scope) = E_Package | |
8787 | and then Static_Elaboration_Desired (Current_Scope)) | |
8788 | then | |
8789 | return 2 ** 24; | |
eaf6e63a | 8790 | elsif Use_Small_Size (N) then |
152f64c2 | 8791 | return 64; |
c42006e9 | 8792 | end if; |
eaf6e63a BD |
8793 | |
8794 | return Default_Size; | |
c42006e9 AC |
8795 | end Max_Aggregate_Size; |
8796 | ||
70482933 RK |
8797 | ----------------------- |
8798 | -- Number_Of_Choices -- | |
8799 | ----------------------- | |
8800 | ||
8801 | function Number_Of_Choices (N : Node_Id) return Nat is | |
8802 | Assoc : Node_Id; | |
8803 | Choice : Node_Id; | |
8804 | ||
8805 | Nb_Choices : Nat := 0; | |
8806 | ||
8807 | begin | |
8808 | if Present (Expressions (N)) then | |
8809 | return 0; | |
8810 | end if; | |
8811 | ||
8812 | Assoc := First (Component_Associations (N)); | |
8813 | while Present (Assoc) loop | |
00f45f30 | 8814 | Choice := First (Choice_List (Assoc)); |
70482933 | 8815 | while Present (Choice) loop |
70482933 RK |
8816 | if Nkind (Choice) /= N_Others_Choice then |
8817 | Nb_Choices := Nb_Choices + 1; | |
8818 | end if; | |
8819 | ||
8820 | Next (Choice); | |
8821 | end loop; | |
8822 | ||
8823 | Next (Assoc); | |
8824 | end loop; | |
8825 | ||
8826 | return Nb_Choices; | |
8827 | end Number_Of_Choices; | |
8828 | ||
07fc65c4 GB |
8829 | ------------------------------------ |
8830 | -- Packed_Array_Aggregate_Handled -- | |
8831 | ------------------------------------ | |
8832 | ||
8833 | -- The current version of this procedure will handle at compile time | |
8834 | -- any array aggregate that meets these conditions: | |
8835 | ||
5eeeed5e | 8836 | -- One and two dimensional, bit packed |
07fc65c4 GB |
8837 | -- Underlying packed type is modular type |
8838 | -- Bounds are within 32-bit Int range | |
8839 | -- All bounds and values are static | |
8840 | ||
a39a553e AC |
8841 | -- Note: for now, in the 2-D case, we only handle component sizes of |
8842 | -- 1, 2, 4 (cases where an integral number of elements occupies a byte). | |
8843 | ||
07fc65c4 GB |
8844 | function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is |
8845 | Loc : constant Source_Ptr := Sloc (N); | |
8846 | Typ : constant Entity_Id := Etype (N); | |
8847 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
8848 | ||
8849 | Not_Handled : exception; | |
8850 | -- Exception raised if this aggregate cannot be handled | |
8851 | ||
8852 | begin | |
5eeeed5e | 8853 | -- Handle one- or two dimensional bit packed array |
07fc65c4 GB |
8854 | |
8855 | if not Is_Bit_Packed_Array (Typ) | |
5eeeed5e | 8856 | or else Number_Dimensions (Typ) > 2 |
07fc65c4 GB |
8857 | then |
8858 | return False; | |
8859 | end if; | |
8860 | ||
5eeeed5e | 8861 | -- If two-dimensional, check whether it can be folded, and transformed |
8ca597af RD |
8862 | -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of |
8863 | -- the original type. | |
5eeeed5e AC |
8864 | |
8865 | if Number_Dimensions (Typ) = 2 then | |
8866 | return Two_Dim_Packed_Array_Handled (N); | |
8867 | end if; | |
8868 | ||
8ca597af | 8869 | if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then |
5eeeed5e AC |
8870 | return False; |
8871 | end if; | |
8872 | ||
78326189 | 8873 | if not Is_Scalar_Type (Ctyp) then |
0f95b178 JM |
8874 | return False; |
8875 | end if; | |
8876 | ||
07fc65c4 GB |
8877 | declare |
8878 | Csiz : constant Nat := UI_To_Int (Component_Size (Typ)); | |
8879 | ||
07fc65c4 | 8880 | function Get_Component_Val (N : Node_Id) return Uint; |
3b9fa2df ES |
8881 | -- Given a expression value N of the component type Ctyp, returns a |
8882 | -- value of Csiz (component size) bits representing this value. If | |
d940c627 | 8883 | -- the value is nonstatic or any other reason exists why the value |
3b9fa2df | 8884 | -- cannot be returned, then Not_Handled is raised. |
07fc65c4 GB |
8885 | |
8886 | ----------------------- | |
8887 | -- Get_Component_Val -- | |
8888 | ----------------------- | |
8889 | ||
8890 | function Get_Component_Val (N : Node_Id) return Uint is | |
fb8e3581 | 8891 | Val : Uint; |
07fc65c4 GB |
8892 | |
8893 | begin | |
8894 | -- We have to analyze the expression here before doing any further | |
8895 | -- processing here. The analysis of such expressions is deferred | |
8896 | -- till expansion to prevent some problems of premature analysis. | |
8897 | ||
8898 | Analyze_And_Resolve (N, Ctyp); | |
8899 | ||
3b9fa2df ES |
8900 | -- Must have a compile time value. String literals have to be |
8901 | -- converted into temporaries as well, because they cannot easily | |
8902 | -- be converted into their bit representation. | |
07fc65c4 | 8903 | |
6b6fcd3e AC |
8904 | if not Compile_Time_Known_Value (N) |
8905 | or else Nkind (N) = N_String_Literal | |
8906 | then | |
07fc65c4 GB |
8907 | raise Not_Handled; |
8908 | end if; | |
8909 | ||
8910 | Val := Expr_Rep_Value (N); | |
8911 | ||
8912 | -- Adjust for bias, and strip proper number of bits | |
8913 | ||
8914 | if Has_Biased_Representation (Ctyp) then | |
8915 | Val := Val - Expr_Value (Type_Low_Bound (Ctyp)); | |
8916 | end if; | |
8917 | ||
8918 | return Val mod Uint_2 ** Csiz; | |
8919 | end Get_Component_Val; | |
8920 | ||
7c4f3267 BD |
8921 | Bounds : constant Range_Nodes := Get_Index_Bounds (First_Index (Typ)); |
8922 | ||
07fc65c4 GB |
8923 | -- Here we know we have a one dimensional bit packed array |
8924 | ||
8925 | begin | |
07fc65c4 GB |
8926 | -- Cannot do anything if bounds are dynamic |
8927 | ||
7c4f3267 BD |
8928 | if not (Compile_Time_Known_Value (Bounds.First) |
8929 | and then | |
8930 | Compile_Time_Known_Value (Bounds.Last)) | |
07fc65c4 GB |
8931 | then |
8932 | return False; | |
8933 | end if; | |
8934 | ||
7c4f3267 BD |
8935 | declare |
8936 | Bounds_Vals : Range_Values; | |
8937 | -- Compile-time known values of bounds | |
8938 | begin | |
8939 | -- Or are silly out of range of int bounds | |
07fc65c4 | 8940 | |
7c4f3267 BD |
8941 | Bounds_Vals.First := Expr_Value (Bounds.First); |
8942 | Bounds_Vals.Last := Expr_Value (Bounds.Last); | |
07fc65c4 | 8943 | |
7c4f3267 BD |
8944 | if not UI_Is_In_Int_Range (Bounds_Vals.First) |
8945 | or else | |
8946 | not UI_Is_In_Int_Range (Bounds_Vals.Last) | |
8947 | then | |
8948 | return False; | |
8949 | end if; | |
07fc65c4 | 8950 | |
7c4f3267 BD |
8951 | -- At this stage we have a suitable aggregate for handling at |
8952 | -- compile time. The only remaining checks are that the values of | |
8953 | -- expressions in the aggregate are compile-time known (checks are | |
8954 | -- performed by Get_Component_Val), and that any subtypes or | |
8955 | -- ranges are statically known. | |
07fc65c4 | 8956 | |
7c4f3267 BD |
8957 | -- If the aggregate is not fully positional at this stage, then |
8958 | -- convert it to positional form. Either this will fail, in which | |
8959 | -- case we can do nothing, or it will succeed, in which case we | |
8960 | -- have succeeded in handling the aggregate and transforming it | |
8961 | -- into a modular value, or it will stay an aggregate, in which | |
8962 | -- case we have failed to create a packed value for it. | |
07fc65c4 | 8963 | |
7c4f3267 BD |
8964 | if Present (Component_Associations (N)) then |
8965 | Convert_To_Positional (N, Handle_Bit_Packed => True); | |
8966 | return Nkind (N) /= N_Aggregate; | |
8967 | end if; | |
07fc65c4 | 8968 | |
7c4f3267 | 8969 | -- Otherwise we are all positional, so convert to proper value |
07fc65c4 | 8970 | |
7c4f3267 BD |
8971 | declare |
8972 | Len : constant Nat := | |
8973 | Int'Max (0, UI_To_Int (Bounds_Vals.Last) - | |
8974 | UI_To_Int (Bounds_Vals.First) + 1); | |
8975 | -- The length of the array (number of elements) | |
07fc65c4 | 8976 | |
7c4f3267 BD |
8977 | Aggregate_Val : Uint; |
8978 | -- Value of aggregate. The value is set in the low order bits | |
8979 | -- of this value. For the little-endian case, the values are | |
8980 | -- stored from low-order to high-order and for the big-endian | |
8981 | -- case the values are stored from high order to low order. | |
8982 | -- Note that gigi will take care of the conversions to left | |
8983 | -- justify the value in the big endian case (because of left | |
8984 | -- justified modular type processing), so we do not have to | |
8985 | -- worry about that here. | |
07fc65c4 | 8986 | |
7c4f3267 BD |
8987 | Lit : Node_Id; |
8988 | -- Integer literal for resulting constructed value | |
07fc65c4 | 8989 | |
7c4f3267 BD |
8990 | Shift : Nat; |
8991 | -- Shift count from low order for next value | |
07fc65c4 | 8992 | |
7c4f3267 BD |
8993 | Incr : Int; |
8994 | -- Shift increment for loop | |
07fc65c4 | 8995 | |
7c4f3267 BD |
8996 | Expr : Node_Id; |
8997 | -- Next expression from positional parameters of aggregate | |
c9a6b38f | 8998 | |
7c4f3267 BD |
8999 | Left_Justified : Boolean; |
9000 | -- Set True if we are filling the high order bits of the target | |
9001 | -- value (i.e. the value is left justified). | |
07fc65c4 | 9002 | |
7c4f3267 BD |
9003 | begin |
9004 | -- For little endian, we fill up the low order bits of the | |
9005 | -- target value. For big endian we fill up the high order bits | |
9006 | -- of the target value (which is a left justified modular | |
9007 | -- value). | |
68f640f2 | 9008 | |
7c4f3267 | 9009 | Left_Justified := Bytes_Big_Endian; |
c9a6b38f | 9010 | |
7c4f3267 | 9011 | -- Switch justification if using -gnatd8 |
c9a6b38f | 9012 | |
7c4f3267 BD |
9013 | if Debug_Flag_8 then |
9014 | Left_Justified := not Left_Justified; | |
9015 | end if; | |
c9a6b38f | 9016 | |
7c4f3267 | 9017 | -- Switch justfification if reverse storage order |
c9a6b38f | 9018 | |
7c4f3267 BD |
9019 | if Reverse_Storage_Order (Base_Type (Typ)) then |
9020 | Left_Justified := not Left_Justified; | |
9021 | end if; | |
07fc65c4 | 9022 | |
7c4f3267 BD |
9023 | if Left_Justified then |
9024 | Shift := Csiz * (Len - 1); | |
9025 | Incr := -Csiz; | |
9026 | else | |
9027 | Shift := 0; | |
9028 | Incr := +Csiz; | |
9029 | end if; | |
07fc65c4 | 9030 | |
7c4f3267 | 9031 | -- Loop to set the values |
fbf5a39b | 9032 | |
7c4f3267 BD |
9033 | if Len = 0 then |
9034 | Aggregate_Val := Uint_0; | |
9035 | else | |
9036 | Expr := First (Expressions (N)); | |
9037 | Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift; | |
9038 | ||
9039 | for J in 2 .. Len loop | |
9040 | Shift := Shift + Incr; | |
9041 | Next (Expr); | |
9042 | Aggregate_Val := | |
9043 | Aggregate_Val + | |
9044 | Get_Component_Val (Expr) * Uint_2 ** Shift; | |
9045 | end loop; | |
9046 | end if; | |
07fc65c4 | 9047 | |
7c4f3267 | 9048 | -- Now we can rewrite with the proper value |
07fc65c4 | 9049 | |
7c4f3267 BD |
9050 | Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val); |
9051 | Set_Print_In_Hex (Lit); | |
07fc65c4 | 9052 | |
7c4f3267 BD |
9053 | -- Construct the expression using this literal. Note that it |
9054 | -- is important to qualify the literal with its proper modular | |
9055 | -- type since universal integer does not have the required | |
9056 | -- range and also this is a left justified modular type, | |
9057 | -- which is important in the big-endian case. | |
07fc65c4 | 9058 | |
7c4f3267 BD |
9059 | Rewrite (N, |
9060 | Unchecked_Convert_To (Typ, | |
9061 | Make_Qualified_Expression (Loc, | |
9062 | Subtype_Mark => | |
9063 | New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc), | |
9064 | Expression => Lit))); | |
07fc65c4 | 9065 | |
7c4f3267 BD |
9066 | Analyze_And_Resolve (N, Typ); |
9067 | return True; | |
9068 | end; | |
07fc65c4 GB |
9069 | end; |
9070 | end; | |
9071 | ||
9072 | exception | |
9073 | when Not_Handled => | |
9074 | return False; | |
9075 | end Packed_Array_Aggregate_Handled; | |
9076 | ||
fbf5a39b AC |
9077 | ---------------------------- |
9078 | -- Has_Mutable_Components -- | |
9079 | ---------------------------- | |
9080 | ||
9081 | function Has_Mutable_Components (Typ : Entity_Id) return Boolean is | |
9082 | Comp : Entity_Id; | |
bee475e2 | 9083 | Ctyp : Entity_Id; |
fbf5a39b AC |
9084 | |
9085 | begin | |
9086 | Comp := First_Component (Typ); | |
fbf5a39b | 9087 | while Present (Comp) loop |
bee475e2 EB |
9088 | Ctyp := Underlying_Type (Etype (Comp)); |
9089 | if Is_Record_Type (Ctyp) | |
9090 | and then Has_Discriminants (Ctyp) | |
9091 | and then not Is_Constrained (Ctyp) | |
fbf5a39b AC |
9092 | then |
9093 | return True; | |
9094 | end if; | |
9095 | ||
9096 | Next_Component (Comp); | |
9097 | end loop; | |
9098 | ||
9099 | return False; | |
9100 | end Has_Mutable_Components; | |
9101 | ||
07fc65c4 GB |
9102 | ------------------------------ |
9103 | -- Initialize_Discriminants -- | |
9104 | ------------------------------ | |
9105 | ||
9106 | procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is | |
9107 | Loc : constant Source_Ptr := Sloc (N); | |
9108 | Bas : constant Entity_Id := Base_Type (Typ); | |
9109 | Par : constant Entity_Id := Etype (Bas); | |
9110 | Decl : constant Node_Id := Parent (Par); | |
9111 | Ref : Node_Id; | |
9112 | ||
9113 | begin | |
9114 | if Is_Tagged_Type (Bas) | |
9115 | and then Is_Derived_Type (Bas) | |
9116 | and then Has_Discriminants (Par) | |
9117 | and then Has_Discriminants (Bas) | |
9118 | and then Number_Discriminants (Bas) /= Number_Discriminants (Par) | |
9119 | and then Nkind (Decl) = N_Full_Type_Declaration | |
9120 | and then Nkind (Type_Definition (Decl)) = N_Record_Definition | |
36a66365 AC |
9121 | and then |
9122 | Present (Variant_Part (Component_List (Type_Definition (Decl)))) | |
07fc65c4 GB |
9123 | and then Nkind (N) /= N_Extension_Aggregate |
9124 | then | |
9125 | ||
fbf5a39b | 9126 | -- Call init proc to set discriminants. |
07fc65c4 GB |
9127 | -- There should eventually be a special procedure for this ??? |
9128 | ||
e4494292 | 9129 | Ref := New_Occurrence_Of (Defining_Identifier (N), Loc); |
07fc65c4 GB |
9130 | Insert_Actions_After (N, |
9131 | Build_Initialization_Call (Sloc (N), Ref, Typ)); | |
9132 | end if; | |
9133 | end Initialize_Discriminants; | |
9134 | ||
3cf3e5c6 AC |
9135 | ---------------- |
9136 | -- Must_Slide -- | |
9137 | ---------------- | |
9138 | ||
9139 | function Must_Slide | |
6732c403 ES |
9140 | (Aggr : Node_Id; |
9141 | Obj_Type : Entity_Id; | |
3cf3e5c6 AC |
9142 | Typ : Entity_Id) return Boolean |
9143 | is | |
3cf3e5c6 | 9144 | begin |
3b9fa2df ES |
9145 | -- No sliding if the type of the object is not established yet, if it is |
9146 | -- an unconstrained type whose actual subtype comes from the aggregate, | |
6732c403 ES |
9147 | -- or if the two types are identical. If the aggregate contains only |
9148 | -- an Others_Clause it gets its type from the context and no sliding | |
9149 | -- is involved either. | |
3cf3e5c6 AC |
9150 | |
9151 | if not Is_Array_Type (Obj_Type) then | |
9152 | return False; | |
9153 | ||
9154 | elsif not Is_Constrained (Obj_Type) then | |
9155 | return False; | |
9156 | ||
9157 | elsif Typ = Obj_Type then | |
9158 | return False; | |
9159 | ||
6732c403 ES |
9160 | elsif Is_Others_Aggregate (Aggr) then |
9161 | return False; | |
9162 | ||
3cf3e5c6 AC |
9163 | else |
9164 | -- Sliding can only occur along the first dimension | |
6732c403 ES |
9165 | -- If any the bounds of non-static sliding is required |
9166 | -- to force potential range checks. | |
3cf3e5c6 | 9167 | |
7c4f3267 BD |
9168 | declare |
9169 | Bounds1 : constant Range_Nodes := | |
9170 | Get_Index_Bounds (First_Index (Typ)); | |
9171 | Bounds2 : constant Range_Nodes := | |
9172 | Get_Index_Bounds (First_Index (Obj_Type)); | |
3cf3e5c6 | 9173 | |
7c4f3267 BD |
9174 | begin |
9175 | if not Is_OK_Static_Expression (Bounds1.First) or else | |
9176 | not Is_OK_Static_Expression (Bounds2.First) or else | |
9177 | not Is_OK_Static_Expression (Bounds1.Last) or else | |
9178 | not Is_OK_Static_Expression (Bounds2.Last) | |
9179 | then | |
6732c403 ES |
9180 | return True; |
9181 | ||
7c4f3267 BD |
9182 | else |
9183 | return Expr_Value (Bounds1.First) /= Expr_Value (Bounds2.First) | |
9184 | or else | |
9185 | Expr_Value (Bounds1.Last) /= Expr_Value (Bounds2.Last); | |
9186 | end if; | |
9187 | end; | |
3cf3e5c6 AC |
9188 | end if; |
9189 | end Must_Slide; | |
9190 | ||
70482933 RK |
9191 | --------------------- |
9192 | -- Sort_Case_Table -- | |
9193 | --------------------- | |
9194 | ||
9195 | procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is | |
fbf5a39b AC |
9196 | L : constant Int := Case_Table'First; |
9197 | U : constant Int := Case_Table'Last; | |
70482933 RK |
9198 | K : Int; |
9199 | J : Int; | |
9200 | T : Case_Bounds; | |
9201 | ||
9202 | begin | |
9203 | K := L; | |
70482933 RK |
9204 | while K /= U loop |
9205 | T := Case_Table (K + 1); | |
70482933 | 9206 | |
5277cab6 | 9207 | J := K + 1; |
70482933 RK |
9208 | while J /= L |
9209 | and then Expr_Value (Case_Table (J - 1).Choice_Lo) > | |
9210 | Expr_Value (T.Choice_Lo) | |
9211 | loop | |
9212 | Case_Table (J) := Case_Table (J - 1); | |
9213 | J := J - 1; | |
9214 | end loop; | |
9215 | ||
9216 | Case_Table (J) := T; | |
9217 | K := K + 1; | |
9218 | end loop; | |
9219 | end Sort_Case_Table; | |
9220 | ||
0f95b178 JM |
9221 | ---------------------------- |
9222 | -- Static_Array_Aggregate -- | |
9223 | ---------------------------- | |
9224 | ||
9225 | function Static_Array_Aggregate (N : Node_Id) return Boolean is | |
2401c98f HK |
9226 | function Is_Static_Component (Nod : Node_Id) return Boolean; |
9227 | -- Return True if Nod has a compile-time known value and can be passed | |
9228 | -- as is to the back-end without further expansion. | |
2a1838cd EB |
9229 | |
9230 | --------------------------- | |
9231 | -- Is_Static_Component -- | |
9232 | --------------------------- | |
9233 | ||
2401c98f | 9234 | function Is_Static_Component (Nod : Node_Id) return Boolean is |
2a1838cd | 9235 | begin |
4a08c95c | 9236 | if Nkind (Nod) in N_Integer_Literal | N_Real_Literal then |
2a1838cd EB |
9237 | return True; |
9238 | ||
2401c98f HK |
9239 | elsif Is_Entity_Name (Nod) |
9240 | and then Present (Entity (Nod)) | |
9241 | and then Ekind (Entity (Nod)) = E_Enumeration_Literal | |
2a1838cd EB |
9242 | then |
9243 | return True; | |
9244 | ||
2401c98f HK |
9245 | elsif Nkind (Nod) = N_Aggregate |
9246 | and then Compile_Time_Known_Aggregate (Nod) | |
2a1838cd EB |
9247 | then |
9248 | return True; | |
9249 | ||
9250 | else | |
9251 | return False; | |
9252 | end if; | |
9253 | end Is_Static_Component; | |
9254 | ||
2401c98f HK |
9255 | -- Local variables |
9256 | ||
9257 | Bounds : constant Node_Id := Aggregate_Bounds (N); | |
9258 | Typ : constant Entity_Id := Etype (N); | |
0f95b178 | 9259 | |
2401c98f HK |
9260 | Agg : Node_Id; |
9261 | Expr : Node_Id; | |
9262 | Lo : Node_Id; | |
9263 | Hi : Node_Id; | |
0f95b178 | 9264 | |
2a1838cd EB |
9265 | -- Start of processing for Static_Array_Aggregate |
9266 | ||
0f95b178 | 9267 | begin |
2a1838cd | 9268 | if Is_Packed (Typ) or else Has_Discriminants (Component_Type (Typ)) then |
0f95b178 JM |
9269 | return False; |
9270 | end if; | |
9271 | ||
9272 | if Present (Bounds) | |
9273 | and then Nkind (Bounds) = N_Range | |
9274 | and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal | |
9275 | and then Nkind (High_Bound (Bounds)) = N_Integer_Literal | |
9276 | then | |
9277 | Lo := Low_Bound (Bounds); | |
9278 | Hi := High_Bound (Bounds); | |
9279 | ||
9280 | if No (Component_Associations (N)) then | |
9281 | ||
2a1838cd | 9282 | -- Verify that all components are static |
0f95b178 JM |
9283 | |
9284 | Expr := First (Expressions (N)); | |
9285 | while Present (Expr) loop | |
2a1838cd | 9286 | if not Is_Static_Component (Expr) then |
0f95b178 JM |
9287 | return False; |
9288 | end if; | |
9289 | ||
9290 | Next (Expr); | |
9291 | end loop; | |
9292 | ||
9293 | return True; | |
9294 | ||
9295 | else | |
9296 | -- We allow only a single named association, either a static | |
9297 | -- range or an others_clause, with a static expression. | |
9298 | ||
9299 | Expr := First (Component_Associations (N)); | |
9300 | ||
9301 | if Present (Expressions (N)) then | |
9302 | return False; | |
9303 | ||
9304 | elsif Present (Next (Expr)) then | |
9305 | return False; | |
9306 | ||
00f45f30 | 9307 | elsif Present (Next (First (Choice_List (Expr)))) then |
0f95b178 JM |
9308 | return False; |
9309 | ||
9310 | else | |
d7f94401 AC |
9311 | -- The aggregate is static if all components are literals, |
9312 | -- or else all its components are static aggregates for the | |
fc534c1c ES |
9313 | -- component type. We also limit the size of a static aggregate |
9314 | -- to prevent runaway static expressions. | |
0f95b178 | 9315 | |
2a1838cd | 9316 | if not Is_Static_Component (Expression (Expr)) then |
0f95b178 | 9317 | return False; |
6a2e4f0b | 9318 | end if; |
fc534c1c | 9319 | |
eaf6e63a | 9320 | if not Aggr_Size_OK (N) then |
fc534c1c | 9321 | return False; |
0f95b178 JM |
9322 | end if; |
9323 | ||
9324 | -- Create a positional aggregate with the right number of | |
9325 | -- copies of the expression. | |
9326 | ||
9327 | Agg := Make_Aggregate (Sloc (N), New_List, No_List); | |
9328 | ||
9329 | for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi)) | |
9330 | loop | |
37368818 | 9331 | Append_To (Expressions (Agg), New_Copy (Expression (Expr))); |
597d7158 | 9332 | |
9b4b0a1a GD |
9333 | -- The copied expression must be analyzed and resolved. |
9334 | -- Besides setting the type, this ensures that static | |
9335 | -- expressions are appropriately marked as such. | |
597d7158 | 9336 | |
9b4b0a1a GD |
9337 | Analyze_And_Resolve |
9338 | (Last (Expressions (Agg)), Component_Type (Typ)); | |
0f95b178 JM |
9339 | end loop; |
9340 | ||
9341 | Set_Aggregate_Bounds (Agg, Bounds); | |
9342 | Set_Etype (Agg, Typ); | |
9343 | Set_Analyzed (Agg); | |
9344 | Rewrite (N, Agg); | |
9345 | Set_Compile_Time_Known_Aggregate (N); | |
9346 | ||
9347 | return True; | |
9348 | end if; | |
9349 | end if; | |
9350 | ||
9351 | else | |
9352 | return False; | |
9353 | end if; | |
9354 | end Static_Array_Aggregate; | |
9b4b0a1a | 9355 | |
937e9676 AC |
9356 | ---------------------------------- |
9357 | -- Two_Dim_Packed_Array_Handled -- | |
9358 | ---------------------------------- | |
9359 | ||
9360 | function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is | |
9361 | Loc : constant Source_Ptr := Sloc (N); | |
9362 | Typ : constant Entity_Id := Etype (N); | |
9363 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
9364 | Comp_Size : constant Int := UI_To_Int (Component_Size (Typ)); | |
9365 | Packed_Array : constant Entity_Id := | |
9366 | Packed_Array_Impl_Type (Base_Type (Typ)); | |
9367 | ||
9368 | One_Comp : Node_Id; | |
9369 | -- Expression in original aggregate | |
9370 | ||
9371 | One_Dim : Node_Id; | |
9372 | -- One-dimensional subaggregate | |
9373 | ||
9374 | begin | |
9375 | ||
9376 | -- For now, only deal with cases where an integral number of elements | |
9377 | -- fit in a single byte. This includes the most common boolean case. | |
9378 | ||
9379 | if not (Comp_Size = 1 or else | |
9380 | Comp_Size = 2 or else | |
9381 | Comp_Size = 4) | |
9382 | then | |
9383 | return False; | |
9384 | end if; | |
9385 | ||
c42006e9 | 9386 | Convert_To_Positional (N, Handle_Bit_Packed => True); |
937e9676 AC |
9387 | |
9388 | -- Verify that all components are static | |
9389 | ||
9390 | if Nkind (N) = N_Aggregate | |
9391 | and then Compile_Time_Known_Aggregate (N) | |
9392 | then | |
9393 | null; | |
9394 | ||
9395 | -- The aggregate may have been reanalyzed and converted already | |
9396 | ||
9397 | elsif Nkind (N) /= N_Aggregate then | |
9398 | return True; | |
9399 | ||
9400 | -- If component associations remain, the aggregate is not static | |
9401 | ||
9402 | elsif Present (Component_Associations (N)) then | |
9403 | return False; | |
9404 | ||
9405 | else | |
9406 | One_Dim := First (Expressions (N)); | |
9407 | while Present (One_Dim) loop | |
9408 | if Present (Component_Associations (One_Dim)) then | |
9409 | return False; | |
9410 | end if; | |
9411 | ||
9412 | One_Comp := First (Expressions (One_Dim)); | |
9413 | while Present (One_Comp) loop | |
9414 | if not Is_OK_Static_Expression (One_Comp) then | |
9415 | return False; | |
9416 | end if; | |
9417 | ||
9418 | Next (One_Comp); | |
9419 | end loop; | |
9420 | ||
9421 | Next (One_Dim); | |
9422 | end loop; | |
9423 | end if; | |
9424 | ||
9425 | -- Two-dimensional aggregate is now fully positional so pack one | |
9426 | -- dimension to create a static one-dimensional array, and rewrite | |
9427 | -- as an unchecked conversion to the original type. | |
9428 | ||
9429 | declare | |
9430 | Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array)); | |
9431 | -- The packed array type is a byte array | |
9432 | ||
9433 | Packed_Num : Nat; | |
9434 | -- Number of components accumulated in current byte | |
9435 | ||
9436 | Comps : List_Id; | |
9437 | -- Assembled list of packed values for equivalent aggregate | |
9438 | ||
9439 | Comp_Val : Uint; | |
9440 | -- Integer value of component | |
9441 | ||
9442 | Incr : Int; | |
9443 | -- Step size for packing | |
9444 | ||
9445 | Init_Shift : Int; | |
9446 | -- Endian-dependent start position for packing | |
9447 | ||
9448 | Shift : Int; | |
9449 | -- Current insertion position | |
9450 | ||
9451 | Val : Int; | |
9452 | -- Component of packed array being assembled | |
9453 | ||
9454 | begin | |
9455 | Comps := New_List; | |
9456 | Val := 0; | |
9457 | Packed_Num := 0; | |
9458 | ||
64ac53f4 | 9459 | -- Account for endianness. See corresponding comment in |
937e9676 AC |
9460 | -- Packed_Array_Aggregate_Handled concerning the following. |
9461 | ||
9462 | if Bytes_Big_Endian | |
9463 | xor Debug_Flag_8 | |
9464 | xor Reverse_Storage_Order (Base_Type (Typ)) | |
9465 | then | |
9466 | Init_Shift := Byte_Size - Comp_Size; | |
9467 | Incr := -Comp_Size; | |
9468 | else | |
9469 | Init_Shift := 0; | |
9470 | Incr := +Comp_Size; | |
9471 | end if; | |
9472 | ||
9473 | -- Iterate over each subaggregate | |
9474 | ||
9475 | Shift := Init_Shift; | |
9476 | One_Dim := First (Expressions (N)); | |
9477 | while Present (One_Dim) loop | |
9478 | One_Comp := First (Expressions (One_Dim)); | |
9479 | while Present (One_Comp) loop | |
9480 | if Packed_Num = Byte_Size / Comp_Size then | |
9481 | ||
9482 | -- Byte is complete, add to list of expressions | |
9483 | ||
9484 | Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps); | |
9485 | Val := 0; | |
9486 | Shift := Init_Shift; | |
9487 | Packed_Num := 0; | |
9488 | ||
9489 | else | |
9490 | Comp_Val := Expr_Rep_Value (One_Comp); | |
9491 | ||
9492 | -- Adjust for bias, and strip proper number of bits | |
9493 | ||
9494 | if Has_Biased_Representation (Ctyp) then | |
9495 | Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp)); | |
9496 | end if; | |
9497 | ||
9498 | Comp_Val := Comp_Val mod Uint_2 ** Comp_Size; | |
9499 | Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift); | |
9500 | Shift := Shift + Incr; | |
99859ea7 | 9501 | Next (One_Comp); |
937e9676 AC |
9502 | Packed_Num := Packed_Num + 1; |
9503 | end if; | |
9504 | end loop; | |
9505 | ||
99859ea7 | 9506 | Next (One_Dim); |
937e9676 AC |
9507 | end loop; |
9508 | ||
9509 | if Packed_Num > 0 then | |
9510 | ||
9511 | -- Add final incomplete byte if present | |
9512 | ||
9513 | Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps); | |
9514 | end if; | |
9515 | ||
9516 | Rewrite (N, | |
9517 | Unchecked_Convert_To (Typ, | |
9518 | Make_Qualified_Expression (Loc, | |
9519 | Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc), | |
9520 | Expression => Make_Aggregate (Loc, Expressions => Comps)))); | |
9521 | Analyze_And_Resolve (N); | |
9522 | return True; | |
9523 | end; | |
9524 | end Two_Dim_Packed_Array_Handled; | |
9525 | ||
70482933 | 9526 | end Exp_Aggr; |