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