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