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