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