]>
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; | |
64 | with Sem_Util; use Sem_Util; | |
ca4bff3a | 65 | use Sem_Util.Storage_Model_Support; |
104f58db BD |
66 | with Sinfo; use Sinfo; |
67 | with Sinfo.Nodes; use Sinfo.Nodes; | |
68 | with Sinfo.Utils; use Sinfo.Utils; | |
69 | with Snames; use Snames; | |
70 | with Stand; use Stand; | |
71 | with Stringt; use Stringt; | |
72 | with Tbuild; use Tbuild; | |
73 | with Uintp; use Uintp; | |
74 | with Urealp; use Urealp; | |
bc50ac71 | 75 | with Warnsw; use Warnsw; |
70482933 RK |
76 | |
77 | package body Exp_Aggr is | |
78 | ||
742084ad MP |
79 | function Build_Assignment_With_Temporary |
80 | (Target : Node_Id; | |
ca4bff3a | 81 | Typ : Entity_Id; |
742084ad MP |
82 | Source : Node_Id) return List_Id; |
83 | -- Returns a list of actions to assign Source to Target of type Typ using | |
ca4bff3a | 84 | -- an extra temporary, which can potentially be large. |
742084ad | 85 | |
70482933 RK |
86 | type Case_Bounds is record |
87 | Choice_Lo : Node_Id; | |
88 | Choice_Hi : Node_Id; | |
89 | Choice_Node : Node_Id; | |
90 | end record; | |
91 | ||
92 | type Case_Table_Type is array (Nat range <>) of Case_Bounds; | |
93 | -- Table type used by Check_Case_Choices procedure | |
94 | ||
9eb8d5b4 AC |
95 | procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id); |
96 | procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id); | |
745f5698 | 97 | procedure Expand_Container_Aggregate (N : Node_Id); |
9eb8d5b4 | 98 | |
a80b1eb7 EB |
99 | function Get_Base_Object (N : Node_Id) return Entity_Id; |
100 | -- Return the base object, i.e. the outermost prefix object, that N refers | |
101 | -- to statically, or Empty if it cannot be determined. The assumption is | |
102 | -- that all dereferences are explicit in the tree rooted at N. | |
103 | ||
df3e68b1 HK |
104 | function Has_Default_Init_Comps (N : Node_Id) return Boolean; |
105 | -- N is an aggregate (record or array). Checks the presence of default | |
106 | -- initialization (<>) in any component (Ada 2005: AI-287). | |
107 | ||
fc84947c | 108 | procedure Initialize_Component |
fc84947c EB |
109 | (N : Node_Id; |
110 | Comp : Node_Id; | |
111 | Comp_Typ : Node_Id; | |
112 | Init_Expr : Node_Id; | |
113 | Stmts : List_Id); | |
c0ceba6c EB |
114 | -- Perform the initialization of component Comp with expected type |
115 | -- Comp_Typ of aggregate N. Init_Expr denotes the initialization | |
fc84947c EB |
116 | -- expression of the component. All generated code is added to Stmts. |
117 | ||
9f51b855 JM |
118 | function Is_CCG_Supported_Aggregate (N : Node_Id) return Boolean; |
119 | -- Return True if aggregate N is located in a context supported by the | |
120 | -- CCG backend; False otherwise. | |
6031f544 | 121 | |
df3e68b1 HK |
122 | function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean; |
123 | -- Returns true if N is an aggregate used to initialize the components | |
b465ef6f | 124 | -- of a statically allocated dispatch table. |
df3e68b1 | 125 | |
937e9676 AC |
126 | function Late_Expansion |
127 | (N : Node_Id; | |
128 | Typ : Entity_Id; | |
129 | Target : Node_Id) return List_Id; | |
130 | -- This routine implements top-down expansion of nested aggregates. In | |
131 | -- doing so, it avoids the generation of temporaries at each level. N is | |
132 | -- a nested record or array aggregate with the Expansion_Delayed flag. | |
133 | -- Typ is the expected type of the aggregate. Target is a (duplicatable) | |
134 | -- expression that will hold the result of the aggregate expansion. | |
135 | ||
136 | function Make_OK_Assignment_Statement | |
137 | (Sloc : Source_Ptr; | |
138 | Name : Node_Id; | |
139 | Expression : Node_Id) return Node_Id; | |
140 | -- This is like Make_Assignment_Statement, except that Assignment_OK | |
141 | -- is set in the left operand. All assignments built by this unit use | |
142 | -- this routine. This is needed to deal with assignments to initialized | |
143 | -- constants that are done in place. | |
144 | ||
3cf3e5c6 | 145 | function Must_Slide |
6732c403 ES |
146 | (Aggr : Node_Id; |
147 | Obj_Type : Entity_Id; | |
3cf3e5c6 AC |
148 | Typ : Entity_Id) return Boolean; |
149 | -- A static array aggregate in an object declaration can in most cases be | |
150 | -- expanded in place. The one exception is when the aggregate is given | |
151 | -- with component associations that specify different bounds from those of | |
152 | -- the type definition in the object declaration. In this pathological | |
153 | -- case the aggregate must slide, and we must introduce an intermediate | |
154 | -- temporary to hold it. | |
155 | -- | |
156 | -- The same holds in an assignment to one-dimensional array of arrays, | |
157 | -- when a component may be given with bounds that differ from those of the | |
158 | -- component type. | |
159 | ||
937e9676 AC |
160 | function Number_Of_Choices (N : Node_Id) return Nat; |
161 | -- Returns the number of discrete choices (not including the others choice | |
162 | -- if present) contained in (sub-)aggregate N. | |
163 | ||
70482933 RK |
164 | procedure Sort_Case_Table (Case_Table : in out Case_Table_Type); |
165 | -- Sort the Case Table using the Lower Bound of each Choice as the key. | |
166 | -- A simple insertion sort is used since the number of choices in a case | |
167 | -- statement of variant part will usually be small and probably in near | |
168 | -- sorted order. | |
169 | ||
170 | ------------------------------------------------------ | |
171 | -- Local subprograms for Record Aggregate Expansion -- | |
172 | ------------------------------------------------------ | |
173 | ||
d4dfb005 BD |
174 | function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean; |
175 | -- True if N is an aggregate (possibly qualified or converted) that is | |
176 | -- being returned from a build-in-place function. | |
177 | ||
df3e68b1 | 178 | function Build_Record_Aggr_Code |
f7e6fc47 RD |
179 | (N : Node_Id; |
180 | Typ : Entity_Id; | |
181 | Lhs : Node_Id) return List_Id; | |
df3e68b1 HK |
182 | -- N is an N_Aggregate or an N_Extension_Aggregate. Typ is the type of the |
183 | -- aggregate. Target is an expression containing the location on which the | |
184 | -- component by component assignments will take place. Returns the list of | |
185 | -- assignments plus all other adjustments needed for tagged and controlled | |
203ddcea | 186 | -- types. |
df3e68b1 HK |
187 | |
188 | procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id); | |
d4dfb005 | 189 | -- Transform a record aggregate into a sequence of assignments performed |
64ac53f4 | 190 | -- component by component. N is an N_Aggregate or N_Extension_Aggregate. |
d4dfb005 | 191 | -- Typ is the type of the record aggregate. |
df3e68b1 | 192 | |
70482933 RK |
193 | procedure Expand_Record_Aggregate |
194 | (N : Node_Id; | |
195 | Orig_Tag : Node_Id := Empty; | |
196 | Parent_Expr : Node_Id := Empty); | |
197 | -- This is the top level procedure for record aggregate expansion. | |
198 | -- Expansion for record aggregates needs expand aggregates for tagged | |
199 | -- record types. Specifically Expand_Record_Aggregate adds the Tag | |
200 | -- field in front of the Component_Association list that was created | |
201 | -- during resolution by Resolve_Record_Aggregate. | |
202 | -- | |
203 | -- N is the record aggregate node. | |
204 | -- Orig_Tag is the value of the Tag that has to be provided for this | |
205 | -- specific aggregate. It carries the tag corresponding to the type | |
206 | -- of the outermost aggregate during the recursive expansion | |
207 | -- Parent_Expr is the ancestor part of the original extension | |
208 | -- aggregate | |
209 | ||
fbf5a39b | 210 | function Has_Mutable_Components (Typ : Entity_Id) return Boolean; |
b465ef6f | 211 | -- Return true if one of the components is of a discriminated type with |
fbf5a39b AC |
212 | -- defaults. An aggregate for a type with mutable components must be |
213 | -- expanded into individual assignments. | |
214 | ||
a80b1eb7 EB |
215 | function In_Place_Assign_OK |
216 | (N : Node_Id; | |
217 | Target_Object : Entity_Id := Empty) return Boolean; | |
4ff5aa0c AC |
218 | -- Predicate to determine whether an aggregate assignment can be done in |
219 | -- place, because none of the new values can depend on the components of | |
220 | -- the target of the assignment. | |
221 | ||
07fc65c4 GB |
222 | procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id); |
223 | -- If the type of the aggregate is a type extension with renamed discrimi- | |
224 | -- nants, we must initialize the hidden discriminants of the parent. | |
225 | -- Otherwise, the target object must not be initialized. The discriminants | |
226 | -- are initialized by calling the initialization procedure for the type. | |
227 | -- This is incorrect if the initialization of other components has any | |
228 | -- side effects. We restrict this call to the case where the parent type | |
229 | -- has a variant part, because this is the only case where the hidden | |
230 | -- discriminants are accessed, namely when calling discriminant checking | |
231 | -- functions of the parent type, and when applying a stream attribute to | |
232 | -- an object of the derived type. | |
233 | ||
70482933 | 234 | ----------------------------------------------------- |
07fc65c4 | 235 | -- Local Subprograms for Array Aggregate Expansion -- |
70482933 RK |
236 | ----------------------------------------------------- |
237 | ||
2fedcc18 EB |
238 | function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean; |
239 | -- Returns true if an aggregate assignment can be done by the back end | |
240 | ||
eaf6e63a | 241 | function Aggr_Size_OK (N : Node_Id) return Boolean; |
841dd0f5 AC |
242 | -- Very large static aggregates present problems to the back-end, and are |
243 | -- transformed into assignments and loops. This function verifies that the | |
244 | -- total number of components of an aggregate is acceptable for rewriting | |
74e7891f RD |
245 | -- into a purely positional static form. Aggr_Size_OK must be called before |
246 | -- calling Flatten. | |
247 | -- | |
841dd0f5 | 248 | -- This function also detects and warns about one-component aggregates that |
d940c627 | 249 | -- appear in a nonstatic context. Even if the component value is static, |
841dd0f5 | 250 | -- such an aggregate must be expanded into an assignment. |
643a0839 | 251 | |
df3e68b1 HK |
252 | function Backend_Processing_Possible (N : Node_Id) return Boolean; |
253 | -- This function checks if array aggregate N can be processed directly | |
b465ef6f | 254 | -- by the backend. If this is the case, True is returned. |
df3e68b1 HK |
255 | |
256 | function Build_Array_Aggr_Code | |
257 | (N : Node_Id; | |
258 | Ctype : Entity_Id; | |
259 | Index : Node_Id; | |
260 | Into : Node_Id; | |
261 | Scalar_Comp : Boolean; | |
262 | Indexes : List_Id := No_List) return List_Id; | |
263 | -- This recursive routine returns a list of statements containing the | |
264 | -- loops and assignments that are needed for the expansion of the array | |
265 | -- aggregate N. | |
266 | -- | |
267 | -- N is the (sub-)aggregate node to be expanded into code. This node has | |
268 | -- been fully analyzed, and its Etype is properly set. | |
269 | -- | |
d74716b3 | 270 | -- Index is the index node corresponding to the array subaggregate N |
df3e68b1 HK |
271 | -- |
272 | -- Into is the target expression into which we are copying the aggregate. | |
273 | -- Note that this node may not have been analyzed yet, and so the Etype | |
274 | -- field may not be set. | |
275 | -- | |
276 | -- Scalar_Comp is True if the component type of the aggregate is scalar | |
277 | -- | |
278 | -- Indexes is the current list of expressions used to index the object we | |
279 | -- are writing into. | |
280 | ||
6f639c98 ES |
281 | procedure Convert_Array_Aggr_In_Allocator |
282 | (Decl : Node_Id; | |
283 | Aggr : Node_Id; | |
284 | Target : Node_Id); | |
285 | -- If the aggregate appears within an allocator and can be expanded in | |
286 | -- place, this routine generates the individual assignments to components | |
287 | -- of the designated object. This is an optimization over the general | |
288 | -- case, where a temporary is first created on the stack and then used to | |
289 | -- construct the allocated object on the heap. | |
290 | ||
07fc65c4 | 291 | procedure Convert_To_Positional |
c42006e9 AC |
292 | (N : Node_Id; |
293 | Handle_Bit_Packed : Boolean := False); | |
07fc65c4 | 294 | -- If possible, convert named notation to positional notation. This |
3cf3e5c6 AC |
295 | -- conversion is possible only in some static cases. If the conversion is |
296 | -- possible, then N is rewritten with the analyzed converted aggregate. | |
c42006e9 | 297 | -- The parameter Handle_Bit_Packed is usually set False (since we do |
3cf3e5c6 AC |
298 | -- not expect the back end to handle bit packed arrays, so the normal case |
299 | -- of conversion is pointless), but in the special case of a call from | |
300 | -- Packed_Array_Aggregate_Handled, we set this parameter to True, since | |
301 | -- these are cases we handle in there. | |
07fc65c4 | 302 | |
70482933 RK |
303 | procedure Expand_Array_Aggregate (N : Node_Id); |
304 | -- This is the top-level routine to perform array aggregate expansion. | |
305 | -- N is the N_Aggregate node to be expanded. | |
306 | ||
dc3af7e2 | 307 | function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean; |
dc3af7e2 AC |
308 | -- For two-dimensional packed aggregates with constant bounds and constant |
309 | -- components, it is preferable to pack the inner aggregates because the | |
310 | -- whole matrix can then be presented to the back-end as a one-dimensional | |
311 | -- list of literals. This is much more efficient than expanding into single | |
2791be24 AC |
312 | -- component assignments. This function determines if the type Typ is for |
313 | -- an array that is suitable for this optimization: it returns True if Typ | |
314 | -- is a two dimensional bit packed array with component size 1, 2, or 4. | |
dc3af7e2 | 315 | |
c42006e9 | 316 | function Max_Aggregate_Size |
eaf6e63a | 317 | (N : Node_Id; |
c42006e9 | 318 | Default_Size : Nat := 5000) return Nat; |
eaf6e63a BD |
319 | -- Return the max size for a static aggregate N. Return Default_Size if no |
320 | -- other special criteria trigger. | |
c42006e9 | 321 | |
07fc65c4 GB |
322 | function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean; |
323 | -- Given an array aggregate, this function handles the case of a packed | |
324 | -- array aggregate with all constant values, where the aggregate can be | |
325 | -- evaluated at compile time. If this is possible, then N is rewritten | |
326 | -- to be its proper compile time value with all the components properly | |
50decc81 RD |
327 | -- assembled. The expression is analyzed and resolved and True is returned. |
328 | -- If this transformation is not possible, N is unchanged and False is | |
329 | -- returned. | |
07fc65c4 | 330 | |
5eeeed5e AC |
331 | function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean; |
332 | -- If the type of the aggregate is a two-dimensional bit_packed array | |
333 | -- it may be transformed into an array of bytes with constant values, | |
334 | -- and presented to the back-end as a static value. The function returns | |
335 | -- false if this transformation cannot be performed. THis is similar to, | |
336 | -- and reuses part of the machinery in Packed_Array_Aggregate_Handled. | |
337 | ||
2fedcc18 EB |
338 | ------------------------------------ |
339 | -- Aggr_Assignment_OK_For_Backend -- | |
340 | ------------------------------------ | |
341 | ||
342 | -- Back-end processing by Gigi/gcc is possible only if all the following | |
343 | -- conditions are met: | |
344 | ||
345 | -- 1. N consists of a single OTHERS choice, possibly recursively, or | |
346 | -- of a single choice, possibly recursively, if it is surrounded by | |
347 | -- a qualified expression whose subtype mark is unconstrained. | |
348 | ||
349 | -- 2. The array type has no null ranges (the purpose of this is to | |
350 | -- avoid a bogus warning for an out-of-range value). | |
351 | ||
352 | -- 3. The array type has no atomic components | |
353 | ||
354 | -- 4. The component type is elementary | |
355 | ||
356 | -- 5. The component size is a multiple of Storage_Unit | |
357 | ||
358 | -- 6. The component size is Storage_Unit or the value is of the form | |
359 | -- M * (1 + A**1 + A**2 + .. A**(K-1)) where A = 2**(Storage_Unit) | |
360 | -- and M in 0 .. A-1. This can also be viewed as K occurrences of | |
361 | -- the Storage_Unit value M, concatenated together. | |
362 | ||
363 | -- The ultimate goal is to generate a call to a fast memset routine | |
364 | -- specifically optimized for the target. | |
365 | ||
366 | function Aggr_Assignment_OK_For_Backend (N : Node_Id) return Boolean is | |
2fedcc18 EB |
367 | |
368 | function Is_OK_Aggregate (Aggr : Node_Id) return Boolean; | |
369 | -- Return true if Aggr is suitable for back-end assignment | |
370 | ||
371 | --------------------- | |
372 | -- Is_OK_Aggregate -- | |
373 | --------------------- | |
374 | ||
375 | function Is_OK_Aggregate (Aggr : Node_Id) return Boolean is | |
376 | Assoc : constant List_Id := Component_Associations (Aggr); | |
377 | ||
378 | begin | |
379 | -- An "others" aggregate is most likely OK, but see below | |
380 | ||
381 | if Is_Others_Aggregate (Aggr) then | |
382 | null; | |
383 | ||
384 | -- An aggregate with a single choice requires a qualified expression | |
385 | -- whose subtype mark is an unconstrained type because we need it to | |
386 | -- have the semantics of an "others" aggregate. | |
387 | ||
388 | elsif Nkind (Parent (N)) = N_Qualified_Expression | |
389 | and then not Is_Constrained (Entity (Subtype_Mark (Parent (N)))) | |
390 | and then Is_Single_Aggregate (Aggr) | |
391 | then | |
392 | null; | |
393 | ||
394 | -- The other cases are not OK | |
395 | ||
396 | else | |
397 | return False; | |
398 | end if; | |
399 | ||
400 | -- In any case we do not support an iterated association | |
401 | ||
402 | return Nkind (First (Assoc)) /= N_Iterated_Component_Association; | |
403 | end Is_OK_Aggregate; | |
404 | ||
7c4f3267 BD |
405 | Bounds : Range_Nodes; |
406 | Csiz : Uint := No_Uint; | |
407 | Ctyp : Entity_Id; | |
408 | Expr : Node_Id; | |
409 | Index : Entity_Id; | |
410 | Nunits : Int; | |
411 | Remainder : Uint; | |
412 | Value : Uint; | |
413 | ||
b120ca61 EB |
414 | -- Start of processing for Aggr_Assignment_OK_For_Backend |
415 | ||
2fedcc18 EB |
416 | begin |
417 | -- Back end doesn't know about <> | |
418 | ||
419 | if Has_Default_Init_Comps (N) then | |
420 | return False; | |
421 | end if; | |
422 | ||
423 | -- Recurse as far as possible to find the innermost component type | |
424 | ||
425 | Ctyp := Etype (N); | |
426 | Expr := N; | |
427 | while Is_Array_Type (Ctyp) loop | |
428 | if Nkind (Expr) /= N_Aggregate | |
429 | or else not Is_OK_Aggregate (Expr) | |
430 | then | |
431 | return False; | |
432 | end if; | |
433 | ||
434 | Index := First_Index (Ctyp); | |
435 | while Present (Index) loop | |
7c4f3267 | 436 | Bounds := Get_Index_Bounds (Index); |
2fedcc18 | 437 | |
7c4f3267 | 438 | if Is_Null_Range (Bounds.First, Bounds.Last) then |
2fedcc18 EB |
439 | return False; |
440 | end if; | |
441 | ||
442 | Next_Index (Index); | |
443 | end loop; | |
444 | ||
445 | Expr := Expression (First (Component_Associations (Expr))); | |
446 | ||
447 | for J in 1 .. Number_Dimensions (Ctyp) - 1 loop | |
448 | if Nkind (Expr) /= N_Aggregate | |
449 | or else not Is_OK_Aggregate (Expr) | |
450 | then | |
451 | return False; | |
452 | end if; | |
453 | ||
454 | Expr := Expression (First (Component_Associations (Expr))); | |
455 | end loop; | |
456 | ||
457 | if Has_Atomic_Components (Ctyp) then | |
458 | return False; | |
459 | end if; | |
460 | ||
461 | Csiz := Component_Size (Ctyp); | |
462 | Ctyp := Component_Type (Ctyp); | |
463 | ||
b120ca61 | 464 | if Is_Full_Access (Ctyp) then |
2fedcc18 EB |
465 | return False; |
466 | end if; | |
467 | end loop; | |
468 | ||
469 | -- Access types need to be dealt with specially | |
470 | ||
471 | if Is_Access_Type (Ctyp) then | |
472 | ||
473 | -- Component_Size is not set by Layout_Type if the component | |
474 | -- type is an access type ??? | |
475 | ||
476 | Csiz := Esize (Ctyp); | |
477 | ||
478 | -- Fat pointers are rejected as they are not really elementary | |
479 | -- for the backend. | |
480 | ||
36e38022 | 481 | if No (Csiz) or else Csiz /= System_Address_Size then |
2fedcc18 EB |
482 | return False; |
483 | end if; | |
484 | ||
485 | -- The supported expressions are NULL and constants, others are | |
486 | -- rejected upfront to avoid being analyzed below, which can be | |
487 | -- problematic for some of them, for example allocators. | |
488 | ||
489 | if Nkind (Expr) /= N_Null and then not Is_Entity_Name (Expr) then | |
490 | return False; | |
491 | end if; | |
492 | ||
493 | -- Scalar types are OK if their size is a multiple of Storage_Unit | |
494 | ||
36e38022 | 495 | elsif Is_Scalar_Type (Ctyp) and then Present (Csiz) then |
2fedcc18 EB |
496 | |
497 | if Csiz mod System_Storage_Unit /= 0 then | |
498 | return False; | |
499 | end if; | |
500 | ||
501 | -- Composite types are rejected | |
502 | ||
503 | else | |
504 | return False; | |
505 | end if; | |
506 | ||
507 | -- If the expression has side effects (e.g. contains calls with | |
508 | -- potential side effects) reject as well. We only preanalyze the | |
509 | -- expression to prevent the removal of intended side effects. | |
510 | ||
511 | Preanalyze_And_Resolve (Expr, Ctyp); | |
512 | ||
513 | if not Side_Effect_Free (Expr) then | |
514 | return False; | |
515 | end if; | |
516 | ||
517 | -- The expression needs to be analyzed if True is returned | |
518 | ||
519 | Analyze_And_Resolve (Expr, Ctyp); | |
520 | ||
521 | -- Strip away any conversions from the expression as they simply | |
522 | -- qualify the real expression. | |
523 | ||
4a08c95c | 524 | while Nkind (Expr) in N_Unchecked_Type_Conversion | N_Type_Conversion |
2fedcc18 EB |
525 | loop |
526 | Expr := Expression (Expr); | |
527 | end loop; | |
528 | ||
529 | Nunits := UI_To_Int (Csiz) / System_Storage_Unit; | |
530 | ||
531 | if Nunits = 1 then | |
532 | return True; | |
533 | end if; | |
534 | ||
535 | if not Compile_Time_Known_Value (Expr) then | |
536 | return False; | |
537 | end if; | |
538 | ||
539 | -- The only supported value for floating point is 0.0 | |
540 | ||
541 | if Is_Floating_Point_Type (Ctyp) then | |
542 | return Expr_Value_R (Expr) = Ureal_0; | |
543 | end if; | |
544 | ||
545 | -- For other types, we can look into the value as an integer, which | |
546 | -- means the representation value for enumeration literals. | |
547 | ||
548 | Value := Expr_Rep_Value (Expr); | |
549 | ||
550 | if Has_Biased_Representation (Ctyp) then | |
551 | Value := Value - Expr_Value (Type_Low_Bound (Ctyp)); | |
552 | end if; | |
553 | ||
554 | -- Values 0 and -1 immediately satisfy the last check | |
555 | ||
556 | if Value = Uint_0 or else Value = Uint_Minus_1 then | |
557 | return True; | |
558 | end if; | |
559 | ||
560 | -- We need to work with an unsigned value | |
561 | ||
562 | if Value < 0 then | |
563 | Value := Value + 2**(System_Storage_Unit * Nunits); | |
564 | end if; | |
565 | ||
566 | Remainder := Value rem 2**System_Storage_Unit; | |
567 | ||
568 | for J in 1 .. Nunits - 1 loop | |
569 | Value := Value / 2**System_Storage_Unit; | |
570 | ||
571 | if Value rem 2**System_Storage_Unit /= Remainder then | |
572 | return False; | |
573 | end if; | |
574 | end loop; | |
575 | ||
576 | return True; | |
577 | end Aggr_Assignment_OK_For_Backend; | |
578 | ||
643a0839 ES |
579 | ------------------ |
580 | -- Aggr_Size_OK -- | |
581 | ------------------ | |
582 | ||
eaf6e63a BD |
583 | function Aggr_Size_OK (N : Node_Id) return Boolean is |
584 | Typ : constant Entity_Id := Etype (N); | |
643a0839 ES |
585 | Lo : Node_Id; |
586 | Hi : Node_Id; | |
587 | Indx : Node_Id; | |
4167b075 | 588 | Size : Uint; |
643a0839 ES |
589 | Lov : Uint; |
590 | Hiv : Uint; | |
591 | ||
303fbb20 AC |
592 | Max_Aggr_Size : Nat; |
593 | -- Determines the maximum size of an array aggregate produced by | |
594 | -- converting named to positional notation (e.g. from others clauses). | |
595 | -- This avoids running away with attempts to convert huge aggregates, | |
596 | -- which hit memory limits in the backend. | |
643a0839 | 597 | |
16e764a7 | 598 | function Component_Count (T : Entity_Id) return Nat; |
457cee0b | 599 | -- The limit is applied to the total number of subcomponents that the |
643a0839 ES |
600 | -- aggregate will have, which is the number of static expressions |
601 | -- that will appear in the flattened array. This requires a recursive | |
16b05213 | 602 | -- computation of the number of scalar components of the structure. |
643a0839 ES |
603 | |
604 | --------------------- | |
605 | -- Component_Count -- | |
606 | --------------------- | |
607 | ||
16e764a7 AC |
608 | function Component_Count (T : Entity_Id) return Nat is |
609 | Res : Nat := 0; | |
643a0839 ES |
610 | Comp : Entity_Id; |
611 | ||
612 | begin | |
613 | if Is_Scalar_Type (T) then | |
614 | return 1; | |
615 | ||
616 | elsif Is_Record_Type (T) then | |
617 | Comp := First_Component (T); | |
618 | while Present (Comp) loop | |
619 | Res := Res + Component_Count (Etype (Comp)); | |
620 | Next_Component (Comp); | |
621 | end loop; | |
622 | ||
623 | return Res; | |
624 | ||
625 | elsif Is_Array_Type (T) then | |
626 | declare | |
627 | Lo : constant Node_Id := | |
15f0f591 | 628 | Type_Low_Bound (Etype (First_Index (T))); |
643a0839 | 629 | Hi : constant Node_Id := |
15f0f591 | 630 | Type_High_Bound (Etype (First_Index (T))); |
643a0839 | 631 | |
16e764a7 | 632 | Siz : constant Nat := Component_Count (Component_Type (T)); |
643a0839 ES |
633 | |
634 | begin | |
b4213ffd AC |
635 | -- Check for superflat arrays, i.e. arrays with such bounds |
636 | -- as 4 .. 2, to insure that this function never returns a | |
637 | -- meaningless negative value. | |
638 | ||
643a0839 ES |
639 | if not Compile_Time_Known_Value (Lo) |
640 | or else not Compile_Time_Known_Value (Hi) | |
b4213ffd | 641 | or else Expr_Value (Hi) < Expr_Value (Lo) |
643a0839 ES |
642 | then |
643 | return 0; | |
b4213ffd | 644 | |
643a0839 | 645 | else |
457cee0b AC |
646 | -- If the number of components is greater than Int'Last, |
647 | -- then return Int'Last, so caller will return False (Aggr | |
648 | -- size is not OK). Otherwise, UI_To_Int will crash. | |
649 | ||
650 | declare | |
651 | UI : constant Uint := | |
16b8ba10 | 652 | (Expr_Value (Hi) - Expr_Value (Lo) + 1) * Siz; |
457cee0b AC |
653 | begin |
654 | if UI_Is_In_Int_Range (UI) then | |
16b8ba10 | 655 | return UI_To_Int (UI); |
457cee0b AC |
656 | else |
657 | return Int'Last; | |
658 | end if; | |
659 | end; | |
643a0839 ES |
660 | end if; |
661 | end; | |
662 | ||
663 | else | |
664 | -- Can only be a null for an access type | |
665 | ||
666 | return 1; | |
667 | end if; | |
668 | end Component_Count; | |
669 | ||
670 | -- Start of processing for Aggr_Size_OK | |
671 | ||
672 | begin | |
c42006e9 | 673 | -- We bump the maximum size unless the aggregate has a single component |
b9ec8463 | 674 | -- association, which will be more efficient if implemented with a loop. |
73b670e3 | 675 | -- The -gnatd_g switch disables this bumping. |
b9ec8463 | 676 | |
73b670e3 BD |
677 | if (No (Expressions (N)) |
678 | and then No (Next (First (Component_Associations (N))))) | |
679 | or else Debug_Flag_Underscore_G | |
b9ec8463 | 680 | then |
eaf6e63a | 681 | Max_Aggr_Size := Max_Aggregate_Size (N); |
c42006e9 | 682 | else |
eaf6e63a | 683 | Max_Aggr_Size := Max_Aggregate_Size (N, 500_000); |
303fbb20 AC |
684 | end if; |
685 | ||
4167b075 | 686 | Size := UI_From_Int (Component_Count (Component_Type (Typ))); |
643a0839 | 687 | |
5277cab6 | 688 | Indx := First_Index (Typ); |
643a0839 ES |
689 | while Present (Indx) loop |
690 | Lo := Type_Low_Bound (Etype (Indx)); | |
691 | Hi := Type_High_Bound (Etype (Indx)); | |
692 | ||
693 | -- Bounds need to be known at compile time | |
694 | ||
695 | if not Compile_Time_Known_Value (Lo) | |
696 | or else not Compile_Time_Known_Value (Hi) | |
697 | then | |
698 | return False; | |
699 | end if; | |
700 | ||
701 | Lov := Expr_Value (Lo); | |
702 | Hiv := Expr_Value (Hi); | |
703 | ||
704 | -- A flat array is always safe | |
705 | ||
706 | if Hiv < Lov then | |
707 | return True; | |
708 | end if; | |
709 | ||
86038a88 | 710 | -- One-component aggregates are suspicious, and if the context type |
d940c627 | 711 | -- is an object declaration with nonstatic bounds it will trip gcc; |
86038a88 | 712 | -- such an aggregate must be expanded into a single assignment. |
58fda84d | 713 | |
36a66365 | 714 | if Hiv = Lov and then Nkind (Parent (N)) = N_Object_Declaration then |
58fda84d ES |
715 | declare |
716 | Index_Type : constant Entity_Id := | |
15f0f591 AC |
717 | Etype |
718 | (First_Index (Etype (Defining_Identifier (Parent (N))))); | |
86038a88 RD |
719 | Indx : Node_Id; |
720 | ||
58fda84d ES |
721 | begin |
722 | if not Compile_Time_Known_Value (Type_Low_Bound (Index_Type)) | |
36a66365 AC |
723 | or else not Compile_Time_Known_Value |
724 | (Type_High_Bound (Index_Type)) | |
58fda84d ES |
725 | then |
726 | if Present (Component_Associations (N)) then | |
727 | Indx := | |
00f45f30 AC |
728 | First |
729 | (Choice_List (First (Component_Associations (N)))); | |
324ac540 | 730 | |
58fda84d ES |
731 | if Is_Entity_Name (Indx) |
732 | and then not Is_Type (Entity (Indx)) | |
733 | then | |
734 | Error_Msg_N | |
324ac540 AC |
735 | ("single component aggregate in " |
736 | & "non-static context??", Indx); | |
737 | Error_Msg_N ("\maybe subtype name was meant??", Indx); | |
58fda84d ES |
738 | end if; |
739 | end if; | |
740 | ||
741 | return False; | |
742 | end if; | |
743 | end; | |
744 | end if; | |
745 | ||
643a0839 ES |
746 | declare |
747 | Rng : constant Uint := Hiv - Lov + 1; | |
748 | ||
749 | begin | |
750 | -- Check if size is too large | |
751 | ||
752 | if not UI_Is_In_Int_Range (Rng) then | |
753 | return False; | |
754 | end if; | |
755 | ||
4167b075 GD |
756 | -- Compute the size using universal arithmetic to avoid the |
757 | -- possibility of overflow on very large aggregates. | |
643a0839 | 758 | |
4167b075 GD |
759 | Size := Size * Rng; |
760 | ||
761 | if Size <= 0 | |
762 | or else Size > Max_Aggr_Size | |
763 | then | |
764 | return False; | |
765 | end if; | |
766 | end; | |
643a0839 ES |
767 | |
768 | -- Bounds must be in integer range, for later array construction | |
769 | ||
770 | if not UI_Is_In_Int_Range (Lov) | |
771 | or else | |
772 | not UI_Is_In_Int_Range (Hiv) | |
773 | then | |
774 | return False; | |
775 | end if; | |
776 | ||
777 | Next_Index (Indx); | |
778 | end loop; | |
779 | ||
780 | return True; | |
781 | end Aggr_Size_OK; | |
782 | ||
70482933 RK |
783 | --------------------------------- |
784 | -- Backend_Processing_Possible -- | |
785 | --------------------------------- | |
786 | ||
787 | -- Backend processing by Gigi/gcc is possible only if all the following | |
788 | -- conditions are met: | |
789 | ||
790 | -- 1. N is fully positional | |
791 | ||
792 | -- 2. N is not a bit-packed array aggregate; | |
793 | ||
794 | -- 3. The size of N's array type must be known at compile time. Note | |
795 | -- that this implies that the component size is also known | |
796 | ||
797 | -- 4. The array type of N does not follow the Fortran layout convention | |
798 | -- or if it does it must be 1 dimensional. | |
799 | ||
0f95b178 JM |
800 | -- 5. The array component type may not be tagged (which could necessitate |
801 | -- reassignment of proper tags). | |
70482933 | 802 | |
0f95b178 JM |
803 | -- 6. The array component type must not have unaligned bit components |
804 | ||
805 | -- 7. None of the components of the aggregate may be bit unaligned | |
806 | -- components. | |
807 | ||
808 | -- 8. There cannot be delayed components, since we do not know enough | |
809 | -- at this stage to know if back end processing is possible. | |
810 | ||
811 | -- 9. There cannot be any discriminated record components, since the | |
812 | -- back end cannot handle this complex case. | |
91b1417d | 813 | |
7f4c1903 | 814 | -- 10. No controlled actions need to be generated for components |
a8f59a33 | 815 | |
7e22a38c AC |
816 | -- 11. When generating C code, N must be part of a N_Object_Declaration |
817 | ||
2d6aa715 AC |
818 | -- 12. When generating C code, N must not include function calls |
819 | ||
70482933 RK |
820 | function Backend_Processing_Possible (N : Node_Id) return Boolean is |
821 | Typ : constant Entity_Id := Etype (N); | |
3cf3e5c6 | 822 | -- Typ is the correct constrained array subtype of the aggregate |
70482933 | 823 | |
0f95b178 JM |
824 | function Component_Check (N : Node_Id; Index : Node_Id) return Boolean; |
825 | -- This routine checks components of aggregate N, enforcing checks | |
d74716b3 | 826 | -- 1, 7, 8, 9, 11, and 12. In the multidimensional case, these checks |
2d6aa715 | 827 | -- are performed on subaggregates. The Index value is the current index |
d74716b3 | 828 | -- being checked in the multidimensional case. |
70482933 | 829 | |
0f95b178 JM |
830 | --------------------- |
831 | -- Component_Check -- | |
832 | --------------------- | |
70482933 | 833 | |
0f95b178 | 834 | function Component_Check (N : Node_Id; Index : Node_Id) return Boolean is |
35f4f238 JM |
835 | function Ultimate_Original_Expression (N : Node_Id) return Node_Id; |
836 | -- Given a type conversion or an unchecked type conversion N, return | |
837 | -- its innermost original expression. | |
838 | ||
839 | ---------------------------------- | |
840 | -- Ultimate_Original_Expression -- | |
841 | ---------------------------------- | |
842 | ||
843 | function Ultimate_Original_Expression (N : Node_Id) return Node_Id is | |
844 | Expr : Node_Id := Original_Node (N); | |
845 | ||
846 | begin | |
4a08c95c AC |
847 | while Nkind (Expr) in |
848 | N_Type_Conversion | N_Unchecked_Type_Conversion | |
35f4f238 JM |
849 | loop |
850 | Expr := Original_Node (Expression (Expr)); | |
851 | end loop; | |
852 | ||
853 | return Expr; | |
854 | end Ultimate_Original_Expression; | |
855 | ||
856 | -- Local variables | |
857 | ||
70482933 RK |
858 | Expr : Node_Id; |
859 | ||
bbe008b6 HK |
860 | -- Start of processing for Component_Check |
861 | ||
70482933 | 862 | begin |
0f95b178 | 863 | -- Checks 1: (no component associations) |
70482933 RK |
864 | |
865 | if Present (Component_Associations (N)) then | |
866 | return False; | |
867 | end if; | |
868 | ||
7ec25b2b AC |
869 | -- Checks 11: The C code generator cannot handle aggregates that are |
870 | -- not part of an object declaration. | |
7e22a38c | 871 | |
4ff5aa0c AC |
872 | if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then |
873 | return False; | |
7e22a38c AC |
874 | end if; |
875 | ||
0f95b178 JM |
876 | -- Checks on components |
877 | ||
70482933 RK |
878 | -- Recurse to check subaggregates, which may appear in qualified |
879 | -- expressions. If delayed, the front-end will have to expand. | |
d940c627 | 880 | -- If the component is a discriminated record, treat as nonstatic, |
5277cab6 | 881 | -- as the back-end cannot handle this properly. |
70482933 RK |
882 | |
883 | Expr := First (Expressions (N)); | |
70482933 | 884 | while Present (Expr) loop |
0f95b178 JM |
885 | |
886 | -- Checks 8: (no delayed components) | |
887 | ||
70482933 RK |
888 | if Is_Delayed_Aggregate (Expr) then |
889 | return False; | |
890 | end if; | |
891 | ||
0f95b178 JM |
892 | -- Checks 9: (no discriminated records) |
893 | ||
5277cab6 ES |
894 | if Present (Etype (Expr)) |
895 | and then Is_Record_Type (Etype (Expr)) | |
896 | and then Has_Discriminants (Etype (Expr)) | |
897 | then | |
898 | return False; | |
899 | end if; | |
900 | ||
0f95b178 JM |
901 | -- Checks 7. Component must not be bit aligned component |
902 | ||
903 | if Possible_Bit_Aligned_Component (Expr) then | |
904 | return False; | |
905 | end if; | |
906 | ||
2d6aa715 AC |
907 | -- Checks 12: (no function call) |
908 | ||
35f4f238 JM |
909 | if Modify_Tree_For_C |
910 | and then | |
911 | Nkind (Ultimate_Original_Expression (Expr)) = N_Function_Call | |
912 | then | |
2d6aa715 AC |
913 | return False; |
914 | end if; | |
915 | ||
0f95b178 JM |
916 | -- Recursion to following indexes for multiple dimension case |
917 | ||
70482933 | 918 | if Present (Next_Index (Index)) |
36a66365 | 919 | and then not Component_Check (Expr, Next_Index (Index)) |
70482933 RK |
920 | then |
921 | return False; | |
922 | end if; | |
923 | ||
0f95b178 JM |
924 | -- All checks for that component finished, on to next |
925 | ||
70482933 RK |
926 | Next (Expr); |
927 | end loop; | |
928 | ||
929 | return True; | |
0f95b178 | 930 | end Component_Check; |
70482933 RK |
931 | |
932 | -- Start of processing for Backend_Processing_Possible | |
933 | ||
934 | begin | |
a8f59a33 | 935 | -- Checks 2 (array not bit packed) and 10 (no controlled actions) |
70482933 | 936 | |
a8f59a33 | 937 | if Is_Bit_Packed_Array (Typ) or else Needs_Finalization (Typ) then |
70482933 RK |
938 | return False; |
939 | end if; | |
940 | ||
a38ff9b1 ES |
941 | -- If component is limited, aggregate must be expanded because each |
942 | -- component assignment must be built in place. | |
943 | ||
51245e2d | 944 | if Is_Limited_View (Component_Type (Typ)) then |
a38ff9b1 ES |
945 | return False; |
946 | end if; | |
947 | ||
d74716b3 | 948 | -- Checks 4 (array must not be multidimensional Fortran case) |
70482933 RK |
949 | |
950 | if Convention (Typ) = Convention_Fortran | |
951 | and then Number_Dimensions (Typ) > 1 | |
952 | then | |
953 | return False; | |
954 | end if; | |
955 | ||
956 | -- Checks 3 (size of array must be known at compile time) | |
957 | ||
958 | if not Size_Known_At_Compile_Time (Typ) then | |
959 | return False; | |
960 | end if; | |
961 | ||
0f95b178 | 962 | -- Checks on components |
70482933 | 963 | |
0f95b178 | 964 | if not Component_Check (N, First_Index (Typ)) then |
70482933 RK |
965 | return False; |
966 | end if; | |
967 | ||
0f95b178 | 968 | -- Checks 5 (if the component type is tagged, then we may need to do |
36a66365 AC |
969 | -- tag adjustments. Perhaps this should be refined to check for any |
970 | -- component associations that actually need tag adjustment, similar | |
d4dfb005 BD |
971 | -- to the test in Component_OK_For_Backend for record aggregates with |
972 | -- tagged components, but not clear whether it's worthwhile ???; in the | |
973 | -- case of virtual machines (no Tagged_Type_Expansion), object tags are | |
974 | -- handled implicitly). | |
70482933 | 975 | |
1f110335 AC |
976 | if Is_Tagged_Type (Component_Type (Typ)) |
977 | and then Tagged_Type_Expansion | |
978 | then | |
70482933 RK |
979 | return False; |
980 | end if; | |
981 | ||
91b1417d AC |
982 | -- Checks 6 (component type must not have bit aligned components) |
983 | ||
984 | if Type_May_Have_Bit_Aligned_Components (Component_Type (Typ)) then | |
985 | return False; | |
986 | end if; | |
987 | ||
70482933 RK |
988 | -- Backend processing is possible |
989 | ||
70482933 RK |
990 | return True; |
991 | end Backend_Processing_Possible; | |
992 | ||
993 | --------------------------- | |
994 | -- Build_Array_Aggr_Code -- | |
995 | --------------------------- | |
996 | ||
997 | -- The code that we generate from a one dimensional aggregate is | |
998 | ||
d74716b3 | 999 | -- 1. If the subaggregate contains discrete choices we |
70482933 RK |
1000 | |
1001 | -- (a) Sort the discrete choices | |
1002 | ||
1003 | -- (b) Otherwise for each discrete choice that specifies a range we | |
1004 | -- emit a loop. If a range specifies a maximum of three values, or | |
1005 | -- we are dealing with an expression we emit a sequence of | |
1006 | -- assignments instead of a loop. | |
1007 | ||
3cf3e5c6 | 1008 | -- (c) Generate the remaining loops to cover the others choice if any |
70482933 RK |
1009 | |
1010 | -- 2. If the aggregate contains positional elements we | |
1011 | ||
8313d0ee | 1012 | -- (a) Translate the positional elements in a series of assignments |
70482933 RK |
1013 | |
1014 | -- (b) Generate a final loop to cover the others choice if any. | |
1015 | -- Note that this final loop has to be a while loop since the case | |
1016 | ||
1017 | -- L : Integer := Integer'Last; | |
1018 | -- H : Integer := Integer'Last; | |
1019 | -- A : array (L .. H) := (1, others =>0); | |
1020 | ||
1021 | -- cannot be handled by a for loop. Thus for the following | |
1022 | ||
8313d0ee | 1023 | -- array (L .. H) := (.. positional elements.., others => E); |
70482933 RK |
1024 | |
1025 | -- we always generate something like: | |
1026 | ||
07fc65c4 GB |
1027 | -- J : Index_Type := Index_Of_Last_Positional_Element; |
1028 | -- while J < H loop | |
1029 | -- J := Index_Base'Succ (J) | |
1030 | -- Tmp (J) := E; | |
70482933 RK |
1031 | -- end loop; |
1032 | ||
1033 | function Build_Array_Aggr_Code | |
1034 | (N : Node_Id; | |
c45b6ae0 | 1035 | Ctype : Entity_Id; |
70482933 RK |
1036 | Index : Node_Id; |
1037 | Into : Node_Id; | |
1038 | Scalar_Comp : Boolean; | |
df3e68b1 | 1039 | Indexes : List_Id := No_List) return List_Id |
70482933 RK |
1040 | is |
1041 | Loc : constant Source_Ptr := Sloc (N); | |
4f061cf2 | 1042 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
1043 | Index_Base : constant Entity_Id := Base_Type (Etype (Index)); |
1044 | Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base); | |
1045 | Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base); | |
1046 | ||
1047 | function Add (Val : Int; To : Node_Id) return Node_Id; | |
3cf3e5c6 AC |
1048 | -- Returns an expression where Val is added to expression To, unless |
1049 | -- To+Val is provably out of To's base type range. To must be an | |
1050 | -- already analyzed expression. | |
70482933 RK |
1051 | |
1052 | function Empty_Range (L, H : Node_Id) return Boolean; | |
3cf3e5c6 | 1053 | -- Returns True if the range defined by L .. H is certainly empty |
70482933 RK |
1054 | |
1055 | function Equal (L, H : Node_Id) return Boolean; | |
3cf3e5c6 | 1056 | -- Returns True if L = H for sure |
70482933 RK |
1057 | |
1058 | function Index_Base_Name return Node_Id; | |
3cf3e5c6 | 1059 | -- Returns a new reference to the index type name |
70482933 | 1060 | |
937e9676 AC |
1061 | function Gen_Assign |
1062 | (Ind : Node_Id; | |
fc84947c | 1063 | Expr : Node_Id) return List_Id; |
d74716b3 AC |
1064 | -- Ind must be a side-effect-free expression. If the input aggregate N |
1065 | -- to Build_Loop contains no subaggregates, then this function returns | |
1066 | -- the assignment statement: | |
70482933 | 1067 | -- |
deeb1604 | 1068 | -- Into (Indexes, Ind) := Expr; |
70482933 | 1069 | -- |
fc84947c | 1070 | -- Otherwise we call Build_Code recursively. |
c45b6ae0 | 1071 | -- |
0ab80019 AC |
1072 | -- Ada 2005 (AI-287): In case of default initialized component, Expr |
1073 | -- is empty and we generate a call to the corresponding IP subprogram. | |
70482933 RK |
1074 | |
1075 | function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id; | |
d74716b3 AC |
1076 | -- Nodes L and H must be side-effect-free expressions. If the input |
1077 | -- aggregate N to Build_Loop contains no subaggregates, this routine | |
1078 | -- returns the for loop statement: | |
70482933 RK |
1079 | -- |
1080 | -- for J in Index_Base'(L) .. Index_Base'(H) loop | |
deeb1604 | 1081 | -- Into (Indexes, J) := Expr; |
70482933 RK |
1082 | -- end loop; |
1083 | -- | |
937e9676 AC |
1084 | -- Otherwise we call Build_Code recursively. As an optimization if the |
1085 | -- loop covers 3 or fewer scalar elements we generate a sequence of | |
1086 | -- assignments. | |
00f45f30 AC |
1087 | -- If the component association that generates the loop comes from an |
1088 | -- Iterated_Component_Association, the loop parameter has the name of | |
1089 | -- the corresponding parameter in the original construct. | |
70482933 RK |
1090 | |
1091 | function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id; | |
d74716b3 AC |
1092 | -- Nodes L and H must be side-effect-free expressions. If the input |
1093 | -- aggregate N to Build_Loop contains no subaggregates, this routine | |
1094 | -- returns the while loop statement: | |
70482933 | 1095 | -- |
07fc65c4 GB |
1096 | -- J : Index_Base := L; |
1097 | -- while J < H loop | |
1098 | -- J := Index_Base'Succ (J); | |
deeb1604 | 1099 | -- Into (Indexes, J) := Expr; |
70482933 RK |
1100 | -- end loop; |
1101 | -- | |
fbf5a39b | 1102 | -- Otherwise we call Build_Code recursively |
70482933 | 1103 | |
59e9bc0b | 1104 | function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id; |
e9999161 AC |
1105 | -- For an association with a box, use value given by aspect |
1106 | -- Default_Component_Value of array type if specified, else use | |
1107 | -- value given by aspect Default_Value for component type itself | |
1108 | -- if specified, else return Empty. | |
59e9bc0b | 1109 | |
70482933 RK |
1110 | function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean; |
1111 | function Local_Expr_Value (E : Node_Id) return Uint; | |
1112 | -- These two Local routines are used to replace the corresponding ones | |
1113 | -- in sem_eval because while processing the bounds of an aggregate with | |
1114 | -- discrete choices whose index type is an enumeration, we build static | |
1115 | -- expressions not recognized by Compile_Time_Known_Value as such since | |
1116 | -- they have not yet been analyzed and resolved. All the expressions in | |
1117 | -- question are things like Index_Base_Name'Val (Const) which we can | |
1118 | -- easily recognize as being constant. | |
1119 | ||
1120 | --------- | |
1121 | -- Add -- | |
1122 | --------- | |
1123 | ||
1124 | function Add (Val : Int; To : Node_Id) return Node_Id is | |
1125 | Expr_Pos : Node_Id; | |
1126 | Expr : Node_Id; | |
1127 | To_Pos : Node_Id; | |
fbf5a39b AC |
1128 | U_To : Uint; |
1129 | U_Val : constant Uint := UI_From_Int (Val); | |
70482933 RK |
1130 | |
1131 | begin | |
1132 | -- Note: do not try to optimize the case of Val = 0, because | |
1133 | -- we need to build a new node with the proper Sloc value anyway. | |
1134 | ||
1135 | -- First test if we can do constant folding | |
1136 | ||
1137 | if Local_Compile_Time_Known_Value (To) then | |
1138 | U_To := Local_Expr_Value (To) + Val; | |
1139 | ||
1140 | -- Determine if our constant is outside the range of the index. | |
1141 | -- If so return an Empty node. This empty node will be caught | |
1142 | -- by Empty_Range below. | |
1143 | ||
1144 | if Compile_Time_Known_Value (Index_Base_L) | |
1145 | and then U_To < Expr_Value (Index_Base_L) | |
1146 | then | |
1147 | return Empty; | |
1148 | ||
1149 | elsif Compile_Time_Known_Value (Index_Base_H) | |
1150 | and then U_To > Expr_Value (Index_Base_H) | |
1151 | then | |
1152 | return Empty; | |
1153 | end if; | |
1154 | ||
1155 | Expr_Pos := Make_Integer_Literal (Loc, U_To); | |
1156 | Set_Is_Static_Expression (Expr_Pos); | |
1157 | ||
1158 | if not Is_Enumeration_Type (Index_Base) then | |
1159 | Expr := Expr_Pos; | |
1160 | ||
1161 | -- If we are dealing with enumeration return | |
1162 | -- Index_Base'Val (Expr_Pos) | |
1163 | ||
1164 | else | |
1165 | Expr := | |
1166 | Make_Attribute_Reference | |
1167 | (Loc, | |
1168 | Prefix => Index_Base_Name, | |
1169 | Attribute_Name => Name_Val, | |
1170 | Expressions => New_List (Expr_Pos)); | |
1171 | end if; | |
1172 | ||
1173 | return Expr; | |
1174 | end if; | |
1175 | ||
1176 | -- If we are here no constant folding possible | |
1177 | ||
1178 | if not Is_Enumeration_Type (Index_Base) then | |
1179 | Expr := | |
1180 | Make_Op_Add (Loc, | |
47c14114 AC |
1181 | Left_Opnd => Duplicate_Subexpr (To), |
1182 | Right_Opnd => Make_Integer_Literal (Loc, U_Val)); | |
70482933 RK |
1183 | |
1184 | -- If we are dealing with enumeration return | |
1185 | -- Index_Base'Val (Index_Base'Pos (To) + Val) | |
1186 | ||
1187 | else | |
1188 | To_Pos := | |
1189 | Make_Attribute_Reference | |
1190 | (Loc, | |
1191 | Prefix => Index_Base_Name, | |
1192 | Attribute_Name => Name_Pos, | |
1193 | Expressions => New_List (Duplicate_Subexpr (To))); | |
1194 | ||
1195 | Expr_Pos := | |
1196 | Make_Op_Add (Loc, | |
47c14114 AC |
1197 | Left_Opnd => To_Pos, |
1198 | Right_Opnd => Make_Integer_Literal (Loc, U_Val)); | |
70482933 RK |
1199 | |
1200 | Expr := | |
1201 | Make_Attribute_Reference | |
1202 | (Loc, | |
1203 | Prefix => Index_Base_Name, | |
1204 | Attribute_Name => Name_Val, | |
1205 | Expressions => New_List (Expr_Pos)); | |
1206 | end if; | |
1207 | ||
1208 | return Expr; | |
1209 | end Add; | |
1210 | ||
1211 | ----------------- | |
1212 | -- Empty_Range -- | |
1213 | ----------------- | |
1214 | ||
1215 | function Empty_Range (L, H : Node_Id) return Boolean is | |
1216 | Is_Empty : Boolean := False; | |
1217 | Low : Node_Id; | |
1218 | High : Node_Id; | |
1219 | ||
1220 | begin | |
1221 | -- First check if L or H were already detected as overflowing the | |
1222 | -- index base range type by function Add above. If this is so Add | |
1223 | -- returns the empty node. | |
1224 | ||
1225 | if No (L) or else No (H) then | |
1226 | return True; | |
1227 | end if; | |
1228 | ||
1229 | for J in 1 .. 3 loop | |
1230 | case J is | |
1231 | ||
1232 | -- L > H range is empty | |
1233 | ||
1234 | when 1 => | |
1235 | Low := L; | |
1236 | High := H; | |
1237 | ||
1238 | -- B_L > H range must be empty | |
1239 | ||
1240 | when 2 => | |
1241 | Low := Index_Base_L; | |
1242 | High := H; | |
1243 | ||
1244 | -- L > B_H range must be empty | |
1245 | ||
1246 | when 3 => | |
1247 | Low := L; | |
1248 | High := Index_Base_H; | |
1249 | end case; | |
1250 | ||
1251 | if Local_Compile_Time_Known_Value (Low) | |
36a66365 AC |
1252 | and then |
1253 | Local_Compile_Time_Known_Value (High) | |
70482933 RK |
1254 | then |
1255 | Is_Empty := | |
1256 | UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High)); | |
1257 | end if; | |
1258 | ||
1259 | exit when Is_Empty; | |
1260 | end loop; | |
1261 | ||
1262 | return Is_Empty; | |
1263 | end Empty_Range; | |
1264 | ||
1265 | ----------- | |
1266 | -- Equal -- | |
1267 | ----------- | |
1268 | ||
1269 | function Equal (L, H : Node_Id) return Boolean is | |
1270 | begin | |
1271 | if L = H then | |
1272 | return True; | |
1273 | ||
1274 | elsif Local_Compile_Time_Known_Value (L) | |
36a66365 AC |
1275 | and then |
1276 | Local_Compile_Time_Known_Value (H) | |
70482933 RK |
1277 | then |
1278 | return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H)); | |
1279 | end if; | |
1280 | ||
1281 | return False; | |
1282 | end Equal; | |
1283 | ||
1284 | ---------------- | |
1285 | -- Gen_Assign -- | |
1286 | ---------------- | |
1287 | ||
937e9676 AC |
1288 | function Gen_Assign |
1289 | (Ind : Node_Id; | |
fc84947c | 1290 | Expr : Node_Id) return List_Id |
937e9676 | 1291 | is |
70482933 | 1292 | function Add_Loop_Actions (Lis : List_Id) return List_Id; |
937e9676 AC |
1293 | -- Collect insert_actions generated in the construction of a loop, |
1294 | -- and prepend them to the sequence of assignments to complete the | |
1295 | -- eventual body of the loop. | |
1296 | ||
70482933 RK |
1297 | ---------------------- |
1298 | -- Add_Loop_Actions -- | |
1299 | ---------------------- | |
1300 | ||
1301 | function Add_Loop_Actions (Lis : List_Id) return List_Id is | |
1302 | Res : List_Id; | |
1303 | ||
1304 | begin | |
0ab80019 | 1305 | -- Ada 2005 (AI-287): Do nothing else in case of default |
6e937c1c | 1306 | -- initialized component. |
c45b6ae0 | 1307 | |
d8f7b976 | 1308 | if No (Expr) then |
c45b6ae0 AC |
1309 | return Lis; |
1310 | ||
1311 | elsif Nkind (Parent (Expr)) = N_Component_Association | |
70482933 RK |
1312 | and then Present (Loop_Actions (Parent (Expr))) |
1313 | then | |
1314 | Append_List (Lis, Loop_Actions (Parent (Expr))); | |
1315 | Res := Loop_Actions (Parent (Expr)); | |
1316 | Set_Loop_Actions (Parent (Expr), No_List); | |
1317 | return Res; | |
1318 | ||
1319 | else | |
1320 | return Lis; | |
1321 | end if; | |
1322 | end Add_Loop_Actions; | |
1323 | ||
10edebe7 AC |
1324 | -- Local variables |
1325 | ||
1326 | Stmts : constant List_Id := New_List; | |
1327 | ||
1328 | Comp_Typ : Entity_Id := Empty; | |
1329 | Expr_Q : Node_Id; | |
1330 | Indexed_Comp : Node_Id; | |
2168d7cc | 1331 | Init_Call : Node_Id; |
10edebe7 | 1332 | New_Indexes : List_Id; |
10edebe7 | 1333 | |
70482933 RK |
1334 | -- Start of processing for Gen_Assign |
1335 | ||
1336 | begin | |
deeb1604 AC |
1337 | if No (Indexes) then |
1338 | New_Indexes := New_List; | |
70482933 | 1339 | else |
deeb1604 | 1340 | New_Indexes := New_Copy_List_Tree (Indexes); |
70482933 RK |
1341 | end if; |
1342 | ||
deeb1604 | 1343 | Append_To (New_Indexes, Ind); |
70482933 | 1344 | |
70482933 RK |
1345 | if Present (Next_Index (Index)) then |
1346 | return | |
1347 | Add_Loop_Actions ( | |
1348 | Build_Array_Aggr_Code | |
c45b6ae0 AC |
1349 | (N => Expr, |
1350 | Ctype => Ctype, | |
1351 | Index => Next_Index (Index), | |
1352 | Into => Into, | |
1353 | Scalar_Comp => Scalar_Comp, | |
df3e68b1 | 1354 | Indexes => New_Indexes)); |
70482933 RK |
1355 | end if; |
1356 | ||
1357 | -- If we get here then we are at a bottom-level (sub-)aggregate | |
1358 | ||
fbf5a39b AC |
1359 | Indexed_Comp := |
1360 | Checks_Off | |
1361 | (Make_Indexed_Component (Loc, | |
1362 | Prefix => New_Copy_Tree (Into), | |
deeb1604 | 1363 | Expressions => New_Indexes)); |
70482933 RK |
1364 | |
1365 | Set_Assignment_OK (Indexed_Comp); | |
1366 | ||
0ab80019 | 1367 | -- Ada 2005 (AI-287): In case of default initialized component, Expr |
6e937c1c | 1368 | -- is not present (and therefore we also initialize Expr_Q to empty). |
c45b6ae0 | 1369 | |
4f061cf2 | 1370 | Expr_Q := Unqualify (Expr); |
70482933 | 1371 | |
36a66365 | 1372 | if Present (Etype (N)) and then Etype (N) /= Any_Composite then |
10edebe7 AC |
1373 | Comp_Typ := Component_Type (Etype (N)); |
1374 | pragma Assert (Comp_Typ = Ctype); -- AI-287 | |
70482933 | 1375 | |
deeb1604 | 1376 | elsif Present (Next (First (New_Indexes))) then |
70482933 | 1377 | |
0ab80019 | 1378 | -- Ada 2005 (AI-287): Do nothing in case of default initialized |
c45b6ae0 AC |
1379 | -- component because we have received the component type in |
1380 | -- the formal parameter Ctype. | |
6e937c1c AC |
1381 | |
1382 | -- ??? Some assert pragmas have been added to check if this new | |
36a66365 | 1383 | -- formal can be used to replace this code in all cases. |
70482933 | 1384 | |
c45b6ae0 | 1385 | if Present (Expr) then |
70482933 | 1386 | |
36a66365 AC |
1387 | -- This is a multidimensional array. Recover the component type |
1388 | -- from the outermost aggregate, because subaggregates do not | |
1389 | -- have an assigned type. | |
70482933 | 1390 | |
c45b6ae0 | 1391 | declare |
5277cab6 | 1392 | P : Node_Id; |
70482933 | 1393 | |
c45b6ae0 | 1394 | begin |
5277cab6 | 1395 | P := Parent (Expr); |
c45b6ae0 | 1396 | while Present (P) loop |
c45b6ae0 AC |
1397 | if Nkind (P) = N_Aggregate |
1398 | and then Present (Etype (P)) | |
1399 | then | |
10edebe7 | 1400 | Comp_Typ := Component_Type (Etype (P)); |
c45b6ae0 AC |
1401 | exit; |
1402 | ||
1403 | else | |
1404 | P := Parent (P); | |
1405 | end if; | |
1406 | end loop; | |
6e937c1c | 1407 | |
10edebe7 | 1408 | pragma Assert (Comp_Typ = Ctype); -- AI-287 |
c45b6ae0 AC |
1409 | end; |
1410 | end if; | |
70482933 RK |
1411 | end if; |
1412 | ||
0ab80019 | 1413 | -- Ada 2005 (AI-287): We only analyze the expression in case of non- |
6e937c1c | 1414 | -- default initialized components (otherwise Expr_Q is not present). |
c45b6ae0 AC |
1415 | |
1416 | if Present (Expr_Q) | |
4a08c95c | 1417 | and then Nkind (Expr_Q) in N_Aggregate | N_Extension_Aggregate |
70482933 | 1418 | then |
d7f94401 AC |
1419 | -- At this stage the Expression may not have been analyzed yet |
1420 | -- because the array aggregate code has not been updated to use | |
1421 | -- the Expansion_Delayed flag and avoid analysis altogether to | |
1422 | -- solve the same problem (see Resolve_Aggr_Expr). So let us do | |
1423 | -- the analysis of non-array aggregates now in order to get the | |
1424 | -- value of Expansion_Delayed flag for the inner aggregate ??? | |
70482933 | 1425 | |
6cbd45e4 PMR |
1426 | -- In the case of an iterated component association, the analysis |
1427 | -- of the generated loop will analyze the expression in the | |
1428 | -- proper context, in which the loop parameter is visible. | |
1429 | ||
d940c627 | 1430 | if Present (Comp_Typ) and then not Is_Array_Type (Comp_Typ) then |
10fdda1c HK |
1431 | if Nkind (Parent (Expr_Q)) = N_Iterated_Component_Association |
1432 | or else Nkind (Parent (Parent ((Expr_Q)))) = | |
1433 | N_Iterated_Component_Association | |
d940c627 ES |
1434 | then |
1435 | null; | |
1436 | else | |
1437 | Analyze_And_Resolve (Expr_Q, Comp_Typ); | |
1438 | end if; | |
70482933 RK |
1439 | end if; |
1440 | ||
1441 | if Is_Delayed_Aggregate (Expr_Q) then | |
3cf3e5c6 | 1442 | |
308e6f3a | 1443 | -- This is either a subaggregate of a multidimensional array, |
3cf3e5c6 AC |
1444 | -- or a component of an array type whose component type is |
1445 | -- also an array. In the latter case, the expression may have | |
1446 | -- component associations that provide different bounds from | |
1447 | -- those of the component type, and sliding must occur. Instead | |
1448 | -- of decomposing the current aggregate assignment, force the | |
937e9676 | 1449 | -- reanalysis of the assignment, so that a temporary will be |
3cf3e5c6 AC |
1450 | -- generated in the usual fashion, and sliding will take place. |
1451 | ||
1452 | if Nkind (Parent (N)) = N_Assignment_Statement | |
10edebe7 | 1453 | and then Is_Array_Type (Comp_Typ) |
3cf3e5c6 | 1454 | and then Present (Component_Associations (Expr_Q)) |
6732c403 | 1455 | and then Must_Slide (N, Comp_Typ, Etype (Expr_Q)) |
3cf3e5c6 AC |
1456 | then |
1457 | Set_Expansion_Delayed (Expr_Q, False); | |
1458 | Set_Analyzed (Expr_Q, False); | |
1459 | ||
1460 | else | |
1461 | return | |
1462 | Add_Loop_Actions ( | |
df3e68b1 | 1463 | Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp)); |
3cf3e5c6 | 1464 | end if; |
70482933 RK |
1465 | end if; |
1466 | end if; | |
1467 | ||
937e9676 | 1468 | if Present (Expr) then |
fc84947c EB |
1469 | Initialize_Component |
1470 | (N => N, | |
1471 | Comp => Indexed_Comp, | |
1472 | Comp_Typ => Comp_Typ, | |
1473 | Init_Expr => Expr, | |
1474 | Stmts => Stmts); | |
937e9676 | 1475 | |
0ab80019 | 1476 | -- Ada 2005 (AI-287): In case of default initialized component, call |
6e937c1c | 1477 | -- the initialization subprogram associated with the component type. |
3b9fa2df ES |
1478 | -- If the component type is an access type, add an explicit null |
1479 | -- assignment, because for the back-end there is an initialization | |
1480 | -- present for the whole aggregate, and no default initialization | |
1481 | -- will take place. | |
1482 | ||
1483 | -- In addition, if the component type is controlled, we must call | |
1484 | -- its Initialize procedure explicitly, because there is no explicit | |
1485 | -- object creation that will invoke it otherwise. | |
70482933 | 1486 | |
937e9676 | 1487 | else |
3b9fa2df | 1488 | if Present (Base_Init_Proc (Base_Type (Ctype))) |
615cbd95 AC |
1489 | or else Has_Task (Base_Type (Ctype)) |
1490 | then | |
10edebe7 | 1491 | Append_List_To (Stmts, |
c45b6ae0 AC |
1492 | Build_Initialization_Call (Loc, |
1493 | Id_Ref => Indexed_Comp, | |
1494 | Typ => Ctype, | |
1495 | With_Default_Init => True)); | |
3b9fa2df | 1496 | |
ffcfb997 ES |
1497 | -- If the component type has invariants, add an invariant |
1498 | -- check after the component is default-initialized. It will | |
1499 | -- be analyzed and resolved before the code for initialization | |
1500 | -- of other components. | |
1501 | ||
1502 | if Has_Invariants (Ctype) then | |
1503 | Set_Etype (Indexed_Comp, Ctype); | |
10edebe7 | 1504 | Append_To (Stmts, Make_Invariant_Call (Indexed_Comp)); |
ffcfb997 | 1505 | end if; |
3b9fa2df ES |
1506 | end if; |
1507 | ||
048e5cef | 1508 | if Needs_Finalization (Ctype) then |
2168d7cc | 1509 | Init_Call := |
37368818 RD |
1510 | Make_Init_Call |
1511 | (Obj_Ref => New_Copy_Tree (Indexed_Comp), | |
2168d7cc AC |
1512 | Typ => Ctype); |
1513 | ||
1514 | -- Guard against a missing [Deep_]Initialize when the component | |
1515 | -- type was not properly frozen. | |
1516 | ||
1517 | if Present (Init_Call) then | |
1518 | Append_To (Stmts, Init_Call); | |
1519 | end if; | |
615cbd95 | 1520 | end if; |
097826df GD |
1521 | |
1522 | -- If Default_Initial_Condition applies to the component type, | |
1523 | -- add a DIC check after the component is default-initialized, | |
1524 | -- as well as after an Initialize procedure is called, in the | |
1525 | -- case of components of a controlled type. It will be analyzed | |
1526 | -- and resolved before the code for initialization of other | |
1527 | -- components. | |
1528 | ||
1529 | -- Theoretically this might also be needed for cases where Expr | |
1530 | -- is not empty, but a default init still applies, such as for | |
1531 | -- Default_Value cases, in which case we won't get here. ??? | |
1532 | ||
1533 | if Has_DIC (Ctype) and then Present (DIC_Procedure (Ctype)) then | |
1534 | Append_To (Stmts, | |
1535 | Build_DIC_Call (Loc, New_Copy_Tree (Indexed_Comp), Ctype)); | |
1536 | end if; | |
70482933 RK |
1537 | end if; |
1538 | ||
10edebe7 | 1539 | return Add_Loop_Actions (Stmts); |
70482933 RK |
1540 | end Gen_Assign; |
1541 | ||
1542 | -------------- | |
1543 | -- Gen_Loop -- | |
1544 | -------------- | |
1545 | ||
1546 | function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is | |
00f45f30 | 1547 | Is_Iterated_Component : constant Boolean := |
898edf75 | 1548 | Parent_Kind (Expr) = N_Iterated_Component_Association; |
00f45f30 | 1549 | |
e3584606 MP |
1550 | Ent : Entity_Id; |
1551 | ||
07fc65c4 | 1552 | L_J : Node_Id; |
70482933 | 1553 | |
240fe2a4 AC |
1554 | L_L : Node_Id; |
1555 | -- Index_Base'(L) | |
1556 | ||
1557 | L_H : Node_Id; | |
1558 | -- Index_Base'(H) | |
1559 | ||
70482933 RK |
1560 | L_Range : Node_Id; |
1561 | -- Index_Base'(L) .. Index_Base'(H) | |
1562 | ||
1563 | L_Iteration_Scheme : Node_Id; | |
07fc65c4 | 1564 | -- L_J in Index_Base'(L) .. Index_Base'(H) |
70482933 RK |
1565 | |
1566 | L_Body : List_Id; | |
1567 | -- The statements to execute in the loop | |
1568 | ||
fbf5a39b AC |
1569 | S : constant List_Id := New_List; |
1570 | -- List of statements | |
70482933 RK |
1571 | |
1572 | Tcopy : Node_Id; | |
1573 | -- Copy of expression tree, used for checking purposes | |
1574 | ||
1575 | begin | |
1576 | -- If loop bounds define an empty range return the null statement | |
1577 | ||
1578 | if Empty_Range (L, H) then | |
1579 | Append_To (S, Make_Null_Statement (Loc)); | |
1580 | ||
0ab80019 | 1581 | -- Ada 2005 (AI-287): Nothing else need to be done in case of |
6e937c1c | 1582 | -- default initialized component. |
70482933 | 1583 | |
d8f7b976 | 1584 | if No (Expr) then |
c45b6ae0 AC |
1585 | null; |
1586 | ||
1587 | else | |
1588 | -- The expression must be type-checked even though no component | |
1589 | -- of the aggregate will have this value. This is done only for | |
1590 | -- actual components of the array, not for subaggregates. Do | |
1591 | -- the check on a copy, because the expression may be shared | |
1592 | -- among several choices, some of which might be non-null. | |
1593 | ||
1594 | if Present (Etype (N)) | |
1595 | and then Is_Array_Type (Etype (N)) | |
1596 | and then No (Next_Index (Index)) | |
1597 | then | |
1598 | Expander_Mode_Save_And_Set (False); | |
1599 | Tcopy := New_Copy_Tree (Expr); | |
1600 | Set_Parent (Tcopy, N); | |
4270e945 PT |
1601 | |
1602 | -- For iterated_component_association analyze and resolve | |
1603 | -- the expression with name of the index parameter visible. | |
1604 | -- To manipulate scopes, we use entity of the implicit loop. | |
1605 | ||
1606 | if Is_Iterated_Component then | |
1607 | declare | |
1608 | Index_Parameter : constant Entity_Id := | |
1609 | Defining_Identifier (Parent (Expr)); | |
1610 | begin | |
1611 | Push_Scope (Scope (Index_Parameter)); | |
1612 | Enter_Name (Index_Parameter); | |
1613 | Analyze_And_Resolve | |
1614 | (Tcopy, Component_Type (Etype (N))); | |
1615 | End_Scope; | |
1616 | end; | |
1617 | ||
1618 | -- For ordinary component association, just analyze and | |
1619 | -- resolve the expression. | |
1620 | ||
1621 | else | |
1622 | Analyze_And_Resolve (Tcopy, Component_Type (Etype (N))); | |
1623 | end if; | |
1624 | ||
c45b6ae0 AC |
1625 | Expander_Mode_Restore; |
1626 | end if; | |
70482933 RK |
1627 | end if; |
1628 | ||
1629 | return S; | |
1630 | ||
00f45f30 AC |
1631 | -- If loop bounds are the same then generate an assignment, unless |
1632 | -- the parent construct is an Iterated_Component_Association. | |
70482933 | 1633 | |
00f45f30 | 1634 | elsif Equal (L, H) and then not Is_Iterated_Component then |
70482933 RK |
1635 | return Gen_Assign (New_Copy_Tree (L), Expr); |
1636 | ||
3b9fa2df ES |
1637 | -- If H - L <= 2 then generate a sequence of assignments when we are |
1638 | -- processing the bottom most aggregate and it contains scalar | |
1639 | -- components. | |
70482933 RK |
1640 | |
1641 | elsif No (Next_Index (Index)) | |
1642 | and then Scalar_Comp | |
1643 | and then Local_Compile_Time_Known_Value (L) | |
1644 | and then Local_Compile_Time_Known_Value (H) | |
1645 | and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2 | |
00f45f30 | 1646 | and then not Is_Iterated_Component |
70482933 RK |
1647 | then |
1648 | Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr)); | |
1649 | Append_List_To (S, Gen_Assign (Add (1, To => L), Expr)); | |
1650 | ||
1651 | if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then | |
1652 | Append_List_To (S, Gen_Assign (Add (2, To => L), Expr)); | |
1653 | end if; | |
1654 | ||
1655 | return S; | |
1656 | end if; | |
1657 | ||
07fc65c4 | 1658 | -- Otherwise construct the loop, starting with the loop index L_J |
70482933 | 1659 | |
00f45f30 | 1660 | if Is_Iterated_Component then |
e3584606 MP |
1661 | |
1662 | -- Create a new scope for the loop variable so that the | |
1663 | -- following Gen_Assign (that ends up calling | |
1664 | -- Preanalyze_And_Resolve) can correctly find it. | |
1665 | ||
1666 | Ent := New_Internal_Entity (E_Loop, | |
1667 | Current_Scope, Loc, 'L'); | |
1668 | Set_Etype (Ent, Standard_Void_Type); | |
1669 | Set_Parent (Ent, Parent (Parent (Expr))); | |
1670 | Push_Scope (Ent); | |
1671 | ||
72cdccfa HK |
1672 | L_J := |
1673 | Make_Defining_Identifier (Loc, | |
1674 | Chars => (Chars (Defining_Identifier (Parent (Expr))))); | |
00f45f30 | 1675 | |
e3584606 MP |
1676 | Enter_Name (L_J); |
1677 | ||
1678 | -- The Etype will be set by a later Analyze call. | |
1679 | Set_Etype (L_J, Any_Type); | |
1680 | ||
1681 | Mutate_Ekind (L_J, E_Variable); | |
25e4024c | 1682 | Set_Is_Not_Self_Hidden (L_J); |
e3584606 | 1683 | Set_Scope (L_J, Ent); |
00f45f30 AC |
1684 | else |
1685 | L_J := Make_Temporary (Loc, 'J', L); | |
1686 | end if; | |
70482933 | 1687 | |
240fe2a4 AC |
1688 | -- Construct "L .. H" in Index_Base. We use a qualified expression |
1689 | -- for the bound to convert to the index base, but we don't need | |
1690 | -- to do that if we already have the base type at hand. | |
1691 | ||
1692 | if Etype (L) = Index_Base then | |
4eac8834 | 1693 | L_L := New_Copy_Tree (L); |
240fe2a4 AC |
1694 | else |
1695 | L_L := | |
1696 | Make_Qualified_Expression (Loc, | |
1697 | Subtype_Mark => Index_Base_Name, | |
00f45f30 | 1698 | Expression => New_Copy_Tree (L)); |
240fe2a4 AC |
1699 | end if; |
1700 | ||
1701 | if Etype (H) = Index_Base then | |
4eac8834 | 1702 | L_H := New_Copy_Tree (H); |
240fe2a4 AC |
1703 | else |
1704 | L_H := | |
1705 | Make_Qualified_Expression (Loc, | |
1706 | Subtype_Mark => Index_Base_Name, | |
00f45f30 | 1707 | Expression => New_Copy_Tree (H)); |
240fe2a4 | 1708 | end if; |
70482933 RK |
1709 | |
1710 | L_Range := | |
240fe2a4 | 1711 | Make_Range (Loc, |
ffcfb997 | 1712 | Low_Bound => L_L, |
240fe2a4 | 1713 | High_Bound => L_H); |
70482933 | 1714 | |
07fc65c4 | 1715 | -- Construct "for L_J in Index_Base range L .. H" |
70482933 RK |
1716 | |
1717 | L_Iteration_Scheme := | |
8313d0ee | 1718 | Make_Iteration_Scheme (Loc, |
70482933 | 1719 | Loop_Parameter_Specification => |
8313d0ee | 1720 | Make_Loop_Parameter_Specification (Loc, |
07fc65c4 | 1721 | Defining_Identifier => L_J, |
70482933 RK |
1722 | Discrete_Subtype_Definition => L_Range)); |
1723 | ||
1724 | -- Construct the statements to execute in the loop body | |
1725 | ||
fc84947c | 1726 | L_Body := Gen_Assign (New_Occurrence_Of (L_J, Loc), Expr); |
70482933 RK |
1727 | |
1728 | -- Construct the final loop | |
1729 | ||
37368818 RD |
1730 | Append_To (S, |
1731 | Make_Implicit_Loop_Statement | |
1732 | (Node => N, | |
1733 | Identifier => Empty, | |
1734 | Iteration_Scheme => L_Iteration_Scheme, | |
1735 | Statements => L_Body)); | |
70482933 | 1736 | |
e3584606 MP |
1737 | if Is_Iterated_Component then |
1738 | End_Scope; | |
1739 | end if; | |
1740 | ||
3b9fa2df ES |
1741 | -- A small optimization: if the aggregate is initialized with a box |
1742 | -- and the component type has no initialization procedure, remove the | |
1743 | -- useless empty loop. | |
0f95b178 JM |
1744 | |
1745 | if Nkind (First (S)) = N_Loop_Statement | |
1746 | and then Is_Empty_List (Statements (First (S))) | |
1747 | then | |
1748 | return New_List (Make_Null_Statement (Loc)); | |
1749 | else | |
1750 | return S; | |
1751 | end if; | |
70482933 RK |
1752 | end Gen_Loop; |
1753 | ||
1754 | --------------- | |
1755 | -- Gen_While -- | |
1756 | --------------- | |
1757 | ||
1758 | -- The code built is | |
1759 | ||
07fc65c4 GB |
1760 | -- W_J : Index_Base := L; |
1761 | -- while W_J < H loop | |
1762 | -- W_J := Index_Base'Succ (W); | |
70482933 RK |
1763 | -- L_Body; |
1764 | -- end loop; | |
1765 | ||
1766 | function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is | |
07fc65c4 | 1767 | W_J : Node_Id; |
70482933 RK |
1768 | |
1769 | W_Decl : Node_Id; | |
07fc65c4 | 1770 | -- W_J : Base_Type := L; |
70482933 RK |
1771 | |
1772 | W_Iteration_Scheme : Node_Id; | |
07fc65c4 | 1773 | -- while W_J < H |
70482933 RK |
1774 | |
1775 | W_Index_Succ : Node_Id; | |
07fc65c4 | 1776 | -- Index_Base'Succ (J) |
70482933 | 1777 | |
fbf5a39b | 1778 | W_Increment : Node_Id; |
07fc65c4 | 1779 | -- W_J := Index_Base'Succ (W) |
70482933 | 1780 | |
fbf5a39b | 1781 | W_Body : constant List_Id := New_List; |
70482933 RK |
1782 | -- The statements to execute in the loop |
1783 | ||
fbf5a39b | 1784 | S : constant List_Id := New_List; |
70482933 RK |
1785 | -- list of statement |
1786 | ||
1787 | begin | |
1788 | -- If loop bounds define an empty range or are equal return null | |
1789 | ||
1790 | if Empty_Range (L, H) or else Equal (L, H) then | |
1791 | Append_To (S, Make_Null_Statement (Loc)); | |
1792 | return S; | |
1793 | end if; | |
1794 | ||
07fc65c4 | 1795 | -- Build the decl of W_J |
70482933 | 1796 | |
191fcb3a | 1797 | W_J := Make_Temporary (Loc, 'J', L); |
70482933 RK |
1798 | W_Decl := |
1799 | Make_Object_Declaration | |
1800 | (Loc, | |
07fc65c4 | 1801 | Defining_Identifier => W_J, |
70482933 RK |
1802 | Object_Definition => Index_Base_Name, |
1803 | Expression => L); | |
1804 | ||
1805 | -- Theoretically we should do a New_Copy_Tree (L) here, but we know | |
1806 | -- that in this particular case L is a fresh Expr generated by | |
1807 | -- Add which we are the only ones to use. | |
1808 | ||
1809 | Append_To (S, W_Decl); | |
1810 | ||
fbf5a39b | 1811 | -- Construct " while W_J < H" |
70482933 RK |
1812 | |
1813 | W_Iteration_Scheme := | |
1814 | Make_Iteration_Scheme | |
1815 | (Loc, | |
1816 | Condition => Make_Op_Lt | |
1817 | (Loc, | |
e4494292 | 1818 | Left_Opnd => New_Occurrence_Of (W_J, Loc), |
70482933 RK |
1819 | Right_Opnd => New_Copy_Tree (H))); |
1820 | ||
1821 | -- Construct the statements to execute in the loop body | |
1822 | ||
1823 | W_Index_Succ := | |
1824 | Make_Attribute_Reference | |
1825 | (Loc, | |
1826 | Prefix => Index_Base_Name, | |
1827 | Attribute_Name => Name_Succ, | |
e4494292 | 1828 | Expressions => New_List (New_Occurrence_Of (W_J, Loc))); |
70482933 RK |
1829 | |
1830 | W_Increment := | |
1831 | Make_OK_Assignment_Statement | |
1832 | (Loc, | |
e4494292 | 1833 | Name => New_Occurrence_Of (W_J, Loc), |
70482933 RK |
1834 | Expression => W_Index_Succ); |
1835 | ||
1836 | Append_To (W_Body, W_Increment); | |
937e9676 | 1837 | |
70482933 | 1838 | Append_List_To (W_Body, |
fc84947c | 1839 | Gen_Assign (New_Occurrence_Of (W_J, Loc), Expr)); |
70482933 RK |
1840 | |
1841 | -- Construct the final loop | |
1842 | ||
37368818 RD |
1843 | Append_To (S, |
1844 | Make_Implicit_Loop_Statement | |
1845 | (Node => N, | |
1846 | Identifier => Empty, | |
1847 | Iteration_Scheme => W_Iteration_Scheme, | |
1848 | Statements => W_Body)); | |
70482933 RK |
1849 | |
1850 | return S; | |
1851 | end Gen_While; | |
1852 | ||
59e9bc0b AC |
1853 | -------------------- |
1854 | -- Get_Assoc_Expr -- | |
1855 | -------------------- | |
1856 | ||
c8df2335 EB |
1857 | -- Duplicate the expression in case we will be generating several loops. |
1858 | -- As a result the expression is no longer shared between the loops and | |
1859 | -- is reevaluated for each such loop. | |
1860 | ||
59e9bc0b | 1861 | function Get_Assoc_Expr (Assoc : Node_Id) return Node_Id is |
e9999161 AC |
1862 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
1863 | ||
59e9bc0b AC |
1864 | begin |
1865 | if Box_Present (Assoc) then | |
d7b2fad2 | 1866 | if Present (Default_Aspect_Component_Value (Typ)) then |
c8df2335 | 1867 | return New_Copy_Tree (Default_Aspect_Component_Value (Typ)); |
d7b2fad2 | 1868 | elsif Needs_Simple_Initialization (Ctype) then |
c8df2335 | 1869 | return New_Copy_Tree (Get_Simple_Init_Val (Ctype, N)); |
59e9bc0b AC |
1870 | else |
1871 | return Empty; | |
1872 | end if; | |
1873 | ||
1874 | else | |
c8df2335 EB |
1875 | -- The expression will be passed to Gen_Loop, which immediately |
1876 | -- calls Parent_Kind on it, so we set Parent when it matters. | |
1877 | ||
1878 | return | |
1879 | Expr : constant Node_Id := New_Copy_Tree (Expression (Assoc)) | |
1880 | do | |
1881 | Copy_Parent (To => Expr, From => Expression (Assoc)); | |
1882 | end return; | |
59e9bc0b AC |
1883 | end if; |
1884 | end Get_Assoc_Expr; | |
1885 | ||
70482933 RK |
1886 | --------------------- |
1887 | -- Index_Base_Name -- | |
1888 | --------------------- | |
1889 | ||
1890 | function Index_Base_Name return Node_Id is | |
1891 | begin | |
e4494292 | 1892 | return New_Occurrence_Of (Index_Base, Sloc (N)); |
70482933 RK |
1893 | end Index_Base_Name; |
1894 | ||
1895 | ------------------------------------ | |
1896 | -- Local_Compile_Time_Known_Value -- | |
1897 | ------------------------------------ | |
1898 | ||
1899 | function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is | |
1900 | begin | |
1901 | return Compile_Time_Known_Value (E) | |
1902 | or else | |
1903 | (Nkind (E) = N_Attribute_Reference | |
fbf5a39b AC |
1904 | and then Attribute_Name (E) = Name_Val |
1905 | and then Compile_Time_Known_Value (First (Expressions (E)))); | |
70482933 RK |
1906 | end Local_Compile_Time_Known_Value; |
1907 | ||
1908 | ---------------------- | |
1909 | -- Local_Expr_Value -- | |
1910 | ---------------------- | |
1911 | ||
1912 | function Local_Expr_Value (E : Node_Id) return Uint is | |
1913 | begin | |
1914 | if Compile_Time_Known_Value (E) then | |
1915 | return Expr_Value (E); | |
1916 | else | |
1917 | return Expr_Value (First (Expressions (E))); | |
1918 | end if; | |
1919 | end Local_Expr_Value; | |
1920 | ||
937e9676 | 1921 | -- Local variables |
70482933 | 1922 | |
937e9676 | 1923 | New_Code : constant List_Id := New_List; |
70482933 | 1924 | |
10c257af ES |
1925 | Aggr_Bounds : constant Range_Nodes := |
1926 | Get_Index_Bounds (Aggregate_Bounds (N)); | |
1927 | Aggr_L : Node_Id renames Aggr_Bounds.First; | |
1928 | Aggr_H : Node_Id renames Aggr_Bounds.Last; | |
d74716b3 AC |
1929 | -- The aggregate bounds of this specific subaggregate. Note that if the |
1930 | -- code generated by Build_Array_Aggr_Code is executed then these bounds | |
1931 | -- are OK. Otherwise a Constraint_Error would have been raised. | |
70482933 | 1932 | |
fbf5a39b AC |
1933 | Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L); |
1934 | Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H); | |
7324bf49 | 1935 | -- After Duplicate_Subexpr these are side-effect free |
70482933 | 1936 | |
937e9676 AC |
1937 | Assoc : Node_Id; |
1938 | Choice : Node_Id; | |
1939 | Expr : Node_Id; | |
70482933 | 1940 | |
7c4f3267 BD |
1941 | Bounds : Range_Nodes; |
1942 | Low : Node_Id renames Bounds.First; | |
1943 | High : Node_Id renames Bounds.Last; | |
1944 | ||
70482933 RK |
1945 | Nb_Choices : Nat := 0; |
1946 | Table : Case_Table_Type (1 .. Number_Of_Choices (N)); | |
1947 | -- Used to sort all the different choice values | |
1948 | ||
1949 | Nb_Elements : Int; | |
1950 | -- Number of elements in the positional aggregate | |
1951 | ||
937e9676 | 1952 | Others_Assoc : Node_Id := Empty; |
70482933 RK |
1953 | |
1954 | -- Start of processing for Build_Array_Aggr_Code | |
1955 | ||
1956 | begin | |
8ba0b4cf | 1957 | -- First before we start, a special case. If we have a bit packed |
fbf5a39b AC |
1958 | -- array represented as a modular type, then clear the value to |
1959 | -- zero first, to ensure that unused bits are properly cleared. | |
1960 | ||
fbf5a39b AC |
1961 | if Present (Typ) |
1962 | and then Is_Bit_Packed_Array (Typ) | |
8ca597af | 1963 | and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) |
fbf5a39b | 1964 | then |
445514c0 EB |
1965 | declare |
1966 | Zero : constant Node_Id := Make_Integer_Literal (Loc, Uint_0); | |
1967 | begin | |
1968 | Analyze_And_Resolve (Zero, Packed_Array_Impl_Type (Typ)); | |
1969 | Append_To (New_Code, | |
1970 | Make_Assignment_Statement (Loc, | |
1971 | Name => New_Copy_Tree (Into), | |
1972 | Expression => Unchecked_Convert_To (Typ, Zero))); | |
1973 | end; | |
fbf5a39b AC |
1974 | end if; |
1975 | ||
0e08f7ab ES |
1976 | -- If the component type contains tasks, we need to build a Master |
1977 | -- entity in the current scope, because it will be needed if build- | |
1978 | -- in-place functions are called in the expanded code. | |
1979 | ||
36a66365 | 1980 | if Nkind (Parent (N)) = N_Object_Declaration and then Has_Task (Typ) then |
0e08f7ab ES |
1981 | Build_Master_Entity (Defining_Identifier (Parent (N))); |
1982 | end if; | |
1983 | ||
70482933 | 1984 | -- STEP 1: Process component associations |
3b9fa2df | 1985 | |
fbf5a39b AC |
1986 | -- For those associations that may generate a loop, initialize |
1987 | -- Loop_Actions to collect inserted actions that may be crated. | |
70482933 | 1988 | |
3b9fa2df ES |
1989 | -- Skip this if no component associations |
1990 | ||
70482933 RK |
1991 | if No (Expressions (N)) then |
1992 | ||
1993 | -- STEP 1 (a): Sort the discrete choices | |
1994 | ||
1995 | Assoc := First (Component_Associations (N)); | |
1996 | while Present (Assoc) loop | |
00f45f30 | 1997 | Choice := First (Choice_List (Assoc)); |
70482933 | 1998 | while Present (Choice) loop |
70482933 | 1999 | if Nkind (Choice) = N_Others_Choice then |
59e9bc0b | 2000 | Others_Assoc := Assoc; |
70482933 RK |
2001 | exit; |
2002 | end if; | |
2003 | ||
7c4f3267 | 2004 | Bounds := Get_Index_Bounds (Choice); |
70482933 | 2005 | |
fbf5a39b AC |
2006 | if Low /= High then |
2007 | Set_Loop_Actions (Assoc, New_List); | |
2008 | end if; | |
2009 | ||
70482933 | 2010 | Nb_Choices := Nb_Choices + 1; |
59e9bc0b AC |
2011 | |
2012 | Table (Nb_Choices) := | |
2013 | (Choice_Lo => Low, | |
2014 | Choice_Hi => High, | |
2015 | Choice_Node => Get_Assoc_Expr (Assoc)); | |
2016 | ||
70482933 RK |
2017 | Next (Choice); |
2018 | end loop; | |
2019 | ||
2020 | Next (Assoc); | |
2021 | end loop; | |
2022 | ||
2023 | -- If there is more than one set of choices these must be static | |
2024 | -- and we can therefore sort them. Remember that Nb_Choices does not | |
2025 | -- account for an others choice. | |
2026 | ||
2027 | if Nb_Choices > 1 then | |
2028 | Sort_Case_Table (Table); | |
2029 | end if; | |
2030 | ||
74580e1b | 2031 | -- STEP 1 (b): take care of the whole set of discrete choices |
70482933 RK |
2032 | |
2033 | for J in 1 .. Nb_Choices loop | |
2034 | Low := Table (J).Choice_Lo; | |
2035 | High := Table (J).Choice_Hi; | |
2036 | Expr := Table (J).Choice_Node; | |
70482933 RK |
2037 | Append_List (Gen_Loop (Low, High, Expr), To => New_Code); |
2038 | end loop; | |
2039 | ||
2040 | -- STEP 1 (c): generate the remaining loops to cover others choice | |
2041 | -- We don't need to generate loops over empty gaps, but if there is | |
2042 | -- a single empty range we must analyze the expression for semantics | |
2043 | ||
59e9bc0b | 2044 | if Present (Others_Assoc) then |
70482933 | 2045 | declare |
c8df2335 | 2046 | First : Boolean := True; |
70482933 RK |
2047 | |
2048 | begin | |
2049 | for J in 0 .. Nb_Choices loop | |
70482933 RK |
2050 | if J = 0 then |
2051 | Low := Aggr_Low; | |
2052 | else | |
2053 | Low := Add (1, To => Table (J).Choice_Hi); | |
2054 | end if; | |
2055 | ||
2056 | if J = Nb_Choices then | |
2057 | High := Aggr_High; | |
2058 | else | |
2059 | High := Add (-1, To => Table (J + 1).Choice_Lo); | |
2060 | end if; | |
2061 | ||
fbf5a39b | 2062 | -- If this is an expansion within an init proc, make |
c84700e7 ES |
2063 | -- sure that discriminant references are replaced by |
2064 | -- the corresponding discriminal. | |
2065 | ||
2066 | if Inside_Init_Proc then | |
2067 | if Is_Entity_Name (Low) | |
2068 | and then Ekind (Entity (Low)) = E_Discriminant | |
2069 | then | |
2070 | Set_Entity (Low, Discriminal (Entity (Low))); | |
2071 | end if; | |
2072 | ||
2073 | if Is_Entity_Name (High) | |
2074 | and then Ekind (Entity (High)) = E_Discriminant | |
2075 | then | |
2076 | Set_Entity (High, Discriminal (Entity (High))); | |
2077 | end if; | |
2078 | end if; | |
2079 | ||
c8df2335 | 2080 | if First or else not Empty_Range (Low, High) then |
70482933 | 2081 | First := False; |
6951cbc9 | 2082 | Set_Loop_Actions (Others_Assoc, New_List); |
c8df2335 EB |
2083 | Expr := Get_Assoc_Expr (Others_Assoc); |
2084 | Append_List (Gen_Loop (Low, High, Expr), To => New_Code); | |
70482933 RK |
2085 | end if; |
2086 | end loop; | |
2087 | end; | |
2088 | end if; | |
2089 | ||
2090 | -- STEP 2: Process positional components | |
2091 | ||
2092 | else | |
2093 | -- STEP 2 (a): Generate the assignments for each positional element | |
2094 | -- Note that here we have to use Aggr_L rather than Aggr_Low because | |
2095 | -- Aggr_L is analyzed and Add wants an analyzed expression. | |
2096 | ||
2097 | Expr := First (Expressions (N)); | |
2098 | Nb_Elements := -1; | |
70482933 RK |
2099 | while Present (Expr) loop |
2100 | Nb_Elements := Nb_Elements + 1; | |
2101 | Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr), | |
2102 | To => New_Code); | |
2103 | Next (Expr); | |
2104 | end loop; | |
2105 | ||
02a82539 | 2106 | -- STEP 2 (b): Generate final loop if an others choice is present. |
70482933 RK |
2107 | -- Here Nb_Elements gives the offset of the last positional element. |
2108 | ||
2109 | if Present (Component_Associations (N)) then | |
2110 | Assoc := Last (Component_Associations (N)); | |
70482933 | 2111 | |
02a82539 | 2112 | if Nkind (Assoc) = N_Iterated_Component_Association then |
81e68a19 | 2113 | -- Ada 2022: generate a loop to have a proper scope for |
02a82539 ES |
2114 | -- the identifier that typically appears in the expression. |
2115 | -- The lower bound of the loop is the position after all | |
2116 | -- previous positional components. | |
6e937c1c | 2117 | |
02a82539 ES |
2118 | Append_List (Gen_Loop (Add (Nb_Elements + 1, To => Aggr_L), |
2119 | Aggr_High, | |
2120 | Expression (Assoc)), | |
2121 | To => New_Code); | |
2122 | else | |
2123 | -- Ada 2005 (AI-287) | |
2124 | ||
2125 | Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L), | |
2126 | Aggr_High, | |
2127 | Get_Assoc_Expr (Assoc)), | |
2128 | To => New_Code); | |
2129 | end if; | |
70482933 RK |
2130 | end if; |
2131 | end if; | |
2132 | ||
2133 | return New_Code; | |
2134 | end Build_Array_Aggr_Code; | |
2135 | ||
742084ad MP |
2136 | ------------------------------------- |
2137 | -- Build_Assignment_With_Temporary -- | |
2138 | ------------------------------------- | |
2139 | ||
2140 | function Build_Assignment_With_Temporary | |
2141 | (Target : Node_Id; | |
ca4bff3a | 2142 | Typ : Entity_Id; |
742084ad MP |
2143 | Source : Node_Id) return List_Id |
2144 | is | |
2145 | Loc : constant Source_Ptr := Sloc (Source); | |
2146 | ||
2147 | Aggr_Code : List_Id; | |
2148 | Tmp : Entity_Id; | |
742084ad MP |
2149 | |
2150 | begin | |
ca4bff3a EB |
2151 | Aggr_Code := New_List; |
2152 | ||
2153 | Tmp := Build_Temporary_On_Secondary_Stack (Loc, Typ, Aggr_Code); | |
2154 | ||
742084ad MP |
2155 | Append_To (Aggr_Code, |
2156 | Make_OK_Assignment_Statement (Loc, | |
ca4bff3a EB |
2157 | Name => |
2158 | Make_Explicit_Dereference (Loc, | |
2159 | Prefix => New_Occurrence_Of (Tmp, Loc)), | |
742084ad MP |
2160 | Expression => Source)); |
2161 | ||
2162 | Append_To (Aggr_Code, | |
2163 | Make_OK_Assignment_Statement (Loc, | |
2164 | Name => Target, | |
ca4bff3a EB |
2165 | Expression => |
2166 | Make_Explicit_Dereference (Loc, | |
2167 | Prefix => New_Occurrence_Of (Tmp, Loc)))); | |
2168 | ||
742084ad MP |
2169 | return Aggr_Code; |
2170 | end Build_Assignment_With_Temporary; | |
2171 | ||
70482933 RK |
2172 | ---------------------------- |
2173 | -- Build_Record_Aggr_Code -- | |
2174 | ---------------------------- | |
2175 | ||
2176 | function Build_Record_Aggr_Code | |
f7e6fc47 RD |
2177 | (N : Node_Id; |
2178 | Typ : Entity_Id; | |
2179 | Lhs : Node_Id) return List_Id | |
70482933 RK |
2180 | is |
2181 | Loc : constant Source_Ptr := Sloc (N); | |
2182 | L : constant List_Id := New_List; | |
70482933 RK |
2183 | N_Typ : constant Entity_Id := Etype (N); |
2184 | ||
2185 | Comp : Node_Id; | |
2186 | Instr : Node_Id; | |
2187 | Ref : Node_Id; | |
0f95b178 | 2188 | Target : Entity_Id; |
70482933 RK |
2189 | Comp_Type : Entity_Id; |
2190 | Selector : Entity_Id; | |
2191 | Comp_Expr : Node_Id; | |
70482933 RK |
2192 | Expr_Q : Node_Id; |
2193 | ||
70482933 RK |
2194 | Ancestor_Is_Subtype_Mark : Boolean := False; |
2195 | ||
2196 | Init_Typ : Entity_Id := Empty; | |
5277cab6 | 2197 | |
df3e68b1 HK |
2198 | Finalization_Done : Boolean := False; |
2199 | -- True if Generate_Finalization_Actions has already been called; calls | |
0f95b178 | 2200 | -- after the first do nothing. |
70482933 | 2201 | |
70482933 | 2202 | function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id; |
3b9fa2df ES |
2203 | -- Returns the value that the given discriminant of an ancestor type |
2204 | -- should receive (in the absence of a conflict with the value provided | |
2205 | -- by an ancestor part of an extension aggregate). | |
70482933 RK |
2206 | |
2207 | procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id); | |
3b9fa2df ES |
2208 | -- Check that each of the discriminant values defined by the ancestor |
2209 | -- part of an extension aggregate match the corresponding values | |
2210 | -- provided by either an association of the aggregate or by the | |
2211 | -- constraint imposed by a parent type (RM95-4.3.2(8)). | |
70482933 | 2212 | |
d8f7b976 ES |
2213 | function Compatible_Int_Bounds |
2214 | (Agg_Bounds : Node_Id; | |
2215 | Typ_Bounds : Node_Id) return Boolean; | |
2216 | -- Return true if Agg_Bounds are equal or within Typ_Bounds. It is | |
2217 | -- assumed that both bounds are integer ranges. | |
2218 | ||
df3e68b1 | 2219 | procedure Generate_Finalization_Actions; |
0f95b178 JM |
2220 | -- Deal with the various controlled type data structure initializations |
2221 | -- (but only if it hasn't been done already). | |
d8f7b976 ES |
2222 | |
2223 | function Get_Constraint_Association (T : Entity_Id) return Node_Id; | |
2224 | -- Returns the first discriminant association in the constraint | |
2225 | -- associated with T, if any, otherwise returns Empty. | |
2226 | ||
71129dde AC |
2227 | function Get_Explicit_Discriminant_Value (D : Entity_Id) return Node_Id; |
2228 | -- If the ancestor part is an unconstrained type and further ancestors | |
2229 | -- do not provide discriminants for it, check aggregate components for | |
2230 | -- values of the discriminants. | |
2231 | ||
3e582869 AC |
2232 | procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id); |
2233 | -- If Typ is derived, and constrains discriminants of the parent type, | |
2234 | -- these discriminants are not components of the aggregate, and must be | |
2feb1f84 | 2235 | -- initialized. The assignments are appended to List. The same is done |
10c257af | 2236 | -- if Typ derives from an already constrained subtype of a discriminated |
2feb1f84 | 2237 | -- parent type. |
3e582869 | 2238 | |
71129dde AC |
2239 | procedure Init_Stored_Discriminants; |
2240 | -- If the type is derived and has inherited discriminants, generate | |
2241 | -- explicit assignments for each, using the store constraint of the | |
2242 | -- type. Note that both visible and stored discriminants must be | |
2243 | -- initialized in case the derived type has some renamed and some | |
2244 | -- constrained discriminants. | |
2245 | ||
2246 | procedure Init_Visible_Discriminants; | |
2247 | -- If type has discriminants, retrieve their values from aggregate, | |
2248 | -- and generate explicit assignments for each. This does not include | |
2249 | -- discriminants inherited from ancestor, which are handled above. | |
2250 | -- The type of the aggregate is a subtype created ealier using the | |
2251 | -- given values of the discriminant components of the aggregate. | |
aab45d22 | 2252 | |
d8f7b976 ES |
2253 | function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean; |
2254 | -- Check whether Bounds is a range node and its lower and higher bounds | |
2255 | -- are integers literals. | |
7b9d0d69 | 2256 | |
937e9676 AC |
2257 | function Replace_Type (Expr : Node_Id) return Traverse_Result; |
2258 | -- If the aggregate contains a self-reference, traverse each expression | |
2259 | -- to replace a possible self-reference with a reference to the proper | |
2260 | -- component of the target of the assignment. | |
2261 | ||
2262 | function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result; | |
2263 | -- If default expression of a component mentions a discriminant of the | |
2264 | -- type, it must be rewritten as the discriminant of the target object. | |
2265 | ||
2266 | --------------------------------- | |
2267 | -- Ancestor_Discriminant_Value -- | |
2268 | --------------------------------- | |
70482933 RK |
2269 | |
2270 | function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is | |
2271 | Assoc : Node_Id; | |
2272 | Assoc_Elmt : Elmt_Id; | |
2273 | Aggr_Comp : Entity_Id; | |
2274 | Corresp_Disc : Entity_Id; | |
2275 | Current_Typ : Entity_Id := Base_Type (Typ); | |
2276 | Parent_Typ : Entity_Id; | |
2277 | Parent_Disc : Entity_Id; | |
2278 | Save_Assoc : Node_Id := Empty; | |
2279 | ||
2280 | begin | |
3b9fa2df ES |
2281 | -- First check any discriminant associations to see if any of them |
2282 | -- provide a value for the discriminant. | |
70482933 RK |
2283 | |
2284 | if Present (Discriminant_Specifications (Parent (Current_Typ))) then | |
2285 | Assoc := First (Component_Associations (N)); | |
2286 | while Present (Assoc) loop | |
2287 | Aggr_Comp := Entity (First (Choices (Assoc))); | |
2288 | ||
2289 | if Ekind (Aggr_Comp) = E_Discriminant then | |
2290 | Save_Assoc := Expression (Assoc); | |
2291 | ||
2292 | Corresp_Disc := Corresponding_Discriminant (Aggr_Comp); | |
2293 | while Present (Corresp_Disc) loop | |
3b9fa2df ES |
2294 | |
2295 | -- If found a corresponding discriminant then return the | |
2296 | -- value given in the aggregate. (Note: this is not | |
2297 | -- correct in the presence of side effects. ???) | |
70482933 RK |
2298 | |
2299 | if Disc = Corresp_Disc then | |
2300 | return Duplicate_Subexpr (Expression (Assoc)); | |
2301 | end if; | |
fbf5a39b | 2302 | |
ffcfb997 | 2303 | Corresp_Disc := Corresponding_Discriminant (Corresp_Disc); |
70482933 RK |
2304 | end loop; |
2305 | end if; | |
2306 | ||
2307 | Next (Assoc); | |
2308 | end loop; | |
2309 | end if; | |
2310 | ||
2311 | -- No match found in aggregate, so chain up parent types to find | |
2312 | -- a constraint that defines the value of the discriminant. | |
2313 | ||
2314 | Parent_Typ := Etype (Current_Typ); | |
2315 | while Current_Typ /= Parent_Typ loop | |
9013065b AC |
2316 | if Has_Discriminants (Parent_Typ) |
2317 | and then not Has_Unknown_Discriminants (Parent_Typ) | |
2318 | then | |
70482933 RK |
2319 | Parent_Disc := First_Discriminant (Parent_Typ); |
2320 | ||
2321 | -- We either get the association from the subtype indication | |
2322 | -- of the type definition itself, or from the discriminant | |
2323 | -- constraint associated with the type entity (which is | |
2324 | -- preferable, but it's not always present ???) | |
2325 | ||
aff557c7 | 2326 | if Is_Empty_Elmt_List (Discriminant_Constraint (Current_Typ)) |
70482933 RK |
2327 | then |
2328 | Assoc := Get_Constraint_Association (Current_Typ); | |
2329 | Assoc_Elmt := No_Elmt; | |
2330 | else | |
2331 | Assoc_Elmt := | |
2332 | First_Elmt (Discriminant_Constraint (Current_Typ)); | |
2333 | Assoc := Node (Assoc_Elmt); | |
2334 | end if; | |
2335 | ||
2336 | -- Traverse the discriminants of the parent type looking | |
2337 | -- for one that corresponds. | |
2338 | ||
2339 | while Present (Parent_Disc) and then Present (Assoc) loop | |
2340 | Corresp_Disc := Parent_Disc; | |
2341 | while Present (Corresp_Disc) | |
2342 | and then Disc /= Corresp_Disc | |
2343 | loop | |
ffcfb997 | 2344 | Corresp_Disc := Corresponding_Discriminant (Corresp_Disc); |
70482933 RK |
2345 | end loop; |
2346 | ||
2347 | if Disc = Corresp_Disc then | |
2348 | if Nkind (Assoc) = N_Discriminant_Association then | |
2349 | Assoc := Expression (Assoc); | |
2350 | end if; | |
2351 | ||
e80f0cb0 RD |
2352 | -- If the located association directly denotes |
2353 | -- a discriminant, then use the value of a saved | |
2354 | -- association of the aggregate. This is an approach | |
2355 | -- used to handle certain cases involving multiple | |
2356 | -- discriminants mapped to a single discriminant of | |
2357 | -- a descendant. It's not clear how to locate the | |
2358 | -- appropriate discriminant value for such cases. ??? | |
70482933 RK |
2359 | |
2360 | if Is_Entity_Name (Assoc) | |
2361 | and then Ekind (Entity (Assoc)) = E_Discriminant | |
2362 | then | |
2363 | Assoc := Save_Assoc; | |
2364 | end if; | |
2365 | ||
2366 | return Duplicate_Subexpr (Assoc); | |
2367 | end if; | |
2368 | ||
2369 | Next_Discriminant (Parent_Disc); | |
2370 | ||
2371 | if No (Assoc_Elmt) then | |
2372 | Next (Assoc); | |
ffcfb997 | 2373 | |
70482933 RK |
2374 | else |
2375 | Next_Elmt (Assoc_Elmt); | |
ffcfb997 | 2376 | |
70482933 RK |
2377 | if Present (Assoc_Elmt) then |
2378 | Assoc := Node (Assoc_Elmt); | |
2379 | else | |
2380 | Assoc := Empty; | |
2381 | end if; | |
2382 | end if; | |
2383 | end loop; | |
2384 | end if; | |
2385 | ||
2386 | Current_Typ := Parent_Typ; | |
2387 | Parent_Typ := Etype (Current_Typ); | |
2388 | end loop; | |
2389 | ||
2390 | -- In some cases there's no ancestor value to locate (such as | |
2391 | -- when an ancestor part given by an expression defines the | |
2392 | -- discriminant value). | |
2393 | ||
2394 | return Empty; | |
2395 | end Ancestor_Discriminant_Value; | |
2396 | ||
2397 | ---------------------------------- | |
2398 | -- Check_Ancestor_Discriminants -- | |
2399 | ---------------------------------- | |
2400 | ||
2401 | procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is | |
5277cab6 | 2402 | Discr : Entity_Id; |
70482933 RK |
2403 | Disc_Value : Node_Id; |
2404 | Cond : Node_Id; | |
2405 | ||
2406 | begin | |
5277cab6 | 2407 | Discr := First_Discriminant (Base_Type (Anc_Typ)); |
70482933 RK |
2408 | while Present (Discr) loop |
2409 | Disc_Value := Ancestor_Discriminant_Value (Discr); | |
2410 | ||
2411 | if Present (Disc_Value) then | |
2412 | Cond := Make_Op_Ne (Loc, | |
ffcfb997 | 2413 | Left_Opnd => |
70482933 RK |
2414 | Make_Selected_Component (Loc, |
2415 | Prefix => New_Copy_Tree (Target), | |
2416 | Selector_Name => New_Occurrence_Of (Discr, Loc)), | |
2417 | Right_Opnd => Disc_Value); | |
2418 | ||
07fc65c4 GB |
2419 | Append_To (L, |
2420 | Make_Raise_Constraint_Error (Loc, | |
2421 | Condition => Cond, | |
2422 | Reason => CE_Discriminant_Check_Failed)); | |
70482933 RK |
2423 | end if; |
2424 | ||
2425 | Next_Discriminant (Discr); | |
2426 | end loop; | |
2427 | end Check_Ancestor_Discriminants; | |
2428 | ||
d8f7b976 ES |
2429 | --------------------------- |
2430 | -- Compatible_Int_Bounds -- | |
2431 | --------------------------- | |
2432 | ||
2433 | function Compatible_Int_Bounds | |
2434 | (Agg_Bounds : Node_Id; | |
2435 | Typ_Bounds : Node_Id) return Boolean | |
2436 | is | |
2437 | Agg_Lo : constant Uint := Intval (Low_Bound (Agg_Bounds)); | |
2438 | Agg_Hi : constant Uint := Intval (High_Bound (Agg_Bounds)); | |
2439 | Typ_Lo : constant Uint := Intval (Low_Bound (Typ_Bounds)); | |
2440 | Typ_Hi : constant Uint := Intval (High_Bound (Typ_Bounds)); | |
2441 | begin | |
2442 | return Typ_Lo <= Agg_Lo and then Agg_Hi <= Typ_Hi; | |
2443 | end Compatible_Int_Bounds; | |
2444 | ||
937e9676 AC |
2445 | ----------------------------------- |
2446 | -- Generate_Finalization_Actions -- | |
2447 | ----------------------------------- | |
2448 | ||
2449 | procedure Generate_Finalization_Actions is | |
2450 | begin | |
2451 | -- Do the work only the first time this is called | |
2452 | ||
2453 | if Finalization_Done then | |
2454 | return; | |
2455 | end if; | |
2456 | ||
2457 | Finalization_Done := True; | |
2458 | ||
2459 | -- Determine the external finalization list. It is either the | |
2460 | -- finalization list of the outer scope or the one coming from an | |
2461 | -- outer aggregate. When the target is not a temporary, the proper | |
2462 | -- scope is the scope of the target rather than the potentially | |
2463 | -- transient current scope. | |
2464 | ||
2465 | if Is_Controlled (Typ) and then Ancestor_Is_Subtype_Mark then | |
2466 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); | |
2467 | Set_Assignment_OK (Ref); | |
2468 | ||
2469 | Append_To (L, | |
2470 | Make_Procedure_Call_Statement (Loc, | |
2471 | Name => | |
2472 | New_Occurrence_Of | |
2473 | (Find_Prim_Op (Init_Typ, Name_Initialize), Loc), | |
2474 | Parameter_Associations => New_List (New_Copy_Tree (Ref)))); | |
2475 | end if; | |
2476 | end Generate_Finalization_Actions; | |
2477 | ||
70482933 RK |
2478 | -------------------------------- |
2479 | -- Get_Constraint_Association -- | |
2480 | -------------------------------- | |
2481 | ||
2482 | function Get_Constraint_Association (T : Entity_Id) return Node_Id is | |
2c17ca0a AC |
2483 | Indic : Node_Id; |
2484 | Typ : Entity_Id; | |
70482933 RK |
2485 | |
2486 | begin | |
2c17ca0a AC |
2487 | Typ := T; |
2488 | ||
598a56c0 ES |
2489 | -- If type is private, get constraint from full view. This was |
2490 | -- previously done in an instance context, but is needed whenever | |
2491 | -- the ancestor part has a discriminant, possibly inherited through | |
2492 | -- multiple derivations. | |
2c17ca0a | 2493 | |
598a56c0 | 2494 | if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then |
2c17ca0a AC |
2495 | Typ := Full_View (Typ); |
2496 | end if; | |
2497 | ||
2498 | Indic := Subtype_Indication (Type_Definition (Parent (Typ))); | |
2499 | ||
598a56c0 | 2500 | -- Verify that the subtype indication carries a constraint |
70482933 RK |
2501 | |
2502 | if Nkind (Indic) = N_Subtype_Indication | |
2503 | and then Present (Constraint (Indic)) | |
2504 | then | |
2505 | return First (Constraints (Constraint (Indic))); | |
2506 | end if; | |
2507 | ||
2508 | return Empty; | |
2509 | end Get_Constraint_Association; | |
2510 | ||
aab45d22 AC |
2511 | ------------------------------------- |
2512 | -- Get_Explicit_Discriminant_Value -- | |
2513 | ------------------------------------- | |
2514 | ||
7893514c RD |
2515 | function Get_Explicit_Discriminant_Value |
2516 | (D : Entity_Id) return Node_Id | |
aab45d22 AC |
2517 | is |
2518 | Assoc : Node_Id; | |
2519 | Choice : Node_Id; | |
2520 | Val : Node_Id; | |
2521 | ||
2522 | begin | |
2523 | -- The aggregate has been normalized and all associations have a | |
2524 | -- single choice. | |
2525 | ||
2526 | Assoc := First (Component_Associations (N)); | |
2527 | while Present (Assoc) loop | |
2528 | Choice := First (Choices (Assoc)); | |
7893514c | 2529 | |
aab45d22 AC |
2530 | if Chars (Choice) = Chars (D) then |
2531 | Val := Expression (Assoc); | |
2532 | Remove (Assoc); | |
2533 | return Val; | |
2534 | end if; | |
2535 | ||
2536 | Next (Assoc); | |
2537 | end loop; | |
2538 | ||
2539 | return Empty; | |
2540 | end Get_Explicit_Discriminant_Value; | |
2541 | ||
3e582869 AC |
2542 | ------------------------------- |
2543 | -- Init_Hidden_Discriminants -- | |
2544 | ------------------------------- | |
2545 | ||
2546 | procedure Init_Hidden_Discriminants (Typ : Entity_Id; List : List_Id) is | |
a25e72b5 AC |
2547 | function Is_Completely_Hidden_Discriminant |
2548 | (Discr : Entity_Id) return Boolean; | |
2549 | -- Determine whether Discr is a completely hidden discriminant of | |
2550 | -- type Typ. | |
2551 | ||
2552 | --------------------------------------- | |
2553 | -- Is_Completely_Hidden_Discriminant -- | |
2554 | --------------------------------------- | |
2555 | ||
2556 | function Is_Completely_Hidden_Discriminant | |
2557 | (Discr : Entity_Id) return Boolean | |
2558 | is | |
2559 | Item : Entity_Id; | |
2560 | ||
2561 | begin | |
2562 | -- Use First/Next_Entity as First/Next_Discriminant do not yield | |
2563 | -- completely hidden discriminants. | |
2564 | ||
2565 | Item := First_Entity (Typ); | |
2566 | while Present (Item) loop | |
2567 | if Ekind (Item) = E_Discriminant | |
2568 | and then Is_Completely_Hidden (Item) | |
2569 | and then Chars (Original_Record_Component (Item)) = | |
2570 | Chars (Discr) | |
2571 | then | |
2572 | return True; | |
2573 | end if; | |
2574 | ||
2575 | Next_Entity (Item); | |
2576 | end loop; | |
2577 | ||
2578 | return False; | |
2579 | end Is_Completely_Hidden_Discriminant; | |
2580 | ||
2581 | -- Local variables | |
2582 | ||
2583 | Base_Typ : Entity_Id; | |
2584 | Discr : Entity_Id; | |
2585 | Discr_Constr : Elmt_Id; | |
2586 | Discr_Init : Node_Id; | |
2587 | Discr_Val : Node_Id; | |
ddce04b8 | 2588 | In_Aggr_Type : Boolean; |
a25e72b5 AC |
2589 | Par_Typ : Entity_Id; |
2590 | ||
2591 | -- Start of processing for Init_Hidden_Discriminants | |
3e582869 AC |
2592 | |
2593 | begin | |
7b536495 AC |
2594 | -- The constraints on the hidden discriminants, if present, are kept |
2595 | -- in the Stored_Constraint list of the type itself, or in that of | |
ddce04b8 AC |
2596 | -- the base type. If not in the constraints of the aggregate itself, |
2597 | -- we examine ancestors to find discriminants that are not renamed | |
2598 | -- by other discriminants but constrained explicitly. | |
2599 | ||
2600 | In_Aggr_Type := True; | |
2feb1f84 | 2601 | |
a25e72b5 AC |
2602 | Base_Typ := Base_Type (Typ); |
2603 | while Is_Derived_Type (Base_Typ) | |
596f7139 | 2604 | and then |
a25e72b5 | 2605 | (Present (Stored_Constraint (Base_Typ)) |
596f7139 AC |
2606 | or else |
2607 | (In_Aggr_Type and then Present (Stored_Constraint (Typ)))) | |
3e582869 | 2608 | loop |
a25e72b5 | 2609 | Par_Typ := Etype (Base_Typ); |
7b536495 | 2610 | |
a25e72b5 | 2611 | if not Has_Discriminants (Par_Typ) then |
2feb1f84 AC |
2612 | return; |
2613 | end if; | |
3e582869 | 2614 | |
a25e72b5 | 2615 | Discr := First_Discriminant (Par_Typ); |
2feb1f84 | 2616 | |
bdc193ba | 2617 | -- We know that one of the stored-constraint lists is present |
2feb1f84 | 2618 | |
a25e72b5 AC |
2619 | if Present (Stored_Constraint (Base_Typ)) then |
2620 | Discr_Constr := First_Elmt (Stored_Constraint (Base_Typ)); | |
bdc193ba AC |
2621 | |
2622 | -- For private extension, stored constraint may be on full view | |
2623 | ||
a25e72b5 AC |
2624 | elsif Is_Private_Type (Base_Typ) |
2625 | and then Present (Full_View (Base_Typ)) | |
2626 | and then Present (Stored_Constraint (Full_View (Base_Typ))) | |
bdc193ba | 2627 | then |
a25e72b5 AC |
2628 | Discr_Constr := |
2629 | First_Elmt (Stored_Constraint (Full_View (Base_Typ))); | |
bdc193ba | 2630 | |
f056076f BD |
2631 | -- Otherwise, no discriminant to process |
2632 | ||
2feb1f84 | 2633 | else |
f056076f | 2634 | Discr_Constr := No_Elmt; |
2feb1f84 AC |
2635 | end if; |
2636 | ||
a25e72b5 AC |
2637 | while Present (Discr) and then Present (Discr_Constr) loop |
2638 | Discr_Val := Node (Discr_Constr); | |
2639 | ||
2640 | -- The parent discriminant is renamed in the derived type, | |
2641 | -- nothing to initialize. | |
3e582869 | 2642 | |
a25e72b5 AC |
2643 | -- type Deriv_Typ (Discr : ...) |
2644 | -- is new Parent_Typ (Discr => Discr); | |
3e582869 | 2645 | |
a25e72b5 AC |
2646 | if Is_Entity_Name (Discr_Val) |
2647 | and then Ekind (Entity (Discr_Val)) = E_Discriminant | |
3e582869 | 2648 | then |
a25e72b5 AC |
2649 | null; |
2650 | ||
2651 | -- When the parent discriminant is constrained at the type | |
2652 | -- extension level, it does not appear in the derived type. | |
2653 | ||
2654 | -- type Deriv_Typ (Discr : ...) | |
2655 | -- is new Parent_Typ (Discr => Discr, | |
2656 | -- Hidden_Discr => Expression); | |
3e582869 | 2657 | |
a25e72b5 AC |
2658 | elsif Is_Completely_Hidden_Discriminant (Discr) then |
2659 | null; | |
2660 | ||
2661 | -- Otherwise initialize the discriminant | |
2662 | ||
2663 | else | |
2664 | Discr_Init := | |
3e582869 | 2665 | Make_OK_Assignment_Statement (Loc, |
a25e72b5 AC |
2666 | Name => |
2667 | Make_Selected_Component (Loc, | |
2668 | Prefix => New_Copy_Tree (Target), | |
2669 | Selector_Name => New_Occurrence_Of (Discr, Loc)), | |
2670 | Expression => New_Copy_Tree (Discr_Val)); | |
3e582869 | 2671 | |
a25e72b5 | 2672 | Append_To (List, Discr_Init); |
3e582869 AC |
2673 | end if; |
2674 | ||
a25e72b5 AC |
2675 | Next_Elmt (Discr_Constr); |
2676 | Next_Discriminant (Discr); | |
3e582869 AC |
2677 | end loop; |
2678 | ||
ddce04b8 | 2679 | In_Aggr_Type := False; |
a25e72b5 | 2680 | Base_Typ := Base_Type (Par_Typ); |
3e582869 AC |
2681 | end loop; |
2682 | end Init_Hidden_Discriminants; | |
2683 | ||
71129dde AC |
2684 | -------------------------------- |
2685 | -- Init_Visible_Discriminants -- | |
2686 | -------------------------------- | |
2687 | ||
2688 | procedure Init_Visible_Discriminants is | |
2689 | Discriminant : Entity_Id; | |
2690 | Discriminant_Value : Node_Id; | |
2691 | ||
2692 | begin | |
2693 | Discriminant := First_Discriminant (Typ); | |
2694 | while Present (Discriminant) loop | |
2695 | Comp_Expr := | |
2696 | Make_Selected_Component (Loc, | |
2697 | Prefix => New_Copy_Tree (Target), | |
2698 | Selector_Name => New_Occurrence_Of (Discriminant, Loc)); | |
2699 | ||
2700 | Discriminant_Value := | |
2701 | Get_Discriminant_Value | |
2702 | (Discriminant, Typ, Discriminant_Constraint (N_Typ)); | |
2703 | ||
2704 | Instr := | |
2705 | Make_OK_Assignment_Statement (Loc, | |
2706 | Name => Comp_Expr, | |
2707 | Expression => New_Copy_Tree (Discriminant_Value)); | |
2708 | ||
71129dde AC |
2709 | Append_To (L, Instr); |
2710 | ||
2711 | Next_Discriminant (Discriminant); | |
2712 | end loop; | |
2713 | end Init_Visible_Discriminants; | |
2714 | ||
2715 | ------------------------------- | |
2716 | -- Init_Stored_Discriminants -- | |
2717 | ------------------------------- | |
2718 | ||
2719 | procedure Init_Stored_Discriminants is | |
2720 | Discriminant : Entity_Id; | |
2721 | Discriminant_Value : Node_Id; | |
2722 | ||
2723 | begin | |
2724 | Discriminant := First_Stored_Discriminant (Typ); | |
2725 | while Present (Discriminant) loop | |
2726 | Comp_Expr := | |
2727 | Make_Selected_Component (Loc, | |
2728 | Prefix => New_Copy_Tree (Target), | |
2729 | Selector_Name => New_Occurrence_Of (Discriminant, Loc)); | |
2730 | ||
2731 | Discriminant_Value := | |
2732 | Get_Discriminant_Value | |
2733 | (Discriminant, N_Typ, Discriminant_Constraint (N_Typ)); | |
2734 | ||
2735 | Instr := | |
2736 | Make_OK_Assignment_Statement (Loc, | |
2737 | Name => Comp_Expr, | |
2738 | Expression => New_Copy_Tree (Discriminant_Value)); | |
2739 | ||
71129dde AC |
2740 | Append_To (L, Instr); |
2741 | ||
2742 | Next_Stored_Discriminant (Discriminant); | |
2743 | end loop; | |
2744 | end Init_Stored_Discriminants; | |
2745 | ||
937e9676 AC |
2746 | ------------------------- |
2747 | -- Is_Int_Range_Bounds -- | |
2748 | ------------------------- | |
2749 | ||
2750 | function Is_Int_Range_Bounds (Bounds : Node_Id) return Boolean is | |
2751 | begin | |
2752 | return Nkind (Bounds) = N_Range | |
2753 | and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal | |
2754 | and then Nkind (High_Bound (Bounds)) = N_Integer_Literal; | |
2755 | end Is_Int_Range_Bounds; | |
f2abc637 | 2756 | |
0f95b178 JM |
2757 | ------------------ |
2758 | -- Replace_Type -- | |
2759 | ------------------ | |
2760 | ||
2761 | function Replace_Type (Expr : Node_Id) return Traverse_Result is | |
2762 | begin | |
acf63f8c ES |
2763 | -- Note regarding the Root_Type test below: Aggregate components for |
2764 | -- self-referential types include attribute references to the current | |
2765 | -- instance, of the form: Typ'access, etc.. These references are | |
2766 | -- rewritten as references to the target of the aggregate: the | |
2767 | -- left-hand side of an assignment, the entity in a declaration, | |
2768 | -- or a temporary. Without this test, we would improperly extended | |
2769 | -- this rewriting to attribute references whose prefix was not the | |
2770 | -- type of the aggregate. | |
2771 | ||
0f95b178 | 2772 | if Nkind (Expr) = N_Attribute_Reference |
acf63f8c | 2773 | and then Is_Entity_Name (Prefix (Expr)) |
0f95b178 | 2774 | and then Is_Type (Entity (Prefix (Expr))) |
acf63f8c | 2775 | and then Root_Type (Etype (N)) = Root_Type (Entity (Prefix (Expr))) |
0f95b178 JM |
2776 | then |
2777 | if Is_Entity_Name (Lhs) then | |
304757d2 | 2778 | Rewrite (Prefix (Expr), New_Occurrence_Of (Entity (Lhs), Loc)); |
0f95b178 JM |
2779 | |
2780 | else | |
2781 | Rewrite (Expr, | |
2782 | Make_Attribute_Reference (Loc, | |
2783 | Attribute_Name => Name_Unrestricted_Access, | |
2784 | Prefix => New_Copy_Tree (Lhs))); | |
2785 | Set_Analyzed (Parent (Expr), False); | |
2786 | end if; | |
2787 | end if; | |
2788 | ||
2789 | return OK; | |
2790 | end Replace_Type; | |
2791 | ||
937e9676 AC |
2792 | -------------------------- |
2793 | -- Rewrite_Discriminant -- | |
2794 | -------------------------- | |
2795 | ||
2796 | function Rewrite_Discriminant (Expr : Node_Id) return Traverse_Result is | |
2797 | begin | |
2798 | if Is_Entity_Name (Expr) | |
2799 | and then Present (Entity (Expr)) | |
2800 | and then Ekind (Entity (Expr)) = E_In_Parameter | |
2801 | and then Present (Discriminal_Link (Entity (Expr))) | |
2802 | and then Scope (Discriminal_Link (Entity (Expr))) = | |
2803 | Base_Type (Etype (N)) | |
2804 | then | |
2805 | Rewrite (Expr, | |
2806 | Make_Selected_Component (Loc, | |
2807 | Prefix => New_Copy_Tree (Lhs), | |
2808 | Selector_Name => Make_Identifier (Loc, Chars (Expr)))); | |
b8411279 ES |
2809 | |
2810 | -- The generated code will be reanalyzed, but if the reference | |
2811 | -- to the discriminant appears within an already analyzed | |
2812 | -- expression (e.g. a conditional) we must set its proper entity | |
2813 | -- now. Context is an initialization procedure. | |
2814 | ||
2815 | Analyze (Expr); | |
937e9676 AC |
2816 | end if; |
2817 | ||
2818 | return OK; | |
2819 | end Rewrite_Discriminant; | |
0f95b178 | 2820 | |
f2abc637 AC |
2821 | procedure Replace_Discriminants is |
2822 | new Traverse_Proc (Rewrite_Discriminant); | |
2823 | ||
937e9676 AC |
2824 | procedure Replace_Self_Reference is |
2825 | new Traverse_Proc (Replace_Type); | |
2826 | ||
70482933 RK |
2827 | -- Start of processing for Build_Record_Aggr_Code |
2828 | ||
2829 | begin | |
0f95b178 JM |
2830 | if Has_Self_Reference (N) then |
2831 | Replace_Self_Reference (N); | |
2832 | end if; | |
2833 | ||
2834 | -- If the target of the aggregate is class-wide, we must convert it | |
2835 | -- to the actual type of the aggregate, so that the proper components | |
2836 | -- are visible. We know already that the types are compatible. | |
2837 | ||
c0ceba6c | 2838 | if Present (Etype (Lhs)) and then Is_Class_Wide_Type (Etype (Lhs)) then |
0f95b178 JM |
2839 | Target := Unchecked_Convert_To (Typ, Lhs); |
2840 | else | |
2841 | Target := Lhs; | |
2842 | end if; | |
2843 | ||
3b9fa2df ES |
2844 | -- Deal with the ancestor part of extension aggregates or with the |
2845 | -- discriminants of the root type. | |
70482933 RK |
2846 | |
2847 | if Nkind (N) = N_Extension_Aggregate then | |
2848 | declare | |
d18dd43c EB |
2849 | Ancestor : constant Node_Id := Ancestor_Part (N); |
2850 | Ancestor_Q : constant Node_Id := Unqualify (Ancestor); | |
2851 | ||
df3e68b1 | 2852 | Assign : List_Id; |
70482933 RK |
2853 | |
2854 | begin | |
d18dd43c | 2855 | -- If the ancestor part is a subtype mark T, we generate |
fbf5a39b | 2856 | |
df3e68b1 HK |
2857 | -- init-proc (T (tmp)); if T is constrained and |
2858 | -- init-proc (S (tmp)); where S applies an appropriate | |
2859 | -- constraint if T is unconstrained | |
70482933 | 2860 | |
df3e68b1 HK |
2861 | if Is_Entity_Name (Ancestor) |
2862 | and then Is_Type (Entity (Ancestor)) | |
2863 | then | |
70482933 RK |
2864 | Ancestor_Is_Subtype_Mark := True; |
2865 | ||
df3e68b1 HK |
2866 | if Is_Constrained (Entity (Ancestor)) then |
2867 | Init_Typ := Entity (Ancestor); | |
70482933 | 2868 | |
3b9fa2df ES |
2869 | -- For an ancestor part given by an unconstrained type mark, |
2870 | -- create a subtype constrained by appropriate corresponding | |
2871 | -- discriminant values coming from either associations of the | |
2872 | -- aggregate or a constraint on a parent type. The subtype will | |
2873 | -- be used to generate the correct default value for the | |
2874 | -- ancestor part. | |
70482933 | 2875 | |
df3e68b1 | 2876 | elsif Has_Discriminants (Entity (Ancestor)) then |
70482933 | 2877 | declare |
df3e68b1 | 2878 | Anc_Typ : constant Entity_Id := Entity (Ancestor); |
fbf5a39b AC |
2879 | Anc_Constr : constant List_Id := New_List; |
2880 | Discrim : Entity_Id; | |
70482933 RK |
2881 | Disc_Value : Node_Id; |
2882 | New_Indic : Node_Id; | |
2883 | Subt_Decl : Node_Id; | |
fbf5a39b | 2884 | |
70482933 | 2885 | begin |
fbf5a39b | 2886 | Discrim := First_Discriminant (Anc_Typ); |
70482933 RK |
2887 | while Present (Discrim) loop |
2888 | Disc_Value := Ancestor_Discriminant_Value (Discrim); | |
aab45d22 AC |
2889 | |
2890 | -- If no usable discriminant in ancestors, check | |
2891 | -- whether aggregate has an explicit value for it. | |
2892 | ||
2893 | if No (Disc_Value) then | |
2894 | Disc_Value := | |
2895 | Get_Explicit_Discriminant_Value (Discrim); | |
2896 | end if; | |
2897 | ||
70482933 RK |
2898 | Append_To (Anc_Constr, Disc_Value); |
2899 | Next_Discriminant (Discrim); | |
2900 | end loop; | |
2901 | ||
2902 | New_Indic := | |
2903 | Make_Subtype_Indication (Loc, | |
2904 | Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc), | |
2905 | Constraint => | |
2906 | Make_Index_Or_Discriminant_Constraint (Loc, | |
2907 | Constraints => Anc_Constr)); | |
2908 | ||
2909 | Init_Typ := Create_Itype (Ekind (Anc_Typ), N); | |
2910 | ||
2911 | Subt_Decl := | |
2912 | Make_Subtype_Declaration (Loc, | |
2913 | Defining_Identifier => Init_Typ, | |
2914 | Subtype_Indication => New_Indic); | |
2915 | ||
3b9fa2df ES |
2916 | -- Itypes must be analyzed with checks off Declaration |
2917 | -- must have a parent for proper handling of subsidiary | |
2918 | -- actions. | |
70482933 | 2919 | |
07fc65c4 | 2920 | Set_Parent (Subt_Decl, N); |
70482933 RK |
2921 | Analyze (Subt_Decl, Suppress => All_Checks); |
2922 | end; | |
2923 | end if; | |
2924 | ||
2925 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); | |
2926 | Set_Assignment_OK (Ref); | |
2927 | ||
64425dff | 2928 | if not Is_Interface (Init_Typ) then |
3bb3f6d6 AC |
2929 | Append_List_To (L, |
2930 | Build_Initialization_Call (Loc, | |
2931 | Id_Ref => Ref, | |
2932 | Typ => Init_Typ, | |
2933 | In_Init_Proc => Within_Init_Proc, | |
2934 | With_Default_Init => Has_Default_Init_Comps (N) | |
2935 | or else | |
2936 | Has_Task (Base_Type (Init_Typ)))); | |
2937 | ||
df3e68b1 HK |
2938 | if Is_Constrained (Entity (Ancestor)) |
2939 | and then Has_Discriminants (Entity (Ancestor)) | |
3bb3f6d6 | 2940 | then |
df3e68b1 | 2941 | Check_Ancestor_Discriminants (Entity (Ancestor)); |
3bb3f6d6 | 2942 | end if; |
f7937111 GD |
2943 | |
2944 | -- If ancestor type has Default_Initialization_Condition, | |
2945 | -- add a DIC check after the ancestor object is initialized | |
2946 | -- by default. | |
2947 | ||
2948 | if Has_DIC (Entity (Ancestor)) | |
2949 | and then Present (DIC_Procedure (Entity (Ancestor))) | |
2950 | then | |
2951 | Append_To (L, | |
2952 | Build_DIC_Call | |
2953 | (Loc, New_Copy_Tree (Ref), Entity (Ancestor))); | |
2954 | end if; | |
70482933 RK |
2955 | end if; |
2956 | ||
11795185 JM |
2957 | -- Handle calls to C++ constructors |
2958 | ||
df3e68b1 HK |
2959 | elsif Is_CPP_Constructor_Call (Ancestor) then |
2960 | Init_Typ := Etype (Ancestor); | |
11795185 JM |
2961 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); |
2962 | Set_Assignment_OK (Ref); | |
2963 | ||
2964 | Append_List_To (L, | |
2965 | Build_Initialization_Call (Loc, | |
2966 | Id_Ref => Ref, | |
2967 | Typ => Init_Typ, | |
2968 | In_Init_Proc => Within_Init_Proc, | |
2969 | With_Default_Init => Has_Default_Init_Comps (N), | |
df3e68b1 | 2970 | Constructor_Ref => Ancestor)); |
11795185 | 2971 | |
c5ee5ad2 BD |
2972 | -- Ada 2005 (AI-287): If the ancestor part is an aggregate of |
2973 | -- limited type, a recursive call expands the ancestor. Note that | |
2974 | -- in the limited case, the ancestor part must be either a | |
d4dfb005 BD |
2975 | -- function call (possibly qualified) or aggregate (definitely |
2976 | -- qualified). | |
65356e64 | 2977 | |
df3e68b1 | 2978 | elsif Is_Limited_Type (Etype (Ancestor)) |
d18dd43c EB |
2979 | and then Nkind (Ancestor_Q) in N_Aggregate |
2980 | | N_Extension_Aggregate | |
c5ee5ad2 | 2981 | then |
7b9d0d69 | 2982 | Append_List_To (L, |
f7e6fc47 | 2983 | Build_Record_Aggr_Code |
d18dd43c EB |
2984 | (N => Ancestor_Q, |
2985 | Typ => Etype (Ancestor_Q), | |
f7e6fc47 | 2986 | Lhs => Target)); |
65356e64 | 2987 | |
d18dd43c | 2988 | -- If the ancestor part is an expression E of type T, we generate |
3b9fa2df | 2989 | |
df3e68b1 | 2990 | -- T (tmp) := E; |
3b9fa2df | 2991 | |
c5ee5ad2 | 2992 | -- In Ada 2005, this includes the case of a (possibly qualified) |
d18dd43c EB |
2993 | -- limited function call. The assignment will later be turned into |
2994 | -- a build-in-place function call (for further details, see | |
c5ee5ad2 | 2995 | -- Make_Build_In_Place_Call_In_Assignment). |
70482933 RK |
2996 | |
2997 | else | |
df3e68b1 | 2998 | Init_Typ := Etype (Ancestor); |
70482933 | 2999 | |
7b9d0d69 ES |
3000 | -- If the ancestor part is an aggregate, force its full |
3001 | -- expansion, which was delayed. | |
3002 | ||
d18dd43c | 3003 | if Nkind (Ancestor_Q) in N_Aggregate | N_Extension_Aggregate |
7b9d0d69 | 3004 | then |
df3e68b1 HK |
3005 | Set_Analyzed (Ancestor, False); |
3006 | Set_Analyzed (Expression (Ancestor), False); | |
7b9d0d69 ES |
3007 | end if; |
3008 | ||
3009 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); | |
7b9d0d69 | 3010 | |
7b9d0d69 ES |
3011 | Assign := New_List ( |
3012 | Make_OK_Assignment_Statement (Loc, | |
3013 | Name => Ref, | |
df3e68b1 | 3014 | Expression => Ancestor)); |
70482933 | 3015 | |
c0ceba6c EB |
3016 | -- Arrange for the component to be adjusted if need be (the |
3017 | -- call will be generated by Make_Tag_Ctrl_Assignment). | |
70482933 | 3018 | |
d18dd43c | 3019 | if Needs_Finalization (Init_Typ) |
c0ceba6c | 3020 | and then not Is_Limited_View (Init_Typ) |
3b9fa2df | 3021 | then |
c0ceba6c EB |
3022 | Set_No_Finalize_Actions (First (Assign)); |
3023 | else | |
3024 | Set_No_Ctrl_Actions (First (Assign)); | |
70482933 RK |
3025 | end if; |
3026 | ||
70482933 | 3027 | Append_To (L, |
c0ceba6c | 3028 | Make_Suppress_Block (Loc, Name_Discriminant_Check, Assign)); |
70482933 RK |
3029 | |
3030 | if Has_Discriminants (Init_Typ) then | |
3031 | Check_Ancestor_Discriminants (Init_Typ); | |
3032 | end if; | |
3033 | end if; | |
3034 | end; | |
3035 | ||
376e7d14 AC |
3036 | -- Generate assignments of hidden discriminants. If the base type is |
3037 | -- an unchecked union, the discriminants are unknown to the back-end | |
3038 | -- and absent from a value of the type, so assignments for them are | |
3039 | -- not emitted. | |
3e582869 AC |
3040 | |
3041 | if Has_Discriminants (Typ) | |
3042 | and then not Is_Unchecked_Union (Base_Type (Typ)) | |
3043 | then | |
3044 | Init_Hidden_Discriminants (Typ, L); | |
3045 | end if; | |
3046 | ||
fbf5a39b AC |
3047 | -- Normal case (not an extension aggregate) |
3048 | ||
70482933 RK |
3049 | else |
3050 | -- Generate the discriminant expressions, component by component. | |
3051 | -- If the base type is an unchecked union, the discriminants are | |
3052 | -- unknown to the back-end and absent from a value of the type, so | |
3053 | -- assignments for them are not emitted. | |
3054 | ||
3055 | if Has_Discriminants (Typ) | |
3056 | and then not Is_Unchecked_Union (Base_Type (Typ)) | |
3057 | then | |
3e582869 | 3058 | Init_Hidden_Discriminants (Typ, L); |
d8f7b976 ES |
3059 | |
3060 | -- Generate discriminant init values for the visible discriminants | |
70482933 | 3061 | |
71129dde | 3062 | Init_Visible_Discriminants; |
70482933 | 3063 | |
71129dde AC |
3064 | if Is_Derived_Type (N_Typ) then |
3065 | Init_Stored_Discriminants; | |
3066 | end if; | |
70482933 RK |
3067 | end if; |
3068 | end if; | |
3069 | ||
28541488 JM |
3070 | -- For CPP types we generate an implicit call to the C++ default |
3071 | -- constructor to ensure the proper initialization of the _Tag | |
3072 | -- component. | |
3073 | ||
36a66365 | 3074 | if Is_CPP_Class (Root_Type (Typ)) and then CPP_Num_Prims (Typ) > 0 then |
cefce34c | 3075 | Invoke_Constructor : declare |
15f0f591 | 3076 | CPP_Parent : constant Entity_Id := Enclosing_CPP_Parent (Typ); |
cefce34c JM |
3077 | |
3078 | procedure Invoke_IC_Proc (T : Entity_Id); | |
3079 | -- Recursive routine used to climb to parents. Required because | |
3080 | -- parents must be initialized before descendants to ensure | |
3081 | -- propagation of inherited C++ slots. | |
3082 | ||
3083 | -------------------- | |
3084 | -- Invoke_IC_Proc -- | |
3085 | -------------------- | |
3086 | ||
3087 | procedure Invoke_IC_Proc (T : Entity_Id) is | |
3088 | begin | |
3089 | -- Avoid generating extra calls. Initialization required | |
3090 | -- only for types defined from the level of derivation of | |
3091 | -- type of the constructor and the type of the aggregate. | |
3092 | ||
3093 | if T = CPP_Parent then | |
3094 | return; | |
3095 | end if; | |
3096 | ||
3097 | Invoke_IC_Proc (Etype (T)); | |
3098 | ||
3099 | -- Generate call to the IC routine | |
3100 | ||
3101 | if Present (CPP_Init_Proc (T)) then | |
3102 | Append_To (L, | |
3103 | Make_Procedure_Call_Statement (Loc, | |
ffcfb997 | 3104 | Name => New_Occurrence_Of (CPP_Init_Proc (T), Loc))); |
cefce34c JM |
3105 | end if; |
3106 | end Invoke_IC_Proc; | |
3107 | ||
3108 | -- Start of processing for Invoke_Constructor | |
3109 | ||
3110 | begin | |
3111 | -- Implicit invocation of the C++ constructor | |
3112 | ||
3113 | if Nkind (N) = N_Aggregate then | |
3114 | Append_To (L, | |
3115 | Make_Procedure_Call_Statement (Loc, | |
37368818 RD |
3116 | Name => |
3117 | New_Occurrence_Of (Base_Init_Proc (CPP_Parent), Loc), | |
cefce34c JM |
3118 | Parameter_Associations => New_List ( |
3119 | Unchecked_Convert_To (CPP_Parent, | |
3120 | New_Copy_Tree (Lhs))))); | |
3121 | end if; | |
3122 | ||
3123 | Invoke_IC_Proc (Typ); | |
3124 | end Invoke_Constructor; | |
28541488 JM |
3125 | end if; |
3126 | ||
70482933 RK |
3127 | -- Generate the assignments, component by component |
3128 | ||
3129 | -- tmp.comp1 := Expr1_From_Aggr; | |
3130 | -- tmp.comp2 := Expr2_From_Aggr; | |
3131 | -- .... | |
3132 | ||
3133 | Comp := First (Component_Associations (N)); | |
3134 | while Present (Comp) loop | |
b7e429ab | 3135 | Selector := Entity (First (Choices (Comp))); |
df0ac6e1 | 3136 | pragma Assert (Present (Selector)); |
70482933 | 3137 | |
236fecbf JM |
3138 | -- C++ constructors |
3139 | ||
3140 | if Is_CPP_Constructor_Call (Expression (Comp)) then | |
3141 | Append_List_To (L, | |
3142 | Build_Initialization_Call (Loc, | |
37368818 RD |
3143 | Id_Ref => |
3144 | Make_Selected_Component (Loc, | |
3145 | Prefix => New_Copy_Tree (Target), | |
3146 | Selector_Name => New_Occurrence_Of (Selector, Loc)), | |
1c612f29 RD |
3147 | Typ => Etype (Selector), |
3148 | Enclos_Type => Typ, | |
236fecbf | 3149 | With_Default_Init => True, |
1c612f29 | 3150 | Constructor_Ref => Expression (Comp))); |
236fecbf | 3151 | |
736f9bed PT |
3152 | elsif Box_Present (Comp) |
3153 | and then Needs_Simple_Initialization (Etype (Selector)) | |
3154 | then | |
3155 | Comp_Expr := | |
3156 | Make_Selected_Component (Loc, | |
3157 | Prefix => New_Copy_Tree (Target), | |
3158 | Selector_Name => New_Occurrence_Of (Selector, Loc)); | |
3159 | ||
c0ceba6c | 3160 | Initialize_Component |
fc84947c EB |
3161 | (N => N, |
3162 | Comp => Comp_Expr, | |
736f9bed PT |
3163 | Comp_Typ => Etype (Selector), |
3164 | Init_Expr => Get_Simple_Init_Val | |
3165 | (Typ => Etype (Selector), | |
3166 | N => Comp, | |
3167 | Size => | |
3168 | (if Known_Esize (Selector) | |
3169 | then Esize (Selector) | |
3170 | else Uint_0)), | |
3171 | Stmts => L); | |
3172 | ||
3b9fa2df | 3173 | -- Ada 2005 (AI-287): For each default-initialized component generate |
52739835 | 3174 | -- a call to the corresponding IP subprogram if available. |
65356e64 | 3175 | |
236fecbf | 3176 | elsif Box_Present (Comp) |
52739835 | 3177 | and then Has_Non_Null_Base_Init_Proc (Etype (Selector)) |
65356e64 | 3178 | then |
5277cab6 | 3179 | if Ekind (Selector) /= E_Discriminant then |
df3e68b1 | 3180 | Generate_Finalization_Actions; |
5277cab6 ES |
3181 | end if; |
3182 | ||
0ab80019 AC |
3183 | -- Ada 2005 (AI-287): If the component type has tasks then |
3184 | -- generate the activation chain and master entities (except | |
3185 | -- in case of an allocator because in that case these entities | |
3186 | -- are generated by Build_Task_Allocate_Block_With_Init_Stmts). | |
c45b6ae0 AC |
3187 | |
3188 | declare | |
91b1417d | 3189 | Ctype : constant Entity_Id := Etype (Selector); |
1c612f29 RD |
3190 | Inside_Allocator : Boolean := False; |
3191 | P : Node_Id := Parent (N); | |
c45b6ae0 AC |
3192 | |
3193 | begin | |
3194 | if Is_Task_Type (Ctype) or else Has_Task (Ctype) then | |
3195 | while Present (P) loop | |
3196 | if Nkind (P) = N_Allocator then | |
3197 | Inside_Allocator := True; | |
3198 | exit; | |
3199 | end if; | |
3200 | ||
3201 | P := Parent (P); | |
3202 | end loop; | |
3203 | ||
3204 | if not Inside_Init_Proc and not Inside_Allocator then | |
3205 | Build_Activation_Chain_Entity (N); | |
c45b6ae0 AC |
3206 | end if; |
3207 | end if; | |
3208 | end; | |
3209 | ||
65356e64 AC |
3210 | Append_List_To (L, |
3211 | Build_Initialization_Call (Loc, | |
1c612f29 RD |
3212 | Id_Ref => Make_Selected_Component (Loc, |
3213 | Prefix => New_Copy_Tree (Target), | |
3214 | Selector_Name => | |
3215 | New_Occurrence_Of (Selector, Loc)), | |
3216 | Typ => Etype (Selector), | |
3217 | Enclos_Type => Typ, | |
c45b6ae0 | 3218 | With_Default_Init => True)); |
65356e64 | 3219 | |
7b9d0d69 | 3220 | -- Prepare for component assignment |
fbf5a39b | 3221 | |
236fecbf | 3222 | elsif Ekind (Selector) /= E_Discriminant |
70482933 RK |
3223 | or else Nkind (N) = N_Extension_Aggregate |
3224 | then | |
7b9d0d69 | 3225 | -- All the discriminants have now been assigned |
3b9fa2df | 3226 | |
7b9d0d69 ES |
3227 | -- This is now a good moment to initialize and attach all the |
3228 | -- controllers. Their position may depend on the discriminants. | |
3229 | ||
5277cab6 | 3230 | if Ekind (Selector) /= E_Discriminant then |
df3e68b1 | 3231 | Generate_Finalization_Actions; |
7b9d0d69 ES |
3232 | end if; |
3233 | ||
38171f43 | 3234 | Comp_Type := Underlying_Type (Etype (Selector)); |
70482933 RK |
3235 | Comp_Expr := |
3236 | Make_Selected_Component (Loc, | |
3237 | Prefix => New_Copy_Tree (Target), | |
3238 | Selector_Name => New_Occurrence_Of (Selector, Loc)); | |
3239 | ||
4f061cf2 | 3240 | Expr_Q := Unqualify (Expression (Comp)); |
70482933 | 3241 | |
7b9d0d69 ES |
3242 | -- Now either create the assignment or generate the code for the |
3243 | -- inner aggregate top-down. | |
fbf5a39b | 3244 | |
70482933 | 3245 | if Is_Delayed_Aggregate (Expr_Q) then |
d8f7b976 ES |
3246 | |
3247 | -- We have the following case of aggregate nesting inside | |
3248 | -- an object declaration: | |
3249 | ||
3250 | -- type Arr_Typ is array (Integer range <>) of ...; | |
3b9fa2df | 3251 | |
d8f7b976 ES |
3252 | -- type Rec_Typ (...) is record |
3253 | -- Obj_Arr_Typ : Arr_Typ (A .. B); | |
3254 | -- end record; | |
3b9fa2df | 3255 | |
d8f7b976 ES |
3256 | -- Obj_Rec_Typ : Rec_Typ := (..., |
3257 | -- Obj_Arr_Typ => (X => (...), Y => (...))); | |
3258 | ||
3259 | -- The length of the ranges of the aggregate and Obj_Add_Typ | |
3260 | -- are equal (B - A = Y - X), but they do not coincide (X /= | |
3261 | -- A and B /= Y). This case requires array sliding which is | |
3262 | -- performed in the following manner: | |
3263 | ||
3264 | -- subtype Arr_Sub is Arr_Typ (X .. Y); | |
3265 | -- Temp : Arr_Sub; | |
3266 | -- Temp (X) := (...); | |
3267 | -- ... | |
3268 | -- Temp (Y) := (...); | |
3269 | -- Obj_Rec_Typ.Obj_Arr_Typ := Temp; | |
3270 | ||
5277cab6 | 3271 | if Ekind (Comp_Type) = E_Array_Subtype |
d8f7b976 ES |
3272 | and then Is_Int_Range_Bounds (Aggregate_Bounds (Expr_Q)) |
3273 | and then Is_Int_Range_Bounds (First_Index (Comp_Type)) | |
3274 | and then not | |
5277cab6 ES |
3275 | Compatible_Int_Bounds |
3276 | (Agg_Bounds => Aggregate_Bounds (Expr_Q), | |
3277 | Typ_Bounds => First_Index (Comp_Type)) | |
d8f7b976 | 3278 | then |
5277cab6 ES |
3279 | -- Create the array subtype with bounds equal to those of |
3280 | -- the corresponding aggregate. | |
d8f7b976 | 3281 | |
5277cab6 | 3282 | declare |
191fcb3a | 3283 | SubE : constant Entity_Id := Make_Temporary (Loc, 'T'); |
d8f7b976 ES |
3284 | |
3285 | SubD : constant Node_Id := | |
15f0f591 AC |
3286 | Make_Subtype_Declaration (Loc, |
3287 | Defining_Identifier => SubE, | |
3288 | Subtype_Indication => | |
3289 | Make_Subtype_Indication (Loc, | |
3290 | Subtype_Mark => | |
e4494292 | 3291 | New_Occurrence_Of (Etype (Comp_Type), Loc), |
15f0f591 AC |
3292 | Constraint => |
3293 | Make_Index_Or_Discriminant_Constraint | |
3294 | (Loc, | |
3295 | Constraints => New_List ( | |
3296 | New_Copy_Tree | |
3297 | (Aggregate_Bounds (Expr_Q)))))); | |
d8f7b976 ES |
3298 | |
3299 | -- Create a temporary array of the above subtype which | |
3300 | -- will be used to capture the aggregate assignments. | |
3301 | ||
faf387e1 | 3302 | TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N); |
d8f7b976 ES |
3303 | |
3304 | TmpD : constant Node_Id := | |
15f0f591 AC |
3305 | Make_Object_Declaration (Loc, |
3306 | Defining_Identifier => TmpE, | |
e4494292 | 3307 | Object_Definition => New_Occurrence_Of (SubE, Loc)); |
d8f7b976 ES |
3308 | |
3309 | begin | |
3310 | Set_No_Initialization (TmpD); | |
3311 | Append_To (L, SubD); | |
3312 | Append_To (L, TmpD); | |
3313 | ||
5277cab6 | 3314 | -- Expand aggregate into assignments to the temp array |
d8f7b976 ES |
3315 | |
3316 | Append_List_To (L, | |
3317 | Late_Expansion (Expr_Q, Comp_Type, | |
e4494292 | 3318 | New_Occurrence_Of (TmpE, Loc))); |
d8f7b976 ES |
3319 | |
3320 | -- Slide | |
3321 | ||
3322 | Append_To (L, | |
3323 | Make_Assignment_Statement (Loc, | |
3324 | Name => New_Copy_Tree (Comp_Expr), | |
e4494292 | 3325 | Expression => New_Occurrence_Of (TmpE, Loc))); |
d8f7b976 ES |
3326 | end; |
3327 | ||
3328 | -- Normal case (sliding not required) | |
3329 | ||
3330 | else | |
3331 | Append_List_To (L, | |
df3e68b1 | 3332 | Late_Expansion (Expr_Q, Comp_Type, Comp_Expr)); |
d8f7b976 | 3333 | end if; |
fbf5a39b | 3334 | |
5277cab6 ES |
3335 | -- Expr_Q is not delayed aggregate |
3336 | ||
70482933 | 3337 | else |
f2abc637 AC |
3338 | if Has_Discriminants (Typ) then |
3339 | Replace_Discriminants (Expr_Q); | |
b3f5eef0 AC |
3340 | |
3341 | -- If the component is an array type that depends on | |
3342 | -- discriminants, and the expression is a single Others | |
3343 | -- clause, create an explicit subtype for it because the | |
3344 | -- backend has troubles recovering the actual bounds. | |
3345 | ||
3346 | if Nkind (Expr_Q) = N_Aggregate | |
3347 | and then Is_Array_Type (Comp_Type) | |
3348 | and then Present (Component_Associations (Expr_Q)) | |
3349 | then | |
3350 | declare | |
3351 | Assoc : constant Node_Id := | |
45ec05e1 | 3352 | First (Component_Associations (Expr_Q)); |
b3f5eef0 AC |
3353 | Decl : Node_Id; |
3354 | ||
3355 | begin | |
f7f8e290 JM |
3356 | if Present (Assoc) |
3357 | and then | |
3358 | Nkind (First (Choices (Assoc))) = N_Others_Choice | |
b3f5eef0 AC |
3359 | then |
3360 | Decl := | |
3361 | Build_Actual_Subtype_Of_Component | |
3362 | (Comp_Type, Comp_Expr); | |
3363 | ||
3364 | -- If the component type does not in fact depend on | |
3365 | -- discriminants, the subtype declaration is empty. | |
3366 | ||
3367 | if Present (Decl) then | |
3368 | Append_To (L, Decl); | |
3369 | Set_Etype (Comp_Expr, Defining_Entity (Decl)); | |
3370 | end if; | |
3371 | end if; | |
3372 | end; | |
3373 | end if; | |
f2abc637 AC |
3374 | end if; |
3375 | ||
c63a2ad6 | 3376 | if Modify_Tree_For_C |
a1e1820b AC |
3377 | and then Nkind (Expr_Q) = N_Aggregate |
3378 | and then Is_Array_Type (Etype (Expr_Q)) | |
3379 | and then Present (First_Index (Etype (Expr_Q))) | |
3380 | then | |
3381 | declare | |
0c5c46a9 | 3382 | Expr_Q_Type : constant Entity_Id := Etype (Expr_Q); |
a1e1820b AC |
3383 | begin |
3384 | Append_List_To (L, | |
3385 | Build_Array_Aggr_Code | |
3386 | (N => Expr_Q, | |
3387 | Ctype => Component_Type (Expr_Q_Type), | |
3388 | Index => First_Index (Expr_Q_Type), | |
3389 | Into => Comp_Expr, | |
937e9676 AC |
3390 | Scalar_Comp => |
3391 | Is_Scalar_Type (Component_Type (Expr_Q_Type)))); | |
a1e1820b AC |
3392 | end; |
3393 | ||
3394 | else | |
fc84947c EB |
3395 | Initialize_Component |
3396 | (N => N, | |
3397 | Comp => Comp_Expr, | |
3398 | Comp_Typ => Etype (Selector), | |
3399 | Init_Expr => Expr_Q, | |
3400 | Stmts => L); | |
70482933 RK |
3401 | end if; |
3402 | end if; | |
fbf5a39b | 3403 | |
37368818 | 3404 | -- comment would be good here ??? |
fbf5a39b AC |
3405 | |
3406 | elsif Ekind (Selector) = E_Discriminant | |
3407 | and then Nkind (N) /= N_Extension_Aggregate | |
3408 | and then Nkind (Parent (N)) = N_Component_Association | |
3409 | and then Is_Constrained (Typ) | |
3410 | then | |
3411 | -- We must check that the discriminant value imposed by the | |
3412 | -- context is the same as the value given in the subaggregate, | |
3413 | -- because after the expansion into assignments there is no | |
3414 | -- record on which to perform a regular discriminant check. | |
3415 | ||
3416 | declare | |
3417 | D_Val : Elmt_Id; | |
3418 | Disc : Entity_Id; | |
3419 | ||
3420 | begin | |
3421 | D_Val := First_Elmt (Discriminant_Constraint (Typ)); | |
3422 | Disc := First_Discriminant (Typ); | |
fbf5a39b AC |
3423 | while Chars (Disc) /= Chars (Selector) loop |
3424 | Next_Discriminant (Disc); | |
3425 | Next_Elmt (D_Val); | |
3426 | end loop; | |
3427 | ||
3428 | pragma Assert (Present (D_Val)); | |
3429 | ||
0f95b178 JM |
3430 | -- This check cannot performed for components that are |
3431 | -- constrained by a current instance, because this is not a | |
3432 | -- value that can be compared with the actual constraint. | |
3433 | ||
3434 | if Nkind (Node (D_Val)) /= N_Attribute_Reference | |
3435 | or else not Is_Entity_Name (Prefix (Node (D_Val))) | |
3436 | or else not Is_Type (Entity (Prefix (Node (D_Val)))) | |
3437 | then | |
3438 | Append_To (L, | |
3439 | Make_Raise_Constraint_Error (Loc, | |
3440 | Condition => | |
3441 | Make_Op_Ne (Loc, | |
37368818 | 3442 | Left_Opnd => New_Copy_Tree (Node (D_Val)), |
0f95b178 | 3443 | Right_Opnd => Expression (Comp)), |
37368818 | 3444 | Reason => CE_Discriminant_Check_Failed)); |
0f95b178 JM |
3445 | |
3446 | else | |
3b9fa2df ES |
3447 | -- Find self-reference in previous discriminant assignment, |
3448 | -- and replace with proper expression. | |
0f95b178 JM |
3449 | |
3450 | declare | |
3451 | Ass : Node_Id; | |
3452 | ||
3453 | begin | |
3454 | Ass := First (L); | |
3455 | while Present (Ass) loop | |
3456 | if Nkind (Ass) = N_Assignment_Statement | |
3457 | and then Nkind (Name (Ass)) = N_Selected_Component | |
3458 | and then Chars (Selector_Name (Name (Ass))) = | |
36a66365 | 3459 | Chars (Disc) |
0f95b178 JM |
3460 | then |
3461 | Set_Expression | |
3462 | (Ass, New_Copy_Tree (Expression (Comp))); | |
3463 | exit; | |
3464 | end if; | |
3465 | Next (Ass); | |
3466 | end loop; | |
3467 | end; | |
3468 | end if; | |
fbf5a39b | 3469 | end; |
70482933 RK |
3470 | end if; |
3471 | ||
f7937111 GD |
3472 | -- If the component association was specified with a box and the |
3473 | -- component type has a Default_Initial_Condition, then generate | |
3474 | -- a call to the DIC procedure. | |
3475 | ||
3476 | if Has_DIC (Etype (Selector)) | |
3477 | and then Was_Default_Init_Box_Association (Comp) | |
3478 | and then Present (DIC_Procedure (Etype (Selector))) | |
3479 | then | |
3480 | Append_To (L, | |
3481 | Build_DIC_Call (Loc, | |
3482 | Make_Selected_Component (Loc, | |
3483 | Prefix => New_Copy_Tree (Target), | |
3484 | Selector_Name => New_Occurrence_Of (Selector, Loc)), | |
3485 | Etype (Selector))); | |
3486 | end if; | |
3487 | ||
70482933 RK |
3488 | Next (Comp); |
3489 | end loop; | |
3490 | ||
28541488 JM |
3491 | -- For CPP types we generated a call to the C++ default constructor |
3492 | -- before the components have been initialized to ensure the proper | |
3493 | -- initialization of the _Tag component (see above). | |
3494 | ||
c0ceba6c | 3495 | if Is_CPP_Class (Typ) then |
28541488 JM |
3496 | null; |
3497 | ||
c0ceba6c EB |
3498 | -- If the type is tagged, the tag needs to be initialized (unless we |
3499 | -- are in VM-mode where tags are implicit). It is done late in the | |
3500 | -- initialization process because in some cases, we call the init | |
3501 | -- proc of an ancestor which will not leave out the right tag. | |
3502 | ||
1f110335 | 3503 | elsif Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then |
70482933 | 3504 | Instr := |
af10c962 EB |
3505 | Make_Tag_Assignment_From_Type |
3506 | (Loc, New_Copy_Tree (Target), Base_Type (Typ)); | |
70482933 RK |
3507 | |
3508 | Append_To (L, Instr); | |
c5ee5ad2 | 3509 | |
bdc193ba | 3510 | -- Ada 2005 (AI-251): If the tagged type has been derived from an |
c5ee5ad2 BD |
3511 | -- abstract interfaces we must also initialize the tags of the |
3512 | -- secondary dispatch tables. | |
3513 | ||
ce2b6ba5 | 3514 | if Has_Interfaces (Base_Type (Typ)) then |
c5ee5ad2 | 3515 | Init_Secondary_Tags |
ed323421 AC |
3516 | (Typ => Base_Type (Typ), |
3517 | Target => Target, | |
3518 | Stmts_List => L, | |
fe683ef6 | 3519 | Init_Tags_List => L); |
c5ee5ad2 | 3520 | end if; |
70482933 RK |
3521 | end if; |
3522 | ||
7b9d0d69 ES |
3523 | -- If the controllers have not been initialized yet (by lack of non- |
3524 | -- discriminant components), let's do it now. | |
70482933 | 3525 | |
df3e68b1 | 3526 | Generate_Finalization_Actions; |
70482933 | 3527 | |
7b9d0d69 | 3528 | return L; |
70482933 RK |
3529 | end Build_Record_Aggr_Code; |
3530 | ||
3531 | ------------------------------- | |
3532 | -- Convert_Aggr_In_Allocator -- | |
3533 | ------------------------------- | |
3534 | ||
fa57ac97 ES |
3535 | procedure Convert_Aggr_In_Allocator |
3536 | (Alloc : Node_Id; | |
3537 | Decl : Node_Id; | |
3538 | Aggr : Node_Id) | |
3539 | is | |
70482933 RK |
3540 | Loc : constant Source_Ptr := Sloc (Aggr); |
3541 | Typ : constant Entity_Id := Etype (Aggr); | |
3542 | Temp : constant Entity_Id := Defining_Identifier (Decl); | |
fbf5a39b AC |
3543 | |
3544 | Occ : constant Node_Id := | |
15f0f591 | 3545 | Unchecked_Convert_To (Typ, |
e4494292 | 3546 | Make_Explicit_Dereference (Loc, New_Occurrence_Of (Temp, Loc))); |
70482933 | 3547 | |
70482933 | 3548 | begin |
6f639c98 ES |
3549 | if Is_Array_Type (Typ) then |
3550 | Convert_Array_Aggr_In_Allocator (Decl, Aggr, Occ); | |
3551 | ||
3552 | elsif Has_Default_Init_Comps (Aggr) then | |
c45b6ae0 AC |
3553 | declare |
3554 | L : constant List_Id := New_List; | |
3555 | Init_Stmts : List_Id; | |
3556 | ||
3557 | begin | |
df3e68b1 | 3558 | Init_Stmts := Late_Expansion (Aggr, Typ, Occ); |
c45b6ae0 | 3559 | |
0f95b178 JM |
3560 | if Has_Task (Typ) then |
3561 | Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts); | |
fa57ac97 | 3562 | Insert_Actions (Alloc, L); |
0f95b178 | 3563 | else |
fa57ac97 | 3564 | Insert_Actions (Alloc, Init_Stmts); |
0f95b178 | 3565 | end if; |
c45b6ae0 AC |
3566 | end; |
3567 | ||
3568 | else | |
df3e68b1 | 3569 | Insert_Actions (Alloc, Late_Expansion (Aggr, Typ, Occ)); |
c45b6ae0 | 3570 | end if; |
70482933 RK |
3571 | end Convert_Aggr_In_Allocator; |
3572 | ||
3573 | -------------------------------- | |
3574 | -- Convert_Aggr_In_Assignment -- | |
3575 | -------------------------------- | |
3576 | ||
3577 | procedure Convert_Aggr_In_Assignment (N : Node_Id) is | |
4f061cf2 | 3578 | Aggr : constant Node_Id := Unqualify (Expression (N)); |
3b9fa2df ES |
3579 | Typ : constant Entity_Id := Etype (Aggr); |
3580 | Occ : constant Node_Id := New_Copy_Tree (Name (N)); | |
70482933 RK |
3581 | |
3582 | begin | |
df3e68b1 | 3583 | Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ)); |
70482933 RK |
3584 | end Convert_Aggr_In_Assignment; |
3585 | ||
3586 | --------------------------------- | |
3587 | -- Convert_Aggr_In_Object_Decl -- | |
3588 | --------------------------------- | |
3589 | ||
3590 | procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is | |
3591 | Obj : constant Entity_Id := Defining_Identifier (N); | |
4f061cf2 | 3592 | Aggr : constant Node_Id := Unqualify (Expression (N)); |
70482933 RK |
3593 | Loc : constant Source_Ptr := Sloc (Aggr); |
3594 | Typ : constant Entity_Id := Etype (Aggr); | |
3595 | Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc); | |
3596 | ||
fff7a6d9 AC |
3597 | Has_Transient_Scope : Boolean := False; |
3598 | ||
fbf5a39b AC |
3599 | function Discriminants_Ok return Boolean; |
3600 | -- If the object type is constrained, the discriminants in the | |
3601 | -- aggregate must be checked against the discriminants of the subtype. | |
3602 | -- This cannot be done using Apply_Discriminant_Checks because after | |
3603 | -- expansion there is no aggregate left to check. | |
3604 | ||
3605 | ---------------------- | |
3606 | -- Discriminants_Ok -- | |
3607 | ---------------------- | |
3608 | ||
3609 | function Discriminants_Ok return Boolean is | |
3610 | Cond : Node_Id := Empty; | |
3611 | Check : Node_Id; | |
3612 | D : Entity_Id; | |
3613 | Disc1 : Elmt_Id; | |
3614 | Disc2 : Elmt_Id; | |
3615 | Val1 : Node_Id; | |
3616 | Val2 : Node_Id; | |
3617 | ||
3618 | begin | |
3619 | D := First_Discriminant (Typ); | |
3620 | Disc1 := First_Elmt (Discriminant_Constraint (Typ)); | |
3621 | Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj))); | |
fbf5a39b AC |
3622 | while Present (Disc1) and then Present (Disc2) loop |
3623 | Val1 := Node (Disc1); | |
3624 | Val2 := Node (Disc2); | |
3625 | ||
3626 | if not Is_OK_Static_Expression (Val1) | |
3627 | or else not Is_OK_Static_Expression (Val2) | |
3628 | then | |
3629 | Check := Make_Op_Ne (Loc, | |
3630 | Left_Opnd => Duplicate_Subexpr (Val1), | |
3631 | Right_Opnd => Duplicate_Subexpr (Val2)); | |
3632 | ||
3633 | if No (Cond) then | |
3634 | Cond := Check; | |
3635 | ||
3636 | else | |
3637 | Cond := Make_Or_Else (Loc, | |
3638 | Left_Opnd => Cond, | |
3639 | Right_Opnd => Check); | |
3640 | end if; | |
3641 | ||
3642 | elsif Expr_Value (Val1) /= Expr_Value (Val2) then | |
3643 | Apply_Compile_Time_Constraint_Error (Aggr, | |
324ac540 | 3644 | Msg => "incorrect value for discriminant&??", |
fbf5a39b AC |
3645 | Reason => CE_Discriminant_Check_Failed, |
3646 | Ent => D); | |
3647 | return False; | |
3648 | end if; | |
3649 | ||
3650 | Next_Discriminant (D); | |
3651 | Next_Elmt (Disc1); | |
3652 | Next_Elmt (Disc2); | |
3653 | end loop; | |
3654 | ||
d940c627 | 3655 | -- If any discriminant constraint is nonstatic, emit a check |
fbf5a39b AC |
3656 | |
3657 | if Present (Cond) then | |
3658 | Insert_Action (N, | |
3659 | Make_Raise_Constraint_Error (Loc, | |
3660 | Condition => Cond, | |
ef1c0511 | 3661 | Reason => CE_Discriminant_Check_Failed)); |
fbf5a39b AC |
3662 | end if; |
3663 | ||
3664 | return True; | |
3665 | end Discriminants_Ok; | |
3666 | ||
3667 | -- Start of processing for Convert_Aggr_In_Object_Decl | |
3668 | ||
70482933 RK |
3669 | begin |
3670 | Set_Assignment_OK (Occ); | |
3671 | ||
fbf5a39b AC |
3672 | if Has_Discriminants (Typ) |
3673 | and then Typ /= Etype (Obj) | |
3674 | and then Is_Constrained (Etype (Obj)) | |
3675 | and then not Discriminants_Ok | |
3676 | then | |
3677 | return; | |
3678 | end if; | |
3679 | ||
0f95b178 JM |
3680 | -- If the context is an extended return statement, it has its own |
3681 | -- finalization machinery (i.e. works like a transient scope) and | |
3682 | -- we do not want to create an additional one, because objects on | |
3683 | -- the finalization list of the return must be moved to the caller's | |
3684 | -- finalization list to complete the return. | |
3685 | ||
fff7a6d9 | 3686 | -- Similarly if the aggregate is limited, it is built in place, and the |
3b9fa2df ES |
3687 | -- controlled components are not assigned to intermediate temporaries |
3688 | -- so there is no need for a transient scope in this case either. | |
3689 | ||
0f95b178 JM |
3690 | if Requires_Transient_Scope (Typ) |
3691 | and then Ekind (Current_Scope) /= E_Return_Statement | |
3b9fa2df | 3692 | and then not Is_Limited_Type (Typ) |
0f95b178 | 3693 | then |
6560f851 | 3694 | Establish_Transient_Scope (Aggr, Manage_Sec_Stack => False); |
fff7a6d9 | 3695 | Has_Transient_Scope := True; |
6f5c2c4b | 3696 | end if; |
02217452 | 3697 | |
6f5c2c4b | 3698 | declare |
fff7a6d9 AC |
3699 | Stmts : constant List_Id := Late_Expansion (Aggr, Typ, Occ); |
3700 | Stmt : Node_Id; | |
3701 | Param : Node_Id; | |
3702 | ||
6f5c2c4b | 3703 | begin |
fff7a6d9 AC |
3704 | -- If Obj is already frozen or if N is wrapped in a transient scope, |
3705 | -- Stmts do not need to be saved in Initialization_Statements since | |
3706 | -- there is no freezing issue. | |
3707 | ||
3708 | if Is_Frozen (Obj) or else Has_Transient_Scope then | |
3709 | Insert_Actions_After (N, Stmts); | |
3710 | else | |
3711 | Stmt := Make_Compound_Statement (Sloc (N), Actions => Stmts); | |
3712 | Insert_Action_After (N, Stmt); | |
3713 | ||
3714 | -- Insert_Action_After may freeze Obj in which case we should | |
3715 | -- remove the compound statement just created and simply insert | |
3716 | -- Stmts after N. | |
3717 | ||
3718 | if Is_Frozen (Obj) then | |
3719 | Remove (Stmt); | |
3720 | Insert_Actions_After (N, Stmts); | |
3721 | else | |
3722 | Set_Initialization_Statements (Obj, Stmt); | |
3723 | end if; | |
3724 | end if; | |
3725 | ||
3726 | -- If Typ has controlled components and a call to a Slice_Assign | |
3727 | -- procedure is part of the initialization statements, then we | |
3728 | -- need to initialize the array component since Slice_Assign will | |
3729 | -- need to adjust it. | |
3730 | ||
3731 | if Has_Controlled_Component (Typ) then | |
3732 | Stmt := First (Stmts); | |
3733 | ||
3734 | while Present (Stmt) loop | |
3735 | if Nkind (Stmt) = N_Procedure_Call_Statement | |
f82fb002 | 3736 | and then Is_TSS (Entity (Name (Stmt)), TSS_Slice_Assign) |
fff7a6d9 AC |
3737 | then |
3738 | Param := First (Parameter_Associations (Stmt)); | |
3739 | Insert_Actions | |
3740 | (Stmt, | |
3741 | Build_Initialization_Call | |
3742 | (Sloc (N), New_Copy_Tree (Param), Etype (Param))); | |
3743 | end if; | |
3744 | ||
3745 | Next (Stmt); | |
3746 | end loop; | |
3747 | end if; | |
6f5c2c4b | 3748 | end; |
6560f851 | 3749 | |
70482933 | 3750 | Set_No_Initialization (N); |
a671959b ES |
3751 | |
3752 | -- After expansion the expression can be removed from the declaration | |
3753 | -- except if the object is class-wide, in which case the aggregate | |
3754 | -- provides the actual type. | |
3755 | ||
3756 | if not Is_Class_Wide_Type (Etype (Obj)) then | |
3757 | Set_Expression (N, Empty); | |
3758 | end if; | |
3759 | ||
07fc65c4 | 3760 | Initialize_Discriminants (N, Typ); |
70482933 RK |
3761 | end Convert_Aggr_In_Object_Decl; |
3762 | ||
6f639c98 | 3763 | ------------------------------------- |
3b9fa2df | 3764 | -- Convert_Array_Aggr_In_Allocator -- |
6f639c98 ES |
3765 | ------------------------------------- |
3766 | ||
3767 | procedure Convert_Array_Aggr_In_Allocator | |
3768 | (Decl : Node_Id; | |
3769 | Aggr : Node_Id; | |
3770 | Target : Node_Id) | |
3771 | is | |
6f639c98 ES |
3772 | Typ : constant Entity_Id := Etype (Aggr); |
3773 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
2fedcc18 EB |
3774 | Aggr_Code : List_Id; |
3775 | New_Aggr : Node_Id; | |
6f639c98 ES |
3776 | |
3777 | begin | |
2fedcc18 EB |
3778 | -- The target is an explicit dereference of the allocated object |
3779 | ||
3780 | -- If the assignment can be done directly by the back end, then | |
3781 | -- reset Set_Expansion_Delayed and do not expand further. | |
3782 | ||
3783 | if not CodePeer_Mode | |
3784 | and then not Modify_Tree_For_C | |
3785 | and then Aggr_Assignment_OK_For_Backend (Aggr) | |
3786 | then | |
3787 | New_Aggr := New_Copy_Tree (Aggr); | |
3788 | Set_Expansion_Delayed (New_Aggr, False); | |
3789 | ||
742084ad MP |
3790 | -- In the case of Target's type using the Designated_Storage_Model |
3791 | -- aspect with a Copy_To procedure, insert a temporary and have the | |
3792 | -- back end handle the assignment to it. Copy the result to the | |
3793 | -- original target. | |
3794 | ||
3795 | if Has_Designated_Storage_Model_Aspect | |
3796 | (Etype (Prefix (Expression (Target)))) | |
3797 | and then Present (Storage_Model_Copy_To | |
3798 | (Storage_Model_Object | |
3799 | (Etype (Prefix (Expression (Target)))))) | |
3800 | then | |
ca4bff3a EB |
3801 | Aggr_Code := |
3802 | Build_Assignment_With_Temporary (Target, Typ, New_Aggr); | |
3803 | ||
742084ad MP |
3804 | else |
3805 | Aggr_Code := | |
3806 | New_List ( | |
3807 | Make_OK_Assignment_Statement (Sloc (New_Aggr), | |
3808 | Name => Target, | |
3809 | Expression => New_Aggr)); | |
3810 | end if; | |
6f639c98 | 3811 | |
2fedcc18 EB |
3812 | -- Or else, generate component assignments to it, as for an aggregate |
3813 | -- that appears on the right-hand side of an assignment statement. | |
2fedcc18 EB |
3814 | else |
3815 | Aggr_Code := | |
3816 | Build_Array_Aggr_Code (Aggr, | |
3817 | Ctype => Ctyp, | |
3818 | Index => First_Index (Typ), | |
3819 | Into => Target, | |
3820 | Scalar_Comp => Is_Scalar_Type (Ctyp)); | |
3821 | end if; | |
6f639c98 ES |
3822 | |
3823 | Insert_Actions_After (Decl, Aggr_Code); | |
3824 | end Convert_Array_Aggr_In_Allocator; | |
3825 | ||
4ff5aa0c AC |
3826 | ------------------------ |
3827 | -- In_Place_Assign_OK -- | |
3828 | ------------------------ | |
3829 | ||
a80b1eb7 EB |
3830 | function In_Place_Assign_OK |
3831 | (N : Node_Id; | |
3832 | Target_Object : Entity_Id := Empty) return Boolean | |
3833 | is | |
4ff5aa0c AC |
3834 | Is_Array : constant Boolean := Is_Array_Type (Etype (N)); |
3835 | ||
a80b1eb7 | 3836 | Aggr_In : Node_Id; |
7c4f3267 | 3837 | Aggr_Bounds : Range_Nodes; |
a80b1eb7 | 3838 | Obj_In : Node_Id; |
7c4f3267 | 3839 | Obj_Bounds : Range_Nodes; |
a80b1eb7 EB |
3840 | Parent_Kind : Node_Kind; |
3841 | Parent_Node : Node_Id; | |
4ff5aa0c AC |
3842 | |
3843 | function Safe_Aggregate (Aggr : Node_Id) return Boolean; | |
3844 | -- Check recursively that each component of a (sub)aggregate does not | |
3845 | -- depend on the variable being assigned to. | |
3846 | ||
3847 | function Safe_Component (Expr : Node_Id) return Boolean; | |
a80b1eb7 EB |
3848 | -- Verify that an expression cannot depend on the target being assigned |
3849 | -- to. Return true for compile-time known values, stand-alone objects, | |
3850 | -- parameters passed by copy, calls to functions that return by copy, | |
3851 | -- selected components thereof only if the aggregate's type is an array, | |
3852 | -- indexed components and slices thereof only if the aggregate's type is | |
3853 | -- a record, and simple expressions involving only these as operands. | |
3854 | -- This is OK whatever the target because, for a component to overlap | |
3855 | -- with the target, it must be either a direct reference to a component | |
3856 | -- of the target, in which case there must be a matching selection or | |
3857 | -- indexation or slicing, or an indirect reference to such a component, | |
3858 | -- which is excluded by the above condition. Additionally, if the target | |
3859 | -- is statically known, return true for arbitrarily nested selections, | |
3860 | -- indexations or slicings, provided that their ultimate prefix is not | |
3861 | -- the target itself. | |
4ff5aa0c AC |
3862 | |
3863 | -------------------- | |
3864 | -- Safe_Aggregate -- | |
3865 | -------------------- | |
3866 | ||
3867 | function Safe_Aggregate (Aggr : Node_Id) return Boolean is | |
3868 | Expr : Node_Id; | |
3869 | ||
3870 | begin | |
3871 | if Nkind (Parent (Aggr)) = N_Iterated_Component_Association then | |
3872 | return False; | |
3873 | end if; | |
3874 | ||
3875 | if Present (Expressions (Aggr)) then | |
3876 | Expr := First (Expressions (Aggr)); | |
3877 | while Present (Expr) loop | |
3878 | if Nkind (Expr) = N_Aggregate then | |
3879 | if not Safe_Aggregate (Expr) then | |
3880 | return False; | |
3881 | end if; | |
3882 | ||
3883 | elsif not Safe_Component (Expr) then | |
3884 | return False; | |
3885 | end if; | |
3886 | ||
3887 | Next (Expr); | |
3888 | end loop; | |
3889 | end if; | |
3890 | ||
3891 | if Present (Component_Associations (Aggr)) then | |
3892 | Expr := First (Component_Associations (Aggr)); | |
3893 | while Present (Expr) loop | |
3894 | if Nkind (Expression (Expr)) = N_Aggregate then | |
3895 | if not Safe_Aggregate (Expression (Expr)) then | |
3896 | return False; | |
3897 | end if; | |
3898 | ||
bc1146e5 HK |
3899 | -- If association has a box, no way to determine yet whether |
3900 | -- default can be assigned in place. | |
4ff5aa0c AC |
3901 | |
3902 | elsif Box_Present (Expr) then | |
3903 | return False; | |
3904 | ||
3905 | elsif not Safe_Component (Expression (Expr)) then | |
3906 | return False; | |
3907 | end if; | |
3908 | ||
3909 | Next (Expr); | |
3910 | end loop; | |
3911 | end if; | |
3912 | ||
3913 | return True; | |
3914 | end Safe_Aggregate; | |
3915 | ||
3916 | -------------------- | |
3917 | -- Safe_Component -- | |
3918 | -------------------- | |
3919 | ||
3920 | function Safe_Component (Expr : Node_Id) return Boolean is | |
3921 | Comp : Node_Id := Expr; | |
3922 | ||
a80b1eb7 EB |
3923 | function Check_Component (C : Node_Id; T_OK : Boolean) return Boolean; |
3924 | -- Do the recursive traversal, after copy. If T_OK is True, return | |
3925 | -- True for a stand-alone object only if the target is statically | |
3926 | -- known and distinct from the object. At the top level, we start | |
3927 | -- with T_OK set to False and set it to True at a deeper level only | |
3928 | -- if we cannot disambiguate the component here without statically | |
3929 | -- knowing the target. Note that this is not optimal, we should do | |
3930 | -- something along the lines of Denotes_Same_Prefix for that. | |
4ff5aa0c AC |
3931 | |
3932 | --------------------- | |
3933 | -- Check_Component -- | |
3934 | --------------------- | |
3935 | ||
a80b1eb7 EB |
3936 | function Check_Component (C : Node_Id; T_OK : Boolean) return Boolean |
3937 | is | |
3938 | ||
3939 | function SDO (E : Entity_Id) return Uint; | |
3940 | -- Return the Scope Depth Of the enclosing dynamic scope of E | |
3941 | ||
3942 | --------- | |
3943 | -- SDO -- | |
3944 | --------- | |
3945 | ||
3946 | function SDO (E : Entity_Id) return Uint is | |
3947 | begin | |
3948 | return Scope_Depth (Enclosing_Dynamic_Scope (E)); | |
3949 | end SDO; | |
3950 | ||
3951 | -- Start of processing for Check_Component | |
3952 | ||
4ff5aa0c | 3953 | begin |
a80b1eb7 | 3954 | if Is_Overloaded (C) then |
4ff5aa0c | 3955 | return False; |
a80b1eb7 EB |
3956 | |
3957 | elsif Compile_Time_Known_Value (C) then | |
3958 | return True; | |
4ff5aa0c AC |
3959 | end if; |
3960 | ||
a80b1eb7 EB |
3961 | case Nkind (C) is |
3962 | when N_Attribute_Reference => | |
3963 | return Check_Component (Prefix (C), T_OK); | |
3964 | ||
3965 | when N_Function_Call => | |
3966 | if Nkind (Name (C)) = N_Explicit_Dereference then | |
3967 | return not Returns_By_Ref (Etype (Name (C))); | |
3968 | else | |
3969 | return not Returns_By_Ref (Entity (Name (C))); | |
3970 | end if; | |
3971 | ||
3972 | when N_Indexed_Component | N_Slice => | |
3973 | -- In a target record, these operations cannot determine | |
3974 | -- alone a component so we can recurse whatever the target. | |
3975 | return Check_Component (Prefix (C), T_OK or else Is_Array); | |
3976 | ||
3977 | when N_Selected_Component => | |
3978 | -- In a target array, this operation cannot determine alone | |
3979 | -- a component so we can recurse whatever the target. | |
3980 | return | |
3981 | Check_Component (Prefix (C), T_OK or else not Is_Array); | |
3982 | ||
3983 | when N_Type_Conversion | N_Unchecked_Type_Conversion => | |
3984 | return Check_Component (Expression (C), T_OK); | |
4ff5aa0c | 3985 | |
a80b1eb7 EB |
3986 | when N_Binary_Op => |
3987 | return Check_Component (Left_Opnd (C), T_OK) | |
3988 | and then Check_Component (Right_Opnd (C), T_OK); | |
4ff5aa0c | 3989 | |
a80b1eb7 EB |
3990 | when N_Unary_Op => |
3991 | return Check_Component (Right_Opnd (C), T_OK); | |
4ff5aa0c | 3992 | |
a80b1eb7 EB |
3993 | when others => |
3994 | if Is_Entity_Name (C) and then Is_Object (Entity (C)) then | |
3995 | -- Case of a formal parameter component. It's either | |
3996 | -- trivial if passed by copy or very annoying if not, | |
3997 | -- because in the latter case it's almost equivalent | |
3998 | -- to a dereference, so the path-based disambiguation | |
3999 | -- logic is totally off and we always need the target. | |
4ff5aa0c | 4000 | |
a80b1eb7 EB |
4001 | if Is_Formal (Entity (C)) then |
4002 | ||
4003 | -- If it is passed by copy, then this is safe | |
4004 | ||
4005 | if Mechanism (Entity (C)) = By_Copy then | |
4006 | return True; | |
4007 | ||
4008 | -- Otherwise, this is safe if the target is present | |
4009 | -- and is at least as deeply nested as the component. | |
4010 | ||
4011 | else | |
4012 | return Present (Target_Object) | |
4013 | and then not Is_Formal (Target_Object) | |
4014 | and then SDO (Target_Object) >= SDO (Entity (C)); | |
4015 | end if; | |
4016 | ||
4017 | -- For a renamed object, recurse | |
4018 | ||
4019 | elsif Present (Renamed_Object (Entity (C))) then | |
4020 | return | |
4021 | Check_Component (Renamed_Object (Entity (C)), T_OK); | |
4022 | ||
4023 | -- If this is safe whatever the target, we are done | |
4024 | ||
4025 | elsif not T_OK then | |
4026 | return True; | |
4027 | ||
4028 | -- If there is no target or the component is the target, | |
4029 | -- this is not safe. | |
4030 | ||
4031 | elsif No (Target_Object) | |
4032 | or else Entity (C) = Target_Object | |
4033 | then | |
4034 | return False; | |
4035 | ||
4036 | -- Case of a formal parameter target. This is safe if it | |
4037 | -- is at most as deeply nested as the component. | |
4038 | ||
4039 | elsif Is_Formal (Target_Object) then | |
4040 | return SDO (Target_Object) <= SDO (Entity (C)); | |
4041 | ||
4042 | -- For distinct stand-alone objects, this is safe | |
4043 | ||
4044 | else | |
4045 | return True; | |
4046 | end if; | |
4ff5aa0c | 4047 | |
a80b1eb7 | 4048 | -- For anything else than an object, this is not safe |
4ff5aa0c | 4049 | |
a80b1eb7 EB |
4050 | else |
4051 | return False; | |
4052 | end if; | |
4053 | end case; | |
4ff5aa0c AC |
4054 | end Check_Component; |
4055 | ||
4056 | -- Start of processing for Safe_Component | |
4057 | ||
4058 | begin | |
4059 | -- If the component appears in an association that may correspond | |
4060 | -- to more than one element, it is not analyzed before expansion | |
4061 | -- into assignments, to avoid side effects. We analyze, but do not | |
4062 | -- resolve the copy, to obtain sufficient entity information for | |
4063 | -- the checks that follow. If component is overloaded we assume | |
4064 | -- an unsafe function call. | |
4065 | ||
4066 | if not Analyzed (Comp) then | |
4067 | if Is_Overloaded (Expr) then | |
4068 | return False; | |
4ff5aa0c AC |
4069 | |
4070 | elsif Nkind (Expr) = N_Allocator then | |
4071 | ||
4072 | -- For now, too complex to analyze | |
4073 | ||
4074 | return False; | |
4075 | ||
bc1146e5 HK |
4076 | elsif Nkind (Parent (Expr)) = N_Iterated_Component_Association then |
4077 | ||
4078 | -- Ditto for iterated component associations, which in general | |
4079 | -- require an enclosing loop and involve nonstatic expressions. | |
4ff5aa0c AC |
4080 | |
4081 | return False; | |
4082 | end if; | |
4083 | ||
4084 | Comp := New_Copy_Tree (Expr); | |
4085 | Set_Parent (Comp, Parent (Expr)); | |
4086 | Analyze (Comp); | |
4087 | end if; | |
4088 | ||
4089 | if Nkind (Comp) = N_Aggregate then | |
4090 | return Safe_Aggregate (Comp); | |
4091 | else | |
a80b1eb7 | 4092 | return Check_Component (Comp, False); |
4ff5aa0c AC |
4093 | end if; |
4094 | end Safe_Component; | |
4095 | ||
4096 | -- Start of processing for In_Place_Assign_OK | |
4097 | ||
4098 | begin | |
e67df677 | 4099 | -- By-copy semantic cannot be guaranteed for controlled objects |
4ff5aa0c | 4100 | |
e67df677 | 4101 | if Needs_Finalization (Etype (N)) then |
4ff5aa0c | 4102 | return False; |
a80b1eb7 | 4103 | end if; |
4ff5aa0c | 4104 | |
a80b1eb7 EB |
4105 | Parent_Node := Parent (N); |
4106 | Parent_Kind := Nkind (Parent_Node); | |
4ff5aa0c | 4107 | |
a80b1eb7 EB |
4108 | if Parent_Kind = N_Qualified_Expression then |
4109 | Parent_Node := Parent (Parent_Node); | |
4110 | Parent_Kind := Nkind (Parent_Node); | |
4111 | end if; | |
4ff5aa0c | 4112 | |
a80b1eb7 EB |
4113 | -- On assignment, sliding can take place, so we cannot do the |
4114 | -- assignment in place unless the bounds of the aggregate are | |
4115 | -- statically equal to those of the target. | |
4ff5aa0c | 4116 | |
a80b1eb7 EB |
4117 | -- If the aggregate is given by an others choice, the bounds are |
4118 | -- derived from the left-hand side, and the assignment is safe if | |
4119 | -- the expression is. | |
4ff5aa0c | 4120 | |
a80b1eb7 EB |
4121 | if Is_Array |
4122 | and then Present (Component_Associations (N)) | |
4123 | and then not Is_Others_Aggregate (N) | |
4124 | then | |
4ff5aa0c AC |
4125 | Aggr_In := First_Index (Etype (N)); |
4126 | ||
d0e9248d EB |
4127 | -- Context is an assignment |
4128 | ||
a80b1eb7 EB |
4129 | if Parent_Kind = N_Assignment_Statement then |
4130 | Obj_In := First_Index (Etype (Name (Parent_Node))); | |
4ff5aa0c | 4131 | |
d0e9248d EB |
4132 | -- Context is an allocator. Check the bounds of the aggregate against |
4133 | -- those of the designated type, except in the case where the type is | |
4134 | -- unconstrained (and then we can directly return true, see below). | |
4135 | ||
4136 | else pragma Assert (Parent_Kind = N_Allocator); | |
4137 | declare | |
4138 | Desig_Typ : constant Entity_Id := | |
4139 | Designated_Type (Etype (Parent_Node)); | |
4140 | begin | |
4141 | if not Is_Constrained (Desig_Typ) then | |
4142 | return True; | |
4143 | end if; | |
4ff5aa0c | 4144 | |
d0e9248d EB |
4145 | Obj_In := First_Index (Desig_Typ); |
4146 | end; | |
4ff5aa0c AC |
4147 | end if; |
4148 | ||
4149 | while Present (Aggr_In) loop | |
7c4f3267 BD |
4150 | Aggr_Bounds := Get_Index_Bounds (Aggr_In); |
4151 | Obj_Bounds := Get_Index_Bounds (Obj_In); | |
4ff5aa0c | 4152 | |
a80b1eb7 EB |
4153 | -- We require static bounds for the target and a static matching |
4154 | -- of low bound for the aggregate. | |
4155 | ||
7c4f3267 BD |
4156 | if not Compile_Time_Known_Value (Obj_Bounds.First) |
4157 | or else not Compile_Time_Known_Value (Obj_Bounds.Last) | |
4158 | or else not Compile_Time_Known_Value (Aggr_Bounds.First) | |
4159 | or else Expr_Value (Aggr_Bounds.First) /= | |
4160 | Expr_Value (Obj_Bounds.First) | |
4ff5aa0c AC |
4161 | then |
4162 | return False; | |
4163 | ||
4164 | -- For an assignment statement we require static matching of | |
4165 | -- bounds. Ditto for an allocator whose qualified expression | |
4166 | -- is a constrained type. If the expression in the allocator | |
4167 | -- is an unconstrained array, we accept an upper bound that | |
4168 | -- is not static, to allow for nonstatic expressions of the | |
4169 | -- base type. Clearly there are further possibilities (with | |
4170 | -- diminishing returns) for safely building arrays in place | |
4171 | -- here. | |
4172 | ||
a80b1eb7 EB |
4173 | elsif Parent_Kind = N_Assignment_Statement |
4174 | or else Is_Constrained (Etype (Parent_Node)) | |
4ff5aa0c | 4175 | then |
7c4f3267 BD |
4176 | if not Compile_Time_Known_Value (Aggr_Bounds.Last) |
4177 | or else Expr_Value (Aggr_Bounds.Last) /= | |
4178 | Expr_Value (Obj_Bounds.Last) | |
4ff5aa0c AC |
4179 | then |
4180 | return False; | |
4181 | end if; | |
4182 | end if; | |
4183 | ||
4184 | Next_Index (Aggr_In); | |
4185 | Next_Index (Obj_In); | |
4186 | end loop; | |
4187 | end if; | |
4188 | ||
a80b1eb7 EB |
4189 | -- Now check the component values themselves, except for an allocator |
4190 | -- for which the target is newly allocated memory. | |
4ff5aa0c | 4191 | |
a80b1eb7 EB |
4192 | if Parent_Kind = N_Allocator then |
4193 | return True; | |
4194 | else | |
4195 | return Safe_Aggregate (N); | |
4196 | end if; | |
4ff5aa0c AC |
4197 | end In_Place_Assign_OK; |
4198 | ||
70482933 RK |
4199 | ---------------------------- |
4200 | -- Convert_To_Assignments -- | |
4201 | ---------------------------- | |
4202 | ||
4203 | procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is | |
4204 | Loc : constant Source_Ptr := Sloc (N); | |
39f346aa | 4205 | T : Entity_Id; |
70482933 RK |
4206 | Temp : Entity_Id; |
4207 | ||
f29afe5f | 4208 | Aggr_Code : List_Id; |
fbf5a39b AC |
4209 | Instr : Node_Id; |
4210 | Target_Expr : Node_Id; | |
4211 | Parent_Kind : Node_Kind; | |
4212 | Unc_Decl : Boolean := False; | |
4213 | Parent_Node : Node_Id; | |
70482933 RK |
4214 | |
4215 | begin | |
4a08c95c | 4216 | pragma Assert (Nkind (N) in N_Aggregate | N_Extension_Aggregate); |
fa57ac97 ES |
4217 | pragma Assert (not Is_Static_Dispatch_Table_Aggregate (N)); |
4218 | pragma Assert (Is_Record_Type (Typ)); | |
4219 | ||
70482933 RK |
4220 | Parent_Node := Parent (N); |
4221 | Parent_Kind := Nkind (Parent_Node); | |
4222 | ||
4223 | if Parent_Kind = N_Qualified_Expression then | |
d4dfb005 | 4224 | -- Check if we are in an unconstrained declaration because in this |
70482933 | 4225 | -- case the current delayed expansion mechanism doesn't work when |
d4dfb005 | 4226 | -- the declared object size depends on the initializing expr. |
70482933 | 4227 | |
937e9676 AC |
4228 | Parent_Node := Parent (Parent_Node); |
4229 | Parent_Kind := Nkind (Parent_Node); | |
fbf5a39b | 4230 | |
937e9676 AC |
4231 | if Parent_Kind = N_Object_Declaration then |
4232 | Unc_Decl := | |
4233 | not Is_Entity_Name (Object_Definition (Parent_Node)) | |
d4dfb005 | 4234 | or else (Nkind (N) = N_Aggregate |
3fc40cd7 PMR |
4235 | and then |
4236 | Has_Discriminants | |
4237 | (Entity (Object_Definition (Parent_Node)))) | |
937e9676 AC |
4238 | or else Is_Class_Wide_Type |
4239 | (Entity (Object_Definition (Parent_Node))); | |
4240 | end if; | |
70482933 RK |
4241 | end if; |
4242 | ||
3b9fa2df ES |
4243 | -- Just set the Delay flag in the cases where the transformation will be |
4244 | -- done top down from above. | |
fbf5a39b | 4245 | |
f037632e | 4246 | if |
fa57ac97 | 4247 | -- Internal aggregate (transformed when expanding the parent) |
8196b58e MP |
4248 | -- excluding the Container aggregate as these are transformed to |
4249 | -- procedure call later. | |
0f95b178 | 4250 | |
8196b58e MP |
4251 | (Parent_Kind in |
4252 | N_Component_Association | N_Aggregate | N_Extension_Aggregate | |
4253 | and then not Is_Container_Aggregate (Parent_Node)) | |
0f95b178 | 4254 | |
fa57ac97 | 4255 | -- Allocator (see Convert_Aggr_In_Allocator) |
70482933 | 4256 | |
fa57ac97 | 4257 | or else Parent_Kind = N_Allocator |
0f95b178 | 4258 | |
fa57ac97 ES |
4259 | -- Object declaration (see Convert_Aggr_In_Object_Decl) |
4260 | ||
4261 | or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl) | |
4262 | ||
4263 | -- Safe assignment (see Convert_Aggr_Assignments). So far only the | |
4264 | -- assignments in init procs are taken into account. | |
4265 | ||
4266 | or else (Parent_Kind = N_Assignment_Statement | |
4267 | and then Inside_Init_Proc) | |
4268 | ||
bdc193ba AC |
4269 | -- (Ada 2005) An inherently limited type in a return statement, which |
4270 | -- will be handled in a build-in-place fashion, and may be rewritten | |
4271 | -- as an extended return and have its own finalization machinery. | |
4272 | -- In the case of a simple return, the aggregate needs to be delayed | |
4273 | -- until the scope for the return statement has been created, so | |
4274 | -- that any finalization chain will be associated with that scope. | |
4275 | -- For extended returns, we delay expansion to avoid the creation | |
4276 | -- of an unwanted transient scope that could result in premature | |
a9bbfbd0 | 4277 | -- finalization of the return object (which is built in place |
bdc193ba | 4278 | -- within the caller's scope). |
fa57ac97 | 4279 | |
d4dfb005 | 4280 | or else Is_Build_In_Place_Aggregate_Return (N) |
70482933 RK |
4281 | then |
4282 | Set_Expansion_Delayed (N); | |
4283 | return; | |
4284 | end if; | |
4285 | ||
a9bbfbd0 AC |
4286 | -- Otherwise, if a transient scope is required, create it now. If we |
4287 | -- are within an initialization procedure do not create such, because | |
4288 | -- the target of the assignment must not be declared within a local | |
4289 | -- block, and because cleanup will take place on return from the | |
4290 | -- initialization procedure. | |
937e9676 | 4291 | |
a9bbfbd0 AC |
4292 | -- Should the condition be more restrictive ??? |
4293 | ||
4294 | if Requires_Transient_Scope (Typ) and then not Inside_Init_Proc then | |
6560f851 | 4295 | Establish_Transient_Scope (N, Manage_Sec_Stack => False); |
70482933 RK |
4296 | end if; |
4297 | ||
bc1146e5 HK |
4298 | -- If the aggregate is nonlimited, create a temporary, since aggregates |
4299 | -- have "by copy" semantics. If it is limited and context is an | |
4ff5aa0c AC |
4300 | -- assignment, this is a subaggregate for an enclosing aggregate being |
4301 | -- expanded. It must be built in place, so use target of the current | |
4302 | -- assignment. | |
70482933 | 4303 | |
3b9fa2df | 4304 | if Is_Limited_Type (Typ) |
e67df677 | 4305 | and then Parent_Kind = N_Assignment_Statement |
3b9fa2df | 4306 | then |
e67df677 EB |
4307 | Target_Expr := New_Copy_Tree (Name (Parent_Node)); |
4308 | Insert_Actions (Parent_Node, | |
df3e68b1 | 4309 | Build_Record_Aggr_Code (N, Typ, Target_Expr)); |
e67df677 | 4310 | Rewrite (Parent_Node, Make_Null_Statement (Loc)); |
70482933 | 4311 | |
a80b1eb7 EB |
4312 | -- Do not declare a temporary to initialize an aggregate assigned to |
4313 | -- a target when in-place assignment is possible, i.e. preserving the | |
4ff5aa0c AC |
4314 | -- by-copy semantic of aggregates. This avoids large stack usage and |
4315 | -- generates more efficient code. | |
e64ac631 | 4316 | |
e67df677 | 4317 | elsif Parent_Kind = N_Assignment_Statement |
a80b1eb7 | 4318 | and then In_Place_Assign_OK (N, Get_Base_Object (Name (Parent_Node))) |
e64ac631 | 4319 | then |
e67df677 EB |
4320 | declare |
4321 | Lhs : constant Node_Id := Name (Parent_Node); | |
4322 | begin | |
4323 | -- Apply discriminant check if required | |
4324 | ||
4325 | if Has_Discriminants (Etype (N)) then | |
4326 | Apply_Discriminant_Check (N, Etype (Lhs), Lhs); | |
4327 | end if; | |
4328 | ||
4329 | -- The check just above may have replaced the aggregate with a CE | |
4330 | ||
4a08c95c | 4331 | if Nkind (N) in N_Aggregate | N_Extension_Aggregate then |
e67df677 EB |
4332 | Target_Expr := New_Copy_Tree (Lhs); |
4333 | Insert_Actions (Parent_Node, | |
4334 | Build_Record_Aggr_Code (N, Typ, Target_Expr)); | |
4335 | Rewrite (Parent_Node, Make_Null_Statement (Loc)); | |
4336 | end if; | |
4337 | end; | |
e64ac631 | 4338 | |
3b9fa2df | 4339 | else |
faf387e1 | 4340 | Temp := Make_Temporary (Loc, 'A', N); |
70482933 | 4341 | |
39f346aa ES |
4342 | -- If the type inherits unknown discriminants, use the view with |
4343 | -- known discriminants if available. | |
4344 | ||
4345 | if Has_Unknown_Discriminants (Typ) | |
36a66365 | 4346 | and then Present (Underlying_Record_View (Typ)) |
39f346aa ES |
4347 | then |
4348 | T := Underlying_Record_View (Typ); | |
4349 | else | |
4350 | T := Typ; | |
4351 | end if; | |
4352 | ||
3b9fa2df ES |
4353 | Instr := |
4354 | Make_Object_Declaration (Loc, | |
4355 | Defining_Identifier => Temp, | |
39f346aa | 4356 | Object_Definition => New_Occurrence_Of (T, Loc)); |
3b9fa2df ES |
4357 | |
4358 | Set_No_Initialization (Instr); | |
4359 | Insert_Action (N, Instr); | |
39f346aa | 4360 | Initialize_Discriminants (Instr, T); |
f29afe5f | 4361 | |
3b9fa2df | 4362 | Target_Expr := New_Occurrence_Of (Temp, Loc); |
f29afe5f AC |
4363 | Aggr_Code := Build_Record_Aggr_Code (N, T, Target_Expr); |
4364 | ||
4365 | -- Save the last assignment statement associated with the aggregate | |
4366 | -- when building a controlled object. This reference is utilized by | |
4367 | -- the finalization machinery when marking an object as successfully | |
4368 | -- initialized. | |
4369 | ||
4370 | if Needs_Finalization (T) then | |
4371 | Set_Last_Aggregate_Assignment (Temp, Last (Aggr_Code)); | |
4372 | end if; | |
4373 | ||
4374 | Insert_Actions (N, Aggr_Code); | |
3b9fa2df | 4375 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
39f346aa | 4376 | Analyze_And_Resolve (N, T); |
3b9fa2df | 4377 | end if; |
70482933 RK |
4378 | end Convert_To_Assignments; |
4379 | ||
07fc65c4 GB |
4380 | --------------------------- |
4381 | -- Convert_To_Positional -- | |
4382 | --------------------------- | |
4383 | ||
4384 | procedure Convert_To_Positional | |
c42006e9 AC |
4385 | (N : Node_Id; |
4386 | Handle_Bit_Packed : Boolean := False) | |
07fc65c4 | 4387 | is |
c42006e9 | 4388 | Typ : constant Entity_Id := Etype (N); |
b748c3d1 | 4389 | Dims : constant Nat := Number_Dimensions (Typ); |
eaf6e63a | 4390 | Max_Others_Replicate : constant Nat := Max_Aggregate_Size (N); |
07fc65c4 | 4391 | |
0f95b178 JM |
4392 | Static_Components : Boolean := True; |
4393 | ||
4394 | procedure Check_Static_Components; | |
3b9fa2df ES |
4395 | -- Check whether all components of the aggregate are compile-time known |
4396 | -- values, and can be passed as is to the back-end without further | |
4397 | -- expansion. | |
0f95b178 | 4398 | |
fbf5a39b | 4399 | function Flatten |
b748c3d1 EB |
4400 | (N : Node_Id; |
4401 | Dims : Nat; | |
4402 | Ix : Node_Id; | |
4403 | Ixb : Node_Id) return Boolean; | |
c2ba82ad EB |
4404 | -- Convert the aggregate into a purely positional form if possible after |
4405 | -- checking that the bounds of all dimensions are known to be static. | |
fbf5a39b | 4406 | |
b748c3d1 EB |
4407 | function Is_Flat (N : Node_Id; Dims : Nat) return Boolean; |
4408 | -- Return True if the aggregate N is flat (which is not trivial in the | |
4409 | -- case of multidimensional aggregates). | |
fbf5a39b | 4410 | |
b748c3d1 | 4411 | function Is_Static_Element (N : Node_Id; Dims : Nat) return Boolean; |
f1e2bf65 EB |
4412 | -- Return True if N, an element of a component association list, i.e. |
4413 | -- N_Component_Association or N_Iterated_Component_Association, has a | |
4414 | -- compile-time known value and can be passed as is to the back-end | |
4415 | -- without further expansion. | |
4416 | -- An Iterated_Component_Association is treated as nonstatic in most | |
4417 | -- cases for now, so there are possibilities for optimization. | |
4418 | ||
0f95b178 JM |
4419 | ----------------------------- |
4420 | -- Check_Static_Components -- | |
4421 | ----------------------------- | |
4422 | ||
bdc193ba AC |
4423 | -- Could use some comments in this body ??? |
4424 | ||
0f95b178 | 4425 | procedure Check_Static_Components is |
f1e2bf65 EB |
4426 | Assoc : Node_Id; |
4427 | Expr : Node_Id; | |
0f95b178 JM |
4428 | |
4429 | begin | |
4430 | Static_Components := True; | |
4431 | ||
4432 | if Nkind (N) = N_String_Literal then | |
4433 | null; | |
4434 | ||
4435 | elsif Present (Expressions (N)) then | |
4436 | Expr := First (Expressions (N)); | |
4437 | while Present (Expr) loop | |
4438 | if Nkind (Expr) /= N_Aggregate | |
4439 | or else not Compile_Time_Known_Aggregate (Expr) | |
4440 | or else Expansion_Delayed (Expr) | |
4441 | then | |
4442 | Static_Components := False; | |
4443 | exit; | |
4444 | end if; | |
4445 | ||
4446 | Next (Expr); | |
4447 | end loop; | |
4448 | end if; | |
4449 | ||
4450 | if Nkind (N) = N_Aggregate | |
21d7ef70 | 4451 | and then Present (Component_Associations (N)) |
0f95b178 | 4452 | then |
f1e2bf65 EB |
4453 | Assoc := First (Component_Associations (N)); |
4454 | while Present (Assoc) loop | |
b748c3d1 | 4455 | if not Is_Static_Element (Assoc, Dims) then |
0f95b178 JM |
4456 | Static_Components := False; |
4457 | exit; | |
4458 | end if; | |
4459 | ||
f1e2bf65 | 4460 | Next (Assoc); |
0f95b178 JM |
4461 | end loop; |
4462 | end if; | |
4463 | end Check_Static_Components; | |
4464 | ||
fbf5a39b AC |
4465 | ------------- |
4466 | -- Flatten -- | |
4467 | ------------- | |
4468 | ||
4469 | function Flatten | |
b748c3d1 EB |
4470 | (N : Node_Id; |
4471 | Dims : Nat; | |
4472 | Ix : Node_Id; | |
4473 | Ixb : Node_Id) return Boolean | |
fbf5a39b AC |
4474 | is |
4475 | Loc : constant Source_Ptr := Sloc (N); | |
4476 | Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb)); | |
4477 | Lo : constant Node_Id := Type_Low_Bound (Etype (Ix)); | |
4478 | Hi : constant Node_Id := Type_High_Bound (Etype (Ix)); | |
fbf5a39b | 4479 | |
b748c3d1 EB |
4480 | function Cannot_Flatten_Next_Aggr (Expr : Node_Id) return Boolean; |
4481 | -- Return true if Expr is an aggregate for the next dimension that | |
4482 | -- cannot be recursively flattened. | |
4483 | ||
4484 | ------------------------------ | |
4485 | -- Cannot_Flatten_Next_Aggr -- | |
4486 | ------------------------------ | |
4487 | ||
4488 | function Cannot_Flatten_Next_Aggr (Expr : Node_Id) return Boolean is | |
4489 | begin | |
4490 | return Nkind (Expr) = N_Aggregate | |
4491 | and then Present (Next_Index (Ix)) | |
4492 | and then not | |
4493 | Flatten (Expr, Dims - 1, Next_Index (Ix), Next_Index (Ixb)); | |
4494 | end Cannot_Flatten_Next_Aggr; | |
4495 | ||
4496 | -- Local variables | |
4497 | ||
4498 | Lov : Uint; | |
4499 | Hiv : Uint; | |
4500 | Others_Present : Boolean; | |
4501 | ||
4502 | -- Start of processing for Flatten | |
3f5a8fee | 4503 | |
6e937c1c | 4504 | begin |
fbf5a39b AC |
4505 | if Nkind (Original_Node (N)) = N_String_Literal then |
4506 | return True; | |
4507 | end if; | |
07fc65c4 | 4508 | |
0f95b178 JM |
4509 | if not Compile_Time_Known_Value (Lo) |
4510 | or else not Compile_Time_Known_Value (Hi) | |
4511 | then | |
4512 | return False; | |
4513 | end if; | |
07fc65c4 | 4514 | |
fbf5a39b AC |
4515 | Lov := Expr_Value (Lo); |
4516 | Hiv := Expr_Value (Hi); | |
07fc65c4 | 4517 | |
3f5a8fee AC |
4518 | -- Check if there is an others choice |
4519 | ||
b748c3d1 EB |
4520 | Others_Present := False; |
4521 | ||
3f5a8fee | 4522 | if Present (Component_Associations (N)) then |
10c257af ES |
4523 | if Is_Empty_List (Component_Associations (N)) then |
4524 | -- an expanded null array aggregate | |
4525 | return False; | |
4526 | end if; | |
4527 | ||
3f5a8fee AC |
4528 | declare |
4529 | Assoc : Node_Id; | |
4530 | Choice : Node_Id; | |
4531 | ||
4532 | begin | |
4533 | Assoc := First (Component_Associations (N)); | |
4534 | while Present (Assoc) loop | |
9f8d1e5c AC |
4535 | |
4536 | -- If this is a box association, flattening is in general | |
4537 | -- not possible because at this point we cannot tell if the | |
4538 | -- default is static or even exists. | |
4539 | ||
4540 | if Box_Present (Assoc) then | |
4541 | return False; | |
00f45f30 AC |
4542 | |
4543 | elsif Nkind (Assoc) = N_Iterated_Component_Association then | |
4544 | return False; | |
9f8d1e5c AC |
4545 | end if; |
4546 | ||
00f45f30 | 4547 | Choice := First (Choice_List (Assoc)); |
3f5a8fee AC |
4548 | |
4549 | while Present (Choice) loop | |
4550 | if Nkind (Choice) = N_Others_Choice then | |
4551 | Others_Present := True; | |
4552 | end if; | |
4553 | ||
4554 | Next (Choice); | |
4555 | end loop; | |
4556 | ||
4557 | Next (Assoc); | |
4558 | end loop; | |
4559 | end; | |
4560 | end if; | |
4561 | ||
4562 | -- If the low bound is not known at compile time and others is not | |
4563 | -- present we can proceed since the bounds can be obtained from the | |
4564 | -- aggregate. | |
4565 | ||
fbf5a39b | 4566 | if Hiv < Lov |
36a66365 | 4567 | or else (not Compile_Time_Known_Value (Blo) and then Others_Present) |
fbf5a39b AC |
4568 | then |
4569 | return False; | |
4570 | end if; | |
07fc65c4 | 4571 | |
3b9fa2df ES |
4572 | -- Determine if set of alternatives is suitable for conversion and |
4573 | -- build an array containing the values in sequence. | |
07fc65c4 | 4574 | |
fbf5a39b AC |
4575 | declare |
4576 | Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv)) | |
4577 | of Node_Id := (others => Empty); | |
4578 | -- The values in the aggregate sorted appropriately | |
07fc65c4 | 4579 | |
fbf5a39b AC |
4580 | Vlist : List_Id; |
4581 | -- Same data as Vals in list form | |
07fc65c4 | 4582 | |
fbf5a39b AC |
4583 | Rep_Count : Nat; |
4584 | -- Used to validate Max_Others_Replicate limit | |
07fc65c4 | 4585 | |
841dd0f5 | 4586 | Elmt : Node_Id; |
b748c3d1 | 4587 | Expr : Node_Id; |
841dd0f5 AC |
4588 | Num : Int := UI_To_Int (Lov); |
4589 | Choice_Index : Int; | |
4590 | Choice : Node_Id; | |
4591 | Lo, Hi : Node_Id; | |
07fc65c4 | 4592 | |
fbf5a39b AC |
4593 | begin |
4594 | if Present (Expressions (N)) then | |
4595 | Elmt := First (Expressions (N)); | |
fbf5a39b | 4596 | while Present (Elmt) loop |
b748c3d1 EB |
4597 | -- In the case of a multidimensional array, check that the |
4598 | -- aggregate can be recursively flattened. | |
4599 | ||
4600 | if Cannot_Flatten_Next_Aggr (Elmt) then | |
fbf5a39b AC |
4601 | return False; |
4602 | end if; | |
07fc65c4 | 4603 | |
f537fc00 | 4604 | -- Duplicate expression for each index it covers |
1541ede1 ES |
4605 | |
4606 | Vals (Num) := New_Copy_Tree (Elmt); | |
fbf5a39b | 4607 | Num := Num + 1; |
07fc65c4 | 4608 | |
fbf5a39b AC |
4609 | Next (Elmt); |
4610 | end loop; | |
4611 | end if; | |
07fc65c4 | 4612 | |
fbf5a39b AC |
4613 | if No (Component_Associations (N)) then |
4614 | return True; | |
4615 | end if; | |
07fc65c4 | 4616 | |
fbf5a39b | 4617 | Elmt := First (Component_Associations (N)); |
07fc65c4 | 4618 | |
b748c3d1 EB |
4619 | Component_Loop : while Present (Elmt) loop |
4620 | Expr := Expression (Elmt); | |
4621 | ||
4622 | -- In the case of a multidimensional array, check that the | |
4623 | -- aggregate can be recursively flattened. | |
4624 | ||
4625 | if Cannot_Flatten_Next_Aggr (Expr) then | |
fbf5a39b AC |
4626 | return False; |
4627 | end if; | |
07fc65c4 | 4628 | |
00f45f30 | 4629 | Choice := First (Choice_List (Elmt)); |
fbf5a39b AC |
4630 | Choice_Loop : while Present (Choice) loop |
4631 | ||
4632 | -- If we have an others choice, fill in the missing elements | |
4633 | -- subject to the limit established by Max_Others_Replicate. | |
4634 | ||
4635 | if Nkind (Choice) = N_Others_Choice then | |
4636 | Rep_Count := 0; | |
4637 | ||
64a87aa5 EB |
4638 | -- If the expression involves a construct that generates |
4639 | -- a loop, we must generate individual assignments and | |
4640 | -- no flattening is possible. | |
4641 | ||
b748c3d1 | 4642 | if Nkind (Expr) = N_Quantified_Expression then |
1f6237e3 ES |
4643 | return False; |
4644 | end if; | |
4645 | ||
fbf5a39b AC |
4646 | for J in Vals'Range loop |
4647 | if No (Vals (J)) then | |
b748c3d1 | 4648 | Vals (J) := New_Copy_Tree (Expr); |
fbf5a39b AC |
4649 | Rep_Count := Rep_Count + 1; |
4650 | ||
4651 | -- Check for maximum others replication. Note that | |
4652 | -- we skip this test if either of the restrictions | |
b748c3d1 | 4653 | -- No_Implicit_Loops or No_Elaboration_Code is |
8926d369 AC |
4654 | -- active, if this is a preelaborable unit or |
4655 | -- a predefined unit, or if the unit must be | |
4656 | -- placed in data memory. This also ensures that | |
d9819bbd AC |
4657 | -- predefined units get the same level of constant |
4658 | -- folding in Ada 95 and Ada 2005, where their | |
4659 | -- categorization has changed. | |
fbf5a39b AC |
4660 | |
4661 | declare | |
4662 | P : constant Entity_Id := | |
89beb653 | 4663 | Cunit_Entity (Current_Sem_Unit); |
fbf5a39b AC |
4664 | |
4665 | begin | |
f1e2bf65 EB |
4666 | -- Check if duplication is always OK and, if so, |
4667 | -- continue processing. | |
7f4c1903 | 4668 | |
b748c3d1 EB |
4669 | if Restriction_Active (No_Implicit_Loops) then |
4670 | null; | |
4671 | ||
4672 | -- If duplication is not always OK, continue | |
4673 | -- only if either the element is static or is | |
4674 | -- an aggregate (we already know it is OK). | |
4675 | ||
4676 | elsif not Is_Static_Element (Elmt, Dims) | |
4677 | and then Nkind (Expr) /= N_Aggregate | |
4678 | then | |
4679 | return False; | |
4680 | ||
4681 | -- Check if duplication is OK for elaboration | |
4682 | -- purposes and, if so, continue processing. | |
4683 | ||
4684 | elsif Restriction_Active (No_Elaboration_Code) | |
d9819bbd AC |
4685 | or else |
4686 | (Ekind (Current_Scope) = E_Package | |
b748c3d1 EB |
4687 | and then |
4688 | Static_Elaboration_Desired (Current_Scope)) | |
fbf5a39b AC |
4689 | or else Is_Preelaborated (P) |
4690 | or else (Ekind (P) = E_Package_Body | |
4691 | and then | |
b748c3d1 | 4692 | Is_Preelaborated (Spec_Entity (P))) |
7f4c1903 | 4693 | or else |
8ab31c0c | 4694 | Is_Predefined_Unit (Get_Source_Unit (P)) |
fbf5a39b AC |
4695 | then |
4696 | null; | |
6e937c1c | 4697 | |
b748c3d1 EB |
4698 | -- Otherwise, check that the replication count |
4699 | -- is not too high. | |
7f4c1903 | 4700 | |
b748c3d1 | 4701 | elsif Rep_Count > Max_Others_Replicate then |
f1e2bf65 | 4702 | return False; |
fbf5a39b AC |
4703 | end if; |
4704 | end; | |
4705 | end if; | |
4706 | end loop; | |
07fc65c4 | 4707 | |
861e589e ES |
4708 | if Rep_Count = 0 |
4709 | and then Warn_On_Redundant_Constructs | |
4710 | then | |
4711 | Error_Msg_N ("there are no others?r?", Elmt); | |
4712 | end if; | |
4713 | ||
fbf5a39b | 4714 | exit Component_Loop; |
07fc65c4 | 4715 | |
deeb1604 | 4716 | -- Case of a subtype mark, identifier or expanded name |
07fc65c4 | 4717 | |
deeb1604 | 4718 | elsif Is_Entity_Name (Choice) |
fbf5a39b AC |
4719 | and then Is_Type (Entity (Choice)) |
4720 | then | |
4721 | Lo := Type_Low_Bound (Etype (Choice)); | |
4722 | Hi := Type_High_Bound (Etype (Choice)); | |
07fc65c4 | 4723 | |
fbf5a39b | 4724 | -- Case of subtype indication |
07fc65c4 | 4725 | |
fbf5a39b AC |
4726 | elsif Nkind (Choice) = N_Subtype_Indication then |
4727 | Lo := Low_Bound (Range_Expression (Constraint (Choice))); | |
4728 | Hi := High_Bound (Range_Expression (Constraint (Choice))); | |
4729 | ||
4730 | -- Case of a range | |
4731 | ||
4732 | elsif Nkind (Choice) = N_Range then | |
4733 | Lo := Low_Bound (Choice); | |
4734 | Hi := High_Bound (Choice); | |
4735 | ||
4736 | -- Normal subexpression case | |
4737 | ||
4738 | else pragma Assert (Nkind (Choice) in N_Subexpr); | |
4739 | if not Compile_Time_Known_Value (Choice) then | |
4740 | return False; | |
4741 | ||
4742 | else | |
841dd0f5 | 4743 | Choice_Index := UI_To_Int (Expr_Value (Choice)); |
bdc193ba | 4744 | |
841dd0f5 | 4745 | if Choice_Index in Vals'Range then |
b748c3d1 | 4746 | Vals (Choice_Index) := New_Copy_Tree (Expr); |
841dd0f5 AC |
4747 | goto Continue; |
4748 | ||
bdc193ba AC |
4749 | -- Choice is statically out-of-range, will be |
4750 | -- rewritten to raise Constraint_Error. | |
841dd0f5 | 4751 | |
bdc193ba | 4752 | else |
841dd0f5 AC |
4753 | return False; |
4754 | end if; | |
07fc65c4 | 4755 | end if; |
fbf5a39b AC |
4756 | end if; |
4757 | ||
64425dff | 4758 | -- Range cases merge with Lo,Hi set |
fbf5a39b AC |
4759 | |
4760 | if not Compile_Time_Known_Value (Lo) | |
4761 | or else | |
4762 | not Compile_Time_Known_Value (Hi) | |
4763 | then | |
4764 | return False; | |
bdc193ba | 4765 | |
fbf5a39b AC |
4766 | else |
4767 | for J in UI_To_Int (Expr_Value (Lo)) .. | |
4768 | UI_To_Int (Expr_Value (Hi)) | |
4769 | loop | |
b748c3d1 | 4770 | Vals (J) := New_Copy_Tree (Expr); |
fbf5a39b AC |
4771 | end loop; |
4772 | end if; | |
07fc65c4 | 4773 | |
fbf5a39b AC |
4774 | <<Continue>> |
4775 | Next (Choice); | |
4776 | end loop Choice_Loop; | |
07fc65c4 | 4777 | |
fbf5a39b AC |
4778 | Next (Elmt); |
4779 | end loop Component_Loop; | |
07fc65c4 | 4780 | |
fbf5a39b | 4781 | -- If we get here the conversion is possible |
07fc65c4 | 4782 | |
fbf5a39b AC |
4783 | Vlist := New_List; |
4784 | for J in Vals'Range loop | |
4785 | Append (Vals (J), Vlist); | |
4786 | end loop; | |
07fc65c4 | 4787 | |
fbf5a39b AC |
4788 | Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist)); |
4789 | Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N))); | |
4790 | return True; | |
4791 | end; | |
4792 | end Flatten; | |
07fc65c4 | 4793 | |
fbf5a39b AC |
4794 | ------------- |
4795 | -- Is_Flat -- | |
4796 | ------------- | |
07fc65c4 | 4797 | |
b748c3d1 | 4798 | function Is_Flat (N : Node_Id; Dims : Nat) return Boolean is |
fbf5a39b | 4799 | Elmt : Node_Id; |
07fc65c4 | 4800 | |
fbf5a39b AC |
4801 | begin |
4802 | if Dims = 0 then | |
4803 | return True; | |
07fc65c4 | 4804 | |
fbf5a39b AC |
4805 | elsif Nkind (N) = N_Aggregate then |
4806 | if Present (Component_Associations (N)) then | |
4807 | return False; | |
07fc65c4 | 4808 | |
fbf5a39b AC |
4809 | else |
4810 | Elmt := First (Expressions (N)); | |
fbf5a39b AC |
4811 | while Present (Elmt) loop |
4812 | if not Is_Flat (Elmt, Dims - 1) then | |
4813 | return False; | |
07fc65c4 | 4814 | end if; |
07fc65c4 | 4815 | |
fbf5a39b AC |
4816 | Next (Elmt); |
4817 | end loop; | |
07fc65c4 | 4818 | |
fbf5a39b AC |
4819 | return True; |
4820 | end if; | |
4821 | else | |
4822 | return True; | |
4823 | end if; | |
4824 | end Is_Flat; | |
07fc65c4 | 4825 | |
f1e2bf65 EB |
4826 | ------------------------- |
4827 | -- Is_Static_Element -- | |
4828 | ------------------------- | |
4829 | ||
b748c3d1 | 4830 | function Is_Static_Element (N : Node_Id; Dims : Nat) return Boolean is |
f1e2bf65 EB |
4831 | Expr : constant Node_Id := Expression (N); |
4832 | ||
4833 | begin | |
b748c3d1 | 4834 | -- In most cases the interesting expressions are unambiguously static |
f1e2bf65 | 4835 | |
b748c3d1 | 4836 | if Compile_Time_Known_Value (Expr) then |
f1e2bf65 EB |
4837 | return True; |
4838 | ||
4839 | elsif Nkind (N) = N_Iterated_Component_Association then | |
4840 | return False; | |
4841 | ||
4842 | elsif Nkind (Expr) = N_Aggregate | |
4843 | and then Compile_Time_Known_Aggregate (Expr) | |
4844 | and then not Expansion_Delayed (Expr) | |
4845 | then | |
4846 | return True; | |
4847 | ||
b748c3d1 EB |
4848 | -- However, one may write static expressions that are syntactically |
4849 | -- ambiguous, so preanalyze the expression before checking it again, | |
4850 | -- but only at the innermost level for a multidimensional array. | |
4851 | ||
4852 | elsif Dims = 1 then | |
4853 | Preanalyze_And_Resolve (Expr, Component_Type (Typ)); | |
4854 | return Compile_Time_Known_Value (Expr); | |
4855 | ||
f1e2bf65 EB |
4856 | else |
4857 | return False; | |
4858 | end if; | |
4859 | end Is_Static_Element; | |
4860 | ||
fbf5a39b | 4861 | -- Start of processing for Convert_To_Positional |
07fc65c4 | 4862 | |
fbf5a39b | 4863 | begin |
6031f544 AC |
4864 | -- Only convert to positional when generating C in case of an |
4865 | -- object declaration, this is the only case where aggregates are | |
4866 | -- supported in C. | |
4867 | ||
9f51b855 | 4868 | if Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then |
6031f544 AC |
4869 | return; |
4870 | end if; | |
4871 | ||
0ab80019 | 4872 | -- Ada 2005 (AI-287): Do not convert in case of default initialized |
c45b6ae0 AC |
4873 | -- components because in this case will need to call the corresponding |
4874 | -- IP procedure. | |
4875 | ||
4876 | if Has_Default_Init_Comps (N) then | |
4877 | return; | |
4878 | end if; | |
4879 | ||
d7db3f4f ES |
4880 | -- A subaggregate may have been flattened but is not known to be |
4881 | -- Compile_Time_Known. Set that flag in cases that cannot require | |
4882 | -- elaboration code, so that the aggregate can be used as the | |
4883 | -- initial value of a thread-local variable. | |
4884 | ||
b748c3d1 | 4885 | if Is_Flat (N, Dims) then |
2a1838cd EB |
4886 | if Static_Array_Aggregate (N) then |
4887 | Set_Compile_Time_Known_Aggregate (N); | |
d7db3f4f ES |
4888 | end if; |
4889 | ||
fbf5a39b AC |
4890 | return; |
4891 | end if; | |
4892 | ||
36a66365 | 4893 | if Is_Bit_Packed_Array (Typ) and then not Handle_Bit_Packed then |
fbf5a39b AC |
4894 | return; |
4895 | end if; | |
07fc65c4 | 4896 | |
3b9fa2df ES |
4897 | -- Do not convert to positional if controlled components are involved |
4898 | -- since these require special processing | |
07fc65c4 | 4899 | |
fbf5a39b AC |
4900 | if Has_Controlled_Component (Typ) then |
4901 | return; | |
4902 | end if; | |
07fc65c4 | 4903 | |
0f95b178 JM |
4904 | Check_Static_Components; |
4905 | ||
4906 | -- If the size is known, or all the components are static, try to | |
4907 | -- build a fully positional aggregate. | |
4908 | ||
21d7ef70 | 4909 | -- The size of the type may not be known for an aggregate with |
0f95b178 JM |
4910 | -- discriminated array components, but if the components are static |
4911 | -- it is still possible to verify statically that the length is | |
4912 | -- compatible with the upper bound of the type, and therefore it is | |
4913 | -- worth flattening such aggregates as well. | |
4914 | ||
eaf6e63a | 4915 | if Aggr_Size_OK (N) |
b748c3d1 EB |
4916 | and then |
4917 | Flatten (N, Dims, First_Index (Typ), First_Index (Base_Type (Typ))) | |
643a0839 | 4918 | then |
0f95b178 JM |
4919 | if Static_Components then |
4920 | Set_Compile_Time_Known_Aggregate (N); | |
4921 | Set_Expansion_Delayed (N, False); | |
4922 | end if; | |
4923 | ||
07fc65c4 | 4924 | Analyze_And_Resolve (N, Typ); |
fbf5a39b | 4925 | end if; |
d9819bbd | 4926 | |
d74716b3 | 4927 | -- If Static_Elaboration_Desired has been specified, diagnose aggregates |
e6807723 AC |
4928 | -- that will still require initialization code. |
4929 | ||
d9819bbd AC |
4930 | if (Ekind (Current_Scope) = E_Package |
4931 | and then Static_Elaboration_Desired (Current_Scope)) | |
4932 | and then Nkind (Parent (N)) = N_Object_Declaration | |
4933 | then | |
4934 | declare | |
4935 | Expr : Node_Id; | |
4936 | ||
4937 | begin | |
e6807723 | 4938 | if Nkind (N) = N_Aggregate and then Present (Expressions (N)) then |
d9819bbd AC |
4939 | Expr := First (Expressions (N)); |
4940 | while Present (Expr) loop | |
c2ba82ad | 4941 | if not Compile_Time_Known_Value (Expr) then |
8926d369 | 4942 | Error_Msg_N |
21d7ef70 | 4943 | ("non-static object requires elaboration code??", N); |
d9819bbd AC |
4944 | exit; |
4945 | end if; | |
8926d369 | 4946 | |
d9819bbd AC |
4947 | Next (Expr); |
4948 | end loop; | |
4949 | ||
4950 | if Present (Component_Associations (N)) then | |
324ac540 | 4951 | Error_Msg_N ("object requires elaboration code??", N); |
d9819bbd AC |
4952 | end if; |
4953 | end if; | |
4954 | end; | |
4955 | end if; | |
07fc65c4 GB |
4956 | end Convert_To_Positional; |
4957 | ||
70482933 RK |
4958 | ---------------------------- |
4959 | -- Expand_Array_Aggregate -- | |
4960 | ---------------------------- | |
4961 | ||
4962 | -- Array aggregate expansion proceeds as follows: | |
4963 | ||
4964 | -- 1. If requested we generate code to perform all the array aggregate | |
4965 | -- bound checks, specifically | |
4966 | ||
4967 | -- (a) Check that the index range defined by aggregate bounds is | |
4968 | -- compatible with corresponding index subtype. | |
4969 | ||
4970 | -- (b) If an others choice is present check that no aggregate | |
4971 | -- index is outside the bounds of the index constraint. | |
4972 | ||
4973 | -- (c) For multidimensional arrays make sure that all subaggregates | |
4974 | -- corresponding to the same dimension have the same bounds. | |
4975 | ||
fbf5a39b | 4976 | -- 2. Check for packed array aggregate which can be converted to a |
b465ef6f | 4977 | -- constant so that the aggregate disappears completely. |
fbf5a39b AC |
4978 | |
4979 | -- 3. Check case of nested aggregate. Generally nested aggregates are | |
4980 | -- handled during the processing of the parent aggregate. | |
4981 | ||
4982 | -- 4. Check if the aggregate can be statically processed. If this is the | |
70482933 RK |
4983 | -- case pass it as is to Gigi. Note that a necessary condition for |
4984 | -- static processing is that the aggregate be fully positional. | |
4985 | ||
bc1146e5 | 4986 | -- 5. If in-place aggregate expansion is possible (i.e. no need to create |
70482933 RK |
4987 | -- a temporary) then mark the aggregate as such and return. Otherwise |
4988 | -- create a new temporary and generate the appropriate initialization | |
4989 | -- code. | |
4990 | ||
4991 | procedure Expand_Array_Aggregate (N : Node_Id) is | |
4992 | Loc : constant Source_Ptr := Sloc (N); | |
4993 | ||
4994 | Typ : constant Entity_Id := Etype (N); | |
4995 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
07fc65c4 | 4996 | -- Typ is the correct constrained array subtype of the aggregate |
70482933 RK |
4997 | -- Ctyp is the corresponding component type. |
4998 | ||
4999 | Aggr_Dimension : constant Pos := Number_Dimensions (Typ); | |
3cf3e5c6 | 5000 | -- Number of aggregate index dimensions |
70482933 RK |
5001 | |
5002 | Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id; | |
5003 | Aggr_High : array (1 .. Aggr_Dimension) of Node_Id; | |
3cf3e5c6 | 5004 | -- Low and High bounds of the constraint for each aggregate index |
70482933 RK |
5005 | |
5006 | Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id; | |
3cf3e5c6 | 5007 | -- The type of each index |
70482933 | 5008 | |
ac43e11e | 5009 | In_Place_Assign_OK_For_Declaration : Boolean := False; |
bc1146e5 | 5010 | -- True if we are to generate an in-place assignment for a declaration |
ac43e11e | 5011 | |
70482933 RK |
5012 | Maybe_In_Place_OK : Boolean; |
5013 | -- If the type is neither controlled nor packed and the aggregate | |
5014 | -- is the expression in an assignment, assignment in place may be | |
5015 | -- possible, provided other conditions are met on the LHS. | |
5016 | ||
07fc65c4 | 5017 | Others_Present : array (1 .. Aggr_Dimension) of Boolean := |
15f0f591 | 5018 | (others => False); |
d74716b3 AC |
5019 | -- If Others_Present (J) is True, then there is an others choice in one |
5020 | -- of the subaggregates of N at dimension J. | |
70482933 RK |
5021 | |
5022 | procedure Build_Constrained_Type (Positional : Boolean); | |
5023 | -- If the subtype is not static or unconstrained, build a constrained | |
5024 | -- type using the computable sizes of the aggregate and its sub- | |
5025 | -- aggregates. | |
5026 | ||
7c4f3267 | 5027 | procedure Check_Bounds (Aggr_Bounds_Node, Index_Bounds_Node : Node_Id); |
70482933 | 5028 | -- Checks that the bounds of Aggr_Bounds are within the bounds defined |
3424f4c3 PT |
5029 | -- by Index_Bounds. For null array aggregate (Ada 2022) check that the |
5030 | -- aggregate bounds define a null range. | |
70482933 RK |
5031 | |
5032 | procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos); | |
d74716b3 AC |
5033 | -- Checks that in a multidimensional array aggregate all subaggregates |
5034 | -- corresponding to the same dimension have the same bounds. Sub_Aggr is | |
5035 | -- an array subaggregate. Dim is the dimension corresponding to the | |
5036 | -- subaggregate. | |
70482933 RK |
5037 | |
5038 | procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos); | |
d74716b3 AC |
5039 | -- Computes the values of array Others_Present. Sub_Aggr is the array |
5040 | -- subaggregate we start the computation from. Dim is the dimension | |
5041 | -- corresponding to the subaggregate. | |
70482933 | 5042 | |
70482933 | 5043 | procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos); |
d74716b3 | 5044 | -- Checks that if an others choice is present in any subaggregate, no |
70482933 | 5045 | -- aggregate index is outside the bounds of the index constraint. |
d74716b3 AC |
5046 | -- Sub_Aggr is an array subaggregate. Dim is the dimension corresponding |
5047 | -- to the subaggregate. | |
70482933 | 5048 | |
8da337c5 AC |
5049 | function Safe_Left_Hand_Side (N : Node_Id) return Boolean; |
5050 | -- In addition to Maybe_In_Place_OK, in order for an aggregate to be | |
5051 | -- built directly into the target of the assignment it must be free | |
6537318f | 5052 | -- of side effects. N is the LHS of an assignment. |
8da337c5 | 5053 | |
fe43084c ES |
5054 | procedure Two_Pass_Aggregate_Expansion (N : Node_Id); |
5055 | -- If the aggregate consists only of iterated associations then the | |
5056 | -- aggregate is constructed in two steps: | |
5057 | -- a) Build an expression to compute the number of elements | |
5058 | -- generated by each iterator, and use the expression to allocate | |
5059 | -- the destination aggregate. | |
5060 | -- b) Generate the loops corresponding to each iterator to insert | |
5061 | -- the elements in their proper positions. | |
5062 | ||
70482933 RK |
5063 | ---------------------------- |
5064 | -- Build_Constrained_Type -- | |
5065 | ---------------------------- | |
5066 | ||
5067 | procedure Build_Constrained_Type (Positional : Boolean) is | |
c8def50f | 5068 | Agg_Type : constant Entity_Id := Make_Temporary (Loc, 'A'); |
fbf5a39b | 5069 | Decl : Node_Id; |
c8def50f | 5070 | Indexes : constant List_Id := New_List; |
b3143037 | 5071 | Num : Nat; |
fbf5a39b | 5072 | Sub_Agg : Node_Id; |
70482933 RK |
5073 | |
5074 | begin | |
70482933 RK |
5075 | -- If the aggregate is purely positional, all its subaggregates |
5076 | -- have the same size. We collect the dimensions from the first | |
5077 | -- subaggregate at each level. | |
5078 | ||
5079 | if Positional then | |
5080 | Sub_Agg := N; | |
5081 | ||
c8def50f | 5082 | for D in 1 .. Aggr_Dimension loop |
c8c37d2b | 5083 | Num := List_Length (Expressions (Sub_Agg)); |
70482933 | 5084 | |
deeb1604 | 5085 | Append_To (Indexes, |
70482933 | 5086 | Make_Range (Loc, |
c8def50f | 5087 | Low_Bound => Make_Integer_Literal (Loc, Uint_1), |
191fcb3a | 5088 | High_Bound => Make_Integer_Literal (Loc, Num))); |
c8c37d2b PT |
5089 | |
5090 | Sub_Agg := First (Expressions (Sub_Agg)); | |
70482933 RK |
5091 | end loop; |
5092 | ||
5093 | else | |
3b9fa2df ES |
5094 | -- We know the aggregate type is unconstrained and the aggregate |
5095 | -- is not processable by the back end, therefore not necessarily | |
5096 | -- positional. Retrieve each dimension bounds (computed earlier). | |
70482933 | 5097 | |
c8def50f | 5098 | for D in 1 .. Aggr_Dimension loop |
37368818 | 5099 | Append_To (Indexes, |
70482933 | 5100 | Make_Range (Loc, |
37368818 RD |
5101 | Low_Bound => Aggr_Low (D), |
5102 | High_Bound => Aggr_High (D))); | |
70482933 RK |
5103 | end loop; |
5104 | end if; | |
5105 | ||
5106 | Decl := | |
5107 | Make_Full_Type_Declaration (Loc, | |
5108 | Defining_Identifier => Agg_Type, | |
bdc193ba | 5109 | Type_Definition => |
70482933 | 5110 | Make_Constrained_Array_Definition (Loc, |
deeb1604 AC |
5111 | Discrete_Subtype_Definitions => Indexes, |
5112 | Component_Definition => | |
a397db96 | 5113 | Make_Component_Definition (Loc, |
a397db96 AC |
5114 | Subtype_Indication => |
5115 | New_Occurrence_Of (Component_Type (Typ), Loc)))); | |
70482933 RK |
5116 | |
5117 | Insert_Action (N, Decl); | |
5118 | Analyze (Decl); | |
5119 | Set_Etype (N, Agg_Type); | |
5120 | Set_Is_Itype (Agg_Type); | |
5121 | Freeze_Itype (Agg_Type, N); | |
5122 | end Build_Constrained_Type; | |
5123 | ||
5124 | ------------------ | |
5125 | -- Check_Bounds -- | |
5126 | ------------------ | |
5127 | ||
7c4f3267 BD |
5128 | procedure Check_Bounds (Aggr_Bounds_Node, Index_Bounds_Node : Node_Id) is |
5129 | Aggr_Bounds : constant Range_Nodes := | |
5130 | Get_Index_Bounds (Aggr_Bounds_Node); | |
5131 | Ind_Bounds : constant Range_Nodes := | |
5132 | Get_Index_Bounds (Index_Bounds_Node); | |
70482933 | 5133 | |
c8def50f | 5134 | Cond : Node_Id; |
70482933 RK |
5135 | |
5136 | begin | |
3424f4c3 PT |
5137 | -- For a null array aggregate check that high bound (i.e., low |
5138 | -- bound predecessor) exists. Fail if low bound is low bound of | |
5139 | -- base subtype (in all cases, including modular). | |
5140 | ||
5141 | if Is_Null_Aggregate (N) then | |
5142 | Insert_Action (N, | |
5143 | Make_Raise_Constraint_Error (Loc, | |
5144 | Condition => | |
5145 | Make_Op_Eq (Loc, | |
5146 | New_Copy_Tree (Aggr_Bounds.First), | |
5147 | New_Copy_Tree | |
5148 | (Type_Low_Bound (Base_Type (Etype (Ind_Bounds.First))))), | |
5149 | Reason => CE_Range_Check_Failed)); | |
5150 | return; | |
5151 | end if; | |
5152 | ||
70482933 | 5153 | -- Generate the following test: |
bdc193ba | 5154 | |
70482933 | 5155 | -- [constraint_error when |
7c4f3267 BD |
5156 | -- Aggr_Bounds.First <= Aggr_Bounds.Last and then |
5157 | -- (Aggr_Bounds.First < Ind_Bounds.First | |
5158 | -- or else Aggr_Bounds.Last > Ind_Bounds.Last)] | |
3b9fa2df | 5159 | |
641d3093 | 5160 | -- As an optimization try to see if some tests are trivially vacuous |
70482933 RK |
5161 | -- because we are comparing an expression against itself. |
5162 | ||
7c4f3267 BD |
5163 | if Aggr_Bounds.First = Ind_Bounds.First |
5164 | and then Aggr_Bounds.Last = Ind_Bounds.Last | |
5165 | then | |
70482933 RK |
5166 | Cond := Empty; |
5167 | ||
7c4f3267 | 5168 | elsif Aggr_Bounds.Last = Ind_Bounds.Last then |
70482933 RK |
5169 | Cond := |
5170 | Make_Op_Lt (Loc, | |
7c4f3267 BD |
5171 | Left_Opnd => |
5172 | Duplicate_Subexpr_Move_Checks (Aggr_Bounds.First), | |
5173 | Right_Opnd => | |
5174 | Duplicate_Subexpr_Move_Checks (Ind_Bounds.First)); | |
70482933 | 5175 | |
7c4f3267 | 5176 | elsif Aggr_Bounds.First = Ind_Bounds.First then |
70482933 RK |
5177 | Cond := |
5178 | Make_Op_Gt (Loc, | |
7c4f3267 BD |
5179 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Bounds.Last), |
5180 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Bounds.Last)); | |
70482933 RK |
5181 | |
5182 | else | |
5183 | Cond := | |
5184 | Make_Or_Else (Loc, | |
5185 | Left_Opnd => | |
5186 | Make_Op_Lt (Loc, | |
7c4f3267 BD |
5187 | Left_Opnd => |
5188 | Duplicate_Subexpr_Move_Checks (Aggr_Bounds.First), | |
5189 | Right_Opnd => | |
5190 | Duplicate_Subexpr_Move_Checks (Ind_Bounds.First)), | |
70482933 RK |
5191 | |
5192 | Right_Opnd => | |
5193 | Make_Op_Gt (Loc, | |
7c4f3267 BD |
5194 | Left_Opnd => Duplicate_Subexpr (Aggr_Bounds.Last), |
5195 | Right_Opnd => Duplicate_Subexpr (Ind_Bounds.Last))); | |
70482933 RK |
5196 | end if; |
5197 | ||
5198 | if Present (Cond) then | |
5199 | Cond := | |
5200 | Make_And_Then (Loc, | |
5201 | Left_Opnd => | |
5202 | Make_Op_Le (Loc, | |
7c4f3267 BD |
5203 | Left_Opnd => |
5204 | Duplicate_Subexpr_Move_Checks (Aggr_Bounds.First), | |
5205 | Right_Opnd => | |
5206 | Duplicate_Subexpr_Move_Checks (Aggr_Bounds.Last)), | |
70482933 RK |
5207 | |
5208 | Right_Opnd => Cond); | |
5209 | ||
5210 | Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False); | |
5211 | Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False); | |
5212 | Insert_Action (N, | |
07fc65c4 GB |
5213 | Make_Raise_Constraint_Error (Loc, |
5214 | Condition => Cond, | |
8fdafe44 | 5215 | Reason => CE_Range_Check_Failed)); |
70482933 RK |
5216 | end if; |
5217 | end Check_Bounds; | |
5218 | ||
5219 | ---------------------------- | |
5220 | -- Check_Same_Aggr_Bounds -- | |
5221 | ---------------------------- | |
5222 | ||
5223 | procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is | |
8ba0b4cf PT |
5224 | Sub_Bounds : constant Range_Nodes := |
5225 | Get_Index_Bounds (Aggregate_Bounds (Sub_Aggr)); | |
10c257af ES |
5226 | Sub_Lo : Node_Id renames Sub_Bounds.First; |
5227 | Sub_Hi : Node_Id renames Sub_Bounds.Last; | |
d74716b3 | 5228 | -- The bounds of this specific subaggregate |
70482933 RK |
5229 | |
5230 | Aggr_Lo : constant Node_Id := Aggr_Low (Dim); | |
5231 | Aggr_Hi : constant Node_Id := Aggr_High (Dim); | |
5232 | -- The bounds of the aggregate for this dimension | |
5233 | ||
5234 | Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim); | |
3cf3e5c6 | 5235 | -- The index type for this dimension.xxx |
70482933 | 5236 | |
8ba0b4cf | 5237 | Cond : Node_Id; |
fbf5a39b AC |
5238 | Assoc : Node_Id; |
5239 | Expr : Node_Id; | |
70482933 RK |
5240 | |
5241 | begin | |
5242 | -- If index checks are on generate the test | |
3b9fa2df | 5243 | |
70482933 RK |
5244 | -- [constraint_error when |
5245 | -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi] | |
3b9fa2df | 5246 | |
70482933 RK |
5247 | -- As an optimization try to see if some tests are trivially vacuos |
5248 | -- because we are comparing an expression against itself. Also for | |
5249 | -- the first dimension the test is trivially vacuous because there | |
5250 | -- is just one aggregate for dimension 1. | |
5251 | ||
5252 | if Index_Checks_Suppressed (Ind_Typ) then | |
5253 | Cond := Empty; | |
5254 | ||
bdc193ba | 5255 | elsif Dim = 1 or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi) |
70482933 RK |
5256 | then |
5257 | Cond := Empty; | |
5258 | ||
5259 | elsif Aggr_Hi = Sub_Hi then | |
5260 | Cond := | |
5261 | Make_Op_Ne (Loc, | |
fbf5a39b AC |
5262 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), |
5263 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)); | |
70482933 RK |
5264 | |
5265 | elsif Aggr_Lo = Sub_Lo then | |
5266 | Cond := | |
5267 | Make_Op_Ne (Loc, | |
fbf5a39b AC |
5268 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi), |
5269 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi)); | |
70482933 RK |
5270 | |
5271 | else | |
5272 | Cond := | |
5273 | Make_Or_Else (Loc, | |
5274 | Left_Opnd => | |
5275 | Make_Op_Ne (Loc, | |
fbf5a39b AC |
5276 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), |
5277 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)), | |
70482933 RK |
5278 | |
5279 | Right_Opnd => | |
5280 | Make_Op_Ne (Loc, | |
5281 | Left_Opnd => Duplicate_Subexpr (Aggr_Hi), | |
5282 | Right_Opnd => Duplicate_Subexpr (Sub_Hi))); | |
5283 | end if; | |
5284 | ||
5285 | if Present (Cond) then | |
5286 | Insert_Action (N, | |
07fc65c4 GB |
5287 | Make_Raise_Constraint_Error (Loc, |
5288 | Condition => Cond, | |
5289 | Reason => CE_Length_Check_Failed)); | |
70482933 RK |
5290 | end if; |
5291 | ||
d74716b3 | 5292 | -- Now look inside the subaggregate to see if there is more work |
70482933 RK |
5293 | |
5294 | if Dim < Aggr_Dimension then | |
5295 | ||
5296 | -- Process positional components | |
5297 | ||
5298 | if Present (Expressions (Sub_Aggr)) then | |
5299 | Expr := First (Expressions (Sub_Aggr)); | |
5300 | while Present (Expr) loop | |
5301 | Check_Same_Aggr_Bounds (Expr, Dim + 1); | |
5302 | Next (Expr); | |
5303 | end loop; | |
5304 | end if; | |
5305 | ||
5306 | -- Process component associations | |
5307 | ||
5308 | if Present (Component_Associations (Sub_Aggr)) then | |
5309 | Assoc := First (Component_Associations (Sub_Aggr)); | |
5310 | while Present (Assoc) loop | |
5311 | Expr := Expression (Assoc); | |
5312 | Check_Same_Aggr_Bounds (Expr, Dim + 1); | |
5313 | Next (Assoc); | |
5314 | end loop; | |
5315 | end if; | |
5316 | end if; | |
5317 | end Check_Same_Aggr_Bounds; | |
5318 | ||
5319 | ---------------------------- | |
5320 | -- Compute_Others_Present -- | |
5321 | ---------------------------- | |
5322 | ||
5323 | procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is | |
fbf5a39b AC |
5324 | Assoc : Node_Id; |
5325 | Expr : Node_Id; | |
70482933 RK |
5326 | |
5327 | begin | |
5328 | if Present (Component_Associations (Sub_Aggr)) then | |
5329 | Assoc := Last (Component_Associations (Sub_Aggr)); | |
07fc65c4 | 5330 | |
10c257af ES |
5331 | if Present (Assoc) |
5332 | and then Nkind (First (Choice_List (Assoc))) = N_Others_Choice | |
5333 | then | |
70482933 | 5334 | Others_Present (Dim) := True; |
e84d25c9 ES |
5335 | |
5336 | -- An others_clause may be superfluous if previous components | |
5337 | -- cover the full given range of a constrained array. In such | |
5338 | -- a case an others_clause does not contribute any additional | |
5339 | -- components and has not been analyzed. We analyze it now to | |
5340 | -- detect type errors in the expression, even though no code | |
5341 | -- will be generated for it. | |
5342 | ||
5343 | if Dim = Aggr_Dimension | |
5344 | and then Nkind (Assoc) /= N_Iterated_Component_Association | |
5345 | and then not Analyzed (Expression (Assoc)) | |
5346 | and then not Box_Present (Assoc) | |
5347 | then | |
5348 | Preanalyze_And_Resolve (Expression (Assoc), Ctyp); | |
5349 | end if; | |
70482933 RK |
5350 | end if; |
5351 | end if; | |
5352 | ||
d74716b3 | 5353 | -- Now look inside the subaggregate to see if there is more work |
70482933 RK |
5354 | |
5355 | if Dim < Aggr_Dimension then | |
5356 | ||
5357 | -- Process positional components | |
5358 | ||
5359 | if Present (Expressions (Sub_Aggr)) then | |
5360 | Expr := First (Expressions (Sub_Aggr)); | |
5361 | while Present (Expr) loop | |
5362 | Compute_Others_Present (Expr, Dim + 1); | |
5363 | Next (Expr); | |
5364 | end loop; | |
5365 | end if; | |
5366 | ||
5367 | -- Process component associations | |
5368 | ||
5369 | if Present (Component_Associations (Sub_Aggr)) then | |
5370 | Assoc := First (Component_Associations (Sub_Aggr)); | |
5371 | while Present (Assoc) loop | |
5372 | Expr := Expression (Assoc); | |
5373 | Compute_Others_Present (Expr, Dim + 1); | |
5374 | Next (Assoc); | |
5375 | end loop; | |
5376 | end if; | |
5377 | end if; | |
5378 | end Compute_Others_Present; | |
5379 | ||
70482933 RK |
5380 | ------------------ |
5381 | -- Others_Check -- | |
5382 | ------------------ | |
5383 | ||
5384 | procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is | |
5385 | Aggr_Lo : constant Node_Id := Aggr_Low (Dim); | |
5386 | Aggr_Hi : constant Node_Id := Aggr_High (Dim); | |
3cf3e5c6 | 5387 | -- The bounds of the aggregate for this dimension |
70482933 RK |
5388 | |
5389 | Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim); | |
3cf3e5c6 | 5390 | -- The index type for this dimension |
70482933 RK |
5391 | |
5392 | Need_To_Check : Boolean := False; | |
5393 | ||
5394 | Choices_Lo : Node_Id := Empty; | |
5395 | Choices_Hi : Node_Id := Empty; | |
d74716b3 | 5396 | -- The lowest and highest discrete choices for a named subaggregate |
70482933 RK |
5397 | |
5398 | Nb_Choices : Int := -1; | |
d74716b3 | 5399 | -- The number of discrete non-others choices in this subaggregate |
70482933 RK |
5400 | |
5401 | Nb_Elements : Uint := Uint_0; | |
5402 | -- The number of elements in a positional aggregate | |
5403 | ||
5404 | Cond : Node_Id := Empty; | |
5405 | ||
5406 | Assoc : Node_Id; | |
5407 | Choice : Node_Id; | |
5408 | Expr : Node_Id; | |
5409 | ||
5410 | begin | |
5411 | -- Check if we have an others choice. If we do make sure that this | |
d74716b3 | 5412 | -- subaggregate contains at least one element in addition to the |
70482933 RK |
5413 | -- others choice. |
5414 | ||
5415 | if Range_Checks_Suppressed (Ind_Typ) then | |
5416 | Need_To_Check := False; | |
5417 | ||
5418 | elsif Present (Expressions (Sub_Aggr)) | |
5419 | and then Present (Component_Associations (Sub_Aggr)) | |
5420 | then | |
10c257af ES |
5421 | Need_To_Check := |
5422 | not (Is_Empty_List (Expressions (Sub_Aggr)) | |
5423 | and then Is_Empty_List | |
5424 | (Component_Associations (Sub_Aggr))); | |
70482933 RK |
5425 | |
5426 | elsif Present (Component_Associations (Sub_Aggr)) then | |
5427 | Assoc := Last (Component_Associations (Sub_Aggr)); | |
5428 | ||
00f45f30 | 5429 | if Nkind (First (Choice_List (Assoc))) /= N_Others_Choice then |
70482933 RK |
5430 | Need_To_Check := False; |
5431 | ||
5432 | else | |
3b9fa2df ES |
5433 | -- Count the number of discrete choices. Start with -1 because |
5434 | -- the others choice does not count. | |
70482933 | 5435 | |
bdc193ba AC |
5436 | -- Is there some reason we do not use List_Length here ??? |
5437 | ||
70482933 RK |
5438 | Nb_Choices := -1; |
5439 | Assoc := First (Component_Associations (Sub_Aggr)); | |
5440 | while Present (Assoc) loop | |
00f45f30 | 5441 | Choice := First (Choice_List (Assoc)); |
70482933 RK |
5442 | while Present (Choice) loop |
5443 | Nb_Choices := Nb_Choices + 1; | |
5444 | Next (Choice); | |
5445 | end loop; | |
5446 | ||
5447 | Next (Assoc); | |
5448 | end loop; | |
5449 | ||
5450 | -- If there is only an others choice nothing to do | |
5451 | ||
5452 | Need_To_Check := (Nb_Choices > 0); | |
5453 | end if; | |
5454 | ||
5455 | else | |
5456 | Need_To_Check := False; | |
5457 | end if; | |
5458 | ||
d74716b3 | 5459 | -- If we are dealing with a positional subaggregate with an others |
3b9fa2df | 5460 | -- choice then compute the number or positional elements. |
70482933 RK |
5461 | |
5462 | if Need_To_Check and then Present (Expressions (Sub_Aggr)) then | |
5463 | Expr := First (Expressions (Sub_Aggr)); | |
5464 | Nb_Elements := Uint_0; | |
5465 | while Present (Expr) loop | |
5466 | Nb_Elements := Nb_Elements + 1; | |
5467 | Next (Expr); | |
5468 | end loop; | |
5469 | ||
5470 | -- If the aggregate contains discrete choices and an others choice | |
5471 | -- compute the smallest and largest discrete choice values. | |
5472 | ||
5473 | elsif Need_To_Check then | |
5474 | Compute_Choices_Lo_And_Choices_Hi : declare | |
07fc65c4 | 5475 | |
70482933 RK |
5476 | Table : Case_Table_Type (1 .. Nb_Choices); |
5477 | -- Used to sort all the different choice values | |
5478 | ||
07fc65c4 | 5479 | J : Pos := 1; |
70482933 RK |
5480 | |
5481 | begin | |
5482 | Assoc := First (Component_Associations (Sub_Aggr)); | |
5483 | while Present (Assoc) loop | |
00f45f30 | 5484 | Choice := First (Choice_List (Assoc)); |
70482933 RK |
5485 | while Present (Choice) loop |
5486 | if Nkind (Choice) = N_Others_Choice then | |
5487 | exit; | |
5488 | end if; | |
5489 | ||
7c4f3267 BD |
5490 | declare |
5491 | Bounds : constant Range_Nodes := | |
5492 | Get_Index_Bounds (Choice); | |
5493 | begin | |
5494 | Table (J).Choice_Lo := Bounds.First; | |
5495 | Table (J).Choice_Hi := Bounds.Last; | |
5496 | end; | |
70482933 | 5497 | |
07fc65c4 | 5498 | J := J + 1; |
70482933 RK |
5499 | Next (Choice); |
5500 | end loop; | |
5501 | ||
5502 | Next (Assoc); | |
5503 | end loop; | |
5504 | ||
5505 | -- Sort the discrete choices | |
5506 | ||
5507 | Sort_Case_Table (Table); | |
5508 | ||
5509 | Choices_Lo := Table (1).Choice_Lo; | |
5510 | Choices_Hi := Table (Nb_Choices).Choice_Hi; | |
5511 | end Compute_Choices_Lo_And_Choices_Hi; | |
5512 | end if; | |
5513 | ||
d74716b3 | 5514 | -- If no others choice in this subaggregate, or the aggregate |
70482933 RK |
5515 | -- comprises only an others choice, nothing to do. |
5516 | ||
5517 | if not Need_To_Check then | |
5518 | Cond := Empty; | |
5519 | ||
3b9fa2df ES |
5520 | -- If we are dealing with an aggregate containing an others choice |
5521 | -- and positional components, we generate the following test: | |
5522 | ||
70482933 RK |
5523 | -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) > |
5524 | -- Ind_Typ'Pos (Aggr_Hi) | |
5525 | -- then | |
5526 | -- raise Constraint_Error; | |
5527 | -- end if; | |
5528 | ||
6a987d78 EB |
5529 | -- in the general case, but the following simpler test: |
5530 | ||
5531 | -- [constraint_error when | |
5532 | -- Aggr_Lo + (Nb_Elements - 1) > Aggr_Hi]; | |
5533 | ||
5534 | -- instead if the index type is a signed integer. | |
5535 | ||
70482933 | 5536 | elsif Nb_Elements > Uint_0 then |
6a987d78 EB |
5537 | if Nb_Elements = Uint_1 then |
5538 | Cond := | |
5539 | Make_Op_Gt (Loc, | |
5540 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), | |
5541 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)); | |
5542 | ||
5543 | elsif Is_Signed_Integer_Type (Ind_Typ) then | |
5544 | Cond := | |
5545 | Make_Op_Gt (Loc, | |
5546 | Left_Opnd => | |
5547 | Make_Op_Add (Loc, | |
5548 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), | |
5549 | Right_Opnd => | |
5550 | Make_Integer_Literal (Loc, Nb_Elements - 1)), | |
5551 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)); | |
70482933 | 5552 | |
6a987d78 EB |
5553 | else |
5554 | Cond := | |
5555 | Make_Op_Gt (Loc, | |
5556 | Left_Opnd => | |
5557 | Make_Op_Add (Loc, | |
5558 | Left_Opnd => | |
5559 | Make_Attribute_Reference (Loc, | |
5560 | Prefix => New_Occurrence_Of (Ind_Typ, Loc), | |
5561 | Attribute_Name => Name_Pos, | |
5562 | Expressions => | |
5563 | New_List | |
5564 | (Duplicate_Subexpr_Move_Checks (Aggr_Lo))), | |
5565 | Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)), | |
5566 | ||
5567 | Right_Opnd => | |
5568 | Make_Attribute_Reference (Loc, | |
5569 | Prefix => New_Occurrence_Of (Ind_Typ, Loc), | |
5570 | Attribute_Name => Name_Pos, | |
5571 | Expressions => New_List ( | |
5572 | Duplicate_Subexpr_Move_Checks (Aggr_Hi)))); | |
5573 | end if; | |
70482933 | 5574 | |
3b9fa2df ES |
5575 | -- If we are dealing with an aggregate containing an others choice |
5576 | -- and discrete choices we generate the following test: | |
5577 | ||
70482933 RK |
5578 | -- [constraint_error when |
5579 | -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi]; | |
5580 | ||
5581 | else | |
5582 | Cond := | |
5583 | Make_Or_Else (Loc, | |
5584 | Left_Opnd => | |
5585 | Make_Op_Lt (Loc, | |
bdc193ba AC |
5586 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Choices_Lo), |
5587 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo)), | |
70482933 RK |
5588 | |
5589 | Right_Opnd => | |
5590 | Make_Op_Gt (Loc, | |
bdc193ba AC |
5591 | Left_Opnd => Duplicate_Subexpr (Choices_Hi), |
5592 | Right_Opnd => Duplicate_Subexpr (Aggr_Hi))); | |
70482933 RK |
5593 | end if; |
5594 | ||
5595 | if Present (Cond) then | |
5596 | Insert_Action (N, | |
07fc65c4 GB |
5597 | Make_Raise_Constraint_Error (Loc, |
5598 | Condition => Cond, | |
5599 | Reason => CE_Length_Check_Failed)); | |
641d3093 TQ |
5600 | -- Questionable reason code, shouldn't that be a |
5601 | -- CE_Range_Check_Failed ??? | |
70482933 RK |
5602 | end if; |
5603 | ||
d74716b3 | 5604 | -- Now look inside the subaggregate to see if there is more work |
70482933 RK |
5605 | |
5606 | if Dim < Aggr_Dimension then | |
5607 | ||
5608 | -- Process positional components | |
5609 | ||
5610 | if Present (Expressions (Sub_Aggr)) then | |
5611 | Expr := First (Expressions (Sub_Aggr)); | |
5612 | while Present (Expr) loop | |
5613 | Others_Check (Expr, Dim + 1); | |
5614 | Next (Expr); | |
5615 | end loop; | |
5616 | end if; | |
5617 | ||
5618 | -- Process component associations | |
5619 | ||
5620 | if Present (Component_Associations (Sub_Aggr)) then | |
5621 | Assoc := First (Component_Associations (Sub_Aggr)); | |
5622 | while Present (Assoc) loop | |
5623 | Expr := Expression (Assoc); | |
5624 | Others_Check (Expr, Dim + 1); | |
5625 | Next (Assoc); | |
5626 | end loop; | |
5627 | end if; | |
5628 | end if; | |
5629 | end Others_Check; | |
5630 | ||
8da337c5 AC |
5631 | ------------------------- |
5632 | -- Safe_Left_Hand_Side -- | |
5633 | ------------------------- | |
5634 | ||
5635 | function Safe_Left_Hand_Side (N : Node_Id) return Boolean is | |
deeb1604 AC |
5636 | function Is_Safe_Index (Indx : Node_Id) return Boolean; |
5637 | -- If the left-hand side includes an indexed component, check that | |
d74716b3 | 5638 | -- the indexes are free of side effects. |
deeb1604 AC |
5639 | |
5640 | ------------------- | |
5641 | -- Is_Safe_Index -- | |
5642 | ------------------- | |
5643 | ||
5644 | function Is_Safe_Index (Indx : Node_Id) return Boolean is | |
5645 | begin | |
5646 | if Is_Entity_Name (Indx) then | |
5647 | return True; | |
5648 | ||
5649 | elsif Nkind (Indx) = N_Integer_Literal then | |
5650 | return True; | |
5651 | ||
5652 | elsif Nkind (Indx) = N_Function_Call | |
5653 | and then Is_Entity_Name (Name (Indx)) | |
36a66365 | 5654 | and then Has_Pragma_Pure_Function (Entity (Name (Indx))) |
deeb1604 AC |
5655 | then |
5656 | return True; | |
5657 | ||
5658 | elsif Nkind (Indx) = N_Type_Conversion | |
5659 | and then Is_Safe_Index (Expression (Indx)) | |
5660 | then | |
5661 | return True; | |
5662 | ||
5663 | else | |
5664 | return False; | |
5665 | end if; | |
5666 | end Is_Safe_Index; | |
5667 | ||
5668 | -- Start of processing for Safe_Left_Hand_Side | |
5669 | ||
8da337c5 AC |
5670 | begin |
5671 | if Is_Entity_Name (N) then | |
5672 | return True; | |
5673 | ||
4a08c95c | 5674 | elsif Nkind (N) in N_Explicit_Dereference | N_Selected_Component |
8da337c5 AC |
5675 | and then Safe_Left_Hand_Side (Prefix (N)) |
5676 | then | |
5677 | return True; | |
5678 | ||
5679 | elsif Nkind (N) = N_Indexed_Component | |
5680 | and then Safe_Left_Hand_Side (Prefix (N)) | |
36a66365 | 5681 | and then Is_Safe_Index (First (Expressions (N))) |
8da337c5 AC |
5682 | then |
5683 | return True; | |
deeb1604 AC |
5684 | |
5685 | elsif Nkind (N) = N_Unchecked_Type_Conversion then | |
5686 | return Safe_Left_Hand_Side (Expression (N)); | |
5687 | ||
8da337c5 AC |
5688 | else |
5689 | return False; | |
5690 | end if; | |
5691 | end Safe_Left_Hand_Side; | |
5692 | ||
fe43084c ES |
5693 | ---------------------------------- |
5694 | -- Two_Pass_Aggregate_Expansion -- | |
5695 | ---------------------------------- | |
5696 | ||
5697 | procedure Two_Pass_Aggregate_Expansion (N : Node_Id) is | |
5698 | Loc : constant Source_Ptr := Sloc (N); | |
5699 | Comp_Type : constant Entity_Id := Etype (N); | |
5700 | Index_Id : constant Entity_Id := Make_Temporary (Loc, 'I', N); | |
5701 | Index_Type : constant Entity_Id := Etype (First_Index (Etype (N))); | |
5702 | Size_Id : constant Entity_Id := Make_Temporary (Loc, 'I', N); | |
5703 | TmpE : constant Entity_Id := Make_Temporary (Loc, 'A', N); | |
5704 | ||
5705 | Assoc : Node_Id := First (Component_Associations (N)); | |
5706 | Incr : Node_Id; | |
5707 | Iter : Node_Id; | |
5708 | New_Comp : Node_Id; | |
5709 | One_Loop : Node_Id; | |
5710 | ||
5711 | Size_Expr_Code : List_Id; | |
5712 | Insertion_Code : List_Id := New_List; | |
5713 | ||
5714 | begin | |
5715 | Size_Expr_Code := New_List ( | |
5716 | Make_Object_Declaration (Loc, | |
5717 | Defining_Identifier => Size_Id, | |
5718 | Object_Definition => New_Occurrence_Of (Standard_Integer, Loc), | |
5719 | Expression => Make_Integer_Literal (Loc, 0))); | |
5720 | ||
5721 | -- First pass: execute the iterators to count the number of elements | |
5722 | -- that will be generated. | |
5723 | ||
5724 | while Present (Assoc) loop | |
5725 | Iter := Iterator_Specification (Assoc); | |
5726 | Incr := Make_Assignment_Statement (Loc, | |
5727 | Name => New_Occurrence_Of (Size_Id, Loc), | |
5728 | Expression => | |
5729 | Make_Op_Add (Loc, | |
5730 | Left_Opnd => New_Occurrence_Of (Size_Id, Loc), | |
5731 | Right_Opnd => Make_Integer_Literal (Loc, 1))); | |
5732 | ||
f3561c06 | 5733 | One_Loop := Make_Implicit_Loop_Statement (N, |
fe43084c ES |
5734 | Iteration_Scheme => |
5735 | Make_Iteration_Scheme (Loc, | |
08613129 | 5736 | Iterator_Specification => New_Copy_Tree (Iter)), |
fe43084c ES |
5737 | Statements => New_List (Incr)); |
5738 | ||
5739 | Append (One_Loop, Size_Expr_Code); | |
5740 | Next (Assoc); | |
5741 | end loop; | |
5742 | ||
5743 | Insert_Actions (N, Size_Expr_Code); | |
5744 | ||
5745 | -- Build a constrained subtype with the calculated length | |
5746 | -- and declare the proper bounded aggregate object. | |
5747 | -- The index type is some discrete type, so the bounds of the | |
5748 | -- constructed array are computed as T'Val (T'Pos (ineger bound)); | |
5749 | ||
5750 | declare | |
5751 | Pos_Lo : constant Node_Id := | |
5752 | Make_Attribute_Reference (Loc, | |
5753 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5754 | Attribute_Name => Name_Pos, | |
5755 | Expressions => New_List ( | |
5756 | Make_Attribute_Reference (Loc, | |
5757 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5758 | Attribute_Name => Name_First))); | |
5759 | ||
5760 | Aggr_Lo : constant Node_Id := | |
5761 | Make_Attribute_Reference (Loc, | |
5762 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5763 | Attribute_Name => Name_Val, | |
5764 | Expressions => New_List (New_Copy_Tree (Pos_Lo))); | |
5765 | ||
5766 | -- Hi = Index_type'Pos (Lo + Size -1). | |
5767 | ||
5768 | Pos_Hi : constant Node_Id := | |
5769 | Make_Op_Add (Loc, | |
5770 | Left_Opnd => New_Copy_Tree (Pos_Lo), | |
5771 | Right_Opnd => | |
5772 | Make_Op_Subtract (Loc, | |
5773 | Left_Opnd => New_Occurrence_Of (Size_Id, Loc), | |
5774 | Right_Opnd => Make_Integer_Literal (Loc, 1))); | |
5775 | ||
5776 | -- Corresponding index value | |
5777 | ||
5778 | Aggr_Hi : constant Node_Id := | |
5779 | Make_Attribute_Reference (Loc, | |
5780 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5781 | Attribute_Name => Name_Val, | |
5782 | Expressions => New_List (New_Copy_Tree (Pos_Hi))); | |
5783 | ||
5784 | SubE : constant Entity_Id := Make_Temporary (Loc, 'T'); | |
5785 | SubD : constant Node_Id := | |
5786 | Make_Subtype_Declaration (Loc, | |
5787 | Defining_Identifier => SubE, | |
5788 | Subtype_Indication => | |
5789 | Make_Subtype_Indication (Loc, | |
5790 | Subtype_Mark => | |
5791 | New_Occurrence_Of (Etype (Comp_Type), Loc), | |
5792 | Constraint => | |
5793 | Make_Index_Or_Discriminant_Constraint | |
5794 | (Loc, | |
5795 | Constraints => | |
5796 | New_List (Make_Range (Loc, Aggr_Lo, Aggr_Hi))))); | |
5797 | ||
5798 | -- Create a temporary array of the above subtype which | |
5799 | -- will be used to capture the aggregate assignments. | |
5800 | ||
5801 | TmpD : constant Node_Id := | |
5802 | Make_Object_Declaration (Loc, | |
5803 | Defining_Identifier => TmpE, | |
5804 | Object_Definition => New_Occurrence_Of (SubE, Loc)); | |
5805 | begin | |
5806 | Insert_Actions (N, New_List (SubD, TmpD)); | |
5807 | end; | |
5808 | ||
5809 | -- Second pass: use the iterators to generate the elements of the | |
5810 | -- aggregate. Insertion index starts at Index_Type'First. We | |
5811 | -- assume that the second evaluation of each iterator generates | |
5812 | -- the same number of elements as the first pass, and consider | |
5813 | -- that the execution is erroneous (even if the RM does not state | |
5814 | -- this explicitly) if the number of elements generated differs | |
5815 | -- between first and second pass. | |
5816 | ||
5817 | Assoc := First (Component_Associations (N)); | |
5818 | ||
5819 | -- Initialize insertion position to first array component. | |
5820 | ||
5821 | Insertion_Code := New_List ( | |
5822 | Make_Object_Declaration (Loc, | |
5823 | Defining_Identifier => Index_Id, | |
5824 | Object_Definition => | |
5825 | New_Occurrence_Of (Index_Type, Loc), | |
5826 | Expression => | |
5827 | Make_Attribute_Reference (Loc, | |
5828 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5829 | Attribute_Name => Name_First))); | |
5830 | ||
5831 | while Present (Assoc) loop | |
5832 | Iter := Iterator_Specification (Assoc); | |
5833 | New_Comp := Make_Assignment_Statement (Loc, | |
5834 | Name => | |
5835 | Make_Indexed_Component (Loc, | |
5836 | Prefix => New_Occurrence_Of (TmpE, Loc), | |
5837 | Expressions => | |
5838 | New_List (New_Occurrence_Of (Index_Id, Loc))), | |
d983abeb | 5839 | Expression => Copy_Separate_Tree (Expression (Assoc))); |
fe43084c ES |
5840 | |
5841 | -- Advance index position for insertion. | |
5842 | ||
5843 | Incr := Make_Assignment_Statement (Loc, | |
5844 | Name => New_Occurrence_Of (Index_Id, Loc), | |
5845 | Expression => | |
5846 | Make_Attribute_Reference (Loc, | |
5847 | Prefix => | |
5848 | New_Occurrence_Of (Index_Type, Loc), | |
5849 | Attribute_Name => Name_Succ, | |
5850 | Expressions => | |
5851 | New_List (New_Occurrence_Of (Index_Id, Loc)))); | |
5852 | ||
8b1a5da3 ES |
5853 | -- Add guard to skip last increment when upper bound is reached. |
5854 | ||
5855 | Incr := Make_If_Statement (Loc, | |
5856 | Condition => | |
5857 | Make_Op_Ne (Loc, | |
5858 | Left_Opnd => New_Occurrence_Of (Index_Id, Loc), | |
5859 | Right_Opnd => | |
5860 | Make_Attribute_Reference (Loc, | |
5861 | Prefix => New_Occurrence_Of (Index_Type, Loc), | |
5862 | Attribute_Name => Name_Last)), | |
5863 | Then_Statements => New_List (Incr)); | |
5864 | ||
f3561c06 | 5865 | One_Loop := Make_Implicit_Loop_Statement (N, |
fe43084c ES |
5866 | Iteration_Scheme => |
5867 | Make_Iteration_Scheme (Loc, | |
08613129 | 5868 | Iterator_Specification => Copy_Separate_Tree (Iter)), |
fe43084c ES |
5869 | Statements => New_List (New_Comp, Incr)); |
5870 | ||
5871 | Append (One_Loop, Insertion_Code); | |
5872 | Next (Assoc); | |
5873 | end loop; | |
5874 | ||
5875 | Insert_Actions (N, Insertion_Code); | |
5876 | ||
5877 | -- Depending on context this may not work for build-in-place | |
5878 | -- arrays ??? | |
5879 | ||
5880 | Rewrite (N, New_Occurrence_Of (TmpE, Loc)); | |
5881 | ||
5882 | end Two_Pass_Aggregate_Expansion; | |
5883 | ||
8da337c5 | 5884 | -- Local variables |
70482933 RK |
5885 | |
5886 | Tmp : Entity_Id; | |
fbf5a39b | 5887 | -- Holds the temporary aggregate value |
70482933 RK |
5888 | |
5889 | Tmp_Decl : Node_Id; | |
fbf5a39b | 5890 | -- Holds the declaration of Tmp |
70482933 RK |
5891 | |
5892 | Aggr_Code : List_Id; | |
5893 | Parent_Node : Node_Id; | |
5894 | Parent_Kind : Node_Kind; | |
5895 | ||
5896 | -- Start of processing for Expand_Array_Aggregate | |
5897 | ||
5898 | begin | |
5899 | -- Do not touch the special aggregates of attributes used for Asm calls | |
5900 | ||
5901 | if Is_RTE (Ctyp, RE_Asm_Input_Operand) | |
5902 | or else Is_RTE (Ctyp, RE_Asm_Output_Operand) | |
5903 | then | |
5904 | return; | |
4a1bfefb | 5905 | |
fe43084c | 5906 | elsif Present (Component_Associations (N)) |
8b1a5da3 ES |
5907 | and then Nkind (First (Component_Associations (N))) = |
5908 | N_Iterated_Component_Association | |
5909 | and then | |
5910 | Present (Iterator_Specification (First (Component_Associations (N)))) | |
fe43084c ES |
5911 | then |
5912 | Two_Pass_Aggregate_Expansion (N); | |
5913 | return; | |
5914 | ||
f5655e4a AC |
5915 | -- Do not attempt expansion if error already detected. We may reach this |
5916 | -- point in spite of previous errors when compiling with -gnatq, to | |
5917 | -- force all possible errors (this is the usual ACATS mode). | |
5918 | ||
5919 | elsif Error_Posted (N) then | |
5920 | return; | |
70482933 RK |
5921 | end if; |
5922 | ||
07fc65c4 | 5923 | -- If the semantic analyzer has determined that aggregate N will raise |
e7c0dd39 | 5924 | -- Constraint_Error at run time, then the aggregate node has been |
07fc65c4 GB |
5925 | -- replaced with an N_Raise_Constraint_Error node and we should |
5926 | -- never get here. | |
70482933 RK |
5927 | |
5928 | pragma Assert (not Raises_Constraint_Error (N)); | |
5929 | ||
3cf3e5c6 | 5930 | -- STEP 1a |
fbf5a39b AC |
5931 | |
5932 | -- Check that the index range defined by aggregate bounds is | |
5933 | -- compatible with corresponding index subtype. | |
70482933 RK |
5934 | |
5935 | Index_Compatibility_Check : declare | |
5936 | Aggr_Index_Range : Node_Id := First_Index (Typ); | |
5937 | -- The current aggregate index range | |
5938 | ||
5939 | Index_Constraint : Node_Id := First_Index (Etype (Typ)); | |
5940 | -- The corresponding index constraint against which we have to | |
5941 | -- check the above aggregate index range. | |
5942 | ||
5943 | begin | |
5944 | Compute_Others_Present (N, 1); | |
5945 | ||
5946 | for J in 1 .. Aggr_Dimension loop | |
bdc193ba AC |
5947 | -- There is no need to emit a check if an others choice is present |
5948 | -- for this array aggregate dimension since in this case one of | |
d74716b3 | 5949 | -- N's subaggregates has taken its bounds from the context and |
bdc193ba | 5950 | -- these bounds must have been checked already. In addition all |
d74716b3 AC |
5951 | -- subaggregates corresponding to the same dimension must all have |
5952 | -- the same bounds (checked in (c) below). | |
70482933 RK |
5953 | |
5954 | if not Range_Checks_Suppressed (Etype (Index_Constraint)) | |
5955 | and then not Others_Present (J) | |
5956 | then | |
3b9fa2df ES |
5957 | -- We don't use Checks.Apply_Range_Check here because it emits |
5958 | -- a spurious check. Namely it checks that the range defined by | |
d74716b3 | 5959 | -- the aggregate bounds is nonempty. But we know this already |
3b9fa2df | 5960 | -- if we get here. |
70482933 RK |
5961 | |
5962 | Check_Bounds (Aggr_Index_Range, Index_Constraint); | |
5963 | end if; | |
5964 | ||
3b9fa2df ES |
5965 | -- Save the low and high bounds of the aggregate index as well as |
5966 | -- the index type for later use in checks (b) and (c) below. | |
70482933 | 5967 | |
10c257af ES |
5968 | Get_Index_Bounds |
5969 | (Aggr_Index_Range, L => Aggr_Low (J), H => Aggr_High (J)); | |
70482933 RK |
5970 | |
5971 | Aggr_Index_Typ (J) := Etype (Index_Constraint); | |
5972 | ||
5973 | Next_Index (Aggr_Index_Range); | |
5974 | Next_Index (Index_Constraint); | |
5975 | end loop; | |
5976 | end Index_Compatibility_Check; | |
5977 | ||
3cf3e5c6 | 5978 | -- STEP 1b |
fbf5a39b | 5979 | |
3b9fa2df ES |
5980 | -- If an others choice is present check that no aggregate index is |
5981 | -- outside the bounds of the index constraint. | |
70482933 RK |
5982 | |
5983 | Others_Check (N, 1); | |
5984 | ||
3cf3e5c6 | 5985 | -- STEP 1c |
fbf5a39b AC |
5986 | |
5987 | -- For multidimensional arrays make sure that all subaggregates | |
5988 | -- corresponding to the same dimension have the same bounds. | |
70482933 RK |
5989 | |
5990 | if Aggr_Dimension > 1 then | |
5991 | Check_Same_Aggr_Bounds (N, 1); | |
5992 | end if; | |
5993 | ||
688a9b51 RD |
5994 | -- STEP 1d |
5995 | ||
5996 | -- If we have a default component value, or simple initialization is | |
5997 | -- required for the component type, then we replace <> in component | |
5998 | -- associations by the required default value. | |
5999 | ||
6000 | declare | |
6001 | Default_Val : Node_Id; | |
6002 | Assoc : Node_Id; | |
6003 | ||
6004 | begin | |
6005 | if (Present (Default_Aspect_Component_Value (Typ)) | |
6006 | or else Needs_Simple_Initialization (Ctyp)) | |
6007 | and then Present (Component_Associations (N)) | |
6008 | then | |
6009 | Assoc := First (Component_Associations (N)); | |
6010 | while Present (Assoc) loop | |
6011 | if Nkind (Assoc) = N_Component_Association | |
6012 | and then Box_Present (Assoc) | |
6013 | then | |
6014 | Set_Box_Present (Assoc, False); | |
6015 | ||
6016 | if Present (Default_Aspect_Component_Value (Typ)) then | |
6017 | Default_Val := Default_Aspect_Component_Value (Typ); | |
6018 | else | |
6019 | Default_Val := Get_Simple_Init_Val (Ctyp, N); | |
6020 | end if; | |
6021 | ||
6022 | Set_Expression (Assoc, New_Copy_Tree (Default_Val)); | |
6023 | Analyze_And_Resolve (Expression (Assoc), Ctyp); | |
6024 | end if; | |
6025 | ||
6026 | Next (Assoc); | |
6027 | end loop; | |
6028 | end if; | |
6029 | end; | |
6030 | ||
3cf3e5c6 | 6031 | -- STEP 2 |
70482933 | 6032 | |
3b9fa2df ES |
6033 | -- Here we test for is packed array aggregate that we can handle at |
6034 | -- compile time. If so, return with transformation done. Note that we do | |
6035 | -- this even if the aggregate is nested, because once we have done this | |
a90bd866 | 6036 | -- processing, there is no more nested aggregate. |
fbf5a39b AC |
6037 | |
6038 | if Packed_Array_Aggregate_Handled (N) then | |
6039 | return; | |
6040 | end if; | |
6041 | ||
6042 | -- At this point we try to convert to positional form | |
70482933 | 6043 | |
c42006e9 | 6044 | Convert_To_Positional (N); |
70482933 | 6045 | |
23a9215f | 6046 | -- If the result is no longer an aggregate (e.g. it may be a string |
fbf5a39b AC |
6047 | -- literal, or a temporary which has the needed value), then we are |
6048 | -- done, since there is no longer a nested aggregate. | |
6049 | ||
70482933 RK |
6050 | if Nkind (N) /= N_Aggregate then |
6051 | return; | |
6052 | ||
5eeeed5e AC |
6053 | -- We are also done if the result is an analyzed aggregate, indicating |
6054 | -- that Convert_To_Positional succeeded and reanalyzed the rewritten | |
6055 | -- aggregate. | |
fbf5a39b | 6056 | |
dc67cfea | 6057 | elsif Analyzed (N) and then Is_Rewrite_Substitution (N) then |
70482933 RK |
6058 | return; |
6059 | end if; | |
6060 | ||
fa57ac97 ES |
6061 | -- If all aggregate components are compile-time known and the aggregate |
6062 | -- has been flattened, nothing left to do. The same occurs if the | |
b465ef6f | 6063 | -- aggregate is used to initialize the components of a statically |
fa57ac97 | 6064 | -- allocated dispatch table. |
0f95b178 | 6065 | |
fa57ac97 ES |
6066 | if Compile_Time_Known_Aggregate (N) |
6067 | or else Is_Static_Dispatch_Table_Aggregate (N) | |
6068 | then | |
0f95b178 JM |
6069 | Set_Expansion_Delayed (N, False); |
6070 | return; | |
6071 | end if; | |
6072 | ||
fbf5a39b AC |
6073 | -- Now see if back end processing is possible |
6074 | ||
70482933 RK |
6075 | if Backend_Processing_Possible (N) then |
6076 | ||
6077 | -- If the aggregate is static but the constraints are not, build | |
6078 | -- a static subtype for the aggregate, so that Gigi can place it | |
6079 | -- in static memory. Perform an unchecked_conversion to the non- | |
6080 | -- static type imposed by the context. | |
6081 | ||
6082 | declare | |
6083 | Itype : constant Entity_Id := Etype (N); | |
6084 | Index : Node_Id; | |
6085 | Needs_Type : Boolean := False; | |
6086 | ||
6087 | begin | |
6088 | Index := First_Index (Itype); | |
70482933 | 6089 | while Present (Index) loop |
edab6088 | 6090 | if not Is_OK_Static_Subtype (Etype (Index)) then |
70482933 RK |
6091 | Needs_Type := True; |
6092 | exit; | |
6093 | else | |
6094 | Next_Index (Index); | |
6095 | end if; | |
6096 | end loop; | |
6097 | ||
6098 | if Needs_Type then | |
6099 | Build_Constrained_Type (Positional => True); | |
6100 | Rewrite (N, Unchecked_Convert_To (Itype, N)); | |
6101 | Analyze (N); | |
6102 | end if; | |
6103 | end; | |
6104 | ||
6105 | return; | |
6106 | end if; | |
6107 | ||
3cf3e5c6 | 6108 | -- STEP 3 |
fbf5a39b | 6109 | |
5ed4ba15 AC |
6110 | -- Delay expansion for nested aggregates: it will be taken care of when |
6111 | -- the parent aggregate is expanded. | |
70482933 RK |
6112 | |
6113 | Parent_Node := Parent (N); | |
6114 | Parent_Kind := Nkind (Parent_Node); | |
6115 | ||
6116 | if Parent_Kind = N_Qualified_Expression then | |
6117 | Parent_Node := Parent (Parent_Node); | |
6118 | Parent_Kind := Nkind (Parent_Node); | |
6119 | end if; | |
6120 | ||
6121 | if Parent_Kind = N_Aggregate | |
6122 | or else Parent_Kind = N_Extension_Aggregate | |
6123 | or else Parent_Kind = N_Component_Association | |
6124 | or else (Parent_Kind = N_Object_Declaration | |
4844a259 | 6125 | and then (Needs_Finalization (Typ) |
ea588d41 | 6126 | or else Is_Special_Return_Object |
4844a259 | 6127 | (Defining_Identifier (Parent_Node)))) |
70482933 RK |
6128 | or else (Parent_Kind = N_Assignment_Statement |
6129 | and then Inside_Init_Proc) | |
6130 | then | |
2a1838cd EB |
6131 | Set_Expansion_Delayed (N, not Static_Array_Aggregate (N)); |
6132 | return; | |
70482933 RK |
6133 | end if; |
6134 | ||
3cf3e5c6 | 6135 | -- STEP 4 |
70482933 | 6136 | |
bc1146e5 | 6137 | -- Check whether in-place aggregate expansion is possible |
70482933 RK |
6138 | |
6139 | -- For object declarations we build the aggregate in place, unless | |
d2a60e59 | 6140 | -- the array is bit-packed. |
70482933 RK |
6141 | |
6142 | -- For assignments we do the assignment in place if all the component | |
d2a60e59 ES |
6143 | -- associations have compile-time known values, or are default- |
6144 | -- initialized limited components, e.g. tasks. For other cases we | |
f037632e BD |
6145 | -- create a temporary. A full analysis for safety of in-place assignment |
6146 | -- is delicate. | |
70482933 | 6147 | |
6f639c98 ES |
6148 | -- For allocators we assign to the designated object in place if the |
6149 | -- aggregate meets the same conditions as other in-place assignments. | |
6150 | -- In this case the aggregate may not come from source but was created | |
6151 | -- for default initialization, e.g. with Initialize_Scalars. | |
6152 | ||
70482933 | 6153 | if Requires_Transient_Scope (Typ) then |
6560f851 | 6154 | Establish_Transient_Scope (N, Manage_Sec_Stack => False); |
70482933 RK |
6155 | end if; |
6156 | ||
92a68a04 | 6157 | -- An array of limited components is built in place |
d2a60e59 ES |
6158 | |
6159 | if Is_Limited_Type (Typ) then | |
6160 | Maybe_In_Place_OK := True; | |
6161 | ||
6162 | elsif Has_Default_Init_Comps (N) then | |
c45b6ae0 | 6163 | Maybe_In_Place_OK := False; |
6f639c98 ES |
6164 | |
6165 | elsif Is_Bit_Packed_Array (Typ) | |
6166 | or else Has_Controlled_Component (Typ) | |
6167 | then | |
6168 | Maybe_In_Place_OK := False; | |
6169 | ||
a80b1eb7 | 6170 | elsif Parent_Kind = N_Assignment_Statement then |
c45b6ae0 | 6171 | Maybe_In_Place_OK := |
a80b1eb7 | 6172 | In_Place_Assign_OK (N, Get_Base_Object (Name (Parent_Node))); |
6f639c98 | 6173 | |
a80b1eb7 EB |
6174 | elsif Parent_Kind = N_Allocator then |
6175 | Maybe_In_Place_OK := In_Place_Assign_OK (N); | |
6176 | ||
6177 | else | |
6178 | Maybe_In_Place_OK := False; | |
c45b6ae0 | 6179 | end if; |
70482933 | 6180 | |
36c73552 AC |
6181 | -- If this is an array of tasks, it will be expanded into build-in-place |
6182 | -- assignments. Build an activation chain for the tasks now. | |
a38ff9b1 | 6183 | |
c8def50f | 6184 | if Has_Task (Typ) then |
a38ff9b1 ES |
6185 | Build_Activation_Chain_Entity (N); |
6186 | end if; | |
6187 | ||
cf6956bb | 6188 | -- Perform in-place expansion of aggregate in an object declaration. |
f3d42000 AC |
6189 | -- Note: actions generated for the aggregate will be captured in an |
6190 | -- expression-with-actions statement so that they can be transferred | |
6191 | -- to freeze actions later if there is an address clause for the | |
6192 | -- object. (Note: we don't use a block statement because this would | |
6193 | -- cause generated freeze nodes to be elaborated in the wrong scope). | |
cf6956bb | 6194 | |
d2a60e59 ES |
6195 | -- Arrays of limited components must be built in place. The code |
6196 | -- previously excluded controlled components but this is an old | |
6197 | -- oversight: the rules in 7.6 (17) are clear. | |
5ed4ba15 | 6198 | |
d4e4e88a | 6199 | if Comes_From_Source (Parent_Node) |
3386e3ae AC |
6200 | and then Parent_Kind = N_Object_Declaration |
6201 | and then Present (Expression (Parent_Node)) | |
6202 | and then not | |
6732c403 | 6203 | Must_Slide (N, Etype (Defining_Identifier (Parent_Node)), Typ) |
3386e3ae | 6204 | and then not Is_Bit_Packed_Array (Typ) |
70482933 | 6205 | then |
ac43e11e | 6206 | In_Place_Assign_OK_For_Declaration := True; |
5ed4ba15 AC |
6207 | Tmp := Defining_Identifier (Parent_Node); |
6208 | Set_No_Initialization (Parent_Node); | |
6209 | Set_Expression (Parent_Node, Empty); | |
70482933 | 6210 | |
ac43e11e AC |
6211 | -- Set kind and type of the entity, for use in the analysis |
6212 | -- of the subsequent assignments. If the nominal type is not | |
70482933 RK |
6213 | -- constrained, build a subtype from the known bounds of the |
6214 | -- aggregate. If the declaration has a subtype mark, use it, | |
6215 | -- otherwise use the itype of the aggregate. | |
6216 | ||
2e02ab86 | 6217 | Mutate_Ekind (Tmp, E_Variable); |
ac43e11e | 6218 | |
70482933 RK |
6219 | if not Is_Constrained (Typ) then |
6220 | Build_Constrained_Type (Positional => False); | |
ac43e11e | 6221 | |
5ed4ba15 AC |
6222 | elsif Is_Entity_Name (Object_Definition (Parent_Node)) |
6223 | and then Is_Constrained (Entity (Object_Definition (Parent_Node))) | |
70482933 | 6224 | then |
5ed4ba15 | 6225 | Set_Etype (Tmp, Entity (Object_Definition (Parent_Node))); |
ac43e11e | 6226 | |
70482933 RK |
6227 | else |
6228 | Set_Size_Known_At_Compile_Time (Typ, False); | |
6229 | Set_Etype (Tmp, Typ); | |
6230 | end if; | |
6231 | ||
a80b1eb7 | 6232 | elsif Maybe_In_Place_OK and then Parent_Kind = N_Allocator then |
6f639c98 ES |
6233 | Set_Expansion_Delayed (N); |
6234 | return; | |
6235 | ||
d2a60e59 ES |
6236 | -- Limited arrays in return statements are expanded when |
6237 | -- enclosing construct is expanded. | |
6238 | ||
6239 | elsif Maybe_In_Place_OK | |
a80b1eb7 | 6240 | and then Parent_Kind = N_Simple_Return_Statement |
d2a60e59 ES |
6241 | then |
6242 | Set_Expansion_Delayed (N); | |
6243 | return; | |
6244 | ||
6537318f ES |
6245 | -- In the remaining cases the aggregate appears in the RHS of an |
6246 | -- assignment, which may be part of the expansion of an object | |
41a7b948 | 6247 | -- declaration. If the aggregate is an actual in a call, itself |
6537318f | 6248 | -- possibly in a RHS, building it in the target is not possible. |
6f639c98 | 6249 | |
70482933 | 6250 | elsif Maybe_In_Place_OK |
6537318f | 6251 | and then Nkind (Parent_Node) not in N_Subprogram_Call |
a80b1eb7 | 6252 | and then Safe_Left_Hand_Side (Name (Parent_Node)) |
70482933 | 6253 | then |
a80b1eb7 | 6254 | Tmp := Name (Parent_Node); |
70482933 RK |
6255 | |
6256 | if Etype (Tmp) /= Etype (N) then | |
6257 | Apply_Length_Check (N, Etype (Tmp)); | |
fbf5a39b AC |
6258 | |
6259 | if Nkind (N) = N_Raise_Constraint_Error then | |
6260 | ||
6261 | -- Static error, nothing further to expand | |
6262 | ||
6263 | return; | |
6264 | end if; | |
70482933 RK |
6265 | end if; |
6266 | ||
36a66365 AC |
6267 | -- If a slice assignment has an aggregate with a single others_choice, |
6268 | -- the assignment can be done in place even if bounds are not static, | |
6269 | -- by converting it into a loop over the discrete range of the slice. | |
6270 | ||
70482933 | 6271 | elsif Maybe_In_Place_OK |
a80b1eb7 | 6272 | and then Nkind (Name (Parent_Node)) = N_Slice |
36a66365 | 6273 | and then Is_Others_Aggregate (N) |
70482933 | 6274 | then |
a80b1eb7 | 6275 | Tmp := Name (Parent_Node); |
70482933 | 6276 | |
36a66365 AC |
6277 | -- Set type of aggregate to be type of lhs in assignment, in order |
6278 | -- to suppress redundant length checks. | |
6279 | ||
6280 | Set_Etype (N, Etype (Tmp)); | |
70482933 | 6281 | |
fbf5a39b AC |
6282 | -- Step 5 |
6283 | ||
bc1146e5 | 6284 | -- In-place aggregate expansion is not possible |
fbf5a39b | 6285 | |
70482933 | 6286 | else |
07fc65c4 | 6287 | Maybe_In_Place_OK := False; |
faf387e1 | 6288 | Tmp := Make_Temporary (Loc, 'A', N); |
70482933 | 6289 | Tmp_Decl := |
bdc193ba AC |
6290 | Make_Object_Declaration (Loc, |
6291 | Defining_Identifier => Tmp, | |
6292 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
70482933 RK |
6293 | Set_No_Initialization (Tmp_Decl, True); |
6294 | ||
6295 | -- If we are within a loop, the temporary will be pushed on the | |
6560f851 HK |
6296 | -- stack at each iteration. If the aggregate is the expression |
6297 | -- for an allocator, it will be immediately copied to the heap | |
6298 | -- and can be reclaimed at once. We create a transient scope | |
6299 | -- around the aggregate for this purpose. | |
70482933 RK |
6300 | |
6301 | if Ekind (Current_Scope) = E_Loop | |
a80b1eb7 | 6302 | and then Parent_Kind = N_Allocator |
70482933 | 6303 | then |
6560f851 | 6304 | Establish_Transient_Scope (N, Manage_Sec_Stack => False); |
9f6cee82 EB |
6305 | |
6306 | -- If the parent is an assignment for which no controlled actions | |
6307 | -- should take place, prevent the temporary from being finalized. | |
6308 | ||
6309 | elsif Parent_Kind = N_Assignment_Statement | |
6310 | and then No_Ctrl_Actions (Parent_Node) | |
6311 | then | |
6312 | Mutate_Ekind (Tmp, E_Variable); | |
6313 | Set_Is_Ignored_Transient (Tmp); | |
70482933 RK |
6314 | end if; |
6315 | ||
6316 | Insert_Action (N, Tmp_Decl); | |
6317 | end if; | |
6318 | ||
36c73552 AC |
6319 | -- Construct and insert the aggregate code. We can safely suppress index |
6320 | -- checks because this code is guaranteed not to raise CE on index | |
6321 | -- checks. However we should *not* suppress all checks. | |
70482933 | 6322 | |
07fc65c4 GB |
6323 | declare |
6324 | Target : Node_Id; | |
6325 | ||
6326 | begin | |
6327 | if Nkind (Tmp) = N_Defining_Identifier then | |
e4494292 | 6328 | Target := New_Occurrence_Of (Tmp, Loc); |
07fc65c4 GB |
6329 | |
6330 | else | |
d2a60e59 ES |
6331 | if Has_Default_Init_Comps (N) |
6332 | and then not Maybe_In_Place_OK | |
6333 | then | |
0ab80019 | 6334 | -- Ada 2005 (AI-287): This case has not been analyzed??? |
c45b6ae0 | 6335 | |
9bc856dd | 6336 | raise Program_Error; |
c45b6ae0 AC |
6337 | end if; |
6338 | ||
0da2c8ac | 6339 | -- Name in assignment is explicit dereference |
07fc65c4 GB |
6340 | |
6341 | Target := New_Copy (Tmp); | |
6342 | end if; | |
6343 | ||
bc1146e5 | 6344 | -- If we are to generate an in-place assignment for a declaration or |
ac43e11e AC |
6345 | -- an assignment statement, and the assignment can be done directly |
6346 | -- by the back end, then do not expand further. | |
6347 | ||
bc1146e5 | 6348 | -- ??? We can also do that if in-place expansion is not possible but |
ac43e11e AC |
6349 | -- then we could go into an infinite recursion. |
6350 | ||
6351 | if (In_Place_Assign_OK_For_Declaration or else Maybe_In_Place_OK) | |
a1e1820b | 6352 | and then not CodePeer_Mode |
c63a2ad6 | 6353 | and then not Modify_Tree_For_C |
ac43e11e | 6354 | and then not Possible_Bit_Aligned_Component (Target) |
6b6bce61 | 6355 | and then not Is_Possibly_Unaligned_Slice (Target) |
ac43e11e AC |
6356 | and then Aggr_Assignment_OK_For_Backend (N) |
6357 | then | |
ac43e11e | 6358 | |
742084ad MP |
6359 | -- In the case of an assignment using an access with the |
6360 | -- Designated_Storage_Model aspect with a Copy_To procedure, | |
6361 | -- insert a temporary and have the back end handle the assignment | |
6362 | -- to it. Copy the result to the original target. | |
6363 | ||
6364 | if Parent_Kind = N_Assignment_Statement | |
6365 | and then Nkind (Name (Parent_Node)) = N_Explicit_Dereference | |
6366 | and then Has_Designated_Storage_Model_Aspect | |
6367 | (Etype (Prefix (Name (Parent_Node)))) | |
6368 | and then Present (Storage_Model_Copy_To | |
6369 | (Storage_Model_Object | |
6370 | (Etype (Prefix (Name (Parent_Node)))))) | |
6371 | then | |
ca4bff3a EB |
6372 | Aggr_Code := Build_Assignment_With_Temporary |
6373 | (Target, Typ, New_Copy_Tree (N)); | |
6374 | ||
742084ad MP |
6375 | else |
6376 | if Maybe_In_Place_OK then | |
6377 | return; | |
6378 | end if; | |
6379 | ||
ca4bff3a EB |
6380 | Aggr_Code := New_List ( |
6381 | Make_Assignment_Statement (Loc, | |
6382 | Name => Target, | |
6383 | Expression => New_Copy_Tree (N))); | |
742084ad | 6384 | end if; |
ca4bff3a | 6385 | |
d6e8719d | 6386 | else |
ac43e11e AC |
6387 | Aggr_Code := |
6388 | Build_Array_Aggr_Code (N, | |
6389 | Ctype => Ctyp, | |
6390 | Index => First_Index (Typ), | |
6391 | Into => Target, | |
6392 | Scalar_Comp => Is_Scalar_Type (Ctyp)); | |
6393 | end if; | |
4ac2bbbd AC |
6394 | |
6395 | -- Save the last assignment statement associated with the aggregate | |
6396 | -- when building a controlled object. This reference is utilized by | |
6397 | -- the finalization machinery when marking an object as successfully | |
6398 | -- initialized. | |
6399 | ||
6400 | if Needs_Finalization (Typ) | |
6401 | and then Is_Entity_Name (Target) | |
6402 | and then Present (Entity (Target)) | |
4a08c95c | 6403 | and then Ekind (Entity (Target)) in E_Constant | E_Variable |
4ac2bbbd AC |
6404 | then |
6405 | Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code)); | |
6406 | end if; | |
07fc65c4 | 6407 | end; |
70482933 | 6408 | |
6782b1ef AC |
6409 | -- If the aggregate is the expression in a declaration, the expanded |
6410 | -- code must be inserted after it. The defining entity might not come | |
6411 | -- from source if this is part of an inlined body, but the declaration | |
6412 | -- itself will. | |
fff7a6d9 | 6413 | -- The test below looks very specialized and kludgy??? |
6782b1ef AC |
6414 | |
6415 | if Comes_From_Source (Tmp) | |
6416 | or else | |
6417 | (Nkind (Parent (N)) = N_Object_Declaration | |
6418 | and then Comes_From_Source (Parent (N)) | |
6419 | and then Tmp = Defining_Entity (Parent (N))) | |
6420 | then | |
fff7a6d9 | 6421 | if Parent_Kind /= N_Object_Declaration or else Is_Frozen (Tmp) then |
cf6956bb | 6422 | Insert_Actions_After (Parent_Node, Aggr_Code); |
fff7a6d9 AC |
6423 | else |
6424 | declare | |
6425 | Comp_Stmt : constant Node_Id := | |
6426 | Make_Compound_Statement | |
6427 | (Sloc (Parent_Node), Actions => Aggr_Code); | |
6428 | begin | |
6429 | Insert_Action_After (Parent_Node, Comp_Stmt); | |
6430 | Set_Initialization_Statements (Tmp, Comp_Stmt); | |
6431 | end; | |
6432 | end if; | |
70482933 RK |
6433 | else |
6434 | Insert_Actions (N, Aggr_Code); | |
6435 | end if; | |
6436 | ||
07fc65c4 GB |
6437 | -- If the aggregate has been assigned in place, remove the original |
6438 | -- assignment. | |
6439 | ||
a80b1eb7 EB |
6440 | if Parent_Kind = N_Assignment_Statement and then Maybe_In_Place_OK then |
6441 | Rewrite (Parent_Node, Make_Null_Statement (Loc)); | |
70482933 | 6442 | |
a80b1eb7 EB |
6443 | -- Or else, if a temporary was created, replace the aggregate with it |
6444 | ||
6445 | elsif Parent_Kind /= N_Object_Declaration | |
6446 | or else Tmp /= Defining_Identifier (Parent_Node) | |
70482933 RK |
6447 | then |
6448 | Rewrite (N, New_Occurrence_Of (Tmp, Loc)); | |
6449 | Analyze_And_Resolve (N, Typ); | |
6450 | end if; | |
6451 | end Expand_Array_Aggregate; | |
6452 | ||
6453 | ------------------------ | |
6454 | -- Expand_N_Aggregate -- | |
6455 | ------------------------ | |
6456 | ||
6457 | procedure Expand_N_Aggregate (N : Node_Id) is | |
74580e1b | 6458 | T : constant Entity_Id := Etype (N); |
70482933 | 6459 | begin |
354c3840 AC |
6460 | -- Record aggregate case |
6461 | ||
74580e1b PT |
6462 | if Is_Record_Type (T) |
6463 | and then not Is_Private_Type (T) | |
fe3463cc | 6464 | then |
70482933 | 6465 | Expand_Record_Aggregate (N); |
354c3840 | 6466 | |
74580e1b | 6467 | elsif Has_Aspect (T, Aspect_Aggregate) then |
745f5698 ES |
6468 | Expand_Container_Aggregate (N); |
6469 | ||
354c3840 AC |
6470 | -- Array aggregate case |
6471 | ||
70482933 | 6472 | else |
354c3840 AC |
6473 | -- A special case, if we have a string subtype with bounds 1 .. N, |
6474 | -- where N is known at compile time, and the aggregate is of the | |
49eef89f AC |
6475 | -- form (others => 'x'), with a single choice and no expressions, |
6476 | -- and N is less than 80 (an arbitrary limit for now), then replace | |
6477 | -- the aggregate by the equivalent string literal (but do not mark | |
a90bd866 | 6478 | -- it as static since it is not). |
354c3840 AC |
6479 | |
6480 | -- Note: this entire circuit is redundant with respect to code in | |
6481 | -- Expand_Array_Aggregate that collapses others choices to positional | |
6482 | -- form, but there are two problems with that circuit: | |
6483 | ||
6484 | -- a) It is limited to very small cases due to ill-understood | |
b465ef6f | 6485 | -- interactions with bootstrapping. That limit is removed by |
354c3840 AC |
6486 | -- use of the No_Implicit_Loops restriction. |
6487 | ||
77a40ec1 | 6488 | -- b) It incorrectly ends up with the resulting expressions being |
354c3840 AC |
6489 | -- considered static when they are not. For example, the |
6490 | -- following test should fail: | |
6491 | ||
6492 | -- pragma Restrictions (No_Implicit_Loops); | |
6493 | -- package NonSOthers4 is | |
6494 | -- B : constant String (1 .. 6) := (others => 'A'); | |
6495 | -- DH : constant String (1 .. 8) := B & "BB"; | |
6496 | -- X : Integer; | |
6497 | -- pragma Export (C, X, Link_Name => DH); | |
6498 | -- end; | |
6499 | ||
6500 | -- But it succeeds (DH looks static to pragma Export) | |
6501 | ||
a90bd866 | 6502 | -- To be sorted out ??? |
354c3840 AC |
6503 | |
6504 | if Present (Component_Associations (N)) then | |
6505 | declare | |
6506 | CA : constant Node_Id := First (Component_Associations (N)); | |
6507 | MX : constant := 80; | |
6508 | ||
6509 | begin | |
10c257af ES |
6510 | if Present (CA) |
6511 | and then Nkind (First (Choice_List (CA))) = N_Others_Choice | |
354c3840 | 6512 | and then Nkind (Expression (CA)) = N_Character_Literal |
49eef89f | 6513 | and then No (Expressions (N)) |
354c3840 AC |
6514 | then |
6515 | declare | |
53f2aaf0 PT |
6516 | X : constant Node_Id := First_Index (T); |
6517 | EC : constant Node_Id := Expression (CA); | |
6518 | CV : constant Uint := Char_Literal_Value (EC); | |
6519 | CC : constant Char_Code := UI_To_CC (CV); | |
354c3840 AC |
6520 | |
6521 | begin | |
6522 | if Nkind (X) = N_Range | |
6523 | and then Compile_Time_Known_Value (Low_Bound (X)) | |
6524 | and then Expr_Value (Low_Bound (X)) = 1 | |
6525 | and then Compile_Time_Known_Value (High_Bound (X)) | |
6526 | then | |
6527 | declare | |
6528 | Hi : constant Uint := Expr_Value (High_Bound (X)); | |
6529 | ||
6530 | begin | |
6531 | if Hi <= MX then | |
6532 | Start_String; | |
6533 | ||
6534 | for J in 1 .. UI_To_Int (Hi) loop | |
53f2aaf0 | 6535 | Store_String_Char (CC); |
354c3840 AC |
6536 | end loop; |
6537 | ||
6538 | Rewrite (N, | |
6539 | Make_String_Literal (Sloc (N), | |
6540 | Strval => End_String)); | |
6541 | ||
53f2aaf0 PT |
6542 | if In_Character_Range (CC) then |
6543 | null; | |
6544 | elsif In_Wide_Character_Range (CC) then | |
354c3840 | 6545 | Set_Has_Wide_Character (N); |
53f2aaf0 PT |
6546 | else |
6547 | Set_Has_Wide_Wide_Character (N); | |
354c3840 AC |
6548 | end if; |
6549 | ||
6550 | Analyze_And_Resolve (N, T); | |
6551 | Set_Is_Static_Expression (N, False); | |
6552 | return; | |
6553 | end if; | |
6554 | end; | |
6555 | end if; | |
6556 | end; | |
6557 | end if; | |
6558 | end; | |
6559 | end if; | |
6560 | ||
6561 | -- Not that special case, so normal expansion of array aggregate | |
6562 | ||
70482933 RK |
6563 | Expand_Array_Aggregate (N); |
6564 | end if; | |
bdc193ba | 6565 | |
fbf5a39b AC |
6566 | exception |
6567 | when RE_Not_Available => | |
6568 | return; | |
70482933 RK |
6569 | end Expand_N_Aggregate; |
6570 | ||
745f5698 ES |
6571 | -------------------------------- |
6572 | -- Expand_Container_Aggregate -- | |
6573 | -------------------------------- | |
6574 | ||
6575 | procedure Expand_Container_Aggregate (N : Node_Id) is | |
ce59f39f GD |
6576 | Loc : constant Source_Ptr := Sloc (N); |
6577 | Typ : constant Entity_Id := Etype (N); | |
6578 | Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate); | |
745f5698 ES |
6579 | |
6580 | Empty_Subp : Node_Id := Empty; | |
6581 | Add_Named_Subp : Node_Id := Empty; | |
6582 | Add_Unnamed_Subp : Node_Id := Empty; | |
6583 | New_Indexed_Subp : Node_Id := Empty; | |
6584 | Assign_Indexed_Subp : Node_Id := Empty; | |
6585 | ||
ce59f39f GD |
6586 | Aggr_Code : constant List_Id := New_List; |
6587 | Temp : constant Entity_Id := Make_Temporary (Loc, 'C', N); | |
745f5698 | 6588 | |
4f6ebe2a | 6589 | Comp : Node_Id; |
745f5698 | 6590 | Decl : Node_Id; |
08c8883f | 6591 | Default : Node_Id; |
ce59f39f | 6592 | Init_Stat : Node_Id; |
08c8883f ES |
6593 | Siz : Int; |
6594 | ||
13112239 ES |
6595 | -- The following are used when the size of the aggregate is not |
6596 | -- static and requires a dynamic evaluation. | |
6597 | Siz_Decl : Node_Id; | |
6598 | Siz_Exp : Node_Id := Empty; | |
6599 | Count_Type : Entity_Id; | |
6600 | ||
08c8883f ES |
6601 | function Aggregate_Size return Int; |
6602 | -- Compute number of entries in aggregate, including choices | |
13112239 | 6603 | -- that cover a range or subtype, as well as iterated constructs. |
08c8883f | 6604 | -- Return -1 if the size is not known statically, in which case |
13112239 ES |
6605 | -- allocate a default size for the aggregate, or build an expression |
6606 | -- to estimate the size dynamically. | |
6607 | ||
6608 | function Build_Siz_Exp (Comp : Node_Id) return Int; | |
6609 | -- When the aggregate contains a single Iterated_Component_Association | |
6610 | -- or Element_Association with non-static bounds, build an expression | |
6611 | -- to be used as the allocated size of the container. This may be an | |
6612 | -- overestimate if a filter is present, but is a safe approximation. | |
fe43084c ES |
6613 | -- If bounds are dynamic the aggregate is created in two passes, and |
6614 | -- the first generates a loop for the sole purpose of computing the | |
41a7b948 | 6615 | -- number of elements that will be generated on the second pass. |
ce59f39f | 6616 | |
7a21651f ES |
6617 | procedure Expand_Iterated_Component (Comp : Node_Id); |
6618 | -- Handle iterated_component_association and iterated_Element | |
6619 | -- association by generating a loop over the specified range, | |
6620 | -- given either by a loop parameter specification or an iterator | |
6621 | -- specification. | |
6622 | ||
08c8883f ES |
6623 | -------------------- |
6624 | -- Aggregate_Size -- | |
6625 | -------------------- | |
6626 | ||
6627 | function Aggregate_Size return Int is | |
6628 | Comp : Node_Id; | |
6629 | Choice : Node_Id; | |
6630 | Lo, Hi : Node_Id; | |
ef7a3876 | 6631 | Siz : Int; |
08c8883f ES |
6632 | |
6633 | procedure Add_Range_Size; | |
13112239 ES |
6634 | -- Compute number of components specified by a component association |
6635 | -- given by a range or subtype name. | |
6636 | ||
6637 | -------------------- | |
6638 | -- Add_Range_Size -- | |
6639 | -------------------- | |
08c8883f ES |
6640 | |
6641 | procedure Add_Range_Size is | |
6642 | begin | |
13112239 ES |
6643 | -- The bounds of the discrete range are integers or enumeration |
6644 | -- literals | |
6645 | ||
08c8883f ES |
6646 | if Nkind (Lo) = N_Integer_Literal then |
6647 | Siz := Siz + UI_To_Int (Intval (Hi)) | |
13112239 ES |
6648 | - UI_To_Int (Intval (Lo)) + 1; |
6649 | else | |
6650 | Siz := Siz + UI_To_Int (Enumeration_Pos (Hi)) | |
6651 | - UI_To_Int (Enumeration_Pos (Lo)) + 1; | |
08c8883f ES |
6652 | end if; |
6653 | end Add_Range_Size; | |
6654 | ||
6655 | begin | |
ef7a3876 | 6656 | -- Aggregate is either all positional or all named |
13112239 | 6657 | |
ef7a3876 | 6658 | Siz := List_Length (Expressions (N)); |
08c8883f ES |
6659 | |
6660 | if Present (Component_Associations (N)) then | |
6661 | Comp := First (Component_Associations (N)); | |
13112239 ES |
6662 | -- If there is a single component association it can be |
6663 | -- an iterated component with dynamic bounds or an element | |
6664 | -- iterator over an iterable object. If it is an array | |
6665 | -- we can use the attribute Length to get its size; | |
6666 | -- for a predefined container the function Length plays | |
6667 | -- the same role. There is no available mechanism for | |
6668 | -- user-defined containers. For now we treat all of these | |
6669 | -- as dynamic. | |
6670 | ||
6671 | if List_Length (Component_Associations (N)) = 1 | |
6672 | and then Nkind (Comp) in N_Iterated_Component_Association | | |
6673 | N_Iterated_Element_Association | |
6674 | then | |
6675 | return Build_Siz_Exp (Comp); | |
daaf0179 ES |
6676 | end if; |
6677 | ||
13112239 ES |
6678 | -- Otherwise all associations must specify static sizes. |
6679 | ||
08c8883f ES |
6680 | while Present (Comp) loop |
6681 | Choice := First (Choice_List (Comp)); | |
6682 | ||
6683 | while Present (Choice) loop | |
6684 | Analyze (Choice); | |
6685 | ||
6686 | if Nkind (Choice) = N_Range then | |
6687 | Lo := Low_Bound (Choice); | |
6688 | Hi := High_Bound (Choice); | |
13112239 | 6689 | Add_Range_Size; |
08c8883f ES |
6690 | |
6691 | elsif Is_Entity_Name (Choice) | |
6692 | and then Is_Type (Entity (Choice)) | |
6693 | then | |
6694 | Lo := Type_Low_Bound (Entity (Choice)); | |
6695 | Hi := Type_High_Bound (Entity (Choice)); | |
13112239 | 6696 | Add_Range_Size; |
08c8883f ES |
6697 | |
6698 | Rewrite (Choice, | |
6699 | Make_Range (Loc, | |
6700 | New_Copy_Tree (Lo), | |
6701 | New_Copy_Tree (Hi))); | |
6702 | ||
6703 | else | |
6704 | -- Single choice (syntax excludes a subtype | |
6705 | -- indication). | |
6706 | ||
6707 | Siz := Siz + 1; | |
6708 | end if; | |
6709 | ||
6710 | Next (Choice); | |
6711 | end loop; | |
6712 | Next (Comp); | |
6713 | end loop; | |
6714 | end if; | |
6715 | ||
6716 | return Siz; | |
6717 | end Aggregate_Size; | |
6718 | ||
13112239 ES |
6719 | ------------------- |
6720 | -- Build_Siz_Exp -- | |
6721 | ------------------- | |
6722 | ||
6723 | function Build_Siz_Exp (Comp : Node_Id) return Int is | |
6724 | Lo, Hi : Node_Id; | |
6725 | begin | |
6726 | if Nkind (Comp) = N_Range then | |
6727 | Lo := Low_Bound (Comp); | |
6728 | Hi := High_Bound (Comp); | |
6729 | Analyze (Lo); | |
6730 | Analyze (Hi); | |
6731 | ||
6732 | -- Compute static size when possible. | |
6733 | ||
6734 | if Is_Static_Expression (Lo) | |
6735 | and then Is_Static_Expression (Hi) | |
6736 | then | |
6737 | if Nkind (Lo) = N_Integer_Literal then | |
6738 | Siz := UI_To_Int (Intval (Hi)) - UI_To_Int (Intval (Lo)) + 1; | |
6739 | else | |
6740 | Siz := UI_To_Int (Enumeration_Pos (Hi)) | |
6741 | - UI_To_Int (Enumeration_Pos (Lo)) + 1; | |
6742 | end if; | |
6743 | return Siz; | |
6744 | ||
6745 | else | |
6746 | Siz_Exp := | |
6747 | Make_Op_Add (Sloc (Comp), | |
6748 | Left_Opnd => | |
6749 | Make_Op_Subtract (Sloc (Comp), | |
6750 | Left_Opnd => New_Copy_Tree (Hi), | |
6751 | Right_Opnd => New_Copy_Tree (Lo)), | |
6752 | Right_Opnd => | |
6753 | Make_Integer_Literal (Loc, 1)); | |
6754 | return -1; | |
6755 | end if; | |
6756 | ||
6757 | elsif Nkind (Comp) = N_Iterated_Component_Association then | |
6758 | return Build_Siz_Exp (First (Discrete_Choices (Comp))); | |
6759 | ||
6760 | elsif Nkind (Comp) = N_Iterated_Element_Association then | |
fe43084c ES |
6761 | return -1; |
6762 | ||
8b1a5da3 | 6763 | -- ??? Need to create code for a loop and add to generated code, |
fe43084c ES |
6764 | -- as is done for array aggregates with iterated element |
6765 | -- associations, instead of using Append operations. | |
13112239 ES |
6766 | |
6767 | else | |
6768 | return -1; | |
6769 | end if; | |
6770 | end Build_Siz_Exp; | |
6771 | ||
4f6ebe2a ES |
6772 | ------------------------------- |
6773 | -- Expand_Iterated_Component -- | |
6774 | ------------------------------- | |
6775 | ||
6776 | procedure Expand_Iterated_Component (Comp : Node_Id) is | |
6777 | Expr : constant Node_Id := Expression (Comp); | |
4f6ebe2a | 6778 | |
c0bab60b ES |
6779 | Key_Expr : Node_Id := Empty; |
6780 | Loop_Id : Entity_Id; | |
4f6ebe2a ES |
6781 | L_Range : Node_Id; |
6782 | L_Iteration_Scheme : Node_Id; | |
6783 | Loop_Stat : Node_Id; | |
f3f1debe | 6784 | Params : List_Id; |
4f6ebe2a ES |
6785 | Stats : List_Id; |
6786 | ||
6787 | begin | |
c0bab60b ES |
6788 | if Nkind (Comp) = N_Iterated_Element_Association then |
6789 | Key_Expr := Key_Expression (Comp); | |
6790 | ||
6791 | -- We create a new entity as loop identifier in all cases, | |
6792 | -- as is done for generated loops elsewhere, as the loop | |
6793 | -- structure has been previously analyzed. | |
6794 | ||
6795 | if Present (Iterator_Specification (Comp)) then | |
6796 | ||
fe43084c | 6797 | -- Either an Iterator_Specification or a Loop_Parameter_ |
c0bab60b ES |
6798 | -- Specification is present. |
6799 | ||
6800 | L_Iteration_Scheme := | |
6801 | Make_Iteration_Scheme (Loc, | |
6802 | Iterator_Specification => Iterator_Specification (Comp)); | |
6803 | Loop_Id := | |
6804 | Make_Defining_Identifier (Loc, | |
6805 | Chars => Chars (Defining_Identifier | |
6806 | (Iterator_Specification (Comp)))); | |
6807 | Set_Defining_Identifier | |
6808 | (Iterator_Specification (L_Iteration_Scheme), Loop_Id); | |
6809 | ||
6810 | else | |
6811 | L_Iteration_Scheme := | |
6812 | Make_Iteration_Scheme (Loc, | |
6813 | Loop_Parameter_Specification => | |
6814 | Loop_Parameter_Specification (Comp)); | |
6815 | Loop_Id := | |
f3f1debe ES |
6816 | Make_Defining_Identifier (Loc, |
6817 | Chars => Chars (Defining_Identifier | |
6818 | (Loop_Parameter_Specification (Comp)))); | |
c0bab60b | 6819 | Set_Defining_Identifier |
f3f1debe ES |
6820 | (Loop_Parameter_Specification |
6821 | (L_Iteration_Scheme), Loop_Id); | |
c0bab60b | 6822 | end if; |
f3f1debe | 6823 | else |
c0bab60b | 6824 | |
f3f1debe | 6825 | -- Iterated_Component_Association. |
8092c199 | 6826 | |
f3f1debe | 6827 | if Present (Iterator_Specification (Comp)) then |
d983abeb PT |
6828 | Loop_Id := |
6829 | Make_Defining_Identifier (Loc, | |
6830 | Chars => Chars (Defining_Identifier | |
6831 | (Iterator_Specification (Comp)))); | |
f3f1debe ES |
6832 | L_Iteration_Scheme := |
6833 | Make_Iteration_Scheme (Loc, | |
6834 | Iterator_Specification => Iterator_Specification (Comp)); | |
6835 | ||
6836 | else | |
41a7b948 | 6837 | -- Loop_Parameter_Specification is parsed with a choice list. |
f3f1debe ES |
6838 | -- where the range is the first (and only) choice. |
6839 | ||
d983abeb PT |
6840 | Loop_Id := |
6841 | Make_Defining_Identifier (Loc, | |
6842 | Chars => Chars (Defining_Identifier (Comp))); | |
f3f1debe ES |
6843 | L_Range := Relocate_Node (First (Discrete_Choices (Comp))); |
6844 | ||
6845 | L_Iteration_Scheme := | |
6846 | Make_Iteration_Scheme (Loc, | |
6847 | Loop_Parameter_Specification => | |
6848 | Make_Loop_Parameter_Specification (Loc, | |
6849 | Defining_Identifier => Loop_Id, | |
6850 | Discrete_Subtype_Definition => L_Range)); | |
6851 | end if; | |
8092c199 | 6852 | end if; |
4f6ebe2a | 6853 | |
0b4034c0 GD |
6854 | -- Build insertion statement. For a positional aggregate, only the |
6855 | -- expression is needed. For a named aggregate, the loop variable, | |
6856 | -- whose type is that of the key, is an additional parameter for | |
6857 | -- the insertion operation. | |
c0bab60b ES |
6858 | -- If a Key_Expression is present, it serves as the additional |
6859 | -- parameter. Otherwise the key is given by the loop parameter | |
6860 | -- itself. | |
4f6ebe2a | 6861 | |
13112239 ES |
6862 | if Present (Add_Unnamed_Subp) |
6863 | and then No (Add_Named_Subp) | |
6864 | then | |
4f6ebe2a ES |
6865 | Stats := New_List |
6866 | (Make_Procedure_Call_Statement (Loc, | |
0b4034c0 GD |
6867 | Name => New_Occurrence_Of (Entity (Add_Unnamed_Subp), Loc), |
6868 | Parameter_Associations => | |
6869 | New_List (New_Occurrence_Of (Temp, Loc), | |
4f6ebe2a ES |
6870 | New_Copy_Tree (Expr)))); |
6871 | else | |
c0bab60b ES |
6872 | -- Named or indexed aggregate, for which a key is present, |
6873 | -- possibly with a specified key_expression. | |
6874 | ||
6875 | if Present (Key_Expr) then | |
f3f1debe ES |
6876 | Params := New_List (New_Occurrence_Of (Temp, Loc), |
6877 | New_Copy_Tree (Key_Expr), | |
6878 | New_Copy_Tree (Expr)); | |
c0bab60b | 6879 | else |
f3f1debe ES |
6880 | Params := New_List (New_Occurrence_Of (Temp, Loc), |
6881 | New_Occurrence_Of (Loop_Id, Loc), | |
6882 | New_Copy_Tree (Expr)); | |
c0bab60b | 6883 | end if; |
f3f1debe ES |
6884 | |
6885 | Stats := New_List | |
6886 | (Make_Procedure_Call_Statement (Loc, | |
6887 | Name => New_Occurrence_Of (Entity (Add_Named_Subp), Loc), | |
6888 | Parameter_Associations => Params)); | |
4f6ebe2a ES |
6889 | end if; |
6890 | ||
d43fbe01 PT |
6891 | Loop_Stat := Make_Implicit_Loop_Statement |
6892 | (Node => N, | |
6893 | Identifier => Empty, | |
6894 | Iteration_Scheme => L_Iteration_Scheme, | |
6895 | Statements => Stats); | |
4f6ebe2a | 6896 | Append (Loop_Stat, Aggr_Code); |
7a21651f | 6897 | |
4f6ebe2a ES |
6898 | end Expand_Iterated_Component; |
6899 | ||
00b21aa9 | 6900 | -- Start of processing for Expand_Container_Aggregate |
08c8883f | 6901 | |
745f5698 ES |
6902 | begin |
6903 | Parse_Aspect_Aggregate (Asp, | |
6904 | Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp, | |
6905 | New_Indexed_Subp, Assign_Indexed_Subp); | |
08c8883f ES |
6906 | |
6907 | -- The constructor for bounded containers is a function with | |
6908 | -- a parameter that sets the size of the container. If the | |
13112239 ES |
6909 | -- size cannot be determined statically we use a default value |
6910 | -- or a dynamic expression. | |
08c8883f ES |
6911 | |
6912 | Siz := Aggregate_Size; | |
7a21651f | 6913 | |
08c8883f ES |
6914 | if Ekind (Entity (Empty_Subp)) = E_Function |
6915 | and then Present (First_Formal (Entity (Empty_Subp))) | |
6916 | then | |
6917 | Default := Default_Value (First_Formal (Entity (Empty_Subp))); | |
13112239 ES |
6918 | |
6919 | -- If aggregate size is not static, we can use default value | |
6920 | -- of formal parameter for allocation. We assume that this | |
08c8883f | 6921 | -- (implementation-dependent) value is static, even though |
13112239 ES |
6922 | -- the AI does not require it. |
6923 | ||
6924 | -- Create declaration for size: a constant literal in the simple | |
6925 | -- case, an expression if iterated component associations may be | |
6926 | -- involved, the default otherwise. | |
08c8883f | 6927 | |
13112239 ES |
6928 | Count_Type := Etype (First_Formal (Entity (Empty_Subp))); |
6929 | if Siz = -1 then | |
6930 | if No (Siz_Exp) then | |
6931 | Siz := UI_To_Int (Intval (Default)); | |
6932 | Siz_Exp := Make_Integer_Literal (Loc, Siz); | |
6933 | ||
6934 | else | |
6935 | Siz_Exp := Make_Type_Conversion (Loc, | |
6936 | Subtype_Mark => | |
6937 | New_Occurrence_Of (Count_Type, Loc), | |
6938 | Expression => Siz_Exp); | |
6939 | end if; | |
6940 | ||
6941 | else | |
6942 | Siz_Exp := Make_Integer_Literal (Loc, Siz); | |
08c8883f ES |
6943 | end if; |
6944 | ||
13112239 ES |
6945 | Siz_Decl := Make_Object_Declaration (Loc, |
6946 | Defining_Identifier => Make_Temporary (Loc, 'S', N), | |
6947 | Object_Definition => | |
6948 | New_Occurrence_Of (Count_Type, Loc), | |
6949 | Expression => Siz_Exp); | |
6950 | Append (Siz_Decl, Aggr_Code); | |
6951 | ||
6952 | if Nkind (Siz_Exp) = N_Integer_Literal then | |
6953 | Init_Stat := | |
6954 | Make_Object_Declaration (Loc, | |
6955 | Defining_Identifier => Temp, | |
6956 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
6957 | Expression => Make_Function_Call (Loc, | |
6958 | Name => New_Occurrence_Of (Entity (Empty_Subp), Loc), | |
6959 | Parameter_Associations => | |
6960 | New_List | |
6961 | (New_Occurrence_Of | |
6962 | (Defining_Identifier (Siz_Decl), Loc)))); | |
6963 | ||
6964 | else | |
6965 | Init_Stat := | |
6966 | Make_Object_Declaration (Loc, | |
6967 | Defining_Identifier => Temp, | |
6968 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
6969 | Expression => Make_Function_Call (Loc, | |
6970 | Name => | |
6971 | New_Occurrence_Of (Entity (New_Indexed_Subp), Loc), | |
6972 | Parameter_Associations => | |
6973 | New_List ( | |
6974 | Make_Integer_Literal (Loc, 1), | |
6975 | New_Occurrence_Of | |
6976 | (Defining_Identifier (Siz_Decl), Loc)))); | |
6977 | end if; | |
08c8883f ES |
6978 | |
6979 | Append (Init_Stat, Aggr_Code); | |
6980 | ||
13112239 ES |
6981 | -- Size is dynamic: Create declaration for object, and intitialize |
6982 | -- with a call to the null container, or an assignment to it. | |
08c8883f ES |
6983 | |
6984 | else | |
6985 | Decl := | |
6986 | Make_Object_Declaration (Loc, | |
6987 | Defining_Identifier => Temp, | |
6988 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
6989 | ||
6990 | Insert_Action (N, Decl); | |
13112239 ES |
6991 | |
6992 | -- The Empty entity is either a parameterless function, or | |
6993 | -- a constant. | |
6994 | ||
08c8883f ES |
6995 | if Ekind (Entity (Empty_Subp)) = E_Function then |
6996 | Init_Stat := Make_Assignment_Statement (Loc, | |
6997 | Name => New_Occurrence_Of (Temp, Loc), | |
6998 | Expression => Make_Function_Call (Loc, | |
6999 | Name => New_Occurrence_Of (Entity (Empty_Subp), Loc))); | |
13112239 | 7000 | |
08c8883f ES |
7001 | else |
7002 | Init_Stat := Make_Assignment_Statement (Loc, | |
7003 | Name => New_Occurrence_Of (Temp, Loc), | |
7004 | Expression => New_Occurrence_Of (Entity (Empty_Subp), Loc)); | |
7005 | end if; | |
7006 | ||
7007 | Append (Init_Stat, Aggr_Code); | |
7008 | end if; | |
745f5698 | 7009 | |
7a21651f ES |
7010 | --------------------------- |
7011 | -- Positional aggregate -- | |
7012 | --------------------------- | |
745f5698 | 7013 | |
08c8883f ES |
7014 | -- If the aggregate is positional the aspect must include |
7015 | -- an Add_Unnamed subprogram. | |
7016 | ||
13112239 | 7017 | if Present (Add_Unnamed_Subp) then |
4f6ebe2a ES |
7018 | if Present (Expressions (N)) then |
7019 | declare | |
7020 | Insert : constant Entity_Id := Entity (Add_Unnamed_Subp); | |
7021 | Comp : Node_Id; | |
7022 | Stat : Node_Id; | |
7023 | ||
7024 | begin | |
7025 | Comp := First (Expressions (N)); | |
7026 | while Present (Comp) loop | |
7027 | Stat := Make_Procedure_Call_Statement (Loc, | |
7028 | Name => New_Occurrence_Of (Insert, Loc), | |
7029 | Parameter_Associations => | |
7030 | New_List (New_Occurrence_Of (Temp, Loc), | |
0b4034c0 | 7031 | New_Copy_Tree (Comp))); |
4f6ebe2a ES |
7032 | Append (Stat, Aggr_Code); |
7033 | Next (Comp); | |
7034 | end loop; | |
7035 | end; | |
7036 | end if; | |
7037 | ||
13112239 ES |
7038 | -- Indexed aggregates are handled below. Unnamed aggregates |
7039 | -- such as sets may include iterated component associations. | |
4f6ebe2a | 7040 | |
13112239 ES |
7041 | if No (New_Indexed_Subp) then |
7042 | Comp := First (Component_Associations (N)); | |
7043 | while Present (Comp) loop | |
7044 | if Nkind (Comp) = N_Iterated_Component_Association then | |
7045 | Expand_Iterated_Component (Comp); | |
7046 | end if; | |
7047 | Next (Comp); | |
7048 | end loop; | |
7049 | end if; | |
4f6ebe2a | 7050 | |
7a21651f ES |
7051 | --------------------- |
7052 | -- Named_Aggregate -- | |
7053 | --------------------- | |
7054 | ||
4f6ebe2a | 7055 | elsif Present (Add_Named_Subp) then |
ce59f39f | 7056 | declare |
4f6ebe2a | 7057 | Insert : constant Entity_Id := Entity (Add_Named_Subp); |
ce59f39f | 7058 | Stat : Node_Id; |
4f6ebe2a | 7059 | Key : Node_Id; |
ce59f39f | 7060 | begin |
4f6ebe2a ES |
7061 | Comp := First (Component_Associations (N)); |
7062 | ||
0b4034c0 | 7063 | -- Each component association may contain several choices; |
4f6ebe2a ES |
7064 | -- generate an insertion statement for each. |
7065 | ||
ce59f39f | 7066 | while Present (Comp) loop |
c0bab60b ES |
7067 | if Nkind (Comp) in N_Iterated_Component_Association |
7068 | | N_Iterated_Element_Association | |
7069 | then | |
4f6ebe2a ES |
7070 | Expand_Iterated_Component (Comp); |
7071 | else | |
7072 | Key := First (Choices (Comp)); | |
7073 | ||
7074 | while Present (Key) loop | |
7075 | Stat := Make_Procedure_Call_Statement (Loc, | |
7076 | Name => New_Occurrence_Of (Insert, Loc), | |
7077 | Parameter_Associations => | |
7078 | New_List (New_Occurrence_Of (Temp, Loc), | |
0b4034c0 GD |
7079 | New_Copy_Tree (Key), |
7080 | New_Copy_Tree (Expression (Comp)))); | |
4f6ebe2a ES |
7081 | Append (Stat, Aggr_Code); |
7082 | ||
7083 | Next (Key); | |
7084 | end loop; | |
7085 | end if; | |
7086 | ||
ce59f39f GD |
7087 | Next (Comp); |
7088 | end loop; | |
7089 | end; | |
08c8883f | 7090 | end if; |
7a21651f ES |
7091 | |
7092 | ----------------------- | |
7093 | -- Indexed_Aggregate -- | |
7094 | ----------------------- | |
7095 | ||
08c8883f ES |
7096 | -- For an indexed aggregate there must be an Assigned_Indexeed |
7097 | -- subprogram. Note that unlike array aggregates, a container | |
7098 | -- aggregate must be fully positional or fully indexed. In the | |
7099 | -- first case the expansion has already taken place. | |
13112239 ES |
7100 | -- TBA: the keys for an indexed aggregate must provide a dense |
7101 | -- range with no repetitions. | |
08c8883f ES |
7102 | |
7103 | if Present (Assign_Indexed_Subp) | |
7104 | and then Present (Component_Associations (N)) | |
7105 | then | |
7a21651f ES |
7106 | declare |
7107 | Insert : constant Entity_Id := Entity (Assign_Indexed_Subp); | |
7108 | Index_Type : constant Entity_Id := | |
7109 | Etype (Next_Formal (First_Formal (Insert))); | |
7110 | ||
7a21651f ES |
7111 | function Expand_Range_Component |
7112 | (Rng : Node_Id; | |
7113 | Expr : Node_Id) return Node_Id; | |
7114 | -- Transform a component assoication with a range into an | |
7115 | -- explicit loop. If the choice is a subtype name, it is | |
7116 | -- rewritten as a range with the corresponding bounds, which | |
7117 | -- are known to be static. | |
7118 | ||
7119 | Comp : Node_Id; | |
7120 | Index : Node_Id; | |
7121 | Pos : Int := 0; | |
7122 | Stat : Node_Id; | |
7123 | Key : Node_Id; | |
7a21651f ES |
7124 | |
7125 | ----------------------------- | |
7126 | -- Expand_Raange_Component -- | |
7127 | ----------------------------- | |
7128 | ||
7129 | function Expand_Range_Component | |
7130 | (Rng : Node_Id; | |
7131 | Expr : Node_Id) return Node_Id | |
7132 | is | |
7133 | Loop_Id : constant Entity_Id := | |
7134 | Make_Temporary (Loc, 'T'); | |
7135 | ||
7136 | L_Iteration_Scheme : Node_Id; | |
7137 | Stats : List_Id; | |
7138 | ||
7139 | begin | |
7140 | L_Iteration_Scheme := | |
7141 | Make_Iteration_Scheme (Loc, | |
7142 | Loop_Parameter_Specification => | |
7143 | Make_Loop_Parameter_Specification (Loc, | |
7144 | Defining_Identifier => Loop_Id, | |
7145 | Discrete_Subtype_Definition => New_Copy_Tree (Rng))); | |
7146 | ||
7147 | Stats := New_List | |
7148 | (Make_Procedure_Call_Statement (Loc, | |
7149 | Name => | |
7150 | New_Occurrence_Of (Entity (Assign_Indexed_Subp), Loc), | |
7151 | Parameter_Associations => | |
7152 | New_List (New_Occurrence_Of (Temp, Loc), | |
7153 | New_Occurrence_Of (Loop_Id, Loc), | |
7154 | New_Copy_Tree (Expr)))); | |
7155 | ||
7156 | return Make_Implicit_Loop_Statement | |
7157 | (Node => N, | |
7158 | Identifier => Empty, | |
7159 | Iteration_Scheme => L_Iteration_Scheme, | |
7160 | Statements => Stats); | |
7161 | end Expand_Range_Component; | |
7162 | ||
7a21651f | 7163 | begin |
08c8883f | 7164 | if Siz > 0 then |
7a21651f ES |
7165 | |
7166 | -- Modify the call to the constructor to allocate the | |
7167 | -- required size for the aggregwte : call the provided | |
7168 | -- constructor rather than the Empty aggregate. | |
7169 | ||
d43fbe01 | 7170 | Index := Make_Op_Add (Loc, |
7a21651f | 7171 | Left_Opnd => New_Copy_Tree (Type_Low_Bound (Index_Type)), |
08c8883f | 7172 | Right_Opnd => Make_Integer_Literal (Loc, Siz - 1)); |
7a21651f ES |
7173 | |
7174 | Set_Expression (Init_Stat, | |
7175 | Make_Function_Call (Loc, | |
7176 | Name => | |
7177 | New_Occurrence_Of (Entity (New_Indexed_Subp), Loc), | |
7178 | Parameter_Associations => | |
7179 | New_List ( | |
7180 | New_Copy_Tree (Type_Low_Bound (Index_Type)), | |
7181 | Index))); | |
7182 | end if; | |
7183 | ||
7184 | if Present (Expressions (N)) then | |
7185 | Comp := First (Expressions (N)); | |
7186 | ||
7187 | while Present (Comp) loop | |
7188 | ||
7189 | -- Compute index position for successive components | |
7190 | -- in the list of expressions, and use the indexed | |
7191 | -- assignment procedure for each. | |
7192 | ||
7193 | Index := Make_Op_Add (Loc, | |
7194 | Left_Opnd => Type_Low_Bound (Index_Type), | |
7195 | Right_Opnd => Make_Integer_Literal (Loc, Pos)); | |
7196 | ||
7197 | Stat := Make_Procedure_Call_Statement (Loc, | |
7198 | Name => New_Occurrence_Of (Insert, Loc), | |
7199 | Parameter_Associations => | |
7200 | New_List (New_Occurrence_Of (Temp, Loc), | |
7201 | Index, | |
7202 | New_Copy_Tree (Comp))); | |
7203 | ||
7204 | Pos := Pos + 1; | |
7205 | ||
7206 | Append (Stat, Aggr_Code); | |
7207 | Next (Comp); | |
7208 | end loop; | |
7209 | end if; | |
7210 | ||
7211 | if Present (Component_Associations (N)) then | |
7212 | Comp := First (Component_Associations (N)); | |
7213 | ||
7214 | -- The choice may be a static value, or a range with | |
7215 | -- static bounds. | |
7216 | ||
7217 | while Present (Comp) loop | |
7218 | if Nkind (Comp) = N_Component_Association then | |
7219 | Key := First (Choices (Comp)); | |
7220 | while Present (Key) loop | |
7221 | ||
7222 | -- If the expression is a box, the corresponding | |
7223 | -- component (s) is left uninitialized. | |
7224 | ||
7225 | if Box_Present (Comp) then | |
7226 | goto Next_Key; | |
7227 | ||
7228 | elsif Nkind (Key) = N_Range then | |
7229 | ||
7230 | -- Create loop for tne specified range, | |
7231 | -- with copies of the expression. | |
7232 | ||
7233 | Stat := | |
7234 | Expand_Range_Component (Key, Expression (Comp)); | |
7235 | ||
7236 | else | |
7237 | Stat := Make_Procedure_Call_Statement (Loc, | |
7238 | Name => New_Occurrence_Of | |
7239 | (Entity (Assign_Indexed_Subp), Loc), | |
7240 | Parameter_Associations => | |
7241 | New_List (New_Occurrence_Of (Temp, Loc), | |
7242 | New_Copy_Tree (Key), | |
7243 | New_Copy_Tree (Expression (Comp)))); | |
7244 | end if; | |
7245 | ||
7246 | Append (Stat, Aggr_Code); | |
7247 | ||
7248 | <<Next_Key>> | |
7249 | Next (Key); | |
7250 | end loop; | |
08c8883f | 7251 | |
7a21651f | 7252 | else |
08c8883f ES |
7253 | -- Iterated component association. Discard |
7254 | -- positional insertion procedure. | |
7255 | ||
7256 | Add_Named_Subp := Assign_Indexed_Subp; | |
7257 | Add_Unnamed_Subp := Empty; | |
7258 | Expand_Iterated_Component (Comp); | |
7a21651f | 7259 | end if; |
08c8883f | 7260 | |
7a21651f ES |
7261 | Next (Comp); |
7262 | end loop; | |
7263 | end if; | |
7264 | end; | |
ce59f39f | 7265 | end if; |
4f6ebe2a | 7266 | |
ce59f39f GD |
7267 | Insert_Actions (N, Aggr_Code); |
7268 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
7269 | Analyze_And_Resolve (N, Typ); | |
745f5698 ES |
7270 | end Expand_Container_Aggregate; |
7271 | ||
9eb8d5b4 AC |
7272 | ------------------------------ |
7273 | -- Expand_N_Delta_Aggregate -- | |
7274 | ------------------------------ | |
7275 | ||
7276 | procedure Expand_N_Delta_Aggregate (N : Node_Id) is | |
9313a26a | 7277 | Loc : constant Source_Ptr := Sloc (N); |
c78efe92 | 7278 | Typ : constant Entity_Id := Etype (Expression (N)); |
9eb8d5b4 AC |
7279 | Decl : Node_Id; |
7280 | ||
7281 | begin | |
9313a26a AC |
7282 | Decl := |
7283 | Make_Object_Declaration (Loc, | |
7284 | Defining_Identifier => Make_Temporary (Loc, 'T'), | |
7285 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
7286 | Expression => New_Copy_Tree (Expression (N))); | |
9eb8d5b4 AC |
7287 | |
7288 | if Is_Array_Type (Etype (N)) then | |
7289 | Expand_Delta_Array_Aggregate (N, New_List (Decl)); | |
7290 | else | |
7291 | Expand_Delta_Record_Aggregate (N, New_List (Decl)); | |
7292 | end if; | |
7293 | end Expand_N_Delta_Aggregate; | |
7294 | ||
7295 | ---------------------------------- | |
7296 | -- Expand_Delta_Array_Aggregate -- | |
7297 | ---------------------------------- | |
7298 | ||
7299 | procedure Expand_Delta_Array_Aggregate (N : Node_Id; Deltas : List_Id) is | |
9313a26a AC |
7300 | Loc : constant Source_Ptr := Sloc (N); |
7301 | Temp : constant Entity_Id := Defining_Identifier (First (Deltas)); | |
7302 | Assoc : Node_Id; | |
7303 | ||
9eb8d5b4 AC |
7304 | function Generate_Loop (C : Node_Id) return Node_Id; |
7305 | -- Generate a loop containing individual component assignments for | |
7306 | -- choices that are ranges, subtype indications, subtype names, and | |
7307 | -- iterated component associations. | |
7308 | ||
9313a26a AC |
7309 | ------------------- |
7310 | -- Generate_Loop -- | |
7311 | ------------------- | |
7312 | ||
9eb8d5b4 AC |
7313 | function Generate_Loop (C : Node_Id) return Node_Id is |
7314 | Sl : constant Source_Ptr := Sloc (C); | |
7315 | Ix : Entity_Id; | |
7316 | ||
7317 | begin | |
7318 | if Nkind (Parent (C)) = N_Iterated_Component_Association then | |
7319 | Ix := | |
7320 | Make_Defining_Identifier (Loc, | |
7321 | Chars => (Chars (Defining_Identifier (Parent (C))))); | |
7322 | else | |
7323 | Ix := Make_Temporary (Sl, 'I'); | |
7324 | end if; | |
7325 | ||
7326 | return | |
f3561c06 | 7327 | Make_Implicit_Loop_Statement (C, |
9313a26a AC |
7328 | Iteration_Scheme => |
7329 | Make_Iteration_Scheme (Sl, | |
7330 | Loop_Parameter_Specification => | |
7331 | Make_Loop_Parameter_Specification (Sl, | |
7332 | Defining_Identifier => Ix, | |
7333 | Discrete_Subtype_Definition => New_Copy_Tree (C))), | |
7334 | ||
7335 | Statements => New_List ( | |
7336 | Make_Assignment_Statement (Sl, | |
7337 | Name => | |
7338 | Make_Indexed_Component (Sl, | |
9eb8d5b4 AC |
7339 | Prefix => New_Occurrence_Of (Temp, Sl), |
7340 | Expressions => New_List (New_Occurrence_Of (Ix, Sl))), | |
9313a26a AC |
7341 | Expression => New_Copy_Tree (Expression (Assoc)))), |
7342 | End_Label => Empty); | |
9eb8d5b4 AC |
7343 | end Generate_Loop; |
7344 | ||
9313a26a AC |
7345 | -- Local variables |
7346 | ||
7347 | Choice : Node_Id; | |
7348 | ||
7349 | -- Start of processing for Expand_Delta_Array_Aggregate | |
7350 | ||
9eb8d5b4 AC |
7351 | begin |
7352 | Assoc := First (Component_Associations (N)); | |
7353 | while Present (Assoc) loop | |
7354 | Choice := First (Choice_List (Assoc)); | |
7355 | if Nkind (Assoc) = N_Iterated_Component_Association then | |
7356 | while Present (Choice) loop | |
7357 | Append_To (Deltas, Generate_Loop (Choice)); | |
7358 | Next (Choice); | |
7359 | end loop; | |
7360 | ||
7361 | else | |
7362 | while Present (Choice) loop | |
7363 | ||
7364 | -- Choice can be given by a range, a subtype indication, a | |
7365 | -- subtype name, a scalar value, or an entity. | |
7366 | ||
7367 | if Nkind (Choice) = N_Range | |
7368 | or else (Is_Entity_Name (Choice) | |
9313a26a | 7369 | and then Is_Type (Entity (Choice))) |
9eb8d5b4 AC |
7370 | then |
7371 | Append_To (Deltas, Generate_Loop (Choice)); | |
7372 | ||
7373 | elsif Nkind (Choice) = N_Subtype_Indication then | |
7374 | Append_To (Deltas, | |
7375 | Generate_Loop (Range_Expression (Constraint (Choice)))); | |
7376 | ||
7377 | else | |
7378 | Append_To (Deltas, | |
9313a26a AC |
7379 | Make_Assignment_Statement (Sloc (Choice), |
7380 | Name => | |
7381 | Make_Indexed_Component (Sloc (Choice), | |
7382 | Prefix => New_Occurrence_Of (Temp, Loc), | |
7383 | Expressions => New_List (New_Copy_Tree (Choice))), | |
7384 | Expression => New_Copy_Tree (Expression (Assoc)))); | |
9eb8d5b4 AC |
7385 | end if; |
7386 | ||
7387 | Next (Choice); | |
7388 | end loop; | |
7389 | end if; | |
7390 | ||
7391 | Next (Assoc); | |
7392 | end loop; | |
7393 | ||
7394 | Insert_Actions (N, Deltas); | |
7395 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
7396 | end Expand_Delta_Array_Aggregate; | |
7397 | ||
7398 | ----------------------------------- | |
7399 | -- Expand_Delta_Record_Aggregate -- | |
7400 | ----------------------------------- | |
7401 | ||
7402 | procedure Expand_Delta_Record_Aggregate (N : Node_Id; Deltas : List_Id) is | |
7403 | Loc : constant Source_Ptr := Sloc (N); | |
7404 | Temp : constant Entity_Id := Defining_Identifier (First (Deltas)); | |
7405 | Assoc : Node_Id; | |
7406 | Choice : Node_Id; | |
7407 | ||
7408 | begin | |
7409 | Assoc := First (Component_Associations (N)); | |
7410 | ||
7411 | while Present (Assoc) loop | |
7412 | Choice := First (Choice_List (Assoc)); | |
7413 | while Present (Choice) loop | |
7414 | Append_To (Deltas, | |
9313a26a AC |
7415 | Make_Assignment_Statement (Sloc (Choice), |
7416 | Name => | |
7417 | Make_Selected_Component (Sloc (Choice), | |
7418 | Prefix => New_Occurrence_Of (Temp, Loc), | |
7419 | Selector_Name => Make_Identifier (Loc, Chars (Choice))), | |
7420 | Expression => New_Copy_Tree (Expression (Assoc)))); | |
9eb8d5b4 AC |
7421 | Next (Choice); |
7422 | end loop; | |
7423 | ||
7424 | Next (Assoc); | |
7425 | end loop; | |
7426 | ||
7427 | Insert_Actions (N, Deltas); | |
7428 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
7429 | end Expand_Delta_Record_Aggregate; | |
7430 | ||
70482933 RK |
7431 | ---------------------------------- |
7432 | -- Expand_N_Extension_Aggregate -- | |
7433 | ---------------------------------- | |
7434 | ||
7435 | -- If the ancestor part is an expression, add a component association for | |
7436 | -- the parent field. If the type of the ancestor part is not the direct | |
d4dfb005 BD |
7437 | -- parent of the expected type, build recursively the needed ancestors. |
7438 | -- If the ancestor part is a subtype_mark, replace aggregate with a | |
7439 | -- declaration for a temporary of the expected type, followed by | |
7440 | -- individual assignments to the given components. | |
70482933 RK |
7441 | |
7442 | procedure Expand_N_Extension_Aggregate (N : Node_Id) is | |
70482933 | 7443 | A : constant Node_Id := Ancestor_Part (N); |
3fc40cd7 | 7444 | Loc : constant Source_Ptr := Sloc (N); |
70482933 RK |
7445 | Typ : constant Entity_Id := Etype (N); |
7446 | ||
7447 | begin | |
fbf5a39b | 7448 | -- If the ancestor is a subtype mark, an init proc must be called |
70482933 RK |
7449 | -- on the resulting object which thus has to be materialized in |
7450 | -- the front-end | |
7451 | ||
7452 | if Is_Entity_Name (A) and then Is_Type (Entity (A)) then | |
7453 | Convert_To_Assignments (N, Typ); | |
7454 | ||
7455 | -- The extension aggregate is transformed into a record aggregate | |
7456 | -- of the following form (c1 and c2 are inherited components) | |
7457 | ||
7458 | -- (Exp with c3 => a, c4 => b) | |
0877856b | 7459 | -- ==> (c1 => Exp.c1, c2 => Exp.c2, c3 => a, c4 => b) |
70482933 RK |
7460 | |
7461 | else | |
7462 | Set_Etype (N, Typ); | |
7463 | ||
1f110335 | 7464 | if Tagged_Type_Expansion then |
70482933 | 7465 | Expand_Record_Aggregate (N, |
a9d8907c JM |
7466 | Orig_Tag => |
7467 | New_Occurrence_Of | |
7468 | (Node (First_Elmt (Access_Disp_Table (Typ))), Loc), | |
70482933 | 7469 | Parent_Expr => A); |
5c34e9cd AC |
7470 | |
7471 | -- No tag is needed in the case of a VM | |
7472 | ||
0f95b178 | 7473 | else |
5c34e9cd | 7474 | Expand_Record_Aggregate (N, Parent_Expr => A); |
70482933 RK |
7475 | end if; |
7476 | end if; | |
fbf5a39b AC |
7477 | |
7478 | exception | |
7479 | when RE_Not_Available => | |
7480 | return; | |
70482933 RK |
7481 | end Expand_N_Extension_Aggregate; |
7482 | ||
7483 | ----------------------------- | |
7484 | -- Expand_Record_Aggregate -- | |
7485 | ----------------------------- | |
7486 | ||
7487 | procedure Expand_Record_Aggregate | |
7488 | (N : Node_Id; | |
7489 | Orig_Tag : Node_Id := Empty; | |
7490 | Parent_Expr : Node_Id := Empty) | |
7491 | is | |
fbf5a39b AC |
7492 | Loc : constant Source_Ptr := Sloc (N); |
7493 | Comps : constant List_Id := Component_Associations (N); | |
7494 | Typ : constant Entity_Id := Etype (N); | |
7495 | Base_Typ : constant Entity_Id := Base_Type (Typ); | |
70482933 | 7496 | |
0f95b178 JM |
7497 | Static_Components : Boolean := True; |
7498 | -- Flag to indicate whether all components are compile-time known, | |
7499 | -- and the aggregate can be constructed statically and handled by | |
d4dfb005 | 7500 | -- the back-end. Set to False by Component_OK_For_Backend. |
70482933 | 7501 | |
54740d7d AC |
7502 | procedure Build_Back_End_Aggregate; |
7503 | -- Build a proper aggregate to be handled by the back-end | |
7504 | ||
f6205414 AC |
7505 | function Compile_Time_Known_Composite_Value (N : Node_Id) return Boolean; |
7506 | -- Returns true if N is an expression of composite type which can be | |
7507 | -- fully evaluated at compile time without raising constraint error. | |
7508 | -- Such expressions can be passed as is to Gigi without any expansion. | |
7509 | -- | |
7510 | -- This returns true for N_Aggregate with Compile_Time_Known_Aggregate | |
7511 | -- set and constants whose expression is such an aggregate, recursively. | |
7512 | ||
d4dfb005 | 7513 | function Component_OK_For_Backend return Boolean; |
b465ef6f | 7514 | -- Check for presence of a component which makes it impossible for the |
0f95b178 JM |
7515 | -- backend to process the aggregate, thus requiring the use of a series |
7516 | -- of assignment statements. Cases checked for are a nested aggregate | |
7517 | -- needing Late_Expansion, the presence of a tagged component which may | |
7518 | -- need tag adjustment, and a bit unaligned component reference. | |
4a76b687 ES |
7519 | -- |
7520 | -- We also force expansion into assignments if a component is of a | |
7521 | -- mutable type (including a private type with discriminants) because | |
7522 | -- in that case the size of the component to be copied may be smaller | |
7523 | -- than the side of the target, and there is no simple way for gigi | |
7524 | -- to compute the size of the object to be copied. | |
7525 | -- | |
7526 | -- NOTE: This is part of the ongoing work to define precisely the | |
7527 | -- interface between front-end and back-end handling of aggregates. | |
7528 | -- In general it is desirable to pass aggregates as they are to gigi, | |
7529 | -- in order to minimize elaboration code. This is one case where the | |
7530 | -- semantics of Ada complicate the analysis and lead to anomalies in | |
7531 | -- the gcc back-end if the aggregate is not expanded into assignments. | |
d4dfb005 BD |
7532 | -- |
7533 | -- NOTE: This sets the global Static_Components to False in most, but | |
7534 | -- not all, cases when it returns False. | |
70482933 | 7535 | |
9b7924dd AC |
7536 | function Has_Per_Object_Constraint (L : List_Id) return Boolean; |
7537 | -- Return True if any element of L has Has_Per_Object_Constraint set. | |
7538 | -- L should be the Choices component of an N_Component_Association. | |
7539 | ||
57a8057a AC |
7540 | function Has_Visible_Private_Ancestor (Id : E) return Boolean; |
7541 | -- If any ancestor of the current type is private, the aggregate | |
b465ef6f | 7542 | -- cannot be built in place. We cannot rely on Has_Private_Ancestor, |
57a8057a AC |
7543 | -- because it will not be set when type and its parent are in the |
7544 | -- same scope, and the parent component needs expansion. | |
7545 | ||
7546 | function Top_Level_Aggregate (N : Node_Id) return Node_Id; | |
7547 | -- For nested aggregates return the ultimate enclosing aggregate; for | |
7548 | -- non-nested aggregates return N. | |
7549 | ||
54740d7d AC |
7550 | ------------------------------ |
7551 | -- Build_Back_End_Aggregate -- | |
7552 | ------------------------------ | |
f6205414 | 7553 | |
54740d7d | 7554 | procedure Build_Back_End_Aggregate is |
4f94fa11 AC |
7555 | Comp : Entity_Id; |
7556 | New_Comp : Node_Id; | |
7557 | Tag_Value : Node_Id; | |
57a8057a AC |
7558 | |
7559 | begin | |
0f95b178 JM |
7560 | if Nkind (N) = N_Aggregate then |
7561 | ||
3b9fa2df ES |
7562 | -- If the aggregate is static and can be handled by the back-end, |
7563 | -- nothing left to do. | |
0f95b178 JM |
7564 | |
7565 | if Static_Components then | |
7566 | Set_Compile_Time_Known_Aggregate (N); | |
7567 | Set_Expansion_Delayed (N, False); | |
7568 | end if; | |
7569 | end if; | |
7570 | ||
07fc65c4 | 7571 | -- If no discriminants, nothing special to do |
70482933 | 7572 | |
07fc65c4 | 7573 | if not Has_Discriminants (Typ) then |
70482933 RK |
7574 | null; |
7575 | ||
07fc65c4 GB |
7576 | -- Case of discriminants present |
7577 | ||
70482933 RK |
7578 | elsif Is_Derived_Type (Typ) then |
7579 | ||
138fc6f1 HK |
7580 | -- For untagged types, non-stored discriminants are replaced with |
7581 | -- stored discriminants, which are the ones that gigi uses to | |
7582 | -- describe the type and its components. | |
70482933 | 7583 | |
07fc65c4 | 7584 | Generate_Aggregate_For_Derived_Type : declare |
fbf5a39b | 7585 | procedure Prepend_Stored_Values (T : Entity_Id); |
3b9fa2df ES |
7586 | -- Scan the list of stored discriminants of the type, and add |
7587 | -- their values to the aggregate being built. | |
07fc65c4 GB |
7588 | |
7589 | --------------------------- | |
fbf5a39b | 7590 | -- Prepend_Stored_Values -- |
07fc65c4 GB |
7591 | --------------------------- |
7592 | ||
fbf5a39b | 7593 | procedure Prepend_Stored_Values (T : Entity_Id) is |
54740d7d AC |
7594 | Discr : Entity_Id; |
7595 | First_Comp : Node_Id := Empty; | |
7596 | ||
07fc65c4 | 7597 | begin |
54740d7d AC |
7598 | Discr := First_Stored_Discriminant (T); |
7599 | while Present (Discr) loop | |
07fc65c4 GB |
7600 | New_Comp := |
7601 | Make_Component_Association (Loc, | |
138fc6f1 | 7602 | Choices => New_List ( |
54740d7d | 7603 | New_Occurrence_Of (Discr, Loc)), |
07fc65c4 | 7604 | Expression => |
bdc193ba AC |
7605 | New_Copy_Tree |
7606 | (Get_Discriminant_Value | |
54740d7d | 7607 | (Discr, |
07fc65c4 GB |
7608 | Typ, |
7609 | Discriminant_Constraint (Typ)))); | |
7610 | ||
7611 | if No (First_Comp) then | |
7612 | Prepend_To (Component_Associations (N), New_Comp); | |
7613 | else | |
7614 | Insert_After (First_Comp, New_Comp); | |
7615 | end if; | |
7616 | ||
7617 | First_Comp := New_Comp; | |
54740d7d | 7618 | Next_Stored_Discriminant (Discr); |
07fc65c4 | 7619 | end loop; |
fbf5a39b | 7620 | end Prepend_Stored_Values; |
07fc65c4 | 7621 | |
54740d7d AC |
7622 | -- Local variables |
7623 | ||
7624 | Constraints : constant List_Id := New_List; | |
7625 | ||
7626 | Discr : Entity_Id; | |
7627 | Decl : Node_Id; | |
7628 | Num_Disc : Nat := 0; | |
81a0f4a3 | 7629 | Num_Stor : Nat := 0; |
54740d7d | 7630 | |
07fc65c4 | 7631 | -- Start of processing for Generate_Aggregate_For_Derived_Type |
70482933 RK |
7632 | |
7633 | begin | |
3b9fa2df | 7634 | -- Remove the associations for the discriminant of derived type |
70482933 | 7635 | |
54740d7d AC |
7636 | declare |
7637 | First_Comp : Node_Id; | |
70482933 | 7638 | |
54740d7d AC |
7639 | begin |
7640 | First_Comp := First (Component_Associations (N)); | |
7641 | while Present (First_Comp) loop | |
7642 | Comp := First_Comp; | |
7643 | Next (First_Comp); | |
7644 | ||
7645 | if Ekind (Entity (First (Choices (Comp)))) = | |
7646 | E_Discriminant | |
7647 | then | |
7648 | Remove (Comp); | |
7649 | Num_Disc := Num_Disc + 1; | |
7650 | end if; | |
7651 | end loop; | |
7652 | end; | |
70482933 | 7653 | |
fbf5a39b AC |
7654 | -- Insert stored discriminant associations in the correct |
7655 | -- order. If there are more stored discriminants than new | |
3b9fa2df ES |
7656 | -- discriminants, there is at least one new discriminant that |
7657 | -- constrains more than one of the stored discriminants. In | |
7658 | -- this case we need to construct a proper subtype of the | |
7659 | -- parent type, in order to supply values to all the | |
fbf5a39b AC |
7660 | -- components. Otherwise there is one-one correspondence |
7661 | -- between the constraints and the stored discriminants. | |
70482933 | 7662 | |
54740d7d AC |
7663 | Discr := First_Stored_Discriminant (Base_Type (Typ)); |
7664 | while Present (Discr) loop | |
81a0f4a3 | 7665 | Num_Stor := Num_Stor + 1; |
54740d7d | 7666 | Next_Stored_Discriminant (Discr); |
70482933 | 7667 | end loop; |
07fc65c4 | 7668 | |
fbf5a39b | 7669 | -- Case of more stored discriminants than new discriminants |
07fc65c4 | 7670 | |
81a0f4a3 | 7671 | if Num_Stor > Num_Disc then |
07fc65c4 | 7672 | |
3b9fa2df ES |
7673 | -- Create a proper subtype of the parent type, which is the |
7674 | -- proper implementation type for the aggregate, and convert | |
7675 | -- it to the intended target type. | |
07fc65c4 | 7676 | |
54740d7d AC |
7677 | Discr := First_Stored_Discriminant (Base_Type (Typ)); |
7678 | while Present (Discr) loop | |
07fc65c4 | 7679 | New_Comp := |
37368818 RD |
7680 | New_Copy_Tree |
7681 | (Get_Discriminant_Value | |
54740d7d | 7682 | (Discr, |
bdc193ba AC |
7683 | Typ, |
7684 | Discriminant_Constraint (Typ))); | |
138fc6f1 | 7685 | |
07fc65c4 | 7686 | Append (New_Comp, Constraints); |
54740d7d | 7687 | Next_Stored_Discriminant (Discr); |
07fc65c4 GB |
7688 | end loop; |
7689 | ||
7690 | Decl := | |
7691 | Make_Subtype_Declaration (Loc, | |
191fcb3a | 7692 | Defining_Identifier => Make_Temporary (Loc, 'T'), |
bdc193ba | 7693 | Subtype_Indication => |
07fc65c4 GB |
7694 | Make_Subtype_Indication (Loc, |
7695 | Subtype_Mark => | |
7696 | New_Occurrence_Of (Etype (Base_Type (Typ)), Loc), | |
bdc193ba | 7697 | Constraint => |
07fc65c4 GB |
7698 | Make_Index_Or_Discriminant_Constraint |
7699 | (Loc, Constraints))); | |
7700 | ||
7701 | Insert_Action (N, Decl); | |
fbf5a39b | 7702 | Prepend_Stored_Values (Base_Type (Typ)); |
07fc65c4 GB |
7703 | |
7704 | Set_Etype (N, Defining_Identifier (Decl)); | |
7705 | Set_Analyzed (N); | |
7706 | ||
7707 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
7708 | Analyze (N); | |
7709 | ||
7710 | -- Case where we do not have fewer new discriminants than | |
3b9fa2df ES |
7711 | -- stored discriminants, so in this case we can simply use the |
7712 | -- stored discriminants of the subtype. | |
07fc65c4 GB |
7713 | |
7714 | else | |
fbf5a39b | 7715 | Prepend_Stored_Values (Typ); |
07fc65c4 GB |
7716 | end if; |
7717 | end Generate_Aggregate_For_Derived_Type; | |
70482933 RK |
7718 | end if; |
7719 | ||
7720 | if Is_Tagged_Type (Typ) then | |
7721 | ||
22243c12 | 7722 | -- In the tagged case, _parent and _tag component must be created |
70482933 | 7723 | |
22243c12 RD |
7724 | -- Reset Null_Present unconditionally. Tagged records always have |
7725 | -- at least one field (the tag or the parent). | |
70482933 RK |
7726 | |
7727 | Set_Null_Record_Present (N, False); | |
7728 | ||
7729 | -- When the current aggregate comes from the expansion of an | |
7730 | -- extension aggregate, the parent expr is replaced by an | |
22243c12 | 7731 | -- aggregate formed by selected components of this expr. |
70482933 | 7732 | |
36a66365 | 7733 | if Present (Parent_Expr) and then Is_Empty_List (Comps) then |
5277cab6 | 7734 | Comp := First_Component_Or_Discriminant (Typ); |
70482933 RK |
7735 | while Present (Comp) loop |
7736 | ||
70482933 RK |
7737 | -- Skip all expander-generated components |
7738 | ||
bdc193ba | 7739 | if not Comes_From_Source (Original_Record_Component (Comp)) |
70482933 RK |
7740 | then |
7741 | null; | |
7742 | ||
7743 | else | |
7744 | New_Comp := | |
7745 | Make_Selected_Component (Loc, | |
bdc193ba | 7746 | Prefix => |
70482933 RK |
7747 | Unchecked_Convert_To (Typ, |
7748 | Duplicate_Subexpr (Parent_Expr, True)), | |
70482933 RK |
7749 | Selector_Name => New_Occurrence_Of (Comp, Loc)); |
7750 | ||
7751 | Append_To (Comps, | |
7752 | Make_Component_Association (Loc, | |
54740d7d AC |
7753 | Choices => New_List ( |
7754 | New_Occurrence_Of (Comp, Loc)), | |
37368818 | 7755 | Expression => New_Comp)); |
70482933 RK |
7756 | |
7757 | Analyze_And_Resolve (New_Comp, Etype (Comp)); | |
7758 | end if; | |
7759 | ||
5277cab6 | 7760 | Next_Component_Or_Discriminant (Comp); |
70482933 RK |
7761 | end loop; |
7762 | end if; | |
7763 | ||
7764 | -- Compute the value for the Tag now, if the type is a root it | |
7765 | -- will be included in the aggregate right away, otherwise it will | |
22243c12 | 7766 | -- be propagated to the parent aggregate. |
70482933 RK |
7767 | |
7768 | if Present (Orig_Tag) then | |
7769 | Tag_Value := Orig_Tag; | |
54740d7d | 7770 | |
1f110335 | 7771 | elsif not Tagged_Type_Expansion then |
70482933 | 7772 | Tag_Value := Empty; |
54740d7d | 7773 | |
70482933 | 7774 | else |
a9d8907c JM |
7775 | Tag_Value := |
7776 | New_Occurrence_Of | |
7777 | (Node (First_Elmt (Access_Disp_Table (Typ))), Loc); | |
70482933 RK |
7778 | end if; |
7779 | ||
7780 | -- For a derived type, an aggregate for the parent is formed with | |
7781 | -- all the inherited components. | |
7782 | ||
7783 | if Is_Derived_Type (Typ) then | |
70482933 RK |
7784 | declare |
7785 | First_Comp : Node_Id; | |
7786 | Parent_Comps : List_Id; | |
7787 | Parent_Aggr : Node_Id; | |
7788 | Parent_Name : Node_Id; | |
7789 | ||
7790 | begin | |
54740d7d | 7791 | First_Comp := First (Component_Associations (N)); |
70482933 | 7792 | Parent_Comps := New_List; |
567bf449 EB |
7793 | |
7794 | -- First skip the discriminants | |
7795 | ||
7796 | while Present (First_Comp) | |
7797 | and then Ekind (Entity (First (Choices (First_Comp)))) | |
7798 | = E_Discriminant | |
7799 | loop | |
7800 | Next (First_Comp); | |
7801 | end loop; | |
7802 | ||
7803 | -- Then remove the inherited component association from the | |
7804 | -- aggregate and store them in the parent aggregate | |
7805 | ||
70482933 | 7806 | while Present (First_Comp) |
36a66365 AC |
7807 | and then |
7808 | Scope (Original_Record_Component | |
7809 | (Entity (First (Choices (First_Comp))))) /= | |
7810 | Base_Typ | |
70482933 RK |
7811 | loop |
7812 | Comp := First_Comp; | |
7813 | Next (First_Comp); | |
7814 | Remove (Comp); | |
7815 | Append (Comp, Parent_Comps); | |
7816 | end loop; | |
7817 | ||
36a66365 AC |
7818 | Parent_Aggr := |
7819 | Make_Aggregate (Loc, | |
7820 | Component_Associations => Parent_Comps); | |
70482933 RK |
7821 | Set_Etype (Parent_Aggr, Etype (Base_Type (Typ))); |
7822 | ||
7823 | -- Find the _parent component | |
7824 | ||
7825 | Comp := First_Component (Typ); | |
7826 | while Chars (Comp) /= Name_uParent loop | |
99859ea7 | 7827 | Next_Component (Comp); |
70482933 RK |
7828 | end loop; |
7829 | ||
7830 | Parent_Name := New_Occurrence_Of (Comp, Loc); | |
7831 | ||
7832 | -- Insert the parent aggregate | |
7833 | ||
7834 | Prepend_To (Component_Associations (N), | |
7835 | Make_Component_Association (Loc, | |
7836 | Choices => New_List (Parent_Name), | |
7837 | Expression => Parent_Aggr)); | |
7838 | ||
7839 | -- Expand recursively the parent propagating the right Tag | |
7840 | ||
22243c12 RD |
7841 | Expand_Record_Aggregate |
7842 | (Parent_Aggr, Tag_Value, Parent_Expr); | |
1b6897ce AC |
7843 | |
7844 | -- The ancestor part may be a nested aggregate that has | |
7845 | -- delayed expansion: recheck now. | |
7846 | ||
d4dfb005 | 7847 | if not Component_OK_For_Backend then |
1b6897ce AC |
7848 | Convert_To_Assignments (N, Typ); |
7849 | end if; | |
70482933 RK |
7850 | end; |
7851 | ||
7852 | -- For a root type, the tag component is added (unless compiling | |
0f95b178 | 7853 | -- for the VMs, where tags are implicit). |
70482933 | 7854 | |
1f110335 | 7855 | elsif Tagged_Type_Expansion then |
70482933 RK |
7856 | declare |
7857 | Tag_Name : constant Node_Id := | |
138fc6f1 HK |
7858 | New_Occurrence_Of |
7859 | (First_Tag_Component (Typ), Loc); | |
70482933 RK |
7860 | Typ_Tag : constant Entity_Id := RTE (RE_Tag); |
7861 | Conv_Node : constant Node_Id := | |
138fc6f1 | 7862 | Unchecked_Convert_To (Typ_Tag, Tag_Value); |
70482933 RK |
7863 | |
7864 | begin | |
7865 | Set_Etype (Conv_Node, Typ_Tag); | |
7866 | Prepend_To (Component_Associations (N), | |
7867 | Make_Component_Association (Loc, | |
7868 | Choices => New_List (Tag_Name), | |
7869 | Expression => Conv_Node)); | |
7870 | end; | |
7871 | end if; | |
7872 | end if; | |
54740d7d AC |
7873 | end Build_Back_End_Aggregate; |
7874 | ||
7875 | ---------------------------------------- | |
7876 | -- Compile_Time_Known_Composite_Value -- | |
7877 | ---------------------------------------- | |
7878 | ||
7879 | function Compile_Time_Known_Composite_Value | |
7880 | (N : Node_Id) return Boolean | |
7881 | is | |
7882 | begin | |
7883 | -- If we have an entity name, then see if it is the name of a | |
7884 | -- constant and if so, test the corresponding constant value. | |
7885 | ||
7886 | if Is_Entity_Name (N) then | |
7887 | declare | |
7888 | E : constant Entity_Id := Entity (N); | |
7889 | V : Node_Id; | |
7890 | begin | |
7891 | if Ekind (E) /= E_Constant then | |
7892 | return False; | |
7893 | else | |
7894 | V := Constant_Value (E); | |
7895 | return Present (V) | |
7896 | and then Compile_Time_Known_Composite_Value (V); | |
7897 | end if; | |
7898 | end; | |
7899 | ||
7900 | -- We have a value, see if it is compile time known | |
7901 | ||
7902 | else | |
7903 | if Nkind (N) = N_Aggregate then | |
7904 | return Compile_Time_Known_Aggregate (N); | |
7905 | end if; | |
7906 | ||
7907 | -- All other types of values are not known at compile time | |
7908 | ||
7909 | return False; | |
7910 | end if; | |
7911 | ||
7912 | end Compile_Time_Known_Composite_Value; | |
7913 | ||
d4dfb005 BD |
7914 | ------------------------------ |
7915 | -- Component_OK_For_Backend -- | |
7916 | ------------------------------ | |
54740d7d | 7917 | |
d4dfb005 | 7918 | function Component_OK_For_Backend return Boolean is |
54740d7d AC |
7919 | C : Node_Id; |
7920 | Expr_Q : Node_Id; | |
7921 | ||
7922 | begin | |
54740d7d AC |
7923 | C := First (Comps); |
7924 | while Present (C) loop | |
7925 | ||
7926 | -- If the component has box initialization, expansion is needed | |
7927 | -- and component is not ready for backend. | |
7928 | ||
7929 | if Box_Present (C) then | |
d4dfb005 | 7930 | return False; |
54740d7d AC |
7931 | end if; |
7932 | ||
4f061cf2 | 7933 | Expr_Q := Unqualify (Expression (C)); |
54740d7d | 7934 | |
f4c26077 ES |
7935 | -- Return False for array components whose bounds raise |
7936 | -- constraint error. | |
7937 | ||
7938 | declare | |
61770974 | 7939 | Comp : constant Entity_Id := First (Choices (C)); |
f4c26077 ES |
7940 | Indx : Node_Id; |
7941 | ||
7942 | begin | |
f4c26077 ES |
7943 | if Present (Etype (Comp)) |
7944 | and then Is_Array_Type (Etype (Comp)) | |
7945 | then | |
7946 | Indx := First_Index (Etype (Comp)); | |
f4c26077 | 7947 | while Present (Indx) loop |
61770974 HK |
7948 | if Nkind (Type_Low_Bound (Etype (Indx))) = |
7949 | N_Raise_Constraint_Error | |
7950 | or else Nkind (Type_High_Bound (Etype (Indx))) = | |
7951 | N_Raise_Constraint_Error | |
f4c26077 ES |
7952 | then |
7953 | return False; | |
7954 | end if; | |
7955 | ||
99859ea7 | 7956 | Next_Index (Indx); |
f4c26077 ES |
7957 | end loop; |
7958 | end if; | |
7959 | end; | |
7960 | ||
d4dfb005 | 7961 | -- Return False if the aggregate has any associations for tagged |
54740d7d AC |
7962 | -- components that may require tag adjustment. |
7963 | ||
7964 | -- These are cases where the source expression may have a tag that | |
7965 | -- could differ from the component tag (e.g., can occur for type | |
7966 | -- conversions and formal parameters). (Tag adjustment not needed | |
7967 | -- if Tagged_Type_Expansion because object tags are implicit in | |
7968 | -- the machine.) | |
7969 | ||
7970 | if Is_Tagged_Type (Etype (Expr_Q)) | |
61770974 HK |
7971 | and then |
7972 | (Nkind (Expr_Q) = N_Type_Conversion | |
7973 | or else | |
7974 | (Is_Entity_Name (Expr_Q) | |
bb6a856b | 7975 | and then Is_Formal (Entity (Expr_Q)))) |
54740d7d AC |
7976 | and then Tagged_Type_Expansion |
7977 | then | |
7978 | Static_Components := False; | |
d4dfb005 | 7979 | return False; |
54740d7d AC |
7980 | |
7981 | elsif Is_Delayed_Aggregate (Expr_Q) then | |
7982 | Static_Components := False; | |
d4dfb005 | 7983 | return False; |
54740d7d | 7984 | |
1f6237e3 ES |
7985 | elsif Nkind (Expr_Q) = N_Quantified_Expression then |
7986 | Static_Components := False; | |
7987 | return False; | |
7988 | ||
54740d7d AC |
7989 | elsif Possible_Bit_Aligned_Component (Expr_Q) then |
7990 | Static_Components := False; | |
d4dfb005 | 7991 | return False; |
54740d7d AC |
7992 | |
7993 | elsif Modify_Tree_For_C | |
7994 | and then Nkind (C) = N_Component_Association | |
7995 | and then Has_Per_Object_Constraint (Choices (C)) | |
7996 | then | |
7997 | Static_Components := False; | |
d4dfb005 | 7998 | return False; |
54740d7d AC |
7999 | |
8000 | elsif Modify_Tree_For_C | |
8001 | and then Nkind (Expr_Q) = N_Identifier | |
8002 | and then Is_Array_Type (Etype (Expr_Q)) | |
8003 | then | |
8004 | Static_Components := False; | |
d4dfb005 | 8005 | return False; |
b276ab7a AC |
8006 | |
8007 | elsif Modify_Tree_For_C | |
8008 | and then Nkind (Expr_Q) = N_Type_Conversion | |
8009 | and then Is_Array_Type (Etype (Expr_Q)) | |
8010 | then | |
8011 | Static_Components := False; | |
d4dfb005 | 8012 | return False; |
54740d7d AC |
8013 | end if; |
8014 | ||
8015 | if Is_Elementary_Type (Etype (Expr_Q)) then | |
8016 | if not Compile_Time_Known_Value (Expr_Q) then | |
8017 | Static_Components := False; | |
8018 | end if; | |
8019 | ||
8020 | elsif not Compile_Time_Known_Composite_Value (Expr_Q) then | |
8021 | Static_Components := False; | |
8022 | ||
8023 | if Is_Private_Type (Etype (Expr_Q)) | |
8024 | and then Has_Discriminants (Etype (Expr_Q)) | |
8025 | then | |
d4dfb005 | 8026 | return False; |
54740d7d AC |
8027 | end if; |
8028 | end if; | |
8029 | ||
8030 | Next (C); | |
8031 | end loop; | |
8032 | ||
d4dfb005 BD |
8033 | return True; |
8034 | end Component_OK_For_Backend; | |
54740d7d AC |
8035 | |
8036 | ------------------------------- | |
8037 | -- Has_Per_Object_Constraint -- | |
8038 | ------------------------------- | |
8039 | ||
8040 | function Has_Per_Object_Constraint (L : List_Id) return Boolean is | |
8041 | N : Node_Id := First (L); | |
8042 | begin | |
8043 | while Present (N) loop | |
8044 | if Is_Entity_Name (N) | |
8045 | and then Present (Entity (N)) | |
8046 | and then Has_Per_Object_Constraint (Entity (N)) | |
8047 | then | |
8048 | return True; | |
8049 | end if; | |
8050 | ||
8051 | Next (N); | |
8052 | end loop; | |
8053 | ||
8054 | return False; | |
8055 | end Has_Per_Object_Constraint; | |
8056 | ||
8057 | ----------------------------------- | |
8058 | -- Has_Visible_Private_Ancestor -- | |
8059 | ----------------------------------- | |
8060 | ||
8061 | function Has_Visible_Private_Ancestor (Id : E) return Boolean is | |
8062 | R : constant Entity_Id := Root_Type (Id); | |
8063 | T1 : Entity_Id := Id; | |
8064 | ||
8065 | begin | |
8066 | loop | |
8067 | if Is_Private_Type (T1) then | |
8068 | return True; | |
8069 | ||
8070 | elsif T1 = R then | |
8071 | return False; | |
8072 | ||
8073 | else | |
8074 | T1 := Etype (T1); | |
8075 | end if; | |
8076 | end loop; | |
8077 | end Has_Visible_Private_Ancestor; | |
4f94fa11 AC |
8078 | |
8079 | ------------------------- | |
8080 | -- Top_Level_Aggregate -- | |
8081 | ------------------------- | |
8082 | ||
8083 | function Top_Level_Aggregate (N : Node_Id) return Node_Id is | |
8084 | Aggr : Node_Id; | |
8085 | ||
8086 | begin | |
8087 | Aggr := N; | |
8088 | while Present (Parent (Aggr)) | |
4a08c95c AC |
8089 | and then Nkind (Parent (Aggr)) in |
8090 | N_Aggregate | N_Component_Association | |
4f94fa11 AC |
8091 | loop |
8092 | Aggr := Parent (Aggr); | |
8093 | end loop; | |
8094 | ||
8095 | return Aggr; | |
8096 | end Top_Level_Aggregate; | |
8097 | ||
8098 | -- Local variables | |
8099 | ||
8100 | Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N); | |
8101 | ||
8102 | -- Start of processing for Expand_Record_Aggregate | |
8103 | ||
8104 | begin | |
4f94fa11 AC |
8105 | -- No special management required for aggregates used to initialize |
8106 | -- statically allocated dispatch tables | |
8107 | ||
c061e99b | 8108 | if Is_Static_Dispatch_Table_Aggregate (N) then |
4f94fa11 | 8109 | return; |
e1dfbb03 SB |
8110 | |
8111 | -- Case pattern aggregates need to remain as aggregates | |
8112 | ||
8113 | elsif Is_Case_Choice_Pattern (N) then | |
8114 | return; | |
70482933 | 8115 | end if; |
0f95b178 | 8116 | |
8973b987 | 8117 | -- If the pragma Aggregate_Individually_Assign is set, always convert to |
efc00a88 PB |
8118 | -- assignments. |
8119 | ||
8120 | if Aggregate_Individually_Assign then | |
8121 | Convert_To_Assignments (N, Typ); | |
8122 | ||
4f94fa11 AC |
8123 | -- Ada 2005 (AI-318-2): We need to convert to assignments if components |
8124 | -- are build-in-place function calls. The assignments will each turn | |
8125 | -- into a build-in-place function call. If components are all static, | |
d4dfb005 | 8126 | -- we can pass the aggregate to the back end regardless of limitedness. |
4f94fa11 AC |
8127 | |
8128 | -- Extension aggregates, aggregates in extended return statements, and | |
8129 | -- aggregates for C++ imported types must be expanded. | |
8130 | ||
efc00a88 | 8131 | elsif Ada_Version >= Ada_2005 and then Is_Limited_View (Typ) then |
4a08c95c AC |
8132 | if Nkind (Parent (N)) not in |
8133 | N_Component_Association | N_Object_Declaration | |
4f94fa11 AC |
8134 | then |
8135 | Convert_To_Assignments (N, Typ); | |
8136 | ||
8137 | elsif Nkind (N) = N_Extension_Aggregate | |
8138 | or else Convention (Typ) = Convention_CPP | |
8139 | then | |
8140 | Convert_To_Assignments (N, Typ); | |
8141 | ||
8142 | elsif not Size_Known_At_Compile_Time (Typ) | |
d4dfb005 | 8143 | or else not Component_OK_For_Backend |
4f94fa11 AC |
8144 | or else not Static_Components |
8145 | then | |
8146 | Convert_To_Assignments (N, Typ); | |
8147 | ||
8148 | -- In all other cases, build a proper aggregate to be handled by | |
23a9215f | 8149 | -- the back-end. |
4f94fa11 AC |
8150 | |
8151 | else | |
54740d7d | 8152 | Build_Back_End_Aggregate; |
4f94fa11 AC |
8153 | end if; |
8154 | ||
8155 | -- Gigi doesn't properly handle temporaries of variable size so we | |
8156 | -- generate it in the front-end | |
8157 | ||
8158 | elsif not Size_Known_At_Compile_Time (Typ) | |
8159 | and then Tagged_Type_Expansion | |
8160 | then | |
8161 | Convert_To_Assignments (N, Typ); | |
8162 | ||
8163 | -- An aggregate used to initialize a controlled object must be turned | |
8164 | -- into component assignments as the components themselves may require | |
8165 | -- finalization actions such as adjustment. | |
8166 | ||
8167 | elsif Needs_Finalization (Typ) then | |
8168 | Convert_To_Assignments (N, Typ); | |
8169 | ||
8170 | -- Ada 2005 (AI-287): In case of default initialized components we | |
8171 | -- convert the aggregate into assignments. | |
8172 | ||
8173 | elsif Has_Default_Init_Comps (N) then | |
8174 | Convert_To_Assignments (N, Typ); | |
8175 | ||
8176 | -- Check components | |
8177 | ||
d4dfb005 | 8178 | elsif not Component_OK_For_Backend then |
4f94fa11 AC |
8179 | Convert_To_Assignments (N, Typ); |
8180 | ||
8181 | -- If an ancestor is private, some components are not inherited and we | |
8182 | -- cannot expand into a record aggregate. | |
8183 | ||
8184 | elsif Has_Visible_Private_Ancestor (Typ) then | |
8185 | Convert_To_Assignments (N, Typ); | |
8186 | ||
8187 | -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi | |
8188 | -- is not able to handle the aggregate for Late_Request. | |
8189 | ||
8190 | elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then | |
8191 | Convert_To_Assignments (N, Typ); | |
8192 | ||
8193 | -- If the tagged types covers interface types we need to initialize all | |
8194 | -- hidden components containing pointers to secondary dispatch tables. | |
8195 | ||
8196 | elsif Is_Tagged_Type (Typ) and then Has_Interfaces (Typ) then | |
8197 | Convert_To_Assignments (N, Typ); | |
8198 | ||
8199 | -- If some components are mutable, the size of the aggregate component | |
8200 | -- may be distinct from the default size of the type component, so | |
8201 | -- we need to expand to insure that the back-end copies the proper | |
8202 | -- size of the data. However, if the aggregate is the initial value of | |
8203 | -- a constant, the target is immutable and might be built statically | |
8204 | -- if components are appropriate. | |
8205 | ||
8206 | elsif Has_Mutable_Components (Typ) | |
8207 | and then | |
8208 | (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration | |
8209 | or else not Constant_Present (Parent (Top_Level_Aggr)) | |
8210 | or else not Static_Components) | |
8211 | then | |
8212 | Convert_To_Assignments (N, Typ); | |
8213 | ||
8214 | -- If the type involved has bit aligned components, then we are not sure | |
8215 | -- that the back end can handle this case correctly. | |
8216 | ||
8217 | elsif Type_May_Have_Bit_Aligned_Components (Typ) then | |
8218 | Convert_To_Assignments (N, Typ); | |
8219 | ||
8220 | -- When generating C, only generate an aggregate when declaring objects | |
8221 | -- since C does not support aggregates in e.g. assignment statements. | |
8222 | ||
9f51b855 | 8223 | elsif Modify_Tree_For_C and then not Is_CCG_Supported_Aggregate (N) then |
4f94fa11 AC |
8224 | Convert_To_Assignments (N, Typ); |
8225 | ||
8226 | -- In all other cases, build a proper aggregate to be handled by gigi | |
8227 | ||
8228 | else | |
54740d7d | 8229 | Build_Back_End_Aggregate; |
4f94fa11 | 8230 | end if; |
70482933 RK |
8231 | end Expand_Record_Aggregate; |
8232 | ||
a80b1eb7 EB |
8233 | --------------------- |
8234 | -- Get_Base_Object -- | |
8235 | --------------------- | |
8236 | ||
8237 | function Get_Base_Object (N : Node_Id) return Entity_Id is | |
8238 | R : Node_Id; | |
8239 | ||
8240 | begin | |
8241 | R := Get_Referenced_Object (N); | |
8242 | ||
4a08c95c | 8243 | while Nkind (R) in N_Indexed_Component | N_Selected_Component | N_Slice |
a80b1eb7 EB |
8244 | loop |
8245 | R := Get_Referenced_Object (Prefix (R)); | |
8246 | end loop; | |
8247 | ||
8248 | if Is_Entity_Name (R) and then Is_Object (Entity (R)) then | |
8249 | return Entity (R); | |
8250 | else | |
8251 | return Empty; | |
8252 | end if; | |
8253 | end Get_Base_Object; | |
8254 | ||
65356e64 AC |
8255 | ---------------------------- |
8256 | -- Has_Default_Init_Comps -- | |
8257 | ---------------------------- | |
8258 | ||
8259 | function Has_Default_Init_Comps (N : Node_Id) return Boolean is | |
b52e1520 | 8260 | Assoc : Node_Id; |
c45b6ae0 | 8261 | Expr : Node_Id; |
b52e1520 | 8262 | -- Component association and expression, respectively |
bdc193ba | 8263 | |
65356e64 | 8264 | begin |
4a08c95c | 8265 | pragma Assert (Nkind (N) in N_Aggregate | N_Extension_Aggregate); |
c45b6ae0 | 8266 | |
c5ee5ad2 BD |
8267 | if Has_Self_Reference (N) then |
8268 | return True; | |
8269 | end if; | |
8270 | ||
b52e1520 PT |
8271 | Assoc := First (Component_Associations (N)); |
8272 | while Present (Assoc) loop | |
8273 | -- Each component association has either a box or an expression | |
c45b6ae0 | 8274 | |
b52e1520 | 8275 | pragma Assert (Box_Present (Assoc) xor Present (Expression (Assoc))); |
65356e64 | 8276 | |
b52e1520 | 8277 | -- Check if any direct component has default initialized components |
c45b6ae0 | 8278 | |
b52e1520 PT |
8279 | if Box_Present (Assoc) then |
8280 | return True; | |
c45b6ae0 | 8281 | |
b52e1520 | 8282 | -- Recursive call in case of aggregate expression |
c45b6ae0 | 8283 | |
b52e1520 PT |
8284 | else |
8285 | Expr := Expression (Assoc); | |
8286 | ||
8287 | if Nkind (Expr) in N_Aggregate | N_Extension_Aggregate | |
8288 | and then Has_Default_Init_Comps (Expr) | |
8289 | then | |
8290 | return True; | |
8291 | end if; | |
c45b6ae0 AC |
8292 | end if; |
8293 | ||
b52e1520 | 8294 | Next (Assoc); |
c45b6ae0 AC |
8295 | end loop; |
8296 | ||
65356e64 AC |
8297 | return False; |
8298 | end Has_Default_Init_Comps; | |
8299 | ||
fc84947c EB |
8300 | -------------------------- |
8301 | -- Initialize_Component -- | |
8302 | -------------------------- | |
8303 | ||
8304 | procedure Initialize_Component | |
fc84947c EB |
8305 | (N : Node_Id; |
8306 | Comp : Node_Id; | |
8307 | Comp_Typ : Node_Id; | |
8308 | Init_Expr : Node_Id; | |
8309 | Stmts : List_Id) | |
8310 | is | |
8311 | Exceptions_OK : constant Boolean := | |
8312 | not Restriction_Active (No_Exception_Propagation); | |
8313 | Finalization_OK : constant Boolean := | |
8314 | Present (Comp_Typ) | |
8315 | and then Needs_Finalization (Comp_Typ); | |
fc84947c EB |
8316 | Loc : constant Source_Ptr := Sloc (N); |
8317 | ||
fc84947c EB |
8318 | Blk_Stmts : List_Id; |
8319 | Init_Stmt : Node_Id; | |
8320 | ||
8321 | begin | |
8322 | pragma Assert (Nkind (Init_Expr) in N_Subexpr); | |
8323 | ||
8324 | -- Protect the initialization statements from aborts. Generate: | |
8325 | ||
8326 | -- Abort_Defer; | |
8327 | ||
8328 | if Finalization_OK and Abort_Allowed then | |
8329 | if Exceptions_OK then | |
8330 | Blk_Stmts := New_List; | |
8331 | else | |
8332 | Blk_Stmts := Stmts; | |
8333 | end if; | |
8334 | ||
8335 | Append_To (Blk_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer)); | |
8336 | ||
8337 | -- Otherwise aborts are not allowed. All generated code is added | |
8338 | -- directly to the input list. | |
8339 | ||
8340 | else | |
8341 | Blk_Stmts := Stmts; | |
8342 | end if; | |
8343 | ||
8344 | -- Initialize the component. Generate: | |
8345 | ||
8346 | -- Comp := Init_Expr; | |
8347 | ||
8348 | -- Note that the initialization expression is not duplicated because | |
8349 | -- either only a single component may be initialized by it (record) | |
8350 | -- or it has already been duplicated if need be (array). | |
8351 | ||
8352 | Init_Stmt := | |
8353 | Make_OK_Assignment_Statement (Loc, | |
8354 | Name => New_Copy_Tree (Comp), | |
8355 | Expression => Relocate_Node (Init_Expr)); | |
fc84947c EB |
8356 | |
8357 | Append_To (Blk_Stmts, Init_Stmt); | |
8358 | ||
c0ceba6c EB |
8359 | -- Arrange for the component to be adjusted if need be (the call will be |
8360 | -- generated by Make_Tag_Ctrl_Assignment). But, in the case of an array | |
8361 | -- aggregate, controlled subaggregates are not considered because each | |
8362 | -- of their individual elements will receive an adjustment of its own. | |
fc84947c EB |
8363 | |
8364 | if Finalization_OK | |
c0ceba6c | 8365 | and then not Is_Limited_View (Comp_Typ) |
fc84947c EB |
8366 | and then not |
8367 | (Is_Array_Type (Etype (N)) | |
8368 | and then Is_Array_Type (Comp_Typ) | |
8369 | and then Needs_Finalization (Component_Type (Comp_Typ)) | |
8370 | and then Nkind (Unqualify (Init_Expr)) = N_Aggregate) | |
8371 | then | |
c0ceba6c | 8372 | Set_No_Finalize_Actions (Init_Stmt); |
fc84947c | 8373 | |
c0ceba6c EB |
8374 | -- Or else, only adjust the tag due to a possible view conversion |
8375 | ||
8376 | else | |
8377 | Set_No_Ctrl_Actions (Init_Stmt); | |
fc84947c | 8378 | |
c0ceba6c EB |
8379 | if Tagged_Type_Expansion and then Is_Tagged_Type (Comp_Typ) then |
8380 | Append_To (Blk_Stmts, | |
8381 | Make_Tag_Assignment_From_Type | |
8382 | (Loc, New_Copy_Tree (Comp), Underlying_Type (Comp_Typ))); | |
fc84947c EB |
8383 | end if; |
8384 | end if; | |
8385 | ||
8386 | -- Complete the protection of the initialization statements | |
8387 | ||
8388 | if Finalization_OK and Abort_Allowed then | |
8389 | ||
8390 | -- Wrap the initialization statements in a block to catch a | |
8391 | -- potential exception. Generate: | |
8392 | ||
8393 | -- begin | |
8394 | -- Abort_Defer; | |
8395 | -- Comp := Init_Expr; | |
8396 | -- Comp._tag := Full_TypP; | |
8397 | -- [Deep_]Adjust (Comp); | |
8398 | -- at end | |
8399 | -- Abort_Undefer_Direct; | |
8400 | -- end; | |
8401 | ||
8402 | if Exceptions_OK then | |
8403 | Append_To (Stmts, | |
8404 | Build_Abort_Undefer_Block (Loc, | |
8405 | Stmts => Blk_Stmts, | |
8406 | Context => N)); | |
8407 | ||
8408 | -- Otherwise exceptions are not propagated. Generate: | |
8409 | ||
8410 | -- Abort_Defer; | |
8411 | -- Comp := Init_Expr; | |
8412 | -- Comp._tag := Full_TypP; | |
8413 | -- [Deep_]Adjust (Comp); | |
8414 | -- Abort_Undefer; | |
8415 | ||
8416 | else | |
8417 | Append_To (Blk_Stmts, | |
8418 | Build_Runtime_Call (Loc, RE_Abort_Undefer)); | |
8419 | end if; | |
8420 | end if; | |
c0ceba6c | 8421 | end Initialize_Component; |
fc84947c | 8422 | |
3fc40cd7 PMR |
8423 | ---------------------------------------- |
8424 | -- Is_Build_In_Place_Aggregate_Return -- | |
8425 | ---------------------------------------- | |
8426 | ||
8427 | function Is_Build_In_Place_Aggregate_Return (N : Node_Id) return Boolean is | |
8428 | P : Node_Id := Parent (N); | |
8429 | ||
8430 | begin | |
8431 | while Nkind (P) = N_Qualified_Expression loop | |
8432 | P := Parent (P); | |
8433 | end loop; | |
8434 | ||
8435 | if Nkind (P) = N_Simple_Return_Statement then | |
8436 | null; | |
8437 | ||
8438 | elsif Nkind (Parent (P)) = N_Extended_Return_Statement then | |
8439 | P := Parent (P); | |
8440 | ||
8441 | else | |
8442 | return False; | |
8443 | end if; | |
8444 | ||
8445 | return | |
8446 | Is_Build_In_Place_Function | |
8447 | (Return_Applies_To (Return_Statement_Entity (P))); | |
8448 | end Is_Build_In_Place_Aggregate_Return; | |
8449 | ||
70482933 RK |
8450 | -------------------------- |
8451 | -- Is_Delayed_Aggregate -- | |
8452 | -------------------------- | |
8453 | ||
8454 | function Is_Delayed_Aggregate (N : Node_Id) return Boolean is | |
4f061cf2 | 8455 | Unqual_N : constant Node_Id := Unqualify (N); |
fbf5a39b | 8456 | |
70482933 | 8457 | begin |
4f061cf2 EB |
8458 | return Nkind (Unqual_N) in N_Aggregate | N_Extension_Aggregate |
8459 | and then Expansion_Delayed (Unqual_N); | |
70482933 RK |
8460 | end Is_Delayed_Aggregate; |
8461 | ||
9f51b855 JM |
8462 | -------------------------------- |
8463 | -- Is_CCG_Supported_Aggregate -- | |
8464 | -------------------------------- | |
6031f544 | 8465 | |
9f51b855 JM |
8466 | function Is_CCG_Supported_Aggregate |
8467 | (N : Node_Id) return Boolean | |
8468 | is | |
4ff5aa0c | 8469 | P : Node_Id := Parent (N); |
2401c98f | 8470 | |
6031f544 | 8471 | begin |
bc1146e5 HK |
8472 | -- Aggregates are not supported for nonstandard rep clauses, since they |
8473 | -- may lead to extra padding fields in CCG. | |
4ff5aa0c | 8474 | |
3bcf8298 | 8475 | if Is_Record_Type (Etype (N)) |
4ff5aa0c AC |
8476 | and then Has_Non_Standard_Rep (Etype (N)) |
8477 | then | |
8478 | return False; | |
8479 | end if; | |
6031f544 | 8480 | |
4ff5aa0c | 8481 | while Present (P) and then Nkind (P) = N_Aggregate loop |
6031f544 AC |
8482 | P := Parent (P); |
8483 | end loop; | |
8484 | ||
d2d56bba | 8485 | -- Check cases where aggregates are supported by the CCG backend |
9f51b855 | 8486 | |
4ff5aa0c | 8487 | if Nkind (P) = N_Object_Declaration then |
d2d56bba JM |
8488 | declare |
8489 | P_Typ : constant Entity_Id := Etype (Defining_Identifier (P)); | |
9f51b855 | 8490 | |
d2d56bba JM |
8491 | begin |
8492 | if Is_Record_Type (P_Typ) then | |
8493 | return True; | |
8494 | else | |
8495 | return Compile_Time_Known_Bounds (P_Typ); | |
8496 | end if; | |
8497 | end; | |
8498 | ||
8499 | elsif Nkind (P) = N_Qualified_Expression then | |
8500 | if Nkind (Parent (P)) = N_Object_Declaration then | |
8501 | declare | |
8502 | P_Typ : constant Entity_Id := | |
8503 | Etype (Defining_Identifier (Parent (P))); | |
8504 | begin | |
8505 | if Is_Record_Type (P_Typ) then | |
8506 | return True; | |
8507 | else | |
8508 | return Compile_Time_Known_Bounds (P_Typ); | |
8509 | end if; | |
8510 | end; | |
8511 | ||
8512 | elsif Nkind (Parent (P)) = N_Allocator then | |
8513 | return True; | |
8514 | end if; | |
9f51b855 JM |
8515 | end if; |
8516 | ||
6031f544 | 8517 | return False; |
9f51b855 | 8518 | end Is_CCG_Supported_Aggregate; |
6031f544 | 8519 | |
fa57ac97 ES |
8520 | ---------------------------------------- |
8521 | -- Is_Static_Dispatch_Table_Aggregate -- | |
8522 | ---------------------------------------- | |
8523 | ||
8524 | function Is_Static_Dispatch_Table_Aggregate (N : Node_Id) return Boolean is | |
8525 | Typ : constant Entity_Id := Base_Type (Etype (N)); | |
8526 | ||
8527 | begin | |
6214b83b | 8528 | return Building_Static_Dispatch_Tables |
1f110335 | 8529 | and then Tagged_Type_Expansion |
fa57ac97 ES |
8530 | |
8531 | -- Avoid circularity when rebuilding the compiler | |
8532 | ||
3477e0b2 PT |
8533 | and then not Is_RTU (Cunit_Entity (Get_Source_Unit (N)), Ada_Tags) |
8534 | and then (Is_RTE (Typ, RE_Dispatch_Table_Wrapper) | |
fa57ac97 | 8535 | or else |
3477e0b2 | 8536 | Is_RTE (Typ, RE_Address_Array) |
fa57ac97 | 8537 | or else |
3477e0b2 | 8538 | Is_RTE (Typ, RE_Type_Specific_Data) |
fa57ac97 | 8539 | or else |
3477e0b2 | 8540 | Is_RTE (Typ, RE_Tag_Table) |
fa57ac97 | 8541 | or else |
3477e0b2 | 8542 | Is_RTE (Typ, RE_Object_Specific_Data) |
c7cb99f8 | 8543 | or else |
3477e0b2 | 8544 | Is_RTE (Typ, RE_Interface_Data) |
fa57ac97 | 8545 | or else |
3477e0b2 | 8546 | Is_RTE (Typ, RE_Interfaces_Array) |
fa57ac97 | 8547 | or else |
3477e0b2 | 8548 | Is_RTE (Typ, RE_Interface_Data_Element)); |
fa57ac97 ES |
8549 | end Is_Static_Dispatch_Table_Aggregate; |
8550 | ||
dc3af7e2 AC |
8551 | ----------------------------- |
8552 | -- Is_Two_Dim_Packed_Array -- | |
8553 | ----------------------------- | |
8554 | ||
8555 | function Is_Two_Dim_Packed_Array (Typ : Entity_Id) return Boolean is | |
36e38022 | 8556 | C : constant Uint := Component_Size (Typ); |
dc3af7e2 AC |
8557 | begin |
8558 | return Number_Dimensions (Typ) = 2 | |
8559 | and then Is_Bit_Packed_Array (Typ) | |
36e38022 | 8560 | and then C in Uint_1 | Uint_2 | Uint_4; -- False if No_Uint |
dc3af7e2 AC |
8561 | end Is_Two_Dim_Packed_Array; |
8562 | ||
70482933 RK |
8563 | -------------------- |
8564 | -- Late_Expansion -- | |
8565 | -------------------- | |
8566 | ||
8567 | function Late_Expansion | |
8568 | (N : Node_Id; | |
8569 | Typ : Entity_Id; | |
df3e68b1 | 8570 | Target : Node_Id) return List_Id |
9bc856dd | 8571 | is |
f29afe5f | 8572 | Aggr_Code : List_Id; |
b748c3d1 | 8573 | New_Aggr : Node_Id; |
f29afe5f | 8574 | |
70482933 | 8575 | begin |
b748c3d1 EB |
8576 | if Is_Array_Type (Typ) then |
8577 | -- If the assignment can be done directly by the back end, then | |
8578 | -- reset Set_Expansion_Delayed and do not expand further. | |
8579 | ||
8580 | if not CodePeer_Mode | |
8581 | and then not Modify_Tree_For_C | |
8582 | and then not Possible_Bit_Aligned_Component (Target) | |
8583 | and then not Is_Possibly_Unaligned_Slice (Target) | |
8584 | and then Aggr_Assignment_OK_For_Backend (N) | |
8585 | then | |
8586 | New_Aggr := New_Copy_Tree (N); | |
8587 | Set_Expansion_Delayed (New_Aggr, False); | |
8588 | ||
8589 | Aggr_Code := | |
8590 | New_List ( | |
8591 | Make_OK_Assignment_Statement (Sloc (New_Aggr), | |
8592 | Name => Target, | |
8593 | Expression => New_Aggr)); | |
8594 | ||
8595 | -- Or else, generate component assignments to it | |
8596 | ||
8597 | else | |
8598 | Aggr_Code := | |
8599 | Build_Array_Aggr_Code | |
8600 | (N => N, | |
8601 | Ctype => Component_Type (Typ), | |
8602 | Index => First_Index (Typ), | |
8603 | Into => Target, | |
8604 | Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)), | |
8605 | Indexes => No_List); | |
8606 | end if; | |
6031f544 | 8607 | |
f3bf0d9a HK |
8608 | -- Directly or indirectly (e.g. access protected procedure) a record |
8609 | ||
8610 | else | |
6031f544 | 8611 | Aggr_Code := Build_Record_Aggr_Code (N, Typ, Target); |
70482933 | 8612 | end if; |
4ac2bbbd AC |
8613 | |
8614 | -- Save the last assignment statement associated with the aggregate | |
8615 | -- when building a controlled object. This reference is utilized by | |
8616 | -- the finalization machinery when marking an object as successfully | |
8617 | -- initialized. | |
8618 | ||
8619 | if Needs_Finalization (Typ) | |
8620 | and then Is_Entity_Name (Target) | |
8621 | and then Present (Entity (Target)) | |
4a08c95c | 8622 | and then Ekind (Entity (Target)) in E_Constant | E_Variable |
4ac2bbbd AC |
8623 | then |
8624 | Set_Last_Aggregate_Assignment (Entity (Target), Last (Aggr_Code)); | |
8625 | end if; | |
8626 | ||
8627 | return Aggr_Code; | |
70482933 RK |
8628 | end Late_Expansion; |
8629 | ||
8630 | ---------------------------------- | |
8631 | -- Make_OK_Assignment_Statement -- | |
8632 | ---------------------------------- | |
8633 | ||
8634 | function Make_OK_Assignment_Statement | |
8635 | (Sloc : Source_Ptr; | |
8636 | Name : Node_Id; | |
0f95b178 | 8637 | Expression : Node_Id) return Node_Id |
70482933 RK |
8638 | is |
8639 | begin | |
8640 | Set_Assignment_OK (Name); | |
8641 | return Make_Assignment_Statement (Sloc, Name, Expression); | |
8642 | end Make_OK_Assignment_Statement; | |
8643 | ||
c42006e9 AC |
8644 | ------------------------ |
8645 | -- Max_Aggregate_Size -- | |
8646 | ------------------------ | |
8647 | ||
8648 | function Max_Aggregate_Size | |
eaf6e63a BD |
8649 | (N : Node_Id; |
8650 | Default_Size : Nat := 5000) return Nat | |
8651 | is | |
eaf6e63a BD |
8652 | function Use_Small_Size (N : Node_Id) return Boolean; |
8653 | -- True if we should return a very small size, which means large | |
8654 | -- aggregates will be implemented as a loop when possible (potentially | |
8655 | -- transformed to memset calls). | |
8656 | ||
8657 | function Aggr_Context (N : Node_Id) return Node_Id; | |
8658 | -- Return the context in which the aggregate appears, not counting | |
8659 | -- qualified expressions and similar. | |
8660 | ||
bcc15039 PT |
8661 | ------------------ |
8662 | -- Aggr_Context -- | |
8663 | ------------------ | |
8664 | ||
eaf6e63a BD |
8665 | function Aggr_Context (N : Node_Id) return Node_Id is |
8666 | Result : Node_Id := Parent (N); | |
8667 | begin | |
4a08c95c AC |
8668 | if Nkind (Result) in N_Qualified_Expression |
8669 | | N_Type_Conversion | |
8670 | | N_Unchecked_Type_Conversion | |
8671 | | N_If_Expression | |
8672 | | N_Case_Expression | |
8673 | | N_Component_Association | |
8674 | | N_Aggregate | |
eaf6e63a BD |
8675 | then |
8676 | Result := Aggr_Context (Result); | |
8677 | end if; | |
8678 | ||
8679 | return Result; | |
8680 | end Aggr_Context; | |
8681 | ||
bcc15039 PT |
8682 | -------------------- |
8683 | -- Use_Small_Size -- | |
8684 | -------------------- | |
8685 | ||
eaf6e63a BD |
8686 | function Use_Small_Size (N : Node_Id) return Boolean is |
8687 | C : constant Node_Id := Aggr_Context (N); | |
8688 | -- The decision depends on the context in which the aggregate occurs, | |
8689 | -- and for variable declarations, whether we are nested inside a | |
8690 | -- subprogram. | |
8691 | begin | |
8692 | case Nkind (C) is | |
8693 | -- True for assignment statements and similar | |
8694 | ||
8695 | when N_Assignment_Statement | |
8696 | | N_Simple_Return_Statement | |
8697 | | N_Allocator | |
8698 | | N_Attribute_Reference | |
8699 | => | |
8700 | return True; | |
8701 | ||
8702 | -- True for nested variable declarations. False for library level | |
8703 | -- variables, and for constants (whether or not nested). | |
8704 | ||
8705 | when N_Object_Declaration => | |
8706 | return not Constant_Present (C) | |
0f3dfe41 | 8707 | and then Is_Subprogram (Current_Scope); |
eaf6e63a BD |
8708 | |
8709 | -- False for all other contexts | |
8710 | ||
8711 | when others => | |
8712 | return False; | |
8713 | end case; | |
8714 | end Use_Small_Size; | |
8715 | ||
bcc15039 PT |
8716 | -- Local variables |
8717 | ||
8718 | Typ : constant Entity_Id := Etype (N); | |
8719 | ||
eaf6e63a BD |
8720 | -- Start of processing for Max_Aggregate_Size |
8721 | ||
c42006e9 | 8722 | begin |
bcc15039 PT |
8723 | -- We use a small limit in CodePeer mode where we favor loops instead of |
8724 | -- thousands of single assignments (from large aggregates). | |
c42006e9 AC |
8725 | |
8726 | -- We also increase the limit to 2**24 (about 16 million) if | |
8727 | -- Restrictions (No_Elaboration_Code) or Restrictions | |
8728 | -- (No_Implicit_Loops) is specified, since in either case we are at risk | |
8729 | -- of declaring the program illegal because of this limit. We also | |
8730 | -- increase the limit when Static_Elaboration_Desired, given that this | |
8731 | -- means that objects are intended to be placed in data memory. | |
8732 | ||
8733 | -- Same if the aggregate is for a packed two-dimensional array, because | |
8734 | -- if components are static it is much more efficient to construct a | |
8735 | -- one-dimensional equivalent array with static components. | |
8736 | ||
8737 | if CodePeer_Mode then | |
8738 | return 100; | |
8739 | elsif Restriction_Active (No_Elaboration_Code) | |
8740 | or else Restriction_Active (No_Implicit_Loops) | |
8741 | or else Is_Two_Dim_Packed_Array (Typ) | |
8742 | or else (Ekind (Current_Scope) = E_Package | |
8743 | and then Static_Elaboration_Desired (Current_Scope)) | |
8744 | then | |
8745 | return 2 ** 24; | |
eaf6e63a | 8746 | elsif Use_Small_Size (N) then |
152f64c2 | 8747 | return 64; |
c42006e9 | 8748 | end if; |
eaf6e63a BD |
8749 | |
8750 | return Default_Size; | |
c42006e9 AC |
8751 | end Max_Aggregate_Size; |
8752 | ||
70482933 RK |
8753 | ----------------------- |
8754 | -- Number_Of_Choices -- | |
8755 | ----------------------- | |
8756 | ||
8757 | function Number_Of_Choices (N : Node_Id) return Nat is | |
8758 | Assoc : Node_Id; | |
8759 | Choice : Node_Id; | |
8760 | ||
8761 | Nb_Choices : Nat := 0; | |
8762 | ||
8763 | begin | |
8764 | if Present (Expressions (N)) then | |
8765 | return 0; | |
8766 | end if; | |
8767 | ||
8768 | Assoc := First (Component_Associations (N)); | |
8769 | while Present (Assoc) loop | |
00f45f30 | 8770 | Choice := First (Choice_List (Assoc)); |
70482933 | 8771 | while Present (Choice) loop |
70482933 RK |
8772 | if Nkind (Choice) /= N_Others_Choice then |
8773 | Nb_Choices := Nb_Choices + 1; | |
8774 | end if; | |
8775 | ||
8776 | Next (Choice); | |
8777 | end loop; | |
8778 | ||
8779 | Next (Assoc); | |
8780 | end loop; | |
8781 | ||
8782 | return Nb_Choices; | |
8783 | end Number_Of_Choices; | |
8784 | ||
07fc65c4 GB |
8785 | ------------------------------------ |
8786 | -- Packed_Array_Aggregate_Handled -- | |
8787 | ------------------------------------ | |
8788 | ||
8789 | -- The current version of this procedure will handle at compile time | |
8790 | -- any array aggregate that meets these conditions: | |
8791 | ||
5eeeed5e | 8792 | -- One and two dimensional, bit packed |
07fc65c4 GB |
8793 | -- Underlying packed type is modular type |
8794 | -- Bounds are within 32-bit Int range | |
8795 | -- All bounds and values are static | |
8796 | ||
a39a553e AC |
8797 | -- Note: for now, in the 2-D case, we only handle component sizes of |
8798 | -- 1, 2, 4 (cases where an integral number of elements occupies a byte). | |
8799 | ||
07fc65c4 GB |
8800 | function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is |
8801 | Loc : constant Source_Ptr := Sloc (N); | |
8802 | Typ : constant Entity_Id := Etype (N); | |
8803 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
8804 | ||
8805 | Not_Handled : exception; | |
8806 | -- Exception raised if this aggregate cannot be handled | |
8807 | ||
8808 | begin | |
5eeeed5e | 8809 | -- Handle one- or two dimensional bit packed array |
07fc65c4 GB |
8810 | |
8811 | if not Is_Bit_Packed_Array (Typ) | |
5eeeed5e | 8812 | or else Number_Dimensions (Typ) > 2 |
07fc65c4 GB |
8813 | then |
8814 | return False; | |
8815 | end if; | |
8816 | ||
5eeeed5e | 8817 | -- If two-dimensional, check whether it can be folded, and transformed |
8ca597af RD |
8818 | -- into a one-dimensional aggregate for the Packed_Array_Impl_Type of |
8819 | -- the original type. | |
5eeeed5e AC |
8820 | |
8821 | if Number_Dimensions (Typ) = 2 then | |
8822 | return Two_Dim_Packed_Array_Handled (N); | |
8823 | end if; | |
8824 | ||
8ca597af | 8825 | if not Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)) then |
5eeeed5e AC |
8826 | return False; |
8827 | end if; | |
8828 | ||
78326189 | 8829 | if not Is_Scalar_Type (Ctyp) then |
0f95b178 JM |
8830 | return False; |
8831 | end if; | |
8832 | ||
07fc65c4 GB |
8833 | declare |
8834 | Csiz : constant Nat := UI_To_Int (Component_Size (Typ)); | |
8835 | ||
07fc65c4 | 8836 | function Get_Component_Val (N : Node_Id) return Uint; |
3b9fa2df ES |
8837 | -- Given a expression value N of the component type Ctyp, returns a |
8838 | -- value of Csiz (component size) bits representing this value. If | |
d940c627 | 8839 | -- the value is nonstatic or any other reason exists why the value |
3b9fa2df | 8840 | -- cannot be returned, then Not_Handled is raised. |
07fc65c4 GB |
8841 | |
8842 | ----------------------- | |
8843 | -- Get_Component_Val -- | |
8844 | ----------------------- | |
8845 | ||
8846 | function Get_Component_Val (N : Node_Id) return Uint is | |
fb8e3581 | 8847 | Val : Uint; |
07fc65c4 GB |
8848 | |
8849 | begin | |
8850 | -- We have to analyze the expression here before doing any further | |
8851 | -- processing here. The analysis of such expressions is deferred | |
8852 | -- till expansion to prevent some problems of premature analysis. | |
8853 | ||
8854 | Analyze_And_Resolve (N, Ctyp); | |
8855 | ||
3b9fa2df ES |
8856 | -- Must have a compile time value. String literals have to be |
8857 | -- converted into temporaries as well, because they cannot easily | |
8858 | -- be converted into their bit representation. | |
07fc65c4 | 8859 | |
6b6fcd3e AC |
8860 | if not Compile_Time_Known_Value (N) |
8861 | or else Nkind (N) = N_String_Literal | |
8862 | then | |
07fc65c4 GB |
8863 | raise Not_Handled; |
8864 | end if; | |
8865 | ||
8866 | Val := Expr_Rep_Value (N); | |
8867 | ||
8868 | -- Adjust for bias, and strip proper number of bits | |
8869 | ||
8870 | if Has_Biased_Representation (Ctyp) then | |
8871 | Val := Val - Expr_Value (Type_Low_Bound (Ctyp)); | |
8872 | end if; | |
8873 | ||
8874 | return Val mod Uint_2 ** Csiz; | |
8875 | end Get_Component_Val; | |
8876 | ||
7c4f3267 BD |
8877 | Bounds : constant Range_Nodes := Get_Index_Bounds (First_Index (Typ)); |
8878 | ||
07fc65c4 GB |
8879 | -- Here we know we have a one dimensional bit packed array |
8880 | ||
8881 | begin | |
07fc65c4 GB |
8882 | -- Cannot do anything if bounds are dynamic |
8883 | ||
7c4f3267 BD |
8884 | if not (Compile_Time_Known_Value (Bounds.First) |
8885 | and then | |
8886 | Compile_Time_Known_Value (Bounds.Last)) | |
07fc65c4 GB |
8887 | then |
8888 | return False; | |
8889 | end if; | |
8890 | ||
7c4f3267 BD |
8891 | declare |
8892 | Bounds_Vals : Range_Values; | |
8893 | -- Compile-time known values of bounds | |
8894 | begin | |
8895 | -- Or are silly out of range of int bounds | |
07fc65c4 | 8896 | |
7c4f3267 BD |
8897 | Bounds_Vals.First := Expr_Value (Bounds.First); |
8898 | Bounds_Vals.Last := Expr_Value (Bounds.Last); | |
07fc65c4 | 8899 | |
7c4f3267 BD |
8900 | if not UI_Is_In_Int_Range (Bounds_Vals.First) |
8901 | or else | |
8902 | not UI_Is_In_Int_Range (Bounds_Vals.Last) | |
8903 | then | |
8904 | return False; | |
8905 | end if; | |
07fc65c4 | 8906 | |
7c4f3267 BD |
8907 | -- At this stage we have a suitable aggregate for handling at |
8908 | -- compile time. The only remaining checks are that the values of | |
8909 | -- expressions in the aggregate are compile-time known (checks are | |
8910 | -- performed by Get_Component_Val), and that any subtypes or | |
8911 | -- ranges are statically known. | |
07fc65c4 | 8912 | |
7c4f3267 BD |
8913 | -- If the aggregate is not fully positional at this stage, then |
8914 | -- convert it to positional form. Either this will fail, in which | |
8915 | -- case we can do nothing, or it will succeed, in which case we | |
8916 | -- have succeeded in handling the aggregate and transforming it | |
8917 | -- into a modular value, or it will stay an aggregate, in which | |
8918 | -- case we have failed to create a packed value for it. | |
07fc65c4 | 8919 | |
7c4f3267 BD |
8920 | if Present (Component_Associations (N)) then |
8921 | Convert_To_Positional (N, Handle_Bit_Packed => True); | |
8922 | return Nkind (N) /= N_Aggregate; | |
8923 | end if; | |
07fc65c4 | 8924 | |
7c4f3267 | 8925 | -- Otherwise we are all positional, so convert to proper value |
07fc65c4 | 8926 | |
7c4f3267 BD |
8927 | declare |
8928 | Len : constant Nat := | |
8929 | Int'Max (0, UI_To_Int (Bounds_Vals.Last) - | |
8930 | UI_To_Int (Bounds_Vals.First) + 1); | |
8931 | -- The length of the array (number of elements) | |
07fc65c4 | 8932 | |
7c4f3267 BD |
8933 | Aggregate_Val : Uint; |
8934 | -- Value of aggregate. The value is set in the low order bits | |
8935 | -- of this value. For the little-endian case, the values are | |
8936 | -- stored from low-order to high-order and for the big-endian | |
8937 | -- case the values are stored from high order to low order. | |
8938 | -- Note that gigi will take care of the conversions to left | |
8939 | -- justify the value in the big endian case (because of left | |
8940 | -- justified modular type processing), so we do not have to | |
8941 | -- worry about that here. | |
07fc65c4 | 8942 | |
7c4f3267 BD |
8943 | Lit : Node_Id; |
8944 | -- Integer literal for resulting constructed value | |
07fc65c4 | 8945 | |
7c4f3267 BD |
8946 | Shift : Nat; |
8947 | -- Shift count from low order for next value | |
07fc65c4 | 8948 | |
7c4f3267 BD |
8949 | Incr : Int; |
8950 | -- Shift increment for loop | |
07fc65c4 | 8951 | |
7c4f3267 BD |
8952 | Expr : Node_Id; |
8953 | -- Next expression from positional parameters of aggregate | |
c9a6b38f | 8954 | |
7c4f3267 BD |
8955 | Left_Justified : Boolean; |
8956 | -- Set True if we are filling the high order bits of the target | |
8957 | -- value (i.e. the value is left justified). | |
07fc65c4 | 8958 | |
7c4f3267 BD |
8959 | begin |
8960 | -- For little endian, we fill up the low order bits of the | |
8961 | -- target value. For big endian we fill up the high order bits | |
8962 | -- of the target value (which is a left justified modular | |
8963 | -- value). | |
68f640f2 | 8964 | |
7c4f3267 | 8965 | Left_Justified := Bytes_Big_Endian; |
c9a6b38f | 8966 | |
7c4f3267 | 8967 | -- Switch justification if using -gnatd8 |
c9a6b38f | 8968 | |
7c4f3267 BD |
8969 | if Debug_Flag_8 then |
8970 | Left_Justified := not Left_Justified; | |
8971 | end if; | |
c9a6b38f | 8972 | |
7c4f3267 | 8973 | -- Switch justfification if reverse storage order |
c9a6b38f | 8974 | |
7c4f3267 BD |
8975 | if Reverse_Storage_Order (Base_Type (Typ)) then |
8976 | Left_Justified := not Left_Justified; | |
8977 | end if; | |
07fc65c4 | 8978 | |
7c4f3267 BD |
8979 | if Left_Justified then |
8980 | Shift := Csiz * (Len - 1); | |
8981 | Incr := -Csiz; | |
8982 | else | |
8983 | Shift := 0; | |
8984 | Incr := +Csiz; | |
8985 | end if; | |
07fc65c4 | 8986 | |
7c4f3267 | 8987 | -- Loop to set the values |
fbf5a39b | 8988 | |
7c4f3267 BD |
8989 | if Len = 0 then |
8990 | Aggregate_Val := Uint_0; | |
8991 | else | |
8992 | Expr := First (Expressions (N)); | |
8993 | Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift; | |
8994 | ||
8995 | for J in 2 .. Len loop | |
8996 | Shift := Shift + Incr; | |
8997 | Next (Expr); | |
8998 | Aggregate_Val := | |
8999 | Aggregate_Val + | |
9000 | Get_Component_Val (Expr) * Uint_2 ** Shift; | |
9001 | end loop; | |
9002 | end if; | |
07fc65c4 | 9003 | |
7c4f3267 | 9004 | -- Now we can rewrite with the proper value |
07fc65c4 | 9005 | |
7c4f3267 BD |
9006 | Lit := Make_Integer_Literal (Loc, Intval => Aggregate_Val); |
9007 | Set_Print_In_Hex (Lit); | |
07fc65c4 | 9008 | |
7c4f3267 BD |
9009 | -- Construct the expression using this literal. Note that it |
9010 | -- is important to qualify the literal with its proper modular | |
9011 | -- type since universal integer does not have the required | |
9012 | -- range and also this is a left justified modular type, | |
9013 | -- which is important in the big-endian case. | |
07fc65c4 | 9014 | |
7c4f3267 BD |
9015 | Rewrite (N, |
9016 | Unchecked_Convert_To (Typ, | |
9017 | Make_Qualified_Expression (Loc, | |
9018 | Subtype_Mark => | |
9019 | New_Occurrence_Of (Packed_Array_Impl_Type (Typ), Loc), | |
9020 | Expression => Lit))); | |
07fc65c4 | 9021 | |
7c4f3267 BD |
9022 | Analyze_And_Resolve (N, Typ); |
9023 | return True; | |
9024 | end; | |
07fc65c4 GB |
9025 | end; |
9026 | end; | |
9027 | ||
9028 | exception | |
9029 | when Not_Handled => | |
9030 | return False; | |
9031 | end Packed_Array_Aggregate_Handled; | |
9032 | ||
fbf5a39b AC |
9033 | ---------------------------- |
9034 | -- Has_Mutable_Components -- | |
9035 | ---------------------------- | |
9036 | ||
9037 | function Has_Mutable_Components (Typ : Entity_Id) return Boolean is | |
9038 | Comp : Entity_Id; | |
bee475e2 | 9039 | Ctyp : Entity_Id; |
fbf5a39b AC |
9040 | |
9041 | begin | |
9042 | Comp := First_Component (Typ); | |
fbf5a39b | 9043 | while Present (Comp) loop |
bee475e2 EB |
9044 | Ctyp := Underlying_Type (Etype (Comp)); |
9045 | if Is_Record_Type (Ctyp) | |
9046 | and then Has_Discriminants (Ctyp) | |
9047 | and then not Is_Constrained (Ctyp) | |
fbf5a39b AC |
9048 | then |
9049 | return True; | |
9050 | end if; | |
9051 | ||
9052 | Next_Component (Comp); | |
9053 | end loop; | |
9054 | ||
9055 | return False; | |
9056 | end Has_Mutable_Components; | |
9057 | ||
07fc65c4 GB |
9058 | ------------------------------ |
9059 | -- Initialize_Discriminants -- | |
9060 | ------------------------------ | |
9061 | ||
9062 | procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is | |
9063 | Loc : constant Source_Ptr := Sloc (N); | |
9064 | Bas : constant Entity_Id := Base_Type (Typ); | |
9065 | Par : constant Entity_Id := Etype (Bas); | |
9066 | Decl : constant Node_Id := Parent (Par); | |
9067 | Ref : Node_Id; | |
9068 | ||
9069 | begin | |
9070 | if Is_Tagged_Type (Bas) | |
9071 | and then Is_Derived_Type (Bas) | |
9072 | and then Has_Discriminants (Par) | |
9073 | and then Has_Discriminants (Bas) | |
9074 | and then Number_Discriminants (Bas) /= Number_Discriminants (Par) | |
9075 | and then Nkind (Decl) = N_Full_Type_Declaration | |
9076 | and then Nkind (Type_Definition (Decl)) = N_Record_Definition | |
36a66365 AC |
9077 | and then |
9078 | Present (Variant_Part (Component_List (Type_Definition (Decl)))) | |
07fc65c4 GB |
9079 | and then Nkind (N) /= N_Extension_Aggregate |
9080 | then | |
9081 | ||
fbf5a39b | 9082 | -- Call init proc to set discriminants. |
07fc65c4 GB |
9083 | -- There should eventually be a special procedure for this ??? |
9084 | ||
e4494292 | 9085 | Ref := New_Occurrence_Of (Defining_Identifier (N), Loc); |
07fc65c4 GB |
9086 | Insert_Actions_After (N, |
9087 | Build_Initialization_Call (Sloc (N), Ref, Typ)); | |
9088 | end if; | |
9089 | end Initialize_Discriminants; | |
9090 | ||
3cf3e5c6 AC |
9091 | ---------------- |
9092 | -- Must_Slide -- | |
9093 | ---------------- | |
9094 | ||
9095 | function Must_Slide | |
6732c403 ES |
9096 | (Aggr : Node_Id; |
9097 | Obj_Type : Entity_Id; | |
3cf3e5c6 AC |
9098 | Typ : Entity_Id) return Boolean |
9099 | is | |
3cf3e5c6 | 9100 | begin |
3b9fa2df ES |
9101 | -- No sliding if the type of the object is not established yet, if it is |
9102 | -- an unconstrained type whose actual subtype comes from the aggregate, | |
6732c403 ES |
9103 | -- or if the two types are identical. If the aggregate contains only |
9104 | -- an Others_Clause it gets its type from the context and no sliding | |
9105 | -- is involved either. | |
3cf3e5c6 AC |
9106 | |
9107 | if not Is_Array_Type (Obj_Type) then | |
9108 | return False; | |
9109 | ||
9110 | elsif not Is_Constrained (Obj_Type) then | |
9111 | return False; | |
9112 | ||
9113 | elsif Typ = Obj_Type then | |
9114 | return False; | |
9115 | ||
6732c403 ES |
9116 | elsif Is_Others_Aggregate (Aggr) then |
9117 | return False; | |
9118 | ||
3cf3e5c6 AC |
9119 | else |
9120 | -- Sliding can only occur along the first dimension | |
6732c403 ES |
9121 | -- If any the bounds of non-static sliding is required |
9122 | -- to force potential range checks. | |
3cf3e5c6 | 9123 | |
7c4f3267 BD |
9124 | declare |
9125 | Bounds1 : constant Range_Nodes := | |
9126 | Get_Index_Bounds (First_Index (Typ)); | |
9127 | Bounds2 : constant Range_Nodes := | |
9128 | Get_Index_Bounds (First_Index (Obj_Type)); | |
3cf3e5c6 | 9129 | |
7c4f3267 BD |
9130 | begin |
9131 | if not Is_OK_Static_Expression (Bounds1.First) or else | |
9132 | not Is_OK_Static_Expression (Bounds2.First) or else | |
9133 | not Is_OK_Static_Expression (Bounds1.Last) or else | |
9134 | not Is_OK_Static_Expression (Bounds2.Last) | |
9135 | then | |
6732c403 ES |
9136 | return True; |
9137 | ||
7c4f3267 BD |
9138 | else |
9139 | return Expr_Value (Bounds1.First) /= Expr_Value (Bounds2.First) | |
9140 | or else | |
9141 | Expr_Value (Bounds1.Last) /= Expr_Value (Bounds2.Last); | |
9142 | end if; | |
9143 | end; | |
3cf3e5c6 AC |
9144 | end if; |
9145 | end Must_Slide; | |
9146 | ||
70482933 RK |
9147 | --------------------- |
9148 | -- Sort_Case_Table -- | |
9149 | --------------------- | |
9150 | ||
9151 | procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is | |
fbf5a39b AC |
9152 | L : constant Int := Case_Table'First; |
9153 | U : constant Int := Case_Table'Last; | |
70482933 RK |
9154 | K : Int; |
9155 | J : Int; | |
9156 | T : Case_Bounds; | |
9157 | ||
9158 | begin | |
9159 | K := L; | |
70482933 RK |
9160 | while K /= U loop |
9161 | T := Case_Table (K + 1); | |
70482933 | 9162 | |
5277cab6 | 9163 | J := K + 1; |
70482933 RK |
9164 | while J /= L |
9165 | and then Expr_Value (Case_Table (J - 1).Choice_Lo) > | |
9166 | Expr_Value (T.Choice_Lo) | |
9167 | loop | |
9168 | Case_Table (J) := Case_Table (J - 1); | |
9169 | J := J - 1; | |
9170 | end loop; | |
9171 | ||
9172 | Case_Table (J) := T; | |
9173 | K := K + 1; | |
9174 | end loop; | |
9175 | end Sort_Case_Table; | |
9176 | ||
0f95b178 JM |
9177 | ---------------------------- |
9178 | -- Static_Array_Aggregate -- | |
9179 | ---------------------------- | |
9180 | ||
9181 | function Static_Array_Aggregate (N : Node_Id) return Boolean is | |
2401c98f HK |
9182 | function Is_Static_Component (Nod : Node_Id) return Boolean; |
9183 | -- Return True if Nod has a compile-time known value and can be passed | |
9184 | -- as is to the back-end without further expansion. | |
2a1838cd EB |
9185 | |
9186 | --------------------------- | |
9187 | -- Is_Static_Component -- | |
9188 | --------------------------- | |
9189 | ||
2401c98f | 9190 | function Is_Static_Component (Nod : Node_Id) return Boolean is |
2a1838cd | 9191 | begin |
4a08c95c | 9192 | if Nkind (Nod) in N_Integer_Literal | N_Real_Literal then |
2a1838cd EB |
9193 | return True; |
9194 | ||
2401c98f HK |
9195 | elsif Is_Entity_Name (Nod) |
9196 | and then Present (Entity (Nod)) | |
9197 | and then Ekind (Entity (Nod)) = E_Enumeration_Literal | |
2a1838cd EB |
9198 | then |
9199 | return True; | |
9200 | ||
2401c98f HK |
9201 | elsif Nkind (Nod) = N_Aggregate |
9202 | and then Compile_Time_Known_Aggregate (Nod) | |
2a1838cd EB |
9203 | then |
9204 | return True; | |
9205 | ||
9206 | else | |
9207 | return False; | |
9208 | end if; | |
9209 | end Is_Static_Component; | |
9210 | ||
2401c98f HK |
9211 | -- Local variables |
9212 | ||
9213 | Bounds : constant Node_Id := Aggregate_Bounds (N); | |
9214 | Typ : constant Entity_Id := Etype (N); | |
0f95b178 | 9215 | |
2401c98f HK |
9216 | Agg : Node_Id; |
9217 | Expr : Node_Id; | |
9218 | Lo : Node_Id; | |
9219 | Hi : Node_Id; | |
0f95b178 | 9220 | |
2a1838cd EB |
9221 | -- Start of processing for Static_Array_Aggregate |
9222 | ||
0f95b178 | 9223 | begin |
2a1838cd | 9224 | if Is_Packed (Typ) or else Has_Discriminants (Component_Type (Typ)) then |
0f95b178 JM |
9225 | return False; |
9226 | end if; | |
9227 | ||
9228 | if Present (Bounds) | |
9229 | and then Nkind (Bounds) = N_Range | |
9230 | and then Nkind (Low_Bound (Bounds)) = N_Integer_Literal | |
9231 | and then Nkind (High_Bound (Bounds)) = N_Integer_Literal | |
9232 | then | |
9233 | Lo := Low_Bound (Bounds); | |
9234 | Hi := High_Bound (Bounds); | |
9235 | ||
9236 | if No (Component_Associations (N)) then | |
9237 | ||
2a1838cd | 9238 | -- Verify that all components are static |
0f95b178 JM |
9239 | |
9240 | Expr := First (Expressions (N)); | |
9241 | while Present (Expr) loop | |
2a1838cd | 9242 | if not Is_Static_Component (Expr) then |
0f95b178 JM |
9243 | return False; |
9244 | end if; | |
9245 | ||
9246 | Next (Expr); | |
9247 | end loop; | |
9248 | ||
9249 | return True; | |
9250 | ||
9251 | else | |
9252 | -- We allow only a single named association, either a static | |
9253 | -- range or an others_clause, with a static expression. | |
9254 | ||
9255 | Expr := First (Component_Associations (N)); | |
9256 | ||
9257 | if Present (Expressions (N)) then | |
9258 | return False; | |
9259 | ||
9260 | elsif Present (Next (Expr)) then | |
9261 | return False; | |
9262 | ||
00f45f30 | 9263 | elsif Present (Next (First (Choice_List (Expr)))) then |
0f95b178 JM |
9264 | return False; |
9265 | ||
9266 | else | |
d7f94401 AC |
9267 | -- The aggregate is static if all components are literals, |
9268 | -- or else all its components are static aggregates for the | |
fc534c1c ES |
9269 | -- component type. We also limit the size of a static aggregate |
9270 | -- to prevent runaway static expressions. | |
0f95b178 | 9271 | |
2a1838cd | 9272 | if not Is_Static_Component (Expression (Expr)) then |
0f95b178 | 9273 | return False; |
6a2e4f0b | 9274 | end if; |
fc534c1c | 9275 | |
eaf6e63a | 9276 | if not Aggr_Size_OK (N) then |
fc534c1c | 9277 | return False; |
0f95b178 JM |
9278 | end if; |
9279 | ||
9280 | -- Create a positional aggregate with the right number of | |
9281 | -- copies of the expression. | |
9282 | ||
9283 | Agg := Make_Aggregate (Sloc (N), New_List, No_List); | |
9284 | ||
9285 | for I in UI_To_Int (Intval (Lo)) .. UI_To_Int (Intval (Hi)) | |
9286 | loop | |
37368818 | 9287 | Append_To (Expressions (Agg), New_Copy (Expression (Expr))); |
597d7158 | 9288 | |
9b4b0a1a GD |
9289 | -- The copied expression must be analyzed and resolved. |
9290 | -- Besides setting the type, this ensures that static | |
9291 | -- expressions are appropriately marked as such. | |
597d7158 | 9292 | |
9b4b0a1a GD |
9293 | Analyze_And_Resolve |
9294 | (Last (Expressions (Agg)), Component_Type (Typ)); | |
0f95b178 JM |
9295 | end loop; |
9296 | ||
9297 | Set_Aggregate_Bounds (Agg, Bounds); | |
9298 | Set_Etype (Agg, Typ); | |
9299 | Set_Analyzed (Agg); | |
9300 | Rewrite (N, Agg); | |
9301 | Set_Compile_Time_Known_Aggregate (N); | |
9302 | ||
9303 | return True; | |
9304 | end if; | |
9305 | end if; | |
9306 | ||
9307 | else | |
9308 | return False; | |
9309 | end if; | |
9310 | end Static_Array_Aggregate; | |
9b4b0a1a | 9311 | |
937e9676 AC |
9312 | ---------------------------------- |
9313 | -- Two_Dim_Packed_Array_Handled -- | |
9314 | ---------------------------------- | |
9315 | ||
9316 | function Two_Dim_Packed_Array_Handled (N : Node_Id) return Boolean is | |
9317 | Loc : constant Source_Ptr := Sloc (N); | |
9318 | Typ : constant Entity_Id := Etype (N); | |
9319 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
9320 | Comp_Size : constant Int := UI_To_Int (Component_Size (Typ)); | |
9321 | Packed_Array : constant Entity_Id := | |
9322 | Packed_Array_Impl_Type (Base_Type (Typ)); | |
9323 | ||
9324 | One_Comp : Node_Id; | |
9325 | -- Expression in original aggregate | |
9326 | ||
9327 | One_Dim : Node_Id; | |
9328 | -- One-dimensional subaggregate | |
9329 | ||
9330 | begin | |
9331 | ||
9332 | -- For now, only deal with cases where an integral number of elements | |
9333 | -- fit in a single byte. This includes the most common boolean case. | |
9334 | ||
9335 | if not (Comp_Size = 1 or else | |
9336 | Comp_Size = 2 or else | |
9337 | Comp_Size = 4) | |
9338 | then | |
9339 | return False; | |
9340 | end if; | |
9341 | ||
c42006e9 | 9342 | Convert_To_Positional (N, Handle_Bit_Packed => True); |
937e9676 AC |
9343 | |
9344 | -- Verify that all components are static | |
9345 | ||
9346 | if Nkind (N) = N_Aggregate | |
9347 | and then Compile_Time_Known_Aggregate (N) | |
9348 | then | |
9349 | null; | |
9350 | ||
9351 | -- The aggregate may have been reanalyzed and converted already | |
9352 | ||
9353 | elsif Nkind (N) /= N_Aggregate then | |
9354 | return True; | |
9355 | ||
9356 | -- If component associations remain, the aggregate is not static | |
9357 | ||
9358 | elsif Present (Component_Associations (N)) then | |
9359 | return False; | |
9360 | ||
9361 | else | |
9362 | One_Dim := First (Expressions (N)); | |
9363 | while Present (One_Dim) loop | |
9364 | if Present (Component_Associations (One_Dim)) then | |
9365 | return False; | |
9366 | end if; | |
9367 | ||
9368 | One_Comp := First (Expressions (One_Dim)); | |
9369 | while Present (One_Comp) loop | |
9370 | if not Is_OK_Static_Expression (One_Comp) then | |
9371 | return False; | |
9372 | end if; | |
9373 | ||
9374 | Next (One_Comp); | |
9375 | end loop; | |
9376 | ||
9377 | Next (One_Dim); | |
9378 | end loop; | |
9379 | end if; | |
9380 | ||
9381 | -- Two-dimensional aggregate is now fully positional so pack one | |
9382 | -- dimension to create a static one-dimensional array, and rewrite | |
9383 | -- as an unchecked conversion to the original type. | |
9384 | ||
9385 | declare | |
9386 | Byte_Size : constant Int := UI_To_Int (Component_Size (Packed_Array)); | |
9387 | -- The packed array type is a byte array | |
9388 | ||
9389 | Packed_Num : Nat; | |
9390 | -- Number of components accumulated in current byte | |
9391 | ||
9392 | Comps : List_Id; | |
9393 | -- Assembled list of packed values for equivalent aggregate | |
9394 | ||
9395 | Comp_Val : Uint; | |
9396 | -- Integer value of component | |
9397 | ||
9398 | Incr : Int; | |
9399 | -- Step size for packing | |
9400 | ||
9401 | Init_Shift : Int; | |
9402 | -- Endian-dependent start position for packing | |
9403 | ||
9404 | Shift : Int; | |
9405 | -- Current insertion position | |
9406 | ||
9407 | Val : Int; | |
9408 | -- Component of packed array being assembled | |
9409 | ||
9410 | begin | |
9411 | Comps := New_List; | |
9412 | Val := 0; | |
9413 | Packed_Num := 0; | |
9414 | ||
64ac53f4 | 9415 | -- Account for endianness. See corresponding comment in |
937e9676 AC |
9416 | -- Packed_Array_Aggregate_Handled concerning the following. |
9417 | ||
9418 | if Bytes_Big_Endian | |
9419 | xor Debug_Flag_8 | |
9420 | xor Reverse_Storage_Order (Base_Type (Typ)) | |
9421 | then | |
9422 | Init_Shift := Byte_Size - Comp_Size; | |
9423 | Incr := -Comp_Size; | |
9424 | else | |
9425 | Init_Shift := 0; | |
9426 | Incr := +Comp_Size; | |
9427 | end if; | |
9428 | ||
9429 | -- Iterate over each subaggregate | |
9430 | ||
9431 | Shift := Init_Shift; | |
9432 | One_Dim := First (Expressions (N)); | |
9433 | while Present (One_Dim) loop | |
9434 | One_Comp := First (Expressions (One_Dim)); | |
9435 | while Present (One_Comp) loop | |
9436 | if Packed_Num = Byte_Size / Comp_Size then | |
9437 | ||
9438 | -- Byte is complete, add to list of expressions | |
9439 | ||
9440 | Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps); | |
9441 | Val := 0; | |
9442 | Shift := Init_Shift; | |
9443 | Packed_Num := 0; | |
9444 | ||
9445 | else | |
9446 | Comp_Val := Expr_Rep_Value (One_Comp); | |
9447 | ||
9448 | -- Adjust for bias, and strip proper number of bits | |
9449 | ||
9450 | if Has_Biased_Representation (Ctyp) then | |
9451 | Comp_Val := Comp_Val - Expr_Value (Type_Low_Bound (Ctyp)); | |
9452 | end if; | |
9453 | ||
9454 | Comp_Val := Comp_Val mod Uint_2 ** Comp_Size; | |
9455 | Val := UI_To_Int (Val + Comp_Val * Uint_2 ** Shift); | |
9456 | Shift := Shift + Incr; | |
99859ea7 | 9457 | Next (One_Comp); |
937e9676 AC |
9458 | Packed_Num := Packed_Num + 1; |
9459 | end if; | |
9460 | end loop; | |
9461 | ||
99859ea7 | 9462 | Next (One_Dim); |
937e9676 AC |
9463 | end loop; |
9464 | ||
9465 | if Packed_Num > 0 then | |
9466 | ||
9467 | -- Add final incomplete byte if present | |
9468 | ||
9469 | Append (Make_Integer_Literal (Sloc (One_Dim), Val), Comps); | |
9470 | end if; | |
9471 | ||
9472 | Rewrite (N, | |
9473 | Unchecked_Convert_To (Typ, | |
9474 | Make_Qualified_Expression (Loc, | |
9475 | Subtype_Mark => New_Occurrence_Of (Packed_Array, Loc), | |
9476 | Expression => Make_Aggregate (Loc, Expressions => Comps)))); | |
9477 | Analyze_And_Resolve (N); | |
9478 | return True; | |
9479 | end; | |
9480 | end Two_Dim_Packed_Array_Handled; | |
9481 | ||
70482933 | 9482 | end Exp_Aggr; |