1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
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;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Sinfo; use Sinfo;
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;
65 package body Exp_Ch5 is
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.
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.
80 function Expand_Assign_Array_Loop
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.
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, or when we have a tagged type with a representation
105 -- clause (this last case is required because holes in the tagged type
106 -- might be filled with components from child types).
108 procedure Expand_Non_Function_Return (N : Node_Id);
109 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
110 -- a procedure body, entry body, accept statement, or extended return
111 -- statement. Note that all non-function returns are simple return
114 procedure Expand_Simple_Function_Return (N : Node_Id);
115 -- Expand simple return from function. In the case where we are returning
116 -- from a function body this is called by Expand_N_Simple_Return_Statement.
118 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id;
119 -- Generate the necessary code for controlled and tagged assignment, that
120 -- is to say, finalization of the target before, adjustment of the target
121 -- after and save and restore of the tag and finalization pointers which
122 -- are not 'part of the value' and must not be changed upon assignment. N
123 -- is the original Assignment node.
125 ------------------------------
126 -- Change_Of_Representation --
127 ------------------------------
129 function Change_Of_Representation (N : Node_Id) return Boolean is
130 Rhs : constant Node_Id := Expression (N);
133 Nkind (Rhs) = N_Type_Conversion
135 not Same_Representation (Etype (Rhs), Etype (Expression (Rhs)));
136 end Change_Of_Representation;
138 -------------------------
139 -- Expand_Assign_Array --
140 -------------------------
142 -- There are two issues here. First, do we let Gigi do a block move, or
143 -- do we expand out into a loop? Second, we need to set the two flags
144 -- Forwards_OK and Backwards_OK which show whether the block move (or
145 -- corresponding loops) can be legitimately done in a forwards (low to
146 -- high) or backwards (high to low) manner.
148 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id) is
149 Loc : constant Source_Ptr := Sloc (N);
151 Lhs : constant Node_Id := Name (N);
153 Act_Lhs : constant Node_Id := Get_Referenced_Object (Lhs);
154 Act_Rhs : Node_Id := Get_Referenced_Object (Rhs);
156 L_Type : constant Entity_Id :=
157 Underlying_Type (Get_Actual_Subtype (Act_Lhs));
158 R_Type : Entity_Id :=
159 Underlying_Type (Get_Actual_Subtype (Act_Rhs));
161 L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice;
162 R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice;
164 Crep : constant Boolean := Change_Of_Representation (N);
169 Ndim : constant Pos := Number_Dimensions (L_Type);
171 Loop_Required : Boolean := False;
172 -- This switch is set to True if the array move must be done using
173 -- an explicit front end generated loop.
175 procedure Apply_Dereference (Arg : Node_Id);
176 -- If the argument is an access to an array, and the assignment is
177 -- converted into a procedure call, apply explicit dereference.
179 function Has_Address_Clause (Exp : Node_Id) return Boolean;
180 -- Test if Exp is a reference to an array whose declaration has
181 -- an address clause, or it is a slice of such an array.
183 function Is_Formal_Array (Exp : Node_Id) return Boolean;
184 -- Test if Exp is a reference to an array which is either a formal
185 -- parameter or a slice of a formal parameter. These are the cases
186 -- where hidden aliasing can occur.
188 function Is_Non_Local_Array (Exp : Node_Id) return Boolean;
189 -- Determine if Exp is a reference to an array variable which is other
190 -- than an object defined in the current scope, or a slice of such
191 -- an object. Such objects can be aliased to parameters (unlike local
192 -- array references).
194 -----------------------
195 -- Apply_Dereference --
196 -----------------------
198 procedure Apply_Dereference (Arg : Node_Id) is
199 Typ : constant Entity_Id := Etype (Arg);
201 if Is_Access_Type (Typ) then
202 Rewrite (Arg, Make_Explicit_Dereference (Loc,
203 Prefix => Relocate_Node (Arg)));
204 Analyze_And_Resolve (Arg, Designated_Type (Typ));
206 end Apply_Dereference;
208 ------------------------
209 -- Has_Address_Clause --
210 ------------------------
212 function Has_Address_Clause (Exp : Node_Id) return Boolean is
215 (Is_Entity_Name (Exp) and then
216 Present (Address_Clause (Entity (Exp))))
218 (Nkind (Exp) = N_Slice and then Has_Address_Clause (Prefix (Exp)));
219 end Has_Address_Clause;
221 ---------------------
222 -- Is_Formal_Array --
223 ---------------------
225 function Is_Formal_Array (Exp : Node_Id) return Boolean is
228 (Is_Entity_Name (Exp) and then Is_Formal (Entity (Exp)))
230 (Nkind (Exp) = N_Slice and then Is_Formal_Array (Prefix (Exp)));
233 ------------------------
234 -- Is_Non_Local_Array --
235 ------------------------
237 function Is_Non_Local_Array (Exp : Node_Id) return Boolean is
239 return (Is_Entity_Name (Exp)
240 and then Scope (Entity (Exp)) /= Current_Scope)
241 or else (Nkind (Exp) = N_Slice
242 and then Is_Non_Local_Array (Prefix (Exp)));
243 end Is_Non_Local_Array;
245 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
247 Lhs_Formal : constant Boolean := Is_Formal_Array (Act_Lhs);
248 Rhs_Formal : constant Boolean := Is_Formal_Array (Act_Rhs);
250 Lhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Lhs);
251 Rhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Rhs);
253 -- Start of processing for Expand_Assign_Array
256 -- Deal with length check. Note that the length check is done with
257 -- respect to the right hand side as given, not a possible underlying
258 -- renamed object, since this would generate incorrect extra checks.
260 Apply_Length_Check (Rhs, L_Type);
262 -- We start by assuming that the move can be done in either direction,
263 -- i.e. that the two sides are completely disjoint.
265 Set_Forwards_OK (N, True);
266 Set_Backwards_OK (N, True);
268 -- Normally it is only the slice case that can lead to overlap, and
269 -- explicit checks for slices are made below. But there is one case
270 -- where the slice can be implicit and invisible to us: when we have a
271 -- one dimensional array, and either both operands are parameters, or
272 -- one is a parameter (which can be a slice passed by reference) and the
273 -- other is a non-local variable. In this case the parameter could be a
274 -- slice that overlaps with the other operand.
276 -- However, if the array subtype is a constrained first subtype in the
277 -- parameter case, then we don't have to worry about overlap, since
278 -- slice assignments aren't possible (other than for a slice denoting
281 -- Note: No overlap is possible if there is a change of representation,
282 -- so we can exclude this case.
287 ((Lhs_Formal and Rhs_Formal)
289 (Lhs_Formal and Rhs_Non_Local_Var)
291 (Rhs_Formal and Lhs_Non_Local_Var))
293 (not Is_Constrained (Etype (Lhs))
294 or else not Is_First_Subtype (Etype (Lhs)))
296 -- In the case of compiling for the Java or .NET Virtual Machine,
297 -- slices are always passed by making a copy, so we don't have to
298 -- worry about overlap. We also want to prevent generation of "<"
299 -- comparisons for array addresses, since that's a meaningless
300 -- operation on the VM.
302 and then VM_Target = No_VM
304 Set_Forwards_OK (N, False);
305 Set_Backwards_OK (N, False);
307 -- Note: the bit-packed case is not worrisome here, since if we have
308 -- a slice passed as a parameter, it is always aligned on a byte
309 -- boundary, and if there are no explicit slices, the assignment
310 -- can be performed directly.
313 -- If either operand has an address clause clear Backwards_OK and
314 -- Forwards_OK, since we cannot tell if the operands overlap. We
315 -- exclude this treatment when Rhs is an aggregate, since we know
316 -- that overlap can't occur.
318 if (Has_Address_Clause (Lhs) and then Nkind (Rhs) /= N_Aggregate)
319 or else Has_Address_Clause (Rhs)
321 Set_Forwards_OK (N, False);
322 Set_Backwards_OK (N, False);
325 -- We certainly must use a loop for change of representation and also
326 -- we use the operand of the conversion on the right hand side as the
327 -- effective right hand side (the component types must match in this
331 Act_Rhs := Get_Referenced_Object (Rhs);
332 R_Type := Get_Actual_Subtype (Act_Rhs);
333 Loop_Required := True;
335 -- We require a loop if the left side is possibly bit unaligned
337 elsif Possible_Bit_Aligned_Component (Lhs)
339 Possible_Bit_Aligned_Component (Rhs)
341 Loop_Required := True;
343 -- Arrays with controlled components are expanded into a loop to force
344 -- calls to Adjust at the component level.
346 elsif Has_Controlled_Component (L_Type) then
347 Loop_Required := True;
349 -- If object is atomic, we cannot tolerate a loop
351 elsif Is_Atomic_Object (Act_Lhs)
353 Is_Atomic_Object (Act_Rhs)
357 -- Loop is required if we have atomic components since we have to
358 -- be sure to do any accesses on an element by element basis.
360 elsif Has_Atomic_Components (L_Type)
361 or else Has_Atomic_Components (R_Type)
362 or else Is_Atomic (Component_Type (L_Type))
363 or else Is_Atomic (Component_Type (R_Type))
365 Loop_Required := True;
367 -- Case where no slice is involved
369 elsif not L_Slice and not R_Slice then
371 -- The following code deals with the case of unconstrained bit packed
372 -- arrays. The problem is that the template for such arrays contains
373 -- the bounds of the actual source level array, but the copy of an
374 -- entire array requires the bounds of the underlying array. It would
375 -- be nice if the back end could take care of this, but right now it
376 -- does not know how, so if we have such a type, then we expand out
377 -- into a loop, which is inefficient but works correctly. If we don't
378 -- do this, we get the wrong length computed for the array to be
379 -- moved. The two cases we need to worry about are:
381 -- Explicit dereference of an unconstrained packed array type as in
382 -- the following example:
385 -- type BITS is array(INTEGER range <>) of BOOLEAN;
386 -- pragma PACK(BITS);
387 -- type A is access BITS;
390 -- P1 := new BITS (1 .. 65_535);
391 -- P2 := new BITS (1 .. 65_535);
395 -- A formal parameter reference with an unconstrained bit array type
396 -- is the other case we need to worry about (here we assume the same
397 -- BITS type declared above):
399 -- procedure Write_All (File : out BITS; Contents : BITS);
401 -- File.Storage := Contents;
404 -- We expand to a loop in either of these two cases
406 -- Question for future thought. Another potentially more efficient
407 -- approach would be to create the actual subtype, and then do an
408 -- unchecked conversion to this actual subtype ???
410 Check_Unconstrained_Bit_Packed_Array : declare
412 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean;
413 -- Function to perform required test for the first case, above
414 -- (dereference of an unconstrained bit packed array).
416 -----------------------
417 -- Is_UBPA_Reference --
418 -----------------------
420 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean is
421 Typ : constant Entity_Id := Underlying_Type (Etype (Opnd));
423 Des_Type : Entity_Id;
426 if Present (Packed_Array_Type (Typ))
427 and then Is_Array_Type (Packed_Array_Type (Typ))
428 and then not Is_Constrained (Packed_Array_Type (Typ))
432 elsif Nkind (Opnd) = N_Explicit_Dereference then
433 P_Type := Underlying_Type (Etype (Prefix (Opnd)));
435 if not Is_Access_Type (P_Type) then
439 Des_Type := Designated_Type (P_Type);
441 Is_Bit_Packed_Array (Des_Type)
442 and then not Is_Constrained (Des_Type);
448 end Is_UBPA_Reference;
450 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
453 if Is_UBPA_Reference (Lhs)
455 Is_UBPA_Reference (Rhs)
457 Loop_Required := True;
459 -- Here if we do not have the case of a reference to a bit packed
460 -- unconstrained array case. In this case gigi can most certainly
461 -- handle the assignment if a forwards move is allowed.
463 -- (could it handle the backwards case also???)
465 elsif Forwards_OK (N) then
468 end Check_Unconstrained_Bit_Packed_Array;
470 -- The back end can always handle the assignment if the right side is a
471 -- string literal (note that overlap is definitely impossible in this
472 -- case). If the type is packed, a string literal is always converted
473 -- into an aggregate, except in the case of a null slice, for which no
474 -- aggregate can be written. In that case, rewrite the assignment as a
475 -- null statement, a length check has already been emitted to verify
476 -- that the range of the left-hand side is empty.
478 -- Note that this code is not executed if we have an assignment of a
479 -- string literal to a non-bit aligned component of a record, a case
480 -- which cannot be handled by the backend.
482 elsif Nkind (Rhs) = N_String_Literal then
483 if String_Length (Strval (Rhs)) = 0
484 and then Is_Bit_Packed_Array (L_Type)
486 Rewrite (N, Make_Null_Statement (Loc));
492 -- If either operand is bit packed, then we need a loop, since we can't
493 -- be sure that the slice is byte aligned. Similarly, if either operand
494 -- is a possibly unaligned slice, then we need a loop (since the back
495 -- end cannot handle unaligned slices).
497 elsif Is_Bit_Packed_Array (L_Type)
498 or else Is_Bit_Packed_Array (R_Type)
499 or else Is_Possibly_Unaligned_Slice (Lhs)
500 or else Is_Possibly_Unaligned_Slice (Rhs)
502 Loop_Required := True;
504 -- If we are not bit-packed, and we have only one slice, then no overlap
505 -- is possible except in the parameter case, so we can let the back end
508 elsif not (L_Slice and R_Slice) then
509 if Forwards_OK (N) then
514 -- If the right-hand side is a string literal, introduce a temporary for
515 -- it, for use in the generated loop that will follow.
517 if Nkind (Rhs) = N_String_Literal then
519 Temp : constant Entity_Id :=
520 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
525 Make_Object_Declaration (Loc,
526 Defining_Identifier => Temp,
527 Object_Definition => New_Occurrence_Of (L_Type, Loc),
528 Expression => Relocate_Node (Rhs));
530 Insert_Action (N, Decl);
531 Rewrite (Rhs, New_Occurrence_Of (Temp, Loc));
532 R_Type := Etype (Temp);
536 -- Come here to complete the analysis
538 -- Loop_Required: Set to True if we know that a loop is required
539 -- regardless of overlap considerations.
541 -- Forwards_OK: Set to False if we already know that a forwards
542 -- move is not safe, else set to True.
544 -- Backwards_OK: Set to False if we already know that a backwards
545 -- move is not safe, else set to True
547 -- Our task at this stage is to complete the overlap analysis, which can
548 -- result in possibly setting Forwards_OK or Backwards_OK to False, and
549 -- then generating the final code, either by deciding that it is OK
550 -- after all to let Gigi handle it, or by generating appropriate code
554 L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
555 R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
557 Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
558 Left_Hi : constant Node_Id := Type_High_Bound (L_Index_Typ);
559 Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
560 Right_Hi : constant Node_Id := Type_High_Bound (R_Index_Typ);
562 Act_L_Array : Node_Id;
563 Act_R_Array : Node_Id;
569 Cresult : Compare_Result;
572 -- Get the expressions for the arrays. If we are dealing with a
573 -- private type, then convert to the underlying type. We can do
574 -- direct assignments to an array that is a private type, but we
575 -- cannot assign to elements of the array without this extra
576 -- unchecked conversion.
578 if Nkind (Act_Lhs) = N_Slice then
579 Larray := Prefix (Act_Lhs);
583 if Is_Private_Type (Etype (Larray)) then
586 (Underlying_Type (Etype (Larray)), Larray);
590 if Nkind (Act_Rhs) = N_Slice then
591 Rarray := Prefix (Act_Rhs);
595 if Is_Private_Type (Etype (Rarray)) then
598 (Underlying_Type (Etype (Rarray)), Rarray);
602 -- If both sides are slices, we must figure out whether it is safe
603 -- to do the move in one direction or the other. It is always safe
604 -- if there is a change of representation since obviously two arrays
605 -- with different representations cannot possibly overlap.
607 if (not Crep) and L_Slice and R_Slice then
608 Act_L_Array := Get_Referenced_Object (Prefix (Act_Lhs));
609 Act_R_Array := Get_Referenced_Object (Prefix (Act_Rhs));
611 -- If both left and right hand arrays are entity names, and refer
612 -- to different entities, then we know that the move is safe (the
613 -- two storage areas are completely disjoint).
615 if Is_Entity_Name (Act_L_Array)
616 and then Is_Entity_Name (Act_R_Array)
617 and then Entity (Act_L_Array) /= Entity (Act_R_Array)
621 -- Otherwise, we assume the worst, which is that the two arrays
622 -- are the same array. There is no need to check if we know that
623 -- is the case, because if we don't know it, we still have to
626 -- Generally if the same array is involved, then we have an
627 -- overlapping case. We will have to really assume the worst (i.e.
628 -- set neither of the OK flags) unless we can determine the lower
629 -- or upper bounds at compile time and compare them.
634 (Left_Lo, Right_Lo, Assume_Valid => True);
636 if Cresult = Unknown then
639 (Left_Hi, Right_Hi, Assume_Valid => True);
643 when LT | LE | EQ => Set_Backwards_OK (N, False);
644 when GT | GE => Set_Forwards_OK (N, False);
645 when NE | Unknown => Set_Backwards_OK (N, False);
646 Set_Forwards_OK (N, False);
651 -- If after that analysis Loop_Required is False, meaning that we
652 -- have not discovered some non-overlap reason for requiring a loop,
653 -- then the outcome depends on the capabilities of the back end.
655 if not Loop_Required then
657 -- The GCC back end can deal with all cases of overlap by falling
658 -- back to memmove if it cannot use a more efficient approach.
660 if VM_Target = No_VM and not AAMP_On_Target then
663 -- Assume other back ends can handle it if Forwards_OK is set
665 elsif Forwards_OK (N) then
668 -- If Forwards_OK is not set, the back end will need something
669 -- like memmove to handle the move. For now, this processing is
670 -- activated using the .s debug flag (-gnatd.s).
672 elsif Debug_Flag_Dot_S then
677 -- At this stage we have to generate an explicit loop, and we have
678 -- the following cases:
680 -- Forwards_OK = True
682 -- Rnn : right_index := right_index'First;
683 -- for Lnn in left-index loop
684 -- left (Lnn) := right (Rnn);
685 -- Rnn := right_index'Succ (Rnn);
688 -- Note: the above code MUST be analyzed with checks off, because
689 -- otherwise the Succ could overflow. But in any case this is more
692 -- Forwards_OK = False, Backwards_OK = True
694 -- Rnn : right_index := right_index'Last;
695 -- for Lnn in reverse left-index loop
696 -- left (Lnn) := right (Rnn);
697 -- Rnn := right_index'Pred (Rnn);
700 -- Note: the above code MUST be analyzed with checks off, because
701 -- otherwise the Pred could overflow. But in any case this is more
704 -- Forwards_OK = Backwards_OK = False
706 -- This only happens if we have the same array on each side. It is
707 -- possible to create situations using overlays that violate this,
708 -- but we simply do not promise to get this "right" in this case.
710 -- There are two possible subcases. If the No_Implicit_Conditionals
711 -- restriction is set, then we generate the following code:
714 -- T : constant <operand-type> := rhs;
719 -- If implicit conditionals are permitted, then we generate:
721 -- if Left_Lo <= Right_Lo then
722 -- <code for Forwards_OK = True above>
724 -- <code for Backwards_OK = True above>
727 -- In order to detect possible aliasing, we examine the renamed
728 -- expression when the source or target is a renaming. However,
729 -- the renaming may be intended to capture an address that may be
730 -- affected by subsequent code, and therefore we must recover
731 -- the actual entity for the expansion that follows, not the
732 -- object it renames. In particular, if source or target designate
733 -- a portion of a dynamically allocated object, the pointer to it
734 -- may be reassigned but the renaming preserves the proper location.
736 if Is_Entity_Name (Rhs)
738 Nkind (Parent (Entity (Rhs))) = N_Object_Renaming_Declaration
739 and then Nkind (Act_Rhs) = N_Slice
744 if Is_Entity_Name (Lhs)
746 Nkind (Parent (Entity (Lhs))) = N_Object_Renaming_Declaration
747 and then Nkind (Act_Lhs) = N_Slice
752 -- Cases where either Forwards_OK or Backwards_OK is true
754 if Forwards_OK (N) or else Backwards_OK (N) then
755 if Needs_Finalization (Component_Type (L_Type))
756 and then Base_Type (L_Type) = Base_Type (R_Type)
758 and then not No_Ctrl_Actions (N)
761 Proc : constant Entity_Id :=
762 TSS (Base_Type (L_Type), TSS_Slice_Assign);
766 Apply_Dereference (Larray);
767 Apply_Dereference (Rarray);
768 Actuals := New_List (
769 Duplicate_Subexpr (Larray, Name_Req => True),
770 Duplicate_Subexpr (Rarray, Name_Req => True),
771 Duplicate_Subexpr (Left_Lo, Name_Req => True),
772 Duplicate_Subexpr (Left_Hi, Name_Req => True),
773 Duplicate_Subexpr (Right_Lo, Name_Req => True),
774 Duplicate_Subexpr (Right_Hi, Name_Req => True));
778 Boolean_Literals (not Forwards_OK (N)), Loc));
781 Make_Procedure_Call_Statement (Loc,
782 Name => New_Reference_To (Proc, Loc),
783 Parameter_Associations => Actuals));
788 Expand_Assign_Array_Loop
789 (N, Larray, Rarray, L_Type, R_Type, Ndim,
790 Rev => not Forwards_OK (N)));
793 -- Case of both are false with No_Implicit_Conditionals
795 elsif Restriction_Active (No_Implicit_Conditionals) then
797 T : constant Entity_Id :=
798 Make_Defining_Identifier (Loc, Chars => Name_T);
802 Make_Block_Statement (Loc,
803 Declarations => New_List (
804 Make_Object_Declaration (Loc,
805 Defining_Identifier => T,
806 Constant_Present => True,
808 New_Occurrence_Of (Etype (Rhs), Loc),
809 Expression => Relocate_Node (Rhs))),
811 Handled_Statement_Sequence =>
812 Make_Handled_Sequence_Of_Statements (Loc,
813 Statements => New_List (
814 Make_Assignment_Statement (Loc,
815 Name => Relocate_Node (Lhs),
816 Expression => New_Occurrence_Of (T, Loc))))));
819 -- Case of both are false with implicit conditionals allowed
822 -- Before we generate this code, we must ensure that the left and
823 -- right side array types are defined. They may be itypes, and we
824 -- cannot let them be defined inside the if, since the first use
825 -- in the then may not be executed.
827 Ensure_Defined (L_Type, N);
828 Ensure_Defined (R_Type, N);
830 -- We normally compare addresses to find out which way round to
831 -- do the loop, since this is reliable, and handles the cases of
832 -- parameters, conversions etc. But we can't do that in the bit
833 -- packed case or the VM case, because addresses don't work there.
835 if not Is_Bit_Packed_Array (L_Type) and then VM_Target = No_VM then
839 Unchecked_Convert_To (RTE (RE_Integer_Address),
840 Make_Attribute_Reference (Loc,
842 Make_Indexed_Component (Loc,
844 Duplicate_Subexpr_Move_Checks (Larray, True),
845 Expressions => New_List (
846 Make_Attribute_Reference (Loc,
850 Attribute_Name => Name_First))),
851 Attribute_Name => Name_Address)),
854 Unchecked_Convert_To (RTE (RE_Integer_Address),
855 Make_Attribute_Reference (Loc,
857 Make_Indexed_Component (Loc,
859 Duplicate_Subexpr_Move_Checks (Rarray, True),
860 Expressions => New_List (
861 Make_Attribute_Reference (Loc,
865 Attribute_Name => Name_First))),
866 Attribute_Name => Name_Address)));
868 -- For the bit packed and VM cases we use the bounds. That's OK,
869 -- because we don't have to worry about parameters, since they
870 -- cannot cause overlap. Perhaps we should worry about weird slice
876 Cleft_Lo := New_Copy_Tree (Left_Lo);
877 Cright_Lo := New_Copy_Tree (Right_Lo);
879 -- If the types do not match we add an implicit conversion
880 -- here to ensure proper match
882 if Etype (Left_Lo) /= Etype (Right_Lo) then
884 Unchecked_Convert_To (Etype (Left_Lo), Cright_Lo);
887 -- Reset the Analyzed flag, because the bounds of the index
888 -- type itself may be universal, and must must be reaanalyzed
889 -- to acquire the proper type for the back end.
891 Set_Analyzed (Cleft_Lo, False);
892 Set_Analyzed (Cright_Lo, False);
896 Left_Opnd => Cleft_Lo,
897 Right_Opnd => Cright_Lo);
900 if Needs_Finalization (Component_Type (L_Type))
901 and then Base_Type (L_Type) = Base_Type (R_Type)
903 and then not No_Ctrl_Actions (N)
906 -- Call TSS procedure for array assignment, passing the
907 -- explicit bounds of right and left hand sides.
910 Proc : constant Entity_Id :=
911 TSS (Base_Type (L_Type), TSS_Slice_Assign);
915 Apply_Dereference (Larray);
916 Apply_Dereference (Rarray);
917 Actuals := New_List (
918 Duplicate_Subexpr (Larray, Name_Req => True),
919 Duplicate_Subexpr (Rarray, Name_Req => True),
920 Duplicate_Subexpr (Left_Lo, Name_Req => True),
921 Duplicate_Subexpr (Left_Hi, Name_Req => True),
922 Duplicate_Subexpr (Right_Lo, Name_Req => True),
923 Duplicate_Subexpr (Right_Hi, Name_Req => True));
927 Right_Opnd => Condition));
930 Make_Procedure_Call_Statement (Loc,
931 Name => New_Reference_To (Proc, Loc),
932 Parameter_Associations => Actuals));
937 Make_Implicit_If_Statement (N,
938 Condition => Condition,
940 Then_Statements => New_List (
941 Expand_Assign_Array_Loop
942 (N, Larray, Rarray, L_Type, R_Type, Ndim,
945 Else_Statements => New_List (
946 Expand_Assign_Array_Loop
947 (N, Larray, Rarray, L_Type, R_Type, Ndim,
952 Analyze (N, Suppress => All_Checks);
956 when RE_Not_Available =>
958 end Expand_Assign_Array;
960 ------------------------------
961 -- Expand_Assign_Array_Loop --
962 ------------------------------
964 -- The following is an example of the loop generated for the case of a
965 -- two-dimensional array:
970 -- for L1b in 1 .. 100 loop
974 -- for L3b in 1 .. 100 loop
975 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
976 -- R4b := Tm1X2'succ(R4b);
979 -- R2b := Tm1X1'succ(R2b);
983 -- Here Rev is False, and Tm1Xn are the subscript types for the right hand
984 -- side. The declarations of R2b and R4b are inserted before the original
985 -- assignment statement.
987 function Expand_Assign_Array_Loop
994 Rev : Boolean) return Node_Id
996 Loc : constant Source_Ptr := Sloc (N);
998 Lnn : array (1 .. Ndim) of Entity_Id;
999 Rnn : array (1 .. Ndim) of Entity_Id;
1000 -- Entities used as subscripts on left and right sides
1002 L_Index_Type : array (1 .. Ndim) of Entity_Id;
1003 R_Index_Type : array (1 .. Ndim) of Entity_Id;
1004 -- Left and right index types
1013 F_Or_L := Name_Last;
1014 S_Or_P := Name_Pred;
1016 F_Or_L := Name_First;
1017 S_Or_P := Name_Succ;
1020 -- Setup index types and subscript entities
1027 L_Index := First_Index (L_Type);
1028 R_Index := First_Index (R_Type);
1030 for J in 1 .. Ndim loop
1032 Make_Defining_Identifier (Loc,
1033 Chars => New_Internal_Name ('L'));
1036 Make_Defining_Identifier (Loc,
1037 Chars => New_Internal_Name ('R'));
1039 L_Index_Type (J) := Etype (L_Index);
1040 R_Index_Type (J) := Etype (R_Index);
1042 Next_Index (L_Index);
1043 Next_Index (R_Index);
1047 -- Now construct the assignment statement
1050 ExprL : constant List_Id := New_List;
1051 ExprR : constant List_Id := New_List;
1054 for J in 1 .. Ndim loop
1055 Append_To (ExprL, New_Occurrence_Of (Lnn (J), Loc));
1056 Append_To (ExprR, New_Occurrence_Of (Rnn (J), Loc));
1060 Make_Assignment_Statement (Loc,
1062 Make_Indexed_Component (Loc,
1063 Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
1064 Expressions => ExprL),
1066 Make_Indexed_Component (Loc,
1067 Prefix => Duplicate_Subexpr (Rarray, Name_Req => True),
1068 Expressions => ExprR));
1070 -- We set assignment OK, since there are some cases, e.g. in object
1071 -- declarations, where we are actually assigning into a constant.
1072 -- If there really is an illegality, it was caught long before now,
1073 -- and was flagged when the original assignment was analyzed.
1075 Set_Assignment_OK (Name (Assign));
1077 -- Propagate the No_Ctrl_Actions flag to individual assignments
1079 Set_No_Ctrl_Actions (Assign, No_Ctrl_Actions (N));
1082 -- Now construct the loop from the inside out, with the last subscript
1083 -- varying most rapidly. Note that Assign is first the raw assignment
1084 -- statement, and then subsequently the loop that wraps it up.
1086 for J in reverse 1 .. Ndim loop
1088 Make_Block_Statement (Loc,
1089 Declarations => New_List (
1090 Make_Object_Declaration (Loc,
1091 Defining_Identifier => Rnn (J),
1092 Object_Definition =>
1093 New_Occurrence_Of (R_Index_Type (J), Loc),
1095 Make_Attribute_Reference (Loc,
1096 Prefix => New_Occurrence_Of (R_Index_Type (J), Loc),
1097 Attribute_Name => F_Or_L))),
1099 Handled_Statement_Sequence =>
1100 Make_Handled_Sequence_Of_Statements (Loc,
1101 Statements => New_List (
1102 Make_Implicit_Loop_Statement (N,
1104 Make_Iteration_Scheme (Loc,
1105 Loop_Parameter_Specification =>
1106 Make_Loop_Parameter_Specification (Loc,
1107 Defining_Identifier => Lnn (J),
1108 Reverse_Present => Rev,
1109 Discrete_Subtype_Definition =>
1110 New_Reference_To (L_Index_Type (J), Loc))),
1112 Statements => New_List (
1115 Make_Assignment_Statement (Loc,
1116 Name => New_Occurrence_Of (Rnn (J), Loc),
1118 Make_Attribute_Reference (Loc,
1120 New_Occurrence_Of (R_Index_Type (J), Loc),
1121 Attribute_Name => S_Or_P,
1122 Expressions => New_List (
1123 New_Occurrence_Of (Rnn (J), Loc)))))))));
1127 end Expand_Assign_Array_Loop;
1129 --------------------------
1130 -- Expand_Assign_Record --
1131 --------------------------
1133 procedure Expand_Assign_Record (N : Node_Id) is
1134 Lhs : constant Node_Id := Name (N);
1135 Rhs : Node_Id := Expression (N);
1136 L_Typ : constant Entity_Id := Base_Type (Etype (Lhs));
1139 -- If change of representation, then extract the real right hand side
1140 -- from the type conversion, and proceed with component-wise assignment,
1141 -- since the two types are not the same as far as the back end is
1144 if Change_Of_Representation (N) then
1145 Rhs := Expression (Rhs);
1147 -- If this may be a case of a large bit aligned component, then proceed
1148 -- with component-wise assignment, to avoid possible clobbering of other
1149 -- components sharing bits in the first or last byte of the component to
1152 elsif Possible_Bit_Aligned_Component (Lhs)
1154 Possible_Bit_Aligned_Component (Rhs)
1158 -- If we have a tagged type that has a complete record representation
1159 -- clause, we must do we must do component-wise assignments, since child
1160 -- types may have used gaps for their components, and we might be
1161 -- dealing with a view conversion.
1163 elsif Is_Fully_Repped_Tagged_Type (L_Typ) then
1166 -- If neither condition met, then nothing special to do, the back end
1167 -- can handle assignment of the entire component as a single entity.
1173 -- At this stage we know that we must do a component wise assignment
1176 Loc : constant Source_Ptr := Sloc (N);
1177 R_Typ : constant Entity_Id := Base_Type (Etype (Rhs));
1178 Decl : constant Node_Id := Declaration_Node (R_Typ);
1182 function Find_Component
1184 Comp : Entity_Id) return Entity_Id;
1185 -- Find the component with the given name in the underlying record
1186 -- declaration for Typ. We need to use the actual entity because the
1187 -- type may be private and resolution by identifier alone would fail.
1189 function Make_Component_List_Assign
1191 U_U : Boolean := False) return List_Id;
1192 -- Returns a sequence of statements to assign the components that
1193 -- are referenced in the given component list. The flag U_U is
1194 -- used to force the usage of the inferred value of the variant
1195 -- part expression as the switch for the generated case statement.
1197 function Make_Field_Assign
1199 U_U : Boolean := False) return Node_Id;
1200 -- Given C, the entity for a discriminant or component, build an
1201 -- assignment for the corresponding field values. The flag U_U
1202 -- signals the presence of an Unchecked_Union and forces the usage
1203 -- of the inferred discriminant value of C as the right hand side
1204 -- of the assignment.
1206 function Make_Field_Assigns (CI : List_Id) return List_Id;
1207 -- Given CI, a component items list, construct series of statements
1208 -- for fieldwise assignment of the corresponding components.
1210 --------------------
1211 -- Find_Component --
1212 --------------------
1214 function Find_Component
1216 Comp : Entity_Id) return Entity_Id
1218 Utyp : constant Entity_Id := Underlying_Type (Typ);
1222 C := First_Entity (Utyp);
1223 while Present (C) loop
1224 if Chars (C) = Chars (Comp) then
1231 raise Program_Error;
1234 --------------------------------
1235 -- Make_Component_List_Assign --
1236 --------------------------------
1238 function Make_Component_List_Assign
1240 U_U : Boolean := False) return List_Id
1242 CI : constant List_Id := Component_Items (CL);
1243 VP : constant Node_Id := Variant_Part (CL);
1253 Result := Make_Field_Assigns (CI);
1255 if Present (VP) then
1256 V := First_Non_Pragma (Variants (VP));
1258 while Present (V) loop
1260 DC := First (Discrete_Choices (V));
1261 while Present (DC) loop
1262 Append_To (DCH, New_Copy_Tree (DC));
1267 Make_Case_Statement_Alternative (Loc,
1268 Discrete_Choices => DCH,
1270 Make_Component_List_Assign (Component_List (V))));
1271 Next_Non_Pragma (V);
1274 -- If we have an Unchecked_Union, use the value of the inferred
1275 -- discriminant of the variant part expression as the switch
1276 -- for the case statement. The case statement may later be
1281 New_Copy (Get_Discriminant_Value (
1284 Discriminant_Constraint (Etype (Rhs))));
1287 Make_Selected_Component (Loc,
1288 Prefix => Duplicate_Subexpr (Rhs),
1290 Make_Identifier (Loc, Chars (Name (VP))));
1294 Make_Case_Statement (Loc,
1296 Alternatives => Alts));
1300 end Make_Component_List_Assign;
1302 -----------------------
1303 -- Make_Field_Assign --
1304 -----------------------
1306 function Make_Field_Assign
1308 U_U : Boolean := False) return Node_Id
1314 -- In the case of an Unchecked_Union, use the discriminant
1315 -- constraint value as on the right hand side of the assignment.
1319 New_Copy (Get_Discriminant_Value (C,
1321 Discriminant_Constraint (Etype (Rhs))));
1324 Make_Selected_Component (Loc,
1325 Prefix => Duplicate_Subexpr (Rhs),
1326 Selector_Name => New_Occurrence_Of (C, Loc));
1330 Make_Assignment_Statement (Loc,
1332 Make_Selected_Component (Loc,
1333 Prefix => Duplicate_Subexpr (Lhs),
1335 New_Occurrence_Of (Find_Component (L_Typ, C), Loc)),
1336 Expression => Expr);
1338 -- Set Assignment_OK, so discriminants can be assigned
1340 Set_Assignment_OK (Name (A), True);
1342 if Componentwise_Assignment (N)
1343 and then Nkind (Name (A)) = N_Selected_Component
1344 and then Chars (Selector_Name (Name (A))) = Name_uParent
1346 Set_Componentwise_Assignment (A);
1350 end Make_Field_Assign;
1352 ------------------------
1353 -- Make_Field_Assigns --
1354 ------------------------
1356 function Make_Field_Assigns (CI : List_Id) return List_Id is
1363 while Present (Item) loop
1365 -- Look for components, but exclude _tag field assignment if
1366 -- the special Componentwise_Assignment flag is set.
1368 if Nkind (Item) = N_Component_Declaration
1369 and then not (Is_Tag (Defining_Identifier (Item))
1370 and then Componentwise_Assignment (N))
1373 (Result, Make_Field_Assign (Defining_Identifier (Item)));
1380 end Make_Field_Assigns;
1382 -- Start of processing for Expand_Assign_Record
1385 -- Note that we use the base types for this processing. This results
1386 -- in some extra work in the constrained case, but the change of
1387 -- representation case is so unusual that it is not worth the effort.
1389 -- First copy the discriminants. This is done unconditionally. It
1390 -- is required in the unconstrained left side case, and also in the
1391 -- case where this assignment was constructed during the expansion
1392 -- of a type conversion (since initialization of discriminants is
1393 -- suppressed in this case). It is unnecessary but harmless in
1396 if Has_Discriminants (L_Typ) then
1397 F := First_Discriminant (R_Typ);
1398 while Present (F) loop
1400 -- If we are expanding the initialization of a derived record
1401 -- that constrains or renames discriminants of the parent, we
1402 -- must use the corresponding discriminant in the parent.
1409 and then Present (Corresponding_Discriminant (F))
1411 CF := Corresponding_Discriminant (F);
1416 if Is_Unchecked_Union (Base_Type (R_Typ)) then
1417 Insert_Action (N, Make_Field_Assign (CF, True));
1419 Insert_Action (N, Make_Field_Assign (CF));
1422 Next_Discriminant (F);
1427 -- We know the underlying type is a record, but its current view
1428 -- may be private. We must retrieve the usable record declaration.
1430 if Nkind_In (Decl, N_Private_Type_Declaration,
1431 N_Private_Extension_Declaration)
1432 and then Present (Full_View (R_Typ))
1434 RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
1436 RDef := Type_Definition (Decl);
1439 if Nkind (RDef) = N_Derived_Type_Definition then
1440 RDef := Record_Extension_Part (RDef);
1443 if Nkind (RDef) = N_Record_Definition
1444 and then Present (Component_List (RDef))
1446 if Is_Unchecked_Union (R_Typ) then
1448 Make_Component_List_Assign (Component_List (RDef), True));
1451 (N, Make_Component_List_Assign (Component_List (RDef)));
1454 Rewrite (N, Make_Null_Statement (Loc));
1457 end Expand_Assign_Record;
1459 -----------------------------------
1460 -- Expand_N_Assignment_Statement --
1461 -----------------------------------
1463 -- This procedure implements various cases where an assignment statement
1464 -- cannot just be passed on to the back end in untransformed state.
1466 procedure Expand_N_Assignment_Statement (N : Node_Id) is
1467 Loc : constant Source_Ptr := Sloc (N);
1468 Lhs : constant Node_Id := Name (N);
1469 Rhs : constant Node_Id := Expression (N);
1470 Typ : constant Entity_Id := Underlying_Type (Etype (Lhs));
1474 -- Special case to check right away, if the Componentwise_Assignment
1475 -- flag is set, this is a reanalysis from the expansion of the primitive
1476 -- assignment procedure for a tagged type, and all we need to do is to
1477 -- expand to assignment of components, because otherwise, we would get
1478 -- infinite recursion (since this looks like a tagged assignment which
1479 -- would normally try to *call* the primitive assignment procedure).
1481 if Componentwise_Assignment (N) then
1482 Expand_Assign_Record (N);
1486 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1488 -- Rewrite an assignment to X'Priority into a run-time call
1490 -- For example: X'Priority := New_Prio_Expr;
1491 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1493 -- Note that although X'Priority is notionally an object, it is quite
1494 -- deliberately not defined as an aliased object in the RM. This means
1495 -- that it works fine to rewrite it as a call, without having to worry
1496 -- about complications that would other arise from X'Priority'Access,
1497 -- which is illegal, because of the lack of aliasing.
1499 if Ada_Version >= Ada_05 then
1502 Conctyp : Entity_Id;
1505 RT_Subprg_Name : Node_Id;
1508 -- Handle chains of renamings
1511 while Nkind (Ent) in N_Has_Entity
1512 and then Present (Entity (Ent))
1513 and then Present (Renamed_Object (Entity (Ent)))
1515 Ent := Renamed_Object (Entity (Ent));
1518 -- The attribute Priority applied to protected objects has been
1519 -- previously expanded into a call to the Get_Ceiling run-time
1522 if Nkind (Ent) = N_Function_Call
1523 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
1525 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling))
1527 -- Look for the enclosing concurrent type
1529 Conctyp := Current_Scope;
1530 while not Is_Concurrent_Type (Conctyp) loop
1531 Conctyp := Scope (Conctyp);
1534 pragma Assert (Is_Protected_Type (Conctyp));
1536 -- Generate the first actual of the call
1538 Subprg := Current_Scope;
1539 while not Present (Protected_Body_Subprogram (Subprg)) loop
1540 Subprg := Scope (Subprg);
1543 -- Select the appropriate run-time call
1545 if Number_Entries (Conctyp) = 0 then
1547 New_Reference_To (RTE (RE_Set_Ceiling), Loc);
1550 New_Reference_To (RTE (RO_PE_Set_Ceiling), Loc);
1554 Make_Procedure_Call_Statement (Loc,
1555 Name => RT_Subprg_Name,
1556 Parameter_Associations => New_List (
1557 New_Copy_Tree (First (Parameter_Associations (Ent))),
1558 Relocate_Node (Expression (N))));
1567 -- First deal with generation of range check if required
1569 if Do_Range_Check (Rhs) then
1570 Set_Do_Range_Check (Rhs, False);
1571 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
1574 -- Check for a special case where a high level transformation is
1575 -- required. If we have either of:
1580 -- where P is a reference to a bit packed array, then we have to unwind
1581 -- the assignment. The exact meaning of being a reference to a bit
1582 -- packed array is as follows:
1584 -- An indexed component whose prefix is a bit packed array is a
1585 -- reference to a bit packed array.
1587 -- An indexed component or selected component whose prefix is a
1588 -- reference to a bit packed array is itself a reference ot a
1589 -- bit packed array.
1591 -- The required transformation is
1593 -- Tnn : prefix_type := P;
1594 -- Tnn.field := rhs;
1599 -- Tnn : prefix_type := P;
1600 -- Tnn (subscr) := rhs;
1603 -- Since P is going to be evaluated more than once, any subscripts
1604 -- in P must have their evaluation forced.
1606 if Nkind_In (Lhs, N_Indexed_Component, N_Selected_Component)
1607 and then Is_Ref_To_Bit_Packed_Array (Prefix (Lhs))
1610 BPAR_Expr : constant Node_Id := Relocate_Node (Prefix (Lhs));
1611 BPAR_Typ : constant Entity_Id := Etype (BPAR_Expr);
1612 Tnn : constant Entity_Id :=
1613 Make_Defining_Identifier (Loc,
1614 Chars => New_Internal_Name ('T'));
1617 -- Insert the post assignment first, because we want to copy the
1618 -- BPAR_Expr tree before it gets analyzed in the context of the
1619 -- pre assignment. Note that we do not analyze the post assignment
1620 -- yet (we cannot till we have completed the analysis of the pre
1621 -- assignment). As usual, the analysis of this post assignment
1622 -- will happen on its own when we "run into" it after finishing
1623 -- the current assignment.
1626 Make_Assignment_Statement (Loc,
1627 Name => New_Copy_Tree (BPAR_Expr),
1628 Expression => New_Occurrence_Of (Tnn, Loc)));
1630 -- At this stage BPAR_Expr is a reference to a bit packed array
1631 -- where the reference was not expanded in the original tree,
1632 -- since it was on the left side of an assignment. But in the
1633 -- pre-assignment statement (the object definition), BPAR_Expr
1634 -- will end up on the right hand side, and must be reexpanded. To
1635 -- achieve this, we reset the analyzed flag of all selected and
1636 -- indexed components down to the actual indexed component for
1637 -- the packed array.
1641 Set_Analyzed (Exp, False);
1644 (Exp, N_Selected_Component, N_Indexed_Component)
1646 Exp := Prefix (Exp);
1652 -- Now we can insert and analyze the pre-assignment
1654 -- If the right-hand side requires a transient scope, it has
1655 -- already been placed on the stack. However, the declaration is
1656 -- inserted in the tree outside of this scope, and must reflect
1657 -- the proper scope for its variable. This awkward bit is forced
1658 -- by the stricter scope discipline imposed by GCC 2.97.
1661 Uses_Transient_Scope : constant Boolean :=
1663 and then N = Node_To_Be_Wrapped;
1666 if Uses_Transient_Scope then
1667 Push_Scope (Scope (Current_Scope));
1670 Insert_Before_And_Analyze (N,
1671 Make_Object_Declaration (Loc,
1672 Defining_Identifier => Tnn,
1673 Object_Definition => New_Occurrence_Of (BPAR_Typ, Loc),
1674 Expression => BPAR_Expr));
1676 if Uses_Transient_Scope then
1681 -- Now fix up the original assignment and continue processing
1683 Rewrite (Prefix (Lhs),
1684 New_Occurrence_Of (Tnn, Loc));
1686 -- We do not need to reanalyze that assignment, and we do not need
1687 -- to worry about references to the temporary, but we do need to
1688 -- make sure that the temporary is not marked as a true constant
1689 -- since we now have a generated assignment to it!
1691 Set_Is_True_Constant (Tnn, False);
1695 -- When we have the appropriate type of aggregate in the expression (it
1696 -- has been determined during analysis of the aggregate by setting the
1697 -- delay flag), let's perform in place assignment and thus avoid
1698 -- creating a temporary.
1700 if Is_Delayed_Aggregate (Rhs) then
1701 Convert_Aggr_In_Assignment (N);
1702 Rewrite (N, Make_Null_Statement (Loc));
1707 -- Apply discriminant check if required. If Lhs is an access type to a
1708 -- designated type with discriminants, we must always check.
1710 if Has_Discriminants (Etype (Lhs)) then
1712 -- Skip discriminant check if change of representation. Will be
1713 -- done when the change of representation is expanded out.
1715 if not Change_Of_Representation (N) then
1716 Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
1719 -- If the type is private without discriminants, and the full type
1720 -- has discriminants (necessarily with defaults) a check may still be
1721 -- necessary if the Lhs is aliased. The private determinants must be
1722 -- visible to build the discriminant constraints.
1724 -- Only an explicit dereference that comes from source indicates
1725 -- aliasing. Access to formals of protected operations and entries
1726 -- create dereferences but are not semantic aliasings.
1728 elsif Is_Private_Type (Etype (Lhs))
1729 and then Has_Discriminants (Typ)
1730 and then Nkind (Lhs) = N_Explicit_Dereference
1731 and then Comes_From_Source (Lhs)
1734 Lt : constant Entity_Id := Etype (Lhs);
1736 Set_Etype (Lhs, Typ);
1737 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1738 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1739 Set_Etype (Lhs, Lt);
1742 -- If the Lhs has a private type with unknown discriminants, it
1743 -- may have a full view with discriminants, but those are nameable
1744 -- only in the underlying type, so convert the Rhs to it before
1745 -- potential checking.
1747 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs)))
1748 and then Has_Discriminants (Typ)
1750 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1751 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1753 -- In the access type case, we need the same discriminant check, and
1754 -- also range checks if we have an access to constrained array.
1756 elsif Is_Access_Type (Etype (Lhs))
1757 and then Is_Constrained (Designated_Type (Etype (Lhs)))
1759 if Has_Discriminants (Designated_Type (Etype (Lhs))) then
1761 -- Skip discriminant check if change of representation. Will be
1762 -- done when the change of representation is expanded out.
1764 if not Change_Of_Representation (N) then
1765 Apply_Discriminant_Check (Rhs, Etype (Lhs));
1768 elsif Is_Array_Type (Designated_Type (Etype (Lhs))) then
1769 Apply_Range_Check (Rhs, Etype (Lhs));
1771 if Is_Constrained (Etype (Lhs)) then
1772 Apply_Length_Check (Rhs, Etype (Lhs));
1775 if Nkind (Rhs) = N_Allocator then
1777 Target_Typ : constant Entity_Id := Etype (Expression (Rhs));
1778 C_Es : Check_Result;
1785 Etype (Designated_Type (Etype (Lhs))));
1797 -- Apply range check for access type case
1799 elsif Is_Access_Type (Etype (Lhs))
1800 and then Nkind (Rhs) = N_Allocator
1801 and then Nkind (Expression (Rhs)) = N_Qualified_Expression
1803 Analyze_And_Resolve (Expression (Rhs));
1805 (Expression (Rhs), Designated_Type (Etype (Lhs)));
1808 -- Ada 2005 (AI-231): Generate the run-time check
1810 if Is_Access_Type (Typ)
1811 and then Can_Never_Be_Null (Etype (Lhs))
1812 and then not Can_Never_Be_Null (Etype (Rhs))
1814 Apply_Constraint_Check (Rhs, Etype (Lhs));
1817 -- Case of assignment to a bit packed array element
1819 if Nkind (Lhs) = N_Indexed_Component
1820 and then Is_Bit_Packed_Array (Etype (Prefix (Lhs)))
1822 Expand_Bit_Packed_Element_Set (N);
1825 -- Build-in-place function call case. Note that we're not yet doing
1826 -- build-in-place for user-written assignment statements (the assignment
1827 -- here came from an aggregate.)
1829 elsif Ada_Version >= Ada_05
1830 and then Is_Build_In_Place_Function_Call (Rhs)
1832 Make_Build_In_Place_Call_In_Assignment (N, Rhs);
1834 elsif Is_Tagged_Type (Typ) and then Is_Value_Type (Etype (Lhs)) then
1836 -- Nothing to do for valuetypes
1837 -- ??? Set_Scope_Is_Transient (False);
1841 elsif Is_Tagged_Type (Typ)
1842 or else (Needs_Finalization (Typ) and then not Is_Array_Type (Typ))
1844 Tagged_Case : declare
1845 L : List_Id := No_List;
1846 Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
1849 -- In the controlled case, we ensure that function calls are
1850 -- evaluated before finalizing the target. In all cases, it makes
1851 -- the expansion easier if the side-effects are removed first.
1853 Remove_Side_Effects (Lhs);
1854 Remove_Side_Effects (Rhs);
1856 -- Avoid recursion in the mechanism
1860 -- If dispatching assignment, we need to dispatch to _assign
1862 if Is_Class_Wide_Type (Typ)
1864 -- If the type is tagged, we may as well use the predefined
1865 -- primitive assignment. This avoids inlining a lot of code
1866 -- and in the class-wide case, the assignment is replaced by
1867 -- dispatch call to _assign. Note that this cannot be done when
1868 -- discriminant checks are locally suppressed (as in extension
1869 -- aggregate expansions) because otherwise the discriminant
1870 -- check will be performed within the _assign call. It is also
1871 -- suppressed for assignments created by the expander that
1872 -- correspond to initializations, where we do want to copy the
1873 -- tag (No_Ctrl_Actions flag set True) by the expander and we
1874 -- do not need to mess with tags ever (Expand_Ctrl_Actions flag
1875 -- is set True in this case).
1877 or else (Is_Tagged_Type (Typ)
1878 and then not Is_Value_Type (Etype (Lhs))
1879 and then Chars (Current_Scope) /= Name_uAssign
1880 and then Expand_Ctrl_Actions
1881 and then not Discriminant_Checks_Suppressed (Empty))
1883 -- Fetch the primitive op _assign and proper type to call it.
1884 -- Because of possible conflicts between private and full view,
1885 -- fetch the proper type directly from the operation profile.
1888 Op : constant Entity_Id :=
1889 Find_Prim_Op (Typ, Name_uAssign);
1890 F_Typ : Entity_Id := Etype (First_Formal (Op));
1893 -- If the assignment is dispatching, make sure to use the
1896 if Is_Class_Wide_Type (Typ) then
1897 F_Typ := Class_Wide_Type (F_Typ);
1902 -- In case of assignment to a class-wide tagged type, before
1903 -- the assignment we generate run-time check to ensure that
1904 -- the tags of source and target match.
1906 if Is_Class_Wide_Type (Typ)
1907 and then Is_Tagged_Type (Typ)
1908 and then Is_Tagged_Type (Underlying_Type (Etype (Rhs)))
1911 Make_Raise_Constraint_Error (Loc,
1915 Make_Selected_Component (Loc,
1916 Prefix => Duplicate_Subexpr (Lhs),
1918 Make_Identifier (Loc,
1919 Chars => Name_uTag)),
1921 Make_Selected_Component (Loc,
1922 Prefix => Duplicate_Subexpr (Rhs),
1924 Make_Identifier (Loc,
1925 Chars => Name_uTag))),
1926 Reason => CE_Tag_Check_Failed));
1930 Make_Procedure_Call_Statement (Loc,
1931 Name => New_Reference_To (Op, Loc),
1932 Parameter_Associations => New_List (
1933 Unchecked_Convert_To (F_Typ,
1934 Duplicate_Subexpr (Lhs)),
1935 Unchecked_Convert_To (F_Typ,
1936 Duplicate_Subexpr (Rhs)))));
1940 L := Make_Tag_Ctrl_Assignment (N);
1942 -- We can't afford to have destructive Finalization Actions in
1943 -- the Self assignment case, so if the target and the source
1944 -- are not obviously different, code is generated to avoid the
1945 -- self assignment case:
1947 -- if lhs'address /= rhs'address then
1948 -- <code for controlled and/or tagged assignment>
1951 -- Skip this if Restriction (No_Finalization) is active
1953 if not Statically_Different (Lhs, Rhs)
1954 and then Expand_Ctrl_Actions
1955 and then not Restriction_Active (No_Finalization)
1958 Make_Implicit_If_Statement (N,
1962 Make_Attribute_Reference (Loc,
1963 Prefix => Duplicate_Subexpr (Lhs),
1964 Attribute_Name => Name_Address),
1967 Make_Attribute_Reference (Loc,
1968 Prefix => Duplicate_Subexpr (Rhs),
1969 Attribute_Name => Name_Address)),
1971 Then_Statements => L));
1974 -- We need to set up an exception handler for implementing
1975 -- 7.6.1(18). The remaining adjustments are tackled by the
1976 -- implementation of adjust for record_controllers (see
1979 -- This is skipped if we have no finalization
1981 if Expand_Ctrl_Actions
1982 and then not Restriction_Active (No_Finalization)
1985 Make_Block_Statement (Loc,
1986 Handled_Statement_Sequence =>
1987 Make_Handled_Sequence_Of_Statements (Loc,
1989 Exception_Handlers => New_List (
1990 Make_Handler_For_Ctrl_Operation (Loc)))));
1995 Make_Block_Statement (Loc,
1996 Handled_Statement_Sequence =>
1997 Make_Handled_Sequence_Of_Statements (Loc, Statements => L)));
1999 -- If no restrictions on aborts, protect the whole assignment
2000 -- for controlled objects as per 9.8(11).
2002 if Needs_Finalization (Typ)
2003 and then Expand_Ctrl_Actions
2004 and then Abort_Allowed
2007 Blk : constant Entity_Id :=
2009 (E_Block, Current_Scope, Sloc (N), 'B');
2012 Set_Scope (Blk, Current_Scope);
2013 Set_Etype (Blk, Standard_Void_Type);
2014 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
2016 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
2017 Set_At_End_Proc (Handled_Statement_Sequence (N),
2018 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
2019 Expand_At_End_Handler
2020 (Handled_Statement_Sequence (N), Blk);
2024 -- N has been rewritten to a block statement for which it is
2025 -- known by construction that no checks are necessary: analyze
2026 -- it with all checks suppressed.
2028 Analyze (N, Suppress => All_Checks);
2034 elsif Is_Array_Type (Typ) then
2036 Actual_Rhs : Node_Id := Rhs;
2039 while Nkind_In (Actual_Rhs, N_Type_Conversion,
2040 N_Qualified_Expression)
2042 Actual_Rhs := Expression (Actual_Rhs);
2045 Expand_Assign_Array (N, Actual_Rhs);
2051 elsif Is_Record_Type (Typ) then
2052 Expand_Assign_Record (N);
2055 -- Scalar types. This is where we perform the processing related to the
2056 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2059 elsif Is_Scalar_Type (Typ) then
2061 -- Case where right side is known valid
2063 if Expr_Known_Valid (Rhs) then
2065 -- Here the right side is valid, so it is fine. The case to deal
2066 -- with is when the left side is a local variable reference whose
2067 -- value is not currently known to be valid. If this is the case,
2068 -- and the assignment appears in an unconditional context, then we
2069 -- can mark the left side as now being valid.
2071 if Is_Local_Variable_Reference (Lhs)
2072 and then not Is_Known_Valid (Entity (Lhs))
2073 and then In_Unconditional_Context (N)
2075 Set_Is_Known_Valid (Entity (Lhs), True);
2078 -- Case where right side may be invalid in the sense of the RM
2079 -- reference above. The RM does not require that we check for the
2080 -- validity on an assignment, but it does require that the assignment
2081 -- of an invalid value not cause erroneous behavior.
2083 -- The general approach in GNAT is to use the Is_Known_Valid flag
2084 -- to avoid the need for validity checking on assignments. However
2085 -- in some cases, we have to do validity checking in order to make
2086 -- sure that the setting of this flag is correct.
2089 -- Validate right side if we are validating copies
2091 if Validity_Checks_On
2092 and then Validity_Check_Copies
2094 -- Skip this if left hand side is an array or record component
2095 -- and elementary component validity checks are suppressed.
2097 if Nkind_In (Lhs, N_Selected_Component, N_Indexed_Component)
2098 and then not Validity_Check_Components
2105 -- We can propagate this to the left side where appropriate
2107 if Is_Local_Variable_Reference (Lhs)
2108 and then not Is_Known_Valid (Entity (Lhs))
2109 and then In_Unconditional_Context (N)
2111 Set_Is_Known_Valid (Entity (Lhs), True);
2114 -- Otherwise check to see what should be done
2116 -- If left side is a local variable, then we just set its flag to
2117 -- indicate that its value may no longer be valid, since we are
2118 -- copying a potentially invalid value.
2120 elsif Is_Local_Variable_Reference (Lhs) then
2121 Set_Is_Known_Valid (Entity (Lhs), False);
2123 -- Check for case of a nonlocal variable on the left side which
2124 -- is currently known to be valid. In this case, we simply ensure
2125 -- that the right side is valid. We only play the game of copying
2126 -- validity status for local variables, since we are doing this
2127 -- statically, not by tracing the full flow graph.
2129 elsif Is_Entity_Name (Lhs)
2130 and then Is_Known_Valid (Entity (Lhs))
2132 -- Note: If Validity_Checking mode is set to none, we ignore
2133 -- the Ensure_Valid call so don't worry about that case here.
2137 -- In all other cases, we can safely copy an invalid value without
2138 -- worrying about the status of the left side. Since it is not a
2139 -- variable reference it will not be considered
2140 -- as being known to be valid in any case.
2148 -- Defend against invalid subscripts on left side if we are in standard
2149 -- validity checking mode. No need to do this if we are checking all
2152 if Validity_Checks_On
2153 and then Validity_Check_Default
2154 and then not Validity_Check_Subscripts
2156 Check_Valid_Lvalue_Subscripts (Lhs);
2160 when RE_Not_Available =>
2162 end Expand_N_Assignment_Statement;
2164 ------------------------------
2165 -- Expand_N_Block_Statement --
2166 ------------------------------
2168 -- Encode entity names defined in block statement
2170 procedure Expand_N_Block_Statement (N : Node_Id) is
2172 Qualify_Entity_Names (N);
2173 end Expand_N_Block_Statement;
2175 -----------------------------
2176 -- Expand_N_Case_Statement --
2177 -----------------------------
2179 procedure Expand_N_Case_Statement (N : Node_Id) is
2180 Loc : constant Source_Ptr := Sloc (N);
2181 Expr : constant Node_Id := Expression (N);
2189 -- Check for the situation where we know at compile time which branch
2192 if Compile_Time_Known_Value (Expr) then
2193 Alt := Find_Static_Alternative (N);
2195 -- Move statements from this alternative after the case statement.
2196 -- They are already analyzed, so will be skipped by the analyzer.
2198 Insert_List_After (N, Statements (Alt));
2200 -- That leaves the case statement as a shell. So now we can kill all
2201 -- other alternatives in the case statement.
2203 Kill_Dead_Code (Expression (N));
2209 -- Loop through case alternatives, skipping pragmas, and skipping
2210 -- the one alternative that we select (and therefore retain).
2212 A := First (Alternatives (N));
2213 while Present (A) loop
2215 and then Nkind (A) = N_Case_Statement_Alternative
2217 Kill_Dead_Code (Statements (A), Warn_On_Deleted_Code);
2224 Rewrite (N, Make_Null_Statement (Loc));
2228 -- Here if the choice is not determined at compile time
2231 Last_Alt : constant Node_Id := Last (Alternatives (N));
2233 Others_Present : Boolean;
2234 Others_Node : Node_Id;
2236 Then_Stms : List_Id;
2237 Else_Stms : List_Id;
2240 if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
2241 Others_Present := True;
2242 Others_Node := Last_Alt;
2244 Others_Present := False;
2247 -- First step is to worry about possible invalid argument. The RM
2248 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2249 -- outside the base range), then Constraint_Error must be raised.
2251 -- Case of validity check required (validity checks are on, the
2252 -- expression is not known to be valid, and the case statement
2253 -- comes from source -- no need to validity check internally
2254 -- generated case statements).
2256 if Validity_Check_Default then
2257 Ensure_Valid (Expr);
2260 -- If there is only a single alternative, just replace it with the
2261 -- sequence of statements since obviously that is what is going to
2262 -- be executed in all cases.
2264 Len := List_Length (Alternatives (N));
2267 -- We still need to evaluate the expression if it has any
2270 Remove_Side_Effects (Expression (N));
2272 Insert_List_After (N, Statements (First (Alternatives (N))));
2274 -- That leaves the case statement as a shell. The alternative that
2275 -- will be executed is reset to a null list. So now we can kill
2276 -- the entire case statement.
2278 Kill_Dead_Code (Expression (N));
2279 Rewrite (N, Make_Null_Statement (Loc));
2283 -- An optimization. If there are only two alternatives, and only
2284 -- a single choice, then rewrite the whole case statement as an
2285 -- if statement, since this can result in subsequent optimizations.
2286 -- This helps not only with case statements in the source of a
2287 -- simple form, but also with generated code (discriminant check
2288 -- functions in particular)
2291 Chlist := Discrete_Choices (First (Alternatives (N)));
2293 if List_Length (Chlist) = 1 then
2294 Choice := First (Chlist);
2296 Then_Stms := Statements (First (Alternatives (N)));
2297 Else_Stms := Statements (Last (Alternatives (N)));
2299 -- For TRUE, generate "expression", not expression = true
2301 if Nkind (Choice) = N_Identifier
2302 and then Entity (Choice) = Standard_True
2304 Cond := Expression (N);
2306 -- For FALSE, generate "expression" and switch then/else
2308 elsif Nkind (Choice) = N_Identifier
2309 and then Entity (Choice) = Standard_False
2311 Cond := Expression (N);
2312 Else_Stms := Statements (First (Alternatives (N)));
2313 Then_Stms := Statements (Last (Alternatives (N)));
2315 -- For a range, generate "expression in range"
2317 elsif Nkind (Choice) = N_Range
2318 or else (Nkind (Choice) = N_Attribute_Reference
2319 and then Attribute_Name (Choice) = Name_Range)
2320 or else (Is_Entity_Name (Choice)
2321 and then Is_Type (Entity (Choice)))
2322 or else Nkind (Choice) = N_Subtype_Indication
2326 Left_Opnd => Expression (N),
2327 Right_Opnd => Relocate_Node (Choice));
2329 -- For any other subexpression "expression = value"
2334 Left_Opnd => Expression (N),
2335 Right_Opnd => Relocate_Node (Choice));
2338 -- Now rewrite the case as an IF
2341 Make_If_Statement (Loc,
2343 Then_Statements => Then_Stms,
2344 Else_Statements => Else_Stms));
2350 -- If the last alternative is not an Others choice, replace it with
2351 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2352 -- the modified case statement, since it's only effect would be to
2353 -- compute the contents of the Others_Discrete_Choices which is not
2354 -- needed by the back end anyway.
2356 -- The reason we do this is that the back end always needs some
2357 -- default for a switch, so if we have not supplied one in the
2358 -- processing above for validity checking, then we need to supply
2361 if not Others_Present then
2362 Others_Node := Make_Others_Choice (Sloc (Last_Alt));
2363 Set_Others_Discrete_Choices
2364 (Others_Node, Discrete_Choices (Last_Alt));
2365 Set_Discrete_Choices (Last_Alt, New_List (Others_Node));
2368 end Expand_N_Case_Statement;
2370 -----------------------------
2371 -- Expand_N_Exit_Statement --
2372 -----------------------------
2374 -- The only processing required is to deal with a possible C/Fortran
2375 -- boolean value used as the condition for the exit statement.
2377 procedure Expand_N_Exit_Statement (N : Node_Id) is
2379 Adjust_Condition (Condition (N));
2380 end Expand_N_Exit_Statement;
2382 ----------------------------------------
2383 -- Expand_N_Extended_Return_Statement --
2384 ----------------------------------------
2386 -- If there is a Handled_Statement_Sequence, we rewrite this:
2388 -- return Result : T := <expression> do
2389 -- <handled_seq_of_stms>
2395 -- Result : T := <expression>;
2397 -- <handled_seq_of_stms>
2401 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
2403 -- return Result : T := <expression>;
2407 -- return <expression>;
2409 -- unless it's build-in-place or there's no <expression>, in which case
2413 -- Result : T := <expression>;
2418 -- Note that this case could have been written by the user as an extended
2419 -- return statement, or could have been transformed to this from a simple
2420 -- return statement.
2422 -- That is, we need to have a reified return object if there are statements
2423 -- (which might refer to it) or if we're doing build-in-place (so we can
2424 -- set its address to the final resting place or if there is no expression
2425 -- (in which case default initial values might need to be set).
2427 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
2428 Loc : constant Source_Ptr := Sloc (N);
2430 Return_Object_Entity : constant Entity_Id :=
2431 First_Entity (Return_Statement_Entity (N));
2432 Return_Object_Decl : constant Node_Id :=
2433 Parent (Return_Object_Entity);
2434 Parent_Function : constant Entity_Id :=
2435 Return_Applies_To (Return_Statement_Entity (N));
2436 Parent_Function_Typ : constant Entity_Id := Etype (Parent_Function);
2437 Is_Build_In_Place : constant Boolean :=
2438 Is_Build_In_Place_Function (Parent_Function);
2440 Return_Stm : Node_Id;
2441 Statements : List_Id;
2442 Handled_Stm_Seq : Node_Id;
2446 function Has_Controlled_Parts (Typ : Entity_Id) return Boolean;
2447 -- Determine whether type Typ is controlled or contains a controlled
2450 function Move_Activation_Chain return Node_Id;
2451 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
2453 -- From current activation chain
2454 -- To activation chain passed in by the caller
2455 -- New_Master master passed in by the caller
2457 function Move_Final_List return Node_Id;
2458 -- Construct call to System.Finalization_Implementation.Move_Final_List
2461 -- From finalization list of the return statement
2462 -- To finalization list passed in by the caller
2464 --------------------------
2465 -- Has_Controlled_Parts --
2466 --------------------------
2468 function Has_Controlled_Parts (Typ : Entity_Id) return Boolean is
2472 or else Has_Controlled_Component (Typ);
2473 end Has_Controlled_Parts;
2475 ---------------------------
2476 -- Move_Activation_Chain --
2477 ---------------------------
2479 function Move_Activation_Chain return Node_Id is
2480 Activation_Chain_Formal : constant Entity_Id :=
2481 Build_In_Place_Formal
2482 (Parent_Function, BIP_Activation_Chain);
2483 To : constant Node_Id :=
2485 (Activation_Chain_Formal, Loc);
2486 Master_Formal : constant Entity_Id :=
2487 Build_In_Place_Formal
2488 (Parent_Function, BIP_Master);
2489 New_Master : constant Node_Id :=
2490 New_Reference_To (Master_Formal, Loc);
2492 Chain_Entity : Entity_Id;
2496 Chain_Entity := First_Entity (Return_Statement_Entity (N));
2497 while Chars (Chain_Entity) /= Name_uChain loop
2498 Chain_Entity := Next_Entity (Chain_Entity);
2502 Make_Attribute_Reference (Loc,
2503 Prefix => New_Reference_To (Chain_Entity, Loc),
2504 Attribute_Name => Name_Unrestricted_Access);
2505 -- ??? Not clear why "Make_Identifier (Loc, Name_uChain)" doesn't
2506 -- work, instead of "New_Reference_To (Chain_Entity, Loc)" above.
2509 Make_Procedure_Call_Statement (Loc,
2510 Name => New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
2511 Parameter_Associations => New_List (From, To, New_Master));
2512 end Move_Activation_Chain;
2514 ---------------------
2515 -- Move_Final_List --
2516 ---------------------
2518 function Move_Final_List return Node_Id is
2519 Flist : constant Entity_Id :=
2520 Finalization_Chain_Entity (Return_Statement_Entity (N));
2522 From : constant Node_Id := New_Reference_To (Flist, Loc);
2524 Caller_Final_List : constant Entity_Id :=
2525 Build_In_Place_Formal
2526 (Parent_Function, BIP_Final_List);
2528 To : constant Node_Id := New_Reference_To (Caller_Final_List, Loc);
2531 -- Catch cases where a finalization chain entity has not been
2532 -- associated with the return statement entity.
2534 pragma Assert (Present (Flist));
2536 -- Build required call
2539 Make_If_Statement (Loc,
2542 Left_Opnd => New_Copy (From),
2543 Right_Opnd => New_Node (N_Null, Loc)),
2546 Make_Procedure_Call_Statement (Loc,
2547 Name => New_Reference_To (RTE (RE_Move_Final_List), Loc),
2548 Parameter_Associations => New_List (From, To))));
2549 end Move_Final_List;
2551 -- Start of processing for Expand_N_Extended_Return_Statement
2554 if Nkind (Return_Object_Decl) = N_Object_Declaration then
2555 Exp := Expression (Return_Object_Decl);
2560 Handled_Stm_Seq := Handled_Statement_Sequence (N);
2562 -- Build a simple_return_statement that returns the return object when
2563 -- there is a statement sequence, or no expression, or the result will
2564 -- be built in place. Note however that we currently do this for all
2565 -- composite cases, even though nonlimited composite results are not yet
2566 -- built in place (though we plan to do so eventually).
2568 if Present (Handled_Stm_Seq)
2569 or else Is_Composite_Type (Etype (Parent_Function))
2572 if No (Handled_Stm_Seq) then
2573 Statements := New_List;
2575 -- If the extended return has a handled statement sequence, then wrap
2576 -- it in a block and use the block as the first statement.
2580 New_List (Make_Block_Statement (Loc,
2581 Declarations => New_List,
2582 Handled_Statement_Sequence => Handled_Stm_Seq));
2585 -- If control gets past the above Statements, we have successfully
2586 -- completed the return statement. If the result type has controlled
2587 -- parts and the return is for a build-in-place function, then we
2588 -- call Move_Final_List to transfer responsibility for finalization
2589 -- of the return object to the caller. An alternative would be to
2590 -- declare a Success flag in the function, initialize it to False,
2591 -- and set it to True here. Then move the Move_Final_List call into
2592 -- the cleanup code, and check Success. If Success then make a call
2593 -- to Move_Final_List else do finalization. Then we can remove the
2594 -- abort-deferral and the nulling-out of the From parameter from
2595 -- Move_Final_List. Note that the current method is not quite correct
2596 -- in the rather obscure case of a select-then-abort statement whose
2597 -- abortable part contains the return statement.
2599 -- Check the type of the function to determine whether to move the
2600 -- finalization list. A special case arises when processing a simple
2601 -- return statement which has been rewritten as an extended return.
2602 -- In that case check the type of the returned object or the original
2605 if Is_Build_In_Place
2607 (Has_Controlled_Parts (Parent_Function_Typ)
2608 or else (Is_Class_Wide_Type (Parent_Function_Typ)
2610 Has_Controlled_Parts (Root_Type (Parent_Function_Typ)))
2611 or else Has_Controlled_Parts (Etype (Return_Object_Entity))
2612 or else (Present (Exp)
2613 and then Has_Controlled_Parts (Etype (Exp))))
2615 Append_To (Statements, Move_Final_List);
2618 -- Similarly to the above Move_Final_List, if the result type
2619 -- contains tasks, we call Move_Activation_Chain. Later, the cleanup
2620 -- code will call Complete_Master, which will terminate any
2621 -- unactivated tasks belonging to the return statement master. But
2622 -- Move_Activation_Chain updates their master to be that of the
2623 -- caller, so they will not be terminated unless the return statement
2624 -- completes unsuccessfully due to exception, abort, goto, or exit.
2625 -- As a formality, we test whether the function requires the result
2626 -- to be built in place, though that's necessarily true for the case
2627 -- of result types with task parts.
2629 if Is_Build_In_Place and Has_Task (Etype (Parent_Function)) then
2630 Append_To (Statements, Move_Activation_Chain);
2633 -- Build a simple_return_statement that returns the return object
2636 Make_Simple_Return_Statement (Loc,
2637 Expression => New_Occurrence_Of (Return_Object_Entity, Loc));
2638 Append_To (Statements, Return_Stm);
2641 Make_Handled_Sequence_Of_Statements (Loc, Statements);
2644 -- Case where we build a block
2646 if Present (Handled_Stm_Seq) then
2648 Make_Block_Statement (Loc,
2649 Declarations => Return_Object_Declarations (N),
2650 Handled_Statement_Sequence => Handled_Stm_Seq);
2652 -- We set the entity of the new block statement to be that of the
2653 -- return statement. This is necessary so that various fields, such
2654 -- as Finalization_Chain_Entity carry over from the return statement
2655 -- to the block. Note that this block is unusual, in that its entity
2656 -- is an E_Return_Statement rather than an E_Block.
2659 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
2661 -- If the object decl was already rewritten as a renaming, then
2662 -- we don't want to do the object allocation and transformation of
2663 -- of the return object declaration to a renaming. This case occurs
2664 -- when the return object is initialized by a call to another
2665 -- build-in-place function, and that function is responsible for the
2666 -- allocation of the return object.
2668 if Is_Build_In_Place
2670 Nkind (Return_Object_Decl) = N_Object_Renaming_Declaration
2672 Set_By_Ref (Return_Stm); -- Return build-in-place results by ref
2674 elsif Is_Build_In_Place then
2676 -- Locate the implicit access parameter associated with the
2677 -- caller-supplied return object and convert the return
2678 -- statement's return object declaration to a renaming of a
2679 -- dereference of the access parameter. If the return object's
2680 -- declaration includes an expression that has not already been
2681 -- expanded as separate assignments, then add an assignment
2682 -- statement to ensure the return object gets initialized.
2685 -- Result : T [:= <expression>];
2692 -- Result : T renames FuncRA.all;
2693 -- [Result := <expression;]
2698 Return_Obj_Id : constant Entity_Id :=
2699 Defining_Identifier (Return_Object_Decl);
2700 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
2701 Return_Obj_Expr : constant Node_Id :=
2702 Expression (Return_Object_Decl);
2703 Result_Subt : constant Entity_Id :=
2704 Etype (Parent_Function);
2705 Constr_Result : constant Boolean :=
2706 Is_Constrained (Result_Subt);
2707 Obj_Alloc_Formal : Entity_Id;
2708 Object_Access : Entity_Id;
2709 Obj_Acc_Deref : Node_Id;
2710 Init_Assignment : Node_Id := Empty;
2713 -- Build-in-place results must be returned by reference
2715 Set_By_Ref (Return_Stm);
2717 -- Retrieve the implicit access parameter passed by the caller
2720 Build_In_Place_Formal (Parent_Function, BIP_Object_Access);
2722 -- If the return object's declaration includes an expression
2723 -- and the declaration isn't marked as No_Initialization, then
2724 -- we need to generate an assignment to the object and insert
2725 -- it after the declaration before rewriting it as a renaming
2726 -- (otherwise we'll lose the initialization). The case where
2727 -- the result type is an interface (or class-wide interface)
2728 -- is also excluded because the context of the function call
2729 -- must be unconstrained, so the initialization will always
2730 -- be done as part of an allocator evaluation (storage pool
2731 -- or secondary stack), never to a constrained target object
2732 -- passed in by the caller. Besides the assignment being
2733 -- unneeded in this case, it avoids problems with trying to
2734 -- generate a dispatching assignment when the return expression
2735 -- is a nonlimited descendant of a limited interface (the
2736 -- interface has no assignment operation).
2738 if Present (Return_Obj_Expr)
2739 and then not No_Initialization (Return_Object_Decl)
2740 and then not Is_Interface (Return_Obj_Typ)
2743 Make_Assignment_Statement (Loc,
2744 Name => New_Reference_To (Return_Obj_Id, Loc),
2745 Expression => Relocate_Node (Return_Obj_Expr));
2746 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
2747 Set_Assignment_OK (Name (Init_Assignment));
2748 Set_No_Ctrl_Actions (Init_Assignment);
2750 Set_Parent (Name (Init_Assignment), Init_Assignment);
2751 Set_Parent (Expression (Init_Assignment), Init_Assignment);
2753 Set_Expression (Return_Object_Decl, Empty);
2755 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
2756 and then not Is_Class_Wide_Type
2757 (Etype (Expression (Init_Assignment)))
2759 Rewrite (Expression (Init_Assignment),
2760 Make_Type_Conversion (Loc,
2763 (Etype (Return_Obj_Id), Loc),
2765 Relocate_Node (Expression (Init_Assignment))));
2768 -- In the case of functions where the calling context can
2769 -- determine the form of allocation needed, initialization
2770 -- is done with each part of the if statement that handles
2771 -- the different forms of allocation (this is true for
2772 -- unconstrained and tagged result subtypes).
2775 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
2777 Insert_After (Return_Object_Decl, Init_Assignment);
2781 -- When the function's subtype is unconstrained, a run-time
2782 -- test is needed to determine the form of allocation to use
2783 -- for the return object. The function has an implicit formal
2784 -- parameter indicating this. If the BIP_Alloc_Form formal has
2785 -- the value one, then the caller has passed access to an
2786 -- existing object for use as the return object. If the value
2787 -- is two, then the return object must be allocated on the
2788 -- secondary stack. Otherwise, the object must be allocated in
2789 -- a storage pool (currently only supported for the global
2790 -- heap, user-defined storage pools TBD ???). We generate an
2791 -- if statement to test the implicit allocation formal and
2792 -- initialize a local access value appropriately, creating
2793 -- allocators in the secondary stack and global heap cases.
2794 -- The special formal also exists and must be tested when the
2795 -- function has a tagged result, even when the result subtype
2796 -- is constrained, because in general such functions can be
2797 -- called in dispatching contexts and must be handled similarly
2798 -- to functions with a class-wide result.
2800 if not Constr_Result
2801 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
2804 Build_In_Place_Formal (Parent_Function, BIP_Alloc_Form);
2807 Ref_Type : Entity_Id;
2808 Ptr_Type_Decl : Node_Id;
2809 Alloc_Obj_Id : Entity_Id;
2810 Alloc_Obj_Decl : Node_Id;
2811 Alloc_If_Stmt : Node_Id;
2812 SS_Allocator : Node_Id;
2813 Heap_Allocator : Node_Id;
2816 -- Reuse the itype created for the function's implicit
2817 -- access formal. This avoids the need to create a new
2818 -- access type here, plus it allows assigning the access
2819 -- formal directly without applying a conversion.
2821 -- Ref_Type := Etype (Object_Access);
2823 -- Create an access type designating the function's
2827 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2830 Make_Full_Type_Declaration (Loc,
2831 Defining_Identifier => Ref_Type,
2833 Make_Access_To_Object_Definition (Loc,
2834 All_Present => True,
2835 Subtype_Indication =>
2836 New_Reference_To (Return_Obj_Typ, Loc)));
2838 Insert_Before (Return_Object_Decl, Ptr_Type_Decl);
2840 -- Create an access object that will be initialized to an
2841 -- access value denoting the return object, either coming
2842 -- from an implicit access value passed in by the caller
2843 -- or from the result of an allocator.
2846 Make_Defining_Identifier (Loc,
2847 Chars => New_Internal_Name ('R'));
2848 Set_Etype (Alloc_Obj_Id, Ref_Type);
2851 Make_Object_Declaration (Loc,
2852 Defining_Identifier => Alloc_Obj_Id,
2853 Object_Definition => New_Reference_To
2856 Insert_Before (Return_Object_Decl, Alloc_Obj_Decl);
2858 -- Create allocators for both the secondary stack and
2859 -- global heap. If there's an initialization expression,
2860 -- then create these as initialized allocators.
2862 if Present (Return_Obj_Expr)
2863 and then not No_Initialization (Return_Object_Decl)
2865 -- Always use the type of the expression for the
2866 -- qualified expression, rather than the result type.
2867 -- In general we cannot always use the result type
2868 -- for the allocator, because the expression might be
2869 -- of a specific type, such as in the case of an
2870 -- aggregate or even a nonlimited object when the
2871 -- result type is a limited class-wide interface type.
2874 Make_Allocator (Loc,
2876 Make_Qualified_Expression (Loc,
2879 (Etype (Return_Obj_Expr), Loc),
2881 New_Copy_Tree (Return_Obj_Expr)));
2884 -- If the function returns a class-wide type we cannot
2885 -- use the return type for the allocator. Instead we
2886 -- use the type of the expression, which must be an
2887 -- aggregate of a definite type.
2889 if Is_Class_Wide_Type (Return_Obj_Typ) then
2891 Make_Allocator (Loc,
2894 (Etype (Return_Obj_Expr), Loc));
2897 Make_Allocator (Loc,
2899 New_Reference_To (Return_Obj_Typ, Loc));
2902 -- If the object requires default initialization then
2903 -- that will happen later following the elaboration of
2904 -- the object renaming. If we don't turn it off here
2905 -- then the object will be default initialized twice.
2907 Set_No_Initialization (Heap_Allocator);
2910 -- If the No_Allocators restriction is active, then only
2911 -- an allocator for secondary stack allocation is needed.
2912 -- It's OK for such allocators to have Comes_From_Source
2913 -- set to False, because gigi knows not to flag them as
2914 -- being a violation of No_Implicit_Heap_Allocations.
2916 if Restriction_Active (No_Allocators) then
2917 SS_Allocator := Heap_Allocator;
2918 Heap_Allocator := Make_Null (Loc);
2920 -- Otherwise the heap allocator may be needed, so we make
2921 -- another allocator for secondary stack allocation.
2924 SS_Allocator := New_Copy_Tree (Heap_Allocator);
2926 -- The heap allocator is marked Comes_From_Source
2927 -- since it corresponds to an explicit user-written
2928 -- allocator (that is, it will only be executed on
2929 -- behalf of callers that call the function as
2930 -- initialization for such an allocator). This
2931 -- prevents errors when No_Implicit_Heap_Allocations
2934 Set_Comes_From_Source (Heap_Allocator, True);
2937 -- The allocator is returned on the secondary stack. We
2938 -- don't do this on VM targets, since the SS is not used.
2940 if VM_Target = No_VM then
2941 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
2942 Set_Procedure_To_Call
2943 (SS_Allocator, RTE (RE_SS_Allocate));
2945 -- The allocator is returned on the secondary stack,
2946 -- so indicate that the function return, as well as
2947 -- the block that encloses the allocator, must not
2948 -- release it. The flags must be set now because the
2949 -- decision to use the secondary stack is done very
2950 -- late in the course of expanding the return
2951 -- statement, past the point where these flags are
2954 Set_Sec_Stack_Needed_For_Return (Parent_Function);
2955 Set_Sec_Stack_Needed_For_Return
2956 (Return_Statement_Entity (N));
2957 Set_Uses_Sec_Stack (Parent_Function);
2958 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
2961 -- Create an if statement to test the BIP_Alloc_Form
2962 -- formal and initialize the access object to either the
2963 -- BIP_Object_Access formal (BIP_Alloc_Form = 0), the
2964 -- result of allocating the object in the secondary stack
2965 -- (BIP_Alloc_Form = 1), or else an allocator to create
2966 -- the return object in the heap (BIP_Alloc_Form = 2).
2968 -- ??? An unchecked type conversion must be made in the
2969 -- case of assigning the access object formal to the
2970 -- local access object, because a normal conversion would
2971 -- be illegal in some cases (such as converting access-
2972 -- to-unconstrained to access-to-constrained), but the
2973 -- the unchecked conversion will presumably fail to work
2974 -- right in just such cases. It's not clear at all how to
2978 Make_If_Statement (Loc,
2982 New_Reference_To (Obj_Alloc_Formal, Loc),
2984 Make_Integer_Literal (Loc,
2985 UI_From_Int (BIP_Allocation_Form'Pos
2986 (Caller_Allocation)))),
2988 New_List (Make_Assignment_Statement (Loc,
2991 (Alloc_Obj_Id, Loc),
2993 Make_Unchecked_Type_Conversion (Loc,
2995 New_Reference_To (Ref_Type, Loc),
2998 (Object_Access, Loc)))),
3000 New_List (Make_Elsif_Part (Loc,
3005 (Obj_Alloc_Formal, Loc),
3007 Make_Integer_Literal (Loc,
3009 BIP_Allocation_Form'Pos
3010 (Secondary_Stack)))),
3013 (Make_Assignment_Statement (Loc,
3016 (Alloc_Obj_Id, Loc),
3020 New_List (Make_Assignment_Statement (Loc,
3023 (Alloc_Obj_Id, Loc),
3027 -- If a separate initialization assignment was created
3028 -- earlier, append that following the assignment of the
3029 -- implicit access formal to the access object, to ensure
3030 -- that the return object is initialized in that case.
3031 -- In this situation, the target of the assignment must
3032 -- be rewritten to denote a dereference of the access to
3033 -- the return object passed in by the caller.
3035 if Present (Init_Assignment) then
3036 Rewrite (Name (Init_Assignment),
3037 Make_Explicit_Dereference (Loc,
3038 Prefix => New_Reference_To (Alloc_Obj_Id, Loc)));
3040 (Name (Init_Assignment), Etype (Return_Obj_Id));
3043 (Then_Statements (Alloc_If_Stmt),
3047 Insert_Before (Return_Object_Decl, Alloc_If_Stmt);
3049 -- Remember the local access object for use in the
3050 -- dereference of the renaming created below.
3052 Object_Access := Alloc_Obj_Id;
3056 -- Replace the return object declaration with a renaming of a
3057 -- dereference of the access value designating the return
3061 Make_Explicit_Dereference (Loc,
3062 Prefix => New_Reference_To (Object_Access, Loc));
3064 Rewrite (Return_Object_Decl,
3065 Make_Object_Renaming_Declaration (Loc,
3066 Defining_Identifier => Return_Obj_Id,
3067 Access_Definition => Empty,
3068 Subtype_Mark => New_Occurrence_Of
3069 (Return_Obj_Typ, Loc),
3070 Name => Obj_Acc_Deref));
3072 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
3076 -- Case where we do not build a block
3079 -- We're about to drop Return_Object_Declarations on the floor, so
3080 -- we need to insert it, in case it got expanded into useful code.
3082 Insert_List_Before (N, Return_Object_Declarations (N));
3084 -- Build simple_return_statement that returns the expression directly
3086 Return_Stm := Make_Simple_Return_Statement (Loc, Expression => Exp);
3088 Result := Return_Stm;
3091 -- Set the flag to prevent infinite recursion
3093 Set_Comes_From_Extended_Return_Statement (Return_Stm);
3095 Rewrite (N, Result);
3097 end Expand_N_Extended_Return_Statement;
3099 -----------------------------
3100 -- Expand_N_Goto_Statement --
3101 -----------------------------
3103 -- Add poll before goto if polling active
3105 procedure Expand_N_Goto_Statement (N : Node_Id) is
3107 Generate_Poll_Call (N);
3108 end Expand_N_Goto_Statement;
3110 ---------------------------
3111 -- Expand_N_If_Statement --
3112 ---------------------------
3114 -- First we deal with the case of C and Fortran convention boolean values,
3115 -- with zero/non-zero semantics.
3117 -- Second, we deal with the obvious rewriting for the cases where the
3118 -- condition of the IF is known at compile time to be True or False.
3120 -- Third, we remove elsif parts which have non-empty Condition_Actions
3121 -- and rewrite as independent if statements. For example:
3132 -- <<condition actions of y>>
3138 -- This rewriting is needed if at least one elsif part has a non-empty
3139 -- Condition_Actions list. We also do the same processing if there is a
3140 -- constant condition in an elsif part (in conjunction with the first
3141 -- processing step mentioned above, for the recursive call made to deal
3142 -- with the created inner if, this deals with properly optimizing the
3143 -- cases of constant elsif conditions).
3145 procedure Expand_N_If_Statement (N : Node_Id) is
3146 Loc : constant Source_Ptr := Sloc (N);
3151 Warn_If_Deleted : constant Boolean :=
3152 Warn_On_Deleted_Code and then Comes_From_Source (N);
3153 -- Indicates whether we want warnings when we delete branches of the
3154 -- if statement based on constant condition analysis. We never want
3155 -- these warnings for expander generated code.
3158 Adjust_Condition (Condition (N));
3160 -- The following loop deals with constant conditions for the IF. We
3161 -- need a loop because as we eliminate False conditions, we grab the
3162 -- first elsif condition and use it as the primary condition.
3164 while Compile_Time_Known_Value (Condition (N)) loop
3166 -- If condition is True, we can simply rewrite the if statement now
3167 -- by replacing it by the series of then statements.
3169 if Is_True (Expr_Value (Condition (N))) then
3171 -- All the else parts can be killed
3173 Kill_Dead_Code (Elsif_Parts (N), Warn_If_Deleted);
3174 Kill_Dead_Code (Else_Statements (N), Warn_If_Deleted);
3176 Hed := Remove_Head (Then_Statements (N));
3177 Insert_List_After (N, Then_Statements (N));
3181 -- If condition is False, then we can delete the condition and
3182 -- the Then statements
3185 -- We do not delete the condition if constant condition warnings
3186 -- are enabled, since otherwise we end up deleting the desired
3187 -- warning. Of course the backend will get rid of this True/False
3188 -- test anyway, so nothing is lost here.
3190 if not Constant_Condition_Warnings then
3191 Kill_Dead_Code (Condition (N));
3194 Kill_Dead_Code (Then_Statements (N), Warn_If_Deleted);
3196 -- If there are no elsif statements, then we simply replace the
3197 -- entire if statement by the sequence of else statements.
3199 if No (Elsif_Parts (N)) then
3200 if No (Else_Statements (N))
3201 or else Is_Empty_List (Else_Statements (N))
3204 Make_Null_Statement (Sloc (N)));
3206 Hed := Remove_Head (Else_Statements (N));
3207 Insert_List_After (N, Else_Statements (N));
3213 -- If there are elsif statements, the first of them becomes the
3214 -- if/then section of the rebuilt if statement This is the case
3215 -- where we loop to reprocess this copied condition.
3218 Hed := Remove_Head (Elsif_Parts (N));
3219 Insert_Actions (N, Condition_Actions (Hed));
3220 Set_Condition (N, Condition (Hed));
3221 Set_Then_Statements (N, Then_Statements (Hed));
3223 -- Hed might have been captured as the condition determining
3224 -- the current value for an entity. Now it is detached from
3225 -- the tree, so a Current_Value pointer in the condition might
3226 -- need to be updated.
3228 Set_Current_Value_Condition (N);
3230 if Is_Empty_List (Elsif_Parts (N)) then
3231 Set_Elsif_Parts (N, No_List);
3237 -- Loop through elsif parts, dealing with constant conditions and
3238 -- possible expression actions that are present.
3240 if Present (Elsif_Parts (N)) then
3241 E := First (Elsif_Parts (N));
3242 while Present (E) loop
3243 Adjust_Condition (Condition (E));
3245 -- If there are condition actions, then rewrite the if statement
3246 -- as indicated above. We also do the same rewrite for a True or
3247 -- False condition. The further processing of this constant
3248 -- condition is then done by the recursive call to expand the
3249 -- newly created if statement
3251 if Present (Condition_Actions (E))
3252 or else Compile_Time_Known_Value (Condition (E))
3254 -- Note this is not an implicit if statement, since it is part
3255 -- of an explicit if statement in the source (or of an implicit
3256 -- if statement that has already been tested).
3259 Make_If_Statement (Sloc (E),
3260 Condition => Condition (E),
3261 Then_Statements => Then_Statements (E),
3262 Elsif_Parts => No_List,
3263 Else_Statements => Else_Statements (N));
3265 -- Elsif parts for new if come from remaining elsif's of parent
3267 while Present (Next (E)) loop
3268 if No (Elsif_Parts (New_If)) then
3269 Set_Elsif_Parts (New_If, New_List);
3272 Append (Remove_Next (E), Elsif_Parts (New_If));
3275 Set_Else_Statements (N, New_List (New_If));
3277 if Present (Condition_Actions (E)) then
3278 Insert_List_Before (New_If, Condition_Actions (E));
3283 if Is_Empty_List (Elsif_Parts (N)) then
3284 Set_Elsif_Parts (N, No_List);
3290 -- No special processing for that elsif part, move to next
3298 -- Some more optimizations applicable if we still have an IF statement
3300 if Nkind (N) /= N_If_Statement then
3304 -- Another optimization, special cases that can be simplified
3306 -- if expression then
3312 -- can be changed to:
3314 -- return expression;
3318 -- if expression then
3324 -- can be changed to:
3326 -- return not (expression);
3328 -- Only do these optimizations if we are at least at -O1 level and
3329 -- do not do them if control flow optimizations are suppressed.
3331 if Optimization_Level > 0
3332 and then not Opt.Suppress_Control_Flow_Optimizations
3334 if Nkind (N) = N_If_Statement
3335 and then No (Elsif_Parts (N))
3336 and then Present (Else_Statements (N))
3337 and then List_Length (Then_Statements (N)) = 1
3338 and then List_Length (Else_Statements (N)) = 1
3341 Then_Stm : constant Node_Id := First (Then_Statements (N));
3342 Else_Stm : constant Node_Id := First (Else_Statements (N));
3345 if Nkind (Then_Stm) = N_Simple_Return_Statement
3347 Nkind (Else_Stm) = N_Simple_Return_Statement
3350 Then_Expr : constant Node_Id := Expression (Then_Stm);
3351 Else_Expr : constant Node_Id := Expression (Else_Stm);
3354 if Nkind (Then_Expr) = N_Identifier
3356 Nkind (Else_Expr) = N_Identifier
3358 if Entity (Then_Expr) = Standard_True
3359 and then Entity (Else_Expr) = Standard_False
3362 Make_Simple_Return_Statement (Loc,
3363 Expression => Relocate_Node (Condition (N))));
3367 elsif Entity (Then_Expr) = Standard_False
3368 and then Entity (Else_Expr) = Standard_True
3371 Make_Simple_Return_Statement (Loc,
3375 Relocate_Node (Condition (N)))));
3385 end Expand_N_If_Statement;
3387 -----------------------------
3388 -- Expand_N_Loop_Statement --
3389 -----------------------------
3391 -- 1. Remove null loop entirely
3392 -- 2. Deal with while condition for C/Fortran boolean
3393 -- 3. Deal with loops with a non-standard enumeration type range
3394 -- 4. Deal with while loops where Condition_Actions is set
3395 -- 5. Insert polling call if required
3397 procedure Expand_N_Loop_Statement (N : Node_Id) is
3398 Loc : constant Source_Ptr := Sloc (N);
3399 Isc : constant Node_Id := Iteration_Scheme (N);
3404 if Is_Null_Loop (N) then
3405 Rewrite (N, Make_Null_Statement (Loc));
3409 -- Deal with condition for C/Fortran Boolean
3411 if Present (Isc) then
3412 Adjust_Condition (Condition (Isc));
3415 -- Generate polling call
3417 if Is_Non_Empty_List (Statements (N)) then
3418 Generate_Poll_Call (First (Statements (N)));
3421 -- Nothing more to do for plain loop with no iteration scheme
3427 -- Note: we do not have to worry about validity checking of the for loop
3428 -- range bounds here, since they were frozen with constant declarations
3429 -- and it is during that process that the validity checking is done.
3431 -- Handle the case where we have a for loop with the range type being an
3432 -- enumeration type with non-standard representation. In this case we
3435 -- for x in [reverse] a .. b loop
3441 -- for xP in [reverse] integer
3442 -- range etype'Pos (a) .. etype'Pos (b) loop
3444 -- x : constant etype := Pos_To_Rep (xP);
3450 if Present (Loop_Parameter_Specification (Isc)) then
3452 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
3453 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
3454 Ltype : constant Entity_Id := Etype (Loop_Id);
3455 Btype : constant Entity_Id := Base_Type (Ltype);
3460 if not Is_Enumeration_Type (Btype)
3461 or else No (Enum_Pos_To_Rep (Btype))
3467 Make_Defining_Identifier (Loc,
3468 Chars => New_External_Name (Chars (Loop_Id), 'P'));
3470 -- If the type has a contiguous representation, successive values
3471 -- can be generated as offsets from the first literal.
3473 if Has_Contiguous_Rep (Btype) then
3475 Unchecked_Convert_To (Btype,
3478 Make_Integer_Literal (Loc,
3479 Enumeration_Rep (First_Literal (Btype))),
3480 Right_Opnd => New_Reference_To (New_Id, Loc)));
3482 -- Use the constructed array Enum_Pos_To_Rep
3485 Make_Indexed_Component (Loc,
3486 Prefix => New_Reference_To (Enum_Pos_To_Rep (Btype), Loc),
3487 Expressions => New_List (New_Reference_To (New_Id, Loc)));
3491 Make_Loop_Statement (Loc,
3492 Identifier => Identifier (N),
3495 Make_Iteration_Scheme (Loc,
3496 Loop_Parameter_Specification =>
3497 Make_Loop_Parameter_Specification (Loc,
3498 Defining_Identifier => New_Id,
3499 Reverse_Present => Reverse_Present (LPS),
3501 Discrete_Subtype_Definition =>
3502 Make_Subtype_Indication (Loc,
3505 New_Reference_To (Standard_Natural, Loc),
3508 Make_Range_Constraint (Loc,
3513 Make_Attribute_Reference (Loc,
3515 New_Reference_To (Btype, Loc),
3517 Attribute_Name => Name_Pos,
3519 Expressions => New_List (
3521 (Type_Low_Bound (Ltype)))),
3524 Make_Attribute_Reference (Loc,
3526 New_Reference_To (Btype, Loc),
3528 Attribute_Name => Name_Pos,
3530 Expressions => New_List (
3532 (Type_High_Bound (Ltype))))))))),
3534 Statements => New_List (
3535 Make_Block_Statement (Loc,
3536 Declarations => New_List (
3537 Make_Object_Declaration (Loc,
3538 Defining_Identifier => Loop_Id,
3539 Constant_Present => True,
3540 Object_Definition => New_Reference_To (Ltype, Loc),
3541 Expression => Expr)),
3543 Handled_Statement_Sequence =>
3544 Make_Handled_Sequence_Of_Statements (Loc,
3545 Statements => Statements (N)))),
3547 End_Label => End_Label (N)));
3551 -- Second case, if we have a while loop with Condition_Actions set, then
3552 -- we change it into a plain loop:
3561 -- <<condition actions>>
3567 and then Present (Condition_Actions (Isc))
3574 Make_Exit_Statement (Sloc (Condition (Isc)),
3576 Make_Op_Not (Sloc (Condition (Isc)),
3577 Right_Opnd => Condition (Isc)));
3579 Prepend (ES, Statements (N));
3580 Insert_List_Before (ES, Condition_Actions (Isc));
3582 -- This is not an implicit loop, since it is generated in response
3583 -- to the loop statement being processed. If this is itself
3584 -- implicit, the restriction has already been checked. If not,
3585 -- it is an explicit loop.
3588 Make_Loop_Statement (Sloc (N),
3589 Identifier => Identifier (N),
3590 Statements => Statements (N),
3591 End_Label => End_Label (N)));
3596 end Expand_N_Loop_Statement;
3598 --------------------------------------
3599 -- Expand_N_Simple_Return_Statement --
3600 --------------------------------------
3602 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
3604 -- Defend against previous errors (i.e. the return statement calls a
3605 -- function that is not available in configurable runtime).
3607 if Present (Expression (N))
3608 and then Nkind (Expression (N)) = N_Empty
3613 -- Distinguish the function and non-function cases:
3615 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
3618 E_Generic_Function =>
3619 Expand_Simple_Function_Return (N);
3622 E_Generic_Procedure |
3625 E_Return_Statement =>
3626 Expand_Non_Function_Return (N);
3629 raise Program_Error;
3633 when RE_Not_Available =>
3635 end Expand_N_Simple_Return_Statement;
3637 --------------------------------
3638 -- Expand_Non_Function_Return --
3639 --------------------------------
3641 procedure Expand_Non_Function_Return (N : Node_Id) is
3642 pragma Assert (No (Expression (N)));
3644 Loc : constant Source_Ptr := Sloc (N);
3645 Scope_Id : Entity_Id :=
3646 Return_Applies_To (Return_Statement_Entity (N));
3647 Kind : constant Entity_Kind := Ekind (Scope_Id);
3650 Goto_Stat : Node_Id;
3654 -- Call _Postconditions procedure if procedure with active
3655 -- postconditions. Here, we use the Postcondition_Proc attribute, which
3656 -- is needed for implicitly-generated returns. Functions never
3657 -- have implicitly-generated returns, and there's no room for
3658 -- Postcondition_Proc in E_Function, so we look up the identifier
3659 -- Name_uPostconditions for function returns (see
3660 -- Expand_Simple_Function_Return).
3662 if Ekind (Scope_Id) = E_Procedure
3663 and then Has_Postconditions (Scope_Id)
3665 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
3667 Make_Procedure_Call_Statement (Loc,
3668 Name => New_Reference_To (Postcondition_Proc (Scope_Id), Loc)));
3671 -- If it is a return from a procedure do no extra steps
3673 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
3676 -- If it is a nested return within an extended one, replace it with a
3677 -- return of the previously declared return object.
3679 elsif Kind = E_Return_Statement then
3681 Make_Simple_Return_Statement (Loc,
3683 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
3684 Set_Comes_From_Extended_Return_Statement (N);
3685 Set_Return_Statement_Entity (N, Scope_Id);
3686 Expand_Simple_Function_Return (N);
3690 pragma Assert (Is_Entry (Scope_Id));
3692 -- Look at the enclosing block to see whether the return is from an
3693 -- accept statement or an entry body.
3695 for J in reverse 0 .. Scope_Stack.Last loop
3696 Scope_Id := Scope_Stack.Table (J).Entity;
3697 exit when Is_Concurrent_Type (Scope_Id);
3700 -- If it is a return from accept statement it is expanded as call to
3701 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
3703 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
3704 -- Expand_N_Accept_Alternative in exp_ch9.adb)
3706 if Is_Task_Type (Scope_Id) then
3709 Make_Procedure_Call_Statement (Loc,
3710 Name => New_Reference_To (RTE (RE_Complete_Rendezvous), Loc));
3711 Insert_Before (N, Call);
3712 -- why not insert actions here???
3715 Acc_Stat := Parent (N);
3716 while Nkind (Acc_Stat) /= N_Accept_Statement loop
3717 Acc_Stat := Parent (Acc_Stat);
3720 Lab_Node := Last (Statements
3721 (Handled_Statement_Sequence (Acc_Stat)));
3723 Goto_Stat := Make_Goto_Statement (Loc,
3724 Name => New_Occurrence_Of
3725 (Entity (Identifier (Lab_Node)), Loc));
3727 Set_Analyzed (Goto_Stat);
3729 Rewrite (N, Goto_Stat);
3732 -- If it is a return from an entry body, put a Complete_Entry_Body call
3733 -- in front of the return.
3735 elsif Is_Protected_Type (Scope_Id) then
3737 Make_Procedure_Call_Statement (Loc,
3739 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
3740 Parameter_Associations => New_List (
3741 Make_Attribute_Reference (Loc,
3744 (Find_Protection_Object (Current_Scope), Loc),
3746 Name_Unchecked_Access)));
3748 Insert_Before (N, Call);
3751 end Expand_Non_Function_Return;
3753 -----------------------------------
3754 -- Expand_Simple_Function_Return --
3755 -----------------------------------
3757 -- The "simple" comes from the syntax rule simple_return_statement.
3758 -- The semantics are not at all simple!
3760 procedure Expand_Simple_Function_Return (N : Node_Id) is
3761 Loc : constant Source_Ptr := Sloc (N);
3763 Scope_Id : constant Entity_Id :=
3764 Return_Applies_To (Return_Statement_Entity (N));
3765 -- The function we are returning from
3767 R_Type : constant Entity_Id := Etype (Scope_Id);
3768 -- The result type of the function
3770 Utyp : constant Entity_Id := Underlying_Type (R_Type);
3772 Exp : constant Node_Id := Expression (N);
3773 pragma Assert (Present (Exp));
3775 Exptyp : constant Entity_Id := Etype (Exp);
3776 -- The type of the expression (not necessarily the same as R_Type)
3778 Subtype_Ind : Node_Id;
3779 -- If the result type of the function is class-wide and the
3780 -- expression has a specific type, then we use the expression's
3781 -- type as the type of the return object. In cases where the
3782 -- expression is an aggregate that is built in place, this avoids
3783 -- the need for an expensive conversion of the return object to
3784 -- the specific type on assignments to the individual components.
3787 if Is_Class_Wide_Type (R_Type)
3788 and then not Is_Class_Wide_Type (Etype (Exp))
3790 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
3792 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
3795 -- For the case of a simple return that does not come from an extended
3796 -- return, in the case of Ada 2005 where we are returning a limited
3797 -- type, we rewrite "return <expression>;" to be:
3799 -- return _anon_ : <return_subtype> := <expression>
3801 -- The expansion produced by Expand_N_Extended_Return_Statement will
3802 -- contain simple return statements (for example, a block containing
3803 -- simple return of the return object), which brings us back here with
3804 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
3805 -- checking for a simple return that does not come from an extended
3806 -- return is to avoid this infinite recursion.
3808 -- The reason for this design is that for Ada 2005 limited returns, we
3809 -- need to reify the return object, so we can build it "in place", and
3810 -- we need a block statement to hang finalization and tasking stuff.
3812 -- ??? In order to avoid disruption, we avoid translating to extended
3813 -- return except in the cases where we really need to (Ada 2005 for
3814 -- inherently limited). We might prefer to do this translation in all
3815 -- cases (except perhaps for the case of Ada 95 inherently limited),
3816 -- in order to fully exercise the Expand_N_Extended_Return_Statement
3817 -- code. This would also allow us to do the build-in-place optimization
3818 -- for efficiency even in cases where it is semantically not required.
3820 -- As before, we check the type of the return expression rather than the
3821 -- return type of the function, because the latter may be a limited
3822 -- class-wide interface type, which is not a limited type, even though
3823 -- the type of the expression may be.
3825 if not Comes_From_Extended_Return_Statement (N)
3826 and then Is_Inherently_Limited_Type (Etype (Expression (N)))
3827 and then Ada_Version >= Ada_05
3828 and then not Debug_Flag_Dot_L
3831 Return_Object_Entity : constant Entity_Id :=
3832 Make_Defining_Identifier (Loc,
3833 New_Internal_Name ('R'));
3834 Obj_Decl : constant Node_Id :=
3835 Make_Object_Declaration (Loc,
3836 Defining_Identifier => Return_Object_Entity,
3837 Object_Definition => Subtype_Ind,
3840 Ext : constant Node_Id := Make_Extended_Return_Statement (Loc,
3841 Return_Object_Declarations => New_List (Obj_Decl));
3842 -- Do not perform this high-level optimization if the result type
3843 -- is an interface because the "this" pointer must be displaced.
3852 -- Here we have a simple return statement that is part of the expansion
3853 -- of an extended return statement (either written by the user, or
3854 -- generated by the above code).
3856 -- Always normalize C/Fortran boolean result. This is not always needed,
3857 -- but it seems a good idea to minimize the passing around of non-
3858 -- normalized values, and in any case this handles the processing of
3859 -- barrier functions for protected types, which turn the condition into
3860 -- a return statement.
3862 if Is_Boolean_Type (Exptyp)
3863 and then Nonzero_Is_True (Exptyp)
3865 Adjust_Condition (Exp);
3866 Adjust_Result_Type (Exp, Exptyp);
3869 -- Do validity check if enabled for returns
3871 if Validity_Checks_On
3872 and then Validity_Check_Returns
3877 -- Check the result expression of a scalar function against the subtype
3878 -- of the function by inserting a conversion. This conversion must
3879 -- eventually be performed for other classes of types, but for now it's
3880 -- only done for scalars.
3883 if Is_Scalar_Type (Exptyp) then
3884 Rewrite (Exp, Convert_To (R_Type, Exp));
3886 -- The expression is resolved to ensure that the conversion gets
3887 -- expanded to generate a possible constraint check.
3889 Analyze_And_Resolve (Exp, R_Type);
3892 -- Deal with returning variable length objects and controlled types
3894 -- Nothing to do if we are returning by reference, or this is not a
3895 -- type that requires special processing (indicated by the fact that
3896 -- it requires a cleanup scope for the secondary stack case).
3898 if Is_Inherently_Limited_Type (Exptyp)
3899 or else Is_Limited_Interface (Exptyp)
3903 elsif not Requires_Transient_Scope (R_Type) then
3905 -- Mutable records with no variable length components are not
3906 -- returned on the sec-stack, so we need to make sure that the
3907 -- backend will only copy back the size of the actual value, and not
3908 -- the maximum size. We create an actual subtype for this purpose.
3911 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
3915 if Has_Discriminants (Ubt)
3916 and then not Is_Constrained (Ubt)
3917 and then not Has_Unchecked_Union (Ubt)
3919 Decl := Build_Actual_Subtype (Ubt, Exp);
3920 Ent := Defining_Identifier (Decl);
3921 Insert_Action (Exp, Decl);
3922 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
3923 Analyze_And_Resolve (Exp);
3927 -- Here if secondary stack is used
3930 -- Make sure that no surrounding block will reclaim the secondary
3931 -- stack on which we are going to put the result. Not only may this
3932 -- introduce secondary stack leaks but worse, if the reclamation is
3933 -- done too early, then the result we are returning may get
3940 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
3941 Set_Sec_Stack_Needed_For_Return (S, True);
3942 S := Enclosing_Dynamic_Scope (S);
3946 -- Optimize the case where the result is a function call. In this
3947 -- case either the result is already on the secondary stack, or is
3948 -- already being returned with the stack pointer depressed and no
3949 -- further processing is required except to set the By_Ref flag to
3950 -- ensure that gigi does not attempt an extra unnecessary copy.
3951 -- (actually not just unnecessary but harmfully wrong in the case
3952 -- of a controlled type, where gigi does not know how to do a copy).
3953 -- To make up for a gcc 2.8.1 deficiency (???), we perform
3954 -- the copy for array types if the constrained status of the
3955 -- target type is different from that of the expression.
3957 if Requires_Transient_Scope (Exptyp)
3959 (not Is_Array_Type (Exptyp)
3960 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
3961 or else CW_Or_Has_Controlled_Part (Utyp))
3962 and then Nkind (Exp) = N_Function_Call
3966 -- Remove side effects from the expression now so that other parts
3967 -- of the expander do not have to reanalyze this node without this
3970 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
3972 -- For controlled types, do the allocation on the secondary stack
3973 -- manually in order to call adjust at the right time:
3975 -- type Anon1 is access R_Type;
3976 -- for Anon1'Storage_pool use ss_pool;
3977 -- Anon2 : anon1 := new R_Type'(expr);
3978 -- return Anon2.all;
3980 -- We do the same for classwide types that are not potentially
3981 -- controlled (by the virtue of restriction No_Finalization) because
3982 -- gigi is not able to properly allocate class-wide types.
3984 elsif CW_Or_Has_Controlled_Part (Utyp) then
3986 Loc : constant Source_Ptr := Sloc (N);
3987 Temp : constant Entity_Id :=
3988 Make_Defining_Identifier (Loc,
3989 Chars => New_Internal_Name ('R'));
3990 Acc_Typ : constant Entity_Id :=
3991 Make_Defining_Identifier (Loc,
3992 Chars => New_Internal_Name ('A'));
3993 Alloc_Node : Node_Id;
3996 Set_Ekind (Acc_Typ, E_Access_Type);
3998 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
4000 -- This is an allocator for the secondary stack, and it's fine
4001 -- to have Comes_From_Source set False on it, as gigi knows not
4002 -- to flag it as a violation of No_Implicit_Heap_Allocations.
4005 Make_Allocator (Loc,
4007 Make_Qualified_Expression (Loc,
4008 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
4009 Expression => Relocate_Node (Exp)));
4011 -- We do not want discriminant checks on the declaration,
4012 -- given that it gets its value from the allocator.
4014 Set_No_Initialization (Alloc_Node);
4016 Insert_List_Before_And_Analyze (N, New_List (
4017 Make_Full_Type_Declaration (Loc,
4018 Defining_Identifier => Acc_Typ,
4020 Make_Access_To_Object_Definition (Loc,
4021 Subtype_Indication => Subtype_Ind)),
4023 Make_Object_Declaration (Loc,
4024 Defining_Identifier => Temp,
4025 Object_Definition => New_Reference_To (Acc_Typ, Loc),
4026 Expression => Alloc_Node)));
4029 Make_Explicit_Dereference (Loc,
4030 Prefix => New_Reference_To (Temp, Loc)));
4032 Analyze_And_Resolve (Exp, R_Type);
4035 -- Otherwise use the gigi mechanism to allocate result on the
4039 Check_Restriction (No_Secondary_Stack, N);
4040 Set_Storage_Pool (N, RTE (RE_SS_Pool));
4042 -- If we are generating code for the VM do not use
4043 -- SS_Allocate since everything is heap-allocated anyway.
4045 if VM_Target = No_VM then
4046 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4051 -- Implement the rules of 6.5(8-10), which require a tag check in the
4052 -- case of a limited tagged return type, and tag reassignment for
4053 -- nonlimited tagged results. These actions are needed when the return
4054 -- type is a specific tagged type and the result expression is a
4055 -- conversion or a formal parameter, because in that case the tag of the
4056 -- expression might differ from the tag of the specific result type.
4058 if Is_Tagged_Type (Utyp)
4059 and then not Is_Class_Wide_Type (Utyp)
4060 and then (Nkind_In (Exp, N_Type_Conversion,
4061 N_Unchecked_Type_Conversion)
4062 or else (Is_Entity_Name (Exp)
4063 and then Ekind (Entity (Exp)) in Formal_Kind))
4065 -- When the return type is limited, perform a check that the
4066 -- tag of the result is the same as the tag of the return type.
4068 if Is_Limited_Type (R_Type) then
4070 Make_Raise_Constraint_Error (Loc,
4074 Make_Selected_Component (Loc,
4075 Prefix => Duplicate_Subexpr (Exp),
4077 New_Reference_To (First_Tag_Component (Utyp), Loc)),
4079 Unchecked_Convert_To (RTE (RE_Tag),
4082 (Access_Disp_Table (Base_Type (Utyp)))),
4084 Reason => CE_Tag_Check_Failed));
4086 -- If the result type is a specific nonlimited tagged type, then we
4087 -- have to ensure that the tag of the result is that of the result
4088 -- type. This is handled by making a copy of the expression in the
4089 -- case where it might have a different tag, namely when the
4090 -- expression is a conversion or a formal parameter. We create a new
4091 -- object of the result type and initialize it from the expression,
4092 -- which will implicitly force the tag to be set appropriately.
4096 Result_Id : constant Entity_Id :=
4097 Make_Defining_Identifier (Loc,
4098 Chars => New_Internal_Name ('R'));
4099 Result_Exp : constant Node_Id :=
4100 New_Reference_To (Result_Id, Loc);
4101 Result_Obj : constant Node_Id :=
4102 Make_Object_Declaration (Loc,
4103 Defining_Identifier => Result_Id,
4104 Object_Definition =>
4105 New_Reference_To (R_Type, Loc),
4106 Constant_Present => True,
4107 Expression => Relocate_Node (Exp));
4110 Set_Assignment_OK (Result_Obj);
4111 Insert_Action (Exp, Result_Obj);
4113 Rewrite (Exp, Result_Exp);
4114 Analyze_And_Resolve (Exp, R_Type);
4118 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
4119 -- a check that the level of the return expression's underlying type
4120 -- is not deeper than the level of the master enclosing the function.
4121 -- Always generate the check when the type of the return expression
4122 -- is class-wide, when it's a type conversion, or when it's a formal
4123 -- parameter. Otherwise, suppress the check in the case where the
4124 -- return expression has a specific type whose level is known not to
4125 -- be statically deeper than the function's result type.
4127 -- Note: accessibility check is skipped in the VM case, since there
4128 -- does not seem to be any practical way to implement this check.
4130 elsif Ada_Version >= Ada_05
4131 and then Tagged_Type_Expansion
4132 and then Is_Class_Wide_Type (R_Type)
4133 and then not Scope_Suppress (Accessibility_Check)
4135 (Is_Class_Wide_Type (Etype (Exp))
4136 or else Nkind_In (Exp, N_Type_Conversion,
4137 N_Unchecked_Type_Conversion)
4138 or else (Is_Entity_Name (Exp)
4139 and then Ekind (Entity (Exp)) in Formal_Kind)
4140 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
4141 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
4147 -- Ada 2005 (AI-251): In class-wide interface objects we displace
4148 -- "this" to reference the base of the object --- required to get
4149 -- access to the TSD of the object.
4151 if Is_Class_Wide_Type (Etype (Exp))
4152 and then Is_Interface (Etype (Exp))
4153 and then Nkind (Exp) = N_Explicit_Dereference
4156 Make_Explicit_Dereference (Loc,
4157 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4158 Make_Function_Call (Loc,
4159 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
4160 Parameter_Associations => New_List (
4161 Unchecked_Convert_To (RTE (RE_Address),
4162 Duplicate_Subexpr (Prefix (Exp)))))));
4165 Make_Attribute_Reference (Loc,
4166 Prefix => Duplicate_Subexpr (Exp),
4167 Attribute_Name => Name_Tag);
4171 Make_Raise_Program_Error (Loc,
4175 Build_Get_Access_Level (Loc, Tag_Node),
4177 Make_Integer_Literal (Loc,
4178 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
4179 Reason => PE_Accessibility_Check_Failed));
4183 -- If we are returning an object that may not be bit-aligned, then
4184 -- copy the value into a temporary first. This copy may need to expand
4185 -- to a loop of component operations..
4187 if Is_Possibly_Unaligned_Slice (Exp)
4188 or else Is_Possibly_Unaligned_Object (Exp)
4191 Tnn : constant Entity_Id :=
4192 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
4195 Make_Object_Declaration (Loc,
4196 Defining_Identifier => Tnn,
4197 Constant_Present => True,
4198 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4199 Expression => Relocate_Node (Exp)),
4200 Suppress => All_Checks);
4201 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4205 -- Generate call to postcondition checks if they are present
4207 if Ekind (Scope_Id) = E_Function
4208 and then Has_Postconditions (Scope_Id)
4210 -- We are going to reference the returned value twice in this case,
4211 -- once in the call to _Postconditions, and once in the actual return
4212 -- statement, but we can't have side effects happening twice, and in
4213 -- any case for efficiency we don't want to do the computation twice.
4215 -- If the returned expression is an entity name, we don't need to
4216 -- worry since it is efficient and safe to reference it twice, that's
4217 -- also true for literals other than string literals, and for the
4218 -- case of X.all where X is an entity name.
4220 if Is_Entity_Name (Exp)
4221 or else Nkind_In (Exp, N_Character_Literal,
4224 or else (Nkind (Exp) = N_Explicit_Dereference
4225 and then Is_Entity_Name (Prefix (Exp)))
4229 -- Otherwise we are going to need a temporary to capture the value
4233 Tnn : constant Entity_Id :=
4234 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
4237 -- For a complex expression of an elementary type, capture
4238 -- value in the temporary and use it as the reference.
4240 if Is_Elementary_Type (R_Type) then
4242 Make_Object_Declaration (Loc,
4243 Defining_Identifier => Tnn,
4244 Constant_Present => True,
4245 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4246 Expression => Relocate_Node (Exp)),
4247 Suppress => All_Checks);
4249 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4251 -- If we have something we can rename, generate a renaming of
4252 -- the object and replace the expression with a reference
4254 elsif Is_Object_Reference (Exp) then
4256 Make_Object_Renaming_Declaration (Loc,
4257 Defining_Identifier => Tnn,
4258 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
4259 Name => Relocate_Node (Exp)),
4260 Suppress => All_Checks);
4262 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4264 -- Otherwise we have something like a string literal or an
4265 -- aggregate. We could copy the value, but that would be
4266 -- inefficient. Instead we make a reference to the value and
4267 -- capture this reference with a renaming, the expression is
4268 -- then replaced by a dereference of this renaming.
4271 -- For now, copy the value, since the code below does not
4272 -- seem to work correctly ???
4275 Make_Object_Declaration (Loc,
4276 Defining_Identifier => Tnn,
4277 Constant_Present => True,
4278 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4279 Expression => Relocate_Node (Exp)),
4280 Suppress => All_Checks);
4282 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4284 -- Insert_Action (Exp,
4285 -- Make_Object_Renaming_Declaration (Loc,
4286 -- Defining_Identifier => Tnn,
4287 -- Access_Definition =>
4288 -- Make_Access_Definition (Loc,
4289 -- All_Present => True,
4290 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
4292 -- Make_Reference (Loc,
4293 -- Prefix => Relocate_Node (Exp))),
4294 -- Suppress => All_Checks);
4297 -- Make_Explicit_Dereference (Loc,
4298 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
4303 -- Generate call to _postconditions
4306 Make_Procedure_Call_Statement (Loc,
4307 Name => Make_Identifier (Loc, Name_uPostconditions),
4308 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
4311 -- Ada 2005 (AI-251): If this return statement corresponds with an
4312 -- simple return statement associated with an extended return statement
4313 -- and the type of the returned object is an interface then generate an
4314 -- implicit conversion to force displacement of the "this" pointer.
4316 if Ada_Version >= Ada_05
4317 and then Comes_From_Extended_Return_Statement (N)
4318 and then Nkind (Expression (N)) = N_Identifier
4319 and then Is_Interface (Utyp)
4320 and then Utyp /= Underlying_Type (Exptyp)
4322 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
4323 Analyze_And_Resolve (Exp);
4325 end Expand_Simple_Function_Return;
4327 ------------------------------
4328 -- Make_Tag_Ctrl_Assignment --
4329 ------------------------------
4331 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id is
4332 Loc : constant Source_Ptr := Sloc (N);
4333 L : constant Node_Id := Name (N);
4334 T : constant Entity_Id := Underlying_Type (Etype (L));
4336 Ctrl_Act : constant Boolean := Needs_Finalization (T)
4337 and then not No_Ctrl_Actions (N);
4339 Component_Assign : constant Boolean :=
4340 Is_Fully_Repped_Tagged_Type (T);
4342 Save_Tag : constant Boolean := Is_Tagged_Type (T)
4343 and then not Component_Assign
4344 and then not No_Ctrl_Actions (N)
4345 and then Tagged_Type_Expansion;
4346 -- Tags are not saved and restored when VM_Target because VM tags are
4347 -- represented implicitly in objects.
4350 Tag_Tmp : Entity_Id;
4352 Prev_Tmp : Entity_Id;
4353 Next_Tmp : Entity_Id;
4359 -- Finalize the target of the assignment when controlled
4361 -- We have two exceptions here:
4363 -- 1. If we are in an init proc since it is an initialization more
4364 -- than an assignment.
4366 -- 2. If the left-hand side is a temporary that was not initialized
4367 -- (or the parent part of a temporary since it is the case in
4368 -- extension aggregates). Such a temporary does not come from
4369 -- source. We must examine the original node for the prefix, because
4370 -- it may be a component of an entry formal, in which case it has
4371 -- been rewritten and does not appear to come from source either.
4373 -- Case of init proc
4375 if not Ctrl_Act then
4378 -- The left hand side is an uninitialized temporary object
4380 elsif Nkind (L) = N_Type_Conversion
4381 and then Is_Entity_Name (Expression (L))
4382 and then Nkind (Parent (Entity (Expression (L)))) =
4383 N_Object_Declaration
4384 and then No_Initialization (Parent (Entity (Expression (L))))
4389 Append_List_To (Res,
4391 (Ref => Duplicate_Subexpr_No_Checks (L),
4393 With_Detach => New_Reference_To (Standard_False, Loc)));
4396 -- Save the Tag in a local variable Tag_Tmp
4400 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4403 Make_Object_Declaration (Loc,
4404 Defining_Identifier => Tag_Tmp,
4405 Object_Definition => New_Reference_To (RTE (RE_Tag), Loc),
4407 Make_Selected_Component (Loc,
4408 Prefix => Duplicate_Subexpr_No_Checks (L),
4409 Selector_Name => New_Reference_To (First_Tag_Component (T),
4412 -- Otherwise Tag_Tmp not used
4419 if VM_Target /= No_VM then
4421 -- Cannot assign part of the object in a VM context, so instead
4422 -- fallback to the previous mechanism, even though it is not
4423 -- completely correct ???
4425 -- Save the Finalization Pointers in local variables Prev_Tmp and
4426 -- Next_Tmp. For objects with Has_Controlled_Component set, these
4427 -- pointers are in the Record_Controller
4429 Ctrl_Ref := Duplicate_Subexpr (L);
4431 if Has_Controlled_Component (T) then
4433 Make_Selected_Component (Loc,
4436 New_Reference_To (Controller_Component (T), Loc));
4440 Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
4443 Make_Object_Declaration (Loc,
4444 Defining_Identifier => Prev_Tmp,
4446 Object_Definition =>
4447 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
4450 Make_Selected_Component (Loc,
4452 Unchecked_Convert_To (RTE (RE_Finalizable), Ctrl_Ref),
4453 Selector_Name => Make_Identifier (Loc, Name_Prev))));
4456 Make_Defining_Identifier (Loc,
4457 Chars => New_Internal_Name ('C'));
4460 Make_Object_Declaration (Loc,
4461 Defining_Identifier => Next_Tmp,
4463 Object_Definition =>
4464 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
4467 Make_Selected_Component (Loc,
4469 Unchecked_Convert_To (RTE (RE_Finalizable),
4470 New_Copy_Tree (Ctrl_Ref)),
4471 Selector_Name => Make_Identifier (Loc, Name_Next))));
4473 -- Do the Assignment
4475 Append_To (Res, Relocate_Node (N));
4478 -- Regular (non VM) processing for controlled types and types with
4479 -- controlled components
4481 -- Variables of such types contain pointers used to chain them in
4482 -- finalization lists, in addition to user data. These pointers
4483 -- are specific to each object of the type, not to the value being
4486 -- Thus they need to be left intact during the assignment. We
4487 -- achieve this by constructing a Storage_Array subtype, and by
4488 -- overlaying objects of this type on the source and target of the
4489 -- assignment. The assignment is then rewritten to assignments of
4490 -- slices of these arrays, copying the user data, and leaving the
4491 -- pointers untouched.
4493 Controlled_Actions : declare
4495 -- A reference to the Prev component of the record controller
4497 First_After_Root : Node_Id := Empty;
4498 -- Index of first byte to be copied (used to skip
4499 -- Root_Controlled in controlled objects).
4501 Last_Before_Hole : Node_Id := Empty;
4502 -- Index of last byte to be copied before outermost record
4505 Hole_Length : Node_Id := Empty;
4506 -- Length of record controller data (Prev and Next pointers)
4508 First_After_Hole : Node_Id := Empty;
4509 -- Index of first byte to be copied after outermost record
4512 Expr, Source_Size : Node_Id;
4513 Source_Actual_Subtype : Entity_Id;
4514 -- Used for computation of the size of the data to be copied
4516 Range_Type : Entity_Id;
4517 Opaque_Type : Entity_Id;
4519 function Build_Slice
4522 Hi : Node_Id) return Node_Id;
4523 -- Build and return a slice of an array of type S overlaid on
4524 -- object Rec, with bounds specified by Lo and Hi. If either
4525 -- bound is empty, a default of S'First (respectively S'Last)
4532 function Build_Slice
4535 Hi : Node_Id) return Node_Id
4540 Opaque : constant Node_Id :=
4541 Unchecked_Convert_To (Opaque_Type,
4542 Make_Attribute_Reference (Loc,
4544 Attribute_Name => Name_Address));
4545 -- Access value designating an opaque storage array of type
4546 -- S overlaid on record Rec.
4549 -- Compute slice bounds using S'First (1) and S'Last as
4550 -- default values when not specified by the caller.
4553 Lo_Bound := Make_Integer_Literal (Loc, 1);
4559 Hi_Bound := Make_Attribute_Reference (Loc,
4560 Prefix => New_Occurrence_Of (Range_Type, Loc),
4561 Attribute_Name => Name_Last);
4566 return Make_Slice (Loc,
4569 Discrete_Range => Make_Range (Loc,
4570 Lo_Bound, Hi_Bound));
4573 -- Start of processing for Controlled_Actions
4576 -- Create a constrained subtype of Storage_Array whose size
4577 -- corresponds to the value being assigned.
4579 -- subtype G is Storage_Offset range
4580 -- 1 .. (Expr'Size + Storage_Unit - 1) / Storage_Unit
4582 Expr := Duplicate_Subexpr_No_Checks (Expression (N));
4584 if Nkind (Expr) = N_Qualified_Expression then
4585 Expr := Expression (Expr);
4588 Source_Actual_Subtype := Etype (Expr);
4590 if Has_Discriminants (Source_Actual_Subtype)
4591 and then not Is_Constrained (Source_Actual_Subtype)
4594 Build_Actual_Subtype (Source_Actual_Subtype, Expr));
4595 Source_Actual_Subtype := Defining_Identifier (Last (Res));
4601 Make_Attribute_Reference (Loc,
4603 New_Occurrence_Of (Source_Actual_Subtype, Loc),
4604 Attribute_Name => Name_Size),
4606 Make_Integer_Literal (Loc,
4607 Intval => System_Storage_Unit - 1));
4610 Make_Op_Divide (Loc,
4611 Left_Opnd => Source_Size,
4613 Make_Integer_Literal (Loc,
4614 Intval => System_Storage_Unit));
4617 Make_Defining_Identifier (Loc,
4618 New_Internal_Name ('G'));
4621 Make_Subtype_Declaration (Loc,
4622 Defining_Identifier => Range_Type,
4623 Subtype_Indication =>
4624 Make_Subtype_Indication (Loc,
4626 New_Reference_To (RTE (RE_Storage_Offset), Loc),
4627 Constraint => Make_Range_Constraint (Loc,
4630 Low_Bound => Make_Integer_Literal (Loc, 1),
4631 High_Bound => Source_Size)))));
4633 -- subtype S is Storage_Array (G)
4636 Make_Subtype_Declaration (Loc,
4637 Defining_Identifier =>
4638 Make_Defining_Identifier (Loc,
4639 New_Internal_Name ('S')),
4640 Subtype_Indication =>
4641 Make_Subtype_Indication (Loc,
4643 New_Reference_To (RTE (RE_Storage_Array), Loc),
4645 Make_Index_Or_Discriminant_Constraint (Loc,
4647 New_List (New_Reference_To (Range_Type, Loc))))));
4649 -- type A is access S
4652 Make_Defining_Identifier (Loc,
4653 Chars => New_Internal_Name ('A'));
4656 Make_Full_Type_Declaration (Loc,
4657 Defining_Identifier => Opaque_Type,
4659 Make_Access_To_Object_Definition (Loc,
4660 Subtype_Indication =>
4662 Defining_Identifier (Last (Res)), Loc))));
4664 -- Generate appropriate slice assignments
4666 First_After_Root := Make_Integer_Literal (Loc, 1);
4668 -- For controlled object, skip Root_Controlled part
4670 if Is_Controlled (T) then
4674 Make_Op_Divide (Loc,
4675 Make_Attribute_Reference (Loc,
4677 New_Occurrence_Of (RTE (RE_Root_Controlled), Loc),
4678 Attribute_Name => Name_Size),
4679 Make_Integer_Literal (Loc, System_Storage_Unit)));
4682 -- For the case of a record with controlled components, skip
4683 -- record controller Prev/Next components. These components
4684 -- constitute a 'hole' in the middle of the data to be copied.
4686 if Has_Controlled_Component (T) then
4688 Make_Selected_Component (Loc,
4690 Make_Selected_Component (Loc,
4691 Prefix => Duplicate_Subexpr_No_Checks (L),
4693 New_Reference_To (Controller_Component (T), Loc)),
4694 Selector_Name => Make_Identifier (Loc, Name_Prev));
4696 -- Last index before hole: determined by position of the
4697 -- _Controller.Prev component.
4700 Make_Defining_Identifier (Loc,
4701 New_Internal_Name ('L'));
4704 Make_Object_Declaration (Loc,
4705 Defining_Identifier => Last_Before_Hole,
4706 Object_Definition => New_Occurrence_Of (
4707 RTE (RE_Storage_Offset), Loc),
4708 Constant_Present => True,
4709 Expression => Make_Op_Add (Loc,
4710 Make_Attribute_Reference (Loc,
4712 Attribute_Name => Name_Position),
4713 Make_Attribute_Reference (Loc,
4714 Prefix => New_Copy_Tree (Prefix (Prev_Ref)),
4715 Attribute_Name => Name_Position))));
4717 -- Hole length: size of the Prev and Next components
4720 Make_Op_Multiply (Loc,
4721 Left_Opnd => Make_Integer_Literal (Loc, Uint_2),
4723 Make_Op_Divide (Loc,
4725 Make_Attribute_Reference (Loc,
4726 Prefix => New_Copy_Tree (Prev_Ref),
4727 Attribute_Name => Name_Size),
4729 Make_Integer_Literal (Loc,
4730 Intval => System_Storage_Unit)));
4732 -- First index after hole
4735 Make_Defining_Identifier (Loc,
4736 New_Internal_Name ('F'));
4739 Make_Object_Declaration (Loc,
4740 Defining_Identifier => First_After_Hole,
4741 Object_Definition => New_Occurrence_Of (
4742 RTE (RE_Storage_Offset), Loc),
4743 Constant_Present => True,
4749 New_Occurrence_Of (Last_Before_Hole, Loc),
4750 Right_Opnd => Hole_Length),
4751 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4754 New_Occurrence_Of (Last_Before_Hole, Loc);
4756 New_Occurrence_Of (First_After_Hole, Loc);
4759 -- Assign the first slice (possibly skipping Root_Controlled,
4760 -- up to the beginning of the record controller if present,
4761 -- up to the end of the object if not).
4763 Append_To (Res, Make_Assignment_Statement (Loc,
4764 Name => Build_Slice (
4765 Rec => Duplicate_Subexpr_No_Checks (L),
4766 Lo => First_After_Root,
4767 Hi => Last_Before_Hole),
4769 Expression => Build_Slice (
4770 Rec => Expression (N),
4771 Lo => First_After_Root,
4772 Hi => New_Copy_Tree (Last_Before_Hole))));
4774 if Present (First_After_Hole) then
4776 -- If a record controller is present, copy the second slice,
4777 -- from right after the _Controller.Next component up to the
4778 -- end of the object.
4780 Append_To (Res, Make_Assignment_Statement (Loc,
4781 Name => Build_Slice (
4782 Rec => Duplicate_Subexpr_No_Checks (L),
4783 Lo => First_After_Hole,
4785 Expression => Build_Slice (
4786 Rec => Duplicate_Subexpr_No_Checks (Expression (N)),
4787 Lo => New_Copy_Tree (First_After_Hole),
4790 end Controlled_Actions;
4793 -- Not controlled case
4797 Asn : constant Node_Id := Relocate_Node (N);
4800 -- If this is the case of a tagged type with a full rep clause,
4801 -- we must expand it into component assignments, so we mark the
4802 -- node as unanalyzed, to get it reanalyzed, but flag it has
4803 -- requiring component-wise assignment so we don't get infinite
4806 if Component_Assign then
4807 Set_Analyzed (Asn, False);
4808 Set_Componentwise_Assignment (Asn, True);
4811 Append_To (Res, Asn);
4819 Make_Assignment_Statement (Loc,
4821 Make_Selected_Component (Loc,
4822 Prefix => Duplicate_Subexpr_No_Checks (L),
4823 Selector_Name => New_Reference_To (First_Tag_Component (T),
4825 Expression => New_Reference_To (Tag_Tmp, Loc)));
4829 if VM_Target /= No_VM then
4830 -- Restore the finalization pointers
4833 Make_Assignment_Statement (Loc,
4835 Make_Selected_Component (Loc,
4837 Unchecked_Convert_To (RTE (RE_Finalizable),
4838 New_Copy_Tree (Ctrl_Ref)),
4839 Selector_Name => Make_Identifier (Loc, Name_Prev)),
4840 Expression => New_Reference_To (Prev_Tmp, Loc)));
4843 Make_Assignment_Statement (Loc,
4845 Make_Selected_Component (Loc,
4847 Unchecked_Convert_To (RTE (RE_Finalizable),
4848 New_Copy_Tree (Ctrl_Ref)),
4849 Selector_Name => Make_Identifier (Loc, Name_Next)),
4850 Expression => New_Reference_To (Next_Tmp, Loc)));
4853 -- Adjust the target after the assignment when controlled (not in the
4854 -- init proc since it is an initialization more than an assignment).
4856 Append_List_To (Res,
4858 Ref => Duplicate_Subexpr_Move_Checks (L),
4860 Flist_Ref => New_Reference_To (RTE (RE_Global_Final_List), Loc),
4861 With_Attach => Make_Integer_Literal (Loc, 0)));
4867 -- Could use comment here ???
4869 when RE_Not_Available =>
4871 end Make_Tag_Ctrl_Assignment;