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.
619 (Left_Lo, Right_Lo, Assume_Valid => True);
621 if Cresult = Unknown then
624 (Left_Hi, Right_Hi, Assume_Valid => True);
628 when LT | LE | EQ => Set_Backwards_OK (N, False);
629 when GT | GE => Set_Forwards_OK (N, False);
630 when NE | Unknown => Set_Backwards_OK (N, False);
631 Set_Forwards_OK (N, False);
636 -- If after that analysis, Forwards_OK is still True, and
637 -- Loop_Required is False, meaning that we have not discovered some
638 -- non-overlap reason for requiring a loop, then we can still let
641 if not Loop_Required then
643 -- Assume gigi can handle it if Forwards_OK is set
645 if Forwards_OK (N) then
648 -- If Forwards_OK is not set, the back end will need something
649 -- like memmove to handle the move. For now, this processing is
650 -- activated using the .s debug flag (-gnatd.s).
652 elsif Debug_Flag_Dot_S then
657 -- At this stage we have to generate an explicit loop, and we have
658 -- the following cases:
660 -- Forwards_OK = True
662 -- Rnn : right_index := right_index'First;
663 -- for Lnn in left-index loop
664 -- left (Lnn) := right (Rnn);
665 -- Rnn := right_index'Succ (Rnn);
668 -- Note: the above code MUST be analyzed with checks off, because
669 -- otherwise the Succ could overflow. But in any case this is more
672 -- Forwards_OK = False, Backwards_OK = True
674 -- Rnn : right_index := right_index'Last;
675 -- for Lnn in reverse left-index loop
676 -- left (Lnn) := right (Rnn);
677 -- Rnn := right_index'Pred (Rnn);
680 -- Note: the above code MUST be analyzed with checks off, because
681 -- otherwise the Pred could overflow. But in any case this is more
684 -- Forwards_OK = Backwards_OK = False
686 -- This only happens if we have the same array on each side. It is
687 -- possible to create situations using overlays that violate this,
688 -- but we simply do not promise to get this "right" in this case.
690 -- There are two possible subcases. If the No_Implicit_Conditionals
691 -- restriction is set, then we generate the following code:
694 -- T : constant <operand-type> := rhs;
699 -- If implicit conditionals are permitted, then we generate:
701 -- if Left_Lo <= Right_Lo then
702 -- <code for Forwards_OK = True above>
704 -- <code for Backwards_OK = True above>
707 -- In order to detect possible aliasing, we examine the renamed
708 -- expression when the source or target is a renaming. However,
709 -- the renaming may be intended to capture an address that may be
710 -- affected by subsequent code, and therefore we must recover
711 -- the actual entity for the expansion that follows, not the
712 -- object it renames. In particular, if source or target designate
713 -- a portion of a dynamically allocated object, the pointer to it
714 -- may be reassigned but the renaming preserves the proper location.
716 if Is_Entity_Name (Rhs)
718 Nkind (Parent (Entity (Rhs))) = N_Object_Renaming_Declaration
719 and then Nkind (Act_Rhs) = N_Slice
724 if Is_Entity_Name (Lhs)
726 Nkind (Parent (Entity (Lhs))) = N_Object_Renaming_Declaration
727 and then Nkind (Act_Lhs) = N_Slice
732 -- Cases where either Forwards_OK or Backwards_OK is true
734 if Forwards_OK (N) or else Backwards_OK (N) then
735 if Needs_Finalization (Component_Type (L_Type))
736 and then Base_Type (L_Type) = Base_Type (R_Type)
738 and then not No_Ctrl_Actions (N)
741 Proc : constant Entity_Id :=
742 TSS (Base_Type (L_Type), TSS_Slice_Assign);
746 Apply_Dereference (Larray);
747 Apply_Dereference (Rarray);
748 Actuals := New_List (
749 Duplicate_Subexpr (Larray, Name_Req => True),
750 Duplicate_Subexpr (Rarray, Name_Req => True),
751 Duplicate_Subexpr (Left_Lo, Name_Req => True),
752 Duplicate_Subexpr (Left_Hi, Name_Req => True),
753 Duplicate_Subexpr (Right_Lo, Name_Req => True),
754 Duplicate_Subexpr (Right_Hi, Name_Req => True));
758 Boolean_Literals (not Forwards_OK (N)), Loc));
761 Make_Procedure_Call_Statement (Loc,
762 Name => New_Reference_To (Proc, Loc),
763 Parameter_Associations => Actuals));
768 Expand_Assign_Array_Loop
769 (N, Larray, Rarray, L_Type, R_Type, Ndim,
770 Rev => not Forwards_OK (N)));
773 -- Case of both are false with No_Implicit_Conditionals
775 elsif Restriction_Active (No_Implicit_Conditionals) then
777 T : constant Entity_Id :=
778 Make_Defining_Identifier (Loc, Chars => Name_T);
782 Make_Block_Statement (Loc,
783 Declarations => New_List (
784 Make_Object_Declaration (Loc,
785 Defining_Identifier => T,
786 Constant_Present => True,
788 New_Occurrence_Of (Etype (Rhs), Loc),
789 Expression => Relocate_Node (Rhs))),
791 Handled_Statement_Sequence =>
792 Make_Handled_Sequence_Of_Statements (Loc,
793 Statements => New_List (
794 Make_Assignment_Statement (Loc,
795 Name => Relocate_Node (Lhs),
796 Expression => New_Occurrence_Of (T, Loc))))));
799 -- Case of both are false with implicit conditionals allowed
802 -- Before we generate this code, we must ensure that the left and
803 -- right side array types are defined. They may be itypes, and we
804 -- cannot let them be defined inside the if, since the first use
805 -- in the then may not be executed.
807 Ensure_Defined (L_Type, N);
808 Ensure_Defined (R_Type, N);
810 -- We normally compare addresses to find out which way round to
811 -- do the loop, since this is reliable, and handles the cases of
812 -- parameters, conversions etc. But we can't do that in the bit
813 -- packed case or the VM case, because addresses don't work there.
815 if not Is_Bit_Packed_Array (L_Type) and then VM_Target = No_VM then
819 Unchecked_Convert_To (RTE (RE_Integer_Address),
820 Make_Attribute_Reference (Loc,
822 Make_Indexed_Component (Loc,
824 Duplicate_Subexpr_Move_Checks (Larray, True),
825 Expressions => New_List (
826 Make_Attribute_Reference (Loc,
830 Attribute_Name => Name_First))),
831 Attribute_Name => Name_Address)),
834 Unchecked_Convert_To (RTE (RE_Integer_Address),
835 Make_Attribute_Reference (Loc,
837 Make_Indexed_Component (Loc,
839 Duplicate_Subexpr_Move_Checks (Rarray, True),
840 Expressions => New_List (
841 Make_Attribute_Reference (Loc,
845 Attribute_Name => Name_First))),
846 Attribute_Name => Name_Address)));
848 -- For the bit packed and VM cases we use the bounds. That's OK,
849 -- because we don't have to worry about parameters, since they
850 -- cannot cause overlap. Perhaps we should worry about weird slice
854 -- Copy the bounds and reset the Analyzed flag, because the
855 -- bounds of the index type itself may be universal, and must
856 -- must be reaanalyzed to acquire the proper type for Gigi.
858 Cleft_Lo := New_Copy_Tree (Left_Lo);
859 Cright_Lo := New_Copy_Tree (Right_Lo);
860 Set_Analyzed (Cleft_Lo, False);
861 Set_Analyzed (Cright_Lo, False);
865 Left_Opnd => Cleft_Lo,
866 Right_Opnd => Cright_Lo);
869 if Needs_Finalization (Component_Type (L_Type))
870 and then Base_Type (L_Type) = Base_Type (R_Type)
872 and then not No_Ctrl_Actions (N)
875 -- Call TSS procedure for array assignment, passing the
876 -- explicit bounds of right and left hand sides.
879 Proc : constant Entity_Id :=
880 TSS (Base_Type (L_Type), TSS_Slice_Assign);
884 Apply_Dereference (Larray);
885 Apply_Dereference (Rarray);
886 Actuals := New_List (
887 Duplicate_Subexpr (Larray, Name_Req => True),
888 Duplicate_Subexpr (Rarray, Name_Req => True),
889 Duplicate_Subexpr (Left_Lo, Name_Req => True),
890 Duplicate_Subexpr (Left_Hi, Name_Req => True),
891 Duplicate_Subexpr (Right_Lo, Name_Req => True),
892 Duplicate_Subexpr (Right_Hi, Name_Req => True));
896 Right_Opnd => Condition));
899 Make_Procedure_Call_Statement (Loc,
900 Name => New_Reference_To (Proc, Loc),
901 Parameter_Associations => Actuals));
906 Make_Implicit_If_Statement (N,
907 Condition => Condition,
909 Then_Statements => New_List (
910 Expand_Assign_Array_Loop
911 (N, Larray, Rarray, L_Type, R_Type, Ndim,
914 Else_Statements => New_List (
915 Expand_Assign_Array_Loop
916 (N, Larray, Rarray, L_Type, R_Type, Ndim,
921 Analyze (N, Suppress => All_Checks);
925 when RE_Not_Available =>
927 end Expand_Assign_Array;
929 ------------------------------
930 -- Expand_Assign_Array_Loop --
931 ------------------------------
933 -- The following is an example of the loop generated for the case of a
934 -- two-dimensional array:
939 -- for L1b in 1 .. 100 loop
943 -- for L3b in 1 .. 100 loop
944 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
945 -- R4b := Tm1X2'succ(R4b);
948 -- R2b := Tm1X1'succ(R2b);
952 -- Here Rev is False, and Tm1Xn are the subscript types for the right hand
953 -- side. The declarations of R2b and R4b are inserted before the original
954 -- assignment statement.
956 function Expand_Assign_Array_Loop
963 Rev : Boolean) return Node_Id
965 Loc : constant Source_Ptr := Sloc (N);
967 Lnn : array (1 .. Ndim) of Entity_Id;
968 Rnn : array (1 .. Ndim) of Entity_Id;
969 -- Entities used as subscripts on left and right sides
971 L_Index_Type : array (1 .. Ndim) of Entity_Id;
972 R_Index_Type : array (1 .. Ndim) of Entity_Id;
973 -- Left and right index types
985 F_Or_L := Name_First;
989 -- Setup index types and subscript entities
996 L_Index := First_Index (L_Type);
997 R_Index := First_Index (R_Type);
999 for J in 1 .. Ndim loop
1001 Make_Defining_Identifier (Loc,
1002 Chars => New_Internal_Name ('L'));
1005 Make_Defining_Identifier (Loc,
1006 Chars => New_Internal_Name ('R'));
1008 L_Index_Type (J) := Etype (L_Index);
1009 R_Index_Type (J) := Etype (R_Index);
1011 Next_Index (L_Index);
1012 Next_Index (R_Index);
1016 -- Now construct the assignment statement
1019 ExprL : constant List_Id := New_List;
1020 ExprR : constant List_Id := New_List;
1023 for J in 1 .. Ndim loop
1024 Append_To (ExprL, New_Occurrence_Of (Lnn (J), Loc));
1025 Append_To (ExprR, New_Occurrence_Of (Rnn (J), Loc));
1029 Make_Assignment_Statement (Loc,
1031 Make_Indexed_Component (Loc,
1032 Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
1033 Expressions => ExprL),
1035 Make_Indexed_Component (Loc,
1036 Prefix => Duplicate_Subexpr (Rarray, Name_Req => True),
1037 Expressions => ExprR));
1039 -- We set assignment OK, since there are some cases, e.g. in object
1040 -- declarations, where we are actually assigning into a constant.
1041 -- If there really is an illegality, it was caught long before now,
1042 -- and was flagged when the original assignment was analyzed.
1044 Set_Assignment_OK (Name (Assign));
1046 -- Propagate the No_Ctrl_Actions flag to individual assignments
1048 Set_No_Ctrl_Actions (Assign, No_Ctrl_Actions (N));
1051 -- Now construct the loop from the inside out, with the last subscript
1052 -- varying most rapidly. Note that Assign is first the raw assignment
1053 -- statement, and then subsequently the loop that wraps it up.
1055 for J in reverse 1 .. Ndim loop
1057 Make_Block_Statement (Loc,
1058 Declarations => New_List (
1059 Make_Object_Declaration (Loc,
1060 Defining_Identifier => Rnn (J),
1061 Object_Definition =>
1062 New_Occurrence_Of (R_Index_Type (J), Loc),
1064 Make_Attribute_Reference (Loc,
1065 Prefix => New_Occurrence_Of (R_Index_Type (J), Loc),
1066 Attribute_Name => F_Or_L))),
1068 Handled_Statement_Sequence =>
1069 Make_Handled_Sequence_Of_Statements (Loc,
1070 Statements => New_List (
1071 Make_Implicit_Loop_Statement (N,
1073 Make_Iteration_Scheme (Loc,
1074 Loop_Parameter_Specification =>
1075 Make_Loop_Parameter_Specification (Loc,
1076 Defining_Identifier => Lnn (J),
1077 Reverse_Present => Rev,
1078 Discrete_Subtype_Definition =>
1079 New_Reference_To (L_Index_Type (J), Loc))),
1081 Statements => New_List (
1084 Make_Assignment_Statement (Loc,
1085 Name => New_Occurrence_Of (Rnn (J), Loc),
1087 Make_Attribute_Reference (Loc,
1089 New_Occurrence_Of (R_Index_Type (J), Loc),
1090 Attribute_Name => S_Or_P,
1091 Expressions => New_List (
1092 New_Occurrence_Of (Rnn (J), Loc)))))))));
1096 end Expand_Assign_Array_Loop;
1098 --------------------------
1099 -- Expand_Assign_Record --
1100 --------------------------
1102 -- The only processing required is in the change of representation case,
1103 -- where we must expand the assignment to a series of field by field
1106 procedure Expand_Assign_Record (N : Node_Id) is
1107 Lhs : constant Node_Id := Name (N);
1108 Rhs : Node_Id := Expression (N);
1111 -- If change of representation, then extract the real right hand side
1112 -- from the type conversion, and proceed with component-wise assignment,
1113 -- since the two types are not the same as far as the back end is
1116 if Change_Of_Representation (N) then
1117 Rhs := Expression (Rhs);
1119 -- If this may be a case of a large bit aligned component, then proceed
1120 -- with component-wise assignment, to avoid possible clobbering of other
1121 -- components sharing bits in the first or last byte of the component to
1124 elsif Possible_Bit_Aligned_Component (Lhs)
1126 Possible_Bit_Aligned_Component (Rhs)
1130 -- If neither condition met, then nothing special to do, the back end
1131 -- can handle assignment of the entire component as a single entity.
1137 -- At this stage we know that we must do a component wise assignment
1140 Loc : constant Source_Ptr := Sloc (N);
1141 R_Typ : constant Entity_Id := Base_Type (Etype (Rhs));
1142 L_Typ : constant Entity_Id := Base_Type (Etype (Lhs));
1143 Decl : constant Node_Id := Declaration_Node (R_Typ);
1147 function Find_Component
1149 Comp : Entity_Id) return Entity_Id;
1150 -- Find the component with the given name in the underlying record
1151 -- declaration for Typ. We need to use the actual entity because the
1152 -- type may be private and resolution by identifier alone would fail.
1154 function Make_Component_List_Assign
1156 U_U : Boolean := False) return List_Id;
1157 -- Returns a sequence of statements to assign the components that
1158 -- are referenced in the given component list. The flag U_U is
1159 -- used to force the usage of the inferred value of the variant
1160 -- part expression as the switch for the generated case statement.
1162 function Make_Field_Assign
1164 U_U : Boolean := False) return Node_Id;
1165 -- Given C, the entity for a discriminant or component, build an
1166 -- assignment for the corresponding field values. The flag U_U
1167 -- signals the presence of an Unchecked_Union and forces the usage
1168 -- of the inferred discriminant value of C as the right hand side
1169 -- of the assignment.
1171 function Make_Field_Assigns (CI : List_Id) return List_Id;
1172 -- Given CI, a component items list, construct series of statements
1173 -- for fieldwise assignment of the corresponding components.
1175 --------------------
1176 -- Find_Component --
1177 --------------------
1179 function Find_Component
1181 Comp : Entity_Id) return Entity_Id
1183 Utyp : constant Entity_Id := Underlying_Type (Typ);
1187 C := First_Entity (Utyp);
1189 while Present (C) loop
1190 if Chars (C) = Chars (Comp) then
1196 raise Program_Error;
1199 --------------------------------
1200 -- Make_Component_List_Assign --
1201 --------------------------------
1203 function Make_Component_List_Assign
1205 U_U : Boolean := False) return List_Id
1207 CI : constant List_Id := Component_Items (CL);
1208 VP : constant Node_Id := Variant_Part (CL);
1218 Result := Make_Field_Assigns (CI);
1220 if Present (VP) then
1222 V := First_Non_Pragma (Variants (VP));
1224 while Present (V) loop
1227 DC := First (Discrete_Choices (V));
1228 while Present (DC) loop
1229 Append_To (DCH, New_Copy_Tree (DC));
1234 Make_Case_Statement_Alternative (Loc,
1235 Discrete_Choices => DCH,
1237 Make_Component_List_Assign (Component_List (V))));
1238 Next_Non_Pragma (V);
1241 -- If we have an Unchecked_Union, use the value of the inferred
1242 -- discriminant of the variant part expression as the switch
1243 -- for the case statement. The case statement may later be
1248 New_Copy (Get_Discriminant_Value (
1251 Discriminant_Constraint (Etype (Rhs))));
1254 Make_Selected_Component (Loc,
1255 Prefix => Duplicate_Subexpr (Rhs),
1257 Make_Identifier (Loc, Chars (Name (VP))));
1261 Make_Case_Statement (Loc,
1263 Alternatives => Alts));
1267 end Make_Component_List_Assign;
1269 -----------------------
1270 -- Make_Field_Assign --
1271 -----------------------
1273 function Make_Field_Assign
1275 U_U : Boolean := False) return Node_Id
1281 -- In the case of an Unchecked_Union, use the discriminant
1282 -- constraint value as on the right hand side of the assignment.
1286 New_Copy (Get_Discriminant_Value (C,
1288 Discriminant_Constraint (Etype (Rhs))));
1291 Make_Selected_Component (Loc,
1292 Prefix => Duplicate_Subexpr (Rhs),
1293 Selector_Name => New_Occurrence_Of (C, Loc));
1297 Make_Assignment_Statement (Loc,
1299 Make_Selected_Component (Loc,
1300 Prefix => Duplicate_Subexpr (Lhs),
1302 New_Occurrence_Of (Find_Component (L_Typ, C), Loc)),
1303 Expression => Expr);
1305 -- Set Assignment_OK, so discriminants can be assigned
1307 Set_Assignment_OK (Name (A), True);
1309 end Make_Field_Assign;
1311 ------------------------
1312 -- Make_Field_Assigns --
1313 ------------------------
1315 function Make_Field_Assigns (CI : List_Id) return List_Id is
1322 while Present (Item) loop
1323 if Nkind (Item) = N_Component_Declaration then
1325 (Result, Make_Field_Assign (Defining_Identifier (Item)));
1332 end Make_Field_Assigns;
1334 -- Start of processing for Expand_Assign_Record
1337 -- Note that we use the base types for this processing. This results
1338 -- in some extra work in the constrained case, but the change of
1339 -- representation case is so unusual that it is not worth the effort.
1341 -- First copy the discriminants. This is done unconditionally. It
1342 -- is required in the unconstrained left side case, and also in the
1343 -- case where this assignment was constructed during the expansion
1344 -- of a type conversion (since initialization of discriminants is
1345 -- suppressed in this case). It is unnecessary but harmless in
1348 if Has_Discriminants (L_Typ) then
1349 F := First_Discriminant (R_Typ);
1350 while Present (F) loop
1352 -- If we are expanding the initialization of a derived record
1353 -- that constrains or renames discriminants of the parent, we
1354 -- must use the corresponding discriminant in the parent.
1361 and then Present (Corresponding_Discriminant (F))
1363 CF := Corresponding_Discriminant (F);
1368 if Is_Unchecked_Union (Base_Type (R_Typ)) then
1369 Insert_Action (N, Make_Field_Assign (CF, True));
1371 Insert_Action (N, Make_Field_Assign (CF));
1374 Next_Discriminant (F);
1379 -- We know the underlying type is a record, but its current view
1380 -- may be private. We must retrieve the usable record declaration.
1382 if Nkind (Decl) = N_Private_Type_Declaration
1383 and then Present (Full_View (R_Typ))
1385 RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
1387 RDef := Type_Definition (Decl);
1390 if Nkind (RDef) = N_Record_Definition
1391 and then Present (Component_List (RDef))
1394 if Is_Unchecked_Union (R_Typ) then
1396 Make_Component_List_Assign (Component_List (RDef), True));
1399 (N, Make_Component_List_Assign (Component_List (RDef)));
1402 Rewrite (N, Make_Null_Statement (Loc));
1406 end Expand_Assign_Record;
1408 -----------------------------------
1409 -- Expand_N_Assignment_Statement --
1410 -----------------------------------
1412 -- This procedure implements various cases where an assignment statement
1413 -- cannot just be passed on to the back end in untransformed state.
1415 procedure Expand_N_Assignment_Statement (N : Node_Id) is
1416 Loc : constant Source_Ptr := Sloc (N);
1417 Lhs : constant Node_Id := Name (N);
1418 Rhs : constant Node_Id := Expression (N);
1419 Typ : constant Entity_Id := Underlying_Type (Etype (Lhs));
1423 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1425 -- Rewrite an assignment to X'Priority into a run-time call
1427 -- For example: X'Priority := New_Prio_Expr;
1428 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1430 -- Note that although X'Priority is notionally an object, it is quite
1431 -- deliberately not defined as an aliased object in the RM. This means
1432 -- that it works fine to rewrite it as a call, without having to worry
1433 -- about complications that would other arise from X'Priority'Access,
1434 -- which is illegal, because of the lack of aliasing.
1436 if Ada_Version >= Ada_05 then
1439 Conctyp : Entity_Id;
1442 RT_Subprg_Name : Node_Id;
1445 -- Handle chains of renamings
1448 while Nkind (Ent) in N_Has_Entity
1449 and then Present (Entity (Ent))
1450 and then Present (Renamed_Object (Entity (Ent)))
1452 Ent := Renamed_Object (Entity (Ent));
1455 -- The attribute Priority applied to protected objects has been
1456 -- previously expanded into a call to the Get_Ceiling run-time
1459 if Nkind (Ent) = N_Function_Call
1460 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
1462 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling))
1464 -- Look for the enclosing concurrent type
1466 Conctyp := Current_Scope;
1467 while not Is_Concurrent_Type (Conctyp) loop
1468 Conctyp := Scope (Conctyp);
1471 pragma Assert (Is_Protected_Type (Conctyp));
1473 -- Generate the first actual of the call
1475 Subprg := Current_Scope;
1476 while not Present (Protected_Body_Subprogram (Subprg)) loop
1477 Subprg := Scope (Subprg);
1480 -- Select the appropriate run-time call
1482 if Number_Entries (Conctyp) = 0 then
1484 New_Reference_To (RTE (RE_Set_Ceiling), Loc);
1487 New_Reference_To (RTE (RO_PE_Set_Ceiling), Loc);
1491 Make_Procedure_Call_Statement (Loc,
1492 Name => RT_Subprg_Name,
1493 Parameter_Associations => New_List (
1494 New_Copy_Tree (First (Parameter_Associations (Ent))),
1495 Relocate_Node (Expression (N))));
1504 -- First deal with generation of range check if required. For now we do
1505 -- this only for discrete types.
1507 if Do_Range_Check (Rhs)
1508 and then Is_Discrete_Type (Typ)
1510 Set_Do_Range_Check (Rhs, False);
1511 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
1514 -- Check for a special case where a high level transformation is
1515 -- required. If we have either of:
1520 -- where P is a reference to a bit packed array, then we have to unwind
1521 -- the assignment. The exact meaning of being a reference to a bit
1522 -- packed array is as follows:
1524 -- An indexed component whose prefix is a bit packed array is a
1525 -- reference to a bit packed array.
1527 -- An indexed component or selected component whose prefix is a
1528 -- reference to a bit packed array is itself a reference ot a
1529 -- bit packed array.
1531 -- The required transformation is
1533 -- Tnn : prefix_type := P;
1534 -- Tnn.field := rhs;
1539 -- Tnn : prefix_type := P;
1540 -- Tnn (subscr) := rhs;
1543 -- Since P is going to be evaluated more than once, any subscripts
1544 -- in P must have their evaluation forced.
1546 if Nkind_In (Lhs, N_Indexed_Component, N_Selected_Component)
1547 and then Is_Ref_To_Bit_Packed_Array (Prefix (Lhs))
1550 BPAR_Expr : constant Node_Id := Relocate_Node (Prefix (Lhs));
1551 BPAR_Typ : constant Entity_Id := Etype (BPAR_Expr);
1552 Tnn : constant Entity_Id :=
1553 Make_Defining_Identifier (Loc,
1554 Chars => New_Internal_Name ('T'));
1557 -- Insert the post assignment first, because we want to copy the
1558 -- BPAR_Expr tree before it gets analyzed in the context of the
1559 -- pre assignment. Note that we do not analyze the post assignment
1560 -- yet (we cannot till we have completed the analysis of the pre
1561 -- assignment). As usual, the analysis of this post assignment
1562 -- will happen on its own when we "run into" it after finishing
1563 -- the current assignment.
1566 Make_Assignment_Statement (Loc,
1567 Name => New_Copy_Tree (BPAR_Expr),
1568 Expression => New_Occurrence_Of (Tnn, Loc)));
1570 -- At this stage BPAR_Expr is a reference to a bit packed array
1571 -- where the reference was not expanded in the original tree,
1572 -- since it was on the left side of an assignment. But in the
1573 -- pre-assignment statement (the object definition), BPAR_Expr
1574 -- will end up on the right hand side, and must be reexpanded. To
1575 -- achieve this, we reset the analyzed flag of all selected and
1576 -- indexed components down to the actual indexed component for
1577 -- the packed array.
1581 Set_Analyzed (Exp, False);
1584 (Exp, N_Selected_Component, N_Indexed_Component)
1586 Exp := Prefix (Exp);
1592 -- Now we can insert and analyze the pre-assignment
1594 -- If the right-hand side requires a transient scope, it has
1595 -- already been placed on the stack. However, the declaration is
1596 -- inserted in the tree outside of this scope, and must reflect
1597 -- the proper scope for its variable. This awkward bit is forced
1598 -- by the stricter scope discipline imposed by GCC 2.97.
1601 Uses_Transient_Scope : constant Boolean :=
1603 and then N = Node_To_Be_Wrapped;
1606 if Uses_Transient_Scope then
1607 Push_Scope (Scope (Current_Scope));
1610 Insert_Before_And_Analyze (N,
1611 Make_Object_Declaration (Loc,
1612 Defining_Identifier => Tnn,
1613 Object_Definition => New_Occurrence_Of (BPAR_Typ, Loc),
1614 Expression => BPAR_Expr));
1616 if Uses_Transient_Scope then
1621 -- Now fix up the original assignment and continue processing
1623 Rewrite (Prefix (Lhs),
1624 New_Occurrence_Of (Tnn, Loc));
1626 -- We do not need to reanalyze that assignment, and we do not need
1627 -- to worry about references to the temporary, but we do need to
1628 -- make sure that the temporary is not marked as a true constant
1629 -- since we now have a generated assignment to it!
1631 Set_Is_True_Constant (Tnn, False);
1635 -- When we have the appropriate type of aggregate in the expression (it
1636 -- has been determined during analysis of the aggregate by setting the
1637 -- delay flag), let's perform in place assignment and thus avoid
1638 -- creating a temporary.
1640 if Is_Delayed_Aggregate (Rhs) then
1641 Convert_Aggr_In_Assignment (N);
1642 Rewrite (N, Make_Null_Statement (Loc));
1647 -- Apply discriminant check if required. If Lhs is an access type to a
1648 -- designated type with discriminants, we must always check.
1650 if Has_Discriminants (Etype (Lhs)) then
1652 -- Skip discriminant check if change of representation. Will be
1653 -- done when the change of representation is expanded out.
1655 if not Change_Of_Representation (N) then
1656 Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
1659 -- If the type is private without discriminants, and the full type
1660 -- has discriminants (necessarily with defaults) a check may still be
1661 -- necessary if the Lhs is aliased. The private determinants must be
1662 -- visible to build the discriminant constraints.
1664 -- Only an explicit dereference that comes from source indicates
1665 -- aliasing. Access to formals of protected operations and entries
1666 -- create dereferences but are not semantic aliasings.
1668 elsif Is_Private_Type (Etype (Lhs))
1669 and then Has_Discriminants (Typ)
1670 and then Nkind (Lhs) = N_Explicit_Dereference
1671 and then Comes_From_Source (Lhs)
1674 Lt : constant Entity_Id := Etype (Lhs);
1676 Set_Etype (Lhs, Typ);
1677 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1678 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1679 Set_Etype (Lhs, Lt);
1682 -- If the Lhs has a private type with unknown discriminants, it
1683 -- may have a full view with discriminants, but those are nameable
1684 -- only in the underlying type, so convert the Rhs to it before
1685 -- potential checking.
1687 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs)))
1688 and then Has_Discriminants (Typ)
1690 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1691 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1693 -- In the access type case, we need the same discriminant check, and
1694 -- also range checks if we have an access to constrained array.
1696 elsif Is_Access_Type (Etype (Lhs))
1697 and then Is_Constrained (Designated_Type (Etype (Lhs)))
1699 if Has_Discriminants (Designated_Type (Etype (Lhs))) then
1701 -- Skip discriminant check if change of representation. Will be
1702 -- done when the change of representation is expanded out.
1704 if not Change_Of_Representation (N) then
1705 Apply_Discriminant_Check (Rhs, Etype (Lhs));
1708 elsif Is_Array_Type (Designated_Type (Etype (Lhs))) then
1709 Apply_Range_Check (Rhs, Etype (Lhs));
1711 if Is_Constrained (Etype (Lhs)) then
1712 Apply_Length_Check (Rhs, Etype (Lhs));
1715 if Nkind (Rhs) = N_Allocator then
1717 Target_Typ : constant Entity_Id := Etype (Expression (Rhs));
1718 C_Es : Check_Result;
1725 Etype (Designated_Type (Etype (Lhs))));
1737 -- Apply range check for access type case
1739 elsif Is_Access_Type (Etype (Lhs))
1740 and then Nkind (Rhs) = N_Allocator
1741 and then Nkind (Expression (Rhs)) = N_Qualified_Expression
1743 Analyze_And_Resolve (Expression (Rhs));
1745 (Expression (Rhs), Designated_Type (Etype (Lhs)));
1748 -- Ada 2005 (AI-231): Generate the run-time check
1750 if Is_Access_Type (Typ)
1751 and then Can_Never_Be_Null (Etype (Lhs))
1752 and then not Can_Never_Be_Null (Etype (Rhs))
1754 Apply_Constraint_Check (Rhs, Etype (Lhs));
1757 -- Case of assignment to a bit packed array element
1759 if Nkind (Lhs) = N_Indexed_Component
1760 and then Is_Bit_Packed_Array (Etype (Prefix (Lhs)))
1762 Expand_Bit_Packed_Element_Set (N);
1765 -- Build-in-place function call case. Note that we're not yet doing
1766 -- build-in-place for user-written assignment statements (the assignment
1767 -- here came from an aggregate.)
1769 elsif Ada_Version >= Ada_05
1770 and then Is_Build_In_Place_Function_Call (Rhs)
1772 Make_Build_In_Place_Call_In_Assignment (N, Rhs);
1774 elsif Is_Tagged_Type (Typ) and then Is_Value_Type (Etype (Lhs)) then
1776 -- Nothing to do for valuetypes
1777 -- ??? Set_Scope_Is_Transient (False);
1781 elsif Is_Tagged_Type (Typ)
1782 or else (Needs_Finalization (Typ) and then not Is_Array_Type (Typ))
1784 Tagged_Case : declare
1785 L : List_Id := No_List;
1786 Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
1789 -- In the controlled case, we need to make sure that function
1790 -- calls are evaluated before finalizing the target. In all cases,
1791 -- it makes the expansion easier if the side-effects are removed
1794 Remove_Side_Effects (Lhs);
1795 Remove_Side_Effects (Rhs);
1797 -- Avoid recursion in the mechanism
1801 -- If dispatching assignment, we need to dispatch to _assign
1803 if Is_Class_Wide_Type (Typ)
1805 -- If the type is tagged, we may as well use the predefined
1806 -- primitive assignment. This avoids inlining a lot of code
1807 -- and in the class-wide case, the assignment is replaced by
1808 -- dispatch call to _assign. Note that this cannot be done when
1809 -- discriminant checks are locally suppressed (as in extension
1810 -- aggregate expansions) because otherwise the discriminant
1811 -- check will be performed within the _assign call. It is also
1812 -- suppressed for assignments created by the expander that
1813 -- correspond to initializations, where we do want to copy the
1814 -- tag (No_Ctrl_Actions flag set True) by the expander and we
1815 -- do not need to mess with tags ever (Expand_Ctrl_Actions flag
1816 -- is set True in this case).
1818 or else (Is_Tagged_Type (Typ)
1819 and then not Is_Value_Type (Etype (Lhs))
1820 and then Chars (Current_Scope) /= Name_uAssign
1821 and then Expand_Ctrl_Actions
1822 and then not Discriminant_Checks_Suppressed (Empty))
1824 -- Fetch the primitive op _assign and proper type to call it.
1825 -- Because of possible conflicts between private and full view
1826 -- the proper type is fetched directly from the operation
1830 Op : constant Entity_Id :=
1831 Find_Prim_Op (Typ, Name_uAssign);
1832 F_Typ : Entity_Id := Etype (First_Formal (Op));
1835 -- If the assignment is dispatching, make sure to use the
1838 if Is_Class_Wide_Type (Typ) then
1839 F_Typ := Class_Wide_Type (F_Typ);
1844 -- In case of assignment to a class-wide tagged type, before
1845 -- the assignment we generate run-time check to ensure that
1846 -- the tags of source and target match.
1848 if Is_Class_Wide_Type (Typ)
1849 and then Is_Tagged_Type (Typ)
1850 and then Is_Tagged_Type (Underlying_Type (Etype (Rhs)))
1853 Make_Raise_Constraint_Error (Loc,
1857 Make_Selected_Component (Loc,
1858 Prefix => Duplicate_Subexpr (Lhs),
1860 Make_Identifier (Loc,
1861 Chars => Name_uTag)),
1863 Make_Selected_Component (Loc,
1864 Prefix => Duplicate_Subexpr (Rhs),
1866 Make_Identifier (Loc,
1867 Chars => Name_uTag))),
1868 Reason => CE_Tag_Check_Failed));
1872 Make_Procedure_Call_Statement (Loc,
1873 Name => New_Reference_To (Op, Loc),
1874 Parameter_Associations => New_List (
1875 Unchecked_Convert_To (F_Typ,
1876 Duplicate_Subexpr (Lhs)),
1877 Unchecked_Convert_To (F_Typ,
1878 Duplicate_Subexpr (Rhs)))));
1882 L := Make_Tag_Ctrl_Assignment (N);
1884 -- We can't afford to have destructive Finalization Actions in
1885 -- the Self assignment case, so if the target and the source
1886 -- are not obviously different, code is generated to avoid the
1887 -- self assignment case:
1889 -- if lhs'address /= rhs'address then
1890 -- <code for controlled and/or tagged assignment>
1893 -- Skip this if Restriction (No_Finalization) is active
1895 if not Statically_Different (Lhs, Rhs)
1896 and then Expand_Ctrl_Actions
1897 and then not Restriction_Active (No_Finalization)
1900 Make_Implicit_If_Statement (N,
1904 Make_Attribute_Reference (Loc,
1905 Prefix => Duplicate_Subexpr (Lhs),
1906 Attribute_Name => Name_Address),
1909 Make_Attribute_Reference (Loc,
1910 Prefix => Duplicate_Subexpr (Rhs),
1911 Attribute_Name => Name_Address)),
1913 Then_Statements => L));
1916 -- We need to set up an exception handler for implementing
1917 -- 7.6.1(18). The remaining adjustments are tackled by the
1918 -- implementation of adjust for record_controllers (see
1921 -- This is skipped if we have no finalization
1923 if Expand_Ctrl_Actions
1924 and then not Restriction_Active (No_Finalization)
1927 Make_Block_Statement (Loc,
1928 Handled_Statement_Sequence =>
1929 Make_Handled_Sequence_Of_Statements (Loc,
1931 Exception_Handlers => New_List (
1932 Make_Handler_For_Ctrl_Operation (Loc)))));
1937 Make_Block_Statement (Loc,
1938 Handled_Statement_Sequence =>
1939 Make_Handled_Sequence_Of_Statements (Loc, Statements => L)));
1941 -- If no restrictions on aborts, protect the whole assignment
1942 -- for controlled objects as per 9.8(11).
1944 if Needs_Finalization (Typ)
1945 and then Expand_Ctrl_Actions
1946 and then Abort_Allowed
1949 Blk : constant Entity_Id :=
1951 (E_Block, Current_Scope, Sloc (N), 'B');
1954 Set_Scope (Blk, Current_Scope);
1955 Set_Etype (Blk, Standard_Void_Type);
1956 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
1958 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
1959 Set_At_End_Proc (Handled_Statement_Sequence (N),
1960 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
1961 Expand_At_End_Handler
1962 (Handled_Statement_Sequence (N), Blk);
1966 -- N has been rewritten to a block statement for which it is
1967 -- known by construction that no checks are necessary: analyze
1968 -- it with all checks suppressed.
1970 Analyze (N, Suppress => All_Checks);
1976 elsif Is_Array_Type (Typ) then
1978 Actual_Rhs : Node_Id := Rhs;
1981 while Nkind_In (Actual_Rhs, N_Type_Conversion,
1982 N_Qualified_Expression)
1984 Actual_Rhs := Expression (Actual_Rhs);
1987 Expand_Assign_Array (N, Actual_Rhs);
1993 elsif Is_Record_Type (Typ) then
1994 Expand_Assign_Record (N);
1997 -- Scalar types. This is where we perform the processing related to the
1998 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2001 elsif Is_Scalar_Type (Typ) then
2003 -- Case where right side is known valid
2005 if Expr_Known_Valid (Rhs) then
2007 -- Here the right side is valid, so it is fine. The case to deal
2008 -- with is when the left side is a local variable reference whose
2009 -- value is not currently known to be valid. If this is the case,
2010 -- and the assignment appears in an unconditional context, then we
2011 -- can mark the left side as now being valid.
2013 if Is_Local_Variable_Reference (Lhs)
2014 and then not Is_Known_Valid (Entity (Lhs))
2015 and then In_Unconditional_Context (N)
2017 Set_Is_Known_Valid (Entity (Lhs), True);
2020 -- Case where right side may be invalid in the sense of the RM
2021 -- reference above. The RM does not require that we check for the
2022 -- validity on an assignment, but it does require that the assignment
2023 -- of an invalid value not cause erroneous behavior.
2025 -- The general approach in GNAT is to use the Is_Known_Valid flag
2026 -- to avoid the need for validity checking on assignments. However
2027 -- in some cases, we have to do validity checking in order to make
2028 -- sure that the setting of this flag is correct.
2031 -- Validate right side if we are validating copies
2033 if Validity_Checks_On
2034 and then Validity_Check_Copies
2036 -- Skip this if left hand side is an array or record component
2037 -- and elementary component validity checks are suppressed.
2039 if Nkind_In (Lhs, N_Selected_Component, N_Indexed_Component)
2040 and then not Validity_Check_Components
2047 -- We can propagate this to the left side where appropriate
2049 if Is_Local_Variable_Reference (Lhs)
2050 and then not Is_Known_Valid (Entity (Lhs))
2051 and then In_Unconditional_Context (N)
2053 Set_Is_Known_Valid (Entity (Lhs), True);
2056 -- Otherwise check to see what should be done
2058 -- If left side is a local variable, then we just set its flag to
2059 -- indicate that its value may no longer be valid, since we are
2060 -- copying a potentially invalid value.
2062 elsif Is_Local_Variable_Reference (Lhs) then
2063 Set_Is_Known_Valid (Entity (Lhs), False);
2065 -- Check for case of a nonlocal variable on the left side which
2066 -- is currently known to be valid. In this case, we simply ensure
2067 -- that the right side is valid. We only play the game of copying
2068 -- validity status for local variables, since we are doing this
2069 -- statically, not by tracing the full flow graph.
2071 elsif Is_Entity_Name (Lhs)
2072 and then Is_Known_Valid (Entity (Lhs))
2074 -- Note: If Validity_Checking mode is set to none, we ignore
2075 -- the Ensure_Valid call so don't worry about that case here.
2079 -- In all other cases, we can safely copy an invalid value without
2080 -- worrying about the status of the left side. Since it is not a
2081 -- variable reference it will not be considered
2082 -- as being known to be valid in any case.
2090 -- Defend against invalid subscripts on left side if we are in standard
2091 -- validity checking mode. No need to do this if we are checking all
2094 if Validity_Checks_On
2095 and then Validity_Check_Default
2096 and then not Validity_Check_Subscripts
2098 Check_Valid_Lvalue_Subscripts (Lhs);
2102 when RE_Not_Available =>
2104 end Expand_N_Assignment_Statement;
2106 ------------------------------
2107 -- Expand_N_Block_Statement --
2108 ------------------------------
2110 -- Encode entity names defined in block statement
2112 procedure Expand_N_Block_Statement (N : Node_Id) is
2114 Qualify_Entity_Names (N);
2115 end Expand_N_Block_Statement;
2117 -----------------------------
2118 -- Expand_N_Case_Statement --
2119 -----------------------------
2121 procedure Expand_N_Case_Statement (N : Node_Id) is
2122 Loc : constant Source_Ptr := Sloc (N);
2123 Expr : constant Node_Id := Expression (N);
2131 -- Check for the situation where we know at compile time which branch
2134 if Compile_Time_Known_Value (Expr) then
2135 Alt := Find_Static_Alternative (N);
2137 -- Move statements from this alternative after the case statement.
2138 -- They are already analyzed, so will be skipped by the analyzer.
2140 Insert_List_After (N, Statements (Alt));
2142 -- That leaves the case statement as a shell. So now we can kill all
2143 -- other alternatives in the case statement.
2145 Kill_Dead_Code (Expression (N));
2151 -- Loop through case alternatives, skipping pragmas, and skipping
2152 -- the one alternative that we select (and therefore retain).
2154 A := First (Alternatives (N));
2155 while Present (A) loop
2157 and then Nkind (A) = N_Case_Statement_Alternative
2159 Kill_Dead_Code (Statements (A), Warn_On_Deleted_Code);
2166 Rewrite (N, Make_Null_Statement (Loc));
2170 -- Here if the choice is not determined at compile time
2173 Last_Alt : constant Node_Id := Last (Alternatives (N));
2175 Others_Present : Boolean;
2176 Others_Node : Node_Id;
2178 Then_Stms : List_Id;
2179 Else_Stms : List_Id;
2182 if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
2183 Others_Present := True;
2184 Others_Node := Last_Alt;
2186 Others_Present := False;
2189 -- First step is to worry about possible invalid argument. The RM
2190 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2191 -- outside the base range), then Constraint_Error must be raised.
2193 -- Case of validity check required (validity checks are on, the
2194 -- expression is not known to be valid, and the case statement
2195 -- comes from source -- no need to validity check internally
2196 -- generated case statements).
2198 if Validity_Check_Default then
2199 Ensure_Valid (Expr);
2202 -- If there is only a single alternative, just replace it with the
2203 -- sequence of statements since obviously that is what is going to
2204 -- be executed in all cases.
2206 Len := List_Length (Alternatives (N));
2209 -- We still need to evaluate the expression if it has any
2212 Remove_Side_Effects (Expression (N));
2214 Insert_List_After (N, Statements (First (Alternatives (N))));
2216 -- That leaves the case statement as a shell. The alternative that
2217 -- will be executed is reset to a null list. So now we can kill
2218 -- the entire case statement.
2220 Kill_Dead_Code (Expression (N));
2221 Rewrite (N, Make_Null_Statement (Loc));
2225 -- An optimization. If there are only two alternatives, and only
2226 -- a single choice, then rewrite the whole case statement as an
2227 -- if statement, since this can result in subsequent optimizations.
2228 -- This helps not only with case statements in the source of a
2229 -- simple form, but also with generated code (discriminant check
2230 -- functions in particular)
2233 Chlist := Discrete_Choices (First (Alternatives (N)));
2235 if List_Length (Chlist) = 1 then
2236 Choice := First (Chlist);
2238 Then_Stms := Statements (First (Alternatives (N)));
2239 Else_Stms := Statements (Last (Alternatives (N)));
2241 -- For TRUE, generate "expression", not expression = true
2243 if Nkind (Choice) = N_Identifier
2244 and then Entity (Choice) = Standard_True
2246 Cond := Expression (N);
2248 -- For FALSE, generate "expression" and switch then/else
2250 elsif Nkind (Choice) = N_Identifier
2251 and then Entity (Choice) = Standard_False
2253 Cond := Expression (N);
2254 Else_Stms := Statements (First (Alternatives (N)));
2255 Then_Stms := Statements (Last (Alternatives (N)));
2257 -- For a range, generate "expression in range"
2259 elsif Nkind (Choice) = N_Range
2260 or else (Nkind (Choice) = N_Attribute_Reference
2261 and then Attribute_Name (Choice) = Name_Range)
2262 or else (Is_Entity_Name (Choice)
2263 and then Is_Type (Entity (Choice)))
2264 or else Nkind (Choice) = N_Subtype_Indication
2268 Left_Opnd => Expression (N),
2269 Right_Opnd => Relocate_Node (Choice));
2271 -- For any other subexpression "expression = value"
2276 Left_Opnd => Expression (N),
2277 Right_Opnd => Relocate_Node (Choice));
2280 -- Now rewrite the case as an IF
2283 Make_If_Statement (Loc,
2285 Then_Statements => Then_Stms,
2286 Else_Statements => Else_Stms));
2292 -- If the last alternative is not an Others choice, replace it with
2293 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2294 -- the modified case statement, since it's only effect would be to
2295 -- compute the contents of the Others_Discrete_Choices which is not
2296 -- needed by the back end anyway.
2298 -- The reason we do this is that the back end always needs some
2299 -- default for a switch, so if we have not supplied one in the
2300 -- processing above for validity checking, then we need to supply
2303 if not Others_Present then
2304 Others_Node := Make_Others_Choice (Sloc (Last_Alt));
2305 Set_Others_Discrete_Choices
2306 (Others_Node, Discrete_Choices (Last_Alt));
2307 Set_Discrete_Choices (Last_Alt, New_List (Others_Node));
2310 end Expand_N_Case_Statement;
2312 -----------------------------
2313 -- Expand_N_Exit_Statement --
2314 -----------------------------
2316 -- The only processing required is to deal with a possible C/Fortran
2317 -- boolean value used as the condition for the exit statement.
2319 procedure Expand_N_Exit_Statement (N : Node_Id) is
2321 Adjust_Condition (Condition (N));
2322 end Expand_N_Exit_Statement;
2324 ----------------------------------------
2325 -- Expand_N_Extended_Return_Statement --
2326 ----------------------------------------
2328 -- If there is a Handled_Statement_Sequence, we rewrite this:
2330 -- return Result : T := <expression> do
2331 -- <handled_seq_of_stms>
2337 -- Result : T := <expression>;
2339 -- <handled_seq_of_stms>
2343 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
2345 -- return Result : T := <expression>;
2349 -- return <expression>;
2351 -- unless it's build-in-place or there's no <expression>, in which case
2355 -- Result : T := <expression>;
2360 -- Note that this case could have been written by the user as an extended
2361 -- return statement, or could have been transformed to this from a simple
2362 -- return statement.
2364 -- That is, we need to have a reified return object if there are statements
2365 -- (which might refer to it) or if we're doing build-in-place (so we can
2366 -- set its address to the final resting place or if there is no expression
2367 -- (in which case default initial values might need to be set).
2369 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
2370 Loc : constant Source_Ptr := Sloc (N);
2372 Return_Object_Entity : constant Entity_Id :=
2373 First_Entity (Return_Statement_Entity (N));
2374 Return_Object_Decl : constant Node_Id :=
2375 Parent (Return_Object_Entity);
2376 Parent_Function : constant Entity_Id :=
2377 Return_Applies_To (Return_Statement_Entity (N));
2378 Parent_Function_Typ : constant Entity_Id := Etype (Parent_Function);
2379 Is_Build_In_Place : constant Boolean :=
2380 Is_Build_In_Place_Function (Parent_Function);
2382 Return_Stm : Node_Id;
2383 Statements : List_Id;
2384 Handled_Stm_Seq : Node_Id;
2388 function Has_Controlled_Parts (Typ : Entity_Id) return Boolean;
2389 -- Determine whether type Typ is controlled or contains a controlled
2392 function Move_Activation_Chain return Node_Id;
2393 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
2395 -- From current activation chain
2396 -- To activation chain passed in by the caller
2397 -- New_Master master passed in by the caller
2399 function Move_Final_List return Node_Id;
2400 -- Construct call to System.Finalization_Implementation.Move_Final_List
2403 -- From finalization list of the return statement
2404 -- To finalization list passed in by the caller
2406 --------------------------
2407 -- Has_Controlled_Parts --
2408 --------------------------
2410 function Has_Controlled_Parts (Typ : Entity_Id) return Boolean is
2414 or else Has_Controlled_Component (Typ);
2415 end Has_Controlled_Parts;
2417 ---------------------------
2418 -- Move_Activation_Chain --
2419 ---------------------------
2421 function Move_Activation_Chain return Node_Id is
2422 Activation_Chain_Formal : constant Entity_Id :=
2423 Build_In_Place_Formal
2424 (Parent_Function, BIP_Activation_Chain);
2425 To : constant Node_Id :=
2427 (Activation_Chain_Formal, Loc);
2428 Master_Formal : constant Entity_Id :=
2429 Build_In_Place_Formal
2430 (Parent_Function, BIP_Master);
2431 New_Master : constant Node_Id :=
2432 New_Reference_To (Master_Formal, Loc);
2434 Chain_Entity : Entity_Id;
2438 Chain_Entity := First_Entity (Return_Statement_Entity (N));
2439 while Chars (Chain_Entity) /= Name_uChain loop
2440 Chain_Entity := Next_Entity (Chain_Entity);
2444 Make_Attribute_Reference (Loc,
2445 Prefix => New_Reference_To (Chain_Entity, Loc),
2446 Attribute_Name => Name_Unrestricted_Access);
2447 -- ??? Not clear why "Make_Identifier (Loc, Name_uChain)" doesn't
2448 -- work, instead of "New_Reference_To (Chain_Entity, Loc)" above.
2451 Make_Procedure_Call_Statement (Loc,
2452 Name => New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
2453 Parameter_Associations => New_List (From, To, New_Master));
2454 end Move_Activation_Chain;
2456 ---------------------
2457 -- Move_Final_List --
2458 ---------------------
2460 function Move_Final_List return Node_Id is
2461 Flist : constant Entity_Id :=
2462 Finalization_Chain_Entity (Return_Statement_Entity (N));
2464 From : constant Node_Id := New_Reference_To (Flist, Loc);
2466 Caller_Final_List : constant Entity_Id :=
2467 Build_In_Place_Formal
2468 (Parent_Function, BIP_Final_List);
2470 To : constant Node_Id := New_Reference_To (Caller_Final_List, Loc);
2473 -- Catch cases where a finalization chain entity has not been
2474 -- associated with the return statement entity.
2476 pragma Assert (Present (Flist));
2478 -- Build required call
2481 Make_If_Statement (Loc,
2484 Left_Opnd => New_Copy (From),
2485 Right_Opnd => New_Node (N_Null, Loc)),
2488 Make_Procedure_Call_Statement (Loc,
2489 Name => New_Reference_To (RTE (RE_Move_Final_List), Loc),
2490 Parameter_Associations => New_List (From, To))));
2491 end Move_Final_List;
2493 -- Start of processing for Expand_N_Extended_Return_Statement
2496 if Nkind (Return_Object_Decl) = N_Object_Declaration then
2497 Exp := Expression (Return_Object_Decl);
2502 Handled_Stm_Seq := Handled_Statement_Sequence (N);
2504 -- Build a simple_return_statement that returns the return object when
2505 -- there is a statement sequence, or no expression, or the result will
2506 -- be built in place. Note however that we currently do this for all
2507 -- composite cases, even though nonlimited composite results are not yet
2508 -- built in place (though we plan to do so eventually).
2510 if Present (Handled_Stm_Seq)
2511 or else Is_Composite_Type (Etype (Parent_Function))
2514 if No (Handled_Stm_Seq) then
2515 Statements := New_List;
2517 -- If the extended return has a handled statement sequence, then wrap
2518 -- it in a block and use the block as the first statement.
2522 New_List (Make_Block_Statement (Loc,
2523 Declarations => New_List,
2524 Handled_Statement_Sequence => Handled_Stm_Seq));
2527 -- If control gets past the above Statements, we have successfully
2528 -- completed the return statement. If the result type has controlled
2529 -- parts and the return is for a build-in-place function, then we
2530 -- call Move_Final_List to transfer responsibility for finalization
2531 -- of the return object to the caller. An alternative would be to
2532 -- declare a Success flag in the function, initialize it to False,
2533 -- and set it to True here. Then move the Move_Final_List call into
2534 -- the cleanup code, and check Success. If Success then make a call
2535 -- to Move_Final_List else do finalization. Then we can remove the
2536 -- abort-deferral and the nulling-out of the From parameter from
2537 -- Move_Final_List. Note that the current method is not quite correct
2538 -- in the rather obscure case of a select-then-abort statement whose
2539 -- abortable part contains the return statement.
2541 -- Check the type of the function to determine whether to move the
2542 -- finalization list. A special case arises when processing a simple
2543 -- return statement which has been rewritten as an extended return.
2544 -- In that case check the type of the returned object or the original
2547 if Is_Build_In_Place
2549 (Has_Controlled_Parts (Parent_Function_Typ)
2550 or else (Is_Class_Wide_Type (Parent_Function_Typ)
2552 Has_Controlled_Parts (Root_Type (Parent_Function_Typ)))
2553 or else Has_Controlled_Parts (Etype (Return_Object_Entity))
2554 or else (Present (Exp)
2555 and then Has_Controlled_Parts (Etype (Exp))))
2557 Append_To (Statements, Move_Final_List);
2560 -- Similarly to the above Move_Final_List, if the result type
2561 -- contains tasks, we call Move_Activation_Chain. Later, the cleanup
2562 -- code will call Complete_Master, which will terminate any
2563 -- unactivated tasks belonging to the return statement master. But
2564 -- Move_Activation_Chain updates their master to be that of the
2565 -- caller, so they will not be terminated unless the return statement
2566 -- completes unsuccessfully due to exception, abort, goto, or exit.
2567 -- As a formality, we test whether the function requires the result
2568 -- to be built in place, though that's necessarily true for the case
2569 -- of result types with task parts.
2571 if Is_Build_In_Place and Has_Task (Etype (Parent_Function)) then
2572 Append_To (Statements, Move_Activation_Chain);
2575 -- Build a simple_return_statement that returns the return object
2578 Make_Simple_Return_Statement (Loc,
2579 Expression => New_Occurrence_Of (Return_Object_Entity, Loc));
2580 Append_To (Statements, Return_Stm);
2583 Make_Handled_Sequence_Of_Statements (Loc, Statements);
2586 -- Case where we build a block
2588 if Present (Handled_Stm_Seq) then
2590 Make_Block_Statement (Loc,
2591 Declarations => Return_Object_Declarations (N),
2592 Handled_Statement_Sequence => Handled_Stm_Seq);
2594 -- We set the entity of the new block statement to be that of the
2595 -- return statement. This is necessary so that various fields, such
2596 -- as Finalization_Chain_Entity carry over from the return statement
2597 -- to the block. Note that this block is unusual, in that its entity
2598 -- is an E_Return_Statement rather than an E_Block.
2601 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
2603 -- If the object decl was already rewritten as a renaming, then
2604 -- we don't want to do the object allocation and transformation of
2605 -- of the return object declaration to a renaming. This case occurs
2606 -- when the return object is initialized by a call to another
2607 -- build-in-place function, and that function is responsible for the
2608 -- allocation of the return object.
2610 if Is_Build_In_Place
2612 Nkind (Return_Object_Decl) = N_Object_Renaming_Declaration
2614 Set_By_Ref (Return_Stm); -- Return build-in-place results by ref
2616 elsif Is_Build_In_Place then
2618 -- Locate the implicit access parameter associated with the
2619 -- caller-supplied return object and convert the return
2620 -- statement's return object declaration to a renaming of a
2621 -- dereference of the access parameter. If the return object's
2622 -- declaration includes an expression that has not already been
2623 -- expanded as separate assignments, then add an assignment
2624 -- statement to ensure the return object gets initialized.
2627 -- Result : T [:= <expression>];
2634 -- Result : T renames FuncRA.all;
2635 -- [Result := <expression;]
2640 Return_Obj_Id : constant Entity_Id :=
2641 Defining_Identifier (Return_Object_Decl);
2642 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
2643 Return_Obj_Expr : constant Node_Id :=
2644 Expression (Return_Object_Decl);
2645 Result_Subt : constant Entity_Id :=
2646 Etype (Parent_Function);
2647 Constr_Result : constant Boolean :=
2648 Is_Constrained (Result_Subt);
2649 Obj_Alloc_Formal : Entity_Id;
2650 Object_Access : Entity_Id;
2651 Obj_Acc_Deref : Node_Id;
2652 Init_Assignment : Node_Id := Empty;
2655 -- Build-in-place results must be returned by reference
2657 Set_By_Ref (Return_Stm);
2659 -- Retrieve the implicit access parameter passed by the caller
2662 Build_In_Place_Formal (Parent_Function, BIP_Object_Access);
2664 -- If the return object's declaration includes an expression
2665 -- and the declaration isn't marked as No_Initialization, then
2666 -- we need to generate an assignment to the object and insert
2667 -- it after the declaration before rewriting it as a renaming
2668 -- (otherwise we'll lose the initialization).
2670 if Present (Return_Obj_Expr)
2671 and then not No_Initialization (Return_Object_Decl)
2674 Make_Assignment_Statement (Loc,
2675 Name => New_Reference_To (Return_Obj_Id, Loc),
2676 Expression => Relocate_Node (Return_Obj_Expr));
2677 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
2678 Set_Assignment_OK (Name (Init_Assignment));
2679 Set_No_Ctrl_Actions (Init_Assignment);
2681 Set_Parent (Name (Init_Assignment), Init_Assignment);
2682 Set_Parent (Expression (Init_Assignment), Init_Assignment);
2684 Set_Expression (Return_Object_Decl, Empty);
2686 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
2687 and then not Is_Class_Wide_Type
2688 (Etype (Expression (Init_Assignment)))
2690 Rewrite (Expression (Init_Assignment),
2691 Make_Type_Conversion (Loc,
2694 (Etype (Return_Obj_Id), Loc),
2696 Relocate_Node (Expression (Init_Assignment))));
2699 -- In the case of functions where the calling context can
2700 -- determine the form of allocation needed, initialization
2701 -- is done with each part of the if statement that handles
2702 -- the different forms of allocation (this is true for
2703 -- unconstrained and tagged result subtypes).
2706 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
2708 Insert_After (Return_Object_Decl, Init_Assignment);
2712 -- When the function's subtype is unconstrained, a run-time
2713 -- test is needed to determine the form of allocation to use
2714 -- for the return object. The function has an implicit formal
2715 -- parameter indicating this. If the BIP_Alloc_Form formal has
2716 -- the value one, then the caller has passed access to an
2717 -- existing object for use as the return object. If the value
2718 -- is two, then the return object must be allocated on the
2719 -- secondary stack. Otherwise, the object must be allocated in
2720 -- a storage pool (currently only supported for the global
2721 -- heap, user-defined storage pools TBD ???). We generate an
2722 -- if statement to test the implicit allocation formal and
2723 -- initialize a local access value appropriately, creating
2724 -- allocators in the secondary stack and global heap cases.
2725 -- The special formal also exists and must be tested when the
2726 -- function has a tagged result, even when the result subtype
2727 -- is constrained, because in general such functions can be
2728 -- called in dispatching contexts and must be handled similarly
2729 -- to functions with a class-wide result.
2731 if not Constr_Result
2732 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
2735 Build_In_Place_Formal (Parent_Function, BIP_Alloc_Form);
2738 Ref_Type : Entity_Id;
2739 Ptr_Type_Decl : Node_Id;
2740 Alloc_Obj_Id : Entity_Id;
2741 Alloc_Obj_Decl : Node_Id;
2742 Alloc_If_Stmt : Node_Id;
2743 SS_Allocator : Node_Id;
2744 Heap_Allocator : Node_Id;
2747 -- Reuse the itype created for the function's implicit
2748 -- access formal. This avoids the need to create a new
2749 -- access type here, plus it allows assigning the access
2750 -- formal directly without applying a conversion.
2752 -- Ref_Type := Etype (Object_Access);
2754 -- Create an access type designating the function's
2758 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2761 Make_Full_Type_Declaration (Loc,
2762 Defining_Identifier => Ref_Type,
2764 Make_Access_To_Object_Definition (Loc,
2765 All_Present => True,
2766 Subtype_Indication =>
2767 New_Reference_To (Return_Obj_Typ, Loc)));
2769 Insert_Before (Return_Object_Decl, Ptr_Type_Decl);
2771 -- Create an access object that will be initialized to an
2772 -- access value denoting the return object, either coming
2773 -- from an implicit access value passed in by the caller
2774 -- or from the result of an allocator.
2777 Make_Defining_Identifier (Loc,
2778 Chars => New_Internal_Name ('R'));
2779 Set_Etype (Alloc_Obj_Id, Ref_Type);
2782 Make_Object_Declaration (Loc,
2783 Defining_Identifier => Alloc_Obj_Id,
2784 Object_Definition => New_Reference_To
2787 Insert_Before (Return_Object_Decl, Alloc_Obj_Decl);
2789 -- Create allocators for both the secondary stack and
2790 -- global heap. If there's an initialization expression,
2791 -- then create these as initialized allocators.
2793 if Present (Return_Obj_Expr)
2794 and then not No_Initialization (Return_Object_Decl)
2797 Make_Allocator (Loc,
2799 Make_Qualified_Expression (Loc,
2801 New_Reference_To (Return_Obj_Typ, Loc),
2803 New_Copy_Tree (Return_Obj_Expr)));
2805 SS_Allocator := New_Copy_Tree (Heap_Allocator);
2808 -- If the function returns a class-wide type we cannot
2809 -- use the return type for the allocator. Instead we
2810 -- use the type of the expression, which must be an
2811 -- aggregate of a definite type.
2813 if Is_Class_Wide_Type (Return_Obj_Typ) then
2815 Make_Allocator (Loc,
2817 (Etype (Return_Obj_Expr), Loc));
2820 Make_Allocator (Loc,
2821 New_Reference_To (Return_Obj_Typ, Loc));
2824 -- If the object requires default initialization then
2825 -- that will happen later following the elaboration of
2826 -- the object renaming. If we don't turn it off here
2827 -- then the object will be default initialized twice.
2829 Set_No_Initialization (Heap_Allocator);
2831 SS_Allocator := New_Copy_Tree (Heap_Allocator);
2834 -- If the No_Allocators restriction is active, then only
2835 -- an allocator for secondary stack allocation is needed.
2837 if Restriction_Active (No_Allocators) then
2838 SS_Allocator := Heap_Allocator;
2839 Heap_Allocator := Make_Null (Loc);
2841 -- Otherwise the heap allocator may be needed, so we
2842 -- make another allocator for secondary stack allocation.
2845 SS_Allocator := New_Copy_Tree (Heap_Allocator);
2847 -- The heap allocator is marked Comes_From_Source
2848 -- since it corresponds to an explicit user-written
2849 -- allocator (that is, it will only be executed on
2850 -- behalf of callers that call the function as
2851 -- initialization for such an allocator). This
2852 -- prevents errors when No_Implicit_Heap_Allocation
2855 Set_Comes_From_Source (Heap_Allocator, True);
2858 -- The allocator is returned on the secondary stack. We
2859 -- don't do this on VM targets, since the SS is not used.
2861 if VM_Target = No_VM then
2862 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
2863 Set_Procedure_To_Call
2864 (SS_Allocator, RTE (RE_SS_Allocate));
2866 -- The allocator is returned on the secondary stack,
2867 -- so indicate that the function return, as well as
2868 -- the block that encloses the allocator, must not
2869 -- release it. The flags must be set now because the
2870 -- decision to use the secondary stack is done very
2871 -- late in the course of expanding the return
2872 -- statement, past the point where these flags are
2875 Set_Sec_Stack_Needed_For_Return (Parent_Function);
2876 Set_Sec_Stack_Needed_For_Return
2877 (Return_Statement_Entity (N));
2878 Set_Uses_Sec_Stack (Parent_Function);
2879 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
2882 -- Create an if statement to test the BIP_Alloc_Form
2883 -- formal and initialize the access object to either the
2884 -- BIP_Object_Access formal (BIP_Alloc_Form = 0), the
2885 -- result of allocating the object in the secondary stack
2886 -- (BIP_Alloc_Form = 1), or else an allocator to create
2887 -- the return object in the heap (BIP_Alloc_Form = 2).
2889 -- ??? An unchecked type conversion must be made in the
2890 -- case of assigning the access object formal to the
2891 -- local access object, because a normal conversion would
2892 -- be illegal in some cases (such as converting access-
2893 -- to-unconstrained to access-to-constrained), but the
2894 -- the unchecked conversion will presumably fail to work
2895 -- right in just such cases. It's not clear at all how to
2899 Make_If_Statement (Loc,
2903 New_Reference_To (Obj_Alloc_Formal, Loc),
2905 Make_Integer_Literal (Loc,
2906 UI_From_Int (BIP_Allocation_Form'Pos
2907 (Caller_Allocation)))),
2909 New_List (Make_Assignment_Statement (Loc,
2912 (Alloc_Obj_Id, Loc),
2914 Make_Unchecked_Type_Conversion (Loc,
2916 New_Reference_To (Ref_Type, Loc),
2919 (Object_Access, Loc)))),
2921 New_List (Make_Elsif_Part (Loc,
2926 (Obj_Alloc_Formal, Loc),
2928 Make_Integer_Literal (Loc,
2930 BIP_Allocation_Form'Pos
2931 (Secondary_Stack)))),
2934 (Make_Assignment_Statement (Loc,
2937 (Alloc_Obj_Id, Loc),
2941 New_List (Make_Assignment_Statement (Loc,
2944 (Alloc_Obj_Id, Loc),
2948 -- If a separate initialization assignment was created
2949 -- earlier, append that following the assignment of the
2950 -- implicit access formal to the access object, to ensure
2951 -- that the return object is initialized in that case.
2952 -- In this situation, the target of the assignment must
2953 -- be rewritten to denote a dereference of the access to
2954 -- the return object passed in by the caller.
2956 if Present (Init_Assignment) then
2957 Rewrite (Name (Init_Assignment),
2958 Make_Explicit_Dereference (Loc,
2959 Prefix => New_Reference_To (Alloc_Obj_Id, Loc)));
2961 (Name (Init_Assignment), Etype (Return_Obj_Id));
2964 (Then_Statements (Alloc_If_Stmt),
2968 Insert_Before (Return_Object_Decl, Alloc_If_Stmt);
2970 -- Remember the local access object for use in the
2971 -- dereference of the renaming created below.
2973 Object_Access := Alloc_Obj_Id;
2977 -- Replace the return object declaration with a renaming of a
2978 -- dereference of the access value designating the return
2982 Make_Explicit_Dereference (Loc,
2983 Prefix => New_Reference_To (Object_Access, Loc));
2985 Rewrite (Return_Object_Decl,
2986 Make_Object_Renaming_Declaration (Loc,
2987 Defining_Identifier => Return_Obj_Id,
2988 Access_Definition => Empty,
2989 Subtype_Mark => New_Occurrence_Of
2990 (Return_Obj_Typ, Loc),
2991 Name => Obj_Acc_Deref));
2993 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
2997 -- Case where we do not build a block
3000 -- We're about to drop Return_Object_Declarations on the floor, so
3001 -- we need to insert it, in case it got expanded into useful code.
3003 Insert_List_Before (N, Return_Object_Declarations (N));
3005 -- Build simple_return_statement that returns the expression directly
3007 Return_Stm := Make_Simple_Return_Statement (Loc, Expression => Exp);
3009 Result := Return_Stm;
3012 -- Set the flag to prevent infinite recursion
3014 Set_Comes_From_Extended_Return_Statement (Return_Stm);
3016 Rewrite (N, Result);
3018 end Expand_N_Extended_Return_Statement;
3020 -----------------------------
3021 -- Expand_N_Goto_Statement --
3022 -----------------------------
3024 -- Add poll before goto if polling active
3026 procedure Expand_N_Goto_Statement (N : Node_Id) is
3028 Generate_Poll_Call (N);
3029 end Expand_N_Goto_Statement;
3031 ---------------------------
3032 -- Expand_N_If_Statement --
3033 ---------------------------
3035 -- First we deal with the case of C and Fortran convention boolean values,
3036 -- with zero/non-zero semantics.
3038 -- Second, we deal with the obvious rewriting for the cases where the
3039 -- condition of the IF is known at compile time to be True or False.
3041 -- Third, we remove elsif parts which have non-empty Condition_Actions
3042 -- and rewrite as independent if statements. For example:
3053 -- <<condition actions of y>>
3059 -- This rewriting is needed if at least one elsif part has a non-empty
3060 -- Condition_Actions list. We also do the same processing if there is a
3061 -- constant condition in an elsif part (in conjunction with the first
3062 -- processing step mentioned above, for the recursive call made to deal
3063 -- with the created inner if, this deals with properly optimizing the
3064 -- cases of constant elsif conditions).
3066 procedure Expand_N_If_Statement (N : Node_Id) is
3067 Loc : constant Source_Ptr := Sloc (N);
3072 Warn_If_Deleted : constant Boolean :=
3073 Warn_On_Deleted_Code and then Comes_From_Source (N);
3074 -- Indicates whether we want warnings when we delete branches of the
3075 -- if statement based on constant condition analysis. We never want
3076 -- these warnings for expander generated code.
3079 Adjust_Condition (Condition (N));
3081 -- The following loop deals with constant conditions for the IF. We
3082 -- need a loop because as we eliminate False conditions, we grab the
3083 -- first elsif condition and use it as the primary condition.
3085 while Compile_Time_Known_Value (Condition (N)) loop
3087 -- If condition is True, we can simply rewrite the if statement now
3088 -- by replacing it by the series of then statements.
3090 if Is_True (Expr_Value (Condition (N))) then
3092 -- All the else parts can be killed
3094 Kill_Dead_Code (Elsif_Parts (N), Warn_If_Deleted);
3095 Kill_Dead_Code (Else_Statements (N), Warn_If_Deleted);
3097 Hed := Remove_Head (Then_Statements (N));
3098 Insert_List_After (N, Then_Statements (N));
3102 -- If condition is False, then we can delete the condition and
3103 -- the Then statements
3106 -- We do not delete the condition if constant condition warnings
3107 -- are enabled, since otherwise we end up deleting the desired
3108 -- warning. Of course the backend will get rid of this True/False
3109 -- test anyway, so nothing is lost here.
3111 if not Constant_Condition_Warnings then
3112 Kill_Dead_Code (Condition (N));
3115 Kill_Dead_Code (Then_Statements (N), Warn_If_Deleted);
3117 -- If there are no elsif statements, then we simply replace the
3118 -- entire if statement by the sequence of else statements.
3120 if No (Elsif_Parts (N)) then
3121 if No (Else_Statements (N))
3122 or else Is_Empty_List (Else_Statements (N))
3125 Make_Null_Statement (Sloc (N)));
3127 Hed := Remove_Head (Else_Statements (N));
3128 Insert_List_After (N, Else_Statements (N));
3134 -- If there are elsif statements, the first of them becomes the
3135 -- if/then section of the rebuilt if statement This is the case
3136 -- where we loop to reprocess this copied condition.
3139 Hed := Remove_Head (Elsif_Parts (N));
3140 Insert_Actions (N, Condition_Actions (Hed));
3141 Set_Condition (N, Condition (Hed));
3142 Set_Then_Statements (N, Then_Statements (Hed));
3144 -- Hed might have been captured as the condition determining
3145 -- the current value for an entity. Now it is detached from
3146 -- the tree, so a Current_Value pointer in the condition might
3147 -- need to be updated.
3149 Set_Current_Value_Condition (N);
3151 if Is_Empty_List (Elsif_Parts (N)) then
3152 Set_Elsif_Parts (N, No_List);
3158 -- Loop through elsif parts, dealing with constant conditions and
3159 -- possible expression actions that are present.
3161 if Present (Elsif_Parts (N)) then
3162 E := First (Elsif_Parts (N));
3163 while Present (E) loop
3164 Adjust_Condition (Condition (E));
3166 -- If there are condition actions, then rewrite the if statement
3167 -- as indicated above. We also do the same rewrite for a True or
3168 -- False condition. The further processing of this constant
3169 -- condition is then done by the recursive call to expand the
3170 -- newly created if statement
3172 if Present (Condition_Actions (E))
3173 or else Compile_Time_Known_Value (Condition (E))
3175 -- Note this is not an implicit if statement, since it is part
3176 -- of an explicit if statement in the source (or of an implicit
3177 -- if statement that has already been tested).
3180 Make_If_Statement (Sloc (E),
3181 Condition => Condition (E),
3182 Then_Statements => Then_Statements (E),
3183 Elsif_Parts => No_List,
3184 Else_Statements => Else_Statements (N));
3186 -- Elsif parts for new if come from remaining elsif's of parent
3188 while Present (Next (E)) loop
3189 if No (Elsif_Parts (New_If)) then
3190 Set_Elsif_Parts (New_If, New_List);
3193 Append (Remove_Next (E), Elsif_Parts (New_If));
3196 Set_Else_Statements (N, New_List (New_If));
3198 if Present (Condition_Actions (E)) then
3199 Insert_List_Before (New_If, Condition_Actions (E));
3204 if Is_Empty_List (Elsif_Parts (N)) then
3205 Set_Elsif_Parts (N, No_List);
3211 -- No special processing for that elsif part, move to next
3219 -- Some more optimizations applicable if we still have an IF statement
3221 if Nkind (N) /= N_If_Statement then
3225 -- Another optimization, special cases that can be simplified
3227 -- if expression then
3233 -- can be changed to:
3235 -- return expression;
3239 -- if expression then
3245 -- can be changed to:
3247 -- return not (expression);
3249 -- Only do these optimizations if we are at least at -O1 level
3251 if Optimization_Level > 0 then
3252 if Nkind (N) = N_If_Statement
3253 and then No (Elsif_Parts (N))
3254 and then Present (Else_Statements (N))
3255 and then List_Length (Then_Statements (N)) = 1
3256 and then List_Length (Else_Statements (N)) = 1
3259 Then_Stm : constant Node_Id := First (Then_Statements (N));
3260 Else_Stm : constant Node_Id := First (Else_Statements (N));
3263 if Nkind (Then_Stm) = N_Simple_Return_Statement
3265 Nkind (Else_Stm) = N_Simple_Return_Statement
3268 Then_Expr : constant Node_Id := Expression (Then_Stm);
3269 Else_Expr : constant Node_Id := Expression (Else_Stm);
3272 if Nkind (Then_Expr) = N_Identifier
3274 Nkind (Else_Expr) = N_Identifier
3276 if Entity (Then_Expr) = Standard_True
3277 and then Entity (Else_Expr) = Standard_False
3280 Make_Simple_Return_Statement (Loc,
3281 Expression => Relocate_Node (Condition (N))));
3285 elsif Entity (Then_Expr) = Standard_False
3286 and then Entity (Else_Expr) = Standard_True
3289 Make_Simple_Return_Statement (Loc,
3293 Relocate_Node (Condition (N)))));
3303 end Expand_N_If_Statement;
3305 -----------------------------
3306 -- Expand_N_Loop_Statement --
3307 -----------------------------
3309 -- 1. Remove null loop entirely
3310 -- 2. Deal with while condition for C/Fortran boolean
3311 -- 3. Deal with loops with a non-standard enumeration type range
3312 -- 4. Deal with while loops where Condition_Actions is set
3313 -- 5. Insert polling call if required
3315 procedure Expand_N_Loop_Statement (N : Node_Id) is
3316 Loc : constant Source_Ptr := Sloc (N);
3317 Isc : constant Node_Id := Iteration_Scheme (N);
3322 if Is_Null_Loop (N) then
3323 Rewrite (N, Make_Null_Statement (Loc));
3327 -- Deal with condition for C/Fortran Boolean
3329 if Present (Isc) then
3330 Adjust_Condition (Condition (Isc));
3333 -- Generate polling call
3335 if Is_Non_Empty_List (Statements (N)) then
3336 Generate_Poll_Call (First (Statements (N)));
3339 -- Nothing more to do for plain loop with no iteration scheme
3345 -- Note: we do not have to worry about validity checking of the for loop
3346 -- range bounds here, since they were frozen with constant declarations
3347 -- and it is during that process that the validity checking is done.
3349 -- Handle the case where we have a for loop with the range type being an
3350 -- enumeration type with non-standard representation. In this case we
3353 -- for x in [reverse] a .. b loop
3359 -- for xP in [reverse] integer
3360 -- range etype'Pos (a) .. etype'Pos (b) loop
3362 -- x : constant etype := Pos_To_Rep (xP);
3368 if Present (Loop_Parameter_Specification (Isc)) then
3370 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
3371 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
3372 Ltype : constant Entity_Id := Etype (Loop_Id);
3373 Btype : constant Entity_Id := Base_Type (Ltype);
3378 if not Is_Enumeration_Type (Btype)
3379 or else No (Enum_Pos_To_Rep (Btype))
3385 Make_Defining_Identifier (Loc,
3386 Chars => New_External_Name (Chars (Loop_Id), 'P'));
3388 -- If the type has a contiguous representation, successive values
3389 -- can be generated as offsets from the first literal.
3391 if Has_Contiguous_Rep (Btype) then
3393 Unchecked_Convert_To (Btype,
3396 Make_Integer_Literal (Loc,
3397 Enumeration_Rep (First_Literal (Btype))),
3398 Right_Opnd => New_Reference_To (New_Id, Loc)));
3400 -- Use the constructed array Enum_Pos_To_Rep
3403 Make_Indexed_Component (Loc,
3404 Prefix => New_Reference_To (Enum_Pos_To_Rep (Btype), Loc),
3405 Expressions => New_List (New_Reference_To (New_Id, Loc)));
3409 Make_Loop_Statement (Loc,
3410 Identifier => Identifier (N),
3413 Make_Iteration_Scheme (Loc,
3414 Loop_Parameter_Specification =>
3415 Make_Loop_Parameter_Specification (Loc,
3416 Defining_Identifier => New_Id,
3417 Reverse_Present => Reverse_Present (LPS),
3419 Discrete_Subtype_Definition =>
3420 Make_Subtype_Indication (Loc,
3423 New_Reference_To (Standard_Natural, Loc),
3426 Make_Range_Constraint (Loc,
3431 Make_Attribute_Reference (Loc,
3433 New_Reference_To (Btype, Loc),
3435 Attribute_Name => Name_Pos,
3437 Expressions => New_List (
3439 (Type_Low_Bound (Ltype)))),
3442 Make_Attribute_Reference (Loc,
3444 New_Reference_To (Btype, Loc),
3446 Attribute_Name => Name_Pos,
3448 Expressions => New_List (
3450 (Type_High_Bound (Ltype))))))))),
3452 Statements => New_List (
3453 Make_Block_Statement (Loc,
3454 Declarations => New_List (
3455 Make_Object_Declaration (Loc,
3456 Defining_Identifier => Loop_Id,
3457 Constant_Present => True,
3458 Object_Definition => New_Reference_To (Ltype, Loc),
3459 Expression => Expr)),
3461 Handled_Statement_Sequence =>
3462 Make_Handled_Sequence_Of_Statements (Loc,
3463 Statements => Statements (N)))),
3465 End_Label => End_Label (N)));
3469 -- Second case, if we have a while loop with Condition_Actions set, then
3470 -- we change it into a plain loop:
3479 -- <<condition actions>>
3485 and then Present (Condition_Actions (Isc))
3492 Make_Exit_Statement (Sloc (Condition (Isc)),
3494 Make_Op_Not (Sloc (Condition (Isc)),
3495 Right_Opnd => Condition (Isc)));
3497 Prepend (ES, Statements (N));
3498 Insert_List_Before (ES, Condition_Actions (Isc));
3500 -- This is not an implicit loop, since it is generated in response
3501 -- to the loop statement being processed. If this is itself
3502 -- implicit, the restriction has already been checked. If not,
3503 -- it is an explicit loop.
3506 Make_Loop_Statement (Sloc (N),
3507 Identifier => Identifier (N),
3508 Statements => Statements (N),
3509 End_Label => End_Label (N)));
3514 end Expand_N_Loop_Statement;
3516 --------------------------------------
3517 -- Expand_N_Simple_Return_Statement --
3518 --------------------------------------
3520 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
3522 -- Defend against previous errors (i.e. the return statement calls a
3523 -- function that is not available in configurable runtime).
3525 if Present (Expression (N))
3526 and then Nkind (Expression (N)) = N_Empty
3531 -- Distinguish the function and non-function cases:
3533 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
3536 E_Generic_Function =>
3537 Expand_Simple_Function_Return (N);
3540 E_Generic_Procedure |
3543 E_Return_Statement =>
3544 Expand_Non_Function_Return (N);
3547 raise Program_Error;
3551 when RE_Not_Available =>
3553 end Expand_N_Simple_Return_Statement;
3555 --------------------------------
3556 -- Expand_Non_Function_Return --
3557 --------------------------------
3559 procedure Expand_Non_Function_Return (N : Node_Id) is
3560 pragma Assert (No (Expression (N)));
3562 Loc : constant Source_Ptr := Sloc (N);
3563 Scope_Id : Entity_Id :=
3564 Return_Applies_To (Return_Statement_Entity (N));
3565 Kind : constant Entity_Kind := Ekind (Scope_Id);
3568 Goto_Stat : Node_Id;
3572 -- Call postconditions procedure if procedure with active postconditions
3574 if Ekind (Scope_Id) = E_Procedure
3575 and then Has_Postconditions (Scope_Id)
3578 Make_Procedure_Call_Statement (Loc,
3579 Name => Make_Identifier (Loc, Name_uPostconditions)));
3582 -- If it is a return from a procedure do no extra steps
3584 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
3587 -- If it is a nested return within an extended one, replace it with a
3588 -- return of the previously declared return object.
3590 elsif Kind = E_Return_Statement then
3592 Make_Simple_Return_Statement (Loc,
3594 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
3595 Set_Comes_From_Extended_Return_Statement (N);
3596 Set_Return_Statement_Entity (N, Scope_Id);
3597 Expand_Simple_Function_Return (N);
3601 pragma Assert (Is_Entry (Scope_Id));
3603 -- Look at the enclosing block to see whether the return is from an
3604 -- accept statement or an entry body.
3606 for J in reverse 0 .. Scope_Stack.Last loop
3607 Scope_Id := Scope_Stack.Table (J).Entity;
3608 exit when Is_Concurrent_Type (Scope_Id);
3611 -- If it is a return from accept statement it is expanded as call to
3612 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
3614 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
3615 -- Expand_N_Accept_Alternative in exp_ch9.adb)
3617 if Is_Task_Type (Scope_Id) then
3620 Make_Procedure_Call_Statement (Loc,
3621 Name => New_Reference_To
3622 (RTE (RE_Complete_Rendezvous), Loc));
3623 Insert_Before (N, Call);
3624 -- why not insert actions here???
3627 Acc_Stat := Parent (N);
3628 while Nkind (Acc_Stat) /= N_Accept_Statement loop
3629 Acc_Stat := Parent (Acc_Stat);
3632 Lab_Node := Last (Statements
3633 (Handled_Statement_Sequence (Acc_Stat)));
3635 Goto_Stat := Make_Goto_Statement (Loc,
3636 Name => New_Occurrence_Of
3637 (Entity (Identifier (Lab_Node)), Loc));
3639 Set_Analyzed (Goto_Stat);
3641 Rewrite (N, Goto_Stat);
3644 -- If it is a return from an entry body, put a Complete_Entry_Body call
3645 -- in front of the return.
3647 elsif Is_Protected_Type (Scope_Id) then
3649 Make_Procedure_Call_Statement (Loc,
3651 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
3652 Parameter_Associations => New_List (
3653 Make_Attribute_Reference (Loc,
3656 (Find_Protection_Object (Current_Scope), Loc),
3658 Name_Unchecked_Access)));
3660 Insert_Before (N, Call);
3663 end Expand_Non_Function_Return;
3665 -----------------------------------
3666 -- Expand_Simple_Function_Return --
3667 -----------------------------------
3669 -- The "simple" comes from the syntax rule simple_return_statement.
3670 -- The semantics are not at all simple!
3672 procedure Expand_Simple_Function_Return (N : Node_Id) is
3673 Loc : constant Source_Ptr := Sloc (N);
3675 Scope_Id : constant Entity_Id :=
3676 Return_Applies_To (Return_Statement_Entity (N));
3677 -- The function we are returning from
3679 R_Type : constant Entity_Id := Etype (Scope_Id);
3680 -- The result type of the function
3682 Utyp : constant Entity_Id := Underlying_Type (R_Type);
3684 Exp : constant Node_Id := Expression (N);
3685 pragma Assert (Present (Exp));
3687 Exptyp : constant Entity_Id := Etype (Exp);
3688 -- The type of the expression (not necessarily the same as R_Type)
3690 Subtype_Ind : Node_Id;
3691 -- If the result type of the function is class-wide and the
3692 -- expression has a specific type, then we use the expression's
3693 -- type as the type of the return object. In cases where the
3694 -- expression is an aggregate that is built in place, this avoids
3695 -- the need for an expensive conversion of the return object to
3696 -- the specific type on assignments to the individual components.
3699 if Is_Class_Wide_Type (R_Type)
3700 and then not Is_Class_Wide_Type (Etype (Exp))
3702 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
3704 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
3707 -- For the case of a simple return that does not come from an extended
3708 -- return, in the case of Ada 2005 where we are returning a limited
3709 -- type, we rewrite "return <expression>;" to be:
3711 -- return _anon_ : <return_subtype> := <expression>
3713 -- The expansion produced by Expand_N_Extended_Return_Statement will
3714 -- contain simple return statements (for example, a block containing
3715 -- simple return of the return object), which brings us back here with
3716 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
3717 -- checking for a simple return that does not come from an extended
3718 -- return is to avoid this infinite recursion.
3720 -- The reason for this design is that for Ada 2005 limited returns, we
3721 -- need to reify the return object, so we can build it "in place", and
3722 -- we need a block statement to hang finalization and tasking stuff.
3724 -- ??? In order to avoid disruption, we avoid translating to extended
3725 -- return except in the cases where we really need to (Ada 2005 for
3726 -- inherently limited). We might prefer to do this translation in all
3727 -- cases (except perhaps for the case of Ada 95 inherently limited),
3728 -- in order to fully exercise the Expand_N_Extended_Return_Statement
3729 -- code. This would also allow us to do the build-in-place optimization
3730 -- for efficiency even in cases where it is semantically not required.
3732 -- As before, we check the type of the return expression rather than the
3733 -- return type of the function, because the latter may be a limited
3734 -- class-wide interface type, which is not a limited type, even though
3735 -- the type of the expression may be.
3737 if not Comes_From_Extended_Return_Statement (N)
3738 and then Is_Inherently_Limited_Type (Etype (Expression (N)))
3739 and then Ada_Version >= Ada_05
3740 and then not Debug_Flag_Dot_L
3743 Return_Object_Entity : constant Entity_Id :=
3744 Make_Defining_Identifier (Loc,
3745 New_Internal_Name ('R'));
3746 Obj_Decl : constant Node_Id :=
3747 Make_Object_Declaration (Loc,
3748 Defining_Identifier => Return_Object_Entity,
3749 Object_Definition => Subtype_Ind,
3752 Ext : constant Node_Id := Make_Extended_Return_Statement (Loc,
3753 Return_Object_Declarations => New_List (Obj_Decl));
3754 -- Do not perform this high-level optimization if the result type
3755 -- is an interface because the "this" pointer must be displaced.
3764 -- Here we have a simple return statement that is part of the expansion
3765 -- of an extended return statement (either written by the user, or
3766 -- generated by the above code).
3768 -- Always normalize C/Fortran boolean result. This is not always needed,
3769 -- but it seems a good idea to minimize the passing around of non-
3770 -- normalized values, and in any case this handles the processing of
3771 -- barrier functions for protected types, which turn the condition into
3772 -- a return statement.
3774 if Is_Boolean_Type (Exptyp)
3775 and then Nonzero_Is_True (Exptyp)
3777 Adjust_Condition (Exp);
3778 Adjust_Result_Type (Exp, Exptyp);
3781 -- Do validity check if enabled for returns
3783 if Validity_Checks_On
3784 and then Validity_Check_Returns
3789 -- Check the result expression of a scalar function against the subtype
3790 -- of the function by inserting a conversion. This conversion must
3791 -- eventually be performed for other classes of types, but for now it's
3792 -- only done for scalars.
3795 if Is_Scalar_Type (Exptyp) then
3796 Rewrite (Exp, Convert_To (R_Type, Exp));
3800 -- Deal with returning variable length objects and controlled types
3802 -- Nothing to do if we are returning by reference, or this is not a
3803 -- type that requires special processing (indicated by the fact that
3804 -- it requires a cleanup scope for the secondary stack case).
3806 if Is_Inherently_Limited_Type (Exptyp)
3807 or else Is_Limited_Interface (Exptyp)
3811 elsif not Requires_Transient_Scope (R_Type) then
3813 -- Mutable records with no variable length components are not
3814 -- returned on the sec-stack, so we need to make sure that the
3815 -- backend will only copy back the size of the actual value, and not
3816 -- the maximum size. We create an actual subtype for this purpose.
3819 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
3823 if Has_Discriminants (Ubt)
3824 and then not Is_Constrained (Ubt)
3825 and then not Has_Unchecked_Union (Ubt)
3827 Decl := Build_Actual_Subtype (Ubt, Exp);
3828 Ent := Defining_Identifier (Decl);
3829 Insert_Action (Exp, Decl);
3830 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
3831 Analyze_And_Resolve (Exp);
3835 -- Here if secondary stack is used
3838 -- Make sure that no surrounding block will reclaim the secondary
3839 -- stack on which we are going to put the result. Not only may this
3840 -- introduce secondary stack leaks but worse, if the reclamation is
3841 -- done too early, then the result we are returning may get
3848 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
3849 Set_Sec_Stack_Needed_For_Return (S, True);
3850 S := Enclosing_Dynamic_Scope (S);
3854 -- Optimize the case where the result is a function call. In this
3855 -- case either the result is already on the secondary stack, or is
3856 -- already being returned with the stack pointer depressed and no
3857 -- further processing is required except to set the By_Ref flag to
3858 -- ensure that gigi does not attempt an extra unnecessary copy.
3859 -- (actually not just unnecessary but harmfully wrong in the case
3860 -- of a controlled type, where gigi does not know how to do a copy).
3861 -- To make up for a gcc 2.8.1 deficiency (???), we perform
3862 -- the copy for array types if the constrained status of the
3863 -- target type is different from that of the expression.
3865 if Requires_Transient_Scope (Exptyp)
3867 (not Is_Array_Type (Exptyp)
3868 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
3869 or else CW_Or_Has_Controlled_Part (Utyp))
3870 and then Nkind (Exp) = N_Function_Call
3874 -- Remove side effects from the expression now so that other parts
3875 -- of the expander do not have to reanalyze this node without this
3878 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
3880 -- For controlled types, do the allocation on the secondary stack
3881 -- manually in order to call adjust at the right time:
3883 -- type Anon1 is access R_Type;
3884 -- for Anon1'Storage_pool use ss_pool;
3885 -- Anon2 : anon1 := new R_Type'(expr);
3886 -- return Anon2.all;
3888 -- We do the same for classwide types that are not potentially
3889 -- controlled (by the virtue of restriction No_Finalization) because
3890 -- gigi is not able to properly allocate class-wide types.
3892 elsif CW_Or_Has_Controlled_Part (Utyp) then
3894 Loc : constant Source_Ptr := Sloc (N);
3895 Temp : constant Entity_Id :=
3896 Make_Defining_Identifier (Loc,
3897 Chars => New_Internal_Name ('R'));
3898 Acc_Typ : constant Entity_Id :=
3899 Make_Defining_Identifier (Loc,
3900 Chars => New_Internal_Name ('A'));
3901 Alloc_Node : Node_Id;
3904 Set_Ekind (Acc_Typ, E_Access_Type);
3906 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
3909 Make_Allocator (Loc,
3911 Make_Qualified_Expression (Loc,
3912 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
3913 Expression => Relocate_Node (Exp)));
3915 -- We do not want discriminant checks on the declaration,
3916 -- given that it gets its value from the allocator.
3918 Set_No_Initialization (Alloc_Node);
3920 Insert_List_Before_And_Analyze (N, New_List (
3921 Make_Full_Type_Declaration (Loc,
3922 Defining_Identifier => Acc_Typ,
3924 Make_Access_To_Object_Definition (Loc,
3925 Subtype_Indication => Subtype_Ind)),
3927 Make_Object_Declaration (Loc,
3928 Defining_Identifier => Temp,
3929 Object_Definition => New_Reference_To (Acc_Typ, Loc),
3930 Expression => Alloc_Node)));
3933 Make_Explicit_Dereference (Loc,
3934 Prefix => New_Reference_To (Temp, Loc)));
3936 Analyze_And_Resolve (Exp, R_Type);
3939 -- Otherwise use the gigi mechanism to allocate result on the
3943 Check_Restriction (No_Secondary_Stack, N);
3944 Set_Storage_Pool (N, RTE (RE_SS_Pool));
3946 -- If we are generating code for the VM do not use
3947 -- SS_Allocate since everything is heap-allocated anyway.
3949 if VM_Target = No_VM then
3950 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3955 -- Implement the rules of 6.5(8-10), which require a tag check in the
3956 -- case of a limited tagged return type, and tag reassignment for
3957 -- nonlimited tagged results. These actions are needed when the return
3958 -- type is a specific tagged type and the result expression is a
3959 -- conversion or a formal parameter, because in that case the tag of the
3960 -- expression might differ from the tag of the specific result type.
3962 if Is_Tagged_Type (Utyp)
3963 and then not Is_Class_Wide_Type (Utyp)
3964 and then (Nkind_In (Exp, N_Type_Conversion,
3965 N_Unchecked_Type_Conversion)
3966 or else (Is_Entity_Name (Exp)
3967 and then Ekind (Entity (Exp)) in Formal_Kind))
3969 -- When the return type is limited, perform a check that the
3970 -- tag of the result is the same as the tag of the return type.
3972 if Is_Limited_Type (R_Type) then
3974 Make_Raise_Constraint_Error (Loc,
3978 Make_Selected_Component (Loc,
3979 Prefix => Duplicate_Subexpr (Exp),
3981 New_Reference_To (First_Tag_Component (Utyp), Loc)),
3983 Unchecked_Convert_To (RTE (RE_Tag),
3986 (Access_Disp_Table (Base_Type (Utyp)))),
3988 Reason => CE_Tag_Check_Failed));
3990 -- If the result type is a specific nonlimited tagged type, then we
3991 -- have to ensure that the tag of the result is that of the result
3992 -- type. This is handled by making a copy of the expression in the
3993 -- case where it might have a different tag, namely when the
3994 -- expression is a conversion or a formal parameter. We create a new
3995 -- object of the result type and initialize it from the expression,
3996 -- which will implicitly force the tag to be set appropriately.
4000 Result_Id : constant Entity_Id :=
4001 Make_Defining_Identifier (Loc,
4002 Chars => New_Internal_Name ('R'));
4003 Result_Exp : constant Node_Id :=
4004 New_Reference_To (Result_Id, Loc);
4005 Result_Obj : constant Node_Id :=
4006 Make_Object_Declaration (Loc,
4007 Defining_Identifier => Result_Id,
4008 Object_Definition =>
4009 New_Reference_To (R_Type, Loc),
4010 Constant_Present => True,
4011 Expression => Relocate_Node (Exp));
4014 Set_Assignment_OK (Result_Obj);
4015 Insert_Action (Exp, Result_Obj);
4017 Rewrite (Exp, Result_Exp);
4018 Analyze_And_Resolve (Exp, R_Type);
4022 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
4023 -- a check that the level of the return expression's underlying type
4024 -- is not deeper than the level of the master enclosing the function.
4025 -- Always generate the check when the type of the return expression
4026 -- is class-wide, when it's a type conversion, or when it's a formal
4027 -- parameter. Otherwise, suppress the check in the case where the
4028 -- return expression has a specific type whose level is known not to
4029 -- be statically deeper than the function's result type.
4031 -- Note: accessibility check is skipped in the VM case, since there
4032 -- does not seem to be any practical way to implement this check.
4034 elsif Ada_Version >= Ada_05
4035 and then VM_Target = No_VM
4036 and then Is_Class_Wide_Type (R_Type)
4037 and then not Scope_Suppress (Accessibility_Check)
4039 (Is_Class_Wide_Type (Etype (Exp))
4040 or else Nkind_In (Exp, N_Type_Conversion,
4041 N_Unchecked_Type_Conversion)
4042 or else (Is_Entity_Name (Exp)
4043 and then Ekind (Entity (Exp)) in Formal_Kind)
4044 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
4045 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
4051 -- Ada 2005 (AI-251): In class-wide interface objects we displace
4052 -- "this" to reference the base of the object --- required to get
4053 -- access to the TSD of the object.
4055 if Is_Class_Wide_Type (Etype (Exp))
4056 and then Is_Interface (Etype (Exp))
4057 and then Nkind (Exp) = N_Explicit_Dereference
4060 Make_Explicit_Dereference (Loc,
4061 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4062 Make_Function_Call (Loc,
4063 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
4064 Parameter_Associations => New_List (
4065 Unchecked_Convert_To (RTE (RE_Address),
4066 Duplicate_Subexpr (Prefix (Exp)))))));
4069 Make_Attribute_Reference (Loc,
4070 Prefix => Duplicate_Subexpr (Exp),
4071 Attribute_Name => Name_Tag);
4075 Make_Raise_Program_Error (Loc,
4079 Build_Get_Access_Level (Loc, Tag_Node),
4081 Make_Integer_Literal (Loc,
4082 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
4083 Reason => PE_Accessibility_Check_Failed));
4087 -- If we are returning an object that may not be bit-aligned, then
4088 -- copy the value into a temporary first. This copy may need to expand
4089 -- to a loop of component operations..
4091 if Is_Possibly_Unaligned_Slice (Exp)
4092 or else Is_Possibly_Unaligned_Object (Exp)
4095 Tnn : constant Entity_Id :=
4096 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
4099 Make_Object_Declaration (Loc,
4100 Defining_Identifier => Tnn,
4101 Constant_Present => True,
4102 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4103 Expression => Relocate_Node (Exp)),
4104 Suppress => All_Checks);
4105 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4109 -- Generate call to postcondition checks if they are present
4111 if Ekind (Scope_Id) = E_Function
4112 and then Has_Postconditions (Scope_Id)
4114 -- We are going to reference the returned value twice in this case,
4115 -- once in the call to _Postconditions, and once in the actual return
4116 -- statement, but we can't have side effects happening twice, and in
4117 -- any case for efficiency we don't want to do the computation twice.
4119 -- If the returned expression is an entity name, we don't need to
4120 -- worry since it is efficient and safe to reference it twice, that's
4121 -- also true for literals other than string literals, and for the
4122 -- case of X.all where X is an entity name.
4124 if Is_Entity_Name (Exp)
4125 or else Nkind_In (Exp, N_Character_Literal,
4128 or else (Nkind (Exp) = N_Explicit_Dereference
4129 and then Is_Entity_Name (Prefix (Exp)))
4133 -- Otherwise we are going to need a temporary to capture the value
4137 Tnn : constant Entity_Id :=
4138 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
4141 -- For a complex expression of an elementary type, capture
4142 -- value in the temporary and use it as the reference.
4144 if Is_Elementary_Type (R_Type) then
4146 Make_Object_Declaration (Loc,
4147 Defining_Identifier => Tnn,
4148 Constant_Present => True,
4149 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4150 Expression => Relocate_Node (Exp)),
4151 Suppress => All_Checks);
4153 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4155 -- If we have something we can rename, generate a renaming of
4156 -- the object and replace the expression with a reference
4158 elsif Is_Object_Reference (Exp) then
4160 Make_Object_Renaming_Declaration (Loc,
4161 Defining_Identifier => Tnn,
4162 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
4163 Name => Relocate_Node (Exp)),
4164 Suppress => All_Checks);
4166 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4168 -- Otherwise we have something like a string literal or an
4169 -- aggregate. We could copy the value, but that would be
4170 -- inefficient. Instead we make a reference to the value and
4171 -- capture this reference with a renaming, the expression is
4172 -- then replaced by a dereference of this renaming.
4175 -- For now, copy the value, since the code below does not
4176 -- seem to work correctly ???
4179 Make_Object_Declaration (Loc,
4180 Defining_Identifier => Tnn,
4181 Constant_Present => True,
4182 Object_Definition => New_Occurrence_Of (R_Type, Loc),
4183 Expression => Relocate_Node (Exp)),
4184 Suppress => All_Checks);
4186 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
4188 -- Insert_Action (Exp,
4189 -- Make_Object_Renaming_Declaration (Loc,
4190 -- Defining_Identifier => Tnn,
4191 -- Access_Definition =>
4192 -- Make_Access_Definition (Loc,
4193 -- All_Present => True,
4194 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
4196 -- Make_Reference (Loc,
4197 -- Prefix => Relocate_Node (Exp))),
4198 -- Suppress => All_Checks);
4201 -- Make_Explicit_Dereference (Loc,
4202 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
4207 -- Generate call to _postconditions
4210 Make_Procedure_Call_Statement (Loc,
4211 Name => Make_Identifier (Loc, Name_uPostconditions),
4212 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
4215 -- Ada 2005 (AI-251): If this return statement corresponds with an
4216 -- simple return statement associated with an extended return statement
4217 -- and the type of the returned object is an interface then generate an
4218 -- implicit conversion to force displacement of the "this" pointer.
4220 if Ada_Version >= Ada_05
4221 and then Comes_From_Extended_Return_Statement (N)
4222 and then Nkind (Expression (N)) = N_Identifier
4223 and then Is_Interface (Utyp)
4224 and then Utyp /= Underlying_Type (Exptyp)
4226 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
4227 Analyze_And_Resolve (Exp);
4229 end Expand_Simple_Function_Return;
4231 ------------------------------
4232 -- Make_Tag_Ctrl_Assignment --
4233 ------------------------------
4235 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id is
4236 Loc : constant Source_Ptr := Sloc (N);
4237 L : constant Node_Id := Name (N);
4238 T : constant Entity_Id := Underlying_Type (Etype (L));
4240 Ctrl_Act : constant Boolean := Needs_Finalization (T)
4241 and then not No_Ctrl_Actions (N);
4243 Save_Tag : constant Boolean := Is_Tagged_Type (T)
4244 and then not No_Ctrl_Actions (N)
4245 and then VM_Target = No_VM;
4246 -- Tags are not saved and restored when VM_Target because VM tags are
4247 -- represented implicitly in objects.
4250 Tag_Tmp : Entity_Id;
4252 Prev_Tmp : Entity_Id;
4253 Next_Tmp : Entity_Id;
4259 -- Finalize the target of the assignment when controlled.
4260 -- We have two exceptions here:
4262 -- 1. If we are in an init proc since it is an initialization
4263 -- more than an assignment
4265 -- 2. If the left-hand side is a temporary that was not initialized
4266 -- (or the parent part of a temporary since it is the case in
4267 -- extension aggregates). Such a temporary does not come from
4268 -- source. We must examine the original node for the prefix, because
4269 -- it may be a component of an entry formal, in which case it has
4270 -- been rewritten and does not appear to come from source either.
4272 -- Case of init proc
4274 if not Ctrl_Act then
4277 -- The left hand side is an uninitialized temporary object
4279 elsif Nkind (L) = N_Type_Conversion
4280 and then Is_Entity_Name (Expression (L))
4281 and then Nkind (Parent (Entity (Expression (L))))
4282 = N_Object_Declaration
4283 and then No_Initialization (Parent (Entity (Expression (L))))
4288 Append_List_To (Res,
4290 Ref => Duplicate_Subexpr_No_Checks (L),
4292 With_Detach => New_Reference_To (Standard_False, Loc)));
4295 -- Save the Tag in a local variable Tag_Tmp
4299 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4302 Make_Object_Declaration (Loc,
4303 Defining_Identifier => Tag_Tmp,
4304 Object_Definition => New_Reference_To (RTE (RE_Tag), Loc),
4306 Make_Selected_Component (Loc,
4307 Prefix => Duplicate_Subexpr_No_Checks (L),
4308 Selector_Name => New_Reference_To (First_Tag_Component (T),
4311 -- Otherwise Tag_Tmp not used
4318 if VM_Target /= No_VM then
4320 -- Cannot assign part of the object in a VM context, so instead
4321 -- fallback to the previous mechanism, even though it is not
4322 -- completely correct ???
4324 -- Save the Finalization Pointers in local variables Prev_Tmp and
4325 -- Next_Tmp. For objects with Has_Controlled_Component set, these
4326 -- pointers are in the Record_Controller
4328 Ctrl_Ref := Duplicate_Subexpr (L);
4330 if Has_Controlled_Component (T) then
4332 Make_Selected_Component (Loc,
4335 New_Reference_To (Controller_Component (T), Loc));
4339 Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
4342 Make_Object_Declaration (Loc,
4343 Defining_Identifier => Prev_Tmp,
4345 Object_Definition =>
4346 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
4349 Make_Selected_Component (Loc,
4351 Unchecked_Convert_To (RTE (RE_Finalizable), Ctrl_Ref),
4352 Selector_Name => Make_Identifier (Loc, Name_Prev))));
4355 Make_Defining_Identifier (Loc,
4356 Chars => New_Internal_Name ('C'));
4359 Make_Object_Declaration (Loc,
4360 Defining_Identifier => Next_Tmp,
4362 Object_Definition =>
4363 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc),
4366 Make_Selected_Component (Loc,
4368 Unchecked_Convert_To (RTE (RE_Finalizable),
4369 New_Copy_Tree (Ctrl_Ref)),
4370 Selector_Name => Make_Identifier (Loc, Name_Next))));
4372 -- Do the Assignment
4374 Append_To (Res, Relocate_Node (N));
4377 -- Regular (non VM) processing for controlled types and types with
4378 -- controlled components
4380 -- Variables of such types contain pointers used to chain them in
4381 -- finalization lists, in addition to user data. These pointers
4382 -- are specific to each object of the type, not to the value being
4385 -- Thus they need to be left intact during the assignment. We
4386 -- achieve this by constructing a Storage_Array subtype, and by
4387 -- overlaying objects of this type on the source and target of the
4388 -- assignment. The assignment is then rewritten to assignments of
4389 -- slices of these arrays, copying the user data, and leaving the
4390 -- pointers untouched.
4392 Controlled_Actions : declare
4394 -- A reference to the Prev component of the record controller
4396 First_After_Root : Node_Id := Empty;
4397 -- Index of first byte to be copied (used to skip
4398 -- Root_Controlled in controlled objects).
4400 Last_Before_Hole : Node_Id := Empty;
4401 -- Index of last byte to be copied before outermost record
4404 Hole_Length : Node_Id := Empty;
4405 -- Length of record controller data (Prev and Next pointers)
4407 First_After_Hole : Node_Id := Empty;
4408 -- Index of first byte to be copied after outermost record
4411 Expr, Source_Size : Node_Id;
4412 Source_Actual_Subtype : Entity_Id;
4413 -- Used for computation of the size of the data to be copied
4415 Range_Type : Entity_Id;
4416 Opaque_Type : Entity_Id;
4418 function Build_Slice
4421 Hi : Node_Id) return Node_Id;
4422 -- Build and return a slice of an array of type S overlaid on
4423 -- object Rec, with bounds specified by Lo and Hi. If either
4424 -- bound is empty, a default of S'First (respectively S'Last)
4431 function Build_Slice
4434 Hi : Node_Id) return Node_Id
4439 Opaque : constant Node_Id :=
4440 Unchecked_Convert_To (Opaque_Type,
4441 Make_Attribute_Reference (Loc,
4443 Attribute_Name => Name_Address));
4444 -- Access value designating an opaque storage array of type
4445 -- S overlaid on record Rec.
4448 -- Compute slice bounds using S'First (1) and S'Last as
4449 -- default values when not specified by the caller.
4452 Lo_Bound := Make_Integer_Literal (Loc, 1);
4458 Hi_Bound := Make_Attribute_Reference (Loc,
4459 Prefix => New_Occurrence_Of (Range_Type, Loc),
4460 Attribute_Name => Name_Last);
4465 return Make_Slice (Loc,
4468 Discrete_Range => Make_Range (Loc,
4469 Lo_Bound, Hi_Bound));
4472 -- Start of processing for Controlled_Actions
4475 -- Create a constrained subtype of Storage_Array whose size
4476 -- corresponds to the value being assigned.
4478 -- subtype G is Storage_Offset range
4479 -- 1 .. (Expr'Size + Storage_Unit - 1) / Storage_Unit
4481 Expr := Duplicate_Subexpr_No_Checks (Expression (N));
4483 if Nkind (Expr) = N_Qualified_Expression then
4484 Expr := Expression (Expr);
4487 Source_Actual_Subtype := Etype (Expr);
4489 if Has_Discriminants (Source_Actual_Subtype)
4490 and then not Is_Constrained (Source_Actual_Subtype)
4493 Build_Actual_Subtype (Source_Actual_Subtype, Expr));
4494 Source_Actual_Subtype := Defining_Identifier (Last (Res));
4500 Make_Attribute_Reference (Loc,
4502 New_Occurrence_Of (Source_Actual_Subtype, Loc),
4503 Attribute_Name => Name_Size),
4505 Make_Integer_Literal (Loc,
4506 Intval => System_Storage_Unit - 1));
4509 Make_Op_Divide (Loc,
4510 Left_Opnd => Source_Size,
4512 Make_Integer_Literal (Loc,
4513 Intval => System_Storage_Unit));
4516 Make_Defining_Identifier (Loc,
4517 New_Internal_Name ('G'));
4520 Make_Subtype_Declaration (Loc,
4521 Defining_Identifier => Range_Type,
4522 Subtype_Indication =>
4523 Make_Subtype_Indication (Loc,
4525 New_Reference_To (RTE (RE_Storage_Offset), Loc),
4526 Constraint => Make_Range_Constraint (Loc,
4529 Low_Bound => Make_Integer_Literal (Loc, 1),
4530 High_Bound => Source_Size)))));
4532 -- subtype S is Storage_Array (G)
4535 Make_Subtype_Declaration (Loc,
4536 Defining_Identifier =>
4537 Make_Defining_Identifier (Loc,
4538 New_Internal_Name ('S')),
4539 Subtype_Indication =>
4540 Make_Subtype_Indication (Loc,
4542 New_Reference_To (RTE (RE_Storage_Array), Loc),
4544 Make_Index_Or_Discriminant_Constraint (Loc,
4546 New_List (New_Reference_To (Range_Type, Loc))))));
4548 -- type A is access S
4551 Make_Defining_Identifier (Loc,
4552 Chars => New_Internal_Name ('A'));
4555 Make_Full_Type_Declaration (Loc,
4556 Defining_Identifier => Opaque_Type,
4558 Make_Access_To_Object_Definition (Loc,
4559 Subtype_Indication =>
4561 Defining_Identifier (Last (Res)), Loc))));
4563 -- Generate appropriate slice assignments
4565 First_After_Root := Make_Integer_Literal (Loc, 1);
4567 -- For the case of a controlled object, skip the
4568 -- Root_Controlled part.
4570 if Is_Controlled (T) then
4574 Make_Op_Divide (Loc,
4575 Make_Attribute_Reference (Loc,
4577 New_Occurrence_Of (RTE (RE_Root_Controlled), Loc),
4578 Attribute_Name => Name_Size),
4579 Make_Integer_Literal (Loc, System_Storage_Unit)));
4582 -- For the case of a record with controlled components, skip
4583 -- the Prev and Next components of the record controller.
4584 -- These components constitute a 'hole' in the middle of the
4585 -- data to be copied.
4587 if Has_Controlled_Component (T) then
4589 Make_Selected_Component (Loc,
4591 Make_Selected_Component (Loc,
4592 Prefix => Duplicate_Subexpr_No_Checks (L),
4594 New_Reference_To (Controller_Component (T), Loc)),
4595 Selector_Name => Make_Identifier (Loc, Name_Prev));
4597 -- Last index before hole: determined by position of
4598 -- the _Controller.Prev component.
4601 Make_Defining_Identifier (Loc,
4602 New_Internal_Name ('L'));
4605 Make_Object_Declaration (Loc,
4606 Defining_Identifier => Last_Before_Hole,
4607 Object_Definition => New_Occurrence_Of (
4608 RTE (RE_Storage_Offset), Loc),
4609 Constant_Present => True,
4610 Expression => Make_Op_Add (Loc,
4611 Make_Attribute_Reference (Loc,
4613 Attribute_Name => Name_Position),
4614 Make_Attribute_Reference (Loc,
4615 Prefix => New_Copy_Tree (Prefix (Prev_Ref)),
4616 Attribute_Name => Name_Position))));
4618 -- Hole length: size of the Prev and Next components
4621 Make_Op_Multiply (Loc,
4622 Left_Opnd => Make_Integer_Literal (Loc, Uint_2),
4624 Make_Op_Divide (Loc,
4626 Make_Attribute_Reference (Loc,
4627 Prefix => New_Copy_Tree (Prev_Ref),
4628 Attribute_Name => Name_Size),
4630 Make_Integer_Literal (Loc,
4631 Intval => System_Storage_Unit)));
4633 -- First index after hole
4636 Make_Defining_Identifier (Loc,
4637 New_Internal_Name ('F'));
4640 Make_Object_Declaration (Loc,
4641 Defining_Identifier => First_After_Hole,
4642 Object_Definition => New_Occurrence_Of (
4643 RTE (RE_Storage_Offset), Loc),
4644 Constant_Present => True,
4650 New_Occurrence_Of (Last_Before_Hole, Loc),
4651 Right_Opnd => Hole_Length),
4652 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4655 New_Occurrence_Of (Last_Before_Hole, Loc);
4657 New_Occurrence_Of (First_After_Hole, Loc);
4660 -- Assign the first slice (possibly skipping Root_Controlled,
4661 -- up to the beginning of the record controller if present,
4662 -- up to the end of the object if not).
4664 Append_To (Res, Make_Assignment_Statement (Loc,
4665 Name => Build_Slice (
4666 Rec => Duplicate_Subexpr_No_Checks (L),
4667 Lo => First_After_Root,
4668 Hi => Last_Before_Hole),
4670 Expression => Build_Slice (
4671 Rec => Expression (N),
4672 Lo => First_After_Root,
4673 Hi => New_Copy_Tree (Last_Before_Hole))));
4675 if Present (First_After_Hole) then
4677 -- If a record controller is present, copy the second slice,
4678 -- from right after the _Controller.Next component up to the
4679 -- end of the object.
4681 Append_To (Res, Make_Assignment_Statement (Loc,
4682 Name => Build_Slice (
4683 Rec => Duplicate_Subexpr_No_Checks (L),
4684 Lo => First_After_Hole,
4686 Expression => Build_Slice (
4687 Rec => Duplicate_Subexpr_No_Checks (Expression (N)),
4688 Lo => New_Copy_Tree (First_After_Hole),
4691 end Controlled_Actions;
4695 Append_To (Res, Relocate_Node (N));
4702 Make_Assignment_Statement (Loc,
4704 Make_Selected_Component (Loc,
4705 Prefix => Duplicate_Subexpr_No_Checks (L),
4706 Selector_Name => New_Reference_To (First_Tag_Component (T),
4708 Expression => New_Reference_To (Tag_Tmp, Loc)));
4712 if VM_Target /= No_VM then
4713 -- Restore the finalization pointers
4716 Make_Assignment_Statement (Loc,
4718 Make_Selected_Component (Loc,
4720 Unchecked_Convert_To (RTE (RE_Finalizable),
4721 New_Copy_Tree (Ctrl_Ref)),
4722 Selector_Name => Make_Identifier (Loc, Name_Prev)),
4723 Expression => New_Reference_To (Prev_Tmp, Loc)));
4726 Make_Assignment_Statement (Loc,
4728 Make_Selected_Component (Loc,
4730 Unchecked_Convert_To (RTE (RE_Finalizable),
4731 New_Copy_Tree (Ctrl_Ref)),
4732 Selector_Name => Make_Identifier (Loc, Name_Next)),
4733 Expression => New_Reference_To (Next_Tmp, Loc)));
4736 -- Adjust the target after the assignment when controlled (not in the
4737 -- init proc since it is an initialization more than an assignment).
4739 Append_List_To (Res,
4741 Ref => Duplicate_Subexpr_Move_Checks (L),
4743 Flist_Ref => New_Reference_To (RTE (RE_Global_Final_List), Loc),
4744 With_Attach => Make_Integer_Literal (Loc, 0)));
4750 -- Could use comment here ???
4752 when RE_Not_Available =>
4754 end Make_Tag_Ctrl_Assignment;