2012-11-06 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / exp_ch5.adb
blob80aabc5acd861e47b8a9daee84b606ae7658a695
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-2012, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Aggr; use Exp_Aggr;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch11; use Exp_Ch11;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Pakd; use Exp_Pakd;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Namet; use Namet;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sinfo; use Sinfo;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Res; use Sem_Res;
56 with Sem_Util; use Sem_Util;
57 with Snames; use Snames;
58 with Stand; use Stand;
59 with Stringt; use Stringt;
60 with Targparm; use Targparm;
61 with Tbuild; use Tbuild;
62 with Validsw; use Validsw;
64 package body Exp_Ch5 is
66 function Change_Of_Representation (N : Node_Id) return Boolean;
67 -- Determine if the right hand side of assignment N is a type conversion
68 -- which requires a change of representation. Called only for the array
69 -- and record cases.
71 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id);
72 -- N is an assignment which assigns an array value. This routine process
73 -- the various special cases and checks required for such assignments,
74 -- including change of representation. Rhs is normally simply the right
75 -- hand side of the assignment, except that if the right hand side is a
76 -- type conversion or a qualified expression, then the RHS is the actual
77 -- expression inside any such type conversions or qualifications.
79 function Expand_Assign_Array_Loop
80 (N : Node_Id;
81 Larray : Entity_Id;
82 Rarray : Entity_Id;
83 L_Type : Entity_Id;
84 R_Type : Entity_Id;
85 Ndim : Pos;
86 Rev : Boolean) return Node_Id;
87 -- N is an assignment statement which assigns an array value. This routine
88 -- expands the assignment into a loop (or nested loops for the case of a
89 -- multi-dimensional array) to do the assignment component by component.
90 -- Larray and Rarray are the entities of the actual arrays on the left
91 -- hand and right hand sides. L_Type and R_Type are the types of these
92 -- arrays (which may not be the same, due to either sliding, or to a
93 -- change of representation case). Ndim is the number of dimensions and
94 -- the parameter Rev indicates if the loops run normally (Rev = False),
95 -- or reversed (Rev = True). The value returned is the constructed
96 -- loop statement. Auxiliary declarations are inserted before node N
97 -- using the standard Insert_Actions mechanism.
99 procedure Expand_Assign_Record (N : Node_Id);
100 -- N is an assignment of a non-tagged record value. This routine handles
101 -- the case where the assignment must be made component by component,
102 -- either because the target is not byte aligned, or there is a change
103 -- of representation, or when we have a tagged type with a representation
104 -- clause (this last case is required because holes in the tagged type
105 -- might be filled with components from child types).
107 procedure Expand_Iterator_Loop (N : Node_Id);
108 -- Expand loop over arrays and containers that uses the form "for X of C"
109 -- with an optional subtype mark, or "for Y in C".
111 procedure Expand_Iterator_Loop_Over_Array (N : Node_Id);
112 -- Expand loop over arrays that uses the form "for X of C"
114 procedure Expand_Loop_Entry_Attributes (N : Node_Id);
115 -- Given a loop statement subject to at least one Loop_Entry attribute,
116 -- expand both the loop and all related Loop_Entry references.
118 procedure Expand_Predicated_Loop (N : Node_Id);
119 -- Expand for loop over predicated subtype
121 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id;
122 -- Generate the necessary code for controlled and tagged assignment, that
123 -- is to say, finalization of the target before, adjustment of the target
124 -- after and save and restore of the tag and finalization pointers which
125 -- are not 'part of the value' and must not be changed upon assignment. N
126 -- is the original Assignment node.
128 ------------------------------
129 -- Change_Of_Representation --
130 ------------------------------
132 function Change_Of_Representation (N : Node_Id) return Boolean is
133 Rhs : constant Node_Id := Expression (N);
134 begin
135 return
136 Nkind (Rhs) = N_Type_Conversion
137 and then
138 not Same_Representation (Etype (Rhs), Etype (Expression (Rhs)));
139 end Change_Of_Representation;
141 -------------------------
142 -- Expand_Assign_Array --
143 -------------------------
145 -- There are two issues here. First, do we let Gigi do a block move, or
146 -- do we expand out into a loop? Second, we need to set the two flags
147 -- Forwards_OK and Backwards_OK which show whether the block move (or
148 -- corresponding loops) can be legitimately done in a forwards (low to
149 -- high) or backwards (high to low) manner.
151 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id) is
152 Loc : constant Source_Ptr := Sloc (N);
154 Lhs : constant Node_Id := Name (N);
156 Act_Lhs : constant Node_Id := Get_Referenced_Object (Lhs);
157 Act_Rhs : Node_Id := Get_Referenced_Object (Rhs);
159 L_Type : constant Entity_Id :=
160 Underlying_Type (Get_Actual_Subtype (Act_Lhs));
161 R_Type : Entity_Id :=
162 Underlying_Type (Get_Actual_Subtype (Act_Rhs));
164 L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice;
165 R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice;
167 Crep : constant Boolean := Change_Of_Representation (N);
169 Larray : Node_Id;
170 Rarray : Node_Id;
172 Ndim : constant Pos := Number_Dimensions (L_Type);
174 Loop_Required : Boolean := False;
175 -- This switch is set to True if the array move must be done using
176 -- an explicit front end generated loop.
178 procedure Apply_Dereference (Arg : Node_Id);
179 -- If the argument is an access to an array, and the assignment is
180 -- converted into a procedure call, apply explicit dereference.
182 function Has_Address_Clause (Exp : Node_Id) return Boolean;
183 -- Test if Exp is a reference to an array whose declaration has
184 -- an address clause, or it is a slice of such an array.
186 function Is_Formal_Array (Exp : Node_Id) return Boolean;
187 -- Test if Exp is a reference to an array which is either a formal
188 -- parameter or a slice of a formal parameter. These are the cases
189 -- where hidden aliasing can occur.
191 function Is_Non_Local_Array (Exp : Node_Id) return Boolean;
192 -- Determine if Exp is a reference to an array variable which is other
193 -- than an object defined in the current scope, or a slice of such
194 -- an object. Such objects can be aliased to parameters (unlike local
195 -- array references).
197 -----------------------
198 -- Apply_Dereference --
199 -----------------------
201 procedure Apply_Dereference (Arg : Node_Id) is
202 Typ : constant Entity_Id := Etype (Arg);
203 begin
204 if Is_Access_Type (Typ) then
205 Rewrite (Arg, Make_Explicit_Dereference (Loc,
206 Prefix => Relocate_Node (Arg)));
207 Analyze_And_Resolve (Arg, Designated_Type (Typ));
208 end if;
209 end Apply_Dereference;
211 ------------------------
212 -- Has_Address_Clause --
213 ------------------------
215 function Has_Address_Clause (Exp : Node_Id) return Boolean is
216 begin
217 return
218 (Is_Entity_Name (Exp) and then
219 Present (Address_Clause (Entity (Exp))))
220 or else
221 (Nkind (Exp) = N_Slice and then Has_Address_Clause (Prefix (Exp)));
222 end Has_Address_Clause;
224 ---------------------
225 -- Is_Formal_Array --
226 ---------------------
228 function Is_Formal_Array (Exp : Node_Id) return Boolean is
229 begin
230 return
231 (Is_Entity_Name (Exp) and then Is_Formal (Entity (Exp)))
232 or else
233 (Nkind (Exp) = N_Slice and then Is_Formal_Array (Prefix (Exp)));
234 end Is_Formal_Array;
236 ------------------------
237 -- Is_Non_Local_Array --
238 ------------------------
240 function Is_Non_Local_Array (Exp : Node_Id) return Boolean is
241 begin
242 return (Is_Entity_Name (Exp)
243 and then Scope (Entity (Exp)) /= Current_Scope)
244 or else (Nkind (Exp) = N_Slice
245 and then Is_Non_Local_Array (Prefix (Exp)));
246 end Is_Non_Local_Array;
248 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
250 Lhs_Formal : constant Boolean := Is_Formal_Array (Act_Lhs);
251 Rhs_Formal : constant Boolean := Is_Formal_Array (Act_Rhs);
253 Lhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Lhs);
254 Rhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Rhs);
256 -- Start of processing for Expand_Assign_Array
258 begin
259 -- Deal with length check. Note that the length check is done with
260 -- respect to the right hand side as given, not a possible underlying
261 -- renamed object, since this would generate incorrect extra checks.
263 Apply_Length_Check (Rhs, L_Type);
265 -- We start by assuming that the move can be done in either direction,
266 -- i.e. that the two sides are completely disjoint.
268 Set_Forwards_OK (N, True);
269 Set_Backwards_OK (N, True);
271 -- Normally it is only the slice case that can lead to overlap, and
272 -- explicit checks for slices are made below. But there is one case
273 -- where the slice can be implicit and invisible to us: when we have a
274 -- one dimensional array, and either both operands are parameters, or
275 -- one is a parameter (which can be a slice passed by reference) and the
276 -- other is a non-local variable. In this case the parameter could be a
277 -- slice that overlaps with the other operand.
279 -- However, if the array subtype is a constrained first subtype in the
280 -- parameter case, then we don't have to worry about overlap, since
281 -- slice assignments aren't possible (other than for a slice denoting
282 -- the whole array).
284 -- Note: No overlap is possible if there is a change of representation,
285 -- so we can exclude this case.
287 if Ndim = 1
288 and then not Crep
289 and then
290 ((Lhs_Formal and Rhs_Formal)
291 or else
292 (Lhs_Formal and Rhs_Non_Local_Var)
293 or else
294 (Rhs_Formal and Lhs_Non_Local_Var))
295 and then
296 (not Is_Constrained (Etype (Lhs))
297 or else not Is_First_Subtype (Etype (Lhs)))
299 -- In the case of compiling for the Java or .NET Virtual Machine,
300 -- slices are always passed by making a copy, so we don't have to
301 -- worry about overlap. We also want to prevent generation of "<"
302 -- comparisons for array addresses, since that's a meaningless
303 -- operation on the VM.
305 and then VM_Target = No_VM
306 then
307 Set_Forwards_OK (N, False);
308 Set_Backwards_OK (N, False);
310 -- Note: the bit-packed case is not worrisome here, since if we have
311 -- a slice passed as a parameter, it is always aligned on a byte
312 -- boundary, and if there are no explicit slices, the assignment
313 -- can be performed directly.
314 end if;
316 -- If either operand has an address clause clear Backwards_OK and
317 -- Forwards_OK, since we cannot tell if the operands overlap. We
318 -- exclude this treatment when Rhs is an aggregate, since we know
319 -- that overlap can't occur.
321 if (Has_Address_Clause (Lhs) and then Nkind (Rhs) /= N_Aggregate)
322 or else Has_Address_Clause (Rhs)
323 then
324 Set_Forwards_OK (N, False);
325 Set_Backwards_OK (N, False);
326 end if;
328 -- We certainly must use a loop for change of representation and also
329 -- we use the operand of the conversion on the right hand side as the
330 -- effective right hand side (the component types must match in this
331 -- situation).
333 if Crep then
334 Act_Rhs := Get_Referenced_Object (Rhs);
335 R_Type := Get_Actual_Subtype (Act_Rhs);
336 Loop_Required := True;
338 -- We require a loop if the left side is possibly bit unaligned
340 elsif Possible_Bit_Aligned_Component (Lhs)
341 or else
342 Possible_Bit_Aligned_Component (Rhs)
343 then
344 Loop_Required := True;
346 -- Arrays with controlled components are expanded into a loop to force
347 -- calls to Adjust at the component level.
349 elsif Has_Controlled_Component (L_Type) then
350 Loop_Required := True;
352 -- If object is atomic, we cannot tolerate a loop
354 elsif Is_Atomic_Object (Act_Lhs)
355 or else
356 Is_Atomic_Object (Act_Rhs)
357 then
358 return;
360 -- Loop is required if we have atomic components since we have to
361 -- be sure to do any accesses on an element by element basis.
363 elsif Has_Atomic_Components (L_Type)
364 or else Has_Atomic_Components (R_Type)
365 or else Is_Atomic (Component_Type (L_Type))
366 or else Is_Atomic (Component_Type (R_Type))
367 then
368 Loop_Required := True;
370 -- Case where no slice is involved
372 elsif not L_Slice and not R_Slice then
374 -- The following code deals with the case of unconstrained bit packed
375 -- arrays. The problem is that the template for such arrays contains
376 -- the bounds of the actual source level array, but the copy of an
377 -- entire array requires the bounds of the underlying array. It would
378 -- be nice if the back end could take care of this, but right now it
379 -- does not know how, so if we have such a type, then we expand out
380 -- into a loop, which is inefficient but works correctly. If we don't
381 -- do this, we get the wrong length computed for the array to be
382 -- moved. The two cases we need to worry about are:
384 -- Explicit dereference of an unconstrained packed array type as in
385 -- the following example:
387 -- procedure C52 is
388 -- type BITS is array(INTEGER range <>) of BOOLEAN;
389 -- pragma PACK(BITS);
390 -- type A is access BITS;
391 -- P1,P2 : A;
392 -- begin
393 -- P1 := new BITS (1 .. 65_535);
394 -- P2 := new BITS (1 .. 65_535);
395 -- P2.ALL := P1.ALL;
396 -- end C52;
398 -- A formal parameter reference with an unconstrained bit array type
399 -- is the other case we need to worry about (here we assume the same
400 -- BITS type declared above):
402 -- procedure Write_All (File : out BITS; Contents : BITS);
403 -- begin
404 -- File.Storage := Contents;
405 -- end Write_All;
407 -- We expand to a loop in either of these two cases
409 -- Question for future thought. Another potentially more efficient
410 -- approach would be to create the actual subtype, and then do an
411 -- unchecked conversion to this actual subtype ???
413 Check_Unconstrained_Bit_Packed_Array : declare
415 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean;
416 -- Function to perform required test for the first case, above
417 -- (dereference of an unconstrained bit packed array).
419 -----------------------
420 -- Is_UBPA_Reference --
421 -----------------------
423 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean is
424 Typ : constant Entity_Id := Underlying_Type (Etype (Opnd));
425 P_Type : Entity_Id;
426 Des_Type : Entity_Id;
428 begin
429 if Present (Packed_Array_Type (Typ))
430 and then Is_Array_Type (Packed_Array_Type (Typ))
431 and then not Is_Constrained (Packed_Array_Type (Typ))
432 then
433 return True;
435 elsif Nkind (Opnd) = N_Explicit_Dereference then
436 P_Type := Underlying_Type (Etype (Prefix (Opnd)));
438 if not Is_Access_Type (P_Type) then
439 return False;
441 else
442 Des_Type := Designated_Type (P_Type);
443 return
444 Is_Bit_Packed_Array (Des_Type)
445 and then not Is_Constrained (Des_Type);
446 end if;
448 else
449 return False;
450 end if;
451 end Is_UBPA_Reference;
453 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
455 begin
456 if Is_UBPA_Reference (Lhs)
457 or else
458 Is_UBPA_Reference (Rhs)
459 then
460 Loop_Required := True;
462 -- Here if we do not have the case of a reference to a bit packed
463 -- unconstrained array case. In this case gigi can most certainly
464 -- handle the assignment if a forwards move is allowed.
466 -- (could it handle the backwards case also???)
468 elsif Forwards_OK (N) then
469 return;
470 end if;
471 end Check_Unconstrained_Bit_Packed_Array;
473 -- The back end can always handle the assignment if the right side is a
474 -- string literal (note that overlap is definitely impossible in this
475 -- case). If the type is packed, a string literal is always converted
476 -- into an aggregate, except in the case of a null slice, for which no
477 -- aggregate can be written. In that case, rewrite the assignment as a
478 -- null statement, a length check has already been emitted to verify
479 -- that the range of the left-hand side is empty.
481 -- Note that this code is not executed if we have an assignment of a
482 -- string literal to a non-bit aligned component of a record, a case
483 -- which cannot be handled by the backend.
485 elsif Nkind (Rhs) = N_String_Literal then
486 if String_Length (Strval (Rhs)) = 0
487 and then Is_Bit_Packed_Array (L_Type)
488 then
489 Rewrite (N, Make_Null_Statement (Loc));
490 Analyze (N);
491 end if;
493 return;
495 -- If either operand is bit packed, then we need a loop, since we can't
496 -- be sure that the slice is byte aligned. Similarly, if either operand
497 -- is a possibly unaligned slice, then we need a loop (since the back
498 -- end cannot handle unaligned slices).
500 elsif Is_Bit_Packed_Array (L_Type)
501 or else Is_Bit_Packed_Array (R_Type)
502 or else Is_Possibly_Unaligned_Slice (Lhs)
503 or else Is_Possibly_Unaligned_Slice (Rhs)
504 then
505 Loop_Required := True;
507 -- If we are not bit-packed, and we have only one slice, then no overlap
508 -- is possible except in the parameter case, so we can let the back end
509 -- handle things.
511 elsif not (L_Slice and R_Slice) then
512 if Forwards_OK (N) then
513 return;
514 end if;
515 end if;
517 -- If the right-hand side is a string literal, introduce a temporary for
518 -- it, for use in the generated loop that will follow.
520 if Nkind (Rhs) = N_String_Literal then
521 declare
522 Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Rhs);
523 Decl : Node_Id;
525 begin
526 Decl :=
527 Make_Object_Declaration (Loc,
528 Defining_Identifier => Temp,
529 Object_Definition => New_Occurrence_Of (L_Type, Loc),
530 Expression => Relocate_Node (Rhs));
532 Insert_Action (N, Decl);
533 Rewrite (Rhs, New_Occurrence_Of (Temp, Loc));
534 R_Type := Etype (Temp);
535 end;
536 end if;
538 -- Come here to complete the analysis
540 -- Loop_Required: Set to True if we know that a loop is required
541 -- regardless of overlap considerations.
543 -- Forwards_OK: Set to False if we already know that a forwards
544 -- move is not safe, else set to True.
546 -- Backwards_OK: Set to False if we already know that a backwards
547 -- move is not safe, else set to True
549 -- Our task at this stage is to complete the overlap analysis, which can
550 -- result in possibly setting Forwards_OK or Backwards_OK to False, and
551 -- then generating the final code, either by deciding that it is OK
552 -- after all to let Gigi handle it, or by generating appropriate code
553 -- in the front end.
555 declare
556 L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
557 R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
559 Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
560 Left_Hi : constant Node_Id := Type_High_Bound (L_Index_Typ);
561 Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
562 Right_Hi : constant Node_Id := Type_High_Bound (R_Index_Typ);
564 Act_L_Array : Node_Id;
565 Act_R_Array : Node_Id;
567 Cleft_Lo : Node_Id;
568 Cright_Lo : Node_Id;
569 Condition : Node_Id;
571 Cresult : Compare_Result;
573 begin
574 -- Get the expressions for the arrays. If we are dealing with a
575 -- private type, then convert to the underlying type. We can do
576 -- direct assignments to an array that is a private type, but we
577 -- cannot assign to elements of the array without this extra
578 -- unchecked conversion.
580 -- Note: We propagate Parent to the conversion nodes to generate
581 -- a well-formed subtree.
583 if Nkind (Act_Lhs) = N_Slice then
584 Larray := Prefix (Act_Lhs);
585 else
586 Larray := Act_Lhs;
588 if Is_Private_Type (Etype (Larray)) then
589 declare
590 Par : constant Node_Id := Parent (Larray);
591 begin
592 Larray :=
593 Unchecked_Convert_To
594 (Underlying_Type (Etype (Larray)), Larray);
595 Set_Parent (Larray, Par);
596 end;
597 end if;
598 end if;
600 if Nkind (Act_Rhs) = N_Slice then
601 Rarray := Prefix (Act_Rhs);
602 else
603 Rarray := Act_Rhs;
605 if Is_Private_Type (Etype (Rarray)) then
606 declare
607 Par : constant Node_Id := Parent (Rarray);
608 begin
609 Rarray :=
610 Unchecked_Convert_To
611 (Underlying_Type (Etype (Rarray)), Rarray);
612 Set_Parent (Rarray, Par);
613 end;
614 end if;
615 end if;
617 -- If both sides are slices, we must figure out whether it is safe
618 -- to do the move in one direction or the other. It is always safe
619 -- if there is a change of representation since obviously two arrays
620 -- with different representations cannot possibly overlap.
622 if (not Crep) and L_Slice and R_Slice then
623 Act_L_Array := Get_Referenced_Object (Prefix (Act_Lhs));
624 Act_R_Array := Get_Referenced_Object (Prefix (Act_Rhs));
626 -- If both left and right hand arrays are entity names, and refer
627 -- to different entities, then we know that the move is safe (the
628 -- two storage areas are completely disjoint).
630 if Is_Entity_Name (Act_L_Array)
631 and then Is_Entity_Name (Act_R_Array)
632 and then Entity (Act_L_Array) /= Entity (Act_R_Array)
633 then
634 null;
636 -- Otherwise, we assume the worst, which is that the two arrays
637 -- are the same array. There is no need to check if we know that
638 -- is the case, because if we don't know it, we still have to
639 -- assume it!
641 -- Generally if the same array is involved, then we have an
642 -- overlapping case. We will have to really assume the worst (i.e.
643 -- set neither of the OK flags) unless we can determine the lower
644 -- or upper bounds at compile time and compare them.
646 else
647 Cresult :=
648 Compile_Time_Compare
649 (Left_Lo, Right_Lo, Assume_Valid => True);
651 if Cresult = Unknown then
652 Cresult :=
653 Compile_Time_Compare
654 (Left_Hi, Right_Hi, Assume_Valid => True);
655 end if;
657 case Cresult is
658 when LT | LE | EQ => Set_Backwards_OK (N, False);
659 when GT | GE => Set_Forwards_OK (N, False);
660 when NE | Unknown => Set_Backwards_OK (N, False);
661 Set_Forwards_OK (N, False);
662 end case;
663 end if;
664 end if;
666 -- If after that analysis Loop_Required is False, meaning that we
667 -- have not discovered some non-overlap reason for requiring a loop,
668 -- then the outcome depends on the capabilities of the back end.
670 if not Loop_Required then
672 -- The GCC back end can deal with all cases of overlap by falling
673 -- back to memmove if it cannot use a more efficient approach.
675 if VM_Target = No_VM and not AAMP_On_Target then
676 return;
678 -- Assume other back ends can handle it if Forwards_OK is set
680 elsif Forwards_OK (N) then
681 return;
683 -- If Forwards_OK is not set, the back end will need something
684 -- like memmove to handle the move. For now, this processing is
685 -- activated using the .s debug flag (-gnatd.s).
687 elsif Debug_Flag_Dot_S then
688 return;
689 end if;
690 end if;
692 -- At this stage we have to generate an explicit loop, and we have
693 -- the following cases:
695 -- Forwards_OK = True
697 -- Rnn : right_index := right_index'First;
698 -- for Lnn in left-index loop
699 -- left (Lnn) := right (Rnn);
700 -- Rnn := right_index'Succ (Rnn);
701 -- end loop;
703 -- Note: the above code MUST be analyzed with checks off, because
704 -- otherwise the Succ could overflow. But in any case this is more
705 -- efficient!
707 -- Forwards_OK = False, Backwards_OK = True
709 -- Rnn : right_index := right_index'Last;
710 -- for Lnn in reverse left-index loop
711 -- left (Lnn) := right (Rnn);
712 -- Rnn := right_index'Pred (Rnn);
713 -- end loop;
715 -- Note: the above code MUST be analyzed with checks off, because
716 -- otherwise the Pred could overflow. But in any case this is more
717 -- efficient!
719 -- Forwards_OK = Backwards_OK = False
721 -- This only happens if we have the same array on each side. It is
722 -- possible to create situations using overlays that violate this,
723 -- but we simply do not promise to get this "right" in this case.
725 -- There are two possible subcases. If the No_Implicit_Conditionals
726 -- restriction is set, then we generate the following code:
728 -- declare
729 -- T : constant <operand-type> := rhs;
730 -- begin
731 -- lhs := T;
732 -- end;
734 -- If implicit conditionals are permitted, then we generate:
736 -- if Left_Lo <= Right_Lo then
737 -- <code for Forwards_OK = True above>
738 -- else
739 -- <code for Backwards_OK = True above>
740 -- end if;
742 -- In order to detect possible aliasing, we examine the renamed
743 -- expression when the source or target is a renaming. However,
744 -- the renaming may be intended to capture an address that may be
745 -- affected by subsequent code, and therefore we must recover
746 -- the actual entity for the expansion that follows, not the
747 -- object it renames. In particular, if source or target designate
748 -- a portion of a dynamically allocated object, the pointer to it
749 -- may be reassigned but the renaming preserves the proper location.
751 if Is_Entity_Name (Rhs)
752 and then
753 Nkind (Parent (Entity (Rhs))) = N_Object_Renaming_Declaration
754 and then Nkind (Act_Rhs) = N_Slice
755 then
756 Rarray := Rhs;
757 end if;
759 if Is_Entity_Name (Lhs)
760 and then
761 Nkind (Parent (Entity (Lhs))) = N_Object_Renaming_Declaration
762 and then Nkind (Act_Lhs) = N_Slice
763 then
764 Larray := Lhs;
765 end if;
767 -- Cases where either Forwards_OK or Backwards_OK is true
769 if Forwards_OK (N) or else Backwards_OK (N) then
770 if Needs_Finalization (Component_Type (L_Type))
771 and then Base_Type (L_Type) = Base_Type (R_Type)
772 and then Ndim = 1
773 and then not No_Ctrl_Actions (N)
774 then
775 declare
776 Proc : constant Entity_Id :=
777 TSS (Base_Type (L_Type), TSS_Slice_Assign);
778 Actuals : List_Id;
780 begin
781 Apply_Dereference (Larray);
782 Apply_Dereference (Rarray);
783 Actuals := New_List (
784 Duplicate_Subexpr (Larray, Name_Req => True),
785 Duplicate_Subexpr (Rarray, Name_Req => True),
786 Duplicate_Subexpr (Left_Lo, Name_Req => True),
787 Duplicate_Subexpr (Left_Hi, Name_Req => True),
788 Duplicate_Subexpr (Right_Lo, Name_Req => True),
789 Duplicate_Subexpr (Right_Hi, Name_Req => True));
791 Append_To (Actuals,
792 New_Occurrence_Of (
793 Boolean_Literals (not Forwards_OK (N)), Loc));
795 Rewrite (N,
796 Make_Procedure_Call_Statement (Loc,
797 Name => New_Reference_To (Proc, Loc),
798 Parameter_Associations => Actuals));
799 end;
801 else
802 Rewrite (N,
803 Expand_Assign_Array_Loop
804 (N, Larray, Rarray, L_Type, R_Type, Ndim,
805 Rev => not Forwards_OK (N)));
806 end if;
808 -- Case of both are false with No_Implicit_Conditionals
810 elsif Restriction_Active (No_Implicit_Conditionals) then
811 declare
812 T : constant Entity_Id :=
813 Make_Defining_Identifier (Loc, Chars => Name_T);
815 begin
816 Rewrite (N,
817 Make_Block_Statement (Loc,
818 Declarations => New_List (
819 Make_Object_Declaration (Loc,
820 Defining_Identifier => T,
821 Constant_Present => True,
822 Object_Definition =>
823 New_Occurrence_Of (Etype (Rhs), Loc),
824 Expression => Relocate_Node (Rhs))),
826 Handled_Statement_Sequence =>
827 Make_Handled_Sequence_Of_Statements (Loc,
828 Statements => New_List (
829 Make_Assignment_Statement (Loc,
830 Name => Relocate_Node (Lhs),
831 Expression => New_Occurrence_Of (T, Loc))))));
832 end;
834 -- Case of both are false with implicit conditionals allowed
836 else
837 -- Before we generate this code, we must ensure that the left and
838 -- right side array types are defined. They may be itypes, and we
839 -- cannot let them be defined inside the if, since the first use
840 -- in the then may not be executed.
842 Ensure_Defined (L_Type, N);
843 Ensure_Defined (R_Type, N);
845 -- We normally compare addresses to find out which way round to
846 -- do the loop, since this is reliable, and handles the cases of
847 -- parameters, conversions etc. But we can't do that in the bit
848 -- packed case or the VM case, because addresses don't work there.
850 if not Is_Bit_Packed_Array (L_Type) and then VM_Target = No_VM then
851 Condition :=
852 Make_Op_Le (Loc,
853 Left_Opnd =>
854 Unchecked_Convert_To (RTE (RE_Integer_Address),
855 Make_Attribute_Reference (Loc,
856 Prefix =>
857 Make_Indexed_Component (Loc,
858 Prefix =>
859 Duplicate_Subexpr_Move_Checks (Larray, True),
860 Expressions => New_List (
861 Make_Attribute_Reference (Loc,
862 Prefix =>
863 New_Reference_To
864 (L_Index_Typ, Loc),
865 Attribute_Name => Name_First))),
866 Attribute_Name => Name_Address)),
868 Right_Opnd =>
869 Unchecked_Convert_To (RTE (RE_Integer_Address),
870 Make_Attribute_Reference (Loc,
871 Prefix =>
872 Make_Indexed_Component (Loc,
873 Prefix =>
874 Duplicate_Subexpr_Move_Checks (Rarray, True),
875 Expressions => New_List (
876 Make_Attribute_Reference (Loc,
877 Prefix =>
878 New_Reference_To
879 (R_Index_Typ, Loc),
880 Attribute_Name => Name_First))),
881 Attribute_Name => Name_Address)));
883 -- For the bit packed and VM cases we use the bounds. That's OK,
884 -- because we don't have to worry about parameters, since they
885 -- cannot cause overlap. Perhaps we should worry about weird slice
886 -- conversions ???
888 else
889 -- Copy the bounds
891 Cleft_Lo := New_Copy_Tree (Left_Lo);
892 Cright_Lo := New_Copy_Tree (Right_Lo);
894 -- If the types do not match we add an implicit conversion
895 -- here to ensure proper match
897 if Etype (Left_Lo) /= Etype (Right_Lo) then
898 Cright_Lo :=
899 Unchecked_Convert_To (Etype (Left_Lo), Cright_Lo);
900 end if;
902 -- Reset the Analyzed flag, because the bounds of the index
903 -- type itself may be universal, and must must be reanalyzed
904 -- to acquire the proper type for the back end.
906 Set_Analyzed (Cleft_Lo, False);
907 Set_Analyzed (Cright_Lo, False);
909 Condition :=
910 Make_Op_Le (Loc,
911 Left_Opnd => Cleft_Lo,
912 Right_Opnd => Cright_Lo);
913 end if;
915 if Needs_Finalization (Component_Type (L_Type))
916 and then Base_Type (L_Type) = Base_Type (R_Type)
917 and then Ndim = 1
918 and then not No_Ctrl_Actions (N)
919 then
921 -- Call TSS procedure for array assignment, passing the
922 -- explicit bounds of right and left hand sides.
924 declare
925 Proc : constant Entity_Id :=
926 TSS (Base_Type (L_Type), TSS_Slice_Assign);
927 Actuals : List_Id;
929 begin
930 Apply_Dereference (Larray);
931 Apply_Dereference (Rarray);
932 Actuals := New_List (
933 Duplicate_Subexpr (Larray, Name_Req => True),
934 Duplicate_Subexpr (Rarray, Name_Req => True),
935 Duplicate_Subexpr (Left_Lo, Name_Req => True),
936 Duplicate_Subexpr (Left_Hi, Name_Req => True),
937 Duplicate_Subexpr (Right_Lo, Name_Req => True),
938 Duplicate_Subexpr (Right_Hi, Name_Req => True));
940 Append_To (Actuals,
941 Make_Op_Not (Loc,
942 Right_Opnd => Condition));
944 Rewrite (N,
945 Make_Procedure_Call_Statement (Loc,
946 Name => New_Reference_To (Proc, Loc),
947 Parameter_Associations => Actuals));
948 end;
950 else
951 Rewrite (N,
952 Make_Implicit_If_Statement (N,
953 Condition => Condition,
955 Then_Statements => New_List (
956 Expand_Assign_Array_Loop
957 (N, Larray, Rarray, L_Type, R_Type, Ndim,
958 Rev => False)),
960 Else_Statements => New_List (
961 Expand_Assign_Array_Loop
962 (N, Larray, Rarray, L_Type, R_Type, Ndim,
963 Rev => True))));
964 end if;
965 end if;
967 Analyze (N, Suppress => All_Checks);
968 end;
970 exception
971 when RE_Not_Available =>
972 return;
973 end Expand_Assign_Array;
975 ------------------------------
976 -- Expand_Assign_Array_Loop --
977 ------------------------------
979 -- The following is an example of the loop generated for the case of a
980 -- two-dimensional array:
982 -- declare
983 -- R2b : Tm1X1 := 1;
984 -- begin
985 -- for L1b in 1 .. 100 loop
986 -- declare
987 -- R4b : Tm1X2 := 1;
988 -- begin
989 -- for L3b in 1 .. 100 loop
990 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
991 -- R4b := Tm1X2'succ(R4b);
992 -- end loop;
993 -- end;
994 -- R2b := Tm1X1'succ(R2b);
995 -- end loop;
996 -- end;
998 -- Here Rev is False, and Tm1Xn are the subscript types for the right hand
999 -- side. The declarations of R2b and R4b are inserted before the original
1000 -- assignment statement.
1002 function Expand_Assign_Array_Loop
1003 (N : Node_Id;
1004 Larray : Entity_Id;
1005 Rarray : Entity_Id;
1006 L_Type : Entity_Id;
1007 R_Type : Entity_Id;
1008 Ndim : Pos;
1009 Rev : Boolean) return Node_Id
1011 Loc : constant Source_Ptr := Sloc (N);
1013 Lnn : array (1 .. Ndim) of Entity_Id;
1014 Rnn : array (1 .. Ndim) of Entity_Id;
1015 -- Entities used as subscripts on left and right sides
1017 L_Index_Type : array (1 .. Ndim) of Entity_Id;
1018 R_Index_Type : array (1 .. Ndim) of Entity_Id;
1019 -- Left and right index types
1021 Assign : Node_Id;
1023 F_Or_L : Name_Id;
1024 S_Or_P : Name_Id;
1026 function Build_Step (J : Nat) return Node_Id;
1027 -- The increment step for the index of the right-hand side is written
1028 -- as an attribute reference (Succ or Pred). This function returns
1029 -- the corresponding node, which is placed at the end of the loop body.
1031 ----------------
1032 -- Build_Step --
1033 ----------------
1035 function Build_Step (J : Nat) return Node_Id is
1036 Step : Node_Id;
1037 Lim : Name_Id;
1039 begin
1040 if Rev then
1041 Lim := Name_First;
1042 else
1043 Lim := Name_Last;
1044 end if;
1046 Step :=
1047 Make_Assignment_Statement (Loc,
1048 Name => New_Occurrence_Of (Rnn (J), Loc),
1049 Expression =>
1050 Make_Attribute_Reference (Loc,
1051 Prefix =>
1052 New_Occurrence_Of (R_Index_Type (J), Loc),
1053 Attribute_Name => S_Or_P,
1054 Expressions => New_List (
1055 New_Occurrence_Of (Rnn (J), Loc))));
1057 -- Note that on the last iteration of the loop, the index is increased
1058 -- (or decreased) past the corresponding bound. This is consistent with
1059 -- the C semantics of the back-end, where such an off-by-one value on a
1060 -- dead index variable is OK. However, in CodePeer mode this leads to
1061 -- spurious warnings, and thus we place a guard around the attribute
1062 -- reference. For obvious reasons we only do this for CodePeer.
1064 if CodePeer_Mode then
1065 Step :=
1066 Make_If_Statement (Loc,
1067 Condition =>
1068 Make_Op_Ne (Loc,
1069 Left_Opnd => New_Occurrence_Of (Lnn (J), Loc),
1070 Right_Opnd =>
1071 Make_Attribute_Reference (Loc,
1072 Prefix => New_Occurrence_Of (L_Index_Type (J), Loc),
1073 Attribute_Name => Lim)),
1074 Then_Statements => New_List (Step));
1075 end if;
1077 return Step;
1078 end Build_Step;
1080 -- Start of processing for Expand_Assign_Array_Loop
1082 begin
1083 if Rev then
1084 F_Or_L := Name_Last;
1085 S_Or_P := Name_Pred;
1086 else
1087 F_Or_L := Name_First;
1088 S_Or_P := Name_Succ;
1089 end if;
1091 -- Setup index types and subscript entities
1093 declare
1094 L_Index : Node_Id;
1095 R_Index : Node_Id;
1097 begin
1098 L_Index := First_Index (L_Type);
1099 R_Index := First_Index (R_Type);
1101 for J in 1 .. Ndim loop
1102 Lnn (J) := Make_Temporary (Loc, 'L');
1103 Rnn (J) := Make_Temporary (Loc, 'R');
1105 L_Index_Type (J) := Etype (L_Index);
1106 R_Index_Type (J) := Etype (R_Index);
1108 Next_Index (L_Index);
1109 Next_Index (R_Index);
1110 end loop;
1111 end;
1113 -- Now construct the assignment statement
1115 declare
1116 ExprL : constant List_Id := New_List;
1117 ExprR : constant List_Id := New_List;
1119 begin
1120 for J in 1 .. Ndim loop
1121 Append_To (ExprL, New_Occurrence_Of (Lnn (J), Loc));
1122 Append_To (ExprR, New_Occurrence_Of (Rnn (J), Loc));
1123 end loop;
1125 Assign :=
1126 Make_Assignment_Statement (Loc,
1127 Name =>
1128 Make_Indexed_Component (Loc,
1129 Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
1130 Expressions => ExprL),
1131 Expression =>
1132 Make_Indexed_Component (Loc,
1133 Prefix => Duplicate_Subexpr (Rarray, Name_Req => True),
1134 Expressions => ExprR));
1136 -- We set assignment OK, since there are some cases, e.g. in object
1137 -- declarations, where we are actually assigning into a constant.
1138 -- If there really is an illegality, it was caught long before now,
1139 -- and was flagged when the original assignment was analyzed.
1141 Set_Assignment_OK (Name (Assign));
1143 -- Propagate the No_Ctrl_Actions flag to individual assignments
1145 Set_No_Ctrl_Actions (Assign, No_Ctrl_Actions (N));
1146 end;
1148 -- Now construct the loop from the inside out, with the last subscript
1149 -- varying most rapidly. Note that Assign is first the raw assignment
1150 -- statement, and then subsequently the loop that wraps it up.
1152 for J in reverse 1 .. Ndim loop
1153 Assign :=
1154 Make_Block_Statement (Loc,
1155 Declarations => New_List (
1156 Make_Object_Declaration (Loc,
1157 Defining_Identifier => Rnn (J),
1158 Object_Definition =>
1159 New_Occurrence_Of (R_Index_Type (J), Loc),
1160 Expression =>
1161 Make_Attribute_Reference (Loc,
1162 Prefix => New_Occurrence_Of (R_Index_Type (J), Loc),
1163 Attribute_Name => F_Or_L))),
1165 Handled_Statement_Sequence =>
1166 Make_Handled_Sequence_Of_Statements (Loc,
1167 Statements => New_List (
1168 Make_Implicit_Loop_Statement (N,
1169 Iteration_Scheme =>
1170 Make_Iteration_Scheme (Loc,
1171 Loop_Parameter_Specification =>
1172 Make_Loop_Parameter_Specification (Loc,
1173 Defining_Identifier => Lnn (J),
1174 Reverse_Present => Rev,
1175 Discrete_Subtype_Definition =>
1176 New_Reference_To (L_Index_Type (J), Loc))),
1178 Statements => New_List (Assign, Build_Step (J))))));
1179 end loop;
1181 return Assign;
1182 end Expand_Assign_Array_Loop;
1184 --------------------------
1185 -- Expand_Assign_Record --
1186 --------------------------
1188 procedure Expand_Assign_Record (N : Node_Id) is
1189 Lhs : constant Node_Id := Name (N);
1190 Rhs : Node_Id := Expression (N);
1191 L_Typ : constant Entity_Id := Base_Type (Etype (Lhs));
1193 begin
1194 -- If change of representation, then extract the real right hand side
1195 -- from the type conversion, and proceed with component-wise assignment,
1196 -- since the two types are not the same as far as the back end is
1197 -- concerned.
1199 if Change_Of_Representation (N) then
1200 Rhs := Expression (Rhs);
1202 -- If this may be a case of a large bit aligned component, then proceed
1203 -- with component-wise assignment, to avoid possible clobbering of other
1204 -- components sharing bits in the first or last byte of the component to
1205 -- be assigned.
1207 elsif Possible_Bit_Aligned_Component (Lhs)
1209 Possible_Bit_Aligned_Component (Rhs)
1210 then
1211 null;
1213 -- If we have a tagged type that has a complete record representation
1214 -- clause, we must do we must do component-wise assignments, since child
1215 -- types may have used gaps for their components, and we might be
1216 -- dealing with a view conversion.
1218 elsif Is_Fully_Repped_Tagged_Type (L_Typ) then
1219 null;
1221 -- If neither condition met, then nothing special to do, the back end
1222 -- can handle assignment of the entire component as a single entity.
1224 else
1225 return;
1226 end if;
1228 -- At this stage we know that we must do a component wise assignment
1230 declare
1231 Loc : constant Source_Ptr := Sloc (N);
1232 R_Typ : constant Entity_Id := Base_Type (Etype (Rhs));
1233 Decl : constant Node_Id := Declaration_Node (R_Typ);
1234 RDef : Node_Id;
1235 F : Entity_Id;
1237 function Find_Component
1238 (Typ : Entity_Id;
1239 Comp : Entity_Id) return Entity_Id;
1240 -- Find the component with the given name in the underlying record
1241 -- declaration for Typ. We need to use the actual entity because the
1242 -- type may be private and resolution by identifier alone would fail.
1244 function Make_Component_List_Assign
1245 (CL : Node_Id;
1246 U_U : Boolean := False) return List_Id;
1247 -- Returns a sequence of statements to assign the components that
1248 -- are referenced in the given component list. The flag U_U is
1249 -- used to force the usage of the inferred value of the variant
1250 -- part expression as the switch for the generated case statement.
1252 function Make_Field_Assign
1253 (C : Entity_Id;
1254 U_U : Boolean := False) return Node_Id;
1255 -- Given C, the entity for a discriminant or component, build an
1256 -- assignment for the corresponding field values. The flag U_U
1257 -- signals the presence of an Unchecked_Union and forces the usage
1258 -- of the inferred discriminant value of C as the right hand side
1259 -- of the assignment.
1261 function Make_Field_Assigns (CI : List_Id) return List_Id;
1262 -- Given CI, a component items list, construct series of statements
1263 -- for fieldwise assignment of the corresponding components.
1265 --------------------
1266 -- Find_Component --
1267 --------------------
1269 function Find_Component
1270 (Typ : Entity_Id;
1271 Comp : Entity_Id) return Entity_Id
1273 Utyp : constant Entity_Id := Underlying_Type (Typ);
1274 C : Entity_Id;
1276 begin
1277 C := First_Entity (Utyp);
1278 while Present (C) loop
1279 if Chars (C) = Chars (Comp) then
1280 return C;
1281 end if;
1283 Next_Entity (C);
1284 end loop;
1286 raise Program_Error;
1287 end Find_Component;
1289 --------------------------------
1290 -- Make_Component_List_Assign --
1291 --------------------------------
1293 function Make_Component_List_Assign
1294 (CL : Node_Id;
1295 U_U : Boolean := False) return List_Id
1297 CI : constant List_Id := Component_Items (CL);
1298 VP : constant Node_Id := Variant_Part (CL);
1300 Alts : List_Id;
1301 DC : Node_Id;
1302 DCH : List_Id;
1303 Expr : Node_Id;
1304 Result : List_Id;
1305 V : Node_Id;
1307 begin
1308 Result := Make_Field_Assigns (CI);
1310 if Present (VP) then
1311 V := First_Non_Pragma (Variants (VP));
1312 Alts := New_List;
1313 while Present (V) loop
1314 DCH := New_List;
1315 DC := First (Discrete_Choices (V));
1316 while Present (DC) loop
1317 Append_To (DCH, New_Copy_Tree (DC));
1318 Next (DC);
1319 end loop;
1321 Append_To (Alts,
1322 Make_Case_Statement_Alternative (Loc,
1323 Discrete_Choices => DCH,
1324 Statements =>
1325 Make_Component_List_Assign (Component_List (V))));
1326 Next_Non_Pragma (V);
1327 end loop;
1329 -- If we have an Unchecked_Union, use the value of the inferred
1330 -- discriminant of the variant part expression as the switch
1331 -- for the case statement. The case statement may later be
1332 -- folded.
1334 if U_U then
1335 Expr :=
1336 New_Copy (Get_Discriminant_Value (
1337 Entity (Name (VP)),
1338 Etype (Rhs),
1339 Discriminant_Constraint (Etype (Rhs))));
1340 else
1341 Expr :=
1342 Make_Selected_Component (Loc,
1343 Prefix => Duplicate_Subexpr (Rhs),
1344 Selector_Name =>
1345 Make_Identifier (Loc, Chars (Name (VP))));
1346 end if;
1348 Append_To (Result,
1349 Make_Case_Statement (Loc,
1350 Expression => Expr,
1351 Alternatives => Alts));
1352 end if;
1354 return Result;
1355 end Make_Component_List_Assign;
1357 -----------------------
1358 -- Make_Field_Assign --
1359 -----------------------
1361 function Make_Field_Assign
1362 (C : Entity_Id;
1363 U_U : Boolean := False) return Node_Id
1365 A : Node_Id;
1366 Expr : Node_Id;
1368 begin
1369 -- In the case of an Unchecked_Union, use the discriminant
1370 -- constraint value as on the right hand side of the assignment.
1372 if U_U then
1373 Expr :=
1374 New_Copy (Get_Discriminant_Value (C,
1375 Etype (Rhs),
1376 Discriminant_Constraint (Etype (Rhs))));
1377 else
1378 Expr :=
1379 Make_Selected_Component (Loc,
1380 Prefix => Duplicate_Subexpr (Rhs),
1381 Selector_Name => New_Occurrence_Of (C, Loc));
1382 end if;
1384 A :=
1385 Make_Assignment_Statement (Loc,
1386 Name =>
1387 Make_Selected_Component (Loc,
1388 Prefix => Duplicate_Subexpr (Lhs),
1389 Selector_Name =>
1390 New_Occurrence_Of (Find_Component (L_Typ, C), Loc)),
1391 Expression => Expr);
1393 -- Set Assignment_OK, so discriminants can be assigned
1395 Set_Assignment_OK (Name (A), True);
1397 if Componentwise_Assignment (N)
1398 and then Nkind (Name (A)) = N_Selected_Component
1399 and then Chars (Selector_Name (Name (A))) = Name_uParent
1400 then
1401 Set_Componentwise_Assignment (A);
1402 end if;
1404 return A;
1405 end Make_Field_Assign;
1407 ------------------------
1408 -- Make_Field_Assigns --
1409 ------------------------
1411 function Make_Field_Assigns (CI : List_Id) return List_Id is
1412 Item : Node_Id;
1413 Result : List_Id;
1415 begin
1416 Item := First (CI);
1417 Result := New_List;
1419 while Present (Item) loop
1421 -- Look for components, but exclude _tag field assignment if
1422 -- the special Componentwise_Assignment flag is set.
1424 if Nkind (Item) = N_Component_Declaration
1425 and then not (Is_Tag (Defining_Identifier (Item))
1426 and then Componentwise_Assignment (N))
1427 then
1428 Append_To
1429 (Result, Make_Field_Assign (Defining_Identifier (Item)));
1430 end if;
1432 Next (Item);
1433 end loop;
1435 return Result;
1436 end Make_Field_Assigns;
1438 -- Start of processing for Expand_Assign_Record
1440 begin
1441 -- Note that we use the base types for this processing. This results
1442 -- in some extra work in the constrained case, but the change of
1443 -- representation case is so unusual that it is not worth the effort.
1445 -- First copy the discriminants. This is done unconditionally. It
1446 -- is required in the unconstrained left side case, and also in the
1447 -- case where this assignment was constructed during the expansion
1448 -- of a type conversion (since initialization of discriminants is
1449 -- suppressed in this case). It is unnecessary but harmless in
1450 -- other cases.
1452 if Has_Discriminants (L_Typ) then
1453 F := First_Discriminant (R_Typ);
1454 while Present (F) loop
1456 -- If we are expanding the initialization of a derived record
1457 -- that constrains or renames discriminants of the parent, we
1458 -- must use the corresponding discriminant in the parent.
1460 declare
1461 CF : Entity_Id;
1463 begin
1464 if Inside_Init_Proc
1465 and then Present (Corresponding_Discriminant (F))
1466 then
1467 CF := Corresponding_Discriminant (F);
1468 else
1469 CF := F;
1470 end if;
1472 if Is_Unchecked_Union (Base_Type (R_Typ)) then
1474 -- Within an initialization procedure this is the
1475 -- assignment to an unchecked union component, in which
1476 -- case there is no discriminant to initialize.
1478 if Inside_Init_Proc then
1479 null;
1481 else
1482 -- The assignment is part of a conversion from a
1483 -- derived unchecked union type with an inferable
1484 -- discriminant, to a parent type.
1486 Insert_Action (N, Make_Field_Assign (CF, True));
1487 end if;
1489 else
1490 Insert_Action (N, Make_Field_Assign (CF));
1491 end if;
1493 Next_Discriminant (F);
1494 end;
1495 end loop;
1496 end if;
1498 -- We know the underlying type is a record, but its current view
1499 -- may be private. We must retrieve the usable record declaration.
1501 if Nkind_In (Decl, N_Private_Type_Declaration,
1502 N_Private_Extension_Declaration)
1503 and then Present (Full_View (R_Typ))
1504 then
1505 RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
1506 else
1507 RDef := Type_Definition (Decl);
1508 end if;
1510 if Nkind (RDef) = N_Derived_Type_Definition then
1511 RDef := Record_Extension_Part (RDef);
1512 end if;
1514 if Nkind (RDef) = N_Record_Definition
1515 and then Present (Component_List (RDef))
1516 then
1517 if Is_Unchecked_Union (R_Typ) then
1518 Insert_Actions (N,
1519 Make_Component_List_Assign (Component_List (RDef), True));
1520 else
1521 Insert_Actions
1522 (N, Make_Component_List_Assign (Component_List (RDef)));
1523 end if;
1525 Rewrite (N, Make_Null_Statement (Loc));
1526 end if;
1527 end;
1528 end Expand_Assign_Record;
1530 ----------------------------------
1531 -- Expand_Loop_Entry_Attributes --
1532 ----------------------------------
1534 procedure Expand_Loop_Entry_Attributes (N : Node_Id) is
1535 procedure Build_Conditional_Block
1536 (Loc : Source_Ptr;
1537 Cond : Node_Id;
1538 Stmt : Node_Id;
1539 If_Stmt : out Node_Id;
1540 Blk_Stmt : out Node_Id);
1541 -- Create a block Blk_Stmt with an empty declarative list and a single
1542 -- statement Stmt. The block is encased in an if statement If_Stmt with
1543 -- condition Cond. If_Stmt is Empty when there is no condition provided.
1545 function Is_Array_Iteration (N : Node_Id) return Boolean;
1546 -- Determine whether loop statement N denotes an Ada 2012 iteration over
1547 -- an array object.
1549 -----------------------------
1550 -- Build_Conditional_Block --
1551 -----------------------------
1553 procedure Build_Conditional_Block
1554 (Loc : Source_Ptr;
1555 Cond : Node_Id;
1556 Stmt : Node_Id;
1557 If_Stmt : out Node_Id;
1558 Blk_Stmt : out Node_Id)
1560 begin
1561 Blk_Stmt :=
1562 Make_Block_Statement (Loc,
1563 Declarations => New_List,
1564 Handled_Statement_Sequence =>
1565 Make_Handled_Sequence_Of_Statements (Loc,
1566 Statements => New_List (Stmt)));
1568 if Present (Cond) then
1569 If_Stmt :=
1570 Make_If_Statement (Loc,
1571 Condition => Cond,
1572 Then_Statements => New_List (Blk_Stmt));
1573 else
1574 If_Stmt := Empty;
1575 end if;
1576 end Build_Conditional_Block;
1578 ------------------------
1579 -- Is_Array_Iteration --
1580 ------------------------
1582 function Is_Array_Iteration (N : Node_Id) return Boolean is
1583 Stmt : constant Node_Id := Original_Node (N);
1584 Iter : Node_Id;
1586 begin
1587 if Nkind (Stmt) = N_Loop_Statement
1588 and then Present (Iteration_Scheme (Stmt))
1589 and then Present (Iterator_Specification (Iteration_Scheme (Stmt)))
1590 then
1591 Iter := Iterator_Specification (Iteration_Scheme (Stmt));
1593 return
1594 Of_Present (Iter)
1595 and then Is_Array_Type (Etype (Name (Iter)));
1596 end if;
1598 return False;
1599 end Is_Array_Iteration;
1601 -- Local variables
1603 Loc : constant Source_Ptr := Sloc (N);
1604 Loop_Id : constant Entity_Id := Identifier (N);
1605 Scheme : constant Node_Id := Iteration_Scheme (N);
1606 Blk : Node_Id;
1607 LE : Node_Id;
1608 LE_Elmt : Elmt_Id;
1609 Result : Node_Id;
1610 Temp : Entity_Id;
1611 Typ : Entity_Id;
1613 -- Start of processing for Expand_Loop_Entry_Attributes
1615 begin
1616 -- The loop will never execute after it has been expanded, no point in
1617 -- processing it.
1619 if Is_Null_Loop (N) then
1620 return;
1622 -- A loop without an identifier cannot be referenced in 'Loop_Entry
1624 elsif No (Loop_Id) then
1625 return;
1627 -- The loop is not subject to 'Loop_Entry
1629 elsif No (Loop_Entry_Attributes (Entity (Loop_Id))) then
1630 return;
1632 -- Step 1: Loop transformations
1634 -- While loops are transformed into:
1636 -- if <Condition> then
1637 -- declare
1638 -- Temp1 : constant <type of Pref1> := <Pref1>;
1639 -- . . .
1640 -- TempN : constant <type of PrefN> := <PrefN>;
1641 -- begin
1642 -- loop
1643 -- <original source statements with attribute rewrites>
1644 -- exit when not <Condition>;
1645 -- end loop;
1646 -- end;
1647 -- end if;
1649 -- Note that loops over iterators and containers are already converted
1650 -- into while loops.
1652 elsif Present (Condition (Scheme)) then
1653 declare
1654 Cond : constant Node_Id := Condition (Scheme);
1656 begin
1657 -- Transform the original while loop into an infinite loop where
1658 -- the last statement checks the negated condition. This placement
1659 -- ensures that the condition will not be evaluated twice on the
1660 -- first iteration.
1662 -- Generate:
1663 -- exit when not <Cond>:
1665 Append_To (Statements (N),
1666 Make_Exit_Statement (Loc,
1667 Condition => Make_Op_Not (Loc, New_Copy_Tree (Cond))));
1669 Build_Conditional_Block (Loc,
1670 Cond => Relocate_Node (Cond),
1671 Stmt => Relocate_Node (N),
1672 If_Stmt => Result,
1673 Blk_Stmt => Blk);
1674 end;
1676 -- Ada 2012 iteration over an array is transformed into:
1678 -- if <Array_Nam>'Length (1) > 0
1679 -- and then <Array_Nam>'Length (N) > 0
1680 -- then
1681 -- declare
1682 -- Temp1 : constant <type of Pref1> := <Pref1>;
1683 -- . . .
1684 -- TempN : constant <type of PrefN> := <PrefN>;
1685 -- begin
1686 -- for X in ... loop -- multiple loops depending on dims
1687 -- <original source statements with attribute rewrites>
1688 -- end loop;
1689 -- end;
1690 -- end if;
1692 elsif Is_Array_Iteration (N) then
1693 declare
1694 Array_Nam : constant Entity_Id :=
1695 Entity (Name (Iterator_Specification
1696 (Iteration_Scheme (Original_Node (N)))));
1697 Num_Dims : constant Pos :=
1698 Number_Dimensions (Etype (Array_Nam));
1699 Cond : Node_Id := Empty;
1700 Check : Node_Id;
1701 Top_Loop : Node_Id;
1703 begin
1704 -- Generate a check which determines whether all dimensions of
1705 -- the array are non-null.
1707 for Dim in 1 .. Num_Dims loop
1708 Check :=
1709 Make_Op_Gt (Loc,
1710 Left_Opnd =>
1711 Make_Attribute_Reference (Loc,
1712 Prefix => New_Reference_To (Array_Nam, Loc),
1713 Attribute_Name => Name_Length,
1714 Expressions => New_List (
1715 Make_Integer_Literal (Loc, Dim))),
1716 Right_Opnd =>
1717 Make_Integer_Literal (Loc, 0));
1719 if No (Cond) then
1720 Cond := Check;
1721 else
1722 Cond :=
1723 Make_And_Then (Loc,
1724 Left_Opnd => Cond,
1725 Right_Opnd => Check);
1726 end if;
1727 end loop;
1729 Top_Loop := Relocate_Node (N);
1730 Set_Analyzed (Top_Loop);
1732 Build_Conditional_Block (Loc,
1733 Cond => Cond,
1734 Stmt => Top_Loop,
1735 If_Stmt => Result,
1736 Blk_Stmt => Blk);
1737 end;
1739 -- For loops are transformed into:
1741 -- if <Low> <= <High> then
1742 -- declare
1743 -- Temp1 : constant <type of Pref1> := <Pref1>;
1744 -- . . .
1745 -- TempN : constant <type of PrefN> := <PrefN>;
1746 -- begin
1747 -- for <Def_Id> in <Low> .. <High> loop
1748 -- <original source statements with attribute rewrites>
1749 -- end loop;
1750 -- end;
1751 -- end if;
1753 elsif Present (Loop_Parameter_Specification (Scheme)) then
1754 declare
1755 Loop_Spec : constant Node_Id :=
1756 Loop_Parameter_Specification (Scheme);
1757 Subt_Def : constant Node_Id :=
1758 Discrete_Subtype_Definition (Loop_Spec);
1759 Cond : Node_Id;
1761 begin
1762 -- At this point in the expansion all discrete subtype definitions
1763 -- should be transformed into ranges.
1765 pragma Assert (Nkind (Subt_Def) = N_Range);
1767 -- Generate
1768 -- Low <= High
1770 Cond :=
1771 Make_Op_Le (Loc,
1772 Left_Opnd => New_Copy_Tree (Low_Bound (Subt_Def)),
1773 Right_Opnd => New_Copy_Tree (High_Bound (Subt_Def)));
1775 Build_Conditional_Block (Loc,
1776 Cond => Cond,
1777 Stmt => Relocate_Node (N),
1778 If_Stmt => Result,
1779 Blk_Stmt => Blk);
1780 end;
1782 -- Infinite loops are transformed into:
1784 -- declare
1785 -- Temp1 : constant <type of Pref1> := <Pref1>;
1786 -- . . .
1787 -- TempN : constant <type of PrefN> := <PrefN>;
1788 -- begin
1789 -- loop
1790 -- <original source statements with attribute rewrites>
1791 -- end loop;
1792 -- end;
1794 else
1795 Build_Conditional_Block (Loc,
1796 Cond => Empty,
1797 Stmt => Relocate_Node (N),
1798 If_Stmt => Result,
1799 Blk_Stmt => Blk);
1801 Result := Blk;
1802 end if;
1804 -- Step 2: Loop_Entry attribute transformations
1806 -- At this point the various loops have been augmented to contain a
1807 -- block. Populate the declarative list of the block with constants
1808 -- which store the value of their relative prefixes at the point of
1809 -- entry in the loop.
1811 LE_Elmt := First_Elmt (Loop_Entry_Attributes (Entity (Loop_Id)));
1812 while Present (LE_Elmt) loop
1813 LE := Node (LE_Elmt);
1814 Typ := Etype (Prefix (LE));
1816 -- Declare a constant to capture the value of the previx of each
1817 -- Loop_Entry attribute.
1819 -- Generate:
1820 -- Temp : constant <type of Pref> := <Pref>;
1822 Temp := Make_Temporary (Loc, 'P');
1824 Append_To (Declarations (Blk),
1825 Make_Object_Declaration (Loc,
1826 Defining_Identifier => Temp,
1827 Constant_Present => True,
1828 Object_Definition => New_Reference_To (Typ, Loc),
1829 Expression => Relocate_Node (Prefix (LE))));
1831 -- Replace the original attribute with a reference to the constant
1833 Rewrite (LE, New_Reference_To (Temp, Loc));
1834 Set_Etype (LE, Typ);
1836 Next_Elmt (LE_Elmt);
1837 end loop;
1839 -- Destroy the list of Loop_Entry attributes to prevent the infinite
1840 -- expansion when analyzing and expanding the newly generated loops.
1842 Set_Loop_Entry_Attributes (Entity (Loop_Id), No_Elist);
1844 Rewrite (N, Result);
1845 Analyze (N);
1846 end Expand_Loop_Entry_Attributes;
1848 -----------------------------------
1849 -- Expand_N_Assignment_Statement --
1850 -----------------------------------
1852 -- This procedure implements various cases where an assignment statement
1853 -- cannot just be passed on to the back end in untransformed state.
1855 procedure Expand_N_Assignment_Statement (N : Node_Id) is
1856 Loc : constant Source_Ptr := Sloc (N);
1857 Crep : constant Boolean := Change_Of_Representation (N);
1858 Lhs : constant Node_Id := Name (N);
1859 Rhs : constant Node_Id := Expression (N);
1860 Typ : constant Entity_Id := Underlying_Type (Etype (Lhs));
1861 Exp : Node_Id;
1863 begin
1864 -- Special case to check right away, if the Componentwise_Assignment
1865 -- flag is set, this is a reanalysis from the expansion of the primitive
1866 -- assignment procedure for a tagged type, and all we need to do is to
1867 -- expand to assignment of components, because otherwise, we would get
1868 -- infinite recursion (since this looks like a tagged assignment which
1869 -- would normally try to *call* the primitive assignment procedure).
1871 if Componentwise_Assignment (N) then
1872 Expand_Assign_Record (N);
1873 return;
1874 end if;
1876 -- Defend against invalid subscripts on left side if we are in standard
1877 -- validity checking mode. No need to do this if we are checking all
1878 -- subscripts.
1880 -- Note that we do this right away, because there are some early return
1881 -- paths in this procedure, and this is required on all paths.
1883 if Validity_Checks_On
1884 and then Validity_Check_Default
1885 and then not Validity_Check_Subscripts
1886 then
1887 Check_Valid_Lvalue_Subscripts (Lhs);
1888 end if;
1890 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1892 -- Rewrite an assignment to X'Priority into a run-time call
1894 -- For example: X'Priority := New_Prio_Expr;
1895 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1897 -- Note that although X'Priority is notionally an object, it is quite
1898 -- deliberately not defined as an aliased object in the RM. This means
1899 -- that it works fine to rewrite it as a call, without having to worry
1900 -- about complications that would other arise from X'Priority'Access,
1901 -- which is illegal, because of the lack of aliasing.
1903 if Ada_Version >= Ada_2005 then
1904 declare
1905 Call : Node_Id;
1906 Conctyp : Entity_Id;
1907 Ent : Entity_Id;
1908 Subprg : Entity_Id;
1909 RT_Subprg_Name : Node_Id;
1911 begin
1912 -- Handle chains of renamings
1914 Ent := Name (N);
1915 while Nkind (Ent) in N_Has_Entity
1916 and then Present (Entity (Ent))
1917 and then Present (Renamed_Object (Entity (Ent)))
1918 loop
1919 Ent := Renamed_Object (Entity (Ent));
1920 end loop;
1922 -- The attribute Priority applied to protected objects has been
1923 -- previously expanded into a call to the Get_Ceiling run-time
1924 -- subprogram.
1926 if Nkind (Ent) = N_Function_Call
1927 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
1928 or else
1929 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling))
1930 then
1931 -- Look for the enclosing concurrent type
1933 Conctyp := Current_Scope;
1934 while not Is_Concurrent_Type (Conctyp) loop
1935 Conctyp := Scope (Conctyp);
1936 end loop;
1938 pragma Assert (Is_Protected_Type (Conctyp));
1940 -- Generate the first actual of the call
1942 Subprg := Current_Scope;
1943 while not Present (Protected_Body_Subprogram (Subprg)) loop
1944 Subprg := Scope (Subprg);
1945 end loop;
1947 -- Select the appropriate run-time call
1949 if Number_Entries (Conctyp) = 0 then
1950 RT_Subprg_Name :=
1951 New_Reference_To (RTE (RE_Set_Ceiling), Loc);
1952 else
1953 RT_Subprg_Name :=
1954 New_Reference_To (RTE (RO_PE_Set_Ceiling), Loc);
1955 end if;
1957 Call :=
1958 Make_Procedure_Call_Statement (Loc,
1959 Name => RT_Subprg_Name,
1960 Parameter_Associations => New_List (
1961 New_Copy_Tree (First (Parameter_Associations (Ent))),
1962 Relocate_Node (Expression (N))));
1964 Rewrite (N, Call);
1965 Analyze (N);
1966 return;
1967 end if;
1968 end;
1969 end if;
1971 -- Deal with assignment checks unless suppressed
1973 if not Suppress_Assignment_Checks (N) then
1975 -- First deal with generation of range check if required
1977 if Do_Range_Check (Rhs) then
1978 Set_Do_Range_Check (Rhs, False);
1979 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
1980 end if;
1982 -- Then generate predicate check if required
1984 Apply_Predicate_Check (Rhs, Typ);
1985 end if;
1987 -- Check for a special case where a high level transformation is
1988 -- required. If we have either of:
1990 -- P.field := rhs;
1991 -- P (sub) := rhs;
1993 -- where P is a reference to a bit packed array, then we have to unwind
1994 -- the assignment. The exact meaning of being a reference to a bit
1995 -- packed array is as follows:
1997 -- An indexed component whose prefix is a bit packed array is a
1998 -- reference to a bit packed array.
2000 -- An indexed component or selected component whose prefix is a
2001 -- reference to a bit packed array is itself a reference ot a
2002 -- bit packed array.
2004 -- The required transformation is
2006 -- Tnn : prefix_type := P;
2007 -- Tnn.field := rhs;
2008 -- P := Tnn;
2010 -- or
2012 -- Tnn : prefix_type := P;
2013 -- Tnn (subscr) := rhs;
2014 -- P := Tnn;
2016 -- Since P is going to be evaluated more than once, any subscripts
2017 -- in P must have their evaluation forced.
2019 if Nkind_In (Lhs, N_Indexed_Component, N_Selected_Component)
2020 and then Is_Ref_To_Bit_Packed_Array (Prefix (Lhs))
2021 then
2022 declare
2023 BPAR_Expr : constant Node_Id := Relocate_Node (Prefix (Lhs));
2024 BPAR_Typ : constant Entity_Id := Etype (BPAR_Expr);
2025 Tnn : constant Entity_Id :=
2026 Make_Temporary (Loc, 'T', BPAR_Expr);
2028 begin
2029 -- Insert the post assignment first, because we want to copy the
2030 -- BPAR_Expr tree before it gets analyzed in the context of the
2031 -- pre assignment. Note that we do not analyze the post assignment
2032 -- yet (we cannot till we have completed the analysis of the pre
2033 -- assignment). As usual, the analysis of this post assignment
2034 -- will happen on its own when we "run into" it after finishing
2035 -- the current assignment.
2037 Insert_After (N,
2038 Make_Assignment_Statement (Loc,
2039 Name => New_Copy_Tree (BPAR_Expr),
2040 Expression => New_Occurrence_Of (Tnn, Loc)));
2042 -- At this stage BPAR_Expr is a reference to a bit packed array
2043 -- where the reference was not expanded in the original tree,
2044 -- since it was on the left side of an assignment. But in the
2045 -- pre-assignment statement (the object definition), BPAR_Expr
2046 -- will end up on the right hand side, and must be reexpanded. To
2047 -- achieve this, we reset the analyzed flag of all selected and
2048 -- indexed components down to the actual indexed component for
2049 -- the packed array.
2051 Exp := BPAR_Expr;
2052 loop
2053 Set_Analyzed (Exp, False);
2055 if Nkind_In
2056 (Exp, N_Selected_Component, N_Indexed_Component)
2057 then
2058 Exp := Prefix (Exp);
2059 else
2060 exit;
2061 end if;
2062 end loop;
2064 -- Now we can insert and analyze the pre-assignment
2066 -- If the right-hand side requires a transient scope, it has
2067 -- already been placed on the stack. However, the declaration is
2068 -- inserted in the tree outside of this scope, and must reflect
2069 -- the proper scope for its variable. This awkward bit is forced
2070 -- by the stricter scope discipline imposed by GCC 2.97.
2072 declare
2073 Uses_Transient_Scope : constant Boolean :=
2074 Scope_Is_Transient
2075 and then N = Node_To_Be_Wrapped;
2077 begin
2078 if Uses_Transient_Scope then
2079 Push_Scope (Scope (Current_Scope));
2080 end if;
2082 Insert_Before_And_Analyze (N,
2083 Make_Object_Declaration (Loc,
2084 Defining_Identifier => Tnn,
2085 Object_Definition => New_Occurrence_Of (BPAR_Typ, Loc),
2086 Expression => BPAR_Expr));
2088 if Uses_Transient_Scope then
2089 Pop_Scope;
2090 end if;
2091 end;
2093 -- Now fix up the original assignment and continue processing
2095 Rewrite (Prefix (Lhs),
2096 New_Occurrence_Of (Tnn, Loc));
2098 -- We do not need to reanalyze that assignment, and we do not need
2099 -- to worry about references to the temporary, but we do need to
2100 -- make sure that the temporary is not marked as a true constant
2101 -- since we now have a generated assignment to it!
2103 Set_Is_True_Constant (Tnn, False);
2104 end;
2105 end if;
2107 -- When we have the appropriate type of aggregate in the expression (it
2108 -- has been determined during analysis of the aggregate by setting the
2109 -- delay flag), let's perform in place assignment and thus avoid
2110 -- creating a temporary.
2112 if Is_Delayed_Aggregate (Rhs) then
2113 Convert_Aggr_In_Assignment (N);
2114 Rewrite (N, Make_Null_Statement (Loc));
2115 Analyze (N);
2116 return;
2117 end if;
2119 -- Apply discriminant check if required. If Lhs is an access type to a
2120 -- designated type with discriminants, we must always check.
2122 if Has_Discriminants (Etype (Lhs)) then
2124 -- Skip discriminant check if change of representation. Will be
2125 -- done when the change of representation is expanded out.
2127 if not Crep then
2128 Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
2129 end if;
2131 -- If the type is private without discriminants, and the full type
2132 -- has discriminants (necessarily with defaults) a check may still be
2133 -- necessary if the Lhs is aliased. The private discriminants must be
2134 -- visible to build the discriminant constraints.
2136 -- Only an explicit dereference that comes from source indicates
2137 -- aliasing. Access to formals of protected operations and entries
2138 -- create dereferences but are not semantic aliasings.
2140 elsif Is_Private_Type (Etype (Lhs))
2141 and then Has_Discriminants (Typ)
2142 and then Nkind (Lhs) = N_Explicit_Dereference
2143 and then Comes_From_Source (Lhs)
2144 then
2145 declare
2146 Lt : constant Entity_Id := Etype (Lhs);
2147 Ubt : Entity_Id := Base_Type (Typ);
2149 begin
2150 -- In the case of an expander-generated record subtype whose base
2151 -- type still appears private, Typ will have been set to that
2152 -- private type rather than the underlying record type (because
2153 -- Underlying type will have returned the record subtype), so it's
2154 -- necessary to apply Underlying_Type again to the base type to
2155 -- get the record type we need for the discriminant check. Such
2156 -- subtypes can be created for assignments in certain cases, such
2157 -- as within an instantiation passed this kind of private type.
2158 -- It would be good to avoid this special test, but making changes
2159 -- to prevent this odd form of record subtype seems difficult. ???
2161 if Is_Private_Type (Ubt) then
2162 Ubt := Underlying_Type (Ubt);
2163 end if;
2165 Set_Etype (Lhs, Ubt);
2166 Rewrite (Rhs, OK_Convert_To (Base_Type (Ubt), Rhs));
2167 Apply_Discriminant_Check (Rhs, Ubt, Lhs);
2168 Set_Etype (Lhs, Lt);
2169 end;
2171 -- If the Lhs has a private type with unknown discriminants, it
2172 -- may have a full view with discriminants, but those are nameable
2173 -- only in the underlying type, so convert the Rhs to it before
2174 -- potential checking.
2176 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs)))
2177 and then Has_Discriminants (Typ)
2178 then
2179 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
2180 Apply_Discriminant_Check (Rhs, Typ, Lhs);
2182 -- In the access type case, we need the same discriminant check, and
2183 -- also range checks if we have an access to constrained array.
2185 elsif Is_Access_Type (Etype (Lhs))
2186 and then Is_Constrained (Designated_Type (Etype (Lhs)))
2187 then
2188 if Has_Discriminants (Designated_Type (Etype (Lhs))) then
2190 -- Skip discriminant check if change of representation. Will be
2191 -- done when the change of representation is expanded out.
2193 if not Crep then
2194 Apply_Discriminant_Check (Rhs, Etype (Lhs));
2195 end if;
2197 elsif Is_Array_Type (Designated_Type (Etype (Lhs))) then
2198 Apply_Range_Check (Rhs, Etype (Lhs));
2200 if Is_Constrained (Etype (Lhs)) then
2201 Apply_Length_Check (Rhs, Etype (Lhs));
2202 end if;
2204 if Nkind (Rhs) = N_Allocator then
2205 declare
2206 Target_Typ : constant Entity_Id := Etype (Expression (Rhs));
2207 C_Es : Check_Result;
2209 begin
2210 C_Es :=
2211 Get_Range_Checks
2212 (Lhs,
2213 Target_Typ,
2214 Etype (Designated_Type (Etype (Lhs))));
2216 Insert_Range_Checks
2217 (C_Es,
2219 Target_Typ,
2220 Sloc (Lhs),
2221 Lhs);
2222 end;
2223 end if;
2224 end if;
2226 -- Apply range check for access type case
2228 elsif Is_Access_Type (Etype (Lhs))
2229 and then Nkind (Rhs) = N_Allocator
2230 and then Nkind (Expression (Rhs)) = N_Qualified_Expression
2231 then
2232 Analyze_And_Resolve (Expression (Rhs));
2233 Apply_Range_Check
2234 (Expression (Rhs), Designated_Type (Etype (Lhs)));
2235 end if;
2237 -- Ada 2005 (AI-231): Generate the run-time check
2239 if Is_Access_Type (Typ)
2240 and then Can_Never_Be_Null (Etype (Lhs))
2241 and then not Can_Never_Be_Null (Etype (Rhs))
2242 then
2243 Apply_Constraint_Check (Rhs, Etype (Lhs));
2244 end if;
2246 -- Ada 2012 (AI05-148): Update current accessibility level if Rhs is a
2247 -- stand-alone obj of an anonymous access type.
2249 if Is_Access_Type (Typ)
2250 and then Is_Entity_Name (Lhs)
2251 and then Present (Effective_Extra_Accessibility (Entity (Lhs))) then
2252 declare
2253 function Lhs_Entity return Entity_Id;
2254 -- Look through renames to find the underlying entity.
2255 -- For assignment to a rename, we don't care about the
2256 -- Enclosing_Dynamic_Scope of the rename declaration.
2258 ----------------
2259 -- Lhs_Entity --
2260 ----------------
2262 function Lhs_Entity return Entity_Id is
2263 Result : Entity_Id := Entity (Lhs);
2265 begin
2266 while Present (Renamed_Object (Result)) loop
2268 -- Renamed_Object must return an Entity_Name here
2269 -- because of preceding "Present (E_E_A (...))" test.
2271 Result := Entity (Renamed_Object (Result));
2272 end loop;
2274 return Result;
2275 end Lhs_Entity;
2277 -- Local Declarations
2279 Access_Check : constant Node_Id :=
2280 Make_Raise_Program_Error (Loc,
2281 Condition =>
2282 Make_Op_Gt (Loc,
2283 Left_Opnd =>
2284 Dynamic_Accessibility_Level (Rhs),
2285 Right_Opnd =>
2286 Make_Integer_Literal (Loc,
2287 Intval =>
2288 Scope_Depth
2289 (Enclosing_Dynamic_Scope
2290 (Lhs_Entity)))),
2291 Reason => PE_Accessibility_Check_Failed);
2293 Access_Level_Update : constant Node_Id :=
2294 Make_Assignment_Statement (Loc,
2295 Name =>
2296 New_Occurrence_Of
2297 (Effective_Extra_Accessibility
2298 (Entity (Lhs)), Loc),
2299 Expression =>
2300 Dynamic_Accessibility_Level (Rhs));
2302 begin
2303 if not Accessibility_Checks_Suppressed (Entity (Lhs)) then
2304 Insert_Action (N, Access_Check);
2305 end if;
2307 Insert_Action (N, Access_Level_Update);
2308 end;
2309 end if;
2311 -- Case of assignment to a bit packed array element. If there is a
2312 -- change of representation this must be expanded into components,
2313 -- otherwise this is a bit-field assignment.
2315 if Nkind (Lhs) = N_Indexed_Component
2316 and then Is_Bit_Packed_Array (Etype (Prefix (Lhs)))
2317 then
2318 -- Normal case, no change of representation
2320 if not Crep then
2321 Expand_Bit_Packed_Element_Set (N);
2322 return;
2324 -- Change of representation case
2326 else
2327 -- Generate the following, to force component-by-component
2328 -- assignments in an efficient way. Otherwise each component
2329 -- will require a temporary and two bit-field manipulations.
2331 -- T1 : Elmt_Type;
2332 -- T1 := RhS;
2333 -- Lhs := T1;
2335 declare
2336 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T');
2337 Stats : List_Id;
2339 begin
2340 Stats :=
2341 New_List (
2342 Make_Object_Declaration (Loc,
2343 Defining_Identifier => Tnn,
2344 Object_Definition =>
2345 New_Occurrence_Of (Etype (Lhs), Loc)),
2346 Make_Assignment_Statement (Loc,
2347 Name => New_Occurrence_Of (Tnn, Loc),
2348 Expression => Relocate_Node (Rhs)),
2349 Make_Assignment_Statement (Loc,
2350 Name => Relocate_Node (Lhs),
2351 Expression => New_Occurrence_Of (Tnn, Loc)));
2353 Insert_Actions (N, Stats);
2354 Rewrite (N, Make_Null_Statement (Loc));
2355 Analyze (N);
2356 end;
2357 end if;
2359 -- Build-in-place function call case. Note that we're not yet doing
2360 -- build-in-place for user-written assignment statements (the assignment
2361 -- here came from an aggregate.)
2363 elsif Ada_Version >= Ada_2005
2364 and then Is_Build_In_Place_Function_Call (Rhs)
2365 then
2366 Make_Build_In_Place_Call_In_Assignment (N, Rhs);
2368 elsif Is_Tagged_Type (Typ) and then Is_Value_Type (Etype (Lhs)) then
2370 -- Nothing to do for valuetypes
2371 -- ??? Set_Scope_Is_Transient (False);
2373 return;
2375 elsif Is_Tagged_Type (Typ)
2376 or else (Needs_Finalization (Typ) and then not Is_Array_Type (Typ))
2377 then
2378 Tagged_Case : declare
2379 L : List_Id := No_List;
2380 Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
2382 begin
2383 -- In the controlled case, we ensure that function calls are
2384 -- evaluated before finalizing the target. In all cases, it makes
2385 -- the expansion easier if the side-effects are removed first.
2387 Remove_Side_Effects (Lhs);
2388 Remove_Side_Effects (Rhs);
2390 -- Avoid recursion in the mechanism
2392 Set_Analyzed (N);
2394 -- If dispatching assignment, we need to dispatch to _assign
2396 if Is_Class_Wide_Type (Typ)
2398 -- If the type is tagged, we may as well use the predefined
2399 -- primitive assignment. This avoids inlining a lot of code
2400 -- and in the class-wide case, the assignment is replaced
2401 -- by a dispatching call to _assign. It is suppressed in the
2402 -- case of assignments created by the expander that correspond
2403 -- to initializations, where we do want to copy the tag
2404 -- (Expand_Ctrl_Actions flag is set True in this case). It is
2405 -- also suppressed if restriction No_Dispatching_Calls is in
2406 -- force because in that case predefined primitives are not
2407 -- generated.
2409 or else (Is_Tagged_Type (Typ)
2410 and then not Is_Value_Type (Etype (Lhs))
2411 and then Chars (Current_Scope) /= Name_uAssign
2412 and then Expand_Ctrl_Actions
2413 and then
2414 not Restriction_Active (No_Dispatching_Calls))
2415 then
2416 if Is_Limited_Type (Typ) then
2418 -- This can happen in an instance when the formal is an
2419 -- extension of a limited interface, and the actual is
2420 -- limited. This is an error according to AI05-0087, but
2421 -- is not caught at the point of instantiation in earlier
2422 -- versions.
2424 -- This is wrong, error messages cannot be issued during
2425 -- expansion, since they would be missed in -gnatc mode ???
2427 Error_Msg_N ("assignment not available on limited type", N);
2428 return;
2429 end if;
2431 -- Fetch the primitive op _assign and proper type to call it.
2432 -- Because of possible conflicts between private and full view,
2433 -- fetch the proper type directly from the operation profile.
2435 declare
2436 Op : constant Entity_Id :=
2437 Find_Prim_Op (Typ, Name_uAssign);
2438 F_Typ : Entity_Id := Etype (First_Formal (Op));
2440 begin
2441 -- If the assignment is dispatching, make sure to use the
2442 -- proper type.
2444 if Is_Class_Wide_Type (Typ) then
2445 F_Typ := Class_Wide_Type (F_Typ);
2446 end if;
2448 L := New_List;
2450 -- In case of assignment to a class-wide tagged type, before
2451 -- the assignment we generate run-time check to ensure that
2452 -- the tags of source and target match.
2454 if Is_Class_Wide_Type (Typ)
2455 and then Is_Tagged_Type (Typ)
2456 and then Is_Tagged_Type (Underlying_Type (Etype (Rhs)))
2457 then
2458 Append_To (L,
2459 Make_Raise_Constraint_Error (Loc,
2460 Condition =>
2461 Make_Op_Ne (Loc,
2462 Left_Opnd =>
2463 Make_Selected_Component (Loc,
2464 Prefix => Duplicate_Subexpr (Lhs),
2465 Selector_Name =>
2466 Make_Identifier (Loc, Name_uTag)),
2467 Right_Opnd =>
2468 Make_Selected_Component (Loc,
2469 Prefix => Duplicate_Subexpr (Rhs),
2470 Selector_Name =>
2471 Make_Identifier (Loc, Name_uTag))),
2472 Reason => CE_Tag_Check_Failed));
2473 end if;
2475 declare
2476 Left_N : Node_Id := Duplicate_Subexpr (Lhs);
2477 Right_N : Node_Id := Duplicate_Subexpr (Rhs);
2479 begin
2480 -- In order to dispatch the call to _assign the type of
2481 -- the actuals must match. Add conversion (if required).
2483 if Etype (Lhs) /= F_Typ then
2484 Left_N := Unchecked_Convert_To (F_Typ, Left_N);
2485 end if;
2487 if Etype (Rhs) /= F_Typ then
2488 Right_N := Unchecked_Convert_To (F_Typ, Right_N);
2489 end if;
2491 Append_To (L,
2492 Make_Procedure_Call_Statement (Loc,
2493 Name => New_Reference_To (Op, Loc),
2494 Parameter_Associations => New_List (
2495 Node1 => Left_N,
2496 Node2 => Right_N)));
2497 end;
2498 end;
2500 else
2501 L := Make_Tag_Ctrl_Assignment (N);
2503 -- We can't afford to have destructive Finalization Actions in
2504 -- the Self assignment case, so if the target and the source
2505 -- are not obviously different, code is generated to avoid the
2506 -- self assignment case:
2508 -- if lhs'address /= rhs'address then
2509 -- <code for controlled and/or tagged assignment>
2510 -- end if;
2512 -- Skip this if Restriction (No_Finalization) is active
2514 if not Statically_Different (Lhs, Rhs)
2515 and then Expand_Ctrl_Actions
2516 and then not Restriction_Active (No_Finalization)
2517 then
2518 L := New_List (
2519 Make_Implicit_If_Statement (N,
2520 Condition =>
2521 Make_Op_Ne (Loc,
2522 Left_Opnd =>
2523 Make_Attribute_Reference (Loc,
2524 Prefix => Duplicate_Subexpr (Lhs),
2525 Attribute_Name => Name_Address),
2527 Right_Opnd =>
2528 Make_Attribute_Reference (Loc,
2529 Prefix => Duplicate_Subexpr (Rhs),
2530 Attribute_Name => Name_Address)),
2532 Then_Statements => L));
2533 end if;
2535 -- We need to set up an exception handler for implementing
2536 -- 7.6.1(18). The remaining adjustments are tackled by the
2537 -- implementation of adjust for record_controllers (see
2538 -- s-finimp.adb).
2540 -- This is skipped if we have no finalization
2542 if Expand_Ctrl_Actions
2543 and then not Restriction_Active (No_Finalization)
2544 then
2545 L := New_List (
2546 Make_Block_Statement (Loc,
2547 Handled_Statement_Sequence =>
2548 Make_Handled_Sequence_Of_Statements (Loc,
2549 Statements => L,
2550 Exception_Handlers => New_List (
2551 Make_Handler_For_Ctrl_Operation (Loc)))));
2552 end if;
2553 end if;
2555 Rewrite (N,
2556 Make_Block_Statement (Loc,
2557 Handled_Statement_Sequence =>
2558 Make_Handled_Sequence_Of_Statements (Loc, Statements => L)));
2560 -- If no restrictions on aborts, protect the whole assignment
2561 -- for controlled objects as per 9.8(11).
2563 if Needs_Finalization (Typ)
2564 and then Expand_Ctrl_Actions
2565 and then Abort_Allowed
2566 then
2567 declare
2568 Blk : constant Entity_Id :=
2569 New_Internal_Entity
2570 (E_Block, Current_Scope, Sloc (N), 'B');
2572 begin
2573 Set_Scope (Blk, Current_Scope);
2574 Set_Etype (Blk, Standard_Void_Type);
2575 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
2577 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
2578 Set_At_End_Proc (Handled_Statement_Sequence (N),
2579 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
2580 Expand_At_End_Handler
2581 (Handled_Statement_Sequence (N), Blk);
2582 end;
2583 end if;
2585 -- N has been rewritten to a block statement for which it is
2586 -- known by construction that no checks are necessary: analyze
2587 -- it with all checks suppressed.
2589 Analyze (N, Suppress => All_Checks);
2590 return;
2591 end Tagged_Case;
2593 -- Array types
2595 elsif Is_Array_Type (Typ) then
2596 declare
2597 Actual_Rhs : Node_Id := Rhs;
2599 begin
2600 while Nkind_In (Actual_Rhs, N_Type_Conversion,
2601 N_Qualified_Expression)
2602 loop
2603 Actual_Rhs := Expression (Actual_Rhs);
2604 end loop;
2606 Expand_Assign_Array (N, Actual_Rhs);
2607 return;
2608 end;
2610 -- Record types
2612 elsif Is_Record_Type (Typ) then
2613 Expand_Assign_Record (N);
2614 return;
2616 -- Scalar types. This is where we perform the processing related to the
2617 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2618 -- scalar values.
2620 elsif Is_Scalar_Type (Typ) then
2622 -- Case where right side is known valid
2624 if Expr_Known_Valid (Rhs) then
2626 -- Here the right side is valid, so it is fine. The case to deal
2627 -- with is when the left side is a local variable reference whose
2628 -- value is not currently known to be valid. If this is the case,
2629 -- and the assignment appears in an unconditional context, then
2630 -- we can mark the left side as now being valid if one of these
2631 -- conditions holds:
2633 -- The expression of the right side has Do_Range_Check set so
2634 -- that we know a range check will be performed. Note that it
2635 -- can be the case that a range check is omitted because we
2636 -- make the assumption that we can assume validity for operands
2637 -- appearing in the right side in determining whether a range
2638 -- check is required
2640 -- The subtype of the right side matches the subtype of the
2641 -- left side. In this case, even though we have not checked
2642 -- the range of the right side, we know it is in range of its
2643 -- subtype if the expression is valid.
2645 if Is_Local_Variable_Reference (Lhs)
2646 and then not Is_Known_Valid (Entity (Lhs))
2647 and then In_Unconditional_Context (N)
2648 then
2649 if Do_Range_Check (Rhs)
2650 or else Etype (Lhs) = Etype (Rhs)
2651 then
2652 Set_Is_Known_Valid (Entity (Lhs), True);
2653 end if;
2654 end if;
2656 -- Case where right side may be invalid in the sense of the RM
2657 -- reference above. The RM does not require that we check for the
2658 -- validity on an assignment, but it does require that the assignment
2659 -- of an invalid value not cause erroneous behavior.
2661 -- The general approach in GNAT is to use the Is_Known_Valid flag
2662 -- to avoid the need for validity checking on assignments. However
2663 -- in some cases, we have to do validity checking in order to make
2664 -- sure that the setting of this flag is correct.
2666 else
2667 -- Validate right side if we are validating copies
2669 if Validity_Checks_On
2670 and then Validity_Check_Copies
2671 then
2672 -- Skip this if left hand side is an array or record component
2673 -- and elementary component validity checks are suppressed.
2675 if Nkind_In (Lhs, N_Selected_Component, N_Indexed_Component)
2676 and then not Validity_Check_Components
2677 then
2678 null;
2679 else
2680 Ensure_Valid (Rhs);
2681 end if;
2683 -- We can propagate this to the left side where appropriate
2685 if Is_Local_Variable_Reference (Lhs)
2686 and then not Is_Known_Valid (Entity (Lhs))
2687 and then In_Unconditional_Context (N)
2688 then
2689 Set_Is_Known_Valid (Entity (Lhs), True);
2690 end if;
2692 -- Otherwise check to see what should be done
2694 -- If left side is a local variable, then we just set its flag to
2695 -- indicate that its value may no longer be valid, since we are
2696 -- copying a potentially invalid value.
2698 elsif Is_Local_Variable_Reference (Lhs) then
2699 Set_Is_Known_Valid (Entity (Lhs), False);
2701 -- Check for case of a nonlocal variable on the left side which
2702 -- is currently known to be valid. In this case, we simply ensure
2703 -- that the right side is valid. We only play the game of copying
2704 -- validity status for local variables, since we are doing this
2705 -- statically, not by tracing the full flow graph.
2707 elsif Is_Entity_Name (Lhs)
2708 and then Is_Known_Valid (Entity (Lhs))
2709 then
2710 -- Note: If Validity_Checking mode is set to none, we ignore
2711 -- the Ensure_Valid call so don't worry about that case here.
2713 Ensure_Valid (Rhs);
2715 -- In all other cases, we can safely copy an invalid value without
2716 -- worrying about the status of the left side. Since it is not a
2717 -- variable reference it will not be considered
2718 -- as being known to be valid in any case.
2720 else
2721 null;
2722 end if;
2723 end if;
2724 end if;
2726 exception
2727 when RE_Not_Available =>
2728 return;
2729 end Expand_N_Assignment_Statement;
2731 ------------------------------
2732 -- Expand_N_Block_Statement --
2733 ------------------------------
2735 -- Encode entity names defined in block statement
2737 procedure Expand_N_Block_Statement (N : Node_Id) is
2738 begin
2739 Qualify_Entity_Names (N);
2740 end Expand_N_Block_Statement;
2742 -----------------------------
2743 -- Expand_N_Case_Statement --
2744 -----------------------------
2746 procedure Expand_N_Case_Statement (N : Node_Id) is
2747 Loc : constant Source_Ptr := Sloc (N);
2748 Expr : constant Node_Id := Expression (N);
2749 Alt : Node_Id;
2750 Len : Nat;
2751 Cond : Node_Id;
2752 Choice : Node_Id;
2753 Chlist : List_Id;
2755 begin
2756 -- Check for the situation where we know at compile time which branch
2757 -- will be taken
2759 if Compile_Time_Known_Value (Expr) then
2760 Alt := Find_Static_Alternative (N);
2762 Process_Statements_For_Controlled_Objects (Alt);
2764 -- Move statements from this alternative after the case statement.
2765 -- They are already analyzed, so will be skipped by the analyzer.
2767 Insert_List_After (N, Statements (Alt));
2769 -- That leaves the case statement as a shell. So now we can kill all
2770 -- other alternatives in the case statement.
2772 Kill_Dead_Code (Expression (N));
2774 declare
2775 Dead_Alt : Node_Id;
2777 begin
2778 -- Loop through case alternatives, skipping pragmas, and skipping
2779 -- the one alternative that we select (and therefore retain).
2781 Dead_Alt := First (Alternatives (N));
2782 while Present (Dead_Alt) loop
2783 if Dead_Alt /= Alt
2784 and then Nkind (Dead_Alt) = N_Case_Statement_Alternative
2785 then
2786 Kill_Dead_Code (Statements (Dead_Alt), Warn_On_Deleted_Code);
2787 end if;
2789 Next (Dead_Alt);
2790 end loop;
2791 end;
2793 Rewrite (N, Make_Null_Statement (Loc));
2794 return;
2795 end if;
2797 -- Here if the choice is not determined at compile time
2799 declare
2800 Last_Alt : constant Node_Id := Last (Alternatives (N));
2802 Others_Present : Boolean;
2803 Others_Node : Node_Id;
2805 Then_Stms : List_Id;
2806 Else_Stms : List_Id;
2808 begin
2809 if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
2810 Others_Present := True;
2811 Others_Node := Last_Alt;
2812 else
2813 Others_Present := False;
2814 end if;
2816 -- First step is to worry about possible invalid argument. The RM
2817 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2818 -- outside the base range), then Constraint_Error must be raised.
2820 -- Case of validity check required (validity checks are on, the
2821 -- expression is not known to be valid, and the case statement
2822 -- comes from source -- no need to validity check internally
2823 -- generated case statements).
2825 if Validity_Check_Default then
2826 Ensure_Valid (Expr);
2827 end if;
2829 -- If there is only a single alternative, just replace it with the
2830 -- sequence of statements since obviously that is what is going to
2831 -- be executed in all cases.
2833 Len := List_Length (Alternatives (N));
2835 if Len = 1 then
2837 -- We still need to evaluate the expression if it has any side
2838 -- effects.
2840 Remove_Side_Effects (Expression (N));
2842 Alt := First (Alternatives (N));
2844 Process_Statements_For_Controlled_Objects (Alt);
2845 Insert_List_After (N, Statements (Alt));
2847 -- That leaves the case statement as a shell. The alternative that
2848 -- will be executed is reset to a null list. So now we can kill
2849 -- the entire case statement.
2851 Kill_Dead_Code (Expression (N));
2852 Rewrite (N, Make_Null_Statement (Loc));
2853 return;
2855 -- An optimization. If there are only two alternatives, and only
2856 -- a single choice, then rewrite the whole case statement as an
2857 -- if statement, since this can result in subsequent optimizations.
2858 -- This helps not only with case statements in the source of a
2859 -- simple form, but also with generated code (discriminant check
2860 -- functions in particular)
2862 elsif Len = 2 then
2863 Chlist := Discrete_Choices (First (Alternatives (N)));
2865 if List_Length (Chlist) = 1 then
2866 Choice := First (Chlist);
2868 Then_Stms := Statements (First (Alternatives (N)));
2869 Else_Stms := Statements (Last (Alternatives (N)));
2871 -- For TRUE, generate "expression", not expression = true
2873 if Nkind (Choice) = N_Identifier
2874 and then Entity (Choice) = Standard_True
2875 then
2876 Cond := Expression (N);
2878 -- For FALSE, generate "expression" and switch then/else
2880 elsif Nkind (Choice) = N_Identifier
2881 and then Entity (Choice) = Standard_False
2882 then
2883 Cond := Expression (N);
2884 Else_Stms := Statements (First (Alternatives (N)));
2885 Then_Stms := Statements (Last (Alternatives (N)));
2887 -- For a range, generate "expression in range"
2889 elsif Nkind (Choice) = N_Range
2890 or else (Nkind (Choice) = N_Attribute_Reference
2891 and then Attribute_Name (Choice) = Name_Range)
2892 or else (Is_Entity_Name (Choice)
2893 and then Is_Type (Entity (Choice)))
2894 or else Nkind (Choice) = N_Subtype_Indication
2895 then
2896 Cond :=
2897 Make_In (Loc,
2898 Left_Opnd => Expression (N),
2899 Right_Opnd => Relocate_Node (Choice));
2901 -- For any other subexpression "expression = value"
2903 else
2904 Cond :=
2905 Make_Op_Eq (Loc,
2906 Left_Opnd => Expression (N),
2907 Right_Opnd => Relocate_Node (Choice));
2908 end if;
2910 -- Now rewrite the case as an IF
2912 Rewrite (N,
2913 Make_If_Statement (Loc,
2914 Condition => Cond,
2915 Then_Statements => Then_Stms,
2916 Else_Statements => Else_Stms));
2917 Analyze (N);
2918 return;
2919 end if;
2920 end if;
2922 -- If the last alternative is not an Others choice, replace it with
2923 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2924 -- the modified case statement, since it's only effect would be to
2925 -- compute the contents of the Others_Discrete_Choices which is not
2926 -- needed by the back end anyway.
2928 -- The reason we do this is that the back end always needs some
2929 -- default for a switch, so if we have not supplied one in the
2930 -- processing above for validity checking, then we need to supply
2931 -- one here.
2933 if not Others_Present then
2934 Others_Node := Make_Others_Choice (Sloc (Last_Alt));
2935 Set_Others_Discrete_Choices
2936 (Others_Node, Discrete_Choices (Last_Alt));
2937 Set_Discrete_Choices (Last_Alt, New_List (Others_Node));
2938 end if;
2940 Alt := First (Alternatives (N));
2941 while Present (Alt)
2942 and then Nkind (Alt) = N_Case_Statement_Alternative
2943 loop
2944 Process_Statements_For_Controlled_Objects (Alt);
2945 Next (Alt);
2946 end loop;
2947 end;
2948 end Expand_N_Case_Statement;
2950 -----------------------------
2951 -- Expand_N_Exit_Statement --
2952 -----------------------------
2954 -- The only processing required is to deal with a possible C/Fortran
2955 -- boolean value used as the condition for the exit statement.
2957 procedure Expand_N_Exit_Statement (N : Node_Id) is
2958 begin
2959 Adjust_Condition (Condition (N));
2960 end Expand_N_Exit_Statement;
2962 -----------------------------
2963 -- Expand_N_Goto_Statement --
2964 -----------------------------
2966 -- Add poll before goto if polling active
2968 procedure Expand_N_Goto_Statement (N : Node_Id) is
2969 begin
2970 Generate_Poll_Call (N);
2971 end Expand_N_Goto_Statement;
2973 ---------------------------
2974 -- Expand_N_If_Statement --
2975 ---------------------------
2977 -- First we deal with the case of C and Fortran convention boolean values,
2978 -- with zero/non-zero semantics.
2980 -- Second, we deal with the obvious rewriting for the cases where the
2981 -- condition of the IF is known at compile time to be True or False.
2983 -- Third, we remove elsif parts which have non-empty Condition_Actions and
2984 -- rewrite as independent if statements. For example:
2986 -- if x then xs
2987 -- elsif y then ys
2988 -- ...
2989 -- end if;
2991 -- becomes
2993 -- if x then xs
2994 -- else
2995 -- <<condition actions of y>>
2996 -- if y then ys
2997 -- ...
2998 -- end if;
2999 -- end if;
3001 -- This rewriting is needed if at least one elsif part has a non-empty
3002 -- Condition_Actions list. We also do the same processing if there is a
3003 -- constant condition in an elsif part (in conjunction with the first
3004 -- processing step mentioned above, for the recursive call made to deal
3005 -- with the created inner if, this deals with properly optimizing the
3006 -- cases of constant elsif conditions).
3008 procedure Expand_N_If_Statement (N : Node_Id) is
3009 Loc : constant Source_Ptr := Sloc (N);
3010 Hed : Node_Id;
3011 E : Node_Id;
3012 New_If : Node_Id;
3014 Warn_If_Deleted : constant Boolean :=
3015 Warn_On_Deleted_Code and then Comes_From_Source (N);
3016 -- Indicates whether we want warnings when we delete branches of the
3017 -- if statement based on constant condition analysis. We never want
3018 -- these warnings for expander generated code.
3020 begin
3021 Process_Statements_For_Controlled_Objects (N);
3023 Adjust_Condition (Condition (N));
3025 -- The following loop deals with constant conditions for the IF. We
3026 -- need a loop because as we eliminate False conditions, we grab the
3027 -- first elsif condition and use it as the primary condition.
3029 while Compile_Time_Known_Value (Condition (N)) loop
3031 -- If condition is True, we can simply rewrite the if statement now
3032 -- by replacing it by the series of then statements.
3034 if Is_True (Expr_Value (Condition (N))) then
3036 -- All the else parts can be killed
3038 Kill_Dead_Code (Elsif_Parts (N), Warn_If_Deleted);
3039 Kill_Dead_Code (Else_Statements (N), Warn_If_Deleted);
3041 Hed := Remove_Head (Then_Statements (N));
3042 Insert_List_After (N, Then_Statements (N));
3043 Rewrite (N, Hed);
3044 return;
3046 -- If condition is False, then we can delete the condition and
3047 -- the Then statements
3049 else
3050 -- We do not delete the condition if constant condition warnings
3051 -- are enabled, since otherwise we end up deleting the desired
3052 -- warning. Of course the backend will get rid of this True/False
3053 -- test anyway, so nothing is lost here.
3055 if not Constant_Condition_Warnings then
3056 Kill_Dead_Code (Condition (N));
3057 end if;
3059 Kill_Dead_Code (Then_Statements (N), Warn_If_Deleted);
3061 -- If there are no elsif statements, then we simply replace the
3062 -- entire if statement by the sequence of else statements.
3064 if No (Elsif_Parts (N)) then
3065 if No (Else_Statements (N))
3066 or else Is_Empty_List (Else_Statements (N))
3067 then
3068 Rewrite (N,
3069 Make_Null_Statement (Sloc (N)));
3070 else
3071 Hed := Remove_Head (Else_Statements (N));
3072 Insert_List_After (N, Else_Statements (N));
3073 Rewrite (N, Hed);
3074 end if;
3076 return;
3078 -- If there are elsif statements, the first of them becomes the
3079 -- if/then section of the rebuilt if statement This is the case
3080 -- where we loop to reprocess this copied condition.
3082 else
3083 Hed := Remove_Head (Elsif_Parts (N));
3084 Insert_Actions (N, Condition_Actions (Hed));
3085 Set_Condition (N, Condition (Hed));
3086 Set_Then_Statements (N, Then_Statements (Hed));
3088 -- Hed might have been captured as the condition determining
3089 -- the current value for an entity. Now it is detached from
3090 -- the tree, so a Current_Value pointer in the condition might
3091 -- need to be updated.
3093 Set_Current_Value_Condition (N);
3095 if Is_Empty_List (Elsif_Parts (N)) then
3096 Set_Elsif_Parts (N, No_List);
3097 end if;
3098 end if;
3099 end if;
3100 end loop;
3102 -- Loop through elsif parts, dealing with constant conditions and
3103 -- possible condition actions that are present.
3105 if Present (Elsif_Parts (N)) then
3106 E := First (Elsif_Parts (N));
3107 while Present (E) loop
3108 Process_Statements_For_Controlled_Objects (E);
3110 Adjust_Condition (Condition (E));
3112 -- If there are condition actions, then rewrite the if statement
3113 -- as indicated above. We also do the same rewrite for a True or
3114 -- False condition. The further processing of this constant
3115 -- condition is then done by the recursive call to expand the
3116 -- newly created if statement
3118 if Present (Condition_Actions (E))
3119 or else Compile_Time_Known_Value (Condition (E))
3120 then
3121 -- Note this is not an implicit if statement, since it is part
3122 -- of an explicit if statement in the source (or of an implicit
3123 -- if statement that has already been tested).
3125 New_If :=
3126 Make_If_Statement (Sloc (E),
3127 Condition => Condition (E),
3128 Then_Statements => Then_Statements (E),
3129 Elsif_Parts => No_List,
3130 Else_Statements => Else_Statements (N));
3132 -- Elsif parts for new if come from remaining elsif's of parent
3134 while Present (Next (E)) loop
3135 if No (Elsif_Parts (New_If)) then
3136 Set_Elsif_Parts (New_If, New_List);
3137 end if;
3139 Append (Remove_Next (E), Elsif_Parts (New_If));
3140 end loop;
3142 Set_Else_Statements (N, New_List (New_If));
3144 if Present (Condition_Actions (E)) then
3145 Insert_List_Before (New_If, Condition_Actions (E));
3146 end if;
3148 Remove (E);
3150 if Is_Empty_List (Elsif_Parts (N)) then
3151 Set_Elsif_Parts (N, No_List);
3152 end if;
3154 Analyze (New_If);
3155 return;
3157 -- No special processing for that elsif part, move to next
3159 else
3160 Next (E);
3161 end if;
3162 end loop;
3163 end if;
3165 -- Some more optimizations applicable if we still have an IF statement
3167 if Nkind (N) /= N_If_Statement then
3168 return;
3169 end if;
3171 -- Another optimization, special cases that can be simplified
3173 -- if expression then
3174 -- return true;
3175 -- else
3176 -- return false;
3177 -- end if;
3179 -- can be changed to:
3181 -- return expression;
3183 -- and
3185 -- if expression then
3186 -- return false;
3187 -- else
3188 -- return true;
3189 -- end if;
3191 -- can be changed to:
3193 -- return not (expression);
3195 -- Only do these optimizations if we are at least at -O1 level and
3196 -- do not do them if control flow optimizations are suppressed.
3198 if Optimization_Level > 0
3199 and then not Opt.Suppress_Control_Flow_Optimizations
3200 then
3201 if Nkind (N) = N_If_Statement
3202 and then No (Elsif_Parts (N))
3203 and then Present (Else_Statements (N))
3204 and then List_Length (Then_Statements (N)) = 1
3205 and then List_Length (Else_Statements (N)) = 1
3206 then
3207 declare
3208 Then_Stm : constant Node_Id := First (Then_Statements (N));
3209 Else_Stm : constant Node_Id := First (Else_Statements (N));
3211 begin
3212 if Nkind (Then_Stm) = N_Simple_Return_Statement
3213 and then
3214 Nkind (Else_Stm) = N_Simple_Return_Statement
3215 then
3216 declare
3217 Then_Expr : constant Node_Id := Expression (Then_Stm);
3218 Else_Expr : constant Node_Id := Expression (Else_Stm);
3220 begin
3221 if Nkind (Then_Expr) = N_Identifier
3222 and then
3223 Nkind (Else_Expr) = N_Identifier
3224 then
3225 if Entity (Then_Expr) = Standard_True
3226 and then Entity (Else_Expr) = Standard_False
3227 then
3228 Rewrite (N,
3229 Make_Simple_Return_Statement (Loc,
3230 Expression => Relocate_Node (Condition (N))));
3231 Analyze (N);
3232 return;
3234 elsif Entity (Then_Expr) = Standard_False
3235 and then Entity (Else_Expr) = Standard_True
3236 then
3237 Rewrite (N,
3238 Make_Simple_Return_Statement (Loc,
3239 Expression =>
3240 Make_Op_Not (Loc,
3241 Right_Opnd =>
3242 Relocate_Node (Condition (N)))));
3243 Analyze (N);
3244 return;
3245 end if;
3246 end if;
3247 end;
3248 end if;
3249 end;
3250 end if;
3251 end if;
3252 end Expand_N_If_Statement;
3254 --------------------------
3255 -- Expand_Iterator_Loop --
3256 --------------------------
3258 procedure Expand_Iterator_Loop (N : Node_Id) is
3259 Isc : constant Node_Id := Iteration_Scheme (N);
3260 I_Spec : constant Node_Id := Iterator_Specification (Isc);
3261 Id : constant Entity_Id := Defining_Identifier (I_Spec);
3262 Loc : constant Source_Ptr := Sloc (N);
3264 Container : constant Node_Id := Name (I_Spec);
3265 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
3266 Cursor : Entity_Id;
3267 Iterator : Entity_Id;
3268 New_Loop : Node_Id;
3269 Stats : List_Id := Statements (N);
3271 begin
3272 -- Processing for arrays
3274 if Is_Array_Type (Container_Typ) then
3275 Expand_Iterator_Loop_Over_Array (N);
3276 return;
3277 end if;
3279 -- Processing for containers
3281 -- For an "of" iterator the name is a container expression, which
3282 -- is transformed into a call to the default iterator.
3284 -- For an iterator of the form "in" the name is a function call
3285 -- that delivers an iterator type.
3287 -- In both cases, analysis of the iterator has introduced an object
3288 -- declaration to capture the domain, so that Container is an entity.
3290 -- The for loop is expanded into a while loop which uses a container
3291 -- specific cursor to desgnate each element.
3293 -- Iter : Iterator_Type := Container.Iterate;
3294 -- Cursor : Cursor_type := First (Iter);
3295 -- while Has_Element (Iter) loop
3296 -- declare
3297 -- -- The block is added when Element_Type is controlled
3299 -- Obj : Pack.Element_Type := Element (Cursor);
3300 -- -- for the "of" loop form
3301 -- begin
3302 -- <original loop statements>
3303 -- end;
3305 -- Cursor := Iter.Next (Cursor);
3306 -- end loop;
3308 -- If "reverse" is present, then the initialization of the cursor
3309 -- uses Last and the step becomes Prev. Pack is the name of the
3310 -- scope where the container package is instantiated.
3312 declare
3313 Element_Type : constant Entity_Id := Etype (Id);
3314 Iter_Type : Entity_Id;
3315 Pack : Entity_Id;
3316 Decl : Node_Id;
3317 Name_Init : Name_Id;
3318 Name_Step : Name_Id;
3320 begin
3321 -- The type of the iterator is the return type of the Iterate
3322 -- function used. For the "of" form this is the default iterator
3323 -- for the type, otherwise it is the type of the explicit
3324 -- function used in the iterator specification. The most common
3325 -- case will be an Iterate function in the container package.
3327 -- The primitive operations of the container type may not be
3328 -- use-visible, so we introduce the name of the enclosing package
3329 -- in the declarations below. The Iterator type is declared in a
3330 -- an instance within the container package itself.
3332 -- If the container type is a derived type, the cursor type is
3333 -- found in the package of the parent type.
3335 if Is_Derived_Type (Container_Typ) then
3336 Pack := Scope (Root_Type (Container_Typ));
3337 else
3338 Pack := Scope (Container_Typ);
3339 end if;
3341 Iter_Type := Etype (Name (I_Spec));
3343 -- The "of" case uses an internally generated cursor whose type
3344 -- is found in the container package. The domain of iteration
3345 -- is expanded into a call to the default Iterator function, but
3346 -- this expansion does not take place in quantified expressions
3347 -- that are analyzed with expansion disabled, and in that case the
3348 -- type of the iterator must be obtained from the aspect.
3350 if Of_Present (I_Spec) then
3351 declare
3352 Default_Iter : constant Entity_Id :=
3353 Entity
3354 (Find_Aspect
3355 (Etype (Container),
3356 Aspect_Default_Iterator));
3358 Container_Arg : Node_Id;
3359 Ent : Entity_Id;
3361 begin
3362 Cursor := Make_Temporary (Loc, 'I');
3364 -- For an container element iterator, the iterator type
3365 -- is obtained from the corresponding aspect, whose return
3366 -- type is descended from the corresponding interface type
3367 -- in some instance of Ada.Iterator_Interfaces. The actuals
3368 -- of that instantiation are Cursor and Has_Element.
3370 Iter_Type := Etype (Default_Iter);
3372 -- The iterator type, which is a class_wide type, may itself
3373 -- be derived locally, so the desired instantiation is the
3374 -- scope of the root type of the iterator type.
3376 Pack := Scope (Root_Type (Etype (Iter_Type)));
3378 -- Rewrite domain of iteration as a call to the default
3379 -- iterator for the container type. If the container is
3380 -- a derived type and the aspect is inherited, convert
3381 -- container to parent type. The Cursor type is also
3382 -- inherited from the scope of the parent.
3384 if Base_Type (Etype (Container)) =
3385 Base_Type (Etype (First_Formal (Default_Iter)))
3386 then
3387 Container_Arg := New_Copy_Tree (Container);
3389 else
3390 Container_Arg :=
3391 Make_Type_Conversion (Loc,
3392 Subtype_Mark =>
3393 New_Occurrence_Of
3394 (Etype (First_Formal (Default_Iter)), Loc),
3395 Expression => New_Copy_Tree (Container));
3396 end if;
3398 Rewrite (Name (I_Spec),
3399 Make_Function_Call (Loc,
3400 Name => New_Occurrence_Of (Default_Iter, Loc),
3401 Parameter_Associations =>
3402 New_List (Container_Arg)));
3403 Analyze_And_Resolve (Name (I_Spec));
3405 -- Find cursor type in proper iterator package, which is an
3406 -- instantiation of Iterator_Interfaces.
3408 Ent := First_Entity (Pack);
3409 while Present (Ent) loop
3410 if Chars (Ent) = Name_Cursor then
3411 Set_Etype (Cursor, Etype (Ent));
3412 exit;
3413 end if;
3414 Next_Entity (Ent);
3415 end loop;
3417 -- Generate:
3418 -- Id : Element_Type renames Container (Cursor);
3419 -- This assumes that the container type has an indexing
3420 -- operation with Cursor. The check that this operation
3421 -- exists is performed in Check_Container_Indexing.
3423 Decl :=
3424 Make_Object_Renaming_Declaration (Loc,
3425 Defining_Identifier => Id,
3426 Subtype_Mark =>
3427 New_Reference_To (Element_Type, Loc),
3428 Name =>
3429 Make_Indexed_Component (Loc,
3430 Prefix => Relocate_Node (Container_Arg),
3431 Expressions =>
3432 New_List (New_Occurrence_Of (Cursor, Loc))));
3434 -- The defining identifier in the iterator is user-visible
3435 -- and must be visible in the debugger.
3437 Set_Debug_Info_Needed (Id);
3439 -- If the container holds controlled objects, wrap the loop
3440 -- statements and element renaming declaration with a block.
3441 -- This ensures that the result of Element (Cusor) is
3442 -- cleaned up after each iteration of the loop.
3444 if Needs_Finalization (Element_Type) then
3446 -- Generate:
3447 -- declare
3448 -- Id : Element_Type := Element (curosr);
3449 -- begin
3450 -- <original loop statements>
3451 -- end;
3453 Stats := New_List (
3454 Make_Block_Statement (Loc,
3455 Declarations => New_List (Decl),
3456 Handled_Statement_Sequence =>
3457 Make_Handled_Sequence_Of_Statements (Loc,
3458 Statements => Stats)));
3460 -- Elements do not need finalization
3462 else
3463 Prepend_To (Stats, Decl);
3464 end if;
3465 end;
3467 -- X in Iterate (S) : type of iterator is type of explicitly
3468 -- given Iterate function, and the loop variable is the cursor.
3469 -- It will be assigned in the loop and must be a variable.
3471 else
3472 Cursor := Id;
3473 Set_Ekind (Cursor, E_Variable);
3474 end if;
3476 Iterator := Make_Temporary (Loc, 'I');
3478 -- Determine the advancement and initialization steps for the
3479 -- cursor.
3481 -- Analysis of the expanded loop will verify that the container
3482 -- has a reverse iterator.
3484 if Reverse_Present (I_Spec) then
3485 Name_Init := Name_Last;
3486 Name_Step := Name_Previous;
3488 else
3489 Name_Init := Name_First;
3490 Name_Step := Name_Next;
3491 end if;
3493 -- For both iterator forms, add a call to the step operation to
3494 -- advance the cursor. Generate:
3496 -- Cursor := Iterator.Next (Cursor);
3498 -- or else
3500 -- Cursor := Next (Cursor);
3502 declare
3503 Rhs : Node_Id;
3505 begin
3506 Rhs :=
3507 Make_Function_Call (Loc,
3508 Name =>
3509 Make_Selected_Component (Loc,
3510 Prefix => New_Reference_To (Iterator, Loc),
3511 Selector_Name => Make_Identifier (Loc, Name_Step)),
3512 Parameter_Associations => New_List (
3513 New_Reference_To (Cursor, Loc)));
3515 Append_To (Stats,
3516 Make_Assignment_Statement (Loc,
3517 Name => New_Occurrence_Of (Cursor, Loc),
3518 Expression => Rhs));
3519 end;
3521 -- Generate:
3522 -- while Iterator.Has_Element loop
3523 -- <Stats>
3524 -- end loop;
3526 -- Has_Element is the second actual in the iterator package
3528 New_Loop :=
3529 Make_Loop_Statement (Loc,
3530 Iteration_Scheme =>
3531 Make_Iteration_Scheme (Loc,
3532 Condition =>
3533 Make_Function_Call (Loc,
3534 Name =>
3535 New_Occurrence_Of (
3536 Next_Entity (First_Entity (Pack)), Loc),
3537 Parameter_Associations =>
3538 New_List (New_Reference_To (Cursor, Loc)))),
3540 Statements => Stats,
3541 End_Label => Empty);
3543 -- If present, preserve identifier of loop, which can be used in
3544 -- an exit statement in the body.
3546 if Present (Identifier (N)) then
3547 Set_Identifier (New_Loop, Relocate_Node (Identifier (N)));
3548 end if;
3550 -- Create the declarations for Iterator and cursor and insert them
3551 -- before the source loop. Given that the domain of iteration is
3552 -- already an entity, the iterator is just a renaming of that
3553 -- entity. Possible optimization ???
3554 -- Generate:
3556 -- I : Iterator_Type renames Container;
3557 -- C : Cursor_Type := Container.[First | Last];
3559 Insert_Action (N,
3560 Make_Object_Renaming_Declaration (Loc,
3561 Defining_Identifier => Iterator,
3562 Subtype_Mark => New_Occurrence_Of (Iter_Type, Loc),
3563 Name => Relocate_Node (Name (I_Spec))));
3565 -- Create declaration for cursor
3567 declare
3568 Decl : Node_Id;
3570 begin
3571 Decl :=
3572 Make_Object_Declaration (Loc,
3573 Defining_Identifier => Cursor,
3574 Object_Definition =>
3575 New_Occurrence_Of (Etype (Cursor), Loc),
3576 Expression =>
3577 Make_Selected_Component (Loc,
3578 Prefix => New_Reference_To (Iterator, Loc),
3579 Selector_Name =>
3580 Make_Identifier (Loc, Name_Init)));
3582 -- The cursor is only modified in expanded code, so it appears
3583 -- as unassigned to the warning machinery. We must suppress
3584 -- this spurious warning explicitly.
3586 Set_Warnings_Off (Cursor);
3587 Set_Assignment_OK (Decl);
3589 Insert_Action (N, Decl);
3590 end;
3592 -- If the range of iteration is given by a function call that
3593 -- returns a container, the finalization actions have been saved
3594 -- in the Condition_Actions of the iterator. Insert them now at
3595 -- the head of the loop.
3597 if Present (Condition_Actions (Isc)) then
3598 Insert_List_Before (N, Condition_Actions (Isc));
3599 end if;
3600 end;
3602 Rewrite (N, New_Loop);
3603 Analyze (N);
3604 end Expand_Iterator_Loop;
3606 -------------------------------------
3607 -- Expand_Iterator_Loop_Over_Array --
3608 -------------------------------------
3610 procedure Expand_Iterator_Loop_Over_Array (N : Node_Id) is
3611 Isc : constant Node_Id := Iteration_Scheme (N);
3612 I_Spec : constant Node_Id := Iterator_Specification (Isc);
3613 Array_Node : constant Node_Id := Name (I_Spec);
3614 Array_Typ : constant Entity_Id := Base_Type (Etype (Array_Node));
3615 Array_Dim : constant Pos := Number_Dimensions (Array_Typ);
3616 Id : constant Entity_Id := Defining_Identifier (I_Spec);
3617 Loc : constant Source_Ptr := Sloc (N);
3618 Stats : constant List_Id := Statements (N);
3619 Core_Loop : Node_Id;
3620 Ind_Comp : Node_Id;
3621 Iterator : Entity_Id;
3623 -- Start of processing for Expand_Iterator_Loop_Over_Array
3625 begin
3626 -- for Element of Array loop
3628 -- This case requires an internally generated cursor to iterate over
3629 -- the array.
3631 if Of_Present (I_Spec) then
3632 Iterator := Make_Temporary (Loc, 'C');
3634 -- Generate:
3635 -- Element : Component_Type renames Array (Iterator);
3637 Ind_Comp :=
3638 Make_Indexed_Component (Loc,
3639 Prefix => Relocate_Node (Array_Node),
3640 Expressions => New_List (New_Reference_To (Iterator, Loc)));
3642 Prepend_To (Stats,
3643 Make_Object_Renaming_Declaration (Loc,
3644 Defining_Identifier => Id,
3645 Subtype_Mark =>
3646 New_Reference_To (Component_Type (Array_Typ), Loc),
3647 Name => Ind_Comp));
3649 -- Mark the loop variable as needing debug info, so that expansion
3650 -- of the renaming will result in Materialize_Entity getting set via
3651 -- Debug_Renaming_Declaration. (This setting is needed here because
3652 -- the setting in Freeze_Entity comes after the expansion, which is
3653 -- too late. ???)
3655 Set_Debug_Info_Needed (Id);
3657 -- for Index in Array loop
3659 -- This case utilizes the already given iterator name
3661 else
3662 Iterator := Id;
3663 end if;
3665 -- Generate:
3667 -- for Iterator in [reverse] Array'Range (Array_Dim) loop
3668 -- Element : Component_Type renames Array (Iterator);
3669 -- <original loop statements>
3670 -- end loop;
3672 Core_Loop :=
3673 Make_Loop_Statement (Loc,
3674 Iteration_Scheme =>
3675 Make_Iteration_Scheme (Loc,
3676 Loop_Parameter_Specification =>
3677 Make_Loop_Parameter_Specification (Loc,
3678 Defining_Identifier => Iterator,
3679 Discrete_Subtype_Definition =>
3680 Make_Attribute_Reference (Loc,
3681 Prefix => Relocate_Node (Array_Node),
3682 Attribute_Name => Name_Range,
3683 Expressions => New_List (
3684 Make_Integer_Literal (Loc, Array_Dim))),
3685 Reverse_Present => Reverse_Present (I_Spec))),
3686 Statements => Stats,
3687 End_Label => Empty);
3689 -- Processing for multidimensional array
3691 if Array_Dim > 1 then
3692 for Dim in 1 .. Array_Dim - 1 loop
3693 Iterator := Make_Temporary (Loc, 'C');
3695 -- Generate the dimension loops starting from the innermost one
3697 -- for Iterator in [reverse] Array'Range (Array_Dim - Dim) loop
3698 -- <core loop>
3699 -- end loop;
3701 Core_Loop :=
3702 Make_Loop_Statement (Loc,
3703 Iteration_Scheme =>
3704 Make_Iteration_Scheme (Loc,
3705 Loop_Parameter_Specification =>
3706 Make_Loop_Parameter_Specification (Loc,
3707 Defining_Identifier => Iterator,
3708 Discrete_Subtype_Definition =>
3709 Make_Attribute_Reference (Loc,
3710 Prefix => Relocate_Node (Array_Node),
3711 Attribute_Name => Name_Range,
3712 Expressions => New_List (
3713 Make_Integer_Literal (Loc, Array_Dim - Dim))),
3714 Reverse_Present => Reverse_Present (I_Spec))),
3715 Statements => New_List (Core_Loop),
3716 End_Label => Empty);
3718 -- Update the previously created object renaming declaration with
3719 -- the new iterator.
3721 Prepend_To (Expressions (Ind_Comp),
3722 New_Reference_To (Iterator, Loc));
3723 end loop;
3724 end if;
3726 -- If original loop has a name, preserve it so it can be recognized by
3727 -- an exit statement in the body of the rewritten loop.
3729 if Present (Identifier (N)) then
3730 Set_Identifier (Core_Loop, Relocate_Node (Identifier (N)));
3731 end if;
3733 Rewrite (N, Core_Loop);
3734 Analyze (N);
3735 end Expand_Iterator_Loop_Over_Array;
3737 -----------------------------
3738 -- Expand_N_Loop_Statement --
3739 -----------------------------
3741 -- 1. Remove null loop entirely
3742 -- 2. Deal with while condition for C/Fortran boolean
3743 -- 3. Deal with loops with a non-standard enumeration type range
3744 -- 4. Deal with while loops where Condition_Actions is set
3745 -- 5. Deal with loops over predicated subtypes
3746 -- 6. Deal with loops with iterators over arrays and containers
3747 -- 7. Insert polling call if required
3749 procedure Expand_N_Loop_Statement (N : Node_Id) is
3750 Loc : constant Source_Ptr := Sloc (N);
3751 Isc : constant Node_Id := Iteration_Scheme (N);
3753 begin
3754 -- Delete null loop
3756 if Is_Null_Loop (N) then
3757 Rewrite (N, Make_Null_Statement (Loc));
3758 return;
3759 end if;
3761 Process_Statements_For_Controlled_Objects (N);
3763 -- Deal with condition for C/Fortran Boolean
3765 if Present (Isc) then
3766 Adjust_Condition (Condition (Isc));
3767 end if;
3769 -- Generate polling call
3771 if Is_Non_Empty_List (Statements (N)) then
3772 Generate_Poll_Call (First (Statements (N)));
3773 end if;
3775 -- Nothing more to do for plain loop with no iteration scheme
3777 if No (Isc) then
3778 null;
3780 -- Case of for loop (Loop_Parameter_Specification present)
3782 -- Note: we do not have to worry about validity checking of the for loop
3783 -- range bounds here, since they were frozen with constant declarations
3784 -- and it is during that process that the validity checking is done.
3786 elsif Present (Loop_Parameter_Specification (Isc)) then
3787 declare
3788 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
3789 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
3790 Ltype : constant Entity_Id := Etype (Loop_Id);
3791 Btype : constant Entity_Id := Base_Type (Ltype);
3792 Expr : Node_Id;
3793 New_Id : Entity_Id;
3795 begin
3796 -- Deal with loop over predicates
3798 if Is_Discrete_Type (Ltype)
3799 and then Present (Predicate_Function (Ltype))
3800 then
3801 Expand_Predicated_Loop (N);
3803 -- Handle the case where we have a for loop with the range type
3804 -- being an enumeration type with non-standard representation.
3805 -- In this case we expand:
3807 -- for x in [reverse] a .. b loop
3808 -- ...
3809 -- end loop;
3811 -- to
3813 -- for xP in [reverse] integer
3814 -- range etype'Pos (a) .. etype'Pos (b)
3815 -- loop
3816 -- declare
3817 -- x : constant etype := Pos_To_Rep (xP);
3818 -- begin
3819 -- ...
3820 -- end;
3821 -- end loop;
3823 elsif Is_Enumeration_Type (Btype)
3824 and then Present (Enum_Pos_To_Rep (Btype))
3825 then
3826 New_Id :=
3827 Make_Defining_Identifier (Loc,
3828 Chars => New_External_Name (Chars (Loop_Id), 'P'));
3830 -- If the type has a contiguous representation, successive
3831 -- values can be generated as offsets from the first literal.
3833 if Has_Contiguous_Rep (Btype) then
3834 Expr :=
3835 Unchecked_Convert_To (Btype,
3836 Make_Op_Add (Loc,
3837 Left_Opnd =>
3838 Make_Integer_Literal (Loc,
3839 Enumeration_Rep (First_Literal (Btype))),
3840 Right_Opnd => New_Reference_To (New_Id, Loc)));
3841 else
3842 -- Use the constructed array Enum_Pos_To_Rep
3844 Expr :=
3845 Make_Indexed_Component (Loc,
3846 Prefix =>
3847 New_Reference_To (Enum_Pos_To_Rep (Btype), Loc),
3848 Expressions =>
3849 New_List (New_Reference_To (New_Id, Loc)));
3850 end if;
3852 Rewrite (N,
3853 Make_Loop_Statement (Loc,
3854 Identifier => Identifier (N),
3856 Iteration_Scheme =>
3857 Make_Iteration_Scheme (Loc,
3858 Loop_Parameter_Specification =>
3859 Make_Loop_Parameter_Specification (Loc,
3860 Defining_Identifier => New_Id,
3861 Reverse_Present => Reverse_Present (LPS),
3863 Discrete_Subtype_Definition =>
3864 Make_Subtype_Indication (Loc,
3866 Subtype_Mark =>
3867 New_Reference_To (Standard_Natural, Loc),
3869 Constraint =>
3870 Make_Range_Constraint (Loc,
3871 Range_Expression =>
3872 Make_Range (Loc,
3874 Low_Bound =>
3875 Make_Attribute_Reference (Loc,
3876 Prefix =>
3877 New_Reference_To (Btype, Loc),
3879 Attribute_Name => Name_Pos,
3881 Expressions => New_List (
3882 Relocate_Node
3883 (Type_Low_Bound (Ltype)))),
3885 High_Bound =>
3886 Make_Attribute_Reference (Loc,
3887 Prefix =>
3888 New_Reference_To (Btype, Loc),
3890 Attribute_Name => Name_Pos,
3892 Expressions => New_List (
3893 Relocate_Node
3894 (Type_High_Bound
3895 (Ltype))))))))),
3897 Statements => New_List (
3898 Make_Block_Statement (Loc,
3899 Declarations => New_List (
3900 Make_Object_Declaration (Loc,
3901 Defining_Identifier => Loop_Id,
3902 Constant_Present => True,
3903 Object_Definition =>
3904 New_Reference_To (Ltype, Loc),
3905 Expression => Expr)),
3907 Handled_Statement_Sequence =>
3908 Make_Handled_Sequence_Of_Statements (Loc,
3909 Statements => Statements (N)))),
3911 End_Label => End_Label (N)));
3913 -- The loop parameter's entity must be removed from the loop
3914 -- scope's entity list, since it will now be located in the
3915 -- new block scope. Any other entities already associated with
3916 -- the loop scope, such as the loop parameter's subtype, will
3917 -- remain there.
3919 pragma Assert (First_Entity (Scope (Loop_Id)) = Loop_Id);
3920 Set_First_Entity (Scope (Loop_Id), Next_Entity (Loop_Id));
3922 if Last_Entity (Scope (Loop_Id)) = Loop_Id then
3923 Set_Last_Entity (Scope (Loop_Id), Empty);
3924 end if;
3926 Analyze (N);
3928 -- Nothing to do with other cases of for loops
3930 else
3931 null;
3932 end if;
3933 end;
3935 -- Second case, if we have a while loop with Condition_Actions set, then
3936 -- we change it into a plain loop:
3938 -- while C loop
3939 -- ...
3940 -- end loop;
3942 -- changed to:
3944 -- loop
3945 -- <<condition actions>>
3946 -- exit when not C;
3947 -- ...
3948 -- end loop
3950 elsif Present (Isc)
3951 and then Present (Condition_Actions (Isc))
3952 and then Present (Condition (Isc))
3953 then
3954 declare
3955 ES : Node_Id;
3957 begin
3958 ES :=
3959 Make_Exit_Statement (Sloc (Condition (Isc)),
3960 Condition =>
3961 Make_Op_Not (Sloc (Condition (Isc)),
3962 Right_Opnd => Condition (Isc)));
3964 Prepend (ES, Statements (N));
3965 Insert_List_Before (ES, Condition_Actions (Isc));
3967 -- This is not an implicit loop, since it is generated in response
3968 -- to the loop statement being processed. If this is itself
3969 -- implicit, the restriction has already been checked. If not,
3970 -- it is an explicit loop.
3972 Rewrite (N,
3973 Make_Loop_Statement (Sloc (N),
3974 Identifier => Identifier (N),
3975 Statements => Statements (N),
3976 End_Label => End_Label (N)));
3978 Analyze (N);
3979 end;
3981 -- Here to deal with iterator case
3983 elsif Present (Isc)
3984 and then Present (Iterator_Specification (Isc))
3985 then
3986 Expand_Iterator_Loop (N);
3987 end if;
3989 -- If the loop is subject to at least one Loop_Entry attribute, it
3990 -- requires additional processing.
3992 if Nkind (N) = N_Loop_Statement then
3993 Expand_Loop_Entry_Attributes (N);
3994 end if;
3995 end Expand_N_Loop_Statement;
3997 ----------------------------
3998 -- Expand_Predicated_Loop --
3999 ----------------------------
4001 -- Note: the expander can handle generation of loops over predicated
4002 -- subtypes for both the dynamic and static cases. Depending on what
4003 -- we decide is allowed in Ada 2012 mode and/or extensions allowed
4004 -- mode, the semantic analyzer may disallow one or both forms.
4006 procedure Expand_Predicated_Loop (N : Node_Id) is
4007 Loc : constant Source_Ptr := Sloc (N);
4008 Isc : constant Node_Id := Iteration_Scheme (N);
4009 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
4010 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
4011 Ltype : constant Entity_Id := Etype (Loop_Id);
4012 Stat : constant List_Id := Static_Predicate (Ltype);
4013 Stmts : constant List_Id := Statements (N);
4015 begin
4016 -- Case of iteration over non-static predicate, should not be possible
4017 -- since this is not allowed by the semantics and should have been
4018 -- caught during analysis of the loop statement.
4020 if No (Stat) then
4021 raise Program_Error;
4023 -- If the predicate list is empty, that corresponds to a predicate of
4024 -- False, in which case the loop won't run at all, and we rewrite the
4025 -- entire loop as a null statement.
4027 elsif Is_Empty_List (Stat) then
4028 Rewrite (N, Make_Null_Statement (Loc));
4029 Analyze (N);
4031 -- For expansion over a static predicate we generate the following
4033 -- declare
4034 -- J : Ltype := min-val;
4035 -- begin
4036 -- loop
4037 -- body
4038 -- case J is
4039 -- when endpoint => J := startpoint;
4040 -- when endpoint => J := startpoint;
4041 -- ...
4042 -- when max-val => exit;
4043 -- when others => J := Lval'Succ (J);
4044 -- end case;
4045 -- end loop;
4046 -- end;
4048 -- To make this a little clearer, let's take a specific example:
4050 -- type Int is range 1 .. 10;
4051 -- subtype L is Int with
4052 -- predicate => L in 3 | 10 | 5 .. 7;
4053 -- ...
4054 -- for L in StaticP loop
4055 -- Put_Line ("static:" & J'Img);
4056 -- end loop;
4058 -- In this case, the loop is transformed into
4060 -- begin
4061 -- J : L := 3;
4062 -- loop
4063 -- body
4064 -- case J is
4065 -- when 3 => J := 5;
4066 -- when 7 => J := 10;
4067 -- when 10 => exit;
4068 -- when others => J := L'Succ (J);
4069 -- end case;
4070 -- end loop;
4071 -- end;
4073 else
4074 Static_Predicate : declare
4075 S : Node_Id;
4076 D : Node_Id;
4077 P : Node_Id;
4078 Alts : List_Id;
4079 Cstm : Node_Id;
4081 function Lo_Val (N : Node_Id) return Node_Id;
4082 -- Given static expression or static range, returns an identifier
4083 -- whose value is the low bound of the expression value or range.
4085 function Hi_Val (N : Node_Id) return Node_Id;
4086 -- Given static expression or static range, returns an identifier
4087 -- whose value is the high bound of the expression value or range.
4089 ------------
4090 -- Hi_Val --
4091 ------------
4093 function Hi_Val (N : Node_Id) return Node_Id is
4094 begin
4095 if Is_Static_Expression (N) then
4096 return New_Copy (N);
4097 else
4098 pragma Assert (Nkind (N) = N_Range);
4099 return New_Copy (High_Bound (N));
4100 end if;
4101 end Hi_Val;
4103 ------------
4104 -- Lo_Val --
4105 ------------
4107 function Lo_Val (N : Node_Id) return Node_Id is
4108 begin
4109 if Is_Static_Expression (N) then
4110 return New_Copy (N);
4111 else
4112 pragma Assert (Nkind (N) = N_Range);
4113 return New_Copy (Low_Bound (N));
4114 end if;
4115 end Lo_Val;
4117 -- Start of processing for Static_Predicate
4119 begin
4120 -- Convert loop identifier to normal variable and reanalyze it so
4121 -- that this conversion works. We have to use the same defining
4122 -- identifier, since there may be references in the loop body.
4124 Set_Analyzed (Loop_Id, False);
4125 Set_Ekind (Loop_Id, E_Variable);
4127 -- In most loops the loop variable is assigned in various
4128 -- alternatives in the body. However, in the rare case when
4129 -- the range specifies a single element, the loop variable
4130 -- may trigger a spurious warning that is could be constant.
4131 -- This warning might as well be suppressed.
4133 Set_Warnings_Off (Loop_Id);
4135 -- Loop to create branches of case statement
4137 Alts := New_List;
4138 P := First (Stat);
4139 while Present (P) loop
4140 if No (Next (P)) then
4141 S := Make_Exit_Statement (Loc);
4142 else
4143 S :=
4144 Make_Assignment_Statement (Loc,
4145 Name => New_Occurrence_Of (Loop_Id, Loc),
4146 Expression => Lo_Val (Next (P)));
4147 Set_Suppress_Assignment_Checks (S);
4148 end if;
4150 Append_To (Alts,
4151 Make_Case_Statement_Alternative (Loc,
4152 Statements => New_List (S),
4153 Discrete_Choices => New_List (Hi_Val (P))));
4155 Next (P);
4156 end loop;
4158 -- Add others choice
4160 S :=
4161 Make_Assignment_Statement (Loc,
4162 Name => New_Occurrence_Of (Loop_Id, Loc),
4163 Expression =>
4164 Make_Attribute_Reference (Loc,
4165 Prefix => New_Occurrence_Of (Ltype, Loc),
4166 Attribute_Name => Name_Succ,
4167 Expressions => New_List (
4168 New_Occurrence_Of (Loop_Id, Loc))));
4169 Set_Suppress_Assignment_Checks (S);
4171 Append_To (Alts,
4172 Make_Case_Statement_Alternative (Loc,
4173 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
4174 Statements => New_List (S)));
4176 -- Construct case statement and append to body statements
4178 Cstm :=
4179 Make_Case_Statement (Loc,
4180 Expression => New_Occurrence_Of (Loop_Id, Loc),
4181 Alternatives => Alts);
4182 Append_To (Stmts, Cstm);
4184 -- Rewrite the loop
4186 D :=
4187 Make_Object_Declaration (Loc,
4188 Defining_Identifier => Loop_Id,
4189 Object_Definition => New_Occurrence_Of (Ltype, Loc),
4190 Expression => Lo_Val (First (Stat)));
4191 Set_Suppress_Assignment_Checks (D);
4193 Rewrite (N,
4194 Make_Block_Statement (Loc,
4195 Declarations => New_List (D),
4196 Handled_Statement_Sequence =>
4197 Make_Handled_Sequence_Of_Statements (Loc,
4198 Statements => New_List (
4199 Make_Loop_Statement (Loc,
4200 Statements => Stmts,
4201 End_Label => Empty)))));
4203 Analyze (N);
4204 end Static_Predicate;
4205 end if;
4206 end Expand_Predicated_Loop;
4208 ------------------------------
4209 -- Make_Tag_Ctrl_Assignment --
4210 ------------------------------
4212 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id is
4213 Asn : constant Node_Id := Relocate_Node (N);
4214 L : constant Node_Id := Name (N);
4215 Loc : constant Source_Ptr := Sloc (N);
4216 Res : constant List_Id := New_List;
4217 T : constant Entity_Id := Underlying_Type (Etype (L));
4219 Comp_Asn : constant Boolean := Is_Fully_Repped_Tagged_Type (T);
4220 Ctrl_Act : constant Boolean := Needs_Finalization (T)
4221 and then not No_Ctrl_Actions (N);
4222 Save_Tag : constant Boolean := Is_Tagged_Type (T)
4223 and then not Comp_Asn
4224 and then not No_Ctrl_Actions (N)
4225 and then Tagged_Type_Expansion;
4226 -- Tags are not saved and restored when VM_Target because VM tags are
4227 -- represented implicitly in objects.
4229 Next_Id : Entity_Id;
4230 Prev_Id : Entity_Id;
4231 Tag_Id : Entity_Id;
4233 begin
4234 -- Finalize the target of the assignment when controlled
4236 -- We have two exceptions here:
4238 -- 1. If we are in an init proc since it is an initialization more
4239 -- than an assignment.
4241 -- 2. If the left-hand side is a temporary that was not initialized
4242 -- (or the parent part of a temporary since it is the case in
4243 -- extension aggregates). Such a temporary does not come from
4244 -- source. We must examine the original node for the prefix, because
4245 -- it may be a component of an entry formal, in which case it has
4246 -- been rewritten and does not appear to come from source either.
4248 -- Case of init proc
4250 if not Ctrl_Act then
4251 null;
4253 -- The left hand side is an uninitialized temporary object
4255 elsif Nkind (L) = N_Type_Conversion
4256 and then Is_Entity_Name (Expression (L))
4257 and then Nkind (Parent (Entity (Expression (L)))) =
4258 N_Object_Declaration
4259 and then No_Initialization (Parent (Entity (Expression (L))))
4260 then
4261 null;
4263 else
4264 Append_To (Res,
4265 Make_Final_Call
4266 (Obj_Ref => Duplicate_Subexpr_No_Checks (L),
4267 Typ => Etype (L)));
4268 end if;
4270 -- Save the Tag in a local variable Tag_Id
4272 if Save_Tag then
4273 Tag_Id := Make_Temporary (Loc, 'A');
4275 Append_To (Res,
4276 Make_Object_Declaration (Loc,
4277 Defining_Identifier => Tag_Id,
4278 Object_Definition => New_Reference_To (RTE (RE_Tag), Loc),
4279 Expression =>
4280 Make_Selected_Component (Loc,
4281 Prefix => Duplicate_Subexpr_No_Checks (L),
4282 Selector_Name =>
4283 New_Reference_To (First_Tag_Component (T), Loc))));
4285 -- Otherwise Tag_Id is not used
4287 else
4288 Tag_Id := Empty;
4289 end if;
4291 -- Save the Prev and Next fields on .NET/JVM. This is not needed on non
4292 -- VM targets since the fields are not part of the object.
4294 if VM_Target /= No_VM
4295 and then Is_Controlled (T)
4296 then
4297 Prev_Id := Make_Temporary (Loc, 'P');
4298 Next_Id := Make_Temporary (Loc, 'N');
4300 -- Generate:
4301 -- Pnn : Root_Controlled_Ptr := Root_Controlled (L).Prev;
4303 Append_To (Res,
4304 Make_Object_Declaration (Loc,
4305 Defining_Identifier => Prev_Id,
4306 Object_Definition =>
4307 New_Reference_To (RTE (RE_Root_Controlled_Ptr), Loc),
4308 Expression =>
4309 Make_Selected_Component (Loc,
4310 Prefix =>
4311 Unchecked_Convert_To
4312 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
4313 Selector_Name =>
4314 Make_Identifier (Loc, Name_Prev))));
4316 -- Generate:
4317 -- Nnn : Root_Controlled_Ptr := Root_Controlled (L).Next;
4319 Append_To (Res,
4320 Make_Object_Declaration (Loc,
4321 Defining_Identifier => Next_Id,
4322 Object_Definition =>
4323 New_Reference_To (RTE (RE_Root_Controlled_Ptr), Loc),
4324 Expression =>
4325 Make_Selected_Component (Loc,
4326 Prefix =>
4327 Unchecked_Convert_To
4328 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
4329 Selector_Name =>
4330 Make_Identifier (Loc, Name_Next))));
4331 end if;
4333 -- If the tagged type has a full rep clause, expand the assignment into
4334 -- component-wise assignments. Mark the node as unanalyzed in order to
4335 -- generate the proper code and propagate this scenario by setting a
4336 -- flag to avoid infinite recursion.
4338 if Comp_Asn then
4339 Set_Analyzed (Asn, False);
4340 Set_Componentwise_Assignment (Asn, True);
4341 end if;
4343 Append_To (Res, Asn);
4345 -- Restore the tag
4347 if Save_Tag then
4348 Append_To (Res,
4349 Make_Assignment_Statement (Loc,
4350 Name =>
4351 Make_Selected_Component (Loc,
4352 Prefix => Duplicate_Subexpr_No_Checks (L),
4353 Selector_Name =>
4354 New_Reference_To (First_Tag_Component (T), Loc)),
4355 Expression => New_Reference_To (Tag_Id, Loc)));
4356 end if;
4358 -- Restore the Prev and Next fields on .NET/JVM
4360 if VM_Target /= No_VM
4361 and then Is_Controlled (T)
4362 then
4363 -- Generate:
4364 -- Root_Controlled (L).Prev := Prev_Id;
4366 Append_To (Res,
4367 Make_Assignment_Statement (Loc,
4368 Name =>
4369 Make_Selected_Component (Loc,
4370 Prefix =>
4371 Unchecked_Convert_To
4372 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
4373 Selector_Name =>
4374 Make_Identifier (Loc, Name_Prev)),
4375 Expression => New_Reference_To (Prev_Id, Loc)));
4377 -- Generate:
4378 -- Root_Controlled (L).Next := Next_Id;
4380 Append_To (Res,
4381 Make_Assignment_Statement (Loc,
4382 Name =>
4383 Make_Selected_Component (Loc,
4384 Prefix =>
4385 Unchecked_Convert_To
4386 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
4387 Selector_Name => Make_Identifier (Loc, Name_Next)),
4388 Expression => New_Reference_To (Next_Id, Loc)));
4389 end if;
4391 -- Adjust the target after the assignment when controlled (not in the
4392 -- init proc since it is an initialization more than an assignment).
4394 if Ctrl_Act then
4395 Append_To (Res,
4396 Make_Adjust_Call
4397 (Obj_Ref => Duplicate_Subexpr_Move_Checks (L),
4398 Typ => Etype (L)));
4399 end if;
4401 return Res;
4403 exception
4405 -- Could use comment here ???
4407 when RE_Not_Available =>
4408 return Empty_List;
4409 end Make_Tag_Ctrl_Assignment;
4411 end Exp_Ch5;