[multiple changes]
[gcc.git] / gcc / ada / exp_ch5.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 5 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch11; use Exp_Ch11;
36 with Exp_Dbug; use Exp_Dbug;
37 with Exp_Pakd; use Exp_Pakd;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Sinfo; use Sinfo;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Res; use Sem_Res;
55 with Sem_Util; use Sem_Util;
56 with Snames; use Snames;
57 with Stand; use Stand;
58 with Stringt; use Stringt;
59 with Targparm; use Targparm;
60 with Tbuild; use Tbuild;
61 with Ttypes; use Ttypes;
62 with Uintp; use Uintp;
63 with Validsw; use Validsw;
64
65 package body Exp_Ch5 is
66
67 function Change_Of_Representation (N : Node_Id) return Boolean;
68 -- Determine if the right hand side of the assignment N is a type
69 -- conversion which requires a change of representation. Called
70 -- only for the array and record cases.
71
72 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id);
73 -- N is an assignment which assigns an array value. This routine process
74 -- the various special cases and checks required for such assignments,
75 -- including change of representation. Rhs is normally simply the right
76 -- hand side of the assignment, except that if the right hand side is
77 -- a type conversion or a qualified expression, then the Rhs is the
78 -- actual expression inside any such type conversions or qualifications.
79
80 function Expand_Assign_Array_Loop
81 (N : Node_Id;
82 Larray : Entity_Id;
83 Rarray : Entity_Id;
84 L_Type : Entity_Id;
85 R_Type : Entity_Id;
86 Ndim : Pos;
87 Rev : Boolean) return Node_Id;
88 -- N is an assignment statement which assigns an array value. This routine
89 -- expands the assignment into a loop (or nested loops for the case of a
90 -- multi-dimensional array) to do the assignment component by component.
91 -- Larray and Rarray are the entities of the actual arrays on the left
92 -- hand and right hand sides. L_Type and R_Type are the types of these
93 -- arrays (which may not be the same, due to either sliding, or to a
94 -- change of representation case). Ndim is the number of dimensions and
95 -- the parameter Rev indicates if the loops run normally (Rev = False),
96 -- or reversed (Rev = True). The value returned is the constructed
97 -- loop statement. Auxiliary declarations are inserted before node N
98 -- using the standard Insert_Actions mechanism.
99
100 procedure Expand_Assign_Record (N : Node_Id);
101 -- N is an assignment of a non-tagged record value. This routine handles
102 -- the case where the assignment must be made component by component,
103 -- either because the target is not byte aligned, or there is a change
104 -- of representation.
105
106 procedure Expand_Non_Function_Return (N : Node_Id);
107 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
108 -- a procedure body, entry body, accept statement, or extended return
109 -- statement. Note that all non-function returns are simple return
110 -- statements.
111
112 procedure Expand_Simple_Function_Return (N : Node_Id);
113 -- Expand simple return from function. In the case where we are returning
114 -- from a function body this is called by Expand_N_Simple_Return_Statement.
115
116 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id;
117 -- Generate the necessary code for controlled and tagged assignment,
118 -- that is to say, finalization of the target before, adjustment of
119 -- the target after and save and restore of the tag and finalization
120 -- pointers which are not 'part of the value' and must not be changed
121 -- upon assignment. N is the original Assignment node.
122
123 ------------------------------
124 -- Change_Of_Representation --
125 ------------------------------
126
127 function Change_Of_Representation (N : Node_Id) return Boolean is
128 Rhs : constant Node_Id := Expression (N);
129 begin
130 return
131 Nkind (Rhs) = N_Type_Conversion
132 and then
133 not Same_Representation (Etype (Rhs), Etype (Expression (Rhs)));
134 end Change_Of_Representation;
135
136 -------------------------
137 -- Expand_Assign_Array --
138 -------------------------
139
140 -- There are two issues here. First, do we let Gigi do a block move, or
141 -- do we expand out into a loop? Second, we need to set the two flags
142 -- Forwards_OK and Backwards_OK which show whether the block move (or
143 -- corresponding loops) can be legitimately done in a forwards (low to
144 -- high) or backwards (high to low) manner.
145
146 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id) is
147 Loc : constant Source_Ptr := Sloc (N);
148
149 Lhs : constant Node_Id := Name (N);
150
151 Act_Lhs : constant Node_Id := Get_Referenced_Object (Lhs);
152 Act_Rhs : Node_Id := Get_Referenced_Object (Rhs);
153
154 L_Type : constant Entity_Id :=
155 Underlying_Type (Get_Actual_Subtype (Act_Lhs));
156 R_Type : Entity_Id :=
157 Underlying_Type (Get_Actual_Subtype (Act_Rhs));
158
159 L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice;
160 R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice;
161
162 Crep : constant Boolean := Change_Of_Representation (N);
163
164 Larray : Node_Id;
165 Rarray : Node_Id;
166
167 Ndim : constant Pos := Number_Dimensions (L_Type);
168
169 Loop_Required : Boolean := False;
170 -- This switch is set to True if the array move must be done using
171 -- an explicit front end generated loop.
172
173 procedure Apply_Dereference (Arg : Node_Id);
174 -- If the argument is an access to an array, and the assignment is
175 -- converted into a procedure call, apply explicit dereference.
176
177 function Has_Address_Clause (Exp : Node_Id) return Boolean;
178 -- Test if Exp is a reference to an array whose declaration has
179 -- an address clause, or it is a slice of such an array.
180
181 function Is_Formal_Array (Exp : Node_Id) return Boolean;
182 -- Test if Exp is a reference to an array which is either a formal
183 -- parameter or a slice of a formal parameter. These are the cases
184 -- where hidden aliasing can occur.
185
186 function Is_Non_Local_Array (Exp : Node_Id) return Boolean;
187 -- Determine if Exp is a reference to an array variable which is other
188 -- than an object defined in the current scope, or a slice of such
189 -- an object. Such objects can be aliased to parameters (unlike local
190 -- array references).
191
192 -----------------------
193 -- Apply_Dereference --
194 -----------------------
195
196 procedure Apply_Dereference (Arg : Node_Id) is
197 Typ : constant Entity_Id := Etype (Arg);
198 begin
199 if Is_Access_Type (Typ) then
200 Rewrite (Arg, Make_Explicit_Dereference (Loc,
201 Prefix => Relocate_Node (Arg)));
202 Analyze_And_Resolve (Arg, Designated_Type (Typ));
203 end if;
204 end Apply_Dereference;
205
206 ------------------------
207 -- Has_Address_Clause --
208 ------------------------
209
210 function Has_Address_Clause (Exp : Node_Id) return Boolean is
211 begin
212 return
213 (Is_Entity_Name (Exp) and then
214 Present (Address_Clause (Entity (Exp))))
215 or else
216 (Nkind (Exp) = N_Slice and then Has_Address_Clause (Prefix (Exp)));
217 end Has_Address_Clause;
218
219 ---------------------
220 -- Is_Formal_Array --
221 ---------------------
222
223 function Is_Formal_Array (Exp : Node_Id) return Boolean is
224 begin
225 return
226 (Is_Entity_Name (Exp) and then Is_Formal (Entity (Exp)))
227 or else
228 (Nkind (Exp) = N_Slice and then Is_Formal_Array (Prefix (Exp)));
229 end Is_Formal_Array;
230
231 ------------------------
232 -- Is_Non_Local_Array --
233 ------------------------
234
235 function Is_Non_Local_Array (Exp : Node_Id) return Boolean is
236 begin
237 return (Is_Entity_Name (Exp)
238 and then Scope (Entity (Exp)) /= Current_Scope)
239 or else (Nkind (Exp) = N_Slice
240 and then Is_Non_Local_Array (Prefix (Exp)));
241 end Is_Non_Local_Array;
242
243 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
244
245 Lhs_Formal : constant Boolean := Is_Formal_Array (Act_Lhs);
246 Rhs_Formal : constant Boolean := Is_Formal_Array (Act_Rhs);
247
248 Lhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Lhs);
249 Rhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Rhs);
250
251 -- Start of processing for Expand_Assign_Array
252
253 begin
254 -- Deal with length check. Note that the length check is done with
255 -- respect to the right hand side as given, not a possible underlying
256 -- renamed object, since this would generate incorrect extra checks.
257
258 Apply_Length_Check (Rhs, L_Type);
259
260 -- We start by assuming that the move can be done in either direction,
261 -- i.e. that the two sides are completely disjoint.
262
263 Set_Forwards_OK (N, True);
264 Set_Backwards_OK (N, True);
265
266 -- Normally it is only the slice case that can lead to overlap, and
267 -- explicit checks for slices are made below. But there is one case
268 -- where the slice can be implicit and invisible to us: when we have a
269 -- one dimensional array, and either both operands are parameters, or
270 -- one is a parameter (which can be a slice passed by reference) and the
271 -- other is a non-local variable. In this case the parameter could be a
272 -- slice that overlaps with the other operand.
273
274 -- However, if the array subtype is a constrained first subtype in the
275 -- parameter case, then we don't have to worry about overlap, since
276 -- slice assignments aren't possible (other than for a slice denoting
277 -- the whole array).
278
279 -- Note: No overlap is possible if there is a change of representation,
280 -- so we can exclude this case.
281
282 if Ndim = 1
283 and then not Crep
284 and then
285 ((Lhs_Formal and Rhs_Formal)
286 or else
287 (Lhs_Formal and Rhs_Non_Local_Var)
288 or else
289 (Rhs_Formal and Lhs_Non_Local_Var))
290 and then
291 (not Is_Constrained (Etype (Lhs))
292 or else not Is_First_Subtype (Etype (Lhs)))
293
294 -- In the case of compiling for the Java or .NET Virtual Machine,
295 -- slices are always passed by making a copy, so we don't have to
296 -- worry about overlap. We also want to prevent generation of "<"
297 -- comparisons for array addresses, since that's a meaningless
298 -- operation on the VM.
299
300 and then VM_Target = No_VM
301 then
302 Set_Forwards_OK (N, False);
303 Set_Backwards_OK (N, False);
304
305 -- Note: the bit-packed case is not worrisome here, since if we have
306 -- a slice passed as a parameter, it is always aligned on a byte
307 -- boundary, and if there are no explicit slices, the assignment
308 -- can be performed directly.
309 end if;
310
311 -- If either operand has an address clause clear Backwards_OK and
312 -- Forwards_OK, since we cannot tell if the operands overlap.
313
314 if Has_Address_Clause (Lhs) or else Has_Address_Clause (Rhs) then
315 Set_Forwards_OK (N, False);
316 Set_Backwards_OK (N, False);
317 end if;
318
319 -- We certainly must use a loop for change of representation and also
320 -- we use the operand of the conversion on the right hand side as the
321 -- effective right hand side (the component types must match in this
322 -- situation).
323
324 if Crep then
325 Act_Rhs := Get_Referenced_Object (Rhs);
326 R_Type := Get_Actual_Subtype (Act_Rhs);
327 Loop_Required := True;
328
329 -- We require a loop if the left side is possibly bit unaligned
330
331 elsif Possible_Bit_Aligned_Component (Lhs)
332 or else
333 Possible_Bit_Aligned_Component (Rhs)
334 then
335 Loop_Required := True;
336
337 -- Arrays with controlled components are expanded into a loop to force
338 -- calls to Adjust at the component level.
339
340 elsif Has_Controlled_Component (L_Type) then
341 Loop_Required := True;
342
343 -- If object is atomic, we cannot tolerate a loop
344
345 elsif Is_Atomic_Object (Act_Lhs)
346 or else
347 Is_Atomic_Object (Act_Rhs)
348 then
349 return;
350
351 -- Loop is required if we have atomic components since we have to
352 -- be sure to do any accesses on an element by element basis.
353
354 elsif Has_Atomic_Components (L_Type)
355 or else Has_Atomic_Components (R_Type)
356 or else Is_Atomic (Component_Type (L_Type))
357 or else Is_Atomic (Component_Type (R_Type))
358 then
359 Loop_Required := True;
360
361 -- Case where no slice is involved
362
363 elsif not L_Slice and not R_Slice then
364
365 -- The following code deals with the case of unconstrained bit packed
366 -- arrays. The problem is that the template for such arrays contains
367 -- the bounds of the actual source level array, but the copy of an
368 -- entire array requires the bounds of the underlying array. It would
369 -- be nice if the back end could take care of this, but right now it
370 -- does not know how, so if we have such a type, then we expand out
371 -- into a loop, which is inefficient but works correctly. If we don't
372 -- do this, we get the wrong length computed for the array to be
373 -- moved. The two cases we need to worry about are:
374
375 -- Explicit deference of an unconstrained packed array type as in the
376 -- following example:
377
378 -- procedure C52 is
379 -- type BITS is array(INTEGER range <>) of BOOLEAN;
380 -- pragma PACK(BITS);
381 -- type A is access BITS;
382 -- P1,P2 : A;
383 -- begin
384 -- P1 := new BITS (1 .. 65_535);
385 -- P2 := new BITS (1 .. 65_535);
386 -- P2.ALL := P1.ALL;
387 -- end C52;
388
389 -- A formal parameter reference with an unconstrained bit array type
390 -- is the other case we need to worry about (here we assume the same
391 -- BITS type declared above):
392
393 -- procedure Write_All (File : out BITS; Contents : BITS);
394 -- begin
395 -- File.Storage := Contents;
396 -- end Write_All;
397
398 -- We expand to a loop in either of these two cases
399
400 -- Question for future thought. Another potentially more efficient
401 -- approach would be to create the actual subtype, and then do an
402 -- unchecked conversion to this actual subtype ???
403
404 Check_Unconstrained_Bit_Packed_Array : declare
405
406 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean;
407 -- Function to perform required test for the first case, above
408 -- (dereference of an unconstrained bit packed array).
409
410 -----------------------
411 -- Is_UBPA_Reference --
412 -----------------------
413
414 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean is
415 Typ : constant Entity_Id := Underlying_Type (Etype (Opnd));
416 P_Type : Entity_Id;
417 Des_Type : Entity_Id;
418
419 begin
420 if Present (Packed_Array_Type (Typ))
421 and then Is_Array_Type (Packed_Array_Type (Typ))
422 and then not Is_Constrained (Packed_Array_Type (Typ))
423 then
424 return True;
425
426 elsif Nkind (Opnd) = N_Explicit_Dereference then
427 P_Type := Underlying_Type (Etype (Prefix (Opnd)));
428
429 if not Is_Access_Type (P_Type) then
430 return False;
431
432 else
433 Des_Type := Designated_Type (P_Type);
434 return
435 Is_Bit_Packed_Array (Des_Type)
436 and then not Is_Constrained (Des_Type);
437 end if;
438
439 else
440 return False;
441 end if;
442 end Is_UBPA_Reference;
443
444 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
445
446 begin
447 if Is_UBPA_Reference (Lhs)
448 or else
449 Is_UBPA_Reference (Rhs)
450 then
451 Loop_Required := True;
452
453 -- Here if we do not have the case of a reference to a bit packed
454 -- unconstrained array case. In this case gigi can most certainly
455 -- handle the assignment if a forwards move is allowed.
456
457 -- (could it handle the backwards case also???)
458
459 elsif Forwards_OK (N) then
460 return;
461 end if;
462 end Check_Unconstrained_Bit_Packed_Array;
463
464 -- The back end can always handle the assignment if the right side is a
465 -- string literal (note that overlap is definitely impossible in this
466 -- case). If the type is packed, a string literal is always converted
467 -- into an aggregate, except in the case of a null slice, for which no
468 -- aggregate can be written. In that case, rewrite the assignment as a
469 -- null statement, a length check has already been emitted to verify
470 -- that the range of the left-hand side is empty.
471
472 -- Note that this code is not executed if we have an assignment of a
473 -- string literal to a non-bit aligned component of a record, a case
474 -- which cannot be handled by the backend.
475
476 elsif Nkind (Rhs) = N_String_Literal then
477 if String_Length (Strval (Rhs)) = 0
478 and then Is_Bit_Packed_Array (L_Type)
479 then
480 Rewrite (N, Make_Null_Statement (Loc));
481 Analyze (N);
482 end if;
483
484 return;
485
486 -- If either operand is bit packed, then we need a loop, since we can't
487 -- be sure that the slice is byte aligned. Similarly, if either operand
488 -- is a possibly unaligned slice, then we need a loop (since the back
489 -- end cannot handle unaligned slices).
490
491 elsif Is_Bit_Packed_Array (L_Type)
492 or else Is_Bit_Packed_Array (R_Type)
493 or else Is_Possibly_Unaligned_Slice (Lhs)
494 or else Is_Possibly_Unaligned_Slice (Rhs)
495 then
496 Loop_Required := True;
497
498 -- If we are not bit-packed, and we have only one slice, then no overlap
499 -- is possible except in the parameter case, so we can let the back end
500 -- handle things.
501
502 elsif not (L_Slice and R_Slice) then
503 if Forwards_OK (N) then
504 return;
505 end if;
506 end if;
507
508 -- If the right-hand side is a string literal, introduce a temporary for
509 -- it, for use in the generated loop that will follow.
510
511 if Nkind (Rhs) = N_String_Literal then
512 declare
513 Temp : constant Entity_Id :=
514 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
515 Decl : Node_Id;
516
517 begin
518 Decl :=
519 Make_Object_Declaration (Loc,
520 Defining_Identifier => Temp,
521 Object_Definition => New_Occurrence_Of (L_Type, Loc),
522 Expression => Relocate_Node (Rhs));
523
524 Insert_Action (N, Decl);
525 Rewrite (Rhs, New_Occurrence_Of (Temp, Loc));
526 R_Type := Etype (Temp);
527 end;
528 end if;
529
530 -- Come here to complete the analysis
531
532 -- Loop_Required: Set to True if we know that a loop is required
533 -- regardless of overlap considerations.
534
535 -- Forwards_OK: Set to False if we already know that a forwards
536 -- move is not safe, else set to True.
537
538 -- Backwards_OK: Set to False if we already know that a backwards
539 -- move is not safe, else set to True
540
541 -- Our task at this stage is to complete the overlap analysis, which can
542 -- result in possibly setting Forwards_OK or Backwards_OK to False, and
543 -- then generating the final code, either by deciding that it is OK
544 -- after all to let Gigi handle it, or by generating appropriate code
545 -- in the front end.
546
547 declare
548 L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
549 R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
550
551 Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
552 Left_Hi : constant Node_Id := Type_High_Bound (L_Index_Typ);
553 Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
554 Right_Hi : constant Node_Id := Type_High_Bound (R_Index_Typ);
555
556 Act_L_Array : Node_Id;
557 Act_R_Array : Node_Id;
558
559 Cleft_Lo : Node_Id;
560 Cright_Lo : Node_Id;
561 Condition : Node_Id;
562
563 Cresult : Compare_Result;
564
565 begin
566 -- Get the expressions for the arrays. If we are dealing with a
567 -- private type, then convert to the underlying type. We can do
568 -- direct assignments to an array that is a private type, but we
569 -- cannot assign to elements of the array without this extra
570 -- unchecked conversion.
571
572 if Nkind (Act_Lhs) = N_Slice then
573 Larray := Prefix (Act_Lhs);
574 else
575 Larray := Act_Lhs;
576
577 if Is_Private_Type (Etype (Larray)) then
578 Larray :=
579 Unchecked_Convert_To
580 (Underlying_Type (Etype (Larray)), Larray);
581 end if;
582 end if;
583
584 if Nkind (Act_Rhs) = N_Slice then
585 Rarray := Prefix (Act_Rhs);
586 else
587 Rarray := Act_Rhs;
588
589 if Is_Private_Type (Etype (Rarray)) then
590 Rarray :=
591 Unchecked_Convert_To
592 (Underlying_Type (Etype (Rarray)), Rarray);
593 end if;
594 end if;
595
596 -- If both sides are slices, we must figure out whether it is safe
597 -- to do the move in one direction or the other. It is always safe
598 -- if there is a change of representation since obviously two arrays
599 -- with different representations cannot possibly overlap.
600
601 if (not Crep) and L_Slice and R_Slice then
602 Act_L_Array := Get_Referenced_Object (Prefix (Act_Lhs));
603 Act_R_Array := Get_Referenced_Object (Prefix (Act_Rhs));
604
605 -- If both left and right hand arrays are entity names, and refer
606 -- to different entities, then we know that the move is safe (the
607 -- two storage areas are completely disjoint).
608
609 if Is_Entity_Name (Act_L_Array)
610 and then Is_Entity_Name (Act_R_Array)
611 and then Entity (Act_L_Array) /= Entity (Act_R_Array)
612 then
613 null;
614
615 -- Otherwise, we assume the worst, which is that the two arrays
616 -- are the same array. There is no need to check if we know that
617 -- is the case, because if we don't know it, we still have to
618 -- assume it!
619
620 -- Generally if the same array is involved, then we have an
621 -- overlapping case. We will have to really assume the worst (i.e.
622 -- set neither of the OK flags) unless we can determine the lower
623 -- or upper bounds at compile time and compare them.
624
625 else
626 Cresult :=
627 Compile_Time_Compare
628 (Left_Lo, Right_Lo, Assume_Valid => True);
629
630 if Cresult = Unknown then
631 Cresult :=
632 Compile_Time_Compare
633 (Left_Hi, Right_Hi, Assume_Valid => True);
634 end if;
635
636 case Cresult is
637 when LT | LE | EQ => Set_Backwards_OK (N, False);
638 when GT | GE => Set_Forwards_OK (N, False);
639 when NE | Unknown => Set_Backwards_OK (N, False);
640 Set_Forwards_OK (N, False);
641 end case;
642 end if;
643 end if;
644
645 -- If after that analysis Loop_Required is False, meaning that we
646 -- have not discovered some non-overlap reason for requiring a loop,
647 -- then the outcome depends on the capabilities of the back end.
648
649 if not Loop_Required then
650
651 -- The GCC back end can deal with all cases of overlap by falling
652 -- back to memmove if it cannot use a more efficient approach.
653
654 if VM_Target = No_VM and not AAMP_On_Target then
655 return;
656
657 -- Assume other back ends can handle it if Forwards_OK is set
658
659 elsif Forwards_OK (N) then
660 return;
661
662 -- If Forwards_OK is not set, the back end will need something
663 -- like memmove to handle the move. For now, this processing is
664 -- activated using the .s debug flag (-gnatd.s).
665
666 elsif Debug_Flag_Dot_S then
667 return;
668 end if;
669 end if;
670
671 -- At this stage we have to generate an explicit loop, and we have
672 -- the following cases:
673
674 -- Forwards_OK = True
675
676 -- Rnn : right_index := right_index'First;
677 -- for Lnn in left-index loop
678 -- left (Lnn) := right (Rnn);
679 -- Rnn := right_index'Succ (Rnn);
680 -- end loop;
681
682 -- Note: the above code MUST be analyzed with checks off, because
683 -- otherwise the Succ could overflow. But in any case this is more
684 -- efficient!
685
686 -- Forwards_OK = False, Backwards_OK = True
687
688 -- Rnn : right_index := right_index'Last;
689 -- for Lnn in reverse left-index loop
690 -- left (Lnn) := right (Rnn);
691 -- Rnn := right_index'Pred (Rnn);
692 -- end loop;
693
694 -- Note: the above code MUST be analyzed with checks off, because
695 -- otherwise the Pred could overflow. But in any case this is more
696 -- efficient!
697
698 -- Forwards_OK = Backwards_OK = False
699
700 -- This only happens if we have the same array on each side. It is
701 -- possible to create situations using overlays that violate this,
702 -- but we simply do not promise to get this "right" in this case.
703
704 -- There are two possible subcases. If the No_Implicit_Conditionals
705 -- restriction is set, then we generate the following code:
706
707 -- declare
708 -- T : constant <operand-type> := rhs;
709 -- begin
710 -- lhs := T;
711 -- end;
712
713 -- If implicit conditionals are permitted, then we generate:
714
715 -- if Left_Lo <= Right_Lo then
716 -- <code for Forwards_OK = True above>
717 -- else
718 -- <code for Backwards_OK = True above>
719 -- end if;
720
721 -- In order to detect possible aliasing, we examine the renamed
722 -- expression when the source or target is a renaming. However,
723 -- the renaming may be intended to capture an address that may be
724 -- affected by subsequent code, and therefore we must recover
725 -- the actual entity for the expansion that follows, not the
726 -- object it renames. In particular, if source or target designate
727 -- a portion of a dynamically allocated object, the pointer to it
728 -- may be reassigned but the renaming preserves the proper location.
729
730 if Is_Entity_Name (Rhs)
731 and then
732 Nkind (Parent (Entity (Rhs))) = N_Object_Renaming_Declaration
733 and then Nkind (Act_Rhs) = N_Slice
734 then
735 Rarray := Rhs;
736 end if;
737
738 if Is_Entity_Name (Lhs)
739 and then
740 Nkind (Parent (Entity (Lhs))) = N_Object_Renaming_Declaration
741 and then Nkind (Act_Lhs) = N_Slice
742 then
743 Larray := Lhs;
744 end if;
745
746 -- Cases where either Forwards_OK or Backwards_OK is true
747
748 if Forwards_OK (N) or else Backwards_OK (N) then
749 if Needs_Finalization (Component_Type (L_Type))
750 and then Base_Type (L_Type) = Base_Type (R_Type)
751 and then Ndim = 1
752 and then not No_Ctrl_Actions (N)
753 then
754 declare
755 Proc : constant Entity_Id :=
756 TSS (Base_Type (L_Type), TSS_Slice_Assign);
757 Actuals : List_Id;
758
759 begin
760 Apply_Dereference (Larray);
761 Apply_Dereference (Rarray);
762 Actuals := New_List (
763 Duplicate_Subexpr (Larray, Name_Req => True),
764 Duplicate_Subexpr (Rarray, Name_Req => True),
765 Duplicate_Subexpr (Left_Lo, Name_Req => True),
766 Duplicate_Subexpr (Left_Hi, Name_Req => True),
767 Duplicate_Subexpr (Right_Lo, Name_Req => True),
768 Duplicate_Subexpr (Right_Hi, Name_Req => True));
769
770 Append_To (Actuals,
771 New_Occurrence_Of (
772 Boolean_Literals (not Forwards_OK (N)), Loc));
773
774 Rewrite (N,
775 Make_Procedure_Call_Statement (Loc,
776 Name => New_Reference_To (Proc, Loc),
777 Parameter_Associations => Actuals));
778 end;
779
780 else
781 Rewrite (N,
782 Expand_Assign_Array_Loop
783 (N, Larray, Rarray, L_Type, R_Type, Ndim,
784 Rev => not Forwards_OK (N)));
785 end if;
786
787 -- Case of both are false with No_Implicit_Conditionals
788
789 elsif Restriction_Active (No_Implicit_Conditionals) then
790 declare
791 T : constant Entity_Id :=
792 Make_Defining_Identifier (Loc, Chars => Name_T);
793
794 begin
795 Rewrite (N,
796 Make_Block_Statement (Loc,
797 Declarations => New_List (
798 Make_Object_Declaration (Loc,
799 Defining_Identifier => T,
800 Constant_Present => True,
801 Object_Definition =>
802 New_Occurrence_Of (Etype (Rhs), Loc),
803 Expression => Relocate_Node (Rhs))),
804
805 Handled_Statement_Sequence =>
806 Make_Handled_Sequence_Of_Statements (Loc,
807 Statements => New_List (
808 Make_Assignment_Statement (Loc,
809 Name => Relocate_Node (Lhs),
810 Expression => New_Occurrence_Of (T, Loc))))));
811 end;
812
813 -- Case of both are false with implicit conditionals allowed
814
815 else
816 -- Before we generate this code, we must ensure that the left and
817 -- right side array types are defined. They may be itypes, and we
818 -- cannot let them be defined inside the if, since the first use
819 -- in the then may not be executed.
820
821 Ensure_Defined (L_Type, N);
822 Ensure_Defined (R_Type, N);
823
824 -- We normally compare addresses to find out which way round to
825 -- do the loop, since this is reliable, and handles the cases of
826 -- parameters, conversions etc. But we can't do that in the bit
827 -- packed case or the VM case, because addresses don't work there.
828
829 if not Is_Bit_Packed_Array (L_Type) and then VM_Target = No_VM then
830 Condition :=
831 Make_Op_Le (Loc,
832 Left_Opnd =>
833 Unchecked_Convert_To (RTE (RE_Integer_Address),
834 Make_Attribute_Reference (Loc,
835 Prefix =>
836 Make_Indexed_Component (Loc,
837 Prefix =>
838 Duplicate_Subexpr_Move_Checks (Larray, True),
839 Expressions => New_List (
840 Make_Attribute_Reference (Loc,
841 Prefix =>
842 New_Reference_To
843 (L_Index_Typ, Loc),
844 Attribute_Name => Name_First))),
845 Attribute_Name => Name_Address)),
846
847 Right_Opnd =>
848 Unchecked_Convert_To (RTE (RE_Integer_Address),
849 Make_Attribute_Reference (Loc,
850 Prefix =>
851 Make_Indexed_Component (Loc,
852 Prefix =>
853 Duplicate_Subexpr_Move_Checks (Rarray, True),
854 Expressions => New_List (
855 Make_Attribute_Reference (Loc,
856 Prefix =>
857 New_Reference_To
858 (R_Index_Typ, Loc),
859 Attribute_Name => Name_First))),
860 Attribute_Name => Name_Address)));
861
862 -- For the bit packed and VM cases we use the bounds. That's OK,
863 -- because we don't have to worry about parameters, since they
864 -- cannot cause overlap. Perhaps we should worry about weird slice
865 -- conversions ???
866
867 else
868 -- Copy the bounds
869
870 Cleft_Lo := New_Copy_Tree (Left_Lo);
871 Cright_Lo := New_Copy_Tree (Right_Lo);
872
873 -- If the types do not match we add an implicit conversion
874 -- here to ensure proper match
875
876 if Etype (Left_Lo) /= Etype (Right_Lo) then
877 Cright_Lo :=
878 Unchecked_Convert_To (Etype (Left_Lo), Cright_Lo);
879 end if;
880
881 -- Reset the Analyzed flag, because the bounds of the index
882 -- type itself may be universal, and must must be reaanalyzed
883 -- to acquire the proper type for the back end.
884
885 Set_Analyzed (Cleft_Lo, False);
886 Set_Analyzed (Cright_Lo, False);
887
888 Condition :=
889 Make_Op_Le (Loc,
890 Left_Opnd => Cleft_Lo,
891 Right_Opnd => Cright_Lo);
892 end if;
893
894 if Needs_Finalization (Component_Type (L_Type))
895 and then Base_Type (L_Type) = Base_Type (R_Type)
896 and then Ndim = 1
897 and then not No_Ctrl_Actions (N)
898 then
899
900 -- Call TSS procedure for array assignment, passing the
901 -- explicit bounds of right and left hand sides.
902
903 declare
904 Proc : constant Entity_Id :=
905 TSS (Base_Type (L_Type), TSS_Slice_Assign);
906 Actuals : List_Id;
907
908 begin
909 Apply_Dereference (Larray);
910 Apply_Dereference (Rarray);
911 Actuals := New_List (
912 Duplicate_Subexpr (Larray, Name_Req => True),
913 Duplicate_Subexpr (Rarray, Name_Req => True),
914 Duplicate_Subexpr (Left_Lo, Name_Req => True),
915 Duplicate_Subexpr (Left_Hi, Name_Req => True),
916 Duplicate_Subexpr (Right_Lo, Name_Req => True),
917 Duplicate_Subexpr (Right_Hi, Name_Req => True));
918
919 Append_To (Actuals,
920 Make_Op_Not (Loc,
921 Right_Opnd => Condition));
922
923 Rewrite (N,
924 Make_Procedure_Call_Statement (Loc,
925 Name => New_Reference_To (Proc, Loc),
926 Parameter_Associations => Actuals));
927 end;
928
929 else
930 Rewrite (N,
931 Make_Implicit_If_Statement (N,
932 Condition => Condition,
933
934 Then_Statements => New_List (
935 Expand_Assign_Array_Loop
936 (N, Larray, Rarray, L_Type, R_Type, Ndim,
937 Rev => False)),
938
939 Else_Statements => New_List (
940 Expand_Assign_Array_Loop
941 (N, Larray, Rarray, L_Type, R_Type, Ndim,
942 Rev => True))));
943 end if;
944 end if;
945
946 Analyze (N, Suppress => All_Checks);
947 end;
948
949 exception
950 when RE_Not_Available =>
951 return;
952 end Expand_Assign_Array;
953
954 ------------------------------
955 -- Expand_Assign_Array_Loop --
956 ------------------------------
957
958 -- The following is an example of the loop generated for the case of a
959 -- two-dimensional array:
960
961 -- declare
962 -- R2b : Tm1X1 := 1;
963 -- begin
964 -- for L1b in 1 .. 100 loop
965 -- declare
966 -- R4b : Tm1X2 := 1;
967 -- begin
968 -- for L3b in 1 .. 100 loop
969 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
970 -- R4b := Tm1X2'succ(R4b);
971 -- end loop;
972 -- end;
973 -- R2b := Tm1X1'succ(R2b);
974 -- end loop;
975 -- end;
976
977 -- Here Rev is False, and Tm1Xn are the subscript types for the right hand
978 -- side. The declarations of R2b and R4b are inserted before the original
979 -- assignment statement.
980
981 function Expand_Assign_Array_Loop
982 (N : Node_Id;
983 Larray : Entity_Id;
984 Rarray : Entity_Id;
985 L_Type : Entity_Id;
986 R_Type : Entity_Id;
987 Ndim : Pos;
988 Rev : Boolean) return Node_Id
989 is
990 Loc : constant Source_Ptr := Sloc (N);
991
992 Lnn : array (1 .. Ndim) of Entity_Id;
993 Rnn : array (1 .. Ndim) of Entity_Id;
994 -- Entities used as subscripts on left and right sides
995
996 L_Index_Type : array (1 .. Ndim) of Entity_Id;
997 R_Index_Type : array (1 .. Ndim) of Entity_Id;
998 -- Left and right index types
999
1000 Assign : Node_Id;
1001
1002 F_Or_L : Name_Id;
1003 S_Or_P : Name_Id;
1004
1005 begin
1006 if Rev then
1007 F_Or_L := Name_Last;
1008 S_Or_P := Name_Pred;
1009 else
1010 F_Or_L := Name_First;
1011 S_Or_P := Name_Succ;
1012 end if;
1013
1014 -- Setup index types and subscript entities
1015
1016 declare
1017 L_Index : Node_Id;
1018 R_Index : Node_Id;
1019
1020 begin
1021 L_Index := First_Index (L_Type);
1022 R_Index := First_Index (R_Type);
1023
1024 for J in 1 .. Ndim loop
1025 Lnn (J) :=
1026 Make_Defining_Identifier (Loc,
1027 Chars => New_Internal_Name ('L'));
1028
1029 Rnn (J) :=
1030 Make_Defining_Identifier (Loc,
1031 Chars => New_Internal_Name ('R'));
1032
1033 L_Index_Type (J) := Etype (L_Index);
1034 R_Index_Type (J) := Etype (R_Index);
1035
1036 Next_Index (L_Index);
1037 Next_Index (R_Index);
1038 end loop;
1039 end;
1040
1041 -- Now construct the assignment statement
1042
1043 declare
1044 ExprL : constant List_Id := New_List;
1045 ExprR : constant List_Id := New_List;
1046
1047 begin
1048 for J in 1 .. Ndim loop
1049 Append_To (ExprL, New_Occurrence_Of (Lnn (J), Loc));
1050 Append_To (ExprR, New_Occurrence_Of (Rnn (J), Loc));
1051 end loop;
1052
1053 Assign :=
1054 Make_Assignment_Statement (Loc,
1055 Name =>
1056 Make_Indexed_Component (Loc,
1057 Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
1058 Expressions => ExprL),
1059 Expression =>
1060 Make_Indexed_Component (Loc,
1061 Prefix => Duplicate_Subexpr (Rarray, Name_Req => True),
1062 Expressions => ExprR));
1063
1064 -- We set assignment OK, since there are some cases, e.g. in object
1065 -- declarations, where we are actually assigning into a constant.
1066 -- If there really is an illegality, it was caught long before now,
1067 -- and was flagged when the original assignment was analyzed.
1068
1069 Set_Assignment_OK (Name (Assign));
1070
1071 -- Propagate the No_Ctrl_Actions flag to individual assignments
1072
1073 Set_No_Ctrl_Actions (Assign, No_Ctrl_Actions (N));
1074 end;
1075
1076 -- Now construct the loop from the inside out, with the last subscript
1077 -- varying most rapidly. Note that Assign is first the raw assignment
1078 -- statement, and then subsequently the loop that wraps it up.
1079
1080 for J in reverse 1 .. Ndim loop
1081 Assign :=
1082 Make_Block_Statement (Loc,
1083 Declarations => New_List (
1084 Make_Object_Declaration (Loc,
1085 Defining_Identifier => Rnn (J),
1086 Object_Definition =>
1087 New_Occurrence_Of (R_Index_Type (J), Loc),
1088 Expression =>
1089 Make_Attribute_Reference (Loc,
1090 Prefix => New_Occurrence_Of (R_Index_Type (J), Loc),
1091 Attribute_Name => F_Or_L))),
1092
1093 Handled_Statement_Sequence =>
1094 Make_Handled_Sequence_Of_Statements (Loc,
1095 Statements => New_List (
1096 Make_Implicit_Loop_Statement (N,
1097 Iteration_Scheme =>
1098 Make_Iteration_Scheme (Loc,
1099 Loop_Parameter_Specification =>
1100 Make_Loop_Parameter_Specification (Loc,
1101 Defining_Identifier => Lnn (J),
1102 Reverse_Present => Rev,
1103 Discrete_Subtype_Definition =>
1104 New_Reference_To (L_Index_Type (J), Loc))),
1105
1106 Statements => New_List (
1107 Assign,
1108
1109 Make_Assignment_Statement (Loc,
1110 Name => New_Occurrence_Of (Rnn (J), Loc),
1111 Expression =>
1112 Make_Attribute_Reference (Loc,
1113 Prefix =>
1114 New_Occurrence_Of (R_Index_Type (J), Loc),
1115 Attribute_Name => S_Or_P,
1116 Expressions => New_List (
1117 New_Occurrence_Of (Rnn (J), Loc)))))))));
1118 end loop;
1119
1120 return Assign;
1121 end Expand_Assign_Array_Loop;
1122
1123 --------------------------
1124 -- Expand_Assign_Record --
1125 --------------------------
1126
1127 -- The only processing required is in the change of representation case,
1128 -- where we must expand the assignment to a series of field by field
1129 -- assignments.
1130
1131 procedure Expand_Assign_Record (N : Node_Id) is
1132 Lhs : constant Node_Id := Name (N);
1133 Rhs : Node_Id := Expression (N);
1134
1135 begin
1136 -- If change of representation, then extract the real right hand side
1137 -- from the type conversion, and proceed with component-wise assignment,
1138 -- since the two types are not the same as far as the back end is
1139 -- concerned.
1140
1141 if Change_Of_Representation (N) then
1142 Rhs := Expression (Rhs);
1143
1144 -- If this may be a case of a large bit aligned component, then proceed
1145 -- with component-wise assignment, to avoid possible clobbering of other
1146 -- components sharing bits in the first or last byte of the component to
1147 -- be assigned.
1148
1149 elsif Possible_Bit_Aligned_Component (Lhs)
1150 or
1151 Possible_Bit_Aligned_Component (Rhs)
1152 then
1153 null;
1154
1155 -- If neither condition met, then nothing special to do, the back end
1156 -- can handle assignment of the entire component as a single entity.
1157
1158 else
1159 return;
1160 end if;
1161
1162 -- At this stage we know that we must do a component wise assignment
1163
1164 declare
1165 Loc : constant Source_Ptr := Sloc (N);
1166 R_Typ : constant Entity_Id := Base_Type (Etype (Rhs));
1167 L_Typ : constant Entity_Id := Base_Type (Etype (Lhs));
1168 Decl : constant Node_Id := Declaration_Node (R_Typ);
1169 RDef : Node_Id;
1170 F : Entity_Id;
1171
1172 function Find_Component
1173 (Typ : Entity_Id;
1174 Comp : Entity_Id) return Entity_Id;
1175 -- Find the component with the given name in the underlying record
1176 -- declaration for Typ. We need to use the actual entity because the
1177 -- type may be private and resolution by identifier alone would fail.
1178
1179 function Make_Component_List_Assign
1180 (CL : Node_Id;
1181 U_U : Boolean := False) return List_Id;
1182 -- Returns a sequence of statements to assign the components that
1183 -- are referenced in the given component list. The flag U_U is
1184 -- used to force the usage of the inferred value of the variant
1185 -- part expression as the switch for the generated case statement.
1186
1187 function Make_Field_Assign
1188 (C : Entity_Id;
1189 U_U : Boolean := False) return Node_Id;
1190 -- Given C, the entity for a discriminant or component, build an
1191 -- assignment for the corresponding field values. The flag U_U
1192 -- signals the presence of an Unchecked_Union and forces the usage
1193 -- of the inferred discriminant value of C as the right hand side
1194 -- of the assignment.
1195
1196 function Make_Field_Assigns (CI : List_Id) return List_Id;
1197 -- Given CI, a component items list, construct series of statements
1198 -- for fieldwise assignment of the corresponding components.
1199
1200 --------------------
1201 -- Find_Component --
1202 --------------------
1203
1204 function Find_Component
1205 (Typ : Entity_Id;
1206 Comp : Entity_Id) return Entity_Id
1207 is
1208 Utyp : constant Entity_Id := Underlying_Type (Typ);
1209 C : Entity_Id;
1210
1211 begin
1212 C := First_Entity (Utyp);
1213
1214 while Present (C) loop
1215 if Chars (C) = Chars (Comp) then
1216 return C;
1217 end if;
1218 Next_Entity (C);
1219 end loop;
1220
1221 raise Program_Error;
1222 end Find_Component;
1223
1224 --------------------------------
1225 -- Make_Component_List_Assign --
1226 --------------------------------
1227
1228 function Make_Component_List_Assign
1229 (CL : Node_Id;
1230 U_U : Boolean := False) return List_Id
1231 is
1232 CI : constant List_Id := Component_Items (CL);
1233 VP : constant Node_Id := Variant_Part (CL);
1234
1235 Alts : List_Id;
1236 DC : Node_Id;
1237 DCH : List_Id;
1238 Expr : Node_Id;
1239 Result : List_Id;
1240 V : Node_Id;
1241
1242 begin
1243 Result := Make_Field_Assigns (CI);
1244
1245 if Present (VP) then
1246
1247 V := First_Non_Pragma (Variants (VP));
1248 Alts := New_List;
1249 while Present (V) loop
1250
1251 DCH := New_List;
1252 DC := First (Discrete_Choices (V));
1253 while Present (DC) loop
1254 Append_To (DCH, New_Copy_Tree (DC));
1255 Next (DC);
1256 end loop;
1257
1258 Append_To (Alts,
1259 Make_Case_Statement_Alternative (Loc,
1260 Discrete_Choices => DCH,
1261 Statements =>
1262 Make_Component_List_Assign (Component_List (V))));
1263 Next_Non_Pragma (V);
1264 end loop;
1265
1266 -- If we have an Unchecked_Union, use the value of the inferred
1267 -- discriminant of the variant part expression as the switch
1268 -- for the case statement. The case statement may later be
1269 -- folded.
1270
1271 if U_U then
1272 Expr :=
1273 New_Copy (Get_Discriminant_Value (
1274 Entity (Name (VP)),
1275 Etype (Rhs),
1276 Discriminant_Constraint (Etype (Rhs))));
1277 else
1278 Expr :=
1279 Make_Selected_Component (Loc,
1280 Prefix => Duplicate_Subexpr (Rhs),
1281 Selector_Name =>
1282 Make_Identifier (Loc, Chars (Name (VP))));
1283 end if;
1284
1285 Append_To (Result,
1286 Make_Case_Statement (Loc,
1287 Expression => Expr,
1288 Alternatives => Alts));
1289 end if;
1290
1291 return Result;
1292 end Make_Component_List_Assign;
1293
1294 -----------------------
1295 -- Make_Field_Assign --
1296 -----------------------
1297
1298 function Make_Field_Assign
1299 (C : Entity_Id;
1300 U_U : Boolean := False) return Node_Id
1301 is
1302 A : Node_Id;
1303 Expr : Node_Id;
1304
1305 begin
1306 -- In the case of an Unchecked_Union, use the discriminant
1307 -- constraint value as on the right hand side of the assignment.
1308
1309 if U_U then
1310 Expr :=
1311 New_Copy (Get_Discriminant_Value (C,
1312 Etype (Rhs),
1313 Discriminant_Constraint (Etype (Rhs))));
1314 else
1315 Expr :=
1316 Make_Selected_Component (Loc,
1317 Prefix => Duplicate_Subexpr (Rhs),
1318 Selector_Name => New_Occurrence_Of (C, Loc));
1319 end if;
1320
1321 A :=
1322 Make_Assignment_Statement (Loc,
1323 Name =>
1324 Make_Selected_Component (Loc,
1325 Prefix => Duplicate_Subexpr (Lhs),
1326 Selector_Name =>
1327 New_Occurrence_Of (Find_Component (L_Typ, C), Loc)),
1328 Expression => Expr);
1329
1330 -- Set Assignment_OK, so discriminants can be assigned
1331
1332 Set_Assignment_OK (Name (A), True);
1333 return A;
1334 end Make_Field_Assign;
1335
1336 ------------------------
1337 -- Make_Field_Assigns --
1338 ------------------------
1339
1340 function Make_Field_Assigns (CI : List_Id) return List_Id is
1341 Item : Node_Id;
1342 Result : List_Id;
1343
1344 begin
1345 Item := First (CI);
1346 Result := New_List;
1347 while Present (Item) loop
1348 if Nkind (Item) = N_Component_Declaration then
1349 Append_To
1350 (Result, Make_Field_Assign (Defining_Identifier (Item)));
1351 end if;
1352
1353 Next (Item);
1354 end loop;
1355
1356 return Result;
1357 end Make_Field_Assigns;
1358
1359 -- Start of processing for Expand_Assign_Record
1360
1361 begin
1362 -- Note that we use the base types for this processing. This results
1363 -- in some extra work in the constrained case, but the change of
1364 -- representation case is so unusual that it is not worth the effort.
1365
1366 -- First copy the discriminants. This is done unconditionally. It
1367 -- is required in the unconstrained left side case, and also in the
1368 -- case where this assignment was constructed during the expansion
1369 -- of a type conversion (since initialization of discriminants is
1370 -- suppressed in this case). It is unnecessary but harmless in
1371 -- other cases.
1372
1373 if Has_Discriminants (L_Typ) then
1374 F := First_Discriminant (R_Typ);
1375 while Present (F) loop
1376
1377 -- If we are expanding the initialization of a derived record
1378 -- that constrains or renames discriminants of the parent, we
1379 -- must use the corresponding discriminant in the parent.
1380
1381 declare
1382 CF : Entity_Id;
1383
1384 begin
1385 if Inside_Init_Proc
1386 and then Present (Corresponding_Discriminant (F))
1387 then
1388 CF := Corresponding_Discriminant (F);
1389 else
1390 CF := F;
1391 end if;
1392
1393 if Is_Unchecked_Union (Base_Type (R_Typ)) then
1394 Insert_Action (N, Make_Field_Assign (CF, True));
1395 else
1396 Insert_Action (N, Make_Field_Assign (CF));
1397 end if;
1398
1399 Next_Discriminant (F);
1400 end;
1401 end loop;
1402 end if;
1403
1404 -- We know the underlying type is a record, but its current view
1405 -- may be private. We must retrieve the usable record declaration.
1406
1407 if Nkind (Decl) = N_Private_Type_Declaration
1408 and then Present (Full_View (R_Typ))
1409 then
1410 RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
1411 else
1412 RDef := Type_Definition (Decl);
1413 end if;
1414
1415 if Nkind (RDef) = N_Record_Definition
1416 and then Present (Component_List (RDef))
1417 then
1418
1419 if Is_Unchecked_Union (R_Typ) then
1420 Insert_Actions (N,
1421 Make_Component_List_Assign (Component_List (RDef), True));
1422 else
1423 Insert_Actions
1424 (N, Make_Component_List_Assign (Component_List (RDef)));
1425 end if;
1426
1427 Rewrite (N, Make_Null_Statement (Loc));
1428 end if;
1429
1430 end;
1431 end Expand_Assign_Record;
1432
1433 -----------------------------------
1434 -- Expand_N_Assignment_Statement --
1435 -----------------------------------
1436
1437 -- This procedure implements various cases where an assignment statement
1438 -- cannot just be passed on to the back end in untransformed state.
1439
1440 procedure Expand_N_Assignment_Statement (N : Node_Id) is
1441 Loc : constant Source_Ptr := Sloc (N);
1442 Lhs : constant Node_Id := Name (N);
1443 Rhs : constant Node_Id := Expression (N);
1444 Typ : constant Entity_Id := Underlying_Type (Etype (Lhs));
1445 Exp : Node_Id;
1446
1447 begin
1448 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1449
1450 -- Rewrite an assignment to X'Priority into a run-time call
1451
1452 -- For example: X'Priority := New_Prio_Expr;
1453 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1454
1455 -- Note that although X'Priority is notionally an object, it is quite
1456 -- deliberately not defined as an aliased object in the RM. This means
1457 -- that it works fine to rewrite it as a call, without having to worry
1458 -- about complications that would other arise from X'Priority'Access,
1459 -- which is illegal, because of the lack of aliasing.
1460
1461 if Ada_Version >= Ada_05 then
1462 declare
1463 Call : Node_Id;
1464 Conctyp : Entity_Id;
1465 Ent : Entity_Id;
1466 Subprg : Entity_Id;
1467 RT_Subprg_Name : Node_Id;
1468
1469 begin
1470 -- Handle chains of renamings
1471
1472 Ent := Name (N);
1473 while Nkind (Ent) in N_Has_Entity
1474 and then Present (Entity (Ent))
1475 and then Present (Renamed_Object (Entity (Ent)))
1476 loop
1477 Ent := Renamed_Object (Entity (Ent));
1478 end loop;
1479
1480 -- The attribute Priority applied to protected objects has been
1481 -- previously expanded into a call to the Get_Ceiling run-time
1482 -- subprogram.
1483
1484 if Nkind (Ent) = N_Function_Call
1485 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
1486 or else
1487 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling))
1488 then
1489 -- Look for the enclosing concurrent type
1490
1491 Conctyp := Current_Scope;
1492 while not Is_Concurrent_Type (Conctyp) loop
1493 Conctyp := Scope (Conctyp);
1494 end loop;
1495
1496 pragma Assert (Is_Protected_Type (Conctyp));
1497
1498 -- Generate the first actual of the call
1499
1500 Subprg := Current_Scope;
1501 while not Present (Protected_Body_Subprogram (Subprg)) loop
1502 Subprg := Scope (Subprg);
1503 end loop;
1504
1505 -- Select the appropriate run-time call
1506
1507 if Number_Entries (Conctyp) = 0 then
1508 RT_Subprg_Name :=
1509 New_Reference_To (RTE (RE_Set_Ceiling), Loc);
1510 else
1511 RT_Subprg_Name :=
1512 New_Reference_To (RTE (RO_PE_Set_Ceiling), Loc);
1513 end if;
1514
1515 Call :=
1516 Make_Procedure_Call_Statement (Loc,
1517 Name => RT_Subprg_Name,
1518 Parameter_Associations => New_List (
1519 New_Copy_Tree (First (Parameter_Associations (Ent))),
1520 Relocate_Node (Expression (N))));
1521
1522 Rewrite (N, Call);
1523 Analyze (N);
1524 return;
1525 end if;
1526 end;
1527 end if;
1528
1529 -- First deal with generation of range check if required. For now we do
1530 -- this only for discrete types.
1531
1532 if Do_Range_Check (Rhs)
1533 and then Is_Discrete_Type (Typ)
1534 then
1535 Set_Do_Range_Check (Rhs, False);
1536 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
1537 end if;
1538
1539 -- Check for a special case where a high level transformation is
1540 -- required. If we have either of:
1541
1542 -- P.field := rhs;
1543 -- P (sub) := rhs;
1544
1545 -- where P is a reference to a bit packed array, then we have to unwind
1546 -- the assignment. The exact meaning of being a reference to a bit
1547 -- packed array is as follows:
1548
1549 -- An indexed component whose prefix is a bit packed array is a
1550 -- reference to a bit packed array.
1551
1552 -- An indexed component or selected component whose prefix is a
1553 -- reference to a bit packed array is itself a reference ot a
1554 -- bit packed array.
1555
1556 -- The required transformation is
1557
1558 -- Tnn : prefix_type := P;
1559 -- Tnn.field := rhs;
1560 -- P := Tnn;
1561
1562 -- or
1563
1564 -- Tnn : prefix_type := P;
1565 -- Tnn (subscr) := rhs;
1566 -- P := Tnn;
1567
1568 -- Since P is going to be evaluated more than once, any subscripts
1569 -- in P must have their evaluation forced.
1570
1571 if Nkind_In (Lhs, N_Indexed_Component, N_Selected_Component)
1572 and then Is_Ref_To_Bit_Packed_Array (Prefix (Lhs))
1573 then
1574 declare
1575 BPAR_Expr : constant Node_Id := Relocate_Node (Prefix (Lhs));
1576 BPAR_Typ : constant Entity_Id := Etype (BPAR_Expr);
1577 Tnn : constant Entity_Id :=
1578 Make_Defining_Identifier (Loc,
1579 Chars => New_Internal_Name ('T'));
1580
1581 begin
1582 -- Insert the post assignment first, because we want to copy the
1583 -- BPAR_Expr tree before it gets analyzed in the context of the
1584 -- pre assignment. Note that we do not analyze the post assignment
1585 -- yet (we cannot till we have completed the analysis of the pre
1586 -- assignment). As usual, the analysis of this post assignment
1587 -- will happen on its own when we "run into" it after finishing
1588 -- the current assignment.
1589
1590 Insert_After (N,
1591 Make_Assignment_Statement (Loc,
1592 Name => New_Copy_Tree (BPAR_Expr),
1593 Expression => New_Occurrence_Of (Tnn, Loc)));
1594
1595 -- At this stage BPAR_Expr is a reference to a bit packed array
1596 -- where the reference was not expanded in the original tree,
1597 -- since it was on the left side of an assignment. But in the
1598 -- pre-assignment statement (the object definition), BPAR_Expr
1599 -- will end up on the right hand side, and must be reexpanded. To
1600 -- achieve this, we reset the analyzed flag of all selected and
1601 -- indexed components down to the actual indexed component for
1602 -- the packed array.
1603
1604 Exp := BPAR_Expr;
1605 loop
1606 Set_Analyzed (Exp, False);
1607
1608 if Nkind_In
1609 (Exp, N_Selected_Component, N_Indexed_Component)
1610 then
1611 Exp := Prefix (Exp);
1612 else
1613 exit;
1614 end if;
1615 end loop;
1616
1617 -- Now we can insert and analyze the pre-assignment
1618
1619 -- If the right-hand side requires a transient scope, it has
1620 -- already been placed on the stack. However, the declaration is
1621 -- inserted in the tree outside of this scope, and must reflect
1622 -- the proper scope for its variable. This awkward bit is forced
1623 -- by the stricter scope discipline imposed by GCC 2.97.
1624
1625 declare
1626 Uses_Transient_Scope : constant Boolean :=
1627 Scope_Is_Transient
1628 and then N = Node_To_Be_Wrapped;
1629
1630 begin
1631 if Uses_Transient_Scope then
1632 Push_Scope (Scope (Current_Scope));
1633 end if;
1634
1635 Insert_Before_And_Analyze (N,
1636 Make_Object_Declaration (Loc,
1637 Defining_Identifier => Tnn,
1638 Object_Definition => New_Occurrence_Of (BPAR_Typ, Loc),
1639 Expression => BPAR_Expr));
1640
1641 if Uses_Transient_Scope then
1642 Pop_Scope;
1643 end if;
1644 end;
1645
1646 -- Now fix up the original assignment and continue processing
1647
1648 Rewrite (Prefix (Lhs),
1649 New_Occurrence_Of (Tnn, Loc));
1650
1651 -- We do not need to reanalyze that assignment, and we do not need
1652 -- to worry about references to the temporary, but we do need to
1653 -- make sure that the temporary is not marked as a true constant
1654 -- since we now have a generated assignment to it!
1655
1656 Set_Is_True_Constant (Tnn, False);
1657 end;
1658 end if;
1659
1660 -- When we have the appropriate type of aggregate in the expression (it
1661 -- has been determined during analysis of the aggregate by setting the
1662 -- delay flag), let's perform in place assignment and thus avoid
1663 -- creating a temporary.
1664
1665 if Is_Delayed_Aggregate (Rhs) then
1666 Convert_Aggr_In_Assignment (N);
1667 Rewrite (N, Make_Null_Statement (Loc));
1668 Analyze (N);
1669 return;
1670 end if;
1671
1672 -- Apply discriminant check if required. If Lhs is an access type to a
1673 -- designated type with discriminants, we must always check.
1674
1675 if Has_Discriminants (Etype (Lhs)) then
1676
1677 -- Skip discriminant check if change of representation. Will be
1678 -- done when the change of representation is expanded out.
1679
1680 if not Change_Of_Representation (N) then
1681 Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
1682 end if;
1683
1684 -- If the type is private without discriminants, and the full type
1685 -- has discriminants (necessarily with defaults) a check may still be
1686 -- necessary if the Lhs is aliased. The private determinants must be
1687 -- visible to build the discriminant constraints.
1688
1689 -- Only an explicit dereference that comes from source indicates
1690 -- aliasing. Access to formals of protected operations and entries
1691 -- create dereferences but are not semantic aliasings.
1692
1693 elsif Is_Private_Type (Etype (Lhs))
1694 and then Has_Discriminants (Typ)
1695 and then Nkind (Lhs) = N_Explicit_Dereference
1696 and then Comes_From_Source (Lhs)
1697 then
1698 declare
1699 Lt : constant Entity_Id := Etype (Lhs);
1700 begin
1701 Set_Etype (Lhs, Typ);
1702 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1703 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1704 Set_Etype (Lhs, Lt);
1705 end;
1706
1707 -- If the Lhs has a private type with unknown discriminants, it
1708 -- may have a full view with discriminants, but those are nameable
1709 -- only in the underlying type, so convert the Rhs to it before
1710 -- potential checking.
1711
1712 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs)))
1713 and then Has_Discriminants (Typ)
1714 then
1715 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1716 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1717
1718 -- In the access type case, we need the same discriminant check, and
1719 -- also range checks if we have an access to constrained array.
1720
1721 elsif Is_Access_Type (Etype (Lhs))
1722 and then Is_Constrained (Designated_Type (Etype (Lhs)))
1723 then
1724 if Has_Discriminants (Designated_Type (Etype (Lhs))) then
1725
1726 -- Skip discriminant check if change of representation. Will be
1727 -- done when the change of representation is expanded out.
1728
1729 if not Change_Of_Representation (N) then
1730 Apply_Discriminant_Check (Rhs, Etype (Lhs));
1731 end if;
1732
1733 elsif Is_Array_Type (Designated_Type (Etype (Lhs))) then
1734 Apply_Range_Check (Rhs, Etype (Lhs));
1735
1736 if Is_Constrained (Etype (Lhs)) then
1737 Apply_Length_Check (Rhs, Etype (Lhs));
1738 end if;
1739
1740 if Nkind (Rhs) = N_Allocator then
1741 declare
1742 Target_Typ : constant Entity_Id := Etype (Expression (Rhs));
1743 C_Es : Check_Result;
1744
1745 begin
1746 C_Es :=
1747 Get_Range_Checks
1748 (Lhs,
1749 Target_Typ,
1750 Etype (Designated_Type (Etype (Lhs))));
1751
1752 Insert_Range_Checks
1753 (C_Es,
1754 N,
1755 Target_Typ,
1756 Sloc (Lhs),
1757 Lhs);
1758 end;
1759 end if;
1760 end if;
1761
1762 -- Apply range check for access type case
1763
1764 elsif Is_Access_Type (Etype (Lhs))
1765 and then Nkind (Rhs) = N_Allocator
1766 and then Nkind (Expression (Rhs)) = N_Qualified_Expression
1767 then
1768 Analyze_And_Resolve (Expression (Rhs));
1769 Apply_Range_Check
1770 (Expression (Rhs), Designated_Type (Etype (Lhs)));
1771 end if;
1772
1773 -- Ada 2005 (AI-231): Generate the run-time check
1774
1775 if Is_Access_Type (Typ)
1776 and then Can_Never_Be_Null (Etype (Lhs))
1777 and then not Can_Never_Be_Null (Etype (Rhs))
1778 then
1779 Apply_Constraint_Check (Rhs, Etype (Lhs));
1780 end if;
1781
1782 -- Case of assignment to a bit packed array element
1783
1784 if Nkind (Lhs) = N_Indexed_Component
1785 and then Is_Bit_Packed_Array (Etype (Prefix (Lhs)))
1786 then
1787 Expand_Bit_Packed_Element_Set (N);
1788 return;
1789
1790 -- Build-in-place function call case. Note that we're not yet doing
1791 -- build-in-place for user-written assignment statements (the assignment
1792 -- here came from an aggregate.)
1793
1794 elsif Ada_Version >= Ada_05
1795 and then Is_Build_In_Place_Function_Call (Rhs)
1796 then
1797 Make_Build_In_Place_Call_In_Assignment (N, Rhs);
1798
1799 elsif Is_Tagged_Type (Typ) and then Is_Value_Type (Etype (Lhs)) then
1800
1801 -- Nothing to do for valuetypes
1802 -- ??? Set_Scope_Is_Transient (False);
1803
1804 return;
1805
1806 elsif Is_Tagged_Type (Typ)
1807 or else (Needs_Finalization (Typ) and then not Is_Array_Type (Typ))
1808 then
1809 Tagged_Case : declare
1810 L : List_Id := No_List;
1811 Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
1812
1813 begin
1814 -- In the controlled case, we need to make sure that function
1815 -- calls are evaluated before finalizing the target. In all cases,
1816 -- it makes the expansion easier if the side-effects are removed
1817 -- first.
1818
1819 Remove_Side_Effects (Lhs);
1820 Remove_Side_Effects (Rhs);
1821
1822 -- Avoid recursion in the mechanism
1823
1824 Set_Analyzed (N);
1825
1826 -- If dispatching assignment, we need to dispatch to _assign
1827
1828 if Is_Class_Wide_Type (Typ)
1829
1830 -- If the type is tagged, we may as well use the predefined
1831 -- primitive assignment. This avoids inlining a lot of code
1832 -- and in the class-wide case, the assignment is replaced by
1833 -- dispatch call to _assign. Note that this cannot be done when
1834 -- discriminant checks are locally suppressed (as in extension
1835 -- aggregate expansions) because otherwise the discriminant
1836 -- check will be performed within the _assign call. It is also
1837 -- suppressed for assignments created by the expander that
1838 -- correspond to initializations, where we do want to copy the
1839 -- tag (No_Ctrl_Actions flag set True) by the expander and we
1840 -- do not need to mess with tags ever (Expand_Ctrl_Actions flag
1841 -- is set True in this case).
1842
1843 or else (Is_Tagged_Type (Typ)
1844 and then not Is_Value_Type (Etype (Lhs))
1845 and then Chars (Current_Scope) /= Name_uAssign
1846 and then Expand_Ctrl_Actions
1847 and then not Discriminant_Checks_Suppressed (Empty))
1848 then
1849 -- Fetch the primitive op _assign and proper type to call it.
1850 -- Because of possible conflicts between private and full view
1851 -- the proper type is fetched directly from the operation
1852 -- profile.
1853
1854 declare
1855 Op : constant Entity_Id :=
1856 Find_Prim_Op (Typ, Name_uAssign);
1857 F_Typ : Entity_Id := Etype (First_Formal (Op));
1858
1859 begin
1860 -- If the assignment is dispatching, make sure to use the
1861 -- proper type.
1862
1863 if Is_Class_Wide_Type (Typ) then
1864 F_Typ := Class_Wide_Type (F_Typ);
1865 end if;
1866
1867 L := New_List;
1868
1869 -- In case of assignment to a class-wide tagged type, before
1870 -- the assignment we generate run-time check to ensure that
1871 -- the tags of source and target match.
1872
1873 if Is_Class_Wide_Type (Typ)
1874 and then Is_Tagged_Type (Typ)
1875 and then Is_Tagged_Type (Underlying_Type (Etype (Rhs)))
1876 then
1877 Append_To (L,
1878 Make_Raise_Constraint_Error (Loc,
1879 Condition =>
1880 Make_Op_Ne (Loc,
1881 Left_Opnd =>
1882 Make_Selected_Component (Loc,
1883 Prefix => Duplicate_Subexpr (Lhs),
1884 Selector_Name =>
1885 Make_Identifier (Loc,
1886 Chars => Name_uTag)),
1887 Right_Opnd =>
1888 Make_Selected_Component (Loc,
1889 Prefix => Duplicate_Subexpr (Rhs),
1890 Selector_Name =>
1891 Make_Identifier (Loc,
1892 Chars => Name_uTag))),
1893 Reason => CE_Tag_Check_Failed));
1894 end if;
1895
1896 Append_To (L,
1897 Make_Procedure_Call_Statement (Loc,
1898 Name => New_Reference_To (Op, Loc),
1899 Parameter_Associations => New_List (
1900 Unchecked_Convert_To (F_Typ,
1901 Duplicate_Subexpr (Lhs)),
1902 Unchecked_Convert_To (F_Typ,
1903 Duplicate_Subexpr (Rhs)))));
1904 end;
1905
1906 else
1907 L := Make_Tag_Ctrl_Assignment (N);
1908
1909 -- We can't afford to have destructive Finalization Actions in
1910 -- the Self assignment case, so if the target and the source
1911 -- are not obviously different, code is generated to avoid the
1912 -- self assignment case:
1913
1914 -- if lhs'address /= rhs'address then
1915 -- <code for controlled and/or tagged assignment>
1916 -- end if;
1917
1918 -- Skip this if Restriction (No_Finalization) is active
1919
1920 if not Statically_Different (Lhs, Rhs)
1921 and then Expand_Ctrl_Actions
1922 and then not Restriction_Active (No_Finalization)
1923 then
1924 L := New_List (
1925 Make_Implicit_If_Statement (N,
1926 Condition =>
1927 Make_Op_Ne (Loc,
1928 Left_Opnd =>
1929 Make_Attribute_Reference (Loc,
1930 Prefix => Duplicate_Subexpr (Lhs),
1931 Attribute_Name => Name_Address),
1932
1933 Right_Opnd =>
1934 Make_Attribute_Reference (Loc,
1935 Prefix => Duplicate_Subexpr (Rhs),
1936 Attribute_Name => Name_Address)),
1937
1938 Then_Statements => L));
1939 end if;
1940
1941 -- We need to set up an exception handler for implementing
1942 -- 7.6.1(18). The remaining adjustments are tackled by the
1943 -- implementation of adjust for record_controllers (see
1944 -- s-finimp.adb).
1945
1946 -- This is skipped if we have no finalization
1947
1948 if Expand_Ctrl_Actions
1949 and then not Restriction_Active (No_Finalization)
1950 then
1951 L := New_List (
1952 Make_Block_Statement (Loc,
1953 Handled_Statement_Sequence =>
1954 Make_Handled_Sequence_Of_Statements (Loc,
1955 Statements => L,
1956 Exception_Handlers => New_List (
1957 Make_Handler_For_Ctrl_Operation (Loc)))));
1958 end if;
1959 end if;
1960
1961 Rewrite (N,
1962 Make_Block_Statement (Loc,
1963 Handled_Statement_Sequence =>
1964 Make_Handled_Sequence_Of_Statements (Loc, Statements => L)));
1965
1966 -- If no restrictions on aborts, protect the whole assignment
1967 -- for controlled objects as per 9.8(11).
1968
1969 if Needs_Finalization (Typ)
1970 and then Expand_Ctrl_Actions
1971 and then Abort_Allowed
1972 then
1973 declare
1974 Blk : constant Entity_Id :=
1975 New_Internal_Entity
1976 (E_Block, Current_Scope, Sloc (N), 'B');
1977
1978 begin
1979 Set_Scope (Blk, Current_Scope);
1980 Set_Etype (Blk, Standard_Void_Type);
1981 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
1982
1983 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
1984 Set_At_End_Proc (Handled_Statement_Sequence (N),
1985 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
1986 Expand_At_End_Handler
1987 (Handled_Statement_Sequence (N), Blk);
1988 end;
1989 end if;
1990
1991 -- N has been rewritten to a block statement for which it is
1992 -- known by construction that no checks are necessary: analyze
1993 -- it with all checks suppressed.
1994
1995 Analyze (N, Suppress => All_Checks);
1996 return;
1997 end Tagged_Case;
1998
1999 -- Array types
2000
2001 elsif Is_Array_Type (Typ) then
2002 declare
2003 Actual_Rhs : Node_Id := Rhs;
2004
2005 begin
2006 while Nkind_In (Actual_Rhs, N_Type_Conversion,
2007 N_Qualified_Expression)
2008 loop
2009 Actual_Rhs := Expression (Actual_Rhs);
2010 end loop;
2011
2012 Expand_Assign_Array (N, Actual_Rhs);
2013 return;
2014 end;
2015
2016 -- Record types
2017
2018 elsif Is_Record_Type (Typ) then
2019 Expand_Assign_Record (N);
2020 return;
2021
2022 -- Scalar types. This is where we perform the processing related to the
2023 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2024 -- scalar values.
2025
2026 elsif Is_Scalar_Type (Typ) then
2027
2028 -- Case where right side is known valid
2029
2030 if Expr_Known_Valid (Rhs) then
2031
2032 -- Here the right side is valid, so it is fine. The case to deal
2033 -- with is when the left side is a local variable reference whose
2034 -- value is not currently known to be valid. If this is the case,
2035 -- and the assignment appears in an unconditional context, then we
2036 -- can mark the left side as now being valid.
2037
2038 if Is_Local_Variable_Reference (Lhs)
2039 and then not Is_Known_Valid (Entity (Lhs))
2040 and then In_Unconditional_Context (N)
2041 then
2042 Set_Is_Known_Valid (Entity (Lhs), True);
2043 end if;
2044
2045 -- Case where right side may be invalid in the sense of the RM
2046 -- reference above. The RM does not require that we check for the
2047 -- validity on an assignment, but it does require that the assignment
2048 -- of an invalid value not cause erroneous behavior.
2049
2050 -- The general approach in GNAT is to use the Is_Known_Valid flag
2051 -- to avoid the need for validity checking on assignments. However
2052 -- in some cases, we have to do validity checking in order to make
2053 -- sure that the setting of this flag is correct.
2054
2055 else
2056 -- Validate right side if we are validating copies
2057
2058 if Validity_Checks_On
2059 and then Validity_Check_Copies
2060 then
2061 -- Skip this if left hand side is an array or record component
2062 -- and elementary component validity checks are suppressed.
2063
2064 if Nkind_In (Lhs, N_Selected_Component, N_Indexed_Component)
2065 and then not Validity_Check_Components
2066 then
2067 null;
2068 else
2069 Ensure_Valid (Rhs);
2070 end if;
2071
2072 -- We can propagate this to the left side where appropriate
2073
2074 if Is_Local_Variable_Reference (Lhs)
2075 and then not Is_Known_Valid (Entity (Lhs))
2076 and then In_Unconditional_Context (N)
2077 then
2078 Set_Is_Known_Valid (Entity (Lhs), True);
2079 end if;
2080
2081 -- Otherwise check to see what should be done
2082
2083 -- If left side is a local variable, then we just set its flag to
2084 -- indicate that its value may no longer be valid, since we are
2085 -- copying a potentially invalid value.
2086
2087 elsif Is_Local_Variable_Reference (Lhs) then
2088 Set_Is_Known_Valid (Entity (Lhs), False);
2089
2090 -- Check for case of a nonlocal variable on the left side which
2091 -- is currently known to be valid. In this case, we simply ensure
2092 -- that the right side is valid. We only play the game of copying
2093 -- validity status for local variables, since we are doing this
2094 -- statically, not by tracing the full flow graph.
2095
2096 elsif Is_Entity_Name (Lhs)
2097 and then Is_Known_Valid (Entity (Lhs))
2098 then
2099 -- Note: If Validity_Checking mode is set to none, we ignore
2100 -- the Ensure_Valid call so don't worry about that case here.
2101
2102 Ensure_Valid (Rhs);
2103
2104 -- In all other cases, we can safely copy an invalid value without
2105 -- worrying about the status of the left side. Since it is not a
2106 -- variable reference it will not be considered
2107 -- as being known to be valid in any case.
2108
2109 else
2110 null;
2111 end if;
2112 end if;
2113 end if;
2114
2115 -- Defend against invalid subscripts on left side if we are in standard
2116 -- validity checking mode. No need to do this if we are checking all
2117 -- subscripts.
2118
2119 if Validity_Checks_On
2120 and then Validity_Check_Default
2121 and then not Validity_Check_Subscripts
2122 then
2123 Check_Valid_Lvalue_Subscripts (Lhs);
2124 end if;
2125
2126 exception
2127 when RE_Not_Available =>
2128 return;
2129 end Expand_N_Assignment_Statement;
2130
2131 ------------------------------
2132 -- Expand_N_Block_Statement --
2133 ------------------------------
2134
2135 -- Encode entity names defined in block statement
2136
2137 procedure Expand_N_Block_Statement (N : Node_Id) is
2138 begin
2139 Qualify_Entity_Names (N);
2140 end Expand_N_Block_Statement;
2141
2142 -----------------------------
2143 -- Expand_N_Case_Statement --
2144 -----------------------------
2145
2146 procedure Expand_N_Case_Statement (N : Node_Id) is
2147 Loc : constant Source_Ptr := Sloc (N);
2148 Expr : constant Node_Id := Expression (N);
2149 Alt : Node_Id;
2150 Len : Nat;
2151 Cond : Node_Id;
2152 Choice : Node_Id;
2153 Chlist : List_Id;
2154
2155 begin
2156 -- Check for the situation where we know at compile time which branch
2157 -- will be taken
2158
2159 if Compile_Time_Known_Value (Expr) then
2160 Alt := Find_Static_Alternative (N);
2161
2162 -- Move statements from this alternative after the case statement.
2163 -- They are already analyzed, so will be skipped by the analyzer.
2164
2165 Insert_List_After (N, Statements (Alt));
2166
2167 -- That leaves the case statement as a shell. So now we can kill all
2168 -- other alternatives in the case statement.
2169
2170 Kill_Dead_Code (Expression (N));
2171
2172 declare
2173 A : Node_Id;
2174
2175 begin
2176 -- Loop through case alternatives, skipping pragmas, and skipping
2177 -- the one alternative that we select (and therefore retain).
2178
2179 A := First (Alternatives (N));
2180 while Present (A) loop
2181 if A /= Alt
2182 and then Nkind (A) = N_Case_Statement_Alternative
2183 then
2184 Kill_Dead_Code (Statements (A), Warn_On_Deleted_Code);
2185 end if;
2186
2187 Next (A);
2188 end loop;
2189 end;
2190
2191 Rewrite (N, Make_Null_Statement (Loc));
2192 return;
2193 end if;
2194
2195 -- Here if the choice is not determined at compile time
2196
2197 declare
2198 Last_Alt : constant Node_Id := Last (Alternatives (N));
2199
2200 Others_Present : Boolean;
2201 Others_Node : Node_Id;
2202
2203 Then_Stms : List_Id;
2204 Else_Stms : List_Id;
2205
2206 begin
2207 if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
2208 Others_Present := True;
2209 Others_Node := Last_Alt;
2210 else
2211 Others_Present := False;
2212 end if;
2213
2214 -- First step is to worry about possible invalid argument. The RM
2215 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2216 -- outside the base range), then Constraint_Error must be raised.
2217
2218 -- Case of validity check required (validity checks are on, the
2219 -- expression is not known to be valid, and the case statement
2220 -- comes from source -- no need to validity check internally
2221 -- generated case statements).
2222
2223 if Validity_Check_Default then
2224 Ensure_Valid (Expr);
2225 end if;
2226
2227 -- If there is only a single alternative, just replace it with the
2228 -- sequence of statements since obviously that is what is going to
2229 -- be executed in all cases.
2230
2231 Len := List_Length (Alternatives (N));
2232
2233 if Len = 1 then
2234 -- We still need to evaluate the expression if it has any
2235 -- side effects.
2236
2237 Remove_Side_Effects (Expression (N));
2238
2239 Insert_List_After (N, Statements (First (Alternatives (N))));
2240
2241 -- That leaves the case statement as a shell. The alternative that
2242 -- will be executed is reset to a null list. So now we can kill
2243 -- the entire case statement.
2244
2245 Kill_Dead_Code (Expression (N));
2246 Rewrite (N, Make_Null_Statement (Loc));
2247 return;
2248 end if;
2249
2250 -- An optimization. If there are only two alternatives, and only
2251 -- a single choice, then rewrite the whole case statement as an
2252 -- if statement, since this can result in subsequent optimizations.
2253 -- This helps not only with case statements in the source of a
2254 -- simple form, but also with generated code (discriminant check
2255 -- functions in particular)
2256
2257 if Len = 2 then
2258 Chlist := Discrete_Choices (First (Alternatives (N)));
2259
2260 if List_Length (Chlist) = 1 then
2261 Choice := First (Chlist);
2262
2263 Then_Stms := Statements (First (Alternatives (N)));
2264 Else_Stms := Statements (Last (Alternatives (N)));
2265
2266 -- For TRUE, generate "expression", not expression = true
2267
2268 if Nkind (Choice) = N_Identifier
2269 and then Entity (Choice) = Standard_True
2270 then
2271 Cond := Expression (N);
2272
2273 -- For FALSE, generate "expression" and switch then/else
2274
2275 elsif Nkind (Choice) = N_Identifier
2276 and then Entity (Choice) = Standard_False
2277 then
2278 Cond := Expression (N);
2279 Else_Stms := Statements (First (Alternatives (N)));
2280 Then_Stms := Statements (Last (Alternatives (N)));
2281
2282 -- For a range, generate "expression in range"
2283
2284 elsif Nkind (Choice) = N_Range
2285 or else (Nkind (Choice) = N_Attribute_Reference
2286 and then Attribute_Name (Choice) = Name_Range)
2287 or else (Is_Entity_Name (Choice)
2288 and then Is_Type (Entity (Choice)))
2289 or else Nkind (Choice) = N_Subtype_Indication
2290 then
2291 Cond :=
2292 Make_In (Loc,
2293 Left_Opnd => Expression (N),
2294 Right_Opnd => Relocate_Node (Choice));
2295
2296 -- For any other subexpression "expression = value"
2297
2298 else
2299 Cond :=
2300 Make_Op_Eq (Loc,
2301 Left_Opnd => Expression (N),
2302 Right_Opnd => Relocate_Node (Choice));
2303 end if;
2304
2305 -- Now rewrite the case as an IF
2306
2307 Rewrite (N,
2308 Make_If_Statement (Loc,
2309 Condition => Cond,
2310 Then_Statements => Then_Stms,
2311 Else_Statements => Else_Stms));
2312 Analyze (N);
2313 return;
2314 end if;
2315 end if;
2316
2317 -- If the last alternative is not an Others choice, replace it with
2318 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2319 -- the modified case statement, since it's only effect would be to
2320 -- compute the contents of the Others_Discrete_Choices which is not
2321 -- needed by the back end anyway.
2322
2323 -- The reason we do this is that the back end always needs some
2324 -- default for a switch, so if we have not supplied one in the
2325 -- processing above for validity checking, then we need to supply
2326 -- one here.
2327
2328 if not Others_Present then
2329 Others_Node := Make_Others_Choice (Sloc (Last_Alt));
2330 Set_Others_Discrete_Choices
2331 (Others_Node, Discrete_Choices (Last_Alt));
2332 Set_Discrete_Choices (Last_Alt, New_List (Others_Node));
2333 end if;
2334 end;
2335 end Expand_N_Case_Statement;
2336
2337 -----------------------------
2338 -- Expand_N_Exit_Statement --
2339 -----------------------------
2340
2341 -- The only processing required is to deal with a possible C/Fortran
2342 -- boolean value used as the condition for the exit statement.
2343
2344 procedure Expand_N_Exit_Statement (N : Node_Id) is
2345 begin
2346 Adjust_Condition (Condition (N));
2347 end Expand_N_Exit_Statement;
2348
2349 ----------------------------------------
2350 -- Expand_N_Extended_Return_Statement --
2351 ----------------------------------------
2352
2353 -- If there is a Handled_Statement_Sequence, we rewrite this:
2354
2355 -- return Result : T := <expression> do
2356 -- <handled_seq_of_stms>
2357 -- end return;
2358
2359 -- to be:
2360
2361 -- declare
2362 -- Result : T := <expression>;
2363 -- begin
2364 -- <handled_seq_of_stms>
2365 -- return Result;
2366 -- end;
2367
2368 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
2369
2370 -- return Result : T := <expression>;
2371
2372 -- to be:
2373
2374 -- return <expression>;
2375
2376 -- unless it's build-in-place or there's no <expression>, in which case
2377 -- we generate:
2378
2379 -- declare
2380 -- Result : T := <expression>;
2381 -- begin
2382 -- return Result;
2383 -- end;
2384
2385 -- Note that this case could have been written by the user as an extended
2386 -- return statement, or could have been transformed to this from a simple
2387 -- return statement.
2388
2389 -- That is, we need to have a reified return object if there are statements
2390 -- (which might refer to it) or if we're doing build-in-place (so we can
2391 -- set its address to the final resting place or if there is no expression
2392 -- (in which case default initial values might need to be set).
2393
2394 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
2395 Loc : constant Source_Ptr := Sloc (N);
2396
2397 Return_Object_Entity : constant Entity_Id :=
2398 First_Entity (Return_Statement_Entity (N));
2399 Return_Object_Decl : constant Node_Id :=
2400 Parent (Return_Object_Entity);
2401 Parent_Function : constant Entity_Id :=
2402 Return_Applies_To (Return_Statement_Entity (N));
2403 Parent_Function_Typ : constant Entity_Id := Etype (Parent_Function);
2404 Is_Build_In_Place : constant Boolean :=
2405 Is_Build_In_Place_Function (Parent_Function);
2406
2407 Return_Stm : Node_Id;
2408 Statements : List_Id;
2409 Handled_Stm_Seq : Node_Id;
2410 Result : Node_Id;
2411 Exp : Node_Id;
2412
2413 function Has_Controlled_Parts (Typ : Entity_Id) return Boolean;
2414 -- Determine whether type Typ is controlled or contains a controlled
2415 -- subcomponent.
2416
2417 function Move_Activation_Chain return Node_Id;
2418 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
2419 -- with parameters:
2420 -- From current activation chain
2421 -- To activation chain passed in by the caller
2422 -- New_Master master passed in by the caller
2423
2424 function Move_Final_List return Node_Id;
2425 -- Construct call to System.Finalization_Implementation.Move_Final_List
2426 -- with parameters:
2427 --
2428 -- From finalization list of the return statement
2429 -- To finalization list passed in by the caller
2430
2431 --------------------------
2432 -- Has_Controlled_Parts --
2433 --------------------------
2434
2435 function Has_Controlled_Parts (Typ : Entity_Id) return Boolean is
2436 begin
2437 return
2438 Is_Controlled (Typ)
2439 or else Has_Controlled_Component (Typ);
2440 end Has_Controlled_Parts;
2441
2442 ---------------------------
2443 -- Move_Activation_Chain --
2444 ---------------------------
2445
2446 function Move_Activation_Chain return Node_Id is
2447 Activation_Chain_Formal : constant Entity_Id :=
2448 Build_In_Place_Formal
2449 (Parent_Function, BIP_Activation_Chain);
2450 To : constant Node_Id :=
2451 New_Reference_To
2452 (Activation_Chain_Formal, Loc);
2453 Master_Formal : constant Entity_Id :=
2454 Build_In_Place_Formal
2455 (Parent_Function, BIP_Master);
2456 New_Master : constant Node_Id :=
2457 New_Reference_To (Master_Formal, Loc);
2458
2459 Chain_Entity : Entity_Id;
2460 From : Node_Id;
2461
2462 begin
2463 Chain_Entity := First_Entity (Return_Statement_Entity (N));
2464 while Chars (Chain_Entity) /= Name_uChain loop
2465 Chain_Entity := Next_Entity (Chain_Entity);
2466 end loop;
2467
2468 From :=
2469 Make_Attribute_Reference (Loc,
2470 Prefix => New_Reference_To (Chain_Entity, Loc),
2471 Attribute_Name => Name_Unrestricted_Access);
2472 -- ??? Not clear why "Make_Identifier (Loc, Name_uChain)" doesn't
2473 -- work, instead of "New_Reference_To (Chain_Entity, Loc)" above.
2474
2475 return
2476 Make_Procedure_Call_Statement (Loc,
2477 Name => New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
2478 Parameter_Associations => New_List (From, To, New_Master));
2479 end Move_Activation_Chain;
2480
2481 ---------------------
2482 -- Move_Final_List --
2483 ---------------------
2484
2485 function Move_Final_List return Node_Id is
2486 Flist : constant Entity_Id :=
2487 Finalization_Chain_Entity (Return_Statement_Entity (N));
2488
2489 From : constant Node_Id := New_Reference_To (Flist, Loc);
2490
2491 Caller_Final_List : constant Entity_Id :=
2492 Build_In_Place_Formal
2493 (Parent_Function, BIP_Final_List);
2494
2495 To : constant Node_Id := New_Reference_To (Caller_Final_List, Loc);
2496
2497 begin
2498 -- Catch cases where a finalization chain entity has not been
2499 -- associated with the return statement entity.
2500
2501 pragma Assert (Present (Flist));
2502
2503 -- Build required call
2504
2505 return
2506 Make_If_Statement (Loc,
2507 Condition =>
2508 Make_Op_Ne (Loc,
2509 Left_Opnd => New_Copy (From),
2510 Right_Opnd => New_Node (N_Null, Loc)),
2511 Then_Statements =>
2512 New_List (
2513 Make_Procedure_Call_Statement (Loc,
2514 Name => New_Reference_To (RTE (RE_Move_Final_List), Loc),
2515 Parameter_Associations => New_List (From, To))));
2516 end Move_Final_List;
2517
2518 -- Start of processing for Expand_N_Extended_Return_Statement
2519
2520 begin
2521 if Nkind (Return_Object_Decl) = N_Object_Declaration then
2522 Exp := Expression (Return_Object_Decl);
2523 else
2524 Exp := Empty;
2525 end if;
2526
2527 Handled_Stm_Seq := Handled_Statement_Sequence (N);
2528
2529 -- Build a simple_return_statement that returns the return object when
2530 -- there is a statement sequence, or no expression, or the result will
2531 -- be built in place. Note however that we currently do this for all
2532 -- composite cases, even though nonlimited composite results are not yet
2533 -- built in place (though we plan to do so eventually).
2534
2535 if Present (Handled_Stm_Seq)
2536 or else Is_Composite_Type (Etype (Parent_Function))
2537 or else No (Exp)
2538 then
2539 if No (Handled_Stm_Seq) then
2540 Statements := New_List;
2541
2542 -- If the extended return has a handled statement sequence, then wrap
2543 -- it in a block and use the block as the first statement.
2544
2545 else
2546 Statements :=
2547 New_List (Make_Block_Statement (Loc,
2548 Declarations => New_List,
2549 Handled_Statement_Sequence => Handled_Stm_Seq));
2550 end if;
2551
2552 -- If control gets past the above Statements, we have successfully
2553 -- completed the return statement. If the result type has controlled
2554 -- parts and the return is for a build-in-place function, then we
2555 -- call Move_Final_List to transfer responsibility for finalization
2556 -- of the return object to the caller. An alternative would be to
2557 -- declare a Success flag in the function, initialize it to False,
2558 -- and set it to True here. Then move the Move_Final_List call into
2559 -- the cleanup code, and check Success. If Success then make a call
2560 -- to Move_Final_List else do finalization. Then we can remove the
2561 -- abort-deferral and the nulling-out of the From parameter from
2562 -- Move_Final_List. Note that the current method is not quite correct
2563 -- in the rather obscure case of a select-then-abort statement whose
2564 -- abortable part contains the return statement.
2565
2566 -- Check the type of the function to determine whether to move the
2567 -- finalization list. A special case arises when processing a simple
2568 -- return statement which has been rewritten as an extended return.
2569 -- In that case check the type of the returned object or the original
2570 -- expression.
2571
2572 if Is_Build_In_Place
2573 and then
2574 (Has_Controlled_Parts (Parent_Function_Typ)
2575 or else (Is_Class_Wide_Type (Parent_Function_Typ)
2576 and then
2577 Has_Controlled_Parts (Root_Type (Parent_Function_Typ)))
2578 or else Has_Controlled_Parts (Etype (Return_Object_Entity))
2579 or else (Present (Exp)
2580 and then Has_Controlled_Parts (Etype (Exp))))
2581 then
2582 Append_To (Statements, Move_Final_List);
2583 end if;
2584
2585 -- Similarly to the above Move_Final_List, if the result type
2586 -- contains tasks, we call Move_Activation_Chain. Later, the cleanup
2587 -- code will call Complete_Master, which will terminate any
2588 -- unactivated tasks belonging to the return statement master. But
2589 -- Move_Activation_Chain updates their master to be that of the
2590 -- caller, so they will not be terminated unless the return statement
2591 -- completes unsuccessfully due to exception, abort, goto, or exit.
2592 -- As a formality, we test whether the function requires the result
2593 -- to be built in place, though that's necessarily true for the case
2594 -- of result types with task parts.
2595
2596 if Is_Build_In_Place and Has_Task (Etype (Parent_Function)) then
2597 Append_To (Statements, Move_Activation_Chain);
2598 end if;
2599
2600 -- Build a simple_return_statement that returns the return object
2601
2602 Return_Stm :=
2603 Make_Simple_Return_Statement (Loc,
2604 Expression => New_Occurrence_Of (Return_Object_Entity, Loc));
2605 Append_To (Statements, Return_Stm);
2606
2607 Handled_Stm_Seq :=
2608 Make_Handled_Sequence_Of_Statements (Loc, Statements);
2609 end if;
2610
2611 -- Case where we build a block
2612
2613 if Present (Handled_Stm_Seq) then
2614 Result :=
2615 Make_Block_Statement (Loc,
2616 Declarations => Return_Object_Declarations (N),
2617 Handled_Statement_Sequence => Handled_Stm_Seq);
2618
2619 -- We set the entity of the new block statement to be that of the
2620 -- return statement. This is necessary so that various fields, such
2621 -- as Finalization_Chain_Entity carry over from the return statement
2622 -- to the block. Note that this block is unusual, in that its entity
2623 -- is an E_Return_Statement rather than an E_Block.
2624
2625 Set_Identifier
2626 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
2627
2628 -- If the object decl was already rewritten as a renaming, then
2629 -- we don't want to do the object allocation and transformation of
2630 -- of the return object declaration to a renaming. This case occurs
2631 -- when the return object is initialized by a call to another
2632 -- build-in-place function, and that function is responsible for the
2633 -- allocation of the return object.
2634
2635 if Is_Build_In_Place
2636 and then
2637 Nkind (Return_Object_Decl) = N_Object_Renaming_Declaration
2638 then
2639 Set_By_Ref (Return_Stm); -- Return build-in-place results by ref
2640
2641 elsif Is_Build_In_Place then
2642
2643 -- Locate the implicit access parameter associated with the
2644 -- caller-supplied return object and convert the return
2645 -- statement's return object declaration to a renaming of a
2646 -- dereference of the access parameter. If the return object's
2647 -- declaration includes an expression that has not already been
2648 -- expanded as separate assignments, then add an assignment
2649 -- statement to ensure the return object gets initialized.
2650
2651 -- declare
2652 -- Result : T [:= <expression>];
2653 -- begin
2654 -- ...
2655
2656 -- is converted to
2657
2658 -- declare
2659 -- Result : T renames FuncRA.all;
2660 -- [Result := <expression;]
2661 -- begin
2662 -- ...
2663
2664 declare
2665 Return_Obj_Id : constant Entity_Id :=
2666 Defining_Identifier (Return_Object_Decl);
2667 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
2668 Return_Obj_Expr : constant Node_Id :=
2669 Expression (Return_Object_Decl);
2670 Result_Subt : constant Entity_Id :=
2671 Etype (Parent_Function);
2672 Constr_Result : constant Boolean :=
2673 Is_Constrained (Result_Subt);
2674 Obj_Alloc_Formal : Entity_Id;
2675 Object_Access : Entity_Id;
2676 Obj_Acc_Deref : Node_Id;
2677 Init_Assignment : Node_Id := Empty;
2678
2679 begin
2680 -- Build-in-place results must be returned by reference
2681
2682 Set_By_Ref (Return_Stm);
2683
2684 -- Retrieve the implicit access parameter passed by the caller
2685
2686 Object_Access :=
2687 Build_In_Place_Formal (Parent_Function, BIP_Object_Access);
2688
2689 -- If the return object's declaration includes an expression
2690 -- and the declaration isn't marked as No_Initialization, then
2691 -- we need to generate an assignment to the object and insert
2692 -- it after the declaration before rewriting it as a renaming
2693 -- (otherwise we'll lose the initialization).
2694
2695 if Present (Return_Obj_Expr)
2696 and then not No_Initialization (Return_Object_Decl)
2697 then
2698 Init_Assignment :=
2699 Make_Assignment_Statement (Loc,
2700 Name => New_Reference_To (Return_Obj_Id, Loc),
2701 Expression => Relocate_Node (Return_Obj_Expr));
2702 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
2703 Set_Assignment_OK (Name (Init_Assignment));
2704 Set_No_Ctrl_Actions (Init_Assignment);
2705
2706 Set_Parent (Name (Init_Assignment), Init_Assignment);
2707 Set_Parent (Expression (Init_Assignment), Init_Assignment);
2708
2709 Set_Expression (Return_Object_Decl, Empty);
2710
2711 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
2712 and then not Is_Class_Wide_Type
2713 (Etype (Expression (Init_Assignment)))
2714 then
2715 Rewrite (Expression (Init_Assignment),
2716 Make_Type_Conversion (Loc,
2717 Subtype_Mark =>
2718 New_Occurrence_Of
2719 (Etype (Return_Obj_Id), Loc),
2720 Expression =>
2721 Relocate_Node (Expression (Init_Assignment))));
2722 end if;
2723
2724 -- In the case of functions where the calling context can
2725 -- determine the form of allocation needed, initialization
2726 -- is done with each part of the if statement that handles
2727 -- the different forms of allocation (this is true for
2728 -- unconstrained and tagged result subtypes).
2729
2730 if Constr_Result
2731 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
2732 then
2733 Insert_After (Return_Object_Decl, Init_Assignment);
2734 end if;
2735 end if;
2736
2737 -- When the function's subtype is unconstrained, a run-time
2738 -- test is needed to determine the form of allocation to use
2739 -- for the return object. The function has an implicit formal
2740 -- parameter indicating this. If the BIP_Alloc_Form formal has
2741 -- the value one, then the caller has passed access to an
2742 -- existing object for use as the return object. If the value
2743 -- is two, then the return object must be allocated on the
2744 -- secondary stack. Otherwise, the object must be allocated in
2745 -- a storage pool (currently only supported for the global
2746 -- heap, user-defined storage pools TBD ???). We generate an
2747 -- if statement to test the implicit allocation formal and
2748 -- initialize a local access value appropriately, creating
2749 -- allocators in the secondary stack and global heap cases.
2750 -- The special formal also exists and must be tested when the
2751 -- function has a tagged result, even when the result subtype
2752 -- is constrained, because in general such functions can be
2753 -- called in dispatching contexts and must be handled similarly
2754 -- to functions with a class-wide result.
2755
2756 if not Constr_Result
2757 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
2758 then
2759 Obj_Alloc_Formal :=
2760 Build_In_Place_Formal (Parent_Function, BIP_Alloc_Form);
2761
2762 declare
2763 Ref_Type : Entity_Id;
2764 Ptr_Type_Decl : Node_Id;
2765 Alloc_Obj_Id : Entity_Id;
2766 Alloc_Obj_Decl : Node_Id;
2767 Alloc_If_Stmt : Node_Id;
2768 SS_Allocator : Node_Id;
2769 Heap_Allocator : Node_Id;
2770
2771 begin
2772 -- Reuse the itype created for the function's implicit
2773 -- access formal. This avoids the need to create a new
2774 -- access type here, plus it allows assigning the access
2775 -- formal directly without applying a conversion.
2776
2777 -- Ref_Type := Etype (Object_Access);
2778
2779 -- Create an access type designating the function's
2780 -- result subtype.
2781
2782 Ref_Type :=
2783 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2784
2785 Ptr_Type_Decl :=
2786 Make_Full_Type_Declaration (Loc,
2787 Defining_Identifier => Ref_Type,
2788 Type_Definition =>
2789 Make_Access_To_Object_Definition (Loc,
2790 All_Present => True,
2791 Subtype_Indication =>
2792 New_Reference_To (Return_Obj_Typ, Loc)));
2793
2794 Insert_Before (Return_Object_Decl, Ptr_Type_Decl);
2795
2796 -- Create an access object that will be initialized to an
2797 -- access value denoting the return object, either coming
2798 -- from an implicit access value passed in by the caller
2799 -- or from the result of an allocator.
2800
2801 Alloc_Obj_Id :=
2802 Make_Defining_Identifier (Loc,
2803 Chars => New_Internal_Name ('R'));
2804 Set_Etype (Alloc_Obj_Id, Ref_Type);
2805
2806 Alloc_Obj_Decl :=
2807 Make_Object_Declaration (Loc,
2808 Defining_Identifier => Alloc_Obj_Id,
2809 Object_Definition => New_Reference_To
2810 (Ref_Type, Loc));
2811
2812 Insert_Before (Return_Object_Decl, Alloc_Obj_Decl);
2813
2814 -- Create allocators for both the secondary stack and
2815 -- global heap. If there's an initialization expression,
2816 -- then create these as initialized allocators.
2817
2818 if Present (Return_Obj_Expr)
2819 and then not No_Initialization (Return_Object_Decl)
2820 then
2821 Heap_Allocator :=
2822 Make_Allocator (Loc,
2823 Expression =>
2824 Make_Qualified_Expression (Loc,
2825 Subtype_Mark =>
2826 New_Reference_To (Return_Obj_Typ, Loc),
2827 Expression =>
2828 New_Copy_Tree (Return_Obj_Expr)));
2829
2830 else
2831 -- If the function returns a class-wide type we cannot
2832 -- use the return type for the allocator. Instead we
2833 -- use the type of the expression, which must be an
2834 -- aggregate of a definite type.
2835
2836 if Is_Class_Wide_Type (Return_Obj_Typ) then
2837 Heap_Allocator :=
2838 Make_Allocator (Loc,
2839 Expression =>
2840 New_Reference_To
2841 (Etype (Return_Obj_Expr), Loc));
2842 else
2843 Heap_Allocator :=
2844 Make_Allocator (Loc,
2845 Expression =>
2846 New_Reference_To (Return_Obj_Typ, Loc));
2847 end if;
2848
2849 -- If the object requires default initialization then
2850 -- that will happen later following the elaboration of
2851 -- the object renaming. If we don't turn it off here
2852 -- then the object will be default initialized twice.
2853
2854 Set_No_Initialization (Heap_Allocator);
2855 end if;
2856
2857 -- If the No_Allocators restriction is active, then only
2858 -- an allocator for secondary stack allocation is needed.
2859 -- It's OK for such allocators to have Comes_From_Source
2860 -- set to False, because gigi knows not to flag them as
2861 -- being a violation of No_Implicit_Heap_Allocations.
2862
2863 if Restriction_Active (No_Allocators) then
2864 SS_Allocator := Heap_Allocator;
2865 Heap_Allocator := Make_Null (Loc);
2866
2867 -- Otherwise the heap allocator may be needed, so we make
2868 -- another allocator for secondary stack allocation.
2869
2870 else
2871 SS_Allocator := New_Copy_Tree (Heap_Allocator);
2872
2873 -- The heap allocator is marked Comes_From_Source
2874 -- since it corresponds to an explicit user-written
2875 -- allocator (that is, it will only be executed on
2876 -- behalf of callers that call the function as
2877 -- initialization for such an allocator). This
2878 -- prevents errors when No_Implicit_Heap_Allocations
2879 -- is in force.
2880
2881 Set_Comes_From_Source (Heap_Allocator, True);
2882 end if;
2883
2884 -- The allocator is returned on the secondary stack. We
2885 -- don't do this on VM targets, since the SS is not used.
2886
2887 if VM_Target = No_VM then
2888 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
2889 Set_Procedure_To_Call
2890 (SS_Allocator, RTE (RE_SS_Allocate));
2891
2892 -- The allocator is returned on the secondary stack,
2893 -- so indicate that the function return, as well as
2894 -- the block that encloses the allocator, must not
2895 -- release it. The flags must be set now because the
2896 -- decision to use the secondary stack is done very
2897 -- late in the course of expanding the return
2898 -- statement, past the point where these flags are
2899 -- normally set.
2900
2901 Set_Sec_Stack_Needed_For_Return (Parent_Function);
2902 Set_Sec_Stack_Needed_For_Return
2903 (Return_Statement_Entity (N));
2904 Set_Uses_Sec_Stack (Parent_Function);
2905 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
2906 end if;
2907
2908 -- Create an if statement to test the BIP_Alloc_Form
2909 -- formal and initialize the access object to either the
2910 -- BIP_Object_Access formal (BIP_Alloc_Form = 0), the
2911 -- result of allocating the object in the secondary stack
2912 -- (BIP_Alloc_Form = 1), or else an allocator to create
2913 -- the return object in the heap (BIP_Alloc_Form = 2).
2914
2915 -- ??? An unchecked type conversion must be made in the
2916 -- case of assigning the access object formal to the
2917 -- local access object, because a normal conversion would
2918 -- be illegal in some cases (such as converting access-
2919 -- to-unconstrained to access-to-constrained), but the
2920 -- the unchecked conversion will presumably fail to work
2921 -- right in just such cases. It's not clear at all how to
2922 -- handle this. ???
2923
2924 Alloc_If_Stmt :=
2925 Make_If_Statement (Loc,
2926 Condition =>
2927 Make_Op_Eq (Loc,
2928 Left_Opnd =>
2929 New_Reference_To (Obj_Alloc_Formal, Loc),
2930 Right_Opnd =>
2931 Make_Integer_Literal (Loc,
2932 UI_From_Int (BIP_Allocation_Form'Pos
2933 (Caller_Allocation)))),
2934 Then_Statements =>
2935 New_List (Make_Assignment_Statement (Loc,
2936 Name =>
2937 New_Reference_To
2938 (Alloc_Obj_Id, Loc),
2939 Expression =>
2940 Make_Unchecked_Type_Conversion (Loc,
2941 Subtype_Mark =>
2942 New_Reference_To (Ref_Type, Loc),
2943 Expression =>
2944 New_Reference_To
2945 (Object_Access, Loc)))),
2946 Elsif_Parts =>
2947 New_List (Make_Elsif_Part (Loc,
2948 Condition =>
2949 Make_Op_Eq (Loc,
2950 Left_Opnd =>
2951 New_Reference_To
2952 (Obj_Alloc_Formal, Loc),
2953 Right_Opnd =>
2954 Make_Integer_Literal (Loc,
2955 UI_From_Int (
2956 BIP_Allocation_Form'Pos
2957 (Secondary_Stack)))),
2958 Then_Statements =>
2959 New_List
2960 (Make_Assignment_Statement (Loc,
2961 Name =>
2962 New_Reference_To
2963 (Alloc_Obj_Id, Loc),
2964 Expression =>
2965 SS_Allocator)))),
2966 Else_Statements =>
2967 New_List (Make_Assignment_Statement (Loc,
2968 Name =>
2969 New_Reference_To
2970 (Alloc_Obj_Id, Loc),
2971 Expression =>
2972 Heap_Allocator)));
2973
2974 -- If a separate initialization assignment was created
2975 -- earlier, append that following the assignment of the
2976 -- implicit access formal to the access object, to ensure
2977 -- that the return object is initialized in that case.
2978 -- In this situation, the target of the assignment must
2979 -- be rewritten to denote a dereference of the access to
2980 -- the return object passed in by the caller.
2981
2982 if Present (Init_Assignment) then
2983 Rewrite (Name (Init_Assignment),
2984 Make_Explicit_Dereference (Loc,
2985 Prefix => New_Reference_To (Alloc_Obj_Id, Loc)));
2986 Set_Etype
2987 (Name (Init_Assignment), Etype (Return_Obj_Id));
2988
2989 Append_To
2990 (Then_Statements (Alloc_If_Stmt),
2991 Init_Assignment);
2992 end if;
2993
2994 Insert_Before (Return_Object_Decl, Alloc_If_Stmt);
2995
2996 -- Remember the local access object for use in the
2997 -- dereference of the renaming created below.
2998
2999 Object_Access := Alloc_Obj_Id;
3000 end;
3001 end if;
3002
3003 -- Replace the return object declaration with a renaming of a
3004 -- dereference of the access value designating the return
3005 -- object.
3006
3007 Obj_Acc_Deref :=
3008 Make_Explicit_Dereference (Loc,
3009 Prefix => New_Reference_To (Object_Access, Loc));
3010
3011 Rewrite (Return_Object_Decl,
3012 Make_Object_Renaming_Declaration (Loc,
3013 Defining_Identifier => Return_Obj_Id,
3014 Access_Definition => Empty,
3015 Subtype_Mark => New_Occurrence_Of
3016 (Return_Obj_Typ, Loc),
3017 Name => Obj_Acc_Deref));
3018
3019 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
3020 end;
3021 end if;
3022
3023 -- Case where we do not build a block
3024
3025 else
3026 -- We're about to drop Return_Object_Declarations on the floor, so
3027 -- we need to insert it, in case it got expanded into useful code.
3028
3029 Insert_List_Before (N, Return_Object_Declarations (N));
3030
3031 -- Build simple_return_statement that returns the expression directly
3032
3033 Return_Stm := Make_Simple_Return_Statement (Loc, Expression => Exp);
3034
3035 Result := Return_Stm;
3036 end if;
3037
3038 -- Set the flag to prevent infinite recursion
3039
3040 Set_Comes_From_Extended_Return_Statement (Return_Stm);
3041
3042 Rewrite (N, Result);
3043 Analyze (N);
3044 end Expand_N_Extended_Return_Statement;
3045
3046 -----------------------------
3047 -- Expand_N_Goto_Statement --
3048 -----------------------------
3049
3050 -- Add poll before goto if polling active
3051
3052 procedure Expand_N_Goto_Statement (N : Node_Id) is
3053 begin
3054 Generate_Poll_Call (N);
3055 end Expand_N_Goto_Statement;
3056
3057 ---------------------------
3058 -- Expand_N_If_Statement --
3059 ---------------------------
3060
3061 -- First we deal with the case of C and Fortran convention boolean values,
3062 -- with zero/non-zero semantics.
3063
3064 -- Second, we deal with the obvious rewriting for the cases where the
3065 -- condition of the IF is known at compile time to be True or False.
3066
3067 -- Third, we remove elsif parts which have non-empty Condition_Actions
3068 -- and rewrite as independent if statements. For example:
3069
3070 -- if x then xs
3071 -- elsif y then ys
3072 -- ...
3073 -- end if;
3074
3075 -- becomes
3076 --
3077 -- if x then xs
3078 -- else
3079 -- <<condition actions of y>>
3080 -- if y then ys
3081 -- ...
3082 -- end if;
3083 -- end if;
3084
3085 -- This rewriting is needed if at least one elsif part has a non-empty
3086 -- Condition_Actions list. We also do the same processing if there is a
3087 -- constant condition in an elsif part (in conjunction with the first
3088 -- processing step mentioned above, for the recursive call made to deal
3089 -- with the created inner if, this deals with properly optimizing the
3090 -- cases of constant elsif conditions).
3091
3092 procedure Expand_N_If_Statement (N : Node_Id) is
3093 Loc : constant Source_Ptr := Sloc (N);
3094 Hed : Node_Id;
3095 E : Node_Id;
3096 New_If : Node_Id;
3097
3098 Warn_If_Deleted : constant Boolean :=
3099 Warn_On_Deleted_Code and then Comes_From_Source (N);
3100 -- Indicates whether we want warnings when we delete branches of the
3101 -- if statement based on constant condition analysis. We never want
3102 -- these warnings for expander generated code.
3103
3104 begin
3105 Adjust_Condition (Condition (N));
3106
3107 -- The following loop deals with constant conditions for the IF. We
3108 -- need a loop because as we eliminate False conditions, we grab the
3109 -- first elsif condition and use it as the primary condition.
3110
3111 while Compile_Time_Known_Value (Condition (N)) loop
3112
3113 -- If condition is True, we can simply rewrite the if statement now
3114 -- by replacing it by the series of then statements.
3115
3116 if Is_True (Expr_Value (Condition (N))) then
3117
3118 -- All the else parts can be killed
3119
3120 Kill_Dead_Code (Elsif_Parts (N), Warn_If_Deleted);
3121 Kill_Dead_Code (Else_Statements (N), Warn_If_Deleted);
3122
3123 Hed := Remove_Head (Then_Statements (N));
3124 Insert_List_After (N, Then_Statements (N));
3125 Rewrite (N, Hed);
3126 return;
3127
3128 -- If condition is False, then we can delete the condition and
3129 -- the Then statements
3130
3131 else
3132 -- We do not delete the condition if constant condition warnings
3133 -- are enabled, since otherwise we end up deleting the desired
3134 -- warning. Of course the backend will get rid of this True/False
3135 -- test anyway, so nothing is lost here.
3136
3137 if not Constant_Condition_Warnings then
3138 Kill_Dead_Code (Condition (N));
3139 end if;
3140
3141 Kill_Dead_Code (Then_Statements (N), Warn_If_Deleted);
3142
3143 -- If there are no elsif statements, then we simply replace the
3144 -- entire if statement by the sequence of else statements.
3145
3146 if No (Elsif_Parts (N)) then
3147 if No (Else_Statements (N))
3148 or else Is_Empty_List (Else_Statements (N))
3149 then
3150 Rewrite (N,
3151 Make_Null_Statement (Sloc (N)));
3152 else
3153 Hed := Remove_Head (Else_Statements (N));
3154 Insert_List_After (N, Else_Statements (N));
3155 Rewrite (N, Hed);
3156 end if;
3157
3158 return;
3159
3160 -- If there are elsif statements, the first of them becomes the
3161 -- if/then section of the rebuilt if statement This is the case
3162 -- where we loop to reprocess this copied condition.
3163
3164 else
3165 Hed := Remove_Head (Elsif_Parts (N));
3166 Insert_Actions (N, Condition_Actions (Hed));
3167 Set_Condition (N, Condition (Hed));
3168 Set_Then_Statements (N, Then_Statements (Hed));
3169
3170 -- Hed might have been captured as the condition determining
3171 -- the current value for an entity. Now it is detached from
3172 -- the tree, so a Current_Value pointer in the condition might
3173 -- need to be updated.
3174
3175 Set_Current_Value_Condition (N);
3176
3177 if Is_Empty_List (Elsif_Parts (N)) then
3178 Set_Elsif_Parts (N, No_List);
3179 end if;
3180 end if;
3181 end if;
3182 end loop;
3183
3184 -- Loop through elsif parts, dealing with constant conditions and
3185 -- possible expression actions that are present.
3186
3187 if Present (Elsif_Parts (N)) then
3188 E := First (Elsif_Parts (N));
3189 while Present (E) loop
3190 Adjust_Condition (Condition (E));
3191
3192 -- If there are condition actions, then rewrite the if statement
3193 -- as indicated above. We also do the same rewrite for a True or
3194 -- False condition. The further processing of this constant
3195 -- condition is then done by the recursive call to expand the
3196 -- newly created if statement
3197
3198 if Present (Condition_Actions (E))
3199 or else Compile_Time_Known_Value (Condition (E))
3200 then
3201 -- Note this is not an implicit if statement, since it is part
3202 -- of an explicit if statement in the source (or of an implicit
3203 -- if statement that has already been tested).
3204
3205 New_If :=
3206 Make_If_Statement (Sloc (E),
3207 Condition => Condition (E),
3208 Then_Statements => Then_Statements (E),
3209 Elsif_Parts => No_List,
3210 Else_Statements => Else_Statements (N));
3211
3212 -- Elsif parts for new if come from remaining elsif's of parent
3213
3214 while Present (Next (E)) loop
3215 if No (Elsif_Parts (New_If)) then
3216 Set_Elsif_Parts (New_If, New_List);
3217 end if;
3218
3219 Append (Remove_Next (E), Elsif_Parts (New_If));
3220 end loop;
3221
3222 Set_Else_Statements (N, New_List (New_If));
3223
3224 if Present (Condition_Actions (E)) then
3225 Insert_List_Before (New_If, Condition_Actions (E));
3226 end if;
3227
3228 Remove (E);
3229
3230 if Is_Empty_List (Elsif_Parts (N)) then
3231 Set_Elsif_Parts (N, No_List);
3232 end if;
3233
3234 Analyze (New_If);
3235 return;
3236
3237 -- No special processing for that elsif part, move to next
3238
3239 else
3240 Next (E);
3241 end if;
3242 end loop;
3243 end if;
3244
3245 -- Some more optimizations applicable if we still have an IF statement
3246
3247 if Nkind (N) /= N_If_Statement then
3248 return;
3249 end if;
3250
3251 -- Another optimization, special cases that can be simplified
3252
3253 -- if expression then
3254 -- return true;
3255 -- else
3256 -- return false;
3257 -- end if;
3258
3259 -- can be changed to:
3260
3261 -- return expression;
3262
3263 -- and
3264
3265 -- if expression then
3266 -- return false;
3267 -- else
3268 -- return true;
3269 -- end if;
3270
3271 -- can be changed to:
3272
3273 -- return not (expression);
3274
3275 -- Only do these optimizations if we are at least at -O1 level and
3276 -- do not do them if control flow optimizations are suppressed.
3277
3278 if Optimization_Level > 0
3279 and then not Opt.Suppress_Control_Flow_Optimizations
3280 then
3281 if Nkind (N) = N_If_Statement
3282 and then No (Elsif_Parts (N))
3283 and then Present (Else_Statements (N))
3284 and then List_Length (Then_Statements (N)) = 1
3285 and then List_Length (Else_Statements (N)) = 1
3286 then
3287 declare
3288 Then_Stm : constant Node_Id := First (Then_Statements (N));
3289 Else_Stm : constant Node_Id := First (Else_Statements (N));
3290
3291 begin
3292 if Nkind (Then_Stm) = N_Simple_Return_Statement
3293 and then
3294 Nkind (Else_Stm) = N_Simple_Return_Statement
3295 then
3296 declare
3297 Then_Expr : constant Node_Id := Expression (Then_Stm);
3298 Else_Expr : constant Node_Id := Expression (Else_Stm);
3299
3300 begin
3301 if Nkind (Then_Expr) = N_Identifier
3302 and then
3303 Nkind (Else_Expr) = N_Identifier
3304 then
3305 if Entity (Then_Expr) = Standard_True
3306 and then Entity (Else_Expr) = Standard_False
3307 then
3308 Rewrite (N,
3309 Make_Simple_Return_Statement (Loc,
3310 Expression => Relocate_Node (Condition (N))));
3311 Analyze (N);
3312 return;
3313
3314 elsif Entity (Then_Expr) = Standard_False
3315 and then Entity (Else_Expr) = Standard_True
3316 then
3317 Rewrite (N,
3318 Make_Simple_Return_Statement (Loc,
3319 Expression =>
3320 Make_Op_Not (Loc,
3321 Right_Opnd =>
3322 Relocate_Node (Condition (N)))));
3323 Analyze (N);
3324 return;
3325 end if;
3326 end if;
3327 end;
3328 end if;
3329 end;
3330 end if;
3331 end if;
3332 end Expand_N_If_Statement;
3333
3334 -----------------------------
3335 -- Expand_N_Loop_Statement --
3336 -----------------------------
3337
3338 -- 1. Remove null loop entirely
3339 -- 2. Deal with while condition for C/Fortran boolean
3340 -- 3. Deal with loops with a non-standard enumeration type range
3341 -- 4. Deal with while loops where Condition_Actions is set
3342 -- 5. Insert polling call if required
3343
3344 procedure Expand_N_Loop_Statement (N : Node_Id) is
3345 Loc : constant Source_Ptr := Sloc (N);
3346 Isc : constant Node_Id := Iteration_Scheme (N);
3347
3348 begin
3349 -- Delete null loop
3350
3351 if Is_Null_Loop (N) then
3352 Rewrite (N, Make_Null_Statement (Loc));
3353 return;
3354 end if;
3355
3356 -- Deal with condition for C/Fortran Boolean
3357
3358 if Present (Isc) then
3359 Adjust_Condition (Condition (Isc));
3360 end if;
3361
3362 -- Generate polling call
3363
3364 if Is_Non_Empty_List (Statements (N)) then
3365 Generate_Poll_Call (First (Statements (N)));
3366 end if;
3367
3368 -- Nothing more to do for plain loop with no iteration scheme
3369
3370 if No (Isc) then
3371 return;
3372 end if;
3373
3374 -- Note: we do not have to worry about validity checking of the for loop
3375 -- range bounds here, since they were frozen with constant declarations
3376 -- and it is during that process that the validity checking is done.
3377
3378 -- Handle the case where we have a for loop with the range type being an
3379 -- enumeration type with non-standard representation. In this case we
3380 -- expand:
3381
3382 -- for x in [reverse] a .. b loop
3383 -- ...
3384 -- end loop;
3385
3386 -- to
3387
3388 -- for xP in [reverse] integer
3389 -- range etype'Pos (a) .. etype'Pos (b) loop
3390 -- declare
3391 -- x : constant etype := Pos_To_Rep (xP);
3392 -- begin
3393 -- ...
3394 -- end;
3395 -- end loop;
3396
3397 if Present (Loop_Parameter_Specification (Isc)) then
3398 declare
3399 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
3400 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
3401 Ltype : constant Entity_Id := Etype (Loop_Id);
3402 Btype : constant Entity_Id := Base_Type (Ltype);
3403 Expr : Node_Id;
3404 New_Id : Entity_Id;
3405
3406 begin
3407 if not Is_Enumeration_Type (Btype)
3408 or else No (Enum_Pos_To_Rep (Btype))
3409 then
3410 return;
3411 end if;
3412
3413 New_Id :=
3414 Make_Defining_Identifier (Loc,
3415 Chars => New_External_Name (Chars (Loop_Id), 'P'));
3416
3417 -- If the type has a contiguous representation, successive values
3418 -- can be generated as offsets from the first literal.
3419
3420 if Has_Contiguous_Rep (Btype) then
3421 Expr :=
3422 Unchecked_Convert_To (Btype,
3423 Make_Op_Add (Loc,
3424 Left_Opnd =>
3425 Make_Integer_Literal (Loc,
3426 Enumeration_Rep (First_Literal (Btype))),
3427 Right_Opnd => New_Reference_To (New_Id, Loc)));
3428 else
3429 -- Use the constructed array Enum_Pos_To_Rep
3430
3431 Expr :=
3432 Make_Indexed_Component (Loc,
3433 Prefix => New_Reference_To (Enum_Pos_To_Rep (Btype), Loc),
3434 Expressions => New_List (New_Reference_To (New_Id, Loc)));
3435 end if;
3436
3437 Rewrite (N,
3438 Make_Loop_Statement (Loc,
3439 Identifier => Identifier (N),
3440
3441 Iteration_Scheme =>
3442 Make_Iteration_Scheme (Loc,
3443 Loop_Parameter_Specification =>
3444 Make_Loop_Parameter_Specification (Loc,
3445 Defining_Identifier => New_Id,
3446 Reverse_Present => Reverse_Present (LPS),
3447
3448 Discrete_Subtype_Definition =>
3449 Make_Subtype_Indication (Loc,
3450
3451 Subtype_Mark =>
3452 New_Reference_To (Standard_Natural, Loc),
3453
3454 Constraint =>
3455 Make_Range_Constraint (Loc,
3456 Range_Expression =>
3457 Make_Range (Loc,
3458
3459 Low_Bound =>
3460 Make_Attribute_Reference (Loc,
3461 Prefix =>
3462 New_Reference_To (Btype, Loc),
3463
3464 Attribute_Name => Name_Pos,
3465
3466 Expressions => New_List (
3467 Relocate_Node
3468 (Type_Low_Bound (Ltype)))),
3469
3470 High_Bound =>
3471 Make_Attribute_Reference (Loc,
3472 Prefix =>
3473 New_Reference_To (Btype, Loc),
3474
3475 Attribute_Name => Name_Pos,
3476
3477 Expressions => New_List (
3478 Relocate_Node
3479 (Type_High_Bound (Ltype))))))))),
3480
3481 Statements => New_List (
3482 Make_Block_Statement (Loc,
3483 Declarations => New_List (
3484 Make_Object_Declaration (Loc,
3485 Defining_Identifier => Loop_Id,
3486 Constant_Present => True,
3487 Object_Definition => New_Reference_To (Ltype, Loc),
3488 Expression => Expr)),
3489
3490 Handled_Statement_Sequence =>
3491 Make_Handled_Sequence_Of_Statements (Loc,
3492 Statements => Statements (N)))),
3493
3494 End_Label => End_Label (N)));
3495 Analyze (N);
3496 end;
3497
3498 -- Second case, if we have a while loop with Condition_Actions set, then
3499 -- we change it into a plain loop:
3500
3501 -- while C loop
3502 -- ...
3503 -- end loop;
3504
3505 -- changed to:
3506
3507 -- loop
3508 -- <<condition actions>>
3509 -- exit when not C;
3510 -- ...
3511 -- end loop
3512
3513 elsif Present (Isc)
3514 and then Present (Condition_Actions (Isc))
3515 then
3516 declare
3517 ES : Node_Id;
3518
3519 begin
3520 ES :=
3521 Make_Exit_Statement (Sloc (Condition (Isc)),
3522 Condition =>
3523 Make_Op_Not (Sloc (Condition (Isc)),
3524 Right_Opnd => Condition (Isc)));
3525
3526 Prepend (ES, Statements (N));
3527 Insert_List_Before (ES, Condition_Actions (Isc));
3528
3529 -- This is not an implicit loop, since it is generated in response
3530 -- to the loop statement being processed. If this is itself
3531 -- implicit, the restriction has already been checked. If not,
3532 -- it is an explicit loop.
3533
3534 Rewrite (N,
3535 Make_Loop_Statement (Sloc (N),
3536 Identifier => Identifier (N),
3537 Statements => Statements (N),
3538 End_Label => End_Label (N)));
3539
3540 Analyze (N);
3541 end;
3542 end if;
3543 end Expand_N_Loop_Statement;
3544
3545 --------------------------------------
3546 -- Expand_N_Simple_Return_Statement --
3547 --------------------------------------
3548
3549 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
3550 begin
3551 -- Defend against previous errors (i.e. the return statement calls a
3552 -- function that is not available in configurable runtime).
3553
3554 if Present (Expression (N))
3555 and then Nkind (Expression (N)) = N_Empty
3556 then
3557 return;
3558 end if;
3559
3560 -- Distinguish the function and non-function cases:
3561
3562 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
3563
3564 when E_Function |
3565 E_Generic_Function =>
3566 Expand_Simple_Function_Return (N);
3567
3568 when E_Procedure |
3569 E_Generic_Procedure |
3570 E_Entry |
3571 E_Entry_Family |
3572 E_Return_Statement =>
3573 Expand_Non_Function_Return (N);
3574
3575 when others =>
3576 raise Program_Error;
3577 end case;
3578
3579 exception
3580 when RE_Not_Available =>
3581 return;
3582 end Expand_N_Simple_Return_Statement;
3583
3584 --------------------------------
3585 -- Expand_Non_Function_Return --
3586 --------------------------------
3587
3588 procedure Expand_Non_Function_Return (N : Node_Id) is
3589 pragma Assert (No (Expression (N)));
3590
3591 Loc : constant Source_Ptr := Sloc (N);
3592 Scope_Id : Entity_Id :=
3593 Return_Applies_To (Return_Statement_Entity (N));
3594 Kind : constant Entity_Kind := Ekind (Scope_Id);
3595 Call : Node_Id;
3596 Acc_Stat : Node_Id;
3597 Goto_Stat : Node_Id;
3598 Lab_Node : Node_Id;
3599
3600 begin
3601 -- Call _Postconditions procedure if procedure with active
3602 -- postconditions. Here, we use the Postcondition_Proc attribute, which
3603 -- is needed for implicitly-generated returns. Functions never
3604 -- have implicitly-generated returns, and there's no room for
3605 -- Postcondition_Proc in E_Function, so we look up the identifier
3606 -- Name_uPostconditions for function returns (see
3607 -- Expand_Simple_Function_Return).
3608
3609 if Ekind (Scope_Id) = E_Procedure
3610 and then Has_Postconditions (Scope_Id)
3611 then
3612 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
3613 Insert_Action (N,
3614 Make_Procedure_Call_Statement (Loc,
3615 Name => New_Reference_To (Postcondition_Proc (Scope_Id), Loc)));
3616 end if;
3617
3618 -- If it is a return from a procedure do no extra steps
3619
3620 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
3621 return;
3622
3623 -- If it is a nested return within an extended one, replace it with a
3624 -- return of the previously declared return object.
3625
3626 elsif Kind = E_Return_Statement then
3627 Rewrite (N,
3628 Make_Simple_Return_Statement (Loc,
3629 Expression =>
3630 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
3631 Set_Comes_From_Extended_Return_Statement (N);
3632 Set_Return_Statement_Entity (N, Scope_Id);
3633 Expand_Simple_Function_Return (N);
3634 return;
3635 end if;
3636
3637 pragma Assert (Is_Entry (Scope_Id));
3638
3639 -- Look at the enclosing block to see whether the return is from an
3640 -- accept statement or an entry body.
3641
3642 for J in reverse 0 .. Scope_Stack.Last loop
3643 Scope_Id := Scope_Stack.Table (J).Entity;
3644 exit when Is_Concurrent_Type (Scope_Id);
3645 end loop;
3646
3647 -- If it is a return from accept statement it is expanded as call to
3648 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
3649
3650 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
3651 -- Expand_N_Accept_Alternative in exp_ch9.adb)
3652
3653 if Is_Task_Type (Scope_Id) then
3654
3655 Call :=
3656 Make_Procedure_Call_Statement (Loc,
3657 Name => New_Reference_To (RTE (RE_Complete_Rendezvous), Loc));
3658 Insert_Before (N, Call);
3659 -- why not insert actions here???
3660 Analyze (Call);
3661
3662 Acc_Stat := Parent (N);
3663 while Nkind (Acc_Stat) /= N_Accept_Statement loop
3664 Acc_Stat := Parent (Acc_Stat);
3665 end loop;
3666
3667 Lab_Node := Last (Statements
3668 (Handled_Statement_Sequence (Acc_Stat)));
3669
3670 Goto_Stat := Make_Goto_Statement (Loc,
3671 Name => New_Occurrence_Of
3672 (Entity (Identifier (Lab_Node)), Loc));
3673
3674 Set_Analyzed (Goto_Stat);
3675
3676 Rewrite (N, Goto_Stat);
3677 Analyze (N);
3678
3679 -- If it is a return from an entry body, put a Complete_Entry_Body call
3680 -- in front of the return.
3681
3682 elsif Is_Protected_Type (Scope_Id) then
3683 Call :=
3684 Make_Procedure_Call_Statement (Loc,
3685 Name =>
3686 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
3687 Parameter_Associations => New_List (
3688 Make_Attribute_Reference (Loc,
3689 Prefix =>
3690 New_Reference_To
3691 (Find_Protection_Object (Current_Scope), Loc),
3692 Attribute_Name =>
3693 Name_Unchecked_Access)));
3694
3695 Insert_Before (N, Call);
3696 Analyze (Call);
3697 end if;
3698 end Expand_Non_Function_Return;
3699
3700 -----------------------------------
3701 -- Expand_Simple_Function_Return --
3702 -----------------------------------
3703
3704 -- The "simple" comes from the syntax rule simple_return_statement.
3705 -- The semantics are not at all simple!
3706
3707 procedure Expand_Simple_Function_Return (N : Node_Id) is
3708 Loc : constant Source_Ptr := Sloc (N);
3709
3710 Scope_Id : constant Entity_Id :=
3711 Return_Applies_To (Return_Statement_Entity (N));
3712 -- The function we are returning from
3713
3714 R_Type : constant Entity_Id := Etype (Scope_Id);
3715 -- The result type of the function
3716
3717 Utyp : constant Entity_Id := Underlying_Type (R_Type);
3718
3719 Exp : constant Node_Id := Expression (N);
3720 pragma Assert (Present (Exp));
3721
3722 Exptyp : constant Entity_Id := Etype (Exp);
3723 -- The type of the expression (not necessarily the same as R_Type)
3724
3725 Subtype_Ind : Node_Id;
3726 -- If the result type of the function is class-wide and the
3727 -- expression has a specific type, then we use the expression's
3728 -- type as the type of the return object. In cases where the
3729 -- expression is an aggregate that is built in place, this avoids
3730 -- the need for an expensive conversion of the return object to
3731 -- the specific type on assignments to the individual components.
3732
3733 begin
3734 if Is_Class_Wide_Type (R_Type)
3735 and then not Is_Class_Wide_Type (Etype (Exp))
3736 then
3737 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
3738 else
3739 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
3740 end if;
3741
3742 -- For the case of a simple return that does not come from an extended
3743 -- return, in the case of Ada 2005 where we are returning a limited
3744 -- type, we rewrite "return <expression>;" to be:
3745
3746 -- return _anon_ : <return_subtype> := <expression>
3747
3748 -- The expansion produced by Expand_N_Extended_Return_Statement will
3749 -- contain simple return statements (for example, a block containing
3750 -- simple return of the return object), which brings us back here with
3751 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
3752 -- checking for a simple return that does not come from an extended
3753 -- return is to avoid this infinite recursion.
3754
3755 -- The reason for this design is that for Ada 2005 limited returns, we
3756 -- need to reify the return object, so we can build it "in place", and
3757 -- we need a block statement to hang finalization and tasking stuff.
3758
3759 -- ??? In order to avoid disruption, we avoid translating to extended
3760 -- return except in the cases where we really need to (Ada 2005 for
3761 -- inherently limited). We might prefer to do this translation in all
3762 -- cases (except perhaps for the case of Ada 95 inherently limited),
3763 -- in order to fully exercise the Expand_N_Extended_Return_Statement
3764 -- code. This would also allow us to do the build-in-place optimization
3765 -- for efficiency even in cases where it is semantically not required.
3766
3767 -- As before, we check the type of the return expression rather than the
3768 -- return type of the function, because the latter may be a limited
3769 -- class-wide interface type, which is not a limited type, even though
3770 -- the type of the expression may be.
3771
3772 if not Comes_From_Extended_Return_Statement (N)
3773 and then Is_Inherently_Limited_Type (Etype (Expression (N)))
3774 and then Ada_Version >= Ada_05
3775 and then not Debug_Flag_Dot_L
3776 then
3777 declare
3778 Return_Object_Entity : constant Entity_Id :=
3779 Make_Defining_Identifier (Loc,
3780 New_Internal_Name ('R'));
3781 Obj_Decl : constant Node_Id :=
3782 Make_Object_Declaration (Loc,
3783 Defining_Identifier => Return_Object_Entity,
3784 Object_Definition => Subtype_Ind,
3785 Expression => Exp);
3786
3787 Ext : constant Node_Id := Make_Extended_Return_Statement (Loc,
3788 Return_Object_Declarations => New_List (Obj_Decl));
3789 -- Do not perform this high-level optimization if the result type
3790 -- is an interface because the "this" pointer must be displaced.
3791
3792 begin
3793 Rewrite (N, Ext);
3794 Analyze (N);
3795 return;
3796 end;
3797 end if;
3798
3799 -- Here we have a simple return statement that is part of the expansion
3800 -- of an extended return statement (either written by the user, or
3801 -- generated by the above code).
3802
3803 -- Always normalize C/Fortran boolean result. This is not always needed,
3804 -- but it seems a good idea to minimize the passing around of non-
3805 -- normalized values, and in any case this handles the processing of
3806 -- barrier functions for protected types, which turn the condition into
3807 -- a return statement.
3808
3809 if Is_Boolean_Type (Exptyp)
3810 and then Nonzero_Is_True (Exptyp)
3811 then
3812 Adjust_Condition (Exp);
3813 Adjust_Result_Type (Exp, Exptyp);
3814 end if;
3815
3816 -- Do validity check if enabled for returns
3817
3818 if Validity_Checks_On
3819 and then Validity_Check_Returns
3820 then
3821 Ensure_Valid (Exp);
3822 end if;
3823
3824 -- Check the result expression of a scalar function against the subtype
3825 -- of the function by inserting a conversion. This conversion must
3826 -- eventually be performed for other classes of types, but for now it's
3827 -- only done for scalars.
3828 -- ???
3829
3830 if Is_Scalar_Type (Exptyp) then
3831 Rewrite (Exp, Convert_To (R_Type, Exp));
3832 Analyze (Exp);
3833 end if;
3834
3835 -- Deal with returning variable length objects and controlled types
3836
3837 -- Nothing to do if we are returning by reference, or this is not a
3838 -- type that requires special processing (indicated by the fact that
3839 -- it requires a cleanup scope for the secondary stack case).
3840
3841 if Is_Inherently_Limited_Type (Exptyp)
3842 or else Is_Limited_Interface (Exptyp)
3843 then
3844 null;
3845
3846 elsif not Requires_Transient_Scope (R_Type) then
3847
3848 -- Mutable records with no variable length components are not
3849 -- returned on the sec-stack, so we need to make sure that the
3850 -- backend will only copy back the size of the actual value, and not
3851 -- the maximum size. We create an actual subtype for this purpose.
3852
3853 declare
3854 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
3855 Decl : Node_Id;
3856 Ent : Entity_Id;
3857 begin
3858 if Has_Discriminants (Ubt)
3859 and then not Is_Constrained (Ubt)
3860 and then not Has_Unchecked_Union (Ubt)
3861 then
3862 Decl := Build_Actual_Subtype (Ubt, Exp);
3863 Ent := Defining_Identifier (Decl);
3864 Insert_Action (Exp, Decl);
3865 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
3866 Analyze_And_Resolve (Exp);
3867 end if;
3868 end;
3869
3870 -- Here if secondary stack is used
3871
3872 else
3873 -- Make sure that no surrounding block will reclaim the secondary
3874 -- stack on which we are going to put the result. Not only may this
3875 -- introduce secondary stack leaks but worse, if the reclamation is
3876 -- done too early, then the result we are returning may get
3877 -- clobbered.
3878
3879 declare
3880 S : Entity_Id;
3881 begin
3882 S := Current_Scope;
3883 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
3884 Set_Sec_Stack_Needed_For_Return (S, True);
3885 S := Enclosing_Dynamic_Scope (S);
3886 end loop;
3887 end;
3888
3889 -- Optimize the case where the result is a function call. In this
3890 -- case either the result is already on the secondary stack, or is
3891 -- already being returned with the stack pointer depressed and no
3892 -- further processing is required except to set the By_Ref flag to
3893 -- ensure that gigi does not attempt an extra unnecessary copy.
3894 -- (actually not just unnecessary but harmfully wrong in the case
3895 -- of a controlled type, where gigi does not know how to do a copy).
3896 -- To make up for a gcc 2.8.1 deficiency (???), we perform
3897 -- the copy for array types if the constrained status of the
3898 -- target type is different from that of the expression.
3899
3900 if Requires_Transient_Scope (Exptyp)
3901 and then
3902 (not Is_Array_Type (Exptyp)
3903 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
3904 or else CW_Or_Has_Controlled_Part (Utyp))
3905 and then Nkind (Exp) = N_Function_Call
3906 then
3907 Set_By_Ref (N);
3908
3909 -- Remove side effects from the expression now so that other parts
3910 -- of the expander do not have to reanalyze this node without this
3911 -- optimization
3912
3913 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
3914
3915 -- For controlled types, do the allocation on the secondary stack
3916 -- manually in order to call adjust at the right time:
3917
3918 -- type Anon1 is access R_Type;
3919 -- for Anon1'Storage_pool use ss_pool;
3920 -- Anon2 : anon1 := new R_Type'(expr);
3921 -- return Anon2.all;
3922
3923 -- We do the same for classwide types that are not potentially
3924 -- controlled (by the virtue of restriction No_Finalization) because
3925 -- gigi is not able to properly allocate class-wide types.
3926
3927 elsif CW_Or_Has_Controlled_Part (Utyp) then
3928 declare
3929 Loc : constant Source_Ptr := Sloc (N);
3930 Temp : constant Entity_Id :=
3931 Make_Defining_Identifier (Loc,
3932 Chars => New_Internal_Name ('R'));
3933 Acc_Typ : constant Entity_Id :=
3934 Make_Defining_Identifier (Loc,
3935 Chars => New_Internal_Name ('A'));
3936 Alloc_Node : Node_Id;
3937
3938 begin
3939 Set_Ekind (Acc_Typ, E_Access_Type);
3940
3941 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
3942
3943 -- This is an allocator for the secondary stack, and it's fine
3944 -- to have Comes_From_Source set False on it, as gigi knows not
3945 -- to flag it as a violation of No_Implicit_Heap_Allocations.
3946
3947 Alloc_Node :=
3948 Make_Allocator (Loc,
3949 Expression =>
3950 Make_Qualified_Expression (Loc,
3951 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
3952 Expression => Relocate_Node (Exp)));
3953
3954 -- We do not want discriminant checks on the declaration,
3955 -- given that it gets its value from the allocator.
3956
3957 Set_No_Initialization (Alloc_Node);
3958
3959 Insert_List_Before_And_Analyze (N, New_List (
3960 Make_Full_Type_Declaration (Loc,
3961 Defining_Identifier => Acc_Typ,
3962 Type_Definition =>
3963 Make_Access_To_Object_Definition (Loc,
3964 Subtype_Indication => Subtype_Ind)),
3965
3966 Make_Object_Declaration (Loc,
3967 Defining_Identifier => Temp,
3968 Object_Definition => New_Reference_To (Acc_Typ, Loc),
3969 Expression => Alloc_Node)));
3970
3971 Rewrite (Exp,
3972 Make_Explicit_Dereference (Loc,
3973 Prefix => New_Reference_To (Temp, Loc)));
3974
3975 Analyze_And_Resolve (Exp, R_Type);
3976 end;
3977
3978 -- Otherwise use the gigi mechanism to allocate result on the
3979 -- secondary stack.
3980
3981 else
3982 Check_Restriction (No_Secondary_Stack, N);
3983 Set_Storage_Pool (N, RTE (RE_SS_Pool));
3984
3985 -- If we are generating code for the VM do not use
3986 -- SS_Allocate since everything is heap-allocated anyway.
3987
3988 if VM_Target = No_VM then
3989 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3990 end if;
3991 end if;
3992 end if;
3993
3994 -- Implement the rules of 6.5(8-10), which require a tag check in the
3995 -- case of a limited tagged return type, and tag reassignment for
3996 -- nonlimited tagged results. These actions are needed when the return
3997 -- type is a specific tagged type and the result expression is a
3998 -- conversion or a formal parameter, because in that case the tag of the
3999 -- expression might differ from the tag of the specific result type.
4000
4001 if Is_Tagged_Type (Utyp)
4002 and then not Is_Class_Wide_Type (Utyp)
4003 and then (Nkind_In (Exp, N_Type_Conversion,
4004 N_Unchecked_Type_Conversion)
4005 or else (Is_Entity_Name (Exp)
4006 and then Ekind (Entity (Exp)) in Formal_Kind))
4007 then
4008 -- When the return type is limited, perform a check that the
4009 -- tag of the result is the same as the tag of the return type.
4010
4011 if Is_Limited_Type (R_Type) then
4012 Insert_Action (Exp,
4013 Make_Raise_Constraint_Error (Loc,
4014 Condition =>
4015 Make_Op_Ne (Loc,
4016 Left_Opnd =>
4017 Make_Selected_Component (Loc,
4018 Prefix => Duplicate_Subexpr (Exp),
4019 Selector_Name =>
4020 New_Reference_To (First_Tag_Component (Utyp), Loc)),
4021 Right_Opnd =>
4022 Unchecked_Convert_To (RTE (RE_Tag),
4023 New_Reference_To
4024 (Node (First_Elmt
4025 (Access_Disp_Table (Base_Type (Utyp)))),
4026 Loc))),
4027 Reason => CE_Tag_Check_Failed));
4028
4029 -- If the result type is a specific nonlimited tagged type, then we
4030 -- have to ensure that the tag of the result is that of the result
4031 -- type. This is handled by making a copy of the expression in the
4032 -- case where it might have a different tag, namely when the
4033 -- expression is a conversion or a formal parameter. We create a new
4034 -- object of the result type and initialize it from the expression,
4035 -- which will implicitly force the tag to be set appropriately.
4036
4037 else
4038 declare
4039 Result_Id : constant Entity_Id :=
4040 Make_Defining_Identifier (Loc,
4041 Chars => New_Internal_Name ('R'));
4042 Result_Exp : constant Node_Id :=
4043 New_Reference_To (Result_Id, Loc);
4044 Result_Obj : constant Node_Id :=
4045 Make_Object_Declaration (Loc,
4046 Defining_Identifier => Result_Id,
4047 Object_Definition =>
4048 New_Reference_To (R_Type, Loc),
4049 Constant_Present => True,
4050 Expression => Relocate_Node (Exp));
4051
4052 begin
4053 Set_Assignment_OK (Result_Obj);
4054 Insert_Action (Exp, Result_Obj);
4055
4056 Rewrite (Exp, Result_Exp);
4057 Analyze_And_Resolve (Exp, R_Type);
4058 end;
4059 end if;
4060
4061 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
4062 -- a check that the level of the return expression's underlying type
4063 -- is not deeper than the level of the master enclosing the function.
4064 -- Always generate the check when the type of the return expression
4065 -- is class-wide, when it's a type conversion, or when it's a formal
4066 -- parameter. Otherwise, suppress the check in the case where the
4067 -- return expression has a specific type whose level is known not to
4068 -- be statically deeper than the function's result type.
4069
4070 -- Note: accessibility check is skipped in the VM case, since there
4071 -- does not seem to be any practical way to implement this check.
4072
4073 elsif Ada_Version >= Ada_05
4074 and then VM_Target = No_VM
4075 and then Is_Class_Wide_Type (R_Type)
4076 and then not Scope_Suppress (Accessibility_Check)
4077 and then
4078 (Is_Class_Wide_Type (Etype (Exp))
4079 or else Nkind_In (Exp, N_Type_Conversion,
4080 N_Unchecked_Type_Conversion)
4081 or else (Is_Entity_Name (Exp)
4082 and then Ekind (Entity (Exp)) in Formal_Kind)
4083 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
4084 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
4085 then
4086 declare
4087 Tag_Node : Node_Id;
4088
4089 begin
4090 -- Ada 2005 (AI-251): In class-wide interface objects we displace
4091 -- "this" to reference the base of the object --- required to get
4092 -- access to the TSD of the object.
4093
4094 if Is_Class_Wide_Type (Etype (Exp))
4095 and then Is_Interface (Etype (Exp))
4096 and then Nkind (Exp) = N_Explicit_Dereference
4097 then
4098 Tag_Node :=
4099 Make_Explicit_Dereference (Loc,
4100 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4101 Make_Function_Call (Loc,
4102 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
4103 Parameter_Associations => New_List (
4104 Unchecked_Convert_To (RTE (RE_Address),
4105 Duplicate_Subexpr (Prefix (Exp)))))));
4106 else
4107 Tag_Node :=
4108 Make_Attribute_Reference (Loc,
4109 Prefix => Duplicate_Subexpr (Exp),
4110 Attribute_Name => Name_Tag);
4111 end if;
4112
4113 Insert_Action (Exp,
4114 Make_Raise_Program_Error (Loc,
4115 Condition =>
4116 Make_Op_Gt (Loc,
4117 Left_Opnd =>
4118 Build_Get_Access_Level (Loc, Tag_Node),
4119 Right_Opnd =>
4120 Make_Integer_Literal (Loc,
4121 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
4122 Reason => PE_Accessibility_Check_Failed));
4123 end;
4124 end if;
4125
4126 -- If we are returning an object that may not be bit-aligned, then
4127 -- copy the value into a temporary first. This copy may need to expand
4128 -- to a loop of component operations..
4129
4130 if Is_Possibly_Unaligned_Slice (Exp)
4131 or else Is_Possibly_Unaligned_Object (Exp)
4132 then
4133 declare
4134 Tnn : constant Entity_Id :=
4135 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
4136 begin
4137 Insert_Action (Exp,
4138 Make_Object_Declaration (Loc,
4139 Defining_Identifier => Tnn,
4140 Constant_Present => True,
4141 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4142 Expression => Relocate_Node (Exp)),
4143 Suppress => All_Checks);
4144 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4145 end;
4146 end if;
4147
4148 -- Generate call to postcondition checks if they are present
4149
4150 if Ekind (Scope_Id) = E_Function
4151 and then Has_Postconditions (Scope_Id)
4152 then
4153 -- We are going to reference the returned value twice in this case,
4154 -- once in the call to _Postconditions, and once in the actual return
4155 -- statement, but we can't have side effects happening twice, and in
4156 -- any case for efficiency we don't want to do the computation twice.
4157
4158 -- If the returned expression is an entity name, we don't need to
4159 -- worry since it is efficient and safe to reference it twice, that's
4160 -- also true for literals other than string literals, and for the
4161 -- case of X.all where X is an entity name.
4162
4163 if Is_Entity_Name (Exp)
4164 or else Nkind_In (Exp, N_Character_Literal,
4165 N_Integer_Literal,
4166 N_Real_Literal)
4167 or else (Nkind (Exp) = N_Explicit_Dereference
4168 and then Is_Entity_Name (Prefix (Exp)))
4169 then
4170 null;
4171
4172 -- Otherwise we are going to need a temporary to capture the value
4173
4174 else
4175 declare
4176 Tnn : constant Entity_Id :=
4177 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
4178
4179 begin
4180 -- For a complex expression of an elementary type, capture
4181 -- value in the temporary and use it as the reference.
4182
4183 if Is_Elementary_Type (R_Type) then
4184 Insert_Action (Exp,
4185 Make_Object_Declaration (Loc,
4186 Defining_Identifier => Tnn,
4187 Constant_Present => True,
4188 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4189 Expression => Relocate_Node (Exp)),
4190 Suppress => All_Checks);
4191
4192 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4193
4194 -- If we have something we can rename, generate a renaming of
4195 -- the object and replace the expression with a reference
4196
4197 elsif Is_Object_Reference (Exp) then
4198 Insert_Action (Exp,
4199 Make_Object_Renaming_Declaration (Loc,
4200 Defining_Identifier => Tnn,
4201 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
4202 Name => Relocate_Node (Exp)),
4203 Suppress => All_Checks);
4204
4205 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4206
4207 -- Otherwise we have something like a string literal or an
4208 -- aggregate. We could copy the value, but that would be
4209 -- inefficient. Instead we make a reference to the value and
4210 -- capture this reference with a renaming, the expression is
4211 -- then replaced by a dereference of this renaming.
4212
4213 else
4214 -- For now, copy the value, since the code below does not
4215 -- seem to work correctly ???
4216
4217 Insert_Action (Exp,
4218 Make_Object_Declaration (Loc,
4219 Defining_Identifier => Tnn,
4220 Constant_Present => True,
4221 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4222 Expression => Relocate_Node (Exp)),
4223 Suppress => All_Checks);
4224
4225 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4226
4227 -- Insert_Action (Exp,
4228 -- Make_Object_Renaming_Declaration (Loc,
4229 -- Defining_Identifier => Tnn,
4230 -- Access_Definition =>
4231 -- Make_Access_Definition (Loc,
4232 -- All_Present => True,
4233 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
4234 -- Name =>
4235 -- Make_Reference (Loc,
4236 -- Prefix => Relocate_Node (Exp))),
4237 -- Suppress => All_Checks);
4238
4239 -- Rewrite (Exp,
4240 -- Make_Explicit_Dereference (Loc,
4241 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
4242 end if;
4243 end;
4244 end if;
4245
4246 -- Generate call to _postconditions
4247
4248 Insert_Action (Exp,
4249 Make_Procedure_Call_Statement (Loc,
4250 Name => Make_Identifier (Loc, Name_uPostconditions),
4251 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
4252 end if;
4253
4254 -- Ada 2005 (AI-251): If this return statement corresponds with an
4255 -- simple return statement associated with an extended return statement
4256 -- and the type of the returned object is an interface then generate an
4257 -- implicit conversion to force displacement of the "this" pointer.
4258
4259 if Ada_Version >= Ada_05
4260 and then Comes_From_Extended_Return_Statement (N)
4261 and then Nkind (Expression (N)) = N_Identifier
4262 and then Is_Interface (Utyp)
4263 and then Utyp /= Underlying_Type (Exptyp)
4264 then
4265 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
4266 Analyze_And_Resolve (Exp);
4267 end if;
4268 end Expand_Simple_Function_Return;
4269
4270 ------------------------------
4271 -- Make_Tag_Ctrl_Assignment --
4272 ------------------------------
4273
4274 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id is
4275 Loc : constant Source_Ptr := Sloc (N);
4276 L : constant Node_Id := Name (N);
4277 T : constant Entity_Id := Underlying_Type (Etype (L));
4278
4279 Ctrl_Act : constant Boolean := Needs_Finalization (T)
4280 and then not No_Ctrl_Actions (N);
4281
4282 Save_Tag : constant Boolean := Is_Tagged_Type (T)
4283 and then not No_Ctrl_Actions (N)
4284 and then VM_Target = No_VM;
4285 -- Tags are not saved and restored when VM_Target because VM tags are
4286 -- represented implicitly in objects.
4287
4288 Res : List_Id;
4289 Tag_Tmp : Entity_Id;
4290
4291 Prev_Tmp : Entity_Id;
4292 Next_Tmp : Entity_Id;
4293 Ctrl_Ref : Node_Id;
4294
4295 begin
4296 Res := New_List;
4297
4298 -- Finalize the target of the assignment when controlled.
4299 -- We have two exceptions here:
4300
4301 -- 1. If we are in an init proc since it is an initialization
4302 -- more than an assignment
4303
4304 -- 2. If the left-hand side is a temporary that was not initialized
4305 -- (or the parent part of a temporary since it is the case in
4306 -- extension aggregates). Such a temporary does not come from
4307 -- source. We must examine the original node for the prefix, because
4308 -- it may be a component of an entry formal, in which case it has
4309 -- been rewritten and does not appear to come from source either.
4310
4311 -- Case of init proc
4312
4313 if not Ctrl_Act then
4314 null;
4315
4316 -- The left hand side is an uninitialized temporary object
4317
4318 elsif Nkind (L) = N_Type_Conversion
4319 and then Is_Entity_Name (Expression (L))
4320 and then Nkind (Parent (Entity (Expression (L))))
4321 = N_Object_Declaration
4322 and then No_Initialization (Parent (Entity (Expression (L))))
4323 then
4324 null;
4325
4326 else
4327 Append_List_To (Res,
4328 Make_Final_Call (
4329 Ref => Duplicate_Subexpr_No_Checks (L),
4330 Typ => Etype (L),
4331 With_Detach => New_Reference_To (Standard_False, Loc)));
4332 end if;
4333
4334 -- Save the Tag in a local variable Tag_Tmp
4335
4336 if Save_Tag then
4337 Tag_Tmp :=
4338 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4339
4340 Append_To (Res,
4341 Make_Object_Declaration (Loc,
4342 Defining_Identifier => Tag_Tmp,
4343 Object_Definition => New_Reference_To (RTE (RE_Tag), Loc),
4344 Expression =>
4345 Make_Selected_Component (Loc,
4346 Prefix => Duplicate_Subexpr_No_Checks (L),
4347 Selector_Name => New_Reference_To (First_Tag_Component (T),
4348 Loc))));
4349
4350 -- Otherwise Tag_Tmp not used
4351
4352 else
4353 Tag_Tmp := Empty;
4354 end if;
4355
4356 if Ctrl_Act then
4357 if VM_Target /= No_VM then
4358
4359 -- Cannot assign part of the object in a VM context, so instead
4360 -- fallback to the previous mechanism, even though it is not
4361 -- completely correct ???
4362
4363 -- Save the Finalization Pointers in local variables Prev_Tmp and
4364 -- Next_Tmp. For objects with Has_Controlled_Component set, these
4365 -- pointers are in the Record_Controller
4366
4367 Ctrl_Ref := Duplicate_Subexpr (L);
4368
4369 if Has_Controlled_Component (T) then
4370 Ctrl_Ref :=
4371 Make_Selected_Component (Loc,
4372 Prefix => Ctrl_Ref,
4373 Selector_Name =>
4374 New_Reference_To (Controller_Component (T), Loc));
4375 end if;
4376
4377 Prev_Tmp :=
4378 Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
4379
4380 Append_To (Res,
4381 Make_Object_Declaration (Loc,
4382 Defining_Identifier => Prev_Tmp,
4383
4384 Object_Definition =>
4385 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
4386
4387 Expression =>
4388 Make_Selected_Component (Loc,
4389 Prefix =>
4390 Unchecked_Convert_To (RTE (RE_Finalizable), Ctrl_Ref),
4391 Selector_Name => Make_Identifier (Loc, Name_Prev))));
4392
4393 Next_Tmp :=
4394 Make_Defining_Identifier (Loc,
4395 Chars => New_Internal_Name ('C'));
4396
4397 Append_To (Res,
4398 Make_Object_Declaration (Loc,
4399 Defining_Identifier => Next_Tmp,
4400
4401 Object_Definition =>
4402 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
4403
4404 Expression =>
4405 Make_Selected_Component (Loc,
4406 Prefix =>
4407 Unchecked_Convert_To (RTE (RE_Finalizable),
4408 New_Copy_Tree (Ctrl_Ref)),
4409 Selector_Name => Make_Identifier (Loc, Name_Next))));
4410
4411 -- Do the Assignment
4412
4413 Append_To (Res, Relocate_Node (N));
4414
4415 else
4416 -- Regular (non VM) processing for controlled types and types with
4417 -- controlled components
4418
4419 -- Variables of such types contain pointers used to chain them in
4420 -- finalization lists, in addition to user data. These pointers
4421 -- are specific to each object of the type, not to the value being
4422 -- assigned.
4423
4424 -- Thus they need to be left intact during the assignment. We
4425 -- achieve this by constructing a Storage_Array subtype, and by
4426 -- overlaying objects of this type on the source and target of the
4427 -- assignment. The assignment is then rewritten to assignments of
4428 -- slices of these arrays, copying the user data, and leaving the
4429 -- pointers untouched.
4430
4431 Controlled_Actions : declare
4432 Prev_Ref : Node_Id;
4433 -- A reference to the Prev component of the record controller
4434
4435 First_After_Root : Node_Id := Empty;
4436 -- Index of first byte to be copied (used to skip
4437 -- Root_Controlled in controlled objects).
4438
4439 Last_Before_Hole : Node_Id := Empty;
4440 -- Index of last byte to be copied before outermost record
4441 -- controller data.
4442
4443 Hole_Length : Node_Id := Empty;
4444 -- Length of record controller data (Prev and Next pointers)
4445
4446 First_After_Hole : Node_Id := Empty;
4447 -- Index of first byte to be copied after outermost record
4448 -- controller data.
4449
4450 Expr, Source_Size : Node_Id;
4451 Source_Actual_Subtype : Entity_Id;
4452 -- Used for computation of the size of the data to be copied
4453
4454 Range_Type : Entity_Id;
4455 Opaque_Type : Entity_Id;
4456
4457 function Build_Slice
4458 (Rec : Entity_Id;
4459 Lo : Node_Id;
4460 Hi : Node_Id) return Node_Id;
4461 -- Build and return a slice of an array of type S overlaid on
4462 -- object Rec, with bounds specified by Lo and Hi. If either
4463 -- bound is empty, a default of S'First (respectively S'Last)
4464 -- is used.
4465
4466 -----------------
4467 -- Build_Slice --
4468 -----------------
4469
4470 function Build_Slice
4471 (Rec : Node_Id;
4472 Lo : Node_Id;
4473 Hi : Node_Id) return Node_Id
4474 is
4475 Lo_Bound : Node_Id;
4476 Hi_Bound : Node_Id;
4477
4478 Opaque : constant Node_Id :=
4479 Unchecked_Convert_To (Opaque_Type,
4480 Make_Attribute_Reference (Loc,
4481 Prefix => Rec,
4482 Attribute_Name => Name_Address));
4483 -- Access value designating an opaque storage array of type
4484 -- S overlaid on record Rec.
4485
4486 begin
4487 -- Compute slice bounds using S'First (1) and S'Last as
4488 -- default values when not specified by the caller.
4489
4490 if No (Lo) then
4491 Lo_Bound := Make_Integer_Literal (Loc, 1);
4492 else
4493 Lo_Bound := Lo;
4494 end if;
4495
4496 if No (Hi) then
4497 Hi_Bound := Make_Attribute_Reference (Loc,
4498 Prefix => New_Occurrence_Of (Range_Type, Loc),
4499 Attribute_Name => Name_Last);
4500 else
4501 Hi_Bound := Hi;
4502 end if;
4503
4504 return Make_Slice (Loc,
4505 Prefix =>
4506 Opaque,
4507 Discrete_Range => Make_Range (Loc,
4508 Lo_Bound, Hi_Bound));
4509 end Build_Slice;
4510
4511 -- Start of processing for Controlled_Actions
4512
4513 begin
4514 -- Create a constrained subtype of Storage_Array whose size
4515 -- corresponds to the value being assigned.
4516
4517 -- subtype G is Storage_Offset range
4518 -- 1 .. (Expr'Size + Storage_Unit - 1) / Storage_Unit
4519
4520 Expr := Duplicate_Subexpr_No_Checks (Expression (N));
4521
4522 if Nkind (Expr) = N_Qualified_Expression then
4523 Expr := Expression (Expr);
4524 end if;
4525
4526 Source_Actual_Subtype := Etype (Expr);
4527
4528 if Has_Discriminants (Source_Actual_Subtype)
4529 and then not Is_Constrained (Source_Actual_Subtype)
4530 then
4531 Append_To (Res,
4532 Build_Actual_Subtype (Source_Actual_Subtype, Expr));
4533 Source_Actual_Subtype := Defining_Identifier (Last (Res));
4534 end if;
4535
4536 Source_Size :=
4537 Make_Op_Add (Loc,
4538 Left_Opnd =>
4539 Make_Attribute_Reference (Loc,
4540 Prefix =>
4541 New_Occurrence_Of (Source_Actual_Subtype, Loc),
4542 Attribute_Name => Name_Size),
4543 Right_Opnd =>
4544 Make_Integer_Literal (Loc,
4545 Intval => System_Storage_Unit - 1));
4546
4547 Source_Size :=
4548 Make_Op_Divide (Loc,
4549 Left_Opnd => Source_Size,
4550 Right_Opnd =>
4551 Make_Integer_Literal (Loc,
4552 Intval => System_Storage_Unit));
4553
4554 Range_Type :=
4555 Make_Defining_Identifier (Loc,
4556 New_Internal_Name ('G'));
4557
4558 Append_To (Res,
4559 Make_Subtype_Declaration (Loc,
4560 Defining_Identifier => Range_Type,
4561 Subtype_Indication =>
4562 Make_Subtype_Indication (Loc,
4563 Subtype_Mark =>
4564 New_Reference_To (RTE (RE_Storage_Offset), Loc),
4565 Constraint => Make_Range_Constraint (Loc,
4566 Range_Expression =>
4567 Make_Range (Loc,
4568 Low_Bound => Make_Integer_Literal (Loc, 1),
4569 High_Bound => Source_Size)))));
4570
4571 -- subtype S is Storage_Array (G)
4572
4573 Append_To (Res,
4574 Make_Subtype_Declaration (Loc,
4575 Defining_Identifier =>
4576 Make_Defining_Identifier (Loc,
4577 New_Internal_Name ('S')),
4578 Subtype_Indication =>
4579 Make_Subtype_Indication (Loc,
4580 Subtype_Mark =>
4581 New_Reference_To (RTE (RE_Storage_Array), Loc),
4582 Constraint =>
4583 Make_Index_Or_Discriminant_Constraint (Loc,
4584 Constraints =>
4585 New_List (New_Reference_To (Range_Type, Loc))))));
4586
4587 -- type A is access S
4588
4589 Opaque_Type :=
4590 Make_Defining_Identifier (Loc,
4591 Chars => New_Internal_Name ('A'));
4592
4593 Append_To (Res,
4594 Make_Full_Type_Declaration (Loc,
4595 Defining_Identifier => Opaque_Type,
4596 Type_Definition =>
4597 Make_Access_To_Object_Definition (Loc,
4598 Subtype_Indication =>
4599 New_Occurrence_Of (
4600 Defining_Identifier (Last (Res)), Loc))));
4601
4602 -- Generate appropriate slice assignments
4603
4604 First_After_Root := Make_Integer_Literal (Loc, 1);
4605
4606 -- For the case of a controlled object, skip the
4607 -- Root_Controlled part.
4608
4609 if Is_Controlled (T) then
4610 First_After_Root :=
4611 Make_Op_Add (Loc,
4612 First_After_Root,
4613 Make_Op_Divide (Loc,
4614 Make_Attribute_Reference (Loc,
4615 Prefix =>
4616 New_Occurrence_Of (RTE (RE_Root_Controlled), Loc),
4617 Attribute_Name => Name_Size),
4618 Make_Integer_Literal (Loc, System_Storage_Unit)));
4619 end if;
4620
4621 -- For the case of a record with controlled components, skip
4622 -- the Prev and Next components of the record controller.
4623 -- These components constitute a 'hole' in the middle of the
4624 -- data to be copied.
4625
4626 if Has_Controlled_Component (T) then
4627 Prev_Ref :=
4628 Make_Selected_Component (Loc,
4629 Prefix =>
4630 Make_Selected_Component (Loc,
4631 Prefix => Duplicate_Subexpr_No_Checks (L),
4632 Selector_Name =>
4633 New_Reference_To (Controller_Component (T), Loc)),
4634 Selector_Name => Make_Identifier (Loc, Name_Prev));
4635
4636 -- Last index before hole: determined by position of
4637 -- the _Controller.Prev component.
4638
4639 Last_Before_Hole :=
4640 Make_Defining_Identifier (Loc,
4641 New_Internal_Name ('L'));
4642
4643 Append_To (Res,
4644 Make_Object_Declaration (Loc,
4645 Defining_Identifier => Last_Before_Hole,
4646 Object_Definition => New_Occurrence_Of (
4647 RTE (RE_Storage_Offset), Loc),
4648 Constant_Present => True,
4649 Expression => Make_Op_Add (Loc,
4650 Make_Attribute_Reference (Loc,
4651 Prefix => Prev_Ref,
4652 Attribute_Name => Name_Position),
4653 Make_Attribute_Reference (Loc,
4654 Prefix => New_Copy_Tree (Prefix (Prev_Ref)),
4655 Attribute_Name => Name_Position))));
4656
4657 -- Hole length: size of the Prev and Next components
4658
4659 Hole_Length :=
4660 Make_Op_Multiply (Loc,
4661 Left_Opnd => Make_Integer_Literal (Loc, Uint_2),
4662 Right_Opnd =>
4663 Make_Op_Divide (Loc,
4664 Left_Opnd =>
4665 Make_Attribute_Reference (Loc,
4666 Prefix => New_Copy_Tree (Prev_Ref),
4667 Attribute_Name => Name_Size),
4668 Right_Opnd =>
4669 Make_Integer_Literal (Loc,
4670 Intval => System_Storage_Unit)));
4671
4672 -- First index after hole
4673
4674 First_After_Hole :=
4675 Make_Defining_Identifier (Loc,
4676 New_Internal_Name ('F'));
4677
4678 Append_To (Res,
4679 Make_Object_Declaration (Loc,
4680 Defining_Identifier => First_After_Hole,
4681 Object_Definition => New_Occurrence_Of (
4682 RTE (RE_Storage_Offset), Loc),
4683 Constant_Present => True,
4684 Expression =>
4685 Make_Op_Add (Loc,
4686 Left_Opnd =>
4687 Make_Op_Add (Loc,
4688 Left_Opnd =>
4689 New_Occurrence_Of (Last_Before_Hole, Loc),
4690 Right_Opnd => Hole_Length),
4691 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4692
4693 Last_Before_Hole :=
4694 New_Occurrence_Of (Last_Before_Hole, Loc);
4695 First_After_Hole :=
4696 New_Occurrence_Of (First_After_Hole, Loc);
4697 end if;
4698
4699 -- Assign the first slice (possibly skipping Root_Controlled,
4700 -- up to the beginning of the record controller if present,
4701 -- up to the end of the object if not).
4702
4703 Append_To (Res, Make_Assignment_Statement (Loc,
4704 Name => Build_Slice (
4705 Rec => Duplicate_Subexpr_No_Checks (L),
4706 Lo => First_After_Root,
4707 Hi => Last_Before_Hole),
4708
4709 Expression => Build_Slice (
4710 Rec => Expression (N),
4711 Lo => First_After_Root,
4712 Hi => New_Copy_Tree (Last_Before_Hole))));
4713
4714 if Present (First_After_Hole) then
4715
4716 -- If a record controller is present, copy the second slice,
4717 -- from right after the _Controller.Next component up to the
4718 -- end of the object.
4719
4720 Append_To (Res, Make_Assignment_Statement (Loc,
4721 Name => Build_Slice (
4722 Rec => Duplicate_Subexpr_No_Checks (L),
4723 Lo => First_After_Hole,
4724 Hi => Empty),
4725 Expression => Build_Slice (
4726 Rec => Duplicate_Subexpr_No_Checks (Expression (N)),
4727 Lo => New_Copy_Tree (First_After_Hole),
4728 Hi => Empty)));
4729 end if;
4730 end Controlled_Actions;
4731 end if;
4732
4733 else
4734 Append_To (Res, Relocate_Node (N));
4735 end if;
4736
4737 -- Restore the tag
4738
4739 if Save_Tag then
4740 Append_To (Res,
4741 Make_Assignment_Statement (Loc,
4742 Name =>
4743 Make_Selected_Component (Loc,
4744 Prefix => Duplicate_Subexpr_No_Checks (L),
4745 Selector_Name => New_Reference_To (First_Tag_Component (T),
4746 Loc)),
4747 Expression => New_Reference_To (Tag_Tmp, Loc)));
4748 end if;
4749
4750 if Ctrl_Act then
4751 if VM_Target /= No_VM then
4752 -- Restore the finalization pointers
4753
4754 Append_To (Res,
4755 Make_Assignment_Statement (Loc,
4756 Name =>
4757 Make_Selected_Component (Loc,
4758 Prefix =>
4759 Unchecked_Convert_To (RTE (RE_Finalizable),
4760 New_Copy_Tree (Ctrl_Ref)),
4761 Selector_Name => Make_Identifier (Loc, Name_Prev)),
4762 Expression => New_Reference_To (Prev_Tmp, Loc)));
4763
4764 Append_To (Res,
4765 Make_Assignment_Statement (Loc,
4766 Name =>
4767 Make_Selected_Component (Loc,
4768 Prefix =>
4769 Unchecked_Convert_To (RTE (RE_Finalizable),
4770 New_Copy_Tree (Ctrl_Ref)),
4771 Selector_Name => Make_Identifier (Loc, Name_Next)),
4772 Expression => New_Reference_To (Next_Tmp, Loc)));
4773 end if;
4774
4775 -- Adjust the target after the assignment when controlled (not in the
4776 -- init proc since it is an initialization more than an assignment).
4777
4778 Append_List_To (Res,
4779 Make_Adjust_Call (
4780 Ref => Duplicate_Subexpr_Move_Checks (L),
4781 Typ => Etype (L),
4782 Flist_Ref => New_Reference_To (RTE (RE_Global_Final_List), Loc),
4783 With_Attach => Make_Integer_Literal (Loc, 0)));
4784 end if;
4785
4786 return Res;
4787
4788 exception
4789 -- Could use comment here ???
4790
4791 when RE_Not_Available =>
4792 return Empty_List;
4793 end Make_Tag_Ctrl_Assignment;
4794
4795 end Exp_Ch5;