]>
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 | -- -- | |
fbf5a39b | 9 | -- Copyright (C) 1992-2003 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- -- | |
13 | -- ware Foundation; either version 2, or (at your option) any later ver- -- | |
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 -- | |
18 | -- Public License distributed with GNAT; see file COPYING. If not, write -- | |
19 | -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, -- | |
20 | -- MA 02111-1307, USA. -- | |
21 | -- -- | |
22 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 23 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
70482933 RK |
24 | -- -- |
25 | ------------------------------------------------------------------------------ | |
26 | ||
27 | with Atree; use Atree; | |
28 | with Checks; use Checks; | |
07fc65c4 | 29 | with Debug; use Debug; |
70482933 RK |
30 | with Einfo; use Einfo; |
31 | with Elists; use Elists; | |
32 | with Expander; use Expander; | |
33 | with Exp_Util; use Exp_Util; | |
34 | with Exp_Ch3; use Exp_Ch3; | |
35 | with Exp_Ch7; use Exp_Ch7; | |
c45b6ae0 | 36 | with Exp_Ch9; use Exp_Ch9; |
70482933 RK |
37 | with Freeze; use Freeze; |
38 | with Hostparm; use Hostparm; | |
39 | with Itypes; use Itypes; | |
07fc65c4 | 40 | with Lib; use Lib; |
70482933 RK |
41 | with Nmake; use Nmake; |
42 | with Nlists; use Nlists; | |
43 | with Restrict; use Restrict; | |
44 | with Rtsfind; use Rtsfind; | |
07fc65c4 | 45 | with Ttypes; use Ttypes; |
70482933 RK |
46 | with Sem; use Sem; |
47 | with Sem_Ch3; use Sem_Ch3; | |
48 | with Sem_Eval; use Sem_Eval; | |
49 | with Sem_Res; use Sem_Res; | |
50 | with Sem_Util; use Sem_Util; | |
51 | with Sinfo; use Sinfo; | |
52 | with Snames; use Snames; | |
53 | with Stand; use Stand; | |
54 | with Tbuild; use Tbuild; | |
55 | with Uintp; use Uintp; | |
56 | ||
57 | package body Exp_Aggr is | |
58 | ||
59 | type Case_Bounds is record | |
60 | Choice_Lo : Node_Id; | |
61 | Choice_Hi : Node_Id; | |
62 | Choice_Node : Node_Id; | |
63 | end record; | |
64 | ||
65 | type Case_Table_Type is array (Nat range <>) of Case_Bounds; | |
66 | -- Table type used by Check_Case_Choices procedure | |
67 | ||
68 | procedure Sort_Case_Table (Case_Table : in out Case_Table_Type); | |
69 | -- Sort the Case Table using the Lower Bound of each Choice as the key. | |
70 | -- A simple insertion sort is used since the number of choices in a case | |
71 | -- statement of variant part will usually be small and probably in near | |
72 | -- sorted order. | |
73 | ||
65356e64 | 74 | function Has_Default_Init_Comps (N : Node_Id) return Boolean; |
19f0526a AC |
75 | -- N is an aggregate (record or array). Checks the presence of default |
76 | -- initialization (<>) in any component (Ada0Y: AI-287) | |
65356e64 | 77 | |
70482933 RK |
78 | ------------------------------------------------------ |
79 | -- Local subprograms for Record Aggregate Expansion -- | |
80 | ------------------------------------------------------ | |
81 | ||
82 | procedure Expand_Record_Aggregate | |
83 | (N : Node_Id; | |
84 | Orig_Tag : Node_Id := Empty; | |
85 | Parent_Expr : Node_Id := Empty); | |
86 | -- This is the top level procedure for record aggregate expansion. | |
87 | -- Expansion for record aggregates needs expand aggregates for tagged | |
88 | -- record types. Specifically Expand_Record_Aggregate adds the Tag | |
89 | -- field in front of the Component_Association list that was created | |
90 | -- during resolution by Resolve_Record_Aggregate. | |
91 | -- | |
92 | -- N is the record aggregate node. | |
93 | -- Orig_Tag is the value of the Tag that has to be provided for this | |
94 | -- specific aggregate. It carries the tag corresponding to the type | |
95 | -- of the outermost aggregate during the recursive expansion | |
96 | -- Parent_Expr is the ancestor part of the original extension | |
97 | -- aggregate | |
98 | ||
99 | procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id); | |
100 | -- N is an N_Aggregate of a N_Extension_Aggregate. Typ is the type of | |
101 | -- the aggregate. Transform the given aggregate into a sequence of | |
102 | -- assignments component per component. | |
103 | ||
104 | function Build_Record_Aggr_Code | |
65356e64 AC |
105 | (N : Node_Id; |
106 | Typ : Entity_Id; | |
107 | Target : Node_Id; | |
108 | Flist : Node_Id := Empty; | |
109 | Obj : Entity_Id := Empty; | |
d05ef0ab | 110 | Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id; |
70482933 RK |
111 | -- N is an N_Aggregate or a N_Extension_Aggregate. Typ is the type |
112 | -- of the aggregate. Target is an expression containing the | |
113 | -- location on which the component by component assignments will | |
114 | -- take place. Returns the list of assignments plus all other | |
115 | -- adjustments needed for tagged and controlled types. Flist is an | |
116 | -- expression representing the finalization list on which to | |
117 | -- attach the controlled components if any. Obj is present in the | |
118 | -- object declaration and dynamic allocation cases, it contains | |
119 | -- an entity that allows to know if the value being created needs to be | |
120 | -- attached to the final list in case of pragma finalize_Storage_Only. | |
65356e64 AC |
121 | -- Is_Limited_Ancestor_Expansion indicates that the function has been |
122 | -- called recursively to expand the limited ancestor to avoid copying it. | |
70482933 | 123 | |
fbf5a39b AC |
124 | function Has_Mutable_Components (Typ : Entity_Id) return Boolean; |
125 | -- Return true if one of the component is of a discriminated type with | |
126 | -- defaults. An aggregate for a type with mutable components must be | |
127 | -- expanded into individual assignments. | |
128 | ||
07fc65c4 GB |
129 | procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id); |
130 | -- If the type of the aggregate is a type extension with renamed discrimi- | |
131 | -- nants, we must initialize the hidden discriminants of the parent. | |
132 | -- Otherwise, the target object must not be initialized. The discriminants | |
133 | -- are initialized by calling the initialization procedure for the type. | |
134 | -- This is incorrect if the initialization of other components has any | |
135 | -- side effects. We restrict this call to the case where the parent type | |
136 | -- has a variant part, because this is the only case where the hidden | |
137 | -- discriminants are accessed, namely when calling discriminant checking | |
138 | -- functions of the parent type, and when applying a stream attribute to | |
139 | -- an object of the derived type. | |
140 | ||
70482933 | 141 | ----------------------------------------------------- |
07fc65c4 | 142 | -- Local Subprograms for Array Aggregate Expansion -- |
70482933 RK |
143 | ----------------------------------------------------- |
144 | ||
07fc65c4 GB |
145 | procedure Convert_To_Positional |
146 | (N : Node_Id; | |
fbf5a39b | 147 | Max_Others_Replicate : Nat := 5; |
07fc65c4 GB |
148 | Handle_Bit_Packed : Boolean := False); |
149 | -- If possible, convert named notation to positional notation. This | |
150 | -- conversion is possible only in some static cases. If the conversion | |
151 | -- is possible, then N is rewritten with the analyzed converted | |
152 | -- aggregate. The parameter Max_Others_Replicate controls the maximum | |
153 | -- number of values corresponding to an others choice that will be | |
154 | -- converted to positional notation (the default of 5 is the normal | |
155 | -- limit, and reflects the fact that normally the loop is better than | |
156 | -- a lot of separate assignments). Note that this limit gets overridden | |
157 | -- in any case if either of the restrictions No_Elaboration_Code or | |
158 | -- No_Implicit_Loops is set. The parameter Handle_Bit_Packed is usually | |
159 | -- set False (since we do not expect the back end to handle bit packed | |
160 | -- arrays, so the normal case of conversion is pointless), but in the | |
161 | -- special case of a call from Packed_Array_Aggregate_Handled, we set | |
162 | -- this parameter to True, since these are cases we handle in there. | |
163 | ||
70482933 RK |
164 | procedure Expand_Array_Aggregate (N : Node_Id); |
165 | -- This is the top-level routine to perform array aggregate expansion. | |
166 | -- N is the N_Aggregate node to be expanded. | |
167 | ||
168 | function Backend_Processing_Possible (N : Node_Id) return Boolean; | |
169 | -- This function checks if array aggregate N can be processed directly | |
170 | -- by Gigi. If this is the case True is returned. | |
171 | ||
172 | function Build_Array_Aggr_Code | |
173 | (N : Node_Id; | |
c45b6ae0 | 174 | Ctype : Entity_Id; |
70482933 RK |
175 | Index : Node_Id; |
176 | Into : Node_Id; | |
177 | Scalar_Comp : Boolean; | |
178 | Indices : List_Id := No_List; | |
d05ef0ab | 179 | Flist : Node_Id := Empty) return List_Id; |
70482933 RK |
180 | -- This recursive routine returns a list of statements containing the |
181 | -- loops and assignments that are needed for the expansion of the array | |
182 | -- aggregate N. | |
183 | -- | |
fbf5a39b AC |
184 | -- N is the (sub-)aggregate node to be expanded into code. This node |
185 | -- has been fully analyzed, and its Etype is properly set. | |
70482933 RK |
186 | -- |
187 | -- Index is the index node corresponding to the array sub-aggregate N. | |
188 | -- | |
189 | -- Into is the target expression into which we are copying the aggregate. | |
fbf5a39b AC |
190 | -- Note that this node may not have been analyzed yet, and so the Etype |
191 | -- field may not be set. | |
70482933 RK |
192 | -- |
193 | -- Scalar_Comp is True if the component type of the aggregate is scalar. | |
194 | -- | |
195 | -- Indices is the current list of expressions used to index the | |
196 | -- object we are writing into. | |
197 | -- | |
198 | -- Flist is an expression representing the finalization list on which | |
199 | -- to attach the controlled components if any. | |
200 | ||
201 | function Number_Of_Choices (N : Node_Id) return Nat; | |
202 | -- Returns the number of discrete choices (not including the others choice | |
203 | -- if present) contained in (sub-)aggregate N. | |
204 | ||
205 | function Late_Expansion | |
206 | (N : Node_Id; | |
207 | Typ : Entity_Id; | |
208 | Target : Node_Id; | |
209 | Flist : Node_Id := Empty; | |
d05ef0ab | 210 | Obj : Entity_Id := Empty) return List_Id; |
70482933 RK |
211 | -- N is a nested (record or array) aggregate that has been marked |
212 | -- with 'Delay_Expansion'. Typ is the expected type of the | |
213 | -- aggregate and Target is a (duplicable) expression that will | |
214 | -- hold the result of the aggregate expansion. Flist is the | |
215 | -- finalization list to be used to attach controlled | |
216 | -- components. 'Obj' when non empty, carries the original object | |
217 | -- being initialized in order to know if it needs to be attached | |
218 | -- to the previous parameter which may not be the case when | |
219 | -- Finalize_Storage_Only is set. Basically this procedure is used | |
220 | -- to implement top-down expansions of nested aggregates. This is | |
221 | -- necessary for avoiding temporaries at each level as well as for | |
222 | -- propagating the right internal finalization list. | |
223 | ||
224 | function Make_OK_Assignment_Statement | |
225 | (Sloc : Source_Ptr; | |
226 | Name : Node_Id; | |
d05ef0ab | 227 | Expression : Node_Id) return Node_Id; |
70482933 RK |
228 | -- This is like Make_Assignment_Statement, except that Assignment_OK |
229 | -- is set in the left operand. All assignments built by this unit | |
230 | -- use this routine. This is needed to deal with assignments to | |
231 | -- initialized constants that are done in place. | |
232 | ||
07fc65c4 GB |
233 | function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean; |
234 | -- Given an array aggregate, this function handles the case of a packed | |
235 | -- array aggregate with all constant values, where the aggregate can be | |
236 | -- evaluated at compile time. If this is possible, then N is rewritten | |
237 | -- to be its proper compile time value with all the components properly | |
238 | -- assembled. The expression is analyzed and resolved and True is | |
239 | -- returned. If this transformation is not possible, N is unchanged | |
240 | -- and False is returned | |
241 | ||
242 | function Safe_Slice_Assignment (N : Node_Id) return Boolean; | |
70482933 RK |
243 | -- If a slice assignment has an aggregate with a single others_choice, |
244 | -- the assignment can be done in place even if bounds are not static, | |
245 | -- by converting it into a loop over the discrete range of the slice. | |
246 | ||
247 | --------------------------------- | |
248 | -- Backend_Processing_Possible -- | |
249 | --------------------------------- | |
250 | ||
251 | -- Backend processing by Gigi/gcc is possible only if all the following | |
252 | -- conditions are met: | |
253 | ||
254 | -- 1. N is fully positional | |
255 | ||
256 | -- 2. N is not a bit-packed array aggregate; | |
257 | ||
258 | -- 3. The size of N's array type must be known at compile time. Note | |
259 | -- that this implies that the component size is also known | |
260 | ||
261 | -- 4. The array type of N does not follow the Fortran layout convention | |
262 | -- or if it does it must be 1 dimensional. | |
263 | ||
264 | -- 5. The array component type is tagged, which may necessitate | |
265 | -- reassignment of proper tags. | |
266 | ||
267 | function Backend_Processing_Possible (N : Node_Id) return Boolean is | |
268 | Typ : constant Entity_Id := Etype (N); | |
269 | -- Typ is the correct constrained array subtype of the aggregate. | |
270 | ||
271 | function Static_Check (N : Node_Id; Index : Node_Id) return Boolean; | |
272 | -- Recursively checks that N is fully positional, returns true if so. | |
273 | ||
274 | ------------------ | |
275 | -- Static_Check -- | |
276 | ------------------ | |
277 | ||
278 | function Static_Check (N : Node_Id; Index : Node_Id) return Boolean is | |
279 | Expr : Node_Id; | |
280 | ||
281 | begin | |
282 | -- Check for component associations | |
283 | ||
284 | if Present (Component_Associations (N)) then | |
285 | return False; | |
286 | end if; | |
287 | ||
288 | -- Recurse to check subaggregates, which may appear in qualified | |
289 | -- expressions. If delayed, the front-end will have to expand. | |
290 | ||
291 | Expr := First (Expressions (N)); | |
292 | ||
293 | while Present (Expr) loop | |
294 | ||
295 | if Is_Delayed_Aggregate (Expr) then | |
296 | return False; | |
297 | end if; | |
298 | ||
299 | if Present (Next_Index (Index)) | |
300 | and then not Static_Check (Expr, Next_Index (Index)) | |
301 | then | |
302 | return False; | |
303 | end if; | |
304 | ||
305 | Next (Expr); | |
306 | end loop; | |
307 | ||
308 | return True; | |
309 | end Static_Check; | |
310 | ||
311 | -- Start of processing for Backend_Processing_Possible | |
312 | ||
313 | begin | |
314 | -- Checks 2 (array must not be bit packed) | |
315 | ||
316 | if Is_Bit_Packed_Array (Typ) then | |
317 | return False; | |
318 | end if; | |
319 | ||
320 | -- Checks 4 (array must not be multi-dimensional Fortran case) | |
321 | ||
322 | if Convention (Typ) = Convention_Fortran | |
323 | and then Number_Dimensions (Typ) > 1 | |
324 | then | |
325 | return False; | |
326 | end if; | |
327 | ||
328 | -- Checks 3 (size of array must be known at compile time) | |
329 | ||
330 | if not Size_Known_At_Compile_Time (Typ) then | |
331 | return False; | |
332 | end if; | |
333 | ||
334 | -- Checks 1 (aggregate must be fully positional) | |
335 | ||
336 | if not Static_Check (N, First_Index (Typ)) then | |
337 | return False; | |
338 | end if; | |
339 | ||
340 | -- Checks 5 (if the component type is tagged, then we may need | |
341 | -- to do tag adjustments; perhaps this should be refined to | |
342 | -- check for any component associations that actually | |
343 | -- need tag adjustment, along the lines of the test that's | |
344 | -- done in Has_Delayed_Nested_Aggregate_Or_Tagged_Comps | |
345 | -- for record aggregates with tagged components, but not | |
346 | -- clear whether it's worthwhile ???; in the case of the | |
347 | -- JVM, object tags are handled implicitly) | |
348 | ||
349 | if Is_Tagged_Type (Component_Type (Typ)) and then not Java_VM then | |
350 | return False; | |
351 | end if; | |
352 | ||
353 | -- Backend processing is possible | |
354 | ||
355 | Set_Compile_Time_Known_Aggregate (N, True); | |
356 | Set_Size_Known_At_Compile_Time (Etype (N), True); | |
357 | return True; | |
358 | end Backend_Processing_Possible; | |
359 | ||
360 | --------------------------- | |
361 | -- Build_Array_Aggr_Code -- | |
362 | --------------------------- | |
363 | ||
364 | -- The code that we generate from a one dimensional aggregate is | |
365 | ||
366 | -- 1. If the sub-aggregate contains discrete choices we | |
367 | ||
368 | -- (a) Sort the discrete choices | |
369 | ||
370 | -- (b) Otherwise for each discrete choice that specifies a range we | |
371 | -- emit a loop. If a range specifies a maximum of three values, or | |
372 | -- we are dealing with an expression we emit a sequence of | |
373 | -- assignments instead of a loop. | |
374 | ||
375 | -- (c) Generate the remaining loops to cover the others choice if any. | |
376 | ||
377 | -- 2. If the aggregate contains positional elements we | |
378 | ||
379 | -- (a) translate the positional elements in a series of assignments. | |
380 | ||
381 | -- (b) Generate a final loop to cover the others choice if any. | |
382 | -- Note that this final loop has to be a while loop since the case | |
383 | ||
384 | -- L : Integer := Integer'Last; | |
385 | -- H : Integer := Integer'Last; | |
386 | -- A : array (L .. H) := (1, others =>0); | |
387 | ||
388 | -- cannot be handled by a for loop. Thus for the following | |
389 | ||
390 | -- array (L .. H) := (.. positional elements.., others =>E); | |
391 | ||
392 | -- we always generate something like: | |
393 | ||
07fc65c4 GB |
394 | -- J : Index_Type := Index_Of_Last_Positional_Element; |
395 | -- while J < H loop | |
396 | -- J := Index_Base'Succ (J) | |
397 | -- Tmp (J) := E; | |
70482933 RK |
398 | -- end loop; |
399 | ||
400 | function Build_Array_Aggr_Code | |
401 | (N : Node_Id; | |
c45b6ae0 | 402 | Ctype : Entity_Id; |
70482933 RK |
403 | Index : Node_Id; |
404 | Into : Node_Id; | |
405 | Scalar_Comp : Boolean; | |
406 | Indices : List_Id := No_List; | |
d05ef0ab | 407 | Flist : Node_Id := Empty) return List_Id |
70482933 RK |
408 | is |
409 | Loc : constant Source_Ptr := Sloc (N); | |
410 | Index_Base : constant Entity_Id := Base_Type (Etype (Index)); | |
411 | Index_Base_L : constant Node_Id := Type_Low_Bound (Index_Base); | |
412 | Index_Base_H : constant Node_Id := Type_High_Bound (Index_Base); | |
413 | ||
414 | function Add (Val : Int; To : Node_Id) return Node_Id; | |
415 | -- Returns an expression where Val is added to expression To, | |
416 | -- unless To+Val is provably out of To's base type range. | |
417 | -- To must be an already analyzed expression. | |
418 | ||
419 | function Empty_Range (L, H : Node_Id) return Boolean; | |
420 | -- Returns True if the range defined by L .. H is certainly empty. | |
421 | ||
422 | function Equal (L, H : Node_Id) return Boolean; | |
423 | -- Returns True if L = H for sure. | |
424 | ||
425 | function Index_Base_Name return Node_Id; | |
426 | -- Returns a new reference to the index type name. | |
427 | ||
428 | function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id; | |
fbf5a39b AC |
429 | -- Ind must be a side-effect free expression. If the input aggregate |
430 | -- N to Build_Loop contains no sub-aggregates, then this function | |
431 | -- returns the assignment statement: | |
70482933 RK |
432 | -- |
433 | -- Into (Indices, Ind) := Expr; | |
434 | -- | |
435 | -- Otherwise we call Build_Code recursively. | |
c45b6ae0 AC |
436 | -- |
437 | -- Ada0Y (AI-287): In case of default initialized component, Expr is | |
438 | -- empty and we generate a call to the corresponding IP subprogram. | |
70482933 RK |
439 | |
440 | function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id; | |
441 | -- Nodes L and H must be side-effect free expressions. | |
442 | -- If the input aggregate N to Build_Loop contains no sub-aggregates, | |
443 | -- This routine returns the for loop statement | |
444 | -- | |
445 | -- for J in Index_Base'(L) .. Index_Base'(H) loop | |
446 | -- Into (Indices, J) := Expr; | |
447 | -- end loop; | |
448 | -- | |
449 | -- Otherwise we call Build_Code recursively. | |
450 | -- As an optimization if the loop covers 3 or less scalar elements we | |
451 | -- generate a sequence of assignments. | |
452 | ||
453 | function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id; | |
454 | -- Nodes L and H must be side-effect free expressions. | |
455 | -- If the input aggregate N to Build_Loop contains no sub-aggregates, | |
456 | -- This routine returns the while loop statement | |
457 | -- | |
07fc65c4 GB |
458 | -- J : Index_Base := L; |
459 | -- while J < H loop | |
460 | -- J := Index_Base'Succ (J); | |
461 | -- Into (Indices, J) := Expr; | |
70482933 RK |
462 | -- end loop; |
463 | -- | |
fbf5a39b | 464 | -- Otherwise we call Build_Code recursively |
70482933 RK |
465 | |
466 | function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean; | |
467 | function Local_Expr_Value (E : Node_Id) return Uint; | |
468 | -- These two Local routines are used to replace the corresponding ones | |
469 | -- in sem_eval because while processing the bounds of an aggregate with | |
470 | -- discrete choices whose index type is an enumeration, we build static | |
471 | -- expressions not recognized by Compile_Time_Known_Value as such since | |
472 | -- they have not yet been analyzed and resolved. All the expressions in | |
473 | -- question are things like Index_Base_Name'Val (Const) which we can | |
474 | -- easily recognize as being constant. | |
475 | ||
476 | --------- | |
477 | -- Add -- | |
478 | --------- | |
479 | ||
480 | function Add (Val : Int; To : Node_Id) return Node_Id is | |
481 | Expr_Pos : Node_Id; | |
482 | Expr : Node_Id; | |
483 | To_Pos : Node_Id; | |
fbf5a39b AC |
484 | U_To : Uint; |
485 | U_Val : constant Uint := UI_From_Int (Val); | |
70482933 RK |
486 | |
487 | begin | |
488 | -- Note: do not try to optimize the case of Val = 0, because | |
489 | -- we need to build a new node with the proper Sloc value anyway. | |
490 | ||
491 | -- First test if we can do constant folding | |
492 | ||
493 | if Local_Compile_Time_Known_Value (To) then | |
494 | U_To := Local_Expr_Value (To) + Val; | |
495 | ||
496 | -- Determine if our constant is outside the range of the index. | |
497 | -- If so return an Empty node. This empty node will be caught | |
498 | -- by Empty_Range below. | |
499 | ||
500 | if Compile_Time_Known_Value (Index_Base_L) | |
501 | and then U_To < Expr_Value (Index_Base_L) | |
502 | then | |
503 | return Empty; | |
504 | ||
505 | elsif Compile_Time_Known_Value (Index_Base_H) | |
506 | and then U_To > Expr_Value (Index_Base_H) | |
507 | then | |
508 | return Empty; | |
509 | end if; | |
510 | ||
511 | Expr_Pos := Make_Integer_Literal (Loc, U_To); | |
512 | Set_Is_Static_Expression (Expr_Pos); | |
513 | ||
514 | if not Is_Enumeration_Type (Index_Base) then | |
515 | Expr := Expr_Pos; | |
516 | ||
517 | -- If we are dealing with enumeration return | |
518 | -- Index_Base'Val (Expr_Pos) | |
519 | ||
520 | else | |
521 | Expr := | |
522 | Make_Attribute_Reference | |
523 | (Loc, | |
524 | Prefix => Index_Base_Name, | |
525 | Attribute_Name => Name_Val, | |
526 | Expressions => New_List (Expr_Pos)); | |
527 | end if; | |
528 | ||
529 | return Expr; | |
530 | end if; | |
531 | ||
532 | -- If we are here no constant folding possible | |
533 | ||
534 | if not Is_Enumeration_Type (Index_Base) then | |
535 | Expr := | |
536 | Make_Op_Add (Loc, | |
537 | Left_Opnd => Duplicate_Subexpr (To), | |
538 | Right_Opnd => Make_Integer_Literal (Loc, U_Val)); | |
539 | ||
540 | -- If we are dealing with enumeration return | |
541 | -- Index_Base'Val (Index_Base'Pos (To) + Val) | |
542 | ||
543 | else | |
544 | To_Pos := | |
545 | Make_Attribute_Reference | |
546 | (Loc, | |
547 | Prefix => Index_Base_Name, | |
548 | Attribute_Name => Name_Pos, | |
549 | Expressions => New_List (Duplicate_Subexpr (To))); | |
550 | ||
551 | Expr_Pos := | |
552 | Make_Op_Add (Loc, | |
553 | Left_Opnd => To_Pos, | |
554 | Right_Opnd => Make_Integer_Literal (Loc, U_Val)); | |
555 | ||
556 | Expr := | |
557 | Make_Attribute_Reference | |
558 | (Loc, | |
559 | Prefix => Index_Base_Name, | |
560 | Attribute_Name => Name_Val, | |
561 | Expressions => New_List (Expr_Pos)); | |
562 | end if; | |
563 | ||
564 | return Expr; | |
565 | end Add; | |
566 | ||
567 | ----------------- | |
568 | -- Empty_Range -- | |
569 | ----------------- | |
570 | ||
571 | function Empty_Range (L, H : Node_Id) return Boolean is | |
572 | Is_Empty : Boolean := False; | |
573 | Low : Node_Id; | |
574 | High : Node_Id; | |
575 | ||
576 | begin | |
577 | -- First check if L or H were already detected as overflowing the | |
578 | -- index base range type by function Add above. If this is so Add | |
579 | -- returns the empty node. | |
580 | ||
581 | if No (L) or else No (H) then | |
582 | return True; | |
583 | end if; | |
584 | ||
585 | for J in 1 .. 3 loop | |
586 | case J is | |
587 | ||
588 | -- L > H range is empty | |
589 | ||
590 | when 1 => | |
591 | Low := L; | |
592 | High := H; | |
593 | ||
594 | -- B_L > H range must be empty | |
595 | ||
596 | when 2 => | |
597 | Low := Index_Base_L; | |
598 | High := H; | |
599 | ||
600 | -- L > B_H range must be empty | |
601 | ||
602 | when 3 => | |
603 | Low := L; | |
604 | High := Index_Base_H; | |
605 | end case; | |
606 | ||
607 | if Local_Compile_Time_Known_Value (Low) | |
608 | and then Local_Compile_Time_Known_Value (High) | |
609 | then | |
610 | Is_Empty := | |
611 | UI_Gt (Local_Expr_Value (Low), Local_Expr_Value (High)); | |
612 | end if; | |
613 | ||
614 | exit when Is_Empty; | |
615 | end loop; | |
616 | ||
617 | return Is_Empty; | |
618 | end Empty_Range; | |
619 | ||
620 | ----------- | |
621 | -- Equal -- | |
622 | ----------- | |
623 | ||
624 | function Equal (L, H : Node_Id) return Boolean is | |
625 | begin | |
626 | if L = H then | |
627 | return True; | |
628 | ||
629 | elsif Local_Compile_Time_Known_Value (L) | |
630 | and then Local_Compile_Time_Known_Value (H) | |
631 | then | |
632 | return UI_Eq (Local_Expr_Value (L), Local_Expr_Value (H)); | |
633 | end if; | |
634 | ||
635 | return False; | |
636 | end Equal; | |
637 | ||
638 | ---------------- | |
639 | -- Gen_Assign -- | |
640 | ---------------- | |
641 | ||
642 | function Gen_Assign (Ind : Node_Id; Expr : Node_Id) return List_Id is | |
fbf5a39b | 643 | L : constant List_Id := New_List; |
70482933 RK |
644 | F : Entity_Id; |
645 | A : Node_Id; | |
646 | ||
647 | New_Indices : List_Id; | |
648 | Indexed_Comp : Node_Id; | |
649 | Expr_Q : Node_Id; | |
650 | Comp_Type : Entity_Id := Empty; | |
651 | ||
652 | function Add_Loop_Actions (Lis : List_Id) return List_Id; | |
653 | -- Collect insert_actions generated in the construction of a | |
654 | -- loop, and prepend them to the sequence of assignments to | |
655 | -- complete the eventual body of the loop. | |
656 | ||
657 | ---------------------- | |
658 | -- Add_Loop_Actions -- | |
659 | ---------------------- | |
660 | ||
661 | function Add_Loop_Actions (Lis : List_Id) return List_Id is | |
662 | Res : List_Id; | |
663 | ||
664 | begin | |
c45b6ae0 AC |
665 | -- Ada0Y (AI-287): Do nothing else in case of default initialized |
666 | -- component | |
667 | ||
668 | if not Present (Expr) then | |
669 | return Lis; | |
670 | ||
671 | elsif Nkind (Parent (Expr)) = N_Component_Association | |
70482933 RK |
672 | and then Present (Loop_Actions (Parent (Expr))) |
673 | then | |
674 | Append_List (Lis, Loop_Actions (Parent (Expr))); | |
675 | Res := Loop_Actions (Parent (Expr)); | |
676 | Set_Loop_Actions (Parent (Expr), No_List); | |
677 | return Res; | |
678 | ||
679 | else | |
680 | return Lis; | |
681 | end if; | |
682 | end Add_Loop_Actions; | |
683 | ||
684 | -- Start of processing for Gen_Assign | |
685 | ||
686 | begin | |
687 | if No (Indices) then | |
688 | New_Indices := New_List; | |
689 | else | |
690 | New_Indices := New_Copy_List_Tree (Indices); | |
691 | end if; | |
692 | ||
693 | Append_To (New_Indices, Ind); | |
694 | ||
695 | if Present (Flist) then | |
696 | F := New_Copy_Tree (Flist); | |
697 | ||
698 | elsif Present (Etype (N)) and then Controlled_Type (Etype (N)) then | |
699 | if Is_Entity_Name (Into) | |
700 | and then Present (Scope (Entity (Into))) | |
701 | then | |
702 | F := Find_Final_List (Scope (Entity (Into))); | |
70482933 RK |
703 | else |
704 | F := Find_Final_List (Current_Scope); | |
705 | end if; | |
706 | else | |
c45b6ae0 | 707 | F := Empty; |
70482933 RK |
708 | end if; |
709 | ||
710 | if Present (Next_Index (Index)) then | |
711 | return | |
712 | Add_Loop_Actions ( | |
713 | Build_Array_Aggr_Code | |
c45b6ae0 AC |
714 | (N => Expr, |
715 | Ctype => Ctype, | |
716 | Index => Next_Index (Index), | |
717 | Into => Into, | |
718 | Scalar_Comp => Scalar_Comp, | |
719 | Indices => New_Indices, | |
720 | Flist => F)); | |
70482933 RK |
721 | end if; |
722 | ||
723 | -- If we get here then we are at a bottom-level (sub-)aggregate | |
724 | ||
fbf5a39b AC |
725 | Indexed_Comp := |
726 | Checks_Off | |
727 | (Make_Indexed_Component (Loc, | |
728 | Prefix => New_Copy_Tree (Into), | |
729 | Expressions => New_Indices)); | |
70482933 RK |
730 | |
731 | Set_Assignment_OK (Indexed_Comp); | |
732 | ||
c45b6ae0 AC |
733 | -- Ada0Y (AI-287): In case of default initialized component, Expr |
734 | -- is not present (and therefore we also initialize Expr_Q to empty) | |
735 | ||
736 | if not Present (Expr) then | |
737 | Expr_Q := Empty; | |
738 | elsif Nkind (Expr) = N_Qualified_Expression then | |
70482933 RK |
739 | Expr_Q := Expression (Expr); |
740 | else | |
741 | Expr_Q := Expr; | |
742 | end if; | |
743 | ||
744 | if Present (Etype (N)) | |
745 | and then Etype (N) /= Any_Composite | |
746 | then | |
747 | Comp_Type := Component_Type (Etype (N)); | |
c45b6ae0 | 748 | pragma Assert (Comp_Type = Ctype); -- AI-287 |
70482933 RK |
749 | |
750 | elsif Present (Next (First (New_Indices))) then | |
751 | ||
c45b6ae0 AC |
752 | -- Ada0Y (AI-287): Do nothing in case of default initialized |
753 | -- component because we have received the component type in | |
754 | -- the formal parameter Ctype. | |
755 | -- ??? I have added some assert pragmas to check if this new | |
756 | -- formal can be used to replace this code in all cases. | |
70482933 | 757 | |
c45b6ae0 | 758 | if Present (Expr) then |
70482933 | 759 | |
c45b6ae0 AC |
760 | -- This is a multidimensional array. Recover the component |
761 | -- type from the outermost aggregate, because subaggregates | |
762 | -- do not have an assigned type. | |
70482933 | 763 | |
c45b6ae0 AC |
764 | declare |
765 | P : Node_Id := Parent (Expr); | |
70482933 | 766 | |
c45b6ae0 AC |
767 | begin |
768 | while Present (P) loop | |
769 | ||
770 | if Nkind (P) = N_Aggregate | |
771 | and then Present (Etype (P)) | |
772 | then | |
773 | Comp_Type := Component_Type (Etype (P)); | |
774 | exit; | |
775 | ||
776 | else | |
777 | P := Parent (P); | |
778 | end if; | |
779 | end loop; | |
780 | pragma Assert (Comp_Type = Ctype); -- AI-287 | |
781 | end; | |
782 | end if; | |
70482933 RK |
783 | end if; |
784 | ||
c45b6ae0 AC |
785 | -- Ada0Y (AI-287): We only analyze the expression in case of non |
786 | -- default initialized components (otherwise Expr_Q is not present) | |
787 | ||
788 | if Present (Expr_Q) | |
789 | and then (Nkind (Expr_Q) = N_Aggregate | |
790 | or else Nkind (Expr_Q) = N_Extension_Aggregate) | |
70482933 | 791 | then |
70482933 RK |
792 | -- At this stage the Expression may not have been |
793 | -- analyzed yet because the array aggregate code has not | |
794 | -- been updated to use the Expansion_Delayed flag and | |
795 | -- avoid analysis altogether to solve the same problem | |
796 | -- (see Resolve_Aggr_Expr) so let's do the analysis of | |
797 | -- non-array aggregates now in order to get the value of | |
798 | -- Expansion_Delayed flag for the inner aggregate ??? | |
799 | ||
800 | if Present (Comp_Type) and then not Is_Array_Type (Comp_Type) then | |
801 | Analyze_And_Resolve (Expr_Q, Comp_Type); | |
802 | end if; | |
803 | ||
804 | if Is_Delayed_Aggregate (Expr_Q) then | |
805 | return | |
806 | Add_Loop_Actions ( | |
807 | Late_Expansion (Expr_Q, Etype (Expr_Q), Indexed_Comp, F)); | |
808 | end if; | |
809 | end if; | |
810 | ||
c45b6ae0 AC |
811 | -- Ada0Y (AI-287): In case of default initialized component, call |
812 | -- the initialization subprogram associated with the component type | |
70482933 | 813 | |
c45b6ae0 | 814 | if not Present (Expr) then |
70482933 | 815 | |
c45b6ae0 AC |
816 | Append_List_To (L, |
817 | Build_Initialization_Call (Loc, | |
818 | Id_Ref => Indexed_Comp, | |
819 | Typ => Ctype, | |
820 | With_Default_Init => True)); | |
70482933 | 821 | |
c45b6ae0 | 822 | else |
70482933 | 823 | |
c45b6ae0 AC |
824 | -- Now generate the assignment with no associated controlled |
825 | -- actions since the target of the assignment may not have | |
826 | -- been initialized, it is not possible to Finalize it as | |
827 | -- expected by normal controlled assignment. The rest of the | |
828 | -- controlled actions are done manually with the proper | |
829 | -- finalization list coming from the context. | |
70482933 | 830 | |
70482933 RK |
831 | A := |
832 | Make_OK_Assignment_Statement (Loc, | |
c45b6ae0 AC |
833 | Name => Indexed_Comp, |
834 | Expression => New_Copy_Tree (Expr)); | |
70482933 | 835 | |
c45b6ae0 AC |
836 | if Present (Comp_Type) and then Controlled_Type (Comp_Type) then |
837 | Set_No_Ctrl_Actions (A); | |
838 | end if; | |
70482933 RK |
839 | |
840 | Append_To (L, A); | |
70482933 | 841 | |
c45b6ae0 AC |
842 | -- Adjust the tag if tagged (because of possible view |
843 | -- conversions), unless compiling for the Java VM | |
844 | -- where tags are implicit. | |
70482933 | 845 | |
c45b6ae0 AC |
846 | if Present (Comp_Type) |
847 | and then Is_Tagged_Type (Comp_Type) | |
848 | and then not Java_VM | |
849 | then | |
850 | A := | |
851 | Make_OK_Assignment_Statement (Loc, | |
852 | Name => | |
853 | Make_Selected_Component (Loc, | |
854 | Prefix => New_Copy_Tree (Indexed_Comp), | |
855 | Selector_Name => | |
856 | New_Reference_To (Tag_Component (Comp_Type), Loc)), | |
857 | ||
858 | Expression => | |
859 | Unchecked_Convert_To (RTE (RE_Tag), | |
860 | New_Reference_To ( | |
861 | Access_Disp_Table (Comp_Type), Loc))); | |
862 | ||
863 | Append_To (L, A); | |
864 | end if; | |
865 | ||
866 | -- Adjust and Attach the component to the proper final list | |
867 | -- which can be the controller of the outer record object or | |
868 | -- the final list associated with the scope | |
869 | ||
870 | if Present (Comp_Type) and then Controlled_Type (Comp_Type) then | |
871 | Append_List_To (L, | |
872 | Make_Adjust_Call ( | |
873 | Ref => New_Copy_Tree (Indexed_Comp), | |
874 | Typ => Comp_Type, | |
875 | Flist_Ref => F, | |
876 | With_Attach => Make_Integer_Literal (Loc, 1))); | |
877 | end if; | |
70482933 RK |
878 | end if; |
879 | ||
880 | return Add_Loop_Actions (L); | |
881 | end Gen_Assign; | |
882 | ||
883 | -------------- | |
884 | -- Gen_Loop -- | |
885 | -------------- | |
886 | ||
887 | function Gen_Loop (L, H : Node_Id; Expr : Node_Id) return List_Id is | |
07fc65c4 | 888 | L_J : Node_Id; |
70482933 RK |
889 | |
890 | L_Range : Node_Id; | |
891 | -- Index_Base'(L) .. Index_Base'(H) | |
892 | ||
893 | L_Iteration_Scheme : Node_Id; | |
07fc65c4 | 894 | -- L_J in Index_Base'(L) .. Index_Base'(H) |
70482933 RK |
895 | |
896 | L_Body : List_Id; | |
897 | -- The statements to execute in the loop | |
898 | ||
fbf5a39b AC |
899 | S : constant List_Id := New_List; |
900 | -- List of statements | |
70482933 RK |
901 | |
902 | Tcopy : Node_Id; | |
903 | -- Copy of expression tree, used for checking purposes | |
904 | ||
905 | begin | |
906 | -- If loop bounds define an empty range return the null statement | |
907 | ||
908 | if Empty_Range (L, H) then | |
909 | Append_To (S, Make_Null_Statement (Loc)); | |
910 | ||
c45b6ae0 AC |
911 | -- Ada0Y (AI-287): Nothing else need to be done in case of |
912 | -- default initialized component | |
70482933 | 913 | |
c45b6ae0 AC |
914 | if not Present (Expr) then |
915 | null; | |
916 | ||
917 | else | |
918 | -- The expression must be type-checked even though no component | |
919 | -- of the aggregate will have this value. This is done only for | |
920 | -- actual components of the array, not for subaggregates. Do | |
921 | -- the check on a copy, because the expression may be shared | |
922 | -- among several choices, some of which might be non-null. | |
923 | ||
924 | if Present (Etype (N)) | |
925 | and then Is_Array_Type (Etype (N)) | |
926 | and then No (Next_Index (Index)) | |
927 | then | |
928 | Expander_Mode_Save_And_Set (False); | |
929 | Tcopy := New_Copy_Tree (Expr); | |
930 | Set_Parent (Tcopy, N); | |
931 | Analyze_And_Resolve (Tcopy, Component_Type (Etype (N))); | |
932 | Expander_Mode_Restore; | |
933 | end if; | |
70482933 RK |
934 | end if; |
935 | ||
936 | return S; | |
937 | ||
938 | -- If loop bounds are the same then generate an assignment | |
939 | ||
940 | elsif Equal (L, H) then | |
941 | return Gen_Assign (New_Copy_Tree (L), Expr); | |
942 | ||
943 | -- If H - L <= 2 then generate a sequence of assignments | |
944 | -- when we are processing the bottom most aggregate and it contains | |
945 | -- scalar components. | |
946 | ||
947 | elsif No (Next_Index (Index)) | |
948 | and then Scalar_Comp | |
949 | and then Local_Compile_Time_Known_Value (L) | |
950 | and then Local_Compile_Time_Known_Value (H) | |
951 | and then Local_Expr_Value (H) - Local_Expr_Value (L) <= 2 | |
952 | then | |
c45b6ae0 | 953 | |
70482933 RK |
954 | Append_List_To (S, Gen_Assign (New_Copy_Tree (L), Expr)); |
955 | Append_List_To (S, Gen_Assign (Add (1, To => L), Expr)); | |
956 | ||
957 | if Local_Expr_Value (H) - Local_Expr_Value (L) = 2 then | |
958 | Append_List_To (S, Gen_Assign (Add (2, To => L), Expr)); | |
959 | end if; | |
960 | ||
961 | return S; | |
962 | end if; | |
963 | ||
07fc65c4 | 964 | -- Otherwise construct the loop, starting with the loop index L_J |
70482933 | 965 | |
07fc65c4 | 966 | L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J')); |
70482933 RK |
967 | |
968 | -- Construct "L .. H" | |
969 | ||
970 | L_Range := | |
971 | Make_Range | |
972 | (Loc, | |
973 | Low_Bound => Make_Qualified_Expression | |
974 | (Loc, | |
975 | Subtype_Mark => Index_Base_Name, | |
976 | Expression => L), | |
977 | High_Bound => Make_Qualified_Expression | |
978 | (Loc, | |
979 | Subtype_Mark => Index_Base_Name, | |
980 | Expression => H)); | |
981 | ||
07fc65c4 | 982 | -- Construct "for L_J in Index_Base range L .. H" |
70482933 RK |
983 | |
984 | L_Iteration_Scheme := | |
985 | Make_Iteration_Scheme | |
986 | (Loc, | |
987 | Loop_Parameter_Specification => | |
988 | Make_Loop_Parameter_Specification | |
989 | (Loc, | |
07fc65c4 | 990 | Defining_Identifier => L_J, |
70482933 RK |
991 | Discrete_Subtype_Definition => L_Range)); |
992 | ||
993 | -- Construct the statements to execute in the loop body | |
994 | ||
07fc65c4 | 995 | L_Body := Gen_Assign (New_Reference_To (L_J, Loc), Expr); |
70482933 RK |
996 | |
997 | -- Construct the final loop | |
998 | ||
999 | Append_To (S, Make_Implicit_Loop_Statement | |
1000 | (Node => N, | |
1001 | Identifier => Empty, | |
1002 | Iteration_Scheme => L_Iteration_Scheme, | |
1003 | Statements => L_Body)); | |
1004 | ||
1005 | return S; | |
1006 | end Gen_Loop; | |
1007 | ||
1008 | --------------- | |
1009 | -- Gen_While -- | |
1010 | --------------- | |
1011 | ||
1012 | -- The code built is | |
1013 | ||
07fc65c4 GB |
1014 | -- W_J : Index_Base := L; |
1015 | -- while W_J < H loop | |
1016 | -- W_J := Index_Base'Succ (W); | |
70482933 RK |
1017 | -- L_Body; |
1018 | -- end loop; | |
1019 | ||
1020 | function Gen_While (L, H : Node_Id; Expr : Node_Id) return List_Id is | |
07fc65c4 | 1021 | W_J : Node_Id; |
70482933 RK |
1022 | |
1023 | W_Decl : Node_Id; | |
07fc65c4 | 1024 | -- W_J : Base_Type := L; |
70482933 RK |
1025 | |
1026 | W_Iteration_Scheme : Node_Id; | |
07fc65c4 | 1027 | -- while W_J < H |
70482933 RK |
1028 | |
1029 | W_Index_Succ : Node_Id; | |
07fc65c4 | 1030 | -- Index_Base'Succ (J) |
70482933 | 1031 | |
fbf5a39b | 1032 | W_Increment : Node_Id; |
07fc65c4 | 1033 | -- W_J := Index_Base'Succ (W) |
70482933 | 1034 | |
fbf5a39b | 1035 | W_Body : constant List_Id := New_List; |
70482933 RK |
1036 | -- The statements to execute in the loop |
1037 | ||
fbf5a39b | 1038 | S : constant List_Id := New_List; |
70482933 RK |
1039 | -- list of statement |
1040 | ||
1041 | begin | |
1042 | -- If loop bounds define an empty range or are equal return null | |
1043 | ||
1044 | if Empty_Range (L, H) or else Equal (L, H) then | |
1045 | Append_To (S, Make_Null_Statement (Loc)); | |
1046 | return S; | |
1047 | end if; | |
1048 | ||
07fc65c4 | 1049 | -- Build the decl of W_J |
70482933 | 1050 | |
07fc65c4 | 1051 | W_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J')); |
70482933 RK |
1052 | W_Decl := |
1053 | Make_Object_Declaration | |
1054 | (Loc, | |
07fc65c4 | 1055 | Defining_Identifier => W_J, |
70482933 RK |
1056 | Object_Definition => Index_Base_Name, |
1057 | Expression => L); | |
1058 | ||
1059 | -- Theoretically we should do a New_Copy_Tree (L) here, but we know | |
1060 | -- that in this particular case L is a fresh Expr generated by | |
1061 | -- Add which we are the only ones to use. | |
1062 | ||
1063 | Append_To (S, W_Decl); | |
1064 | ||
fbf5a39b | 1065 | -- Construct " while W_J < H" |
70482933 RK |
1066 | |
1067 | W_Iteration_Scheme := | |
1068 | Make_Iteration_Scheme | |
1069 | (Loc, | |
1070 | Condition => Make_Op_Lt | |
1071 | (Loc, | |
07fc65c4 | 1072 | Left_Opnd => New_Reference_To (W_J, Loc), |
70482933 RK |
1073 | Right_Opnd => New_Copy_Tree (H))); |
1074 | ||
1075 | -- Construct the statements to execute in the loop body | |
1076 | ||
1077 | W_Index_Succ := | |
1078 | Make_Attribute_Reference | |
1079 | (Loc, | |
1080 | Prefix => Index_Base_Name, | |
1081 | Attribute_Name => Name_Succ, | |
07fc65c4 | 1082 | Expressions => New_List (New_Reference_To (W_J, Loc))); |
70482933 RK |
1083 | |
1084 | W_Increment := | |
1085 | Make_OK_Assignment_Statement | |
1086 | (Loc, | |
07fc65c4 | 1087 | Name => New_Reference_To (W_J, Loc), |
70482933 RK |
1088 | Expression => W_Index_Succ); |
1089 | ||
1090 | Append_To (W_Body, W_Increment); | |
1091 | Append_List_To (W_Body, | |
07fc65c4 | 1092 | Gen_Assign (New_Reference_To (W_J, Loc), Expr)); |
70482933 RK |
1093 | |
1094 | -- Construct the final loop | |
1095 | ||
1096 | Append_To (S, Make_Implicit_Loop_Statement | |
1097 | (Node => N, | |
1098 | Identifier => Empty, | |
1099 | Iteration_Scheme => W_Iteration_Scheme, | |
1100 | Statements => W_Body)); | |
1101 | ||
1102 | return S; | |
1103 | end Gen_While; | |
1104 | ||
1105 | --------------------- | |
1106 | -- Index_Base_Name -- | |
1107 | --------------------- | |
1108 | ||
1109 | function Index_Base_Name return Node_Id is | |
1110 | begin | |
1111 | return New_Reference_To (Index_Base, Sloc (N)); | |
1112 | end Index_Base_Name; | |
1113 | ||
1114 | ------------------------------------ | |
1115 | -- Local_Compile_Time_Known_Value -- | |
1116 | ------------------------------------ | |
1117 | ||
1118 | function Local_Compile_Time_Known_Value (E : Node_Id) return Boolean is | |
1119 | begin | |
1120 | return Compile_Time_Known_Value (E) | |
1121 | or else | |
1122 | (Nkind (E) = N_Attribute_Reference | |
fbf5a39b AC |
1123 | and then Attribute_Name (E) = Name_Val |
1124 | and then Compile_Time_Known_Value (First (Expressions (E)))); | |
70482933 RK |
1125 | end Local_Compile_Time_Known_Value; |
1126 | ||
1127 | ---------------------- | |
1128 | -- Local_Expr_Value -- | |
1129 | ---------------------- | |
1130 | ||
1131 | function Local_Expr_Value (E : Node_Id) return Uint is | |
1132 | begin | |
1133 | if Compile_Time_Known_Value (E) then | |
1134 | return Expr_Value (E); | |
1135 | else | |
1136 | return Expr_Value (First (Expressions (E))); | |
1137 | end if; | |
1138 | end Local_Expr_Value; | |
1139 | ||
1140 | -- Build_Array_Aggr_Code Variables | |
1141 | ||
1142 | Assoc : Node_Id; | |
1143 | Choice : Node_Id; | |
1144 | Expr : Node_Id; | |
fbf5a39b | 1145 | Typ : Entity_Id; |
70482933 | 1146 | |
c45b6ae0 AC |
1147 | Others_Expr : Node_Id := Empty; |
1148 | Others_Mbox_Present : Boolean := False; | |
70482933 RK |
1149 | |
1150 | Aggr_L : constant Node_Id := Low_Bound (Aggregate_Bounds (N)); | |
1151 | Aggr_H : constant Node_Id := High_Bound (Aggregate_Bounds (N)); | |
1152 | -- The aggregate bounds of this specific sub-aggregate. Note that if | |
1153 | -- the code generated by Build_Array_Aggr_Code is executed then these | |
1154 | -- bounds are OK. Otherwise a Constraint_Error would have been raised. | |
1155 | ||
fbf5a39b AC |
1156 | Aggr_Low : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_L); |
1157 | Aggr_High : constant Node_Id := Duplicate_Subexpr_No_Checks (Aggr_H); | |
70482933 RK |
1158 | -- After Duplicate_Subexpr these are side-effect free. |
1159 | ||
c45b6ae0 AC |
1160 | Low : Node_Id; |
1161 | High : Node_Id; | |
70482933 RK |
1162 | |
1163 | Nb_Choices : Nat := 0; | |
1164 | Table : Case_Table_Type (1 .. Number_Of_Choices (N)); | |
1165 | -- Used to sort all the different choice values | |
1166 | ||
1167 | Nb_Elements : Int; | |
1168 | -- Number of elements in the positional aggregate | |
1169 | ||
fbf5a39b | 1170 | New_Code : constant List_Id := New_List; |
70482933 RK |
1171 | |
1172 | -- Start of processing for Build_Array_Aggr_Code | |
1173 | ||
1174 | begin | |
fbf5a39b AC |
1175 | -- First before we start, a special case. if we have a bit packed |
1176 | -- array represented as a modular type, then clear the value to | |
1177 | -- zero first, to ensure that unused bits are properly cleared. | |
1178 | ||
1179 | Typ := Etype (N); | |
1180 | ||
1181 | if Present (Typ) | |
1182 | and then Is_Bit_Packed_Array (Typ) | |
1183 | and then Is_Modular_Integer_Type (Packed_Array_Type (Typ)) | |
1184 | then | |
1185 | Append_To (New_Code, | |
1186 | Make_Assignment_Statement (Loc, | |
1187 | Name => New_Copy_Tree (Into), | |
1188 | Expression => | |
1189 | Unchecked_Convert_To (Typ, | |
1190 | Make_Integer_Literal (Loc, Uint_0)))); | |
1191 | end if; | |
1192 | ||
1193 | -- We can skip this | |
70482933 | 1194 | -- STEP 1: Process component associations |
fbf5a39b AC |
1195 | -- For those associations that may generate a loop, initialize |
1196 | -- Loop_Actions to collect inserted actions that may be crated. | |
70482933 RK |
1197 | |
1198 | if No (Expressions (N)) then | |
1199 | ||
1200 | -- STEP 1 (a): Sort the discrete choices | |
1201 | ||
1202 | Assoc := First (Component_Associations (N)); | |
1203 | while Present (Assoc) loop | |
70482933 RK |
1204 | Choice := First (Choices (Assoc)); |
1205 | while Present (Choice) loop | |
70482933 | 1206 | if Nkind (Choice) = N_Others_Choice then |
fbf5a39b | 1207 | Set_Loop_Actions (Assoc, New_List); |
c45b6ae0 AC |
1208 | |
1209 | if Box_Present (Assoc) then | |
1210 | Others_Mbox_Present := True; | |
1211 | else | |
1212 | Others_Expr := Expression (Assoc); | |
1213 | end if; | |
70482933 RK |
1214 | exit; |
1215 | end if; | |
1216 | ||
1217 | Get_Index_Bounds (Choice, Low, High); | |
1218 | ||
fbf5a39b AC |
1219 | if Low /= High then |
1220 | Set_Loop_Actions (Assoc, New_List); | |
1221 | end if; | |
1222 | ||
70482933 | 1223 | Nb_Choices := Nb_Choices + 1; |
c45b6ae0 AC |
1224 | if Box_Present (Assoc) then |
1225 | Table (Nb_Choices) := (Choice_Lo => Low, | |
1226 | Choice_Hi => High, | |
1227 | Choice_Node => Empty); | |
1228 | else | |
1229 | Table (Nb_Choices) := (Choice_Lo => Low, | |
1230 | Choice_Hi => High, | |
1231 | Choice_Node => Expression (Assoc)); | |
1232 | end if; | |
70482933 RK |
1233 | Next (Choice); |
1234 | end loop; | |
1235 | ||
1236 | Next (Assoc); | |
1237 | end loop; | |
1238 | ||
1239 | -- If there is more than one set of choices these must be static | |
1240 | -- and we can therefore sort them. Remember that Nb_Choices does not | |
1241 | -- account for an others choice. | |
1242 | ||
1243 | if Nb_Choices > 1 then | |
1244 | Sort_Case_Table (Table); | |
1245 | end if; | |
1246 | ||
1247 | -- STEP 1 (b): take care of the whole set of discrete choices. | |
1248 | ||
1249 | for J in 1 .. Nb_Choices loop | |
1250 | Low := Table (J).Choice_Lo; | |
1251 | High := Table (J).Choice_Hi; | |
1252 | Expr := Table (J).Choice_Node; | |
70482933 RK |
1253 | Append_List (Gen_Loop (Low, High, Expr), To => New_Code); |
1254 | end loop; | |
1255 | ||
1256 | -- STEP 1 (c): generate the remaining loops to cover others choice | |
1257 | -- We don't need to generate loops over empty gaps, but if there is | |
1258 | -- a single empty range we must analyze the expression for semantics | |
1259 | ||
c45b6ae0 | 1260 | if Present (Others_Expr) or else Others_Mbox_Present then |
70482933 RK |
1261 | declare |
1262 | First : Boolean := True; | |
1263 | ||
1264 | begin | |
1265 | for J in 0 .. Nb_Choices loop | |
70482933 RK |
1266 | if J = 0 then |
1267 | Low := Aggr_Low; | |
1268 | else | |
1269 | Low := Add (1, To => Table (J).Choice_Hi); | |
1270 | end if; | |
1271 | ||
1272 | if J = Nb_Choices then | |
1273 | High := Aggr_High; | |
1274 | else | |
1275 | High := Add (-1, To => Table (J + 1).Choice_Lo); | |
1276 | end if; | |
1277 | ||
fbf5a39b | 1278 | -- If this is an expansion within an init proc, make |
c84700e7 ES |
1279 | -- sure that discriminant references are replaced by |
1280 | -- the corresponding discriminal. | |
1281 | ||
1282 | if Inside_Init_Proc then | |
1283 | if Is_Entity_Name (Low) | |
1284 | and then Ekind (Entity (Low)) = E_Discriminant | |
1285 | then | |
1286 | Set_Entity (Low, Discriminal (Entity (Low))); | |
1287 | end if; | |
1288 | ||
1289 | if Is_Entity_Name (High) | |
1290 | and then Ekind (Entity (High)) = E_Discriminant | |
1291 | then | |
1292 | Set_Entity (High, Discriminal (Entity (High))); | |
1293 | end if; | |
1294 | end if; | |
1295 | ||
70482933 RK |
1296 | if First |
1297 | or else not Empty_Range (Low, High) | |
1298 | then | |
1299 | First := False; | |
1300 | Append_List | |
1301 | (Gen_Loop (Low, High, Others_Expr), To => New_Code); | |
1302 | end if; | |
1303 | end loop; | |
1304 | end; | |
1305 | end if; | |
1306 | ||
1307 | -- STEP 2: Process positional components | |
1308 | ||
1309 | else | |
1310 | -- STEP 2 (a): Generate the assignments for each positional element | |
1311 | -- Note that here we have to use Aggr_L rather than Aggr_Low because | |
1312 | -- Aggr_L is analyzed and Add wants an analyzed expression. | |
1313 | ||
1314 | Expr := First (Expressions (N)); | |
1315 | Nb_Elements := -1; | |
1316 | ||
1317 | while Present (Expr) loop | |
1318 | Nb_Elements := Nb_Elements + 1; | |
1319 | Append_List (Gen_Assign (Add (Nb_Elements, To => Aggr_L), Expr), | |
1320 | To => New_Code); | |
1321 | Next (Expr); | |
1322 | end loop; | |
1323 | ||
1324 | -- STEP 2 (b): Generate final loop if an others choice is present | |
1325 | -- Here Nb_Elements gives the offset of the last positional element. | |
1326 | ||
1327 | if Present (Component_Associations (N)) then | |
1328 | Assoc := Last (Component_Associations (N)); | |
70482933 | 1329 | |
c45b6ae0 AC |
1330 | -- Ada0Y (AI-287) |
1331 | if Box_Present (Assoc) then | |
1332 | Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L), | |
1333 | Aggr_High, | |
1334 | Empty), | |
1335 | To => New_Code); | |
1336 | else | |
1337 | Expr := Expression (Assoc); | |
1338 | ||
1339 | Append_List (Gen_While (Add (Nb_Elements, To => Aggr_L), | |
1340 | Aggr_High, | |
1341 | Expr), -- AI-287 | |
1342 | To => New_Code); | |
1343 | end if; | |
70482933 RK |
1344 | end if; |
1345 | end if; | |
1346 | ||
1347 | return New_Code; | |
1348 | end Build_Array_Aggr_Code; | |
1349 | ||
1350 | ---------------------------- | |
1351 | -- Build_Record_Aggr_Code -- | |
1352 | ---------------------------- | |
1353 | ||
1354 | function Build_Record_Aggr_Code | |
65356e64 AC |
1355 | (N : Node_Id; |
1356 | Typ : Entity_Id; | |
1357 | Target : Node_Id; | |
1358 | Flist : Node_Id := Empty; | |
1359 | Obj : Entity_Id := Empty; | |
d05ef0ab | 1360 | Is_Limited_Ancestor_Expansion : Boolean := False) return List_Id |
70482933 RK |
1361 | is |
1362 | Loc : constant Source_Ptr := Sloc (N); | |
1363 | L : constant List_Id := New_List; | |
1364 | Start_L : constant List_Id := New_List; | |
1365 | N_Typ : constant Entity_Id := Etype (N); | |
1366 | ||
1367 | Comp : Node_Id; | |
1368 | Instr : Node_Id; | |
1369 | Ref : Node_Id; | |
1370 | F : Node_Id; | |
1371 | Comp_Type : Entity_Id; | |
1372 | Selector : Entity_Id; | |
1373 | Comp_Expr : Node_Id; | |
70482933 RK |
1374 | Expr_Q : Node_Id; |
1375 | ||
1376 | Internal_Final_List : Node_Id; | |
1377 | ||
1378 | -- If this is an internal aggregate, the External_Final_List is an | |
1379 | -- expression for the controller record of the enclosing type. | |
1380 | -- If the current aggregate has several controlled components, this | |
1381 | -- expression will appear in several calls to attach to the finali- | |
1382 | -- zation list, and it must not be shared. | |
1383 | ||
1384 | External_Final_List : Node_Id; | |
1385 | Ancestor_Is_Expression : Boolean := False; | |
1386 | Ancestor_Is_Subtype_Mark : Boolean := False; | |
1387 | ||
1388 | Init_Typ : Entity_Id := Empty; | |
1389 | Attach : Node_Id; | |
1390 | ||
1391 | function Get_Constraint_Association (T : Entity_Id) return Node_Id; | |
1392 | -- Returns the first discriminant association in the constraint | |
1393 | -- associated with T, if any, otherwise returns Empty. | |
1394 | ||
1395 | function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id; | |
1396 | -- Returns the value that the given discriminant of an ancestor | |
1397 | -- type should receive (in the absence of a conflict with the | |
1398 | -- value provided by an ancestor part of an extension aggregate). | |
1399 | ||
1400 | procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id); | |
1401 | -- Check that each of the discriminant values defined by the | |
1402 | -- ancestor part of an extension aggregate match the corresponding | |
1403 | -- values provided by either an association of the aggregate or | |
1404 | -- by the constraint imposed by a parent type (RM95-4.3.2(8)). | |
1405 | ||
1406 | function Init_Controller | |
1407 | (Target : Node_Id; | |
1408 | Typ : Entity_Id; | |
1409 | F : Node_Id; | |
1410 | Attach : Node_Id; | |
d05ef0ab | 1411 | Init_Pr : Boolean) return List_Id; |
70482933 RK |
1412 | -- returns the list of statements necessary to initialize the internal |
1413 | -- controller of the (possible) ancestor typ into target and attach | |
1414 | -- it to finalization list F. Init_Pr conditions the call to the | |
fbf5a39b | 1415 | -- init proc since it may already be done due to ancestor initialization |
70482933 RK |
1416 | |
1417 | --------------------------------- | |
1418 | -- Ancestor_Discriminant_Value -- | |
1419 | --------------------------------- | |
1420 | ||
1421 | function Ancestor_Discriminant_Value (Disc : Entity_Id) return Node_Id is | |
1422 | Assoc : Node_Id; | |
1423 | Assoc_Elmt : Elmt_Id; | |
1424 | Aggr_Comp : Entity_Id; | |
1425 | Corresp_Disc : Entity_Id; | |
1426 | Current_Typ : Entity_Id := Base_Type (Typ); | |
1427 | Parent_Typ : Entity_Id; | |
1428 | Parent_Disc : Entity_Id; | |
1429 | Save_Assoc : Node_Id := Empty; | |
1430 | ||
1431 | begin | |
1432 | -- First check any discriminant associations to see if | |
1433 | -- any of them provide a value for the discriminant. | |
1434 | ||
1435 | if Present (Discriminant_Specifications (Parent (Current_Typ))) then | |
1436 | Assoc := First (Component_Associations (N)); | |
1437 | while Present (Assoc) loop | |
1438 | Aggr_Comp := Entity (First (Choices (Assoc))); | |
1439 | ||
1440 | if Ekind (Aggr_Comp) = E_Discriminant then | |
1441 | Save_Assoc := Expression (Assoc); | |
1442 | ||
1443 | Corresp_Disc := Corresponding_Discriminant (Aggr_Comp); | |
1444 | while Present (Corresp_Disc) loop | |
1445 | -- If found a corresponding discriminant then return | |
1446 | -- the value given in the aggregate. (Note: this is | |
1447 | -- not correct in the presence of side effects. ???) | |
1448 | ||
1449 | if Disc = Corresp_Disc then | |
1450 | return Duplicate_Subexpr (Expression (Assoc)); | |
1451 | end if; | |
fbf5a39b | 1452 | |
70482933 RK |
1453 | Corresp_Disc := |
1454 | Corresponding_Discriminant (Corresp_Disc); | |
1455 | end loop; | |
1456 | end if; | |
1457 | ||
1458 | Next (Assoc); | |
1459 | end loop; | |
1460 | end if; | |
1461 | ||
1462 | -- No match found in aggregate, so chain up parent types to find | |
1463 | -- a constraint that defines the value of the discriminant. | |
1464 | ||
1465 | Parent_Typ := Etype (Current_Typ); | |
1466 | while Current_Typ /= Parent_Typ loop | |
1467 | if Has_Discriminants (Parent_Typ) then | |
1468 | Parent_Disc := First_Discriminant (Parent_Typ); | |
1469 | ||
1470 | -- We either get the association from the subtype indication | |
1471 | -- of the type definition itself, or from the discriminant | |
1472 | -- constraint associated with the type entity (which is | |
1473 | -- preferable, but it's not always present ???) | |
1474 | ||
1475 | if Is_Empty_Elmt_List ( | |
1476 | Discriminant_Constraint (Current_Typ)) | |
1477 | then | |
1478 | Assoc := Get_Constraint_Association (Current_Typ); | |
1479 | Assoc_Elmt := No_Elmt; | |
1480 | else | |
1481 | Assoc_Elmt := | |
1482 | First_Elmt (Discriminant_Constraint (Current_Typ)); | |
1483 | Assoc := Node (Assoc_Elmt); | |
1484 | end if; | |
1485 | ||
1486 | -- Traverse the discriminants of the parent type looking | |
1487 | -- for one that corresponds. | |
1488 | ||
1489 | while Present (Parent_Disc) and then Present (Assoc) loop | |
1490 | Corresp_Disc := Parent_Disc; | |
1491 | while Present (Corresp_Disc) | |
1492 | and then Disc /= Corresp_Disc | |
1493 | loop | |
1494 | Corresp_Disc := | |
1495 | Corresponding_Discriminant (Corresp_Disc); | |
1496 | end loop; | |
1497 | ||
1498 | if Disc = Corresp_Disc then | |
1499 | if Nkind (Assoc) = N_Discriminant_Association then | |
1500 | Assoc := Expression (Assoc); | |
1501 | end if; | |
1502 | ||
1503 | -- If the located association directly denotes | |
1504 | -- a discriminant, then use the value of a saved | |
1505 | -- association of the aggregate. This is a kludge | |
1506 | -- to handle certain cases involving multiple | |
1507 | -- discriminants mapped to a single discriminant | |
1508 | -- of a descendant. It's not clear how to locate the | |
1509 | -- appropriate discriminant value for such cases. ??? | |
1510 | ||
1511 | if Is_Entity_Name (Assoc) | |
1512 | and then Ekind (Entity (Assoc)) = E_Discriminant | |
1513 | then | |
1514 | Assoc := Save_Assoc; | |
1515 | end if; | |
1516 | ||
1517 | return Duplicate_Subexpr (Assoc); | |
1518 | end if; | |
1519 | ||
1520 | Next_Discriminant (Parent_Disc); | |
1521 | ||
1522 | if No (Assoc_Elmt) then | |
1523 | Next (Assoc); | |
1524 | else | |
1525 | Next_Elmt (Assoc_Elmt); | |
1526 | if Present (Assoc_Elmt) then | |
1527 | Assoc := Node (Assoc_Elmt); | |
1528 | else | |
1529 | Assoc := Empty; | |
1530 | end if; | |
1531 | end if; | |
1532 | end loop; | |
1533 | end if; | |
1534 | ||
1535 | Current_Typ := Parent_Typ; | |
1536 | Parent_Typ := Etype (Current_Typ); | |
1537 | end loop; | |
1538 | ||
1539 | -- In some cases there's no ancestor value to locate (such as | |
1540 | -- when an ancestor part given by an expression defines the | |
1541 | -- discriminant value). | |
1542 | ||
1543 | return Empty; | |
1544 | end Ancestor_Discriminant_Value; | |
1545 | ||
1546 | ---------------------------------- | |
1547 | -- Check_Ancestor_Discriminants -- | |
1548 | ---------------------------------- | |
1549 | ||
1550 | procedure Check_Ancestor_Discriminants (Anc_Typ : Entity_Id) is | |
1551 | Discr : Entity_Id := First_Discriminant (Base_Type (Anc_Typ)); | |
1552 | Disc_Value : Node_Id; | |
1553 | Cond : Node_Id; | |
1554 | ||
1555 | begin | |
1556 | while Present (Discr) loop | |
1557 | Disc_Value := Ancestor_Discriminant_Value (Discr); | |
1558 | ||
1559 | if Present (Disc_Value) then | |
1560 | Cond := Make_Op_Ne (Loc, | |
1561 | Left_Opnd => | |
1562 | Make_Selected_Component (Loc, | |
1563 | Prefix => New_Copy_Tree (Target), | |
1564 | Selector_Name => New_Occurrence_Of (Discr, Loc)), | |
1565 | Right_Opnd => Disc_Value); | |
1566 | ||
07fc65c4 GB |
1567 | Append_To (L, |
1568 | Make_Raise_Constraint_Error (Loc, | |
1569 | Condition => Cond, | |
1570 | Reason => CE_Discriminant_Check_Failed)); | |
70482933 RK |
1571 | end if; |
1572 | ||
1573 | Next_Discriminant (Discr); | |
1574 | end loop; | |
1575 | end Check_Ancestor_Discriminants; | |
1576 | ||
1577 | -------------------------------- | |
1578 | -- Get_Constraint_Association -- | |
1579 | -------------------------------- | |
1580 | ||
1581 | function Get_Constraint_Association (T : Entity_Id) return Node_Id is | |
1582 | Typ_Def : constant Node_Id := Type_Definition (Parent (T)); | |
1583 | Indic : constant Node_Id := Subtype_Indication (Typ_Def); | |
1584 | ||
1585 | begin | |
1586 | -- ??? Also need to cover case of a type mark denoting a subtype | |
1587 | -- with constraint. | |
1588 | ||
1589 | if Nkind (Indic) = N_Subtype_Indication | |
1590 | and then Present (Constraint (Indic)) | |
1591 | then | |
1592 | return First (Constraints (Constraint (Indic))); | |
1593 | end if; | |
1594 | ||
1595 | return Empty; | |
1596 | end Get_Constraint_Association; | |
1597 | ||
1598 | --------------------- | |
1599 | -- Init_controller -- | |
1600 | --------------------- | |
1601 | ||
1602 | function Init_Controller | |
1603 | (Target : Node_Id; | |
1604 | Typ : Entity_Id; | |
1605 | F : Node_Id; | |
1606 | Attach : Node_Id; | |
d05ef0ab | 1607 | Init_Pr : Boolean) return List_Id |
70482933 | 1608 | is |
fbf5a39b | 1609 | L : constant List_Id := New_List; |
70482933 | 1610 | Ref : Node_Id; |
70482933 RK |
1611 | |
1612 | begin | |
fbf5a39b AC |
1613 | -- Generate: |
1614 | -- init-proc (target._controller); | |
70482933 RK |
1615 | -- initialize (target._controller); |
1616 | -- Attach_to_Final_List (target._controller, F); | |
1617 | ||
fbf5a39b AC |
1618 | Ref := |
1619 | Make_Selected_Component (Loc, | |
1620 | Prefix => Convert_To (Typ, New_Copy_Tree (Target)), | |
1621 | Selector_Name => Make_Identifier (Loc, Name_uController)); | |
70482933 RK |
1622 | Set_Assignment_OK (Ref); |
1623 | ||
19f0526a AC |
1624 | -- Ada0Y (AI-287): Give support to default initialization of limited |
1625 | -- types and components | |
70482933 | 1626 | |
65356e64 | 1627 | if (Nkind (Target) = N_Identifier |
c45b6ae0 | 1628 | and then Present (Etype (Target)) |
65356e64 AC |
1629 | and then Is_Limited_Type (Etype (Target))) |
1630 | or else (Nkind (Target) = N_Selected_Component | |
c45b6ae0 | 1631 | and then Present (Etype (Selector_Name (Target))) |
65356e64 AC |
1632 | and then Is_Limited_Type (Etype (Selector_Name (Target)))) |
1633 | or else (Nkind (Target) = N_Unchecked_Type_Conversion | |
c45b6ae0 | 1634 | and then Present (Etype (Target)) |
65356e64 | 1635 | and then Is_Limited_Type (Etype (Target))) |
c45b6ae0 AC |
1636 | or else (Nkind (Target) = N_Unchecked_Expression |
1637 | and then Nkind (Expression (Target)) = N_Indexed_Component | |
1638 | and then Present (Etype (Prefix (Expression (Target)))) | |
1639 | and then Is_Limited_Type | |
1640 | (Etype (Prefix (Expression (Target))))) | |
65356e64 AC |
1641 | then |
1642 | ||
1643 | if Init_Pr then | |
1644 | Append_List_To (L, | |
1645 | Build_Initialization_Call (Loc, | |
1646 | Id_Ref => Ref, | |
1647 | Typ => RTE (RE_Limited_Record_Controller), | |
1648 | In_Init_Proc => Within_Init_Proc)); | |
1649 | end if; | |
1650 | ||
1651 | Append_To (L, | |
1652 | Make_Procedure_Call_Statement (Loc, | |
1653 | Name => | |
1654 | New_Reference_To | |
1655 | (Find_Prim_Op (RTE (RE_Limited_Record_Controller), | |
1656 | Name_Initialize), Loc), | |
1657 | Parameter_Associations => New_List (New_Copy_Tree (Ref)))); | |
1658 | ||
1659 | else | |
1660 | if Init_Pr then | |
1661 | Append_List_To (L, | |
1662 | Build_Initialization_Call (Loc, | |
1663 | Id_Ref => Ref, | |
1664 | Typ => RTE (RE_Record_Controller), | |
1665 | In_Init_Proc => Within_Init_Proc)); | |
1666 | end if; | |
1667 | ||
1668 | Append_To (L, | |
1669 | Make_Procedure_Call_Statement (Loc, | |
1670 | Name => | |
1671 | New_Reference_To (Find_Prim_Op (RTE (RE_Record_Controller), | |
1672 | Name_Initialize), Loc), | |
1673 | Parameter_Associations => New_List (New_Copy_Tree (Ref)))); | |
1674 | ||
1675 | end if; | |
70482933 RK |
1676 | |
1677 | Append_To (L, | |
1678 | Make_Attach_Call ( | |
1679 | Obj_Ref => New_Copy_Tree (Ref), | |
1680 | Flist_Ref => F, | |
1681 | With_Attach => Attach)); | |
1682 | return L; | |
1683 | end Init_Controller; | |
1684 | ||
1685 | -- Start of processing for Build_Record_Aggr_Code | |
1686 | ||
1687 | begin | |
70482933 RK |
1688 | -- Deal with the ancestor part of extension aggregates |
1689 | -- or with the discriminants of the root type | |
1690 | ||
1691 | if Nkind (N) = N_Extension_Aggregate then | |
1692 | declare | |
1693 | A : constant Node_Id := Ancestor_Part (N); | |
1694 | ||
1695 | begin | |
70482933 | 1696 | -- If the ancestor part is a subtype mark "T", we generate |
fbf5a39b AC |
1697 | |
1698 | -- init-proc (T(tmp)); if T is constrained and | |
1699 | -- init-proc (S(tmp)); where S applies an appropriate | |
70482933 RK |
1700 | -- constraint if T is unconstrained |
1701 | ||
1702 | if Is_Entity_Name (A) and then Is_Type (Entity (A)) then | |
70482933 RK |
1703 | Ancestor_Is_Subtype_Mark := True; |
1704 | ||
1705 | if Is_Constrained (Entity (A)) then | |
1706 | Init_Typ := Entity (A); | |
1707 | ||
1708 | -- For an ancestor part given by an unconstrained type | |
1709 | -- mark, create a subtype constrained by appropriate | |
1710 | -- corresponding discriminant values coming from either | |
1711 | -- associations of the aggregate or a constraint on | |
1712 | -- a parent type. The subtype will be used to generate | |
1713 | -- the correct default value for the ancestor part. | |
1714 | ||
1715 | elsif Has_Discriminants (Entity (A)) then | |
1716 | declare | |
fbf5a39b AC |
1717 | Anc_Typ : constant Entity_Id := Entity (A); |
1718 | Anc_Constr : constant List_Id := New_List; | |
1719 | Discrim : Entity_Id; | |
70482933 RK |
1720 | Disc_Value : Node_Id; |
1721 | New_Indic : Node_Id; | |
1722 | Subt_Decl : Node_Id; | |
fbf5a39b | 1723 | |
70482933 | 1724 | begin |
fbf5a39b | 1725 | Discrim := First_Discriminant (Anc_Typ); |
70482933 RK |
1726 | while Present (Discrim) loop |
1727 | Disc_Value := Ancestor_Discriminant_Value (Discrim); | |
1728 | Append_To (Anc_Constr, Disc_Value); | |
1729 | Next_Discriminant (Discrim); | |
1730 | end loop; | |
1731 | ||
1732 | New_Indic := | |
1733 | Make_Subtype_Indication (Loc, | |
1734 | Subtype_Mark => New_Occurrence_Of (Anc_Typ, Loc), | |
1735 | Constraint => | |
1736 | Make_Index_Or_Discriminant_Constraint (Loc, | |
1737 | Constraints => Anc_Constr)); | |
1738 | ||
1739 | Init_Typ := Create_Itype (Ekind (Anc_Typ), N); | |
1740 | ||
1741 | Subt_Decl := | |
1742 | Make_Subtype_Declaration (Loc, | |
1743 | Defining_Identifier => Init_Typ, | |
1744 | Subtype_Indication => New_Indic); | |
1745 | ||
1746 | -- Itypes must be analyzed with checks off | |
07fc65c4 GB |
1747 | -- Declaration must have a parent for proper |
1748 | -- handling of subsidiary actions. | |
70482933 | 1749 | |
07fc65c4 | 1750 | Set_Parent (Subt_Decl, N); |
70482933 RK |
1751 | Analyze (Subt_Decl, Suppress => All_Checks); |
1752 | end; | |
1753 | end if; | |
1754 | ||
1755 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); | |
1756 | Set_Assignment_OK (Ref); | |
1757 | ||
c45b6ae0 AC |
1758 | if Has_Default_Init_Comps (N) |
1759 | or else Has_Task (Base_Type (Init_Typ)) | |
1760 | then | |
1761 | Append_List_To (Start_L, | |
1762 | Build_Initialization_Call (Loc, | |
1763 | Id_Ref => Ref, | |
1764 | Typ => Init_Typ, | |
1765 | In_Init_Proc => Within_Init_Proc, | |
1766 | With_Default_Init => True)); | |
1767 | else | |
1768 | Append_List_To (Start_L, | |
1769 | Build_Initialization_Call (Loc, | |
1770 | Id_Ref => Ref, | |
1771 | Typ => Init_Typ, | |
1772 | In_Init_Proc => Within_Init_Proc)); | |
1773 | end if; | |
70482933 RK |
1774 | |
1775 | if Is_Constrained (Entity (A)) | |
1776 | and then Has_Discriminants (Entity (A)) | |
1777 | then | |
1778 | Check_Ancestor_Discriminants (Entity (A)); | |
1779 | end if; | |
1780 | ||
19f0526a AC |
1781 | -- Ada0Y (AI-287): If the ancestor part is a limited type, a |
1782 | -- recursive call expands the ancestor. | |
65356e64 AC |
1783 | |
1784 | elsif Is_Limited_Type (Etype (A)) then | |
1785 | Ancestor_Is_Expression := True; | |
1786 | ||
1787 | Append_List_To (Start_L, | |
1788 | Build_Record_Aggr_Code ( | |
1789 | N => Expression (A), | |
1790 | Typ => Etype (Expression (A)), | |
1791 | Target => Target, | |
1792 | Flist => Flist, | |
1793 | Obj => Obj, | |
1794 | Is_Limited_Ancestor_Expansion => True)); | |
1795 | ||
70482933 RK |
1796 | -- If the ancestor part is an expression "E", we generate |
1797 | -- T(tmp) := E; | |
1798 | ||
1799 | else | |
1800 | Ancestor_Is_Expression := True; | |
1801 | Init_Typ := Etype (A); | |
1802 | ||
1803 | -- Assign the tag before doing the assignment to make sure | |
1804 | -- that the dispatching call in the subsequent deep_adjust | |
1805 | -- works properly (unless Java_VM, where tags are implicit). | |
1806 | ||
1807 | if not Java_VM then | |
1808 | Instr := | |
1809 | Make_OK_Assignment_Statement (Loc, | |
1810 | Name => | |
1811 | Make_Selected_Component (Loc, | |
1812 | Prefix => New_Copy_Tree (Target), | |
1813 | Selector_Name => New_Reference_To ( | |
1814 | Tag_Component (Base_Type (Typ)), Loc)), | |
1815 | ||
1816 | Expression => | |
1817 | Unchecked_Convert_To (RTE (RE_Tag), | |
1818 | New_Reference_To ( | |
1819 | Access_Disp_Table (Base_Type (Typ)), Loc))); | |
1820 | ||
1821 | Set_Assignment_OK (Name (Instr)); | |
1822 | Append_To (L, Instr); | |
1823 | end if; | |
1824 | ||
1825 | -- If the ancestor part is an aggregate, force its full | |
1826 | -- expansion, which was delayed. | |
1827 | ||
1828 | if Nkind (A) = N_Qualified_Expression | |
1829 | and then (Nkind (Expression (A)) = N_Aggregate | |
1830 | or else | |
1831 | Nkind (Expression (A)) = N_Extension_Aggregate) | |
1832 | then | |
1833 | Set_Analyzed (A, False); | |
1834 | Set_Analyzed (Expression (A), False); | |
1835 | end if; | |
1836 | ||
1837 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); | |
1838 | Set_Assignment_OK (Ref); | |
1839 | Append_To (L, | |
1840 | Make_Unsuppress_Block (Loc, | |
1841 | Name_Discriminant_Check, | |
1842 | New_List ( | |
1843 | Make_OK_Assignment_Statement (Loc, | |
1844 | Name => Ref, | |
1845 | Expression => A)))); | |
1846 | ||
1847 | if Has_Discriminants (Init_Typ) then | |
1848 | Check_Ancestor_Discriminants (Init_Typ); | |
1849 | end if; | |
1850 | end if; | |
1851 | end; | |
1852 | ||
fbf5a39b AC |
1853 | -- Normal case (not an extension aggregate) |
1854 | ||
70482933 RK |
1855 | else |
1856 | -- Generate the discriminant expressions, component by component. | |
1857 | -- If the base type is an unchecked union, the discriminants are | |
1858 | -- unknown to the back-end and absent from a value of the type, so | |
1859 | -- assignments for them are not emitted. | |
1860 | ||
1861 | if Has_Discriminants (Typ) | |
1862 | and then not Is_Unchecked_Union (Base_Type (Typ)) | |
1863 | then | |
70482933 RK |
1864 | -- ??? The discriminants of the object not inherited in the type |
1865 | -- of the object should be initialized here | |
1866 | ||
1867 | null; | |
1868 | ||
1869 | -- Generate discriminant init values | |
1870 | ||
1871 | declare | |
1872 | Discriminant : Entity_Id; | |
1873 | Discriminant_Value : Node_Id; | |
1874 | ||
1875 | begin | |
fbf5a39b | 1876 | Discriminant := First_Stored_Discriminant (Typ); |
70482933 RK |
1877 | |
1878 | while Present (Discriminant) loop | |
1879 | ||
1880 | Comp_Expr := | |
1881 | Make_Selected_Component (Loc, | |
1882 | Prefix => New_Copy_Tree (Target), | |
1883 | Selector_Name => New_Occurrence_Of (Discriminant, Loc)); | |
1884 | ||
1885 | Discriminant_Value := | |
1886 | Get_Discriminant_Value ( | |
1887 | Discriminant, | |
1888 | N_Typ, | |
1889 | Discriminant_Constraint (N_Typ)); | |
1890 | ||
1891 | Instr := | |
1892 | Make_OK_Assignment_Statement (Loc, | |
1893 | Name => Comp_Expr, | |
1894 | Expression => New_Copy_Tree (Discriminant_Value)); | |
1895 | ||
1896 | Set_No_Ctrl_Actions (Instr); | |
1897 | Append_To (L, Instr); | |
1898 | ||
fbf5a39b | 1899 | Next_Stored_Discriminant (Discriminant); |
70482933 RK |
1900 | end loop; |
1901 | end; | |
1902 | end if; | |
1903 | end if; | |
1904 | ||
1905 | -- Generate the assignments, component by component | |
1906 | ||
1907 | -- tmp.comp1 := Expr1_From_Aggr; | |
1908 | -- tmp.comp2 := Expr2_From_Aggr; | |
1909 | -- .... | |
1910 | ||
1911 | Comp := First (Component_Associations (N)); | |
1912 | while Present (Comp) loop | |
1913 | Selector := Entity (First (Choices (Comp))); | |
1914 | ||
c45b6ae0 | 1915 | -- Ada0Y (AI-287): Default initialization of a limited component |
65356e64 AC |
1916 | |
1917 | if Box_Present (Comp) | |
1918 | and then Is_Limited_Type (Etype (Selector)) | |
1919 | then | |
c45b6ae0 AC |
1920 | |
1921 | -- Ada0Y (AI-287): If the component type has tasks then generate | |
1922 | -- the activation chain and master entities (except in case of an | |
1923 | -- allocator because in that case these entities are generated | |
1924 | -- by Build_Task_Allocate_Block_With_Init_Stmts) | |
1925 | ||
1926 | declare | |
1927 | Ctype : Entity_Id := Etype (Selector); | |
1928 | Inside_Allocator : Boolean := False; | |
1929 | P : Node_Id := Parent (N); | |
1930 | ||
1931 | begin | |
1932 | if Is_Task_Type (Ctype) or else Has_Task (Ctype) then | |
1933 | while Present (P) loop | |
1934 | if Nkind (P) = N_Allocator then | |
1935 | Inside_Allocator := True; | |
1936 | exit; | |
1937 | end if; | |
1938 | ||
1939 | P := Parent (P); | |
1940 | end loop; | |
1941 | ||
1942 | if not Inside_Init_Proc and not Inside_Allocator then | |
1943 | Build_Activation_Chain_Entity (N); | |
1944 | Build_Master_Entity (Etype (N)); | |
1945 | end if; | |
1946 | end if; | |
1947 | end; | |
1948 | ||
65356e64 AC |
1949 | Append_List_To (L, |
1950 | Build_Initialization_Call (Loc, | |
1951 | Id_Ref => Make_Selected_Component (Loc, | |
1952 | Prefix => New_Copy_Tree (Target), | |
1953 | Selector_Name => New_Occurrence_Of (Selector, | |
c45b6ae0 AC |
1954 | Loc)), |
1955 | Typ => Etype (Selector), | |
1956 | With_Default_Init => True)); | |
65356e64 AC |
1957 | |
1958 | goto Next_Comp; | |
1959 | end if; | |
1960 | ||
fbf5a39b AC |
1961 | -- ??? |
1962 | ||
70482933 RK |
1963 | if Ekind (Selector) /= E_Discriminant |
1964 | or else Nkind (N) = N_Extension_Aggregate | |
1965 | then | |
1966 | Comp_Type := Etype (Selector); | |
70482933 RK |
1967 | Comp_Expr := |
1968 | Make_Selected_Component (Loc, | |
1969 | Prefix => New_Copy_Tree (Target), | |
1970 | Selector_Name => New_Occurrence_Of (Selector, Loc)); | |
1971 | ||
1972 | if Nkind (Expression (Comp)) = N_Qualified_Expression then | |
1973 | Expr_Q := Expression (Expression (Comp)); | |
1974 | else | |
1975 | Expr_Q := Expression (Comp); | |
1976 | end if; | |
1977 | ||
1978 | -- The controller is the one of the parent type defining | |
1979 | -- the component (in case of inherited components). | |
1980 | ||
1981 | if Controlled_Type (Comp_Type) then | |
1982 | Internal_Final_List := | |
1983 | Make_Selected_Component (Loc, | |
1984 | Prefix => Convert_To ( | |
1985 | Scope (Original_Record_Component (Selector)), | |
1986 | New_Copy_Tree (Target)), | |
1987 | Selector_Name => | |
1988 | Make_Identifier (Loc, Name_uController)); | |
fbf5a39b | 1989 | |
70482933 RK |
1990 | Internal_Final_List := |
1991 | Make_Selected_Component (Loc, | |
1992 | Prefix => Internal_Final_List, | |
1993 | Selector_Name => Make_Identifier (Loc, Name_F)); | |
1994 | ||
1995 | -- The internal final list can be part of a constant object | |
1996 | ||
1997 | Set_Assignment_OK (Internal_Final_List); | |
fbf5a39b | 1998 | |
70482933 RK |
1999 | else |
2000 | Internal_Final_List := Empty; | |
2001 | end if; | |
2002 | ||
fbf5a39b AC |
2003 | -- ??? |
2004 | ||
70482933 RK |
2005 | if Is_Delayed_Aggregate (Expr_Q) then |
2006 | Append_List_To (L, | |
2007 | Late_Expansion (Expr_Q, Comp_Type, Comp_Expr, | |
2008 | Internal_Final_List)); | |
fbf5a39b | 2009 | |
70482933 RK |
2010 | else |
2011 | Instr := | |
2012 | Make_OK_Assignment_Statement (Loc, | |
2013 | Name => Comp_Expr, | |
2014 | Expression => Expression (Comp)); | |
2015 | ||
2016 | Set_No_Ctrl_Actions (Instr); | |
2017 | Append_To (L, Instr); | |
2018 | ||
2019 | -- Adjust the tag if tagged (because of possible view | |
2020 | -- conversions), unless compiling for the Java VM | |
2021 | -- where tags are implicit. | |
2022 | ||
2023 | -- tmp.comp._tag := comp_typ'tag; | |
2024 | ||
2025 | if Is_Tagged_Type (Comp_Type) and then not Java_VM then | |
2026 | Instr := | |
2027 | Make_OK_Assignment_Statement (Loc, | |
2028 | Name => | |
2029 | Make_Selected_Component (Loc, | |
2030 | Prefix => New_Copy_Tree (Comp_Expr), | |
2031 | Selector_Name => | |
2032 | New_Reference_To (Tag_Component (Comp_Type), Loc)), | |
2033 | ||
2034 | Expression => | |
2035 | Unchecked_Convert_To (RTE (RE_Tag), | |
2036 | New_Reference_To ( | |
2037 | Access_Disp_Table (Comp_Type), Loc))); | |
2038 | ||
2039 | Append_To (L, Instr); | |
2040 | end if; | |
2041 | ||
2042 | -- Adjust and Attach the component to the proper controller | |
2043 | -- Adjust (tmp.comp); | |
2044 | -- Attach_To_Final_List (tmp.comp, | |
2045 | -- comp_typ (tmp)._record_controller.f) | |
2046 | ||
2047 | if Controlled_Type (Comp_Type) then | |
2048 | Append_List_To (L, | |
2049 | Make_Adjust_Call ( | |
2050 | Ref => New_Copy_Tree (Comp_Expr), | |
2051 | Typ => Comp_Type, | |
2052 | Flist_Ref => Internal_Final_List, | |
2053 | With_Attach => Make_Integer_Literal (Loc, 1))); | |
2054 | end if; | |
2055 | end if; | |
fbf5a39b AC |
2056 | |
2057 | -- ??? | |
2058 | ||
2059 | elsif Ekind (Selector) = E_Discriminant | |
2060 | and then Nkind (N) /= N_Extension_Aggregate | |
2061 | and then Nkind (Parent (N)) = N_Component_Association | |
2062 | and then Is_Constrained (Typ) | |
2063 | then | |
2064 | -- We must check that the discriminant value imposed by the | |
2065 | -- context is the same as the value given in the subaggregate, | |
2066 | -- because after the expansion into assignments there is no | |
2067 | -- record on which to perform a regular discriminant check. | |
2068 | ||
2069 | declare | |
2070 | D_Val : Elmt_Id; | |
2071 | Disc : Entity_Id; | |
2072 | ||
2073 | begin | |
2074 | D_Val := First_Elmt (Discriminant_Constraint (Typ)); | |
2075 | Disc := First_Discriminant (Typ); | |
2076 | ||
2077 | while Chars (Disc) /= Chars (Selector) loop | |
2078 | Next_Discriminant (Disc); | |
2079 | Next_Elmt (D_Val); | |
2080 | end loop; | |
2081 | ||
2082 | pragma Assert (Present (D_Val)); | |
2083 | ||
2084 | Append_To (L, | |
2085 | Make_Raise_Constraint_Error (Loc, | |
2086 | Condition => | |
2087 | Make_Op_Ne (Loc, | |
2088 | Left_Opnd => New_Copy_Tree (Node (D_Val)), | |
2089 | Right_Opnd => Expression (Comp)), | |
2090 | Reason => CE_Discriminant_Check_Failed)); | |
2091 | end; | |
70482933 RK |
2092 | end if; |
2093 | ||
65356e64 AC |
2094 | <<Next_Comp>> |
2095 | ||
70482933 RK |
2096 | Next (Comp); |
2097 | end loop; | |
2098 | ||
2099 | -- If the type is tagged, the tag needs to be initialized (unless | |
2100 | -- compiling for the Java VM where tags are implicit). It is done | |
2101 | -- late in the initialization process because in some cases, we call | |
fbf5a39b | 2102 | -- the init proc of an ancestor which will not leave out the right tag |
70482933 RK |
2103 | |
2104 | if Ancestor_Is_Expression then | |
2105 | null; | |
2106 | ||
2107 | elsif Is_Tagged_Type (Typ) and then not Java_VM then | |
2108 | Instr := | |
2109 | Make_OK_Assignment_Statement (Loc, | |
2110 | Name => | |
2111 | Make_Selected_Component (Loc, | |
2112 | Prefix => New_Copy_Tree (Target), | |
2113 | Selector_Name => | |
2114 | New_Reference_To (Tag_Component (Base_Type (Typ)), Loc)), | |
2115 | ||
2116 | Expression => | |
2117 | Unchecked_Convert_To (RTE (RE_Tag), | |
2118 | New_Reference_To (Access_Disp_Table (Base_Type (Typ)), Loc))); | |
2119 | ||
2120 | Append_To (L, Instr); | |
2121 | end if; | |
2122 | ||
2123 | -- Now deal with the various controlled type data structure | |
2124 | -- initializations | |
2125 | ||
2126 | if Present (Obj) | |
2127 | and then Finalize_Storage_Only (Typ) | |
2128 | and then (Is_Library_Level_Entity (Obj) | |
2129 | or else Entity (Constant_Value (RTE (RE_Garbage_Collected))) | |
2130 | = Standard_True) | |
2131 | then | |
2132 | Attach := Make_Integer_Literal (Loc, 0); | |
2133 | ||
2134 | elsif Nkind (Parent (N)) = N_Qualified_Expression | |
2135 | and then Nkind (Parent (Parent (N))) = N_Allocator | |
2136 | then | |
2137 | Attach := Make_Integer_Literal (Loc, 2); | |
2138 | ||
2139 | else | |
2140 | Attach := Make_Integer_Literal (Loc, 1); | |
2141 | end if; | |
2142 | ||
2143 | -- Determine the external finalization list. It is either the | |
2144 | -- finalization list of the outer-scope or the one coming from | |
2145 | -- an outer aggregate. When the target is not a temporary, the | |
2146 | -- proper scope is the scope of the target rather than the | |
2147 | -- potentially transient current scope. | |
2148 | ||
2149 | if Controlled_Type (Typ) then | |
2150 | if Present (Flist) then | |
2151 | External_Final_List := New_Copy_Tree (Flist); | |
2152 | ||
2153 | elsif Is_Entity_Name (Target) | |
2154 | and then Present (Scope (Entity (Target))) | |
2155 | then | |
2156 | External_Final_List := Find_Final_List (Scope (Entity (Target))); | |
2157 | ||
2158 | else | |
2159 | External_Final_List := Find_Final_List (Current_Scope); | |
2160 | end if; | |
2161 | ||
2162 | else | |
2163 | External_Final_List := Empty; | |
2164 | end if; | |
2165 | ||
fbf5a39b | 2166 | -- Initialize and attach the outer object in the is_controlled case |
70482933 RK |
2167 | |
2168 | if Is_Controlled (Typ) then | |
2169 | if Ancestor_Is_Subtype_Mark then | |
2170 | Ref := Convert_To (Init_Typ, New_Copy_Tree (Target)); | |
2171 | Set_Assignment_OK (Ref); | |
2172 | Append_To (L, | |
2173 | Make_Procedure_Call_Statement (Loc, | |
2174 | Name => New_Reference_To ( | |
2175 | Find_Prim_Op (Init_Typ, Name_Initialize), Loc), | |
2176 | Parameter_Associations => New_List (New_Copy_Tree (Ref)))); | |
2177 | end if; | |
2178 | ||
fbf5a39b | 2179 | if not Has_Controlled_Component (Typ) then |
70482933 RK |
2180 | Ref := New_Copy_Tree (Target); |
2181 | Set_Assignment_OK (Ref); | |
2182 | Append_To (Start_L, | |
2183 | Make_Attach_Call ( | |
2184 | Obj_Ref => Ref, | |
2185 | Flist_Ref => New_Copy_Tree (External_Final_List), | |
2186 | With_Attach => Attach)); | |
2187 | end if; | |
2188 | end if; | |
2189 | ||
fbf5a39b | 2190 | -- In the Has_Controlled component case, all the intermediate |
70482933 RK |
2191 | -- controllers must be initialized |
2192 | ||
65356e64 AC |
2193 | if Has_Controlled_Component (Typ) |
2194 | and not Is_Limited_Ancestor_Expansion | |
2195 | then | |
70482933 RK |
2196 | declare |
2197 | Inner_Typ : Entity_Id; | |
2198 | Outer_Typ : Entity_Id; | |
2199 | At_Root : Boolean; | |
2200 | ||
2201 | begin | |
2202 | ||
2203 | Outer_Typ := Base_Type (Typ); | |
2204 | ||
fbf5a39b | 2205 | -- Find outer type with a controller |
70482933 RK |
2206 | |
2207 | while Outer_Typ /= Init_Typ | |
2208 | and then not Has_New_Controlled_Component (Outer_Typ) | |
2209 | loop | |
2210 | Outer_Typ := Etype (Outer_Typ); | |
2211 | end loop; | |
2212 | ||
fbf5a39b | 2213 | -- Attach it to the outer record controller to the |
70482933 RK |
2214 | -- external final list |
2215 | ||
2216 | if Outer_Typ = Init_Typ then | |
2217 | Append_List_To (Start_L, | |
2218 | Init_Controller ( | |
2219 | Target => Target, | |
2220 | Typ => Outer_Typ, | |
2221 | F => External_Final_List, | |
2222 | Attach => Attach, | |
2223 | Init_Pr => Ancestor_Is_Expression)); | |
fbf5a39b AC |
2224 | |
2225 | At_Root := True; | |
70482933 RK |
2226 | Inner_Typ := Init_Typ; |
2227 | ||
2228 | else | |
2229 | Append_List_To (Start_L, | |
2230 | Init_Controller ( | |
2231 | Target => Target, | |
2232 | Typ => Outer_Typ, | |
2233 | F => External_Final_List, | |
2234 | Attach => Attach, | |
2235 | Init_Pr => True)); | |
2236 | ||
2237 | Inner_Typ := Etype (Outer_Typ); | |
2238 | At_Root := | |
2239 | not Is_Tagged_Type (Typ) or else Inner_Typ = Outer_Typ; | |
2240 | end if; | |
2241 | ||
fbf5a39b AC |
2242 | -- The outer object has to be attached as well |
2243 | ||
2244 | if Is_Controlled (Typ) then | |
2245 | Ref := New_Copy_Tree (Target); | |
2246 | Set_Assignment_OK (Ref); | |
2247 | Append_To (Start_L, | |
2248 | Make_Attach_Call ( | |
2249 | Obj_Ref => Ref, | |
2250 | Flist_Ref => New_Copy_Tree (External_Final_List), | |
2251 | With_Attach => New_Copy_Tree (Attach))); | |
2252 | end if; | |
2253 | ||
70482933 RK |
2254 | -- Initialize the internal controllers for tagged types with |
2255 | -- more than one controller. | |
2256 | ||
2257 | while not At_Root and then Inner_Typ /= Init_Typ loop | |
2258 | if Has_New_Controlled_Component (Inner_Typ) then | |
2259 | F := | |
2260 | Make_Selected_Component (Loc, | |
2261 | Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)), | |
2262 | Selector_Name => | |
2263 | Make_Identifier (Loc, Name_uController)); | |
fbf5a39b AC |
2264 | F := |
2265 | Make_Selected_Component (Loc, | |
2266 | Prefix => F, | |
2267 | Selector_Name => Make_Identifier (Loc, Name_F)); | |
2268 | ||
70482933 RK |
2269 | Append_List_To (Start_L, |
2270 | Init_Controller ( | |
2271 | Target => Target, | |
2272 | Typ => Inner_Typ, | |
2273 | F => F, | |
2274 | Attach => Make_Integer_Literal (Loc, 1), | |
2275 | Init_Pr => True)); | |
2276 | Outer_Typ := Inner_Typ; | |
2277 | end if; | |
2278 | ||
2279 | -- Stop at the root | |
2280 | ||
2281 | At_Root := Inner_Typ = Etype (Inner_Typ); | |
2282 | Inner_Typ := Etype (Inner_Typ); | |
2283 | end loop; | |
2284 | ||
fbf5a39b | 2285 | -- If not done yet attach the controller of the ancestor part |
70482933 RK |
2286 | |
2287 | if Outer_Typ /= Init_Typ | |
2288 | and then Inner_Typ = Init_Typ | |
2289 | and then Has_Controlled_Component (Init_Typ) | |
2290 | then | |
2291 | F := | |
2292 | Make_Selected_Component (Loc, | |
2293 | Prefix => Convert_To (Outer_Typ, New_Copy_Tree (Target)), | |
2294 | Selector_Name => Make_Identifier (Loc, Name_uController)); | |
fbf5a39b AC |
2295 | F := |
2296 | Make_Selected_Component (Loc, | |
2297 | Prefix => F, | |
2298 | Selector_Name => Make_Identifier (Loc, Name_F)); | |
70482933 RK |
2299 | |
2300 | Attach := Make_Integer_Literal (Loc, 1); | |
2301 | Append_List_To (Start_L, | |
2302 | Init_Controller ( | |
2303 | Target => Target, | |
2304 | Typ => Init_Typ, | |
2305 | F => F, | |
2306 | Attach => Attach, | |
2307 | Init_Pr => Ancestor_Is_Expression)); | |
2308 | end if; | |
2309 | end; | |
2310 | end if; | |
2311 | ||
2312 | Append_List_To (Start_L, L); | |
2313 | return Start_L; | |
2314 | end Build_Record_Aggr_Code; | |
2315 | ||
2316 | ------------------------------- | |
2317 | -- Convert_Aggr_In_Allocator -- | |
2318 | ------------------------------- | |
2319 | ||
2320 | procedure Convert_Aggr_In_Allocator (Decl, Aggr : Node_Id) is | |
2321 | Loc : constant Source_Ptr := Sloc (Aggr); | |
2322 | Typ : constant Entity_Id := Etype (Aggr); | |
2323 | Temp : constant Entity_Id := Defining_Identifier (Decl); | |
fbf5a39b AC |
2324 | |
2325 | Occ : constant Node_Id := | |
2326 | Unchecked_Convert_To (Typ, | |
2327 | Make_Explicit_Dereference (Loc, | |
2328 | New_Reference_To (Temp, Loc))); | |
70482933 RK |
2329 | |
2330 | Access_Type : constant Entity_Id := Etype (Temp); | |
2331 | ||
2332 | begin | |
c45b6ae0 AC |
2333 | if Has_Default_Init_Comps (Aggr) then |
2334 | declare | |
2335 | L : constant List_Id := New_List; | |
2336 | Init_Stmts : List_Id; | |
2337 | ||
2338 | begin | |
2339 | Init_Stmts := Late_Expansion (Aggr, Typ, Occ, | |
2340 | Find_Final_List (Access_Type), | |
2341 | Associated_Final_Chain (Base_Type (Access_Type))); | |
2342 | ||
2343 | Build_Task_Allocate_Block_With_Init_Stmts (L, Aggr, Init_Stmts); | |
2344 | Insert_Actions_After (Decl, L); | |
2345 | end; | |
2346 | ||
2347 | else | |
2348 | Insert_Actions_After (Decl, | |
2349 | Late_Expansion (Aggr, Typ, Occ, | |
2350 | Find_Final_List (Access_Type), | |
2351 | Associated_Final_Chain (Base_Type (Access_Type)))); | |
2352 | end if; | |
70482933 RK |
2353 | end Convert_Aggr_In_Allocator; |
2354 | ||
2355 | -------------------------------- | |
2356 | -- Convert_Aggr_In_Assignment -- | |
2357 | -------------------------------- | |
2358 | ||
2359 | procedure Convert_Aggr_In_Assignment (N : Node_Id) is | |
fbf5a39b | 2360 | Aggr : Node_Id := Expression (N); |
70482933 RK |
2361 | Typ : constant Entity_Id := Etype (Aggr); |
2362 | Occ : constant Node_Id := New_Copy_Tree (Name (N)); | |
2363 | ||
2364 | begin | |
2365 | if Nkind (Aggr) = N_Qualified_Expression then | |
2366 | Aggr := Expression (Aggr); | |
2367 | end if; | |
2368 | ||
2369 | Insert_Actions_After (N, | |
2370 | Late_Expansion (Aggr, Typ, Occ, | |
2371 | Find_Final_List (Typ, New_Copy_Tree (Occ)))); | |
2372 | end Convert_Aggr_In_Assignment; | |
2373 | ||
2374 | --------------------------------- | |
2375 | -- Convert_Aggr_In_Object_Decl -- | |
2376 | --------------------------------- | |
2377 | ||
2378 | procedure Convert_Aggr_In_Object_Decl (N : Node_Id) is | |
2379 | Obj : constant Entity_Id := Defining_Identifier (N); | |
fbf5a39b | 2380 | Aggr : Node_Id := Expression (N); |
70482933 RK |
2381 | Loc : constant Source_Ptr := Sloc (Aggr); |
2382 | Typ : constant Entity_Id := Etype (Aggr); | |
2383 | Occ : constant Node_Id := New_Occurrence_Of (Obj, Loc); | |
2384 | ||
fbf5a39b AC |
2385 | function Discriminants_Ok return Boolean; |
2386 | -- If the object type is constrained, the discriminants in the | |
2387 | -- aggregate must be checked against the discriminants of the subtype. | |
2388 | -- This cannot be done using Apply_Discriminant_Checks because after | |
2389 | -- expansion there is no aggregate left to check. | |
2390 | ||
2391 | ---------------------- | |
2392 | -- Discriminants_Ok -- | |
2393 | ---------------------- | |
2394 | ||
2395 | function Discriminants_Ok return Boolean is | |
2396 | Cond : Node_Id := Empty; | |
2397 | Check : Node_Id; | |
2398 | D : Entity_Id; | |
2399 | Disc1 : Elmt_Id; | |
2400 | Disc2 : Elmt_Id; | |
2401 | Val1 : Node_Id; | |
2402 | Val2 : Node_Id; | |
2403 | ||
2404 | begin | |
2405 | D := First_Discriminant (Typ); | |
2406 | Disc1 := First_Elmt (Discriminant_Constraint (Typ)); | |
2407 | Disc2 := First_Elmt (Discriminant_Constraint (Etype (Obj))); | |
2408 | ||
2409 | while Present (Disc1) and then Present (Disc2) loop | |
2410 | Val1 := Node (Disc1); | |
2411 | Val2 := Node (Disc2); | |
2412 | ||
2413 | if not Is_OK_Static_Expression (Val1) | |
2414 | or else not Is_OK_Static_Expression (Val2) | |
2415 | then | |
2416 | Check := Make_Op_Ne (Loc, | |
2417 | Left_Opnd => Duplicate_Subexpr (Val1), | |
2418 | Right_Opnd => Duplicate_Subexpr (Val2)); | |
2419 | ||
2420 | if No (Cond) then | |
2421 | Cond := Check; | |
2422 | ||
2423 | else | |
2424 | Cond := Make_Or_Else (Loc, | |
2425 | Left_Opnd => Cond, | |
2426 | Right_Opnd => Check); | |
2427 | end if; | |
2428 | ||
2429 | elsif Expr_Value (Val1) /= Expr_Value (Val2) then | |
2430 | Apply_Compile_Time_Constraint_Error (Aggr, | |
2431 | Msg => "incorrect value for discriminant&?", | |
2432 | Reason => CE_Discriminant_Check_Failed, | |
2433 | Ent => D); | |
2434 | return False; | |
2435 | end if; | |
2436 | ||
2437 | Next_Discriminant (D); | |
2438 | Next_Elmt (Disc1); | |
2439 | Next_Elmt (Disc2); | |
2440 | end loop; | |
2441 | ||
2442 | -- If any discriminant constraint is non-static, emit a check. | |
2443 | ||
2444 | if Present (Cond) then | |
2445 | Insert_Action (N, | |
2446 | Make_Raise_Constraint_Error (Loc, | |
2447 | Condition => Cond, | |
2448 | Reason => CE_Discriminant_Check_Failed)); | |
2449 | end if; | |
2450 | ||
2451 | return True; | |
2452 | end Discriminants_Ok; | |
2453 | ||
2454 | -- Start of processing for Convert_Aggr_In_Object_Decl | |
2455 | ||
70482933 RK |
2456 | begin |
2457 | Set_Assignment_OK (Occ); | |
2458 | ||
2459 | if Nkind (Aggr) = N_Qualified_Expression then | |
2460 | Aggr := Expression (Aggr); | |
2461 | end if; | |
2462 | ||
fbf5a39b AC |
2463 | if Has_Discriminants (Typ) |
2464 | and then Typ /= Etype (Obj) | |
2465 | and then Is_Constrained (Etype (Obj)) | |
2466 | and then not Discriminants_Ok | |
2467 | then | |
2468 | return; | |
2469 | end if; | |
2470 | ||
70482933 RK |
2471 | Insert_Actions_After (N, Late_Expansion (Aggr, Typ, Occ, Obj => Obj)); |
2472 | Set_No_Initialization (N); | |
07fc65c4 | 2473 | Initialize_Discriminants (N, Typ); |
70482933 RK |
2474 | end Convert_Aggr_In_Object_Decl; |
2475 | ||
2476 | ---------------------------- | |
2477 | -- Convert_To_Assignments -- | |
2478 | ---------------------------- | |
2479 | ||
2480 | procedure Convert_To_Assignments (N : Node_Id; Typ : Entity_Id) is | |
2481 | Loc : constant Source_Ptr := Sloc (N); | |
2482 | Temp : Entity_Id; | |
2483 | ||
fbf5a39b AC |
2484 | Instr : Node_Id; |
2485 | Target_Expr : Node_Id; | |
2486 | Parent_Kind : Node_Kind; | |
2487 | Unc_Decl : Boolean := False; | |
2488 | Parent_Node : Node_Id; | |
70482933 RK |
2489 | |
2490 | begin | |
70482933 RK |
2491 | Parent_Node := Parent (N); |
2492 | Parent_Kind := Nkind (Parent_Node); | |
2493 | ||
2494 | if Parent_Kind = N_Qualified_Expression then | |
2495 | ||
2496 | -- Check if we are in a unconstrained declaration because in this | |
2497 | -- case the current delayed expansion mechanism doesn't work when | |
2498 | -- the declared object size depend on the initializing expr. | |
2499 | ||
2500 | begin | |
2501 | Parent_Node := Parent (Parent_Node); | |
2502 | Parent_Kind := Nkind (Parent_Node); | |
fbf5a39b | 2503 | |
70482933 RK |
2504 | if Parent_Kind = N_Object_Declaration then |
2505 | Unc_Decl := | |
2506 | not Is_Entity_Name (Object_Definition (Parent_Node)) | |
fbf5a39b AC |
2507 | or else Has_Discriminants |
2508 | (Entity (Object_Definition (Parent_Node))) | |
2509 | or else Is_Class_Wide_Type | |
2510 | (Entity (Object_Definition (Parent_Node))); | |
70482933 RK |
2511 | end if; |
2512 | end; | |
2513 | end if; | |
2514 | ||
2515 | -- Just set the Delay flag in the following cases where the | |
2516 | -- transformation will be done top down from above | |
fbf5a39b | 2517 | |
70482933 RK |
2518 | -- - internal aggregate (transformed when expanding the parent) |
2519 | -- - allocators (see Convert_Aggr_In_Allocator) | |
2520 | -- - object decl (see Convert_Aggr_In_Object_Decl) | |
2521 | -- - safe assignments (see Convert_Aggr_Assignments) | |
fbf5a39b | 2522 | -- so far only the assignments in the init procs are taken |
70482933 RK |
2523 | -- into account |
2524 | ||
2525 | if Parent_Kind = N_Aggregate | |
2526 | or else Parent_Kind = N_Extension_Aggregate | |
2527 | or else Parent_Kind = N_Component_Association | |
2528 | or else Parent_Kind = N_Allocator | |
2529 | or else (Parent_Kind = N_Object_Declaration and then not Unc_Decl) | |
2530 | or else (Parent_Kind = N_Assignment_Statement | |
2531 | and then Inside_Init_Proc) | |
2532 | then | |
2533 | Set_Expansion_Delayed (N); | |
2534 | return; | |
2535 | end if; | |
2536 | ||
2537 | if Requires_Transient_Scope (Typ) then | |
2538 | Establish_Transient_Scope (N, Sec_Stack => | |
2539 | Is_Controlled (Typ) or else Has_Controlled_Component (Typ)); | |
2540 | end if; | |
2541 | ||
2542 | -- Create the temporary | |
2543 | ||
2544 | Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('A')); | |
2545 | ||
2546 | Instr := | |
2547 | Make_Object_Declaration (Loc, | |
2548 | Defining_Identifier => Temp, | |
2549 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
2550 | ||
2551 | Set_No_Initialization (Instr); | |
2552 | Insert_Action (N, Instr); | |
07fc65c4 | 2553 | Initialize_Discriminants (Instr, Typ); |
70482933 RK |
2554 | Target_Expr := New_Occurrence_Of (Temp, Loc); |
2555 | ||
2556 | Insert_Actions (N, Build_Record_Aggr_Code (N, Typ, Target_Expr)); | |
2557 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
2558 | Analyze_And_Resolve (N, Typ); | |
2559 | end Convert_To_Assignments; | |
2560 | ||
07fc65c4 GB |
2561 | --------------------------- |
2562 | -- Convert_To_Positional -- | |
2563 | --------------------------- | |
2564 | ||
2565 | procedure Convert_To_Positional | |
2566 | (N : Node_Id; | |
fbf5a39b | 2567 | Max_Others_Replicate : Nat := 5; |
07fc65c4 GB |
2568 | Handle_Bit_Packed : Boolean := False) |
2569 | is | |
fbf5a39b | 2570 | Typ : constant Entity_Id := Etype (N); |
07fc65c4 | 2571 | |
fbf5a39b | 2572 | function Flatten |
d05ef0ab AC |
2573 | (N : Node_Id; |
2574 | Ix : Node_Id; | |
2575 | Ixb : Node_Id) return Boolean; | |
fbf5a39b AC |
2576 | -- Convert the aggregate into a purely positional form if possible. |
2577 | ||
2578 | function Is_Flat (N : Node_Id; Dims : Int) return Boolean; | |
2579 | -- Non trivial for multidimensional aggregate. | |
2580 | ||
2581 | ------------- | |
2582 | -- Flatten -- | |
2583 | ------------- | |
2584 | ||
2585 | function Flatten | |
d05ef0ab AC |
2586 | (N : Node_Id; |
2587 | Ix : Node_Id; | |
2588 | Ixb : Node_Id) return Boolean | |
fbf5a39b AC |
2589 | is |
2590 | Loc : constant Source_Ptr := Sloc (N); | |
2591 | Blo : constant Node_Id := Type_Low_Bound (Etype (Ixb)); | |
2592 | Lo : constant Node_Id := Type_Low_Bound (Etype (Ix)); | |
2593 | Hi : constant Node_Id := Type_High_Bound (Etype (Ix)); | |
2594 | Lov : Uint; | |
2595 | Hiv : Uint; | |
2596 | ||
2597 | -- The following constant determines the maximum size of an | |
2598 | -- aggregate produced by converting named to positional | |
2599 | -- notation (e.g. from others clauses). This avoids running | |
2600 | -- away with attempts to convert huge aggregates. | |
2601 | ||
2602 | -- The normal limit is 5000, but we increase this limit to | |
2603 | -- 2**24 (about 16 million) if Restrictions (No_Elaboration_Code) | |
2604 | -- or Restrictions (No_Implicit_Loops) is specified, since in | |
2605 | -- either case, we are at risk of declaring the program illegal | |
2606 | -- because of this limit. | |
2607 | ||
2608 | Max_Aggr_Size : constant Nat := | |
2609 | 5000 + (2 ** 24 - 5000) * Boolean'Pos | |
2610 | (Restrictions (No_Elaboration_Code) | |
2611 | or else | |
2612 | Restrictions (No_Implicit_Loops)); | |
2613 | begin | |
07fc65c4 | 2614 | |
fbf5a39b AC |
2615 | if Nkind (Original_Node (N)) = N_String_Literal then |
2616 | return True; | |
2617 | end if; | |
07fc65c4 | 2618 | |
fbf5a39b | 2619 | -- Bounds need to be known at compile time |
07fc65c4 | 2620 | |
fbf5a39b AC |
2621 | if not Compile_Time_Known_Value (Lo) |
2622 | or else not Compile_Time_Known_Value (Hi) | |
2623 | then | |
2624 | return False; | |
2625 | end if; | |
07fc65c4 | 2626 | |
fbf5a39b AC |
2627 | -- Get bounds and check reasonable size (positive, not too large) |
2628 | -- Also only handle bounds starting at the base type low bound | |
2629 | -- for now since the compiler isn't able to handle different low | |
2630 | -- bounds yet. Case such as new String'(3..5 => ' ') will get | |
2631 | -- the wrong bounds, though it seems that the aggregate should | |
2632 | -- retain the bounds set on its Etype (see C64103E and CC1311B). | |
07fc65c4 | 2633 | |
fbf5a39b AC |
2634 | Lov := Expr_Value (Lo); |
2635 | Hiv := Expr_Value (Hi); | |
07fc65c4 | 2636 | |
fbf5a39b AC |
2637 | if Hiv < Lov |
2638 | or else (Hiv - Lov > Max_Aggr_Size) | |
2639 | or else not Compile_Time_Known_Value (Blo) | |
2640 | or else (Lov /= Expr_Value (Blo)) | |
2641 | then | |
2642 | return False; | |
2643 | end if; | |
07fc65c4 | 2644 | |
fbf5a39b | 2645 | -- Bounds must be in integer range (for array Vals below) |
07fc65c4 | 2646 | |
fbf5a39b AC |
2647 | if not UI_Is_In_Int_Range (Lov) |
2648 | or else | |
2649 | not UI_Is_In_Int_Range (Hiv) | |
2650 | then | |
2651 | return False; | |
2652 | end if; | |
07fc65c4 | 2653 | |
fbf5a39b AC |
2654 | -- Determine if set of alternatives is suitable for conversion |
2655 | -- and build an array containing the values in sequence. | |
07fc65c4 | 2656 | |
fbf5a39b AC |
2657 | declare |
2658 | Vals : array (UI_To_Int (Lov) .. UI_To_Int (Hiv)) | |
2659 | of Node_Id := (others => Empty); | |
2660 | -- The values in the aggregate sorted appropriately | |
07fc65c4 | 2661 | |
fbf5a39b AC |
2662 | Vlist : List_Id; |
2663 | -- Same data as Vals in list form | |
07fc65c4 | 2664 | |
fbf5a39b AC |
2665 | Rep_Count : Nat; |
2666 | -- Used to validate Max_Others_Replicate limit | |
07fc65c4 | 2667 | |
fbf5a39b AC |
2668 | Elmt : Node_Id; |
2669 | Num : Int := UI_To_Int (Lov); | |
2670 | Choice : Node_Id; | |
2671 | Lo, Hi : Node_Id; | |
07fc65c4 | 2672 | |
fbf5a39b AC |
2673 | begin |
2674 | if Present (Expressions (N)) then | |
2675 | Elmt := First (Expressions (N)); | |
2676 | ||
2677 | while Present (Elmt) loop | |
2678 | if Nkind (Elmt) = N_Aggregate | |
2679 | and then Present (Next_Index (Ix)) | |
2680 | and then | |
2681 | not Flatten (Elmt, Next_Index (Ix), Next_Index (Ixb)) | |
2682 | then | |
2683 | return False; | |
2684 | end if; | |
07fc65c4 | 2685 | |
fbf5a39b AC |
2686 | Vals (Num) := Relocate_Node (Elmt); |
2687 | Num := Num + 1; | |
07fc65c4 | 2688 | |
fbf5a39b AC |
2689 | Next (Elmt); |
2690 | end loop; | |
2691 | end if; | |
07fc65c4 | 2692 | |
fbf5a39b AC |
2693 | if No (Component_Associations (N)) then |
2694 | return True; | |
2695 | end if; | |
07fc65c4 | 2696 | |
fbf5a39b | 2697 | Elmt := First (Component_Associations (N)); |
07fc65c4 | 2698 | |
fbf5a39b AC |
2699 | if Nkind (Expression (Elmt)) = N_Aggregate then |
2700 | if Present (Next_Index (Ix)) | |
2701 | and then | |
2702 | not Flatten | |
2703 | (Expression (Elmt), Next_Index (Ix), Next_Index (Ixb)) | |
2704 | then | |
2705 | return False; | |
2706 | end if; | |
2707 | end if; | |
07fc65c4 | 2708 | |
fbf5a39b AC |
2709 | Component_Loop : while Present (Elmt) loop |
2710 | Choice := First (Choices (Elmt)); | |
2711 | Choice_Loop : while Present (Choice) loop | |
2712 | ||
2713 | -- If we have an others choice, fill in the missing elements | |
2714 | -- subject to the limit established by Max_Others_Replicate. | |
2715 | ||
2716 | if Nkind (Choice) = N_Others_Choice then | |
2717 | Rep_Count := 0; | |
2718 | ||
2719 | for J in Vals'Range loop | |
2720 | if No (Vals (J)) then | |
2721 | Vals (J) := New_Copy_Tree (Expression (Elmt)); | |
2722 | Rep_Count := Rep_Count + 1; | |
2723 | ||
2724 | -- Check for maximum others replication. Note that | |
2725 | -- we skip this test if either of the restrictions | |
2726 | -- No_Elaboration_Code or No_Implicit_Loops is | |
2727 | -- active, or if this is a preelaborable unit. | |
2728 | ||
2729 | declare | |
2730 | P : constant Entity_Id := | |
2731 | Cunit_Entity (Current_Sem_Unit); | |
2732 | ||
2733 | begin | |
2734 | if Restrictions (No_Elaboration_Code) | |
2735 | or else Restrictions (No_Implicit_Loops) | |
2736 | or else Is_Preelaborated (P) | |
2737 | or else (Ekind (P) = E_Package_Body | |
2738 | and then | |
2739 | Is_Preelaborated (Spec_Entity (P))) | |
2740 | then | |
2741 | null; | |
2742 | elsif Rep_Count > Max_Others_Replicate then | |
2743 | return False; | |
2744 | end if; | |
2745 | end; | |
2746 | end if; | |
2747 | end loop; | |
07fc65c4 | 2748 | |
fbf5a39b | 2749 | exit Component_Loop; |
07fc65c4 | 2750 | |
fbf5a39b | 2751 | -- Case of a subtype mark |
07fc65c4 | 2752 | |
fbf5a39b AC |
2753 | elsif Nkind (Choice) = N_Identifier |
2754 | and then Is_Type (Entity (Choice)) | |
2755 | then | |
2756 | Lo := Type_Low_Bound (Etype (Choice)); | |
2757 | Hi := Type_High_Bound (Etype (Choice)); | |
07fc65c4 | 2758 | |
fbf5a39b | 2759 | -- Case of subtype indication |
07fc65c4 | 2760 | |
fbf5a39b AC |
2761 | elsif Nkind (Choice) = N_Subtype_Indication then |
2762 | Lo := Low_Bound (Range_Expression (Constraint (Choice))); | |
2763 | Hi := High_Bound (Range_Expression (Constraint (Choice))); | |
2764 | ||
2765 | -- Case of a range | |
2766 | ||
2767 | elsif Nkind (Choice) = N_Range then | |
2768 | Lo := Low_Bound (Choice); | |
2769 | Hi := High_Bound (Choice); | |
2770 | ||
2771 | -- Normal subexpression case | |
2772 | ||
2773 | else pragma Assert (Nkind (Choice) in N_Subexpr); | |
2774 | if not Compile_Time_Known_Value (Choice) then | |
2775 | return False; | |
2776 | ||
2777 | else | |
2778 | Vals (UI_To_Int (Expr_Value (Choice))) := | |
2779 | New_Copy_Tree (Expression (Elmt)); | |
2780 | goto Continue; | |
07fc65c4 | 2781 | end if; |
fbf5a39b AC |
2782 | end if; |
2783 | ||
2784 | -- Range cases merge with Lo,Hi said | |
2785 | ||
2786 | if not Compile_Time_Known_Value (Lo) | |
2787 | or else | |
2788 | not Compile_Time_Known_Value (Hi) | |
2789 | then | |
2790 | return False; | |
2791 | else | |
2792 | for J in UI_To_Int (Expr_Value (Lo)) .. | |
2793 | UI_To_Int (Expr_Value (Hi)) | |
2794 | loop | |
2795 | Vals (J) := New_Copy_Tree (Expression (Elmt)); | |
2796 | end loop; | |
2797 | end if; | |
07fc65c4 | 2798 | |
fbf5a39b AC |
2799 | <<Continue>> |
2800 | Next (Choice); | |
2801 | end loop Choice_Loop; | |
07fc65c4 | 2802 | |
fbf5a39b AC |
2803 | Next (Elmt); |
2804 | end loop Component_Loop; | |
07fc65c4 | 2805 | |
fbf5a39b | 2806 | -- If we get here the conversion is possible |
07fc65c4 | 2807 | |
fbf5a39b AC |
2808 | Vlist := New_List; |
2809 | for J in Vals'Range loop | |
2810 | Append (Vals (J), Vlist); | |
2811 | end loop; | |
07fc65c4 | 2812 | |
fbf5a39b AC |
2813 | Rewrite (N, Make_Aggregate (Loc, Expressions => Vlist)); |
2814 | Set_Aggregate_Bounds (N, Aggregate_Bounds (Original_Node (N))); | |
2815 | return True; | |
2816 | end; | |
2817 | end Flatten; | |
07fc65c4 | 2818 | |
fbf5a39b AC |
2819 | ------------- |
2820 | -- Is_Flat -- | |
2821 | ------------- | |
07fc65c4 | 2822 | |
fbf5a39b AC |
2823 | function Is_Flat (N : Node_Id; Dims : Int) return Boolean is |
2824 | Elmt : Node_Id; | |
07fc65c4 | 2825 | |
fbf5a39b AC |
2826 | begin |
2827 | if Dims = 0 then | |
2828 | return True; | |
07fc65c4 | 2829 | |
fbf5a39b AC |
2830 | elsif Nkind (N) = N_Aggregate then |
2831 | if Present (Component_Associations (N)) then | |
2832 | return False; | |
07fc65c4 | 2833 | |
fbf5a39b AC |
2834 | else |
2835 | Elmt := First (Expressions (N)); | |
2836 | ||
2837 | while Present (Elmt) loop | |
2838 | if not Is_Flat (Elmt, Dims - 1) then | |
2839 | return False; | |
07fc65c4 | 2840 | end if; |
07fc65c4 | 2841 | |
fbf5a39b AC |
2842 | Next (Elmt); |
2843 | end loop; | |
07fc65c4 | 2844 | |
fbf5a39b AC |
2845 | return True; |
2846 | end if; | |
2847 | else | |
2848 | return True; | |
2849 | end if; | |
2850 | end Is_Flat; | |
07fc65c4 | 2851 | |
fbf5a39b | 2852 | -- Start of processing for Convert_To_Positional |
07fc65c4 | 2853 | |
fbf5a39b | 2854 | begin |
c45b6ae0 AC |
2855 | -- Ada0Y (AI-287): Do not convert in case of default initialized |
2856 | -- components because in this case will need to call the corresponding | |
2857 | -- IP procedure. | |
2858 | ||
2859 | if Has_Default_Init_Comps (N) then | |
2860 | return; | |
2861 | end if; | |
2862 | ||
fbf5a39b AC |
2863 | if Is_Flat (N, Number_Dimensions (Typ)) then |
2864 | return; | |
2865 | end if; | |
2866 | ||
2867 | if Is_Bit_Packed_Array (Typ) | |
2868 | and then not Handle_Bit_Packed | |
2869 | then | |
2870 | return; | |
2871 | end if; | |
07fc65c4 | 2872 | |
fbf5a39b AC |
2873 | -- Do not convert to positional if controlled components are |
2874 | -- involved since these require special processing | |
07fc65c4 | 2875 | |
fbf5a39b AC |
2876 | if Has_Controlled_Component (Typ) then |
2877 | return; | |
2878 | end if; | |
07fc65c4 | 2879 | |
fbf5a39b | 2880 | if Flatten (N, First_Index (Typ), First_Index (Base_Type (Typ))) then |
07fc65c4 | 2881 | Analyze_And_Resolve (N, Typ); |
fbf5a39b | 2882 | end if; |
07fc65c4 GB |
2883 | end Convert_To_Positional; |
2884 | ||
70482933 RK |
2885 | ---------------------------- |
2886 | -- Expand_Array_Aggregate -- | |
2887 | ---------------------------- | |
2888 | ||
2889 | -- Array aggregate expansion proceeds as follows: | |
2890 | ||
2891 | -- 1. If requested we generate code to perform all the array aggregate | |
2892 | -- bound checks, specifically | |
2893 | ||
2894 | -- (a) Check that the index range defined by aggregate bounds is | |
2895 | -- compatible with corresponding index subtype. | |
2896 | ||
2897 | -- (b) If an others choice is present check that no aggregate | |
2898 | -- index is outside the bounds of the index constraint. | |
2899 | ||
2900 | -- (c) For multidimensional arrays make sure that all subaggregates | |
2901 | -- corresponding to the same dimension have the same bounds. | |
2902 | ||
fbf5a39b AC |
2903 | -- 2. Check for packed array aggregate which can be converted to a |
2904 | -- constant so that the aggregate disappeares completely. | |
2905 | ||
2906 | -- 3. Check case of nested aggregate. Generally nested aggregates are | |
2907 | -- handled during the processing of the parent aggregate. | |
2908 | ||
2909 | -- 4. Check if the aggregate can be statically processed. If this is the | |
70482933 RK |
2910 | -- case pass it as is to Gigi. Note that a necessary condition for |
2911 | -- static processing is that the aggregate be fully positional. | |
2912 | ||
fbf5a39b | 2913 | -- 5. If in place aggregate expansion is possible (i.e. no need to create |
70482933 RK |
2914 | -- a temporary) then mark the aggregate as such and return. Otherwise |
2915 | -- create a new temporary and generate the appropriate initialization | |
2916 | -- code. | |
2917 | ||
2918 | procedure Expand_Array_Aggregate (N : Node_Id) is | |
2919 | Loc : constant Source_Ptr := Sloc (N); | |
2920 | ||
2921 | Typ : constant Entity_Id := Etype (N); | |
2922 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
07fc65c4 | 2923 | -- Typ is the correct constrained array subtype of the aggregate |
70482933 RK |
2924 | -- Ctyp is the corresponding component type. |
2925 | ||
2926 | Aggr_Dimension : constant Pos := Number_Dimensions (Typ); | |
2927 | -- Number of aggregate index dimensions. | |
2928 | ||
2929 | Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id; | |
2930 | Aggr_High : array (1 .. Aggr_Dimension) of Node_Id; | |
2931 | -- Low and High bounds of the constraint for each aggregate index. | |
2932 | ||
2933 | Aggr_Index_Typ : array (1 .. Aggr_Dimension) of Entity_Id; | |
2934 | -- The type of each index. | |
2935 | ||
2936 | Maybe_In_Place_OK : Boolean; | |
2937 | -- If the type is neither controlled nor packed and the aggregate | |
2938 | -- is the expression in an assignment, assignment in place may be | |
2939 | -- possible, provided other conditions are met on the LHS. | |
2940 | ||
07fc65c4 GB |
2941 | Others_Present : array (1 .. Aggr_Dimension) of Boolean := |
2942 | (others => False); | |
2943 | -- If Others_Present (J) is True, then there is an others choice | |
2944 | -- in one of the sub-aggregates of N at dimension J. | |
70482933 RK |
2945 | |
2946 | procedure Build_Constrained_Type (Positional : Boolean); | |
2947 | -- If the subtype is not static or unconstrained, build a constrained | |
2948 | -- type using the computable sizes of the aggregate and its sub- | |
2949 | -- aggregates. | |
2950 | ||
2951 | procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id); | |
2952 | -- Checks that the bounds of Aggr_Bounds are within the bounds defined | |
2953 | -- by Index_Bounds. | |
2954 | ||
2955 | procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos); | |
2956 | -- Checks that in a multi-dimensional array aggregate all subaggregates | |
2957 | -- corresponding to the same dimension have the same bounds. | |
2958 | -- Sub_Aggr is an array sub-aggregate. Dim is the dimension | |
2959 | -- corresponding to the sub-aggregate. | |
2960 | ||
2961 | procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos); | |
2962 | -- Computes the values of array Others_Present. Sub_Aggr is the | |
2963 | -- array sub-aggregate we start the computation from. Dim is the | |
2964 | -- dimension corresponding to the sub-aggregate. | |
2965 | ||
70482933 RK |
2966 | function Has_Address_Clause (D : Node_Id) return Boolean; |
2967 | -- If the aggregate is the expression in an object declaration, it | |
2968 | -- cannot be expanded in place. This function does a lookahead in the | |
2969 | -- current declarative part to find an address clause for the object | |
2970 | -- being declared. | |
2971 | ||
2972 | function In_Place_Assign_OK return Boolean; | |
2973 | -- Simple predicate to determine whether an aggregate assignment can | |
2974 | -- be done in place, because none of the new values can depend on the | |
2975 | -- components of the target of the assignment. | |
2976 | ||
fbf5a39b AC |
2977 | function Must_Slide (N : Node_Id; Typ : Entity_Id) return Boolean; |
2978 | -- A static aggregate in an object declaration can in most cases be | |
2979 | -- expanded in place. The one exception is when the aggregate is given | |
2980 | -- with component associations that specify different bounds from those | |
2981 | -- of the type definition in the object declaration. In this rather | |
2982 | -- pathological case the aggregate must slide, and we must introduce | |
2983 | -- an intermediate temporary to hold it. | |
2984 | ||
70482933 RK |
2985 | procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos); |
2986 | -- Checks that if an others choice is present in any sub-aggregate no | |
2987 | -- aggregate index is outside the bounds of the index constraint. | |
2988 | -- Sub_Aggr is an array sub-aggregate. Dim is the dimension | |
2989 | -- corresponding to the sub-aggregate. | |
2990 | ||
2991 | ---------------------------- | |
2992 | -- Build_Constrained_Type -- | |
2993 | ---------------------------- | |
2994 | ||
2995 | procedure Build_Constrained_Type (Positional : Boolean) is | |
fbf5a39b AC |
2996 | Loc : constant Source_Ptr := Sloc (N); |
2997 | Agg_Type : Entity_Id; | |
2998 | Comp : Node_Id; | |
2999 | Decl : Node_Id; | |
3000 | Typ : constant Entity_Id := Etype (N); | |
3001 | Indices : constant List_Id := New_List; | |
3002 | Num : Int; | |
3003 | Sub_Agg : Node_Id; | |
70482933 RK |
3004 | |
3005 | begin | |
3006 | Agg_Type := | |
3007 | Make_Defining_Identifier ( | |
3008 | Loc, New_Internal_Name ('A')); | |
3009 | ||
3010 | -- If the aggregate is purely positional, all its subaggregates | |
3011 | -- have the same size. We collect the dimensions from the first | |
3012 | -- subaggregate at each level. | |
3013 | ||
3014 | if Positional then | |
3015 | Sub_Agg := N; | |
3016 | ||
3017 | for D in 1 .. Number_Dimensions (Typ) loop | |
3018 | Comp := First (Expressions (Sub_Agg)); | |
3019 | ||
3020 | Sub_Agg := Comp; | |
3021 | Num := 0; | |
3022 | ||
3023 | while Present (Comp) loop | |
3024 | Num := Num + 1; | |
3025 | Next (Comp); | |
3026 | end loop; | |
3027 | ||
3028 | Append ( | |
3029 | Make_Range (Loc, | |
3030 | Low_Bound => Make_Integer_Literal (Loc, 1), | |
3031 | High_Bound => | |
3032 | Make_Integer_Literal (Loc, Num)), | |
3033 | Indices); | |
3034 | end loop; | |
3035 | ||
3036 | else | |
70482933 RK |
3037 | -- We know the aggregate type is unconstrained and the |
3038 | -- aggregate is not processable by the back end, therefore | |
3039 | -- not necessarily positional. Retrieve the bounds of each | |
3040 | -- dimension as computed earlier. | |
3041 | ||
3042 | for D in 1 .. Number_Dimensions (Typ) loop | |
3043 | Append ( | |
3044 | Make_Range (Loc, | |
3045 | Low_Bound => Aggr_Low (D), | |
3046 | High_Bound => Aggr_High (D)), | |
3047 | Indices); | |
3048 | end loop; | |
3049 | end if; | |
3050 | ||
3051 | Decl := | |
3052 | Make_Full_Type_Declaration (Loc, | |
3053 | Defining_Identifier => Agg_Type, | |
3054 | Type_Definition => | |
3055 | Make_Constrained_Array_Definition (Loc, | |
3056 | Discrete_Subtype_Definitions => Indices, | |
3057 | Subtype_Indication => | |
3058 | New_Occurrence_Of (Component_Type (Typ), Loc))); | |
3059 | ||
3060 | Insert_Action (N, Decl); | |
3061 | Analyze (Decl); | |
3062 | Set_Etype (N, Agg_Type); | |
3063 | Set_Is_Itype (Agg_Type); | |
3064 | Freeze_Itype (Agg_Type, N); | |
3065 | end Build_Constrained_Type; | |
3066 | ||
3067 | ------------------ | |
3068 | -- Check_Bounds -- | |
3069 | ------------------ | |
3070 | ||
3071 | procedure Check_Bounds (Aggr_Bounds : Node_Id; Index_Bounds : Node_Id) is | |
3072 | Aggr_Lo : Node_Id; | |
3073 | Aggr_Hi : Node_Id; | |
3074 | ||
3075 | Ind_Lo : Node_Id; | |
3076 | Ind_Hi : Node_Id; | |
3077 | ||
3078 | Cond : Node_Id := Empty; | |
3079 | ||
3080 | begin | |
3081 | Get_Index_Bounds (Aggr_Bounds, Aggr_Lo, Aggr_Hi); | |
3082 | Get_Index_Bounds (Index_Bounds, Ind_Lo, Ind_Hi); | |
3083 | ||
3084 | -- Generate the following test: | |
3085 | -- | |
3086 | -- [constraint_error when | |
3087 | -- Aggr_Lo <= Aggr_Hi and then | |
3088 | -- (Aggr_Lo < Ind_Lo or else Aggr_Hi > Ind_Hi)] | |
3089 | -- | |
3090 | -- As an optimization try to see if some tests are trivially vacuos | |
3091 | -- because we are comparing an expression against itself. | |
3092 | ||
3093 | if Aggr_Lo = Ind_Lo and then Aggr_Hi = Ind_Hi then | |
3094 | Cond := Empty; | |
3095 | ||
3096 | elsif Aggr_Hi = Ind_Hi then | |
3097 | Cond := | |
3098 | Make_Op_Lt (Loc, | |
fbf5a39b AC |
3099 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), |
3100 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)); | |
70482933 RK |
3101 | |
3102 | elsif Aggr_Lo = Ind_Lo then | |
3103 | Cond := | |
3104 | Make_Op_Gt (Loc, | |
fbf5a39b AC |
3105 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi), |
3106 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Hi)); | |
70482933 RK |
3107 | |
3108 | else | |
3109 | Cond := | |
3110 | Make_Or_Else (Loc, | |
3111 | Left_Opnd => | |
3112 | Make_Op_Lt (Loc, | |
fbf5a39b AC |
3113 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), |
3114 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Ind_Lo)), | |
70482933 RK |
3115 | |
3116 | Right_Opnd => | |
3117 | Make_Op_Gt (Loc, | |
3118 | Left_Opnd => Duplicate_Subexpr (Aggr_Hi), | |
3119 | Right_Opnd => Duplicate_Subexpr (Ind_Hi))); | |
3120 | end if; | |
3121 | ||
3122 | if Present (Cond) then | |
3123 | Cond := | |
3124 | Make_And_Then (Loc, | |
3125 | Left_Opnd => | |
3126 | Make_Op_Le (Loc, | |
fbf5a39b AC |
3127 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), |
3128 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi)), | |
70482933 RK |
3129 | |
3130 | Right_Opnd => Cond); | |
3131 | ||
3132 | Set_Analyzed (Left_Opnd (Left_Opnd (Cond)), False); | |
3133 | Set_Analyzed (Right_Opnd (Left_Opnd (Cond)), False); | |
3134 | Insert_Action (N, | |
07fc65c4 GB |
3135 | Make_Raise_Constraint_Error (Loc, |
3136 | Condition => Cond, | |
3137 | Reason => CE_Length_Check_Failed)); | |
70482933 RK |
3138 | end if; |
3139 | end Check_Bounds; | |
3140 | ||
3141 | ---------------------------- | |
3142 | -- Check_Same_Aggr_Bounds -- | |
3143 | ---------------------------- | |
3144 | ||
3145 | procedure Check_Same_Aggr_Bounds (Sub_Aggr : Node_Id; Dim : Pos) is | |
3146 | Sub_Lo : constant Node_Id := Low_Bound (Aggregate_Bounds (Sub_Aggr)); | |
3147 | Sub_Hi : constant Node_Id := High_Bound (Aggregate_Bounds (Sub_Aggr)); | |
3148 | -- The bounds of this specific sub-aggregate. | |
3149 | ||
3150 | Aggr_Lo : constant Node_Id := Aggr_Low (Dim); | |
3151 | Aggr_Hi : constant Node_Id := Aggr_High (Dim); | |
3152 | -- The bounds of the aggregate for this dimension | |
3153 | ||
3154 | Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim); | |
3155 | -- The index type for this dimension. | |
3156 | ||
fbf5a39b | 3157 | Cond : Node_Id := Empty; |
70482933 | 3158 | |
fbf5a39b AC |
3159 | Assoc : Node_Id; |
3160 | Expr : Node_Id; | |
70482933 RK |
3161 | |
3162 | begin | |
3163 | -- If index checks are on generate the test | |
3164 | -- | |
3165 | -- [constraint_error when | |
3166 | -- Aggr_Lo /= Sub_Lo or else Aggr_Hi /= Sub_Hi] | |
3167 | -- | |
3168 | -- As an optimization try to see if some tests are trivially vacuos | |
3169 | -- because we are comparing an expression against itself. Also for | |
3170 | -- the first dimension the test is trivially vacuous because there | |
3171 | -- is just one aggregate for dimension 1. | |
3172 | ||
3173 | if Index_Checks_Suppressed (Ind_Typ) then | |
3174 | Cond := Empty; | |
3175 | ||
3176 | elsif Dim = 1 | |
3177 | or else (Aggr_Lo = Sub_Lo and then Aggr_Hi = Sub_Hi) | |
3178 | then | |
3179 | Cond := Empty; | |
3180 | ||
3181 | elsif Aggr_Hi = Sub_Hi then | |
3182 | Cond := | |
3183 | Make_Op_Ne (Loc, | |
fbf5a39b AC |
3184 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), |
3185 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)); | |
70482933 RK |
3186 | |
3187 | elsif Aggr_Lo = Sub_Lo then | |
3188 | Cond := | |
3189 | Make_Op_Ne (Loc, | |
fbf5a39b AC |
3190 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Hi), |
3191 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Hi)); | |
70482933 RK |
3192 | |
3193 | else | |
3194 | Cond := | |
3195 | Make_Or_Else (Loc, | |
3196 | Left_Opnd => | |
3197 | Make_Op_Ne (Loc, | |
fbf5a39b AC |
3198 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Aggr_Lo), |
3199 | Right_Opnd => Duplicate_Subexpr_Move_Checks (Sub_Lo)), | |
70482933 RK |
3200 | |
3201 | Right_Opnd => | |
3202 | Make_Op_Ne (Loc, | |
3203 | Left_Opnd => Duplicate_Subexpr (Aggr_Hi), | |
3204 | Right_Opnd => Duplicate_Subexpr (Sub_Hi))); | |
3205 | end if; | |
3206 | ||
3207 | if Present (Cond) then | |
3208 | Insert_Action (N, | |
07fc65c4 GB |
3209 | Make_Raise_Constraint_Error (Loc, |
3210 | Condition => Cond, | |
3211 | Reason => CE_Length_Check_Failed)); | |
70482933 RK |
3212 | end if; |
3213 | ||
3214 | -- Now look inside the sub-aggregate to see if there is more work | |
3215 | ||
3216 | if Dim < Aggr_Dimension then | |
3217 | ||
3218 | -- Process positional components | |
3219 | ||
3220 | if Present (Expressions (Sub_Aggr)) then | |
3221 | Expr := First (Expressions (Sub_Aggr)); | |
3222 | while Present (Expr) loop | |
3223 | Check_Same_Aggr_Bounds (Expr, Dim + 1); | |
3224 | Next (Expr); | |
3225 | end loop; | |
3226 | end if; | |
3227 | ||
3228 | -- Process component associations | |
3229 | ||
3230 | if Present (Component_Associations (Sub_Aggr)) then | |
3231 | Assoc := First (Component_Associations (Sub_Aggr)); | |
3232 | while Present (Assoc) loop | |
3233 | Expr := Expression (Assoc); | |
3234 | Check_Same_Aggr_Bounds (Expr, Dim + 1); | |
3235 | Next (Assoc); | |
3236 | end loop; | |
3237 | end if; | |
3238 | end if; | |
3239 | end Check_Same_Aggr_Bounds; | |
3240 | ||
3241 | ---------------------------- | |
3242 | -- Compute_Others_Present -- | |
3243 | ---------------------------- | |
3244 | ||
3245 | procedure Compute_Others_Present (Sub_Aggr : Node_Id; Dim : Pos) is | |
fbf5a39b AC |
3246 | Assoc : Node_Id; |
3247 | Expr : Node_Id; | |
70482933 RK |
3248 | |
3249 | begin | |
3250 | if Present (Component_Associations (Sub_Aggr)) then | |
3251 | Assoc := Last (Component_Associations (Sub_Aggr)); | |
07fc65c4 | 3252 | |
70482933 RK |
3253 | if Nkind (First (Choices (Assoc))) = N_Others_Choice then |
3254 | Others_Present (Dim) := True; | |
3255 | end if; | |
3256 | end if; | |
3257 | ||
3258 | -- Now look inside the sub-aggregate to see if there is more work | |
3259 | ||
3260 | if Dim < Aggr_Dimension then | |
3261 | ||
3262 | -- Process positional components | |
3263 | ||
3264 | if Present (Expressions (Sub_Aggr)) then | |
3265 | Expr := First (Expressions (Sub_Aggr)); | |
3266 | while Present (Expr) loop | |
3267 | Compute_Others_Present (Expr, Dim + 1); | |
3268 | Next (Expr); | |
3269 | end loop; | |
3270 | end if; | |
3271 | ||
3272 | -- Process component associations | |
3273 | ||
3274 | if Present (Component_Associations (Sub_Aggr)) then | |
3275 | Assoc := First (Component_Associations (Sub_Aggr)); | |
3276 | while Present (Assoc) loop | |
3277 | Expr := Expression (Assoc); | |
3278 | Compute_Others_Present (Expr, Dim + 1); | |
3279 | Next (Assoc); | |
3280 | end loop; | |
3281 | end if; | |
3282 | end if; | |
3283 | end Compute_Others_Present; | |
3284 | ||
fbf5a39b AC |
3285 | ------------------------ |
3286 | -- Has_Address_Clause -- | |
3287 | ------------------------ | |
70482933 RK |
3288 | |
3289 | function Has_Address_Clause (D : Node_Id) return Boolean is | |
fbf5a39b | 3290 | Id : constant Entity_Id := Defining_Identifier (D); |
70482933 RK |
3291 | Decl : Node_Id := Next (D); |
3292 | ||
3293 | begin | |
3294 | while Present (Decl) loop | |
70482933 RK |
3295 | if Nkind (Decl) = N_At_Clause |
3296 | and then Chars (Identifier (Decl)) = Chars (Id) | |
3297 | then | |
3298 | return True; | |
3299 | ||
3300 | elsif Nkind (Decl) = N_Attribute_Definition_Clause | |
3301 | and then Chars (Decl) = Name_Address | |
3302 | and then Chars (Name (Decl)) = Chars (Id) | |
3303 | then | |
3304 | return True; | |
3305 | end if; | |
3306 | ||
3307 | Next (Decl); | |
3308 | end loop; | |
3309 | ||
3310 | return False; | |
3311 | end Has_Address_Clause; | |
3312 | ||
3313 | ------------------------ | |
3314 | -- In_Place_Assign_OK -- | |
3315 | ------------------------ | |
3316 | ||
3317 | function In_Place_Assign_OK return Boolean is | |
3318 | Aggr_In : Node_Id; | |
3319 | Aggr_Lo : Node_Id; | |
3320 | Aggr_Hi : Node_Id; | |
3321 | Obj_In : Node_Id; | |
3322 | Obj_Lo : Node_Id; | |
3323 | Obj_Hi : Node_Id; | |
3324 | ||
07fc65c4 GB |
3325 | function Is_Others_Aggregate (Aggr : Node_Id) return Boolean; |
3326 | -- Aggregates that consist of a single Others choice are safe | |
3327 | -- if the single expression is. | |
3328 | ||
70482933 RK |
3329 | function Safe_Aggregate (Aggr : Node_Id) return Boolean; |
3330 | -- Check recursively that each component of a (sub)aggregate does | |
3331 | -- not depend on the variable being assigned to. | |
3332 | ||
3333 | function Safe_Component (Expr : Node_Id) return Boolean; | |
3334 | -- Verify that an expression cannot depend on the variable being | |
3335 | -- assigned to. Room for improvement here (but less than before). | |
3336 | ||
07fc65c4 GB |
3337 | ------------------------- |
3338 | -- Is_Others_Aggregate -- | |
3339 | ------------------------- | |
3340 | ||
3341 | function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is | |
3342 | begin | |
3343 | return No (Expressions (Aggr)) | |
3344 | and then Nkind | |
3345 | (First (Choices (First (Component_Associations (Aggr))))) | |
3346 | = N_Others_Choice; | |
3347 | end Is_Others_Aggregate; | |
3348 | ||
70482933 RK |
3349 | -------------------- |
3350 | -- Safe_Aggregate -- | |
3351 | -------------------- | |
3352 | ||
3353 | function Safe_Aggregate (Aggr : Node_Id) return Boolean is | |
3354 | Expr : Node_Id; | |
3355 | ||
3356 | begin | |
3357 | if Present (Expressions (Aggr)) then | |
3358 | Expr := First (Expressions (Aggr)); | |
3359 | ||
3360 | while Present (Expr) loop | |
3361 | if Nkind (Expr) = N_Aggregate then | |
3362 | if not Safe_Aggregate (Expr) then | |
3363 | return False; | |
3364 | end if; | |
3365 | ||
3366 | elsif not Safe_Component (Expr) then | |
3367 | return False; | |
3368 | end if; | |
3369 | ||
3370 | Next (Expr); | |
3371 | end loop; | |
3372 | end if; | |
3373 | ||
3374 | if Present (Component_Associations (Aggr)) then | |
3375 | Expr := First (Component_Associations (Aggr)); | |
3376 | ||
3377 | while Present (Expr) loop | |
3378 | if Nkind (Expression (Expr)) = N_Aggregate then | |
3379 | if not Safe_Aggregate (Expression (Expr)) then | |
3380 | return False; | |
3381 | end if; | |
3382 | ||
3383 | elsif not Safe_Component (Expression (Expr)) then | |
3384 | return False; | |
3385 | end if; | |
3386 | ||
3387 | Next (Expr); | |
3388 | end loop; | |
3389 | end if; | |
3390 | ||
3391 | return True; | |
3392 | end Safe_Aggregate; | |
3393 | ||
3394 | -------------------- | |
3395 | -- Safe_Component -- | |
3396 | -------------------- | |
3397 | ||
3398 | function Safe_Component (Expr : Node_Id) return Boolean is | |
3399 | Comp : Node_Id := Expr; | |
3400 | ||
3401 | function Check_Component (Comp : Node_Id) return Boolean; | |
3402 | -- Do the recursive traversal, after copy. | |
3403 | ||
fbf5a39b AC |
3404 | --------------------- |
3405 | -- Check_Component -- | |
3406 | --------------------- | |
3407 | ||
70482933 RK |
3408 | function Check_Component (Comp : Node_Id) return Boolean is |
3409 | begin | |
3410 | if Is_Overloaded (Comp) then | |
3411 | return False; | |
3412 | end if; | |
3413 | ||
3414 | return Compile_Time_Known_Value (Comp) | |
3415 | ||
3416 | or else (Is_Entity_Name (Comp) | |
3417 | and then Present (Entity (Comp)) | |
3418 | and then No (Renamed_Object (Entity (Comp)))) | |
3419 | ||
3420 | or else (Nkind (Comp) = N_Attribute_Reference | |
3421 | and then Check_Component (Prefix (Comp))) | |
3422 | ||
3423 | or else (Nkind (Comp) in N_Binary_Op | |
3424 | and then Check_Component (Left_Opnd (Comp)) | |
3425 | and then Check_Component (Right_Opnd (Comp))) | |
3426 | ||
3427 | or else (Nkind (Comp) in N_Unary_Op | |
3428 | and then Check_Component (Right_Opnd (Comp))) | |
3429 | ||
3430 | or else (Nkind (Comp) = N_Selected_Component | |
3431 | and then Check_Component (Prefix (Comp))); | |
3432 | end Check_Component; | |
3433 | ||
fbf5a39b | 3434 | -- Start of processing for Safe_Component |
70482933 RK |
3435 | |
3436 | begin | |
3437 | -- If the component appears in an association that may | |
3438 | -- correspond to more than one element, it is not analyzed | |
3439 | -- before the expansion into assignments, to avoid side effects. | |
3440 | -- We analyze, but do not resolve the copy, to obtain sufficient | |
3441 | -- entity information for the checks that follow. If component is | |
3442 | -- overloaded we assume an unsafe function call. | |
3443 | ||
3444 | if not Analyzed (Comp) then | |
3445 | if Is_Overloaded (Expr) then | |
3446 | return False; | |
07fc65c4 GB |
3447 | |
3448 | elsif Nkind (Expr) = N_Aggregate | |
3449 | and then not Is_Others_Aggregate (Expr) | |
3450 | then | |
3451 | return False; | |
3452 | ||
3453 | elsif Nkind (Expr) = N_Allocator then | |
3454 | -- For now, too complex to analyze. | |
3455 | ||
3456 | return False; | |
70482933 RK |
3457 | end if; |
3458 | ||
3459 | Comp := New_Copy_Tree (Expr); | |
07fc65c4 | 3460 | Set_Parent (Comp, Parent (Expr)); |
70482933 RK |
3461 | Analyze (Comp); |
3462 | end if; | |
3463 | ||
07fc65c4 GB |
3464 | if Nkind (Comp) = N_Aggregate then |
3465 | return Safe_Aggregate (Comp); | |
3466 | else | |
3467 | return Check_Component (Comp); | |
3468 | end if; | |
70482933 RK |
3469 | end Safe_Component; |
3470 | ||
3471 | -- Start of processing for In_Place_Assign_OK | |
3472 | ||
3473 | begin | |
3474 | if Present (Component_Associations (N)) then | |
3475 | ||
3476 | -- On assignment, sliding can take place, so we cannot do the | |
3477 | -- assignment in place unless the bounds of the aggregate are | |
3478 | -- statically equal to those of the target. | |
3479 | ||
3480 | -- If the aggregate is given by an others choice, the bounds | |
3481 | -- are derived from the left-hand side, and the assignment is | |
3482 | -- safe if the expression is. | |
3483 | ||
07fc65c4 | 3484 | if Is_Others_Aggregate (N) then |
70482933 RK |
3485 | return |
3486 | Safe_Component | |
3487 | (Expression (First (Component_Associations (N)))); | |
3488 | end if; | |
3489 | ||
3490 | Aggr_In := First_Index (Etype (N)); | |
3491 | Obj_In := First_Index (Etype (Name (Parent (N)))); | |
3492 | ||
3493 | while Present (Aggr_In) loop | |
3494 | Get_Index_Bounds (Aggr_In, Aggr_Lo, Aggr_Hi); | |
3495 | Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi); | |
3496 | ||
3497 | if not Compile_Time_Known_Value (Aggr_Lo) | |
3498 | or else not Compile_Time_Known_Value (Aggr_Hi) | |
3499 | or else not Compile_Time_Known_Value (Obj_Lo) | |
3500 | or else not Compile_Time_Known_Value (Obj_Hi) | |
3501 | or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo) | |
3502 | or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi) | |
3503 | then | |
3504 | return False; | |
3505 | end if; | |
3506 | ||
3507 | Next_Index (Aggr_In); | |
3508 | Next_Index (Obj_In); | |
3509 | end loop; | |
3510 | end if; | |
3511 | ||
3512 | -- Now check the component values themselves. | |
3513 | ||
3514 | return Safe_Aggregate (N); | |
3515 | end In_Place_Assign_OK; | |
3516 | ||
fbf5a39b AC |
3517 | ---------------- |
3518 | -- Must_Slide -- | |
3519 | ---------------- | |
3520 | ||
3521 | function Must_Slide (N : Node_Id; Typ : Entity_Id) return Boolean | |
3522 | is | |
3523 | Obj_Type : Entity_Id := Etype (Defining_Identifier (Parent (N))); | |
3524 | ||
3525 | L1, L2, H1, H2 : Node_Id; | |
3526 | ||
3527 | begin | |
3528 | -- No sliding if the type of the object is not established yet, if | |
3529 | -- it is an unconstrained type whose actual subtype comes from the | |
3530 | -- aggregate, or if the two types are identical. | |
3531 | ||
3532 | if not Is_Array_Type (Obj_Type) then | |
3533 | return False; | |
3534 | ||
3535 | elsif not Is_Constrained (Obj_Type) then | |
3536 | return False; | |
3537 | ||
3538 | elsif Typ = Obj_Type then | |
3539 | return False; | |
3540 | ||
3541 | else | |
3542 | -- Sliding can only occur along the first dimension | |
3543 | ||
3544 | Get_Index_Bounds (First_Index (Typ), L1, H1); | |
3545 | Get_Index_Bounds (First_Index (Obj_Type), L2, H2); | |
3546 | ||
3547 | if not Is_Static_Expression (L1) | |
3548 | or else not Is_Static_Expression (L2) | |
3549 | or else not Is_Static_Expression (H1) | |
3550 | or else not Is_Static_Expression (H2) | |
3551 | then | |
3552 | return False; | |
3553 | else | |
3554 | return Expr_Value (L1) /= Expr_Value (L2) | |
3555 | or else Expr_Value (H1) /= Expr_Value (H2); | |
3556 | end if; | |
3557 | end if; | |
3558 | end Must_Slide; | |
3559 | ||
70482933 RK |
3560 | ------------------ |
3561 | -- Others_Check -- | |
3562 | ------------------ | |
3563 | ||
3564 | procedure Others_Check (Sub_Aggr : Node_Id; Dim : Pos) is | |
3565 | Aggr_Lo : constant Node_Id := Aggr_Low (Dim); | |
3566 | Aggr_Hi : constant Node_Id := Aggr_High (Dim); | |
3567 | -- The bounds of the aggregate for this dimension. | |
3568 | ||
3569 | Ind_Typ : constant Entity_Id := Aggr_Index_Typ (Dim); | |
3570 | -- The index type for this dimension. | |
3571 | ||
3572 | Need_To_Check : Boolean := False; | |
3573 | ||
3574 | Choices_Lo : Node_Id := Empty; | |
3575 | Choices_Hi : Node_Id := Empty; | |
3576 | -- The lowest and highest discrete choices for a named sub-aggregate | |
3577 | ||
3578 | Nb_Choices : Int := -1; | |
3579 | -- The number of discrete non-others choices in this sub-aggregate | |
3580 | ||
3581 | Nb_Elements : Uint := Uint_0; | |
3582 | -- The number of elements in a positional aggregate | |
3583 | ||
3584 | Cond : Node_Id := Empty; | |
3585 | ||
3586 | Assoc : Node_Id; | |
3587 | Choice : Node_Id; | |
3588 | Expr : Node_Id; | |
3589 | ||
3590 | begin | |
3591 | -- Check if we have an others choice. If we do make sure that this | |
3592 | -- sub-aggregate contains at least one element in addition to the | |
3593 | -- others choice. | |
3594 | ||
3595 | if Range_Checks_Suppressed (Ind_Typ) then | |
3596 | Need_To_Check := False; | |
3597 | ||
3598 | elsif Present (Expressions (Sub_Aggr)) | |
3599 | and then Present (Component_Associations (Sub_Aggr)) | |
3600 | then | |
3601 | Need_To_Check := True; | |
3602 | ||
3603 | elsif Present (Component_Associations (Sub_Aggr)) then | |
3604 | Assoc := Last (Component_Associations (Sub_Aggr)); | |
3605 | ||
3606 | if Nkind (First (Choices (Assoc))) /= N_Others_Choice then | |
3607 | Need_To_Check := False; | |
3608 | ||
3609 | else | |
3610 | -- Count the number of discrete choices. Start with -1 | |
3611 | -- because the others choice does not count. | |
3612 | ||
3613 | Nb_Choices := -1; | |
3614 | Assoc := First (Component_Associations (Sub_Aggr)); | |
3615 | while Present (Assoc) loop | |
3616 | Choice := First (Choices (Assoc)); | |
3617 | while Present (Choice) loop | |
3618 | Nb_Choices := Nb_Choices + 1; | |
3619 | Next (Choice); | |
3620 | end loop; | |
3621 | ||
3622 | Next (Assoc); | |
3623 | end loop; | |
3624 | ||
3625 | -- If there is only an others choice nothing to do | |
3626 | ||
3627 | Need_To_Check := (Nb_Choices > 0); | |
3628 | end if; | |
3629 | ||
3630 | else | |
3631 | Need_To_Check := False; | |
3632 | end if; | |
3633 | ||
3634 | -- If we are dealing with a positional sub-aggregate with an | |
07fc65c4 | 3635 | -- others choice then compute the number or positional elements. |
70482933 RK |
3636 | |
3637 | if Need_To_Check and then Present (Expressions (Sub_Aggr)) then | |
3638 | Expr := First (Expressions (Sub_Aggr)); | |
3639 | Nb_Elements := Uint_0; | |
3640 | while Present (Expr) loop | |
3641 | Nb_Elements := Nb_Elements + 1; | |
3642 | Next (Expr); | |
3643 | end loop; | |
3644 | ||
3645 | -- If the aggregate contains discrete choices and an others choice | |
3646 | -- compute the smallest and largest discrete choice values. | |
3647 | ||
3648 | elsif Need_To_Check then | |
3649 | Compute_Choices_Lo_And_Choices_Hi : declare | |
07fc65c4 | 3650 | |
70482933 RK |
3651 | Table : Case_Table_Type (1 .. Nb_Choices); |
3652 | -- Used to sort all the different choice values | |
3653 | ||
07fc65c4 | 3654 | J : Pos := 1; |
70482933 RK |
3655 | Low : Node_Id; |
3656 | High : Node_Id; | |
3657 | ||
3658 | begin | |
3659 | Assoc := First (Component_Associations (Sub_Aggr)); | |
3660 | while Present (Assoc) loop | |
3661 | Choice := First (Choices (Assoc)); | |
3662 | while Present (Choice) loop | |
3663 | if Nkind (Choice) = N_Others_Choice then | |
3664 | exit; | |
3665 | end if; | |
3666 | ||
3667 | Get_Index_Bounds (Choice, Low, High); | |
07fc65c4 GB |
3668 | Table (J).Choice_Lo := Low; |
3669 | Table (J).Choice_Hi := High; | |
70482933 | 3670 | |
07fc65c4 | 3671 | J := J + 1; |
70482933 RK |
3672 | Next (Choice); |
3673 | end loop; | |
3674 | ||
3675 | Next (Assoc); | |
3676 | end loop; | |
3677 | ||
3678 | -- Sort the discrete choices | |
3679 | ||
3680 | Sort_Case_Table (Table); | |
3681 | ||
3682 | Choices_Lo := Table (1).Choice_Lo; | |
3683 | Choices_Hi := Table (Nb_Choices).Choice_Hi; | |
3684 | end Compute_Choices_Lo_And_Choices_Hi; | |
3685 | end if; | |
3686 | ||
3687 | -- If no others choice in this sub-aggregate, or the aggregate | |
3688 | -- comprises only an others choice, nothing to do. | |
3689 | ||
3690 | if not Need_To_Check then | |
3691 | Cond := Empty; | |
3692 | ||
3693 | -- If we are dealing with an aggregate containing an others | |
3694 | -- choice and positional components, we generate the following test: | |
3695 | -- | |
3696 | -- if Ind_Typ'Pos (Aggr_Lo) + (Nb_Elements - 1) > | |
3697 | -- Ind_Typ'Pos (Aggr_Hi) | |
3698 | -- then | |
3699 | -- raise Constraint_Error; | |
3700 | -- end if; | |
3701 | ||
3702 | elsif Nb_Elements > Uint_0 then | |
3703 | Cond := | |
3704 | Make_Op_Gt (Loc, | |
3705 | Left_Opnd => | |
3706 | Make_Op_Add (Loc, | |
3707 | Left_Opnd => | |
3708 | Make_Attribute_Reference (Loc, | |
3709 | Prefix => New_Reference_To (Ind_Typ, Loc), | |
3710 | Attribute_Name => Name_Pos, | |
3711 | Expressions => | |
fbf5a39b AC |
3712 | New_List |
3713 | (Duplicate_Subexpr_Move_Checks (Aggr_Lo))), | |
70482933 RK |
3714 | Right_Opnd => Make_Integer_Literal (Loc, Nb_Elements - 1)), |
3715 | ||
3716 | Right_Opnd => | |
3717 | Make_Attribute_Reference (Loc, | |
3718 | Prefix => New_Reference_To (Ind_Typ, Loc), | |
3719 | Attribute_Name => Name_Pos, | |
fbf5a39b AC |
3720 | Expressions => New_List ( |
3721 | Duplicate_Subexpr_Move_Checks (Aggr_Hi)))); | |
70482933 RK |
3722 | |
3723 | -- If we are dealing with an aggregate containing an others | |
3724 | -- choice and discrete choices we generate the following test: | |
3725 | -- | |
3726 | -- [constraint_error when | |
3727 | -- Choices_Lo < Aggr_Lo or else Choices_Hi > Aggr_Hi]; | |
3728 | ||
3729 | else | |
3730 | Cond := | |
3731 | Make_Or_Else (Loc, | |
3732 | Left_Opnd => | |
3733 | Make_Op_Lt (Loc, | |
fbf5a39b AC |
3734 | Left_Opnd => |
3735 | Duplicate_Subexpr_Move_Checks (Choices_Lo), | |
3736 | Right_Opnd => | |
3737 | Duplicate_Subexpr_Move_Checks (Aggr_Lo)), | |
70482933 RK |
3738 | |
3739 | Right_Opnd => | |
3740 | Make_Op_Gt (Loc, | |
fbf5a39b AC |
3741 | Left_Opnd => |
3742 | Duplicate_Subexpr (Choices_Hi), | |
3743 | Right_Opnd => | |
3744 | Duplicate_Subexpr (Aggr_Hi))); | |
70482933 RK |
3745 | end if; |
3746 | ||
3747 | if Present (Cond) then | |
3748 | Insert_Action (N, | |
07fc65c4 GB |
3749 | Make_Raise_Constraint_Error (Loc, |
3750 | Condition => Cond, | |
3751 | Reason => CE_Length_Check_Failed)); | |
70482933 RK |
3752 | end if; |
3753 | ||
3754 | -- Now look inside the sub-aggregate to see if there is more work | |
3755 | ||
3756 | if Dim < Aggr_Dimension then | |
3757 | ||
3758 | -- Process positional components | |
3759 | ||
3760 | if Present (Expressions (Sub_Aggr)) then | |
3761 | Expr := First (Expressions (Sub_Aggr)); | |
3762 | while Present (Expr) loop | |
3763 | Others_Check (Expr, Dim + 1); | |
3764 | Next (Expr); | |
3765 | end loop; | |
3766 | end if; | |
3767 | ||
3768 | -- Process component associations | |
3769 | ||
3770 | if Present (Component_Associations (Sub_Aggr)) then | |
3771 | Assoc := First (Component_Associations (Sub_Aggr)); | |
3772 | while Present (Assoc) loop | |
3773 | Expr := Expression (Assoc); | |
3774 | Others_Check (Expr, Dim + 1); | |
3775 | Next (Assoc); | |
3776 | end loop; | |
3777 | end if; | |
3778 | end if; | |
3779 | end Others_Check; | |
3780 | ||
3781 | -- Remaining Expand_Array_Aggregate variables | |
3782 | ||
3783 | Tmp : Entity_Id; | |
fbf5a39b | 3784 | -- Holds the temporary aggregate value |
70482933 RK |
3785 | |
3786 | Tmp_Decl : Node_Id; | |
fbf5a39b | 3787 | -- Holds the declaration of Tmp |
70482933 RK |
3788 | |
3789 | Aggr_Code : List_Id; | |
3790 | Parent_Node : Node_Id; | |
3791 | Parent_Kind : Node_Kind; | |
3792 | ||
3793 | -- Start of processing for Expand_Array_Aggregate | |
3794 | ||
3795 | begin | |
3796 | -- Do not touch the special aggregates of attributes used for Asm calls | |
3797 | ||
3798 | if Is_RTE (Ctyp, RE_Asm_Input_Operand) | |
3799 | or else Is_RTE (Ctyp, RE_Asm_Output_Operand) | |
3800 | then | |
3801 | return; | |
3802 | end if; | |
3803 | ||
07fc65c4 GB |
3804 | -- If the semantic analyzer has determined that aggregate N will raise |
3805 | -- Constraint_Error at run-time, then the aggregate node has been | |
3806 | -- replaced with an N_Raise_Constraint_Error node and we should | |
3807 | -- never get here. | |
70482933 RK |
3808 | |
3809 | pragma Assert (not Raises_Constraint_Error (N)); | |
3810 | ||
fbf5a39b AC |
3811 | -- STEP 1a. |
3812 | ||
3813 | -- Check that the index range defined by aggregate bounds is | |
3814 | -- compatible with corresponding index subtype. | |
70482933 RK |
3815 | |
3816 | Index_Compatibility_Check : declare | |
3817 | Aggr_Index_Range : Node_Id := First_Index (Typ); | |
3818 | -- The current aggregate index range | |
3819 | ||
3820 | Index_Constraint : Node_Id := First_Index (Etype (Typ)); | |
3821 | -- The corresponding index constraint against which we have to | |
3822 | -- check the above aggregate index range. | |
3823 | ||
3824 | begin | |
3825 | Compute_Others_Present (N, 1); | |
3826 | ||
3827 | for J in 1 .. Aggr_Dimension loop | |
3828 | -- There is no need to emit a check if an others choice is | |
3829 | -- present for this array aggregate dimension since in this | |
3830 | -- case one of N's sub-aggregates has taken its bounds from the | |
3831 | -- context and these bounds must have been checked already. In | |
3832 | -- addition all sub-aggregates corresponding to the same | |
3833 | -- dimension must all have the same bounds (checked in (c) below). | |
3834 | ||
3835 | if not Range_Checks_Suppressed (Etype (Index_Constraint)) | |
3836 | and then not Others_Present (J) | |
3837 | then | |
3838 | -- We don't use Checks.Apply_Range_Check here because it | |
3839 | -- emits a spurious check. Namely it checks that the range | |
3840 | -- defined by the aggregate bounds is non empty. But we know | |
3841 | -- this already if we get here. | |
3842 | ||
3843 | Check_Bounds (Aggr_Index_Range, Index_Constraint); | |
3844 | end if; | |
3845 | ||
3846 | -- Save the low and high bounds of the aggregate index as well | |
3847 | -- as the index type for later use in checks (b) and (c) below. | |
3848 | ||
3849 | Aggr_Low (J) := Low_Bound (Aggr_Index_Range); | |
3850 | Aggr_High (J) := High_Bound (Aggr_Index_Range); | |
3851 | ||
3852 | Aggr_Index_Typ (J) := Etype (Index_Constraint); | |
3853 | ||
3854 | Next_Index (Aggr_Index_Range); | |
3855 | Next_Index (Index_Constraint); | |
3856 | end loop; | |
3857 | end Index_Compatibility_Check; | |
3858 | ||
fbf5a39b AC |
3859 | -- STEP 1b. |
3860 | ||
3861 | -- If an others choice is present check that no aggregate | |
3862 | -- index is outside the bounds of the index constraint. | |
70482933 RK |
3863 | |
3864 | Others_Check (N, 1); | |
3865 | ||
fbf5a39b AC |
3866 | -- STEP 1c. |
3867 | ||
3868 | -- For multidimensional arrays make sure that all subaggregates | |
3869 | -- corresponding to the same dimension have the same bounds. | |
70482933 RK |
3870 | |
3871 | if Aggr_Dimension > 1 then | |
3872 | Check_Same_Aggr_Bounds (N, 1); | |
3873 | end if; | |
3874 | ||
3875 | -- STEP 2. | |
3876 | ||
fbf5a39b AC |
3877 | -- Here we test for is packed array aggregate that we can handle |
3878 | -- at compile time. If so, return with transformation done. Note | |
3879 | -- that we do this even if the aggregate is nested, because once | |
3880 | -- we have done this processing, there is no more nested aggregate! | |
3881 | ||
3882 | if Packed_Array_Aggregate_Handled (N) then | |
3883 | return; | |
3884 | end if; | |
3885 | ||
3886 | -- At this point we try to convert to positional form | |
70482933 RK |
3887 | |
3888 | Convert_To_Positional (N); | |
3889 | ||
fbf5a39b AC |
3890 | -- if the result is no longer an aggregate (e.g. it may be a string |
3891 | -- literal, or a temporary which has the needed value), then we are | |
3892 | -- done, since there is no longer a nested aggregate. | |
3893 | ||
70482933 RK |
3894 | if Nkind (N) /= N_Aggregate then |
3895 | return; | |
3896 | ||
fbf5a39b AC |
3897 | -- We are also done if the result is an analyzed aggregate |
3898 | -- This case could use more comments ??? | |
3899 | ||
70482933 RK |
3900 | elsif Analyzed (N) |
3901 | and then N /= Original_Node (N) | |
3902 | then | |
3903 | return; | |
3904 | end if; | |
3905 | ||
fbf5a39b AC |
3906 | -- Now see if back end processing is possible |
3907 | ||
70482933 RK |
3908 | if Backend_Processing_Possible (N) then |
3909 | ||
3910 | -- If the aggregate is static but the constraints are not, build | |
3911 | -- a static subtype for the aggregate, so that Gigi can place it | |
3912 | -- in static memory. Perform an unchecked_conversion to the non- | |
3913 | -- static type imposed by the context. | |
3914 | ||
3915 | declare | |
3916 | Itype : constant Entity_Id := Etype (N); | |
3917 | Index : Node_Id; | |
3918 | Needs_Type : Boolean := False; | |
3919 | ||
3920 | begin | |
3921 | Index := First_Index (Itype); | |
3922 | ||
3923 | while Present (Index) loop | |
3924 | if not Is_Static_Subtype (Etype (Index)) then | |
3925 | Needs_Type := True; | |
3926 | exit; | |
3927 | else | |
3928 | Next_Index (Index); | |
3929 | end if; | |
3930 | end loop; | |
3931 | ||
3932 | if Needs_Type then | |
3933 | Build_Constrained_Type (Positional => True); | |
3934 | Rewrite (N, Unchecked_Convert_To (Itype, N)); | |
3935 | Analyze (N); | |
3936 | end if; | |
3937 | end; | |
3938 | ||
3939 | return; | |
3940 | end if; | |
3941 | ||
fbf5a39b AC |
3942 | -- STEP 3. |
3943 | ||
70482933 RK |
3944 | -- Delay expansion for nested aggregates it will be taken care of |
3945 | -- when the parent aggregate is expanded | |
3946 | ||
3947 | Parent_Node := Parent (N); | |
3948 | Parent_Kind := Nkind (Parent_Node); | |
3949 | ||
3950 | if Parent_Kind = N_Qualified_Expression then | |
3951 | Parent_Node := Parent (Parent_Node); | |
3952 | Parent_Kind := Nkind (Parent_Node); | |
3953 | end if; | |
3954 | ||
3955 | if Parent_Kind = N_Aggregate | |
3956 | or else Parent_Kind = N_Extension_Aggregate | |
3957 | or else Parent_Kind = N_Component_Association | |
3958 | or else (Parent_Kind = N_Object_Declaration | |
3959 | and then Controlled_Type (Typ)) | |
3960 | or else (Parent_Kind = N_Assignment_Statement | |
3961 | and then Inside_Init_Proc) | |
3962 | then | |
3963 | Set_Expansion_Delayed (N); | |
3964 | return; | |
3965 | end if; | |
3966 | ||
fbf5a39b | 3967 | -- STEP 4. |
70482933 RK |
3968 | |
3969 | -- Look if in place aggregate expansion is possible | |
3970 | ||
3971 | -- For object declarations we build the aggregate in place, unless | |
3972 | -- the array is bit-packed or the component is controlled. | |
3973 | ||
3974 | -- For assignments we do the assignment in place if all the component | |
3975 | -- associations have compile-time known values. For other cases we | |
3976 | -- create a temporary. The analysis for safety of on-line assignment | |
3977 | -- is delicate, i.e. we don't know how to do it fully yet ??? | |
3978 | ||
3979 | if Requires_Transient_Scope (Typ) then | |
3980 | Establish_Transient_Scope | |
3981 | (N, Sec_Stack => Has_Controlled_Component (Typ)); | |
3982 | end if; | |
3983 | ||
c45b6ae0 AC |
3984 | if Has_Default_Init_Comps (N) then |
3985 | Maybe_In_Place_OK := False; | |
3986 | else | |
3987 | Maybe_In_Place_OK := | |
3988 | Comes_From_Source (N) | |
3989 | and then Nkind (Parent (N)) = N_Assignment_Statement | |
3990 | and then not Is_Bit_Packed_Array (Typ) | |
3991 | and then not Has_Controlled_Component (Typ) | |
3992 | and then In_Place_Assign_OK; | |
3993 | end if; | |
70482933 | 3994 | |
c45b6ae0 AC |
3995 | if not Has_Default_Init_Comps (N) |
3996 | and then Comes_From_Source (Parent (N)) | |
70482933 | 3997 | and then Nkind (Parent (N)) = N_Object_Declaration |
fbf5a39b | 3998 | and then not Must_Slide (N, Typ) |
70482933 RK |
3999 | and then N = Expression (Parent (N)) |
4000 | and then not Is_Bit_Packed_Array (Typ) | |
4001 | and then not Has_Controlled_Component (Typ) | |
4002 | and then not Has_Address_Clause (Parent (N)) | |
4003 | then | |
70482933 RK |
4004 | Tmp := Defining_Identifier (Parent (N)); |
4005 | Set_No_Initialization (Parent (N)); | |
4006 | Set_Expression (Parent (N), Empty); | |
4007 | ||
4008 | -- Set the type of the entity, for use in the analysis of the | |
4009 | -- subsequent indexed assignments. If the nominal type is not | |
4010 | -- constrained, build a subtype from the known bounds of the | |
4011 | -- aggregate. If the declaration has a subtype mark, use it, | |
4012 | -- otherwise use the itype of the aggregate. | |
4013 | ||
4014 | if not Is_Constrained (Typ) then | |
4015 | Build_Constrained_Type (Positional => False); | |
4016 | elsif Is_Entity_Name (Object_Definition (Parent (N))) | |
4017 | and then Is_Constrained (Entity (Object_Definition (Parent (N)))) | |
4018 | then | |
4019 | Set_Etype (Tmp, Entity (Object_Definition (Parent (N)))); | |
4020 | else | |
4021 | Set_Size_Known_At_Compile_Time (Typ, False); | |
4022 | Set_Etype (Tmp, Typ); | |
4023 | end if; | |
4024 | ||
4025 | elsif Maybe_In_Place_OK | |
4026 | and then Is_Entity_Name (Name (Parent (N))) | |
4027 | then | |
4028 | Tmp := Entity (Name (Parent (N))); | |
4029 | ||
4030 | if Etype (Tmp) /= Etype (N) then | |
4031 | Apply_Length_Check (N, Etype (Tmp)); | |
fbf5a39b AC |
4032 | |
4033 | if Nkind (N) = N_Raise_Constraint_Error then | |
4034 | ||
4035 | -- Static error, nothing further to expand | |
4036 | ||
4037 | return; | |
4038 | end if; | |
70482933 RK |
4039 | end if; |
4040 | ||
07fc65c4 GB |
4041 | elsif Maybe_In_Place_OK |
4042 | and then Nkind (Name (Parent (N))) = N_Explicit_Dereference | |
4043 | and then Is_Entity_Name (Prefix (Name (Parent (N)))) | |
4044 | then | |
4045 | Tmp := Name (Parent (N)); | |
4046 | ||
4047 | if Etype (Tmp) /= Etype (N) then | |
4048 | Apply_Length_Check (N, Etype (Tmp)); | |
4049 | end if; | |
4050 | ||
70482933 RK |
4051 | elsif Maybe_In_Place_OK |
4052 | and then Nkind (Name (Parent (N))) = N_Slice | |
07fc65c4 | 4053 | and then Safe_Slice_Assignment (N) |
70482933 | 4054 | then |
07fc65c4 | 4055 | -- Safe_Slice_Assignment rewrites assignment as a loop |
70482933 RK |
4056 | |
4057 | return; | |
4058 | ||
fbf5a39b AC |
4059 | -- Step 5 |
4060 | ||
4061 | -- In place aggregate expansion is not possible | |
4062 | ||
70482933 | 4063 | else |
07fc65c4 | 4064 | Maybe_In_Place_OK := False; |
70482933 RK |
4065 | Tmp := Make_Defining_Identifier (Loc, New_Internal_Name ('A')); |
4066 | Tmp_Decl := | |
4067 | Make_Object_Declaration | |
4068 | (Loc, | |
4069 | Defining_Identifier => Tmp, | |
4070 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
4071 | Set_No_Initialization (Tmp_Decl, True); | |
4072 | ||
4073 | -- If we are within a loop, the temporary will be pushed on the | |
4074 | -- stack at each iteration. If the aggregate is the expression for | |
4075 | -- an allocator, it will be immediately copied to the heap and can | |
4076 | -- be reclaimed at once. We create a transient scope around the | |
4077 | -- aggregate for this purpose. | |
4078 | ||
4079 | if Ekind (Current_Scope) = E_Loop | |
4080 | and then Nkind (Parent (Parent (N))) = N_Allocator | |
4081 | then | |
4082 | Establish_Transient_Scope (N, False); | |
4083 | end if; | |
4084 | ||
4085 | Insert_Action (N, Tmp_Decl); | |
4086 | end if; | |
4087 | ||
4088 | -- Construct and insert the aggregate code. We can safely suppress | |
4089 | -- index checks because this code is guaranteed not to raise CE | |
4090 | -- on index checks. However we should *not* suppress all checks. | |
4091 | ||
07fc65c4 GB |
4092 | declare |
4093 | Target : Node_Id; | |
4094 | ||
4095 | begin | |
4096 | if Nkind (Tmp) = N_Defining_Identifier then | |
4097 | Target := New_Reference_To (Tmp, Loc); | |
4098 | ||
4099 | else | |
c45b6ae0 AC |
4100 | |
4101 | if Has_Default_Init_Comps (N) then | |
4102 | ||
4103 | -- Ada0Y (AI-287): This case has not been analyzed??? | |
4104 | ||
4105 | pragma Assert (False); | |
4106 | null; | |
4107 | end if; | |
4108 | ||
07fc65c4 GB |
4109 | -- Name in assignment is explicit dereference. |
4110 | ||
4111 | Target := New_Copy (Tmp); | |
4112 | end if; | |
4113 | ||
4114 | Aggr_Code := | |
4115 | Build_Array_Aggr_Code (N, | |
c45b6ae0 | 4116 | Ctype => Ctyp, |
07fc65c4 GB |
4117 | Index => First_Index (Typ), |
4118 | Into => Target, | |
4119 | Scalar_Comp => Is_Scalar_Type (Ctyp)); | |
4120 | end; | |
70482933 RK |
4121 | |
4122 | if Comes_From_Source (Tmp) then | |
4123 | Insert_Actions_After (Parent (N), Aggr_Code); | |
4124 | ||
4125 | else | |
4126 | Insert_Actions (N, Aggr_Code); | |
4127 | end if; | |
4128 | ||
07fc65c4 GB |
4129 | -- If the aggregate has been assigned in place, remove the original |
4130 | -- assignment. | |
4131 | ||
70482933 | 4132 | if Nkind (Parent (N)) = N_Assignment_Statement |
07fc65c4 | 4133 | and then Maybe_In_Place_OK |
70482933 RK |
4134 | then |
4135 | Rewrite (Parent (N), Make_Null_Statement (Loc)); | |
70482933 RK |
4136 | |
4137 | elsif Nkind (Parent (N)) /= N_Object_Declaration | |
4138 | or else Tmp /= Defining_Identifier (Parent (N)) | |
4139 | then | |
4140 | Rewrite (N, New_Occurrence_Of (Tmp, Loc)); | |
4141 | Analyze_And_Resolve (N, Typ); | |
4142 | end if; | |
4143 | end Expand_Array_Aggregate; | |
4144 | ||
4145 | ------------------------ | |
4146 | -- Expand_N_Aggregate -- | |
4147 | ------------------------ | |
4148 | ||
4149 | procedure Expand_N_Aggregate (N : Node_Id) is | |
4150 | begin | |
4151 | if Is_Record_Type (Etype (N)) then | |
4152 | Expand_Record_Aggregate (N); | |
4153 | else | |
4154 | Expand_Array_Aggregate (N); | |
4155 | end if; | |
fbf5a39b AC |
4156 | |
4157 | exception | |
4158 | when RE_Not_Available => | |
4159 | return; | |
70482933 RK |
4160 | end Expand_N_Aggregate; |
4161 | ||
4162 | ---------------------------------- | |
4163 | -- Expand_N_Extension_Aggregate -- | |
4164 | ---------------------------------- | |
4165 | ||
4166 | -- If the ancestor part is an expression, add a component association for | |
4167 | -- the parent field. If the type of the ancestor part is not the direct | |
4168 | -- parent of the expected type, build recursively the needed ancestors. | |
4169 | -- If the ancestor part is a subtype_mark, replace aggregate with a decla- | |
4170 | -- ration for a temporary of the expected type, followed by individual | |
4171 | -- assignments to the given components. | |
4172 | ||
4173 | procedure Expand_N_Extension_Aggregate (N : Node_Id) is | |
4174 | Loc : constant Source_Ptr := Sloc (N); | |
4175 | A : constant Node_Id := Ancestor_Part (N); | |
4176 | Typ : constant Entity_Id := Etype (N); | |
4177 | ||
4178 | begin | |
fbf5a39b | 4179 | -- If the ancestor is a subtype mark, an init proc must be called |
70482933 RK |
4180 | -- on the resulting object which thus has to be materialized in |
4181 | -- the front-end | |
4182 | ||
4183 | if Is_Entity_Name (A) and then Is_Type (Entity (A)) then | |
4184 | Convert_To_Assignments (N, Typ); | |
4185 | ||
4186 | -- The extension aggregate is transformed into a record aggregate | |
4187 | -- of the following form (c1 and c2 are inherited components) | |
4188 | ||
4189 | -- (Exp with c3 => a, c4 => b) | |
4190 | -- ==> (c1 => Exp.c1, c2 => Exp.c2, c1 => a, c2 => b) | |
4191 | ||
4192 | else | |
4193 | Set_Etype (N, Typ); | |
4194 | ||
4195 | -- No tag is needed in the case of Java_VM | |
4196 | ||
4197 | if Java_VM then | |
4198 | Expand_Record_Aggregate (N, | |
4199 | Parent_Expr => A); | |
4200 | else | |
4201 | Expand_Record_Aggregate (N, | |
4202 | Orig_Tag => New_Occurrence_Of (Access_Disp_Table (Typ), Loc), | |
4203 | Parent_Expr => A); | |
4204 | end if; | |
4205 | end if; | |
fbf5a39b AC |
4206 | |
4207 | exception | |
4208 | when RE_Not_Available => | |
4209 | return; | |
70482933 RK |
4210 | end Expand_N_Extension_Aggregate; |
4211 | ||
4212 | ----------------------------- | |
4213 | -- Expand_Record_Aggregate -- | |
4214 | ----------------------------- | |
4215 | ||
4216 | procedure Expand_Record_Aggregate | |
4217 | (N : Node_Id; | |
4218 | Orig_Tag : Node_Id := Empty; | |
4219 | Parent_Expr : Node_Id := Empty) | |
4220 | is | |
fbf5a39b AC |
4221 | Loc : constant Source_Ptr := Sloc (N); |
4222 | Comps : constant List_Id := Component_Associations (N); | |
4223 | Typ : constant Entity_Id := Etype (N); | |
4224 | Base_Typ : constant Entity_Id := Base_Type (Typ); | |
70482933 RK |
4225 | |
4226 | function Has_Delayed_Nested_Aggregate_Or_Tagged_Comps return Boolean; | |
4227 | -- Checks the presence of a nested aggregate which needs Late_Expansion | |
4228 | -- or the presence of tagged components which may need tag adjustment. | |
4229 | ||
4230 | -------------------------------------------------- | |
4231 | -- Has_Delayed_Nested_Aggregate_Or_Tagged_Comps -- | |
4232 | -------------------------------------------------- | |
4233 | ||
4234 | function Has_Delayed_Nested_Aggregate_Or_Tagged_Comps return Boolean is | |
fbf5a39b | 4235 | C : Node_Id; |
70482933 RK |
4236 | Expr_Q : Node_Id; |
4237 | ||
4238 | begin | |
4239 | if No (Comps) then | |
4240 | return False; | |
4241 | end if; | |
4242 | ||
4243 | C := First (Comps); | |
4244 | while Present (C) loop | |
70482933 RK |
4245 | if Nkind (Expression (C)) = N_Qualified_Expression then |
4246 | Expr_Q := Expression (Expression (C)); | |
4247 | else | |
4248 | Expr_Q := Expression (C); | |
4249 | end if; | |
4250 | ||
4251 | -- Return true if the aggregate has any associations for | |
4252 | -- tagged components that may require tag adjustment. | |
4253 | -- These are cases where the source expression may have | |
4254 | -- a tag that could differ from the component tag (e.g., | |
4255 | -- can occur for type conversions and formal parameters). | |
4256 | -- (Tag adjustment is not needed if Java_VM because object | |
4257 | -- tags are implicit in the JVM.) | |
4258 | ||
4259 | if Is_Tagged_Type (Etype (Expr_Q)) | |
4260 | and then (Nkind (Expr_Q) = N_Type_Conversion | |
4261 | or else (Is_Entity_Name (Expr_Q) | |
4262 | and then Ekind (Entity (Expr_Q)) in Formal_Kind)) | |
4263 | and then not Java_VM | |
4264 | then | |
4265 | return True; | |
4266 | end if; | |
4267 | ||
4268 | if Is_Delayed_Aggregate (Expr_Q) then | |
4269 | return True; | |
4270 | end if; | |
4271 | ||
4272 | Next (C); | |
4273 | end loop; | |
4274 | ||
4275 | return False; | |
fbf5a39b | 4276 | end Has_Delayed_Nested_Aggregate_Or_Tagged_Comps; |
70482933 RK |
4277 | |
4278 | -- Remaining Expand_Record_Aggregate variables | |
4279 | ||
4280 | Tag_Value : Node_Id; | |
4281 | Comp : Entity_Id; | |
4282 | New_Comp : Node_Id; | |
4283 | ||
4284 | -- Start of processing for Expand_Record_Aggregate | |
4285 | ||
4286 | begin | |
fbf5a39b AC |
4287 | -- If the aggregate is to be assigned to an atomic variable, we |
4288 | -- have to prevent a piecemeal assignment even if the aggregate | |
4289 | -- is to be expanded. We create a temporary for the aggregate, and | |
4290 | -- assign the temporary instead, so that the back end can generate | |
4291 | -- an atomic move for it. | |
4292 | ||
4293 | if Is_Atomic (Typ) | |
4294 | and then (Nkind (Parent (N)) = N_Object_Declaration | |
4295 | or else Nkind (Parent (N)) = N_Assignment_Statement) | |
4296 | and then Comes_From_Source (Parent (N)) | |
4297 | then | |
4298 | Expand_Atomic_Aggregate (N, Typ); | |
4299 | return; | |
4300 | end if; | |
4301 | ||
70482933 RK |
4302 | -- Gigi doesn't handle properly temporaries of variable size |
4303 | -- so we generate it in the front-end | |
4304 | ||
4305 | if not Size_Known_At_Compile_Time (Typ) then | |
4306 | Convert_To_Assignments (N, Typ); | |
4307 | ||
4308 | -- Temporaries for controlled aggregates need to be attached to a | |
4309 | -- final chain in order to be properly finalized, so it has to | |
4310 | -- be created in the front-end | |
4311 | ||
4312 | elsif Is_Controlled (Typ) | |
4313 | or else Has_Controlled_Component (Base_Type (Typ)) | |
4314 | then | |
4315 | Convert_To_Assignments (N, Typ); | |
4316 | ||
19f0526a AC |
4317 | -- Ada0Y (AI-287): In case of default initialized components we convert |
4318 | -- the aggregate into assignments. | |
4319 | ||
65356e64 AC |
4320 | elsif Has_Default_Init_Comps (N) then |
4321 | Convert_To_Assignments (N, Typ); | |
4322 | ||
70482933 RK |
4323 | elsif Has_Delayed_Nested_Aggregate_Or_Tagged_Comps then |
4324 | Convert_To_Assignments (N, Typ); | |
4325 | ||
4326 | -- If an ancestor is private, some components are not inherited and | |
4327 | -- we cannot expand into a record aggregate | |
4328 | ||
4329 | elsif Has_Private_Ancestor (Typ) then | |
4330 | Convert_To_Assignments (N, Typ); | |
4331 | ||
4332 | -- ??? The following was done to compile fxacc00.ads in the ACVCs. Gigi | |
4333 | -- is not able to handle the aggregate for Late_Request. | |
4334 | ||
4335 | elsif Is_Tagged_Type (Typ) and then Has_Discriminants (Typ) then | |
4336 | Convert_To_Assignments (N, Typ); | |
4337 | ||
fbf5a39b AC |
4338 | -- If some components are mutable, the size of the aggregate component |
4339 | -- may be disctinct from the default size of the type component, so | |
4340 | -- we need to expand to insure that the back-end copies the proper | |
4341 | -- size of the data. | |
4342 | ||
4343 | elsif Has_Mutable_Components (Typ) then | |
4344 | Convert_To_Assignments (N, Typ); | |
4345 | ||
70482933 RK |
4346 | -- In all other cases we generate a proper aggregate that |
4347 | -- can be handled by gigi. | |
4348 | ||
4349 | else | |
07fc65c4 | 4350 | -- If no discriminants, nothing special to do |
70482933 | 4351 | |
07fc65c4 | 4352 | if not Has_Discriminants (Typ) then |
70482933 RK |
4353 | null; |
4354 | ||
07fc65c4 GB |
4355 | -- Case of discriminants present |
4356 | ||
70482933 RK |
4357 | elsif Is_Derived_Type (Typ) then |
4358 | ||
fbf5a39b AC |
4359 | -- For untagged types, non-stored discriminants are replaced |
4360 | -- with stored discriminants, which are the ones that gigi uses | |
07fc65c4 | 4361 | -- to describe the type and its components. |
70482933 | 4362 | |
07fc65c4 | 4363 | Generate_Aggregate_For_Derived_Type : declare |
fbf5a39b | 4364 | Constraints : constant List_Id := New_List; |
70482933 RK |
4365 | First_Comp : Node_Id; |
4366 | Discriminant : Entity_Id; | |
07fc65c4 GB |
4367 | Decl : Node_Id; |
4368 | Num_Disc : Int := 0; | |
4369 | Num_Gird : Int := 0; | |
4370 | ||
fbf5a39b AC |
4371 | procedure Prepend_Stored_Values (T : Entity_Id); |
4372 | -- Scan the list of stored discriminants of the type, and | |
07fc65c4 GB |
4373 | -- add their values to the aggregate being built. |
4374 | ||
4375 | --------------------------- | |
fbf5a39b | 4376 | -- Prepend_Stored_Values -- |
07fc65c4 GB |
4377 | --------------------------- |
4378 | ||
fbf5a39b | 4379 | procedure Prepend_Stored_Values (T : Entity_Id) is |
07fc65c4 | 4380 | begin |
fbf5a39b | 4381 | Discriminant := First_Stored_Discriminant (T); |
07fc65c4 GB |
4382 | |
4383 | while Present (Discriminant) loop | |
4384 | New_Comp := | |
4385 | Make_Component_Association (Loc, | |
4386 | Choices => | |
4387 | New_List (New_Occurrence_Of (Discriminant, Loc)), | |
4388 | ||
4389 | Expression => | |
4390 | New_Copy_Tree ( | |
4391 | Get_Discriminant_Value ( | |
4392 | Discriminant, | |
4393 | Typ, | |
4394 | Discriminant_Constraint (Typ)))); | |
4395 | ||
4396 | if No (First_Comp) then | |
4397 | Prepend_To (Component_Associations (N), New_Comp); | |
4398 | else | |
4399 | Insert_After (First_Comp, New_Comp); | |
4400 | end if; | |
4401 | ||
4402 | First_Comp := New_Comp; | |
fbf5a39b | 4403 | Next_Stored_Discriminant (Discriminant); |
07fc65c4 | 4404 | end loop; |
fbf5a39b | 4405 | end Prepend_Stored_Values; |
07fc65c4 GB |
4406 | |
4407 | -- Start of processing for Generate_Aggregate_For_Derived_Type | |
70482933 RK |
4408 | |
4409 | begin | |
07fc65c4 GB |
4410 | -- Remove the associations for the discriminant of |
4411 | -- the derived type. | |
70482933 RK |
4412 | |
4413 | First_Comp := First (Component_Associations (N)); | |
4414 | ||
4415 | while Present (First_Comp) loop | |
4416 | Comp := First_Comp; | |
4417 | Next (First_Comp); | |
4418 | ||
4419 | if Ekind (Entity (First (Choices (Comp)))) = | |
4420 | E_Discriminant | |
4421 | then | |
4422 | Remove (Comp); | |
07fc65c4 | 4423 | Num_Disc := Num_Disc + 1; |
70482933 RK |
4424 | end if; |
4425 | end loop; | |
4426 | ||
fbf5a39b AC |
4427 | -- Insert stored discriminant associations in the correct |
4428 | -- order. If there are more stored discriminants than new | |
07fc65c4 | 4429 | -- discriminants, there is at least one new discriminant |
fbf5a39b AC |
4430 | -- that constrains more than one of the stored discriminants. |
4431 | -- In this case we need to construct a proper subtype of | |
4432 | -- the parent type, in order to supply values to all the | |
4433 | -- components. Otherwise there is one-one correspondence | |
4434 | -- between the constraints and the stored discriminants. | |
70482933 RK |
4435 | |
4436 | First_Comp := Empty; | |
70482933 | 4437 | |
fbf5a39b | 4438 | Discriminant := First_Stored_Discriminant (Base_Type (Typ)); |
70482933 | 4439 | |
07fc65c4 GB |
4440 | while Present (Discriminant) loop |
4441 | Num_Gird := Num_Gird + 1; | |
fbf5a39b | 4442 | Next_Stored_Discriminant (Discriminant); |
70482933 | 4443 | end loop; |
07fc65c4 | 4444 | |
fbf5a39b | 4445 | -- Case of more stored discriminants than new discriminants |
07fc65c4 GB |
4446 | |
4447 | if Num_Gird > Num_Disc then | |
4448 | ||
4449 | -- Create a proper subtype of the parent type, which is | |
4450 | -- the proper implementation type for the aggregate, and | |
4451 | -- convert it to the intended target type. | |
4452 | ||
fbf5a39b | 4453 | Discriminant := First_Stored_Discriminant (Base_Type (Typ)); |
07fc65c4 GB |
4454 | |
4455 | while Present (Discriminant) loop | |
4456 | New_Comp := | |
4457 | New_Copy_Tree ( | |
4458 | Get_Discriminant_Value ( | |
4459 | Discriminant, | |
4460 | Typ, | |
4461 | Discriminant_Constraint (Typ))); | |
4462 | Append (New_Comp, Constraints); | |
fbf5a39b | 4463 | Next_Stored_Discriminant (Discriminant); |
07fc65c4 GB |
4464 | end loop; |
4465 | ||
4466 | Decl := | |
4467 | Make_Subtype_Declaration (Loc, | |
4468 | Defining_Identifier => | |
4469 | Make_Defining_Identifier (Loc, | |
4470 | New_Internal_Name ('T')), | |
4471 | Subtype_Indication => | |
4472 | Make_Subtype_Indication (Loc, | |
4473 | Subtype_Mark => | |
4474 | New_Occurrence_Of (Etype (Base_Type (Typ)), Loc), | |
4475 | Constraint => | |
4476 | Make_Index_Or_Discriminant_Constraint | |
4477 | (Loc, Constraints))); | |
4478 | ||
4479 | Insert_Action (N, Decl); | |
fbf5a39b | 4480 | Prepend_Stored_Values (Base_Type (Typ)); |
07fc65c4 GB |
4481 | |
4482 | Set_Etype (N, Defining_Identifier (Decl)); | |
4483 | Set_Analyzed (N); | |
4484 | ||
4485 | Rewrite (N, Unchecked_Convert_To (Typ, N)); | |
4486 | Analyze (N); | |
4487 | ||
4488 | -- Case where we do not have fewer new discriminants than | |
fbf5a39b AC |
4489 | -- stored discriminants, so in this case we can simply |
4490 | -- use the stored discriminants of the subtype. | |
07fc65c4 GB |
4491 | |
4492 | else | |
fbf5a39b | 4493 | Prepend_Stored_Values (Typ); |
07fc65c4 GB |
4494 | end if; |
4495 | end Generate_Aggregate_For_Derived_Type; | |
70482933 RK |
4496 | end if; |
4497 | ||
4498 | if Is_Tagged_Type (Typ) then | |
4499 | ||
4500 | -- The tagged case, _parent and _tag component must be created. | |
4501 | ||
4502 | -- Reset null_present unconditionally. tagged records always have | |
4503 | -- at least one field (the tag or the parent) | |
4504 | ||
4505 | Set_Null_Record_Present (N, False); | |
4506 | ||
4507 | -- When the current aggregate comes from the expansion of an | |
4508 | -- extension aggregate, the parent expr is replaced by an | |
4509 | -- aggregate formed by selected components of this expr | |
4510 | ||
4511 | if Present (Parent_Expr) | |
4512 | and then Is_Empty_List (Comps) | |
4513 | then | |
4514 | Comp := First_Entity (Typ); | |
4515 | while Present (Comp) loop | |
4516 | ||
4517 | -- Skip all entities that aren't discriminants or components | |
4518 | ||
4519 | if Ekind (Comp) /= E_Discriminant | |
4520 | and then Ekind (Comp) /= E_Component | |
4521 | then | |
4522 | null; | |
4523 | ||
4524 | -- Skip all expander-generated components | |
4525 | ||
4526 | elsif | |
4527 | not Comes_From_Source (Original_Record_Component (Comp)) | |
4528 | then | |
4529 | null; | |
4530 | ||
4531 | else | |
4532 | New_Comp := | |
4533 | Make_Selected_Component (Loc, | |
4534 | Prefix => | |
4535 | Unchecked_Convert_To (Typ, | |
4536 | Duplicate_Subexpr (Parent_Expr, True)), | |
4537 | ||
4538 | Selector_Name => New_Occurrence_Of (Comp, Loc)); | |
4539 | ||
4540 | Append_To (Comps, | |
4541 | Make_Component_Association (Loc, | |
4542 | Choices => | |
4543 | New_List (New_Occurrence_Of (Comp, Loc)), | |
4544 | Expression => | |
4545 | New_Comp)); | |
4546 | ||
4547 | Analyze_And_Resolve (New_Comp, Etype (Comp)); | |
4548 | end if; | |
4549 | ||
4550 | Next_Entity (Comp); | |
4551 | end loop; | |
4552 | end if; | |
4553 | ||
4554 | -- Compute the value for the Tag now, if the type is a root it | |
4555 | -- will be included in the aggregate right away, otherwise it will | |
4556 | -- be propagated to the parent aggregate | |
4557 | ||
4558 | if Present (Orig_Tag) then | |
4559 | Tag_Value := Orig_Tag; | |
4560 | elsif Java_VM then | |
4561 | Tag_Value := Empty; | |
4562 | else | |
4563 | Tag_Value := New_Occurrence_Of (Access_Disp_Table (Typ), Loc); | |
4564 | end if; | |
4565 | ||
4566 | -- For a derived type, an aggregate for the parent is formed with | |
4567 | -- all the inherited components. | |
4568 | ||
4569 | if Is_Derived_Type (Typ) then | |
4570 | ||
4571 | declare | |
4572 | First_Comp : Node_Id; | |
4573 | Parent_Comps : List_Id; | |
4574 | Parent_Aggr : Node_Id; | |
4575 | Parent_Name : Node_Id; | |
4576 | ||
4577 | begin | |
4578 | -- Remove the inherited component association from the | |
4579 | -- aggregate and store them in the parent aggregate | |
4580 | ||
4581 | First_Comp := First (Component_Associations (N)); | |
4582 | Parent_Comps := New_List; | |
4583 | ||
4584 | while Present (First_Comp) | |
4585 | and then Scope (Original_Record_Component ( | |
4586 | Entity (First (Choices (First_Comp))))) /= Base_Typ | |
4587 | loop | |
4588 | Comp := First_Comp; | |
4589 | Next (First_Comp); | |
4590 | Remove (Comp); | |
4591 | Append (Comp, Parent_Comps); | |
4592 | end loop; | |
4593 | ||
4594 | Parent_Aggr := Make_Aggregate (Loc, | |
4595 | Component_Associations => Parent_Comps); | |
4596 | Set_Etype (Parent_Aggr, Etype (Base_Type (Typ))); | |
4597 | ||
4598 | -- Find the _parent component | |
4599 | ||
4600 | Comp := First_Component (Typ); | |
4601 | while Chars (Comp) /= Name_uParent loop | |
4602 | Comp := Next_Component (Comp); | |
4603 | end loop; | |
4604 | ||
4605 | Parent_Name := New_Occurrence_Of (Comp, Loc); | |
4606 | ||
4607 | -- Insert the parent aggregate | |
4608 | ||
4609 | Prepend_To (Component_Associations (N), | |
4610 | Make_Component_Association (Loc, | |
4611 | Choices => New_List (Parent_Name), | |
4612 | Expression => Parent_Aggr)); | |
4613 | ||
4614 | -- Expand recursively the parent propagating the right Tag | |
4615 | ||
4616 | Expand_Record_Aggregate ( | |
4617 | Parent_Aggr, Tag_Value, Parent_Expr); | |
4618 | end; | |
4619 | ||
4620 | -- For a root type, the tag component is added (unless compiling | |
4621 | -- for the Java VM, where tags are implicit). | |
4622 | ||
4623 | elsif not Java_VM then | |
4624 | declare | |
4625 | Tag_Name : constant Node_Id := | |
4626 | New_Occurrence_Of (Tag_Component (Typ), Loc); | |
4627 | Typ_Tag : constant Entity_Id := RTE (RE_Tag); | |
4628 | Conv_Node : constant Node_Id := | |
4629 | Unchecked_Convert_To (Typ_Tag, Tag_Value); | |
4630 | ||
4631 | begin | |
4632 | Set_Etype (Conv_Node, Typ_Tag); | |
4633 | Prepend_To (Component_Associations (N), | |
4634 | Make_Component_Association (Loc, | |
4635 | Choices => New_List (Tag_Name), | |
4636 | Expression => Conv_Node)); | |
4637 | end; | |
4638 | end if; | |
4639 | end if; | |
4640 | end if; | |
4641 | end Expand_Record_Aggregate; | |
4642 | ||
65356e64 AC |
4643 | ---------------------------- |
4644 | -- Has_Default_Init_Comps -- | |
4645 | ---------------------------- | |
4646 | ||
4647 | function Has_Default_Init_Comps (N : Node_Id) return Boolean is | |
d05ef0ab AC |
4648 | Comps : constant List_Id := Component_Associations (N); |
4649 | C : Node_Id; | |
c45b6ae0 | 4650 | Expr : Node_Id; |
65356e64 AC |
4651 | begin |
4652 | pragma Assert (Nkind (N) = N_Aggregate | |
c45b6ae0 AC |
4653 | or else Nkind (N) = N_Extension_Aggregate); |
4654 | ||
65356e64 AC |
4655 | if No (Comps) then |
4656 | return False; | |
4657 | end if; | |
4658 | ||
c45b6ae0 AC |
4659 | -- Check if any direct component has default initialized components |
4660 | ||
65356e64 AC |
4661 | C := First (Comps); |
4662 | while Present (C) loop | |
4663 | if Box_Present (C) then | |
4664 | return True; | |
4665 | end if; | |
4666 | ||
4667 | Next (C); | |
4668 | end loop; | |
c45b6ae0 AC |
4669 | |
4670 | -- Recursive call in case of aggregate expression | |
4671 | ||
4672 | C := First (Comps); | |
4673 | while Present (C) loop | |
4674 | Expr := Expression (C); | |
4675 | ||
4676 | if Present (Expr) | |
4677 | and then (Nkind (Expr) = N_Aggregate | |
4678 | or else Nkind (Expr) = N_Extension_Aggregate) | |
4679 | and then Has_Default_Init_Comps (Expr) | |
4680 | then | |
4681 | return True; | |
4682 | end if; | |
4683 | ||
4684 | Next (C); | |
4685 | end loop; | |
4686 | ||
65356e64 AC |
4687 | return False; |
4688 | end Has_Default_Init_Comps; | |
4689 | ||
70482933 RK |
4690 | -------------------------- |
4691 | -- Is_Delayed_Aggregate -- | |
4692 | -------------------------- | |
4693 | ||
4694 | function Is_Delayed_Aggregate (N : Node_Id) return Boolean is | |
fbf5a39b | 4695 | Node : Node_Id := N; |
70482933 | 4696 | Kind : Node_Kind := Nkind (Node); |
fbf5a39b | 4697 | |
70482933 RK |
4698 | begin |
4699 | if Kind = N_Qualified_Expression then | |
4700 | Node := Expression (Node); | |
4701 | Kind := Nkind (Node); | |
4702 | end if; | |
4703 | ||
4704 | if Kind /= N_Aggregate and then Kind /= N_Extension_Aggregate then | |
4705 | return False; | |
4706 | else | |
4707 | return Expansion_Delayed (Node); | |
4708 | end if; | |
4709 | end Is_Delayed_Aggregate; | |
4710 | ||
4711 | -------------------- | |
4712 | -- Late_Expansion -- | |
4713 | -------------------- | |
4714 | ||
4715 | function Late_Expansion | |
4716 | (N : Node_Id; | |
4717 | Typ : Entity_Id; | |
4718 | Target : Node_Id; | |
fbf5a39b | 4719 | Flist : Node_Id := Empty; |
c45b6ae0 | 4720 | Obj : Entity_Id := Empty) return List_Id is |
70482933 RK |
4721 | begin |
4722 | if Is_Record_Type (Etype (N)) then | |
4723 | return Build_Record_Aggr_Code (N, Typ, Target, Flist, Obj); | |
c45b6ae0 | 4724 | elsif Is_Array_Type (Etype (N)) then |
70482933 RK |
4725 | return |
4726 | Build_Array_Aggr_Code | |
c45b6ae0 AC |
4727 | (N => N, |
4728 | Ctype => Component_Type (Etype (N)), | |
4729 | Index => First_Index (Typ), | |
4730 | Into => Target, | |
4731 | Scalar_Comp => Is_Scalar_Type (Component_Type (Typ)), | |
4732 | Indices => No_List, | |
4733 | Flist => Flist); | |
4734 | else | |
4735 | pragma Assert (False); | |
4736 | return New_List; | |
70482933 RK |
4737 | end if; |
4738 | end Late_Expansion; | |
4739 | ||
4740 | ---------------------------------- | |
4741 | -- Make_OK_Assignment_Statement -- | |
4742 | ---------------------------------- | |
4743 | ||
4744 | function Make_OK_Assignment_Statement | |
4745 | (Sloc : Source_Ptr; | |
4746 | Name : Node_Id; | |
d05ef0ab | 4747 | Expression : Node_Id) return Node_Id |
70482933 RK |
4748 | is |
4749 | begin | |
4750 | Set_Assignment_OK (Name); | |
4751 | return Make_Assignment_Statement (Sloc, Name, Expression); | |
4752 | end Make_OK_Assignment_Statement; | |
4753 | ||
4754 | ----------------------- | |
4755 | -- Number_Of_Choices -- | |
4756 | ----------------------- | |
4757 | ||
4758 | function Number_Of_Choices (N : Node_Id) return Nat is | |
4759 | Assoc : Node_Id; | |
4760 | Choice : Node_Id; | |
4761 | ||
4762 | Nb_Choices : Nat := 0; | |
4763 | ||
4764 | begin | |
4765 | if Present (Expressions (N)) then | |
4766 | return 0; | |
4767 | end if; | |
4768 | ||
4769 | Assoc := First (Component_Associations (N)); | |
4770 | while Present (Assoc) loop | |
4771 | ||
4772 | Choice := First (Choices (Assoc)); | |
4773 | while Present (Choice) loop | |
4774 | ||
4775 | if Nkind (Choice) /= N_Others_Choice then | |
4776 | Nb_Choices := Nb_Choices + 1; | |
4777 | end if; | |
4778 | ||
4779 | Next (Choice); | |
4780 | end loop; | |
4781 | ||
4782 | Next (Assoc); | |
4783 | end loop; | |
4784 | ||
4785 | return Nb_Choices; | |
4786 | end Number_Of_Choices; | |
4787 | ||
07fc65c4 GB |
4788 | ------------------------------------ |
4789 | -- Packed_Array_Aggregate_Handled -- | |
4790 | ------------------------------------ | |
4791 | ||
4792 | -- The current version of this procedure will handle at compile time | |
4793 | -- any array aggregate that meets these conditions: | |
4794 | ||
4795 | -- One dimensional, bit packed | |
4796 | -- Underlying packed type is modular type | |
4797 | -- Bounds are within 32-bit Int range | |
4798 | -- All bounds and values are static | |
4799 | ||
4800 | function Packed_Array_Aggregate_Handled (N : Node_Id) return Boolean is | |
4801 | Loc : constant Source_Ptr := Sloc (N); | |
4802 | Typ : constant Entity_Id := Etype (N); | |
4803 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
4804 | ||
4805 | Not_Handled : exception; | |
4806 | -- Exception raised if this aggregate cannot be handled | |
4807 | ||
4808 | begin | |
4809 | -- For now, handle only one dimensional bit packed arrays | |
4810 | ||
4811 | if not Is_Bit_Packed_Array (Typ) | |
4812 | or else Number_Dimensions (Typ) > 1 | |
4813 | or else not Is_Modular_Integer_Type (Packed_Array_Type (Typ)) | |
4814 | then | |
4815 | return False; | |
4816 | end if; | |
4817 | ||
4818 | declare | |
4819 | Csiz : constant Nat := UI_To_Int (Component_Size (Typ)); | |
4820 | ||
4821 | Lo : Node_Id; | |
4822 | Hi : Node_Id; | |
4823 | -- Bounds of index type | |
4824 | ||
4825 | Lob : Uint; | |
4826 | Hib : Uint; | |
4827 | -- Values of bounds if compile time known | |
4828 | ||
4829 | function Get_Component_Val (N : Node_Id) return Uint; | |
4830 | -- Given a expression value N of the component type Ctyp, returns | |
4831 | -- A value of Csiz (component size) bits representing this value. | |
4832 | -- If the value is non-static or any other reason exists why the | |
4833 | -- value cannot be returned, then Not_Handled is raised. | |
4834 | ||
4835 | ----------------------- | |
4836 | -- Get_Component_Val -- | |
4837 | ----------------------- | |
4838 | ||
4839 | function Get_Component_Val (N : Node_Id) return Uint is | |
4840 | Val : Uint; | |
4841 | ||
4842 | begin | |
4843 | -- We have to analyze the expression here before doing any further | |
4844 | -- processing here. The analysis of such expressions is deferred | |
4845 | -- till expansion to prevent some problems of premature analysis. | |
4846 | ||
4847 | Analyze_And_Resolve (N, Ctyp); | |
4848 | ||
4849 | -- Must have a compile time value | |
4850 | ||
4851 | if not Compile_Time_Known_Value (N) then | |
4852 | raise Not_Handled; | |
4853 | end if; | |
4854 | ||
4855 | Val := Expr_Rep_Value (N); | |
4856 | ||
4857 | -- Adjust for bias, and strip proper number of bits | |
4858 | ||
4859 | if Has_Biased_Representation (Ctyp) then | |
4860 | Val := Val - Expr_Value (Type_Low_Bound (Ctyp)); | |
4861 | end if; | |
4862 | ||
4863 | return Val mod Uint_2 ** Csiz; | |
4864 | end Get_Component_Val; | |
4865 | ||
4866 | -- Here we know we have a one dimensional bit packed array | |
4867 | ||
4868 | begin | |
4869 | Get_Index_Bounds (First_Index (Typ), Lo, Hi); | |
4870 | ||
4871 | -- Cannot do anything if bounds are dynamic | |
4872 | ||
4873 | if not Compile_Time_Known_Value (Lo) | |
4874 | or else | |
4875 | not Compile_Time_Known_Value (Hi) | |
4876 | then | |
4877 | return False; | |
4878 | end if; | |
4879 | ||
4880 | -- Or are silly out of range of int bounds | |
4881 | ||
4882 | Lob := Expr_Value (Lo); | |
4883 | Hib := Expr_Value (Hi); | |
4884 | ||
4885 | if not UI_Is_In_Int_Range (Lob) | |
4886 | or else | |
4887 | not UI_Is_In_Int_Range (Hib) | |
4888 | then | |
4889 | return False; | |
4890 | end if; | |
4891 | ||
4892 | -- At this stage we have a suitable aggregate for handling | |
4893 | -- at compile time (the only remaining checks, are that the | |
4894 | -- values of expressions in the aggregate are compile time | |
4895 | -- known (check performed by Get_Component_Val), and that | |
4896 | -- any subtypes or ranges are statically known. | |
4897 | ||
4898 | -- If the aggregate is not fully positional at this stage, | |
4899 | -- then convert it to positional form. Either this will fail, | |
4900 | -- in which case we can do nothing, or it will succeed, in | |
4901 | -- which case we have succeeded in handling the aggregate, | |
4902 | -- or it will stay an aggregate, in which case we have failed | |
4903 | -- to handle this case. | |
4904 | ||
4905 | if Present (Component_Associations (N)) then | |
4906 | Convert_To_Positional | |
4907 | (N, Max_Others_Replicate => 64, Handle_Bit_Packed => True); | |
4908 | return Nkind (N) /= N_Aggregate; | |
4909 | end if; | |
4910 | ||
4911 | -- Otherwise we are all positional, so convert to proper value | |
4912 | ||
4913 | declare | |
4914 | Lov : constant Nat := UI_To_Int (Lob); | |
4915 | Hiv : constant Nat := UI_To_Int (Hib); | |
4916 | ||
4917 | Len : constant Nat := Int'Max (0, Hiv - Lov + 1); | |
4918 | -- The length of the array (number of elements) | |
4919 | ||
4920 | Aggregate_Val : Uint; | |
4921 | -- Value of aggregate. The value is set in the low order | |
4922 | -- bits of this value. For the little-endian case, the | |
4923 | -- values are stored from low-order to high-order and | |
4924 | -- for the big-endian case the values are stored from | |
4925 | -- high-order to low-order. Note that gigi will take care | |
4926 | -- of the conversions to left justify the value in the big | |
4927 | -- endian case (because of left justified modular type | |
4928 | -- processing), so we do not have to worry about that here. | |
4929 | ||
4930 | Lit : Node_Id; | |
4931 | -- Integer literal for resulting constructed value | |
4932 | ||
4933 | Shift : Nat; | |
4934 | -- Shift count from low order for next value | |
4935 | ||
4936 | Incr : Int; | |
4937 | -- Shift increment for loop | |
4938 | ||
4939 | Expr : Node_Id; | |
4940 | -- Next expression from positional parameters of aggregate | |
4941 | ||
4942 | begin | |
4943 | -- For little endian, we fill up the low order bits of the | |
4944 | -- target value. For big endian we fill up the high order | |
4945 | -- bits of the target value (which is a left justified | |
4946 | -- modular value). | |
4947 | ||
4948 | if Bytes_Big_Endian xor Debug_Flag_8 then | |
4949 | Shift := Csiz * (Len - 1); | |
4950 | Incr := -Csiz; | |
4951 | else | |
4952 | Shift := 0; | |
4953 | Incr := +Csiz; | |
4954 | end if; | |
4955 | ||
4956 | -- Loop to set the values | |
4957 | ||
fbf5a39b AC |
4958 | if Len = 0 then |
4959 | Aggregate_Val := Uint_0; | |
4960 | else | |
4961 | Expr := First (Expressions (N)); | |
4962 | Aggregate_Val := Get_Component_Val (Expr) * Uint_2 ** Shift; | |
4963 | ||
4964 | for J in 2 .. Len loop | |
4965 | Shift := Shift + Incr; | |
4966 | Next (Expr); | |
4967 | Aggregate_Val := | |
4968 | Aggregate_Val + Get_Component_Val (Expr) * Uint_2 ** Shift; | |
4969 | end loop; | |
4970 | end if; | |
07fc65c4 GB |
4971 | |
4972 | -- Now we can rewrite with the proper value | |
4973 | ||
4974 | Lit := | |
4975 | Make_Integer_Literal (Loc, | |
4976 | Intval => Aggregate_Val); | |
4977 | Set_Print_In_Hex (Lit); | |
4978 | ||
4979 | -- Construct the expression using this literal. Note that it is | |
4980 | -- important to qualify the literal with its proper modular type | |
4981 | -- since universal integer does not have the required range and | |
4982 | -- also this is a left justified modular type, which is important | |
4983 | -- in the big-endian case. | |
4984 | ||
4985 | Rewrite (N, | |
4986 | Unchecked_Convert_To (Typ, | |
4987 | Make_Qualified_Expression (Loc, | |
4988 | Subtype_Mark => | |
4989 | New_Occurrence_Of (Packed_Array_Type (Typ), Loc), | |
4990 | Expression => Lit))); | |
4991 | ||
4992 | Analyze_And_Resolve (N, Typ); | |
4993 | return True; | |
4994 | end; | |
4995 | end; | |
4996 | ||
4997 | exception | |
4998 | when Not_Handled => | |
4999 | return False; | |
5000 | end Packed_Array_Aggregate_Handled; | |
5001 | ||
fbf5a39b AC |
5002 | ---------------------------- |
5003 | -- Has_Mutable_Components -- | |
5004 | ---------------------------- | |
5005 | ||
5006 | function Has_Mutable_Components (Typ : Entity_Id) return Boolean is | |
5007 | Comp : Entity_Id; | |
5008 | ||
5009 | begin | |
5010 | Comp := First_Component (Typ); | |
5011 | ||
5012 | while Present (Comp) loop | |
5013 | if Is_Record_Type (Etype (Comp)) | |
5014 | and then Has_Discriminants (Etype (Comp)) | |
5015 | and then not Is_Constrained (Etype (Comp)) | |
5016 | then | |
5017 | return True; | |
5018 | end if; | |
5019 | ||
5020 | Next_Component (Comp); | |
5021 | end loop; | |
5022 | ||
5023 | return False; | |
5024 | end Has_Mutable_Components; | |
5025 | ||
07fc65c4 GB |
5026 | ------------------------------ |
5027 | -- Initialize_Discriminants -- | |
5028 | ------------------------------ | |
5029 | ||
5030 | procedure Initialize_Discriminants (N : Node_Id; Typ : Entity_Id) is | |
5031 | Loc : constant Source_Ptr := Sloc (N); | |
5032 | Bas : constant Entity_Id := Base_Type (Typ); | |
5033 | Par : constant Entity_Id := Etype (Bas); | |
5034 | Decl : constant Node_Id := Parent (Par); | |
5035 | Ref : Node_Id; | |
5036 | ||
5037 | begin | |
5038 | if Is_Tagged_Type (Bas) | |
5039 | and then Is_Derived_Type (Bas) | |
5040 | and then Has_Discriminants (Par) | |
5041 | and then Has_Discriminants (Bas) | |
5042 | and then Number_Discriminants (Bas) /= Number_Discriminants (Par) | |
5043 | and then Nkind (Decl) = N_Full_Type_Declaration | |
5044 | and then Nkind (Type_Definition (Decl)) = N_Record_Definition | |
5045 | and then Present | |
5046 | (Variant_Part (Component_List (Type_Definition (Decl)))) | |
5047 | and then Nkind (N) /= N_Extension_Aggregate | |
5048 | then | |
5049 | ||
fbf5a39b | 5050 | -- Call init proc to set discriminants. |
07fc65c4 GB |
5051 | -- There should eventually be a special procedure for this ??? |
5052 | ||
5053 | Ref := New_Reference_To (Defining_Identifier (N), Loc); | |
5054 | Insert_Actions_After (N, | |
5055 | Build_Initialization_Call (Sloc (N), Ref, Typ)); | |
5056 | end if; | |
5057 | end Initialize_Discriminants; | |
5058 | ||
70482933 RK |
5059 | --------------------------- |
5060 | -- Safe_Slice_Assignment -- | |
5061 | --------------------------- | |
5062 | ||
07fc65c4 | 5063 | function Safe_Slice_Assignment (N : Node_Id) return Boolean is |
70482933 RK |
5064 | Loc : constant Source_Ptr := Sloc (Parent (N)); |
5065 | Pref : constant Node_Id := Prefix (Name (Parent (N))); | |
5066 | Range_Node : constant Node_Id := Discrete_Range (Name (Parent (N))); | |
5067 | Expr : Node_Id; | |
07fc65c4 | 5068 | L_J : Entity_Id; |
70482933 RK |
5069 | L_Iter : Node_Id; |
5070 | L_Body : Node_Id; | |
5071 | Stat : Node_Id; | |
5072 | ||
5073 | begin | |
07fc65c4 | 5074 | -- Generate: for J in Range loop Pref (J) := Expr; end loop; |
70482933 RK |
5075 | |
5076 | if Comes_From_Source (N) | |
5077 | and then No (Expressions (N)) | |
5078 | and then Nkind (First (Choices (First (Component_Associations (N))))) | |
5079 | = N_Others_Choice | |
5080 | then | |
5081 | Expr := | |
5082 | Expression (First (Component_Associations (N))); | |
07fc65c4 | 5083 | L_J := Make_Defining_Identifier (Loc, New_Internal_Name ('J')); |
70482933 RK |
5084 | |
5085 | L_Iter := | |
5086 | Make_Iteration_Scheme (Loc, | |
5087 | Loop_Parameter_Specification => | |
5088 | Make_Loop_Parameter_Specification | |
5089 | (Loc, | |
07fc65c4 | 5090 | Defining_Identifier => L_J, |
70482933 RK |
5091 | Discrete_Subtype_Definition => Relocate_Node (Range_Node))); |
5092 | ||
5093 | L_Body := | |
5094 | Make_Assignment_Statement (Loc, | |
5095 | Name => | |
5096 | Make_Indexed_Component (Loc, | |
5097 | Prefix => Relocate_Node (Pref), | |
07fc65c4 | 5098 | Expressions => New_List (New_Occurrence_Of (L_J, Loc))), |
70482933 RK |
5099 | Expression => Relocate_Node (Expr)); |
5100 | ||
5101 | -- Construct the final loop | |
5102 | ||
5103 | Stat := | |
5104 | Make_Implicit_Loop_Statement | |
5105 | (Node => Parent (N), | |
5106 | Identifier => Empty, | |
5107 | Iteration_Scheme => L_Iter, | |
5108 | Statements => New_List (L_Body)); | |
5109 | ||
fbf5a39b AC |
5110 | -- Set type of aggregate to be type of lhs in assignment, |
5111 | -- to suppress redundant length checks. | |
5112 | ||
5113 | Set_Etype (N, Etype (Name (Parent (N)))); | |
5114 | ||
70482933 RK |
5115 | Rewrite (Parent (N), Stat); |
5116 | Analyze (Parent (N)); | |
5117 | return True; | |
5118 | ||
5119 | else | |
5120 | return False; | |
5121 | end if; | |
5122 | end Safe_Slice_Assignment; | |
5123 | ||
5124 | --------------------- | |
5125 | -- Sort_Case_Table -- | |
5126 | --------------------- | |
5127 | ||
5128 | procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is | |
fbf5a39b AC |
5129 | L : constant Int := Case_Table'First; |
5130 | U : constant Int := Case_Table'Last; | |
70482933 RK |
5131 | K : Int; |
5132 | J : Int; | |
5133 | T : Case_Bounds; | |
5134 | ||
5135 | begin | |
5136 | K := L; | |
5137 | ||
5138 | while K /= U loop | |
5139 | T := Case_Table (K + 1); | |
5140 | J := K + 1; | |
5141 | ||
5142 | while J /= L | |
5143 | and then Expr_Value (Case_Table (J - 1).Choice_Lo) > | |
5144 | Expr_Value (T.Choice_Lo) | |
5145 | loop | |
5146 | Case_Table (J) := Case_Table (J - 1); | |
5147 | J := J - 1; | |
5148 | end loop; | |
5149 | ||
5150 | Case_Table (J) := T; | |
5151 | K := K + 1; | |
5152 | end loop; | |
5153 | end Sort_Case_Table; | |
5154 | ||
5155 | end Exp_Aggr; |