1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with 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 Exp_Aggr; use Exp_Aggr;
32 with Exp_Ch6; use Exp_Ch6;
33 with Exp_Ch7; use Exp_Ch7;
34 with Exp_Ch11; use Exp_Ch11;
35 with Exp_Dbug; use Exp_Dbug;
36 with Exp_Pakd; use Exp_Pakd;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Rtsfind; use Rtsfind;
46 with Sinfo; use Sinfo;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch13; use Sem_Ch13;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Util; use Sem_Util;
55 with Snames; use Snames;
56 with Stand; use Stand;
57 with Stringt; use Stringt;
58 with Targparm; use Targparm;
59 with Tbuild; use Tbuild;
60 with Validsw; use Validsw;
62 package body Exp_Ch5 is
64 function Change_Of_Representation (N : Node_Id) return Boolean;
65 -- Determine if the right hand side of assignment N is a type conversion
66 -- which requires a change of representation. Called only for the array
69 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id);
70 -- N is an assignment which assigns an array value. This routine process
71 -- the various special cases and checks required for such assignments,
72 -- including change of representation. Rhs is normally simply the right
73 -- hand side of the assignment, except that if the right hand side is a
74 -- type conversion or a qualified expression, then the RHS is the actual
75 -- expression inside any such type conversions or qualifications.
77 function Expand_Assign_Array_Loop
84 Rev : Boolean) return Node_Id;
85 -- N is an assignment statement which assigns an array value. This routine
86 -- expands the assignment into a loop (or nested loops for the case of a
87 -- multi-dimensional array) to do the assignment component by component.
88 -- Larray and Rarray are the entities of the actual arrays on the left
89 -- hand and right hand sides. L_Type and R_Type are the types of these
90 -- arrays (which may not be the same, due to either sliding, or to a
91 -- change of representation case). Ndim is the number of dimensions and
92 -- the parameter Rev indicates if the loops run normally (Rev = False),
93 -- or reversed (Rev = True). The value returned is the constructed
94 -- loop statement. Auxiliary declarations are inserted before node N
95 -- using the standard Insert_Actions mechanism.
97 procedure Expand_Assign_Record (N : Node_Id);
98 -- N is an assignment of a non-tagged record value. This routine handles
99 -- the case where the assignment must be made component by component,
100 -- either because the target is not byte aligned, or there is a change
101 -- of representation, or when we have a tagged type with a representation
102 -- clause (this last case is required because holes in the tagged type
103 -- might be filled with components from child types).
105 procedure Expand_Iterator_Loop (N : Node_Id);
106 -- Expand loop over arrays and containers that uses the form "for X of C"
107 -- with an optional subtype mark, or "for Y in C".
109 procedure Expand_Predicated_Loop (N : Node_Id);
110 -- Expand for loop over predicated subtype
112 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id;
113 -- Generate the necessary code for controlled and tagged assignment, that
114 -- is to say, finalization of the target before, adjustment of the target
115 -- after and save and restore of the tag and finalization pointers which
116 -- are not 'part of the value' and must not be changed upon assignment. N
117 -- is the original Assignment node.
119 ------------------------------
120 -- Change_Of_Representation --
121 ------------------------------
123 function Change_Of_Representation (N : Node_Id) return Boolean is
124 Rhs : constant Node_Id := Expression (N);
127 Nkind (Rhs) = N_Type_Conversion
129 not Same_Representation (Etype (Rhs), Etype (Expression (Rhs)));
130 end Change_Of_Representation;
132 -------------------------
133 -- Expand_Assign_Array --
134 -------------------------
136 -- There are two issues here. First, do we let Gigi do a block move, or
137 -- do we expand out into a loop? Second, we need to set the two flags
138 -- Forwards_OK and Backwards_OK which show whether the block move (or
139 -- corresponding loops) can be legitimately done in a forwards (low to
140 -- high) or backwards (high to low) manner.
142 procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id) is
143 Loc : constant Source_Ptr := Sloc (N);
145 Lhs : constant Node_Id := Name (N);
147 Act_Lhs : constant Node_Id := Get_Referenced_Object (Lhs);
148 Act_Rhs : Node_Id := Get_Referenced_Object (Rhs);
150 L_Type : constant Entity_Id :=
151 Underlying_Type (Get_Actual_Subtype (Act_Lhs));
152 R_Type : Entity_Id :=
153 Underlying_Type (Get_Actual_Subtype (Act_Rhs));
155 L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice;
156 R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice;
158 Crep : constant Boolean := Change_Of_Representation (N);
163 Ndim : constant Pos := Number_Dimensions (L_Type);
165 Loop_Required : Boolean := False;
166 -- This switch is set to True if the array move must be done using
167 -- an explicit front end generated loop.
169 procedure Apply_Dereference (Arg : Node_Id);
170 -- If the argument is an access to an array, and the assignment is
171 -- converted into a procedure call, apply explicit dereference.
173 function Has_Address_Clause (Exp : Node_Id) return Boolean;
174 -- Test if Exp is a reference to an array whose declaration has
175 -- an address clause, or it is a slice of such an array.
177 function Is_Formal_Array (Exp : Node_Id) return Boolean;
178 -- Test if Exp is a reference to an array which is either a formal
179 -- parameter or a slice of a formal parameter. These are the cases
180 -- where hidden aliasing can occur.
182 function Is_Non_Local_Array (Exp : Node_Id) return Boolean;
183 -- Determine if Exp is a reference to an array variable which is other
184 -- than an object defined in the current scope, or a slice of such
185 -- an object. Such objects can be aliased to parameters (unlike local
186 -- array references).
188 -----------------------
189 -- Apply_Dereference --
190 -----------------------
192 procedure Apply_Dereference (Arg : Node_Id) is
193 Typ : constant Entity_Id := Etype (Arg);
195 if Is_Access_Type (Typ) then
196 Rewrite (Arg, Make_Explicit_Dereference (Loc,
197 Prefix => Relocate_Node (Arg)));
198 Analyze_And_Resolve (Arg, Designated_Type (Typ));
200 end Apply_Dereference;
202 ------------------------
203 -- Has_Address_Clause --
204 ------------------------
206 function Has_Address_Clause (Exp : Node_Id) return Boolean is
209 (Is_Entity_Name (Exp) and then
210 Present (Address_Clause (Entity (Exp))))
212 (Nkind (Exp) = N_Slice and then Has_Address_Clause (Prefix (Exp)));
213 end Has_Address_Clause;
215 ---------------------
216 -- Is_Formal_Array --
217 ---------------------
219 function Is_Formal_Array (Exp : Node_Id) return Boolean is
222 (Is_Entity_Name (Exp) and then Is_Formal (Entity (Exp)))
224 (Nkind (Exp) = N_Slice and then Is_Formal_Array (Prefix (Exp)));
227 ------------------------
228 -- Is_Non_Local_Array --
229 ------------------------
231 function Is_Non_Local_Array (Exp : Node_Id) return Boolean is
233 return (Is_Entity_Name (Exp)
234 and then Scope (Entity (Exp)) /= Current_Scope)
235 or else (Nkind (Exp) = N_Slice
236 and then Is_Non_Local_Array (Prefix (Exp)));
237 end Is_Non_Local_Array;
239 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
241 Lhs_Formal : constant Boolean := Is_Formal_Array (Act_Lhs);
242 Rhs_Formal : constant Boolean := Is_Formal_Array (Act_Rhs);
244 Lhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Lhs);
245 Rhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Rhs);
247 -- Start of processing for Expand_Assign_Array
250 -- Deal with length check. Note that the length check is done with
251 -- respect to the right hand side as given, not a possible underlying
252 -- renamed object, since this would generate incorrect extra checks.
254 Apply_Length_Check (Rhs, L_Type);
256 -- We start by assuming that the move can be done in either direction,
257 -- i.e. that the two sides are completely disjoint.
259 Set_Forwards_OK (N, True);
260 Set_Backwards_OK (N, True);
262 -- Normally it is only the slice case that can lead to overlap, and
263 -- explicit checks for slices are made below. But there is one case
264 -- where the slice can be implicit and invisible to us: when we have a
265 -- one dimensional array, and either both operands are parameters, or
266 -- one is a parameter (which can be a slice passed by reference) and the
267 -- other is a non-local variable. In this case the parameter could be a
268 -- slice that overlaps with the other operand.
270 -- However, if the array subtype is a constrained first subtype in the
271 -- parameter case, then we don't have to worry about overlap, since
272 -- slice assignments aren't possible (other than for a slice denoting
275 -- Note: No overlap is possible if there is a change of representation,
276 -- so we can exclude this case.
281 ((Lhs_Formal and Rhs_Formal)
283 (Lhs_Formal and Rhs_Non_Local_Var)
285 (Rhs_Formal and Lhs_Non_Local_Var))
287 (not Is_Constrained (Etype (Lhs))
288 or else not Is_First_Subtype (Etype (Lhs)))
290 -- In the case of compiling for the Java or .NET Virtual Machine,
291 -- slices are always passed by making a copy, so we don't have to
292 -- worry about overlap. We also want to prevent generation of "<"
293 -- comparisons for array addresses, since that's a meaningless
294 -- operation on the VM.
296 and then VM_Target = No_VM
298 Set_Forwards_OK (N, False);
299 Set_Backwards_OK (N, False);
301 -- Note: the bit-packed case is not worrisome here, since if we have
302 -- a slice passed as a parameter, it is always aligned on a byte
303 -- boundary, and if there are no explicit slices, the assignment
304 -- can be performed directly.
307 -- If either operand has an address clause clear Backwards_OK and
308 -- Forwards_OK, since we cannot tell if the operands overlap. We
309 -- exclude this treatment when Rhs is an aggregate, since we know
310 -- that overlap can't occur.
312 if (Has_Address_Clause (Lhs) and then Nkind (Rhs) /= N_Aggregate)
313 or else Has_Address_Clause (Rhs)
315 Set_Forwards_OK (N, False);
316 Set_Backwards_OK (N, False);
319 -- We certainly must use a loop for change of representation and also
320 -- we use the operand of the conversion on the right hand side as the
321 -- effective right hand side (the component types must match in this
325 Act_Rhs := Get_Referenced_Object (Rhs);
326 R_Type := Get_Actual_Subtype (Act_Rhs);
327 Loop_Required := True;
329 -- We require a loop if the left side is possibly bit unaligned
331 elsif Possible_Bit_Aligned_Component (Lhs)
333 Possible_Bit_Aligned_Component (Rhs)
335 Loop_Required := True;
337 -- Arrays with controlled components are expanded into a loop to force
338 -- calls to Adjust at the component level.
340 elsif Has_Controlled_Component (L_Type) then
341 Loop_Required := True;
343 -- If object is atomic, we cannot tolerate a loop
345 elsif Is_Atomic_Object (Act_Lhs)
347 Is_Atomic_Object (Act_Rhs)
351 -- Loop is required if we have atomic components since we have to
352 -- be sure to do any accesses on an element by element basis.
354 elsif Has_Atomic_Components (L_Type)
355 or else Has_Atomic_Components (R_Type)
356 or else Is_Atomic (Component_Type (L_Type))
357 or else Is_Atomic (Component_Type (R_Type))
359 Loop_Required := True;
361 -- Case where no slice is involved
363 elsif not L_Slice and not R_Slice then
365 -- The following code deals with the case of unconstrained bit packed
366 -- arrays. The problem is that the template for such arrays contains
367 -- the bounds of the actual source level array, but the copy of an
368 -- entire array requires the bounds of the underlying array. It would
369 -- be nice if the back end could take care of this, but right now it
370 -- does not know how, so if we have such a type, then we expand out
371 -- into a loop, which is inefficient but works correctly. If we don't
372 -- do this, we get the wrong length computed for the array to be
373 -- moved. The two cases we need to worry about are:
375 -- Explicit dereference of an unconstrained packed array type as in
376 -- the following example:
379 -- type BITS is array(INTEGER range <>) of BOOLEAN;
380 -- pragma PACK(BITS);
381 -- type A is access BITS;
384 -- P1 := new BITS (1 .. 65_535);
385 -- P2 := new BITS (1 .. 65_535);
389 -- A formal parameter reference with an unconstrained bit array type
390 -- is the other case we need to worry about (here we assume the same
391 -- BITS type declared above):
393 -- procedure Write_All (File : out BITS; Contents : BITS);
395 -- File.Storage := Contents;
398 -- We expand to a loop in either of these two cases
400 -- Question for future thought. Another potentially more efficient
401 -- approach would be to create the actual subtype, and then do an
402 -- unchecked conversion to this actual subtype ???
404 Check_Unconstrained_Bit_Packed_Array : declare
406 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean;
407 -- Function to perform required test for the first case, above
408 -- (dereference of an unconstrained bit packed array).
410 -----------------------
411 -- Is_UBPA_Reference --
412 -----------------------
414 function Is_UBPA_Reference (Opnd : Node_Id) return Boolean is
415 Typ : constant Entity_Id := Underlying_Type (Etype (Opnd));
417 Des_Type : Entity_Id;
420 if Present (Packed_Array_Type (Typ))
421 and then Is_Array_Type (Packed_Array_Type (Typ))
422 and then not Is_Constrained (Packed_Array_Type (Typ))
426 elsif Nkind (Opnd) = N_Explicit_Dereference then
427 P_Type := Underlying_Type (Etype (Prefix (Opnd)));
429 if not Is_Access_Type (P_Type) then
433 Des_Type := Designated_Type (P_Type);
435 Is_Bit_Packed_Array (Des_Type)
436 and then not Is_Constrained (Des_Type);
442 end Is_UBPA_Reference;
444 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
447 if Is_UBPA_Reference (Lhs)
449 Is_UBPA_Reference (Rhs)
451 Loop_Required := True;
453 -- Here if we do not have the case of a reference to a bit packed
454 -- unconstrained array case. In this case gigi can most certainly
455 -- handle the assignment if a forwards move is allowed.
457 -- (could it handle the backwards case also???)
459 elsif Forwards_OK (N) then
462 end Check_Unconstrained_Bit_Packed_Array;
464 -- The back end can always handle the assignment if the right side is a
465 -- string literal (note that overlap is definitely impossible in this
466 -- case). If the type is packed, a string literal is always converted
467 -- into an aggregate, except in the case of a null slice, for which no
468 -- aggregate can be written. In that case, rewrite the assignment as a
469 -- null statement, a length check has already been emitted to verify
470 -- that the range of the left-hand side is empty.
472 -- Note that this code is not executed if we have an assignment of a
473 -- string literal to a non-bit aligned component of a record, a case
474 -- which cannot be handled by the backend.
476 elsif Nkind (Rhs) = N_String_Literal then
477 if String_Length (Strval (Rhs)) = 0
478 and then Is_Bit_Packed_Array (L_Type)
480 Rewrite (N, Make_Null_Statement (Loc));
486 -- If either operand is bit packed, then we need a loop, since we can't
487 -- be sure that the slice is byte aligned. Similarly, if either operand
488 -- is a possibly unaligned slice, then we need a loop (since the back
489 -- end cannot handle unaligned slices).
491 elsif Is_Bit_Packed_Array (L_Type)
492 or else Is_Bit_Packed_Array (R_Type)
493 or else Is_Possibly_Unaligned_Slice (Lhs)
494 or else Is_Possibly_Unaligned_Slice (Rhs)
496 Loop_Required := True;
498 -- If we are not bit-packed, and we have only one slice, then no overlap
499 -- is possible except in the parameter case, so we can let the back end
502 elsif not (L_Slice and R_Slice) then
503 if Forwards_OK (N) then
508 -- If the right-hand side is a string literal, introduce a temporary for
509 -- it, for use in the generated loop that will follow.
511 if Nkind (Rhs) = N_String_Literal then
513 Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Rhs);
518 Make_Object_Declaration (Loc,
519 Defining_Identifier => Temp,
520 Object_Definition => New_Occurrence_Of (L_Type, Loc),
521 Expression => Relocate_Node (Rhs));
523 Insert_Action (N, Decl);
524 Rewrite (Rhs, New_Occurrence_Of (Temp, Loc));
525 R_Type := Etype (Temp);
529 -- Come here to complete the analysis
531 -- Loop_Required: Set to True if we know that a loop is required
532 -- regardless of overlap considerations.
534 -- Forwards_OK: Set to False if we already know that a forwards
535 -- move is not safe, else set to True.
537 -- Backwards_OK: Set to False if we already know that a backwards
538 -- move is not safe, else set to True
540 -- Our task at this stage is to complete the overlap analysis, which can
541 -- result in possibly setting Forwards_OK or Backwards_OK to False, and
542 -- then generating the final code, either by deciding that it is OK
543 -- after all to let Gigi handle it, or by generating appropriate code
547 L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
548 R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
550 Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
551 Left_Hi : constant Node_Id := Type_High_Bound (L_Index_Typ);
552 Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
553 Right_Hi : constant Node_Id := Type_High_Bound (R_Index_Typ);
555 Act_L_Array : Node_Id;
556 Act_R_Array : Node_Id;
562 Cresult : Compare_Result;
565 -- Get the expressions for the arrays. If we are dealing with a
566 -- private type, then convert to the underlying type. We can do
567 -- direct assignments to an array that is a private type, but we
568 -- cannot assign to elements of the array without this extra
569 -- unchecked conversion.
571 -- Note: We propagate Parent to the conversion nodes to generate
572 -- a well-formed subtree.
574 if Nkind (Act_Lhs) = N_Slice then
575 Larray := Prefix (Act_Lhs);
579 if Is_Private_Type (Etype (Larray)) then
581 Par : constant Node_Id := Parent (Larray);
585 (Underlying_Type (Etype (Larray)), Larray);
586 Set_Parent (Larray, Par);
591 if Nkind (Act_Rhs) = N_Slice then
592 Rarray := Prefix (Act_Rhs);
596 if Is_Private_Type (Etype (Rarray)) then
598 Par : constant Node_Id := Parent (Rarray);
602 (Underlying_Type (Etype (Rarray)), Rarray);
603 Set_Parent (Rarray, Par);
608 -- If both sides are slices, we must figure out whether it is safe
609 -- to do the move in one direction or the other. It is always safe
610 -- if there is a change of representation since obviously two arrays
611 -- with different representations cannot possibly overlap.
613 if (not Crep) and L_Slice and R_Slice then
614 Act_L_Array := Get_Referenced_Object (Prefix (Act_Lhs));
615 Act_R_Array := Get_Referenced_Object (Prefix (Act_Rhs));
617 -- If both left and right hand arrays are entity names, and refer
618 -- to different entities, then we know that the move is safe (the
619 -- two storage areas are completely disjoint).
621 if Is_Entity_Name (Act_L_Array)
622 and then Is_Entity_Name (Act_R_Array)
623 and then Entity (Act_L_Array) /= Entity (Act_R_Array)
627 -- Otherwise, we assume the worst, which is that the two arrays
628 -- are the same array. There is no need to check if we know that
629 -- is the case, because if we don't know it, we still have to
632 -- Generally if the same array is involved, then we have an
633 -- overlapping case. We will have to really assume the worst (i.e.
634 -- set neither of the OK flags) unless we can determine the lower
635 -- or upper bounds at compile time and compare them.
640 (Left_Lo, Right_Lo, Assume_Valid => True);
642 if Cresult = Unknown then
645 (Left_Hi, Right_Hi, Assume_Valid => True);
649 when LT | LE | EQ => Set_Backwards_OK (N, False);
650 when GT | GE => Set_Forwards_OK (N, False);
651 when NE | Unknown => Set_Backwards_OK (N, False);
652 Set_Forwards_OK (N, False);
657 -- If after that analysis Loop_Required is False, meaning that we
658 -- have not discovered some non-overlap reason for requiring a loop,
659 -- then the outcome depends on the capabilities of the back end.
661 if not Loop_Required then
663 -- The GCC back end can deal with all cases of overlap by falling
664 -- back to memmove if it cannot use a more efficient approach.
666 if VM_Target = No_VM and not AAMP_On_Target then
669 -- Assume other back ends can handle it if Forwards_OK is set
671 elsif Forwards_OK (N) then
674 -- If Forwards_OK is not set, the back end will need something
675 -- like memmove to handle the move. For now, this processing is
676 -- activated using the .s debug flag (-gnatd.s).
678 elsif Debug_Flag_Dot_S then
683 -- At this stage we have to generate an explicit loop, and we have
684 -- the following cases:
686 -- Forwards_OK = True
688 -- Rnn : right_index := right_index'First;
689 -- for Lnn in left-index loop
690 -- left (Lnn) := right (Rnn);
691 -- Rnn := right_index'Succ (Rnn);
694 -- Note: the above code MUST be analyzed with checks off, because
695 -- otherwise the Succ could overflow. But in any case this is more
698 -- Forwards_OK = False, Backwards_OK = True
700 -- Rnn : right_index := right_index'Last;
701 -- for Lnn in reverse left-index loop
702 -- left (Lnn) := right (Rnn);
703 -- Rnn := right_index'Pred (Rnn);
706 -- Note: the above code MUST be analyzed with checks off, because
707 -- otherwise the Pred could overflow. But in any case this is more
710 -- Forwards_OK = Backwards_OK = False
712 -- This only happens if we have the same array on each side. It is
713 -- possible to create situations using overlays that violate this,
714 -- but we simply do not promise to get this "right" in this case.
716 -- There are two possible subcases. If the No_Implicit_Conditionals
717 -- restriction is set, then we generate the following code:
720 -- T : constant <operand-type> := rhs;
725 -- If implicit conditionals are permitted, then we generate:
727 -- if Left_Lo <= Right_Lo then
728 -- <code for Forwards_OK = True above>
730 -- <code for Backwards_OK = True above>
733 -- In order to detect possible aliasing, we examine the renamed
734 -- expression when the source or target is a renaming. However,
735 -- the renaming may be intended to capture an address that may be
736 -- affected by subsequent code, and therefore we must recover
737 -- the actual entity for the expansion that follows, not the
738 -- object it renames. In particular, if source or target designate
739 -- a portion of a dynamically allocated object, the pointer to it
740 -- may be reassigned but the renaming preserves the proper location.
742 if Is_Entity_Name (Rhs)
744 Nkind (Parent (Entity (Rhs))) = N_Object_Renaming_Declaration
745 and then Nkind (Act_Rhs) = N_Slice
750 if Is_Entity_Name (Lhs)
752 Nkind (Parent (Entity (Lhs))) = N_Object_Renaming_Declaration
753 and then Nkind (Act_Lhs) = N_Slice
758 -- Cases where either Forwards_OK or Backwards_OK is true
760 if Forwards_OK (N) or else Backwards_OK (N) then
761 if Needs_Finalization (Component_Type (L_Type))
762 and then Base_Type (L_Type) = Base_Type (R_Type)
764 and then not No_Ctrl_Actions (N)
767 Proc : constant Entity_Id :=
768 TSS (Base_Type (L_Type), TSS_Slice_Assign);
772 Apply_Dereference (Larray);
773 Apply_Dereference (Rarray);
774 Actuals := New_List (
775 Duplicate_Subexpr (Larray, Name_Req => True),
776 Duplicate_Subexpr (Rarray, Name_Req => True),
777 Duplicate_Subexpr (Left_Lo, Name_Req => True),
778 Duplicate_Subexpr (Left_Hi, Name_Req => True),
779 Duplicate_Subexpr (Right_Lo, Name_Req => True),
780 Duplicate_Subexpr (Right_Hi, Name_Req => True));
784 Boolean_Literals (not Forwards_OK (N)), Loc));
787 Make_Procedure_Call_Statement (Loc,
788 Name => New_Reference_To (Proc, Loc),
789 Parameter_Associations => Actuals));
794 Expand_Assign_Array_Loop
795 (N, Larray, Rarray, L_Type, R_Type, Ndim,
796 Rev => not Forwards_OK (N)));
799 -- Case of both are false with No_Implicit_Conditionals
801 elsif Restriction_Active (No_Implicit_Conditionals) then
803 T : constant Entity_Id :=
804 Make_Defining_Identifier (Loc, Chars => Name_T);
808 Make_Block_Statement (Loc,
809 Declarations => New_List (
810 Make_Object_Declaration (Loc,
811 Defining_Identifier => T,
812 Constant_Present => True,
814 New_Occurrence_Of (Etype (Rhs), Loc),
815 Expression => Relocate_Node (Rhs))),
817 Handled_Statement_Sequence =>
818 Make_Handled_Sequence_Of_Statements (Loc,
819 Statements => New_List (
820 Make_Assignment_Statement (Loc,
821 Name => Relocate_Node (Lhs),
822 Expression => New_Occurrence_Of (T, Loc))))));
825 -- Case of both are false with implicit conditionals allowed
828 -- Before we generate this code, we must ensure that the left and
829 -- right side array types are defined. They may be itypes, and we
830 -- cannot let them be defined inside the if, since the first use
831 -- in the then may not be executed.
833 Ensure_Defined (L_Type, N);
834 Ensure_Defined (R_Type, N);
836 -- We normally compare addresses to find out which way round to
837 -- do the loop, since this is reliable, and handles the cases of
838 -- parameters, conversions etc. But we can't do that in the bit
839 -- packed case or the VM case, because addresses don't work there.
841 if not Is_Bit_Packed_Array (L_Type) and then VM_Target = No_VM then
845 Unchecked_Convert_To (RTE (RE_Integer_Address),
846 Make_Attribute_Reference (Loc,
848 Make_Indexed_Component (Loc,
850 Duplicate_Subexpr_Move_Checks (Larray, True),
851 Expressions => New_List (
852 Make_Attribute_Reference (Loc,
856 Attribute_Name => Name_First))),
857 Attribute_Name => Name_Address)),
860 Unchecked_Convert_To (RTE (RE_Integer_Address),
861 Make_Attribute_Reference (Loc,
863 Make_Indexed_Component (Loc,
865 Duplicate_Subexpr_Move_Checks (Rarray, True),
866 Expressions => New_List (
867 Make_Attribute_Reference (Loc,
871 Attribute_Name => Name_First))),
872 Attribute_Name => Name_Address)));
874 -- For the bit packed and VM cases we use the bounds. That's OK,
875 -- because we don't have to worry about parameters, since they
876 -- cannot cause overlap. Perhaps we should worry about weird slice
882 Cleft_Lo := New_Copy_Tree (Left_Lo);
883 Cright_Lo := New_Copy_Tree (Right_Lo);
885 -- If the types do not match we add an implicit conversion
886 -- here to ensure proper match
888 if Etype (Left_Lo) /= Etype (Right_Lo) then
890 Unchecked_Convert_To (Etype (Left_Lo), Cright_Lo);
893 -- Reset the Analyzed flag, because the bounds of the index
894 -- type itself may be universal, and must must be reanalyzed
895 -- to acquire the proper type for the back end.
897 Set_Analyzed (Cleft_Lo, False);
898 Set_Analyzed (Cright_Lo, False);
902 Left_Opnd => Cleft_Lo,
903 Right_Opnd => Cright_Lo);
906 if Needs_Finalization (Component_Type (L_Type))
907 and then Base_Type (L_Type) = Base_Type (R_Type)
909 and then not No_Ctrl_Actions (N)
912 -- Call TSS procedure for array assignment, passing the
913 -- explicit bounds of right and left hand sides.
916 Proc : constant Entity_Id :=
917 TSS (Base_Type (L_Type), TSS_Slice_Assign);
921 Apply_Dereference (Larray);
922 Apply_Dereference (Rarray);
923 Actuals := New_List (
924 Duplicate_Subexpr (Larray, Name_Req => True),
925 Duplicate_Subexpr (Rarray, Name_Req => True),
926 Duplicate_Subexpr (Left_Lo, Name_Req => True),
927 Duplicate_Subexpr (Left_Hi, Name_Req => True),
928 Duplicate_Subexpr (Right_Lo, Name_Req => True),
929 Duplicate_Subexpr (Right_Hi, Name_Req => True));
933 Right_Opnd => Condition));
936 Make_Procedure_Call_Statement (Loc,
937 Name => New_Reference_To (Proc, Loc),
938 Parameter_Associations => Actuals));
943 Make_Implicit_If_Statement (N,
944 Condition => Condition,
946 Then_Statements => New_List (
947 Expand_Assign_Array_Loop
948 (N, Larray, Rarray, L_Type, R_Type, Ndim,
951 Else_Statements => New_List (
952 Expand_Assign_Array_Loop
953 (N, Larray, Rarray, L_Type, R_Type, Ndim,
958 Analyze (N, Suppress => All_Checks);
962 when RE_Not_Available =>
964 end Expand_Assign_Array;
966 ------------------------------
967 -- Expand_Assign_Array_Loop --
968 ------------------------------
970 -- The following is an example of the loop generated for the case of a
971 -- two-dimensional array:
976 -- for L1b in 1 .. 100 loop
980 -- for L3b in 1 .. 100 loop
981 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
982 -- R4b := Tm1X2'succ(R4b);
985 -- R2b := Tm1X1'succ(R2b);
989 -- Here Rev is False, and Tm1Xn are the subscript types for the right hand
990 -- side. The declarations of R2b and R4b are inserted before the original
991 -- assignment statement.
993 function Expand_Assign_Array_Loop
1000 Rev : Boolean) return Node_Id
1002 Loc : constant Source_Ptr := Sloc (N);
1004 Lnn : array (1 .. Ndim) of Entity_Id;
1005 Rnn : array (1 .. Ndim) of Entity_Id;
1006 -- Entities used as subscripts on left and right sides
1008 L_Index_Type : array (1 .. Ndim) of Entity_Id;
1009 R_Index_Type : array (1 .. Ndim) of Entity_Id;
1010 -- Left and right index types
1017 function Build_Step (J : Nat) return Node_Id;
1018 -- The increment step for the index of the right-hand side is written
1019 -- as an attribute reference (Succ or Pred). This function returns
1020 -- the corresponding node, which is placed at the end of the loop body.
1026 function Build_Step (J : Nat) return Node_Id is
1038 Make_Assignment_Statement (Loc,
1039 Name => New_Occurrence_Of (Rnn (J), Loc),
1041 Make_Attribute_Reference (Loc,
1043 New_Occurrence_Of (R_Index_Type (J), Loc),
1044 Attribute_Name => S_Or_P,
1045 Expressions => New_List (
1046 New_Occurrence_Of (Rnn (J), Loc))));
1048 -- Note that on the last iteration of the loop, the index is increased
1049 -- (or decreased) past the corresponding bound. This is consistent with
1050 -- the C semantics of the back-end, where such an off-by-one value on a
1051 -- dead index variable is OK. However, in CodePeer mode this leads to
1052 -- spurious warnings, and thus we place a guard around the attribute
1053 -- reference. For obvious reasons we only do this for CodePeer.
1055 if CodePeer_Mode then
1057 Make_If_Statement (Loc,
1060 Left_Opnd => New_Occurrence_Of (Lnn (J), Loc),
1062 Make_Attribute_Reference (Loc,
1063 Prefix => New_Occurrence_Of (L_Index_Type (J), Loc),
1064 Attribute_Name => Lim)),
1065 Then_Statements => New_List (Step));
1071 -- Start of processing for Expand_Assign_Array_Loop
1075 F_Or_L := Name_Last;
1076 S_Or_P := Name_Pred;
1078 F_Or_L := Name_First;
1079 S_Or_P := Name_Succ;
1082 -- Setup index types and subscript entities
1089 L_Index := First_Index (L_Type);
1090 R_Index := First_Index (R_Type);
1092 for J in 1 .. Ndim loop
1093 Lnn (J) := Make_Temporary (Loc, 'L');
1094 Rnn (J) := Make_Temporary (Loc, 'R');
1096 L_Index_Type (J) := Etype (L_Index);
1097 R_Index_Type (J) := Etype (R_Index);
1099 Next_Index (L_Index);
1100 Next_Index (R_Index);
1104 -- Now construct the assignment statement
1107 ExprL : constant List_Id := New_List;
1108 ExprR : constant List_Id := New_List;
1111 for J in 1 .. Ndim loop
1112 Append_To (ExprL, New_Occurrence_Of (Lnn (J), Loc));
1113 Append_To (ExprR, New_Occurrence_Of (Rnn (J), Loc));
1117 Make_Assignment_Statement (Loc,
1119 Make_Indexed_Component (Loc,
1120 Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
1121 Expressions => ExprL),
1123 Make_Indexed_Component (Loc,
1124 Prefix => Duplicate_Subexpr (Rarray, Name_Req => True),
1125 Expressions => ExprR));
1127 -- We set assignment OK, since there are some cases, e.g. in object
1128 -- declarations, where we are actually assigning into a constant.
1129 -- If there really is an illegality, it was caught long before now,
1130 -- and was flagged when the original assignment was analyzed.
1132 Set_Assignment_OK (Name (Assign));
1134 -- Propagate the No_Ctrl_Actions flag to individual assignments
1136 Set_No_Ctrl_Actions (Assign, No_Ctrl_Actions (N));
1139 -- Now construct the loop from the inside out, with the last subscript
1140 -- varying most rapidly. Note that Assign is first the raw assignment
1141 -- statement, and then subsequently the loop that wraps it up.
1143 for J in reverse 1 .. Ndim loop
1145 Make_Block_Statement (Loc,
1146 Declarations => New_List (
1147 Make_Object_Declaration (Loc,
1148 Defining_Identifier => Rnn (J),
1149 Object_Definition =>
1150 New_Occurrence_Of (R_Index_Type (J), Loc),
1152 Make_Attribute_Reference (Loc,
1153 Prefix => New_Occurrence_Of (R_Index_Type (J), Loc),
1154 Attribute_Name => F_Or_L))),
1156 Handled_Statement_Sequence =>
1157 Make_Handled_Sequence_Of_Statements (Loc,
1158 Statements => New_List (
1159 Make_Implicit_Loop_Statement (N,
1161 Make_Iteration_Scheme (Loc,
1162 Loop_Parameter_Specification =>
1163 Make_Loop_Parameter_Specification (Loc,
1164 Defining_Identifier => Lnn (J),
1165 Reverse_Present => Rev,
1166 Discrete_Subtype_Definition =>
1167 New_Reference_To (L_Index_Type (J), Loc))),
1169 Statements => New_List (Assign, Build_Step (J))))));
1173 end Expand_Assign_Array_Loop;
1175 --------------------------
1176 -- Expand_Assign_Record --
1177 --------------------------
1179 procedure Expand_Assign_Record (N : Node_Id) is
1180 Lhs : constant Node_Id := Name (N);
1181 Rhs : Node_Id := Expression (N);
1182 L_Typ : constant Entity_Id := Base_Type (Etype (Lhs));
1185 -- If change of representation, then extract the real right hand side
1186 -- from the type conversion, and proceed with component-wise assignment,
1187 -- since the two types are not the same as far as the back end is
1190 if Change_Of_Representation (N) then
1191 Rhs := Expression (Rhs);
1193 -- If this may be a case of a large bit aligned component, then proceed
1194 -- with component-wise assignment, to avoid possible clobbering of other
1195 -- components sharing bits in the first or last byte of the component to
1198 elsif Possible_Bit_Aligned_Component (Lhs)
1200 Possible_Bit_Aligned_Component (Rhs)
1204 -- If we have a tagged type that has a complete record representation
1205 -- clause, we must do we must do component-wise assignments, since child
1206 -- types may have used gaps for their components, and we might be
1207 -- dealing with a view conversion.
1209 elsif Is_Fully_Repped_Tagged_Type (L_Typ) then
1212 -- If neither condition met, then nothing special to do, the back end
1213 -- can handle assignment of the entire component as a single entity.
1219 -- At this stage we know that we must do a component wise assignment
1222 Loc : constant Source_Ptr := Sloc (N);
1223 R_Typ : constant Entity_Id := Base_Type (Etype (Rhs));
1224 Decl : constant Node_Id := Declaration_Node (R_Typ);
1228 function Find_Component
1230 Comp : Entity_Id) return Entity_Id;
1231 -- Find the component with the given name in the underlying record
1232 -- declaration for Typ. We need to use the actual entity because the
1233 -- type may be private and resolution by identifier alone would fail.
1235 function Make_Component_List_Assign
1237 U_U : Boolean := False) return List_Id;
1238 -- Returns a sequence of statements to assign the components that
1239 -- are referenced in the given component list. The flag U_U is
1240 -- used to force the usage of the inferred value of the variant
1241 -- part expression as the switch for the generated case statement.
1243 function Make_Field_Assign
1245 U_U : Boolean := False) return Node_Id;
1246 -- Given C, the entity for a discriminant or component, build an
1247 -- assignment for the corresponding field values. The flag U_U
1248 -- signals the presence of an Unchecked_Union and forces the usage
1249 -- of the inferred discriminant value of C as the right hand side
1250 -- of the assignment.
1252 function Make_Field_Assigns (CI : List_Id) return List_Id;
1253 -- Given CI, a component items list, construct series of statements
1254 -- for fieldwise assignment of the corresponding components.
1256 --------------------
1257 -- Find_Component --
1258 --------------------
1260 function Find_Component
1262 Comp : Entity_Id) return Entity_Id
1264 Utyp : constant Entity_Id := Underlying_Type (Typ);
1268 C := First_Entity (Utyp);
1269 while Present (C) loop
1270 if Chars (C) = Chars (Comp) then
1277 raise Program_Error;
1280 --------------------------------
1281 -- Make_Component_List_Assign --
1282 --------------------------------
1284 function Make_Component_List_Assign
1286 U_U : Boolean := False) return List_Id
1288 CI : constant List_Id := Component_Items (CL);
1289 VP : constant Node_Id := Variant_Part (CL);
1299 Result := Make_Field_Assigns (CI);
1301 if Present (VP) then
1302 V := First_Non_Pragma (Variants (VP));
1304 while Present (V) loop
1306 DC := First (Discrete_Choices (V));
1307 while Present (DC) loop
1308 Append_To (DCH, New_Copy_Tree (DC));
1313 Make_Case_Statement_Alternative (Loc,
1314 Discrete_Choices => DCH,
1316 Make_Component_List_Assign (Component_List (V))));
1317 Next_Non_Pragma (V);
1320 -- If we have an Unchecked_Union, use the value of the inferred
1321 -- discriminant of the variant part expression as the switch
1322 -- for the case statement. The case statement may later be
1327 New_Copy (Get_Discriminant_Value (
1330 Discriminant_Constraint (Etype (Rhs))));
1333 Make_Selected_Component (Loc,
1334 Prefix => Duplicate_Subexpr (Rhs),
1336 Make_Identifier (Loc, Chars (Name (VP))));
1340 Make_Case_Statement (Loc,
1342 Alternatives => Alts));
1346 end Make_Component_List_Assign;
1348 -----------------------
1349 -- Make_Field_Assign --
1350 -----------------------
1352 function Make_Field_Assign
1354 U_U : Boolean := False) return Node_Id
1360 -- In the case of an Unchecked_Union, use the discriminant
1361 -- constraint value as on the right hand side of the assignment.
1365 New_Copy (Get_Discriminant_Value (C,
1367 Discriminant_Constraint (Etype (Rhs))));
1370 Make_Selected_Component (Loc,
1371 Prefix => Duplicate_Subexpr (Rhs),
1372 Selector_Name => New_Occurrence_Of (C, Loc));
1376 Make_Assignment_Statement (Loc,
1378 Make_Selected_Component (Loc,
1379 Prefix => Duplicate_Subexpr (Lhs),
1381 New_Occurrence_Of (Find_Component (L_Typ, C), Loc)),
1382 Expression => Expr);
1384 -- Set Assignment_OK, so discriminants can be assigned
1386 Set_Assignment_OK (Name (A), True);
1388 if Componentwise_Assignment (N)
1389 and then Nkind (Name (A)) = N_Selected_Component
1390 and then Chars (Selector_Name (Name (A))) = Name_uParent
1392 Set_Componentwise_Assignment (A);
1396 end Make_Field_Assign;
1398 ------------------------
1399 -- Make_Field_Assigns --
1400 ------------------------
1402 function Make_Field_Assigns (CI : List_Id) return List_Id is
1410 while Present (Item) loop
1412 -- Look for components, but exclude _tag field assignment if
1413 -- the special Componentwise_Assignment flag is set.
1415 if Nkind (Item) = N_Component_Declaration
1416 and then not (Is_Tag (Defining_Identifier (Item))
1417 and then Componentwise_Assignment (N))
1420 (Result, Make_Field_Assign (Defining_Identifier (Item)));
1427 end Make_Field_Assigns;
1429 -- Start of processing for Expand_Assign_Record
1432 -- Note that we use the base types for this processing. This results
1433 -- in some extra work in the constrained case, but the change of
1434 -- representation case is so unusual that it is not worth the effort.
1436 -- First copy the discriminants. This is done unconditionally. It
1437 -- is required in the unconstrained left side case, and also in the
1438 -- case where this assignment was constructed during the expansion
1439 -- of a type conversion (since initialization of discriminants is
1440 -- suppressed in this case). It is unnecessary but harmless in
1443 if Has_Discriminants (L_Typ) then
1444 F := First_Discriminant (R_Typ);
1445 while Present (F) loop
1447 -- If we are expanding the initialization of a derived record
1448 -- that constrains or renames discriminants of the parent, we
1449 -- must use the corresponding discriminant in the parent.
1456 and then Present (Corresponding_Discriminant (F))
1458 CF := Corresponding_Discriminant (F);
1463 if Is_Unchecked_Union (Base_Type (R_Typ)) then
1464 Insert_Action (N, Make_Field_Assign (CF, True));
1466 Insert_Action (N, Make_Field_Assign (CF));
1469 Next_Discriminant (F);
1474 -- We know the underlying type is a record, but its current view
1475 -- may be private. We must retrieve the usable record declaration.
1477 if Nkind_In (Decl, N_Private_Type_Declaration,
1478 N_Private_Extension_Declaration)
1479 and then Present (Full_View (R_Typ))
1481 RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
1483 RDef := Type_Definition (Decl);
1486 if Nkind (RDef) = N_Derived_Type_Definition then
1487 RDef := Record_Extension_Part (RDef);
1490 if Nkind (RDef) = N_Record_Definition
1491 and then Present (Component_List (RDef))
1493 if Is_Unchecked_Union (R_Typ) then
1495 Make_Component_List_Assign (Component_List (RDef), True));
1498 (N, Make_Component_List_Assign (Component_List (RDef)));
1501 Rewrite (N, Make_Null_Statement (Loc));
1504 end Expand_Assign_Record;
1506 -----------------------------------
1507 -- Expand_N_Assignment_Statement --
1508 -----------------------------------
1510 -- This procedure implements various cases where an assignment statement
1511 -- cannot just be passed on to the back end in untransformed state.
1513 procedure Expand_N_Assignment_Statement (N : Node_Id) is
1514 Loc : constant Source_Ptr := Sloc (N);
1515 Crep : constant Boolean := Change_Of_Representation (N);
1516 Lhs : constant Node_Id := Name (N);
1517 Rhs : constant Node_Id := Expression (N);
1518 Typ : constant Entity_Id := Underlying_Type (Etype (Lhs));
1522 -- Special case to check right away, if the Componentwise_Assignment
1523 -- flag is set, this is a reanalysis from the expansion of the primitive
1524 -- assignment procedure for a tagged type, and all we need to do is to
1525 -- expand to assignment of components, because otherwise, we would get
1526 -- infinite recursion (since this looks like a tagged assignment which
1527 -- would normally try to *call* the primitive assignment procedure).
1529 if Componentwise_Assignment (N) then
1530 Expand_Assign_Record (N);
1534 -- Defend against invalid subscripts on left side if we are in standard
1535 -- validity checking mode. No need to do this if we are checking all
1538 -- Note that we do this right away, because there are some early return
1539 -- paths in this procedure, and this is required on all paths.
1541 if Validity_Checks_On
1542 and then Validity_Check_Default
1543 and then not Validity_Check_Subscripts
1545 Check_Valid_Lvalue_Subscripts (Lhs);
1548 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1550 -- Rewrite an assignment to X'Priority into a run-time call
1552 -- For example: X'Priority := New_Prio_Expr;
1553 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1555 -- Note that although X'Priority is notionally an object, it is quite
1556 -- deliberately not defined as an aliased object in the RM. This means
1557 -- that it works fine to rewrite it as a call, without having to worry
1558 -- about complications that would other arise from X'Priority'Access,
1559 -- which is illegal, because of the lack of aliasing.
1561 if Ada_Version >= Ada_2005 then
1564 Conctyp : Entity_Id;
1567 RT_Subprg_Name : Node_Id;
1570 -- Handle chains of renamings
1573 while Nkind (Ent) in N_Has_Entity
1574 and then Present (Entity (Ent))
1575 and then Present (Renamed_Object (Entity (Ent)))
1577 Ent := Renamed_Object (Entity (Ent));
1580 -- The attribute Priority applied to protected objects has been
1581 -- previously expanded into a call to the Get_Ceiling run-time
1584 if Nkind (Ent) = N_Function_Call
1585 and then (Entity (Name (Ent)) = RTE (RE_Get_Ceiling)
1587 Entity (Name (Ent)) = RTE (RO_PE_Get_Ceiling))
1589 -- Look for the enclosing concurrent type
1591 Conctyp := Current_Scope;
1592 while not Is_Concurrent_Type (Conctyp) loop
1593 Conctyp := Scope (Conctyp);
1596 pragma Assert (Is_Protected_Type (Conctyp));
1598 -- Generate the first actual of the call
1600 Subprg := Current_Scope;
1601 while not Present (Protected_Body_Subprogram (Subprg)) loop
1602 Subprg := Scope (Subprg);
1605 -- Select the appropriate run-time call
1607 if Number_Entries (Conctyp) = 0 then
1609 New_Reference_To (RTE (RE_Set_Ceiling), Loc);
1612 New_Reference_To (RTE (RO_PE_Set_Ceiling), Loc);
1616 Make_Procedure_Call_Statement (Loc,
1617 Name => RT_Subprg_Name,
1618 Parameter_Associations => New_List (
1619 New_Copy_Tree (First (Parameter_Associations (Ent))),
1620 Relocate_Node (Expression (N))));
1629 -- Deal with assignment checks unless suppressed
1631 if not Suppress_Assignment_Checks (N) then
1633 -- First deal with generation of range check if required
1635 if Do_Range_Check (Rhs) then
1636 Set_Do_Range_Check (Rhs, False);
1637 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
1640 -- Then generate predicate check if required
1642 Apply_Predicate_Check (Rhs, Typ);
1645 -- Check for a special case where a high level transformation is
1646 -- required. If we have either of:
1651 -- where P is a reference to a bit packed array, then we have to unwind
1652 -- the assignment. The exact meaning of being a reference to a bit
1653 -- packed array is as follows:
1655 -- An indexed component whose prefix is a bit packed array is a
1656 -- reference to a bit packed array.
1658 -- An indexed component or selected component whose prefix is a
1659 -- reference to a bit packed array is itself a reference ot a
1660 -- bit packed array.
1662 -- The required transformation is
1664 -- Tnn : prefix_type := P;
1665 -- Tnn.field := rhs;
1670 -- Tnn : prefix_type := P;
1671 -- Tnn (subscr) := rhs;
1674 -- Since P is going to be evaluated more than once, any subscripts
1675 -- in P must have their evaluation forced.
1677 if Nkind_In (Lhs, N_Indexed_Component, N_Selected_Component)
1678 and then Is_Ref_To_Bit_Packed_Array (Prefix (Lhs))
1681 BPAR_Expr : constant Node_Id := Relocate_Node (Prefix (Lhs));
1682 BPAR_Typ : constant Entity_Id := Etype (BPAR_Expr);
1683 Tnn : constant Entity_Id :=
1684 Make_Temporary (Loc, 'T', BPAR_Expr);
1687 -- Insert the post assignment first, because we want to copy the
1688 -- BPAR_Expr tree before it gets analyzed in the context of the
1689 -- pre assignment. Note that we do not analyze the post assignment
1690 -- yet (we cannot till we have completed the analysis of the pre
1691 -- assignment). As usual, the analysis of this post assignment
1692 -- will happen on its own when we "run into" it after finishing
1693 -- the current assignment.
1696 Make_Assignment_Statement (Loc,
1697 Name => New_Copy_Tree (BPAR_Expr),
1698 Expression => New_Occurrence_Of (Tnn, Loc)));
1700 -- At this stage BPAR_Expr is a reference to a bit packed array
1701 -- where the reference was not expanded in the original tree,
1702 -- since it was on the left side of an assignment. But in the
1703 -- pre-assignment statement (the object definition), BPAR_Expr
1704 -- will end up on the right hand side, and must be reexpanded. To
1705 -- achieve this, we reset the analyzed flag of all selected and
1706 -- indexed components down to the actual indexed component for
1707 -- the packed array.
1711 Set_Analyzed (Exp, False);
1714 (Exp, N_Selected_Component, N_Indexed_Component)
1716 Exp := Prefix (Exp);
1722 -- Now we can insert and analyze the pre-assignment
1724 -- If the right-hand side requires a transient scope, it has
1725 -- already been placed on the stack. However, the declaration is
1726 -- inserted in the tree outside of this scope, and must reflect
1727 -- the proper scope for its variable. This awkward bit is forced
1728 -- by the stricter scope discipline imposed by GCC 2.97.
1731 Uses_Transient_Scope : constant Boolean :=
1733 and then N = Node_To_Be_Wrapped;
1736 if Uses_Transient_Scope then
1737 Push_Scope (Scope (Current_Scope));
1740 Insert_Before_And_Analyze (N,
1741 Make_Object_Declaration (Loc,
1742 Defining_Identifier => Tnn,
1743 Object_Definition => New_Occurrence_Of (BPAR_Typ, Loc),
1744 Expression => BPAR_Expr));
1746 if Uses_Transient_Scope then
1751 -- Now fix up the original assignment and continue processing
1753 Rewrite (Prefix (Lhs),
1754 New_Occurrence_Of (Tnn, Loc));
1756 -- We do not need to reanalyze that assignment, and we do not need
1757 -- to worry about references to the temporary, but we do need to
1758 -- make sure that the temporary is not marked as a true constant
1759 -- since we now have a generated assignment to it!
1761 Set_Is_True_Constant (Tnn, False);
1765 -- When we have the appropriate type of aggregate in the expression (it
1766 -- has been determined during analysis of the aggregate by setting the
1767 -- delay flag), let's perform in place assignment and thus avoid
1768 -- creating a temporary.
1770 if Is_Delayed_Aggregate (Rhs) then
1771 Convert_Aggr_In_Assignment (N);
1772 Rewrite (N, Make_Null_Statement (Loc));
1777 -- Apply discriminant check if required. If Lhs is an access type to a
1778 -- designated type with discriminants, we must always check.
1780 if Has_Discriminants (Etype (Lhs)) then
1782 -- Skip discriminant check if change of representation. Will be
1783 -- done when the change of representation is expanded out.
1786 Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
1789 -- If the type is private without discriminants, and the full type
1790 -- has discriminants (necessarily with defaults) a check may still be
1791 -- necessary if the Lhs is aliased. The private discriminants must be
1792 -- visible to build the discriminant constraints.
1794 -- Only an explicit dereference that comes from source indicates
1795 -- aliasing. Access to formals of protected operations and entries
1796 -- create dereferences but are not semantic aliasings.
1798 elsif Is_Private_Type (Etype (Lhs))
1799 and then Has_Discriminants (Typ)
1800 and then Nkind (Lhs) = N_Explicit_Dereference
1801 and then Comes_From_Source (Lhs)
1804 Lt : constant Entity_Id := Etype (Lhs);
1805 Ubt : Entity_Id := Base_Type (Typ);
1808 -- In the case of an expander-generated record subtype whose base
1809 -- type still appears private, Typ will have been set to that
1810 -- private type rather than the underlying record type (because
1811 -- Underlying type will have returned the record subtype), so it's
1812 -- necessary to apply Underlying_Type again to the base type to
1813 -- get the record type we need for the discriminant check. Such
1814 -- subtypes can be created for assignments in certain cases, such
1815 -- as within an instantiation passed this kind of private type.
1816 -- It would be good to avoid this special test, but making changes
1817 -- to prevent this odd form of record subtype seems difficult. ???
1819 if Is_Private_Type (Ubt) then
1820 Ubt := Underlying_Type (Ubt);
1823 Set_Etype (Lhs, Ubt);
1824 Rewrite (Rhs, OK_Convert_To (Base_Type (Ubt), Rhs));
1825 Apply_Discriminant_Check (Rhs, Ubt, Lhs);
1826 Set_Etype (Lhs, Lt);
1829 -- If the Lhs has a private type with unknown discriminants, it
1830 -- may have a full view with discriminants, but those are nameable
1831 -- only in the underlying type, so convert the Rhs to it before
1832 -- potential checking.
1834 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs)))
1835 and then Has_Discriminants (Typ)
1837 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
1838 Apply_Discriminant_Check (Rhs, Typ, Lhs);
1840 -- In the access type case, we need the same discriminant check, and
1841 -- also range checks if we have an access to constrained array.
1843 elsif Is_Access_Type (Etype (Lhs))
1844 and then Is_Constrained (Designated_Type (Etype (Lhs)))
1846 if Has_Discriminants (Designated_Type (Etype (Lhs))) then
1848 -- Skip discriminant check if change of representation. Will be
1849 -- done when the change of representation is expanded out.
1852 Apply_Discriminant_Check (Rhs, Etype (Lhs));
1855 elsif Is_Array_Type (Designated_Type (Etype (Lhs))) then
1856 Apply_Range_Check (Rhs, Etype (Lhs));
1858 if Is_Constrained (Etype (Lhs)) then
1859 Apply_Length_Check (Rhs, Etype (Lhs));
1862 if Nkind (Rhs) = N_Allocator then
1864 Target_Typ : constant Entity_Id := Etype (Expression (Rhs));
1865 C_Es : Check_Result;
1872 Etype (Designated_Type (Etype (Lhs))));
1884 -- Apply range check for access type case
1886 elsif Is_Access_Type (Etype (Lhs))
1887 and then Nkind (Rhs) = N_Allocator
1888 and then Nkind (Expression (Rhs)) = N_Qualified_Expression
1890 Analyze_And_Resolve (Expression (Rhs));
1892 (Expression (Rhs), Designated_Type (Etype (Lhs)));
1895 -- Ada 2005 (AI-231): Generate the run-time check
1897 if Is_Access_Type (Typ)
1898 and then Can_Never_Be_Null (Etype (Lhs))
1899 and then not Can_Never_Be_Null (Etype (Rhs))
1901 Apply_Constraint_Check (Rhs, Etype (Lhs));
1904 -- Ada 2012 (AI05-148): Update current accessibility level if Rhs is a
1905 -- stand-alone obj of an anonymous access type.
1907 if Is_Access_Type (Typ)
1908 and then Is_Entity_Name (Lhs)
1909 and then Present (Effective_Extra_Accessibility (Entity (Lhs))) then
1911 function Lhs_Entity return Entity_Id;
1912 -- Look through renames to find the underlying entity.
1913 -- For assignment to a rename, we don't care about the
1914 -- Enclosing_Dynamic_Scope of the rename declaration.
1920 function Lhs_Entity return Entity_Id is
1921 Result : Entity_Id := Entity (Lhs);
1924 while Present (Renamed_Object (Result)) loop
1926 -- Renamed_Object must return an Entity_Name here
1927 -- because of preceding "Present (E_E_A (...))" test.
1929 Result := Entity (Renamed_Object (Result));
1935 -- Local Declarations
1937 Access_Check : constant Node_Id :=
1938 Make_Raise_Program_Error (Loc,
1942 Dynamic_Accessibility_Level (Rhs),
1944 Make_Integer_Literal (Loc,
1947 (Enclosing_Dynamic_Scope
1949 Reason => PE_Accessibility_Check_Failed);
1951 Access_Level_Update : constant Node_Id :=
1952 Make_Assignment_Statement (Loc,
1955 (Effective_Extra_Accessibility
1956 (Entity (Lhs)), Loc),
1958 Dynamic_Accessibility_Level (Rhs));
1961 if not Accessibility_Checks_Suppressed (Entity (Lhs)) then
1962 Insert_Action (N, Access_Check);
1965 Insert_Action (N, Access_Level_Update);
1969 -- Case of assignment to a bit packed array element. If there is a
1970 -- change of representation this must be expanded into components,
1971 -- otherwise this is a bit-field assignment.
1973 if Nkind (Lhs) = N_Indexed_Component
1974 and then Is_Bit_Packed_Array (Etype (Prefix (Lhs)))
1976 -- Normal case, no change of representation
1979 Expand_Bit_Packed_Element_Set (N);
1982 -- Change of representation case
1985 -- Generate the following, to force component-by-component
1986 -- assignments in an efficient way. Otherwise each component
1987 -- will require a temporary and two bit-field manipulations.
1994 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T');
2000 Make_Object_Declaration (Loc,
2001 Defining_Identifier => Tnn,
2002 Object_Definition =>
2003 New_Occurrence_Of (Etype (Lhs), Loc)),
2004 Make_Assignment_Statement (Loc,
2005 Name => New_Occurrence_Of (Tnn, Loc),
2006 Expression => Relocate_Node (Rhs)),
2007 Make_Assignment_Statement (Loc,
2008 Name => Relocate_Node (Lhs),
2009 Expression => New_Occurrence_Of (Tnn, Loc)));
2011 Insert_Actions (N, Stats);
2012 Rewrite (N, Make_Null_Statement (Loc));
2017 -- Build-in-place function call case. Note that we're not yet doing
2018 -- build-in-place for user-written assignment statements (the assignment
2019 -- here came from an aggregate.)
2021 elsif Ada_Version >= Ada_2005
2022 and then Is_Build_In_Place_Function_Call (Rhs)
2024 Make_Build_In_Place_Call_In_Assignment (N, Rhs);
2026 elsif Is_Tagged_Type (Typ) and then Is_Value_Type (Etype (Lhs)) then
2028 -- Nothing to do for valuetypes
2029 -- ??? Set_Scope_Is_Transient (False);
2033 elsif Is_Tagged_Type (Typ)
2034 or else (Needs_Finalization (Typ) and then not Is_Array_Type (Typ))
2036 Tagged_Case : declare
2037 L : List_Id := No_List;
2038 Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
2041 -- In the controlled case, we ensure that function calls are
2042 -- evaluated before finalizing the target. In all cases, it makes
2043 -- the expansion easier if the side-effects are removed first.
2045 Remove_Side_Effects (Lhs);
2046 Remove_Side_Effects (Rhs);
2048 -- Avoid recursion in the mechanism
2052 -- If dispatching assignment, we need to dispatch to _assign
2054 if Is_Class_Wide_Type (Typ)
2056 -- If the type is tagged, we may as well use the predefined
2057 -- primitive assignment. This avoids inlining a lot of code
2058 -- and in the class-wide case, the assignment is replaced
2059 -- by a dispatching call to _assign. It is suppressed in the
2060 -- case of assignments created by the expander that correspond
2061 -- to initializations, where we do want to copy the tag
2062 -- (Expand_Ctrl_Actions flag is set True in this case). It is
2063 -- also suppressed if restriction No_Dispatching_Calls is in
2064 -- force because in that case predefined primitives are not
2067 or else (Is_Tagged_Type (Typ)
2068 and then not Is_Value_Type (Etype (Lhs))
2069 and then Chars (Current_Scope) /= Name_uAssign
2070 and then Expand_Ctrl_Actions
2072 not Restriction_Active (No_Dispatching_Calls))
2074 -- Fetch the primitive op _assign and proper type to call it.
2075 -- Because of possible conflicts between private and full view,
2076 -- fetch the proper type directly from the operation profile.
2079 Op : constant Entity_Id :=
2080 Find_Prim_Op (Typ, Name_uAssign);
2081 F_Typ : Entity_Id := Etype (First_Formal (Op));
2084 -- If the assignment is dispatching, make sure to use the
2087 if Is_Class_Wide_Type (Typ) then
2088 F_Typ := Class_Wide_Type (F_Typ);
2093 -- In case of assignment to a class-wide tagged type, before
2094 -- the assignment we generate run-time check to ensure that
2095 -- the tags of source and target match.
2097 if Is_Class_Wide_Type (Typ)
2098 and then Is_Tagged_Type (Typ)
2099 and then Is_Tagged_Type (Underlying_Type (Etype (Rhs)))
2102 Make_Raise_Constraint_Error (Loc,
2106 Make_Selected_Component (Loc,
2107 Prefix => Duplicate_Subexpr (Lhs),
2109 Make_Identifier (Loc, Name_uTag)),
2111 Make_Selected_Component (Loc,
2112 Prefix => Duplicate_Subexpr (Rhs),
2114 Make_Identifier (Loc, Name_uTag))),
2115 Reason => CE_Tag_Check_Failed));
2119 Left_N : Node_Id := Duplicate_Subexpr (Lhs);
2120 Right_N : Node_Id := Duplicate_Subexpr (Rhs);
2123 -- In order to dispatch the call to _assign the type of
2124 -- the actuals must match. Add conversion (if required).
2126 if Etype (Lhs) /= F_Typ then
2127 Left_N := Unchecked_Convert_To (F_Typ, Left_N);
2130 if Etype (Rhs) /= F_Typ then
2131 Right_N := Unchecked_Convert_To (F_Typ, Right_N);
2135 Make_Procedure_Call_Statement (Loc,
2136 Name => New_Reference_To (Op, Loc),
2137 Parameter_Associations => New_List (
2139 Node2 => Right_N)));
2144 L := Make_Tag_Ctrl_Assignment (N);
2146 -- We can't afford to have destructive Finalization Actions in
2147 -- the Self assignment case, so if the target and the source
2148 -- are not obviously different, code is generated to avoid the
2149 -- self assignment case:
2151 -- if lhs'address /= rhs'address then
2152 -- <code for controlled and/or tagged assignment>
2155 -- Skip this if Restriction (No_Finalization) is active
2157 if not Statically_Different (Lhs, Rhs)
2158 and then Expand_Ctrl_Actions
2159 and then not Restriction_Active (No_Finalization)
2162 Make_Implicit_If_Statement (N,
2166 Make_Attribute_Reference (Loc,
2167 Prefix => Duplicate_Subexpr (Lhs),
2168 Attribute_Name => Name_Address),
2171 Make_Attribute_Reference (Loc,
2172 Prefix => Duplicate_Subexpr (Rhs),
2173 Attribute_Name => Name_Address)),
2175 Then_Statements => L));
2178 -- We need to set up an exception handler for implementing
2179 -- 7.6.1(18). The remaining adjustments are tackled by the
2180 -- implementation of adjust for record_controllers (see
2183 -- This is skipped if we have no finalization
2185 if Expand_Ctrl_Actions
2186 and then not Restriction_Active (No_Finalization)
2189 Make_Block_Statement (Loc,
2190 Handled_Statement_Sequence =>
2191 Make_Handled_Sequence_Of_Statements (Loc,
2193 Exception_Handlers => New_List (
2194 Make_Handler_For_Ctrl_Operation (Loc)))));
2199 Make_Block_Statement (Loc,
2200 Handled_Statement_Sequence =>
2201 Make_Handled_Sequence_Of_Statements (Loc, Statements => L)));
2203 -- If no restrictions on aborts, protect the whole assignment
2204 -- for controlled objects as per 9.8(11).
2206 if Needs_Finalization (Typ)
2207 and then Expand_Ctrl_Actions
2208 and then Abort_Allowed
2211 Blk : constant Entity_Id :=
2213 (E_Block, Current_Scope, Sloc (N), 'B');
2216 Set_Scope (Blk, Current_Scope);
2217 Set_Etype (Blk, Standard_Void_Type);
2218 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
2220 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
2221 Set_At_End_Proc (Handled_Statement_Sequence (N),
2222 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
2223 Expand_At_End_Handler
2224 (Handled_Statement_Sequence (N), Blk);
2228 -- N has been rewritten to a block statement for which it is
2229 -- known by construction that no checks are necessary: analyze
2230 -- it with all checks suppressed.
2232 Analyze (N, Suppress => All_Checks);
2238 elsif Is_Array_Type (Typ) then
2240 Actual_Rhs : Node_Id := Rhs;
2243 while Nkind_In (Actual_Rhs, N_Type_Conversion,
2244 N_Qualified_Expression)
2246 Actual_Rhs := Expression (Actual_Rhs);
2249 Expand_Assign_Array (N, Actual_Rhs);
2255 elsif Is_Record_Type (Typ) then
2256 Expand_Assign_Record (N);
2259 -- Scalar types. This is where we perform the processing related to the
2260 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2263 elsif Is_Scalar_Type (Typ) then
2265 -- Case where right side is known valid
2267 if Expr_Known_Valid (Rhs) then
2269 -- Here the right side is valid, so it is fine. The case to deal
2270 -- with is when the left side is a local variable reference whose
2271 -- value is not currently known to be valid. If this is the case,
2272 -- and the assignment appears in an unconditional context, then
2273 -- we can mark the left side as now being valid if one of these
2274 -- conditions holds:
2276 -- The expression of the right side has Do_Range_Check set so
2277 -- that we know a range check will be performed. Note that it
2278 -- can be the case that a range check is omitted because we
2279 -- make the assumption that we can assume validity for operands
2280 -- appearing in the right side in determining whether a range
2281 -- check is required
2283 -- The subtype of the right side matches the subtype of the
2284 -- left side. In this case, even though we have not checked
2285 -- the range of the right side, we know it is in range of its
2286 -- subtype if the expression is valid.
2288 if Is_Local_Variable_Reference (Lhs)
2289 and then not Is_Known_Valid (Entity (Lhs))
2290 and then In_Unconditional_Context (N)
2292 if Do_Range_Check (Rhs)
2293 or else Etype (Lhs) = Etype (Rhs)
2295 Set_Is_Known_Valid (Entity (Lhs), True);
2299 -- Case where right side may be invalid in the sense of the RM
2300 -- reference above. The RM does not require that we check for the
2301 -- validity on an assignment, but it does require that the assignment
2302 -- of an invalid value not cause erroneous behavior.
2304 -- The general approach in GNAT is to use the Is_Known_Valid flag
2305 -- to avoid the need for validity checking on assignments. However
2306 -- in some cases, we have to do validity checking in order to make
2307 -- sure that the setting of this flag is correct.
2310 -- Validate right side if we are validating copies
2312 if Validity_Checks_On
2313 and then Validity_Check_Copies
2315 -- Skip this if left hand side is an array or record component
2316 -- and elementary component validity checks are suppressed.
2318 if Nkind_In (Lhs, N_Selected_Component, N_Indexed_Component)
2319 and then not Validity_Check_Components
2326 -- We can propagate this to the left side where appropriate
2328 if Is_Local_Variable_Reference (Lhs)
2329 and then not Is_Known_Valid (Entity (Lhs))
2330 and then In_Unconditional_Context (N)
2332 Set_Is_Known_Valid (Entity (Lhs), True);
2335 -- Otherwise check to see what should be done
2337 -- If left side is a local variable, then we just set its flag to
2338 -- indicate that its value may no longer be valid, since we are
2339 -- copying a potentially invalid value.
2341 elsif Is_Local_Variable_Reference (Lhs) then
2342 Set_Is_Known_Valid (Entity (Lhs), False);
2344 -- Check for case of a nonlocal variable on the left side which
2345 -- is currently known to be valid. In this case, we simply ensure
2346 -- that the right side is valid. We only play the game of copying
2347 -- validity status for local variables, since we are doing this
2348 -- statically, not by tracing the full flow graph.
2350 elsif Is_Entity_Name (Lhs)
2351 and then Is_Known_Valid (Entity (Lhs))
2353 -- Note: If Validity_Checking mode is set to none, we ignore
2354 -- the Ensure_Valid call so don't worry about that case here.
2358 -- In all other cases, we can safely copy an invalid value without
2359 -- worrying about the status of the left side. Since it is not a
2360 -- variable reference it will not be considered
2361 -- as being known to be valid in any case.
2370 when RE_Not_Available =>
2372 end Expand_N_Assignment_Statement;
2374 ------------------------------
2375 -- Expand_N_Block_Statement --
2376 ------------------------------
2378 -- Encode entity names defined in block statement
2380 procedure Expand_N_Block_Statement (N : Node_Id) is
2382 Qualify_Entity_Names (N);
2383 end Expand_N_Block_Statement;
2385 -----------------------------
2386 -- Expand_N_Case_Statement --
2387 -----------------------------
2389 procedure Expand_N_Case_Statement (N : Node_Id) is
2390 Loc : constant Source_Ptr := Sloc (N);
2391 Expr : constant Node_Id := Expression (N);
2399 -- Check for the situation where we know at compile time which branch
2402 if Compile_Time_Known_Value (Expr) then
2403 Alt := Find_Static_Alternative (N);
2405 Process_Statements_For_Controlled_Objects (Alt);
2407 -- Move statements from this alternative after the case statement.
2408 -- They are already analyzed, so will be skipped by the analyzer.
2410 Insert_List_After (N, Statements (Alt));
2412 -- That leaves the case statement as a shell. So now we can kill all
2413 -- other alternatives in the case statement.
2415 Kill_Dead_Code (Expression (N));
2421 -- Loop through case alternatives, skipping pragmas, and skipping
2422 -- the one alternative that we select (and therefore retain).
2424 Dead_Alt := First (Alternatives (N));
2425 while Present (Dead_Alt) loop
2427 and then Nkind (Dead_Alt) = N_Case_Statement_Alternative
2429 Kill_Dead_Code (Statements (Dead_Alt), Warn_On_Deleted_Code);
2436 Rewrite (N, Make_Null_Statement (Loc));
2440 -- Here if the choice is not determined at compile time
2443 Last_Alt : constant Node_Id := Last (Alternatives (N));
2445 Others_Present : Boolean;
2446 Others_Node : Node_Id;
2448 Then_Stms : List_Id;
2449 Else_Stms : List_Id;
2452 if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
2453 Others_Present := True;
2454 Others_Node := Last_Alt;
2456 Others_Present := False;
2459 -- First step is to worry about possible invalid argument. The RM
2460 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2461 -- outside the base range), then Constraint_Error must be raised.
2463 -- Case of validity check required (validity checks are on, the
2464 -- expression is not known to be valid, and the case statement
2465 -- comes from source -- no need to validity check internally
2466 -- generated case statements).
2468 if Validity_Check_Default then
2469 Ensure_Valid (Expr);
2472 -- If there is only a single alternative, just replace it with the
2473 -- sequence of statements since obviously that is what is going to
2474 -- be executed in all cases.
2476 Len := List_Length (Alternatives (N));
2480 -- We still need to evaluate the expression if it has any side
2483 Remove_Side_Effects (Expression (N));
2485 Alt := First (Alternatives (N));
2487 Process_Statements_For_Controlled_Objects (Alt);
2488 Insert_List_After (N, Statements (Alt));
2490 -- That leaves the case statement as a shell. The alternative that
2491 -- will be executed is reset to a null list. So now we can kill
2492 -- the entire case statement.
2494 Kill_Dead_Code (Expression (N));
2495 Rewrite (N, Make_Null_Statement (Loc));
2498 -- An optimization. If there are only two alternatives, and only
2499 -- a single choice, then rewrite the whole case statement as an
2500 -- if statement, since this can result in subsequent optimizations.
2501 -- This helps not only with case statements in the source of a
2502 -- simple form, but also with generated code (discriminant check
2503 -- functions in particular)
2506 Chlist := Discrete_Choices (First (Alternatives (N)));
2508 if List_Length (Chlist) = 1 then
2509 Choice := First (Chlist);
2511 Then_Stms := Statements (First (Alternatives (N)));
2512 Else_Stms := Statements (Last (Alternatives (N)));
2514 -- For TRUE, generate "expression", not expression = true
2516 if Nkind (Choice) = N_Identifier
2517 and then Entity (Choice) = Standard_True
2519 Cond := Expression (N);
2521 -- For FALSE, generate "expression" and switch then/else
2523 elsif Nkind (Choice) = N_Identifier
2524 and then Entity (Choice) = Standard_False
2526 Cond := Expression (N);
2527 Else_Stms := Statements (First (Alternatives (N)));
2528 Then_Stms := Statements (Last (Alternatives (N)));
2530 -- For a range, generate "expression in range"
2532 elsif Nkind (Choice) = N_Range
2533 or else (Nkind (Choice) = N_Attribute_Reference
2534 and then Attribute_Name (Choice) = Name_Range)
2535 or else (Is_Entity_Name (Choice)
2536 and then Is_Type (Entity (Choice)))
2537 or else Nkind (Choice) = N_Subtype_Indication
2541 Left_Opnd => Expression (N),
2542 Right_Opnd => Relocate_Node (Choice));
2544 -- For any other subexpression "expression = value"
2549 Left_Opnd => Expression (N),
2550 Right_Opnd => Relocate_Node (Choice));
2553 -- Now rewrite the case as an IF
2556 Make_If_Statement (Loc,
2558 Then_Statements => Then_Stms,
2559 Else_Statements => Else_Stms));
2565 -- If the last alternative is not an Others choice, replace it with
2566 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2567 -- the modified case statement, since it's only effect would be to
2568 -- compute the contents of the Others_Discrete_Choices which is not
2569 -- needed by the back end anyway.
2571 -- The reason we do this is that the back end always needs some
2572 -- default for a switch, so if we have not supplied one in the
2573 -- processing above for validity checking, then we need to supply
2576 if not Others_Present then
2577 Others_Node := Make_Others_Choice (Sloc (Last_Alt));
2578 Set_Others_Discrete_Choices
2579 (Others_Node, Discrete_Choices (Last_Alt));
2580 Set_Discrete_Choices (Last_Alt, New_List (Others_Node));
2583 Alt := First (Alternatives (N));
2585 and then Nkind (Alt) = N_Case_Statement_Alternative
2587 Process_Statements_For_Controlled_Objects (Alt);
2591 end Expand_N_Case_Statement;
2593 -----------------------------
2594 -- Expand_N_Exit_Statement --
2595 -----------------------------
2597 -- The only processing required is to deal with a possible C/Fortran
2598 -- boolean value used as the condition for the exit statement.
2600 procedure Expand_N_Exit_Statement (N : Node_Id) is
2602 Adjust_Condition (Condition (N));
2603 end Expand_N_Exit_Statement;
2605 -----------------------------
2606 -- Expand_N_Goto_Statement --
2607 -----------------------------
2609 -- Add poll before goto if polling active
2611 procedure Expand_N_Goto_Statement (N : Node_Id) is
2613 Generate_Poll_Call (N);
2614 end Expand_N_Goto_Statement;
2616 ---------------------------
2617 -- Expand_N_If_Statement --
2618 ---------------------------
2620 -- First we deal with the case of C and Fortran convention boolean values,
2621 -- with zero/non-zero semantics.
2623 -- Second, we deal with the obvious rewriting for the cases where the
2624 -- condition of the IF is known at compile time to be True or False.
2626 -- Third, we remove elsif parts which have non-empty Condition_Actions and
2627 -- rewrite as independent if statements. For example:
2638 -- <<condition actions of y>>
2644 -- This rewriting is needed if at least one elsif part has a non-empty
2645 -- Condition_Actions list. We also do the same processing if there is a
2646 -- constant condition in an elsif part (in conjunction with the first
2647 -- processing step mentioned above, for the recursive call made to deal
2648 -- with the created inner if, this deals with properly optimizing the
2649 -- cases of constant elsif conditions).
2651 procedure Expand_N_If_Statement (N : Node_Id) is
2652 Loc : constant Source_Ptr := Sloc (N);
2657 Warn_If_Deleted : constant Boolean :=
2658 Warn_On_Deleted_Code and then Comes_From_Source (N);
2659 -- Indicates whether we want warnings when we delete branches of the
2660 -- if statement based on constant condition analysis. We never want
2661 -- these warnings for expander generated code.
2664 Process_Statements_For_Controlled_Objects (N);
2666 Adjust_Condition (Condition (N));
2668 -- The following loop deals with constant conditions for the IF. We
2669 -- need a loop because as we eliminate False conditions, we grab the
2670 -- first elsif condition and use it as the primary condition.
2672 while Compile_Time_Known_Value (Condition (N)) loop
2674 -- If condition is True, we can simply rewrite the if statement now
2675 -- by replacing it by the series of then statements.
2677 if Is_True (Expr_Value (Condition (N))) then
2679 -- All the else parts can be killed
2681 Kill_Dead_Code (Elsif_Parts (N), Warn_If_Deleted);
2682 Kill_Dead_Code (Else_Statements (N), Warn_If_Deleted);
2684 Hed := Remove_Head (Then_Statements (N));
2685 Insert_List_After (N, Then_Statements (N));
2689 -- If condition is False, then we can delete the condition and
2690 -- the Then statements
2693 -- We do not delete the condition if constant condition warnings
2694 -- are enabled, since otherwise we end up deleting the desired
2695 -- warning. Of course the backend will get rid of this True/False
2696 -- test anyway, so nothing is lost here.
2698 if not Constant_Condition_Warnings then
2699 Kill_Dead_Code (Condition (N));
2702 Kill_Dead_Code (Then_Statements (N), Warn_If_Deleted);
2704 -- If there are no elsif statements, then we simply replace the
2705 -- entire if statement by the sequence of else statements.
2707 if No (Elsif_Parts (N)) then
2708 if No (Else_Statements (N))
2709 or else Is_Empty_List (Else_Statements (N))
2712 Make_Null_Statement (Sloc (N)));
2714 Hed := Remove_Head (Else_Statements (N));
2715 Insert_List_After (N, Else_Statements (N));
2721 -- If there are elsif statements, the first of them becomes the
2722 -- if/then section of the rebuilt if statement This is the case
2723 -- where we loop to reprocess this copied condition.
2726 Hed := Remove_Head (Elsif_Parts (N));
2727 Insert_Actions (N, Condition_Actions (Hed));
2728 Set_Condition (N, Condition (Hed));
2729 Set_Then_Statements (N, Then_Statements (Hed));
2731 -- Hed might have been captured as the condition determining
2732 -- the current value for an entity. Now it is detached from
2733 -- the tree, so a Current_Value pointer in the condition might
2734 -- need to be updated.
2736 Set_Current_Value_Condition (N);
2738 if Is_Empty_List (Elsif_Parts (N)) then
2739 Set_Elsif_Parts (N, No_List);
2745 -- Loop through elsif parts, dealing with constant conditions and
2746 -- possible expression actions that are present.
2748 if Present (Elsif_Parts (N)) then
2749 E := First (Elsif_Parts (N));
2750 while Present (E) loop
2751 Process_Statements_For_Controlled_Objects (E);
2753 Adjust_Condition (Condition (E));
2755 -- If there are condition actions, then rewrite the if statement
2756 -- as indicated above. We also do the same rewrite for a True or
2757 -- False condition. The further processing of this constant
2758 -- condition is then done by the recursive call to expand the
2759 -- newly created if statement
2761 if Present (Condition_Actions (E))
2762 or else Compile_Time_Known_Value (Condition (E))
2764 -- Note this is not an implicit if statement, since it is part
2765 -- of an explicit if statement in the source (or of an implicit
2766 -- if statement that has already been tested).
2769 Make_If_Statement (Sloc (E),
2770 Condition => Condition (E),
2771 Then_Statements => Then_Statements (E),
2772 Elsif_Parts => No_List,
2773 Else_Statements => Else_Statements (N));
2775 -- Elsif parts for new if come from remaining elsif's of parent
2777 while Present (Next (E)) loop
2778 if No (Elsif_Parts (New_If)) then
2779 Set_Elsif_Parts (New_If, New_List);
2782 Append (Remove_Next (E), Elsif_Parts (New_If));
2785 Set_Else_Statements (N, New_List (New_If));
2787 if Present (Condition_Actions (E)) then
2788 Insert_List_Before (New_If, Condition_Actions (E));
2793 if Is_Empty_List (Elsif_Parts (N)) then
2794 Set_Elsif_Parts (N, No_List);
2800 -- No special processing for that elsif part, move to next
2808 -- Some more optimizations applicable if we still have an IF statement
2810 if Nkind (N) /= N_If_Statement then
2814 -- Another optimization, special cases that can be simplified
2816 -- if expression then
2822 -- can be changed to:
2824 -- return expression;
2828 -- if expression then
2834 -- can be changed to:
2836 -- return not (expression);
2838 -- Only do these optimizations if we are at least at -O1 level and
2839 -- do not do them if control flow optimizations are suppressed.
2841 if Optimization_Level > 0
2842 and then not Opt.Suppress_Control_Flow_Optimizations
2844 if Nkind (N) = N_If_Statement
2845 and then No (Elsif_Parts (N))
2846 and then Present (Else_Statements (N))
2847 and then List_Length (Then_Statements (N)) = 1
2848 and then List_Length (Else_Statements (N)) = 1
2851 Then_Stm : constant Node_Id := First (Then_Statements (N));
2852 Else_Stm : constant Node_Id := First (Else_Statements (N));
2855 if Nkind (Then_Stm) = N_Simple_Return_Statement
2857 Nkind (Else_Stm) = N_Simple_Return_Statement
2860 Then_Expr : constant Node_Id := Expression (Then_Stm);
2861 Else_Expr : constant Node_Id := Expression (Else_Stm);
2864 if Nkind (Then_Expr) = N_Identifier
2866 Nkind (Else_Expr) = N_Identifier
2868 if Entity (Then_Expr) = Standard_True
2869 and then Entity (Else_Expr) = Standard_False
2872 Make_Simple_Return_Statement (Loc,
2873 Expression => Relocate_Node (Condition (N))));
2877 elsif Entity (Then_Expr) = Standard_False
2878 and then Entity (Else_Expr) = Standard_True
2881 Make_Simple_Return_Statement (Loc,
2885 Relocate_Node (Condition (N)))));
2895 end Expand_N_If_Statement;
2897 --------------------------
2898 -- Expand_Iterator_Loop --
2899 --------------------------
2901 procedure Expand_Iterator_Loop (N : Node_Id) is
2902 Isc : constant Node_Id := Iteration_Scheme (N);
2903 I_Spec : constant Node_Id := Iterator_Specification (Isc);
2904 Id : constant Entity_Id := Defining_Identifier (I_Spec);
2905 Loc : constant Source_Ptr := Sloc (N);
2907 Container : constant Node_Id := Name (I_Spec);
2908 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
2910 Iterator : Entity_Id;
2912 Stats : List_Id := Statements (N);
2915 -- Processing for arrays
2917 if Is_Array_Type (Container_Typ) then
2919 -- for Element of Array loop
2921 -- This case requires an internally generated cursor to iterate over
2924 if Of_Present (I_Spec) then
2925 Iterator := Make_Temporary (Loc, 'C');
2928 -- Element : Component_Type renames Container (Iterator);
2931 Make_Object_Renaming_Declaration (Loc,
2932 Defining_Identifier => Id,
2934 New_Reference_To (Component_Type (Container_Typ), Loc),
2936 Make_Indexed_Component (Loc,
2937 Prefix => Relocate_Node (Container),
2938 Expressions => New_List (
2939 New_Reference_To (Iterator, Loc)))));
2941 -- for Index in Array loop
2943 -- This case utilizes the already given iterator name
2950 -- for Iterator in [reverse] Container'Range loop
2951 -- Element : Component_Type renames Container (Iterator);
2952 -- -- for the "of" form
2954 -- <original loop statements>
2958 Make_Loop_Statement (Loc,
2960 Make_Iteration_Scheme (Loc,
2961 Loop_Parameter_Specification =>
2962 Make_Loop_Parameter_Specification (Loc,
2963 Defining_Identifier => Iterator,
2964 Discrete_Subtype_Definition =>
2965 Make_Attribute_Reference (Loc,
2966 Prefix => Relocate_Node (Container),
2967 Attribute_Name => Name_Range),
2968 Reverse_Present => Reverse_Present (I_Spec))),
2969 Statements => Stats,
2970 End_Label => Empty);
2972 -- Processing for containers
2975 -- For an "of" iterator the name is a container expression, which
2976 -- is transformed into a call to the default iterator.
2978 -- For an iterator of the form "in" the name is a function call
2979 -- that delivers an iterator type.
2981 -- In both cases, analysis of the iterator has introduced an object
2982 -- declaration to capture the domain, so that Container is an entity.
2984 -- The for loop is expanded into a while loop which uses a container
2985 -- specific cursor to desgnate each element.
2987 -- Iter : Iterator_Type := Container.Iterate;
2988 -- Cursor : Cursor_type := First (Iter);
2989 -- while Has_Element (Iter) loop
2991 -- -- the block is added when Element_Type is controlled
2993 -- Obj : Pack.Element_Type := Element (Cursor);
2994 -- -- for the "of" loop form
2996 -- <original loop statements>
2999 -- Cursor := Iter.Next (Cursor);
3002 -- If "reverse" is present, then the initialization of the cursor
3003 -- uses Last and the step becomes Prev. Pack is the name of the
3004 -- scope where the container package is instantiated.
3007 Element_Type : constant Entity_Id := Etype (Id);
3008 Iter_Type : Entity_Id;
3011 Name_Init : Name_Id;
3012 Name_Step : Name_Id;
3015 -- The type of the iterator is the return type of the Iterate
3016 -- function used. For the "of" form this is the default iterator
3017 -- for the type, otherwise it is the type of the explicit
3018 -- function used in the iterator specification. The most common
3019 -- case will be an Iterate function in the container package.
3021 -- The primitive operations of the container type may not be
3022 -- use-visible, so we introduce the name of the enclosing package
3023 -- in the declarations below. The Iterator type is declared in a
3024 -- an instance within the container package itself.
3026 -- If the container type is a derived type, the cursor type is
3027 -- found in the package of the parent type.
3029 if Is_Derived_Type (Container_Typ) then
3030 Pack := Scope (Root_Type (Container_Typ));
3032 Pack := Scope (Container_Typ);
3035 Iter_Type := Etype (Name (I_Spec));
3037 if Is_Iterator (Iter_Type) then
3038 Pack := Scope (Pack);
3041 -- The "of" case uses an internally generated cursor whose type
3042 -- is found in the container package. The domain of iteration
3043 -- is expanded into a call to the default Iterator function, but
3044 -- this expansion does not take place in a quantifier expressions
3045 -- that are analyzed with expansion disabled, and in that case the
3046 -- type of the iterator must be obtained from the aspect.
3048 if Of_Present (I_Spec) then
3050 Default_Iter : constant Entity_Id :=
3054 Aspect_Default_Iterator));
3056 Container_Arg : Node_Id;
3060 Cursor := Make_Temporary (Loc, 'I');
3062 if Is_Iterator (Iter_Type) then
3066 Iter_Type := Etype (Default_Iter);
3068 -- Rewrite domain of iteration as a call to the default
3069 -- iterator for the container type. If the container is
3070 -- a derived type and the aspect is inherited, convert
3071 -- container to parent type. The Cursor type is also
3072 -- inherited from the scope of the parent.
3074 if Base_Type (Etype (Container)) =
3075 Base_Type (Etype (First_Formal (Default_Iter)))
3077 Container_Arg := New_Copy_Tree (Container);
3081 Make_Type_Conversion (Loc,
3084 (Etype (First_Formal (Default_Iter)), Loc),
3085 Expression => New_Copy_Tree (Container));
3088 Rewrite (Name (I_Spec),
3089 Make_Function_Call (Loc,
3090 Name => New_Occurrence_Of (Default_Iter, Loc),
3091 Parameter_Associations =>
3092 New_List (Container_Arg)));
3093 Analyze_And_Resolve (Name (I_Spec));
3096 -- Find cursor type in proper container package.
3098 Ent := First_Entity (Pack);
3099 while Present (Ent) loop
3100 if Chars (Ent) = Name_Cursor then
3101 Set_Etype (Cursor, Etype (Ent));
3108 -- Id : Element_Type renames Pack.Element (Cursor);
3111 Make_Object_Renaming_Declaration (Loc,
3112 Defining_Identifier => Id,
3114 New_Reference_To (Element_Type, Loc),
3116 Make_Indexed_Component (Loc,
3117 Prefix => Make_Selected_Component (Loc,
3118 Prefix => New_Reference_To (Pack, Loc),
3120 Make_Identifier (Loc, Chars => Name_Element)),
3122 New_List (New_Occurrence_Of (Cursor, Loc))));
3124 -- If the container holds controlled objects, wrap the loop
3125 -- statements and element renaming declaration with a block.
3126 -- This ensures that the result of Element (Iterator) is
3127 -- cleaned up after each iteration of the loop.
3129 if Needs_Finalization (Element_Type) then
3133 -- Id : Element_Type := Pack.Element (Iterator);
3135 -- <original loop statements>
3139 Make_Block_Statement (Loc,
3140 Declarations => New_List (Decl),
3141 Handled_Statement_Sequence =>
3142 Make_Handled_Sequence_Of_Statements (Loc,
3143 Statements => Stats)));
3145 -- Elements do not need finalization
3148 Prepend_To (Stats, Decl);
3152 -- X in Iterate (S) : type of iterator is type of explicitly
3153 -- given Iterate function, and the loop variable is the cursor.
3154 -- It will be assigned in the loop and must be a variable.
3158 Set_Ekind (Cursor, E_Variable);
3161 Iterator := Make_Temporary (Loc, 'I');
3163 -- Determine the advancement and initialization steps for the
3166 -- Must verify that the container has a reverse iterator ???
3168 if Reverse_Present (I_Spec) then
3169 Name_Init := Name_Last;
3170 Name_Step := Name_Previous;
3172 Name_Init := Name_First;
3173 Name_Step := Name_Next;
3176 -- For both iterator forms, add a call to the step operation to
3177 -- advance the cursor. Generate:
3179 -- Cursor := Iterator.Next (Cursor);
3183 -- Cursor := Next (Cursor);
3190 Make_Function_Call (Loc,
3192 Make_Selected_Component (Loc,
3193 Prefix => New_Reference_To (Iterator, Loc),
3194 Selector_Name => Make_Identifier (Loc, Name_Step)),
3195 Parameter_Associations => New_List (
3196 New_Reference_To (Cursor, Loc)));
3199 Make_Assignment_Statement (Loc,
3200 Name => New_Occurrence_Of (Cursor, Loc),
3201 Expression => Rhs));
3205 -- while Iterator.Has_Element loop
3210 Make_Loop_Statement (Loc,
3212 Make_Iteration_Scheme (Loc,
3214 Make_Function_Call (Loc,
3216 Make_Selected_Component (Loc,
3217 Prefix => New_Occurrence_Of (Pack, Loc),
3219 Make_Identifier (Loc, Name_Has_Element)),
3221 Parameter_Associations =>
3223 New_Reference_To (Cursor, Loc)))),
3224 Statements => Stats,
3225 End_Label => Empty);
3227 -- Create the declarations for Iterator and cursor and insert then
3228 -- before the source loop. Given that the domain of iteration is
3229 -- already an entity, the iterator is just a renaming of that
3230 -- entity. Possible optimization ???
3233 -- I : Iterator_Type renames Container;
3234 -- C : Pack.Cursor_Type := Container.[First | Last];
3242 Make_Object_Renaming_Declaration (Loc,
3243 Defining_Identifier => Iterator,
3244 Subtype_Mark => New_Occurrence_Of (Iter_Type, Loc),
3245 Name => Relocate_Node (Name (I_Spec)));
3247 -- Create declaration for cursor
3250 Make_Object_Declaration (Loc,
3251 Defining_Identifier => Cursor,
3252 Object_Definition =>
3253 New_Occurrence_Of (Etype (Cursor), Loc),
3255 Make_Selected_Component (Loc,
3256 Prefix => New_Reference_To (Iterator, Loc),
3258 Make_Identifier (Loc, Name_Init)));
3260 Set_Assignment_OK (Decl2);
3262 Insert_Actions (N, New_List (Decl1, Decl2));
3265 -- The Iterator is not modified in the source, but of course will
3266 -- be updated in the generated code. Indicate that it is actually
3267 -- set to prevent spurious warnings.
3269 Set_Never_Set_In_Source (Iterator, False);
3271 -- If the range of iteration is given by a function call that
3272 -- returns a container, the finalization actions have been saved
3273 -- in the Condition_Actions of the iterator. Insert them now at
3274 -- the head of the loop.
3276 if Present (Condition_Actions (Isc)) then
3277 Insert_List_Before (N, Condition_Actions (Isc));
3282 Rewrite (N, New_Loop);
3284 end Expand_Iterator_Loop;
3286 -----------------------------
3287 -- Expand_N_Loop_Statement --
3288 -----------------------------
3290 -- 1. Remove null loop entirely
3291 -- 2. Deal with while condition for C/Fortran boolean
3292 -- 3. Deal with loops with a non-standard enumeration type range
3293 -- 4. Deal with while loops where Condition_Actions is set
3294 -- 5. Deal with loops over predicated subtypes
3295 -- 6. Deal with loops with iterators over arrays and containers
3296 -- 7. Insert polling call if required
3298 procedure Expand_N_Loop_Statement (N : Node_Id) is
3299 Loc : constant Source_Ptr := Sloc (N);
3300 Isc : constant Node_Id := Iteration_Scheme (N);
3305 if Is_Null_Loop (N) then
3306 Rewrite (N, Make_Null_Statement (Loc));
3310 Process_Statements_For_Controlled_Objects (N);
3312 -- Deal with condition for C/Fortran Boolean
3314 if Present (Isc) then
3315 Adjust_Condition (Condition (Isc));
3318 -- Generate polling call
3320 if Is_Non_Empty_List (Statements (N)) then
3321 Generate_Poll_Call (First (Statements (N)));
3324 -- Nothing more to do for plain loop with no iteration scheme
3329 -- Case of for loop (Loop_Parameter_Specification present)
3331 -- Note: we do not have to worry about validity checking of the for loop
3332 -- range bounds here, since they were frozen with constant declarations
3333 -- and it is during that process that the validity checking is done.
3335 elsif Present (Loop_Parameter_Specification (Isc)) then
3337 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
3338 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
3339 Ltype : constant Entity_Id := Etype (Loop_Id);
3340 Btype : constant Entity_Id := Base_Type (Ltype);
3345 -- Deal with loop over predicates
3347 if Is_Discrete_Type (Ltype)
3348 and then Present (Predicate_Function (Ltype))
3350 Expand_Predicated_Loop (N);
3352 -- Handle the case where we have a for loop with the range type
3353 -- being an enumeration type with non-standard representation.
3354 -- In this case we expand:
3356 -- for x in [reverse] a .. b loop
3362 -- for xP in [reverse] integer
3363 -- range etype'Pos (a) .. etype'Pos (b)
3366 -- x : constant etype := Pos_To_Rep (xP);
3372 elsif Is_Enumeration_Type (Btype)
3373 and then Present (Enum_Pos_To_Rep (Btype))
3376 Make_Defining_Identifier (Loc,
3377 Chars => New_External_Name (Chars (Loop_Id), 'P'));
3379 -- If the type has a contiguous representation, successive
3380 -- values can be generated as offsets from the first literal.
3382 if Has_Contiguous_Rep (Btype) then
3384 Unchecked_Convert_To (Btype,
3387 Make_Integer_Literal (Loc,
3388 Enumeration_Rep (First_Literal (Btype))),
3389 Right_Opnd => New_Reference_To (New_Id, Loc)));
3391 -- Use the constructed array Enum_Pos_To_Rep
3394 Make_Indexed_Component (Loc,
3396 New_Reference_To (Enum_Pos_To_Rep (Btype), Loc),
3398 New_List (New_Reference_To (New_Id, Loc)));
3402 Make_Loop_Statement (Loc,
3403 Identifier => Identifier (N),
3406 Make_Iteration_Scheme (Loc,
3407 Loop_Parameter_Specification =>
3408 Make_Loop_Parameter_Specification (Loc,
3409 Defining_Identifier => New_Id,
3410 Reverse_Present => Reverse_Present (LPS),
3412 Discrete_Subtype_Definition =>
3413 Make_Subtype_Indication (Loc,
3416 New_Reference_To (Standard_Natural, Loc),
3419 Make_Range_Constraint (Loc,
3424 Make_Attribute_Reference (Loc,
3426 New_Reference_To (Btype, Loc),
3428 Attribute_Name => Name_Pos,
3430 Expressions => New_List (
3432 (Type_Low_Bound (Ltype)))),
3435 Make_Attribute_Reference (Loc,
3437 New_Reference_To (Btype, Loc),
3439 Attribute_Name => Name_Pos,
3441 Expressions => New_List (
3446 Statements => New_List (
3447 Make_Block_Statement (Loc,
3448 Declarations => New_List (
3449 Make_Object_Declaration (Loc,
3450 Defining_Identifier => Loop_Id,
3451 Constant_Present => True,
3452 Object_Definition =>
3453 New_Reference_To (Ltype, Loc),
3454 Expression => Expr)),
3456 Handled_Statement_Sequence =>
3457 Make_Handled_Sequence_Of_Statements (Loc,
3458 Statements => Statements (N)))),
3460 End_Label => End_Label (N)));
3462 -- The loop parameter's entity must be removed from the loop
3463 -- scope's entity list, since it will now be located in the
3464 -- new block scope. Any other entities already associated with
3465 -- the loop scope, such as the loop parameter's subtype, will
3468 pragma Assert (First_Entity (Scope (Loop_Id)) = Loop_Id);
3469 Set_First_Entity (Scope (Loop_Id), Next_Entity (Loop_Id));
3471 if Last_Entity (Scope (Loop_Id)) = Loop_Id then
3472 Set_Last_Entity (Scope (Loop_Id), Empty);
3477 -- Nothing to do with other cases of for loops
3484 -- Second case, if we have a while loop with Condition_Actions set, then
3485 -- we change it into a plain loop:
3494 -- <<condition actions>>
3500 and then Present (Condition_Actions (Isc))
3501 and then Present (Condition (Isc))
3508 Make_Exit_Statement (Sloc (Condition (Isc)),
3510 Make_Op_Not (Sloc (Condition (Isc)),
3511 Right_Opnd => Condition (Isc)));
3513 Prepend (ES, Statements (N));
3514 Insert_List_Before (ES, Condition_Actions (Isc));
3516 -- This is not an implicit loop, since it is generated in response
3517 -- to the loop statement being processed. If this is itself
3518 -- implicit, the restriction has already been checked. If not,
3519 -- it is an explicit loop.
3522 Make_Loop_Statement (Sloc (N),
3523 Identifier => Identifier (N),
3524 Statements => Statements (N),
3525 End_Label => End_Label (N)));
3530 -- Here to deal with iterator case
3533 and then Present (Iterator_Specification (Isc))
3535 Expand_Iterator_Loop (N);
3537 end Expand_N_Loop_Statement;
3539 ----------------------------
3540 -- Expand_Predicated_Loop --
3541 ----------------------------
3543 -- Note: the expander can handle generation of loops over predicated
3544 -- subtypes for both the dynamic and static cases. Depending on what
3545 -- we decide is allowed in Ada 2012 mode and/or extensions allowed
3546 -- mode, the semantic analyzer may disallow one or both forms.
3548 procedure Expand_Predicated_Loop (N : Node_Id) is
3549 Loc : constant Source_Ptr := Sloc (N);
3550 Isc : constant Node_Id := Iteration_Scheme (N);
3551 LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
3552 Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
3553 Ltype : constant Entity_Id := Etype (Loop_Id);
3554 Stat : constant List_Id := Static_Predicate (Ltype);
3555 Stmts : constant List_Id := Statements (N);
3558 -- Case of iteration over non-static predicate, should not be possible
3559 -- since this is not allowed by the semantics and should have been
3560 -- caught during analysis of the loop statement.
3563 raise Program_Error;
3565 -- If the predicate list is empty, that corresponds to a predicate of
3566 -- False, in which case the loop won't run at all, and we rewrite the
3567 -- entire loop as a null statement.
3569 elsif Is_Empty_List (Stat) then
3570 Rewrite (N, Make_Null_Statement (Loc));
3573 -- For expansion over a static predicate we generate the following
3576 -- J : Ltype := min-val;
3581 -- when endpoint => J := startpoint;
3582 -- when endpoint => J := startpoint;
3584 -- when max-val => exit;
3585 -- when others => J := Lval'Succ (J);
3590 -- To make this a little clearer, let's take a specific example:
3592 -- type Int is range 1 .. 10;
3593 -- subtype L is Int with
3594 -- predicate => L in 3 | 10 | 5 .. 7;
3596 -- for L in StaticP loop
3597 -- Put_Line ("static:" & J'Img);
3600 -- In this case, the loop is transformed into
3607 -- when 3 => J := 5;
3608 -- when 7 => J := 10;
3610 -- when others => J := L'Succ (J);
3616 Static_Predicate : declare
3623 function Lo_Val (N : Node_Id) return Node_Id;
3624 -- Given static expression or static range, returns an identifier
3625 -- whose value is the low bound of the expression value or range.
3627 function Hi_Val (N : Node_Id) return Node_Id;
3628 -- Given static expression or static range, returns an identifier
3629 -- whose value is the high bound of the expression value or range.
3635 function Hi_Val (N : Node_Id) return Node_Id is
3637 if Is_Static_Expression (N) then
3638 return New_Copy (N);
3640 pragma Assert (Nkind (N) = N_Range);
3641 return New_Copy (High_Bound (N));
3649 function Lo_Val (N : Node_Id) return Node_Id is
3651 if Is_Static_Expression (N) then
3652 return New_Copy (N);
3654 pragma Assert (Nkind (N) = N_Range);
3655 return New_Copy (Low_Bound (N));
3659 -- Start of processing for Static_Predicate
3662 -- Convert loop identifier to normal variable and reanalyze it so
3663 -- that this conversion works. We have to use the same defining
3664 -- identifier, since there may be references in the loop body.
3666 Set_Analyzed (Loop_Id, False);
3667 Set_Ekind (Loop_Id, E_Variable);
3669 -- Loop to create branches of case statement
3673 while Present (P) loop
3674 if No (Next (P)) then
3675 S := Make_Exit_Statement (Loc);
3678 Make_Assignment_Statement (Loc,
3679 Name => New_Occurrence_Of (Loop_Id, Loc),
3680 Expression => Lo_Val (Next (P)));
3681 Set_Suppress_Assignment_Checks (S);
3685 Make_Case_Statement_Alternative (Loc,
3686 Statements => New_List (S),
3687 Discrete_Choices => New_List (Hi_Val (P))));
3692 -- Add others choice
3695 Make_Assignment_Statement (Loc,
3696 Name => New_Occurrence_Of (Loop_Id, Loc),
3698 Make_Attribute_Reference (Loc,
3699 Prefix => New_Occurrence_Of (Ltype, Loc),
3700 Attribute_Name => Name_Succ,
3701 Expressions => New_List (
3702 New_Occurrence_Of (Loop_Id, Loc))));
3703 Set_Suppress_Assignment_Checks (S);
3706 Make_Case_Statement_Alternative (Loc,
3707 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
3708 Statements => New_List (S)));
3710 -- Construct case statement and append to body statements
3713 Make_Case_Statement (Loc,
3714 Expression => New_Occurrence_Of (Loop_Id, Loc),
3715 Alternatives => Alts);
3716 Append_To (Stmts, Cstm);
3721 Make_Object_Declaration (Loc,
3722 Defining_Identifier => Loop_Id,
3723 Object_Definition => New_Occurrence_Of (Ltype, Loc),
3724 Expression => Lo_Val (First (Stat)));
3725 Set_Suppress_Assignment_Checks (D);
3728 Make_Block_Statement (Loc,
3729 Declarations => New_List (D),
3730 Handled_Statement_Sequence =>
3731 Make_Handled_Sequence_Of_Statements (Loc,
3732 Statements => New_List (
3733 Make_Loop_Statement (Loc,
3734 Statements => Stmts,
3735 End_Label => Empty)))));
3738 end Static_Predicate;
3740 end Expand_Predicated_Loop;
3742 ------------------------------
3743 -- Make_Tag_Ctrl_Assignment --
3744 ------------------------------
3746 function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id is
3747 Asn : constant Node_Id := Relocate_Node (N);
3748 L : constant Node_Id := Name (N);
3749 Loc : constant Source_Ptr := Sloc (N);
3750 Res : constant List_Id := New_List;
3751 T : constant Entity_Id := Underlying_Type (Etype (L));
3753 Comp_Asn : constant Boolean := Is_Fully_Repped_Tagged_Type (T);
3754 Ctrl_Act : constant Boolean := Needs_Finalization (T)
3755 and then not No_Ctrl_Actions (N);
3756 Save_Tag : constant Boolean := Is_Tagged_Type (T)
3757 and then not Comp_Asn
3758 and then not No_Ctrl_Actions (N)
3759 and then Tagged_Type_Expansion;
3760 -- Tags are not saved and restored when VM_Target because VM tags are
3761 -- represented implicitly in objects.
3763 Next_Id : Entity_Id;
3764 Prev_Id : Entity_Id;
3768 -- Finalize the target of the assignment when controlled
3770 -- We have two exceptions here:
3772 -- 1. If we are in an init proc since it is an initialization more
3773 -- than an assignment.
3775 -- 2. If the left-hand side is a temporary that was not initialized
3776 -- (or the parent part of a temporary since it is the case in
3777 -- extension aggregates). Such a temporary does not come from
3778 -- source. We must examine the original node for the prefix, because
3779 -- it may be a component of an entry formal, in which case it has
3780 -- been rewritten and does not appear to come from source either.
3782 -- Case of init proc
3784 if not Ctrl_Act then
3787 -- The left hand side is an uninitialized temporary object
3789 elsif Nkind (L) = N_Type_Conversion
3790 and then Is_Entity_Name (Expression (L))
3791 and then Nkind (Parent (Entity (Expression (L)))) =
3792 N_Object_Declaration
3793 and then No_Initialization (Parent (Entity (Expression (L))))
3800 (Obj_Ref => Duplicate_Subexpr_No_Checks (L),
3804 -- Save the Tag in a local variable Tag_Id
3807 Tag_Id := Make_Temporary (Loc, 'A');
3810 Make_Object_Declaration (Loc,
3811 Defining_Identifier => Tag_Id,
3812 Object_Definition => New_Reference_To (RTE (RE_Tag), Loc),
3814 Make_Selected_Component (Loc,
3815 Prefix => Duplicate_Subexpr_No_Checks (L),
3817 New_Reference_To (First_Tag_Component (T), Loc))));
3819 -- Otherwise Tag_Id is not used
3825 -- Save the Prev and Next fields on .NET/JVM. This is not needed on non
3826 -- VM targets since the fields are not part of the object.
3828 if VM_Target /= No_VM
3829 and then Is_Controlled (T)
3831 Prev_Id := Make_Temporary (Loc, 'P');
3832 Next_Id := Make_Temporary (Loc, 'N');
3835 -- Pnn : Root_Controlled_Ptr := Root_Controlled (L).Prev;
3838 Make_Object_Declaration (Loc,
3839 Defining_Identifier => Prev_Id,
3840 Object_Definition =>
3841 New_Reference_To (RTE (RE_Root_Controlled_Ptr), Loc),
3843 Make_Selected_Component (Loc,
3845 Unchecked_Convert_To
3846 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
3848 Make_Identifier (Loc, Name_Prev))));
3851 -- Nnn : Root_Controlled_Ptr := Root_Controlled (L).Next;
3854 Make_Object_Declaration (Loc,
3855 Defining_Identifier => Next_Id,
3856 Object_Definition =>
3857 New_Reference_To (RTE (RE_Root_Controlled_Ptr), Loc),
3859 Make_Selected_Component (Loc,
3861 Unchecked_Convert_To
3862 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
3864 Make_Identifier (Loc, Name_Next))));
3867 -- If the tagged type has a full rep clause, expand the assignment into
3868 -- component-wise assignments. Mark the node as unanalyzed in order to
3869 -- generate the proper code and propagate this scenario by setting a
3870 -- flag to avoid infinite recursion.
3873 Set_Analyzed (Asn, False);
3874 Set_Componentwise_Assignment (Asn, True);
3877 Append_To (Res, Asn);
3883 Make_Assignment_Statement (Loc,
3885 Make_Selected_Component (Loc,
3886 Prefix => Duplicate_Subexpr_No_Checks (L),
3888 New_Reference_To (First_Tag_Component (T), Loc)),
3889 Expression => New_Reference_To (Tag_Id, Loc)));
3892 -- Restore the Prev and Next fields on .NET/JVM
3894 if VM_Target /= No_VM
3895 and then Is_Controlled (T)
3898 -- Root_Controlled (L).Prev := Prev_Id;
3901 Make_Assignment_Statement (Loc,
3903 Make_Selected_Component (Loc,
3905 Unchecked_Convert_To
3906 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
3908 Make_Identifier (Loc, Name_Prev)),
3909 Expression => New_Reference_To (Prev_Id, Loc)));
3912 -- Root_Controlled (L).Next := Next_Id;
3915 Make_Assignment_Statement (Loc,
3917 Make_Selected_Component (Loc,
3919 Unchecked_Convert_To
3920 (RTE (RE_Root_Controlled), New_Copy_Tree (L)),
3921 Selector_Name => Make_Identifier (Loc, Name_Next)),
3922 Expression => New_Reference_To (Next_Id, Loc)));
3925 -- Adjust the target after the assignment when controlled (not in the
3926 -- init proc since it is an initialization more than an assignment).
3931 (Obj_Ref => Duplicate_Subexpr_Move_Checks (L),
3939 -- Could use comment here ???
3941 when RE_Not_Available =>
3943 end Make_Tag_Ctrl_Assignment;