1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Aggr; use Exp_Aggr;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch11; use Exp_Ch11;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Pakd; use Exp_Pakd;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Hostparm; use Hostparm;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sinfo; use Sinfo;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Res; use Sem_Res;
55 with Sem_Util; use Sem_Util;
56 with Snames; use Snames;
57 with Stand; use Stand;
58 with Stringt; use Stringt;
59 with 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 Enable_New_Return_Processing : constant Boolean := True;
67 -- ??? This flag is temporary. False causes the compiler to use the old
68 -- version of Analyze_Return_Statement; True, the new version, which does
69 -- not yet work. We probably want this to match the corresponding thing
72 function Change_Of_Representation (N : Node_Id) return Boolean;
73 -- Determine if the right hand side of the assignment N is a type
74 -- conversion which requires a change of representation. Called
75 -- only for the array and record cases.
77 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id);
78 -- N is an assignment which assigns an array value. This routine process
79 -- the various special cases and checks required for such assignments,
80 -- including change of representation. Rhs is normally simply the right
81 -- hand side of the assignment, except that if the right hand side is
82 -- a type conversion or a qualified expression, then the Rhs is the
83 -- actual expression inside any such type conversions or qualifications.
85 function Expand_Assign_Array_Loop
92 Rev : Boolean) return Node_Id;
93 -- N is an assignment statement which assigns an array value. This routine
94 -- expands the assignment into a loop (or nested loops for the case of a
95 -- multi-dimensional array) to do the assignment component by component.
96 -- Larray and Rarray are the entities of the actual arrays on the left
97 -- hand and right hand sides. L_Type and R_Type are the types of these
98 -- arrays (which may not be the same, due to either sliding, or to a
99 -- change of representation case). Ndim is the number of dimensions and
100 -- the parameter Rev indicates if the loops run normally (Rev = False),
101 -- or reversed (Rev = True). The value returned is the constructed
102 -- loop statement. Auxiliary declarations are inserted before node N
103 -- using the standard Insert_Actions mechanism.
105 procedure Expand_Assign_Record (N : Node_Id);
106 -- N is an assignment of a non-tagged record value. This routine handles
107 -- the case where the assignment must be made component by component,
108 -- either because the target is not byte aligned, or there is a change
109 -- of representation.
111 procedure Expand_Non_Function_Return (N : Node_Id);
112 -- Called by Expand_Simple_Return in case we're returning from a procedure
113 -- body, entry body, accept statement, or extended returns statement.
114 -- Note that all non-function returns are simple return statements.
116 procedure Expand_Simple_Function_Return (N : Node_Id);
117 -- Expand simple return from function. Called by Expand_Simple_Return in
118 -- case we're returning from a function body.
120 procedure Expand_Simple_Return (N : Node_Id);
121 -- Expansion for simple return statements. Calls either
122 -- Expand_Simple_Function_Return or Expand_Non_Function_Return.
124 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id;
125 -- Generate the necessary code for controlled and tagged assignment,
126 -- that is to say, finalization of the target before, adjustement of
127 -- the target after and save and restore of the tag and finalization
128 -- pointers which are not 'part of the value' and must not be changed
129 -- upon assignment. N is the original Assignment node.
131 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean;
132 -- This function is used in processing the assignment of a record or
133 -- indexed component. The argument N is either the left hand or right
134 -- hand side of an assignment, and this function determines if there
135 -- is a record component reference where the record may be bit aligned
136 -- in a manner that causes trouble for the back end (see description
137 -- of Exp_Util.Component_May_Be_Bit_Aligned for further details).
139 ------------------------------
140 -- Change_Of_Representation --
141 ------------------------------
143 function Change_Of_Representation (N : Node_Id) return Boolean is
144 Rhs : constant Node_Id := Expression (N);
147 Nkind (Rhs) = N_Type_Conversion
149 not Same_Representation (Etype (Rhs), Etype (Expression (Rhs)));
150 end Change_Of_Representation;
152 -------------------------
153 -- Expand_Assign_Array --
154 -------------------------
156 -- There are two issues here. First, do we let Gigi do a block move, or
157 -- do we expand out into a loop? Second, we need to set the two flags
158 -- Forwards_OK and Backwards_OK which show whether the block move (or
159 -- corresponding loops) can be legitimately done in a forwards (low to
160 -- high) or backwards (high to low) manner.
162 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id) is
163 Loc : constant Source_Ptr := Sloc (N);
165 Lhs : constant Node_Id := Name (N);
167 Act_Lhs : constant Node_Id := Get_Referenced_Object (Lhs);
168 Act_Rhs : Node_Id := Get_Referenced_Object (Rhs);
170 L_Type : constant Entity_Id :=
171 Underlying_Type (Get_Actual_Subtype (Act_Lhs));
172 R_Type : Entity_Id :=
173 Underlying_Type (Get_Actual_Subtype (Act_Rhs));
175 L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice;
176 R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice;
178 Crep : constant Boolean := Change_Of_Representation (N);
183 Ndim : constant Pos := Number_Dimensions (L_Type);
185 Loop_Required : Boolean := False;
186 -- This switch is set to True if the array move must be done using
187 -- an explicit front end generated loop.
189 procedure Apply_Dereference (Arg : in out Node_Id);
190 -- If the argument is an access to an array, and the assignment is
191 -- converted into a procedure call, apply explicit dereference.
193 function Has_Address_Clause (Exp : Node_Id) return Boolean;
194 -- Test if Exp is a reference to an array whose declaration has
195 -- an address clause, or it is a slice of such an array.
197 function Is_Formal_Array (Exp : Node_Id) return Boolean;
198 -- Test if Exp is a reference to an array which is either a formal
199 -- parameter or a slice of a formal parameter. These are the cases
200 -- where hidden aliasing can occur.
202 function Is_Non_Local_Array (Exp : Node_Id) return Boolean;
203 -- Determine if Exp is a reference to an array variable which is other
204 -- than an object defined in the current scope, or a slice of such
205 -- an object. Such objects can be aliased to parameters (unlike local
206 -- array references).
208 -----------------------
209 -- Apply_Dereference --
210 -----------------------
212 procedure Apply_Dereference (Arg : in out Node_Id) is
213 Typ : constant Entity_Id := Etype (Arg);
215 if Is_Access_Type (Typ) then
216 Rewrite (Arg, Make_Explicit_Dereference (Loc,
217 Prefix => Relocate_Node (Arg)));
218 Analyze_And_Resolve (Arg, Designated_Type (Typ));
220 end Apply_Dereference;
222 ------------------------
223 -- Has_Address_Clause --
224 ------------------------
226 function Has_Address_Clause (Exp : Node_Id) return Boolean is
229 (Is_Entity_Name (Exp) and then
230 Present (Address_Clause (Entity (Exp))))
232 (Nkind (Exp) = N_Slice and then Has_Address_Clause (Prefix (Exp)));
233 end Has_Address_Clause;
235 ---------------------
236 -- Is_Formal_Array --
237 ---------------------
239 function Is_Formal_Array (Exp : Node_Id) return Boolean is
242 (Is_Entity_Name (Exp) and then Is_Formal (Entity (Exp)))
244 (Nkind (Exp) = N_Slice and then Is_Formal_Array (Prefix (Exp)));
247 ------------------------
248 -- Is_Non_Local_Array --
249 ------------------------
251 function Is_Non_Local_Array (Exp : Node_Id) return Boolean is
253 return (Is_Entity_Name (Exp)
254 and then Scope (Entity (Exp)) /= Current_Scope)
255 or else (Nkind (Exp) = N_Slice
256 and then Is_Non_Local_Array (Prefix (Exp)));
257 end Is_Non_Local_Array;
259 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
261 Lhs_Formal : constant Boolean := Is_Formal_Array (Act_Lhs);
262 Rhs_Formal : constant Boolean := Is_Formal_Array (Act_Rhs);
264 Lhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Lhs);
265 Rhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Rhs);
267 -- Start of processing for Expand_Assign_Array
270 -- Deal with length check, note that the length check is done with
271 -- respect to the right hand side as given, not a possible underlying
272 -- renamed object, since this would generate incorrect extra checks.
274 Apply_Length_Check (Rhs, L_Type);
276 -- We start by assuming that the move can be done in either
277 -- direction, i.e. that the two sides are completely disjoint.
279 Set_Forwards_OK (N, True);
280 Set_Backwards_OK (N, True);
282 -- Normally it is only the slice case that can lead to overlap,
283 -- and explicit checks for slices are made below. But there is
284 -- one case where the slice can be implicit and invisible to us
285 -- and that is the case where we have a one dimensional array,
286 -- and either both operands are parameters, or one is a parameter
287 -- and the other is a global variable. In this case the parameter
288 -- could be a slice that overlaps with the other parameter.
290 -- Check for the case of slices requiring an explicit loop. Normally
291 -- it is only the explicit slice cases that bother us, but in the
292 -- case of one dimensional arrays, parameters can be slices that
293 -- are passed by reference, so we can have aliasing for assignments
294 -- from one parameter to another, or assignments between parameters
295 -- and nonlocal variables. However, if the array subtype is a
296 -- constrained first subtype in the parameter case, then we don't
297 -- have to worry about overlap, since slice assignments aren't
298 -- possible (other than for a slice denoting the whole array).
300 -- Note: overlap is never possible if there is a change of
301 -- representation, so we can exclude this case.
306 ((Lhs_Formal and Rhs_Formal)
308 (Lhs_Formal and Rhs_Non_Local_Var)
310 (Rhs_Formal and Lhs_Non_Local_Var))
312 (not Is_Constrained (Etype (Lhs))
313 or else not Is_First_Subtype (Etype (Lhs)))
315 -- In the case of compiling for the Java Virtual Machine,
316 -- slices are always passed by making a copy, so we don't
317 -- have to worry about overlap. We also want to prevent
318 -- generation of "<" comparisons for array addresses,
319 -- since that's a meaningless operation on the JVM.
323 Set_Forwards_OK (N, False);
324 Set_Backwards_OK (N, False);
326 -- Note: the bit-packed case is not worrisome here, since if
327 -- we have a slice passed as a parameter, it is always aligned
328 -- on a byte boundary, and if there are no explicit slices, the
329 -- assignment can be performed directly.
332 -- We certainly must use a loop for change of representation
333 -- and also we use the operand of the conversion on the right
334 -- hand side as the effective right hand side (the component
335 -- types must match in this situation).
338 Act_Rhs := Get_Referenced_Object (Rhs);
339 R_Type := Get_Actual_Subtype (Act_Rhs);
340 Loop_Required := True;
342 -- We require a loop if the left side is possibly bit unaligned
344 elsif Possible_Bit_Aligned_Component (Lhs)
346 Possible_Bit_Aligned_Component (Rhs)
348 Loop_Required := True;
350 -- Arrays with controlled components are expanded into a loop
351 -- to force calls to adjust at the component level.
353 elsif Has_Controlled_Component (L_Type) then
354 Loop_Required := True;
356 -- If object is atomic, we cannot tolerate a loop
358 elsif Is_Atomic_Object (Act_Lhs)
360 Is_Atomic_Object (Act_Rhs)
364 -- Loop is required if we have atomic components since we have to
365 -- be sure to do any accesses on an element by element basis.
367 elsif Has_Atomic_Components (L_Type)
368 or else Has_Atomic_Components (R_Type)
369 or else Is_Atomic (Component_Type (L_Type))
370 or else Is_Atomic (Component_Type (R_Type))
372 Loop_Required := True;
374 -- Case where no slice is involved
376 elsif not L_Slice and not R_Slice then
378 -- The following code deals with the case of unconstrained bit
379 -- packed arrays. The problem is that the template for such
380 -- arrays contains the bounds of the actual source level array,
382 -- But the copy of an entire array requires the bounds of the
383 -- underlying array. It would be nice if the back end could take
384 -- care of this, but right now it does not know how, so if we
385 -- have such a type, then we expand out into a loop, which is
386 -- inefficient but works correctly. If we don't do this, we
387 -- get the wrong length computed for the array to be moved.
388 -- The two cases we need to worry about are:
390 -- Explicit deference of an unconstrained packed array type as
391 -- in the following example:
394 -- type BITS is array(INTEGER range <>) of BOOLEAN;
395 -- pragma PACK(BITS);
396 -- type A is access BITS;
399 -- P1 := new BITS (1 .. 65_535);
400 -- P2 := new BITS (1 .. 65_535);
404 -- A formal parameter reference with an unconstrained bit
405 -- array type is the other case we need to worry about (here
406 -- we assume the same BITS type declared above):
408 -- procedure Write_All (File : out BITS; Contents : BITS);
410 -- File.Storage := Contents;
413 -- We expand to a loop in either of these two cases
415 -- Question for future thought. Another potentially more efficient
416 -- approach would be to create the actual subtype, and then do an
417 -- unchecked conversion to this actual subtype ???
419 Check_Unconstrained_Bit_Packed_Array : declare
421 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean;
422 -- Function to perform required test for the first case,
423 -- above (dereference of an unconstrained bit packed array)
425 -----------------------
426 -- Is_UBPA_Reference --
427 -----------------------
429 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean is
430 Typ : constant Entity_Id := Underlying_Type (Etype (Opnd));
432 Des_Type : Entity_Id;
435 if Present (Packed_Array_Type (Typ))
436 and then Is_Array_Type (Packed_Array_Type (Typ))
437 and then not Is_Constrained (Packed_Array_Type (Typ))
441 elsif Nkind (Opnd) = N_Explicit_Dereference then
442 P_Type := Underlying_Type (Etype (Prefix (Opnd)));
444 if not Is_Access_Type (P_Type) then
448 Des_Type := Designated_Type (P_Type);
450 Is_Bit_Packed_Array (Des_Type)
451 and then not Is_Constrained (Des_Type);
457 end Is_UBPA_Reference;
459 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
462 if Is_UBPA_Reference (Lhs)
464 Is_UBPA_Reference (Rhs)
466 Loop_Required := True;
468 -- Here if we do not have the case of a reference to a bit
469 -- packed unconstrained array case. In this case gigi can
470 -- most certainly handle the assignment if a forwards move
473 -- (could it handle the backwards case also???)
475 elsif Forwards_OK (N) then
478 end Check_Unconstrained_Bit_Packed_Array;
480 -- The back end can always handle the assignment if the right side is a
481 -- string literal (note that overlap is definitely impossible in this
482 -- case). If the type is packed, a string literal is always converted
483 -- into aggregate, except in the case of a null slice, for which no
484 -- aggregate can be written. In that case, rewrite the assignment as a
485 -- null statement, a length check has already been emitted to verify
486 -- that the range of the left-hand side is empty.
488 -- Note that this code is not executed if we had an assignment of
489 -- a string literal to a non-bit aligned component of a record, a
490 -- case which cannot be handled by the backend
492 elsif Nkind (Rhs) = N_String_Literal then
493 if String_Length (Strval (Rhs)) = 0
494 and then Is_Bit_Packed_Array (L_Type)
496 Rewrite (N, Make_Null_Statement (Loc));
502 -- If either operand is bit packed, then we need a loop, since we
503 -- can't be sure that the slice is byte aligned. Similarly, if either
504 -- operand is a possibly unaligned slice, then we need a loop (since
505 -- the back end cannot handle unaligned slices).
507 elsif Is_Bit_Packed_Array (L_Type)
508 or else Is_Bit_Packed_Array (R_Type)
509 or else Is_Possibly_Unaligned_Slice (Lhs)
510 or else Is_Possibly_Unaligned_Slice (Rhs)
512 Loop_Required := True;
514 -- If we are not bit-packed, and we have only one slice, then no
515 -- overlap is possible except in the parameter case, so we can let
516 -- the back end handle things.
518 elsif not (L_Slice and R_Slice) then
519 if Forwards_OK (N) then
524 -- If the right-hand side is a string literal, introduce a temporary
525 -- for it, for use in the generated loop that will follow.
527 if Nkind (Rhs) = N_String_Literal then
529 Temp : constant Entity_Id :=
530 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
535 Make_Object_Declaration (Loc,
536 Defining_Identifier => Temp,
537 Object_Definition => New_Occurrence_Of (L_Type, Loc),
538 Expression => Relocate_Node (Rhs));
540 Insert_Action (N, Decl);
541 Rewrite (Rhs, New_Occurrence_Of (Temp, Loc));
542 R_Type := Etype (Temp);
546 -- Come here to complete the analysis
548 -- Loop_Required: Set to True if we know that a loop is required
549 -- regardless of overlap considerations.
551 -- Forwards_OK: Set to False if we already know that a forwards
552 -- move is not safe, else set to True.
554 -- Backwards_OK: Set to False if we already know that a backwards
555 -- move is not safe, else set to True
557 -- Our task at this stage is to complete the overlap analysis, which
558 -- can result in possibly setting Forwards_OK or Backwards_OK to
559 -- False, and then generating the final code, either by deciding
560 -- that it is OK after all to let Gigi handle it, or by generating
561 -- appropriate code in the front end.
564 L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
565 R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
567 Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
568 Left_Hi : constant Node_Id := Type_High_Bound (L_Index_Typ);
569 Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
570 Right_Hi : constant Node_Id := Type_High_Bound (R_Index_Typ);
572 Act_L_Array : Node_Id;
573 Act_R_Array : Node_Id;
579 Cresult : Compare_Result;
582 -- Get the expressions for the arrays. If we are dealing with a
583 -- private type, then convert to the underlying type. We can do
584 -- direct assignments to an array that is a private type, but
585 -- we cannot assign to elements of the array without this extra
586 -- unchecked conversion.
588 if Nkind (Act_Lhs) = N_Slice then
589 Larray := Prefix (Act_Lhs);
593 if Is_Private_Type (Etype (Larray)) then
596 (Underlying_Type (Etype (Larray)), Larray);
600 if Nkind (Act_Rhs) = N_Slice then
601 Rarray := Prefix (Act_Rhs);
605 if Is_Private_Type (Etype (Rarray)) then
608 (Underlying_Type (Etype (Rarray)), Rarray);
612 -- If both sides are slices, we must figure out whether
613 -- it is safe to do the move in one direction or the other
614 -- It is always safe if there is a change of representation
615 -- since obviously two arrays with different representations
616 -- cannot possibly overlap.
618 if (not Crep) and L_Slice and R_Slice then
619 Act_L_Array := Get_Referenced_Object (Prefix (Act_Lhs));
620 Act_R_Array := Get_Referenced_Object (Prefix (Act_Rhs));
622 -- If both left and right hand arrays are entity names, and
623 -- refer to different entities, then we know that the move
624 -- is safe (the two storage areas are completely disjoint).
626 if Is_Entity_Name (Act_L_Array)
627 and then Is_Entity_Name (Act_R_Array)
628 and then Entity (Act_L_Array) /= Entity (Act_R_Array)
632 -- Otherwise, we assume the worst, which is that the two
633 -- arrays are the same array. There is no need to check if
634 -- we know that is the case, because if we don't know it,
635 -- we still have to assume it!
637 -- Generally if the same array is involved, then we have
638 -- an overlapping case. We will have to really assume the
639 -- worst (i.e. set neither of the OK flags) unless we can
640 -- determine the lower or upper bounds at compile time and
644 Cresult := Compile_Time_Compare (Left_Lo, Right_Lo);
646 if Cresult = Unknown then
647 Cresult := Compile_Time_Compare (Left_Hi, Right_Hi);
651 when LT | LE | EQ => Set_Backwards_OK (N, False);
652 when GT | GE => Set_Forwards_OK (N, False);
653 when NE | Unknown => Set_Backwards_OK (N, False);
654 Set_Forwards_OK (N, False);
659 -- If after that analysis, Forwards_OK is still True, and
660 -- Loop_Required is False, meaning that we have not discovered
661 -- some non-overlap reason for requiring a loop, then we can
662 -- still let gigi handle it.
664 if not Loop_Required then
665 if Forwards_OK (N) then
669 -- Here is where a memmove would be appropriate ???
673 -- At this stage we have to generate an explicit loop, and
674 -- we have the following cases:
676 -- Forwards_OK = True
678 -- Rnn : right_index := right_index'First;
679 -- for Lnn in left-index loop
680 -- left (Lnn) := right (Rnn);
681 -- Rnn := right_index'Succ (Rnn);
684 -- Note: the above code MUST be analyzed with checks off,
685 -- because otherwise the Succ could overflow. But in any
686 -- case this is more efficient!
688 -- Forwards_OK = False, Backwards_OK = True
690 -- Rnn : right_index := right_index'Last;
691 -- for Lnn in reverse left-index loop
692 -- left (Lnn) := right (Rnn);
693 -- Rnn := right_index'Pred (Rnn);
696 -- Note: the above code MUST be analyzed with checks off,
697 -- because otherwise the Pred could overflow. But in any
698 -- case this is more efficient!
700 -- Forwards_OK = Backwards_OK = False
702 -- This only happens if we have the same array on each side. It is
703 -- possible to create situations using overlays that violate this,
704 -- but we simply do not promise to get this "right" in this case.
706 -- There are two possible subcases. If the No_Implicit_Conditionals
707 -- restriction is set, then we generate the following code:
710 -- T : constant <operand-type> := rhs;
715 -- If implicit conditionals are permitted, then we generate:
717 -- if Left_Lo <= Right_Lo then
718 -- <code for Forwards_OK = True above>
720 -- <code for Backwards_OK = True above>
723 -- Cases where either Forwards_OK or Backwards_OK is true
725 if Forwards_OK (N) or else Backwards_OK (N) then
726 if Controlled_Type (Component_Type (L_Type))
727 and then Base_Type (L_Type) = Base_Type (R_Type)
729 and then not No_Ctrl_Actions (N)
732 Proc : constant Entity_Id :=
733 TSS (Base_Type (L_Type), TSS_Slice_Assign);
737 Apply_Dereference (Larray);
738 Apply_Dereference (Rarray);
739 Actuals := New_List (
740 Duplicate_Subexpr (Larray, Name_Req => True),
741 Duplicate_Subexpr (Rarray, Name_Req => True),
742 Duplicate_Subexpr (Left_Lo, Name_Req => True),
743 Duplicate_Subexpr (Left_Hi, Name_Req => True),
744 Duplicate_Subexpr (Right_Lo, Name_Req => True),
745 Duplicate_Subexpr (Right_Hi, Name_Req => True));
749 Boolean_Literals (not Forwards_OK (N)), Loc));
752 Make_Procedure_Call_Statement (Loc,
753 Name => New_Reference_To (Proc, Loc),
754 Parameter_Associations => Actuals));
759 Expand_Assign_Array_Loop
760 (N, Larray, Rarray, L_Type, R_Type, Ndim,
761 Rev => not Forwards_OK (N)));
764 -- Case of both are false with No_Implicit_Conditionals
766 elsif Restriction_Active (No_Implicit_Conditionals) then
768 T : constant Entity_Id :=
769 Make_Defining_Identifier (Loc, Chars => Name_T);
773 Make_Block_Statement (Loc,
774 Declarations => New_List (
775 Make_Object_Declaration (Loc,
776 Defining_Identifier => T,
777 Constant_Present => True,
779 New_Occurrence_Of (Etype (Rhs), Loc),
780 Expression => Relocate_Node (Rhs))),
782 Handled_Statement_Sequence =>
783 Make_Handled_Sequence_Of_Statements (Loc,
784 Statements => New_List (
785 Make_Assignment_Statement (Loc,
786 Name => Relocate_Node (Lhs),
787 Expression => New_Occurrence_Of (T, Loc))))));
790 -- Case of both are false with implicit conditionals allowed
793 -- Before we generate this code, we must ensure that the
794 -- left and right side array types are defined. They may
795 -- be itypes, and we cannot let them be defined inside the
796 -- if, since the first use in the then may not be executed.
798 Ensure_Defined (L_Type, N);
799 Ensure_Defined (R_Type, N);
801 -- We normally compare addresses to find out which way round
802 -- to do the loop, since this is realiable, and handles the
803 -- cases of parameters, conversions etc. But we can't do that
804 -- in the bit packed case or the Java VM case, because addresses
807 if not Is_Bit_Packed_Array (L_Type) and then not Java_VM then
811 Unchecked_Convert_To (RTE (RE_Integer_Address),
812 Make_Attribute_Reference (Loc,
814 Make_Indexed_Component (Loc,
816 Duplicate_Subexpr_Move_Checks (Larray, True),
817 Expressions => New_List (
818 Make_Attribute_Reference (Loc,
822 Attribute_Name => Name_First))),
823 Attribute_Name => Name_Address)),
826 Unchecked_Convert_To (RTE (RE_Integer_Address),
827 Make_Attribute_Reference (Loc,
829 Make_Indexed_Component (Loc,
831 Duplicate_Subexpr_Move_Checks (Rarray, True),
832 Expressions => New_List (
833 Make_Attribute_Reference (Loc,
837 Attribute_Name => Name_First))),
838 Attribute_Name => Name_Address)));
840 -- For the bit packed and Java VM cases we use the bounds.
841 -- That's OK, because we don't have to worry about parameters,
842 -- since they cannot cause overlap. Perhaps we should worry
843 -- about weird slice conversions ???
846 -- Copy the bounds and reset the Analyzed flag, because the
847 -- bounds of the index type itself may be universal, and must
848 -- must be reaanalyzed to acquire the proper type for Gigi.
850 Cleft_Lo := New_Copy_Tree (Left_Lo);
851 Cright_Lo := New_Copy_Tree (Right_Lo);
852 Set_Analyzed (Cleft_Lo, False);
853 Set_Analyzed (Cright_Lo, False);
857 Left_Opnd => Cleft_Lo,
858 Right_Opnd => Cright_Lo);
861 if Controlled_Type (Component_Type (L_Type))
862 and then Base_Type (L_Type) = Base_Type (R_Type)
864 and then not No_Ctrl_Actions (N)
867 -- Call TSS procedure for array assignment, passing the
868 -- the explicit bounds of right and left hand sides.
871 Proc : constant Node_Id :=
872 TSS (Base_Type (L_Type), TSS_Slice_Assign);
876 Apply_Dereference (Larray);
877 Apply_Dereference (Rarray);
878 Actuals := New_List (
879 Duplicate_Subexpr (Larray, Name_Req => True),
880 Duplicate_Subexpr (Rarray, Name_Req => True),
881 Duplicate_Subexpr (Left_Lo, Name_Req => True),
882 Duplicate_Subexpr (Left_Hi, Name_Req => True),
883 Duplicate_Subexpr (Right_Lo, Name_Req => True),
884 Duplicate_Subexpr (Right_Hi, Name_Req => True));
888 Right_Opnd => Condition));
891 Make_Procedure_Call_Statement (Loc,
892 Name => New_Reference_To (Proc, Loc),
893 Parameter_Associations => Actuals));
898 Make_Implicit_If_Statement (N,
899 Condition => Condition,
901 Then_Statements => New_List (
902 Expand_Assign_Array_Loop
903 (N, Larray, Rarray, L_Type, R_Type, Ndim,
906 Else_Statements => New_List (
907 Expand_Assign_Array_Loop
908 (N, Larray, Rarray, L_Type, R_Type, Ndim,
913 Analyze (N, Suppress => All_Checks);
917 when RE_Not_Available =>
919 end Expand_Assign_Array;
921 ------------------------------
922 -- Expand_Assign_Array_Loop --
923 ------------------------------
925 -- The following is an example of the loop generated for the case of
926 -- a two-dimensional array:
931 -- for L1b in 1 .. 100 loop
935 -- for L3b in 1 .. 100 loop
936 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
937 -- R4b := Tm1X2'succ(R4b);
940 -- R2b := Tm1X1'succ(R2b);
944 -- Here Rev is False, and Tm1Xn are the subscript types for the right
945 -- hand side. The declarations of R2b and R4b are inserted before the
946 -- original assignment statement.
948 function Expand_Assign_Array_Loop
955 Rev : Boolean) return Node_Id
957 Loc : constant Source_Ptr := Sloc (N);
959 Lnn : array (1 .. Ndim) of Entity_Id;
960 Rnn : array (1 .. Ndim) of Entity_Id;
961 -- Entities used as subscripts on left and right sides
963 L_Index_Type : array (1 .. Ndim) of Entity_Id;
964 R_Index_Type : array (1 .. Ndim) of Entity_Id;
965 -- Left and right index types
977 F_Or_L := Name_First;
981 -- Setup index types and subscript entities
988 L_Index := First_Index (L_Type);
989 R_Index := First_Index (R_Type);
991 for J in 1 .. Ndim loop
993 Make_Defining_Identifier (Loc,
994 Chars => New_Internal_Name ('L'));
997 Make_Defining_Identifier (Loc,
998 Chars => New_Internal_Name ('R'));
1000 L_Index_Type (J) := Etype (L_Index);
1001 R_Index_Type (J) := Etype (R_Index);
1003 Next_Index (L_Index);
1004 Next_Index (R_Index);
1008 -- Now construct the assignment statement
1011 ExprL : constant List_Id := New_List;
1012 ExprR : constant List_Id := New_List;
1015 for J in 1 .. Ndim loop
1016 Append_To (ExprL, New_Occurrence_Of (Lnn (J), Loc));
1017 Append_To (ExprR, New_Occurrence_Of (Rnn (J), Loc));
1021 Make_Assignment_Statement (Loc,
1023 Make_Indexed_Component (Loc,
1024 Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
1025 Expressions => ExprL),
1027 Make_Indexed_Component (Loc,
1028 Prefix => Duplicate_Subexpr (Rarray, Name_Req => True),
1029 Expressions => ExprR));
1031 -- We set assignment OK, since there are some cases, e.g. in object
1032 -- declarations, where we are actually assigning into a constant.
1033 -- If there really is an illegality, it was caught long before now,
1034 -- and was flagged when the original assignment was analyzed.
1036 Set_Assignment_OK (Name (Assign));
1038 -- Propagate the No_Ctrl_Actions flag to individual assignments
1040 Set_No_Ctrl_Actions (Assign, No_Ctrl_Actions (N));
1043 -- Now construct the loop from the inside out, with the last subscript
1044 -- varying most rapidly. Note that Assign is first the raw assignment
1045 -- statement, and then subsequently the loop that wraps it up.
1047 for J in reverse 1 .. Ndim loop
1049 Make_Block_Statement (Loc,
1050 Declarations => New_List (
1051 Make_Object_Declaration (Loc,
1052 Defining_Identifier => Rnn (J),
1053 Object_Definition =>
1054 New_Occurrence_Of (R_Index_Type (J), Loc),
1056 Make_Attribute_Reference (Loc,
1057 Prefix => New_Occurrence_Of (R_Index_Type (J), Loc),
1058 Attribute_Name => F_Or_L))),
1060 Handled_Statement_Sequence =>
1061 Make_Handled_Sequence_Of_Statements (Loc,
1062 Statements => New_List (
1063 Make_Implicit_Loop_Statement (N,
1065 Make_Iteration_Scheme (Loc,
1066 Loop_Parameter_Specification =>
1067 Make_Loop_Parameter_Specification (Loc,
1068 Defining_Identifier => Lnn (J),
1069 Reverse_Present => Rev,
1070 Discrete_Subtype_Definition =>
1071 New_Reference_To (L_Index_Type (J), Loc))),
1073 Statements => New_List (
1076 Make_Assignment_Statement (Loc,
1077 Name => New_Occurrence_Of (Rnn (J), Loc),
1079 Make_Attribute_Reference (Loc,
1081 New_Occurrence_Of (R_Index_Type (J), Loc),
1082 Attribute_Name => S_Or_P,
1083 Expressions => New_List (
1084 New_Occurrence_Of (Rnn (J), Loc)))))))));
1088 end Expand_Assign_Array_Loop;
1090 --------------------------
1091 -- Expand_Assign_Record --
1092 --------------------------
1094 -- The only processing required is in the change of representation
1095 -- case, where we must expand the assignment to a series of field
1096 -- by field assignments.
1098 procedure Expand_Assign_Record (N : Node_Id) is
1099 Lhs : constant Node_Id := Name (N);
1100 Rhs : Node_Id := Expression (N);
1103 -- If change of representation, then extract the real right hand
1104 -- side from the type conversion, and proceed with component-wise
1105 -- assignment, since the two types are not the same as far as the
1106 -- back end is concerned.
1108 if Change_Of_Representation (N) then
1109 Rhs := Expression (Rhs);
1111 -- If this may be a case of a large bit aligned component, then
1112 -- proceed with component-wise assignment, to avoid possible
1113 -- clobbering of other components sharing bits in the first or
1114 -- last byte of the component to be assigned.
1116 elsif Possible_Bit_Aligned_Component (Lhs)
1118 Possible_Bit_Aligned_Component (Rhs)
1122 -- If neither condition met, then nothing special to do, the back end
1123 -- can handle assignment of the entire component as a single entity.
1129 -- At this stage we know that we must do a component wise assignment
1132 Loc : constant Source_Ptr := Sloc (N);
1133 R_Typ : constant Entity_Id := Base_Type (Etype (Rhs));
1134 L_Typ : constant Entity_Id := Base_Type (Etype (Lhs));
1135 Decl : constant Node_Id := Declaration_Node (R_Typ);
1139 function Find_Component
1141 Comp : Entity_Id) return Entity_Id;
1142 -- Find the component with the given name in the underlying record
1143 -- declaration for Typ. We need to use the actual entity because
1144 -- the type may be private and resolution by identifier alone would
1147 function Make_Component_List_Assign
1149 U_U : Boolean := False) return List_Id;
1150 -- Returns a sequence of statements to assign the components that
1151 -- are referenced in the given component list. The flag U_U is
1152 -- used to force the usage of the inferred value of the variant
1153 -- part expression as the switch for the generated case statement.
1155 function Make_Field_Assign
1157 U_U : Boolean := False) return Node_Id;
1158 -- Given C, the entity for a discriminant or component, build an
1159 -- assignment for the corresponding field values. The flag U_U
1160 -- signals the presence of an Unchecked_Union and forces the usage
1161 -- of the inferred discriminant value of C as the right hand side
1162 -- of the assignment.
1164 function Make_Field_Assigns (CI : List_Id) return List_Id;
1165 -- Given CI, a component items list, construct series of statements
1166 -- for fieldwise assignment of the corresponding components.
1168 --------------------
1169 -- Find_Component --
1170 --------------------
1172 function Find_Component
1174 Comp : Entity_Id) return Entity_Id
1176 Utyp : constant Entity_Id := Underlying_Type (Typ);
1180 C := First_Entity (Utyp);
1182 while Present (C) loop
1183 if Chars (C) = Chars (Comp) then
1189 raise Program_Error;
1192 --------------------------------
1193 -- Make_Component_List_Assign --
1194 --------------------------------
1196 function Make_Component_List_Assign
1198 U_U : Boolean := False) return List_Id
1200 CI : constant List_Id := Component_Items (CL);
1201 VP : constant Node_Id := Variant_Part (CL);
1211 Result := Make_Field_Assigns (CI);
1213 if Present (VP) then
1215 V := First_Non_Pragma (Variants (VP));
1217 while Present (V) loop
1220 DC := First (Discrete_Choices (V));
1221 while Present (DC) loop
1222 Append_To (DCH, New_Copy_Tree (DC));
1227 Make_Case_Statement_Alternative (Loc,
1228 Discrete_Choices => DCH,
1230 Make_Component_List_Assign (Component_List (V))));
1231 Next_Non_Pragma (V);
1234 -- If we have an Unchecked_Union, use the value of the inferred
1235 -- discriminant of the variant part expression as the switch
1236 -- for the case statement. The case statement may later be
1241 New_Copy (Get_Discriminant_Value (
1244 Discriminant_Constraint (Etype (Rhs))));
1247 Make_Selected_Component (Loc,
1248 Prefix => Duplicate_Subexpr (Rhs),
1250 Make_Identifier (Loc, Chars (Name (VP))));
1254 Make_Case_Statement (Loc,
1256 Alternatives => Alts));
1260 end Make_Component_List_Assign;
1262 -----------------------
1263 -- Make_Field_Assign --
1264 -----------------------
1266 function Make_Field_Assign
1268 U_U : Boolean := False) return Node_Id
1274 -- In the case of an Unchecked_Union, use the discriminant
1275 -- constraint value as on the right hand side of the assignment.
1279 New_Copy (Get_Discriminant_Value (C,
1281 Discriminant_Constraint (Etype (Rhs))));
1284 Make_Selected_Component (Loc,
1285 Prefix => Duplicate_Subexpr (Rhs),
1286 Selector_Name => New_Occurrence_Of (C, Loc));
1290 Make_Assignment_Statement (Loc,
1292 Make_Selected_Component (Loc,
1293 Prefix => Duplicate_Subexpr (Lhs),
1295 New_Occurrence_Of (Find_Component (L_Typ, C), Loc)),
1296 Expression => Expr);
1298 -- Set Assignment_OK, so discriminants can be assigned
1300 Set_Assignment_OK (Name (A), True);
1302 end Make_Field_Assign;
1304 ------------------------
1305 -- Make_Field_Assigns --
1306 ------------------------
1308 function Make_Field_Assigns (CI : List_Id) return List_Id is
1315 while Present (Item) loop
1316 if Nkind (Item) = N_Component_Declaration then
1318 (Result, Make_Field_Assign (Defining_Identifier (Item)));
1325 end Make_Field_Assigns;
1327 -- Start of processing for Expand_Assign_Record
1330 -- Note that we use the base types for this processing. This results
1331 -- in some extra work in the constrained case, but the change of
1332 -- representation case is so unusual that it is not worth the effort.
1334 -- First copy the discriminants. This is done unconditionally. It
1335 -- is required in the unconstrained left side case, and also in the
1336 -- case where this assignment was constructed during the expansion
1337 -- of a type conversion (since initialization of discriminants is
1338 -- suppressed in this case). It is unnecessary but harmless in
1341 if Has_Discriminants (L_Typ) then
1342 F := First_Discriminant (R_Typ);
1343 while Present (F) loop
1345 if Is_Unchecked_Union (Base_Type (R_Typ)) then
1346 Insert_Action (N, Make_Field_Assign (F, True));
1348 Insert_Action (N, Make_Field_Assign (F));
1351 Next_Discriminant (F);
1355 -- We know the underlying type is a record, but its current view
1356 -- may be private. We must retrieve the usable record declaration.
1358 if Nkind (Decl) = N_Private_Type_Declaration
1359 and then Present (Full_View (R_Typ))
1361 RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
1363 RDef := Type_Definition (Decl);
1366 if Nkind (RDef) = N_Record_Definition
1367 and then Present (Component_List (RDef))
1370 if Is_Unchecked_Union (R_Typ) then
1372 Make_Component_List_Assign (Component_List (RDef), True));
1375 (N, Make_Component_List_Assign (Component_List (RDef)));
1378 Rewrite (N, Make_Null_Statement (Loc));
1382 end Expand_Assign_Record;
1384 -----------------------------------
1385 -- Expand_N_Assignment_Statement --
1386 -----------------------------------
1388 -- This procedure implements various cases where an assignment statement
1389 -- cannot just be passed on to the back end in untransformed state.
1391 procedure Expand_N_Assignment_Statement (N : Node_Id) is
1392 Loc : constant Source_Ptr := Sloc (N);
1393 Lhs : constant Node_Id := Name (N);
1394 Rhs : constant Node_Id := Expression (N);
1395 Typ : constant Entity_Id := Underlying_Type (Etype (Lhs));
1399 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1401 -- Rewrite an assignment to X'Priority into a run-time call
1403 -- For example: X'Priority := New_Prio_Expr;
1404 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1406 -- Note that although X'Priority is notionally an object, it is quite
1407 -- deliberately not defined as an aliased object in the RM. This means
1408 -- that it works fine to rewrite it as a call, without having to worry
1409 -- about complications that would other arise from X'Priority'Access,
1410 -- which is illegal, because of the lack of aliasing.
1412 if Ada_Version >= Ada_05 then
1415 Conctyp : Entity_Id;
1417 Object_Parm : Node_Id;
1419 RT_Subprg_Name : Node_Id;
1422 -- Handle chains of renamings
1425 while Nkind (Ent) in N_Has_Entity
1426 and then Present (Entity (Ent))
1427 and then Present (Renamed_Object (Entity (Ent)))
1429 Ent := Renamed_Object (Entity (Ent));
1432 -- The attribute Priority applied to protected objects has been
1433 -- previously expanded into calls to the Get_Ceiling run-time
1436 if Nkind (Ent) = N_Function_Call
1437 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
1439 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling))
1441 -- Look for the enclosing concurrent type
1443 Conctyp := Current_Scope;
1444 while not Is_Concurrent_Type (Conctyp) loop
1445 Conctyp := Scope (Conctyp);
1448 pragma Assert (Is_Protected_Type (Conctyp));
1450 -- Generate the first actual of the call
1452 Subprg := Current_Scope;
1453 while not Present (Protected_Body_Subprogram (Subprg)) loop
1454 Subprg := Scope (Subprg);
1458 Make_Attribute_Reference (Loc,
1460 Make_Selected_Component (Loc,
1461 Prefix => New_Reference_To
1463 (Protected_Body_Subprogram (Subprg)),
1466 Make_Identifier (Loc, Name_uObject)),
1467 Attribute_Name => Name_Unchecked_Access);
1469 -- Select the appropriate run-time call
1471 if Number_Entries (Conctyp) = 0 then
1473 New_Reference_To (RTE (RE_Set_Ceiling), Loc);
1476 New_Reference_To (RTE (RO_PE_Set_Ceiling), Loc);
1480 Make_Procedure_Call_Statement (Loc,
1481 Name => RT_Subprg_Name,
1482 Parameter_Associations =>
1483 New_List (Object_Parm,
1484 Relocate_Node (Expression (N))));
1493 -- First deal with generation of range check if required. For now we do
1494 -- this only for discrete types.
1496 if Do_Range_Check (Rhs)
1497 and then Is_Discrete_Type (Typ)
1499 Set_Do_Range_Check (Rhs, False);
1500 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
1503 -- Check for a special case where a high level transformation is
1504 -- required. If we have either of:
1509 -- where P is a reference to a bit packed array, then we have to unwind
1510 -- the assignment. The exact meaning of being a reference to a bit
1511 -- packed array is as follows:
1513 -- An indexed component whose prefix is a bit packed array is a
1514 -- reference to a bit packed array.
1516 -- An indexed component or selected component whose prefix is a
1517 -- reference to a bit packed array is itself a reference ot a
1518 -- bit packed array.
1520 -- The required transformation is
1522 -- Tnn : prefix_type := P;
1523 -- Tnn.field := rhs;
1528 -- Tnn : prefix_type := P;
1529 -- Tnn (subscr) := rhs;
1532 -- Since P is going to be evaluated more than once, any subscripts
1533 -- in P must have their evaluation forced.
1535 if (Nkind (Lhs) = N_Indexed_Component
1537 Nkind (Lhs) = N_Selected_Component)
1538 and then Is_Ref_To_Bit_Packed_Array (Prefix (Lhs))
1541 BPAR_Expr : constant Node_Id := Relocate_Node (Prefix (Lhs));
1542 BPAR_Typ : constant Entity_Id := Etype (BPAR_Expr);
1543 Tnn : constant Entity_Id :=
1544 Make_Defining_Identifier (Loc,
1545 Chars => New_Internal_Name ('T'));
1548 -- Insert the post assignment first, because we want to copy
1549 -- the BPAR_Expr tree before it gets analyzed in the context
1550 -- of the pre assignment. Note that we do not analyze the
1551 -- post assignment yet (we cannot till we have completed the
1552 -- analysis of the pre assignment). As usual, the analysis
1553 -- of this post assignment will happen on its own when we
1554 -- "run into" it after finishing the current assignment.
1557 Make_Assignment_Statement (Loc,
1558 Name => New_Copy_Tree (BPAR_Expr),
1559 Expression => New_Occurrence_Of (Tnn, Loc)));
1561 -- At this stage BPAR_Expr is a reference to a bit packed
1562 -- array where the reference was not expanded in the original
1563 -- tree, since it was on the left side of an assignment. But
1564 -- in the pre-assignment statement (the object definition),
1565 -- BPAR_Expr will end up on the right hand side, and must be
1566 -- reexpanded. To achieve this, we reset the analyzed flag
1567 -- of all selected and indexed components down to the actual
1568 -- indexed component for the packed array.
1572 Set_Analyzed (Exp, False);
1574 if Nkind (Exp) = N_Selected_Component
1576 Nkind (Exp) = N_Indexed_Component
1578 Exp := Prefix (Exp);
1584 -- Now we can insert and analyze the pre-assignment
1586 -- If the right-hand side requires a transient scope, it has
1587 -- already been placed on the stack. However, the declaration is
1588 -- inserted in the tree outside of this scope, and must reflect
1589 -- the proper scope for its variable. This awkward bit is forced
1590 -- by the stricter scope discipline imposed by GCC 2.97.
1593 Uses_Transient_Scope : constant Boolean :=
1595 and then N = Node_To_Be_Wrapped;
1598 if Uses_Transient_Scope then
1599 New_Scope (Scope (Current_Scope));
1602 Insert_Before_And_Analyze (N,
1603 Make_Object_Declaration (Loc,
1604 Defining_Identifier => Tnn,
1605 Object_Definition => New_Occurrence_Of (BPAR_Typ, Loc),
1606 Expression => BPAR_Expr));
1608 if Uses_Transient_Scope then
1613 -- Now fix up the original assignment and continue processing
1615 Rewrite (Prefix (Lhs),
1616 New_Occurrence_Of (Tnn, Loc));
1618 -- We do not need to reanalyze that assignment, and we do not need
1619 -- to worry about references to the temporary, but we do need to
1620 -- make sure that the temporary is not marked as a true constant
1621 -- since we now have a generate assignment to it!
1623 Set_Is_True_Constant (Tnn, False);
1627 -- When we have the appropriate type of aggregate in the
1628 -- expression (it has been determined during analysis of the
1629 -- aggregate by setting the delay flag), let's perform in place
1630 -- assignment and thus avoid creating a temporay.
1632 if Is_Delayed_Aggregate (Rhs) then
1633 Convert_Aggr_In_Assignment (N);
1634 Rewrite (N, Make_Null_Statement (Loc));
1639 -- Apply discriminant check if required. If Lhs is an access type
1640 -- to a designated type with discriminants, we must always check.
1642 if Has_Discriminants (Etype (Lhs)) then
1644 -- Skip discriminant check if change of representation. Will be
1645 -- done when the change of representation is expanded out.
1647 if not Change_Of_Representation (N) then
1648 Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
1651 -- If the type is private without discriminants, and the full type
1652 -- has discriminants (necessarily with defaults) a check may still be
1653 -- necessary if the Lhs is aliased. The private determinants must be
1654 -- visible to build the discriminant constraints.
1656 -- Only an explicit dereference that comes from source indicates
1657 -- aliasing. Access to formals of protected operations and entries
1658 -- create dereferences but are not semantic aliasings.
1660 elsif Is_Private_Type (Etype (Lhs))
1661 and then Has_Discriminants (Typ)
1662 and then Nkind (Lhs) = N_Explicit_Dereference
1663 and then Comes_From_Source (Lhs)
1666 Lt : constant Entity_Id := Etype (Lhs);
1668 Set_Etype (Lhs, Typ);
1669 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1670 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1671 Set_Etype (Lhs, Lt);
1674 -- If the Lhs has a private type with unknown discriminants, it
1675 -- may have a full view with discriminants, but those are nameable
1676 -- only in the underlying type, so convert the Rhs to it before
1677 -- potential checking.
1679 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs)))
1680 and then Has_Discriminants (Typ)
1682 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1683 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1685 -- In the access type case, we need the same discriminant check,
1686 -- and also range checks if we have an access to constrained array.
1688 elsif Is_Access_Type (Etype (Lhs))
1689 and then Is_Constrained (Designated_Type (Etype (Lhs)))
1691 if Has_Discriminants (Designated_Type (Etype (Lhs))) then
1693 -- Skip discriminant check if change of representation. Will be
1694 -- done when the change of representation is expanded out.
1696 if not Change_Of_Representation (N) then
1697 Apply_Discriminant_Check (Rhs, Etype (Lhs));
1700 elsif Is_Array_Type (Designated_Type (Etype (Lhs))) then
1701 Apply_Range_Check (Rhs, Etype (Lhs));
1703 if Is_Constrained (Etype (Lhs)) then
1704 Apply_Length_Check (Rhs, Etype (Lhs));
1707 if Nkind (Rhs) = N_Allocator then
1709 Target_Typ : constant Entity_Id := Etype (Expression (Rhs));
1710 C_Es : Check_Result;
1717 Etype (Designated_Type (Etype (Lhs))));
1729 -- Apply range check for access type case
1731 elsif Is_Access_Type (Etype (Lhs))
1732 and then Nkind (Rhs) = N_Allocator
1733 and then Nkind (Expression (Rhs)) = N_Qualified_Expression
1735 Analyze_And_Resolve (Expression (Rhs));
1737 (Expression (Rhs), Designated_Type (Etype (Lhs)));
1740 -- Ada 2005 (AI-231): Generate the run-time check
1742 if Is_Access_Type (Typ)
1743 and then Can_Never_Be_Null (Etype (Lhs))
1744 and then not Can_Never_Be_Null (Etype (Rhs))
1746 Apply_Constraint_Check (Rhs, Etype (Lhs));
1749 -- Case of assignment to a bit packed array element
1751 if Nkind (Lhs) = N_Indexed_Component
1752 and then Is_Bit_Packed_Array (Etype (Prefix (Lhs)))
1754 Expand_Bit_Packed_Element_Set (N);
1757 -- Build-in-place function call case. Note that we're not yet doing
1758 -- build-in-place for user-written assignment statements; the
1759 -- assignment here came from an aggregate.
1761 elsif Ada_Version >= Ada_05
1762 and then Is_Build_In_Place_Function_Call (Rhs)
1764 Make_Build_In_Place_Call_In_Assignment (N, Rhs);
1766 elsif Is_Tagged_Type (Typ)
1767 or else (Controlled_Type (Typ) and then not Is_Array_Type (Typ))
1769 Tagged_Case : declare
1770 L : List_Id := No_List;
1771 Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
1774 -- In the controlled case, we need to make sure that function
1775 -- calls are evaluated before finalizing the target. In all
1776 -- cases, it makes the expansion easier if the side-effects
1777 -- are removed first.
1779 Remove_Side_Effects (Lhs);
1780 Remove_Side_Effects (Rhs);
1782 -- Avoid recursion in the mechanism
1786 -- If dispatching assignment, we need to dispatch to _assign
1788 if Is_Class_Wide_Type (Typ)
1790 -- If the type is tagged, we may as well use the predefined
1791 -- primitive assignment. This avoids inlining a lot of code
1792 -- and in the class-wide case, the assignment is replaced by
1793 -- dispatch call to _assign. Note that this cannot be done
1794 -- when discriminant checks are locally suppressed (as in
1795 -- extension aggregate expansions) because otherwise the
1796 -- discriminant check will be performed within the _assign
1797 -- call. It is also suppressed for assignmments created by the
1798 -- expander that correspond to initializations, where we do
1799 -- want to copy the tag (No_Ctrl_Actions flag set True).
1800 -- by the expander and we do not need to mess with tags ever
1801 -- (Expand_Ctrl_Actions flag is set True in this case).
1803 or else (Is_Tagged_Type (Typ)
1804 and then Chars (Current_Scope) /= Name_uAssign
1805 and then Expand_Ctrl_Actions
1806 and then not Discriminant_Checks_Suppressed (Empty))
1808 -- Fetch the primitive op _assign and proper type to call
1809 -- it. Because of possible conflits between private and
1810 -- full view the proper type is fetched directly from the
1811 -- operation profile.
1814 Op : constant Entity_Id :=
1815 Find_Prim_Op (Typ, Name_uAssign);
1816 F_Typ : Entity_Id := Etype (First_Formal (Op));
1819 -- If the assignment is dispatching, make sure to use the
1822 if Is_Class_Wide_Type (Typ) then
1823 F_Typ := Class_Wide_Type (F_Typ);
1828 -- In case of assignment to a class-wide tagged type, before
1829 -- the assignment we generate run-time check to ensure that
1830 -- the tags of source and target match.
1832 if Is_Class_Wide_Type (Typ)
1833 and then Is_Tagged_Type (Typ)
1834 and then Is_Tagged_Type (Underlying_Type (Etype (Rhs)))
1837 Make_Raise_Constraint_Error (Loc,
1841 Make_Selected_Component (Loc,
1842 Prefix => Duplicate_Subexpr (Lhs),
1844 Make_Identifier (Loc,
1845 Chars => Name_uTag)),
1847 Make_Selected_Component (Loc,
1848 Prefix => Duplicate_Subexpr (Rhs),
1850 Make_Identifier (Loc,
1851 Chars => Name_uTag))),
1852 Reason => CE_Tag_Check_Failed));
1856 Make_Procedure_Call_Statement (Loc,
1857 Name => New_Reference_To (Op, Loc),
1858 Parameter_Associations => New_List (
1859 Unchecked_Convert_To (F_Typ,
1860 Duplicate_Subexpr (Lhs)),
1861 Unchecked_Convert_To (F_Typ,
1862 Duplicate_Subexpr (Rhs)))));
1866 L := Make_Tag_Ctrl_Assignment (N);
1868 -- We can't afford to have destructive Finalization Actions
1869 -- in the Self assignment case, so if the target and the
1870 -- source are not obviously different, code is generated to
1871 -- avoid the self assignment case:
1873 -- if lhs'address /= rhs'address then
1874 -- <code for controlled and/or tagged assignment>
1877 if not Statically_Different (Lhs, Rhs)
1878 and then Expand_Ctrl_Actions
1881 Make_Implicit_If_Statement (N,
1885 Make_Attribute_Reference (Loc,
1886 Prefix => Duplicate_Subexpr (Lhs),
1887 Attribute_Name => Name_Address),
1890 Make_Attribute_Reference (Loc,
1891 Prefix => Duplicate_Subexpr (Rhs),
1892 Attribute_Name => Name_Address)),
1894 Then_Statements => L));
1897 -- We need to set up an exception handler for implementing
1898 -- 7.6.1 (18). The remaining adjustments are tackled by the
1899 -- implementation of adjust for record_controllers (see
1902 -- This is skipped if we have no finalization
1904 if Expand_Ctrl_Actions
1905 and then not Restriction_Active (No_Finalization)
1908 Make_Block_Statement (Loc,
1909 Handled_Statement_Sequence =>
1910 Make_Handled_Sequence_Of_Statements (Loc,
1912 Exception_Handlers => New_List (
1913 Make_Implicit_Exception_Handler (Loc,
1914 Exception_Choices =>
1915 New_List (Make_Others_Choice (Loc)),
1916 Statements => New_List (
1917 Make_Raise_Program_Error (Loc,
1919 PE_Finalize_Raised_Exception)
1925 Make_Block_Statement (Loc,
1926 Handled_Statement_Sequence =>
1927 Make_Handled_Sequence_Of_Statements (Loc, Statements => L)));
1929 -- If no restrictions on aborts, protect the whole assignement
1930 -- for controlled objects as per 9.8(11).
1932 if Controlled_Type (Typ)
1933 and then Expand_Ctrl_Actions
1934 and then Abort_Allowed
1937 Blk : constant Entity_Id :=
1939 (E_Block, Current_Scope, Sloc (N), 'B');
1942 Set_Scope (Blk, Current_Scope);
1943 Set_Etype (Blk, Standard_Void_Type);
1944 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
1946 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
1947 Set_At_End_Proc (Handled_Statement_Sequence (N),
1948 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
1949 Expand_At_End_Handler
1950 (Handled_Statement_Sequence (N), Blk);
1954 -- N has been rewritten to a block statement for which it is
1955 -- known by construction that no checks are necessary: analyze
1956 -- it with all checks suppressed.
1958 Analyze (N, Suppress => All_Checks);
1964 elsif Is_Array_Type (Typ) then
1966 Actual_Rhs : Node_Id := Rhs;
1969 while Nkind (Actual_Rhs) = N_Type_Conversion
1971 Nkind (Actual_Rhs) = N_Qualified_Expression
1973 Actual_Rhs := Expression (Actual_Rhs);
1976 Expand_Assign_Array (N, Actual_Rhs);
1982 elsif Is_Record_Type (Typ) then
1983 Expand_Assign_Record (N);
1986 -- Scalar types. This is where we perform the processing related
1987 -- to the requirements of (RM 13.9.1(9-11)) concerning the handling
1988 -- of invalid scalar values.
1990 elsif Is_Scalar_Type (Typ) then
1992 -- Case where right side is known valid
1994 if Expr_Known_Valid (Rhs) then
1996 -- Here the right side is valid, so it is fine. The case to
1997 -- deal with is when the left side is a local variable reference
1998 -- whose value is not currently known to be valid. If this is
1999 -- the case, and the assignment appears in an unconditional
2000 -- context, then we can mark the left side as now being valid.
2002 if Is_Local_Variable_Reference (Lhs)
2003 and then not Is_Known_Valid (Entity (Lhs))
2004 and then In_Unconditional_Context (N)
2006 Set_Is_Known_Valid (Entity (Lhs), True);
2009 -- Case where right side may be invalid in the sense of the RM
2010 -- reference above. The RM does not require that we check for
2011 -- the validity on an assignment, but it does require that the
2012 -- assignment of an invalid value not cause erroneous behavior.
2014 -- The general approach in GNAT is to use the Is_Known_Valid flag
2015 -- to avoid the need for validity checking on assignments. However
2016 -- in some cases, we have to do validity checking in order to make
2017 -- sure that the setting of this flag is correct.
2020 -- Validate right side if we are validating copies
2022 if Validity_Checks_On
2023 and then Validity_Check_Copies
2025 -- Skip this if left hand side is an array or record component
2026 -- and elementary component validity checks are suppressed.
2028 if (Nkind (Lhs) = N_Selected_Component
2030 Nkind (Lhs) = N_Indexed_Component)
2031 and then not Validity_Check_Components
2038 -- We can propagate this to the left side where appropriate
2040 if Is_Local_Variable_Reference (Lhs)
2041 and then not Is_Known_Valid (Entity (Lhs))
2042 and then In_Unconditional_Context (N)
2044 Set_Is_Known_Valid (Entity (Lhs), True);
2047 -- Otherwise check to see what should be done
2049 -- If left side is a local variable, then we just set its
2050 -- flag to indicate that its value may no longer be valid,
2051 -- since we are copying a potentially invalid value.
2053 elsif Is_Local_Variable_Reference (Lhs) then
2054 Set_Is_Known_Valid (Entity (Lhs), False);
2056 -- Check for case of a nonlocal variable on the left side
2057 -- which is currently known to be valid. In this case, we
2058 -- simply ensure that the right side is valid. We only play
2059 -- the game of copying validity status for local variables,
2060 -- since we are doing this statically, not by tracing the
2063 elsif Is_Entity_Name (Lhs)
2064 and then Is_Known_Valid (Entity (Lhs))
2066 -- Note that the Ensure_Valid call is ignored if the
2067 -- Validity_Checking mode is set to none so we do not
2068 -- need to worry about that case here.
2072 -- In all other cases, we can safely copy an invalid value
2073 -- without worrying about the status of the left side. Since
2074 -- it is not a variable reference it will not be considered
2075 -- as being known to be valid in any case.
2083 -- Defend against invalid subscripts on left side if we are in
2084 -- standard validity checking mode. No need to do this if we
2085 -- are checking all subscripts.
2087 if Validity_Checks_On
2088 and then Validity_Check_Default
2089 and then not Validity_Check_Subscripts
2091 Check_Valid_Lvalue_Subscripts (Lhs);
2095 when RE_Not_Available =>
2097 end Expand_N_Assignment_Statement;
2099 ------------------------------
2100 -- Expand_N_Block_Statement --
2101 ------------------------------
2103 -- Encode entity names defined in block statement
2105 procedure Expand_N_Block_Statement (N : Node_Id) is
2107 Qualify_Entity_Names (N);
2108 end Expand_N_Block_Statement;
2110 -----------------------------
2111 -- Expand_N_Case_Statement --
2112 -----------------------------
2114 procedure Expand_N_Case_Statement (N : Node_Id) is
2115 Loc : constant Source_Ptr := Sloc (N);
2116 Expr : constant Node_Id := Expression (N);
2124 -- Check for the situation where we know at compile time which
2125 -- branch will be taken
2127 if Compile_Time_Known_Value (Expr) then
2128 Alt := Find_Static_Alternative (N);
2130 -- Move the statements from this alternative after the case
2131 -- statement. They are already analyzed, so will be skipped
2134 Insert_List_After (N, Statements (Alt));
2136 -- That leaves the case statement as a shell. So now we can kill all
2137 -- other alternatives in the case statement.
2139 Kill_Dead_Code (Expression (N));
2145 -- Loop through case alternatives, skipping pragmas, and skipping
2146 -- the one alternative that we select (and therefore retain).
2148 A := First (Alternatives (N));
2149 while Present (A) loop
2151 and then Nkind (A) = N_Case_Statement_Alternative
2153 Kill_Dead_Code (Statements (A), Warn_On_Deleted_Code);
2160 Rewrite (N, Make_Null_Statement (Loc));
2164 -- Here if the choice is not determined at compile time
2167 Last_Alt : constant Node_Id := Last (Alternatives (N));
2169 Others_Present : Boolean;
2170 Others_Node : Node_Id;
2172 Then_Stms : List_Id;
2173 Else_Stms : List_Id;
2176 if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
2177 Others_Present := True;
2178 Others_Node := Last_Alt;
2180 Others_Present := False;
2183 -- First step is to worry about possible invalid argument. The RM
2184 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2185 -- outside the base range), then Constraint_Error must be raised.
2187 -- Case of validity check required (validity checks are on, the
2188 -- expression is not known to be valid, and the case statement
2189 -- comes from source -- no need to validity check internally
2190 -- generated case statements).
2192 if Validity_Check_Default then
2193 Ensure_Valid (Expr);
2196 -- If there is only a single alternative, just replace it with
2197 -- the sequence of statements since obviously that is what is
2198 -- going to be executed in all cases.
2200 Len := List_Length (Alternatives (N));
2203 -- We still need to evaluate the expression if it has any
2206 Remove_Side_Effects (Expression (N));
2208 Insert_List_After (N, Statements (First (Alternatives (N))));
2210 -- That leaves the case statement as a shell. The alternative
2211 -- that will be executed is reset to a null list. So now we can
2212 -- kill the entire case statement.
2214 Kill_Dead_Code (Expression (N));
2215 Rewrite (N, Make_Null_Statement (Loc));
2219 -- An optimization. If there are only two alternatives, and only
2220 -- a single choice, then rewrite the whole case statement as an
2221 -- if statement, since this can result in susbequent optimizations.
2222 -- This helps not only with case statements in the source of a
2223 -- simple form, but also with generated code (discriminant check
2224 -- functions in particular)
2227 Chlist := Discrete_Choices (First (Alternatives (N)));
2229 if List_Length (Chlist) = 1 then
2230 Choice := First (Chlist);
2232 Then_Stms := Statements (First (Alternatives (N)));
2233 Else_Stms := Statements (Last (Alternatives (N)));
2235 -- For TRUE, generate "expression", not expression = true
2237 if Nkind (Choice) = N_Identifier
2238 and then Entity (Choice) = Standard_True
2240 Cond := Expression (N);
2242 -- For FALSE, generate "expression" and switch then/else
2244 elsif Nkind (Choice) = N_Identifier
2245 and then Entity (Choice) = Standard_False
2247 Cond := Expression (N);
2248 Else_Stms := Statements (First (Alternatives (N)));
2249 Then_Stms := Statements (Last (Alternatives (N)));
2251 -- For a range, generate "expression in range"
2253 elsif Nkind (Choice) = N_Range
2254 or else (Nkind (Choice) = N_Attribute_Reference
2255 and then Attribute_Name (Choice) = Name_Range)
2256 or else (Is_Entity_Name (Choice)
2257 and then Is_Type (Entity (Choice)))
2258 or else Nkind (Choice) = N_Subtype_Indication
2262 Left_Opnd => Expression (N),
2263 Right_Opnd => Relocate_Node (Choice));
2265 -- For any other subexpression "expression = value"
2270 Left_Opnd => Expression (N),
2271 Right_Opnd => Relocate_Node (Choice));
2274 -- Now rewrite the case as an IF
2277 Make_If_Statement (Loc,
2279 Then_Statements => Then_Stms,
2280 Else_Statements => Else_Stms));
2286 -- If the last alternative is not an Others choice, replace it
2287 -- with an N_Others_Choice. Note that we do not bother to call
2288 -- Analyze on the modified case statement, since it's only effect
2289 -- would be to compute the contents of the Others_Discrete_Choices
2290 -- which is not needed by the back end anyway.
2292 -- The reason we do this is that the back end always needs some
2293 -- default for a switch, so if we have not supplied one in the
2294 -- processing above for validity checking, then we need to
2297 if not Others_Present then
2298 Others_Node := Make_Others_Choice (Sloc (Last_Alt));
2299 Set_Others_Discrete_Choices
2300 (Others_Node, Discrete_Choices (Last_Alt));
2301 Set_Discrete_Choices (Last_Alt, New_List (Others_Node));
2304 end Expand_N_Case_Statement;
2306 -----------------------------
2307 -- Expand_N_Exit_Statement --
2308 -----------------------------
2310 -- The only processing required is to deal with a possible C/Fortran
2311 -- boolean value used as the condition for the exit statement.
2313 procedure Expand_N_Exit_Statement (N : Node_Id) is
2315 Adjust_Condition (Condition (N));
2316 end Expand_N_Exit_Statement;
2318 ----------------------------------------
2319 -- Expand_N_Extended_Return_Statement --
2320 ----------------------------------------
2322 -- If there is a Handled_Statement_Sequence, we rewrite this:
2324 -- return Result : T := <expression> do
2325 -- <handled_seq_of_stms>
2331 -- Result : T := <expression>;
2333 -- <handled_seq_of_stms>
2337 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
2339 -- return Result : T := <expression>;
2343 -- return <expression>;
2345 -- unless it's build-in-place or there's no <expression>, in which case
2349 -- Result : T := <expression>;
2354 -- Note that this case could have been written by the user as an extended
2355 -- return statement, or could have been transformed to this from a simple
2356 -- return statement.
2358 -- That is, we need to have a reified return object if there are statements
2359 -- (which might refer to it) or if we're doing build-in-place (so we can
2360 -- set its address to the final resting place -- but that key part is not
2361 -- yet implemented) or if there is no expression (in which case default
2362 -- initial values might need to be set).
2364 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
2365 Loc : constant Source_Ptr := Sloc (N);
2367 Return_Object_Entity : constant Entity_Id :=
2368 First_Entity (Return_Statement_Entity (N));
2369 Return_Object_Decl : constant Node_Id :=
2370 Parent (Return_Object_Entity);
2371 Parent_Function : constant Entity_Id :=
2372 Return_Applies_To (Return_Statement_Entity (N));
2373 Is_Build_In_Place : constant Boolean :=
2374 Is_Build_In_Place_Function (Parent_Function);
2376 Return_Stm : Node_Id;
2377 Statements : List_Id;
2378 Handled_Stm_Seq : Node_Id;
2382 function Move_Activation_Chain return Node_Id;
2383 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
2385 -- From current activation chain
2386 -- To activation chain passed in by the caller
2387 -- New_Master master passed in by the caller
2389 function Move_Final_List return Node_Id;
2390 -- Construct call to System.Finalization_Implementation.Move_Final_List
2392 -- From finalization list of the return statement
2393 -- To finalization list passed in by the caller
2395 ---------------------
2396 -- Move_Activation_Chain --
2397 ---------------------
2399 function Move_Activation_Chain return Node_Id is
2400 Activation_Chain_Formal : constant Entity_Id :=
2401 Build_In_Place_Formal (Parent_Function, BIP_Activation_Chain);
2402 To : constant Node_Id :=
2403 New_Reference_To (Activation_Chain_Formal, Loc);
2404 Master_Formal : constant Entity_Id :=
2405 Build_In_Place_Formal (Parent_Function, BIP_Master);
2406 New_Master : constant Node_Id :=
2407 New_Reference_To (Master_Formal, Loc);
2409 Chain_Entity : Entity_Id;
2412 Chain_Entity := First_Entity (Return_Statement_Entity (N));
2413 while Chars (Chain_Entity) /= Name_uChain loop
2414 Chain_Entity := Next_Entity (Chain_Entity);
2418 Make_Attribute_Reference (Loc,
2419 Prefix => New_Reference_To (Chain_Entity, Loc),
2420 Attribute_Name => Name_Unrestricted_Access);
2421 -- ??? I'm not sure why "Make_Identifier (Loc, Name_uChain)" doesn't
2422 -- work, instead of "New_Reference_To (Chain_Entity, Loc)" above.
2425 Make_Procedure_Call_Statement (Loc,
2426 Name => New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
2427 Parameter_Associations => New_List (From, To, New_Master));
2428 end Move_Activation_Chain;
2430 ---------------------
2431 -- Move_Final_List --
2432 ---------------------
2434 function Move_Final_List return Node_Id is
2435 Flist : constant Entity_Id :=
2436 Finalization_Chain_Entity (Return_Statement_Entity (N));
2438 From : constant Node_Id := New_Reference_To (Flist, Loc);
2440 Caller_Final_List : constant Entity_Id :=
2441 Build_In_Place_Formal
2442 (Parent_Function, BIP_Final_List);
2444 To : constant Node_Id :=
2445 New_Reference_To (Caller_Final_List, Loc);
2449 Make_Procedure_Call_Statement (Loc,
2450 Name => New_Reference_To (RTE (RE_Move_Final_List), Loc),
2451 Parameter_Associations => New_List (From, To));
2452 end Move_Final_List;
2454 -- Start of processing for Expand_N_Extended_Return_Statement
2457 if Nkind (Return_Object_Decl) = N_Object_Declaration then
2458 Exp := Expression (Return_Object_Decl);
2463 Handled_Stm_Seq := Handled_Statement_Sequence (N);
2465 -- Build a simple_return_statement that returns the return object when
2466 -- there is a statement sequence, or no expression, or the result will
2467 -- be built in place. Note however that we currently do this for all
2468 -- composite cases, even though nonlimited composite results are not yet
2469 -- built in place (though we plan to do so eventually).
2471 if Present (Handled_Stm_Seq)
2472 or else Is_Composite_Type (Etype (Parent_Function))
2475 Statements := New_List;
2477 if Present (Handled_Stm_Seq) then
2478 Append_To (Statements, Handled_Stm_Seq);
2481 -- If control gets past the above Statements, we have successfully
2482 -- completed the return statement. If the result type has controlled
2483 -- parts, we call Move_Final_List to transfer responsibility for
2484 -- finalization of the return object to the caller. An alternative
2485 -- would be to declare a Success flag in the function, initialize it
2486 -- to False, and set it to True here. Then move the Move_Final_List
2487 -- call into the cleanup code, and check Success. If Success then
2488 -- Move_Final_List else do finalization. Then we can remove the
2489 -- abort-deferral and the nulling-out of the From parameter from
2490 -- Move_Final_List. Note that the current method is not quite
2491 -- correct in the rather obscure case of a select-then-abort
2492 -- statement whose abortable part contains the return statement.
2494 if Is_Controlled (Etype (Parent_Function))
2495 or else Has_Controlled_Component (Etype (Parent_Function))
2497 Append_To (Statements, Move_Final_List);
2500 -- Similarly to the above Move_Final_List, if the result type
2501 -- contains tasks, we call Move_Activation_Chain. Later, the cleanup
2502 -- code will call Complete_Master, which will terminate any
2503 -- unactivated tasks belonging to the return statement master. But
2504 -- Move_Activation_Chain updates their master to be that of the
2505 -- caller, so they will not be terminated unless the return
2506 -- statement completes unsuccessfully due to exception, abort, goto,
2509 if Has_Task (Etype (Parent_Function)) then
2510 Append_To (Statements, Move_Activation_Chain);
2513 -- Build a simple_return_statement that returns the return object
2516 Make_Return_Statement (Loc,
2517 Expression => New_Occurrence_Of (Return_Object_Entity, Loc));
2518 Append_To (Statements, Return_Stm);
2521 Make_Handled_Sequence_Of_Statements (Loc, Statements);
2524 -- Case where we build a block
2526 if Present (Handled_Stm_Seq) then
2528 Make_Block_Statement (Loc,
2529 Declarations => Return_Object_Declarations (N),
2530 Handled_Statement_Sequence => Handled_Stm_Seq);
2532 -- We set the entity of the new block statement to be that of the
2533 -- return statement. This is necessary so that various fields, such
2534 -- as Finalization_Chain_Entity carry over from the return statement
2535 -- to the block. Note that this block is unusual, in that its entity
2536 -- is an E_Return_Statement rather than an E_Block.
2539 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
2541 -- If the object decl was already rewritten as a renaming, then
2542 -- we don't want to do the object allocation and transformation of
2543 -- of the return object declaration to a renaming. This case occurs
2544 -- when the return object is initialized by a call to another
2545 -- build-in-place function, and that function is responsible for the
2546 -- allocation of the return object.
2548 if Is_Build_In_Place
2550 Nkind (Return_Object_Decl) = N_Object_Renaming_Declaration
2552 Set_By_Ref (Return_Stm); -- Return build-in-place results by ref
2554 elsif Is_Build_In_Place then
2556 -- Locate the implicit access parameter associated with the
2557 -- the caller-supplied return object and convert the return
2558 -- statement's return object declaration to a renaming of a
2559 -- dereference of the access parameter. If the return object's
2560 -- declaration includes an expression that has not already been
2561 -- expanded as separate assignments, then add an assignment
2562 -- statement to ensure the return object gets initialized.
2565 -- Result : T [:= <expression>];
2572 -- Result : T renames FuncRA.all;
2573 -- [Result := <expression;]
2578 Return_Obj_Id : constant Entity_Id :=
2579 Defining_Identifier (Return_Object_Decl);
2580 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
2581 Return_Obj_Expr : constant Node_Id :=
2582 Expression (Return_Object_Decl);
2583 Result_Subt : constant Entity_Id :=
2584 Etype (Parent_Function);
2585 Constr_Result : constant Boolean :=
2586 Is_Constrained (Result_Subt);
2587 Obj_Alloc_Formal : Entity_Id;
2588 Object_Access : Entity_Id;
2589 Obj_Acc_Deref : Node_Id;
2590 Init_Assignment : Node_Id := Empty;
2593 -- Build-in-place results must be returned by reference
2595 Set_By_Ref (Return_Stm);
2597 -- Retrieve the implicit access parameter passed by the caller
2600 Build_In_Place_Formal (Parent_Function, BIP_Object_Access);
2602 -- If the return object's declaration includes an expression
2603 -- and the declaration isn't marked as No_Initialization, then
2604 -- we need to generate an assignment to the object and insert
2605 -- it after the declaration before rewriting it as a renaming
2606 -- (otherwise we'll lose the initialization).
2608 if Present (Return_Obj_Expr)
2609 and then not No_Initialization (Return_Object_Decl)
2612 Make_Assignment_Statement (Loc,
2613 Name => New_Reference_To (Return_Obj_Id, Loc),
2614 Expression => Relocate_Node (Return_Obj_Expr));
2615 Set_Assignment_OK (Name (Init_Assignment));
2616 Set_No_Ctrl_Actions (Init_Assignment);
2618 Set_Parent (Expression (Init_Assignment), Init_Assignment);
2620 Set_Expression (Return_Object_Decl, Empty);
2622 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
2623 and then not Is_Class_Wide_Type
2624 (Etype (Expression (Init_Assignment)))
2626 Rewrite (Expression (Init_Assignment),
2627 Make_Type_Conversion (Loc,
2630 (Etype (Return_Obj_Id), Loc),
2632 Relocate_Node (Expression (Init_Assignment))));
2635 if Constr_Result then
2636 Insert_After (Return_Object_Decl, Init_Assignment);
2640 -- When the function's subtype is unconstrained, a run-time
2641 -- test is needed to determine the form of allocation to use
2642 -- for the return object. The function has an implicit formal
2643 -- parameter that indicates this. If the BIP_Alloc_Form formal
2644 -- has the value one, then the caller has passed access to an
2645 -- existing object for use as the return object. If the value
2646 -- is two, then the return object must be allocated on the
2647 -- secondary stack. Otherwise, the object must be allocated in
2648 -- a storage pool. Currently the last case is only supported
2649 -- for the global heap (user-defined storage pools TBD ???). We
2650 -- generate an if statement to test the implicit allocation
2651 -- formal and initialize a local access value appropriately,
2652 -- creating allocators in the secondary stack and global heap
2655 if not Constr_Result then
2657 Build_In_Place_Formal (Parent_Function, BIP_Alloc_Form);
2660 Ref_Type : Entity_Id;
2661 Ptr_Type_Decl : Node_Id;
2662 Alloc_Obj_Id : Entity_Id;
2663 Alloc_Obj_Decl : Node_Id;
2664 Alloc_If_Stmt : Node_Id;
2665 SS_Allocator : Node_Id;
2666 Heap_Allocator : Node_Id;
2669 -- Reuse the itype created for the function's implicit
2670 -- access formal. This avoids the need to create a new
2671 -- access type here, plus it allows assigning the access
2672 -- formal directly without applying a conversion.
2674 -- Ref_Type := Etype (Object_Access);
2676 -- Create an access type designating the function's
2680 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2683 Make_Full_Type_Declaration (Loc,
2684 Defining_Identifier => Ref_Type,
2686 Make_Access_To_Object_Definition (Loc,
2687 All_Present => True,
2688 Subtype_Indication =>
2689 New_Reference_To (Return_Obj_Typ, Loc)));
2691 Insert_Before_And_Analyze
2692 (Return_Object_Decl, Ptr_Type_Decl);
2694 -- Create an access object that will be initialized to an
2695 -- access value denoting the return object, either coming
2696 -- from an implicit access value passed in by the caller
2697 -- or from the result of an allocator.
2700 Make_Defining_Identifier (Loc,
2701 Chars => New_Internal_Name ('R'));
2702 Set_Etype (Alloc_Obj_Id, Ref_Type);
2705 Make_Object_Declaration (Loc,
2706 Defining_Identifier => Alloc_Obj_Id,
2707 Object_Definition => New_Reference_To
2710 Insert_Before_And_Analyze
2711 (Return_Object_Decl, Alloc_Obj_Decl);
2713 -- Create allocators for both the secondary stack and
2714 -- global heap. If there's an initialization expression,
2715 -- then create these as initialized allocators.
2717 if Present (Return_Obj_Expr)
2718 and then not No_Initialization (Return_Object_Decl)
2721 Make_Allocator (Loc,
2723 Make_Qualified_Expression (Loc,
2725 New_Reference_To (Return_Obj_Typ, Loc),
2727 New_Copy_Tree (Return_Obj_Expr)));
2729 SS_Allocator := New_Copy_Tree (Heap_Allocator);
2733 Make_Allocator (Loc,
2734 New_Reference_To (Return_Obj_Typ, Loc));
2736 -- If the object requires default initialization then
2737 -- that will happen later following the elaboration of
2738 -- the object renaming. If we don't turn it off here
2739 -- then the object will be default initialized twice.
2741 Set_No_Initialization (Heap_Allocator);
2743 SS_Allocator := New_Copy_Tree (Heap_Allocator);
2747 (SS_Allocator, RTE (RE_SS_Pool));
2748 Set_Procedure_To_Call
2749 (SS_Allocator, RTE (RE_SS_Allocate));
2751 -- Create an if statement to test the BIP_Alloc_Form
2752 -- formal and initialize the access object to either the
2753 -- BIP_Object_Access formal (BIP_Alloc_Form = 0), the
2754 -- result of allocaing the object in the secondary stack
2755 -- (BIP_Alloc_Form = 1), or else an allocator to create
2756 -- the return object in the heap (BIP_Alloc_Form = 2).
2758 -- ??? An unchecked type conversion must be made in the
2759 -- case of assigning the access object formal to the
2760 -- local access object, because a normal conversion would
2761 -- be illegal in some cases (such as converting access-
2762 -- to-unconstrained to access-to-constrained), but the
2763 -- the unchecked conversion will presumably fail to work
2764 -- right in just such cases. It's not clear at all how to
2768 Make_If_Statement (Loc,
2772 New_Reference_To (Obj_Alloc_Formal, Loc),
2774 Make_Integer_Literal (Loc,
2775 UI_From_Int (BIP_Allocation_Form'Pos
2776 (Caller_Allocation)))),
2778 New_List (Make_Assignment_Statement (Loc,
2781 (Alloc_Obj_Id, Loc),
2783 Make_Unchecked_Type_Conversion (Loc,
2785 New_Reference_To (Ref_Type, Loc),
2788 (Object_Access, Loc)))),
2790 New_List (Make_Elsif_Part (Loc,
2795 (Obj_Alloc_Formal, Loc),
2797 Make_Integer_Literal (Loc,
2799 BIP_Allocation_Form'Pos
2800 (Secondary_Stack)))),
2803 (Make_Assignment_Statement (Loc,
2806 (Alloc_Obj_Id, Loc),
2810 New_List (Make_Assignment_Statement (Loc,
2813 (Alloc_Obj_Id, Loc),
2817 -- If a separate initialization assignment was created
2818 -- earlier, append that following the assignment of the
2819 -- implicit access formal to the access object, to ensure
2820 -- that the return object is initialized in that case.
2822 if Present (Init_Assignment) then
2824 (Then_Statements (Alloc_If_Stmt),
2828 Insert_After_And_Analyze (Alloc_Obj_Decl, Alloc_If_Stmt);
2830 -- Remember the local access object for use in the
2831 -- dereference of the renaming created below.
2833 Object_Access := Alloc_Obj_Id;
2837 -- Replace the return object declaration with a renaming of a
2838 -- dereference of the access value designating the return
2842 Make_Explicit_Dereference (Loc,
2843 Prefix => New_Reference_To (Object_Access, Loc));
2845 Rewrite (Return_Object_Decl,
2846 Make_Object_Renaming_Declaration (Loc,
2847 Defining_Identifier => Return_Obj_Id,
2848 Access_Definition => Empty,
2849 Subtype_Mark => New_Occurrence_Of
2850 (Return_Obj_Typ, Loc),
2851 Name => Obj_Acc_Deref));
2853 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
2857 -- Case where we do not build a block
2860 -- We're about to drop Return_Object_Declarations on the floor, so
2861 -- we need to insert it, in case it got expanded into useful code.
2863 Insert_List_Before (N, Return_Object_Declarations (N));
2865 -- Build simple_return_statement that returns the expression directly
2867 Return_Stm := Make_Return_Statement (Loc, Expression => Exp);
2869 Result := Return_Stm;
2872 -- Set the flag to prevent infinite recursion
2874 Set_Comes_From_Extended_Return_Statement (Return_Stm);
2876 Rewrite (N, Result);
2878 end Expand_N_Extended_Return_Statement;
2880 -----------------------------
2881 -- Expand_N_Goto_Statement --
2882 -----------------------------
2884 -- Add poll before goto if polling active
2886 procedure Expand_N_Goto_Statement (N : Node_Id) is
2888 Generate_Poll_Call (N);
2889 end Expand_N_Goto_Statement;
2891 ---------------------------
2892 -- Expand_N_If_Statement --
2893 ---------------------------
2895 -- First we deal with the case of C and Fortran convention boolean values,
2896 -- with zero/non-zero semantics.
2898 -- Second, we deal with the obvious rewriting for the cases where the
2899 -- condition of the IF is known at compile time to be True or False.
2901 -- Third, we remove elsif parts which have non-empty Condition_Actions
2902 -- and rewrite as independent if statements. For example:
2913 -- <<condition actions of y>>
2919 -- This rewriting is needed if at least one elsif part has a non-empty
2920 -- Condition_Actions list. We also do the same processing if there is a
2921 -- constant condition in an elsif part (in conjunction with the first
2922 -- processing step mentioned above, for the recursive call made to deal
2923 -- with the created inner if, this deals with properly optimizing the
2924 -- cases of constant elsif conditions).
2926 procedure Expand_N_If_Statement (N : Node_Id) is
2927 Loc : constant Source_Ptr := Sloc (N);
2933 Adjust_Condition (Condition (N));
2935 -- The following loop deals with constant conditions for the IF. We
2936 -- need a loop because as we eliminate False conditions, we grab the
2937 -- first elsif condition and use it as the primary condition.
2939 while Compile_Time_Known_Value (Condition (N)) loop
2941 -- If condition is True, we can simply rewrite the if statement now
2942 -- by replacing it by the series of then statements.
2944 if Is_True (Expr_Value (Condition (N))) then
2946 -- All the else parts can be killed
2948 Kill_Dead_Code (Elsif_Parts (N), Warn_On_Deleted_Code);
2949 Kill_Dead_Code (Else_Statements (N), Warn_On_Deleted_Code);
2951 Hed := Remove_Head (Then_Statements (N));
2952 Insert_List_After (N, Then_Statements (N));
2956 -- If condition is False, then we can delete the condition and
2957 -- the Then statements
2960 -- We do not delete the condition if constant condition warnings
2961 -- are enabled, since otherwise we end up deleting the desired
2962 -- warning. Of course the backend will get rid of this True/False
2963 -- test anyway, so nothing is lost here.
2965 if not Constant_Condition_Warnings then
2966 Kill_Dead_Code (Condition (N));
2969 Kill_Dead_Code (Then_Statements (N), Warn_On_Deleted_Code);
2971 -- If there are no elsif statements, then we simply replace the
2972 -- entire if statement by the sequence of else statements.
2974 if No (Elsif_Parts (N)) then
2975 if No (Else_Statements (N))
2976 or else Is_Empty_List (Else_Statements (N))
2979 Make_Null_Statement (Sloc (N)));
2981 Hed := Remove_Head (Else_Statements (N));
2982 Insert_List_After (N, Else_Statements (N));
2988 -- If there are elsif statements, the first of them becomes the
2989 -- if/then section of the rebuilt if statement This is the case
2990 -- where we loop to reprocess this copied condition.
2993 Hed := Remove_Head (Elsif_Parts (N));
2994 Insert_Actions (N, Condition_Actions (Hed));
2995 Set_Condition (N, Condition (Hed));
2996 Set_Then_Statements (N, Then_Statements (Hed));
2998 -- Hed might have been captured as the condition determining
2999 -- the current value for an entity. Now it is detached from
3000 -- the tree, so a Current_Value pointer in the condition might
3001 -- need to be updated.
3003 Set_Current_Value_Condition (N);
3005 if Is_Empty_List (Elsif_Parts (N)) then
3006 Set_Elsif_Parts (N, No_List);
3012 -- Loop through elsif parts, dealing with constant conditions and
3013 -- possible expression actions that are present.
3015 if Present (Elsif_Parts (N)) then
3016 E := First (Elsif_Parts (N));
3017 while Present (E) loop
3018 Adjust_Condition (Condition (E));
3020 -- If there are condition actions, then rewrite the if statement
3021 -- as indicated above. We also do the same rewrite for a True or
3022 -- False condition. The further processing of this constant
3023 -- condition is then done by the recursive call to expand the
3024 -- newly created if statement
3026 if Present (Condition_Actions (E))
3027 or else Compile_Time_Known_Value (Condition (E))
3029 -- Note this is not an implicit if statement, since it is part
3030 -- of an explicit if statement in the source (or of an implicit
3031 -- if statement that has already been tested).
3034 Make_If_Statement (Sloc (E),
3035 Condition => Condition (E),
3036 Then_Statements => Then_Statements (E),
3037 Elsif_Parts => No_List,
3038 Else_Statements => Else_Statements (N));
3040 -- Elsif parts for new if come from remaining elsif's of parent
3042 while Present (Next (E)) loop
3043 if No (Elsif_Parts (New_If)) then
3044 Set_Elsif_Parts (New_If, New_List);
3047 Append (Remove_Next (E), Elsif_Parts (New_If));
3050 Set_Else_Statements (N, New_List (New_If));
3052 if Present (Condition_Actions (E)) then
3053 Insert_List_Before (New_If, Condition_Actions (E));
3058 if Is_Empty_List (Elsif_Parts (N)) then
3059 Set_Elsif_Parts (N, No_List);
3065 -- No special processing for that elsif part, move to next
3073 -- Some more optimizations applicable if we still have an IF statement
3075 if Nkind (N) /= N_If_Statement then
3079 -- Another optimization, special cases that can be simplified
3081 -- if expression then
3087 -- can be changed to:
3089 -- return expression;
3093 -- if expression then
3099 -- can be changed to:
3101 -- return not (expression);
3103 if Nkind (N) = N_If_Statement
3104 and then No (Elsif_Parts (N))
3105 and then Present (Else_Statements (N))
3106 and then List_Length (Then_Statements (N)) = 1
3107 and then List_Length (Else_Statements (N)) = 1
3110 Then_Stm : constant Node_Id := First (Then_Statements (N));
3111 Else_Stm : constant Node_Id := First (Else_Statements (N));
3114 if Nkind (Then_Stm) = N_Return_Statement
3116 Nkind (Else_Stm) = N_Return_Statement
3119 Then_Expr : constant Node_Id := Expression (Then_Stm);
3120 Else_Expr : constant Node_Id := Expression (Else_Stm);
3123 if Nkind (Then_Expr) = N_Identifier
3125 Nkind (Else_Expr) = N_Identifier
3127 if Entity (Then_Expr) = Standard_True
3128 and then Entity (Else_Expr) = Standard_False
3131 Make_Return_Statement (Loc,
3132 Expression => Relocate_Node (Condition (N))));
3136 elsif Entity (Then_Expr) = Standard_False
3137 and then Entity (Else_Expr) = Standard_True
3140 Make_Return_Statement (Loc,
3143 Right_Opnd => Relocate_Node (Condition (N)))));
3152 end Expand_N_If_Statement;
3154 -----------------------------
3155 -- Expand_N_Loop_Statement --
3156 -----------------------------
3158 -- 1. Deal with while condition for C/Fortran boolean
3159 -- 2. Deal with loops with a non-standard enumeration type range
3160 -- 3. Deal with while loops where Condition_Actions is set
3161 -- 4. Insert polling call if required
3163 procedure Expand_N_Loop_Statement (N : Node_Id) is
3164 Loc : constant Source_Ptr := Sloc (N);
3165 Isc : constant Node_Id := Iteration_Scheme (N);
3168 if Present (Isc) then
3169 Adjust_Condition (Condition (Isc));
3172 if Is_Non_Empty_List (Statements (N)) then
3173 Generate_Poll_Call (First (Statements (N)));
3176 -- Nothing more to do for plain loop with no iteration scheme
3182 -- Note: we do not have to worry about validity chekcing of the for loop
3183 -- range bounds here, since they were frozen with constant declarations
3184 -- and it is during that process that the validity checking is done.
3186 -- Handle the case where we have a for loop with the range type being an
3187 -- enumeration type with non-standard representation. In this case we
3190 -- for x in [reverse] a .. b loop
3196 -- for xP in [reverse] integer
3197 -- range etype'Pos (a) .. etype'Pos (b) loop
3199 -- x : constant etype := Pos_To_Rep (xP);
3205 if Present (Loop_Parameter_Specification (Isc)) then
3207 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
3208 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
3209 Ltype : constant Entity_Id := Etype (Loop_Id);
3210 Btype : constant Entity_Id := Base_Type (Ltype);
3215 if not Is_Enumeration_Type (Btype)
3216 or else No (Enum_Pos_To_Rep (Btype))
3222 Make_Defining_Identifier (Loc,
3223 Chars => New_External_Name (Chars (Loop_Id), 'P'));
3225 -- If the type has a contiguous representation, successive values
3226 -- can be generated as offsets from the first literal.
3228 if Has_Contiguous_Rep (Btype) then
3230 Unchecked_Convert_To (Btype,
3233 Make_Integer_Literal (Loc,
3234 Enumeration_Rep (First_Literal (Btype))),
3235 Right_Opnd => New_Reference_To (New_Id, Loc)));
3237 -- Use the constructed array Enum_Pos_To_Rep
3240 Make_Indexed_Component (Loc,
3241 Prefix => New_Reference_To (Enum_Pos_To_Rep (Btype), Loc),
3242 Expressions => New_List (New_Reference_To (New_Id, Loc)));
3246 Make_Loop_Statement (Loc,
3247 Identifier => Identifier (N),
3250 Make_Iteration_Scheme (Loc,
3251 Loop_Parameter_Specification =>
3252 Make_Loop_Parameter_Specification (Loc,
3253 Defining_Identifier => New_Id,
3254 Reverse_Present => Reverse_Present (LPS),
3256 Discrete_Subtype_Definition =>
3257 Make_Subtype_Indication (Loc,
3260 New_Reference_To (Standard_Natural, Loc),
3263 Make_Range_Constraint (Loc,
3268 Make_Attribute_Reference (Loc,
3270 New_Reference_To (Btype, Loc),
3272 Attribute_Name => Name_Pos,
3274 Expressions => New_List (
3276 (Type_Low_Bound (Ltype)))),
3279 Make_Attribute_Reference (Loc,
3281 New_Reference_To (Btype, Loc),
3283 Attribute_Name => Name_Pos,
3285 Expressions => New_List (
3287 (Type_High_Bound (Ltype))))))))),
3289 Statements => New_List (
3290 Make_Block_Statement (Loc,
3291 Declarations => New_List (
3292 Make_Object_Declaration (Loc,
3293 Defining_Identifier => Loop_Id,
3294 Constant_Present => True,
3295 Object_Definition => New_Reference_To (Ltype, Loc),
3296 Expression => Expr)),
3298 Handled_Statement_Sequence =>
3299 Make_Handled_Sequence_Of_Statements (Loc,
3300 Statements => Statements (N)))),
3302 End_Label => End_Label (N)));
3306 -- Second case, if we have a while loop with Condition_Actions set, then
3307 -- we change it into a plain loop:
3316 -- <<condition actions>>
3322 and then Present (Condition_Actions (Isc))
3329 Make_Exit_Statement (Sloc (Condition (Isc)),
3331 Make_Op_Not (Sloc (Condition (Isc)),
3332 Right_Opnd => Condition (Isc)));
3334 Prepend (ES, Statements (N));
3335 Insert_List_Before (ES, Condition_Actions (Isc));
3337 -- This is not an implicit loop, since it is generated in response
3338 -- to the loop statement being processed. If this is itself
3339 -- implicit, the restriction has already been checked. If not,
3340 -- it is an explicit loop.
3343 Make_Loop_Statement (Sloc (N),
3344 Identifier => Identifier (N),
3345 Statements => Statements (N),
3346 End_Label => End_Label (N)));
3351 end Expand_N_Loop_Statement;
3353 -------------------------------
3354 -- Expand_N_Return_Statement --
3355 -------------------------------
3357 procedure Expand_N_Return_Statement (N : Node_Id) is
3358 Loc : constant Source_Ptr := Sloc (N);
3359 Exp : constant Node_Id := Expression (N);
3363 Scope_Id : Entity_Id;
3367 Goto_Stat : Node_Id;
3370 Return_Type : Entity_Id;
3371 Result_Exp : Node_Id;
3372 Result_Id : Entity_Id;
3373 Result_Obj : Node_Id;
3376 if Enable_New_Return_Processing then -- ???Temporary hack
3377 Expand_Simple_Return (N);
3381 -- Case where returned expression is present
3383 if Present (Exp) then
3385 -- Always normalize C/Fortran boolean result. This is not always
3386 -- necessary, but it seems a good idea to minimize the passing
3387 -- around of non-normalized values, and in any case this handles
3388 -- the processing of barrier functions for protected types, which
3389 -- turn the condition into a return statement.
3391 Exptyp := Etype (Exp);
3393 if Is_Boolean_Type (Exptyp)
3394 and then Nonzero_Is_True (Exptyp)
3396 Adjust_Condition (Exp);
3397 Adjust_Result_Type (Exp, Exptyp);
3400 -- Do validity check if enabled for returns
3402 if Validity_Checks_On
3403 and then Validity_Check_Returns
3409 -- Find relevant enclosing scope from which return is returning
3411 Cur_Idx := Scope_Stack.Last;
3413 Scope_Id := Scope_Stack.Table (Cur_Idx).Entity;
3415 if Ekind (Scope_Id) /= E_Block
3416 and then Ekind (Scope_Id) /= E_Loop
3421 Cur_Idx := Cur_Idx - 1;
3422 pragma Assert (Cur_Idx >= 0);
3425 -- ???I believe the above code is no longer necessary
3426 pragma Assert (Scope_Id =
3427 Return_Applies_To (Return_Statement_Entity (N)));
3430 Kind := Ekind (Scope_Id);
3432 -- If it is a return from procedures do no extra steps
3434 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
3438 pragma Assert (Is_Entry (Scope_Id));
3440 -- Look at the enclosing block to see whether the return is from an
3441 -- accept statement or an entry body.
3443 for J in reverse 0 .. Cur_Idx loop
3444 Scope_Id := Scope_Stack.Table (J).Entity;
3445 exit when Is_Concurrent_Type (Scope_Id);
3448 -- If it is a return from accept statement it should be expanded
3449 -- as a call to RTS Complete_Rendezvous and a goto to the end of
3452 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
3453 -- Expand_N_Accept_Alternative in exp_ch9.adb)
3455 if Is_Task_Type (Scope_Id) then
3457 Call := (Make_Procedure_Call_Statement (Loc,
3458 Name => New_Reference_To
3459 (RTE (RE_Complete_Rendezvous), Loc)));
3460 Insert_Before (N, Call);
3461 -- why not insert actions here???
3464 Acc_Stat := Parent (N);
3465 while Nkind (Acc_Stat) /= N_Accept_Statement loop
3466 Acc_Stat := Parent (Acc_Stat);
3469 Lab_Node := Last (Statements
3470 (Handled_Statement_Sequence (Acc_Stat)));
3472 Goto_Stat := Make_Goto_Statement (Loc,
3473 Name => New_Occurrence_Of
3474 (Entity (Identifier (Lab_Node)), Loc));
3476 Set_Analyzed (Goto_Stat);
3478 Rewrite (N, Goto_Stat);
3481 -- If it is a return from an entry body, put a Complete_Entry_Body
3482 -- call in front of the return.
3484 elsif Is_Protected_Type (Scope_Id) then
3487 Make_Procedure_Call_Statement (Loc,
3488 Name => New_Reference_To
3489 (RTE (RE_Complete_Entry_Body), Loc),
3490 Parameter_Associations => New_List
3491 (Make_Attribute_Reference (Loc,
3495 (Corresponding_Body (Parent (Scope_Id))),
3497 Attribute_Name => Name_Unchecked_Access)));
3499 Insert_Before (N, Call);
3507 Return_Type := Etype (Scope_Id);
3508 Utyp := Underlying_Type (Return_Type);
3510 -- Check the result expression of a scalar function against the subtype
3511 -- of the function by inserting a conversion. This conversion must
3512 -- eventually be performed for other classes of types, but for now it's
3513 -- only done for scalars. ???
3515 if Is_Scalar_Type (T) then
3516 Rewrite (Exp, Convert_To (Return_Type, Exp));
3520 -- Deal with returning variable length objects and controlled types
3522 -- Nothing to do if we are returning by reference, or this is not type
3523 -- that requires special processing (indicated by the fact that it
3524 -- requires a cleanup scope for the secondary stack case).
3526 if Is_Inherently_Limited_Type (T) then
3529 elsif not Requires_Transient_Scope (Return_Type) then
3531 -- Mutable records with no variable length components are not
3532 -- returned on the sec-stack, so we need to make sure that the
3533 -- backend will only copy back the size of the actual value, and not
3534 -- the maximum size. We create an actual subtype for this purpose.
3537 Ubt : constant Entity_Id := Underlying_Type (Base_Type (T));
3542 if Has_Discriminants (Ubt)
3543 and then not Is_Constrained (Ubt)
3544 and then not Has_Unchecked_Union (Ubt)
3546 Decl := Build_Actual_Subtype (Ubt, Exp);
3547 Ent := Defining_Identifier (Decl);
3548 Insert_Action (Exp, Decl);
3550 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
3551 Analyze_And_Resolve (Exp);
3555 -- Here if secondary stack is used
3558 -- Make sure that no surrounding block will reclaim the secondary
3559 -- stack on which we are going to put the result. Not only may this
3560 -- introduce secondary stack leaks but worse, if the reclamation is
3561 -- done too early, then the result we are returning may get
3562 -- clobbered. See example in 7417-003.
3565 S : Entity_Id := Current_Scope;
3568 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
3569 Set_Sec_Stack_Needed_For_Return (S, True);
3570 S := Enclosing_Dynamic_Scope (S);
3574 -- Optimize the case where the result is a function call. In this
3575 -- case either the result is already on the secondary stack, or is
3576 -- already being returned with the stack pointer depressed and no
3577 -- further processing is required except to set the By_Ref flag to
3578 -- ensure that gigi does not attempt an extra unnecessary copy
3579 -- (actually not just unnecessary but harmfully wrong in the case of
3580 -- a controlled type, where gigi does not know how to do a copy). To
3581 -- make up for a gcc 2.8.1 deficiency (???), we perform the copy for
3582 -- array types if the constrained status of the target type is
3583 -- different from that of the expression.
3585 if Requires_Transient_Scope (T)
3587 (not Is_Array_Type (T)
3588 or else Is_Constrained (T) = Is_Constrained (Return_Type)
3589 or else Is_Class_Wide_Type (Utyp)
3590 or else Controlled_Type (T))
3591 and then Nkind (Exp) = N_Function_Call
3595 -- Remove side effects from the expression now so that other parts
3596 -- of the expander do not have to reanalyze the node without this
3599 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
3601 -- For controlled types, do the allocation on the secondary stack
3602 -- manually in order to call adjust at the right time:
3604 -- type Anon1 is access Return_Type;
3605 -- for Anon1'Storage_pool use ss_pool;
3606 -- Anon2 : anon1 := new Return_Type'(expr);
3607 -- return Anon2.all;
3609 -- We do the same for classwide types that are not potentially
3610 -- controlled (by the virtue of restriction No_Finalization) because
3611 -- gigi is not able to properly allocate class-wide types.
3613 elsif CW_Or_Controlled_Type (Utyp) then
3615 Loc : constant Source_Ptr := Sloc (N);
3616 Temp : constant Entity_Id :=
3617 Make_Defining_Identifier (Loc,
3618 Chars => New_Internal_Name ('R'));
3619 Acc_Typ : constant Entity_Id :=
3620 Make_Defining_Identifier (Loc,
3621 Chars => New_Internal_Name ('A'));
3622 Alloc_Node : Node_Id;
3625 Set_Ekind (Acc_Typ, E_Access_Type);
3627 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
3630 Make_Allocator (Loc,
3632 Make_Qualified_Expression (Loc,
3633 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
3634 Expression => Relocate_Node (Exp)));
3636 Insert_List_Before_And_Analyze (N, New_List (
3637 Make_Full_Type_Declaration (Loc,
3638 Defining_Identifier => Acc_Typ,
3640 Make_Access_To_Object_Definition (Loc,
3641 Subtype_Indication =>
3642 New_Reference_To (Return_Type, Loc))),
3644 Make_Object_Declaration (Loc,
3645 Defining_Identifier => Temp,
3646 Object_Definition => New_Reference_To (Acc_Typ, Loc),
3647 Expression => Alloc_Node)));
3650 Make_Explicit_Dereference (Loc,
3651 Prefix => New_Reference_To (Temp, Loc)));
3653 Analyze_And_Resolve (Exp, Return_Type);
3656 -- Otherwise use the gigi mechanism to allocate result on the
3660 Set_Storage_Pool (N, RTE (RE_SS_Pool));
3662 -- If we are generating code for the Java VM do not use
3663 -- SS_Allocate since everything is heap-allocated anyway.
3666 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3671 -- Implement the rules of 6.5(8-10), which require a tag check in the
3672 -- case of a limited tagged return type, and tag reassignment for
3673 -- nonlimited tagged results. These actions are needed when the return
3674 -- type is a specific tagged type and the result expression is a
3675 -- conversion or a formal parameter, because in that case the tag of the
3676 -- expression might differ from the tag of the specific result type.
3678 if Is_Tagged_Type (Utyp)
3679 and then not Is_Class_Wide_Type (Utyp)
3680 and then (Nkind (Exp) = N_Type_Conversion
3681 or else Nkind (Exp) = N_Unchecked_Type_Conversion
3682 or else (Is_Entity_Name (Exp)
3683 and then Ekind (Entity (Exp)) in Formal_Kind))
3685 -- When the return type is limited, perform a check that the tag of
3686 -- the result is the same as the tag of the return type.
3688 if Is_Limited_Type (Return_Type) then
3690 Make_Raise_Constraint_Error (Loc,
3694 Make_Selected_Component (Loc,
3695 Prefix => Duplicate_Subexpr (Exp),
3697 New_Reference_To (First_Tag_Component (Utyp), Loc)),
3699 Unchecked_Convert_To (RTE (RE_Tag),
3702 (Access_Disp_Table (Base_Type (Utyp)))),
3704 Reason => CE_Tag_Check_Failed));
3706 -- If the result type is a specific nonlimited tagged type, then we
3707 -- have to ensure that the tag of the result is that of the result
3708 -- type. This is handled by making a copy of the expression in the
3709 -- case where it might have a different tag, namely when the
3710 -- expression is a conversion or a formal parameter. We create a new
3711 -- object of the result type and initialize it from the expression,
3712 -- which will implicitly force the tag to be set appropriately.
3716 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3717 Result_Exp := New_Reference_To (Result_Id, Loc);
3720 Make_Object_Declaration (Loc,
3721 Defining_Identifier => Result_Id,
3722 Object_Definition => New_Reference_To (Return_Type, Loc),
3723 Constant_Present => True,
3724 Expression => Relocate_Node (Exp));
3726 Set_Assignment_OK (Result_Obj);
3727 Insert_Action (Exp, Result_Obj);
3729 Rewrite (Exp, Result_Exp);
3730 Analyze_And_Resolve (Exp, Return_Type);
3733 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
3734 -- a check that the level of the return expression's underlying type
3735 -- is not deeper than the level of the master enclosing the function.
3736 -- Always generate the check when the type of the return expression
3737 -- is class-wide, when it's a type conversion, or when it's a formal
3738 -- parameter. Otherwise, suppress the check in the case where the
3739 -- return expression has a specific type whose level is known not to
3740 -- be statically deeper than the function's result type.
3742 elsif Ada_Version >= Ada_05
3743 and then Is_Class_Wide_Type (Return_Type)
3744 and then not Scope_Suppress (Accessibility_Check)
3746 (Is_Class_Wide_Type (Etype (Exp))
3747 or else Nkind (Exp) = N_Type_Conversion
3748 or else Nkind (Exp) = N_Unchecked_Type_Conversion
3749 or else (Is_Entity_Name (Exp)
3750 and then Ekind (Entity (Exp)) in Formal_Kind)
3751 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
3752 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
3755 Make_Raise_Program_Error (Loc,
3759 Build_Get_Access_Level (Loc,
3760 Make_Attribute_Reference (Loc,
3761 Prefix => Duplicate_Subexpr (Exp),
3762 Attribute_Name => Name_Tag)),
3764 Make_Integer_Literal (Loc,
3765 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
3766 Reason => PE_Accessibility_Check_Failed));
3770 when RE_Not_Available =>
3772 end Expand_N_Return_Statement;
3774 --------------------------------
3775 -- Expand_Non_Function_Return --
3776 --------------------------------
3778 procedure Expand_Non_Function_Return (N : Node_Id) is
3779 pragma Assert (No (Expression (N)));
3781 Loc : constant Source_Ptr := Sloc (N);
3782 Scope_Id : Entity_Id :=
3783 Return_Applies_To (Return_Statement_Entity (N));
3784 Kind : constant Entity_Kind := Ekind (Scope_Id);
3787 Goto_Stat : Node_Id;
3791 -- If it is a return from procedures do no extra steps
3793 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
3796 -- If it is a nested return within an extended one, replace it with a
3797 -- return of the previously declared return object.
3799 elsif Kind = E_Return_Statement then
3801 Make_Return_Statement (Loc,
3803 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
3804 Set_Comes_From_Extended_Return_Statement (N);
3805 Set_Return_Statement_Entity (N, Scope_Id);
3806 Expand_Simple_Function_Return (N);
3810 pragma Assert (Is_Entry (Scope_Id));
3812 -- Look at the enclosing block to see whether the return is from an
3813 -- accept statement or an entry body.
3815 for J in reverse 0 .. Scope_Stack.Last loop
3816 Scope_Id := Scope_Stack.Table (J).Entity;
3817 exit when Is_Concurrent_Type (Scope_Id);
3820 -- If it is a return from accept statement it is expanded as call to
3821 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
3823 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
3824 -- Expand_N_Accept_Alternative in exp_ch9.adb)
3826 if Is_Task_Type (Scope_Id) then
3829 Make_Procedure_Call_Statement (Loc,
3830 Name => New_Reference_To
3831 (RTE (RE_Complete_Rendezvous), Loc));
3832 Insert_Before (N, Call);
3833 -- why not insert actions here???
3836 Acc_Stat := Parent (N);
3837 while Nkind (Acc_Stat) /= N_Accept_Statement loop
3838 Acc_Stat := Parent (Acc_Stat);
3841 Lab_Node := Last (Statements
3842 (Handled_Statement_Sequence (Acc_Stat)));
3844 Goto_Stat := Make_Goto_Statement (Loc,
3845 Name => New_Occurrence_Of
3846 (Entity (Identifier (Lab_Node)), Loc));
3848 Set_Analyzed (Goto_Stat);
3850 Rewrite (N, Goto_Stat);
3853 -- If it is a return from an entry body, put a Complete_Entry_Body call
3854 -- in front of the return.
3856 elsif Is_Protected_Type (Scope_Id) then
3858 Make_Procedure_Call_Statement (Loc,
3859 Name => New_Reference_To
3860 (RTE (RE_Complete_Entry_Body), Loc),
3861 Parameter_Associations => New_List
3862 (Make_Attribute_Reference (Loc,
3866 (Corresponding_Body (Parent (Scope_Id))),
3868 Attribute_Name => Name_Unchecked_Access)));
3870 Insert_Before (N, Call);
3873 end Expand_Non_Function_Return;
3875 --------------------------
3876 -- Expand_Simple_Return --
3877 --------------------------
3879 procedure Expand_Simple_Return (N : Node_Id) is
3881 -- Distinguish the function and non-function cases:
3883 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
3886 E_Generic_Function =>
3887 Expand_Simple_Function_Return (N);
3890 E_Generic_Procedure |
3893 E_Return_Statement =>
3894 Expand_Non_Function_Return (N);
3897 raise Program_Error;
3901 when RE_Not_Available =>
3903 end Expand_Simple_Return;
3905 -----------------------------------
3906 -- Expand_Simple_Function_Return --
3907 -----------------------------------
3909 -- The "simple" comes from the syntax rule simple_return_statement.
3910 -- The semantics are not at all simple!
3912 procedure Expand_Simple_Function_Return (N : Node_Id) is
3913 Loc : constant Source_Ptr := Sloc (N);
3915 Scope_Id : constant Entity_Id :=
3916 Return_Applies_To (Return_Statement_Entity (N));
3917 -- The function we are returning from
3919 R_Type : constant Entity_Id := Etype (Scope_Id);
3920 -- The result type of the function
3922 Utyp : constant Entity_Id := Underlying_Type (R_Type);
3924 Exp : constant Node_Id := Expression (N);
3925 pragma Assert (Present (Exp));
3927 Exptyp : constant Entity_Id := Etype (Exp);
3928 -- The type of the expression (not necessarily the same as R_Type)
3931 -- We rewrite "return <expression>;" to be:
3933 -- return _anon_ : <return_subtype> := <expression>
3935 -- The expansion produced by Expand_N_Extended_Return_Statement will
3936 -- contain simple return statements (for example, a block containing
3937 -- simple return of the return object), which brings us back here with
3938 -- Comes_From_Extended_Return_Statement set. To avoid infinite
3939 -- recursion, we do not transform into an extended return if
3940 -- Comes_From_Extended_Return_Statement is True.
3942 -- The reason for this design is that for Ada 2005 limited returns, we
3943 -- need to reify the return object, so we can build it "in place", and
3944 -- we need a block statement to hang finalization and tasking stuff.
3946 -- ??? In order to avoid disruption, we avoid translating to extended
3947 -- return except in the cases where we really need to (Ada 2005
3948 -- inherently limited). We would prefer eventually to do this
3949 -- translation in all cases except perhaps for the case of Ada 95
3950 -- inherently limited, in order to fully exercise the code in
3951 -- Expand_N_Extended_Return_Statement, and in order to do
3952 -- build-in-place for efficiency when it is not required.
3954 if not Comes_From_Extended_Return_Statement (N)
3955 and then Is_Inherently_Limited_Type (R_Type) -- ???
3956 and then Ada_Version >= Ada_05 -- ???
3957 and then not Debug_Flag_Dot_L
3960 Return_Object_Entity : constant Entity_Id :=
3961 Make_Defining_Identifier (Loc,
3962 New_Internal_Name ('R'));
3964 Subtype_Ind : constant Node_Id := New_Occurrence_Of (R_Type, Loc);
3966 Obj_Decl : constant Node_Id :=
3967 Make_Object_Declaration (Loc,
3968 Defining_Identifier => Return_Object_Entity,
3969 Object_Definition => Subtype_Ind,
3972 Ext : constant Node_Id := Make_Extended_Return_Statement (Loc,
3973 Return_Object_Declarations => New_List (Obj_Decl));
3982 -- Here we have a simple return statement that is part of the expansion
3983 -- of an extended return statement (either written by the user, or
3984 -- generated by the above code).
3986 -- Always normalize C/Fortran boolean result. This is not always needed,
3987 -- but it seems a good idea to minimize the passing around of non-
3988 -- normalized values, and in any case this handles the processing of
3989 -- barrier functions for protected types, which turn the condition into
3990 -- a return statement.
3992 if Is_Boolean_Type (Exptyp)
3993 and then Nonzero_Is_True (Exptyp)
3995 Adjust_Condition (Exp);
3996 Adjust_Result_Type (Exp, Exptyp);
3999 -- Do validity check if enabled for returns
4001 if Validity_Checks_On
4002 and then Validity_Check_Returns
4007 -- Check the result expression of a scalar function against the subtype
4008 -- of the function by inserting a conversion. This conversion must
4009 -- eventually be performed for other classes of types, but for now it's
4010 -- only done for scalars.
4013 if Is_Scalar_Type (Exptyp) then
4014 Rewrite (Exp, Convert_To (R_Type, Exp));
4018 -- Deal with returning variable length objects and controlled types
4020 -- Nothing to do if we are returning by reference, or this is not a
4021 -- type that requires special processing (indicated by the fact that
4022 -- it requires a cleanup scope for the secondary stack case).
4024 if Is_Inherently_Limited_Type (Exptyp) then
4027 elsif not Requires_Transient_Scope (R_Type) then
4029 -- Mutable records with no variable length components are not
4030 -- returned on the sec-stack, so we need to make sure that the
4031 -- backend will only copy back the size of the actual value, and not
4032 -- the maximum size. We create an actual subtype for this purpose.
4035 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
4039 if Has_Discriminants (Ubt)
4040 and then not Is_Constrained (Ubt)
4041 and then not Has_Unchecked_Union (Ubt)
4043 Decl := Build_Actual_Subtype (Ubt, Exp);
4044 Ent := Defining_Identifier (Decl);
4045 Insert_Action (Exp, Decl);
4046 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
4047 Analyze_And_Resolve (Exp);
4051 -- Here if secondary stack is used
4054 -- Make sure that no surrounding block will reclaim the secondary
4055 -- stack on which we are going to put the result. Not only may this
4056 -- introduce secondary stack leaks but worse, if the reclamation is
4057 -- done too early, then the result we are returning may get
4058 -- clobbered. See example in 7417-003.
4064 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
4065 Set_Sec_Stack_Needed_For_Return (S, True);
4066 S := Enclosing_Dynamic_Scope (S);
4070 -- Optimize the case where the result is a function call. In this
4071 -- case either the result is already on the secondary stack, or is
4072 -- already being returned with the stack pointer depressed and no
4073 -- further processing is required except to set the By_Ref flag to
4074 -- ensure that gigi does not attempt an extra unnecessary copy.
4075 -- (actually not just unnecessary but harmfully wrong in the case
4076 -- of a controlled type, where gigi does not know how to do a copy).
4077 -- To make up for a gcc 2.8.1 deficiency (???), we perform
4078 -- the copy for array types if the constrained status of the
4079 -- target type is different from that of the expression.
4081 if Requires_Transient_Scope (Exptyp)
4083 (not Is_Array_Type (Exptyp)
4084 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
4085 or else CW_Or_Controlled_Type (Utyp))
4086 and then Nkind (Exp) = N_Function_Call
4090 -- Remove side effects from the expression now so that other parts
4091 -- of the expander do not have to reanalyze this node without this
4094 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
4096 -- For controlled types, do the allocation on the secondary stack
4097 -- manually in order to call adjust at the right time:
4099 -- type Anon1 is access R_Type;
4100 -- for Anon1'Storage_pool use ss_pool;
4101 -- Anon2 : anon1 := new R_Type'(expr);
4102 -- return Anon2.all;
4104 -- We do the same for classwide types that are not potentially
4105 -- controlled (by the virtue of restriction No_Finalization) because
4106 -- gigi is not able to properly allocate class-wide types.
4108 elsif CW_Or_Controlled_Type (Utyp) then
4110 Loc : constant Source_Ptr := Sloc (N);
4111 Temp : constant Entity_Id :=
4112 Make_Defining_Identifier (Loc,
4113 Chars => New_Internal_Name ('R'));
4114 Acc_Typ : constant Entity_Id :=
4115 Make_Defining_Identifier (Loc,
4116 Chars => New_Internal_Name ('A'));
4117 Alloc_Node : Node_Id;
4120 Set_Ekind (Acc_Typ, E_Access_Type);
4122 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
4125 Make_Allocator (Loc,
4127 Make_Qualified_Expression (Loc,
4128 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
4129 Expression => Relocate_Node (Exp)));
4131 Insert_List_Before_And_Analyze (N, New_List (
4132 Make_Full_Type_Declaration (Loc,
4133 Defining_Identifier => Acc_Typ,
4135 Make_Access_To_Object_Definition (Loc,
4136 Subtype_Indication =>
4137 New_Reference_To (R_Type, Loc))),
4139 Make_Object_Declaration (Loc,
4140 Defining_Identifier => Temp,
4141 Object_Definition => New_Reference_To (Acc_Typ, Loc),
4142 Expression => Alloc_Node)));
4145 Make_Explicit_Dereference (Loc,
4146 Prefix => New_Reference_To (Temp, Loc)));
4148 Analyze_And_Resolve (Exp, R_Type);
4151 -- Otherwise use the gigi mechanism to allocate result on the
4155 Set_Storage_Pool (N, RTE (RE_SS_Pool));
4157 -- If we are generating code for the Java VM do not use
4158 -- SS_Allocate since everything is heap-allocated anyway.
4161 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4166 -- Implement the rules of 6.5(8-10), which require a tag check in the
4167 -- case of a limited tagged return type, and tag reassignment for
4168 -- nonlimited tagged results. These actions are needed when the return
4169 -- type is a specific tagged type and the result expression is a
4170 -- conversion or a formal parameter, because in that case the tag of the
4171 -- expression might differ from the tag of the specific result type.
4173 if Is_Tagged_Type (Utyp)
4174 and then not Is_Class_Wide_Type (Utyp)
4175 and then (Nkind (Exp) = N_Type_Conversion
4176 or else Nkind (Exp) = N_Unchecked_Type_Conversion
4177 or else (Is_Entity_Name (Exp)
4178 and then Ekind (Entity (Exp)) in Formal_Kind))
4180 -- When the return type is limited, perform a check that the
4181 -- tag of the result is the same as the tag of the return type.
4183 if Is_Limited_Type (R_Type) then
4185 Make_Raise_Constraint_Error (Loc,
4189 Make_Selected_Component (Loc,
4190 Prefix => Duplicate_Subexpr (Exp),
4192 New_Reference_To (First_Tag_Component (Utyp), Loc)),
4194 Unchecked_Convert_To (RTE (RE_Tag),
4197 (Access_Disp_Table (Base_Type (Utyp)))),
4199 Reason => CE_Tag_Check_Failed));
4201 -- If the result type is a specific nonlimited tagged type, then we
4202 -- have to ensure that the tag of the result is that of the result
4203 -- type. This is handled by making a copy of the expression in the
4204 -- case where it might have a different tag, namely when the
4205 -- expression is a conversion or a formal parameter. We create a new
4206 -- object of the result type and initialize it from the expression,
4207 -- which will implicitly force the tag to be set appropriately.
4211 Result_Id : constant Entity_Id :=
4212 Make_Defining_Identifier (Loc,
4213 Chars => New_Internal_Name ('R'));
4214 Result_Exp : constant Node_Id :=
4215 New_Reference_To (Result_Id, Loc);
4216 Result_Obj : constant Node_Id :=
4217 Make_Object_Declaration (Loc,
4218 Defining_Identifier => Result_Id,
4219 Object_Definition =>
4220 New_Reference_To (R_Type, Loc),
4221 Constant_Present => True,
4222 Expression => Relocate_Node (Exp));
4225 Set_Assignment_OK (Result_Obj);
4226 Insert_Action (Exp, Result_Obj);
4228 Rewrite (Exp, Result_Exp);
4229 Analyze_And_Resolve (Exp, R_Type);
4233 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
4234 -- a check that the level of the return expression's underlying type
4235 -- is not deeper than the level of the master enclosing the function.
4236 -- Always generate the check when the type of the return expression
4237 -- is class-wide, when it's a type conversion, or when it's a formal
4238 -- parameter. Otherwise, suppress the check in the case where the
4239 -- return expression has a specific type whose level is known not to
4240 -- be statically deeper than the function's result type.
4242 elsif Ada_Version >= Ada_05
4243 and then Is_Class_Wide_Type (R_Type)
4244 and then not Scope_Suppress (Accessibility_Check)
4246 (Is_Class_Wide_Type (Etype (Exp))
4247 or else Nkind (Exp) = N_Type_Conversion
4248 or else Nkind (Exp) = N_Unchecked_Type_Conversion
4249 or else (Is_Entity_Name (Exp)
4250 and then Ekind (Entity (Exp)) in Formal_Kind)
4251 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
4252 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
4255 Make_Raise_Program_Error (Loc,
4259 Build_Get_Access_Level (Loc,
4260 Make_Attribute_Reference (Loc,
4261 Prefix => Duplicate_Subexpr (Exp),
4262 Attribute_Name => Name_Tag)),
4264 Make_Integer_Literal (Loc,
4265 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
4266 Reason => PE_Accessibility_Check_Failed));
4268 end Expand_Simple_Function_Return;
4270 ------------------------------
4271 -- Make_Tag_Ctrl_Assignment --
4272 ------------------------------
4274 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id is
4275 Loc : constant Source_Ptr := Sloc (N);
4276 L : constant Node_Id := Name (N);
4277 T : constant Entity_Id := Underlying_Type (Etype (L));
4279 Ctrl_Act : constant Boolean := Controlled_Type (T)
4280 and then not No_Ctrl_Actions (N);
4282 Save_Tag : constant Boolean := Is_Tagged_Type (T)
4283 and then not No_Ctrl_Actions (N)
4284 and then not Java_VM;
4285 -- Tags are not saved and restored when Java_VM because JVM tags are
4286 -- represented implicitly in objects.
4289 Tag_Tmp : Entity_Id;
4294 -- Finalize the target of the assignment when controlled.
4295 -- We have two exceptions here:
4297 -- 1. If we are in an init proc since it is an initialization
4298 -- more than an assignment
4300 -- 2. If the left-hand side is a temporary that was not initialized
4301 -- (or the parent part of a temporary since it is the case in
4302 -- extension aggregates). Such a temporary does not come from
4303 -- source. We must examine the original node for the prefix, because
4304 -- it may be a component of an entry formal, in which case it has
4305 -- been rewritten and does not appear to come from source either.
4307 -- Case of init proc
4309 if not Ctrl_Act then
4312 -- The left hand side is an uninitialized temporary
4314 elsif Nkind (L) = N_Type_Conversion
4315 and then Is_Entity_Name (Expression (L))
4316 and then No_Initialization (Parent (Entity (Expression (L))))
4320 Append_List_To (Res,
4322 Ref => Duplicate_Subexpr_No_Checks (L),
4324 With_Detach => New_Reference_To (Standard_False, Loc)));
4327 -- Save the Tag in a local variable Tag_Tmp
4331 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4334 Make_Object_Declaration (Loc,
4335 Defining_Identifier => Tag_Tmp,
4336 Object_Definition => New_Reference_To (RTE (RE_Tag), Loc),
4338 Make_Selected_Component (Loc,
4339 Prefix => Duplicate_Subexpr_No_Checks (L),
4340 Selector_Name => New_Reference_To (First_Tag_Component (T),
4343 -- Otherwise Tag_Tmp not used
4349 -- Processing for controlled types and types with controlled components
4351 -- Variables of such types contain pointers used to chain them in
4352 -- finalization lists, in addition to user data. These pointers are
4353 -- specific to each object of the type, not to the value being assigned.
4354 -- Thus they need to be left intact during the assignment. We achieve
4355 -- this by constructing a Storage_Array subtype, and by overlaying
4356 -- objects of this type on the source and target of the assignment. The
4357 -- assignment is then rewritten to assignments of slices of these
4358 -- arrays, copying the user data, and leaving the pointers untouched.
4361 Controlled_Actions : declare
4363 -- A reference to the Prev component of the record controller
4365 First_After_Root : Node_Id := Empty;
4366 -- Index of first byte to be copied (used to skip
4367 -- Root_Controlled in controlled objects).
4369 Last_Before_Hole : Node_Id := Empty;
4370 -- Index of last byte to be copied before outermost record
4373 Hole_Length : Node_Id := Empty;
4374 -- Length of record controller data (Prev and Next pointers)
4376 First_After_Hole : Node_Id := Empty;
4377 -- Index of first byte to be copied after outermost record
4380 Expr, Source_Size : Node_Id;
4381 Source_Actual_Subtype : Entity_Id;
4382 -- Used for computation of the size of the data to be copied
4384 Range_Type : Entity_Id;
4385 Opaque_Type : Entity_Id;
4387 function Build_Slice
4390 Hi : Node_Id) return Node_Id;
4391 -- Build and return a slice of an array of type S overlaid on
4392 -- object Rec, with bounds specified by Lo and Hi. If either bound
4393 -- is empty, a default of S'First (respectively S'Last) is used.
4399 function Build_Slice
4402 Hi : Node_Id) return Node_Id
4407 Opaque : constant Node_Id :=
4408 Unchecked_Convert_To (Opaque_Type,
4409 Make_Attribute_Reference (Loc,
4411 Attribute_Name => Name_Address));
4412 -- Access value designating an opaque storage array of type S
4413 -- overlaid on record Rec.
4416 -- Compute slice bounds using S'First (1) and S'Last as default
4417 -- values when not specified by the caller.
4420 Lo_Bound := Make_Integer_Literal (Loc, 1);
4426 Hi_Bound := Make_Attribute_Reference (Loc,
4427 Prefix => New_Occurrence_Of (Range_Type, Loc),
4428 Attribute_Name => Name_Last);
4433 return Make_Slice (Loc,
4436 Discrete_Range => Make_Range (Loc,
4437 Lo_Bound, Hi_Bound));
4440 -- Start of processing for Controlled_Actions
4443 -- Create a constrained subtype of Storage_Array whose size
4444 -- corresponds to the value being assigned.
4446 -- subtype G is Storage_Offset range
4447 -- 1 .. (Expr'Size + Storage_Unit - 1) / Storage_Unit
4449 Expr := Duplicate_Subexpr_No_Checks (Expression (N));
4451 if Nkind (Expr) = N_Qualified_Expression then
4452 Expr := Expression (Expr);
4455 Source_Actual_Subtype := Etype (Expr);
4457 if Has_Discriminants (Source_Actual_Subtype)
4458 and then not Is_Constrained (Source_Actual_Subtype)
4461 Build_Actual_Subtype (Source_Actual_Subtype, Expr));
4462 Source_Actual_Subtype := Defining_Identifier (Last (Res));
4468 Make_Attribute_Reference (Loc,
4470 New_Occurrence_Of (Source_Actual_Subtype, Loc),
4474 Make_Integer_Literal (Loc,
4475 System_Storage_Unit - 1));
4477 Make_Op_Divide (Loc,
4478 Left_Opnd => Source_Size,
4480 Make_Integer_Literal (Loc,
4481 Intval => System_Storage_Unit));
4484 Make_Defining_Identifier (Loc,
4485 New_Internal_Name ('G'));
4488 Make_Subtype_Declaration (Loc,
4489 Defining_Identifier => Range_Type,
4490 Subtype_Indication =>
4491 Make_Subtype_Indication (Loc,
4493 New_Reference_To (RTE (RE_Storage_Offset), Loc),
4494 Constraint => Make_Range_Constraint (Loc,
4497 Low_Bound => Make_Integer_Literal (Loc, 1),
4498 High_Bound => Source_Size)))));
4500 -- subtype S is Storage_Array (G)
4503 Make_Subtype_Declaration (Loc,
4504 Defining_Identifier =>
4505 Make_Defining_Identifier (Loc,
4506 New_Internal_Name ('S')),
4507 Subtype_Indication =>
4508 Make_Subtype_Indication (Loc,
4510 New_Reference_To (RTE (RE_Storage_Array), Loc),
4512 Make_Index_Or_Discriminant_Constraint (Loc,
4514 New_List (New_Reference_To (Range_Type, Loc))))));
4516 -- type A is access S
4519 Make_Defining_Identifier (Loc,
4520 Chars => New_Internal_Name ('A'));
4523 Make_Full_Type_Declaration (Loc,
4524 Defining_Identifier => Opaque_Type,
4526 Make_Access_To_Object_Definition (Loc,
4527 Subtype_Indication =>
4529 Defining_Identifier (Last (Res)), Loc))));
4531 -- Generate appropriate slice assignments
4533 First_After_Root := Make_Integer_Literal (Loc, 1);
4535 -- For the case of a controlled object, skip the
4536 -- Root_Controlled part.
4538 if Is_Controlled (T) then
4542 Make_Op_Divide (Loc,
4543 Make_Attribute_Reference (Loc,
4545 New_Occurrence_Of (RTE (RE_Root_Controlled), Loc),
4546 Attribute_Name => Name_Size),
4547 Make_Integer_Literal (Loc, System_Storage_Unit)));
4550 -- For the case of a record with controlled components, skip
4551 -- the Prev and Next components of the record controller.
4552 -- These components constitute a 'hole' in the middle of the
4553 -- data to be copied.
4555 if Has_Controlled_Component (T) then
4557 Make_Selected_Component (Loc,
4559 Make_Selected_Component (Loc,
4560 Prefix => Duplicate_Subexpr_No_Checks (L),
4562 New_Reference_To (Controller_Component (T), Loc)),
4563 Selector_Name => Make_Identifier (Loc, Name_Prev));
4565 -- Last index before hole: determined by position of
4566 -- the _Controller.Prev component.
4569 Make_Defining_Identifier (Loc,
4570 New_Internal_Name ('L'));
4573 Make_Object_Declaration (Loc,
4574 Defining_Identifier => Last_Before_Hole,
4575 Object_Definition => New_Occurrence_Of (
4576 RTE (RE_Storage_Offset), Loc),
4577 Constant_Present => True,
4578 Expression => Make_Op_Add (Loc,
4579 Make_Attribute_Reference (Loc,
4581 Attribute_Name => Name_Position),
4582 Make_Attribute_Reference (Loc,
4583 Prefix => New_Copy_Tree (Prefix (Prev_Ref)),
4584 Attribute_Name => Name_Position))));
4586 -- Hole length: size of the Prev and Next components
4589 Make_Op_Multiply (Loc,
4590 Left_Opnd => Make_Integer_Literal (Loc, Uint_2),
4592 Make_Op_Divide (Loc,
4594 Make_Attribute_Reference (Loc,
4595 Prefix => New_Copy_Tree (Prev_Ref),
4596 Attribute_Name => Name_Size),
4598 Make_Integer_Literal (Loc,
4599 Intval => System_Storage_Unit)));
4601 -- First index after hole
4604 Make_Defining_Identifier (Loc,
4605 New_Internal_Name ('F'));
4608 Make_Object_Declaration (Loc,
4609 Defining_Identifier => First_After_Hole,
4610 Object_Definition => New_Occurrence_Of (
4611 RTE (RE_Storage_Offset), Loc),
4612 Constant_Present => True,
4618 New_Occurrence_Of (Last_Before_Hole, Loc),
4619 Right_Opnd => Hole_Length),
4620 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4622 Last_Before_Hole := New_Occurrence_Of (Last_Before_Hole, Loc);
4623 First_After_Hole := New_Occurrence_Of (First_After_Hole, Loc);
4626 -- Assign the first slice (possibly skipping Root_Controlled,
4627 -- up to the beginning of the record controller if present,
4628 -- up to the end of the object if not).
4630 Append_To (Res, Make_Assignment_Statement (Loc,
4631 Name => Build_Slice (
4632 Rec => Duplicate_Subexpr_No_Checks (L),
4633 Lo => First_After_Root,
4634 Hi => Last_Before_Hole),
4636 Expression => Build_Slice (
4637 Rec => Expression (N),
4638 Lo => First_After_Root,
4639 Hi => New_Copy_Tree (Last_Before_Hole))));
4641 if Present (First_After_Hole) then
4643 -- If a record controller is present, copy the second slice,
4644 -- from right after the _Controller.Next component up to the
4645 -- end of the object.
4647 Append_To (Res, Make_Assignment_Statement (Loc,
4648 Name => Build_Slice (
4649 Rec => Duplicate_Subexpr_No_Checks (L),
4650 Lo => First_After_Hole,
4652 Expression => Build_Slice (
4653 Rec => Duplicate_Subexpr_No_Checks (Expression (N)),
4654 Lo => New_Copy_Tree (First_After_Hole),
4657 end Controlled_Actions;
4660 Append_To (Res, Relocate_Node (N));
4667 Make_Assignment_Statement (Loc,
4669 Make_Selected_Component (Loc,
4670 Prefix => Duplicate_Subexpr_No_Checks (L),
4671 Selector_Name => New_Reference_To (First_Tag_Component (T),
4673 Expression => New_Reference_To (Tag_Tmp, Loc)));
4676 -- Adjust the target after the assignment when controlled (not in the
4677 -- init proc since it is an initialization more than an assignment).
4680 Append_List_To (Res,
4682 Ref => Duplicate_Subexpr_Move_Checks (L),
4684 Flist_Ref => New_Reference_To (RTE (RE_Global_Final_List), Loc),
4685 With_Attach => Make_Integer_Literal (Loc, 0)));
4691 -- Could use comment here ???
4693 when RE_Not_Available =>
4695 end Make_Tag_Ctrl_Assignment;
4697 ------------------------------------
4698 -- Possible_Bit_Aligned_Component --
4699 ------------------------------------
4701 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
4705 -- Case of indexed component
4707 when N_Indexed_Component =>
4709 P : constant Node_Id := Prefix (N);
4710 Ptyp : constant Entity_Id := Etype (P);
4713 -- If we know the component size and it is less than 64, then
4714 -- we are definitely OK. The back end always does assignment
4715 -- of misaligned small objects correctly.
4717 if Known_Static_Component_Size (Ptyp)
4718 and then Component_Size (Ptyp) <= 64
4722 -- Otherwise, we need to test the prefix, to see if we are
4723 -- indexing from a possibly unaligned component.
4726 return Possible_Bit_Aligned_Component (P);
4730 -- Case of selected component
4732 when N_Selected_Component =>
4734 P : constant Node_Id := Prefix (N);
4735 Comp : constant Entity_Id := Entity (Selector_Name (N));
4738 -- If there is no component clause, then we are in the clear
4739 -- since the back end will never misalign a large component
4740 -- unless it is forced to do so. In the clear means we need
4741 -- only the recursive test on the prefix.
4743 if Component_May_Be_Bit_Aligned (Comp) then
4746 return Possible_Bit_Aligned_Component (P);
4750 -- If we have neither a record nor array component, it means that we
4751 -- have fallen off the top testing prefixes recursively, and we now
4752 -- have a stand alone object, where we don't have a problem.
4758 end Possible_Bit_Aligned_Component;