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