1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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. If the
1798 -- type has unknown discriminants, more elaborate processing below.
1800 if Has_Discriminants
(Etype
(Lhs
))
1801 and then not Has_Unknown_Discriminants
(Etype
(Lhs
))
1803 -- Skip discriminant check if change of representation. Will be
1804 -- done when the change of representation is expanded out.
1807 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
), Lhs
);
1810 -- If the type is private without discriminants, and the full type
1811 -- has discriminants (necessarily with defaults) a check may still be
1812 -- necessary if the Lhs is aliased. The private discriminants must be
1813 -- visible to build the discriminant constraints.
1815 -- Only an explicit dereference that comes from source indicates
1816 -- aliasing. Access to formals of protected operations and entries
1817 -- create dereferences but are not semantic aliasings.
1819 elsif Is_Private_Type
(Etype
(Lhs
))
1820 and then Has_Discriminants
(Typ
)
1821 and then Nkind
(Lhs
) = N_Explicit_Dereference
1822 and then Comes_From_Source
(Lhs
)
1825 Lt
: constant Entity_Id
:= Etype
(Lhs
);
1826 Ubt
: Entity_Id
:= Base_Type
(Typ
);
1829 -- In the case of an expander-generated record subtype whose base
1830 -- type still appears private, Typ will have been set to that
1831 -- private type rather than the underlying record type (because
1832 -- Underlying type will have returned the record subtype), so it's
1833 -- necessary to apply Underlying_Type again to the base type to
1834 -- get the record type we need for the discriminant check. Such
1835 -- subtypes can be created for assignments in certain cases, such
1836 -- as within an instantiation passed this kind of private type.
1837 -- It would be good to avoid this special test, but making changes
1838 -- to prevent this odd form of record subtype seems difficult. ???
1840 if Is_Private_Type
(Ubt
) then
1841 Ubt
:= Underlying_Type
(Ubt
);
1844 Set_Etype
(Lhs
, Ubt
);
1845 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Ubt
), Rhs
));
1846 Apply_Discriminant_Check
(Rhs
, Ubt
, Lhs
);
1847 Set_Etype
(Lhs
, Lt
);
1850 -- If the Lhs has a private type with unknown discriminants, it
1851 -- may have a full view with discriminants, but those are nameable
1852 -- only in the underlying type, so convert the Rhs to it before
1853 -- potential checking.
1855 elsif Has_Unknown_Discriminants
(Base_Type
(Etype
(Lhs
)))
1856 and then Has_Discriminants
(Typ
)
1858 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Typ
), Rhs
));
1859 Apply_Discriminant_Check
(Rhs
, Typ
, Lhs
);
1861 -- In the access type case, we need the same discriminant check, and
1862 -- also range checks if we have an access to constrained array.
1864 elsif Is_Access_Type
(Etype
(Lhs
))
1865 and then Is_Constrained
(Designated_Type
(Etype
(Lhs
)))
1867 if Has_Discriminants
(Designated_Type
(Etype
(Lhs
))) then
1869 -- Skip discriminant check if change of representation. Will be
1870 -- done when the change of representation is expanded out.
1873 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
));
1876 elsif Is_Array_Type
(Designated_Type
(Etype
(Lhs
))) then
1877 Apply_Range_Check
(Rhs
, Etype
(Lhs
));
1879 if Is_Constrained
(Etype
(Lhs
)) then
1880 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
1883 if Nkind
(Rhs
) = N_Allocator
then
1885 Target_Typ
: constant Entity_Id
:= Etype
(Expression
(Rhs
));
1886 C_Es
: Check_Result
;
1893 Etype
(Designated_Type
(Etype
(Lhs
))));
1905 -- Apply range check for access type case
1907 elsif Is_Access_Type
(Etype
(Lhs
))
1908 and then Nkind
(Rhs
) = N_Allocator
1909 and then Nkind
(Expression
(Rhs
)) = N_Qualified_Expression
1911 Analyze_And_Resolve
(Expression
(Rhs
));
1913 (Expression
(Rhs
), Designated_Type
(Etype
(Lhs
)));
1916 -- Ada 2005 (AI-231): Generate the run-time check
1918 if Is_Access_Type
(Typ
)
1919 and then Can_Never_Be_Null
(Etype
(Lhs
))
1920 and then not Can_Never_Be_Null
(Etype
(Rhs
))
1922 Apply_Constraint_Check
(Rhs
, Etype
(Lhs
));
1925 -- Ada 2012 (AI05-148): Update current accessibility level if Rhs is a
1926 -- stand-alone obj of an anonymous access type.
1928 if Is_Access_Type
(Typ
)
1929 and then Is_Entity_Name
(Lhs
)
1930 and then Present
(Effective_Extra_Accessibility
(Entity
(Lhs
))) then
1932 function Lhs_Entity
return Entity_Id
;
1933 -- Look through renames to find the underlying entity.
1934 -- For assignment to a rename, we don't care about the
1935 -- Enclosing_Dynamic_Scope of the rename declaration.
1941 function Lhs_Entity
return Entity_Id
is
1942 Result
: Entity_Id
:= Entity
(Lhs
);
1945 while Present
(Renamed_Object
(Result
)) loop
1947 -- Renamed_Object must return an Entity_Name here
1948 -- because of preceding "Present (E_E_A (...))" test.
1950 Result
:= Entity
(Renamed_Object
(Result
));
1956 -- Local Declarations
1958 Access_Check
: constant Node_Id
:=
1959 Make_Raise_Program_Error
(Loc
,
1963 Dynamic_Accessibility_Level
(Rhs
),
1965 Make_Integer_Literal
(Loc
,
1968 (Enclosing_Dynamic_Scope
1970 Reason
=> PE_Accessibility_Check_Failed
);
1972 Access_Level_Update
: constant Node_Id
:=
1973 Make_Assignment_Statement
(Loc
,
1976 (Effective_Extra_Accessibility
1977 (Entity
(Lhs
)), Loc
),
1979 Dynamic_Accessibility_Level
(Rhs
));
1982 if not Accessibility_Checks_Suppressed
(Entity
(Lhs
)) then
1983 Insert_Action
(N
, Access_Check
);
1986 Insert_Action
(N
, Access_Level_Update
);
1990 -- Case of assignment to a bit packed array element. If there is a
1991 -- change of representation this must be expanded into components,
1992 -- otherwise this is a bit-field assignment.
1994 if Nkind
(Lhs
) = N_Indexed_Component
1995 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
)))
1997 -- Normal case, no change of representation
2000 Expand_Bit_Packed_Element_Set
(N
);
2003 -- Change of representation case
2006 -- Generate the following, to force component-by-component
2007 -- assignments in an efficient way. Otherwise each component
2008 -- will require a temporary and two bit-field manipulations.
2015 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
2021 Make_Object_Declaration
(Loc
,
2022 Defining_Identifier
=> Tnn
,
2023 Object_Definition
=>
2024 New_Occurrence_Of
(Etype
(Lhs
), Loc
)),
2025 Make_Assignment_Statement
(Loc
,
2026 Name
=> New_Occurrence_Of
(Tnn
, Loc
),
2027 Expression
=> Relocate_Node
(Rhs
)),
2028 Make_Assignment_Statement
(Loc
,
2029 Name
=> Relocate_Node
(Lhs
),
2030 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
2032 Insert_Actions
(N
, Stats
);
2033 Rewrite
(N
, Make_Null_Statement
(Loc
));
2038 -- Build-in-place function call case. Note that we're not yet doing
2039 -- build-in-place for user-written assignment statements (the assignment
2040 -- here came from an aggregate.)
2042 elsif Ada_Version
>= Ada_2005
2043 and then Is_Build_In_Place_Function_Call
(Rhs
)
2045 Make_Build_In_Place_Call_In_Assignment
(N
, Rhs
);
2047 elsif Is_Tagged_Type
(Typ
) and then Is_Value_Type
(Etype
(Lhs
)) then
2049 -- Nothing to do for valuetypes
2050 -- ??? Set_Scope_Is_Transient (False);
2054 elsif Is_Tagged_Type
(Typ
)
2055 or else (Needs_Finalization
(Typ
) and then not Is_Array_Type
(Typ
))
2057 Tagged_Case
: declare
2058 L
: List_Id
:= No_List
;
2059 Expand_Ctrl_Actions
: constant Boolean := not No_Ctrl_Actions
(N
);
2062 -- In the controlled case, we ensure that function calls are
2063 -- evaluated before finalizing the target. In all cases, it makes
2064 -- the expansion easier if the side-effects are removed first.
2066 Remove_Side_Effects
(Lhs
);
2067 Remove_Side_Effects
(Rhs
);
2069 -- Avoid recursion in the mechanism
2073 -- If dispatching assignment, we need to dispatch to _assign
2075 if Is_Class_Wide_Type
(Typ
)
2077 -- If the type is tagged, we may as well use the predefined
2078 -- primitive assignment. This avoids inlining a lot of code
2079 -- and in the class-wide case, the assignment is replaced
2080 -- by a dispatching call to _assign. It is suppressed in the
2081 -- case of assignments created by the expander that correspond
2082 -- to initializations, where we do want to copy the tag
2083 -- (Expand_Ctrl_Actions flag is set True in this case). It is
2084 -- also suppressed if restriction No_Dispatching_Calls is in
2085 -- force because in that case predefined primitives are not
2088 or else (Is_Tagged_Type
(Typ
)
2089 and then not Is_Value_Type
(Etype
(Lhs
))
2090 and then Chars
(Current_Scope
) /= Name_uAssign
2091 and then Expand_Ctrl_Actions
2093 not Restriction_Active
(No_Dispatching_Calls
))
2095 if Is_Limited_Type
(Typ
) then
2097 -- This can happen in an instance when the formal is an
2098 -- extension of a limited interface, and the actual is
2099 -- limited. This is an error according to AI05-0087, but
2100 -- is not caught at the point of instantiation in earlier
2103 -- This is wrong, error messages cannot be issued during
2104 -- expansion, since they would be missed in -gnatc mode ???
2106 Error_Msg_N
("assignment not available on limited type", N
);
2110 -- Fetch the primitive op _assign and proper type to call it.
2111 -- Because of possible conflicts between private and full view,
2112 -- fetch the proper type directly from the operation profile.
2115 Op
: constant Entity_Id
:=
2116 Find_Prim_Op
(Typ
, Name_uAssign
);
2117 F_Typ
: Entity_Id
:= Etype
(First_Formal
(Op
));
2120 -- If the assignment is dispatching, make sure to use the
2123 if Is_Class_Wide_Type
(Typ
) then
2124 F_Typ
:= Class_Wide_Type
(F_Typ
);
2129 -- In case of assignment to a class-wide tagged type, before
2130 -- the assignment we generate run-time check to ensure that
2131 -- the tags of source and target match.
2133 if not Tag_Checks_Suppressed
(Typ
)
2134 and then Is_Class_Wide_Type
(Typ
)
2135 and then Is_Tagged_Type
(Typ
)
2136 and then Is_Tagged_Type
(Underlying_Type
(Etype
(Rhs
)))
2139 Make_Raise_Constraint_Error
(Loc
,
2143 Make_Selected_Component
(Loc
,
2144 Prefix
=> Duplicate_Subexpr
(Lhs
),
2146 Make_Identifier
(Loc
, Name_uTag
)),
2148 Make_Selected_Component
(Loc
,
2149 Prefix
=> Duplicate_Subexpr
(Rhs
),
2151 Make_Identifier
(Loc
, Name_uTag
))),
2152 Reason
=> CE_Tag_Check_Failed
));
2156 Left_N
: Node_Id
:= Duplicate_Subexpr
(Lhs
);
2157 Right_N
: Node_Id
:= Duplicate_Subexpr
(Rhs
);
2160 -- In order to dispatch the call to _assign the type of
2161 -- the actuals must match. Add conversion (if required).
2163 if Etype
(Lhs
) /= F_Typ
then
2164 Left_N
:= Unchecked_Convert_To
(F_Typ
, Left_N
);
2167 if Etype
(Rhs
) /= F_Typ
then
2168 Right_N
:= Unchecked_Convert_To
(F_Typ
, Right_N
);
2172 Make_Procedure_Call_Statement
(Loc
,
2173 Name
=> New_Reference_To
(Op
, Loc
),
2174 Parameter_Associations
=> New_List
(
2176 Node2
=> Right_N
)));
2181 L
:= Make_Tag_Ctrl_Assignment
(N
);
2183 -- We can't afford to have destructive Finalization Actions in
2184 -- the Self assignment case, so if the target and the source
2185 -- are not obviously different, code is generated to avoid the
2186 -- self assignment case:
2188 -- if lhs'address /= rhs'address then
2189 -- <code for controlled and/or tagged assignment>
2192 -- Skip this if Restriction (No_Finalization) is active
2194 if not Statically_Different
(Lhs
, Rhs
)
2195 and then Expand_Ctrl_Actions
2196 and then not Restriction_Active
(No_Finalization
)
2199 Make_Implicit_If_Statement
(N
,
2203 Make_Attribute_Reference
(Loc
,
2204 Prefix
=> Duplicate_Subexpr
(Lhs
),
2205 Attribute_Name
=> Name_Address
),
2208 Make_Attribute_Reference
(Loc
,
2209 Prefix
=> Duplicate_Subexpr
(Rhs
),
2210 Attribute_Name
=> Name_Address
)),
2212 Then_Statements
=> L
));
2215 -- We need to set up an exception handler for implementing
2216 -- 7.6.1(18). The remaining adjustments are tackled by the
2217 -- implementation of adjust for record_controllers (see
2220 -- This is skipped if we have no finalization
2222 if Expand_Ctrl_Actions
2223 and then not Restriction_Active
(No_Finalization
)
2226 Make_Block_Statement
(Loc
,
2227 Handled_Statement_Sequence
=>
2228 Make_Handled_Sequence_Of_Statements
(Loc
,
2230 Exception_Handlers
=> New_List
(
2231 Make_Handler_For_Ctrl_Operation
(Loc
)))));
2236 Make_Block_Statement
(Loc
,
2237 Handled_Statement_Sequence
=>
2238 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> L
)));
2240 -- If no restrictions on aborts, protect the whole assignment
2241 -- for controlled objects as per 9.8(11).
2243 if Needs_Finalization
(Typ
)
2244 and then Expand_Ctrl_Actions
2245 and then Abort_Allowed
2248 Blk
: constant Entity_Id
:=
2250 (E_Block
, Current_Scope
, Sloc
(N
), 'B');
2253 Set_Scope
(Blk
, Current_Scope
);
2254 Set_Etype
(Blk
, Standard_Void_Type
);
2255 Set_Identifier
(N
, New_Occurrence_Of
(Blk
, Sloc
(N
)));
2257 Prepend_To
(L
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
2258 Set_At_End_Proc
(Handled_Statement_Sequence
(N
),
2259 New_Occurrence_Of
(RTE
(RE_Abort_Undefer_Direct
), Loc
));
2260 Expand_At_End_Handler
2261 (Handled_Statement_Sequence
(N
), Blk
);
2265 -- N has been rewritten to a block statement for which it is
2266 -- known by construction that no checks are necessary: analyze
2267 -- it with all checks suppressed.
2269 Analyze
(N
, Suppress
=> All_Checks
);
2275 elsif Is_Array_Type
(Typ
) then
2277 Actual_Rhs
: Node_Id
:= Rhs
;
2280 while Nkind_In
(Actual_Rhs
, N_Type_Conversion
,
2281 N_Qualified_Expression
)
2283 Actual_Rhs
:= Expression
(Actual_Rhs
);
2286 Expand_Assign_Array
(N
, Actual_Rhs
);
2292 elsif Is_Record_Type
(Typ
) then
2293 Expand_Assign_Record
(N
);
2296 -- Scalar types. This is where we perform the processing related to the
2297 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2300 elsif Is_Scalar_Type
(Typ
) then
2302 -- Case where right side is known valid
2304 if Expr_Known_Valid
(Rhs
) then
2306 -- Here the right side is valid, so it is fine. The case to deal
2307 -- with is when the left side is a local variable reference whose
2308 -- value is not currently known to be valid. If this is the case,
2309 -- and the assignment appears in an unconditional context, then
2310 -- we can mark the left side as now being valid if one of these
2311 -- conditions holds:
2313 -- The expression of the right side has Do_Range_Check set so
2314 -- that we know a range check will be performed. Note that it
2315 -- can be the case that a range check is omitted because we
2316 -- make the assumption that we can assume validity for operands
2317 -- appearing in the right side in determining whether a range
2318 -- check is required
2320 -- The subtype of the right side matches the subtype of the
2321 -- left side. In this case, even though we have not checked
2322 -- the range of the right side, we know it is in range of its
2323 -- subtype if the expression is valid.
2325 if Is_Local_Variable_Reference
(Lhs
)
2326 and then not Is_Known_Valid
(Entity
(Lhs
))
2327 and then In_Unconditional_Context
(N
)
2329 if Do_Range_Check
(Rhs
)
2330 or else Etype
(Lhs
) = Etype
(Rhs
)
2332 Set_Is_Known_Valid
(Entity
(Lhs
), True);
2336 -- Case where right side may be invalid in the sense of the RM
2337 -- reference above. The RM does not require that we check for the
2338 -- validity on an assignment, but it does require that the assignment
2339 -- of an invalid value not cause erroneous behavior.
2341 -- The general approach in GNAT is to use the Is_Known_Valid flag
2342 -- to avoid the need for validity checking on assignments. However
2343 -- in some cases, we have to do validity checking in order to make
2344 -- sure that the setting of this flag is correct.
2347 -- Validate right side if we are validating copies
2349 if Validity_Checks_On
2350 and then Validity_Check_Copies
2352 -- Skip this if left hand side is an array or record component
2353 -- and elementary component validity checks are suppressed.
2355 if Nkind_In
(Lhs
, N_Selected_Component
, N_Indexed_Component
)
2356 and then not Validity_Check_Components
2363 -- We can propagate this to the left side where appropriate
2365 if Is_Local_Variable_Reference
(Lhs
)
2366 and then not Is_Known_Valid
(Entity
(Lhs
))
2367 and then In_Unconditional_Context
(N
)
2369 Set_Is_Known_Valid
(Entity
(Lhs
), True);
2372 -- Otherwise check to see what should be done
2374 -- If left side is a local variable, then we just set its flag to
2375 -- indicate that its value may no longer be valid, since we are
2376 -- copying a potentially invalid value.
2378 elsif Is_Local_Variable_Reference
(Lhs
) then
2379 Set_Is_Known_Valid
(Entity
(Lhs
), False);
2381 -- Check for case of a nonlocal variable on the left side which
2382 -- is currently known to be valid. In this case, we simply ensure
2383 -- that the right side is valid. We only play the game of copying
2384 -- validity status for local variables, since we are doing this
2385 -- statically, not by tracing the full flow graph.
2387 elsif Is_Entity_Name
(Lhs
)
2388 and then Is_Known_Valid
(Entity
(Lhs
))
2390 -- Note: If Validity_Checking mode is set to none, we ignore
2391 -- the Ensure_Valid call so don't worry about that case here.
2395 -- In all other cases, we can safely copy an invalid value without
2396 -- worrying about the status of the left side. Since it is not a
2397 -- variable reference it will not be considered
2398 -- as being known to be valid in any case.
2407 when RE_Not_Available
=>
2409 end Expand_N_Assignment_Statement
;
2411 ------------------------------
2412 -- Expand_N_Block_Statement --
2413 ------------------------------
2415 -- Encode entity names defined in block statement
2417 procedure Expand_N_Block_Statement
(N
: Node_Id
) is
2419 Qualify_Entity_Names
(N
);
2420 end Expand_N_Block_Statement
;
2422 -----------------------------
2423 -- Expand_N_Case_Statement --
2424 -----------------------------
2426 procedure Expand_N_Case_Statement
(N
: Node_Id
) is
2427 Loc
: constant Source_Ptr
:= Sloc
(N
);
2428 Expr
: constant Node_Id
:= Expression
(N
);
2436 -- Check for the situation where we know at compile time which branch
2439 if Compile_Time_Known_Value
(Expr
) then
2440 Alt
:= Find_Static_Alternative
(N
);
2442 Process_Statements_For_Controlled_Objects
(Alt
);
2444 -- Move statements from this alternative after the case statement.
2445 -- They are already analyzed, so will be skipped by the analyzer.
2447 Insert_List_After
(N
, Statements
(Alt
));
2449 -- That leaves the case statement as a shell. So now we can kill all
2450 -- other alternatives in the case statement.
2452 Kill_Dead_Code
(Expression
(N
));
2458 -- Loop through case alternatives, skipping pragmas, and skipping
2459 -- the one alternative that we select (and therefore retain).
2461 Dead_Alt
:= First
(Alternatives
(N
));
2462 while Present
(Dead_Alt
) loop
2464 and then Nkind
(Dead_Alt
) = N_Case_Statement_Alternative
2466 Kill_Dead_Code
(Statements
(Dead_Alt
), Warn_On_Deleted_Code
);
2473 Rewrite
(N
, Make_Null_Statement
(Loc
));
2477 -- Here if the choice is not determined at compile time
2480 Last_Alt
: constant Node_Id
:= Last
(Alternatives
(N
));
2482 Others_Present
: Boolean;
2483 Others_Node
: Node_Id
;
2485 Then_Stms
: List_Id
;
2486 Else_Stms
: List_Id
;
2489 if Nkind
(First
(Discrete_Choices
(Last_Alt
))) = N_Others_Choice
then
2490 Others_Present
:= True;
2491 Others_Node
:= Last_Alt
;
2493 Others_Present
:= False;
2496 -- First step is to worry about possible invalid argument. The RM
2497 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2498 -- outside the base range), then Constraint_Error must be raised.
2500 -- Case of validity check required (validity checks are on, the
2501 -- expression is not known to be valid, and the case statement
2502 -- comes from source -- no need to validity check internally
2503 -- generated case statements).
2505 if Validity_Check_Default
then
2506 Ensure_Valid
(Expr
);
2509 -- If there is only a single alternative, just replace it with the
2510 -- sequence of statements since obviously that is what is going to
2511 -- be executed in all cases.
2513 Len
:= List_Length
(Alternatives
(N
));
2517 -- We still need to evaluate the expression if it has any side
2520 Remove_Side_Effects
(Expression
(N
));
2522 Alt
:= First
(Alternatives
(N
));
2524 Process_Statements_For_Controlled_Objects
(Alt
);
2525 Insert_List_After
(N
, Statements
(Alt
));
2527 -- That leaves the case statement as a shell. The alternative that
2528 -- will be executed is reset to a null list. So now we can kill
2529 -- the entire case statement.
2531 Kill_Dead_Code
(Expression
(N
));
2532 Rewrite
(N
, Make_Null_Statement
(Loc
));
2535 -- An optimization. If there are only two alternatives, and only
2536 -- a single choice, then rewrite the whole case statement as an
2537 -- if statement, since this can result in subsequent optimizations.
2538 -- This helps not only with case statements in the source of a
2539 -- simple form, but also with generated code (discriminant check
2540 -- functions in particular).
2542 -- Note: it is OK to do this before expanding out choices for any
2543 -- static predicates, since the if statement processing will handle
2544 -- the static predicate case fine.
2547 Chlist
:= Discrete_Choices
(First
(Alternatives
(N
)));
2549 if List_Length
(Chlist
) = 1 then
2550 Choice
:= First
(Chlist
);
2552 Then_Stms
:= Statements
(First
(Alternatives
(N
)));
2553 Else_Stms
:= Statements
(Last
(Alternatives
(N
)));
2555 -- For TRUE, generate "expression", not expression = true
2557 if Nkind
(Choice
) = N_Identifier
2558 and then Entity
(Choice
) = Standard_True
2560 Cond
:= Expression
(N
);
2562 -- For FALSE, generate "expression" and switch then/else
2564 elsif Nkind
(Choice
) = N_Identifier
2565 and then Entity
(Choice
) = Standard_False
2567 Cond
:= Expression
(N
);
2568 Else_Stms
:= Statements
(First
(Alternatives
(N
)));
2569 Then_Stms
:= Statements
(Last
(Alternatives
(N
)));
2571 -- For a range, generate "expression in range"
2573 elsif Nkind
(Choice
) = N_Range
2574 or else (Nkind
(Choice
) = N_Attribute_Reference
2575 and then Attribute_Name
(Choice
) = Name_Range
)
2576 or else (Is_Entity_Name
(Choice
)
2577 and then Is_Type
(Entity
(Choice
)))
2578 or else Nkind
(Choice
) = N_Subtype_Indication
2582 Left_Opnd
=> Expression
(N
),
2583 Right_Opnd
=> Relocate_Node
(Choice
));
2585 -- For any other subexpression "expression = value"
2590 Left_Opnd
=> Expression
(N
),
2591 Right_Opnd
=> Relocate_Node
(Choice
));
2594 -- Now rewrite the case as an IF
2597 Make_If_Statement
(Loc
,
2599 Then_Statements
=> Then_Stms
,
2600 Else_Statements
=> Else_Stms
));
2606 -- If the last alternative is not an Others choice, replace it with
2607 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2608 -- the modified case statement, since it's only effect would be to
2609 -- compute the contents of the Others_Discrete_Choices which is not
2610 -- needed by the back end anyway.
2612 -- The reason we do this is that the back end always needs some
2613 -- default for a switch, so if we have not supplied one in the
2614 -- processing above for validity checking, then we need to supply
2617 if not Others_Present
then
2618 Others_Node
:= Make_Others_Choice
(Sloc
(Last_Alt
));
2619 Set_Others_Discrete_Choices
2620 (Others_Node
, Discrete_Choices
(Last_Alt
));
2621 Set_Discrete_Choices
(Last_Alt
, New_List
(Others_Node
));
2624 -- Deal with possible declarations of controlled objects, and also
2625 -- with rewriting choice sequences for static predicate references.
2627 Alt
:= First_Non_Pragma
(Alternatives
(N
));
2628 while Present
(Alt
) loop
2629 Process_Statements_For_Controlled_Objects
(Alt
);
2631 if Has_SP_Choice
(Alt
) then
2632 Expand_Static_Predicates_In_Choices
(Alt
);
2635 Next_Non_Pragma
(Alt
);
2638 end Expand_N_Case_Statement
;
2640 -----------------------------
2641 -- Expand_N_Exit_Statement --
2642 -----------------------------
2644 -- The only processing required is to deal with a possible C/Fortran
2645 -- boolean value used as the condition for the exit statement.
2647 procedure Expand_N_Exit_Statement
(N
: Node_Id
) is
2649 Adjust_Condition
(Condition
(N
));
2650 end Expand_N_Exit_Statement
;
2652 -----------------------------
2653 -- Expand_N_Goto_Statement --
2654 -----------------------------
2656 -- Add poll before goto if polling active
2658 procedure Expand_N_Goto_Statement
(N
: Node_Id
) is
2660 Generate_Poll_Call
(N
);
2661 end Expand_N_Goto_Statement
;
2663 ---------------------------
2664 -- Expand_N_If_Statement --
2665 ---------------------------
2667 -- First we deal with the case of C and Fortran convention boolean values,
2668 -- with zero/non-zero semantics.
2670 -- Second, we deal with the obvious rewriting for the cases where the
2671 -- condition of the IF is known at compile time to be True or False.
2673 -- Third, we remove elsif parts which have non-empty Condition_Actions and
2674 -- rewrite as independent if statements. For example:
2685 -- <<condition actions of y>>
2691 -- This rewriting is needed if at least one elsif part has a non-empty
2692 -- Condition_Actions list. We also do the same processing if there is a
2693 -- constant condition in an elsif part (in conjunction with the first
2694 -- processing step mentioned above, for the recursive call made to deal
2695 -- with the created inner if, this deals with properly optimizing the
2696 -- cases of constant elsif conditions).
2698 procedure Expand_N_If_Statement
(N
: Node_Id
) is
2699 Loc
: constant Source_Ptr
:= Sloc
(N
);
2704 Warn_If_Deleted
: constant Boolean :=
2705 Warn_On_Deleted_Code
and then Comes_From_Source
(N
);
2706 -- Indicates whether we want warnings when we delete branches of the
2707 -- if statement based on constant condition analysis. We never want
2708 -- these warnings for expander generated code.
2711 Process_Statements_For_Controlled_Objects
(N
);
2713 Adjust_Condition
(Condition
(N
));
2715 -- The following loop deals with constant conditions for the IF. We
2716 -- need a loop because as we eliminate False conditions, we grab the
2717 -- first elsif condition and use it as the primary condition.
2719 while Compile_Time_Known_Value
(Condition
(N
)) loop
2721 -- If condition is True, we can simply rewrite the if statement now
2722 -- by replacing it by the series of then statements.
2724 if Is_True
(Expr_Value
(Condition
(N
))) then
2726 -- All the else parts can be killed
2728 Kill_Dead_Code
(Elsif_Parts
(N
), Warn_If_Deleted
);
2729 Kill_Dead_Code
(Else_Statements
(N
), Warn_If_Deleted
);
2731 Hed
:= Remove_Head
(Then_Statements
(N
));
2732 Insert_List_After
(N
, Then_Statements
(N
));
2736 -- If condition is False, then we can delete the condition and
2737 -- the Then statements
2740 -- We do not delete the condition if constant condition warnings
2741 -- are enabled, since otherwise we end up deleting the desired
2742 -- warning. Of course the backend will get rid of this True/False
2743 -- test anyway, so nothing is lost here.
2745 if not Constant_Condition_Warnings
then
2746 Kill_Dead_Code
(Condition
(N
));
2749 Kill_Dead_Code
(Then_Statements
(N
), Warn_If_Deleted
);
2751 -- If there are no elsif statements, then we simply replace the
2752 -- entire if statement by the sequence of else statements.
2754 if No
(Elsif_Parts
(N
)) then
2755 if No
(Else_Statements
(N
))
2756 or else Is_Empty_List
(Else_Statements
(N
))
2759 Make_Null_Statement
(Sloc
(N
)));
2761 Hed
:= Remove_Head
(Else_Statements
(N
));
2762 Insert_List_After
(N
, Else_Statements
(N
));
2768 -- If there are elsif statements, the first of them becomes the
2769 -- if/then section of the rebuilt if statement This is the case
2770 -- where we loop to reprocess this copied condition.
2773 Hed
:= Remove_Head
(Elsif_Parts
(N
));
2774 Insert_Actions
(N
, Condition_Actions
(Hed
));
2775 Set_Condition
(N
, Condition
(Hed
));
2776 Set_Then_Statements
(N
, Then_Statements
(Hed
));
2778 -- Hed might have been captured as the condition determining
2779 -- the current value for an entity. Now it is detached from
2780 -- the tree, so a Current_Value pointer in the condition might
2781 -- need to be updated.
2783 Set_Current_Value_Condition
(N
);
2785 if Is_Empty_List
(Elsif_Parts
(N
)) then
2786 Set_Elsif_Parts
(N
, No_List
);
2792 -- Loop through elsif parts, dealing with constant conditions and
2793 -- possible condition actions that are present.
2795 if Present
(Elsif_Parts
(N
)) then
2796 E
:= First
(Elsif_Parts
(N
));
2797 while Present
(E
) loop
2798 Process_Statements_For_Controlled_Objects
(E
);
2800 Adjust_Condition
(Condition
(E
));
2802 -- If there are condition actions, then rewrite the if statement
2803 -- as indicated above. We also do the same rewrite for a True or
2804 -- False condition. The further processing of this constant
2805 -- condition is then done by the recursive call to expand the
2806 -- newly created if statement
2808 if Present
(Condition_Actions
(E
))
2809 or else Compile_Time_Known_Value
(Condition
(E
))
2811 -- Note this is not an implicit if statement, since it is part
2812 -- of an explicit if statement in the source (or of an implicit
2813 -- if statement that has already been tested).
2816 Make_If_Statement
(Sloc
(E
),
2817 Condition
=> Condition
(E
),
2818 Then_Statements
=> Then_Statements
(E
),
2819 Elsif_Parts
=> No_List
,
2820 Else_Statements
=> Else_Statements
(N
));
2822 -- Elsif parts for new if come from remaining elsif's of parent
2824 while Present
(Next
(E
)) loop
2825 if No
(Elsif_Parts
(New_If
)) then
2826 Set_Elsif_Parts
(New_If
, New_List
);
2829 Append
(Remove_Next
(E
), Elsif_Parts
(New_If
));
2832 Set_Else_Statements
(N
, New_List
(New_If
));
2834 if Present
(Condition_Actions
(E
)) then
2835 Insert_List_Before
(New_If
, Condition_Actions
(E
));
2840 if Is_Empty_List
(Elsif_Parts
(N
)) then
2841 Set_Elsif_Parts
(N
, No_List
);
2847 -- No special processing for that elsif part, move to next
2855 -- Some more optimizations applicable if we still have an IF statement
2857 if Nkind
(N
) /= N_If_Statement
then
2861 -- Another optimization, special cases that can be simplified
2863 -- if expression then
2869 -- can be changed to:
2871 -- return expression;
2875 -- if expression then
2881 -- can be changed to:
2883 -- return not (expression);
2885 -- Only do these optimizations if we are at least at -O1 level and
2886 -- do not do them if control flow optimizations are suppressed.
2888 if Optimization_Level
> 0
2889 and then not Opt
.Suppress_Control_Flow_Optimizations
2891 if Nkind
(N
) = N_If_Statement
2892 and then No
(Elsif_Parts
(N
))
2893 and then Present
(Else_Statements
(N
))
2894 and then List_Length
(Then_Statements
(N
)) = 1
2895 and then List_Length
(Else_Statements
(N
)) = 1
2898 Then_Stm
: constant Node_Id
:= First
(Then_Statements
(N
));
2899 Else_Stm
: constant Node_Id
:= First
(Else_Statements
(N
));
2902 if Nkind
(Then_Stm
) = N_Simple_Return_Statement
2904 Nkind
(Else_Stm
) = N_Simple_Return_Statement
2907 Then_Expr
: constant Node_Id
:= Expression
(Then_Stm
);
2908 Else_Expr
: constant Node_Id
:= Expression
(Else_Stm
);
2911 if Nkind
(Then_Expr
) = N_Identifier
2913 Nkind
(Else_Expr
) = N_Identifier
2915 if Entity
(Then_Expr
) = Standard_True
2916 and then Entity
(Else_Expr
) = Standard_False
2919 Make_Simple_Return_Statement
(Loc
,
2920 Expression
=> Relocate_Node
(Condition
(N
))));
2924 elsif Entity
(Then_Expr
) = Standard_False
2925 and then Entity
(Else_Expr
) = Standard_True
2928 Make_Simple_Return_Statement
(Loc
,
2932 Relocate_Node
(Condition
(N
)))));
2942 end Expand_N_If_Statement
;
2944 --------------------------
2945 -- Expand_Iterator_Loop --
2946 --------------------------
2948 procedure Expand_Iterator_Loop
(N
: Node_Id
) is
2949 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
2950 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
2951 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
2952 Loc
: constant Source_Ptr
:= Sloc
(N
);
2954 Container
: constant Node_Id
:= Name
(I_Spec
);
2955 Container_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
2957 Iterator
: Entity_Id
;
2959 Stats
: List_Id
:= Statements
(N
);
2962 -- Processing for arrays
2964 if Is_Array_Type
(Container_Typ
) then
2965 Expand_Iterator_Loop_Over_Array
(N
);
2969 -- Processing for containers
2971 -- For an "of" iterator the name is a container expression, which
2972 -- is transformed into a call to the default iterator.
2974 -- For an iterator of the form "in" the name is a function call
2975 -- that delivers an iterator type.
2977 -- In both cases, analysis of the iterator has introduced an object
2978 -- declaration to capture the domain, so that Container is an entity.
2980 -- The for loop is expanded into a while loop which uses a container
2981 -- specific cursor to desgnate each element.
2983 -- Iter : Iterator_Type := Container.Iterate;
2984 -- Cursor : Cursor_type := First (Iter);
2985 -- while Has_Element (Iter) loop
2987 -- -- The block is added when Element_Type is controlled
2989 -- Obj : Pack.Element_Type := Element (Cursor);
2990 -- -- for the "of" loop form
2992 -- <original loop statements>
2995 -- Cursor := Iter.Next (Cursor);
2998 -- If "reverse" is present, then the initialization of the cursor
2999 -- uses Last and the step becomes Prev. Pack is the name of the
3000 -- scope where the container package is instantiated.
3003 Element_Type
: constant Entity_Id
:= Etype
(Id
);
3004 Iter_Type
: Entity_Id
;
3007 Name_Init
: Name_Id
;
3008 Name_Step
: Name_Id
;
3011 -- The type of the iterator is the return type of the Iterate
3012 -- function used. For the "of" form this is the default iterator
3013 -- for the type, otherwise it is the type of the explicit
3014 -- function used in the iterator specification. The most common
3015 -- case will be an Iterate function in the container package.
3017 -- The primitive operations of the container type may not be
3018 -- use-visible, so we introduce the name of the enclosing package
3019 -- in the declarations below. The Iterator type is declared in a
3020 -- an instance within the container package itself.
3022 -- If the container type is a derived type, the cursor type is
3023 -- found in the package of the parent type.
3025 if Is_Derived_Type
(Container_Typ
) then
3026 Pack
:= Scope
(Root_Type
(Container_Typ
));
3028 Pack
:= Scope
(Container_Typ
);
3031 Iter_Type
:= Etype
(Name
(I_Spec
));
3033 -- The "of" case uses an internally generated cursor whose type
3034 -- is found in the container package. The domain of iteration
3035 -- is expanded into a call to the default Iterator function, but
3036 -- this expansion does not take place in quantified expressions
3037 -- that are analyzed with expansion disabled, and in that case the
3038 -- type of the iterator must be obtained from the aspect.
3040 if Of_Present
(I_Spec
) then
3042 Default_Iter
: constant Entity_Id
:=
3044 (Find_Value_Of_Aspect
3046 Aspect_Default_Iterator
));
3048 Container_Arg
: Node_Id
;
3052 Cursor
:= Make_Temporary
(Loc
, 'I');
3054 -- For an container element iterator, the iterator type
3055 -- is obtained from the corresponding aspect, whose return
3056 -- type is descended from the corresponding interface type
3057 -- in some instance of Ada.Iterator_Interfaces. The actuals
3058 -- of that instantiation are Cursor and Has_Element.
3060 Iter_Type
:= Etype
(Default_Iter
);
3062 -- The iterator type, which is a class_wide type, may itself
3063 -- be derived locally, so the desired instantiation is the
3064 -- scope of the root type of the iterator type.
3066 Pack
:= Scope
(Root_Type
(Etype
(Iter_Type
)));
3068 -- Rewrite domain of iteration as a call to the default
3069 -- iterator for the container type. If the container is
3070 -- a derived type and the aspect is inherited, convert
3071 -- container to parent type. The Cursor type is also
3072 -- inherited from the scope of the parent.
3074 if Base_Type
(Etype
(Container
)) =
3075 Base_Type
(Etype
(First_Formal
(Default_Iter
)))
3077 Container_Arg
:= New_Copy_Tree
(Container
);
3081 Make_Type_Conversion
(Loc
,
3084 (Etype
(First_Formal
(Default_Iter
)), Loc
),
3085 Expression
=> New_Copy_Tree
(Container
));
3088 Rewrite
(Name
(I_Spec
),
3089 Make_Function_Call
(Loc
,
3090 Name
=> New_Occurrence_Of
(Default_Iter
, Loc
),
3091 Parameter_Associations
=>
3092 New_List
(Container_Arg
)));
3093 Analyze_And_Resolve
(Name
(I_Spec
));
3095 -- Find cursor type in proper iterator package, which is an
3096 -- instantiation of Iterator_Interfaces.
3098 Ent
:= First_Entity
(Pack
);
3099 while Present
(Ent
) loop
3100 if Chars
(Ent
) = Name_Cursor
then
3101 Set_Etype
(Cursor
, Etype
(Ent
));
3108 -- Id : Element_Type renames Container (Cursor);
3109 -- This assumes that the container type has an indexing
3110 -- operation with Cursor. The check that this operation
3111 -- exists is performed in Check_Container_Indexing.
3114 Make_Object_Renaming_Declaration
(Loc
,
3115 Defining_Identifier
=> Id
,
3117 New_Reference_To
(Element_Type
, Loc
),
3119 Make_Indexed_Component
(Loc
,
3120 Prefix
=> Relocate_Node
(Container_Arg
),
3122 New_List
(New_Occurrence_Of
(Cursor
, Loc
))));
3124 -- The defining identifier in the iterator is user-visible
3125 -- and must be visible in the debugger.
3127 Set_Debug_Info_Needed
(Id
);
3129 -- If the container holds controlled objects, wrap the loop
3130 -- statements and element renaming declaration with a block.
3131 -- This ensures that the result of Element (Cusor) is
3132 -- cleaned up after each iteration of the loop.
3134 if Needs_Finalization
(Element_Type
) then
3138 -- Id : Element_Type := Element (curosr);
3140 -- <original loop statements>
3144 Make_Block_Statement
(Loc
,
3145 Declarations
=> New_List
(Decl
),
3146 Handled_Statement_Sequence
=>
3147 Make_Handled_Sequence_Of_Statements
(Loc
,
3148 Statements
=> Stats
)));
3150 -- Elements do not need finalization
3153 Prepend_To
(Stats
, Decl
);
3157 -- X in Iterate (S) : type of iterator is type of explicitly
3158 -- given Iterate function, and the loop variable is the cursor.
3159 -- It will be assigned in the loop and must be a variable.
3163 Set_Ekind
(Cursor
, E_Variable
);
3166 Iterator
:= Make_Temporary
(Loc
, 'I');
3168 -- Determine the advancement and initialization steps for the
3171 -- Analysis of the expanded loop will verify that the container
3172 -- has a reverse iterator.
3174 if Reverse_Present
(I_Spec
) then
3175 Name_Init
:= Name_Last
;
3176 Name_Step
:= Name_Previous
;
3179 Name_Init
:= Name_First
;
3180 Name_Step
:= Name_Next
;
3183 -- For both iterator forms, add a call to the step operation to
3184 -- advance the cursor. Generate:
3186 -- Cursor := Iterator.Next (Cursor);
3190 -- Cursor := Next (Cursor);
3197 Make_Function_Call
(Loc
,
3199 Make_Selected_Component
(Loc
,
3200 Prefix
=> New_Reference_To
(Iterator
, Loc
),
3201 Selector_Name
=> Make_Identifier
(Loc
, Name_Step
)),
3202 Parameter_Associations
=> New_List
(
3203 New_Reference_To
(Cursor
, Loc
)));
3206 Make_Assignment_Statement
(Loc
,
3207 Name
=> New_Occurrence_Of
(Cursor
, Loc
),
3208 Expression
=> Rhs
));
3212 -- while Iterator.Has_Element loop
3216 -- Has_Element is the second actual in the iterator package
3219 Make_Loop_Statement
(Loc
,
3221 Make_Iteration_Scheme
(Loc
,
3223 Make_Function_Call
(Loc
,
3226 Next_Entity
(First_Entity
(Pack
)), Loc
),
3227 Parameter_Associations
=>
3228 New_List
(New_Reference_To
(Cursor
, Loc
)))),
3230 Statements
=> Stats
,
3231 End_Label
=> Empty
);
3233 -- If present, preserve identifier of loop, which can be used in
3234 -- an exit statement in the body.
3236 if Present
(Identifier
(N
)) then
3237 Set_Identifier
(New_Loop
, Relocate_Node
(Identifier
(N
)));
3240 -- Create the declarations for Iterator and cursor and insert them
3241 -- before the source loop. Given that the domain of iteration is
3242 -- already an entity, the iterator is just a renaming of that
3243 -- entity. Possible optimization ???
3246 -- I : Iterator_Type renames Container;
3247 -- C : Cursor_Type := Container.[First | Last];
3250 Make_Object_Renaming_Declaration
(Loc
,
3251 Defining_Identifier
=> Iterator
,
3252 Subtype_Mark
=> New_Occurrence_Of
(Iter_Type
, Loc
),
3253 Name
=> Relocate_Node
(Name
(I_Spec
))));
3255 -- Create declaration for cursor
3262 Make_Object_Declaration
(Loc
,
3263 Defining_Identifier
=> Cursor
,
3264 Object_Definition
=>
3265 New_Occurrence_Of
(Etype
(Cursor
), Loc
),
3267 Make_Selected_Component
(Loc
,
3268 Prefix
=> New_Reference_To
(Iterator
, Loc
),
3270 Make_Identifier
(Loc
, Name_Init
)));
3272 -- The cursor is only modified in expanded code, so it appears
3273 -- as unassigned to the warning machinery. We must suppress
3274 -- this spurious warning explicitly.
3276 Set_Warnings_Off
(Cursor
);
3277 Set_Assignment_OK
(Decl
);
3279 Insert_Action
(N
, Decl
);
3282 -- If the range of iteration is given by a function call that
3283 -- returns a container, the finalization actions have been saved
3284 -- in the Condition_Actions of the iterator. Insert them now at
3285 -- the head of the loop.
3287 if Present
(Condition_Actions
(Isc
)) then
3288 Insert_List_Before
(N
, Condition_Actions
(Isc
));
3292 Rewrite
(N
, New_Loop
);
3294 end Expand_Iterator_Loop
;
3296 -------------------------------------
3297 -- Expand_Iterator_Loop_Over_Array --
3298 -------------------------------------
3300 procedure Expand_Iterator_Loop_Over_Array
(N
: Node_Id
) is
3301 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3302 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
3303 Array_Node
: constant Node_Id
:= Name
(I_Spec
);
3304 Array_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Array_Node
));
3305 Array_Dim
: constant Pos
:= Number_Dimensions
(Array_Typ
);
3306 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3307 Loc
: constant Source_Ptr
:= Sloc
(N
);
3308 Stats
: constant List_Id
:= Statements
(N
);
3309 Core_Loop
: Node_Id
;
3311 Iterator
: Entity_Id
;
3313 -- Start of processing for Expand_Iterator_Loop_Over_Array
3316 -- for Element of Array loop
3318 -- This case requires an internally generated cursor to iterate over
3321 if Of_Present
(I_Spec
) then
3322 Iterator
:= Make_Temporary
(Loc
, 'C');
3325 -- Element : Component_Type renames Array (Iterator);
3328 Make_Indexed_Component
(Loc
,
3329 Prefix
=> Relocate_Node
(Array_Node
),
3330 Expressions
=> New_List
(New_Reference_To
(Iterator
, Loc
)));
3333 Make_Object_Renaming_Declaration
(Loc
,
3334 Defining_Identifier
=> Id
,
3336 New_Reference_To
(Component_Type
(Array_Typ
), Loc
),
3339 -- Mark the loop variable as needing debug info, so that expansion
3340 -- of the renaming will result in Materialize_Entity getting set via
3341 -- Debug_Renaming_Declaration. (This setting is needed here because
3342 -- the setting in Freeze_Entity comes after the expansion, which is
3345 Set_Debug_Info_Needed
(Id
);
3347 -- for Index in Array loop
3349 -- This case utilizes the already given iterator name
3357 -- for Iterator in [reverse] Array'Range (Array_Dim) loop
3358 -- Element : Component_Type renames Array (Iterator);
3359 -- <original loop statements>
3363 Make_Loop_Statement
(Loc
,
3365 Make_Iteration_Scheme
(Loc
,
3366 Loop_Parameter_Specification
=>
3367 Make_Loop_Parameter_Specification
(Loc
,
3368 Defining_Identifier
=> Iterator
,
3369 Discrete_Subtype_Definition
=>
3370 Make_Attribute_Reference
(Loc
,
3371 Prefix
=> Relocate_Node
(Array_Node
),
3372 Attribute_Name
=> Name_Range
,
3373 Expressions
=> New_List
(
3374 Make_Integer_Literal
(Loc
, Array_Dim
))),
3375 Reverse_Present
=> Reverse_Present
(I_Spec
))),
3376 Statements
=> Stats
,
3377 End_Label
=> Empty
);
3379 -- Processing for multidimensional array
3381 if Array_Dim
> 1 then
3382 for Dim
in 1 .. Array_Dim
- 1 loop
3383 Iterator
:= Make_Temporary
(Loc
, 'C');
3385 -- Generate the dimension loops starting from the innermost one
3387 -- for Iterator in [reverse] Array'Range (Array_Dim - Dim) loop
3392 Make_Loop_Statement
(Loc
,
3394 Make_Iteration_Scheme
(Loc
,
3395 Loop_Parameter_Specification
=>
3396 Make_Loop_Parameter_Specification
(Loc
,
3397 Defining_Identifier
=> Iterator
,
3398 Discrete_Subtype_Definition
=>
3399 Make_Attribute_Reference
(Loc
,
3400 Prefix
=> Relocate_Node
(Array_Node
),
3401 Attribute_Name
=> Name_Range
,
3402 Expressions
=> New_List
(
3403 Make_Integer_Literal
(Loc
, Array_Dim
- Dim
))),
3404 Reverse_Present
=> Reverse_Present
(I_Spec
))),
3405 Statements
=> New_List
(Core_Loop
),
3406 End_Label
=> Empty
);
3408 -- Update the previously created object renaming declaration with
3409 -- the new iterator.
3411 Prepend_To
(Expressions
(Ind_Comp
),
3412 New_Reference_To
(Iterator
, Loc
));
3416 -- If original loop has a source name, preserve it so it can be
3417 -- recognized by an exit statement in the body of the rewritten loop.
3418 -- This only concerns source names: the generated name of an anonymous
3419 -- loop will be create again during the subsequent analysis below.
3421 if Present
(Identifier
(N
))
3422 and then Comes_From_Source
(Identifier
(N
))
3424 Set_Identifier
(Core_Loop
, Relocate_Node
(Identifier
(N
)));
3427 Rewrite
(N
, Core_Loop
);
3429 end Expand_Iterator_Loop_Over_Array
;
3431 -----------------------------
3432 -- Expand_N_Loop_Statement --
3433 -----------------------------
3435 -- 1. Remove null loop entirely
3436 -- 2. Deal with while condition for C/Fortran boolean
3437 -- 3. Deal with loops with a non-standard enumeration type range
3438 -- 4. Deal with while loops where Condition_Actions is set
3439 -- 5. Deal with loops over predicated subtypes
3440 -- 6. Deal with loops with iterators over arrays and containers
3441 -- 7. Insert polling call if required
3443 procedure Expand_N_Loop_Statement
(N
: Node_Id
) is
3444 Loc
: constant Source_Ptr
:= Sloc
(N
);
3445 Scheme
: constant Node_Id
:= Iteration_Scheme
(N
);
3451 if Is_Null_Loop
(N
) then
3452 Rewrite
(N
, Make_Null_Statement
(Loc
));
3456 -- Deal with condition for C/Fortran Boolean
3458 if Present
(Scheme
) then
3459 Adjust_Condition
(Condition
(Scheme
));
3462 -- Generate polling call
3464 if Is_Non_Empty_List
(Statements
(N
)) then
3465 Generate_Poll_Call
(First
(Statements
(N
)));
3468 -- Nothing more to do for plain loop with no iteration scheme
3473 -- Case of for loop (Loop_Parameter_Specification present)
3475 -- Note: we do not have to worry about validity checking of the for loop
3476 -- range bounds here, since they were frozen with constant declarations
3477 -- and it is during that process that the validity checking is done.
3479 elsif Present
(Loop_Parameter_Specification
(Scheme
)) then
3481 LPS
: constant Node_Id
:=
3482 Loop_Parameter_Specification
(Scheme
);
3483 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
3484 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
3485 Btype
: constant Entity_Id
:= Base_Type
(Ltype
);
3491 -- Deal with loop over predicates
3493 if Is_Discrete_Type
(Ltype
)
3494 and then Present
(Predicate_Function
(Ltype
))
3496 Expand_Predicated_Loop
(N
);
3498 -- Handle the case where we have a for loop with the range type
3499 -- being an enumeration type with non-standard representation.
3500 -- In this case we expand:
3502 -- for x in [reverse] a .. b loop
3508 -- for xP in [reverse] integer
3509 -- range etype'Pos (a) .. etype'Pos (b)
3512 -- x : constant etype := Pos_To_Rep (xP);
3518 elsif Is_Enumeration_Type
(Btype
)
3519 and then Present
(Enum_Pos_To_Rep
(Btype
))
3522 Make_Defining_Identifier
(Loc
,
3523 Chars
=> New_External_Name
(Chars
(Loop_Id
), 'P'));
3525 -- If the type has a contiguous representation, successive
3526 -- values can be generated as offsets from the first literal.
3528 if Has_Contiguous_Rep
(Btype
) then
3530 Unchecked_Convert_To
(Btype
,
3533 Make_Integer_Literal
(Loc
,
3534 Enumeration_Rep
(First_Literal
(Btype
))),
3535 Right_Opnd
=> New_Reference_To
(New_Id
, Loc
)));
3537 -- Use the constructed array Enum_Pos_To_Rep
3540 Make_Indexed_Component
(Loc
,
3542 New_Reference_To
(Enum_Pos_To_Rep
(Btype
), Loc
),
3544 New_List
(New_Reference_To
(New_Id
, Loc
)));
3547 -- Build declaration for loop identifier
3551 Make_Object_Declaration
(Loc
,
3552 Defining_Identifier
=> Loop_Id
,
3553 Constant_Present
=> True,
3554 Object_Definition
=> New_Reference_To
(Ltype
, Loc
),
3555 Expression
=> Expr
));
3558 Make_Loop_Statement
(Loc
,
3559 Identifier
=> Identifier
(N
),
3562 Make_Iteration_Scheme
(Loc
,
3563 Loop_Parameter_Specification
=>
3564 Make_Loop_Parameter_Specification
(Loc
,
3565 Defining_Identifier
=> New_Id
,
3566 Reverse_Present
=> Reverse_Present
(LPS
),
3568 Discrete_Subtype_Definition
=>
3569 Make_Subtype_Indication
(Loc
,
3572 New_Reference_To
(Standard_Natural
, Loc
),
3575 Make_Range_Constraint
(Loc
,
3580 Make_Attribute_Reference
(Loc
,
3582 New_Reference_To
(Btype
, Loc
),
3584 Attribute_Name
=> Name_Pos
,
3586 Expressions
=> New_List
(
3588 (Type_Low_Bound
(Ltype
)))),
3591 Make_Attribute_Reference
(Loc
,
3593 New_Reference_To
(Btype
, Loc
),
3595 Attribute_Name
=> Name_Pos
,
3597 Expressions
=> New_List
(
3602 Statements
=> New_List
(
3603 Make_Block_Statement
(Loc
,
3604 Declarations
=> Decls
,
3605 Handled_Statement_Sequence
=>
3606 Make_Handled_Sequence_Of_Statements
(Loc
,
3607 Statements
=> Statements
(N
)))),
3609 End_Label
=> End_Label
(N
)));
3611 -- The loop parameter's entity must be removed from the loop
3612 -- scope's entity list and rendered invisible, since it will
3613 -- now be located in the new block scope. Any other entities
3614 -- already associated with the loop scope, such as the loop
3615 -- parameter's subtype, will remain there.
3617 -- In an element loop, the loop will contain a declaration for
3618 -- a cursor variable; otherwise the loop id is the first entity
3619 -- in the scope constructed for the loop.
3621 if Comes_From_Source
(Loop_Id
) then
3622 pragma Assert
(First_Entity
(Scope
(Loop_Id
)) = Loop_Id
);
3626 Set_First_Entity
(Scope
(Loop_Id
), Next_Entity
(Loop_Id
));
3627 Remove_Homonym
(Loop_Id
);
3629 if Last_Entity
(Scope
(Loop_Id
)) = Loop_Id
then
3630 Set_Last_Entity
(Scope
(Loop_Id
), Empty
);
3635 -- Nothing to do with other cases of for loops
3642 -- Second case, if we have a while loop with Condition_Actions set, then
3643 -- we change it into a plain loop:
3652 -- <<condition actions>>
3657 elsif Present
(Scheme
)
3658 and then Present
(Condition_Actions
(Scheme
))
3659 and then Present
(Condition
(Scheme
))
3666 Make_Exit_Statement
(Sloc
(Condition
(Scheme
)),
3668 Make_Op_Not
(Sloc
(Condition
(Scheme
)),
3669 Right_Opnd
=> Condition
(Scheme
)));
3671 Prepend
(ES
, Statements
(N
));
3672 Insert_List_Before
(ES
, Condition_Actions
(Scheme
));
3674 -- This is not an implicit loop, since it is generated in response
3675 -- to the loop statement being processed. If this is itself
3676 -- implicit, the restriction has already been checked. If not,
3677 -- it is an explicit loop.
3680 Make_Loop_Statement
(Sloc
(N
),
3681 Identifier
=> Identifier
(N
),
3682 Statements
=> Statements
(N
),
3683 End_Label
=> End_Label
(N
)));
3688 -- Here to deal with iterator case
3690 elsif Present
(Scheme
)
3691 and then Present
(Iterator_Specification
(Scheme
))
3693 Expand_Iterator_Loop
(N
);
3696 -- When the iteration scheme mentiones attribute 'Loop_Entry, the loop
3697 -- is transformed into a conditional block where the original loop is
3698 -- the sole statement. Inspect the statements of the nested loop for
3699 -- controlled objects.
3703 if Subject_To_Loop_Entry_Attributes
(Stmt
) then
3704 Stmt
:= Find_Loop_In_Conditional_Block
(Stmt
);
3707 Process_Statements_For_Controlled_Objects
(Stmt
);
3708 end Expand_N_Loop_Statement
;
3710 ----------------------------
3711 -- Expand_Predicated_Loop --
3712 ----------------------------
3714 -- Note: the expander can handle generation of loops over predicated
3715 -- subtypes for both the dynamic and static cases. Depending on what
3716 -- we decide is allowed in Ada 2012 mode and/or extensions allowed
3717 -- mode, the semantic analyzer may disallow one or both forms.
3719 procedure Expand_Predicated_Loop
(N
: Node_Id
) is
3720 Loc
: constant Source_Ptr
:= Sloc
(N
);
3721 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3722 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Isc
);
3723 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
3724 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
3725 Stat
: constant List_Id
:= Static_Predicate
(Ltype
);
3726 Stmts
: constant List_Id
:= Statements
(N
);
3729 -- Case of iteration over non-static predicate, should not be possible
3730 -- since this is not allowed by the semantics and should have been
3731 -- caught during analysis of the loop statement.
3734 raise Program_Error
;
3736 -- If the predicate list is empty, that corresponds to a predicate of
3737 -- False, in which case the loop won't run at all, and we rewrite the
3738 -- entire loop as a null statement.
3740 elsif Is_Empty_List
(Stat
) then
3741 Rewrite
(N
, Make_Null_Statement
(Loc
));
3744 -- For expansion over a static predicate we generate the following
3747 -- J : Ltype := min-val;
3752 -- when endpoint => J := startpoint;
3753 -- when endpoint => J := startpoint;
3755 -- when max-val => exit;
3756 -- when others => J := Lval'Succ (J);
3761 -- To make this a little clearer, let's take a specific example:
3763 -- type Int is range 1 .. 10;
3764 -- subtype L is Int with
3765 -- predicate => L in 3 | 10 | 5 .. 7;
3767 -- for L in StaticP loop
3768 -- Put_Line ("static:" & J'Img);
3771 -- In this case, the loop is transformed into
3778 -- when 3 => J := 5;
3779 -- when 7 => J := 10;
3781 -- when others => J := L'Succ (J);
3787 Static_Predicate
: declare
3794 function Lo_Val
(N
: Node_Id
) return Node_Id
;
3795 -- Given static expression or static range, returns an identifier
3796 -- whose value is the low bound of the expression value or range.
3798 function Hi_Val
(N
: Node_Id
) return Node_Id
;
3799 -- Given static expression or static range, returns an identifier
3800 -- whose value is the high bound of the expression value or range.
3806 function Hi_Val
(N
: Node_Id
) return Node_Id
is
3808 if Is_Static_Expression
(N
) then
3809 return New_Copy
(N
);
3811 pragma Assert
(Nkind
(N
) = N_Range
);
3812 return New_Copy
(High_Bound
(N
));
3820 function Lo_Val
(N
: Node_Id
) return Node_Id
is
3822 if Is_Static_Expression
(N
) then
3823 return New_Copy
(N
);
3825 pragma Assert
(Nkind
(N
) = N_Range
);
3826 return New_Copy
(Low_Bound
(N
));
3830 -- Start of processing for Static_Predicate
3833 -- Convert loop identifier to normal variable and reanalyze it so
3834 -- that this conversion works. We have to use the same defining
3835 -- identifier, since there may be references in the loop body.
3837 Set_Analyzed
(Loop_Id
, False);
3838 Set_Ekind
(Loop_Id
, E_Variable
);
3840 -- In most loops the loop variable is assigned in various
3841 -- alternatives in the body. However, in the rare case when
3842 -- the range specifies a single element, the loop variable
3843 -- may trigger a spurious warning that is could be constant.
3844 -- This warning might as well be suppressed.
3846 Set_Warnings_Off
(Loop_Id
);
3848 -- Loop to create branches of case statement
3852 while Present
(P
) loop
3853 if No
(Next
(P
)) then
3854 S
:= Make_Exit_Statement
(Loc
);
3857 Make_Assignment_Statement
(Loc
,
3858 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
3859 Expression
=> Lo_Val
(Next
(P
)));
3860 Set_Suppress_Assignment_Checks
(S
);
3864 Make_Case_Statement_Alternative
(Loc
,
3865 Statements
=> New_List
(S
),
3866 Discrete_Choices
=> New_List
(Hi_Val
(P
))));
3871 -- Add others choice
3874 Make_Assignment_Statement
(Loc
,
3875 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
3877 Make_Attribute_Reference
(Loc
,
3878 Prefix
=> New_Occurrence_Of
(Ltype
, Loc
),
3879 Attribute_Name
=> Name_Succ
,
3880 Expressions
=> New_List
(
3881 New_Occurrence_Of
(Loop_Id
, Loc
))));
3882 Set_Suppress_Assignment_Checks
(S
);
3885 Make_Case_Statement_Alternative
(Loc
,
3886 Discrete_Choices
=> New_List
(Make_Others_Choice
(Loc
)),
3887 Statements
=> New_List
(S
)));
3889 -- Construct case statement and append to body statements
3892 Make_Case_Statement
(Loc
,
3893 Expression
=> New_Occurrence_Of
(Loop_Id
, Loc
),
3894 Alternatives
=> Alts
);
3895 Append_To
(Stmts
, Cstm
);
3900 Make_Object_Declaration
(Loc
,
3901 Defining_Identifier
=> Loop_Id
,
3902 Object_Definition
=> New_Occurrence_Of
(Ltype
, Loc
),
3903 Expression
=> Lo_Val
(First
(Stat
)));
3904 Set_Suppress_Assignment_Checks
(D
);
3907 Make_Block_Statement
(Loc
,
3908 Declarations
=> New_List
(D
),
3909 Handled_Statement_Sequence
=>
3910 Make_Handled_Sequence_Of_Statements
(Loc
,
3911 Statements
=> New_List
(
3912 Make_Loop_Statement
(Loc
,
3913 Statements
=> Stmts
,
3914 End_Label
=> Empty
)))));
3917 end Static_Predicate
;
3919 end Expand_Predicated_Loop
;
3921 ------------------------------
3922 -- Make_Tag_Ctrl_Assignment --
3923 ------------------------------
3925 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
is
3926 Asn
: constant Node_Id
:= Relocate_Node
(N
);
3927 L
: constant Node_Id
:= Name
(N
);
3928 Loc
: constant Source_Ptr
:= Sloc
(N
);
3929 Res
: constant List_Id
:= New_List
;
3930 T
: constant Entity_Id
:= Underlying_Type
(Etype
(L
));
3932 Comp_Asn
: constant Boolean := Is_Fully_Repped_Tagged_Type
(T
);
3933 Ctrl_Act
: constant Boolean := Needs_Finalization
(T
)
3934 and then not No_Ctrl_Actions
(N
);
3935 Save_Tag
: constant Boolean := Is_Tagged_Type
(T
)
3936 and then not Comp_Asn
3937 and then not No_Ctrl_Actions
(N
)
3938 and then Tagged_Type_Expansion
;
3939 -- Tags are not saved and restored when VM_Target because VM tags are
3940 -- represented implicitly in objects.
3942 Next_Id
: Entity_Id
;
3943 Prev_Id
: Entity_Id
;
3947 -- Finalize the target of the assignment when controlled
3949 -- We have two exceptions here:
3951 -- 1. If we are in an init proc since it is an initialization more
3952 -- than an assignment.
3954 -- 2. If the left-hand side is a temporary that was not initialized
3955 -- (or the parent part of a temporary since it is the case in
3956 -- extension aggregates). Such a temporary does not come from
3957 -- source. We must examine the original node for the prefix, because
3958 -- it may be a component of an entry formal, in which case it has
3959 -- been rewritten and does not appear to come from source either.
3961 -- Case of init proc
3963 if not Ctrl_Act
then
3966 -- The left hand side is an uninitialized temporary object
3968 elsif Nkind
(L
) = N_Type_Conversion
3969 and then Is_Entity_Name
(Expression
(L
))
3970 and then Nkind
(Parent
(Entity
(Expression
(L
)))) =
3971 N_Object_Declaration
3972 and then No_Initialization
(Parent
(Entity
(Expression
(L
))))
3979 (Obj_Ref
=> Duplicate_Subexpr_No_Checks
(L
),
3983 -- Save the Tag in a local variable Tag_Id
3986 Tag_Id
:= Make_Temporary
(Loc
, 'A');
3989 Make_Object_Declaration
(Loc
,
3990 Defining_Identifier
=> Tag_Id
,
3991 Object_Definition
=> New_Reference_To
(RTE
(RE_Tag
), Loc
),
3993 Make_Selected_Component
(Loc
,
3994 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
3996 New_Reference_To
(First_Tag_Component
(T
), Loc
))));
3998 -- Otherwise Tag_Id is not used
4004 -- Save the Prev and Next fields on .NET/JVM. This is not needed on non
4005 -- VM targets since the fields are not part of the object.
4007 if VM_Target
/= No_VM
4008 and then Is_Controlled
(T
)
4010 Prev_Id
:= Make_Temporary
(Loc
, 'P');
4011 Next_Id
:= Make_Temporary
(Loc
, 'N');
4014 -- Pnn : Root_Controlled_Ptr := Root_Controlled (L).Prev;
4017 Make_Object_Declaration
(Loc
,
4018 Defining_Identifier
=> Prev_Id
,
4019 Object_Definition
=>
4020 New_Reference_To
(RTE
(RE_Root_Controlled_Ptr
), Loc
),
4022 Make_Selected_Component
(Loc
,
4024 Unchecked_Convert_To
4025 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4027 Make_Identifier
(Loc
, Name_Prev
))));
4030 -- Nnn : Root_Controlled_Ptr := Root_Controlled (L).Next;
4033 Make_Object_Declaration
(Loc
,
4034 Defining_Identifier
=> Next_Id
,
4035 Object_Definition
=>
4036 New_Reference_To
(RTE
(RE_Root_Controlled_Ptr
), Loc
),
4038 Make_Selected_Component
(Loc
,
4040 Unchecked_Convert_To
4041 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4043 Make_Identifier
(Loc
, Name_Next
))));
4046 -- If the tagged type has a full rep clause, expand the assignment into
4047 -- component-wise assignments. Mark the node as unanalyzed in order to
4048 -- generate the proper code and propagate this scenario by setting a
4049 -- flag to avoid infinite recursion.
4052 Set_Analyzed
(Asn
, False);
4053 Set_Componentwise_Assignment
(Asn
, True);
4056 Append_To
(Res
, Asn
);
4062 Make_Assignment_Statement
(Loc
,
4064 Make_Selected_Component
(Loc
,
4065 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
4067 New_Reference_To
(First_Tag_Component
(T
), Loc
)),
4068 Expression
=> New_Reference_To
(Tag_Id
, Loc
)));
4071 -- Restore the Prev and Next fields on .NET/JVM
4073 if VM_Target
/= No_VM
4074 and then Is_Controlled
(T
)
4077 -- Root_Controlled (L).Prev := Prev_Id;
4080 Make_Assignment_Statement
(Loc
,
4082 Make_Selected_Component
(Loc
,
4084 Unchecked_Convert_To
4085 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4087 Make_Identifier
(Loc
, Name_Prev
)),
4088 Expression
=> New_Reference_To
(Prev_Id
, Loc
)));
4091 -- Root_Controlled (L).Next := Next_Id;
4094 Make_Assignment_Statement
(Loc
,
4096 Make_Selected_Component
(Loc
,
4098 Unchecked_Convert_To
4099 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4100 Selector_Name
=> Make_Identifier
(Loc
, Name_Next
)),
4101 Expression
=> New_Reference_To
(Next_Id
, Loc
)));
4104 -- Adjust the target after the assignment when controlled (not in the
4105 -- init proc since it is an initialization more than an assignment).
4110 (Obj_Ref
=> Duplicate_Subexpr_Move_Checks
(L
),
4118 -- Could use comment here ???
4120 when RE_Not_Available
=>
4122 end Make_Tag_Ctrl_Assignment
;