1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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_Ch3; use Sem_Ch3;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch13; use Sem_Ch13;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Util; use Sem_Util;
55 with Snames; use Snames;
56 with Stand; use Stand;
57 with Stringt; use Stringt;
58 with Targparm; use Targparm;
59 with Tbuild; use Tbuild;
60 with Ttypes; use Ttypes;
61 with Uintp; use Uintp;
62 with Validsw; use Validsw;
64 package body Exp_Ch5 is
66 function Change_Of_Representation (N : Node_Id) return Boolean;
67 -- Determine if the right hand side of the assignment N is a type
68 -- conversion which requires a change of representation. Called
69 -- only for the array and record cases.
71 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id);
72 -- N is an assignment which assigns an array value. This routine process
73 -- the various special cases and checks required for such assignments,
74 -- including change of representation. Rhs is normally simply the right
75 -- hand side of the assignment, except that if the right hand side is
76 -- a type conversion or a qualified expression, then the Rhs is the
77 -- actual expression inside any such type conversions or qualifications.
79 function Expand_Assign_Array_Loop
86 Rev : Boolean) return Node_Id;
87 -- N is an assignment statement which assigns an array value. This routine
88 -- expands the assignment into a loop (or nested loops for the case of a
89 -- multi-dimensional array) to do the assignment component by component.
90 -- Larray and Rarray are the entities of the actual arrays on the left
91 -- hand and right hand sides. L_Type and R_Type are the types of these
92 -- arrays (which may not be the same, due to either sliding, or to a
93 -- change of representation case). Ndim is the number of dimensions and
94 -- the parameter Rev indicates if the loops run normally (Rev = False),
95 -- or reversed (Rev = True). The value returned is the constructed
96 -- loop statement. Auxiliary declarations are inserted before node N
97 -- using the standard Insert_Actions mechanism.
99 procedure Expand_Assign_Record (N : Node_Id);
100 -- N is an assignment of a non-tagged record value. This routine handles
101 -- the case where the assignment must be made component by component,
102 -- either because the target is not byte aligned, or there is a change
103 -- of representation.
105 procedure Expand_Non_Function_Return (N : Node_Id);
106 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
107 -- a procedure body, entry body, accept statement, or extended return
108 -- statement. Note that all non-function returns are simple return
111 procedure Expand_Simple_Function_Return (N : Node_Id);
112 -- Expand simple return from function. In the case where we are returning
113 -- from a function body this is called by Expand_N_Simple_Return_Statement.
115 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id;
116 -- Generate the necessary code for controlled and tagged assignment,
117 -- that is to say, finalization of the target before, adjustment of
118 -- the target after and save and restore of the tag and finalization
119 -- pointers which are not 'part of the value' and must not be changed
120 -- upon assignment. N is the original Assignment node.
122 ------------------------------
123 -- Change_Of_Representation --
124 ------------------------------
126 function Change_Of_Representation (N : Node_Id) return Boolean is
127 Rhs : constant Node_Id := Expression (N);
130 Nkind (Rhs) = N_Type_Conversion
132 not Same_Representation (Etype (Rhs), Etype (Expression (Rhs)));
133 end Change_Of_Representation;
135 -------------------------
136 -- Expand_Assign_Array --
137 -------------------------
139 -- There are two issues here. First, do we let Gigi do a block move, or
140 -- do we expand out into a loop? Second, we need to set the two flags
141 -- Forwards_OK and Backwards_OK which show whether the block move (or
142 -- corresponding loops) can be legitimately done in a forwards (low to
143 -- high) or backwards (high to low) manner.
145 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id) is
146 Loc : constant Source_Ptr := Sloc (N);
148 Lhs : constant Node_Id := Name (N);
150 Act_Lhs : constant Node_Id := Get_Referenced_Object (Lhs);
151 Act_Rhs : Node_Id := Get_Referenced_Object (Rhs);
153 L_Type : constant Entity_Id :=
154 Underlying_Type (Get_Actual_Subtype (Act_Lhs));
155 R_Type : Entity_Id :=
156 Underlying_Type (Get_Actual_Subtype (Act_Rhs));
158 L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice;
159 R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice;
161 Crep : constant Boolean := Change_Of_Representation (N);
166 Ndim : constant Pos := Number_Dimensions (L_Type);
168 Loop_Required : Boolean := False;
169 -- This switch is set to True if the array move must be done using
170 -- an explicit front end generated loop.
172 procedure Apply_Dereference (Arg : Node_Id);
173 -- If the argument is an access to an array, and the assignment is
174 -- converted into a procedure call, apply explicit dereference.
176 function Has_Address_Clause (Exp : Node_Id) return Boolean;
177 -- Test if Exp is a reference to an array whose declaration has
178 -- an address clause, or it is a slice of such an array.
180 function Is_Formal_Array (Exp : Node_Id) return Boolean;
181 -- Test if Exp is a reference to an array which is either a formal
182 -- parameter or a slice of a formal parameter. These are the cases
183 -- where hidden aliasing can occur.
185 function Is_Non_Local_Array (Exp : Node_Id) return Boolean;
186 -- Determine if Exp is a reference to an array variable which is other
187 -- than an object defined in the current scope, or a slice of such
188 -- an object. Such objects can be aliased to parameters (unlike local
189 -- array references).
191 -----------------------
192 -- Apply_Dereference --
193 -----------------------
195 procedure Apply_Dereference (Arg : Node_Id) is
196 Typ : constant Entity_Id := Etype (Arg);
198 if Is_Access_Type (Typ) then
199 Rewrite (Arg, Make_Explicit_Dereference (Loc,
200 Prefix => Relocate_Node (Arg)));
201 Analyze_And_Resolve (Arg, Designated_Type (Typ));
203 end Apply_Dereference;
205 ------------------------
206 -- Has_Address_Clause --
207 ------------------------
209 function Has_Address_Clause (Exp : Node_Id) return Boolean is
212 (Is_Entity_Name (Exp) and then
213 Present (Address_Clause (Entity (Exp))))
215 (Nkind (Exp) = N_Slice and then Has_Address_Clause (Prefix (Exp)));
216 end Has_Address_Clause;
218 ---------------------
219 -- Is_Formal_Array --
220 ---------------------
222 function Is_Formal_Array (Exp : Node_Id) return Boolean is
225 (Is_Entity_Name (Exp) and then Is_Formal (Entity (Exp)))
227 (Nkind (Exp) = N_Slice and then Is_Formal_Array (Prefix (Exp)));
230 ------------------------
231 -- Is_Non_Local_Array --
232 ------------------------
234 function Is_Non_Local_Array (Exp : Node_Id) return Boolean is
236 return (Is_Entity_Name (Exp)
237 and then Scope (Entity (Exp)) /= Current_Scope)
238 or else (Nkind (Exp) = N_Slice
239 and then Is_Non_Local_Array (Prefix (Exp)));
240 end Is_Non_Local_Array;
242 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
244 Lhs_Formal : constant Boolean := Is_Formal_Array (Act_Lhs);
245 Rhs_Formal : constant Boolean := Is_Formal_Array (Act_Rhs);
247 Lhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Lhs);
248 Rhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Rhs);
250 -- Start of processing for Expand_Assign_Array
253 -- Deal with length check. Note that the length check is done with
254 -- respect to the right hand side as given, not a possible underlying
255 -- renamed object, since this would generate incorrect extra checks.
257 Apply_Length_Check (Rhs, L_Type);
259 -- We start by assuming that the move can be done in either direction,
260 -- i.e. that the two sides are completely disjoint.
262 Set_Forwards_OK (N, True);
263 Set_Backwards_OK (N, True);
265 -- Normally it is only the slice case that can lead to overlap, and
266 -- explicit checks for slices are made below. But there is one case
267 -- where the slice can be implicit and invisible to us: when we have a
268 -- one dimensional array, and either both operands are parameters, or
269 -- one is a parameter (which can be a slice passed by reference) and the
270 -- other is a non-local variable. In this case the parameter could be a
271 -- slice that overlaps with the other operand.
273 -- However, if the array subtype is a constrained first subtype in the
274 -- parameter case, then we don't have to worry about overlap, since
275 -- slice assignments aren't possible (other than for a slice denoting
278 -- Note: No overlap is possible if there is a change of representation,
279 -- so we can exclude this case.
284 ((Lhs_Formal and Rhs_Formal)
286 (Lhs_Formal and Rhs_Non_Local_Var)
288 (Rhs_Formal and Lhs_Non_Local_Var))
290 (not Is_Constrained (Etype (Lhs))
291 or else not Is_First_Subtype (Etype (Lhs)))
293 -- In the case of compiling for the Java or .NET Virtual Machine,
294 -- slices are always passed by making a copy, so we don't have to
295 -- worry about overlap. We also want to prevent generation of "<"
296 -- comparisons for array addresses, since that's a meaningless
297 -- operation on the VM.
299 and then VM_Target = No_VM
301 Set_Forwards_OK (N, False);
302 Set_Backwards_OK (N, False);
304 -- Note: the bit-packed case is not worrisome here, since if we have
305 -- a slice passed as a parameter, it is always aligned on a byte
306 -- boundary, and if there are no explicit slices, the assignment
307 -- can be performed directly.
310 -- We certainly must use a loop for change of representation and also
311 -- we use the operand of the conversion on the right hand side as the
312 -- effective right hand side (the component types must match in this
316 Act_Rhs := Get_Referenced_Object (Rhs);
317 R_Type := Get_Actual_Subtype (Act_Rhs);
318 Loop_Required := True;
320 -- We require a loop if the left side is possibly bit unaligned
322 elsif Possible_Bit_Aligned_Component (Lhs)
324 Possible_Bit_Aligned_Component (Rhs)
326 Loop_Required := True;
328 -- Arrays with controlled components are expanded into a loop to force
329 -- calls to Adjust at the component level.
331 elsif Has_Controlled_Component (L_Type) then
332 Loop_Required := True;
334 -- If object is atomic, we cannot tolerate a loop
336 elsif Is_Atomic_Object (Act_Lhs)
338 Is_Atomic_Object (Act_Rhs)
342 -- Loop is required if we have atomic components since we have to
343 -- be sure to do any accesses on an element by element basis.
345 elsif Has_Atomic_Components (L_Type)
346 or else Has_Atomic_Components (R_Type)
347 or else Is_Atomic (Component_Type (L_Type))
348 or else Is_Atomic (Component_Type (R_Type))
350 Loop_Required := True;
352 -- Case where no slice is involved
354 elsif not L_Slice and not R_Slice then
356 -- The following code deals with the case of unconstrained bit packed
357 -- arrays. The problem is that the template for such arrays contains
358 -- the bounds of the actual source level array, but the copy of an
359 -- entire array requires the bounds of the underlying array. It would
360 -- be nice if the back end could take care of this, but right now it
361 -- does not know how, so if we have such a type, then we expand out
362 -- into a loop, which is inefficient but works correctly. If we don't
363 -- do this, we get the wrong length computed for the array to be
364 -- moved. The two cases we need to worry about are:
366 -- Explicit deference of an unconstrained packed array type as in the
367 -- following example:
370 -- type BITS is array(INTEGER range <>) of BOOLEAN;
371 -- pragma PACK(BITS);
372 -- type A is access BITS;
375 -- P1 := new BITS (1 .. 65_535);
376 -- P2 := new BITS (1 .. 65_535);
380 -- A formal parameter reference with an unconstrained bit array type
381 -- is the other case we need to worry about (here we assume the same
382 -- BITS type declared above):
384 -- procedure Write_All (File : out BITS; Contents : BITS);
386 -- File.Storage := Contents;
389 -- We expand to a loop in either of these two cases
391 -- Question for future thought. Another potentially more efficient
392 -- approach would be to create the actual subtype, and then do an
393 -- unchecked conversion to this actual subtype ???
395 Check_Unconstrained_Bit_Packed_Array : declare
397 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean;
398 -- Function to perform required test for the first case, above
399 -- (dereference of an unconstrained bit packed array).
401 -----------------------
402 -- Is_UBPA_Reference --
403 -----------------------
405 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean is
406 Typ : constant Entity_Id := Underlying_Type (Etype (Opnd));
408 Des_Type : Entity_Id;
411 if Present (Packed_Array_Type (Typ))
412 and then Is_Array_Type (Packed_Array_Type (Typ))
413 and then not Is_Constrained (Packed_Array_Type (Typ))
417 elsif Nkind (Opnd) = N_Explicit_Dereference then
418 P_Type := Underlying_Type (Etype (Prefix (Opnd)));
420 if not Is_Access_Type (P_Type) then
424 Des_Type := Designated_Type (P_Type);
426 Is_Bit_Packed_Array (Des_Type)
427 and then not Is_Constrained (Des_Type);
433 end Is_UBPA_Reference;
435 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
438 if Is_UBPA_Reference (Lhs)
440 Is_UBPA_Reference (Rhs)
442 Loop_Required := True;
444 -- Here if we do not have the case of a reference to a bit packed
445 -- unconstrained array case. In this case gigi can most certainly
446 -- handle the assignment if a forwards move is allowed.
448 -- (could it handle the backwards case also???)
450 elsif Forwards_OK (N) then
453 end Check_Unconstrained_Bit_Packed_Array;
455 -- The back end can always handle the assignment if the right side is a
456 -- string literal (note that overlap is definitely impossible in this
457 -- case). If the type is packed, a string literal is always converted
458 -- into an aggregate, except in the case of a null slice, for which no
459 -- aggregate can be written. In that case, rewrite the assignment as a
460 -- null statement, a length check has already been emitted to verify
461 -- that the range of the left-hand side is empty.
463 -- Note that this code is not executed if we have an assignment of a
464 -- string literal to a non-bit aligned component of a record, a case
465 -- which cannot be handled by the backend.
467 elsif Nkind (Rhs) = N_String_Literal then
468 if String_Length (Strval (Rhs)) = 0
469 and then Is_Bit_Packed_Array (L_Type)
471 Rewrite (N, Make_Null_Statement (Loc));
477 -- If either operand is bit packed, then we need a loop, since we can't
478 -- be sure that the slice is byte aligned. Similarly, if either operand
479 -- is a possibly unaligned slice, then we need a loop (since the back
480 -- end cannot handle unaligned slices).
482 elsif Is_Bit_Packed_Array (L_Type)
483 or else Is_Bit_Packed_Array (R_Type)
484 or else Is_Possibly_Unaligned_Slice (Lhs)
485 or else Is_Possibly_Unaligned_Slice (Rhs)
487 Loop_Required := True;
489 -- If we are not bit-packed, and we have only one slice, then no overlap
490 -- is possible except in the parameter case, so we can let the back end
493 elsif not (L_Slice and R_Slice) then
494 if Forwards_OK (N) then
499 -- If the right-hand side is a string literal, introduce a temporary for
500 -- it, for use in the generated loop that will follow.
502 if Nkind (Rhs) = N_String_Literal then
504 Temp : constant Entity_Id :=
505 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
510 Make_Object_Declaration (Loc,
511 Defining_Identifier => Temp,
512 Object_Definition => New_Occurrence_Of (L_Type, Loc),
513 Expression => Relocate_Node (Rhs));
515 Insert_Action (N, Decl);
516 Rewrite (Rhs, New_Occurrence_Of (Temp, Loc));
517 R_Type := Etype (Temp);
521 -- Come here to complete the analysis
523 -- Loop_Required: Set to True if we know that a loop is required
524 -- regardless of overlap considerations.
526 -- Forwards_OK: Set to False if we already know that a forwards
527 -- move is not safe, else set to True.
529 -- Backwards_OK: Set to False if we already know that a backwards
530 -- move is not safe, else set to True
532 -- Our task at this stage is to complete the overlap analysis, which can
533 -- result in possibly setting Forwards_OK or Backwards_OK to False, and
534 -- then generating the final code, either by deciding that it is OK
535 -- after all to let Gigi handle it, or by generating appropriate code
539 L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
540 R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
542 Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
543 Left_Hi : constant Node_Id := Type_High_Bound (L_Index_Typ);
544 Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
545 Right_Hi : constant Node_Id := Type_High_Bound (R_Index_Typ);
547 Act_L_Array : Node_Id;
548 Act_R_Array : Node_Id;
554 Cresult : Compare_Result;
557 -- Get the expressions for the arrays. If we are dealing with a
558 -- private type, then convert to the underlying type. We can do
559 -- direct assignments to an array that is a private type, but we
560 -- cannot assign to elements of the array without this extra
561 -- unchecked conversion.
563 if Nkind (Act_Lhs) = N_Slice then
564 Larray := Prefix (Act_Lhs);
568 if Is_Private_Type (Etype (Larray)) then
571 (Underlying_Type (Etype (Larray)), Larray);
575 if Nkind (Act_Rhs) = N_Slice then
576 Rarray := Prefix (Act_Rhs);
580 if Is_Private_Type (Etype (Rarray)) then
583 (Underlying_Type (Etype (Rarray)), Rarray);
587 -- If both sides are slices, we must figure out whether it is safe
588 -- to do the move in one direction or the other. It is always safe
589 -- if there is a change of representation since obviously two arrays
590 -- with different representations cannot possibly overlap.
592 if (not Crep) and L_Slice and R_Slice then
593 Act_L_Array := Get_Referenced_Object (Prefix (Act_Lhs));
594 Act_R_Array := Get_Referenced_Object (Prefix (Act_Rhs));
596 -- If both left and right hand arrays are entity names, and refer
597 -- to different entities, then we know that the move is safe (the
598 -- two storage areas are completely disjoint).
600 if Is_Entity_Name (Act_L_Array)
601 and then Is_Entity_Name (Act_R_Array)
602 and then Entity (Act_L_Array) /= Entity (Act_R_Array)
606 -- Otherwise, we assume the worst, which is that the two arrays
607 -- are the same array. There is no need to check if we know that
608 -- is the case, because if we don't know it, we still have to
611 -- Generally if the same array is involved, then we have an
612 -- overlapping case. We will have to really assume the worst (i.e.
613 -- set neither of the OK flags) unless we can determine the lower
614 -- or upper bounds at compile time and compare them.
617 Cresult := Compile_Time_Compare (Left_Lo, Right_Lo);
619 if Cresult = Unknown then
620 Cresult := Compile_Time_Compare (Left_Hi, Right_Hi);
624 when LT | LE | EQ => Set_Backwards_OK (N, False);
625 when GT | GE => Set_Forwards_OK (N, False);
626 when NE | Unknown => Set_Backwards_OK (N, False);
627 Set_Forwards_OK (N, False);
632 -- If after that analysis, Forwards_OK is still True, and
633 -- Loop_Required is False, meaning that we have not discovered some
634 -- non-overlap reason for requiring a loop, then we can still let
637 if not Loop_Required then
639 -- Assume gigi can handle it if Forwards_OK is set
641 if Forwards_OK (N) then
644 -- If Forwards_OK is not set, the back end will need something
645 -- like memmove to handle the move. For now, this processing is
646 -- activated using the .s debug flag (-gnatd.s).
648 elsif Debug_Flag_Dot_S then
653 -- At this stage we have to generate an explicit loop, and we have
654 -- the following cases:
656 -- Forwards_OK = True
658 -- Rnn : right_index := right_index'First;
659 -- for Lnn in left-index loop
660 -- left (Lnn) := right (Rnn);
661 -- Rnn := right_index'Succ (Rnn);
664 -- Note: the above code MUST be analyzed with checks off, because
665 -- otherwise the Succ could overflow. But in any case this is more
668 -- Forwards_OK = False, Backwards_OK = True
670 -- Rnn : right_index := right_index'Last;
671 -- for Lnn in reverse left-index loop
672 -- left (Lnn) := right (Rnn);
673 -- Rnn := right_index'Pred (Rnn);
676 -- Note: the above code MUST be analyzed with checks off, because
677 -- otherwise the Pred could overflow. But in any case this is more
680 -- Forwards_OK = Backwards_OK = False
682 -- This only happens if we have the same array on each side. It is
683 -- possible to create situations using overlays that violate this,
684 -- but we simply do not promise to get this "right" in this case.
686 -- There are two possible subcases. If the No_Implicit_Conditionals
687 -- restriction is set, then we generate the following code:
690 -- T : constant <operand-type> := rhs;
695 -- If implicit conditionals are permitted, then we generate:
697 -- if Left_Lo <= Right_Lo then
698 -- <code for Forwards_OK = True above>
700 -- <code for Backwards_OK = True above>
703 -- In order to detect possible aliasing, we examine the renamed
704 -- expression when the source or target is a renaming. However,
705 -- the renaming may be intended to capture an address that may be
706 -- affected by subsequent code, and therefore we must recover
707 -- the actual entity for the expansion that follows, not the
708 -- object it renames. In particular, if source or target designate
709 -- a portion of a dynamically allocated object, the pointer to it
710 -- may be reassigned but the renaming preserves the proper location.
712 if Is_Entity_Name (Rhs)
714 Nkind (Parent (Entity (Rhs))) = N_Object_Renaming_Declaration
715 and then Nkind (Act_Rhs) = N_Slice
720 if Is_Entity_Name (Lhs)
722 Nkind (Parent (Entity (Lhs))) = N_Object_Renaming_Declaration
723 and then Nkind (Act_Lhs) = N_Slice
728 -- Cases where either Forwards_OK or Backwards_OK is true
730 if Forwards_OK (N) or else Backwards_OK (N) then
731 if Controlled_Type (Component_Type (L_Type))
732 and then Base_Type (L_Type) = Base_Type (R_Type)
734 and then not No_Ctrl_Actions (N)
737 Proc : constant Entity_Id :=
738 TSS (Base_Type (L_Type), TSS_Slice_Assign);
742 Apply_Dereference (Larray);
743 Apply_Dereference (Rarray);
744 Actuals := New_List (
745 Duplicate_Subexpr (Larray, Name_Req => True),
746 Duplicate_Subexpr (Rarray, Name_Req => True),
747 Duplicate_Subexpr (Left_Lo, Name_Req => True),
748 Duplicate_Subexpr (Left_Hi, Name_Req => True),
749 Duplicate_Subexpr (Right_Lo, Name_Req => True),
750 Duplicate_Subexpr (Right_Hi, Name_Req => True));
754 Boolean_Literals (not Forwards_OK (N)), Loc));
757 Make_Procedure_Call_Statement (Loc,
758 Name => New_Reference_To (Proc, Loc),
759 Parameter_Associations => Actuals));
764 Expand_Assign_Array_Loop
765 (N, Larray, Rarray, L_Type, R_Type, Ndim,
766 Rev => not Forwards_OK (N)));
769 -- Case of both are false with No_Implicit_Conditionals
771 elsif Restriction_Active (No_Implicit_Conditionals) then
773 T : constant Entity_Id :=
774 Make_Defining_Identifier (Loc, Chars => Name_T);
778 Make_Block_Statement (Loc,
779 Declarations => New_List (
780 Make_Object_Declaration (Loc,
781 Defining_Identifier => T,
782 Constant_Present => True,
784 New_Occurrence_Of (Etype (Rhs), Loc),
785 Expression => Relocate_Node (Rhs))),
787 Handled_Statement_Sequence =>
788 Make_Handled_Sequence_Of_Statements (Loc,
789 Statements => New_List (
790 Make_Assignment_Statement (Loc,
791 Name => Relocate_Node (Lhs),
792 Expression => New_Occurrence_Of (T, Loc))))));
795 -- Case of both are false with implicit conditionals allowed
798 -- Before we generate this code, we must ensure that the left and
799 -- right side array types are defined. They may be itypes, and we
800 -- cannot let them be defined inside the if, since the first use
801 -- in the then may not be executed.
803 Ensure_Defined (L_Type, N);
804 Ensure_Defined (R_Type, N);
806 -- We normally compare addresses to find out which way round to
807 -- do the loop, since this is reliable, and handles the cases of
808 -- parameters, conversions etc. But we can't do that in the bit
809 -- packed case or the VM case, because addresses don't work there.
811 if not Is_Bit_Packed_Array (L_Type) and then VM_Target = No_VM then
815 Unchecked_Convert_To (RTE (RE_Integer_Address),
816 Make_Attribute_Reference (Loc,
818 Make_Indexed_Component (Loc,
820 Duplicate_Subexpr_Move_Checks (Larray, True),
821 Expressions => New_List (
822 Make_Attribute_Reference (Loc,
826 Attribute_Name => Name_First))),
827 Attribute_Name => Name_Address)),
830 Unchecked_Convert_To (RTE (RE_Integer_Address),
831 Make_Attribute_Reference (Loc,
833 Make_Indexed_Component (Loc,
835 Duplicate_Subexpr_Move_Checks (Rarray, True),
836 Expressions => New_List (
837 Make_Attribute_Reference (Loc,
841 Attribute_Name => Name_First))),
842 Attribute_Name => Name_Address)));
844 -- For the bit packed and VM cases we use the bounds. That's OK,
845 -- because we don't have to worry about parameters, since they
846 -- cannot cause overlap. Perhaps we should worry about weird slice
850 -- Copy the bounds and reset the Analyzed flag, because the
851 -- bounds of the index type itself may be universal, and must
852 -- must be reaanalyzed to acquire the proper type for Gigi.
854 Cleft_Lo := New_Copy_Tree (Left_Lo);
855 Cright_Lo := New_Copy_Tree (Right_Lo);
856 Set_Analyzed (Cleft_Lo, False);
857 Set_Analyzed (Cright_Lo, False);
861 Left_Opnd => Cleft_Lo,
862 Right_Opnd => Cright_Lo);
865 if Controlled_Type (Component_Type (L_Type))
866 and then Base_Type (L_Type) = Base_Type (R_Type)
868 and then not No_Ctrl_Actions (N)
871 -- Call TSS procedure for array assignment, passing the
872 -- explicit bounds of right and left hand sides.
875 Proc : constant Node_Id :=
876 TSS (Base_Type (L_Type), TSS_Slice_Assign);
880 Apply_Dereference (Larray);
881 Apply_Dereference (Rarray);
882 Actuals := New_List (
883 Duplicate_Subexpr (Larray, Name_Req => True),
884 Duplicate_Subexpr (Rarray, Name_Req => True),
885 Duplicate_Subexpr (Left_Lo, Name_Req => True),
886 Duplicate_Subexpr (Left_Hi, Name_Req => True),
887 Duplicate_Subexpr (Right_Lo, Name_Req => True),
888 Duplicate_Subexpr (Right_Hi, Name_Req => True));
892 Right_Opnd => Condition));
895 Make_Procedure_Call_Statement (Loc,
896 Name => New_Reference_To (Proc, Loc),
897 Parameter_Associations => Actuals));
902 Make_Implicit_If_Statement (N,
903 Condition => Condition,
905 Then_Statements => New_List (
906 Expand_Assign_Array_Loop
907 (N, Larray, Rarray, L_Type, R_Type, Ndim,
910 Else_Statements => New_List (
911 Expand_Assign_Array_Loop
912 (N, Larray, Rarray, L_Type, R_Type, Ndim,
917 Analyze (N, Suppress => All_Checks);
921 when RE_Not_Available =>
923 end Expand_Assign_Array;
925 ------------------------------
926 -- Expand_Assign_Array_Loop --
927 ------------------------------
929 -- The following is an example of the loop generated for the case of a
930 -- two-dimensional array:
935 -- for L1b in 1 .. 100 loop
939 -- for L3b in 1 .. 100 loop
940 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
941 -- R4b := Tm1X2'succ(R4b);
944 -- R2b := Tm1X1'succ(R2b);
948 -- Here Rev is False, and Tm1Xn are the subscript types for the right hand
949 -- side. The declarations of R2b and R4b are inserted before the original
950 -- assignment statement.
952 function Expand_Assign_Array_Loop
959 Rev : Boolean) return Node_Id
961 Loc : constant Source_Ptr := Sloc (N);
963 Lnn : array (1 .. Ndim) of Entity_Id;
964 Rnn : array (1 .. Ndim) of Entity_Id;
965 -- Entities used as subscripts on left and right sides
967 L_Index_Type : array (1 .. Ndim) of Entity_Id;
968 R_Index_Type : array (1 .. Ndim) of Entity_Id;
969 -- Left and right index types
981 F_Or_L := Name_First;
985 -- Setup index types and subscript entities
992 L_Index := First_Index (L_Type);
993 R_Index := First_Index (R_Type);
995 for J in 1 .. Ndim loop
997 Make_Defining_Identifier (Loc,
998 Chars => New_Internal_Name ('L'));
1001 Make_Defining_Identifier (Loc,
1002 Chars => New_Internal_Name ('R'));
1004 L_Index_Type (J) := Etype (L_Index);
1005 R_Index_Type (J) := Etype (R_Index);
1007 Next_Index (L_Index);
1008 Next_Index (R_Index);
1012 -- Now construct the assignment statement
1015 ExprL : constant List_Id := New_List;
1016 ExprR : constant List_Id := New_List;
1019 for J in 1 .. Ndim loop
1020 Append_To (ExprL, New_Occurrence_Of (Lnn (J), Loc));
1021 Append_To (ExprR, New_Occurrence_Of (Rnn (J), Loc));
1025 Make_Assignment_Statement (Loc,
1027 Make_Indexed_Component (Loc,
1028 Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
1029 Expressions => ExprL),
1031 Make_Indexed_Component (Loc,
1032 Prefix => Duplicate_Subexpr (Rarray, Name_Req => True),
1033 Expressions => ExprR));
1035 -- We set assignment OK, since there are some cases, e.g. in object
1036 -- declarations, where we are actually assigning into a constant.
1037 -- If there really is an illegality, it was caught long before now,
1038 -- and was flagged when the original assignment was analyzed.
1040 Set_Assignment_OK (Name (Assign));
1042 -- Propagate the No_Ctrl_Actions flag to individual assignments
1044 Set_No_Ctrl_Actions (Assign, No_Ctrl_Actions (N));
1047 -- Now construct the loop from the inside out, with the last subscript
1048 -- varying most rapidly. Note that Assign is first the raw assignment
1049 -- statement, and then subsequently the loop that wraps it up.
1051 for J in reverse 1 .. Ndim loop
1053 Make_Block_Statement (Loc,
1054 Declarations => New_List (
1055 Make_Object_Declaration (Loc,
1056 Defining_Identifier => Rnn (J),
1057 Object_Definition =>
1058 New_Occurrence_Of (R_Index_Type (J), Loc),
1060 Make_Attribute_Reference (Loc,
1061 Prefix => New_Occurrence_Of (R_Index_Type (J), Loc),
1062 Attribute_Name => F_Or_L))),
1064 Handled_Statement_Sequence =>
1065 Make_Handled_Sequence_Of_Statements (Loc,
1066 Statements => New_List (
1067 Make_Implicit_Loop_Statement (N,
1069 Make_Iteration_Scheme (Loc,
1070 Loop_Parameter_Specification =>
1071 Make_Loop_Parameter_Specification (Loc,
1072 Defining_Identifier => Lnn (J),
1073 Reverse_Present => Rev,
1074 Discrete_Subtype_Definition =>
1075 New_Reference_To (L_Index_Type (J), Loc))),
1077 Statements => New_List (
1080 Make_Assignment_Statement (Loc,
1081 Name => New_Occurrence_Of (Rnn (J), Loc),
1083 Make_Attribute_Reference (Loc,
1085 New_Occurrence_Of (R_Index_Type (J), Loc),
1086 Attribute_Name => S_Or_P,
1087 Expressions => New_List (
1088 New_Occurrence_Of (Rnn (J), Loc)))))))));
1092 end Expand_Assign_Array_Loop;
1094 --------------------------
1095 -- Expand_Assign_Record --
1096 --------------------------
1098 -- The only processing required is in the change of representation case,
1099 -- where we must expand the assignment to a series of field by field
1102 procedure Expand_Assign_Record (N : Node_Id) is
1103 Lhs : constant Node_Id := Name (N);
1104 Rhs : Node_Id := Expression (N);
1107 -- If change of representation, then extract the real right hand side
1108 -- from the type conversion, and proceed with component-wise assignment,
1109 -- since the two types are not the same as far as the back end is
1112 if Change_Of_Representation (N) then
1113 Rhs := Expression (Rhs);
1115 -- If this may be a case of a large bit aligned component, then proceed
1116 -- with component-wise assignment, to avoid possible clobbering of other
1117 -- components sharing bits in the first or last byte of the component to
1120 elsif Possible_Bit_Aligned_Component (Lhs)
1122 Possible_Bit_Aligned_Component (Rhs)
1126 -- If neither condition met, then nothing special to do, the back end
1127 -- can handle assignment of the entire component as a single entity.
1133 -- At this stage we know that we must do a component wise assignment
1136 Loc : constant Source_Ptr := Sloc (N);
1137 R_Typ : constant Entity_Id := Base_Type (Etype (Rhs));
1138 L_Typ : constant Entity_Id := Base_Type (Etype (Lhs));
1139 Decl : constant Node_Id := Declaration_Node (R_Typ);
1143 function Find_Component
1145 Comp : Entity_Id) return Entity_Id;
1146 -- Find the component with the given name in the underlying record
1147 -- declaration for Typ. We need to use the actual entity because the
1148 -- type may be private and resolution by identifier alone would fail.
1150 function Make_Component_List_Assign
1152 U_U : Boolean := False) return List_Id;
1153 -- Returns a sequence of statements to assign the components that
1154 -- are referenced in the given component list. The flag U_U is
1155 -- used to force the usage of the inferred value of the variant
1156 -- part expression as the switch for the generated case statement.
1158 function Make_Field_Assign
1160 U_U : Boolean := False) return Node_Id;
1161 -- Given C, the entity for a discriminant or component, build an
1162 -- assignment for the corresponding field values. The flag U_U
1163 -- signals the presence of an Unchecked_Union and forces the usage
1164 -- of the inferred discriminant value of C as the right hand side
1165 -- of the assignment.
1167 function Make_Field_Assigns (CI : List_Id) return List_Id;
1168 -- Given CI, a component items list, construct series of statements
1169 -- for fieldwise assignment of the corresponding components.
1171 --------------------
1172 -- Find_Component --
1173 --------------------
1175 function Find_Component
1177 Comp : Entity_Id) return Entity_Id
1179 Utyp : constant Entity_Id := Underlying_Type (Typ);
1183 C := First_Entity (Utyp);
1185 while Present (C) loop
1186 if Chars (C) = Chars (Comp) then
1192 raise Program_Error;
1195 --------------------------------
1196 -- Make_Component_List_Assign --
1197 --------------------------------
1199 function Make_Component_List_Assign
1201 U_U : Boolean := False) return List_Id
1203 CI : constant List_Id := Component_Items (CL);
1204 VP : constant Node_Id := Variant_Part (CL);
1214 Result := Make_Field_Assigns (CI);
1216 if Present (VP) then
1218 V := First_Non_Pragma (Variants (VP));
1220 while Present (V) loop
1223 DC := First (Discrete_Choices (V));
1224 while Present (DC) loop
1225 Append_To (DCH, New_Copy_Tree (DC));
1230 Make_Case_Statement_Alternative (Loc,
1231 Discrete_Choices => DCH,
1233 Make_Component_List_Assign (Component_List (V))));
1234 Next_Non_Pragma (V);
1237 -- If we have an Unchecked_Union, use the value of the inferred
1238 -- discriminant of the variant part expression as the switch
1239 -- for the case statement. The case statement may later be
1244 New_Copy (Get_Discriminant_Value (
1247 Discriminant_Constraint (Etype (Rhs))));
1250 Make_Selected_Component (Loc,
1251 Prefix => Duplicate_Subexpr (Rhs),
1253 Make_Identifier (Loc, Chars (Name (VP))));
1257 Make_Case_Statement (Loc,
1259 Alternatives => Alts));
1263 end Make_Component_List_Assign;
1265 -----------------------
1266 -- Make_Field_Assign --
1267 -----------------------
1269 function Make_Field_Assign
1271 U_U : Boolean := False) return Node_Id
1277 -- In the case of an Unchecked_Union, use the discriminant
1278 -- constraint value as on the right hand side of the assignment.
1282 New_Copy (Get_Discriminant_Value (C,
1284 Discriminant_Constraint (Etype (Rhs))));
1287 Make_Selected_Component (Loc,
1288 Prefix => Duplicate_Subexpr (Rhs),
1289 Selector_Name => New_Occurrence_Of (C, Loc));
1293 Make_Assignment_Statement (Loc,
1295 Make_Selected_Component (Loc,
1296 Prefix => Duplicate_Subexpr (Lhs),
1298 New_Occurrence_Of (Find_Component (L_Typ, C), Loc)),
1299 Expression => Expr);
1301 -- Set Assignment_OK, so discriminants can be assigned
1303 Set_Assignment_OK (Name (A), True);
1305 end Make_Field_Assign;
1307 ------------------------
1308 -- Make_Field_Assigns --
1309 ------------------------
1311 function Make_Field_Assigns (CI : List_Id) return List_Id is
1318 while Present (Item) loop
1319 if Nkind (Item) = N_Component_Declaration then
1321 (Result, Make_Field_Assign (Defining_Identifier (Item)));
1328 end Make_Field_Assigns;
1330 -- Start of processing for Expand_Assign_Record
1333 -- Note that we use the base types for this processing. This results
1334 -- in some extra work in the constrained case, but the change of
1335 -- representation case is so unusual that it is not worth the effort.
1337 -- First copy the discriminants. This is done unconditionally. It
1338 -- is required in the unconstrained left side case, and also in the
1339 -- case where this assignment was constructed during the expansion
1340 -- of a type conversion (since initialization of discriminants is
1341 -- suppressed in this case). It is unnecessary but harmless in
1344 if Has_Discriminants (L_Typ) then
1345 F := First_Discriminant (R_Typ);
1346 while Present (F) loop
1348 if Is_Unchecked_Union (Base_Type (R_Typ)) then
1349 Insert_Action (N, Make_Field_Assign (F, True));
1351 Insert_Action (N, Make_Field_Assign (F));
1354 Next_Discriminant (F);
1358 -- We know the underlying type is a record, but its current view
1359 -- may be private. We must retrieve the usable record declaration.
1361 if Nkind (Decl) = N_Private_Type_Declaration
1362 and then Present (Full_View (R_Typ))
1364 RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
1366 RDef := Type_Definition (Decl);
1369 if Nkind (RDef) = N_Record_Definition
1370 and then Present (Component_List (RDef))
1373 if Is_Unchecked_Union (R_Typ) then
1375 Make_Component_List_Assign (Component_List (RDef), True));
1378 (N, Make_Component_List_Assign (Component_List (RDef)));
1381 Rewrite (N, Make_Null_Statement (Loc));
1385 end Expand_Assign_Record;
1387 -----------------------------------
1388 -- Expand_N_Assignment_Statement --
1389 -----------------------------------
1391 -- This procedure implements various cases where an assignment statement
1392 -- cannot just be passed on to the back end in untransformed state.
1394 procedure Expand_N_Assignment_Statement (N : Node_Id) is
1395 Loc : constant Source_Ptr := Sloc (N);
1396 Lhs : constant Node_Id := Name (N);
1397 Rhs : constant Node_Id := Expression (N);
1398 Typ : constant Entity_Id := Underlying_Type (Etype (Lhs));
1402 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1404 -- Rewrite an assignment to X'Priority into a run-time call
1406 -- For example: X'Priority := New_Prio_Expr;
1407 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1409 -- Note that although X'Priority is notionally an object, it is quite
1410 -- deliberately not defined as an aliased object in the RM. This means
1411 -- that it works fine to rewrite it as a call, without having to worry
1412 -- about complications that would other arise from X'Priority'Access,
1413 -- which is illegal, because of the lack of aliasing.
1415 if Ada_Version >= Ada_05 then
1418 Conctyp : Entity_Id;
1421 RT_Subprg_Name : Node_Id;
1424 -- Handle chains of renamings
1427 while Nkind (Ent) in N_Has_Entity
1428 and then Present (Entity (Ent))
1429 and then Present (Renamed_Object (Entity (Ent)))
1431 Ent := Renamed_Object (Entity (Ent));
1434 -- The attribute Priority applied to protected objects has been
1435 -- previously expanded into a call to the Get_Ceiling run-time
1438 if Nkind (Ent) = N_Function_Call
1439 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
1441 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling))
1443 -- Look for the enclosing concurrent type
1445 Conctyp := Current_Scope;
1446 while not Is_Concurrent_Type (Conctyp) loop
1447 Conctyp := Scope (Conctyp);
1450 pragma Assert (Is_Protected_Type (Conctyp));
1452 -- Generate the first actual of the call
1454 Subprg := Current_Scope;
1455 while not Present (Protected_Body_Subprogram (Subprg)) loop
1456 Subprg := Scope (Subprg);
1459 -- Select the appropriate run-time call
1461 if Number_Entries (Conctyp) = 0 then
1463 New_Reference_To (RTE (RE_Set_Ceiling), Loc);
1466 New_Reference_To (RTE (RO_PE_Set_Ceiling), Loc);
1470 Make_Procedure_Call_Statement (Loc,
1471 Name => RT_Subprg_Name,
1472 Parameter_Associations => New_List (
1473 New_Copy_Tree (First (Parameter_Associations (Ent))),
1474 Relocate_Node (Expression (N))));
1483 -- First deal with generation of range check if required. For now we do
1484 -- this only for discrete types.
1486 if Do_Range_Check (Rhs)
1487 and then Is_Discrete_Type (Typ)
1489 Set_Do_Range_Check (Rhs, False);
1490 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
1493 -- Check for a special case where a high level transformation is
1494 -- required. If we have either of:
1499 -- where P is a reference to a bit packed array, then we have to unwind
1500 -- the assignment. The exact meaning of being a reference to a bit
1501 -- packed array is as follows:
1503 -- An indexed component whose prefix is a bit packed array is a
1504 -- reference to a bit packed array.
1506 -- An indexed component or selected component whose prefix is a
1507 -- reference to a bit packed array is itself a reference ot a
1508 -- bit packed array.
1510 -- The required transformation is
1512 -- Tnn : prefix_type := P;
1513 -- Tnn.field := rhs;
1518 -- Tnn : prefix_type := P;
1519 -- Tnn (subscr) := rhs;
1522 -- Since P is going to be evaluated more than once, any subscripts
1523 -- in P must have their evaluation forced.
1525 if Nkind_In (Lhs, N_Indexed_Component, N_Selected_Component)
1526 and then Is_Ref_To_Bit_Packed_Array (Prefix (Lhs))
1529 BPAR_Expr : constant Node_Id := Relocate_Node (Prefix (Lhs));
1530 BPAR_Typ : constant Entity_Id := Etype (BPAR_Expr);
1531 Tnn : constant Entity_Id :=
1532 Make_Defining_Identifier (Loc,
1533 Chars => New_Internal_Name ('T'));
1536 -- Insert the post assignment first, because we want to copy the
1537 -- BPAR_Expr tree before it gets analyzed in the context of the
1538 -- pre assignment. Note that we do not analyze the post assignment
1539 -- yet (we cannot till we have completed the analysis of the pre
1540 -- assignment). As usual, the analysis of this post assignment
1541 -- will happen on its own when we "run into" it after finishing
1542 -- the current assignment.
1545 Make_Assignment_Statement (Loc,
1546 Name => New_Copy_Tree (BPAR_Expr),
1547 Expression => New_Occurrence_Of (Tnn, Loc)));
1549 -- At this stage BPAR_Expr is a reference to a bit packed array
1550 -- where the reference was not expanded in the original tree,
1551 -- since it was on the left side of an assignment. But in the
1552 -- pre-assignment statement (the object definition), BPAR_Expr
1553 -- will end up on the right hand side, and must be reexpanded. To
1554 -- achieve this, we reset the analyzed flag of all selected and
1555 -- indexed components down to the actual indexed component for
1556 -- the packed array.
1560 Set_Analyzed (Exp, False);
1563 (Exp, N_Selected_Component, N_Indexed_Component)
1565 Exp := Prefix (Exp);
1571 -- Now we can insert and analyze the pre-assignment
1573 -- If the right-hand side requires a transient scope, it has
1574 -- already been placed on the stack. However, the declaration is
1575 -- inserted in the tree outside of this scope, and must reflect
1576 -- the proper scope for its variable. This awkward bit is forced
1577 -- by the stricter scope discipline imposed by GCC 2.97.
1580 Uses_Transient_Scope : constant Boolean :=
1582 and then N = Node_To_Be_Wrapped;
1585 if Uses_Transient_Scope then
1586 Push_Scope (Scope (Current_Scope));
1589 Insert_Before_And_Analyze (N,
1590 Make_Object_Declaration (Loc,
1591 Defining_Identifier => Tnn,
1592 Object_Definition => New_Occurrence_Of (BPAR_Typ, Loc),
1593 Expression => BPAR_Expr));
1595 if Uses_Transient_Scope then
1600 -- Now fix up the original assignment and continue processing
1602 Rewrite (Prefix (Lhs),
1603 New_Occurrence_Of (Tnn, Loc));
1605 -- We do not need to reanalyze that assignment, and we do not need
1606 -- to worry about references to the temporary, but we do need to
1607 -- make sure that the temporary is not marked as a true constant
1608 -- since we now have a generated assignment to it!
1610 Set_Is_True_Constant (Tnn, False);
1614 -- When we have the appropriate type of aggregate in the expression (it
1615 -- has been determined during analysis of the aggregate by setting the
1616 -- delay flag), let's perform in place assignment and thus avoid
1617 -- creating a temporary.
1619 if Is_Delayed_Aggregate (Rhs) then
1620 Convert_Aggr_In_Assignment (N);
1621 Rewrite (N, Make_Null_Statement (Loc));
1626 -- Apply discriminant check if required. If Lhs is an access type to a
1627 -- designated type with discriminants, we must always check.
1629 if Has_Discriminants (Etype (Lhs)) then
1631 -- Skip discriminant check if change of representation. Will be
1632 -- done when the change of representation is expanded out.
1634 if not Change_Of_Representation (N) then
1635 Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
1638 -- If the type is private without discriminants, and the full type
1639 -- has discriminants (necessarily with defaults) a check may still be
1640 -- necessary if the Lhs is aliased. The private determinants must be
1641 -- visible to build the discriminant constraints.
1643 -- Only an explicit dereference that comes from source indicates
1644 -- aliasing. Access to formals of protected operations and entries
1645 -- create dereferences but are not semantic aliasings.
1647 elsif Is_Private_Type (Etype (Lhs))
1648 and then Has_Discriminants (Typ)
1649 and then Nkind (Lhs) = N_Explicit_Dereference
1650 and then Comes_From_Source (Lhs)
1653 Lt : constant Entity_Id := Etype (Lhs);
1655 Set_Etype (Lhs, Typ);
1656 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1657 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1658 Set_Etype (Lhs, Lt);
1661 -- If the Lhs has a private type with unknown discriminants, it
1662 -- may have a full view with discriminants, but those are nameable
1663 -- only in the underlying type, so convert the Rhs to it before
1664 -- potential checking.
1666 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs)))
1667 and then Has_Discriminants (Typ)
1669 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1670 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1672 -- In the access type case, we need the same discriminant check, and
1673 -- also range checks if we have an access to constrained array.
1675 elsif Is_Access_Type (Etype (Lhs))
1676 and then Is_Constrained (Designated_Type (Etype (Lhs)))
1678 if Has_Discriminants (Designated_Type (Etype (Lhs))) then
1680 -- Skip discriminant check if change of representation. Will be
1681 -- done when the change of representation is expanded out.
1683 if not Change_Of_Representation (N) then
1684 Apply_Discriminant_Check (Rhs, Etype (Lhs));
1687 elsif Is_Array_Type (Designated_Type (Etype (Lhs))) then
1688 Apply_Range_Check (Rhs, Etype (Lhs));
1690 if Is_Constrained (Etype (Lhs)) then
1691 Apply_Length_Check (Rhs, Etype (Lhs));
1694 if Nkind (Rhs) = N_Allocator then
1696 Target_Typ : constant Entity_Id := Etype (Expression (Rhs));
1697 C_Es : Check_Result;
1704 Etype (Designated_Type (Etype (Lhs))));
1716 -- Apply range check for access type case
1718 elsif Is_Access_Type (Etype (Lhs))
1719 and then Nkind (Rhs) = N_Allocator
1720 and then Nkind (Expression (Rhs)) = N_Qualified_Expression
1722 Analyze_And_Resolve (Expression (Rhs));
1724 (Expression (Rhs), Designated_Type (Etype (Lhs)));
1727 -- Ada 2005 (AI-231): Generate the run-time check
1729 if Is_Access_Type (Typ)
1730 and then Can_Never_Be_Null (Etype (Lhs))
1731 and then not Can_Never_Be_Null (Etype (Rhs))
1733 Apply_Constraint_Check (Rhs, Etype (Lhs));
1736 -- Case of assignment to a bit packed array element
1738 if Nkind (Lhs) = N_Indexed_Component
1739 and then Is_Bit_Packed_Array (Etype (Prefix (Lhs)))
1741 Expand_Bit_Packed_Element_Set (N);
1744 -- Build-in-place function call case. Note that we're not yet doing
1745 -- build-in-place for user-written assignment statements (the assignment
1746 -- here came from an aggregate.)
1748 elsif Ada_Version >= Ada_05
1749 and then Is_Build_In_Place_Function_Call (Rhs)
1751 Make_Build_In_Place_Call_In_Assignment (N, Rhs);
1753 elsif Is_Tagged_Type (Typ) and then Is_Value_Type (Etype (Lhs)) then
1755 -- Nothing to do for valuetypes
1756 -- ??? Set_Scope_Is_Transient (False);
1760 elsif Is_Tagged_Type (Typ)
1761 or else (Controlled_Type (Typ) and then not Is_Array_Type (Typ))
1763 Tagged_Case : declare
1764 L : List_Id := No_List;
1765 Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
1768 -- In the controlled case, we need to make sure that function
1769 -- calls are evaluated before finalizing the target. In all cases,
1770 -- it makes the expansion easier if the side-effects are removed
1773 Remove_Side_Effects (Lhs);
1774 Remove_Side_Effects (Rhs);
1776 -- Avoid recursion in the mechanism
1780 -- If dispatching assignment, we need to dispatch to _assign
1782 if Is_Class_Wide_Type (Typ)
1784 -- If the type is tagged, we may as well use the predefined
1785 -- primitive assignment. This avoids inlining a lot of code
1786 -- and in the class-wide case, the assignment is replaced by
1787 -- dispatch call to _assign. Note that this cannot be done when
1788 -- discriminant checks are locally suppressed (as in extension
1789 -- aggregate expansions) because otherwise the discriminant
1790 -- check will be performed within the _assign call. It is also
1791 -- suppressed for assignments created by the expander that
1792 -- correspond to initializations, where we do want to copy the
1793 -- tag (No_Ctrl_Actions flag set True) by the expander and we
1794 -- do not need to mess with tags ever (Expand_Ctrl_Actions flag
1795 -- is set True in this case).
1797 or else (Is_Tagged_Type (Typ)
1798 and then not Is_Value_Type (Etype (Lhs))
1799 and then Chars (Current_Scope) /= Name_uAssign
1800 and then Expand_Ctrl_Actions
1801 and then not Discriminant_Checks_Suppressed (Empty))
1803 -- Fetch the primitive op _assign and proper type to call it.
1804 -- Because of possible conflicts between private and full view
1805 -- the proper type is fetched directly from the operation
1809 Op : constant Entity_Id :=
1810 Find_Prim_Op (Typ, Name_uAssign);
1811 F_Typ : Entity_Id := Etype (First_Formal (Op));
1814 -- If the assignment is dispatching, make sure to use the
1817 if Is_Class_Wide_Type (Typ) then
1818 F_Typ := Class_Wide_Type (F_Typ);
1823 -- In case of assignment to a class-wide tagged type, before
1824 -- the assignment we generate run-time check to ensure that
1825 -- the tags of source and target match.
1827 if Is_Class_Wide_Type (Typ)
1828 and then Is_Tagged_Type (Typ)
1829 and then Is_Tagged_Type (Underlying_Type (Etype (Rhs)))
1832 Make_Raise_Constraint_Error (Loc,
1836 Make_Selected_Component (Loc,
1837 Prefix => Duplicate_Subexpr (Lhs),
1839 Make_Identifier (Loc,
1840 Chars => Name_uTag)),
1842 Make_Selected_Component (Loc,
1843 Prefix => Duplicate_Subexpr (Rhs),
1845 Make_Identifier (Loc,
1846 Chars => Name_uTag))),
1847 Reason => CE_Tag_Check_Failed));
1851 Make_Procedure_Call_Statement (Loc,
1852 Name => New_Reference_To (Op, Loc),
1853 Parameter_Associations => New_List (
1854 Unchecked_Convert_To (F_Typ,
1855 Duplicate_Subexpr (Lhs)),
1856 Unchecked_Convert_To (F_Typ,
1857 Duplicate_Subexpr (Rhs)))));
1861 L := Make_Tag_Ctrl_Assignment (N);
1863 -- We can't afford to have destructive Finalization Actions in
1864 -- the Self assignment case, so if the target and the source
1865 -- are not obviously different, code is generated to avoid the
1866 -- self assignment case:
1868 -- if lhs'address /= rhs'address then
1869 -- <code for controlled and/or tagged assignment>
1872 if not Statically_Different (Lhs, Rhs)
1873 and then Expand_Ctrl_Actions
1876 Make_Implicit_If_Statement (N,
1880 Make_Attribute_Reference (Loc,
1881 Prefix => Duplicate_Subexpr (Lhs),
1882 Attribute_Name => Name_Address),
1885 Make_Attribute_Reference (Loc,
1886 Prefix => Duplicate_Subexpr (Rhs),
1887 Attribute_Name => Name_Address)),
1889 Then_Statements => L));
1892 -- We need to set up an exception handler for implementing
1893 -- 7.6.1(18). The remaining adjustments are tackled by the
1894 -- implementation of adjust for record_controllers (see
1897 -- This is skipped if we have no finalization
1899 if Expand_Ctrl_Actions
1900 and then not Restriction_Active (No_Finalization)
1903 Make_Block_Statement (Loc,
1904 Handled_Statement_Sequence =>
1905 Make_Handled_Sequence_Of_Statements (Loc,
1907 Exception_Handlers => New_List (
1908 Make_Handler_For_Ctrl_Operation (Loc)))));
1913 Make_Block_Statement (Loc,
1914 Handled_Statement_Sequence =>
1915 Make_Handled_Sequence_Of_Statements (Loc, Statements => L)));
1917 -- If no restrictions on aborts, protect the whole assignment
1918 -- for controlled objects as per 9.8(11).
1920 if Controlled_Type (Typ)
1921 and then Expand_Ctrl_Actions
1922 and then Abort_Allowed
1925 Blk : constant Entity_Id :=
1927 (E_Block, Current_Scope, Sloc (N), 'B');
1930 Set_Scope (Blk, Current_Scope);
1931 Set_Etype (Blk, Standard_Void_Type);
1932 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
1934 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
1935 Set_At_End_Proc (Handled_Statement_Sequence (N),
1936 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
1937 Expand_At_End_Handler
1938 (Handled_Statement_Sequence (N), Blk);
1942 -- N has been rewritten to a block statement for which it is
1943 -- known by construction that no checks are necessary: analyze
1944 -- it with all checks suppressed.
1946 Analyze (N, Suppress => All_Checks);
1952 elsif Is_Array_Type (Typ) then
1954 Actual_Rhs : Node_Id := Rhs;
1957 while Nkind_In (Actual_Rhs, N_Type_Conversion,
1958 N_Qualified_Expression)
1960 Actual_Rhs := Expression (Actual_Rhs);
1963 Expand_Assign_Array (N, Actual_Rhs);
1969 elsif Is_Record_Type (Typ) then
1970 Expand_Assign_Record (N);
1973 -- Scalar types. This is where we perform the processing related to the
1974 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
1977 elsif Is_Scalar_Type (Typ) then
1979 -- Case where right side is known valid
1981 if Expr_Known_Valid (Rhs) then
1983 -- Here the right side is valid, so it is fine. The case to deal
1984 -- with is when the left side is a local variable reference whose
1985 -- value is not currently known to be valid. If this is the case,
1986 -- and the assignment appears in an unconditional context, then we
1987 -- can mark the left side as now being valid.
1989 if Is_Local_Variable_Reference (Lhs)
1990 and then not Is_Known_Valid (Entity (Lhs))
1991 and then In_Unconditional_Context (N)
1993 Set_Is_Known_Valid (Entity (Lhs), True);
1996 -- Case where right side may be invalid in the sense of the RM
1997 -- reference above. The RM does not require that we check for the
1998 -- validity on an assignment, but it does require that the assignment
1999 -- of an invalid value not cause erroneous behavior.
2001 -- The general approach in GNAT is to use the Is_Known_Valid flag
2002 -- to avoid the need for validity checking on assignments. However
2003 -- in some cases, we have to do validity checking in order to make
2004 -- sure that the setting of this flag is correct.
2007 -- Validate right side if we are validating copies
2009 if Validity_Checks_On
2010 and then Validity_Check_Copies
2012 -- Skip this if left hand side is an array or record component
2013 -- and elementary component validity checks are suppressed.
2015 if Nkind_In (Lhs, N_Selected_Component, N_Indexed_Component)
2016 and then not Validity_Check_Components
2023 -- We can propagate this to the left side where appropriate
2025 if Is_Local_Variable_Reference (Lhs)
2026 and then not Is_Known_Valid (Entity (Lhs))
2027 and then In_Unconditional_Context (N)
2029 Set_Is_Known_Valid (Entity (Lhs), True);
2032 -- Otherwise check to see what should be done
2034 -- If left side is a local variable, then we just set its flag to
2035 -- indicate that its value may no longer be valid, since we are
2036 -- copying a potentially invalid value.
2038 elsif Is_Local_Variable_Reference (Lhs) then
2039 Set_Is_Known_Valid (Entity (Lhs), False);
2041 -- Check for case of a nonlocal variable on the left side which
2042 -- is currently known to be valid. In this case, we simply ensure
2043 -- that the right side is valid. We only play the game of copying
2044 -- validity status for local variables, since we are doing this
2045 -- statically, not by tracing the full flow graph.
2047 elsif Is_Entity_Name (Lhs)
2048 and then Is_Known_Valid (Entity (Lhs))
2050 -- Note: If Validity_Checking mode is set to none, we ignore
2051 -- the Ensure_Valid call so don't worry about that case here.
2055 -- In all other cases, we can safely copy an invalid value without
2056 -- worrying about the status of the left side. Since it is not a
2057 -- variable reference it will not be considered
2058 -- as being known to be valid in any case.
2066 -- Defend against invalid subscripts on left side if we are in standard
2067 -- validity checking mode. No need to do this if we are checking all
2070 if Validity_Checks_On
2071 and then Validity_Check_Default
2072 and then not Validity_Check_Subscripts
2074 Check_Valid_Lvalue_Subscripts (Lhs);
2078 when RE_Not_Available =>
2080 end Expand_N_Assignment_Statement;
2082 ------------------------------
2083 -- Expand_N_Block_Statement --
2084 ------------------------------
2086 -- Encode entity names defined in block statement
2088 procedure Expand_N_Block_Statement (N : Node_Id) is
2090 Qualify_Entity_Names (N);
2091 end Expand_N_Block_Statement;
2093 -----------------------------
2094 -- Expand_N_Case_Statement --
2095 -----------------------------
2097 procedure Expand_N_Case_Statement (N : Node_Id) is
2098 Loc : constant Source_Ptr := Sloc (N);
2099 Expr : constant Node_Id := Expression (N);
2107 -- Check for the situation where we know at compile time which branch
2110 if Compile_Time_Known_Value (Expr) then
2111 Alt := Find_Static_Alternative (N);
2113 -- Move statements from this alternative after the case statement.
2114 -- They are already analyzed, so will be skipped by the analyzer.
2116 Insert_List_After (N, Statements (Alt));
2118 -- That leaves the case statement as a shell. So now we can kill all
2119 -- other alternatives in the case statement.
2121 Kill_Dead_Code (Expression (N));
2127 -- Loop through case alternatives, skipping pragmas, and skipping
2128 -- the one alternative that we select (and therefore retain).
2130 A := First (Alternatives (N));
2131 while Present (A) loop
2133 and then Nkind (A) = N_Case_Statement_Alternative
2135 Kill_Dead_Code (Statements (A), Warn_On_Deleted_Code);
2142 Rewrite (N, Make_Null_Statement (Loc));
2146 -- Here if the choice is not determined at compile time
2149 Last_Alt : constant Node_Id := Last (Alternatives (N));
2151 Others_Present : Boolean;
2152 Others_Node : Node_Id;
2154 Then_Stms : List_Id;
2155 Else_Stms : List_Id;
2158 if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
2159 Others_Present := True;
2160 Others_Node := Last_Alt;
2162 Others_Present := False;
2165 -- First step is to worry about possible invalid argument. The RM
2166 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2167 -- outside the base range), then Constraint_Error must be raised.
2169 -- Case of validity check required (validity checks are on, the
2170 -- expression is not known to be valid, and the case statement
2171 -- comes from source -- no need to validity check internally
2172 -- generated case statements).
2174 if Validity_Check_Default then
2175 Ensure_Valid (Expr);
2178 -- If there is only a single alternative, just replace it with the
2179 -- sequence of statements since obviously that is what is going to
2180 -- be executed in all cases.
2182 Len := List_Length (Alternatives (N));
2185 -- We still need to evaluate the expression if it has any
2188 Remove_Side_Effects (Expression (N));
2190 Insert_List_After (N, Statements (First (Alternatives (N))));
2192 -- That leaves the case statement as a shell. The alternative that
2193 -- will be executed is reset to a null list. So now we can kill
2194 -- the entire case statement.
2196 Kill_Dead_Code (Expression (N));
2197 Rewrite (N, Make_Null_Statement (Loc));
2201 -- An optimization. If there are only two alternatives, and only
2202 -- a single choice, then rewrite the whole case statement as an
2203 -- if statement, since this can result in subsequent optimizations.
2204 -- This helps not only with case statements in the source of a
2205 -- simple form, but also with generated code (discriminant check
2206 -- functions in particular)
2209 Chlist := Discrete_Choices (First (Alternatives (N)));
2211 if List_Length (Chlist) = 1 then
2212 Choice := First (Chlist);
2214 Then_Stms := Statements (First (Alternatives (N)));
2215 Else_Stms := Statements (Last (Alternatives (N)));
2217 -- For TRUE, generate "expression", not expression = true
2219 if Nkind (Choice) = N_Identifier
2220 and then Entity (Choice) = Standard_True
2222 Cond := Expression (N);
2224 -- For FALSE, generate "expression" and switch then/else
2226 elsif Nkind (Choice) = N_Identifier
2227 and then Entity (Choice) = Standard_False
2229 Cond := Expression (N);
2230 Else_Stms := Statements (First (Alternatives (N)));
2231 Then_Stms := Statements (Last (Alternatives (N)));
2233 -- For a range, generate "expression in range"
2235 elsif Nkind (Choice) = N_Range
2236 or else (Nkind (Choice) = N_Attribute_Reference
2237 and then Attribute_Name (Choice) = Name_Range)
2238 or else (Is_Entity_Name (Choice)
2239 and then Is_Type (Entity (Choice)))
2240 or else Nkind (Choice) = N_Subtype_Indication
2244 Left_Opnd => Expression (N),
2245 Right_Opnd => Relocate_Node (Choice));
2247 -- For any other subexpression "expression = value"
2252 Left_Opnd => Expression (N),
2253 Right_Opnd => Relocate_Node (Choice));
2256 -- Now rewrite the case as an IF
2259 Make_If_Statement (Loc,
2261 Then_Statements => Then_Stms,
2262 Else_Statements => Else_Stms));
2268 -- If the last alternative is not an Others choice, replace it with
2269 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2270 -- the modified case statement, since it's only effect would be to
2271 -- compute the contents of the Others_Discrete_Choices which is not
2272 -- needed by the back end anyway.
2274 -- The reason we do this is that the back end always needs some
2275 -- default for a switch, so if we have not supplied one in the
2276 -- processing above for validity checking, then we need to supply
2279 if not Others_Present then
2280 Others_Node := Make_Others_Choice (Sloc (Last_Alt));
2281 Set_Others_Discrete_Choices
2282 (Others_Node, Discrete_Choices (Last_Alt));
2283 Set_Discrete_Choices (Last_Alt, New_List (Others_Node));
2286 end Expand_N_Case_Statement;
2288 -----------------------------
2289 -- Expand_N_Exit_Statement --
2290 -----------------------------
2292 -- The only processing required is to deal with a possible C/Fortran
2293 -- boolean value used as the condition for the exit statement.
2295 procedure Expand_N_Exit_Statement (N : Node_Id) is
2297 Adjust_Condition (Condition (N));
2298 end Expand_N_Exit_Statement;
2300 ----------------------------------------
2301 -- Expand_N_Extended_Return_Statement --
2302 ----------------------------------------
2304 -- If there is a Handled_Statement_Sequence, we rewrite this:
2306 -- return Result : T := <expression> do
2307 -- <handled_seq_of_stms>
2313 -- Result : T := <expression>;
2315 -- <handled_seq_of_stms>
2319 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
2321 -- return Result : T := <expression>;
2325 -- return <expression>;
2327 -- unless it's build-in-place or there's no <expression>, in which case
2331 -- Result : T := <expression>;
2336 -- Note that this case could have been written by the user as an extended
2337 -- return statement, or could have been transformed to this from a simple
2338 -- return statement.
2340 -- That is, we need to have a reified return object if there are statements
2341 -- (which might refer to it) or if we're doing build-in-place (so we can
2342 -- set its address to the final resting place or if there is no expression
2343 -- (in which case default initial values might need to be set).
2345 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
2346 Loc : constant Source_Ptr := Sloc (N);
2348 Return_Object_Entity : constant Entity_Id :=
2349 First_Entity (Return_Statement_Entity (N));
2350 Return_Object_Decl : constant Node_Id :=
2351 Parent (Return_Object_Entity);
2352 Parent_Function : constant Entity_Id :=
2353 Return_Applies_To (Return_Statement_Entity (N));
2354 Is_Build_In_Place : constant Boolean :=
2355 Is_Build_In_Place_Function (Parent_Function);
2357 Return_Stm : Node_Id;
2358 Statements : List_Id;
2359 Handled_Stm_Seq : Node_Id;
2363 function Move_Activation_Chain return Node_Id;
2364 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
2366 -- From current activation chain
2367 -- To activation chain passed in by the caller
2368 -- New_Master master passed in by the caller
2370 function Move_Final_List return Node_Id;
2371 -- Construct call to System.Finalization_Implementation.Move_Final_List
2374 -- From finalization list of the return statement
2375 -- To finalization list passed in by the caller
2377 ---------------------------
2378 -- Move_Activation_Chain --
2379 ---------------------------
2381 function Move_Activation_Chain return Node_Id is
2382 Activation_Chain_Formal : constant Entity_Id :=
2383 Build_In_Place_Formal
2384 (Parent_Function, BIP_Activation_Chain);
2385 To : constant Node_Id :=
2387 (Activation_Chain_Formal, Loc);
2388 Master_Formal : constant Entity_Id :=
2389 Build_In_Place_Formal
2390 (Parent_Function, BIP_Master);
2391 New_Master : constant Node_Id :=
2392 New_Reference_To (Master_Formal, Loc);
2394 Chain_Entity : Entity_Id;
2398 Chain_Entity := First_Entity (Return_Statement_Entity (N));
2399 while Chars (Chain_Entity) /= Name_uChain loop
2400 Chain_Entity := Next_Entity (Chain_Entity);
2404 Make_Attribute_Reference (Loc,
2405 Prefix => New_Reference_To (Chain_Entity, Loc),
2406 Attribute_Name => Name_Unrestricted_Access);
2407 -- ??? Not clear why "Make_Identifier (Loc, Name_uChain)" doesn't
2408 -- work, instead of "New_Reference_To (Chain_Entity, Loc)" above.
2411 Make_Procedure_Call_Statement (Loc,
2412 Name => New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
2413 Parameter_Associations => New_List (From, To, New_Master));
2414 end Move_Activation_Chain;
2416 ---------------------
2417 -- Move_Final_List --
2418 ---------------------
2420 function Move_Final_List return Node_Id is
2421 Flist : constant Entity_Id :=
2422 Finalization_Chain_Entity (Return_Statement_Entity (N));
2424 From : constant Node_Id := New_Reference_To (Flist, Loc);
2426 Caller_Final_List : constant Entity_Id :=
2427 Build_In_Place_Formal
2428 (Parent_Function, BIP_Final_List);
2430 To : constant Node_Id := New_Reference_To (Caller_Final_List, Loc);
2433 -- Catch cases where a finalization chain entity has not been
2434 -- associated with the return statement entity.
2436 pragma Assert (Present (Flist));
2438 -- Build required call
2441 Make_If_Statement (Loc,
2444 Left_Opnd => New_Copy (From),
2445 Right_Opnd => New_Node (N_Null, Loc)),
2448 Make_Procedure_Call_Statement (Loc,
2449 Name => New_Reference_To (RTE (RE_Move_Final_List), Loc),
2450 Parameter_Associations => New_List (From, To))));
2451 end Move_Final_List;
2453 -- Start of processing for Expand_N_Extended_Return_Statement
2456 if Nkind (Return_Object_Decl) = N_Object_Declaration then
2457 Exp := Expression (Return_Object_Decl);
2462 Handled_Stm_Seq := Handled_Statement_Sequence (N);
2464 -- Build a simple_return_statement that returns the return object when
2465 -- there is a statement sequence, or no expression, or the result will
2466 -- be built in place. Note however that we currently do this for all
2467 -- composite cases, even though nonlimited composite results are not yet
2468 -- built in place (though we plan to do so eventually).
2470 if Present (Handled_Stm_Seq)
2471 or else Is_Composite_Type (Etype (Parent_Function))
2474 if No (Handled_Stm_Seq) then
2475 Statements := New_List;
2477 -- If the extended return has a handled statement sequence, then wrap
2478 -- it in a block and use the block as the first statement.
2482 New_List (Make_Block_Statement (Loc,
2483 Declarations => New_List,
2484 Handled_Statement_Sequence => Handled_Stm_Seq));
2487 -- If control gets past the above Statements, we have successfully
2488 -- completed the return statement. If the result type has controlled
2489 -- parts and the return is for a build-in-place function, then we
2490 -- call Move_Final_List to transfer responsibility for finalization
2491 -- of the return object to the caller. An alternative would be to
2492 -- declare a Success flag in the function, initialize it to False,
2493 -- and set it to True here. Then move the Move_Final_List call into
2494 -- the cleanup code, and check Success. If Success then make a call
2495 -- to Move_Final_List else do finalization. Then we can remove the
2496 -- abort-deferral and the nulling-out of the From parameter from
2497 -- Move_Final_List. Note that the current method is not quite correct
2498 -- in the rather obscure case of a select-then-abort statement whose
2499 -- abortable part contains the return statement.
2501 -- We test the type of the expression as well as the return type
2502 -- of the function, because the latter may be a class-wide type
2503 -- which is always treated as controlled, while the expression itself
2504 -- has to have a definite type. The expression may be absent if a
2505 -- constrained aggregate has been expanded into component assignments
2506 -- so we have to check for this as well.
2508 if Is_Build_In_Place
2509 and then Controlled_Type (Etype (Parent_Function))
2511 if not Is_Class_Wide_Type (Etype (Parent_Function))
2514 and then Controlled_Type (Etype (Exp)))
2516 Append_To (Statements, Move_Final_List);
2520 -- Similarly to the above Move_Final_List, if the result type
2521 -- contains tasks, we call Move_Activation_Chain. Later, the cleanup
2522 -- code will call Complete_Master, which will terminate any
2523 -- unactivated tasks belonging to the return statement master. But
2524 -- Move_Activation_Chain updates their master to be that of the
2525 -- caller, so they will not be terminated unless the return statement
2526 -- completes unsuccessfully due to exception, abort, goto, or exit.
2527 -- As a formality, we test whether the function requires the result
2528 -- to be built in place, though that's necessarily true for the case
2529 -- of result types with task parts.
2531 if Is_Build_In_Place and Has_Task (Etype (Parent_Function)) then
2532 Append_To (Statements, Move_Activation_Chain);
2535 -- Build a simple_return_statement that returns the return object
2538 Make_Simple_Return_Statement (Loc,
2539 Expression => New_Occurrence_Of (Return_Object_Entity, Loc));
2540 Append_To (Statements, Return_Stm);
2543 Make_Handled_Sequence_Of_Statements (Loc, Statements);
2546 -- Case where we build a block
2548 if Present (Handled_Stm_Seq) then
2550 Make_Block_Statement (Loc,
2551 Declarations => Return_Object_Declarations (N),
2552 Handled_Statement_Sequence => Handled_Stm_Seq);
2554 -- We set the entity of the new block statement to be that of the
2555 -- return statement. This is necessary so that various fields, such
2556 -- as Finalization_Chain_Entity carry over from the return statement
2557 -- to the block. Note that this block is unusual, in that its entity
2558 -- is an E_Return_Statement rather than an E_Block.
2561 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
2563 -- If the object decl was already rewritten as a renaming, then
2564 -- we don't want to do the object allocation and transformation of
2565 -- of the return object declaration to a renaming. This case occurs
2566 -- when the return object is initialized by a call to another
2567 -- build-in-place function, and that function is responsible for the
2568 -- allocation of the return object.
2570 if Is_Build_In_Place
2572 Nkind (Return_Object_Decl) = N_Object_Renaming_Declaration
2574 Set_By_Ref (Return_Stm); -- Return build-in-place results by ref
2576 elsif Is_Build_In_Place then
2578 -- Locate the implicit access parameter associated with the
2579 -- caller-supplied return object and convert the return
2580 -- statement's return object declaration to a renaming of a
2581 -- dereference of the access parameter. If the return object's
2582 -- declaration includes an expression that has not already been
2583 -- expanded as separate assignments, then add an assignment
2584 -- statement to ensure the return object gets initialized.
2587 -- Result : T [:= <expression>];
2594 -- Result : T renames FuncRA.all;
2595 -- [Result := <expression;]
2600 Return_Obj_Id : constant Entity_Id :=
2601 Defining_Identifier (Return_Object_Decl);
2602 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
2603 Return_Obj_Expr : constant Node_Id :=
2604 Expression (Return_Object_Decl);
2605 Result_Subt : constant Entity_Id :=
2606 Etype (Parent_Function);
2607 Constr_Result : constant Boolean :=
2608 Is_Constrained (Result_Subt);
2609 Obj_Alloc_Formal : Entity_Id;
2610 Object_Access : Entity_Id;
2611 Obj_Acc_Deref : Node_Id;
2612 Init_Assignment : Node_Id := Empty;
2615 -- Build-in-place results must be returned by reference
2617 Set_By_Ref (Return_Stm);
2619 -- Retrieve the implicit access parameter passed by the caller
2622 Build_In_Place_Formal (Parent_Function, BIP_Object_Access);
2624 -- If the return object's declaration includes an expression
2625 -- and the declaration isn't marked as No_Initialization, then
2626 -- we need to generate an assignment to the object and insert
2627 -- it after the declaration before rewriting it as a renaming
2628 -- (otherwise we'll lose the initialization).
2630 if Present (Return_Obj_Expr)
2631 and then not No_Initialization (Return_Object_Decl)
2634 Make_Assignment_Statement (Loc,
2635 Name => New_Reference_To (Return_Obj_Id, Loc),
2636 Expression => Relocate_Node (Return_Obj_Expr));
2637 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
2638 Set_Assignment_OK (Name (Init_Assignment));
2639 Set_No_Ctrl_Actions (Init_Assignment);
2641 Set_Parent (Name (Init_Assignment), Init_Assignment);
2642 Set_Parent (Expression (Init_Assignment), Init_Assignment);
2644 Set_Expression (Return_Object_Decl, Empty);
2646 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
2647 and then not Is_Class_Wide_Type
2648 (Etype (Expression (Init_Assignment)))
2650 Rewrite (Expression (Init_Assignment),
2651 Make_Type_Conversion (Loc,
2654 (Etype (Return_Obj_Id), Loc),
2656 Relocate_Node (Expression (Init_Assignment))));
2659 -- In the case of functions where the calling context can
2660 -- determine the form of allocation needed, initialization
2661 -- is done with each part of the if statement that handles
2662 -- the different forms of allocation (this is true for
2663 -- unconstrained and tagged result subtypes).
2666 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
2668 Insert_After (Return_Object_Decl, Init_Assignment);
2672 -- When the function's subtype is unconstrained, a run-time
2673 -- test is needed to determine the form of allocation to use
2674 -- for the return object. The function has an implicit formal
2675 -- parameter indicating this. If the BIP_Alloc_Form formal has
2676 -- the value one, then the caller has passed access to an
2677 -- existing object for use as the return object. If the value
2678 -- is two, then the return object must be allocated on the
2679 -- secondary stack. Otherwise, the object must be allocated in
2680 -- a storage pool (currently only supported for the global
2681 -- heap, user-defined storage pools TBD ???). We generate an
2682 -- if statement to test the implicit allocation formal and
2683 -- initialize a local access value appropriately, creating
2684 -- allocators in the secondary stack and global heap cases.
2685 -- The special formal also exists and must be tested when the
2686 -- function has a tagged result, even when the result subtype
2687 -- is constrained, because in general such functions can be
2688 -- called in dispatching contexts and must be handled similarly
2689 -- to functions with a class-wide result.
2691 if not Constr_Result
2692 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
2695 Build_In_Place_Formal (Parent_Function, BIP_Alloc_Form);
2698 Ref_Type : Entity_Id;
2699 Ptr_Type_Decl : Node_Id;
2700 Alloc_Obj_Id : Entity_Id;
2701 Alloc_Obj_Decl : Node_Id;
2702 Alloc_If_Stmt : Node_Id;
2703 SS_Allocator : Node_Id;
2704 Heap_Allocator : Node_Id;
2707 -- Reuse the itype created for the function's implicit
2708 -- access formal. This avoids the need to create a new
2709 -- access type here, plus it allows assigning the access
2710 -- formal directly without applying a conversion.
2712 -- Ref_Type := Etype (Object_Access);
2714 -- Create an access type designating the function's
2718 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2721 Make_Full_Type_Declaration (Loc,
2722 Defining_Identifier => Ref_Type,
2724 Make_Access_To_Object_Definition (Loc,
2725 All_Present => True,
2726 Subtype_Indication =>
2727 New_Reference_To (Return_Obj_Typ, Loc)));
2729 Insert_Before (Return_Object_Decl, Ptr_Type_Decl);
2731 -- Create an access object that will be initialized to an
2732 -- access value denoting the return object, either coming
2733 -- from an implicit access value passed in by the caller
2734 -- or from the result of an allocator.
2737 Make_Defining_Identifier (Loc,
2738 Chars => New_Internal_Name ('R'));
2739 Set_Etype (Alloc_Obj_Id, Ref_Type);
2742 Make_Object_Declaration (Loc,
2743 Defining_Identifier => Alloc_Obj_Id,
2744 Object_Definition => New_Reference_To
2747 Insert_Before (Return_Object_Decl, Alloc_Obj_Decl);
2749 -- Create allocators for both the secondary stack and
2750 -- global heap. If there's an initialization expression,
2751 -- then create these as initialized allocators.
2753 if Present (Return_Obj_Expr)
2754 and then not No_Initialization (Return_Object_Decl)
2757 Make_Allocator (Loc,
2759 Make_Qualified_Expression (Loc,
2761 New_Reference_To (Return_Obj_Typ, Loc),
2763 New_Copy_Tree (Return_Obj_Expr)));
2765 SS_Allocator := New_Copy_Tree (Heap_Allocator);
2768 -- If the function returns a class-wide type we cannot
2769 -- use the return type for the allocator. Instead we
2770 -- use the type of the expression, which must be an
2771 -- aggregate of a definite type.
2773 if Is_Class_Wide_Type (Return_Obj_Typ) then
2775 Make_Allocator (Loc,
2777 (Etype (Return_Obj_Expr), Loc));
2780 Make_Allocator (Loc,
2781 New_Reference_To (Return_Obj_Typ, Loc));
2784 -- If the object requires default initialization then
2785 -- that will happen later following the elaboration of
2786 -- the object renaming. If we don't turn it off here
2787 -- then the object will be default initialized twice.
2789 Set_No_Initialization (Heap_Allocator);
2791 SS_Allocator := New_Copy_Tree (Heap_Allocator);
2794 -- If the No_Allocators restriction is active, then only
2795 -- an allocator for secondary stack allocation is needed.
2797 if Restriction_Active (No_Allocators) then
2798 SS_Allocator := Heap_Allocator;
2799 Heap_Allocator := Make_Null (Loc);
2801 -- Otherwise the heap allocator may be needed, so we
2802 -- make another allocator for secondary stack allocation.
2805 SS_Allocator := New_Copy_Tree (Heap_Allocator);
2807 -- The heap allocator is marked Comes_From_Source
2808 -- since it corresponds to an explicit user-written
2809 -- allocator (that is, it will only be executed on
2810 -- behalf of callers that call the function as
2811 -- initialization for such an allocator). This
2812 -- prevents errors when No_Implicit_Heap_Allocation
2815 Set_Comes_From_Source (Heap_Allocator, True);
2818 -- The allocator is returned on the secondary stack. We
2819 -- don't do this on VM targets, since the SS is not used.
2821 if VM_Target = No_VM then
2822 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
2823 Set_Procedure_To_Call
2824 (SS_Allocator, RTE (RE_SS_Allocate));
2826 -- The allocator is returned on the secondary stack,
2827 -- so indicate that the function return, as well as
2828 -- the block that encloses the allocator, must not
2829 -- release it. The flags must be set now because the
2830 -- decision to use the secondary stack is done very
2831 -- late in the course of expanding the return
2832 -- statement, past the point where these flags are
2835 Set_Sec_Stack_Needed_For_Return (Parent_Function);
2836 Set_Sec_Stack_Needed_For_Return
2837 (Return_Statement_Entity (N));
2838 Set_Uses_Sec_Stack (Parent_Function);
2839 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
2842 -- Create an if statement to test the BIP_Alloc_Form
2843 -- formal and initialize the access object to either the
2844 -- BIP_Object_Access formal (BIP_Alloc_Form = 0), the
2845 -- result of allocating the object in the secondary stack
2846 -- (BIP_Alloc_Form = 1), or else an allocator to create
2847 -- the return object in the heap (BIP_Alloc_Form = 2).
2849 -- ??? An unchecked type conversion must be made in the
2850 -- case of assigning the access object formal to the
2851 -- local access object, because a normal conversion would
2852 -- be illegal in some cases (such as converting access-
2853 -- to-unconstrained to access-to-constrained), but the
2854 -- the unchecked conversion will presumably fail to work
2855 -- right in just such cases. It's not clear at all how to
2859 Make_If_Statement (Loc,
2863 New_Reference_To (Obj_Alloc_Formal, Loc),
2865 Make_Integer_Literal (Loc,
2866 UI_From_Int (BIP_Allocation_Form'Pos
2867 (Caller_Allocation)))),
2869 New_List (Make_Assignment_Statement (Loc,
2872 (Alloc_Obj_Id, Loc),
2874 Make_Unchecked_Type_Conversion (Loc,
2876 New_Reference_To (Ref_Type, Loc),
2879 (Object_Access, Loc)))),
2881 New_List (Make_Elsif_Part (Loc,
2886 (Obj_Alloc_Formal, Loc),
2888 Make_Integer_Literal (Loc,
2890 BIP_Allocation_Form'Pos
2891 (Secondary_Stack)))),
2894 (Make_Assignment_Statement (Loc,
2897 (Alloc_Obj_Id, Loc),
2901 New_List (Make_Assignment_Statement (Loc,
2904 (Alloc_Obj_Id, Loc),
2908 -- If a separate initialization assignment was created
2909 -- earlier, append that following the assignment of the
2910 -- implicit access formal to the access object, to ensure
2911 -- that the return object is initialized in that case.
2912 -- In this situation, the target of the assignment must
2913 -- be rewritten to denote a dereference of the access to
2914 -- the return object passed in by the caller.
2916 if Present (Init_Assignment) then
2917 Rewrite (Name (Init_Assignment),
2918 Make_Explicit_Dereference (Loc,
2919 Prefix => New_Reference_To (Alloc_Obj_Id, Loc)));
2921 (Name (Init_Assignment), Etype (Return_Obj_Id));
2924 (Then_Statements (Alloc_If_Stmt),
2928 Insert_Before (Return_Object_Decl, Alloc_If_Stmt);
2930 -- Remember the local access object for use in the
2931 -- dereference of the renaming created below.
2933 Object_Access := Alloc_Obj_Id;
2937 -- Replace the return object declaration with a renaming of a
2938 -- dereference of the access value designating the return
2942 Make_Explicit_Dereference (Loc,
2943 Prefix => New_Reference_To (Object_Access, Loc));
2945 Rewrite (Return_Object_Decl,
2946 Make_Object_Renaming_Declaration (Loc,
2947 Defining_Identifier => Return_Obj_Id,
2948 Access_Definition => Empty,
2949 Subtype_Mark => New_Occurrence_Of
2950 (Return_Obj_Typ, Loc),
2951 Name => Obj_Acc_Deref));
2953 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
2957 -- Case where we do not build a block
2960 -- We're about to drop Return_Object_Declarations on the floor, so
2961 -- we need to insert it, in case it got expanded into useful code.
2963 Insert_List_Before (N, Return_Object_Declarations (N));
2965 -- Build simple_return_statement that returns the expression directly
2967 Return_Stm := Make_Simple_Return_Statement (Loc, Expression => Exp);
2969 Result := Return_Stm;
2972 -- Set the flag to prevent infinite recursion
2974 Set_Comes_From_Extended_Return_Statement (Return_Stm);
2976 Rewrite (N, Result);
2978 end Expand_N_Extended_Return_Statement;
2980 -----------------------------
2981 -- Expand_N_Goto_Statement --
2982 -----------------------------
2984 -- Add poll before goto if polling active
2986 procedure Expand_N_Goto_Statement (N : Node_Id) is
2988 Generate_Poll_Call (N);
2989 end Expand_N_Goto_Statement;
2991 ---------------------------
2992 -- Expand_N_If_Statement --
2993 ---------------------------
2995 -- First we deal with the case of C and Fortran convention boolean values,
2996 -- with zero/non-zero semantics.
2998 -- Second, we deal with the obvious rewriting for the cases where the
2999 -- condition of the IF is known at compile time to be True or False.
3001 -- Third, we remove elsif parts which have non-empty Condition_Actions
3002 -- and rewrite as independent if statements. For example:
3013 -- <<condition actions of y>>
3019 -- This rewriting is needed if at least one elsif part has a non-empty
3020 -- Condition_Actions list. We also do the same processing if there is a
3021 -- constant condition in an elsif part (in conjunction with the first
3022 -- processing step mentioned above, for the recursive call made to deal
3023 -- with the created inner if, this deals with properly optimizing the
3024 -- cases of constant elsif conditions).
3026 procedure Expand_N_If_Statement (N : Node_Id) is
3027 Loc : constant Source_Ptr := Sloc (N);
3032 Warn_If_Deleted : constant Boolean :=
3033 Warn_On_Deleted_Code and then Comes_From_Source (N);
3034 -- Indicates whether we want warnings when we delete branches of the
3035 -- if statement based on constant condition analysis. We never want
3036 -- these warnings for expander generated code.
3039 Adjust_Condition (Condition (N));
3041 -- The following loop deals with constant conditions for the IF. We
3042 -- need a loop because as we eliminate False conditions, we grab the
3043 -- first elsif condition and use it as the primary condition.
3045 while Compile_Time_Known_Value (Condition (N)) loop
3047 -- If condition is True, we can simply rewrite the if statement now
3048 -- by replacing it by the series of then statements.
3050 if Is_True (Expr_Value (Condition (N))) then
3052 -- All the else parts can be killed
3054 Kill_Dead_Code (Elsif_Parts (N), Warn_If_Deleted);
3055 Kill_Dead_Code (Else_Statements (N), Warn_If_Deleted);
3057 Hed := Remove_Head (Then_Statements (N));
3058 Insert_List_After (N, Then_Statements (N));
3062 -- If condition is False, then we can delete the condition and
3063 -- the Then statements
3066 -- We do not delete the condition if constant condition warnings
3067 -- are enabled, since otherwise we end up deleting the desired
3068 -- warning. Of course the backend will get rid of this True/False
3069 -- test anyway, so nothing is lost here.
3071 if not Constant_Condition_Warnings then
3072 Kill_Dead_Code (Condition (N));
3075 Kill_Dead_Code (Then_Statements (N), Warn_If_Deleted);
3077 -- If there are no elsif statements, then we simply replace the
3078 -- entire if statement by the sequence of else statements.
3080 if No (Elsif_Parts (N)) then
3081 if No (Else_Statements (N))
3082 or else Is_Empty_List (Else_Statements (N))
3085 Make_Null_Statement (Sloc (N)));
3087 Hed := Remove_Head (Else_Statements (N));
3088 Insert_List_After (N, Else_Statements (N));
3094 -- If there are elsif statements, the first of them becomes the
3095 -- if/then section of the rebuilt if statement This is the case
3096 -- where we loop to reprocess this copied condition.
3099 Hed := Remove_Head (Elsif_Parts (N));
3100 Insert_Actions (N, Condition_Actions (Hed));
3101 Set_Condition (N, Condition (Hed));
3102 Set_Then_Statements (N, Then_Statements (Hed));
3104 -- Hed might have been captured as the condition determining
3105 -- the current value for an entity. Now it is detached from
3106 -- the tree, so a Current_Value pointer in the condition might
3107 -- need to be updated.
3109 Set_Current_Value_Condition (N);
3111 if Is_Empty_List (Elsif_Parts (N)) then
3112 Set_Elsif_Parts (N, No_List);
3118 -- Loop through elsif parts, dealing with constant conditions and
3119 -- possible expression actions that are present.
3121 if Present (Elsif_Parts (N)) then
3122 E := First (Elsif_Parts (N));
3123 while Present (E) loop
3124 Adjust_Condition (Condition (E));
3126 -- If there are condition actions, then rewrite the if statement
3127 -- as indicated above. We also do the same rewrite for a True or
3128 -- False condition. The further processing of this constant
3129 -- condition is then done by the recursive call to expand the
3130 -- newly created if statement
3132 if Present (Condition_Actions (E))
3133 or else Compile_Time_Known_Value (Condition (E))
3135 -- Note this is not an implicit if statement, since it is part
3136 -- of an explicit if statement in the source (or of an implicit
3137 -- if statement that has already been tested).
3140 Make_If_Statement (Sloc (E),
3141 Condition => Condition (E),
3142 Then_Statements => Then_Statements (E),
3143 Elsif_Parts => No_List,
3144 Else_Statements => Else_Statements (N));
3146 -- Elsif parts for new if come from remaining elsif's of parent
3148 while Present (Next (E)) loop
3149 if No (Elsif_Parts (New_If)) then
3150 Set_Elsif_Parts (New_If, New_List);
3153 Append (Remove_Next (E), Elsif_Parts (New_If));
3156 Set_Else_Statements (N, New_List (New_If));
3158 if Present (Condition_Actions (E)) then
3159 Insert_List_Before (New_If, Condition_Actions (E));
3164 if Is_Empty_List (Elsif_Parts (N)) then
3165 Set_Elsif_Parts (N, No_List);
3171 -- No special processing for that elsif part, move to next
3179 -- Some more optimizations applicable if we still have an IF statement
3181 if Nkind (N) /= N_If_Statement then
3185 -- Another optimization, special cases that can be simplified
3187 -- if expression then
3193 -- can be changed to:
3195 -- return expression;
3199 -- if expression then
3205 -- can be changed to:
3207 -- return not (expression);
3209 -- Only do these optimizations if we are at least at -O1 level
3211 if Optimization_Level > 0 then
3212 if Nkind (N) = N_If_Statement
3213 and then No (Elsif_Parts (N))
3214 and then Present (Else_Statements (N))
3215 and then List_Length (Then_Statements (N)) = 1
3216 and then List_Length (Else_Statements (N)) = 1
3219 Then_Stm : constant Node_Id := First (Then_Statements (N));
3220 Else_Stm : constant Node_Id := First (Else_Statements (N));
3223 if Nkind (Then_Stm) = N_Simple_Return_Statement
3225 Nkind (Else_Stm) = N_Simple_Return_Statement
3228 Then_Expr : constant Node_Id := Expression (Then_Stm);
3229 Else_Expr : constant Node_Id := Expression (Else_Stm);
3232 if Nkind (Then_Expr) = N_Identifier
3234 Nkind (Else_Expr) = N_Identifier
3236 if Entity (Then_Expr) = Standard_True
3237 and then Entity (Else_Expr) = Standard_False
3240 Make_Simple_Return_Statement (Loc,
3241 Expression => Relocate_Node (Condition (N))));
3245 elsif Entity (Then_Expr) = Standard_False
3246 and then Entity (Else_Expr) = Standard_True
3249 Make_Simple_Return_Statement (Loc,
3253 Relocate_Node (Condition (N)))));
3263 end Expand_N_If_Statement;
3265 -----------------------------
3266 -- Expand_N_Loop_Statement --
3267 -----------------------------
3269 -- 1. Deal with while condition for C/Fortran boolean
3270 -- 2. Deal with loops with a non-standard enumeration type range
3271 -- 3. Deal with while loops where Condition_Actions is set
3272 -- 4. Insert polling call if required
3274 procedure Expand_N_Loop_Statement (N : Node_Id) is
3275 Loc : constant Source_Ptr := Sloc (N);
3276 Isc : constant Node_Id := Iteration_Scheme (N);
3279 if Present (Isc) then
3280 Adjust_Condition (Condition (Isc));
3283 if Is_Non_Empty_List (Statements (N)) then
3284 Generate_Poll_Call (First (Statements (N)));
3287 -- Nothing more to do for plain loop with no iteration scheme
3293 -- Note: we do not have to worry about validity checking of the for loop
3294 -- range bounds here, since they were frozen with constant declarations
3295 -- and it is during that process that the validity checking is done.
3297 -- Handle the case where we have a for loop with the range type being an
3298 -- enumeration type with non-standard representation. In this case we
3301 -- for x in [reverse] a .. b loop
3307 -- for xP in [reverse] integer
3308 -- range etype'Pos (a) .. etype'Pos (b) loop
3310 -- x : constant etype := Pos_To_Rep (xP);
3316 if Present (Loop_Parameter_Specification (Isc)) then
3318 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
3319 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
3320 Ltype : constant Entity_Id := Etype (Loop_Id);
3321 Btype : constant Entity_Id := Base_Type (Ltype);
3326 if not Is_Enumeration_Type (Btype)
3327 or else No (Enum_Pos_To_Rep (Btype))
3333 Make_Defining_Identifier (Loc,
3334 Chars => New_External_Name (Chars (Loop_Id), 'P'));
3336 -- If the type has a contiguous representation, successive values
3337 -- can be generated as offsets from the first literal.
3339 if Has_Contiguous_Rep (Btype) then
3341 Unchecked_Convert_To (Btype,
3344 Make_Integer_Literal (Loc,
3345 Enumeration_Rep (First_Literal (Btype))),
3346 Right_Opnd => New_Reference_To (New_Id, Loc)));
3348 -- Use the constructed array Enum_Pos_To_Rep
3351 Make_Indexed_Component (Loc,
3352 Prefix => New_Reference_To (Enum_Pos_To_Rep (Btype), Loc),
3353 Expressions => New_List (New_Reference_To (New_Id, Loc)));
3357 Make_Loop_Statement (Loc,
3358 Identifier => Identifier (N),
3361 Make_Iteration_Scheme (Loc,
3362 Loop_Parameter_Specification =>
3363 Make_Loop_Parameter_Specification (Loc,
3364 Defining_Identifier => New_Id,
3365 Reverse_Present => Reverse_Present (LPS),
3367 Discrete_Subtype_Definition =>
3368 Make_Subtype_Indication (Loc,
3371 New_Reference_To (Standard_Natural, Loc),
3374 Make_Range_Constraint (Loc,
3379 Make_Attribute_Reference (Loc,
3381 New_Reference_To (Btype, Loc),
3383 Attribute_Name => Name_Pos,
3385 Expressions => New_List (
3387 (Type_Low_Bound (Ltype)))),
3390 Make_Attribute_Reference (Loc,
3392 New_Reference_To (Btype, Loc),
3394 Attribute_Name => Name_Pos,
3396 Expressions => New_List (
3398 (Type_High_Bound (Ltype))))))))),
3400 Statements => New_List (
3401 Make_Block_Statement (Loc,
3402 Declarations => New_List (
3403 Make_Object_Declaration (Loc,
3404 Defining_Identifier => Loop_Id,
3405 Constant_Present => True,
3406 Object_Definition => New_Reference_To (Ltype, Loc),
3407 Expression => Expr)),
3409 Handled_Statement_Sequence =>
3410 Make_Handled_Sequence_Of_Statements (Loc,
3411 Statements => Statements (N)))),
3413 End_Label => End_Label (N)));
3417 -- Second case, if we have a while loop with Condition_Actions set, then
3418 -- we change it into a plain loop:
3427 -- <<condition actions>>
3433 and then Present (Condition_Actions (Isc))
3440 Make_Exit_Statement (Sloc (Condition (Isc)),
3442 Make_Op_Not (Sloc (Condition (Isc)),
3443 Right_Opnd => Condition (Isc)));
3445 Prepend (ES, Statements (N));
3446 Insert_List_Before (ES, Condition_Actions (Isc));
3448 -- This is not an implicit loop, since it is generated in response
3449 -- to the loop statement being processed. If this is itself
3450 -- implicit, the restriction has already been checked. If not,
3451 -- it is an explicit loop.
3454 Make_Loop_Statement (Sloc (N),
3455 Identifier => Identifier (N),
3456 Statements => Statements (N),
3457 End_Label => End_Label (N)));
3462 end Expand_N_Loop_Statement;
3464 --------------------------------------
3465 -- Expand_N_Simple_Return_Statement --
3466 --------------------------------------
3468 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
3470 -- Defend agains previous errors (ie. the return statement calls a
3471 -- function that is not available in configurable runtime).
3473 if Present (Expression (N))
3474 and then Nkind (Expression (N)) = N_Empty
3479 -- Distinguish the function and non-function cases:
3481 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
3484 E_Generic_Function =>
3485 Expand_Simple_Function_Return (N);
3488 E_Generic_Procedure |
3491 E_Return_Statement =>
3492 Expand_Non_Function_Return (N);
3495 raise Program_Error;
3499 when RE_Not_Available =>
3501 end Expand_N_Simple_Return_Statement;
3503 --------------------------------
3504 -- Expand_Non_Function_Return --
3505 --------------------------------
3507 procedure Expand_Non_Function_Return (N : Node_Id) is
3508 pragma Assert (No (Expression (N)));
3510 Loc : constant Source_Ptr := Sloc (N);
3511 Scope_Id : Entity_Id :=
3512 Return_Applies_To (Return_Statement_Entity (N));
3513 Kind : constant Entity_Kind := Ekind (Scope_Id);
3516 Goto_Stat : Node_Id;
3520 -- Call postconditions procedure if procedure with active postconditions
3522 if Ekind (Scope_Id) = E_Procedure
3523 and then Has_Postconditions (Scope_Id)
3526 Make_Procedure_Call_Statement (Loc,
3527 Name => Make_Identifier (Loc, Name_uPostconditions)));
3530 -- If it is a return from a procedure do no extra steps
3532 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
3535 -- If it is a nested return within an extended one, replace it with a
3536 -- return of the previously declared return object.
3538 elsif Kind = E_Return_Statement then
3540 Make_Simple_Return_Statement (Loc,
3542 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
3543 Set_Comes_From_Extended_Return_Statement (N);
3544 Set_Return_Statement_Entity (N, Scope_Id);
3545 Expand_Simple_Function_Return (N);
3549 pragma Assert (Is_Entry (Scope_Id));
3551 -- Look at the enclosing block to see whether the return is from an
3552 -- accept statement or an entry body.
3554 for J in reverse 0 .. Scope_Stack.Last loop
3555 Scope_Id := Scope_Stack.Table (J).Entity;
3556 exit when Is_Concurrent_Type (Scope_Id);
3559 -- If it is a return from accept statement it is expanded as call to
3560 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
3562 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
3563 -- Expand_N_Accept_Alternative in exp_ch9.adb)
3565 if Is_Task_Type (Scope_Id) then
3568 Make_Procedure_Call_Statement (Loc,
3569 Name => New_Reference_To
3570 (RTE (RE_Complete_Rendezvous), Loc));
3571 Insert_Before (N, Call);
3572 -- why not insert actions here???
3575 Acc_Stat := Parent (N);
3576 while Nkind (Acc_Stat) /= N_Accept_Statement loop
3577 Acc_Stat := Parent (Acc_Stat);
3580 Lab_Node := Last (Statements
3581 (Handled_Statement_Sequence (Acc_Stat)));
3583 Goto_Stat := Make_Goto_Statement (Loc,
3584 Name => New_Occurrence_Of
3585 (Entity (Identifier (Lab_Node)), Loc));
3587 Set_Analyzed (Goto_Stat);
3589 Rewrite (N, Goto_Stat);
3592 -- If it is a return from an entry body, put a Complete_Entry_Body call
3593 -- in front of the return.
3595 elsif Is_Protected_Type (Scope_Id) then
3597 Make_Procedure_Call_Statement (Loc,
3599 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
3600 Parameter_Associations => New_List (
3601 Make_Attribute_Reference (Loc,
3604 (Find_Protection_Object (Current_Scope), Loc),
3606 Name_Unchecked_Access)));
3608 Insert_Before (N, Call);
3611 end Expand_Non_Function_Return;
3613 -----------------------------------
3614 -- Expand_Simple_Function_Return --
3615 -----------------------------------
3617 -- The "simple" comes from the syntax rule simple_return_statement.
3618 -- The semantics are not at all simple!
3620 procedure Expand_Simple_Function_Return (N : Node_Id) is
3621 Loc : constant Source_Ptr := Sloc (N);
3623 Scope_Id : constant Entity_Id :=
3624 Return_Applies_To (Return_Statement_Entity (N));
3625 -- The function we are returning from
3627 R_Type : constant Entity_Id := Etype (Scope_Id);
3628 -- The result type of the function
3630 Utyp : constant Entity_Id := Underlying_Type (R_Type);
3632 Exp : constant Node_Id := Expression (N);
3633 pragma Assert (Present (Exp));
3635 Exptyp : constant Entity_Id := Etype (Exp);
3636 -- The type of the expression (not necessarily the same as R_Type)
3639 -- For the case of a simple return that does not come from an extended
3640 -- return, in the case of Ada 2005 where we are returning a limited
3641 -- type, we rewrite "return <expression>;" to be:
3643 -- return _anon_ : <return_subtype> := <expression>
3645 -- The expansion produced by Expand_N_Extended_Return_Statement will
3646 -- contain simple return statements (for example, a block containing
3647 -- simple return of the return object), which brings us back here with
3648 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
3649 -- checking for a simple return that does not come from an extended
3650 -- return is to avoid this infinite recursion.
3652 -- The reason for this design is that for Ada 2005 limited returns, we
3653 -- need to reify the return object, so we can build it "in place", and
3654 -- we need a block statement to hang finalization and tasking stuff.
3656 -- ??? In order to avoid disruption, we avoid translating to extended
3657 -- return except in the cases where we really need to (Ada 2005 for
3658 -- inherently limited). We might prefer to do this translation in all
3659 -- cases (except perhaps for the case of Ada 95 inherently limited),
3660 -- in order to fully exercise the Expand_N_Extended_Return_Statement
3661 -- code. This would also allow us to to the build-in-place optimization
3662 -- for efficiency even in cases where it is semantically not required.
3664 -- As before, we check the type of the return expression rather than the
3665 -- return type of the function, because the latter may be a limited
3666 -- class-wide interface type, which is not a limited type, even though
3667 -- the type of the expression may be.
3669 if not Comes_From_Extended_Return_Statement (N)
3670 and then Is_Inherently_Limited_Type (Etype (Expression (N)))
3671 and then Ada_Version >= Ada_05
3672 and then not Debug_Flag_Dot_L
3675 Return_Object_Entity : constant Entity_Id :=
3676 Make_Defining_Identifier (Loc,
3677 New_Internal_Name ('R'));
3679 Subtype_Ind : constant Node_Id := New_Occurrence_Of (R_Type, Loc);
3681 Obj_Decl : constant Node_Id :=
3682 Make_Object_Declaration (Loc,
3683 Defining_Identifier => Return_Object_Entity,
3684 Object_Definition => Subtype_Ind,
3687 Ext : constant Node_Id := Make_Extended_Return_Statement (Loc,
3688 Return_Object_Declarations => New_List (Obj_Decl));
3697 -- Here we have a simple return statement that is part of the expansion
3698 -- of an extended return statement (either written by the user, or
3699 -- generated by the above code).
3701 -- Always normalize C/Fortran boolean result. This is not always needed,
3702 -- but it seems a good idea to minimize the passing around of non-
3703 -- normalized values, and in any case this handles the processing of
3704 -- barrier functions for protected types, which turn the condition into
3705 -- a return statement.
3707 if Is_Boolean_Type (Exptyp)
3708 and then Nonzero_Is_True (Exptyp)
3710 Adjust_Condition (Exp);
3711 Adjust_Result_Type (Exp, Exptyp);
3714 -- Do validity check if enabled for returns
3716 if Validity_Checks_On
3717 and then Validity_Check_Returns
3722 -- Check the result expression of a scalar function against the subtype
3723 -- of the function by inserting a conversion. This conversion must
3724 -- eventually be performed for other classes of types, but for now it's
3725 -- only done for scalars.
3728 if Is_Scalar_Type (Exptyp) then
3729 Rewrite (Exp, Convert_To (R_Type, Exp));
3733 -- Deal with returning variable length objects and controlled types
3735 -- Nothing to do if we are returning by reference, or this is not a
3736 -- type that requires special processing (indicated by the fact that
3737 -- it requires a cleanup scope for the secondary stack case).
3739 if Is_Inherently_Limited_Type (Exptyp)
3740 or else Is_Limited_Interface (Exptyp)
3744 elsif not Requires_Transient_Scope (R_Type) then
3746 -- Mutable records with no variable length components are not
3747 -- returned on the sec-stack, so we need to make sure that the
3748 -- backend will only copy back the size of the actual value, and not
3749 -- the maximum size. We create an actual subtype for this purpose.
3752 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
3756 if Has_Discriminants (Ubt)
3757 and then not Is_Constrained (Ubt)
3758 and then not Has_Unchecked_Union (Ubt)
3760 Decl := Build_Actual_Subtype (Ubt, Exp);
3761 Ent := Defining_Identifier (Decl);
3762 Insert_Action (Exp, Decl);
3763 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
3764 Analyze_And_Resolve (Exp);
3768 -- Here if secondary stack is used
3771 -- Make sure that no surrounding block will reclaim the secondary
3772 -- stack on which we are going to put the result. Not only may this
3773 -- introduce secondary stack leaks but worse, if the reclamation is
3774 -- done too early, then the result we are returning may get
3781 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
3782 Set_Sec_Stack_Needed_For_Return (S, True);
3783 S := Enclosing_Dynamic_Scope (S);
3787 -- Optimize the case where the result is a function call. In this
3788 -- case either the result is already on the secondary stack, or is
3789 -- already being returned with the stack pointer depressed and no
3790 -- further processing is required except to set the By_Ref flag to
3791 -- ensure that gigi does not attempt an extra unnecessary copy.
3792 -- (actually not just unnecessary but harmfully wrong in the case
3793 -- of a controlled type, where gigi does not know how to do a copy).
3794 -- To make up for a gcc 2.8.1 deficiency (???), we perform
3795 -- the copy for array types if the constrained status of the
3796 -- target type is different from that of the expression.
3798 if Requires_Transient_Scope (Exptyp)
3800 (not Is_Array_Type (Exptyp)
3801 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
3802 or else CW_Or_Controlled_Type (Utyp))
3803 and then Nkind (Exp) = N_Function_Call
3807 -- Remove side effects from the expression now so that other parts
3808 -- of the expander do not have to reanalyze this node without this
3811 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
3813 -- For controlled types, do the allocation on the secondary stack
3814 -- manually in order to call adjust at the right time:
3816 -- type Anon1 is access R_Type;
3817 -- for Anon1'Storage_pool use ss_pool;
3818 -- Anon2 : anon1 := new R_Type'(expr);
3819 -- return Anon2.all;
3821 -- We do the same for classwide types that are not potentially
3822 -- controlled (by the virtue of restriction No_Finalization) because
3823 -- gigi is not able to properly allocate class-wide types.
3825 elsif CW_Or_Controlled_Type (Utyp) then
3827 Loc : constant Source_Ptr := Sloc (N);
3828 Temp : constant Entity_Id :=
3829 Make_Defining_Identifier (Loc,
3830 Chars => New_Internal_Name ('R'));
3831 Acc_Typ : constant Entity_Id :=
3832 Make_Defining_Identifier (Loc,
3833 Chars => New_Internal_Name ('A'));
3834 Alloc_Node : Node_Id;
3837 Set_Ekind (Acc_Typ, E_Access_Type);
3839 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
3842 Make_Allocator (Loc,
3844 Make_Qualified_Expression (Loc,
3845 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
3846 Expression => Relocate_Node (Exp)));
3848 Insert_List_Before_And_Analyze (N, New_List (
3849 Make_Full_Type_Declaration (Loc,
3850 Defining_Identifier => Acc_Typ,
3852 Make_Access_To_Object_Definition (Loc,
3853 Subtype_Indication =>
3854 New_Reference_To (R_Type, Loc))),
3856 Make_Object_Declaration (Loc,
3857 Defining_Identifier => Temp,
3858 Object_Definition => New_Reference_To (Acc_Typ, Loc),
3859 Expression => Alloc_Node)));
3862 Make_Explicit_Dereference (Loc,
3863 Prefix => New_Reference_To (Temp, Loc)));
3865 Analyze_And_Resolve (Exp, R_Type);
3868 -- Otherwise use the gigi mechanism to allocate result on the
3872 Set_Storage_Pool (N, RTE (RE_SS_Pool));
3874 -- If we are generating code for the VM do not use
3875 -- SS_Allocate since everything is heap-allocated anyway.
3877 if VM_Target = No_VM then
3878 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3883 -- Implement the rules of 6.5(8-10), which require a tag check in the
3884 -- case of a limited tagged return type, and tag reassignment for
3885 -- nonlimited tagged results. These actions are needed when the return
3886 -- type is a specific tagged type and the result expression is a
3887 -- conversion or a formal parameter, because in that case the tag of the
3888 -- expression might differ from the tag of the specific result type.
3890 if Is_Tagged_Type (Utyp)
3891 and then not Is_Class_Wide_Type (Utyp)
3892 and then (Nkind_In (Exp, N_Type_Conversion,
3893 N_Unchecked_Type_Conversion)
3894 or else (Is_Entity_Name (Exp)
3895 and then Ekind (Entity (Exp)) in Formal_Kind))
3897 -- When the return type is limited, perform a check that the
3898 -- tag of the result is the same as the tag of the return type.
3900 if Is_Limited_Type (R_Type) then
3902 Make_Raise_Constraint_Error (Loc,
3906 Make_Selected_Component (Loc,
3907 Prefix => Duplicate_Subexpr (Exp),
3909 New_Reference_To (First_Tag_Component (Utyp), Loc)),
3911 Unchecked_Convert_To (RTE (RE_Tag),
3914 (Access_Disp_Table (Base_Type (Utyp)))),
3916 Reason => CE_Tag_Check_Failed));
3918 -- If the result type is a specific nonlimited tagged type, then we
3919 -- have to ensure that the tag of the result is that of the result
3920 -- type. This is handled by making a copy of the expression in the
3921 -- case where it might have a different tag, namely when the
3922 -- expression is a conversion or a formal parameter. We create a new
3923 -- object of the result type and initialize it from the expression,
3924 -- which will implicitly force the tag to be set appropriately.
3928 Result_Id : constant Entity_Id :=
3929 Make_Defining_Identifier (Loc,
3930 Chars => New_Internal_Name ('R'));
3931 Result_Exp : constant Node_Id :=
3932 New_Reference_To (Result_Id, Loc);
3933 Result_Obj : constant Node_Id :=
3934 Make_Object_Declaration (Loc,
3935 Defining_Identifier => Result_Id,
3936 Object_Definition =>
3937 New_Reference_To (R_Type, Loc),
3938 Constant_Present => True,
3939 Expression => Relocate_Node (Exp));
3942 Set_Assignment_OK (Result_Obj);
3943 Insert_Action (Exp, Result_Obj);
3945 Rewrite (Exp, Result_Exp);
3946 Analyze_And_Resolve (Exp, R_Type);
3950 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
3951 -- a check that the level of the return expression's underlying type
3952 -- is not deeper than the level of the master enclosing the function.
3953 -- Always generate the check when the type of the return expression
3954 -- is class-wide, when it's a type conversion, or when it's a formal
3955 -- parameter. Otherwise, suppress the check in the case where the
3956 -- return expression has a specific type whose level is known not to
3957 -- be statically deeper than the function's result type.
3959 -- Note: accessibility check is skipped in the VM case, since there
3960 -- does not seem to be any practical way to implement this check.
3962 elsif Ada_Version >= Ada_05
3963 and then VM_Target = No_VM
3964 and then Is_Class_Wide_Type (R_Type)
3965 and then not Scope_Suppress (Accessibility_Check)
3967 (Is_Class_Wide_Type (Etype (Exp))
3968 or else Nkind_In (Exp, N_Type_Conversion,
3969 N_Unchecked_Type_Conversion)
3970 or else (Is_Entity_Name (Exp)
3971 and then Ekind (Entity (Exp)) in Formal_Kind)
3972 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
3973 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
3979 -- Ada 2005 (AI-251): In class-wide interface objects we displace
3980 -- "this" to reference the base of the object --- required to get
3981 -- access to the TSD of the object.
3983 if Is_Class_Wide_Type (Etype (Exp))
3984 and then Is_Interface (Etype (Exp))
3985 and then Nkind (Exp) = N_Explicit_Dereference
3988 Make_Explicit_Dereference (Loc,
3989 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
3990 Make_Function_Call (Loc,
3991 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
3992 Parameter_Associations => New_List (
3993 Unchecked_Convert_To (RTE (RE_Address),
3994 Duplicate_Subexpr (Prefix (Exp)))))));
3997 Make_Attribute_Reference (Loc,
3998 Prefix => Duplicate_Subexpr (Exp),
3999 Attribute_Name => Name_Tag);
4003 Make_Raise_Program_Error (Loc,
4007 Build_Get_Access_Level (Loc, Tag_Node),
4009 Make_Integer_Literal (Loc,
4010 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
4011 Reason => PE_Accessibility_Check_Failed));
4015 -- Generate call to postcondition checks if they are present
4017 if Ekind (Scope_Id) = E_Function
4018 and then Has_Postconditions (Scope_Id)
4020 -- We are going to reference the returned value twice in this case,
4021 -- once in the call to _Postconditions, and once in the actual return
4022 -- statement, but we can't have side effects happening twice, and in
4023 -- any case for efficiency we don't want to do the computation twice.
4025 -- If the returned expression is an entity name, we don't need to
4026 -- worry since it is efficient and safe to reference it twice, that's
4027 -- also true for literals other than string literals, and for the
4028 -- case of X.all where X is an entity name.
4030 if Is_Entity_Name (Exp)
4031 or else Nkind_In (Exp, N_Character_Literal,
4034 or else (Nkind (Exp) = N_Explicit_Dereference
4035 and then Is_Entity_Name (Prefix (Exp)))
4039 -- Otherwise we are going to need a temporary to capture the value
4043 Tnn : constant Entity_Id :=
4044 Make_Defining_Identifier (Loc,
4045 New_Internal_Name ('T'));
4048 -- For a complex expression of an elementary type, capture
4049 -- value in the temporary and use it as the reference.
4051 if Is_Elementary_Type (R_Type) then
4053 Make_Object_Declaration (Loc,
4054 Defining_Identifier => Tnn,
4055 Constant_Present => True,
4056 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4057 Expression => Relocate_Node (Exp)),
4058 Suppress => All_Checks);
4060 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4062 -- If we have something we can rename, generate a renaming of
4063 -- the object and replace the expression with a reference
4065 elsif Is_Object_Reference (Exp) then
4067 Make_Object_Renaming_Declaration (Loc,
4068 Defining_Identifier => Tnn,
4069 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
4070 Name => Relocate_Node (Exp)),
4071 Suppress => All_Checks);
4073 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4075 -- Otherwise we have something like a string literal or an
4076 -- aggregate. We could copy the value, but that would be
4077 -- inefficient. Instead we make a reference to the value and
4078 -- capture this reference with a renaming, the expression is
4079 -- then replaced by a dereference of this renaming.
4082 -- For now, copy the value, since the code below does not
4083 -- seem to work correctly ???
4086 Make_Object_Declaration (Loc,
4087 Defining_Identifier => Tnn,
4088 Constant_Present => True,
4089 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4090 Expression => Relocate_Node (Exp)),
4091 Suppress => All_Checks);
4093 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4095 -- Insert_Action (Exp,
4096 -- Make_Object_Renaming_Declaration (Loc,
4097 -- Defining_Identifier => Tnn,
4098 -- Access_Definition =>
4099 -- Make_Access_Definition (Loc,
4100 -- All_Present => True,
4101 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
4103 -- Make_Reference (Loc,
4104 -- Prefix => Relocate_Node (Exp))),
4105 -- Suppress => All_Checks);
4108 -- Make_Explicit_Dereference (Loc,
4109 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
4114 -- Generate call to _postconditions
4117 Make_Procedure_Call_Statement (Loc,
4118 Name => Make_Identifier (Loc, Name_uPostconditions),
4119 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
4121 end Expand_Simple_Function_Return;
4123 ------------------------------
4124 -- Make_Tag_Ctrl_Assignment --
4125 ------------------------------
4127 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id is
4128 Loc : constant Source_Ptr := Sloc (N);
4129 L : constant Node_Id := Name (N);
4130 T : constant Entity_Id := Underlying_Type (Etype (L));
4132 Ctrl_Act : constant Boolean := Controlled_Type (T)
4133 and then not No_Ctrl_Actions (N);
4135 Save_Tag : constant Boolean := Is_Tagged_Type (T)
4136 and then not No_Ctrl_Actions (N)
4137 and then VM_Target = No_VM;
4138 -- Tags are not saved and restored when VM_Target because VM tags are
4139 -- represented implicitly in objects.
4142 Tag_Tmp : Entity_Id;
4144 Prev_Tmp : Entity_Id;
4145 Next_Tmp : Entity_Id;
4151 -- Finalize the target of the assignment when controlled.
4152 -- We have two exceptions here:
4154 -- 1. If we are in an init proc since it is an initialization
4155 -- more than an assignment
4157 -- 2. If the left-hand side is a temporary that was not initialized
4158 -- (or the parent part of a temporary since it is the case in
4159 -- extension aggregates). Such a temporary does not come from
4160 -- source. We must examine the original node for the prefix, because
4161 -- it may be a component of an entry formal, in which case it has
4162 -- been rewritten and does not appear to come from source either.
4164 -- Case of init proc
4166 if not Ctrl_Act then
4169 -- The left hand side is an uninitialized temporary
4171 elsif Nkind (L) = N_Type_Conversion
4172 and then Is_Entity_Name (Expression (L))
4173 and then No_Initialization (Parent (Entity (Expression (L))))
4177 Append_List_To (Res,
4179 Ref => Duplicate_Subexpr_No_Checks (L),
4181 With_Detach => New_Reference_To (Standard_False, Loc)));
4184 -- Save the Tag in a local variable Tag_Tmp
4188 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4191 Make_Object_Declaration (Loc,
4192 Defining_Identifier => Tag_Tmp,
4193 Object_Definition => New_Reference_To (RTE (RE_Tag), Loc),
4195 Make_Selected_Component (Loc,
4196 Prefix => Duplicate_Subexpr_No_Checks (L),
4197 Selector_Name => New_Reference_To (First_Tag_Component (T),
4200 -- Otherwise Tag_Tmp not used
4207 if VM_Target /= No_VM then
4209 -- Cannot assign part of the object in a VM context, so instead
4210 -- fallback to the previous mechanism, even though it is not
4211 -- completely correct ???
4213 -- Save the Finalization Pointers in local variables Prev_Tmp and
4214 -- Next_Tmp. For objects with Has_Controlled_Component set, these
4215 -- pointers are in the Record_Controller
4217 Ctrl_Ref := Duplicate_Subexpr (L);
4219 if Has_Controlled_Component (T) then
4221 Make_Selected_Component (Loc,
4224 New_Reference_To (Controller_Component (T), Loc));
4228 Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
4231 Make_Object_Declaration (Loc,
4232 Defining_Identifier => Prev_Tmp,
4234 Object_Definition =>
4235 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
4238 Make_Selected_Component (Loc,
4240 Unchecked_Convert_To (RTE (RE_Finalizable), Ctrl_Ref),
4241 Selector_Name => Make_Identifier (Loc, Name_Prev))));
4244 Make_Defining_Identifier (Loc,
4245 Chars => New_Internal_Name ('C'));
4248 Make_Object_Declaration (Loc,
4249 Defining_Identifier => Next_Tmp,
4251 Object_Definition =>
4252 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
4255 Make_Selected_Component (Loc,
4257 Unchecked_Convert_To (RTE (RE_Finalizable),
4258 New_Copy_Tree (Ctrl_Ref)),
4259 Selector_Name => Make_Identifier (Loc, Name_Next))));
4261 -- Do the Assignment
4263 Append_To (Res, Relocate_Node (N));
4266 -- Regular (non VM) processing for controlled types and types with
4267 -- controlled components
4269 -- Variables of such types contain pointers used to chain them in
4270 -- finalization lists, in addition to user data. These pointers
4271 -- are specific to each object of the type, not to the value being
4274 -- Thus they need to be left intact during the assignment. We
4275 -- achieve this by constructing a Storage_Array subtype, and by
4276 -- overlaying objects of this type on the source and target of the
4277 -- assignment. The assignment is then rewritten to assignments of
4278 -- slices of these arrays, copying the user data, and leaving the
4279 -- pointers untouched.
4281 Controlled_Actions : declare
4283 -- A reference to the Prev component of the record controller
4285 First_After_Root : Node_Id := Empty;
4286 -- Index of first byte to be copied (used to skip
4287 -- Root_Controlled in controlled objects).
4289 Last_Before_Hole : Node_Id := Empty;
4290 -- Index of last byte to be copied before outermost record
4293 Hole_Length : Node_Id := Empty;
4294 -- Length of record controller data (Prev and Next pointers)
4296 First_After_Hole : Node_Id := Empty;
4297 -- Index of first byte to be copied after outermost record
4300 Expr, Source_Size : Node_Id;
4301 Source_Actual_Subtype : Entity_Id;
4302 -- Used for computation of the size of the data to be copied
4304 Range_Type : Entity_Id;
4305 Opaque_Type : Entity_Id;
4307 function Build_Slice
4310 Hi : Node_Id) return Node_Id;
4311 -- Build and return a slice of an array of type S overlaid on
4312 -- object Rec, with bounds specified by Lo and Hi. If either
4313 -- bound is empty, a default of S'First (respectively S'Last)
4320 function Build_Slice
4323 Hi : Node_Id) return Node_Id
4328 Opaque : constant Node_Id :=
4329 Unchecked_Convert_To (Opaque_Type,
4330 Make_Attribute_Reference (Loc,
4332 Attribute_Name => Name_Address));
4333 -- Access value designating an opaque storage array of type
4334 -- S overlaid on record Rec.
4337 -- Compute slice bounds using S'First (1) and S'Last as
4338 -- default values when not specified by the caller.
4341 Lo_Bound := Make_Integer_Literal (Loc, 1);
4347 Hi_Bound := Make_Attribute_Reference (Loc,
4348 Prefix => New_Occurrence_Of (Range_Type, Loc),
4349 Attribute_Name => Name_Last);
4354 return Make_Slice (Loc,
4357 Discrete_Range => Make_Range (Loc,
4358 Lo_Bound, Hi_Bound));
4361 -- Start of processing for Controlled_Actions
4364 -- Create a constrained subtype of Storage_Array whose size
4365 -- corresponds to the value being assigned.
4367 -- subtype G is Storage_Offset range
4368 -- 1 .. (Expr'Size + Storage_Unit - 1) / Storage_Unit
4370 Expr := Duplicate_Subexpr_No_Checks (Expression (N));
4372 if Nkind (Expr) = N_Qualified_Expression then
4373 Expr := Expression (Expr);
4376 Source_Actual_Subtype := Etype (Expr);
4378 if Has_Discriminants (Source_Actual_Subtype)
4379 and then not Is_Constrained (Source_Actual_Subtype)
4382 Build_Actual_Subtype (Source_Actual_Subtype, Expr));
4383 Source_Actual_Subtype := Defining_Identifier (Last (Res));
4389 Make_Attribute_Reference (Loc,
4391 New_Occurrence_Of (Source_Actual_Subtype, Loc),
4392 Attribute_Name => Name_Size),
4394 Make_Integer_Literal (Loc,
4395 Intval => System_Storage_Unit - 1));
4398 Make_Op_Divide (Loc,
4399 Left_Opnd => Source_Size,
4401 Make_Integer_Literal (Loc,
4402 Intval => System_Storage_Unit));
4405 Make_Defining_Identifier (Loc,
4406 New_Internal_Name ('G'));
4409 Make_Subtype_Declaration (Loc,
4410 Defining_Identifier => Range_Type,
4411 Subtype_Indication =>
4412 Make_Subtype_Indication (Loc,
4414 New_Reference_To (RTE (RE_Storage_Offset), Loc),
4415 Constraint => Make_Range_Constraint (Loc,
4418 Low_Bound => Make_Integer_Literal (Loc, 1),
4419 High_Bound => Source_Size)))));
4421 -- subtype S is Storage_Array (G)
4424 Make_Subtype_Declaration (Loc,
4425 Defining_Identifier =>
4426 Make_Defining_Identifier (Loc,
4427 New_Internal_Name ('S')),
4428 Subtype_Indication =>
4429 Make_Subtype_Indication (Loc,
4431 New_Reference_To (RTE (RE_Storage_Array), Loc),
4433 Make_Index_Or_Discriminant_Constraint (Loc,
4435 New_List (New_Reference_To (Range_Type, Loc))))));
4437 -- type A is access S
4440 Make_Defining_Identifier (Loc,
4441 Chars => New_Internal_Name ('A'));
4444 Make_Full_Type_Declaration (Loc,
4445 Defining_Identifier => Opaque_Type,
4447 Make_Access_To_Object_Definition (Loc,
4448 Subtype_Indication =>
4450 Defining_Identifier (Last (Res)), Loc))));
4452 -- Generate appropriate slice assignments
4454 First_After_Root := Make_Integer_Literal (Loc, 1);
4456 -- For the case of a controlled object, skip the
4457 -- Root_Controlled part.
4459 if Is_Controlled (T) then
4463 Make_Op_Divide (Loc,
4464 Make_Attribute_Reference (Loc,
4466 New_Occurrence_Of (RTE (RE_Root_Controlled), Loc),
4467 Attribute_Name => Name_Size),
4468 Make_Integer_Literal (Loc, System_Storage_Unit)));
4471 -- For the case of a record with controlled components, skip
4472 -- the Prev and Next components of the record controller.
4473 -- These components constitute a 'hole' in the middle of the
4474 -- data to be copied.
4476 if Has_Controlled_Component (T) then
4478 Make_Selected_Component (Loc,
4480 Make_Selected_Component (Loc,
4481 Prefix => Duplicate_Subexpr_No_Checks (L),
4483 New_Reference_To (Controller_Component (T), Loc)),
4484 Selector_Name => Make_Identifier (Loc, Name_Prev));
4486 -- Last index before hole: determined by position of
4487 -- the _Controller.Prev component.
4490 Make_Defining_Identifier (Loc,
4491 New_Internal_Name ('L'));
4494 Make_Object_Declaration (Loc,
4495 Defining_Identifier => Last_Before_Hole,
4496 Object_Definition => New_Occurrence_Of (
4497 RTE (RE_Storage_Offset), Loc),
4498 Constant_Present => True,
4499 Expression => Make_Op_Add (Loc,
4500 Make_Attribute_Reference (Loc,
4502 Attribute_Name => Name_Position),
4503 Make_Attribute_Reference (Loc,
4504 Prefix => New_Copy_Tree (Prefix (Prev_Ref)),
4505 Attribute_Name => Name_Position))));
4507 -- Hole length: size of the Prev and Next components
4510 Make_Op_Multiply (Loc,
4511 Left_Opnd => Make_Integer_Literal (Loc, Uint_2),
4513 Make_Op_Divide (Loc,
4515 Make_Attribute_Reference (Loc,
4516 Prefix => New_Copy_Tree (Prev_Ref),
4517 Attribute_Name => Name_Size),
4519 Make_Integer_Literal (Loc,
4520 Intval => System_Storage_Unit)));
4522 -- First index after hole
4525 Make_Defining_Identifier (Loc,
4526 New_Internal_Name ('F'));
4529 Make_Object_Declaration (Loc,
4530 Defining_Identifier => First_After_Hole,
4531 Object_Definition => New_Occurrence_Of (
4532 RTE (RE_Storage_Offset), Loc),
4533 Constant_Present => True,
4539 New_Occurrence_Of (Last_Before_Hole, Loc),
4540 Right_Opnd => Hole_Length),
4541 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4544 New_Occurrence_Of (Last_Before_Hole, Loc);
4546 New_Occurrence_Of (First_After_Hole, Loc);
4549 -- Assign the first slice (possibly skipping Root_Controlled,
4550 -- up to the beginning of the record controller if present,
4551 -- up to the end of the object if not).
4553 Append_To (Res, Make_Assignment_Statement (Loc,
4554 Name => Build_Slice (
4555 Rec => Duplicate_Subexpr_No_Checks (L),
4556 Lo => First_After_Root,
4557 Hi => Last_Before_Hole),
4559 Expression => Build_Slice (
4560 Rec => Expression (N),
4561 Lo => First_After_Root,
4562 Hi => New_Copy_Tree (Last_Before_Hole))));
4564 if Present (First_After_Hole) then
4566 -- If a record controller is present, copy the second slice,
4567 -- from right after the _Controller.Next component up to the
4568 -- end of the object.
4570 Append_To (Res, Make_Assignment_Statement (Loc,
4571 Name => Build_Slice (
4572 Rec => Duplicate_Subexpr_No_Checks (L),
4573 Lo => First_After_Hole,
4575 Expression => Build_Slice (
4576 Rec => Duplicate_Subexpr_No_Checks (Expression (N)),
4577 Lo => New_Copy_Tree (First_After_Hole),
4580 end Controlled_Actions;
4584 Append_To (Res, Relocate_Node (N));
4591 Make_Assignment_Statement (Loc,
4593 Make_Selected_Component (Loc,
4594 Prefix => Duplicate_Subexpr_No_Checks (L),
4595 Selector_Name => New_Reference_To (First_Tag_Component (T),
4597 Expression => New_Reference_To (Tag_Tmp, Loc)));
4601 if VM_Target /= No_VM then
4602 -- Restore the finalization pointers
4605 Make_Assignment_Statement (Loc,
4607 Make_Selected_Component (Loc,
4609 Unchecked_Convert_To (RTE (RE_Finalizable),
4610 New_Copy_Tree (Ctrl_Ref)),
4611 Selector_Name => Make_Identifier (Loc, Name_Prev)),
4612 Expression => New_Reference_To (Prev_Tmp, Loc)));
4615 Make_Assignment_Statement (Loc,
4617 Make_Selected_Component (Loc,
4619 Unchecked_Convert_To (RTE (RE_Finalizable),
4620 New_Copy_Tree (Ctrl_Ref)),
4621 Selector_Name => Make_Identifier (Loc, Name_Next)),
4622 Expression => New_Reference_To (Next_Tmp, Loc)));
4625 -- Adjust the target after the assignment when controlled (not in the
4626 -- init proc since it is an initialization more than an assignment).
4628 Append_List_To (Res,
4630 Ref => Duplicate_Subexpr_Move_Checks (L),
4632 Flist_Ref => New_Reference_To (RTE (RE_Global_Final_List), Loc),
4633 With_Attach => Make_Integer_Literal (Loc, 0)));
4639 -- Could use comment here ???
4641 when RE_Not_Available =>
4643 end Make_Tag_Ctrl_Assignment;