1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, 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 Errout
; use Errout
;
32 with Exp_Aggr
; use Exp_Aggr
;
33 with Exp_Ch6
; use Exp_Ch6
;
34 with Exp_Ch7
; use Exp_Ch7
;
35 with Exp_Ch11
; use Exp_Ch11
;
36 with Exp_Dbug
; use Exp_Dbug
;
37 with Exp_Pakd
; use Exp_Pakd
;
38 with Exp_Tss
; use Exp_Tss
;
39 with Exp_Util
; use Exp_Util
;
40 with Namet
; use Namet
;
41 with Nlists
; use Nlists
;
42 with Nmake
; use Nmake
;
44 with Restrict
; use Restrict
;
45 with Rident
; use Rident
;
46 with Rtsfind
; use Rtsfind
;
47 with Sinfo
; use Sinfo
;
49 with Sem_Aux
; use Sem_Aux
;
50 with Sem_Ch3
; use Sem_Ch3
;
51 with Sem_Ch8
; use Sem_Ch8
;
52 with Sem_Ch13
; use Sem_Ch13
;
53 with Sem_Eval
; use Sem_Eval
;
54 with Sem_Res
; use Sem_Res
;
55 with Sem_Util
; use Sem_Util
;
56 with Snames
; use Snames
;
57 with Stand
; use Stand
;
58 with Stringt
; use Stringt
;
59 with Targparm
; use Targparm
;
60 with Tbuild
; use Tbuild
;
61 with Validsw
; use Validsw
;
63 package body Exp_Ch5
is
65 function Change_Of_Representation
(N
: Node_Id
) return Boolean;
66 -- Determine if the right hand side of assignment N is a type conversion
67 -- which requires a change of representation. Called only for the array
70 procedure Expand_Assign_Array
(N
: Node_Id
; Rhs
: Node_Id
);
71 -- N is an assignment which assigns an array value. This routine process
72 -- the various special cases and checks required for such assignments,
73 -- including change of representation. Rhs is normally simply the right
74 -- hand side of the assignment, except that if the right hand side is a
75 -- type conversion or a qualified expression, then the RHS is the actual
76 -- expression inside any such type conversions or qualifications.
78 function Expand_Assign_Array_Loop
85 Rev
: Boolean) return Node_Id
;
86 -- N is an assignment statement which assigns an array value. This routine
87 -- expands the assignment into a loop (or nested loops for the case of a
88 -- multi-dimensional array) to do the assignment component by component.
89 -- Larray and Rarray are the entities of the actual arrays on the left
90 -- hand and right hand sides. L_Type and R_Type are the types of these
91 -- arrays (which may not be the same, due to either sliding, or to a
92 -- change of representation case). Ndim is the number of dimensions and
93 -- the parameter Rev indicates if the loops run normally (Rev = False),
94 -- or reversed (Rev = True). The value returned is the constructed
95 -- loop statement. Auxiliary declarations are inserted before node N
96 -- using the standard Insert_Actions mechanism.
98 procedure Expand_Assign_Record
(N
: Node_Id
);
99 -- N is an assignment of a non-tagged record value. This routine handles
100 -- the case where the assignment must be made component by component,
101 -- either because the target is not byte aligned, or there is a change
102 -- of representation, or when we have a tagged type with a representation
103 -- clause (this last case is required because holes in the tagged type
104 -- might be filled with components from child types).
106 procedure Expand_Iterator_Loop
(N
: Node_Id
);
107 -- Expand loop over arrays and containers that uses the form "for X of C"
108 -- with an optional subtype mark, or "for Y in C".
110 procedure Expand_Iterator_Loop_Over_Array
(N
: Node_Id
);
111 -- Expand loop over arrays that uses the form "for X of C"
113 procedure Expand_Predicated_Loop
(N
: Node_Id
);
114 -- Expand for loop over predicated subtype
116 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
;
117 -- Generate the necessary code for controlled and tagged assignment, that
118 -- is to say, finalization of the target before, adjustment of the target
119 -- after and save and restore of the tag and finalization pointers which
120 -- are not 'part of the value' and must not be changed upon assignment. N
121 -- is the original Assignment node.
123 ------------------------------
124 -- Change_Of_Representation --
125 ------------------------------
127 function Change_Of_Representation
(N
: Node_Id
) return Boolean is
128 Rhs
: constant Node_Id
:= Expression
(N
);
131 Nkind
(Rhs
) = N_Type_Conversion
133 not Same_Representation
(Etype
(Rhs
), Etype
(Expression
(Rhs
)));
134 end Change_Of_Representation
;
136 -------------------------
137 -- Expand_Assign_Array --
138 -------------------------
140 -- There are two issues here. First, do we let Gigi do a block move, or
141 -- do we expand out into a loop? Second, we need to set the two flags
142 -- Forwards_OK and Backwards_OK which show whether the block move (or
143 -- corresponding loops) can be legitimately done in a forwards (low to
144 -- high) or backwards (high to low) manner.
146 procedure Expand_Assign_Array
(N
: Node_Id
; Rhs
: Node_Id
) is
147 Loc
: constant Source_Ptr
:= Sloc
(N
);
149 Lhs
: constant Node_Id
:= Name
(N
);
151 Act_Lhs
: constant Node_Id
:= Get_Referenced_Object
(Lhs
);
152 Act_Rhs
: Node_Id
:= Get_Referenced_Object
(Rhs
);
154 L_Type
: constant Entity_Id
:=
155 Underlying_Type
(Get_Actual_Subtype
(Act_Lhs
));
156 R_Type
: Entity_Id
:=
157 Underlying_Type
(Get_Actual_Subtype
(Act_Rhs
));
159 L_Slice
: constant Boolean := Nkind
(Act_Lhs
) = N_Slice
;
160 R_Slice
: constant Boolean := Nkind
(Act_Rhs
) = N_Slice
;
162 Crep
: constant Boolean := Change_Of_Representation
(N
);
167 Ndim
: constant Pos
:= Number_Dimensions
(L_Type
);
169 Loop_Required
: Boolean := False;
170 -- This switch is set to True if the array move must be done using
171 -- an explicit front end generated loop.
173 procedure Apply_Dereference
(Arg
: Node_Id
);
174 -- If the argument is an access to an array, and the assignment is
175 -- converted into a procedure call, apply explicit dereference.
177 function Has_Address_Clause
(Exp
: Node_Id
) return Boolean;
178 -- Test if Exp is a reference to an array whose declaration has
179 -- an address clause, or it is a slice of such an array.
181 function Is_Formal_Array
(Exp
: Node_Id
) return Boolean;
182 -- Test if Exp is a reference to an array which is either a formal
183 -- parameter or a slice of a formal parameter. These are the cases
184 -- where hidden aliasing can occur.
186 function Is_Non_Local_Array
(Exp
: Node_Id
) return Boolean;
187 -- Determine if Exp is a reference to an array variable which is other
188 -- than an object defined in the current scope, or a slice of such
189 -- an object. Such objects can be aliased to parameters (unlike local
190 -- array references).
192 -----------------------
193 -- Apply_Dereference --
194 -----------------------
196 procedure Apply_Dereference
(Arg
: Node_Id
) is
197 Typ
: constant Entity_Id
:= Etype
(Arg
);
199 if Is_Access_Type
(Typ
) then
200 Rewrite
(Arg
, Make_Explicit_Dereference
(Loc
,
201 Prefix
=> Relocate_Node
(Arg
)));
202 Analyze_And_Resolve
(Arg
, Designated_Type
(Typ
));
204 end Apply_Dereference
;
206 ------------------------
207 -- Has_Address_Clause --
208 ------------------------
210 function Has_Address_Clause
(Exp
: Node_Id
) return Boolean is
213 (Is_Entity_Name
(Exp
) and then
214 Present
(Address_Clause
(Entity
(Exp
))))
216 (Nkind
(Exp
) = N_Slice
and then Has_Address_Clause
(Prefix
(Exp
)));
217 end Has_Address_Clause
;
219 ---------------------
220 -- Is_Formal_Array --
221 ---------------------
223 function Is_Formal_Array
(Exp
: Node_Id
) return Boolean is
226 (Is_Entity_Name
(Exp
) and then Is_Formal
(Entity
(Exp
)))
228 (Nkind
(Exp
) = N_Slice
and then Is_Formal_Array
(Prefix
(Exp
)));
231 ------------------------
232 -- Is_Non_Local_Array --
233 ------------------------
235 function Is_Non_Local_Array
(Exp
: Node_Id
) return Boolean is
237 return (Is_Entity_Name
(Exp
)
238 and then Scope
(Entity
(Exp
)) /= Current_Scope
)
239 or else (Nkind
(Exp
) = N_Slice
240 and then Is_Non_Local_Array
(Prefix
(Exp
)));
241 end Is_Non_Local_Array
;
243 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
245 Lhs_Formal
: constant Boolean := Is_Formal_Array
(Act_Lhs
);
246 Rhs_Formal
: constant Boolean := Is_Formal_Array
(Act_Rhs
);
248 Lhs_Non_Local_Var
: constant Boolean := Is_Non_Local_Array
(Act_Lhs
);
249 Rhs_Non_Local_Var
: constant Boolean := Is_Non_Local_Array
(Act_Rhs
);
251 -- Start of processing for Expand_Assign_Array
254 -- Deal with length check. Note that the length check is done with
255 -- respect to the right hand side as given, not a possible underlying
256 -- renamed object, since this would generate incorrect extra checks.
258 Apply_Length_Check
(Rhs
, L_Type
);
260 -- We start by assuming that the move can be done in either direction,
261 -- i.e. that the two sides are completely disjoint.
263 Set_Forwards_OK
(N
, True);
264 Set_Backwards_OK
(N
, True);
266 -- Normally it is only the slice case that can lead to overlap, and
267 -- explicit checks for slices are made below. But there is one case
268 -- where the slice can be implicit and invisible to us: when we have a
269 -- one dimensional array, and either both operands are parameters, or
270 -- one is a parameter (which can be a slice passed by reference) and the
271 -- other is a non-local variable. In this case the parameter could be a
272 -- slice that overlaps with the other operand.
274 -- However, if the array subtype is a constrained first subtype in the
275 -- parameter case, then we don't have to worry about overlap, since
276 -- slice assignments aren't possible (other than for a slice denoting
279 -- Note: No overlap is possible if there is a change of representation,
280 -- so we can exclude this case.
285 ((Lhs_Formal
and Rhs_Formal
)
287 (Lhs_Formal
and Rhs_Non_Local_Var
)
289 (Rhs_Formal
and Lhs_Non_Local_Var
))
291 (not Is_Constrained
(Etype
(Lhs
))
292 or else not Is_First_Subtype
(Etype
(Lhs
)))
294 -- In the case of compiling for the Java or .NET Virtual Machine,
295 -- slices are always passed by making a copy, so we don't have to
296 -- worry about overlap. We also want to prevent generation of "<"
297 -- comparisons for array addresses, since that's a meaningless
298 -- operation on the VM.
300 and then VM_Target
= No_VM
302 Set_Forwards_OK
(N
, False);
303 Set_Backwards_OK
(N
, False);
305 -- Note: the bit-packed case is not worrisome here, since if we have
306 -- a slice passed as a parameter, it is always aligned on a byte
307 -- boundary, and if there are no explicit slices, the assignment
308 -- can be performed directly.
311 -- If either operand has an address clause clear Backwards_OK and
312 -- Forwards_OK, since we cannot tell if the operands overlap. We
313 -- exclude this treatment when Rhs is an aggregate, since we know
314 -- that overlap can't occur.
316 if (Has_Address_Clause
(Lhs
) and then Nkind
(Rhs
) /= N_Aggregate
)
317 or else Has_Address_Clause
(Rhs
)
319 Set_Forwards_OK
(N
, False);
320 Set_Backwards_OK
(N
, False);
323 -- We certainly must use a loop for change of representation and also
324 -- we use the operand of the conversion on the right hand side as the
325 -- effective right hand side (the component types must match in this
329 Act_Rhs
:= Get_Referenced_Object
(Rhs
);
330 R_Type
:= Get_Actual_Subtype
(Act_Rhs
);
331 Loop_Required
:= True;
333 -- We require a loop if the left side is possibly bit unaligned
335 elsif Possible_Bit_Aligned_Component
(Lhs
)
337 Possible_Bit_Aligned_Component
(Rhs
)
339 Loop_Required
:= True;
341 -- Arrays with controlled components are expanded into a loop to force
342 -- calls to Adjust at the component level.
344 elsif Has_Controlled_Component
(L_Type
) then
345 Loop_Required
:= True;
347 -- If object is atomic, we cannot tolerate a loop
349 elsif Is_Atomic_Object
(Act_Lhs
)
351 Is_Atomic_Object
(Act_Rhs
)
355 -- Loop is required if we have atomic components since we have to
356 -- be sure to do any accesses on an element by element basis.
358 elsif Has_Atomic_Components
(L_Type
)
359 or else Has_Atomic_Components
(R_Type
)
360 or else Is_Atomic
(Component_Type
(L_Type
))
361 or else Is_Atomic
(Component_Type
(R_Type
))
363 Loop_Required
:= True;
365 -- Case where no slice is involved
367 elsif not L_Slice
and not R_Slice
then
369 -- The following code deals with the case of unconstrained bit packed
370 -- arrays. The problem is that the template for such arrays contains
371 -- the bounds of the actual source level array, but the copy of an
372 -- entire array requires the bounds of the underlying array. It would
373 -- be nice if the back end could take care of this, but right now it
374 -- does not know how, so if we have such a type, then we expand out
375 -- into a loop, which is inefficient but works correctly. If we don't
376 -- do this, we get the wrong length computed for the array to be
377 -- moved. The two cases we need to worry about are:
379 -- Explicit dereference of an unconstrained packed array type as in
380 -- the following example:
383 -- type BITS is array(INTEGER range <>) of BOOLEAN;
384 -- pragma PACK(BITS);
385 -- type A is access BITS;
388 -- P1 := new BITS (1 .. 65_535);
389 -- P2 := new BITS (1 .. 65_535);
393 -- A formal parameter reference with an unconstrained bit array type
394 -- is the other case we need to worry about (here we assume the same
395 -- BITS type declared above):
397 -- procedure Write_All (File : out BITS; Contents : BITS);
399 -- File.Storage := Contents;
402 -- We expand to a loop in either of these two cases
404 -- Question for future thought. Another potentially more efficient
405 -- approach would be to create the actual subtype, and then do an
406 -- unchecked conversion to this actual subtype ???
408 Check_Unconstrained_Bit_Packed_Array
: declare
410 function Is_UBPA_Reference
(Opnd
: Node_Id
) return Boolean;
411 -- Function to perform required test for the first case, above
412 -- (dereference of an unconstrained bit packed array).
414 -----------------------
415 -- Is_UBPA_Reference --
416 -----------------------
418 function Is_UBPA_Reference
(Opnd
: Node_Id
) return Boolean is
419 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Opnd
));
421 Des_Type
: Entity_Id
;
424 if Present
(Packed_Array_Type
(Typ
))
425 and then Is_Array_Type
(Packed_Array_Type
(Typ
))
426 and then not Is_Constrained
(Packed_Array_Type
(Typ
))
430 elsif Nkind
(Opnd
) = N_Explicit_Dereference
then
431 P_Type
:= Underlying_Type
(Etype
(Prefix
(Opnd
)));
433 if not Is_Access_Type
(P_Type
) then
437 Des_Type
:= Designated_Type
(P_Type
);
439 Is_Bit_Packed_Array
(Des_Type
)
440 and then not Is_Constrained
(Des_Type
);
446 end Is_UBPA_Reference
;
448 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
451 if Is_UBPA_Reference
(Lhs
)
453 Is_UBPA_Reference
(Rhs
)
455 Loop_Required
:= True;
457 -- Here if we do not have the case of a reference to a bit packed
458 -- unconstrained array case. In this case gigi can most certainly
459 -- handle the assignment if a forwards move is allowed.
461 -- (could it handle the backwards case also???)
463 elsif Forwards_OK
(N
) then
466 end Check_Unconstrained_Bit_Packed_Array
;
468 -- The back end can always handle the assignment if the right side is a
469 -- string literal (note that overlap is definitely impossible in this
470 -- case). If the type is packed, a string literal is always converted
471 -- into an aggregate, except in the case of a null slice, for which no
472 -- aggregate can be written. In that case, rewrite the assignment as a
473 -- null statement, a length check has already been emitted to verify
474 -- that the range of the left-hand side is empty.
476 -- Note that this code is not executed if we have an assignment of a
477 -- string literal to a non-bit aligned component of a record, a case
478 -- which cannot be handled by the backend.
480 elsif Nkind
(Rhs
) = N_String_Literal
then
481 if String_Length
(Strval
(Rhs
)) = 0
482 and then Is_Bit_Packed_Array
(L_Type
)
484 Rewrite
(N
, Make_Null_Statement
(Loc
));
490 -- If either operand is bit packed, then we need a loop, since we can't
491 -- be sure that the slice is byte aligned. Similarly, if either operand
492 -- is a possibly unaligned slice, then we need a loop (since the back
493 -- end cannot handle unaligned slices).
495 elsif Is_Bit_Packed_Array
(L_Type
)
496 or else Is_Bit_Packed_Array
(R_Type
)
497 or else Is_Possibly_Unaligned_Slice
(Lhs
)
498 or else Is_Possibly_Unaligned_Slice
(Rhs
)
500 Loop_Required
:= True;
502 -- If we are not bit-packed, and we have only one slice, then no overlap
503 -- is possible except in the parameter case, so we can let the back end
506 elsif not (L_Slice
and R_Slice
) then
507 if Forwards_OK
(N
) then
512 -- If the right-hand side is a string literal, introduce a temporary for
513 -- it, for use in the generated loop that will follow.
515 if Nkind
(Rhs
) = N_String_Literal
then
517 Temp
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', Rhs
);
522 Make_Object_Declaration
(Loc
,
523 Defining_Identifier
=> Temp
,
524 Object_Definition
=> New_Occurrence_Of
(L_Type
, Loc
),
525 Expression
=> Relocate_Node
(Rhs
));
527 Insert_Action
(N
, Decl
);
528 Rewrite
(Rhs
, New_Occurrence_Of
(Temp
, Loc
));
529 R_Type
:= Etype
(Temp
);
533 -- Come here to complete the analysis
535 -- Loop_Required: Set to True if we know that a loop is required
536 -- regardless of overlap considerations.
538 -- Forwards_OK: Set to False if we already know that a forwards
539 -- move is not safe, else set to True.
541 -- Backwards_OK: Set to False if we already know that a backwards
542 -- move is not safe, else set to True
544 -- Our task at this stage is to complete the overlap analysis, which can
545 -- result in possibly setting Forwards_OK or Backwards_OK to False, and
546 -- then generating the final code, either by deciding that it is OK
547 -- after all to let Gigi handle it, or by generating appropriate code
551 L_Index_Typ
: constant Node_Id
:= Etype
(First_Index
(L_Type
));
552 R_Index_Typ
: constant Node_Id
:= Etype
(First_Index
(R_Type
));
554 Left_Lo
: constant Node_Id
:= Type_Low_Bound
(L_Index_Typ
);
555 Left_Hi
: constant Node_Id
:= Type_High_Bound
(L_Index_Typ
);
556 Right_Lo
: constant Node_Id
:= Type_Low_Bound
(R_Index_Typ
);
557 Right_Hi
: constant Node_Id
:= Type_High_Bound
(R_Index_Typ
);
559 Act_L_Array
: Node_Id
;
560 Act_R_Array
: Node_Id
;
566 Cresult
: Compare_Result
;
569 -- Get the expressions for the arrays. If we are dealing with a
570 -- private type, then convert to the underlying type. We can do
571 -- direct assignments to an array that is a private type, but we
572 -- cannot assign to elements of the array without this extra
573 -- unchecked conversion.
575 -- Note: We propagate Parent to the conversion nodes to generate
576 -- a well-formed subtree.
578 if Nkind
(Act_Lhs
) = N_Slice
then
579 Larray
:= Prefix
(Act_Lhs
);
583 if Is_Private_Type
(Etype
(Larray
)) then
585 Par
: constant Node_Id
:= Parent
(Larray
);
589 (Underlying_Type
(Etype
(Larray
)), Larray
);
590 Set_Parent
(Larray
, Par
);
595 if Nkind
(Act_Rhs
) = N_Slice
then
596 Rarray
:= Prefix
(Act_Rhs
);
600 if Is_Private_Type
(Etype
(Rarray
)) then
602 Par
: constant Node_Id
:= Parent
(Rarray
);
606 (Underlying_Type
(Etype
(Rarray
)), Rarray
);
607 Set_Parent
(Rarray
, Par
);
612 -- If both sides are slices, we must figure out whether it is safe
613 -- to do the move in one direction or the other. It is always safe
614 -- if there is a change of representation since obviously two arrays
615 -- with different representations cannot possibly overlap.
617 if (not Crep
) and L_Slice
and R_Slice
then
618 Act_L_Array
:= Get_Referenced_Object
(Prefix
(Act_Lhs
));
619 Act_R_Array
:= Get_Referenced_Object
(Prefix
(Act_Rhs
));
621 -- If both left and right hand arrays are entity names, and refer
622 -- to different entities, then we know that the move is safe (the
623 -- two storage areas are completely disjoint).
625 if Is_Entity_Name
(Act_L_Array
)
626 and then Is_Entity_Name
(Act_R_Array
)
627 and then Entity
(Act_L_Array
) /= Entity
(Act_R_Array
)
631 -- Otherwise, we assume the worst, which is that the two arrays
632 -- are the same array. There is no need to check if we know that
633 -- is the case, because if we don't know it, we still have to
636 -- Generally if the same array is involved, then we have an
637 -- overlapping case. We will have to really assume the worst (i.e.
638 -- set neither of the OK flags) unless we can determine the lower
639 -- or upper bounds at compile time and compare them.
644 (Left_Lo
, Right_Lo
, Assume_Valid
=> True);
646 if Cresult
= Unknown
then
649 (Left_Hi
, Right_Hi
, Assume_Valid
=> True);
653 when LT | LE | EQ
=> Set_Backwards_OK
(N
, False);
654 when GT | GE
=> Set_Forwards_OK
(N
, False);
655 when NE | Unknown
=> Set_Backwards_OK
(N
, False);
656 Set_Forwards_OK
(N
, False);
661 -- If after that analysis Loop_Required is False, meaning that we
662 -- have not discovered some non-overlap reason for requiring a loop,
663 -- then the outcome depends on the capabilities of the back end.
665 if not Loop_Required
then
667 -- The GCC back end can deal with all cases of overlap by falling
668 -- back to memmove if it cannot use a more efficient approach.
670 if VM_Target
= No_VM
and not AAMP_On_Target
then
673 -- Assume other back ends can handle it if Forwards_OK is set
675 elsif Forwards_OK
(N
) then
678 -- If Forwards_OK is not set, the back end will need something
679 -- like memmove to handle the move. For now, this processing is
680 -- activated using the .s debug flag (-gnatd.s).
682 elsif Debug_Flag_Dot_S
then
687 -- At this stage we have to generate an explicit loop, and we have
688 -- the following cases:
690 -- Forwards_OK = True
692 -- Rnn : right_index := right_index'First;
693 -- for Lnn in left-index loop
694 -- left (Lnn) := right (Rnn);
695 -- Rnn := right_index'Succ (Rnn);
698 -- Note: the above code MUST be analyzed with checks off, because
699 -- otherwise the Succ could overflow. But in any case this is more
702 -- Forwards_OK = False, Backwards_OK = True
704 -- Rnn : right_index := right_index'Last;
705 -- for Lnn in reverse left-index loop
706 -- left (Lnn) := right (Rnn);
707 -- Rnn := right_index'Pred (Rnn);
710 -- Note: the above code MUST be analyzed with checks off, because
711 -- otherwise the Pred could overflow. But in any case this is more
714 -- Forwards_OK = Backwards_OK = False
716 -- This only happens if we have the same array on each side. It is
717 -- possible to create situations using overlays that violate this,
718 -- but we simply do not promise to get this "right" in this case.
720 -- There are two possible subcases. If the No_Implicit_Conditionals
721 -- restriction is set, then we generate the following code:
724 -- T : constant <operand-type> := rhs;
729 -- If implicit conditionals are permitted, then we generate:
731 -- if Left_Lo <= Right_Lo then
732 -- <code for Forwards_OK = True above>
734 -- <code for Backwards_OK = True above>
737 -- In order to detect possible aliasing, we examine the renamed
738 -- expression when the source or target is a renaming. However,
739 -- the renaming may be intended to capture an address that may be
740 -- affected by subsequent code, and therefore we must recover
741 -- the actual entity for the expansion that follows, not the
742 -- object it renames. In particular, if source or target designate
743 -- a portion of a dynamically allocated object, the pointer to it
744 -- may be reassigned but the renaming preserves the proper location.
746 if Is_Entity_Name
(Rhs
)
748 Nkind
(Parent
(Entity
(Rhs
))) = N_Object_Renaming_Declaration
749 and then Nkind
(Act_Rhs
) = N_Slice
754 if Is_Entity_Name
(Lhs
)
756 Nkind
(Parent
(Entity
(Lhs
))) = N_Object_Renaming_Declaration
757 and then Nkind
(Act_Lhs
) = N_Slice
762 -- Cases where either Forwards_OK or Backwards_OK is true
764 if Forwards_OK
(N
) or else Backwards_OK
(N
) then
765 if Needs_Finalization
(Component_Type
(L_Type
))
766 and then Base_Type
(L_Type
) = Base_Type
(R_Type
)
768 and then not No_Ctrl_Actions
(N
)
771 Proc
: constant Entity_Id
:=
772 TSS
(Base_Type
(L_Type
), TSS_Slice_Assign
);
776 Apply_Dereference
(Larray
);
777 Apply_Dereference
(Rarray
);
778 Actuals
:= New_List
(
779 Duplicate_Subexpr
(Larray
, Name_Req
=> True),
780 Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
781 Duplicate_Subexpr
(Left_Lo
, Name_Req
=> True),
782 Duplicate_Subexpr
(Left_Hi
, Name_Req
=> True),
783 Duplicate_Subexpr
(Right_Lo
, Name_Req
=> True),
784 Duplicate_Subexpr
(Right_Hi
, Name_Req
=> True));
788 Boolean_Literals
(not Forwards_OK
(N
)), Loc
));
791 Make_Procedure_Call_Statement
(Loc
,
792 Name
=> New_Reference_To
(Proc
, Loc
),
793 Parameter_Associations
=> Actuals
));
798 Expand_Assign_Array_Loop
799 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
800 Rev
=> not Forwards_OK
(N
)));
803 -- Case of both are false with No_Implicit_Conditionals
805 elsif Restriction_Active
(No_Implicit_Conditionals
) then
807 T
: constant Entity_Id
:=
808 Make_Defining_Identifier
(Loc
, Chars
=> Name_T
);
812 Make_Block_Statement
(Loc
,
813 Declarations
=> New_List
(
814 Make_Object_Declaration
(Loc
,
815 Defining_Identifier
=> T
,
816 Constant_Present
=> True,
818 New_Occurrence_Of
(Etype
(Rhs
), Loc
),
819 Expression
=> Relocate_Node
(Rhs
))),
821 Handled_Statement_Sequence
=>
822 Make_Handled_Sequence_Of_Statements
(Loc
,
823 Statements
=> New_List
(
824 Make_Assignment_Statement
(Loc
,
825 Name
=> Relocate_Node
(Lhs
),
826 Expression
=> New_Occurrence_Of
(T
, Loc
))))));
829 -- Case of both are false with implicit conditionals allowed
832 -- Before we generate this code, we must ensure that the left and
833 -- right side array types are defined. They may be itypes, and we
834 -- cannot let them be defined inside the if, since the first use
835 -- in the then may not be executed.
837 Ensure_Defined
(L_Type
, N
);
838 Ensure_Defined
(R_Type
, N
);
840 -- We normally compare addresses to find out which way round to
841 -- do the loop, since this is reliable, and handles the cases of
842 -- parameters, conversions etc. But we can't do that in the bit
843 -- packed case or the VM case, because addresses don't work there.
845 if not Is_Bit_Packed_Array
(L_Type
) and then VM_Target
= No_VM
then
849 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
850 Make_Attribute_Reference
(Loc
,
852 Make_Indexed_Component
(Loc
,
854 Duplicate_Subexpr_Move_Checks
(Larray
, True),
855 Expressions
=> New_List
(
856 Make_Attribute_Reference
(Loc
,
860 Attribute_Name
=> Name_First
))),
861 Attribute_Name
=> Name_Address
)),
864 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
865 Make_Attribute_Reference
(Loc
,
867 Make_Indexed_Component
(Loc
,
869 Duplicate_Subexpr_Move_Checks
(Rarray
, True),
870 Expressions
=> New_List
(
871 Make_Attribute_Reference
(Loc
,
875 Attribute_Name
=> Name_First
))),
876 Attribute_Name
=> Name_Address
)));
878 -- For the bit packed and VM cases we use the bounds. That's OK,
879 -- because we don't have to worry about parameters, since they
880 -- cannot cause overlap. Perhaps we should worry about weird slice
886 Cleft_Lo
:= New_Copy_Tree
(Left_Lo
);
887 Cright_Lo
:= New_Copy_Tree
(Right_Lo
);
889 -- If the types do not match we add an implicit conversion
890 -- here to ensure proper match
892 if Etype
(Left_Lo
) /= Etype
(Right_Lo
) then
894 Unchecked_Convert_To
(Etype
(Left_Lo
), Cright_Lo
);
897 -- Reset the Analyzed flag, because the bounds of the index
898 -- type itself may be universal, and must must be reanalyzed
899 -- to acquire the proper type for the back end.
901 Set_Analyzed
(Cleft_Lo
, False);
902 Set_Analyzed
(Cright_Lo
, False);
906 Left_Opnd
=> Cleft_Lo
,
907 Right_Opnd
=> Cright_Lo
);
910 if Needs_Finalization
(Component_Type
(L_Type
))
911 and then Base_Type
(L_Type
) = Base_Type
(R_Type
)
913 and then not No_Ctrl_Actions
(N
)
916 -- Call TSS procedure for array assignment, passing the
917 -- explicit bounds of right and left hand sides.
920 Proc
: constant Entity_Id
:=
921 TSS
(Base_Type
(L_Type
), TSS_Slice_Assign
);
925 Apply_Dereference
(Larray
);
926 Apply_Dereference
(Rarray
);
927 Actuals
:= New_List
(
928 Duplicate_Subexpr
(Larray
, Name_Req
=> True),
929 Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
930 Duplicate_Subexpr
(Left_Lo
, Name_Req
=> True),
931 Duplicate_Subexpr
(Left_Hi
, Name_Req
=> True),
932 Duplicate_Subexpr
(Right_Lo
, Name_Req
=> True),
933 Duplicate_Subexpr
(Right_Hi
, Name_Req
=> True));
937 Right_Opnd
=> Condition
));
940 Make_Procedure_Call_Statement
(Loc
,
941 Name
=> New_Reference_To
(Proc
, Loc
),
942 Parameter_Associations
=> Actuals
));
947 Make_Implicit_If_Statement
(N
,
948 Condition
=> Condition
,
950 Then_Statements
=> New_List
(
951 Expand_Assign_Array_Loop
952 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
955 Else_Statements
=> New_List
(
956 Expand_Assign_Array_Loop
957 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
962 Analyze
(N
, Suppress
=> All_Checks
);
966 when RE_Not_Available
=>
968 end Expand_Assign_Array
;
970 ------------------------------
971 -- Expand_Assign_Array_Loop --
972 ------------------------------
974 -- The following is an example of the loop generated for the case of a
975 -- two-dimensional array:
980 -- for L1b in 1 .. 100 loop
984 -- for L3b in 1 .. 100 loop
985 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
986 -- R4b := Tm1X2'succ(R4b);
989 -- R2b := Tm1X1'succ(R2b);
993 -- Here Rev is False, and Tm1Xn are the subscript types for the right hand
994 -- side. The declarations of R2b and R4b are inserted before the original
995 -- assignment statement.
997 function Expand_Assign_Array_Loop
1004 Rev
: Boolean) return Node_Id
1006 Loc
: constant Source_Ptr
:= Sloc
(N
);
1008 Lnn
: array (1 .. Ndim
) of Entity_Id
;
1009 Rnn
: array (1 .. Ndim
) of Entity_Id
;
1010 -- Entities used as subscripts on left and right sides
1012 L_Index_Type
: array (1 .. Ndim
) of Entity_Id
;
1013 R_Index_Type
: array (1 .. Ndim
) of Entity_Id
;
1014 -- Left and right index types
1021 function Build_Step
(J
: Nat
) return Node_Id
;
1022 -- The increment step for the index of the right-hand side is written
1023 -- as an attribute reference (Succ or Pred). This function returns
1024 -- the corresponding node, which is placed at the end of the loop body.
1030 function Build_Step
(J
: Nat
) return Node_Id
is
1042 Make_Assignment_Statement
(Loc
,
1043 Name
=> New_Occurrence_Of
(Rnn
(J
), Loc
),
1045 Make_Attribute_Reference
(Loc
,
1047 New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1048 Attribute_Name
=> S_Or_P
,
1049 Expressions
=> New_List
(
1050 New_Occurrence_Of
(Rnn
(J
), Loc
))));
1052 -- Note that on the last iteration of the loop, the index is increased
1053 -- (or decreased) past the corresponding bound. This is consistent with
1054 -- the C semantics of the back-end, where such an off-by-one value on a
1055 -- dead index variable is OK. However, in CodePeer mode this leads to
1056 -- spurious warnings, and thus we place a guard around the attribute
1057 -- reference. For obvious reasons we only do this for CodePeer.
1059 if CodePeer_Mode
then
1061 Make_If_Statement
(Loc
,
1064 Left_Opnd
=> New_Occurrence_Of
(Lnn
(J
), Loc
),
1066 Make_Attribute_Reference
(Loc
,
1067 Prefix
=> New_Occurrence_Of
(L_Index_Type
(J
), Loc
),
1068 Attribute_Name
=> Lim
)),
1069 Then_Statements
=> New_List
(Step
));
1075 -- Start of processing for Expand_Assign_Array_Loop
1079 F_Or_L
:= Name_Last
;
1080 S_Or_P
:= Name_Pred
;
1082 F_Or_L
:= Name_First
;
1083 S_Or_P
:= Name_Succ
;
1086 -- Setup index types and subscript entities
1093 L_Index
:= First_Index
(L_Type
);
1094 R_Index
:= First_Index
(R_Type
);
1096 for J
in 1 .. Ndim
loop
1097 Lnn
(J
) := Make_Temporary
(Loc
, 'L');
1098 Rnn
(J
) := Make_Temporary
(Loc
, 'R');
1100 L_Index_Type
(J
) := Etype
(L_Index
);
1101 R_Index_Type
(J
) := Etype
(R_Index
);
1103 Next_Index
(L_Index
);
1104 Next_Index
(R_Index
);
1108 -- Now construct the assignment statement
1111 ExprL
: constant List_Id
:= New_List
;
1112 ExprR
: constant List_Id
:= New_List
;
1115 for J
in 1 .. Ndim
loop
1116 Append_To
(ExprL
, New_Occurrence_Of
(Lnn
(J
), Loc
));
1117 Append_To
(ExprR
, New_Occurrence_Of
(Rnn
(J
), Loc
));
1121 Make_Assignment_Statement
(Loc
,
1123 Make_Indexed_Component
(Loc
,
1124 Prefix
=> Duplicate_Subexpr
(Larray
, Name_Req
=> True),
1125 Expressions
=> ExprL
),
1127 Make_Indexed_Component
(Loc
,
1128 Prefix
=> Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
1129 Expressions
=> ExprR
));
1131 -- We set assignment OK, since there are some cases, e.g. in object
1132 -- declarations, where we are actually assigning into a constant.
1133 -- If there really is an illegality, it was caught long before now,
1134 -- and was flagged when the original assignment was analyzed.
1136 Set_Assignment_OK
(Name
(Assign
));
1138 -- Propagate the No_Ctrl_Actions flag to individual assignments
1140 Set_No_Ctrl_Actions
(Assign
, No_Ctrl_Actions
(N
));
1143 -- Now construct the loop from the inside out, with the last subscript
1144 -- varying most rapidly. Note that Assign is first the raw assignment
1145 -- statement, and then subsequently the loop that wraps it up.
1147 for J
in reverse 1 .. Ndim
loop
1149 Make_Block_Statement
(Loc
,
1150 Declarations
=> New_List
(
1151 Make_Object_Declaration
(Loc
,
1152 Defining_Identifier
=> Rnn
(J
),
1153 Object_Definition
=>
1154 New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1156 Make_Attribute_Reference
(Loc
,
1157 Prefix
=> New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1158 Attribute_Name
=> F_Or_L
))),
1160 Handled_Statement_Sequence
=>
1161 Make_Handled_Sequence_Of_Statements
(Loc
,
1162 Statements
=> New_List
(
1163 Make_Implicit_Loop_Statement
(N
,
1165 Make_Iteration_Scheme
(Loc
,
1166 Loop_Parameter_Specification
=>
1167 Make_Loop_Parameter_Specification
(Loc
,
1168 Defining_Identifier
=> Lnn
(J
),
1169 Reverse_Present
=> Rev
,
1170 Discrete_Subtype_Definition
=>
1171 New_Reference_To
(L_Index_Type
(J
), Loc
))),
1173 Statements
=> New_List
(Assign
, Build_Step
(J
))))));
1177 end Expand_Assign_Array_Loop
;
1179 --------------------------
1180 -- Expand_Assign_Record --
1181 --------------------------
1183 procedure Expand_Assign_Record
(N
: Node_Id
) is
1184 Lhs
: constant Node_Id
:= Name
(N
);
1185 Rhs
: Node_Id
:= Expression
(N
);
1186 L_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Lhs
));
1189 -- If change of representation, then extract the real right hand side
1190 -- from the type conversion, and proceed with component-wise assignment,
1191 -- since the two types are not the same as far as the back end is
1194 if Change_Of_Representation
(N
) then
1195 Rhs
:= Expression
(Rhs
);
1197 -- If this may be a case of a large bit aligned component, then proceed
1198 -- with component-wise assignment, to avoid possible clobbering of other
1199 -- components sharing bits in the first or last byte of the component to
1202 elsif Possible_Bit_Aligned_Component
(Lhs
)
1204 Possible_Bit_Aligned_Component
(Rhs
)
1208 -- If we have a tagged type that has a complete record representation
1209 -- clause, we must do we must do component-wise assignments, since child
1210 -- types may have used gaps for their components, and we might be
1211 -- dealing with a view conversion.
1213 elsif Is_Fully_Repped_Tagged_Type
(L_Typ
) then
1216 -- If neither condition met, then nothing special to do, the back end
1217 -- can handle assignment of the entire component as a single entity.
1223 -- At this stage we know that we must do a component wise assignment
1226 Loc
: constant Source_Ptr
:= Sloc
(N
);
1227 R_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Rhs
));
1228 Decl
: constant Node_Id
:= Declaration_Node
(R_Typ
);
1232 function Find_Component
1234 Comp
: Entity_Id
) return Entity_Id
;
1235 -- Find the component with the given name in the underlying record
1236 -- declaration for Typ. We need to use the actual entity because the
1237 -- type may be private and resolution by identifier alone would fail.
1239 function Make_Component_List_Assign
1241 U_U
: Boolean := False) return List_Id
;
1242 -- Returns a sequence of statements to assign the components that
1243 -- are referenced in the given component list. The flag U_U is
1244 -- used to force the usage of the inferred value of the variant
1245 -- part expression as the switch for the generated case statement.
1247 function Make_Field_Assign
1249 U_U
: Boolean := False) return Node_Id
;
1250 -- Given C, the entity for a discriminant or component, build an
1251 -- assignment for the corresponding field values. The flag U_U
1252 -- signals the presence of an Unchecked_Union and forces the usage
1253 -- of the inferred discriminant value of C as the right hand side
1254 -- of the assignment.
1256 function Make_Field_Assigns
(CI
: List_Id
) return List_Id
;
1257 -- Given CI, a component items list, construct series of statements
1258 -- for fieldwise assignment of the corresponding components.
1260 --------------------
1261 -- Find_Component --
1262 --------------------
1264 function Find_Component
1266 Comp
: Entity_Id
) return Entity_Id
1268 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
1272 C
:= First_Entity
(Utyp
);
1273 while Present
(C
) loop
1274 if Chars
(C
) = Chars
(Comp
) then
1281 raise Program_Error
;
1284 --------------------------------
1285 -- Make_Component_List_Assign --
1286 --------------------------------
1288 function Make_Component_List_Assign
1290 U_U
: Boolean := False) return List_Id
1292 CI
: constant List_Id
:= Component_Items
(CL
);
1293 VP
: constant Node_Id
:= Variant_Part
(CL
);
1303 Result
:= Make_Field_Assigns
(CI
);
1305 if Present
(VP
) then
1306 V
:= First_Non_Pragma
(Variants
(VP
));
1308 while Present
(V
) loop
1310 DC
:= First
(Discrete_Choices
(V
));
1311 while Present
(DC
) loop
1312 Append_To
(DCH
, New_Copy_Tree
(DC
));
1317 Make_Case_Statement_Alternative
(Loc
,
1318 Discrete_Choices
=> DCH
,
1320 Make_Component_List_Assign
(Component_List
(V
))));
1321 Next_Non_Pragma
(V
);
1324 -- If we have an Unchecked_Union, use the value of the inferred
1325 -- discriminant of the variant part expression as the switch
1326 -- for the case statement. The case statement may later be
1331 New_Copy
(Get_Discriminant_Value
(
1334 Discriminant_Constraint
(Etype
(Rhs
))));
1337 Make_Selected_Component
(Loc
,
1338 Prefix
=> Duplicate_Subexpr
(Rhs
),
1340 Make_Identifier
(Loc
, Chars
(Name
(VP
))));
1344 Make_Case_Statement
(Loc
,
1346 Alternatives
=> Alts
));
1350 end Make_Component_List_Assign
;
1352 -----------------------
1353 -- Make_Field_Assign --
1354 -----------------------
1356 function Make_Field_Assign
1358 U_U
: Boolean := False) return Node_Id
1364 -- In the case of an Unchecked_Union, use the discriminant
1365 -- constraint value as on the right hand side of the assignment.
1369 New_Copy
(Get_Discriminant_Value
(C
,
1371 Discriminant_Constraint
(Etype
(Rhs
))));
1374 Make_Selected_Component
(Loc
,
1375 Prefix
=> Duplicate_Subexpr
(Rhs
),
1376 Selector_Name
=> New_Occurrence_Of
(C
, Loc
));
1380 Make_Assignment_Statement
(Loc
,
1382 Make_Selected_Component
(Loc
,
1383 Prefix
=> Duplicate_Subexpr
(Lhs
),
1385 New_Occurrence_Of
(Find_Component
(L_Typ
, C
), Loc
)),
1386 Expression
=> Expr
);
1388 -- Set Assignment_OK, so discriminants can be assigned
1390 Set_Assignment_OK
(Name
(A
), True);
1392 if Componentwise_Assignment
(N
)
1393 and then Nkind
(Name
(A
)) = N_Selected_Component
1394 and then Chars
(Selector_Name
(Name
(A
))) = Name_uParent
1396 Set_Componentwise_Assignment
(A
);
1400 end Make_Field_Assign
;
1402 ------------------------
1403 -- Make_Field_Assigns --
1404 ------------------------
1406 function Make_Field_Assigns
(CI
: List_Id
) return List_Id
is
1414 while Present
(Item
) loop
1416 -- Look for components, but exclude _tag field assignment if
1417 -- the special Componentwise_Assignment flag is set.
1419 if Nkind
(Item
) = N_Component_Declaration
1420 and then not (Is_Tag
(Defining_Identifier
(Item
))
1421 and then Componentwise_Assignment
(N
))
1424 (Result
, Make_Field_Assign
(Defining_Identifier
(Item
)));
1431 end Make_Field_Assigns
;
1433 -- Start of processing for Expand_Assign_Record
1436 -- Note that we use the base types for this processing. This results
1437 -- in some extra work in the constrained case, but the change of
1438 -- representation case is so unusual that it is not worth the effort.
1440 -- First copy the discriminants. This is done unconditionally. It
1441 -- is required in the unconstrained left side case, and also in the
1442 -- case where this assignment was constructed during the expansion
1443 -- of a type conversion (since initialization of discriminants is
1444 -- suppressed in this case). It is unnecessary but harmless in
1447 if Has_Discriminants
(L_Typ
) then
1448 F
:= First_Discriminant
(R_Typ
);
1449 while Present
(F
) loop
1451 -- If we are expanding the initialization of a derived record
1452 -- that constrains or renames discriminants of the parent, we
1453 -- must use the corresponding discriminant in the parent.
1460 and then Present
(Corresponding_Discriminant
(F
))
1462 CF
:= Corresponding_Discriminant
(F
);
1467 if Is_Unchecked_Union
(Base_Type
(R_Typ
)) then
1469 -- Within an initialization procedure this is the
1470 -- assignment to an unchecked union component, in which
1471 -- case there is no discriminant to initialize.
1473 if Inside_Init_Proc
then
1477 -- The assignment is part of a conversion from a
1478 -- derived unchecked union type with an inferable
1479 -- discriminant, to a parent type.
1481 Insert_Action
(N
, Make_Field_Assign
(CF
, True));
1485 Insert_Action
(N
, Make_Field_Assign
(CF
));
1488 Next_Discriminant
(F
);
1493 -- We know the underlying type is a record, but its current view
1494 -- may be private. We must retrieve the usable record declaration.
1496 if Nkind_In
(Decl
, N_Private_Type_Declaration
,
1497 N_Private_Extension_Declaration
)
1498 and then Present
(Full_View
(R_Typ
))
1500 RDef
:= Type_Definition
(Declaration_Node
(Full_View
(R_Typ
)));
1502 RDef
:= Type_Definition
(Decl
);
1505 if Nkind
(RDef
) = N_Derived_Type_Definition
then
1506 RDef
:= Record_Extension_Part
(RDef
);
1509 if Nkind
(RDef
) = N_Record_Definition
1510 and then Present
(Component_List
(RDef
))
1512 if Is_Unchecked_Union
(R_Typ
) then
1514 Make_Component_List_Assign
(Component_List
(RDef
), True));
1517 (N
, Make_Component_List_Assign
(Component_List
(RDef
)));
1520 Rewrite
(N
, Make_Null_Statement
(Loc
));
1523 end Expand_Assign_Record
;
1525 -----------------------------------
1526 -- Expand_N_Assignment_Statement --
1527 -----------------------------------
1529 -- This procedure implements various cases where an assignment statement
1530 -- cannot just be passed on to the back end in untransformed state.
1532 procedure Expand_N_Assignment_Statement
(N
: Node_Id
) is
1533 Loc
: constant Source_Ptr
:= Sloc
(N
);
1534 Crep
: constant Boolean := Change_Of_Representation
(N
);
1535 Lhs
: constant Node_Id
:= Name
(N
);
1536 Rhs
: constant Node_Id
:= Expression
(N
);
1537 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Lhs
));
1541 -- Special case to check right away, if the Componentwise_Assignment
1542 -- flag is set, this is a reanalysis from the expansion of the primitive
1543 -- assignment procedure for a tagged type, and all we need to do is to
1544 -- expand to assignment of components, because otherwise, we would get
1545 -- infinite recursion (since this looks like a tagged assignment which
1546 -- would normally try to *call* the primitive assignment procedure).
1548 if Componentwise_Assignment
(N
) then
1549 Expand_Assign_Record
(N
);
1553 -- Defend against invalid subscripts on left side if we are in standard
1554 -- validity checking mode. No need to do this if we are checking all
1557 -- Note that we do this right away, because there are some early return
1558 -- paths in this procedure, and this is required on all paths.
1560 if Validity_Checks_On
1561 and then Validity_Check_Default
1562 and then not Validity_Check_Subscripts
1564 Check_Valid_Lvalue_Subscripts
(Lhs
);
1567 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1569 -- Rewrite an assignment to X'Priority into a run-time call
1571 -- For example: X'Priority := New_Prio_Expr;
1572 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1574 -- Note that although X'Priority is notionally an object, it is quite
1575 -- deliberately not defined as an aliased object in the RM. This means
1576 -- that it works fine to rewrite it as a call, without having to worry
1577 -- about complications that would other arise from X'Priority'Access,
1578 -- which is illegal, because of the lack of aliasing.
1580 if Ada_Version
>= Ada_2005
then
1583 Conctyp
: Entity_Id
;
1586 RT_Subprg_Name
: Node_Id
;
1589 -- Handle chains of renamings
1592 while Nkind
(Ent
) in N_Has_Entity
1593 and then Present
(Entity
(Ent
))
1594 and then Present
(Renamed_Object
(Entity
(Ent
)))
1596 Ent
:= Renamed_Object
(Entity
(Ent
));
1599 -- The attribute Priority applied to protected objects has been
1600 -- previously expanded into a call to the Get_Ceiling run-time
1603 if Nkind
(Ent
) = N_Function_Call
1604 and then (Entity
(Name
(Ent
)) = RTE
(RE_Get_Ceiling
)
1606 Entity
(Name
(Ent
)) = RTE
(RO_PE_Get_Ceiling
))
1608 -- Look for the enclosing concurrent type
1610 Conctyp
:= Current_Scope
;
1611 while not Is_Concurrent_Type
(Conctyp
) loop
1612 Conctyp
:= Scope
(Conctyp
);
1615 pragma Assert
(Is_Protected_Type
(Conctyp
));
1617 -- Generate the first actual of the call
1619 Subprg
:= Current_Scope
;
1620 while not Present
(Protected_Body_Subprogram
(Subprg
)) loop
1621 Subprg
:= Scope
(Subprg
);
1624 -- Select the appropriate run-time call
1626 if Number_Entries
(Conctyp
) = 0 then
1628 New_Reference_To
(RTE
(RE_Set_Ceiling
), Loc
);
1631 New_Reference_To
(RTE
(RO_PE_Set_Ceiling
), Loc
);
1635 Make_Procedure_Call_Statement
(Loc
,
1636 Name
=> RT_Subprg_Name
,
1637 Parameter_Associations
=> New_List
(
1638 New_Copy_Tree
(First
(Parameter_Associations
(Ent
))),
1639 Relocate_Node
(Expression
(N
))));
1648 -- Deal with assignment checks unless suppressed
1650 if not Suppress_Assignment_Checks
(N
) then
1652 -- First deal with generation of range check if required
1654 if Do_Range_Check
(Rhs
) then
1655 Set_Do_Range_Check
(Rhs
, False);
1656 Generate_Range_Check
(Rhs
, Typ
, CE_Range_Check_Failed
);
1659 -- Then generate predicate check if required
1661 Apply_Predicate_Check
(Rhs
, Typ
);
1664 -- Check for a special case where a high level transformation is
1665 -- required. If we have either of:
1670 -- where P is a reference to a bit packed array, then we have to unwind
1671 -- the assignment. The exact meaning of being a reference to a bit
1672 -- packed array is as follows:
1674 -- An indexed component whose prefix is a bit packed array is a
1675 -- reference to a bit packed array.
1677 -- An indexed component or selected component whose prefix is a
1678 -- reference to a bit packed array is itself a reference ot a
1679 -- bit packed array.
1681 -- The required transformation is
1683 -- Tnn : prefix_type := P;
1684 -- Tnn.field := rhs;
1689 -- Tnn : prefix_type := P;
1690 -- Tnn (subscr) := rhs;
1693 -- Since P is going to be evaluated more than once, any subscripts
1694 -- in P must have their evaluation forced.
1696 if Nkind_In
(Lhs
, N_Indexed_Component
, N_Selected_Component
)
1697 and then Is_Ref_To_Bit_Packed_Array
(Prefix
(Lhs
))
1700 BPAR_Expr
: constant Node_Id
:= Relocate_Node
(Prefix
(Lhs
));
1701 BPAR_Typ
: constant Entity_Id
:= Etype
(BPAR_Expr
);
1702 Tnn
: constant Entity_Id
:=
1703 Make_Temporary
(Loc
, 'T', BPAR_Expr
);
1706 -- Insert the post assignment first, because we want to copy the
1707 -- BPAR_Expr tree before it gets analyzed in the context of the
1708 -- pre assignment. Note that we do not analyze the post assignment
1709 -- yet (we cannot till we have completed the analysis of the pre
1710 -- assignment). As usual, the analysis of this post assignment
1711 -- will happen on its own when we "run into" it after finishing
1712 -- the current assignment.
1715 Make_Assignment_Statement
(Loc
,
1716 Name
=> New_Copy_Tree
(BPAR_Expr
),
1717 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
1719 -- At this stage BPAR_Expr is a reference to a bit packed array
1720 -- where the reference was not expanded in the original tree,
1721 -- since it was on the left side of an assignment. But in the
1722 -- pre-assignment statement (the object definition), BPAR_Expr
1723 -- will end up on the right hand side, and must be reexpanded. To
1724 -- achieve this, we reset the analyzed flag of all selected and
1725 -- indexed components down to the actual indexed component for
1726 -- the packed array.
1730 Set_Analyzed
(Exp
, False);
1733 (Exp
, N_Selected_Component
, N_Indexed_Component
)
1735 Exp
:= Prefix
(Exp
);
1741 -- Now we can insert and analyze the pre-assignment
1743 -- If the right-hand side requires a transient scope, it has
1744 -- already been placed on the stack. However, the declaration is
1745 -- inserted in the tree outside of this scope, and must reflect
1746 -- the proper scope for its variable. This awkward bit is forced
1747 -- by the stricter scope discipline imposed by GCC 2.97.
1750 Uses_Transient_Scope
: constant Boolean :=
1752 and then N
= Node_To_Be_Wrapped
;
1755 if Uses_Transient_Scope
then
1756 Push_Scope
(Scope
(Current_Scope
));
1759 Insert_Before_And_Analyze
(N
,
1760 Make_Object_Declaration
(Loc
,
1761 Defining_Identifier
=> Tnn
,
1762 Object_Definition
=> New_Occurrence_Of
(BPAR_Typ
, Loc
),
1763 Expression
=> BPAR_Expr
));
1765 if Uses_Transient_Scope
then
1770 -- Now fix up the original assignment and continue processing
1772 Rewrite
(Prefix
(Lhs
),
1773 New_Occurrence_Of
(Tnn
, Loc
));
1775 -- We do not need to reanalyze that assignment, and we do not need
1776 -- to worry about references to the temporary, but we do need to
1777 -- make sure that the temporary is not marked as a true constant
1778 -- since we now have a generated assignment to it!
1780 Set_Is_True_Constant
(Tnn
, False);
1784 -- When we have the appropriate type of aggregate in the expression (it
1785 -- has been determined during analysis of the aggregate by setting the
1786 -- delay flag), let's perform in place assignment and thus avoid
1787 -- creating a temporary.
1789 if Is_Delayed_Aggregate
(Rhs
) then
1790 Convert_Aggr_In_Assignment
(N
);
1791 Rewrite
(N
, Make_Null_Statement
(Loc
));
1796 -- Apply discriminant check if required. If Lhs is an access type to a
1797 -- designated type with discriminants, we must always check.
1799 if Has_Discriminants
(Etype
(Lhs
)) then
1801 -- Skip discriminant check if change of representation. Will be
1802 -- done when the change of representation is expanded out.
1805 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
), Lhs
);
1808 -- If the type is private without discriminants, and the full type
1809 -- has discriminants (necessarily with defaults) a check may still be
1810 -- necessary if the Lhs is aliased. The private discriminants must be
1811 -- visible to build the discriminant constraints.
1813 -- Only an explicit dereference that comes from source indicates
1814 -- aliasing. Access to formals of protected operations and entries
1815 -- create dereferences but are not semantic aliasings.
1817 elsif Is_Private_Type
(Etype
(Lhs
))
1818 and then Has_Discriminants
(Typ
)
1819 and then Nkind
(Lhs
) = N_Explicit_Dereference
1820 and then Comes_From_Source
(Lhs
)
1823 Lt
: constant Entity_Id
:= Etype
(Lhs
);
1824 Ubt
: Entity_Id
:= Base_Type
(Typ
);
1827 -- In the case of an expander-generated record subtype whose base
1828 -- type still appears private, Typ will have been set to that
1829 -- private type rather than the underlying record type (because
1830 -- Underlying type will have returned the record subtype), so it's
1831 -- necessary to apply Underlying_Type again to the base type to
1832 -- get the record type we need for the discriminant check. Such
1833 -- subtypes can be created for assignments in certain cases, such
1834 -- as within an instantiation passed this kind of private type.
1835 -- It would be good to avoid this special test, but making changes
1836 -- to prevent this odd form of record subtype seems difficult. ???
1838 if Is_Private_Type
(Ubt
) then
1839 Ubt
:= Underlying_Type
(Ubt
);
1842 Set_Etype
(Lhs
, Ubt
);
1843 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Ubt
), Rhs
));
1844 Apply_Discriminant_Check
(Rhs
, Ubt
, Lhs
);
1845 Set_Etype
(Lhs
, Lt
);
1848 -- If the Lhs has a private type with unknown discriminants, it
1849 -- may have a full view with discriminants, but those are nameable
1850 -- only in the underlying type, so convert the Rhs to it before
1851 -- potential checking.
1853 elsif Has_Unknown_Discriminants
(Base_Type
(Etype
(Lhs
)))
1854 and then Has_Discriminants
(Typ
)
1856 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Typ
), Rhs
));
1857 Apply_Discriminant_Check
(Rhs
, Typ
, Lhs
);
1859 -- In the access type case, we need the same discriminant check, and
1860 -- also range checks if we have an access to constrained array.
1862 elsif Is_Access_Type
(Etype
(Lhs
))
1863 and then Is_Constrained
(Designated_Type
(Etype
(Lhs
)))
1865 if Has_Discriminants
(Designated_Type
(Etype
(Lhs
))) then
1867 -- Skip discriminant check if change of representation. Will be
1868 -- done when the change of representation is expanded out.
1871 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
));
1874 elsif Is_Array_Type
(Designated_Type
(Etype
(Lhs
))) then
1875 Apply_Range_Check
(Rhs
, Etype
(Lhs
));
1877 if Is_Constrained
(Etype
(Lhs
)) then
1878 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
1881 if Nkind
(Rhs
) = N_Allocator
then
1883 Target_Typ
: constant Entity_Id
:= Etype
(Expression
(Rhs
));
1884 C_Es
: Check_Result
;
1891 Etype
(Designated_Type
(Etype
(Lhs
))));
1903 -- Apply range check for access type case
1905 elsif Is_Access_Type
(Etype
(Lhs
))
1906 and then Nkind
(Rhs
) = N_Allocator
1907 and then Nkind
(Expression
(Rhs
)) = N_Qualified_Expression
1909 Analyze_And_Resolve
(Expression
(Rhs
));
1911 (Expression
(Rhs
), Designated_Type
(Etype
(Lhs
)));
1914 -- Ada 2005 (AI-231): Generate the run-time check
1916 if Is_Access_Type
(Typ
)
1917 and then Can_Never_Be_Null
(Etype
(Lhs
))
1918 and then not Can_Never_Be_Null
(Etype
(Rhs
))
1920 Apply_Constraint_Check
(Rhs
, Etype
(Lhs
));
1923 -- Ada 2012 (AI05-148): Update current accessibility level if Rhs is a
1924 -- stand-alone obj of an anonymous access type.
1926 if Is_Access_Type
(Typ
)
1927 and then Is_Entity_Name
(Lhs
)
1928 and then Present
(Effective_Extra_Accessibility
(Entity
(Lhs
))) then
1930 function Lhs_Entity
return Entity_Id
;
1931 -- Look through renames to find the underlying entity.
1932 -- For assignment to a rename, we don't care about the
1933 -- Enclosing_Dynamic_Scope of the rename declaration.
1939 function Lhs_Entity
return Entity_Id
is
1940 Result
: Entity_Id
:= Entity
(Lhs
);
1943 while Present
(Renamed_Object
(Result
)) loop
1945 -- Renamed_Object must return an Entity_Name here
1946 -- because of preceding "Present (E_E_A (...))" test.
1948 Result
:= Entity
(Renamed_Object
(Result
));
1954 -- Local Declarations
1956 Access_Check
: constant Node_Id
:=
1957 Make_Raise_Program_Error
(Loc
,
1961 Dynamic_Accessibility_Level
(Rhs
),
1963 Make_Integer_Literal
(Loc
,
1966 (Enclosing_Dynamic_Scope
1968 Reason
=> PE_Accessibility_Check_Failed
);
1970 Access_Level_Update
: constant Node_Id
:=
1971 Make_Assignment_Statement
(Loc
,
1974 (Effective_Extra_Accessibility
1975 (Entity
(Lhs
)), Loc
),
1977 Dynamic_Accessibility_Level
(Rhs
));
1980 if not Accessibility_Checks_Suppressed
(Entity
(Lhs
)) then
1981 Insert_Action
(N
, Access_Check
);
1984 Insert_Action
(N
, Access_Level_Update
);
1988 -- Case of assignment to a bit packed array element. If there is a
1989 -- change of representation this must be expanded into components,
1990 -- otherwise this is a bit-field assignment.
1992 if Nkind
(Lhs
) = N_Indexed_Component
1993 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
)))
1995 -- Normal case, no change of representation
1998 Expand_Bit_Packed_Element_Set
(N
);
2001 -- Change of representation case
2004 -- Generate the following, to force component-by-component
2005 -- assignments in an efficient way. Otherwise each component
2006 -- will require a temporary and two bit-field manipulations.
2013 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
2019 Make_Object_Declaration
(Loc
,
2020 Defining_Identifier
=> Tnn
,
2021 Object_Definition
=>
2022 New_Occurrence_Of
(Etype
(Lhs
), Loc
)),
2023 Make_Assignment_Statement
(Loc
,
2024 Name
=> New_Occurrence_Of
(Tnn
, Loc
),
2025 Expression
=> Relocate_Node
(Rhs
)),
2026 Make_Assignment_Statement
(Loc
,
2027 Name
=> Relocate_Node
(Lhs
),
2028 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
2030 Insert_Actions
(N
, Stats
);
2031 Rewrite
(N
, Make_Null_Statement
(Loc
));
2036 -- Build-in-place function call case. Note that we're not yet doing
2037 -- build-in-place for user-written assignment statements (the assignment
2038 -- here came from an aggregate.)
2040 elsif Ada_Version
>= Ada_2005
2041 and then Is_Build_In_Place_Function_Call
(Rhs
)
2043 Make_Build_In_Place_Call_In_Assignment
(N
, Rhs
);
2045 elsif Is_Tagged_Type
(Typ
) and then Is_Value_Type
(Etype
(Lhs
)) then
2047 -- Nothing to do for valuetypes
2048 -- ??? Set_Scope_Is_Transient (False);
2052 elsif Is_Tagged_Type
(Typ
)
2053 or else (Needs_Finalization
(Typ
) and then not Is_Array_Type
(Typ
))
2055 Tagged_Case
: declare
2056 L
: List_Id
:= No_List
;
2057 Expand_Ctrl_Actions
: constant Boolean := not No_Ctrl_Actions
(N
);
2060 -- In the controlled case, we ensure that function calls are
2061 -- evaluated before finalizing the target. In all cases, it makes
2062 -- the expansion easier if the side-effects are removed first.
2064 Remove_Side_Effects
(Lhs
);
2065 Remove_Side_Effects
(Rhs
);
2067 -- Avoid recursion in the mechanism
2071 -- If dispatching assignment, we need to dispatch to _assign
2073 if Is_Class_Wide_Type
(Typ
)
2075 -- If the type is tagged, we may as well use the predefined
2076 -- primitive assignment. This avoids inlining a lot of code
2077 -- and in the class-wide case, the assignment is replaced
2078 -- by a dispatching call to _assign. It is suppressed in the
2079 -- case of assignments created by the expander that correspond
2080 -- to initializations, where we do want to copy the tag
2081 -- (Expand_Ctrl_Actions flag is set True in this case). It is
2082 -- also suppressed if restriction No_Dispatching_Calls is in
2083 -- force because in that case predefined primitives are not
2086 or else (Is_Tagged_Type
(Typ
)
2087 and then not Is_Value_Type
(Etype
(Lhs
))
2088 and then Chars
(Current_Scope
) /= Name_uAssign
2089 and then Expand_Ctrl_Actions
2091 not Restriction_Active
(No_Dispatching_Calls
))
2093 if Is_Limited_Type
(Typ
) then
2095 -- This can happen in an instance when the formal is an
2096 -- extension of a limited interface, and the actual is
2097 -- limited. This is an error according to AI05-0087, but
2098 -- is not caught at the point of instantiation in earlier
2101 -- This is wrong, error messages cannot be issued during
2102 -- expansion, since they would be missed in -gnatc mode ???
2104 Error_Msg_N
("assignment not available on limited type", N
);
2108 -- Fetch the primitive op _assign and proper type to call it.
2109 -- Because of possible conflicts between private and full view,
2110 -- fetch the proper type directly from the operation profile.
2113 Op
: constant Entity_Id
:=
2114 Find_Prim_Op
(Typ
, Name_uAssign
);
2115 F_Typ
: Entity_Id
:= Etype
(First_Formal
(Op
));
2118 -- If the assignment is dispatching, make sure to use the
2121 if Is_Class_Wide_Type
(Typ
) then
2122 F_Typ
:= Class_Wide_Type
(F_Typ
);
2127 -- In case of assignment to a class-wide tagged type, before
2128 -- the assignment we generate run-time check to ensure that
2129 -- the tags of source and target match.
2131 if Is_Class_Wide_Type
(Typ
)
2132 and then Is_Tagged_Type
(Typ
)
2133 and then Is_Tagged_Type
(Underlying_Type
(Etype
(Rhs
)))
2136 Make_Raise_Constraint_Error
(Loc
,
2140 Make_Selected_Component
(Loc
,
2141 Prefix
=> Duplicate_Subexpr
(Lhs
),
2143 Make_Identifier
(Loc
, Name_uTag
)),
2145 Make_Selected_Component
(Loc
,
2146 Prefix
=> Duplicate_Subexpr
(Rhs
),
2148 Make_Identifier
(Loc
, Name_uTag
))),
2149 Reason
=> CE_Tag_Check_Failed
));
2153 Left_N
: Node_Id
:= Duplicate_Subexpr
(Lhs
);
2154 Right_N
: Node_Id
:= Duplicate_Subexpr
(Rhs
);
2157 -- In order to dispatch the call to _assign the type of
2158 -- the actuals must match. Add conversion (if required).
2160 if Etype
(Lhs
) /= F_Typ
then
2161 Left_N
:= Unchecked_Convert_To
(F_Typ
, Left_N
);
2164 if Etype
(Rhs
) /= F_Typ
then
2165 Right_N
:= Unchecked_Convert_To
(F_Typ
, Right_N
);
2169 Make_Procedure_Call_Statement
(Loc
,
2170 Name
=> New_Reference_To
(Op
, Loc
),
2171 Parameter_Associations
=> New_List
(
2173 Node2
=> Right_N
)));
2178 L
:= Make_Tag_Ctrl_Assignment
(N
);
2180 -- We can't afford to have destructive Finalization Actions in
2181 -- the Self assignment case, so if the target and the source
2182 -- are not obviously different, code is generated to avoid the
2183 -- self assignment case:
2185 -- if lhs'address /= rhs'address then
2186 -- <code for controlled and/or tagged assignment>
2189 -- Skip this if Restriction (No_Finalization) is active
2191 if not Statically_Different
(Lhs
, Rhs
)
2192 and then Expand_Ctrl_Actions
2193 and then not Restriction_Active
(No_Finalization
)
2196 Make_Implicit_If_Statement
(N
,
2200 Make_Attribute_Reference
(Loc
,
2201 Prefix
=> Duplicate_Subexpr
(Lhs
),
2202 Attribute_Name
=> Name_Address
),
2205 Make_Attribute_Reference
(Loc
,
2206 Prefix
=> Duplicate_Subexpr
(Rhs
),
2207 Attribute_Name
=> Name_Address
)),
2209 Then_Statements
=> L
));
2212 -- We need to set up an exception handler for implementing
2213 -- 7.6.1(18). The remaining adjustments are tackled by the
2214 -- implementation of adjust for record_controllers (see
2217 -- This is skipped if we have no finalization
2219 if Expand_Ctrl_Actions
2220 and then not Restriction_Active
(No_Finalization
)
2223 Make_Block_Statement
(Loc
,
2224 Handled_Statement_Sequence
=>
2225 Make_Handled_Sequence_Of_Statements
(Loc
,
2227 Exception_Handlers
=> New_List
(
2228 Make_Handler_For_Ctrl_Operation
(Loc
)))));
2233 Make_Block_Statement
(Loc
,
2234 Handled_Statement_Sequence
=>
2235 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> L
)));
2237 -- If no restrictions on aborts, protect the whole assignment
2238 -- for controlled objects as per 9.8(11).
2240 if Needs_Finalization
(Typ
)
2241 and then Expand_Ctrl_Actions
2242 and then Abort_Allowed
2245 Blk
: constant Entity_Id
:=
2247 (E_Block
, Current_Scope
, Sloc
(N
), 'B');
2250 Set_Scope
(Blk
, Current_Scope
);
2251 Set_Etype
(Blk
, Standard_Void_Type
);
2252 Set_Identifier
(N
, New_Occurrence_Of
(Blk
, Sloc
(N
)));
2254 Prepend_To
(L
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
2255 Set_At_End_Proc
(Handled_Statement_Sequence
(N
),
2256 New_Occurrence_Of
(RTE
(RE_Abort_Undefer_Direct
), Loc
));
2257 Expand_At_End_Handler
2258 (Handled_Statement_Sequence
(N
), Blk
);
2262 -- N has been rewritten to a block statement for which it is
2263 -- known by construction that no checks are necessary: analyze
2264 -- it with all checks suppressed.
2266 Analyze
(N
, Suppress
=> All_Checks
);
2272 elsif Is_Array_Type
(Typ
) then
2274 Actual_Rhs
: Node_Id
:= Rhs
;
2277 while Nkind_In
(Actual_Rhs
, N_Type_Conversion
,
2278 N_Qualified_Expression
)
2280 Actual_Rhs
:= Expression
(Actual_Rhs
);
2283 Expand_Assign_Array
(N
, Actual_Rhs
);
2289 elsif Is_Record_Type
(Typ
) then
2290 Expand_Assign_Record
(N
);
2293 -- Scalar types. This is where we perform the processing related to the
2294 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2297 elsif Is_Scalar_Type
(Typ
) then
2299 -- Case where right side is known valid
2301 if Expr_Known_Valid
(Rhs
) then
2303 -- Here the right side is valid, so it is fine. The case to deal
2304 -- with is when the left side is a local variable reference whose
2305 -- value is not currently known to be valid. If this is the case,
2306 -- and the assignment appears in an unconditional context, then
2307 -- we can mark the left side as now being valid if one of these
2308 -- conditions holds:
2310 -- The expression of the right side has Do_Range_Check set so
2311 -- that we know a range check will be performed. Note that it
2312 -- can be the case that a range check is omitted because we
2313 -- make the assumption that we can assume validity for operands
2314 -- appearing in the right side in determining whether a range
2315 -- check is required
2317 -- The subtype of the right side matches the subtype of the
2318 -- left side. In this case, even though we have not checked
2319 -- the range of the right side, we know it is in range of its
2320 -- subtype if the expression is valid.
2322 if Is_Local_Variable_Reference
(Lhs
)
2323 and then not Is_Known_Valid
(Entity
(Lhs
))
2324 and then In_Unconditional_Context
(N
)
2326 if Do_Range_Check
(Rhs
)
2327 or else Etype
(Lhs
) = Etype
(Rhs
)
2329 Set_Is_Known_Valid
(Entity
(Lhs
), True);
2333 -- Case where right side may be invalid in the sense of the RM
2334 -- reference above. The RM does not require that we check for the
2335 -- validity on an assignment, but it does require that the assignment
2336 -- of an invalid value not cause erroneous behavior.
2338 -- The general approach in GNAT is to use the Is_Known_Valid flag
2339 -- to avoid the need for validity checking on assignments. However
2340 -- in some cases, we have to do validity checking in order to make
2341 -- sure that the setting of this flag is correct.
2344 -- Validate right side if we are validating copies
2346 if Validity_Checks_On
2347 and then Validity_Check_Copies
2349 -- Skip this if left hand side is an array or record component
2350 -- and elementary component validity checks are suppressed.
2352 if Nkind_In
(Lhs
, N_Selected_Component
, N_Indexed_Component
)
2353 and then not Validity_Check_Components
2360 -- We can propagate this to the left side where appropriate
2362 if Is_Local_Variable_Reference
(Lhs
)
2363 and then not Is_Known_Valid
(Entity
(Lhs
))
2364 and then In_Unconditional_Context
(N
)
2366 Set_Is_Known_Valid
(Entity
(Lhs
), True);
2369 -- Otherwise check to see what should be done
2371 -- If left side is a local variable, then we just set its flag to
2372 -- indicate that its value may no longer be valid, since we are
2373 -- copying a potentially invalid value.
2375 elsif Is_Local_Variable_Reference
(Lhs
) then
2376 Set_Is_Known_Valid
(Entity
(Lhs
), False);
2378 -- Check for case of a nonlocal variable on the left side which
2379 -- is currently known to be valid. In this case, we simply ensure
2380 -- that the right side is valid. We only play the game of copying
2381 -- validity status for local variables, since we are doing this
2382 -- statically, not by tracing the full flow graph.
2384 elsif Is_Entity_Name
(Lhs
)
2385 and then Is_Known_Valid
(Entity
(Lhs
))
2387 -- Note: If Validity_Checking mode is set to none, we ignore
2388 -- the Ensure_Valid call so don't worry about that case here.
2392 -- In all other cases, we can safely copy an invalid value without
2393 -- worrying about the status of the left side. Since it is not a
2394 -- variable reference it will not be considered
2395 -- as being known to be valid in any case.
2404 when RE_Not_Available
=>
2406 end Expand_N_Assignment_Statement
;
2408 ------------------------------
2409 -- Expand_N_Block_Statement --
2410 ------------------------------
2412 -- Encode entity names defined in block statement
2414 procedure Expand_N_Block_Statement
(N
: Node_Id
) is
2416 Qualify_Entity_Names
(N
);
2417 end Expand_N_Block_Statement
;
2419 -----------------------------
2420 -- Expand_N_Case_Statement --
2421 -----------------------------
2423 procedure Expand_N_Case_Statement
(N
: Node_Id
) is
2424 Loc
: constant Source_Ptr
:= Sloc
(N
);
2425 Expr
: constant Node_Id
:= Expression
(N
);
2433 -- Check for the situation where we know at compile time which branch
2436 if Compile_Time_Known_Value
(Expr
) then
2437 Alt
:= Find_Static_Alternative
(N
);
2439 Process_Statements_For_Controlled_Objects
(Alt
);
2441 -- Move statements from this alternative after the case statement.
2442 -- They are already analyzed, so will be skipped by the analyzer.
2444 Insert_List_After
(N
, Statements
(Alt
));
2446 -- That leaves the case statement as a shell. So now we can kill all
2447 -- other alternatives in the case statement.
2449 Kill_Dead_Code
(Expression
(N
));
2455 -- Loop through case alternatives, skipping pragmas, and skipping
2456 -- the one alternative that we select (and therefore retain).
2458 Dead_Alt
:= First
(Alternatives
(N
));
2459 while Present
(Dead_Alt
) loop
2461 and then Nkind
(Dead_Alt
) = N_Case_Statement_Alternative
2463 Kill_Dead_Code
(Statements
(Dead_Alt
), Warn_On_Deleted_Code
);
2470 Rewrite
(N
, Make_Null_Statement
(Loc
));
2474 -- Here if the choice is not determined at compile time
2477 Last_Alt
: constant Node_Id
:= Last
(Alternatives
(N
));
2479 Others_Present
: Boolean;
2480 Others_Node
: Node_Id
;
2482 Then_Stms
: List_Id
;
2483 Else_Stms
: List_Id
;
2486 if Nkind
(First
(Discrete_Choices
(Last_Alt
))) = N_Others_Choice
then
2487 Others_Present
:= True;
2488 Others_Node
:= Last_Alt
;
2490 Others_Present
:= False;
2493 -- First step is to worry about possible invalid argument. The RM
2494 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2495 -- outside the base range), then Constraint_Error must be raised.
2497 -- Case of validity check required (validity checks are on, the
2498 -- expression is not known to be valid, and the case statement
2499 -- comes from source -- no need to validity check internally
2500 -- generated case statements).
2502 if Validity_Check_Default
then
2503 Ensure_Valid
(Expr
);
2506 -- If there is only a single alternative, just replace it with the
2507 -- sequence of statements since obviously that is what is going to
2508 -- be executed in all cases.
2510 Len
:= List_Length
(Alternatives
(N
));
2514 -- We still need to evaluate the expression if it has any side
2517 Remove_Side_Effects
(Expression
(N
));
2519 Alt
:= First
(Alternatives
(N
));
2521 Process_Statements_For_Controlled_Objects
(Alt
);
2522 Insert_List_After
(N
, Statements
(Alt
));
2524 -- That leaves the case statement as a shell. The alternative that
2525 -- will be executed is reset to a null list. So now we can kill
2526 -- the entire case statement.
2528 Kill_Dead_Code
(Expression
(N
));
2529 Rewrite
(N
, Make_Null_Statement
(Loc
));
2532 -- An optimization. If there are only two alternatives, and only
2533 -- a single choice, then rewrite the whole case statement as an
2534 -- if statement, since this can result in subsequent optimizations.
2535 -- This helps not only with case statements in the source of a
2536 -- simple form, but also with generated code (discriminant check
2537 -- functions in particular)
2540 Chlist
:= Discrete_Choices
(First
(Alternatives
(N
)));
2542 if List_Length
(Chlist
) = 1 then
2543 Choice
:= First
(Chlist
);
2545 Then_Stms
:= Statements
(First
(Alternatives
(N
)));
2546 Else_Stms
:= Statements
(Last
(Alternatives
(N
)));
2548 -- For TRUE, generate "expression", not expression = true
2550 if Nkind
(Choice
) = N_Identifier
2551 and then Entity
(Choice
) = Standard_True
2553 Cond
:= Expression
(N
);
2555 -- For FALSE, generate "expression" and switch then/else
2557 elsif Nkind
(Choice
) = N_Identifier
2558 and then Entity
(Choice
) = Standard_False
2560 Cond
:= Expression
(N
);
2561 Else_Stms
:= Statements
(First
(Alternatives
(N
)));
2562 Then_Stms
:= Statements
(Last
(Alternatives
(N
)));
2564 -- For a range, generate "expression in range"
2566 elsif Nkind
(Choice
) = N_Range
2567 or else (Nkind
(Choice
) = N_Attribute_Reference
2568 and then Attribute_Name
(Choice
) = Name_Range
)
2569 or else (Is_Entity_Name
(Choice
)
2570 and then Is_Type
(Entity
(Choice
)))
2571 or else Nkind
(Choice
) = N_Subtype_Indication
2575 Left_Opnd
=> Expression
(N
),
2576 Right_Opnd
=> Relocate_Node
(Choice
));
2578 -- For any other subexpression "expression = value"
2583 Left_Opnd
=> Expression
(N
),
2584 Right_Opnd
=> Relocate_Node
(Choice
));
2587 -- Now rewrite the case as an IF
2590 Make_If_Statement
(Loc
,
2592 Then_Statements
=> Then_Stms
,
2593 Else_Statements
=> Else_Stms
));
2599 -- If the last alternative is not an Others choice, replace it with
2600 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2601 -- the modified case statement, since it's only effect would be to
2602 -- compute the contents of the Others_Discrete_Choices which is not
2603 -- needed by the back end anyway.
2605 -- The reason we do this is that the back end always needs some
2606 -- default for a switch, so if we have not supplied one in the
2607 -- processing above for validity checking, then we need to supply
2610 if not Others_Present
then
2611 Others_Node
:= Make_Others_Choice
(Sloc
(Last_Alt
));
2612 Set_Others_Discrete_Choices
2613 (Others_Node
, Discrete_Choices
(Last_Alt
));
2614 Set_Discrete_Choices
(Last_Alt
, New_List
(Others_Node
));
2617 Alt
:= First
(Alternatives
(N
));
2619 and then Nkind
(Alt
) = N_Case_Statement_Alternative
2621 Process_Statements_For_Controlled_Objects
(Alt
);
2625 end Expand_N_Case_Statement
;
2627 -----------------------------
2628 -- Expand_N_Exit_Statement --
2629 -----------------------------
2631 -- The only processing required is to deal with a possible C/Fortran
2632 -- boolean value used as the condition for the exit statement.
2634 procedure Expand_N_Exit_Statement
(N
: Node_Id
) is
2636 Adjust_Condition
(Condition
(N
));
2637 end Expand_N_Exit_Statement
;
2639 -----------------------------
2640 -- Expand_N_Goto_Statement --
2641 -----------------------------
2643 -- Add poll before goto if polling active
2645 procedure Expand_N_Goto_Statement
(N
: Node_Id
) is
2647 Generate_Poll_Call
(N
);
2648 end Expand_N_Goto_Statement
;
2650 ---------------------------
2651 -- Expand_N_If_Statement --
2652 ---------------------------
2654 -- First we deal with the case of C and Fortran convention boolean values,
2655 -- with zero/non-zero semantics.
2657 -- Second, we deal with the obvious rewriting for the cases where the
2658 -- condition of the IF is known at compile time to be True or False.
2660 -- Third, we remove elsif parts which have non-empty Condition_Actions and
2661 -- rewrite as independent if statements. For example:
2672 -- <<condition actions of y>>
2678 -- This rewriting is needed if at least one elsif part has a non-empty
2679 -- Condition_Actions list. We also do the same processing if there is a
2680 -- constant condition in an elsif part (in conjunction with the first
2681 -- processing step mentioned above, for the recursive call made to deal
2682 -- with the created inner if, this deals with properly optimizing the
2683 -- cases of constant elsif conditions).
2685 procedure Expand_N_If_Statement
(N
: Node_Id
) is
2686 Loc
: constant Source_Ptr
:= Sloc
(N
);
2691 Warn_If_Deleted
: constant Boolean :=
2692 Warn_On_Deleted_Code
and then Comes_From_Source
(N
);
2693 -- Indicates whether we want warnings when we delete branches of the
2694 -- if statement based on constant condition analysis. We never want
2695 -- these warnings for expander generated code.
2698 Process_Statements_For_Controlled_Objects
(N
);
2700 Adjust_Condition
(Condition
(N
));
2702 -- The following loop deals with constant conditions for the IF. We
2703 -- need a loop because as we eliminate False conditions, we grab the
2704 -- first elsif condition and use it as the primary condition.
2706 while Compile_Time_Known_Value
(Condition
(N
)) loop
2708 -- If condition is True, we can simply rewrite the if statement now
2709 -- by replacing it by the series of then statements.
2711 if Is_True
(Expr_Value
(Condition
(N
))) then
2713 -- All the else parts can be killed
2715 Kill_Dead_Code
(Elsif_Parts
(N
), Warn_If_Deleted
);
2716 Kill_Dead_Code
(Else_Statements
(N
), Warn_If_Deleted
);
2718 Hed
:= Remove_Head
(Then_Statements
(N
));
2719 Insert_List_After
(N
, Then_Statements
(N
));
2723 -- If condition is False, then we can delete the condition and
2724 -- the Then statements
2727 -- We do not delete the condition if constant condition warnings
2728 -- are enabled, since otherwise we end up deleting the desired
2729 -- warning. Of course the backend will get rid of this True/False
2730 -- test anyway, so nothing is lost here.
2732 if not Constant_Condition_Warnings
then
2733 Kill_Dead_Code
(Condition
(N
));
2736 Kill_Dead_Code
(Then_Statements
(N
), Warn_If_Deleted
);
2738 -- If there are no elsif statements, then we simply replace the
2739 -- entire if statement by the sequence of else statements.
2741 if No
(Elsif_Parts
(N
)) then
2742 if No
(Else_Statements
(N
))
2743 or else Is_Empty_List
(Else_Statements
(N
))
2746 Make_Null_Statement
(Sloc
(N
)));
2748 Hed
:= Remove_Head
(Else_Statements
(N
));
2749 Insert_List_After
(N
, Else_Statements
(N
));
2755 -- If there are elsif statements, the first of them becomes the
2756 -- if/then section of the rebuilt if statement This is the case
2757 -- where we loop to reprocess this copied condition.
2760 Hed
:= Remove_Head
(Elsif_Parts
(N
));
2761 Insert_Actions
(N
, Condition_Actions
(Hed
));
2762 Set_Condition
(N
, Condition
(Hed
));
2763 Set_Then_Statements
(N
, Then_Statements
(Hed
));
2765 -- Hed might have been captured as the condition determining
2766 -- the current value for an entity. Now it is detached from
2767 -- the tree, so a Current_Value pointer in the condition might
2768 -- need to be updated.
2770 Set_Current_Value_Condition
(N
);
2772 if Is_Empty_List
(Elsif_Parts
(N
)) then
2773 Set_Elsif_Parts
(N
, No_List
);
2779 -- Loop through elsif parts, dealing with constant conditions and
2780 -- possible condition actions that are present.
2782 if Present
(Elsif_Parts
(N
)) then
2783 E
:= First
(Elsif_Parts
(N
));
2784 while Present
(E
) loop
2785 Process_Statements_For_Controlled_Objects
(E
);
2787 Adjust_Condition
(Condition
(E
));
2789 -- If there are condition actions, then rewrite the if statement
2790 -- as indicated above. We also do the same rewrite for a True or
2791 -- False condition. The further processing of this constant
2792 -- condition is then done by the recursive call to expand the
2793 -- newly created if statement
2795 if Present
(Condition_Actions
(E
))
2796 or else Compile_Time_Known_Value
(Condition
(E
))
2798 -- Note this is not an implicit if statement, since it is part
2799 -- of an explicit if statement in the source (or of an implicit
2800 -- if statement that has already been tested).
2803 Make_If_Statement
(Sloc
(E
),
2804 Condition
=> Condition
(E
),
2805 Then_Statements
=> Then_Statements
(E
),
2806 Elsif_Parts
=> No_List
,
2807 Else_Statements
=> Else_Statements
(N
));
2809 -- Elsif parts for new if come from remaining elsif's of parent
2811 while Present
(Next
(E
)) loop
2812 if No
(Elsif_Parts
(New_If
)) then
2813 Set_Elsif_Parts
(New_If
, New_List
);
2816 Append
(Remove_Next
(E
), Elsif_Parts
(New_If
));
2819 Set_Else_Statements
(N
, New_List
(New_If
));
2821 if Present
(Condition_Actions
(E
)) then
2822 Insert_List_Before
(New_If
, Condition_Actions
(E
));
2827 if Is_Empty_List
(Elsif_Parts
(N
)) then
2828 Set_Elsif_Parts
(N
, No_List
);
2834 -- No special processing for that elsif part, move to next
2842 -- Some more optimizations applicable if we still have an IF statement
2844 if Nkind
(N
) /= N_If_Statement
then
2848 -- Another optimization, special cases that can be simplified
2850 -- if expression then
2856 -- can be changed to:
2858 -- return expression;
2862 -- if expression then
2868 -- can be changed to:
2870 -- return not (expression);
2872 -- Only do these optimizations if we are at least at -O1 level and
2873 -- do not do them if control flow optimizations are suppressed.
2875 if Optimization_Level
> 0
2876 and then not Opt
.Suppress_Control_Flow_Optimizations
2878 if Nkind
(N
) = N_If_Statement
2879 and then No
(Elsif_Parts
(N
))
2880 and then Present
(Else_Statements
(N
))
2881 and then List_Length
(Then_Statements
(N
)) = 1
2882 and then List_Length
(Else_Statements
(N
)) = 1
2885 Then_Stm
: constant Node_Id
:= First
(Then_Statements
(N
));
2886 Else_Stm
: constant Node_Id
:= First
(Else_Statements
(N
));
2889 if Nkind
(Then_Stm
) = N_Simple_Return_Statement
2891 Nkind
(Else_Stm
) = N_Simple_Return_Statement
2894 Then_Expr
: constant Node_Id
:= Expression
(Then_Stm
);
2895 Else_Expr
: constant Node_Id
:= Expression
(Else_Stm
);
2898 if Nkind
(Then_Expr
) = N_Identifier
2900 Nkind
(Else_Expr
) = N_Identifier
2902 if Entity
(Then_Expr
) = Standard_True
2903 and then Entity
(Else_Expr
) = Standard_False
2906 Make_Simple_Return_Statement
(Loc
,
2907 Expression
=> Relocate_Node
(Condition
(N
))));
2911 elsif Entity
(Then_Expr
) = Standard_False
2912 and then Entity
(Else_Expr
) = Standard_True
2915 Make_Simple_Return_Statement
(Loc
,
2919 Relocate_Node
(Condition
(N
)))));
2929 end Expand_N_If_Statement
;
2931 --------------------------
2932 -- Expand_Iterator_Loop --
2933 --------------------------
2935 procedure Expand_Iterator_Loop
(N
: Node_Id
) is
2936 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
2937 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
2938 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
2939 Loc
: constant Source_Ptr
:= Sloc
(N
);
2941 Container
: constant Node_Id
:= Name
(I_Spec
);
2942 Container_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
2944 Iterator
: Entity_Id
;
2946 Stats
: List_Id
:= Statements
(N
);
2949 -- Processing for arrays
2951 if Is_Array_Type
(Container_Typ
) then
2952 Expand_Iterator_Loop_Over_Array
(N
);
2956 -- Processing for containers
2958 -- For an "of" iterator the name is a container expression, which
2959 -- is transformed into a call to the default iterator.
2961 -- For an iterator of the form "in" the name is a function call
2962 -- that delivers an iterator type.
2964 -- In both cases, analysis of the iterator has introduced an object
2965 -- declaration to capture the domain, so that Container is an entity.
2967 -- The for loop is expanded into a while loop which uses a container
2968 -- specific cursor to desgnate each element.
2970 -- Iter : Iterator_Type := Container.Iterate;
2971 -- Cursor : Cursor_type := First (Iter);
2972 -- while Has_Element (Iter) loop
2974 -- -- The block is added when Element_Type is controlled
2976 -- Obj : Pack.Element_Type := Element (Cursor);
2977 -- -- for the "of" loop form
2979 -- <original loop statements>
2982 -- Cursor := Iter.Next (Cursor);
2985 -- If "reverse" is present, then the initialization of the cursor
2986 -- uses Last and the step becomes Prev. Pack is the name of the
2987 -- scope where the container package is instantiated.
2990 Element_Type
: constant Entity_Id
:= Etype
(Id
);
2991 Iter_Type
: Entity_Id
;
2994 Name_Init
: Name_Id
;
2995 Name_Step
: Name_Id
;
2998 -- The type of the iterator is the return type of the Iterate
2999 -- function used. For the "of" form this is the default iterator
3000 -- for the type, otherwise it is the type of the explicit
3001 -- function used in the iterator specification. The most common
3002 -- case will be an Iterate function in the container package.
3004 -- The primitive operations of the container type may not be
3005 -- use-visible, so we introduce the name of the enclosing package
3006 -- in the declarations below. The Iterator type is declared in a
3007 -- an instance within the container package itself.
3009 -- If the container type is a derived type, the cursor type is
3010 -- found in the package of the parent type.
3012 if Is_Derived_Type
(Container_Typ
) then
3013 Pack
:= Scope
(Root_Type
(Container_Typ
));
3015 Pack
:= Scope
(Container_Typ
);
3018 Iter_Type
:= Etype
(Name
(I_Spec
));
3020 -- The "of" case uses an internally generated cursor whose type
3021 -- is found in the container package. The domain of iteration
3022 -- is expanded into a call to the default Iterator function, but
3023 -- this expansion does not take place in quantified expressions
3024 -- that are analyzed with expansion disabled, and in that case the
3025 -- type of the iterator must be obtained from the aspect.
3027 if Of_Present
(I_Spec
) then
3029 Default_Iter
: constant Entity_Id
:=
3033 Aspect_Default_Iterator
));
3035 Container_Arg
: Node_Id
;
3039 Cursor
:= Make_Temporary
(Loc
, 'I');
3041 -- For an container element iterator, the iterator type
3042 -- is obtained from the corresponding aspect, whose return
3043 -- type is descended from the corresponding interface type
3044 -- in some instance of Ada.Iterator_Interfaces. The actuals
3045 -- of that instantiation are Cursor and Has_Element.
3047 Iter_Type
:= Etype
(Default_Iter
);
3049 -- The iterator type, which is a class_wide type, may itself
3050 -- be derived locally, so the desired instantiation is the
3051 -- scope of the root type of the iterator type.
3053 Pack
:= Scope
(Root_Type
(Etype
(Iter_Type
)));
3055 -- Rewrite domain of iteration as a call to the default
3056 -- iterator for the container type. If the container is
3057 -- a derived type and the aspect is inherited, convert
3058 -- container to parent type. The Cursor type is also
3059 -- inherited from the scope of the parent.
3061 if Base_Type
(Etype
(Container
)) =
3062 Base_Type
(Etype
(First_Formal
(Default_Iter
)))
3064 Container_Arg
:= New_Copy_Tree
(Container
);
3068 Make_Type_Conversion
(Loc
,
3071 (Etype
(First_Formal
(Default_Iter
)), Loc
),
3072 Expression
=> New_Copy_Tree
(Container
));
3075 Rewrite
(Name
(I_Spec
),
3076 Make_Function_Call
(Loc
,
3077 Name
=> New_Occurrence_Of
(Default_Iter
, Loc
),
3078 Parameter_Associations
=>
3079 New_List
(Container_Arg
)));
3080 Analyze_And_Resolve
(Name
(I_Spec
));
3082 -- Find cursor type in proper iterator package, which is an
3083 -- instantiation of Iterator_Interfaces.
3085 Ent
:= First_Entity
(Pack
);
3086 while Present
(Ent
) loop
3087 if Chars
(Ent
) = Name_Cursor
then
3088 Set_Etype
(Cursor
, Etype
(Ent
));
3095 -- Id : Element_Type renames Container (Cursor);
3096 -- This assumes that the container type has an indexing
3097 -- operation with Cursor. The check that this operation
3098 -- exists is performed in Check_Container_Indexing.
3101 Make_Object_Renaming_Declaration
(Loc
,
3102 Defining_Identifier
=> Id
,
3104 New_Reference_To
(Element_Type
, Loc
),
3106 Make_Indexed_Component
(Loc
,
3107 Prefix
=> Relocate_Node
(Container_Arg
),
3109 New_List
(New_Occurrence_Of
(Cursor
, Loc
))));
3111 -- If the container holds controlled objects, wrap the loop
3112 -- statements and element renaming declaration with a block.
3113 -- This ensures that the result of Element (Cusor) is
3114 -- cleaned up after each iteration of the loop.
3116 if Needs_Finalization
(Element_Type
) then
3120 -- Id : Element_Type := Element (curosr);
3122 -- <original loop statements>
3126 Make_Block_Statement
(Loc
,
3127 Declarations
=> New_List
(Decl
),
3128 Handled_Statement_Sequence
=>
3129 Make_Handled_Sequence_Of_Statements
(Loc
,
3130 Statements
=> Stats
)));
3132 -- Elements do not need finalization
3135 Prepend_To
(Stats
, Decl
);
3139 -- X in Iterate (S) : type of iterator is type of explicitly
3140 -- given Iterate function, and the loop variable is the cursor.
3141 -- It will be assigned in the loop and must be a variable.
3145 Set_Ekind
(Cursor
, E_Variable
);
3148 Iterator
:= Make_Temporary
(Loc
, 'I');
3150 -- Determine the advancement and initialization steps for the
3153 -- Analysis of the expanded loop will verify that the container
3154 -- has a reverse iterator.
3156 if Reverse_Present
(I_Spec
) then
3157 Name_Init
:= Name_Last
;
3158 Name_Step
:= Name_Previous
;
3161 Name_Init
:= Name_First
;
3162 Name_Step
:= Name_Next
;
3165 -- For both iterator forms, add a call to the step operation to
3166 -- advance the cursor. Generate:
3168 -- Cursor := Iterator.Next (Cursor);
3172 -- Cursor := Next (Cursor);
3179 Make_Function_Call
(Loc
,
3181 Make_Selected_Component
(Loc
,
3182 Prefix
=> New_Reference_To
(Iterator
, Loc
),
3183 Selector_Name
=> Make_Identifier
(Loc
, Name_Step
)),
3184 Parameter_Associations
=> New_List
(
3185 New_Reference_To
(Cursor
, Loc
)));
3188 Make_Assignment_Statement
(Loc
,
3189 Name
=> New_Occurrence_Of
(Cursor
, Loc
),
3190 Expression
=> Rhs
));
3194 -- while Iterator.Has_Element loop
3198 -- Has_Element is the second actual in the iterator package
3201 Make_Loop_Statement
(Loc
,
3203 Make_Iteration_Scheme
(Loc
,
3205 Make_Function_Call
(Loc
,
3208 Next_Entity
(First_Entity
(Pack
)), Loc
),
3209 Parameter_Associations
=>
3210 New_List
(New_Reference_To
(Cursor
, Loc
)))),
3212 Statements
=> Stats
,
3213 End_Label
=> Empty
);
3215 -- If present, preserve identifier of loop, which can be used in
3216 -- an exit statement in the body.
3218 if Present
(Identifier
(N
)) then
3219 Set_Identifier
(New_Loop
, Relocate_Node
(Identifier
(N
)));
3222 -- Create the declarations for Iterator and cursor and insert them
3223 -- before the source loop. Given that the domain of iteration is
3224 -- already an entity, the iterator is just a renaming of that
3225 -- entity. Possible optimization ???
3228 -- I : Iterator_Type renames Container;
3229 -- C : Cursor_Type := Container.[First | Last];
3232 Make_Object_Renaming_Declaration
(Loc
,
3233 Defining_Identifier
=> Iterator
,
3234 Subtype_Mark
=> New_Occurrence_Of
(Iter_Type
, Loc
),
3235 Name
=> Relocate_Node
(Name
(I_Spec
))));
3237 -- Create declaration for cursor
3244 Make_Object_Declaration
(Loc
,
3245 Defining_Identifier
=> Cursor
,
3246 Object_Definition
=>
3247 New_Occurrence_Of
(Etype
(Cursor
), Loc
),
3249 Make_Selected_Component
(Loc
,
3250 Prefix
=> New_Reference_To
(Iterator
, Loc
),
3252 Make_Identifier
(Loc
, Name_Init
)));
3254 -- The cursor is only modified in expanded code, so it appears
3255 -- as unassigned to the warning machinery. We must suppress
3256 -- this spurious warning explicitly.
3258 Set_Warnings_Off
(Cursor
);
3259 Set_Assignment_OK
(Decl
);
3261 Insert_Action
(N
, Decl
);
3264 -- If the range of iteration is given by a function call that
3265 -- returns a container, the finalization actions have been saved
3266 -- in the Condition_Actions of the iterator. Insert them now at
3267 -- the head of the loop.
3269 if Present
(Condition_Actions
(Isc
)) then
3270 Insert_List_Before
(N
, Condition_Actions
(Isc
));
3274 Rewrite
(N
, New_Loop
);
3276 end Expand_Iterator_Loop
;
3278 -------------------------------------
3279 -- Expand_Iterator_Loop_Over_Array --
3280 -------------------------------------
3282 procedure Expand_Iterator_Loop_Over_Array
(N
: Node_Id
) is
3283 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3284 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
3285 Array_Node
: constant Node_Id
:= Name
(I_Spec
);
3286 Array_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Array_Node
));
3287 Array_Dim
: constant Pos
:= Number_Dimensions
(Array_Typ
);
3288 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3289 Loc
: constant Source_Ptr
:= Sloc
(N
);
3290 Stats
: constant List_Id
:= Statements
(N
);
3291 Core_Loop
: Node_Id
;
3293 Iterator
: Entity_Id
;
3295 -- Start of processing for Expand_Iterator_Loop_Over_Array
3298 -- for Element of Array loop
3300 -- This case requires an internally generated cursor to iterate over
3303 if Of_Present
(I_Spec
) then
3304 Iterator
:= Make_Temporary
(Loc
, 'C');
3307 -- Element : Component_Type renames Array (Iterator);
3310 Make_Indexed_Component
(Loc
,
3311 Prefix
=> Relocate_Node
(Array_Node
),
3312 Expressions
=> New_List
(New_Reference_To
(Iterator
, Loc
)));
3315 Make_Object_Renaming_Declaration
(Loc
,
3316 Defining_Identifier
=> Id
,
3318 New_Reference_To
(Component_Type
(Array_Typ
), Loc
),
3321 -- Mark the loop variable as needing debug info, so that expansion
3322 -- of the renaming will result in Materialize_Entity getting set via
3323 -- Debug_Renaming_Declaration. (This setting is needed here because
3324 -- the setting in Freeze_Entity comes after the expansion, which is
3327 Set_Debug_Info_Needed
(Id
);
3329 -- for Index in Array loop
3331 -- This case utilizes the already given iterator name
3339 -- for Iterator in [reverse] Array'Range (Array_Dim) loop
3340 -- Element : Component_Type renames Array (Iterator);
3341 -- <original loop statements>
3345 Make_Loop_Statement
(Loc
,
3347 Make_Iteration_Scheme
(Loc
,
3348 Loop_Parameter_Specification
=>
3349 Make_Loop_Parameter_Specification
(Loc
,
3350 Defining_Identifier
=> Iterator
,
3351 Discrete_Subtype_Definition
=>
3352 Make_Attribute_Reference
(Loc
,
3353 Prefix
=> Relocate_Node
(Array_Node
),
3354 Attribute_Name
=> Name_Range
,
3355 Expressions
=> New_List
(
3356 Make_Integer_Literal
(Loc
, Array_Dim
))),
3357 Reverse_Present
=> Reverse_Present
(I_Spec
))),
3358 Statements
=> Stats
,
3359 End_Label
=> Empty
);
3361 -- Processing for multidimensional array
3363 if Array_Dim
> 1 then
3364 for Dim
in 1 .. Array_Dim
- 1 loop
3365 Iterator
:= Make_Temporary
(Loc
, 'C');
3367 -- Generate the dimension loops starting from the innermost one
3369 -- for Iterator in [reverse] Array'Range (Array_Dim - Dim) loop
3374 Make_Loop_Statement
(Loc
,
3376 Make_Iteration_Scheme
(Loc
,
3377 Loop_Parameter_Specification
=>
3378 Make_Loop_Parameter_Specification
(Loc
,
3379 Defining_Identifier
=> Iterator
,
3380 Discrete_Subtype_Definition
=>
3381 Make_Attribute_Reference
(Loc
,
3382 Prefix
=> Relocate_Node
(Array_Node
),
3383 Attribute_Name
=> Name_Range
,
3384 Expressions
=> New_List
(
3385 Make_Integer_Literal
(Loc
, Array_Dim
- Dim
))),
3386 Reverse_Present
=> Reverse_Present
(I_Spec
))),
3387 Statements
=> New_List
(Core_Loop
),
3388 End_Label
=> Empty
);
3390 -- Update the previously created object renaming declaration with
3391 -- the new iterator.
3393 Prepend_To
(Expressions
(Ind_Comp
),
3394 New_Reference_To
(Iterator
, Loc
));
3398 -- If original loop has a name, preserve it so it can be recognized by
3399 -- an exit statement in the body of the rewritten loop.
3401 if Present
(Identifier
(N
)) then
3402 Set_Identifier
(Core_Loop
, Relocate_Node
(Identifier
(N
)));
3405 Rewrite
(N
, Core_Loop
);
3407 end Expand_Iterator_Loop_Over_Array
;
3409 -----------------------------
3410 -- Expand_N_Loop_Statement --
3411 -----------------------------
3413 -- 1. Remove null loop entirely
3414 -- 2. Deal with while condition for C/Fortran boolean
3415 -- 3. Deal with loops with a non-standard enumeration type range
3416 -- 4. Deal with while loops where Condition_Actions is set
3417 -- 5. Deal with loops over predicated subtypes
3418 -- 6. Deal with loops with iterators over arrays and containers
3419 -- 7. Insert polling call if required
3421 procedure Expand_N_Loop_Statement
(N
: Node_Id
) is
3422 Loc
: constant Source_Ptr
:= Sloc
(N
);
3423 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3428 if Is_Null_Loop
(N
) then
3429 Rewrite
(N
, Make_Null_Statement
(Loc
));
3433 Process_Statements_For_Controlled_Objects
(N
);
3435 -- Deal with condition for C/Fortran Boolean
3437 if Present
(Isc
) then
3438 Adjust_Condition
(Condition
(Isc
));
3441 -- Generate polling call
3443 if Is_Non_Empty_List
(Statements
(N
)) then
3444 Generate_Poll_Call
(First
(Statements
(N
)));
3447 -- Nothing more to do for plain loop with no iteration scheme
3452 -- Case of for loop (Loop_Parameter_Specification present)
3454 -- Note: we do not have to worry about validity checking of the for loop
3455 -- range bounds here, since they were frozen with constant declarations
3456 -- and it is during that process that the validity checking is done.
3458 elsif Present
(Loop_Parameter_Specification
(Isc
)) then
3460 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Isc
);
3461 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
3462 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
3463 Btype
: constant Entity_Id
:= Base_Type
(Ltype
);
3468 -- Deal with loop over predicates
3470 if Is_Discrete_Type
(Ltype
)
3471 and then Present
(Predicate_Function
(Ltype
))
3473 Expand_Predicated_Loop
(N
);
3475 -- Handle the case where we have a for loop with the range type
3476 -- being an enumeration type with non-standard representation.
3477 -- In this case we expand:
3479 -- for x in [reverse] a .. b loop
3485 -- for xP in [reverse] integer
3486 -- range etype'Pos (a) .. etype'Pos (b)
3489 -- x : constant etype := Pos_To_Rep (xP);
3495 elsif Is_Enumeration_Type
(Btype
)
3496 and then Present
(Enum_Pos_To_Rep
(Btype
))
3499 Make_Defining_Identifier
(Loc
,
3500 Chars
=> New_External_Name
(Chars
(Loop_Id
), 'P'));
3502 -- If the type has a contiguous representation, successive
3503 -- values can be generated as offsets from the first literal.
3505 if Has_Contiguous_Rep
(Btype
) then
3507 Unchecked_Convert_To
(Btype
,
3510 Make_Integer_Literal
(Loc
,
3511 Enumeration_Rep
(First_Literal
(Btype
))),
3512 Right_Opnd
=> New_Reference_To
(New_Id
, Loc
)));
3514 -- Use the constructed array Enum_Pos_To_Rep
3517 Make_Indexed_Component
(Loc
,
3519 New_Reference_To
(Enum_Pos_To_Rep
(Btype
), Loc
),
3521 New_List
(New_Reference_To
(New_Id
, Loc
)));
3525 Make_Loop_Statement
(Loc
,
3526 Identifier
=> Identifier
(N
),
3529 Make_Iteration_Scheme
(Loc
,
3530 Loop_Parameter_Specification
=>
3531 Make_Loop_Parameter_Specification
(Loc
,
3532 Defining_Identifier
=> New_Id
,
3533 Reverse_Present
=> Reverse_Present
(LPS
),
3535 Discrete_Subtype_Definition
=>
3536 Make_Subtype_Indication
(Loc
,
3539 New_Reference_To
(Standard_Natural
, Loc
),
3542 Make_Range_Constraint
(Loc
,
3547 Make_Attribute_Reference
(Loc
,
3549 New_Reference_To
(Btype
, Loc
),
3551 Attribute_Name
=> Name_Pos
,
3553 Expressions
=> New_List
(
3555 (Type_Low_Bound
(Ltype
)))),
3558 Make_Attribute_Reference
(Loc
,
3560 New_Reference_To
(Btype
, Loc
),
3562 Attribute_Name
=> Name_Pos
,
3564 Expressions
=> New_List
(
3569 Statements
=> New_List
(
3570 Make_Block_Statement
(Loc
,
3571 Declarations
=> New_List
(
3572 Make_Object_Declaration
(Loc
,
3573 Defining_Identifier
=> Loop_Id
,
3574 Constant_Present
=> True,
3575 Object_Definition
=>
3576 New_Reference_To
(Ltype
, Loc
),
3577 Expression
=> Expr
)),
3579 Handled_Statement_Sequence
=>
3580 Make_Handled_Sequence_Of_Statements
(Loc
,
3581 Statements
=> Statements
(N
)))),
3583 End_Label
=> End_Label
(N
)));
3585 -- The loop parameter's entity must be removed from the loop
3586 -- scope's entity list, since it will now be located in the
3587 -- new block scope. Any other entities already associated with
3588 -- the loop scope, such as the loop parameter's subtype, will
3591 pragma Assert
(First_Entity
(Scope
(Loop_Id
)) = Loop_Id
);
3592 Set_First_Entity
(Scope
(Loop_Id
), Next_Entity
(Loop_Id
));
3594 if Last_Entity
(Scope
(Loop_Id
)) = Loop_Id
then
3595 Set_Last_Entity
(Scope
(Loop_Id
), Empty
);
3600 -- Nothing to do with other cases of for loops
3607 -- Second case, if we have a while loop with Condition_Actions set, then
3608 -- we change it into a plain loop:
3617 -- <<condition actions>>
3623 and then Present
(Condition_Actions
(Isc
))
3624 and then Present
(Condition
(Isc
))
3631 Make_Exit_Statement
(Sloc
(Condition
(Isc
)),
3633 Make_Op_Not
(Sloc
(Condition
(Isc
)),
3634 Right_Opnd
=> Condition
(Isc
)));
3636 Prepend
(ES
, Statements
(N
));
3637 Insert_List_Before
(ES
, Condition_Actions
(Isc
));
3639 -- This is not an implicit loop, since it is generated in response
3640 -- to the loop statement being processed. If this is itself
3641 -- implicit, the restriction has already been checked. If not,
3642 -- it is an explicit loop.
3645 Make_Loop_Statement
(Sloc
(N
),
3646 Identifier
=> Identifier
(N
),
3647 Statements
=> Statements
(N
),
3648 End_Label
=> End_Label
(N
)));
3653 -- Here to deal with iterator case
3656 and then Present
(Iterator_Specification
(Isc
))
3658 Expand_Iterator_Loop
(N
);
3660 end Expand_N_Loop_Statement
;
3662 ----------------------------
3663 -- Expand_Predicated_Loop --
3664 ----------------------------
3666 -- Note: the expander can handle generation of loops over predicated
3667 -- subtypes for both the dynamic and static cases. Depending on what
3668 -- we decide is allowed in Ada 2012 mode and/or extensions allowed
3669 -- mode, the semantic analyzer may disallow one or both forms.
3671 procedure Expand_Predicated_Loop
(N
: Node_Id
) is
3672 Loc
: constant Source_Ptr
:= Sloc
(N
);
3673 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3674 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Isc
);
3675 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
3676 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
3677 Stat
: constant List_Id
:= Static_Predicate
(Ltype
);
3678 Stmts
: constant List_Id
:= Statements
(N
);
3681 -- Case of iteration over non-static predicate, should not be possible
3682 -- since this is not allowed by the semantics and should have been
3683 -- caught during analysis of the loop statement.
3686 raise Program_Error
;
3688 -- If the predicate list is empty, that corresponds to a predicate of
3689 -- False, in which case the loop won't run at all, and we rewrite the
3690 -- entire loop as a null statement.
3692 elsif Is_Empty_List
(Stat
) then
3693 Rewrite
(N
, Make_Null_Statement
(Loc
));
3696 -- For expansion over a static predicate we generate the following
3699 -- J : Ltype := min-val;
3704 -- when endpoint => J := startpoint;
3705 -- when endpoint => J := startpoint;
3707 -- when max-val => exit;
3708 -- when others => J := Lval'Succ (J);
3713 -- To make this a little clearer, let's take a specific example:
3715 -- type Int is range 1 .. 10;
3716 -- subtype L is Int with
3717 -- predicate => L in 3 | 10 | 5 .. 7;
3719 -- for L in StaticP loop
3720 -- Put_Line ("static:" & J'Img);
3723 -- In this case, the loop is transformed into
3730 -- when 3 => J := 5;
3731 -- when 7 => J := 10;
3733 -- when others => J := L'Succ (J);
3739 Static_Predicate
: declare
3746 function Lo_Val
(N
: Node_Id
) return Node_Id
;
3747 -- Given static expression or static range, returns an identifier
3748 -- whose value is the low bound of the expression value or range.
3750 function Hi_Val
(N
: Node_Id
) return Node_Id
;
3751 -- Given static expression or static range, returns an identifier
3752 -- whose value is the high bound of the expression value or range.
3758 function Hi_Val
(N
: Node_Id
) return Node_Id
is
3760 if Is_Static_Expression
(N
) then
3761 return New_Copy
(N
);
3763 pragma Assert
(Nkind
(N
) = N_Range
);
3764 return New_Copy
(High_Bound
(N
));
3772 function Lo_Val
(N
: Node_Id
) return Node_Id
is
3774 if Is_Static_Expression
(N
) then
3775 return New_Copy
(N
);
3777 pragma Assert
(Nkind
(N
) = N_Range
);
3778 return New_Copy
(Low_Bound
(N
));
3782 -- Start of processing for Static_Predicate
3785 -- Convert loop identifier to normal variable and reanalyze it so
3786 -- that this conversion works. We have to use the same defining
3787 -- identifier, since there may be references in the loop body.
3789 Set_Analyzed
(Loop_Id
, False);
3790 Set_Ekind
(Loop_Id
, E_Variable
);
3792 -- In most loops the loop variable is assigned in various
3793 -- alternatives in the body. However, in the rare case when
3794 -- the range specifies a single element, the loop variable
3795 -- may trigger a spurious warning that is could be constant.
3796 -- This warning might as well be suppressed.
3798 Set_Warnings_Off
(Loop_Id
);
3800 -- Loop to create branches of case statement
3804 while Present
(P
) loop
3805 if No
(Next
(P
)) then
3806 S
:= Make_Exit_Statement
(Loc
);
3809 Make_Assignment_Statement
(Loc
,
3810 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
3811 Expression
=> Lo_Val
(Next
(P
)));
3812 Set_Suppress_Assignment_Checks
(S
);
3816 Make_Case_Statement_Alternative
(Loc
,
3817 Statements
=> New_List
(S
),
3818 Discrete_Choices
=> New_List
(Hi_Val
(P
))));
3823 -- Add others choice
3826 Make_Assignment_Statement
(Loc
,
3827 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
3829 Make_Attribute_Reference
(Loc
,
3830 Prefix
=> New_Occurrence_Of
(Ltype
, Loc
),
3831 Attribute_Name
=> Name_Succ
,
3832 Expressions
=> New_List
(
3833 New_Occurrence_Of
(Loop_Id
, Loc
))));
3834 Set_Suppress_Assignment_Checks
(S
);
3837 Make_Case_Statement_Alternative
(Loc
,
3838 Discrete_Choices
=> New_List
(Make_Others_Choice
(Loc
)),
3839 Statements
=> New_List
(S
)));
3841 -- Construct case statement and append to body statements
3844 Make_Case_Statement
(Loc
,
3845 Expression
=> New_Occurrence_Of
(Loop_Id
, Loc
),
3846 Alternatives
=> Alts
);
3847 Append_To
(Stmts
, Cstm
);
3852 Make_Object_Declaration
(Loc
,
3853 Defining_Identifier
=> Loop_Id
,
3854 Object_Definition
=> New_Occurrence_Of
(Ltype
, Loc
),
3855 Expression
=> Lo_Val
(First
(Stat
)));
3856 Set_Suppress_Assignment_Checks
(D
);
3859 Make_Block_Statement
(Loc
,
3860 Declarations
=> New_List
(D
),
3861 Handled_Statement_Sequence
=>
3862 Make_Handled_Sequence_Of_Statements
(Loc
,
3863 Statements
=> New_List
(
3864 Make_Loop_Statement
(Loc
,
3865 Statements
=> Stmts
,
3866 End_Label
=> Empty
)))));
3869 end Static_Predicate
;
3871 end Expand_Predicated_Loop
;
3873 ------------------------------
3874 -- Make_Tag_Ctrl_Assignment --
3875 ------------------------------
3877 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
is
3878 Asn
: constant Node_Id
:= Relocate_Node
(N
);
3879 L
: constant Node_Id
:= Name
(N
);
3880 Loc
: constant Source_Ptr
:= Sloc
(N
);
3881 Res
: constant List_Id
:= New_List
;
3882 T
: constant Entity_Id
:= Underlying_Type
(Etype
(L
));
3884 Comp_Asn
: constant Boolean := Is_Fully_Repped_Tagged_Type
(T
);
3885 Ctrl_Act
: constant Boolean := Needs_Finalization
(T
)
3886 and then not No_Ctrl_Actions
(N
);
3887 Save_Tag
: constant Boolean := Is_Tagged_Type
(T
)
3888 and then not Comp_Asn
3889 and then not No_Ctrl_Actions
(N
)
3890 and then Tagged_Type_Expansion
;
3891 -- Tags are not saved and restored when VM_Target because VM tags are
3892 -- represented implicitly in objects.
3894 Next_Id
: Entity_Id
;
3895 Prev_Id
: Entity_Id
;
3899 -- Finalize the target of the assignment when controlled
3901 -- We have two exceptions here:
3903 -- 1. If we are in an init proc since it is an initialization more
3904 -- than an assignment.
3906 -- 2. If the left-hand side is a temporary that was not initialized
3907 -- (or the parent part of a temporary since it is the case in
3908 -- extension aggregates). Such a temporary does not come from
3909 -- source. We must examine the original node for the prefix, because
3910 -- it may be a component of an entry formal, in which case it has
3911 -- been rewritten and does not appear to come from source either.
3913 -- Case of init proc
3915 if not Ctrl_Act
then
3918 -- The left hand side is an uninitialized temporary object
3920 elsif Nkind
(L
) = N_Type_Conversion
3921 and then Is_Entity_Name
(Expression
(L
))
3922 and then Nkind
(Parent
(Entity
(Expression
(L
)))) =
3923 N_Object_Declaration
3924 and then No_Initialization
(Parent
(Entity
(Expression
(L
))))
3931 (Obj_Ref
=> Duplicate_Subexpr_No_Checks
(L
),
3935 -- Save the Tag in a local variable Tag_Id
3938 Tag_Id
:= Make_Temporary
(Loc
, 'A');
3941 Make_Object_Declaration
(Loc
,
3942 Defining_Identifier
=> Tag_Id
,
3943 Object_Definition
=> New_Reference_To
(RTE
(RE_Tag
), Loc
),
3945 Make_Selected_Component
(Loc
,
3946 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
3948 New_Reference_To
(First_Tag_Component
(T
), Loc
))));
3950 -- Otherwise Tag_Id is not used
3956 -- Save the Prev and Next fields on .NET/JVM. This is not needed on non
3957 -- VM targets since the fields are not part of the object.
3959 if VM_Target
/= No_VM
3960 and then Is_Controlled
(T
)
3962 Prev_Id
:= Make_Temporary
(Loc
, 'P');
3963 Next_Id
:= Make_Temporary
(Loc
, 'N');
3966 -- Pnn : Root_Controlled_Ptr := Root_Controlled (L).Prev;
3969 Make_Object_Declaration
(Loc
,
3970 Defining_Identifier
=> Prev_Id
,
3971 Object_Definition
=>
3972 New_Reference_To
(RTE
(RE_Root_Controlled_Ptr
), Loc
),
3974 Make_Selected_Component
(Loc
,
3976 Unchecked_Convert_To
3977 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
3979 Make_Identifier
(Loc
, Name_Prev
))));
3982 -- Nnn : Root_Controlled_Ptr := Root_Controlled (L).Next;
3985 Make_Object_Declaration
(Loc
,
3986 Defining_Identifier
=> Next_Id
,
3987 Object_Definition
=>
3988 New_Reference_To
(RTE
(RE_Root_Controlled_Ptr
), Loc
),
3990 Make_Selected_Component
(Loc
,
3992 Unchecked_Convert_To
3993 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
3995 Make_Identifier
(Loc
, Name_Next
))));
3998 -- If the tagged type has a full rep clause, expand the assignment into
3999 -- component-wise assignments. Mark the node as unanalyzed in order to
4000 -- generate the proper code and propagate this scenario by setting a
4001 -- flag to avoid infinite recursion.
4004 Set_Analyzed
(Asn
, False);
4005 Set_Componentwise_Assignment
(Asn
, True);
4008 Append_To
(Res
, Asn
);
4014 Make_Assignment_Statement
(Loc
,
4016 Make_Selected_Component
(Loc
,
4017 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
4019 New_Reference_To
(First_Tag_Component
(T
), Loc
)),
4020 Expression
=> New_Reference_To
(Tag_Id
, Loc
)));
4023 -- Restore the Prev and Next fields on .NET/JVM
4025 if VM_Target
/= No_VM
4026 and then Is_Controlled
(T
)
4029 -- Root_Controlled (L).Prev := Prev_Id;
4032 Make_Assignment_Statement
(Loc
,
4034 Make_Selected_Component
(Loc
,
4036 Unchecked_Convert_To
4037 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4039 Make_Identifier
(Loc
, Name_Prev
)),
4040 Expression
=> New_Reference_To
(Prev_Id
, Loc
)));
4043 -- Root_Controlled (L).Next := Next_Id;
4046 Make_Assignment_Statement
(Loc
,
4048 Make_Selected_Component
(Loc
,
4050 Unchecked_Convert_To
4051 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4052 Selector_Name
=> Make_Identifier
(Loc
, Name_Next
)),
4053 Expression
=> New_Reference_To
(Next_Id
, Loc
)));
4056 -- Adjust the target after the assignment when controlled (not in the
4057 -- init proc since it is an initialization more than an assignment).
4062 (Obj_Ref
=> Duplicate_Subexpr_Move_Checks
(L
),
4070 -- Could use comment here ???
4072 when RE_Not_Available
=>
4074 end Make_Tag_Ctrl_Assignment
;