1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
29 with Einfo
; use Einfo
;
30 with Exp_Aggr
; use Exp_Aggr
;
31 with Exp_Ch7
; use Exp_Ch7
;
32 with Exp_Ch11
; use Exp_Ch11
;
33 with Exp_Dbug
; use Exp_Dbug
;
34 with Exp_Pakd
; use Exp_Pakd
;
35 with Exp_Tss
; use Exp_Tss
;
36 with Exp_Util
; use Exp_Util
;
37 with Hostparm
; use Hostparm
;
38 with Nlists
; use Nlists
;
39 with Nmake
; use Nmake
;
41 with Restrict
; use Restrict
;
42 with Rident
; use Rident
;
43 with Rtsfind
; use Rtsfind
;
44 with Sinfo
; use Sinfo
;
46 with Sem_Ch3
; use Sem_Ch3
;
47 with Sem_Ch8
; use Sem_Ch8
;
48 with Sem_Ch13
; use Sem_Ch13
;
49 with Sem_Eval
; use Sem_Eval
;
50 with Sem_Res
; use Sem_Res
;
51 with Sem_Util
; use Sem_Util
;
52 with Snames
; use Snames
;
53 with Stand
; use Stand
;
54 with Stringt
; use Stringt
;
55 with Tbuild
; use Tbuild
;
56 with Ttypes
; use Ttypes
;
57 with Uintp
; use Uintp
;
58 with Validsw
; use Validsw
;
60 package body Exp_Ch5
is
62 function Change_Of_Representation
(N
: Node_Id
) return Boolean;
63 -- Determine if the right hand side of the assignment N is a type
64 -- conversion which requires a change of representation. Called
65 -- only for the array and record cases.
67 procedure Expand_Assign_Array
(N
: Node_Id
; Rhs
: Node_Id
);
68 -- N is an assignment which assigns an array value. This routine process
69 -- the various special cases and checks required for such assignments,
70 -- including change of representation. Rhs is normally simply the right
71 -- hand side of the assignment, except that if the right hand side is
72 -- a type conversion or a qualified expression, then the Rhs is the
73 -- actual expression inside any such type conversions or qualifications.
75 function Expand_Assign_Array_Loop
82 Rev
: Boolean) return Node_Id
;
83 -- N is an assignment statement which assigns an array value. This routine
84 -- expands the assignment into a loop (or nested loops for the case of a
85 -- multi-dimensional array) to do the assignment component by component.
86 -- Larray and Rarray are the entities of the actual arrays on the left
87 -- hand and right hand sides. L_Type and R_Type are the types of these
88 -- arrays (which may not be the same, due to either sliding, or to a
89 -- change of representation case). Ndim is the number of dimensions and
90 -- the parameter Rev indicates if the loops run normally (Rev = False),
91 -- or reversed (Rev = True). The value returned is the constructed
92 -- loop statement. Auxiliary declarations are inserted before node N
93 -- using the standard Insert_Actions mechanism.
95 procedure Expand_Assign_Record
(N
: Node_Id
);
96 -- N is an assignment of a non-tagged record value. This routine handles
97 -- the case where the assignment must be made component by component,
98 -- either because the target is not byte aligned, or there is a change
101 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
;
102 -- Generate the necessary code for controlled and tagged assignment,
103 -- that is to say, finalization of the target before, adjustement of
104 -- the target after and save and restore of the tag and finalization
105 -- pointers which are not 'part of the value' and must not be changed
106 -- upon assignment. N is the original Assignment node.
108 function Possible_Bit_Aligned_Component
(N
: Node_Id
) return Boolean;
109 -- This function is used in processing the assignment of a record or
110 -- indexed component. The argument N is either the left hand or right
111 -- hand side of an assignment, and this function determines if there
112 -- is a record component reference where the record may be bit aligned
113 -- in a manner that causes trouble for the back end (see description
114 -- of Exp_Util.Component_May_Be_Bit_Aligned for further details).
116 ------------------------------
117 -- Change_Of_Representation --
118 ------------------------------
120 function Change_Of_Representation
(N
: Node_Id
) return Boolean is
121 Rhs
: constant Node_Id
:= Expression
(N
);
124 Nkind
(Rhs
) = N_Type_Conversion
126 not Same_Representation
(Etype
(Rhs
), Etype
(Expression
(Rhs
)));
127 end Change_Of_Representation
;
129 -------------------------
130 -- Expand_Assign_Array --
131 -------------------------
133 -- There are two issues here. First, do we let Gigi do a block move, or
134 -- do we expand out into a loop? Second, we need to set the two flags
135 -- Forwards_OK and Backwards_OK which show whether the block move (or
136 -- corresponding loops) can be legitimately done in a forwards (low to
137 -- high) or backwards (high to low) manner.
139 procedure Expand_Assign_Array
(N
: Node_Id
; Rhs
: Node_Id
) is
140 Loc
: constant Source_Ptr
:= Sloc
(N
);
142 Lhs
: constant Node_Id
:= Name
(N
);
144 Act_Lhs
: constant Node_Id
:= Get_Referenced_Object
(Lhs
);
145 Act_Rhs
: Node_Id
:= Get_Referenced_Object
(Rhs
);
147 L_Type
: constant Entity_Id
:=
148 Underlying_Type
(Get_Actual_Subtype
(Act_Lhs
));
149 R_Type
: Entity_Id
:=
150 Underlying_Type
(Get_Actual_Subtype
(Act_Rhs
));
152 L_Slice
: constant Boolean := Nkind
(Act_Lhs
) = N_Slice
;
153 R_Slice
: constant Boolean := Nkind
(Act_Rhs
) = N_Slice
;
155 Crep
: constant Boolean := Change_Of_Representation
(N
);
160 Ndim
: constant Pos
:= Number_Dimensions
(L_Type
);
162 Loop_Required
: Boolean := False;
163 -- This switch is set to True if the array move must be done using
164 -- an explicit front end generated loop.
166 procedure Apply_Dereference
(Arg
: in out Node_Id
);
167 -- If the argument is an access to an array, and the assignment is
168 -- converted into a procedure call, apply explicit dereference.
170 function Has_Address_Clause
(Exp
: Node_Id
) return Boolean;
171 -- Test if Exp is a reference to an array whose declaration has
172 -- an address clause, or it is a slice of such an array.
174 function Is_Formal_Array
(Exp
: Node_Id
) return Boolean;
175 -- Test if Exp is a reference to an array which is either a formal
176 -- parameter or a slice of a formal parameter. These are the cases
177 -- where hidden aliasing can occur.
179 function Is_Non_Local_Array
(Exp
: Node_Id
) return Boolean;
180 -- Determine if Exp is a reference to an array variable which is other
181 -- than an object defined in the current scope, or a slice of such
182 -- an object. Such objects can be aliased to parameters (unlike local
183 -- array references).
185 -----------------------
186 -- Apply_Dereference --
187 -----------------------
189 procedure Apply_Dereference
(Arg
: in out Node_Id
) is
190 Typ
: constant Entity_Id
:= Etype
(Arg
);
192 if Is_Access_Type
(Typ
) then
193 Rewrite
(Arg
, Make_Explicit_Dereference
(Loc
,
194 Prefix
=> Relocate_Node
(Arg
)));
195 Analyze_And_Resolve
(Arg
, Designated_Type
(Typ
));
197 end Apply_Dereference
;
199 ------------------------
200 -- Has_Address_Clause --
201 ------------------------
203 function Has_Address_Clause
(Exp
: Node_Id
) return Boolean is
206 (Is_Entity_Name
(Exp
) and then
207 Present
(Address_Clause
(Entity
(Exp
))))
209 (Nkind
(Exp
) = N_Slice
and then Has_Address_Clause
(Prefix
(Exp
)));
210 end Has_Address_Clause
;
212 ---------------------
213 -- Is_Formal_Array --
214 ---------------------
216 function Is_Formal_Array
(Exp
: Node_Id
) return Boolean is
219 (Is_Entity_Name
(Exp
) and then Is_Formal
(Entity
(Exp
)))
221 (Nkind
(Exp
) = N_Slice
and then Is_Formal_Array
(Prefix
(Exp
)));
224 ------------------------
225 -- Is_Non_Local_Array --
226 ------------------------
228 function Is_Non_Local_Array
(Exp
: Node_Id
) return Boolean is
230 return (Is_Entity_Name
(Exp
)
231 and then Scope
(Entity
(Exp
)) /= Current_Scope
)
232 or else (Nkind
(Exp
) = N_Slice
233 and then Is_Non_Local_Array
(Prefix
(Exp
)));
234 end Is_Non_Local_Array
;
236 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
238 Lhs_Formal
: constant Boolean := Is_Formal_Array
(Act_Lhs
);
239 Rhs_Formal
: constant Boolean := Is_Formal_Array
(Act_Rhs
);
241 Lhs_Non_Local_Var
: constant Boolean := Is_Non_Local_Array
(Act_Lhs
);
242 Rhs_Non_Local_Var
: constant Boolean := Is_Non_Local_Array
(Act_Rhs
);
244 -- Start of processing for Expand_Assign_Array
247 -- Deal with length check, note that the length check is done with
248 -- respect to the right hand side as given, not a possible underlying
249 -- renamed object, since this would generate incorrect extra checks.
251 Apply_Length_Check
(Rhs
, L_Type
);
253 -- We start by assuming that the move can be done in either
254 -- direction, i.e. that the two sides are completely disjoint.
256 Set_Forwards_OK
(N
, True);
257 Set_Backwards_OK
(N
, True);
259 -- Normally it is only the slice case that can lead to overlap,
260 -- and explicit checks for slices are made below. But there is
261 -- one case where the slice can be implicit and invisible to us
262 -- and that is the case where we have a one dimensional array,
263 -- and either both operands are parameters, or one is a parameter
264 -- and the other is a global variable. In this case the parameter
265 -- could be a slice that overlaps with the other parameter.
267 -- Check for the case of slices requiring an explicit loop. Normally
268 -- it is only the explicit slice cases that bother us, but in the
269 -- case of one dimensional arrays, parameters can be slices that
270 -- are passed by reference, so we can have aliasing for assignments
271 -- from one parameter to another, or assignments between parameters
272 -- and nonlocal variables. However, if the array subtype is a
273 -- constrained first subtype in the parameter case, then we don't
274 -- have to worry about overlap, since slice assignments aren't
275 -- possible (other than for a slice denoting the whole array).
277 -- Note: overlap is never possible if there is a change of
278 -- representation, so we can exclude this case.
283 ((Lhs_Formal
and Rhs_Formal
)
285 (Lhs_Formal
and Rhs_Non_Local_Var
)
287 (Rhs_Formal
and Lhs_Non_Local_Var
))
289 (not Is_Constrained
(Etype
(Lhs
))
290 or else not Is_First_Subtype
(Etype
(Lhs
)))
292 -- In the case of compiling for the Java Virtual Machine,
293 -- slices are always passed by making a copy, so we don't
294 -- have to worry about overlap. We also want to prevent
295 -- generation of "<" comparisons for array addresses,
296 -- since that's a meaningless operation on the JVM.
300 Set_Forwards_OK
(N
, False);
301 Set_Backwards_OK
(N
, False);
303 -- Note: the bit-packed case is not worrisome here, since if
304 -- we have a slice passed as a parameter, it is always aligned
305 -- on a byte boundary, and if there are no explicit slices, the
306 -- assignment can be performed directly.
309 -- We certainly must use a loop for change of representation
310 -- and also we use the operand of the conversion on the right
311 -- hand side as the effective right hand side (the component
312 -- types must match in this situation).
315 Act_Rhs
:= Get_Referenced_Object
(Rhs
);
316 R_Type
:= Get_Actual_Subtype
(Act_Rhs
);
317 Loop_Required
:= True;
319 -- We require a loop if the left side is possibly bit unaligned
321 elsif Possible_Bit_Aligned_Component
(Lhs
)
323 Possible_Bit_Aligned_Component
(Rhs
)
325 Loop_Required
:= True;
327 -- Arrays with controlled components are expanded into a loop
328 -- to force calls to adjust at the component level.
330 elsif Has_Controlled_Component
(L_Type
) then
331 Loop_Required
:= True;
333 -- If object is atomic, we cannot tolerate a loop
335 elsif Is_Atomic_Object
(Act_Lhs
)
337 Is_Atomic_Object
(Act_Rhs
)
341 -- Loop is required if we have atomic components since we have to
342 -- be sure to do any accesses on an element by element basis.
344 elsif Has_Atomic_Components
(L_Type
)
345 or else Has_Atomic_Components
(R_Type
)
346 or else Is_Atomic
(Component_Type
(L_Type
))
347 or else Is_Atomic
(Component_Type
(R_Type
))
349 Loop_Required
:= True;
351 -- Case where no slice is involved
353 elsif not L_Slice
and not R_Slice
then
355 -- The following code deals with the case of unconstrained bit
356 -- packed arrays. The problem is that the template for such
357 -- arrays contains the bounds of the actual source level array,
359 -- But the copy of an entire array requires the bounds of the
360 -- underlying array. It would be nice if the back end could take
361 -- care of this, but right now it does not know how, so if we
362 -- have such a type, then we expand out into a loop, which is
363 -- inefficient but works correctly. If we don't do this, we
364 -- get the wrong length computed for the array to be moved.
365 -- The two cases we need to worry about are:
367 -- Explicit deference of an unconstrained packed array type as
368 -- in the following example:
371 -- type BITS is array(INTEGER range <>) of BOOLEAN;
372 -- pragma PACK(BITS);
373 -- type A is access BITS;
376 -- P1 := new BITS (1 .. 65_535);
377 -- P2 := new BITS (1 .. 65_535);
381 -- A formal parameter reference with an unconstrained bit
382 -- array type is the other case we need to worry about (here
383 -- we assume the same BITS type declared above:
385 -- procedure Write_All (File : out BITS; Contents : in BITS);
387 -- File.Storage := Contents;
390 -- We expand to a loop in either of these two cases
392 -- Question for future thought. Another potentially more efficient
393 -- approach would be to create the actual subtype, and then do an
394 -- unchecked conversion to this actual subtype ???
396 Check_Unconstrained_Bit_Packed_Array
: declare
398 function Is_UBPA_Reference
(Opnd
: Node_Id
) return Boolean;
399 -- Function to perform required test for the first case,
400 -- above (dereference of an unconstrained bit packed array)
402 -----------------------
403 -- Is_UBPA_Reference --
404 -----------------------
406 function Is_UBPA_Reference
(Opnd
: Node_Id
) return Boolean is
407 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Opnd
));
409 Des_Type
: Entity_Id
;
412 if Present
(Packed_Array_Type
(Typ
))
413 and then Is_Array_Type
(Packed_Array_Type
(Typ
))
414 and then not Is_Constrained
(Packed_Array_Type
(Typ
))
418 elsif Nkind
(Opnd
) = N_Explicit_Dereference
then
419 P_Type
:= Underlying_Type
(Etype
(Prefix
(Opnd
)));
421 if not Is_Access_Type
(P_Type
) then
425 Des_Type
:= Designated_Type
(P_Type
);
427 Is_Bit_Packed_Array
(Des_Type
)
428 and then not Is_Constrained
(Des_Type
);
434 end Is_UBPA_Reference
;
436 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
439 if Is_UBPA_Reference
(Lhs
)
441 Is_UBPA_Reference
(Rhs
)
443 Loop_Required
:= True;
445 -- Here if we do not have the case of a reference to a bit
446 -- packed unconstrained array case. In this case gigi can
447 -- most certainly handle the assignment if a forwards move
450 -- (could it handle the backwards case also???)
452 elsif Forwards_OK
(N
) then
455 end Check_Unconstrained_Bit_Packed_Array
;
457 -- Gigi can always handle the assignment if the right side is a string
458 -- literal (note that overlap is definitely impossible in this case).
459 -- If the type is packed, a string literal is always converted into a
460 -- aggregate, except in the case of a null slice, for which no aggregate
461 -- can be written. In that case, rewrite the assignment as a null
462 -- statement, a length check has already been emitted to verify that
463 -- the range of the left-hand side is empty.
465 -- Note that this code is not executed if we had an assignment of
466 -- a string literal to a non-bit aligned component of a record, a
467 -- case which cannot be handled by the backend
469 elsif Nkind
(Rhs
) = N_String_Literal
then
470 if String_Length
(Strval
(Rhs
)) = 0
471 and then Is_Bit_Packed_Array
(L_Type
)
473 Rewrite
(N
, Make_Null_Statement
(Loc
));
479 -- If either operand is bit packed, then we need a loop, since we
480 -- can't be sure that the slice is byte aligned. Similarly, if either
481 -- operand is a possibly unaligned slice, then we need a loop (since
482 -- gigi cannot handle unaligned slices).
484 elsif Is_Bit_Packed_Array
(L_Type
)
485 or else Is_Bit_Packed_Array
(R_Type
)
486 or else Is_Possibly_Unaligned_Slice
(Lhs
)
487 or else Is_Possibly_Unaligned_Slice
(Rhs
)
489 Loop_Required
:= True;
491 -- If we are not bit-packed, and we have only one slice, then no
492 -- overlap is possible except in the parameter case, so we can let
493 -- gigi handle things.
495 elsif not (L_Slice
and R_Slice
) then
496 if Forwards_OK
(N
) then
501 -- If the right-hand side is a string literal, introduce a temporary
502 -- for it, for use in the generated loop that will follow.
504 if Nkind
(Rhs
) = N_String_Literal
then
506 Temp
: constant Entity_Id
:=
507 Make_Defining_Identifier
(Loc
, Name_T
);
512 Make_Object_Declaration
(Loc
,
513 Defining_Identifier
=> Temp
,
514 Object_Definition
=> New_Occurrence_Of
(L_Type
, Loc
),
515 Expression
=> Relocate_Node
(Rhs
));
517 Insert_Action
(N
, Decl
);
518 Rewrite
(Rhs
, New_Occurrence_Of
(Temp
, Loc
));
519 R_Type
:= Etype
(Temp
);
523 -- Come here to complete the analysis
525 -- Loop_Required: Set to True if we know that a loop is required
526 -- regardless of overlap considerations.
528 -- Forwards_OK: Set to False if we already know that a forwards
529 -- move is not safe, else set to True.
531 -- Backwards_OK: Set to False if we already know that a backwards
532 -- move is not safe, else set to True
534 -- Our task at this stage is to complete the overlap analysis, which
535 -- can result in possibly setting Forwards_OK or Backwards_OK to
536 -- False, and then generating the final code, either by deciding
537 -- that it is OK after all to let Gigi handle it, or by generating
538 -- appropriate code in the front end.
541 L_Index_Typ
: constant Node_Id
:= Etype
(First_Index
(L_Type
));
542 R_Index_Typ
: constant Node_Id
:= Etype
(First_Index
(R_Type
));
544 Left_Lo
: constant Node_Id
:= Type_Low_Bound
(L_Index_Typ
);
545 Left_Hi
: constant Node_Id
:= Type_High_Bound
(L_Index_Typ
);
546 Right_Lo
: constant Node_Id
:= Type_Low_Bound
(R_Index_Typ
);
547 Right_Hi
: constant Node_Id
:= Type_High_Bound
(R_Index_Typ
);
549 Act_L_Array
: Node_Id
;
550 Act_R_Array
: Node_Id
;
556 Cresult
: Compare_Result
;
559 -- Get the expressions for the arrays. If we are dealing with a
560 -- private type, then convert to the underlying type. We can do
561 -- direct assignments to an array that is a private type, but
562 -- we cannot assign to elements of the array without this extra
563 -- unchecked conversion.
565 if Nkind
(Act_Lhs
) = N_Slice
then
566 Larray
:= Prefix
(Act_Lhs
);
570 if Is_Private_Type
(Etype
(Larray
)) then
573 (Underlying_Type
(Etype
(Larray
)), Larray
);
577 if Nkind
(Act_Rhs
) = N_Slice
then
578 Rarray
:= Prefix
(Act_Rhs
);
582 if Is_Private_Type
(Etype
(Rarray
)) then
585 (Underlying_Type
(Etype
(Rarray
)), Rarray
);
589 -- If both sides are slices, we must figure out whether
590 -- it is safe to do the move in one direction or the other
591 -- It is always safe if there is a change of representation
592 -- since obviously two arrays with different representations
593 -- cannot possibly overlap.
595 if (not Crep
) and L_Slice
and R_Slice
then
596 Act_L_Array
:= Get_Referenced_Object
(Prefix
(Act_Lhs
));
597 Act_R_Array
:= Get_Referenced_Object
(Prefix
(Act_Rhs
));
599 -- If both left and right hand arrays are entity names, and
600 -- refer to different entities, then we know that the move
601 -- is safe (the two storage areas are completely disjoint).
603 if Is_Entity_Name
(Act_L_Array
)
604 and then Is_Entity_Name
(Act_R_Array
)
605 and then Entity
(Act_L_Array
) /= Entity
(Act_R_Array
)
609 -- Otherwise, we assume the worst, which is that the two
610 -- arrays are the same array. There is no need to check if
611 -- we know that is the case, because if we don't know it,
612 -- we still have to assume it!
614 -- Generally if the same array is involved, then we have
615 -- an overlapping case. We will have to really assume the
616 -- worst (i.e. set neither of the OK flags) unless we can
617 -- determine the lower or upper bounds at compile time and
621 Cresult
:= Compile_Time_Compare
(Left_Lo
, Right_Lo
);
623 if Cresult
= Unknown
then
624 Cresult
:= Compile_Time_Compare
(Left_Hi
, Right_Hi
);
628 when LT | LE | EQ
=> Set_Backwards_OK
(N
, False);
629 when GT | GE
=> Set_Forwards_OK
(N
, False);
630 when NE | Unknown
=> Set_Backwards_OK
(N
, False);
631 Set_Forwards_OK
(N
, False);
636 -- If after that analysis, Forwards_OK is still True, and
637 -- Loop_Required is False, meaning that we have not discovered
638 -- some non-overlap reason for requiring a loop, then we can
639 -- still let gigi handle it.
641 if not Loop_Required
then
642 if Forwards_OK
(N
) then
647 -- Here is where a memmove would be appropriate ???
651 -- At this stage we have to generate an explicit loop, and
652 -- we have the following cases:
654 -- Forwards_OK = True
656 -- Rnn : right_index := right_index'First;
657 -- for Lnn in left-index loop
658 -- left (Lnn) := right (Rnn);
659 -- Rnn := right_index'Succ (Rnn);
662 -- Note: the above code MUST be analyzed with checks off,
663 -- because otherwise the Succ could overflow. But in any
664 -- case this is more efficient!
666 -- Forwards_OK = False, Backwards_OK = True
668 -- Rnn : right_index := right_index'Last;
669 -- for Lnn in reverse left-index loop
670 -- left (Lnn) := right (Rnn);
671 -- Rnn := right_index'Pred (Rnn);
674 -- Note: the above code MUST be analyzed with checks off,
675 -- because otherwise the Pred could overflow. But in any
676 -- case this is more efficient!
678 -- Forwards_OK = Backwards_OK = False
680 -- This only happens if we have the same array on each side. It is
681 -- possible to create situations using overlays that violate this,
682 -- but we simply do not promise to get this "right" in this case.
684 -- There are two possible subcases. If the No_Implicit_Conditionals
685 -- restriction is set, then we generate the following code:
688 -- T : constant <operand-type> := rhs;
693 -- If implicit conditionals are permitted, then we generate:
695 -- if Left_Lo <= Right_Lo then
696 -- <code for Forwards_OK = True above>
698 -- <code for Backwards_OK = True above>
701 -- Cases where either Forwards_OK or Backwards_OK is true
703 if Forwards_OK
(N
) or else Backwards_OK
(N
) then
704 if Controlled_Type
(Component_Type
(L_Type
))
705 and then Base_Type
(L_Type
) = Base_Type
(R_Type
)
707 and then not No_Ctrl_Actions
(N
)
710 Proc
: constant Entity_Id
:=
711 TSS
(Base_Type
(L_Type
), TSS_Slice_Assign
);
715 Apply_Dereference
(Larray
);
716 Apply_Dereference
(Rarray
);
717 Actuals
:= New_List
(
718 Duplicate_Subexpr
(Larray
, Name_Req
=> True),
719 Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
720 Duplicate_Subexpr
(Left_Lo
, Name_Req
=> True),
721 Duplicate_Subexpr
(Left_Hi
, Name_Req
=> True),
722 Duplicate_Subexpr
(Right_Lo
, Name_Req
=> True),
723 Duplicate_Subexpr
(Right_Hi
, Name_Req
=> True));
727 Boolean_Literals
(not Forwards_OK
(N
)), Loc
));
730 Make_Procedure_Call_Statement
(Loc
,
731 Name
=> New_Reference_To
(Proc
, Loc
),
732 Parameter_Associations
=> Actuals
));
737 Expand_Assign_Array_Loop
738 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
739 Rev
=> not Forwards_OK
(N
)));
742 -- Case of both are false with No_Implicit_Conditionals
744 elsif Restriction_Active
(No_Implicit_Conditionals
) then
746 T
: constant Entity_Id
:=
747 Make_Defining_Identifier
(Loc
, Chars
=> Name_T
);
751 Make_Block_Statement
(Loc
,
752 Declarations
=> New_List
(
753 Make_Object_Declaration
(Loc
,
754 Defining_Identifier
=> T
,
755 Constant_Present
=> True,
757 New_Occurrence_Of
(Etype
(Rhs
), Loc
),
758 Expression
=> Relocate_Node
(Rhs
))),
760 Handled_Statement_Sequence
=>
761 Make_Handled_Sequence_Of_Statements
(Loc
,
762 Statements
=> New_List
(
763 Make_Assignment_Statement
(Loc
,
764 Name
=> Relocate_Node
(Lhs
),
765 Expression
=> New_Occurrence_Of
(T
, Loc
))))));
768 -- Case of both are false with implicit conditionals allowed
771 -- Before we generate this code, we must ensure that the
772 -- left and right side array types are defined. They may
773 -- be itypes, and we cannot let them be defined inside the
774 -- if, since the first use in the then may not be executed.
776 Ensure_Defined
(L_Type
, N
);
777 Ensure_Defined
(R_Type
, N
);
779 -- We normally compare addresses to find out which way round
780 -- to do the loop, since this is realiable, and handles the
781 -- cases of parameters, conversions etc. But we can't do that
782 -- in the bit packed case or the Java VM case, because addresses
785 if not Is_Bit_Packed_Array
(L_Type
) and then not Java_VM
then
789 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
790 Make_Attribute_Reference
(Loc
,
792 Make_Indexed_Component
(Loc
,
794 Duplicate_Subexpr_Move_Checks
(Larray
, True),
795 Expressions
=> New_List
(
796 Make_Attribute_Reference
(Loc
,
800 Attribute_Name
=> Name_First
))),
801 Attribute_Name
=> Name_Address
)),
804 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
805 Make_Attribute_Reference
(Loc
,
807 Make_Indexed_Component
(Loc
,
809 Duplicate_Subexpr_Move_Checks
(Rarray
, True),
810 Expressions
=> New_List
(
811 Make_Attribute_Reference
(Loc
,
815 Attribute_Name
=> Name_First
))),
816 Attribute_Name
=> Name_Address
)));
818 -- For the bit packed and Java VM cases we use the bounds.
819 -- That's OK, because we don't have to worry about parameters,
820 -- since they cannot cause overlap. Perhaps we should worry
821 -- about weird slice conversions ???
824 -- Copy the bounds and reset the Analyzed flag, because the
825 -- bounds of the index type itself may be universal, and must
826 -- must be reaanalyzed to acquire the proper type for Gigi.
828 Cleft_Lo
:= New_Copy_Tree
(Left_Lo
);
829 Cright_Lo
:= New_Copy_Tree
(Right_Lo
);
830 Set_Analyzed
(Cleft_Lo
, False);
831 Set_Analyzed
(Cright_Lo
, False);
835 Left_Opnd
=> Cleft_Lo
,
836 Right_Opnd
=> Cright_Lo
);
839 if Controlled_Type
(Component_Type
(L_Type
))
840 and then Base_Type
(L_Type
) = Base_Type
(R_Type
)
842 and then not No_Ctrl_Actions
(N
)
845 -- Call TSS procedure for array assignment, passing the
846 -- the explicit bounds of right- and left-hand side.
849 Proc
: constant Node_Id
:=
850 TSS
(Base_Type
(L_Type
), TSS_Slice_Assign
);
854 Apply_Dereference
(Larray
);
855 Apply_Dereference
(Rarray
);
856 Actuals
:= New_List
(
857 Duplicate_Subexpr
(Larray
, Name_Req
=> True),
858 Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
859 Duplicate_Subexpr
(Left_Lo
, Name_Req
=> True),
860 Duplicate_Subexpr
(Left_Hi
, Name_Req
=> True),
861 Duplicate_Subexpr
(Right_Lo
, Name_Req
=> True),
862 Duplicate_Subexpr
(Right_Hi
, Name_Req
=> True));
866 Right_Opnd
=> Condition
));
869 Make_Procedure_Call_Statement
(Loc
,
870 Name
=> New_Reference_To
(Proc
, Loc
),
871 Parameter_Associations
=> Actuals
));
876 Make_Implicit_If_Statement
(N
,
877 Condition
=> Condition
,
879 Then_Statements
=> New_List
(
880 Expand_Assign_Array_Loop
881 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
884 Else_Statements
=> New_List
(
885 Expand_Assign_Array_Loop
886 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
891 Analyze
(N
, Suppress
=> All_Checks
);
895 when RE_Not_Available
=>
897 end Expand_Assign_Array
;
899 ------------------------------
900 -- Expand_Assign_Array_Loop --
901 ------------------------------
903 -- The following is an example of the loop generated for the case of
904 -- a two-dimensional array:
909 -- for L1b in 1 .. 100 loop
913 -- for L3b in 1 .. 100 loop
914 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
915 -- R4b := Tm1X2'succ(R4b);
918 -- R2b := Tm1X1'succ(R2b);
922 -- Here Rev is False, and Tm1Xn are the subscript types for the right
923 -- hand side. The declarations of R2b and R4b are inserted before the
924 -- original assignment statement.
926 function Expand_Assign_Array_Loop
933 Rev
: Boolean) return Node_Id
935 Loc
: constant Source_Ptr
:= Sloc
(N
);
937 Lnn
: array (1 .. Ndim
) of Entity_Id
;
938 Rnn
: array (1 .. Ndim
) of Entity_Id
;
939 -- Entities used as subscripts on left and right sides
941 L_Index_Type
: array (1 .. Ndim
) of Entity_Id
;
942 R_Index_Type
: array (1 .. Ndim
) of Entity_Id
;
943 -- Left and right index types
955 F_Or_L
:= Name_First
;
959 -- Setup index types and subscript entities
966 L_Index
:= First_Index
(L_Type
);
967 R_Index
:= First_Index
(R_Type
);
969 for J
in 1 .. Ndim
loop
971 Make_Defining_Identifier
(Loc
,
972 Chars
=> New_Internal_Name
('L'));
975 Make_Defining_Identifier
(Loc
,
976 Chars
=> New_Internal_Name
('R'));
978 L_Index_Type
(J
) := Etype
(L_Index
);
979 R_Index_Type
(J
) := Etype
(R_Index
);
981 Next_Index
(L_Index
);
982 Next_Index
(R_Index
);
986 -- Now construct the assignment statement
989 ExprL
: constant List_Id
:= New_List
;
990 ExprR
: constant List_Id
:= New_List
;
993 for J
in 1 .. Ndim
loop
994 Append_To
(ExprL
, New_Occurrence_Of
(Lnn
(J
), Loc
));
995 Append_To
(ExprR
, New_Occurrence_Of
(Rnn
(J
), Loc
));
999 Make_Assignment_Statement
(Loc
,
1001 Make_Indexed_Component
(Loc
,
1002 Prefix
=> Duplicate_Subexpr
(Larray
, Name_Req
=> True),
1003 Expressions
=> ExprL
),
1005 Make_Indexed_Component
(Loc
,
1006 Prefix
=> Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
1007 Expressions
=> ExprR
));
1009 -- Propagate the No_Ctrl_Actions flag to individual assignments
1011 Set_No_Ctrl_Actions
(Assign
, No_Ctrl_Actions
(N
));
1014 -- Now construct the loop from the inside out, with the last subscript
1015 -- varying most rapidly. Note that Assign is first the raw assignment
1016 -- statement, and then subsequently the loop that wraps it up.
1018 for J
in reverse 1 .. Ndim
loop
1020 Make_Block_Statement
(Loc
,
1021 Declarations
=> New_List
(
1022 Make_Object_Declaration
(Loc
,
1023 Defining_Identifier
=> Rnn
(J
),
1024 Object_Definition
=>
1025 New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1027 Make_Attribute_Reference
(Loc
,
1028 Prefix
=> New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1029 Attribute_Name
=> F_Or_L
))),
1031 Handled_Statement_Sequence
=>
1032 Make_Handled_Sequence_Of_Statements
(Loc
,
1033 Statements
=> New_List
(
1034 Make_Implicit_Loop_Statement
(N
,
1036 Make_Iteration_Scheme
(Loc
,
1037 Loop_Parameter_Specification
=>
1038 Make_Loop_Parameter_Specification
(Loc
,
1039 Defining_Identifier
=> Lnn
(J
),
1040 Reverse_Present
=> Rev
,
1041 Discrete_Subtype_Definition
=>
1042 New_Reference_To
(L_Index_Type
(J
), Loc
))),
1044 Statements
=> New_List
(
1047 Make_Assignment_Statement
(Loc
,
1048 Name
=> New_Occurrence_Of
(Rnn
(J
), Loc
),
1050 Make_Attribute_Reference
(Loc
,
1052 New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1053 Attribute_Name
=> S_Or_P
,
1054 Expressions
=> New_List
(
1055 New_Occurrence_Of
(Rnn
(J
), Loc
)))))))));
1059 end Expand_Assign_Array_Loop
;
1061 --------------------------
1062 -- Expand_Assign_Record --
1063 --------------------------
1065 -- The only processing required is in the change of representation
1066 -- case, where we must expand the assignment to a series of field
1067 -- by field assignments.
1069 procedure Expand_Assign_Record
(N
: Node_Id
) is
1070 Lhs
: constant Node_Id
:= Name
(N
);
1071 Rhs
: Node_Id
:= Expression
(N
);
1074 -- If change of representation, then extract the real right hand
1075 -- side from the type conversion, and proceed with component-wise
1076 -- assignment, since the two types are not the same as far as the
1077 -- back end is concerned.
1079 if Change_Of_Representation
(N
) then
1080 Rhs
:= Expression
(Rhs
);
1082 -- If this may be a case of a large bit aligned component, then
1083 -- proceed with component-wise assignment, to avoid possible
1084 -- clobbering of other components sharing bits in the first or
1085 -- last byte of the component to be assigned.
1087 elsif Possible_Bit_Aligned_Component
(Lhs
)
1089 Possible_Bit_Aligned_Component
(Rhs
)
1093 -- If neither condition met, then nothing special to do, the back end
1094 -- can handle assignment of the entire component as a single entity.
1100 -- At this stage we know that we must do a component wise assignment
1103 Loc
: constant Source_Ptr
:= Sloc
(N
);
1104 R_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Rhs
));
1105 L_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Lhs
));
1106 Decl
: constant Node_Id
:= Declaration_Node
(R_Typ
);
1110 function Find_Component
1112 Comp
: Entity_Id
) return Entity_Id
;
1113 -- Find the component with the given name in the underlying record
1114 -- declaration for Typ. We need to use the actual entity because
1115 -- the type may be private and resolution by identifier alone would
1118 function Make_Component_List_Assign
1120 U_U
: Boolean := False) return List_Id
;
1121 -- Returns a sequence of statements to assign the components that
1122 -- are referenced in the given component list. The flag U_U is
1123 -- used to force the usage of the inferred value of the variant
1124 -- part expression as the switch for the generated case statement.
1126 function Make_Field_Assign
1128 U_U
: Boolean := False) return Node_Id
;
1129 -- Given C, the entity for a discriminant or component, build an
1130 -- assignment for the corresponding field values. The flag U_U
1131 -- signals the presence of an Unchecked_Union and forces the usage
1132 -- of the inferred discriminant value of C as the right hand side
1133 -- of the assignment.
1135 function Make_Field_Assigns
(CI
: List_Id
) return List_Id
;
1136 -- Given CI, a component items list, construct series of statements
1137 -- for fieldwise assignment of the corresponding components.
1139 --------------------
1140 -- Find_Component --
1141 --------------------
1143 function Find_Component
1145 Comp
: Entity_Id
) return Entity_Id
1147 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
1151 C
:= First_Entity
(Utyp
);
1153 while Present
(C
) loop
1154 if Chars
(C
) = Chars
(Comp
) then
1160 raise Program_Error
;
1163 --------------------------------
1164 -- Make_Component_List_Assign --
1165 --------------------------------
1167 function Make_Component_List_Assign
1169 U_U
: Boolean := False) return List_Id
1171 CI
: constant List_Id
:= Component_Items
(CL
);
1172 VP
: constant Node_Id
:= Variant_Part
(CL
);
1182 Result
:= Make_Field_Assigns
(CI
);
1184 if Present
(VP
) then
1186 V
:= First_Non_Pragma
(Variants
(VP
));
1188 while Present
(V
) loop
1191 DC
:= First
(Discrete_Choices
(V
));
1192 while Present
(DC
) loop
1193 Append_To
(DCH
, New_Copy_Tree
(DC
));
1198 Make_Case_Statement_Alternative
(Loc
,
1199 Discrete_Choices
=> DCH
,
1201 Make_Component_List_Assign
(Component_List
(V
))));
1202 Next_Non_Pragma
(V
);
1205 -- If we have an Unchecked_Union, use the value of the inferred
1206 -- discriminant of the variant part expression as the switch
1207 -- for the case statement. The case statement may later be
1212 New_Copy
(Get_Discriminant_Value
(
1215 Discriminant_Constraint
(Etype
(Rhs
))));
1218 Make_Selected_Component
(Loc
,
1219 Prefix
=> Duplicate_Subexpr
(Rhs
),
1221 Make_Identifier
(Loc
, Chars
(Name
(VP
))));
1225 Make_Case_Statement
(Loc
,
1227 Alternatives
=> Alts
));
1231 end Make_Component_List_Assign
;
1233 -----------------------
1234 -- Make_Field_Assign --
1235 -----------------------
1237 function Make_Field_Assign
1239 U_U
: Boolean := False) return Node_Id
1245 -- In the case of an Unchecked_Union, use the discriminant
1246 -- constraint value as on the right hand side of the assignment.
1250 New_Copy
(Get_Discriminant_Value
(C
,
1252 Discriminant_Constraint
(Etype
(Rhs
))));
1255 Make_Selected_Component
(Loc
,
1256 Prefix
=> Duplicate_Subexpr
(Rhs
),
1257 Selector_Name
=> New_Occurrence_Of
(C
, Loc
));
1261 Make_Assignment_Statement
(Loc
,
1263 Make_Selected_Component
(Loc
,
1264 Prefix
=> Duplicate_Subexpr
(Lhs
),
1266 New_Occurrence_Of
(Find_Component
(L_Typ
, C
), Loc
)),
1267 Expression
=> Expr
);
1269 -- Set Assignment_OK, so discriminants can be assigned
1271 Set_Assignment_OK
(Name
(A
), True);
1273 end Make_Field_Assign
;
1275 ------------------------
1276 -- Make_Field_Assigns --
1277 ------------------------
1279 function Make_Field_Assigns
(CI
: List_Id
) return List_Id
is
1286 while Present
(Item
) loop
1287 if Nkind
(Item
) = N_Component_Declaration
then
1289 (Result
, Make_Field_Assign
(Defining_Identifier
(Item
)));
1296 end Make_Field_Assigns
;
1298 -- Start of processing for Expand_Assign_Record
1301 -- Note that we use the base types for this processing. This results
1302 -- in some extra work in the constrained case, but the change of
1303 -- representation case is so unusual that it is not worth the effort.
1305 -- First copy the discriminants. This is done unconditionally. It
1306 -- is required in the unconstrained left side case, and also in the
1307 -- case where this assignment was constructed during the expansion
1308 -- of a type conversion (since initialization of discriminants is
1309 -- suppressed in this case). It is unnecessary but harmless in
1312 if Has_Discriminants
(L_Typ
) then
1313 F
:= First_Discriminant
(R_Typ
);
1314 while Present
(F
) loop
1316 if Is_Unchecked_Union
(Base_Type
(R_Typ
)) then
1317 Insert_Action
(N
, Make_Field_Assign
(F
, True));
1319 Insert_Action
(N
, Make_Field_Assign
(F
));
1322 Next_Discriminant
(F
);
1326 -- We know the underlying type is a record, but its current view
1327 -- may be private. We must retrieve the usable record declaration.
1329 if Nkind
(Decl
) = N_Private_Type_Declaration
1330 and then Present
(Full_View
(R_Typ
))
1332 RDef
:= Type_Definition
(Declaration_Node
(Full_View
(R_Typ
)));
1334 RDef
:= Type_Definition
(Decl
);
1337 if Nkind
(RDef
) = N_Record_Definition
1338 and then Present
(Component_List
(RDef
))
1341 if Is_Unchecked_Union
(R_Typ
) then
1343 Make_Component_List_Assign
(Component_List
(RDef
), True));
1346 (N
, Make_Component_List_Assign
(Component_List
(RDef
)));
1349 Rewrite
(N
, Make_Null_Statement
(Loc
));
1353 end Expand_Assign_Record
;
1355 -----------------------------------
1356 -- Expand_N_Assignment_Statement --
1357 -----------------------------------
1359 -- For array types, deal with slice assignments and setting the flags
1360 -- to indicate if it can be statically determined which direction the
1361 -- move should go in. Also deal with generating range/length checks.
1363 procedure Expand_N_Assignment_Statement
(N
: Node_Id
) is
1364 Loc
: constant Source_Ptr
:= Sloc
(N
);
1365 Lhs
: constant Node_Id
:= Name
(N
);
1366 Rhs
: constant Node_Id
:= Expression
(N
);
1367 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Lhs
));
1371 -- First deal with generation of range check if required. For now
1372 -- we do this only for discrete types.
1374 if Do_Range_Check
(Rhs
)
1375 and then Is_Discrete_Type
(Typ
)
1377 Set_Do_Range_Check
(Rhs
, False);
1378 Generate_Range_Check
(Rhs
, Typ
, CE_Range_Check_Failed
);
1381 -- Check for a special case where a high level transformation is
1382 -- required. If we have either of:
1387 -- where P is a reference to a bit packed array, then we have to unwind
1388 -- the assignment. The exact meaning of being a reference to a bit
1389 -- packed array is as follows:
1391 -- An indexed component whose prefix is a bit packed array is a
1392 -- reference to a bit packed array.
1394 -- An indexed component or selected component whose prefix is a
1395 -- reference to a bit packed array is itself a reference ot a
1396 -- bit packed array.
1398 -- The required transformation is
1400 -- Tnn : prefix_type := P;
1401 -- Tnn.field := rhs;
1406 -- Tnn : prefix_type := P;
1407 -- Tnn (subscr) := rhs;
1410 -- Since P is going to be evaluated more than once, any subscripts
1411 -- in P must have their evaluation forced.
1413 if (Nkind
(Lhs
) = N_Indexed_Component
1415 Nkind
(Lhs
) = N_Selected_Component
)
1416 and then Is_Ref_To_Bit_Packed_Array
(Prefix
(Lhs
))
1419 BPAR_Expr
: constant Node_Id
:= Relocate_Node
(Prefix
(Lhs
));
1420 BPAR_Typ
: constant Entity_Id
:= Etype
(BPAR_Expr
);
1421 Tnn
: constant Entity_Id
:=
1422 Make_Defining_Identifier
(Loc
,
1423 Chars
=> New_Internal_Name
('T'));
1426 -- Insert the post assignment first, because we want to copy
1427 -- the BPAR_Expr tree before it gets analyzed in the context
1428 -- of the pre assignment. Note that we do not analyze the
1429 -- post assignment yet (we cannot till we have completed the
1430 -- analysis of the pre assignment). As usual, the analysis
1431 -- of this post assignment will happen on its own when we
1432 -- "run into" it after finishing the current assignment.
1435 Make_Assignment_Statement
(Loc
,
1436 Name
=> New_Copy_Tree
(BPAR_Expr
),
1437 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
1439 -- At this stage BPAR_Expr is a reference to a bit packed
1440 -- array where the reference was not expanded in the original
1441 -- tree, since it was on the left side of an assignment. But
1442 -- in the pre-assignment statement (the object definition),
1443 -- BPAR_Expr will end up on the right hand side, and must be
1444 -- reexpanded. To achieve this, we reset the analyzed flag
1445 -- of all selected and indexed components down to the actual
1446 -- indexed component for the packed array.
1450 Set_Analyzed
(Exp
, False);
1452 if Nkind
(Exp
) = N_Selected_Component
1454 Nkind
(Exp
) = N_Indexed_Component
1456 Exp
:= Prefix
(Exp
);
1462 -- Now we can insert and analyze the pre-assignment
1464 -- If the right-hand side requires a transient scope, it has
1465 -- already been placed on the stack. However, the declaration is
1466 -- inserted in the tree outside of this scope, and must reflect
1467 -- the proper scope for its variable. This awkward bit is forced
1468 -- by the stricter scope discipline imposed by GCC 2.97.
1471 Uses_Transient_Scope
: constant Boolean :=
1472 Scope_Is_Transient
and then N
= Node_To_Be_Wrapped
;
1475 if Uses_Transient_Scope
then
1476 New_Scope
(Scope
(Current_Scope
));
1479 Insert_Before_And_Analyze
(N
,
1480 Make_Object_Declaration
(Loc
,
1481 Defining_Identifier
=> Tnn
,
1482 Object_Definition
=> New_Occurrence_Of
(BPAR_Typ
, Loc
),
1483 Expression
=> BPAR_Expr
));
1485 if Uses_Transient_Scope
then
1490 -- Now fix up the original assignment and continue processing
1492 Rewrite
(Prefix
(Lhs
),
1493 New_Occurrence_Of
(Tnn
, Loc
));
1495 -- We do not need to reanalyze that assignment, and we do not need
1496 -- to worry about references to the temporary, but we do need to
1497 -- make sure that the temporary is not marked as a true constant
1498 -- since we now have a generate assignment to it!
1500 Set_Is_True_Constant
(Tnn
, False);
1504 -- When we have the appropriate type of aggregate in the
1505 -- expression (it has been determined during analysis of the
1506 -- aggregate by setting the delay flag), let's perform in place
1507 -- assignment and thus avoid creating a temporay.
1509 if Is_Delayed_Aggregate
(Rhs
) then
1510 Convert_Aggr_In_Assignment
(N
);
1511 Rewrite
(N
, Make_Null_Statement
(Loc
));
1516 -- Apply discriminant check if required. If Lhs is an access type
1517 -- to a designated type with discriminants, we must always check.
1519 if Has_Discriminants
(Etype
(Lhs
)) then
1521 -- Skip discriminant check if change of representation. Will be
1522 -- done when the change of representation is expanded out.
1524 if not Change_Of_Representation
(N
) then
1525 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
), Lhs
);
1528 -- If the type is private without discriminants, and the full type
1529 -- has discriminants (necessarily with defaults) a check may still be
1530 -- necessary if the Lhs is aliased. The private determinants must be
1531 -- visible to build the discriminant constraints.
1533 -- Only an explicit dereference that comes from source indicates
1534 -- aliasing. Access to formals of protected operations and entries
1535 -- create dereferences but are not semantic aliasings.
1537 elsif Is_Private_Type
(Etype
(Lhs
))
1538 and then Has_Discriminants
(Typ
)
1539 and then Nkind
(Lhs
) = N_Explicit_Dereference
1540 and then Comes_From_Source
(Lhs
)
1543 Lt
: constant Entity_Id
:= Etype
(Lhs
);
1545 Set_Etype
(Lhs
, Typ
);
1546 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Typ
), Rhs
));
1547 Apply_Discriminant_Check
(Rhs
, Typ
, Lhs
);
1548 Set_Etype
(Lhs
, Lt
);
1551 -- If the Lhs has a private type with unknown discriminants, it
1552 -- may have a full view with discriminants, but those are nameable
1553 -- only in the underlying type, so convert the Rhs to it before
1554 -- potential checking.
1556 elsif Has_Unknown_Discriminants
(Base_Type
(Etype
(Lhs
)))
1557 and then Has_Discriminants
(Typ
)
1559 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Typ
), Rhs
));
1560 Apply_Discriminant_Check
(Rhs
, Typ
, Lhs
);
1562 -- In the access type case, we need the same discriminant check,
1563 -- and also range checks if we have an access to constrained array.
1565 elsif Is_Access_Type
(Etype
(Lhs
))
1566 and then Is_Constrained
(Designated_Type
(Etype
(Lhs
)))
1568 if Has_Discriminants
(Designated_Type
(Etype
(Lhs
))) then
1570 -- Skip discriminant check if change of representation. Will be
1571 -- done when the change of representation is expanded out.
1573 if not Change_Of_Representation
(N
) then
1574 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
));
1577 elsif Is_Array_Type
(Designated_Type
(Etype
(Lhs
))) then
1578 Apply_Range_Check
(Rhs
, Etype
(Lhs
));
1580 if Is_Constrained
(Etype
(Lhs
)) then
1581 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
1584 if Nkind
(Rhs
) = N_Allocator
then
1586 Target_Typ
: constant Entity_Id
:= Etype
(Expression
(Rhs
));
1587 C_Es
: Check_Result
;
1594 Etype
(Designated_Type
(Etype
(Lhs
))));
1606 -- Apply range check for access type case
1608 elsif Is_Access_Type
(Etype
(Lhs
))
1609 and then Nkind
(Rhs
) = N_Allocator
1610 and then Nkind
(Expression
(Rhs
)) = N_Qualified_Expression
1612 Analyze_And_Resolve
(Expression
(Rhs
));
1614 (Expression
(Rhs
), Designated_Type
(Etype
(Lhs
)));
1617 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
1618 -- type to force the corresponding run-time check
1620 if Is_Access_Type
(Typ
)
1622 ((Is_Entity_Name
(Lhs
) and then Can_Never_Be_Null
(Entity
(Lhs
)))
1623 or else Can_Never_Be_Null
(Etype
(Lhs
)))
1625 Rewrite
(Rhs
, Convert_To
(Etype
(Lhs
),
1626 Relocate_Node
(Rhs
)));
1627 Analyze_And_Resolve
(Rhs
, Etype
(Lhs
));
1630 -- If we are assigning an access type and the left side is an
1631 -- entity, then make sure that Is_Known_Non_Null properly
1632 -- reflects the state of the entity after the assignment
1634 if Is_Access_Type
(Typ
)
1635 and then Is_Entity_Name
(Lhs
)
1636 and then Known_Non_Null
(Rhs
)
1637 and then Safe_To_Capture_Value
(N
, Entity
(Lhs
))
1639 Set_Is_Known_Non_Null
(Entity
(Lhs
), Known_Non_Null
(Rhs
));
1642 -- Case of assignment to a bit packed array element
1644 if Nkind
(Lhs
) = N_Indexed_Component
1645 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
)))
1647 Expand_Bit_Packed_Element_Set
(N
);
1650 -- Case of tagged type assignment
1652 elsif Is_Tagged_Type
(Typ
)
1653 or else (Controlled_Type
(Typ
) and then not Is_Array_Type
(Typ
))
1655 Tagged_Case
: declare
1656 L
: List_Id
:= No_List
;
1657 Expand_Ctrl_Actions
: constant Boolean := not No_Ctrl_Actions
(N
);
1660 -- In the controlled case, we need to make sure that function
1661 -- calls are evaluated before finalizing the target. In all
1662 -- cases, it makes the expansion easier if the side-effects
1663 -- are removed first.
1665 Remove_Side_Effects
(Lhs
);
1666 Remove_Side_Effects
(Rhs
);
1668 -- Avoid recursion in the mechanism
1672 -- If dispatching assignment, we need to dispatch to _assign
1674 if Is_Class_Wide_Type
(Typ
)
1676 -- If the type is tagged, we may as well use the predefined
1677 -- primitive assignment. This avoids inlining a lot of code
1678 -- and in the class-wide case, the assignment is replaced by
1679 -- a dispatch call to _assign. Note that this cannot be done
1680 -- when discriminant checks are locally suppressed (as in
1681 -- extension aggregate expansions) because otherwise the
1682 -- discriminant check will be performed within the _assign
1685 or else (Is_Tagged_Type
(Typ
)
1686 and then Chars
(Current_Scope
) /= Name_uAssign
1687 and then Expand_Ctrl_Actions
1688 and then not Discriminant_Checks_Suppressed
(Empty
))
1690 -- Fetch the primitive op _assign and proper type to call
1691 -- it. Because of possible conflits between private and
1692 -- full view the proper type is fetched directly from the
1693 -- operation profile.
1696 Op
: constant Entity_Id
:=
1697 Find_Prim_Op
(Typ
, Name_uAssign
);
1698 F_Typ
: Entity_Id
:= Etype
(First_Formal
(Op
));
1701 -- If the assignment is dispatching, make sure to use the
1702 -- ??? where is rest of this comment ???
1704 if Is_Class_Wide_Type
(Typ
) then
1705 F_Typ
:= Class_Wide_Type
(F_Typ
);
1709 Make_Procedure_Call_Statement
(Loc
,
1710 Name
=> New_Reference_To
(Op
, Loc
),
1711 Parameter_Associations
=> New_List
(
1712 Unchecked_Convert_To
(F_Typ
, Duplicate_Subexpr
(Lhs
)),
1713 Unchecked_Convert_To
(F_Typ
,
1714 Duplicate_Subexpr
(Rhs
)))));
1718 L
:= Make_Tag_Ctrl_Assignment
(N
);
1720 -- We can't afford to have destructive Finalization Actions
1721 -- in the Self assignment case, so if the target and the
1722 -- source are not obviously different, code is generated to
1723 -- avoid the self assignment case
1725 -- if lhs'address /= rhs'address then
1726 -- <code for controlled and/or tagged assignment>
1729 if not Statically_Different
(Lhs
, Rhs
)
1730 and then Expand_Ctrl_Actions
1733 Make_Implicit_If_Statement
(N
,
1737 Make_Attribute_Reference
(Loc
,
1738 Prefix
=> Duplicate_Subexpr
(Lhs
),
1739 Attribute_Name
=> Name_Address
),
1742 Make_Attribute_Reference
(Loc
,
1743 Prefix
=> Duplicate_Subexpr
(Rhs
),
1744 Attribute_Name
=> Name_Address
)),
1746 Then_Statements
=> L
));
1749 -- We need to set up an exception handler for implementing
1750 -- 7.6.1 (18). The remaining adjustments are tackled by the
1751 -- implementation of adjust for record_controllers (see
1754 -- This is skipped if we have no finalization
1756 if Expand_Ctrl_Actions
1757 and then not Restriction_Active
(No_Finalization
)
1760 Make_Block_Statement
(Loc
,
1761 Handled_Statement_Sequence
=>
1762 Make_Handled_Sequence_Of_Statements
(Loc
,
1764 Exception_Handlers
=> New_List
(
1765 Make_Exception_Handler
(Loc
,
1766 Exception_Choices
=>
1767 New_List
(Make_Others_Choice
(Loc
)),
1768 Statements
=> New_List
(
1769 Make_Raise_Program_Error
(Loc
,
1771 PE_Finalize_Raised_Exception
)
1777 Make_Block_Statement
(Loc
,
1778 Handled_Statement_Sequence
=>
1779 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> L
)));
1781 -- If no restrictions on aborts, protect the whole assignement
1782 -- for controlled objects as per 9.8(11)
1784 if Controlled_Type
(Typ
)
1785 and then Expand_Ctrl_Actions
1786 and then Abort_Allowed
1789 Blk
: constant Entity_Id
:=
1790 New_Internal_Entity
(
1791 E_Block
, Current_Scope
, Sloc
(N
), 'B');
1794 Set_Scope
(Blk
, Current_Scope
);
1795 Set_Etype
(Blk
, Standard_Void_Type
);
1796 Set_Identifier
(N
, New_Occurrence_Of
(Blk
, Sloc
(N
)));
1798 Prepend_To
(L
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
1799 Set_At_End_Proc
(Handled_Statement_Sequence
(N
),
1800 New_Occurrence_Of
(RTE
(RE_Abort_Undefer_Direct
), Loc
));
1801 Expand_At_End_Handler
1802 (Handled_Statement_Sequence
(N
), Blk
);
1812 elsif Is_Array_Type
(Typ
) then
1814 Actual_Rhs
: Node_Id
:= Rhs
;
1817 while Nkind
(Actual_Rhs
) = N_Type_Conversion
1819 Nkind
(Actual_Rhs
) = N_Qualified_Expression
1821 Actual_Rhs
:= Expression
(Actual_Rhs
);
1824 Expand_Assign_Array
(N
, Actual_Rhs
);
1830 elsif Is_Record_Type
(Typ
) then
1831 Expand_Assign_Record
(N
);
1834 -- Scalar types. This is where we perform the processing related
1835 -- to the requirements of (RM 13.9.1(9-11)) concerning the handling
1836 -- of invalid scalar values.
1838 elsif Is_Scalar_Type
(Typ
) then
1840 -- Case where right side is known valid
1842 if Expr_Known_Valid
(Rhs
) then
1844 -- Here the right side is valid, so it is fine. The case to
1845 -- deal with is when the left side is a local variable reference
1846 -- whose value is not currently known to be valid. If this is
1847 -- the case, and the assignment appears in an unconditional
1848 -- context, then we can mark the left side as now being valid.
1850 if Is_Local_Variable_Reference
(Lhs
)
1851 and then not Is_Known_Valid
(Entity
(Lhs
))
1852 and then In_Unconditional_Context
(N
)
1854 Set_Is_Known_Valid
(Entity
(Lhs
), True);
1857 -- Case where right side may be invalid in the sense of the RM
1858 -- reference above. The RM does not require that we check for
1859 -- the validity on an assignment, but it does require that the
1860 -- assignment of an invalid value not cause erroneous behavior.
1862 -- The general approach in GNAT is to use the Is_Known_Valid flag
1863 -- to avoid the need for validity checking on assignments. However
1864 -- in some cases, we have to do validity checking in order to make
1865 -- sure that the setting of this flag is correct.
1868 -- Validate right side if we are validating copies
1870 if Validity_Checks_On
1871 and then Validity_Check_Copies
1875 -- We can propagate this to the left side where appropriate
1877 if Is_Local_Variable_Reference
(Lhs
)
1878 and then not Is_Known_Valid
(Entity
(Lhs
))
1879 and then In_Unconditional_Context
(N
)
1881 Set_Is_Known_Valid
(Entity
(Lhs
), True);
1884 -- Otherwise check to see what should be done
1886 -- If left side is a local variable, then we just set its
1887 -- flag to indicate that its value may no longer be valid,
1888 -- since we are copying a potentially invalid value.
1890 elsif Is_Local_Variable_Reference
(Lhs
) then
1891 Set_Is_Known_Valid
(Entity
(Lhs
), False);
1893 -- Check for case of a nonlocal variable on the left side
1894 -- which is currently known to be valid. In this case, we
1895 -- simply ensure that the right side is valid. We only play
1896 -- the game of copying validity status for local variables,
1897 -- since we are doing this statically, not by tracing the
1900 elsif Is_Entity_Name
(Lhs
)
1901 and then Is_Known_Valid
(Entity
(Lhs
))
1903 -- Note that the Ensure_Valid call is ignored if the
1904 -- Validity_Checking mode is set to none so we do not
1905 -- need to worry about that case here.
1909 -- In all other cases, we can safely copy an invalid value
1910 -- without worrying about the status of the left side. Since
1911 -- it is not a variable reference it will not be considered
1912 -- as being known to be valid in any case.
1920 -- Defend against invalid subscripts on left side if we are in
1921 -- standard validity checking mode. No need to do this if we
1922 -- are checking all subscripts.
1924 if Validity_Checks_On
1925 and then Validity_Check_Default
1926 and then not Validity_Check_Subscripts
1928 Check_Valid_Lvalue_Subscripts
(Lhs
);
1932 when RE_Not_Available
=>
1934 end Expand_N_Assignment_Statement
;
1936 ------------------------------
1937 -- Expand_N_Block_Statement --
1938 ------------------------------
1940 -- Encode entity names defined in block statement
1942 procedure Expand_N_Block_Statement
(N
: Node_Id
) is
1944 Qualify_Entity_Names
(N
);
1945 end Expand_N_Block_Statement
;
1947 -----------------------------
1948 -- Expand_N_Case_Statement --
1949 -----------------------------
1951 procedure Expand_N_Case_Statement
(N
: Node_Id
) is
1952 Loc
: constant Source_Ptr
:= Sloc
(N
);
1953 Expr
: constant Node_Id
:= Expression
(N
);
1961 -- Check for the situation where we know at compile time which
1962 -- branch will be taken
1964 if Compile_Time_Known_Value
(Expr
) then
1965 Alt
:= Find_Static_Alternative
(N
);
1967 -- Move the statements from this alternative after the case
1968 -- statement. They are already analyzed, so will be skipped
1971 Insert_List_After
(N
, Statements
(Alt
));
1973 -- That leaves the case statement as a shell. The alternative
1974 -- that will be executed is reset to a null list. So now we can
1975 -- kill the entire case statement.
1977 Kill_Dead_Code
(Expression
(N
));
1978 Kill_Dead_Code
(Alternatives
(N
));
1979 Rewrite
(N
, Make_Null_Statement
(Loc
));
1983 -- Here if the choice is not determined at compile time
1986 Last_Alt
: constant Node_Id
:= Last
(Alternatives
(N
));
1988 Others_Present
: Boolean;
1989 Others_Node
: Node_Id
;
1991 Then_Stms
: List_Id
;
1992 Else_Stms
: List_Id
;
1995 if Nkind
(First
(Discrete_Choices
(Last_Alt
))) = N_Others_Choice
then
1996 Others_Present
:= True;
1997 Others_Node
:= Last_Alt
;
1999 Others_Present
:= False;
2002 -- First step is to worry about possible invalid argument. The RM
2003 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2004 -- outside the base range), then Constraint_Error must be raised.
2006 -- Case of validity check required (validity checks are on, the
2007 -- expression is not known to be valid, and the case statement
2008 -- comes from source -- no need to validity check internally
2009 -- generated case statements).
2011 if Validity_Check_Default
then
2012 Ensure_Valid
(Expr
);
2015 -- If there is only a single alternative, just replace it with
2016 -- the sequence of statements since obviously that is what is
2017 -- going to be executed in all cases.
2019 Len
:= List_Length
(Alternatives
(N
));
2022 -- We still need to evaluate the expression if it has any
2025 Remove_Side_Effects
(Expression
(N
));
2027 Insert_List_After
(N
, Statements
(First
(Alternatives
(N
))));
2029 -- That leaves the case statement as a shell. The alternative
2030 -- that will be executed is reset to a null list. So now we can
2031 -- kill the entire case statement.
2033 Kill_Dead_Code
(Expression
(N
));
2034 Rewrite
(N
, Make_Null_Statement
(Loc
));
2038 -- An optimization. If there are only two alternatives, and only
2039 -- a single choice, then rewrite the whole case statement as an
2040 -- if statement, since this can result in susbequent optimizations.
2041 -- This helps not only with case statements in the source of a
2042 -- simple form, but also with generated code (discriminant check
2043 -- functions in particular)
2046 Chlist
:= Discrete_Choices
(First
(Alternatives
(N
)));
2048 if List_Length
(Chlist
) = 1 then
2049 Choice
:= First
(Chlist
);
2051 Then_Stms
:= Statements
(First
(Alternatives
(N
)));
2052 Else_Stms
:= Statements
(Last
(Alternatives
(N
)));
2054 -- For TRUE, generate "expression", not expression = true
2056 if Nkind
(Choice
) = N_Identifier
2057 and then Entity
(Choice
) = Standard_True
2059 Cond
:= Expression
(N
);
2061 -- For FALSE, generate "expression" and switch then/else
2063 elsif Nkind
(Choice
) = N_Identifier
2064 and then Entity
(Choice
) = Standard_False
2066 Cond
:= Expression
(N
);
2067 Else_Stms
:= Statements
(First
(Alternatives
(N
)));
2068 Then_Stms
:= Statements
(Last
(Alternatives
(N
)));
2070 -- For a range, generate "expression in range"
2072 elsif Nkind
(Choice
) = N_Range
2073 or else (Nkind
(Choice
) = N_Attribute_Reference
2074 and then Attribute_Name
(Choice
) = Name_Range
)
2075 or else (Is_Entity_Name
(Choice
)
2076 and then Is_Type
(Entity
(Choice
)))
2077 or else Nkind
(Choice
) = N_Subtype_Indication
2081 Left_Opnd
=> Expression
(N
),
2082 Right_Opnd
=> Relocate_Node
(Choice
));
2084 -- For any other subexpression "expression = value"
2089 Left_Opnd
=> Expression
(N
),
2090 Right_Opnd
=> Relocate_Node
(Choice
));
2093 -- Now rewrite the case as an IF
2096 Make_If_Statement
(Loc
,
2098 Then_Statements
=> Then_Stms
,
2099 Else_Statements
=> Else_Stms
));
2105 -- If the last alternative is not an Others choice, replace it
2106 -- with an N_Others_Choice. Note that we do not bother to call
2107 -- Analyze on the modified case statement, since it's only effect
2108 -- would be to compute the contents of the Others_Discrete_Choices
2109 -- which is not needed by the back end anyway.
2111 -- The reason we do this is that the back end always needs some
2112 -- default for a switch, so if we have not supplied one in the
2113 -- processing above for validity checking, then we need to
2116 if not Others_Present
then
2117 Others_Node
:= Make_Others_Choice
(Sloc
(Last_Alt
));
2118 Set_Others_Discrete_Choices
2119 (Others_Node
, Discrete_Choices
(Last_Alt
));
2120 Set_Discrete_Choices
(Last_Alt
, New_List
(Others_Node
));
2123 end Expand_N_Case_Statement
;
2125 -----------------------------
2126 -- Expand_N_Exit_Statement --
2127 -----------------------------
2129 -- The only processing required is to deal with a possible C/Fortran
2130 -- boolean value used as the condition for the exit statement.
2132 procedure Expand_N_Exit_Statement
(N
: Node_Id
) is
2134 Adjust_Condition
(Condition
(N
));
2135 end Expand_N_Exit_Statement
;
2137 -----------------------------
2138 -- Expand_N_Goto_Statement --
2139 -----------------------------
2141 -- Add poll before goto if polling active
2143 procedure Expand_N_Goto_Statement
(N
: Node_Id
) is
2145 Generate_Poll_Call
(N
);
2146 end Expand_N_Goto_Statement
;
2148 ---------------------------
2149 -- Expand_N_If_Statement --
2150 ---------------------------
2152 -- First we deal with the case of C and Fortran convention boolean
2153 -- values, with zero/non-zero semantics.
2155 -- Second, we deal with the obvious rewriting for the cases where the
2156 -- condition of the IF is known at compile time to be True or False.
2158 -- Third, we remove elsif parts which have non-empty Condition_Actions
2159 -- and rewrite as independent if statements. For example:
2170 -- <<condition actions of y>>
2176 -- This rewriting is needed if at least one elsif part has a non-empty
2177 -- Condition_Actions list. We also do the same processing if there is
2178 -- a constant condition in an elsif part (in conjunction with the first
2179 -- processing step mentioned above, for the recursive call made to deal
2180 -- with the created inner if, this deals with properly optimizing the
2181 -- cases of constant elsif conditions).
2183 procedure Expand_N_If_Statement
(N
: Node_Id
) is
2184 Loc
: constant Source_Ptr
:= Sloc
(N
);
2190 Adjust_Condition
(Condition
(N
));
2192 -- The following loop deals with constant conditions for the IF. We
2193 -- need a loop because as we eliminate False conditions, we grab the
2194 -- first elsif condition and use it as the primary condition.
2196 while Compile_Time_Known_Value
(Condition
(N
)) loop
2198 -- If condition is True, we can simply rewrite the if statement
2199 -- now by replacing it by the series of then statements.
2201 if Is_True
(Expr_Value
(Condition
(N
))) then
2203 -- All the else parts can be killed
2205 Kill_Dead_Code
(Elsif_Parts
(N
));
2206 Kill_Dead_Code
(Else_Statements
(N
));
2208 Hed
:= Remove_Head
(Then_Statements
(N
));
2209 Insert_List_After
(N
, Then_Statements
(N
));
2213 -- If condition is False, then we can delete the condition and
2214 -- the Then statements
2217 -- We do not delete the condition if constant condition
2218 -- warnings are enabled, since otherwise we end up deleting
2219 -- the desired warning. Of course the backend will get rid
2220 -- of this True/False test anyway, so nothing is lost here.
2222 if not Constant_Condition_Warnings
then
2223 Kill_Dead_Code
(Condition
(N
));
2226 Kill_Dead_Code
(Then_Statements
(N
));
2228 -- If there are no elsif statements, then we simply replace
2229 -- the entire if statement by the sequence of else statements.
2231 if No
(Elsif_Parts
(N
)) then
2233 if No
(Else_Statements
(N
))
2234 or else Is_Empty_List
(Else_Statements
(N
))
2237 Make_Null_Statement
(Sloc
(N
)));
2240 Hed
:= Remove_Head
(Else_Statements
(N
));
2241 Insert_List_After
(N
, Else_Statements
(N
));
2247 -- If there are elsif statements, the first of them becomes
2248 -- the if/then section of the rebuilt if statement This is
2249 -- the case where we loop to reprocess this copied condition.
2252 Hed
:= Remove_Head
(Elsif_Parts
(N
));
2253 Insert_Actions
(N
, Condition_Actions
(Hed
));
2254 Set_Condition
(N
, Condition
(Hed
));
2255 Set_Then_Statements
(N
, Then_Statements
(Hed
));
2257 if Is_Empty_List
(Elsif_Parts
(N
)) then
2258 Set_Elsif_Parts
(N
, No_List
);
2264 -- Loop through elsif parts, dealing with constant conditions and
2265 -- possible expression actions that are present.
2267 if Present
(Elsif_Parts
(N
)) then
2268 E
:= First
(Elsif_Parts
(N
));
2269 while Present
(E
) loop
2270 Adjust_Condition
(Condition
(E
));
2272 -- If there are condition actions, then we rewrite the if
2273 -- statement as indicated above. We also do the same rewrite
2274 -- if the condition is True or False. The further processing
2275 -- of this constant condition is then done by the recursive
2276 -- call to expand the newly created if statement
2278 if Present
(Condition_Actions
(E
))
2279 or else Compile_Time_Known_Value
(Condition
(E
))
2281 -- Note this is not an implicit if statement, since it is
2282 -- part of an explicit if statement in the source (or of an
2283 -- implicit if statement that has already been tested).
2286 Make_If_Statement
(Sloc
(E
),
2287 Condition
=> Condition
(E
),
2288 Then_Statements
=> Then_Statements
(E
),
2289 Elsif_Parts
=> No_List
,
2290 Else_Statements
=> Else_Statements
(N
));
2292 -- Elsif parts for new if come from remaining elsif's of parent
2294 while Present
(Next
(E
)) loop
2295 if No
(Elsif_Parts
(New_If
)) then
2296 Set_Elsif_Parts
(New_If
, New_List
);
2299 Append
(Remove_Next
(E
), Elsif_Parts
(New_If
));
2302 Set_Else_Statements
(N
, New_List
(New_If
));
2304 if Present
(Condition_Actions
(E
)) then
2305 Insert_List_Before
(New_If
, Condition_Actions
(E
));
2310 if Is_Empty_List
(Elsif_Parts
(N
)) then
2311 Set_Elsif_Parts
(N
, No_List
);
2317 -- No special processing for that elsif part, move to next
2325 -- Some more optimizations applicable if we still have an IF statement
2327 if Nkind
(N
) /= N_If_Statement
then
2331 -- Another optimization, special cases that can be simplified
2333 -- if expression then
2339 -- can be changed to:
2341 -- return expression;
2345 -- if expression then
2351 -- can be changed to:
2353 -- return not (expression);
2355 if Nkind
(N
) = N_If_Statement
2356 and then No
(Elsif_Parts
(N
))
2357 and then Present
(Else_Statements
(N
))
2358 and then List_Length
(Then_Statements
(N
)) = 1
2359 and then List_Length
(Else_Statements
(N
)) = 1
2362 Then_Stm
: constant Node_Id
:= First
(Then_Statements
(N
));
2363 Else_Stm
: constant Node_Id
:= First
(Else_Statements
(N
));
2366 if Nkind
(Then_Stm
) = N_Return_Statement
2368 Nkind
(Else_Stm
) = N_Return_Statement
2371 Then_Expr
: constant Node_Id
:= Expression
(Then_Stm
);
2372 Else_Expr
: constant Node_Id
:= Expression
(Else_Stm
);
2375 if Nkind
(Then_Expr
) = N_Identifier
2377 Nkind
(Else_Expr
) = N_Identifier
2379 if Entity
(Then_Expr
) = Standard_True
2380 and then Entity
(Else_Expr
) = Standard_False
2383 Make_Return_Statement
(Loc
,
2384 Expression
=> Relocate_Node
(Condition
(N
))));
2388 elsif Entity
(Then_Expr
) = Standard_False
2389 and then Entity
(Else_Expr
) = Standard_True
2392 Make_Return_Statement
(Loc
,
2395 Right_Opnd
=> Relocate_Node
(Condition
(N
)))));
2404 end Expand_N_If_Statement
;
2406 -----------------------------
2407 -- Expand_N_Loop_Statement --
2408 -----------------------------
2410 -- 1. Deal with while condition for C/Fortran boolean
2411 -- 2. Deal with loops with a non-standard enumeration type range
2412 -- 3. Deal with while loops where Condition_Actions is set
2413 -- 4. Insert polling call if required
2415 procedure Expand_N_Loop_Statement
(N
: Node_Id
) is
2416 Loc
: constant Source_Ptr
:= Sloc
(N
);
2417 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
2420 if Present
(Isc
) then
2421 Adjust_Condition
(Condition
(Isc
));
2424 if Is_Non_Empty_List
(Statements
(N
)) then
2425 Generate_Poll_Call
(First
(Statements
(N
)));
2432 -- Handle the case where we have a for loop with the range type being
2433 -- an enumeration type with non-standard representation. In this case
2436 -- for x in [reverse] a .. b loop
2442 -- for xP in [reverse] integer
2443 -- range etype'Pos (a) .. etype'Pos (b) loop
2445 -- x : constant etype := Pos_To_Rep (xP);
2451 if Present
(Loop_Parameter_Specification
(Isc
)) then
2453 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Isc
);
2454 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
2455 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
2456 Btype
: constant Entity_Id
:= Base_Type
(Ltype
);
2461 if not Is_Enumeration_Type
(Btype
)
2462 or else No
(Enum_Pos_To_Rep
(Btype
))
2468 Make_Defining_Identifier
(Loc
,
2469 Chars
=> New_External_Name
(Chars
(Loop_Id
), 'P'));
2471 -- If the type has a contiguous representation, successive
2472 -- values can be generated as offsets from the first literal.
2474 if Has_Contiguous_Rep
(Btype
) then
2476 Unchecked_Convert_To
(Btype
,
2479 Make_Integer_Literal
(Loc
,
2480 Enumeration_Rep
(First_Literal
(Btype
))),
2481 Right_Opnd
=> New_Reference_To
(New_Id
, Loc
)));
2483 -- Use the constructed array Enum_Pos_To_Rep
2486 Make_Indexed_Component
(Loc
,
2487 Prefix
=> New_Reference_To
(Enum_Pos_To_Rep
(Btype
), Loc
),
2488 Expressions
=> New_List
(New_Reference_To
(New_Id
, Loc
)));
2492 Make_Loop_Statement
(Loc
,
2493 Identifier
=> Identifier
(N
),
2496 Make_Iteration_Scheme
(Loc
,
2497 Loop_Parameter_Specification
=>
2498 Make_Loop_Parameter_Specification
(Loc
,
2499 Defining_Identifier
=> New_Id
,
2500 Reverse_Present
=> Reverse_Present
(LPS
),
2502 Discrete_Subtype_Definition
=>
2503 Make_Subtype_Indication
(Loc
,
2506 New_Reference_To
(Standard_Natural
, Loc
),
2509 Make_Range_Constraint
(Loc
,
2514 Make_Attribute_Reference
(Loc
,
2516 New_Reference_To
(Btype
, Loc
),
2518 Attribute_Name
=> Name_Pos
,
2520 Expressions
=> New_List
(
2522 (Type_Low_Bound
(Ltype
)))),
2525 Make_Attribute_Reference
(Loc
,
2527 New_Reference_To
(Btype
, Loc
),
2529 Attribute_Name
=> Name_Pos
,
2531 Expressions
=> New_List
(
2533 (Type_High_Bound
(Ltype
))))))))),
2535 Statements
=> New_List
(
2536 Make_Block_Statement
(Loc
,
2537 Declarations
=> New_List
(
2538 Make_Object_Declaration
(Loc
,
2539 Defining_Identifier
=> Loop_Id
,
2540 Constant_Present
=> True,
2541 Object_Definition
=> New_Reference_To
(Ltype
, Loc
),
2542 Expression
=> Expr
)),
2544 Handled_Statement_Sequence
=>
2545 Make_Handled_Sequence_Of_Statements
(Loc
,
2546 Statements
=> Statements
(N
)))),
2548 End_Label
=> End_Label
(N
)));
2552 -- Second case, if we have a while loop with Condition_Actions set,
2553 -- then we change it into a plain loop:
2562 -- <<condition actions>>
2568 and then Present
(Condition_Actions
(Isc
))
2575 Make_Exit_Statement
(Sloc
(Condition
(Isc
)),
2577 Make_Op_Not
(Sloc
(Condition
(Isc
)),
2578 Right_Opnd
=> Condition
(Isc
)));
2580 Prepend
(ES
, Statements
(N
));
2581 Insert_List_Before
(ES
, Condition_Actions
(Isc
));
2583 -- This is not an implicit loop, since it is generated in
2584 -- response to the loop statement being processed. If this
2585 -- is itself implicit, the restriction has already been
2586 -- checked. If not, it is an explicit loop.
2589 Make_Loop_Statement
(Sloc
(N
),
2590 Identifier
=> Identifier
(N
),
2591 Statements
=> Statements
(N
),
2592 End_Label
=> End_Label
(N
)));
2597 end Expand_N_Loop_Statement
;
2599 -------------------------------
2600 -- Expand_N_Return_Statement --
2601 -------------------------------
2603 procedure Expand_N_Return_Statement
(N
: Node_Id
) is
2604 Loc
: constant Source_Ptr
:= Sloc
(N
);
2605 Exp
: constant Node_Id
:= Expression
(N
);
2609 Scope_Id
: Entity_Id
;
2613 Goto_Stat
: Node_Id
;
2616 Return_Type
: Entity_Id
;
2617 Result_Exp
: Node_Id
;
2618 Result_Id
: Entity_Id
;
2619 Result_Obj
: Node_Id
;
2622 -- Case where returned expression is present
2624 if Present
(Exp
) then
2626 -- Always normalize C/Fortran boolean result. This is not always
2627 -- necessary, but it seems a good idea to minimize the passing
2628 -- around of non-normalized values, and in any case this handles
2629 -- the processing of barrier functions for protected types, which
2630 -- turn the condition into a return statement.
2632 Exptyp
:= Etype
(Exp
);
2634 if Is_Boolean_Type
(Exptyp
)
2635 and then Nonzero_Is_True
(Exptyp
)
2637 Adjust_Condition
(Exp
);
2638 Adjust_Result_Type
(Exp
, Exptyp
);
2641 -- Do validity check if enabled for returns
2643 if Validity_Checks_On
2644 and then Validity_Check_Returns
2650 -- Find relevant enclosing scope from which return is returning
2652 Cur_Idx
:= Scope_Stack
.Last
;
2654 Scope_Id
:= Scope_Stack
.Table
(Cur_Idx
).Entity
;
2656 if Ekind
(Scope_Id
) /= E_Block
2657 and then Ekind
(Scope_Id
) /= E_Loop
2662 Cur_Idx
:= Cur_Idx
- 1;
2663 pragma Assert
(Cur_Idx
>= 0);
2668 Kind
:= Ekind
(Scope_Id
);
2670 -- If it is a return from procedures do no extra steps
2672 if Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
2676 pragma Assert
(Is_Entry
(Scope_Id
));
2678 -- Look at the enclosing block to see whether the return is from
2679 -- an accept statement or an entry body.
2681 for J
in reverse 0 .. Cur_Idx
loop
2682 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
2683 exit when Is_Concurrent_Type
(Scope_Id
);
2686 -- If it is a return from accept statement it should be expanded
2687 -- as a call to RTS Complete_Rendezvous and a goto to the end of
2690 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
2691 -- Expand_N_Accept_Alternative in exp_ch9.adb)
2693 if Is_Task_Type
(Scope_Id
) then
2695 Call
:= (Make_Procedure_Call_Statement
(Loc
,
2696 Name
=> New_Reference_To
2697 (RTE
(RE_Complete_Rendezvous
), Loc
)));
2698 Insert_Before
(N
, Call
);
2699 -- why not insert actions here???
2702 Acc_Stat
:= Parent
(N
);
2703 while Nkind
(Acc_Stat
) /= N_Accept_Statement
loop
2704 Acc_Stat
:= Parent
(Acc_Stat
);
2707 Lab_Node
:= Last
(Statements
2708 (Handled_Statement_Sequence
(Acc_Stat
)));
2710 Goto_Stat
:= Make_Goto_Statement
(Loc
,
2711 Name
=> New_Occurrence_Of
2712 (Entity
(Identifier
(Lab_Node
)), Loc
));
2714 Set_Analyzed
(Goto_Stat
);
2716 Rewrite
(N
, Goto_Stat
);
2719 -- If it is a return from an entry body, put a Complete_Entry_Body
2720 -- call in front of the return.
2722 elsif Is_Protected_Type
(Scope_Id
) then
2725 Make_Procedure_Call_Statement
(Loc
,
2726 Name
=> New_Reference_To
2727 (RTE
(RE_Complete_Entry_Body
), Loc
),
2728 Parameter_Associations
=> New_List
2729 (Make_Attribute_Reference
(Loc
,
2733 (Corresponding_Body
(Parent
(Scope_Id
))),
2735 Attribute_Name
=> Name_Unchecked_Access
)));
2737 Insert_Before
(N
, Call
);
2746 Return_Type
:= Etype
(Scope_Id
);
2747 Utyp
:= Underlying_Type
(Return_Type
);
2749 -- Check the result expression of a scalar function against
2750 -- the subtype of the function by inserting a conversion.
2751 -- This conversion must eventually be performed for other
2752 -- classes of types, but for now it's only done for scalars.
2755 if Is_Scalar_Type
(T
) then
2756 Rewrite
(Exp
, Convert_To
(Return_Type
, Exp
));
2760 -- Implement the rules of 6.5(8-10), which require a tag check in
2761 -- the case of a limited tagged return type, and tag reassignment
2762 -- for nonlimited tagged results. These actions are needed when
2763 -- the return type is a specific tagged type and the result
2764 -- expression is a conversion or a formal parameter, because in
2765 -- that case the tag of the expression might differ from the tag
2766 -- of the specific result type.
2768 if Is_Tagged_Type
(Utyp
)
2769 and then not Is_Class_Wide_Type
(Utyp
)
2770 and then (Nkind
(Exp
) = N_Type_Conversion
2771 or else Nkind
(Exp
) = N_Unchecked_Type_Conversion
2772 or else (Is_Entity_Name
(Exp
)
2773 and then Ekind
(Entity
(Exp
)) in Formal_Kind
))
2775 -- When the return type is limited, perform a check that the
2776 -- tag of the result is the same as the tag of the return type.
2778 if Is_Limited_Type
(Return_Type
) then
2780 Make_Raise_Constraint_Error
(Loc
,
2784 Make_Selected_Component
(Loc
,
2785 Prefix
=> Duplicate_Subexpr
(Exp
),
2787 New_Reference_To
(Tag_Component
(Utyp
), Loc
)),
2789 Unchecked_Convert_To
(RTE
(RE_Tag
),
2791 (Access_Disp_Table
(Base_Type
(Utyp
)), Loc
))),
2792 Reason
=> CE_Tag_Check_Failed
));
2794 -- If the result type is a specific nonlimited tagged type,
2795 -- then we have to ensure that the tag of the result is that
2796 -- of the result type. This is handled by making a copy of the
2797 -- expression in the case where it might have a different tag,
2798 -- namely when the expression is a conversion or a formal
2799 -- parameter. We create a new object of the result type and
2800 -- initialize it from the expression, which will implicitly
2801 -- force the tag to be set appropriately.
2805 Make_Defining_Identifier
(Loc
, New_Internal_Name
('R'));
2806 Result_Exp
:= New_Reference_To
(Result_Id
, Loc
);
2809 Make_Object_Declaration
(Loc
,
2810 Defining_Identifier
=> Result_Id
,
2811 Object_Definition
=> New_Reference_To
(Return_Type
, Loc
),
2812 Constant_Present
=> True,
2813 Expression
=> Relocate_Node
(Exp
));
2815 Set_Assignment_OK
(Result_Obj
);
2816 Insert_Action
(Exp
, Result_Obj
);
2818 Rewrite
(Exp
, Result_Exp
);
2819 Analyze_And_Resolve
(Exp
, Return_Type
);
2823 -- Deal with returning variable length objects and controlled types
2825 -- Nothing to do if we are returning by reference, or this is not
2826 -- a type that requires special processing (indicated by the fact
2827 -- that it requires a cleanup scope for the secondary stack case)
2829 if Is_Return_By_Reference_Type
(T
)
2830 or else not Requires_Transient_Scope
(Return_Type
)
2834 -- Case of secondary stack not used
2836 elsif Function_Returns_With_DSP
(Scope_Id
) then
2838 -- Here what we need to do is to always return by reference, since
2839 -- we will return with the stack pointer depressed. We may need to
2840 -- do a copy to a local temporary before doing this return.
2842 No_Secondary_Stack_Case
: declare
2843 Local_Copy_Required
: Boolean := False;
2844 -- Set to True if a local copy is required
2846 Copy_Ent
: Entity_Id
;
2847 -- Used for the target entity if a copy is required
2850 -- Declaration used to create copy if needed
2852 procedure Test_Copy_Required
(Expr
: Node_Id
);
2853 -- Determines if Expr represents a return value for which a
2854 -- copy is required. More specifically, a copy is not required
2855 -- if Expr represents an object or component of an object that
2856 -- is either in the local subprogram frame, or is constant.
2857 -- If a copy is required, then Local_Copy_Required is set True.
2859 ------------------------
2860 -- Test_Copy_Required --
2861 ------------------------
2863 procedure Test_Copy_Required
(Expr
: Node_Id
) is
2867 -- If component, test prefix (object containing component)
2869 if Nkind
(Expr
) = N_Indexed_Component
2871 Nkind
(Expr
) = N_Selected_Component
2873 Test_Copy_Required
(Prefix
(Expr
));
2876 -- See if we have an entity name
2878 elsif Is_Entity_Name
(Expr
) then
2879 Ent
:= Entity
(Expr
);
2881 -- Constant entity is always OK, no copy required
2883 if Ekind
(Ent
) = E_Constant
then
2886 -- No copy required for local variable
2888 elsif Ekind
(Ent
) = E_Variable
2889 and then Scope
(Ent
) = Current_Subprogram
2895 -- All other cases require a copy
2897 Local_Copy_Required
:= True;
2898 end Test_Copy_Required
;
2900 -- Start of processing for No_Secondary_Stack_Case
2903 -- No copy needed if result is from a function call.
2904 -- In this case the result is already being returned by
2905 -- reference with the stack pointer depressed.
2907 -- To make up for a gcc 2.8.1 deficiency (???), we perform
2908 -- the copy for array types if the constrained status of the
2909 -- target type is different from that of the expression.
2911 if Requires_Transient_Scope
(T
)
2913 (not Is_Array_Type
(T
)
2914 or else Is_Constrained
(T
) = Is_Constrained
(Return_Type
)
2915 or else Controlled_Type
(T
))
2916 and then Nkind
(Exp
) = N_Function_Call
2920 -- We always need a local copy for a controlled type, since
2921 -- we are required to finalize the local value before return.
2922 -- The copy will automatically include the required finalize.
2923 -- Moreover, gigi cannot make this copy, since we need special
2924 -- processing to ensure proper behavior for finalization.
2926 -- Note: the reason we are returning with a depressed stack
2927 -- pointer in the controlled case (even if the type involved
2928 -- is constrained) is that we must make a local copy to deal
2929 -- properly with the requirement that the local result be
2932 elsif Controlled_Type
(Utyp
) then
2934 Make_Defining_Identifier
(Loc
,
2935 Chars
=> New_Internal_Name
('R'));
2937 -- Build declaration to do the copy, and insert it, setting
2938 -- Assignment_OK, because we may be copying a limited type.
2939 -- In addition we set the special flag to inhibit finalize
2940 -- attachment if this is a controlled type (since this attach
2941 -- must be done by the caller, otherwise if we attach it here
2942 -- we will finalize the returned result prematurely).
2945 Make_Object_Declaration
(Loc
,
2946 Defining_Identifier
=> Copy_Ent
,
2947 Object_Definition
=> New_Occurrence_Of
(Return_Type
, Loc
),
2948 Expression
=> Relocate_Node
(Exp
));
2950 Set_Assignment_OK
(Decl
);
2951 Set_Delay_Finalize_Attach
(Decl
);
2952 Insert_Action
(N
, Decl
);
2954 -- Now the actual return uses the copied value
2956 Rewrite
(Exp
, New_Occurrence_Of
(Copy_Ent
, Loc
));
2957 Analyze_And_Resolve
(Exp
, Return_Type
);
2959 -- Since we have made the copy, gigi does not have to, so
2960 -- we set the By_Ref flag to prevent another copy being made.
2964 -- Non-controlled cases
2967 Test_Copy_Required
(Exp
);
2969 -- If a local copy is required, then gigi will make the
2970 -- copy, otherwise, we can return the result directly,
2971 -- so set By_Ref to suppress the gigi copy.
2973 if not Local_Copy_Required
then
2977 end No_Secondary_Stack_Case
;
2979 -- Here if secondary stack is used
2982 -- Make sure that no surrounding block will reclaim the
2983 -- secondary-stack on which we are going to put the result.
2984 -- Not only may this introduce secondary stack leaks but worse,
2985 -- if the reclamation is done too early, then the result we are
2986 -- returning may get clobbered. See example in 7417-003.
2989 S
: Entity_Id
:= Current_Scope
;
2992 while Ekind
(S
) = E_Block
or else Ekind
(S
) = E_Loop
loop
2993 Set_Sec_Stack_Needed_For_Return
(S
, True);
2994 S
:= Enclosing_Dynamic_Scope
(S
);
2998 -- Optimize the case where the result is a function call. In this
2999 -- case either the result is already on the secondary stack, or is
3000 -- already being returned with the stack pointer depressed and no
3001 -- further processing is required except to set the By_Ref flag to
3002 -- ensure that gigi does not attempt an extra unnecessary copy.
3003 -- (actually not just unnecessary but harmfully wrong in the case
3004 -- of a controlled type, where gigi does not know how to do a copy).
3005 -- To make up for a gcc 2.8.1 deficiency (???), we perform
3006 -- the copy for array types if the constrained status of the
3007 -- target type is different from that of the expression.
3009 if Requires_Transient_Scope
(T
)
3011 (not Is_Array_Type
(T
)
3012 or else Is_Constrained
(T
) = Is_Constrained
(Return_Type
)
3013 or else Controlled_Type
(T
))
3014 and then Nkind
(Exp
) = N_Function_Call
3018 -- For controlled types, do the allocation on the sec-stack
3019 -- manually in order to call adjust at the right time
3020 -- type Anon1 is access Return_Type;
3021 -- for Anon1'Storage_pool use ss_pool;
3022 -- Anon2 : anon1 := new Return_Type'(expr);
3023 -- return Anon2.all;
3025 elsif Controlled_Type
(Utyp
) then
3027 Loc
: constant Source_Ptr
:= Sloc
(N
);
3028 Temp
: constant Entity_Id
:=
3029 Make_Defining_Identifier
(Loc
,
3030 Chars
=> New_Internal_Name
('R'));
3031 Acc_Typ
: constant Entity_Id
:=
3032 Make_Defining_Identifier
(Loc
,
3033 Chars
=> New_Internal_Name
('A'));
3034 Alloc_Node
: Node_Id
;
3037 Set_Ekind
(Acc_Typ
, E_Access_Type
);
3039 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_SS_Pool
));
3042 Make_Allocator
(Loc
,
3044 Make_Qualified_Expression
(Loc
,
3045 Subtype_Mark
=> New_Reference_To
(Etype
(Exp
), Loc
),
3046 Expression
=> Relocate_Node
(Exp
)));
3048 Insert_List_Before_And_Analyze
(N
, New_List
(
3049 Make_Full_Type_Declaration
(Loc
,
3050 Defining_Identifier
=> Acc_Typ
,
3052 Make_Access_To_Object_Definition
(Loc
,
3053 Subtype_Indication
=>
3054 New_Reference_To
(Return_Type
, Loc
))),
3056 Make_Object_Declaration
(Loc
,
3057 Defining_Identifier
=> Temp
,
3058 Object_Definition
=> New_Reference_To
(Acc_Typ
, Loc
),
3059 Expression
=> Alloc_Node
)));
3062 Make_Explicit_Dereference
(Loc
,
3063 Prefix
=> New_Reference_To
(Temp
, Loc
)));
3065 Analyze_And_Resolve
(Exp
, Return_Type
);
3068 -- Otherwise use the gigi mechanism to allocate result on the
3072 Set_Storage_Pool
(N
, RTE
(RE_SS_Pool
));
3074 -- If we are generating code for the Java VM do not use
3075 -- SS_Allocate since everything is heap-allocated anyway.
3078 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
3084 when RE_Not_Available
=>
3086 end Expand_N_Return_Statement
;
3088 ------------------------------
3089 -- Make_Tag_Ctrl_Assignment --
3090 ------------------------------
3092 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
is
3093 Loc
: constant Source_Ptr
:= Sloc
(N
);
3094 L
: constant Node_Id
:= Name
(N
);
3095 T
: constant Entity_Id
:= Underlying_Type
(Etype
(L
));
3097 Ctrl_Act
: constant Boolean := Controlled_Type
(T
)
3098 and then not No_Ctrl_Actions
(N
);
3100 Save_Tag
: constant Boolean := Is_Tagged_Type
(T
)
3101 and then not No_Ctrl_Actions
(N
)
3102 and then not Java_VM
;
3103 -- Tags are not saved and restored when Java_VM because JVM tags
3104 -- are represented implicitly in objects.
3107 Tag_Tmp
: Entity_Id
;
3112 -- Finalize the target of the assignment when controlled.
3113 -- We have two exceptions here:
3115 -- 1. If we are in an init proc since it is an initialization
3116 -- more than an assignment
3118 -- 2. If the left-hand side is a temporary that was not initialized
3119 -- (or the parent part of a temporary since it is the case in
3120 -- extension aggregates). Such a temporary does not come from
3121 -- source. We must examine the original node for the prefix, because
3122 -- it may be a component of an entry formal, in which case it has
3123 -- been rewritten and does not appear to come from source either.
3125 -- Case of init proc
3127 if not Ctrl_Act
then
3130 -- The left hand side is an uninitialized temporary
3132 elsif Nkind
(L
) = N_Type_Conversion
3133 and then Is_Entity_Name
(Expression
(L
))
3134 and then No_Initialization
(Parent
(Entity
(Expression
(L
))))
3138 Append_List_To
(Res
,
3140 Ref
=> Duplicate_Subexpr_No_Checks
(L
),
3142 With_Detach
=> New_Reference_To
(Standard_False
, Loc
)));
3145 -- Save the Tag in a local variable Tag_Tmp
3149 Make_Defining_Identifier
(Loc
, New_Internal_Name
('A'));
3152 Make_Object_Declaration
(Loc
,
3153 Defining_Identifier
=> Tag_Tmp
,
3154 Object_Definition
=> New_Reference_To
(RTE
(RE_Tag
), Loc
),
3156 Make_Selected_Component
(Loc
,
3157 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
3158 Selector_Name
=> New_Reference_To
(Tag_Component
(T
), Loc
))));
3160 -- Otherwise Tag_Tmp not used
3166 -- Processing for controlled types and types with controlled components
3168 -- Variables of such types contain pointers used to chain them in
3169 -- finalization lists, in addition to user data. These pointers are
3170 -- specific to each object of the type, not to the value being assigned.
3171 -- Thus they need to be left intact during the assignment. We achieve
3172 -- this by constructing a Storage_Array subtype, and by overlaying
3173 -- objects of this type on the source and target of the assignment.
3174 -- The assignment is then rewritten to assignments of slices of these
3175 -- arrays, copying the user data, and leaving the pointers untouched.
3178 Controlled_Actions
: declare
3180 -- A reference to the Prev component of the record controller
3182 First_After_Root
: Node_Id
:= Empty
;
3183 -- Index of first byte to be copied (used to skip
3184 -- Root_Controlled in controlled objects).
3186 Last_Before_Hole
: Node_Id
:= Empty
;
3187 -- Index of last byte to be copied before outermost record
3190 Hole_Length
: Node_Id
:= Empty
;
3191 -- Length of record controller data (Prev and Next pointers)
3193 First_After_Hole
: Node_Id
:= Empty
;
3194 -- Index of first byte to be copied after outermost record
3197 Expr
, Source_Size
: Node_Id
;
3198 -- Used for computation of the size of the data to be copied
3200 Range_Type
: Entity_Id
;
3201 Opaque_Type
: Entity_Id
;
3203 function Build_Slice
3206 Hi
: Node_Id
) return Node_Id
;
3207 -- Build and return a slice of an array of type S overlaid
3208 -- on object Rec, with bounds specified by Lo and Hi. If either
3209 -- bound is empty, a default of S'First (respectively S'Last)
3216 function Build_Slice
3219 Hi
: Node_Id
) return Node_Id
3224 Opaque
: constant Node_Id
:=
3225 Unchecked_Convert_To
(Opaque_Type
,
3226 Make_Attribute_Reference
(Loc
,
3228 Attribute_Name
=> Name_Address
));
3229 -- Access value designating an opaque storage array of
3230 -- type S overlaid on record Rec.
3233 -- Compute slice bounds using S'First (1) and S'Last
3234 -- as default values when not specified by the caller.
3237 Lo_Bound
:= Make_Integer_Literal
(Loc
, 1);
3243 Hi_Bound
:= Make_Attribute_Reference
(Loc
,
3244 Prefix
=> New_Occurrence_Of
(Range_Type
, Loc
),
3245 Attribute_Name
=> Name_Last
);
3250 return Make_Slice
(Loc
,
3253 Discrete_Range
=> Make_Range
(Loc
,
3254 Lo_Bound
, Hi_Bound
));
3257 -- Start of processing for Controlled_Actions
3260 -- Create a constrained subtype of Storage_Array whose size
3261 -- corresponds to the value being assigned.
3263 -- subtype G is Storage_Offset range
3264 -- 1 .. (Expr'Size + Storage_Unit - 1) / Storage_Unit
3266 Expr
:= Duplicate_Subexpr_No_Checks
(Expression
(N
));
3268 if Nkind
(Expr
) = N_Qualified_Expression
then
3269 Expr
:= Expression
(Expr
);
3275 Make_Attribute_Reference
(Loc
,
3281 Make_Integer_Literal
(Loc
,
3282 System_Storage_Unit
- 1));
3284 -- If Expr is a type conversion, standard Ada does not allow
3285 -- 'Size to be taken on it, but Gigi can handle this case,
3286 -- and thus we can determine the amount of data to be copied.
3287 -- The appropriate circuitry is enabled only for conversions
3288 -- that do not Come_From_Source.
3290 Set_Comes_From_Source
(Prefix
(Left_Opnd
(Source_Size
)), False);
3293 Make_Op_Divide
(Loc
,
3294 Left_Opnd
=> Source_Size
,
3296 Make_Integer_Literal
(Loc
,
3297 Intval
=> System_Storage_Unit
));
3300 Make_Defining_Identifier
(Loc
,
3301 New_Internal_Name
('G'));
3304 Make_Subtype_Declaration
(Loc
,
3305 Defining_Identifier
=> Range_Type
,
3306 Subtype_Indication
=>
3307 Make_Subtype_Indication
(Loc
,
3309 New_Reference_To
(RTE
(RE_Storage_Offset
), Loc
),
3310 Constraint
=> Make_Range_Constraint
(Loc
,
3313 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
3314 High_Bound
=> Source_Size
)))));
3316 -- subtype S is Storage_Array (G)
3319 Make_Subtype_Declaration
(Loc
,
3320 Defining_Identifier
=>
3321 Make_Defining_Identifier
(Loc
,
3322 New_Internal_Name
('S')),
3323 Subtype_Indication
=>
3324 Make_Subtype_Indication
(Loc
,
3326 New_Reference_To
(RTE
(RE_Storage_Array
), Loc
),
3328 Make_Index_Or_Discriminant_Constraint
(Loc
,
3330 New_List
(New_Reference_To
(Range_Type
, Loc
))))));
3332 -- type A is access S
3335 Make_Defining_Identifier
(Loc
,
3336 Chars
=> New_Internal_Name
('A'));
3339 Make_Full_Type_Declaration
(Loc
,
3340 Defining_Identifier
=> Opaque_Type
,
3342 Make_Access_To_Object_Definition
(Loc
,
3343 Subtype_Indication
=>
3345 Defining_Identifier
(Last
(Res
)), Loc
))));
3347 -- Generate appropriate slice assignments
3349 First_After_Root
:= Make_Integer_Literal
(Loc
, 1);
3351 -- For the case of a controlled object, skip the
3352 -- Root_Controlled part.
3354 if Is_Controlled
(T
) then
3358 Make_Op_Divide
(Loc
,
3359 Make_Attribute_Reference
(Loc
,
3361 New_Occurrence_Of
(RTE
(RE_Root_Controlled
), Loc
),
3362 Attribute_Name
=> Name_Size
),
3363 Make_Integer_Literal
(Loc
, System_Storage_Unit
)));
3366 -- For the case of a record with controlled components, skip
3367 -- the Prev and Next components of the record controller.
3368 -- These components constitute a 'hole' in the middle of the
3369 -- data to be copied.
3371 if Has_Controlled_Component
(T
) then
3373 Make_Selected_Component
(Loc
,
3375 Make_Selected_Component
(Loc
,
3376 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
3378 New_Reference_To
(Controller_Component
(T
), Loc
)),
3379 Selector_Name
=> Make_Identifier
(Loc
, Name_Prev
));
3381 -- Last index before hole: determined by position of
3382 -- the _Controller.Prev component.
3385 Make_Defining_Identifier
(Loc
,
3386 New_Internal_Name
('L'));
3389 Make_Object_Declaration
(Loc
,
3390 Defining_Identifier
=> Last_Before_Hole
,
3391 Object_Definition
=> New_Occurrence_Of
(
3392 RTE
(RE_Storage_Offset
), Loc
),
3393 Constant_Present
=> True,
3394 Expression
=> Make_Op_Add
(Loc
,
3395 Make_Attribute_Reference
(Loc
,
3397 Attribute_Name
=> Name_Position
),
3398 Make_Attribute_Reference
(Loc
,
3399 Prefix
=> New_Copy_Tree
(Prefix
(Prev_Ref
)),
3400 Attribute_Name
=> Name_Position
))));
3402 -- Hole length: size of the Prev and Next components
3405 Make_Op_Multiply
(Loc
,
3406 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_2
),
3408 Make_Op_Divide
(Loc
,
3410 Make_Attribute_Reference
(Loc
,
3411 Prefix
=> New_Copy_Tree
(Prev_Ref
),
3412 Attribute_Name
=> Name_Size
),
3414 Make_Integer_Literal
(Loc
,
3415 Intval
=> System_Storage_Unit
)));
3417 -- First index after hole
3420 Make_Defining_Identifier
(Loc
,
3421 New_Internal_Name
('F'));
3424 Make_Object_Declaration
(Loc
,
3425 Defining_Identifier
=> First_After_Hole
,
3426 Object_Definition
=> New_Occurrence_Of
(
3427 RTE
(RE_Storage_Offset
), Loc
),
3428 Constant_Present
=> True,
3434 New_Occurrence_Of
(Last_Before_Hole
, Loc
),
3435 Right_Opnd
=> Hole_Length
),
3436 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1))));
3438 Last_Before_Hole
:= New_Occurrence_Of
(Last_Before_Hole
, Loc
);
3439 First_After_Hole
:= New_Occurrence_Of
(First_After_Hole
, Loc
);
3442 -- Assign the first slice (possibly skipping Root_Controlled,
3443 -- up to the beginning of the record controller if present,
3444 -- up to the end of the object if not).
3446 Append_To
(Res
, Make_Assignment_Statement
(Loc
,
3447 Name
=> Build_Slice
(
3448 Rec
=> Duplicate_Subexpr_No_Checks
(L
),
3449 Lo
=> First_After_Root
,
3450 Hi
=> Last_Before_Hole
),
3452 Expression
=> Build_Slice
(
3453 Rec
=> Expression
(N
),
3454 Lo
=> First_After_Root
,
3455 Hi
=> New_Copy_Tree
(Last_Before_Hole
))));
3457 if Present
(First_After_Hole
) then
3459 -- If a record controller is present, copy the second slice,
3460 -- from right after the _Controller.Next component up to the
3461 -- end of the object.
3463 Append_To
(Res
, Make_Assignment_Statement
(Loc
,
3464 Name
=> Build_Slice
(
3465 Rec
=> Duplicate_Subexpr_No_Checks
(L
),
3466 Lo
=> First_After_Hole
,
3468 Expression
=> Build_Slice
(
3469 Rec
=> Duplicate_Subexpr_No_Checks
(Expression
(N
)),
3470 Lo
=> New_Copy_Tree
(First_After_Hole
),
3473 end Controlled_Actions
;
3476 Append_To
(Res
, Relocate_Node
(N
));
3483 Make_Assignment_Statement
(Loc
,
3485 Make_Selected_Component
(Loc
,
3486 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
3487 Selector_Name
=> New_Reference_To
(Tag_Component
(T
), Loc
)),
3488 Expression
=> New_Reference_To
(Tag_Tmp
, Loc
)));
3491 -- Adjust the target after the assignment when controlled (not in the
3492 -- init proc since it is an initialization more than an assignment).
3495 Append_List_To
(Res
,
3497 Ref
=> Duplicate_Subexpr_Move_Checks
(L
),
3499 Flist_Ref
=> New_Reference_To
(RTE
(RE_Global_Final_List
), Loc
),
3500 With_Attach
=> Make_Integer_Literal
(Loc
, 0)));
3506 -- Could use comment here ???
3508 when RE_Not_Available
=>
3510 end Make_Tag_Ctrl_Assignment
;
3512 ------------------------------------
3513 -- Possible_Bit_Aligned_Component --
3514 ------------------------------------
3516 function Possible_Bit_Aligned_Component
(N
: Node_Id
) return Boolean is
3520 -- Case of indexed component
3522 when N_Indexed_Component
=>
3524 P
: constant Node_Id
:= Prefix
(N
);
3525 Ptyp
: constant Entity_Id
:= Etype
(P
);
3528 -- If we know the component size and it is less than 64, then
3529 -- we are definitely OK. The back end always does assignment
3530 -- of misaligned small objects correctly.
3532 if Known_Static_Component_Size
(Ptyp
)
3533 and then Component_Size
(Ptyp
) <= 64
3537 -- Otherwise, we need to test the prefix, to see if we are
3538 -- indexing from a possibly unaligned component.
3541 return Possible_Bit_Aligned_Component
(P
);
3545 -- Case of selected component
3547 when N_Selected_Component
=>
3549 P
: constant Node_Id
:= Prefix
(N
);
3550 Comp
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
3553 -- If there is no component clause, then we are in the clear
3554 -- since the back end will never misalign a large component
3555 -- unless it is forced to do so. In the clear means we need
3556 -- only the recursive test on the prefix.
3558 if Component_May_Be_Bit_Aligned
(Comp
) then
3561 return Possible_Bit_Aligned_Component
(P
);
3565 -- If we have neither a record nor array component, it means that
3566 -- we have fallen off the top testing prefixes recursively, and
3567 -- we now have a stand alone object, where we don't have a problem
3573 end Possible_Bit_Aligned_Component
;