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