1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
29 with Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Errout
; use Errout
;
32 with Exp_Aggr
; use Exp_Aggr
;
33 with Exp_Ch6
; use Exp_Ch6
;
34 with Exp_Ch7
; use Exp_Ch7
;
35 with Exp_Ch11
; use Exp_Ch11
;
36 with Exp_Dbug
; use Exp_Dbug
;
37 with Exp_Pakd
; use Exp_Pakd
;
38 with Exp_Tss
; use Exp_Tss
;
39 with Exp_Util
; use Exp_Util
;
40 with Namet
; use Namet
;
41 with Nlists
; use Nlists
;
42 with Nmake
; use Nmake
;
44 with Restrict
; use Restrict
;
45 with Rident
; use Rident
;
46 with Rtsfind
; use Rtsfind
;
47 with Sinfo
; use Sinfo
;
49 with Sem_Aux
; use Sem_Aux
;
50 with Sem_Ch3
; use Sem_Ch3
;
51 with Sem_Ch8
; use Sem_Ch8
;
52 with Sem_Ch13
; use Sem_Ch13
;
53 with Sem_Eval
; use Sem_Eval
;
54 with Sem_Res
; use Sem_Res
;
55 with Sem_Util
; use Sem_Util
;
56 with Snames
; use Snames
;
57 with Stand
; use Stand
;
58 with Stringt
; use Stringt
;
59 with Targparm
; use Targparm
;
60 with Tbuild
; use Tbuild
;
61 with Validsw
; use Validsw
;
63 package body Exp_Ch5
is
65 procedure Build_Formal_Container_Iteration
67 Container
: Entity_Id
;
70 Advance
: out Node_Id
;
71 New_Loop
: out Node_Id
);
72 -- Utility to create declarations and loop statement for both forms
73 -- of formal container iterators.
75 function Change_Of_Representation
(N
: Node_Id
) return Boolean;
76 -- Determine if the right hand side of assignment N is a type conversion
77 -- which requires a change of representation. Called only for the array
80 procedure Expand_Assign_Array
(N
: Node_Id
; Rhs
: Node_Id
);
81 -- N is an assignment which assigns an array value. This routine process
82 -- the various special cases and checks required for such assignments,
83 -- including change of representation. Rhs is normally simply the right
84 -- hand side of the assignment, except that if the right hand side is a
85 -- type conversion or a qualified expression, then the RHS is the actual
86 -- expression inside any such type conversions or qualifications.
88 function Expand_Assign_Array_Loop
95 Rev
: Boolean) return Node_Id
;
96 -- N is an assignment statement which assigns an array value. This routine
97 -- expands the assignment into a loop (or nested loops for the case of a
98 -- multi-dimensional array) to do the assignment component by component.
99 -- Larray and Rarray are the entities of the actual arrays on the left
100 -- hand and right hand sides. L_Type and R_Type are the types of these
101 -- arrays (which may not be the same, due to either sliding, or to a
102 -- change of representation case). Ndim is the number of dimensions and
103 -- the parameter Rev indicates if the loops run normally (Rev = False),
104 -- or reversed (Rev = True). The value returned is the constructed
105 -- loop statement. Auxiliary declarations are inserted before node N
106 -- using the standard Insert_Actions mechanism.
108 procedure Expand_Assign_Record
(N
: Node_Id
);
109 -- N is an assignment of a untagged record value. This routine handles
110 -- the case where the assignment must be made component by component,
111 -- either because the target is not byte aligned, or there is a change
112 -- of representation, or when we have a tagged type with a representation
113 -- clause (this last case is required because holes in the tagged type
114 -- might be filled with components from child types).
116 procedure Expand_Formal_Container_Loop
(N
: Node_Id
);
117 -- Use the primitives specified in an Iterable aspect to expand a loop
118 -- over a so-called formal container, primarily for SPARK usage.
120 procedure Expand_Formal_Container_Element_Loop
(N
: Node_Id
);
121 -- Same, for an iterator of the form " For E of C". In this case the
122 -- iterator provides the name of the element, and the cursor is generated
125 procedure Expand_Iterator_Loop
(N
: Node_Id
);
126 -- Expand loop over arrays and containers that uses the form "for X of C"
127 -- with an optional subtype mark, or "for Y in C".
129 procedure Expand_Iterator_Loop_Over_Array
(N
: Node_Id
);
130 -- Expand loop over arrays that uses the form "for X of C"
132 procedure Expand_Predicated_Loop
(N
: Node_Id
);
133 -- Expand for loop over predicated subtype
135 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
;
136 -- Generate the necessary code for controlled and tagged assignment, that
137 -- is to say, finalization of the target before, adjustment of the target
138 -- after and save and restore of the tag and finalization pointers which
139 -- are not 'part of the value' and must not be changed upon assignment. N
140 -- is the original Assignment node.
142 --------------------------------------
143 -- Build_Formal_Container_iteration --
144 --------------------------------------
146 procedure Build_Formal_Container_Iteration
148 Container
: Entity_Id
;
151 Advance
: out Node_Id
;
152 New_Loop
: out Node_Id
)
154 Loc
: constant Source_Ptr
:= Sloc
(N
);
155 Stats
: constant List_Id
:= Statements
(N
);
156 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
157 First_Op
: constant Entity_Id
:=
158 Get_Iterable_Type_Primitive
(Typ
, Name_First
);
159 Next_Op
: constant Entity_Id
:=
160 Get_Iterable_Type_Primitive
(Typ
, Name_Next
);
162 Has_Element_Op
: constant Entity_Id
:=
163 Get_Iterable_Type_Primitive
(Typ
, Name_Has_Element
);
165 -- Declaration for Cursor
168 Make_Object_Declaration
(Loc
,
169 Defining_Identifier
=> Cursor
,
170 Object_Definition
=> New_Occurrence_Of
(Etype
(First_Op
), Loc
),
172 Make_Function_Call
(Loc
,
173 Name
=> New_Occurrence_Of
(First_Op
, Loc
),
174 Parameter_Associations
=> New_List
(
175 New_Occurrence_Of
(Container
, Loc
))));
177 -- Statement that advances cursor in loop
180 Make_Assignment_Statement
(Loc
,
181 Name
=> New_Occurrence_Of
(Cursor
, Loc
),
183 Make_Function_Call
(Loc
,
184 Name
=> New_Occurrence_Of
(Next_Op
, Loc
),
185 Parameter_Associations
=> New_List
(
186 New_Occurrence_Of
(Container
, Loc
),
187 New_Occurrence_Of
(Cursor
, Loc
))));
189 -- Iterator is rewritten as a while_loop
192 Make_Loop_Statement
(Loc
,
194 Make_Iteration_Scheme
(Loc
,
196 Make_Function_Call
(Loc
,
197 Name
=> New_Occurrence_Of
(Has_Element_Op
, Loc
),
198 Parameter_Associations
=> New_List
(
199 New_Occurrence_Of
(Container
, Loc
),
200 New_Occurrence_Of
(Cursor
, Loc
)))),
203 end Build_Formal_Container_Iteration
;
205 ------------------------------
206 -- Change_Of_Representation --
207 ------------------------------
209 function Change_Of_Representation
(N
: Node_Id
) return Boolean is
210 Rhs
: constant Node_Id
:= Expression
(N
);
213 Nkind
(Rhs
) = N_Type_Conversion
215 not Same_Representation
(Etype
(Rhs
), Etype
(Expression
(Rhs
)));
216 end Change_Of_Representation
;
218 -------------------------
219 -- Expand_Assign_Array --
220 -------------------------
222 -- There are two issues here. First, do we let Gigi do a block move, or
223 -- do we expand out into a loop? Second, we need to set the two flags
224 -- Forwards_OK and Backwards_OK which show whether the block move (or
225 -- corresponding loops) can be legitimately done in a forwards (low to
226 -- high) or backwards (high to low) manner.
228 procedure Expand_Assign_Array
(N
: Node_Id
; Rhs
: Node_Id
) is
229 Loc
: constant Source_Ptr
:= Sloc
(N
);
231 Lhs
: constant Node_Id
:= Name
(N
);
233 Act_Lhs
: constant Node_Id
:= Get_Referenced_Object
(Lhs
);
234 Act_Rhs
: Node_Id
:= Get_Referenced_Object
(Rhs
);
236 L_Type
: constant Entity_Id
:=
237 Underlying_Type
(Get_Actual_Subtype
(Act_Lhs
));
238 R_Type
: Entity_Id
:=
239 Underlying_Type
(Get_Actual_Subtype
(Act_Rhs
));
241 L_Slice
: constant Boolean := Nkind
(Act_Lhs
) = N_Slice
;
242 R_Slice
: constant Boolean := Nkind
(Act_Rhs
) = N_Slice
;
244 Crep
: constant Boolean := Change_Of_Representation
(N
);
249 Ndim
: constant Pos
:= Number_Dimensions
(L_Type
);
251 Loop_Required
: Boolean := False;
252 -- This switch is set to True if the array move must be done using
253 -- an explicit front end generated loop.
255 procedure Apply_Dereference
(Arg
: Node_Id
);
256 -- If the argument is an access to an array, and the assignment is
257 -- converted into a procedure call, apply explicit dereference.
259 function Has_Address_Clause
(Exp
: Node_Id
) return Boolean;
260 -- Test if Exp is a reference to an array whose declaration has
261 -- an address clause, or it is a slice of such an array.
263 function Is_Formal_Array
(Exp
: Node_Id
) return Boolean;
264 -- Test if Exp is a reference to an array which is either a formal
265 -- parameter or a slice of a formal parameter. These are the cases
266 -- where hidden aliasing can occur.
268 function Is_Non_Local_Array
(Exp
: Node_Id
) return Boolean;
269 -- Determine if Exp is a reference to an array variable which is other
270 -- than an object defined in the current scope, or a slice of such
271 -- an object. Such objects can be aliased to parameters (unlike local
272 -- array references).
274 -----------------------
275 -- Apply_Dereference --
276 -----------------------
278 procedure Apply_Dereference
(Arg
: Node_Id
) is
279 Typ
: constant Entity_Id
:= Etype
(Arg
);
281 if Is_Access_Type
(Typ
) then
282 Rewrite
(Arg
, Make_Explicit_Dereference
(Loc
,
283 Prefix
=> Relocate_Node
(Arg
)));
284 Analyze_And_Resolve
(Arg
, Designated_Type
(Typ
));
286 end Apply_Dereference
;
288 ------------------------
289 -- Has_Address_Clause --
290 ------------------------
292 function Has_Address_Clause
(Exp
: Node_Id
) return Boolean is
295 (Is_Entity_Name
(Exp
) and then
296 Present
(Address_Clause
(Entity
(Exp
))))
298 (Nkind
(Exp
) = N_Slice
and then Has_Address_Clause
(Prefix
(Exp
)));
299 end Has_Address_Clause
;
301 ---------------------
302 -- Is_Formal_Array --
303 ---------------------
305 function Is_Formal_Array
(Exp
: Node_Id
) return Boolean is
308 (Is_Entity_Name
(Exp
) and then Is_Formal
(Entity
(Exp
)))
310 (Nkind
(Exp
) = N_Slice
and then Is_Formal_Array
(Prefix
(Exp
)));
313 ------------------------
314 -- Is_Non_Local_Array --
315 ------------------------
317 function Is_Non_Local_Array
(Exp
: Node_Id
) return Boolean is
319 return (Is_Entity_Name
(Exp
)
320 and then Scope
(Entity
(Exp
)) /= Current_Scope
)
321 or else (Nkind
(Exp
) = N_Slice
322 and then Is_Non_Local_Array
(Prefix
(Exp
)));
323 end Is_Non_Local_Array
;
325 -- Determine if Lhs, Rhs are formal arrays or nonlocal arrays
327 Lhs_Formal
: constant Boolean := Is_Formal_Array
(Act_Lhs
);
328 Rhs_Formal
: constant Boolean := Is_Formal_Array
(Act_Rhs
);
330 Lhs_Non_Local_Var
: constant Boolean := Is_Non_Local_Array
(Act_Lhs
);
331 Rhs_Non_Local_Var
: constant Boolean := Is_Non_Local_Array
(Act_Rhs
);
333 -- Start of processing for Expand_Assign_Array
336 -- Deal with length check. Note that the length check is done with
337 -- respect to the right hand side as given, not a possible underlying
338 -- renamed object, since this would generate incorrect extra checks.
340 Apply_Length_Check
(Rhs
, L_Type
);
342 -- We start by assuming that the move can be done in either direction,
343 -- i.e. that the two sides are completely disjoint.
345 Set_Forwards_OK
(N
, True);
346 Set_Backwards_OK
(N
, True);
348 -- Normally it is only the slice case that can lead to overlap, and
349 -- explicit checks for slices are made below. But there is one case
350 -- where the slice can be implicit and invisible to us: when we have a
351 -- one dimensional array, and either both operands are parameters, or
352 -- one is a parameter (which can be a slice passed by reference) and the
353 -- other is a non-local variable. In this case the parameter could be a
354 -- slice that overlaps with the other operand.
356 -- However, if the array subtype is a constrained first subtype in the
357 -- parameter case, then we don't have to worry about overlap, since
358 -- slice assignments aren't possible (other than for a slice denoting
361 -- Note: No overlap is possible if there is a change of representation,
362 -- so we can exclude this case.
367 ((Lhs_Formal
and Rhs_Formal
)
369 (Lhs_Formal
and Rhs_Non_Local_Var
)
371 (Rhs_Formal
and Lhs_Non_Local_Var
))
373 (not Is_Constrained
(Etype
(Lhs
))
374 or else not Is_First_Subtype
(Etype
(Lhs
)))
376 -- In the case of compiling for the Java or .NET Virtual Machine,
377 -- slices are always passed by making a copy, so we don't have to
378 -- worry about overlap. We also want to prevent generation of "<"
379 -- comparisons for array addresses, since that's a meaningless
380 -- operation on the VM.
382 and then VM_Target
= No_VM
384 Set_Forwards_OK
(N
, False);
385 Set_Backwards_OK
(N
, False);
387 -- Note: the bit-packed case is not worrisome here, since if we have
388 -- a slice passed as a parameter, it is always aligned on a byte
389 -- boundary, and if there are no explicit slices, the assignment
390 -- can be performed directly.
393 -- If either operand has an address clause clear Backwards_OK and
394 -- Forwards_OK, since we cannot tell if the operands overlap. We
395 -- exclude this treatment when Rhs is an aggregate, since we know
396 -- that overlap can't occur.
398 if (Has_Address_Clause
(Lhs
) and then Nkind
(Rhs
) /= N_Aggregate
)
399 or else Has_Address_Clause
(Rhs
)
401 Set_Forwards_OK
(N
, False);
402 Set_Backwards_OK
(N
, False);
405 -- We certainly must use a loop for change of representation and also
406 -- we use the operand of the conversion on the right hand side as the
407 -- effective right hand side (the component types must match in this
411 Act_Rhs
:= Get_Referenced_Object
(Rhs
);
412 R_Type
:= Get_Actual_Subtype
(Act_Rhs
);
413 Loop_Required
:= True;
415 -- We require a loop if the left side is possibly bit unaligned
417 elsif Possible_Bit_Aligned_Component
(Lhs
)
419 Possible_Bit_Aligned_Component
(Rhs
)
421 Loop_Required
:= True;
423 -- Arrays with controlled components are expanded into a loop to force
424 -- calls to Adjust at the component level.
426 elsif Has_Controlled_Component
(L_Type
) then
427 Loop_Required
:= True;
429 -- If object is atomic, we cannot tolerate a loop
431 elsif Is_Atomic_Object
(Act_Lhs
)
433 Is_Atomic_Object
(Act_Rhs
)
437 -- Loop is required if we have atomic components since we have to
438 -- be sure to do any accesses on an element by element basis.
440 elsif Has_Atomic_Components
(L_Type
)
441 or else Has_Atomic_Components
(R_Type
)
442 or else Is_Atomic
(Component_Type
(L_Type
))
443 or else Is_Atomic
(Component_Type
(R_Type
))
445 Loop_Required
:= True;
447 -- Case where no slice is involved
449 elsif not L_Slice
and not R_Slice
then
451 -- The following code deals with the case of unconstrained bit packed
452 -- arrays. The problem is that the template for such arrays contains
453 -- the bounds of the actual source level array, but the copy of an
454 -- entire array requires the bounds of the underlying array. It would
455 -- be nice if the back end could take care of this, but right now it
456 -- does not know how, so if we have such a type, then we expand out
457 -- into a loop, which is inefficient but works correctly. If we don't
458 -- do this, we get the wrong length computed for the array to be
459 -- moved. The two cases we need to worry about are:
461 -- Explicit dereference of an unconstrained packed array type as in
462 -- the following example:
465 -- type BITS is array(INTEGER range <>) of BOOLEAN;
466 -- pragma PACK(BITS);
467 -- type A is access BITS;
470 -- P1 := new BITS (1 .. 65_535);
471 -- P2 := new BITS (1 .. 65_535);
475 -- A formal parameter reference with an unconstrained bit array type
476 -- is the other case we need to worry about (here we assume the same
477 -- BITS type declared above):
479 -- procedure Write_All (File : out BITS; Contents : BITS);
481 -- File.Storage := Contents;
484 -- We expand to a loop in either of these two cases
486 -- Question for future thought. Another potentially more efficient
487 -- approach would be to create the actual subtype, and then do an
488 -- unchecked conversion to this actual subtype ???
490 Check_Unconstrained_Bit_Packed_Array
: declare
492 function Is_UBPA_Reference
(Opnd
: Node_Id
) return Boolean;
493 -- Function to perform required test for the first case, above
494 -- (dereference of an unconstrained bit packed array).
496 -----------------------
497 -- Is_UBPA_Reference --
498 -----------------------
500 function Is_UBPA_Reference
(Opnd
: Node_Id
) return Boolean is
501 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Opnd
));
503 Des_Type
: Entity_Id
;
506 if Present
(Packed_Array_Impl_Type
(Typ
))
507 and then Is_Array_Type
(Packed_Array_Impl_Type
(Typ
))
508 and then not Is_Constrained
(Packed_Array_Impl_Type
(Typ
))
512 elsif Nkind
(Opnd
) = N_Explicit_Dereference
then
513 P_Type
:= Underlying_Type
(Etype
(Prefix
(Opnd
)));
515 if not Is_Access_Type
(P_Type
) then
519 Des_Type
:= Designated_Type
(P_Type
);
521 Is_Bit_Packed_Array
(Des_Type
)
522 and then not Is_Constrained
(Des_Type
);
528 end Is_UBPA_Reference
;
530 -- Start of processing for Check_Unconstrained_Bit_Packed_Array
533 if Is_UBPA_Reference
(Lhs
)
535 Is_UBPA_Reference
(Rhs
)
537 Loop_Required
:= True;
539 -- Here if we do not have the case of a reference to a bit packed
540 -- unconstrained array case. In this case gigi can most certainly
541 -- handle the assignment if a forwards move is allowed.
543 -- (could it handle the backwards case also???)
545 elsif Forwards_OK
(N
) then
548 end Check_Unconstrained_Bit_Packed_Array
;
550 -- The back end can always handle the assignment if the right side is a
551 -- string literal (note that overlap is definitely impossible in this
552 -- case). If the type is packed, a string literal is always converted
553 -- into an aggregate, except in the case of a null slice, for which no
554 -- aggregate can be written. In that case, rewrite the assignment as a
555 -- null statement, a length check has already been emitted to verify
556 -- that the range of the left-hand side is empty.
558 -- Note that this code is not executed if we have an assignment of a
559 -- string literal to a non-bit aligned component of a record, a case
560 -- which cannot be handled by the backend.
562 elsif Nkind
(Rhs
) = N_String_Literal
then
563 if String_Length
(Strval
(Rhs
)) = 0
564 and then Is_Bit_Packed_Array
(L_Type
)
566 Rewrite
(N
, Make_Null_Statement
(Loc
));
572 -- If either operand is bit packed, then we need a loop, since we can't
573 -- be sure that the slice is byte aligned. Similarly, if either operand
574 -- is a possibly unaligned slice, then we need a loop (since the back
575 -- end cannot handle unaligned slices).
577 elsif Is_Bit_Packed_Array
(L_Type
)
578 or else Is_Bit_Packed_Array
(R_Type
)
579 or else Is_Possibly_Unaligned_Slice
(Lhs
)
580 or else Is_Possibly_Unaligned_Slice
(Rhs
)
582 Loop_Required
:= True;
584 -- If we are not bit-packed, and we have only one slice, then no overlap
585 -- is possible except in the parameter case, so we can let the back end
588 elsif not (L_Slice
and R_Slice
) then
589 if Forwards_OK
(N
) then
594 -- If the right-hand side is a string literal, introduce a temporary for
595 -- it, for use in the generated loop that will follow.
597 if Nkind
(Rhs
) = N_String_Literal
then
599 Temp
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', Rhs
);
604 Make_Object_Declaration
(Loc
,
605 Defining_Identifier
=> Temp
,
606 Object_Definition
=> New_Occurrence_Of
(L_Type
, Loc
),
607 Expression
=> Relocate_Node
(Rhs
));
609 Insert_Action
(N
, Decl
);
610 Rewrite
(Rhs
, New_Occurrence_Of
(Temp
, Loc
));
611 R_Type
:= Etype
(Temp
);
615 -- Come here to complete the analysis
617 -- Loop_Required: Set to True if we know that a loop is required
618 -- regardless of overlap considerations.
620 -- Forwards_OK: Set to False if we already know that a forwards
621 -- move is not safe, else set to True.
623 -- Backwards_OK: Set to False if we already know that a backwards
624 -- move is not safe, else set to True
626 -- Our task at this stage is to complete the overlap analysis, which can
627 -- result in possibly setting Forwards_OK or Backwards_OK to False, and
628 -- then generating the final code, either by deciding that it is OK
629 -- after all to let Gigi handle it, or by generating appropriate code
633 L_Index_Typ
: constant Node_Id
:= Etype
(First_Index
(L_Type
));
634 R_Index_Typ
: constant Node_Id
:= Etype
(First_Index
(R_Type
));
636 Left_Lo
: constant Node_Id
:= Type_Low_Bound
(L_Index_Typ
);
637 Left_Hi
: constant Node_Id
:= Type_High_Bound
(L_Index_Typ
);
638 Right_Lo
: constant Node_Id
:= Type_Low_Bound
(R_Index_Typ
);
639 Right_Hi
: constant Node_Id
:= Type_High_Bound
(R_Index_Typ
);
641 Act_L_Array
: Node_Id
;
642 Act_R_Array
: Node_Id
;
648 Cresult
: Compare_Result
;
651 -- Get the expressions for the arrays. If we are dealing with a
652 -- private type, then convert to the underlying type. We can do
653 -- direct assignments to an array that is a private type, but we
654 -- cannot assign to elements of the array without this extra
655 -- unchecked conversion.
657 -- Note: We propagate Parent to the conversion nodes to generate
658 -- a well-formed subtree.
660 if Nkind
(Act_Lhs
) = N_Slice
then
661 Larray
:= Prefix
(Act_Lhs
);
665 if Is_Private_Type
(Etype
(Larray
)) then
667 Par
: constant Node_Id
:= Parent
(Larray
);
671 (Underlying_Type
(Etype
(Larray
)), Larray
);
672 Set_Parent
(Larray
, Par
);
677 if Nkind
(Act_Rhs
) = N_Slice
then
678 Rarray
:= Prefix
(Act_Rhs
);
682 if Is_Private_Type
(Etype
(Rarray
)) then
684 Par
: constant Node_Id
:= Parent
(Rarray
);
688 (Underlying_Type
(Etype
(Rarray
)), Rarray
);
689 Set_Parent
(Rarray
, Par
);
694 -- If both sides are slices, we must figure out whether it is safe
695 -- to do the move in one direction or the other. It is always safe
696 -- if there is a change of representation since obviously two arrays
697 -- with different representations cannot possibly overlap.
699 if (not Crep
) and L_Slice
and R_Slice
then
700 Act_L_Array
:= Get_Referenced_Object
(Prefix
(Act_Lhs
));
701 Act_R_Array
:= Get_Referenced_Object
(Prefix
(Act_Rhs
));
703 -- If both left and right hand arrays are entity names, and refer
704 -- to different entities, then we know that the move is safe (the
705 -- two storage areas are completely disjoint).
707 if Is_Entity_Name
(Act_L_Array
)
708 and then Is_Entity_Name
(Act_R_Array
)
709 and then Entity
(Act_L_Array
) /= Entity
(Act_R_Array
)
713 -- Otherwise, we assume the worst, which is that the two arrays
714 -- are the same array. There is no need to check if we know that
715 -- is the case, because if we don't know it, we still have to
718 -- Generally if the same array is involved, then we have an
719 -- overlapping case. We will have to really assume the worst (i.e.
720 -- set neither of the OK flags) unless we can determine the lower
721 -- or upper bounds at compile time and compare them.
726 (Left_Lo
, Right_Lo
, Assume_Valid
=> True);
728 if Cresult
= Unknown
then
731 (Left_Hi
, Right_Hi
, Assume_Valid
=> True);
735 when LT | LE | EQ
=> Set_Backwards_OK
(N
, False);
736 when GT | GE
=> Set_Forwards_OK
(N
, False);
737 when NE | Unknown
=> Set_Backwards_OK
(N
, False);
738 Set_Forwards_OK
(N
, False);
743 -- If after that analysis Loop_Required is False, meaning that we
744 -- have not discovered some non-overlap reason for requiring a loop,
745 -- then the outcome depends on the capabilities of the back end.
747 if not Loop_Required
then
749 -- The GCC back end can deal with all cases of overlap by falling
750 -- back to memmove if it cannot use a more efficient approach.
752 if VM_Target
= No_VM
and not AAMP_On_Target
then
755 -- Assume other back ends can handle it if Forwards_OK is set
757 elsif Forwards_OK
(N
) then
760 -- If Forwards_OK is not set, the back end will need something
761 -- like memmove to handle the move. For now, this processing is
762 -- activated using the .s debug flag (-gnatd.s).
764 elsif Debug_Flag_Dot_S
then
769 -- At this stage we have to generate an explicit loop, and we have
770 -- the following cases:
772 -- Forwards_OK = True
774 -- Rnn : right_index := right_index'First;
775 -- for Lnn in left-index loop
776 -- left (Lnn) := right (Rnn);
777 -- Rnn := right_index'Succ (Rnn);
780 -- Note: the above code MUST be analyzed with checks off, because
781 -- otherwise the Succ could overflow. But in any case this is more
784 -- Forwards_OK = False, Backwards_OK = True
786 -- Rnn : right_index := right_index'Last;
787 -- for Lnn in reverse left-index loop
788 -- left (Lnn) := right (Rnn);
789 -- Rnn := right_index'Pred (Rnn);
792 -- Note: the above code MUST be analyzed with checks off, because
793 -- otherwise the Pred could overflow. But in any case this is more
796 -- Forwards_OK = Backwards_OK = False
798 -- This only happens if we have the same array on each side. It is
799 -- possible to create situations using overlays that violate this,
800 -- but we simply do not promise to get this "right" in this case.
802 -- There are two possible subcases. If the No_Implicit_Conditionals
803 -- restriction is set, then we generate the following code:
806 -- T : constant <operand-type> := rhs;
811 -- If implicit conditionals are permitted, then we generate:
813 -- if Left_Lo <= Right_Lo then
814 -- <code for Forwards_OK = True above>
816 -- <code for Backwards_OK = True above>
819 -- In order to detect possible aliasing, we examine the renamed
820 -- expression when the source or target is a renaming. However,
821 -- the renaming may be intended to capture an address that may be
822 -- affected by subsequent code, and therefore we must recover
823 -- the actual entity for the expansion that follows, not the
824 -- object it renames. In particular, if source or target designate
825 -- a portion of a dynamically allocated object, the pointer to it
826 -- may be reassigned but the renaming preserves the proper location.
828 if Is_Entity_Name
(Rhs
)
830 Nkind
(Parent
(Entity
(Rhs
))) = N_Object_Renaming_Declaration
831 and then Nkind
(Act_Rhs
) = N_Slice
836 if Is_Entity_Name
(Lhs
)
838 Nkind
(Parent
(Entity
(Lhs
))) = N_Object_Renaming_Declaration
839 and then Nkind
(Act_Lhs
) = N_Slice
844 -- Cases where either Forwards_OK or Backwards_OK is true
846 if Forwards_OK
(N
) or else Backwards_OK
(N
) then
847 if Needs_Finalization
(Component_Type
(L_Type
))
848 and then Base_Type
(L_Type
) = Base_Type
(R_Type
)
850 and then not No_Ctrl_Actions
(N
)
853 Proc
: constant Entity_Id
:=
854 TSS
(Base_Type
(L_Type
), TSS_Slice_Assign
);
858 Apply_Dereference
(Larray
);
859 Apply_Dereference
(Rarray
);
860 Actuals
:= New_List
(
861 Duplicate_Subexpr
(Larray
, Name_Req
=> True),
862 Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
863 Duplicate_Subexpr
(Left_Lo
, Name_Req
=> True),
864 Duplicate_Subexpr
(Left_Hi
, Name_Req
=> True),
865 Duplicate_Subexpr
(Right_Lo
, Name_Req
=> True),
866 Duplicate_Subexpr
(Right_Hi
, Name_Req
=> True));
870 Boolean_Literals
(not Forwards_OK
(N
)), Loc
));
873 Make_Procedure_Call_Statement
(Loc
,
874 Name
=> New_Occurrence_Of
(Proc
, Loc
),
875 Parameter_Associations
=> Actuals
));
880 Expand_Assign_Array_Loop
881 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
882 Rev
=> not Forwards_OK
(N
)));
885 -- Case of both are false with No_Implicit_Conditionals
887 elsif Restriction_Active
(No_Implicit_Conditionals
) then
889 T
: constant Entity_Id
:=
890 Make_Defining_Identifier
(Loc
, Chars
=> Name_T
);
894 Make_Block_Statement
(Loc
,
895 Declarations
=> New_List
(
896 Make_Object_Declaration
(Loc
,
897 Defining_Identifier
=> T
,
898 Constant_Present
=> True,
900 New_Occurrence_Of
(Etype
(Rhs
), Loc
),
901 Expression
=> Relocate_Node
(Rhs
))),
903 Handled_Statement_Sequence
=>
904 Make_Handled_Sequence_Of_Statements
(Loc
,
905 Statements
=> New_List
(
906 Make_Assignment_Statement
(Loc
,
907 Name
=> Relocate_Node
(Lhs
),
908 Expression
=> New_Occurrence_Of
(T
, Loc
))))));
911 -- Case of both are false with implicit conditionals allowed
914 -- Before we generate this code, we must ensure that the left and
915 -- right side array types are defined. They may be itypes, and we
916 -- cannot let them be defined inside the if, since the first use
917 -- in the then may not be executed.
919 Ensure_Defined
(L_Type
, N
);
920 Ensure_Defined
(R_Type
, N
);
922 -- We normally compare addresses to find out which way round to
923 -- do the loop, since this is reliable, and handles the cases of
924 -- parameters, conversions etc. But we can't do that in the bit
925 -- packed case or the VM case, because addresses don't work there.
927 if not Is_Bit_Packed_Array
(L_Type
) and then VM_Target
= No_VM
then
931 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
932 Make_Attribute_Reference
(Loc
,
934 Make_Indexed_Component
(Loc
,
936 Duplicate_Subexpr_Move_Checks
(Larray
, True),
937 Expressions
=> New_List
(
938 Make_Attribute_Reference
(Loc
,
942 Attribute_Name
=> Name_First
))),
943 Attribute_Name
=> Name_Address
)),
946 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
947 Make_Attribute_Reference
(Loc
,
949 Make_Indexed_Component
(Loc
,
951 Duplicate_Subexpr_Move_Checks
(Rarray
, True),
952 Expressions
=> New_List
(
953 Make_Attribute_Reference
(Loc
,
957 Attribute_Name
=> Name_First
))),
958 Attribute_Name
=> Name_Address
)));
960 -- For the bit packed and VM cases we use the bounds. That's OK,
961 -- because we don't have to worry about parameters, since they
962 -- cannot cause overlap. Perhaps we should worry about weird slice
968 Cleft_Lo
:= New_Copy_Tree
(Left_Lo
);
969 Cright_Lo
:= New_Copy_Tree
(Right_Lo
);
971 -- If the types do not match we add an implicit conversion
972 -- here to ensure proper match
974 if Etype
(Left_Lo
) /= Etype
(Right_Lo
) then
976 Unchecked_Convert_To
(Etype
(Left_Lo
), Cright_Lo
);
979 -- Reset the Analyzed flag, because the bounds of the index
980 -- type itself may be universal, and must must be reanalyzed
981 -- to acquire the proper type for the back end.
983 Set_Analyzed
(Cleft_Lo
, False);
984 Set_Analyzed
(Cright_Lo
, False);
988 Left_Opnd
=> Cleft_Lo
,
989 Right_Opnd
=> Cright_Lo
);
992 if Needs_Finalization
(Component_Type
(L_Type
))
993 and then Base_Type
(L_Type
) = Base_Type
(R_Type
)
995 and then not No_Ctrl_Actions
(N
)
998 -- Call TSS procedure for array assignment, passing the
999 -- explicit bounds of right and left hand sides.
1002 Proc
: constant Entity_Id
:=
1003 TSS
(Base_Type
(L_Type
), TSS_Slice_Assign
);
1007 Apply_Dereference
(Larray
);
1008 Apply_Dereference
(Rarray
);
1009 Actuals
:= New_List
(
1010 Duplicate_Subexpr
(Larray
, Name_Req
=> True),
1011 Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
1012 Duplicate_Subexpr
(Left_Lo
, Name_Req
=> True),
1013 Duplicate_Subexpr
(Left_Hi
, Name_Req
=> True),
1014 Duplicate_Subexpr
(Right_Lo
, Name_Req
=> True),
1015 Duplicate_Subexpr
(Right_Hi
, Name_Req
=> True));
1019 Right_Opnd
=> Condition
));
1022 Make_Procedure_Call_Statement
(Loc
,
1023 Name
=> New_Occurrence_Of
(Proc
, Loc
),
1024 Parameter_Associations
=> Actuals
));
1029 Make_Implicit_If_Statement
(N
,
1030 Condition
=> Condition
,
1032 Then_Statements
=> New_List
(
1033 Expand_Assign_Array_Loop
1034 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
1037 Else_Statements
=> New_List
(
1038 Expand_Assign_Array_Loop
1039 (N
, Larray
, Rarray
, L_Type
, R_Type
, Ndim
,
1044 Analyze
(N
, Suppress
=> All_Checks
);
1048 when RE_Not_Available
=>
1050 end Expand_Assign_Array
;
1052 ------------------------------
1053 -- Expand_Assign_Array_Loop --
1054 ------------------------------
1056 -- The following is an example of the loop generated for the case of a
1057 -- two-dimensional array:
1060 -- R2b : Tm1X1 := 1;
1062 -- for L1b in 1 .. 100 loop
1064 -- R4b : Tm1X2 := 1;
1066 -- for L3b in 1 .. 100 loop
1067 -- vm1 (L1b, L3b) := vm2 (R2b, R4b);
1068 -- R4b := Tm1X2'succ(R4b);
1071 -- R2b := Tm1X1'succ(R2b);
1075 -- Here Rev is False, and Tm1Xn are the subscript types for the right hand
1076 -- side. The declarations of R2b and R4b are inserted before the original
1077 -- assignment statement.
1079 function Expand_Assign_Array_Loop
1086 Rev
: Boolean) return Node_Id
1088 Loc
: constant Source_Ptr
:= Sloc
(N
);
1090 Lnn
: array (1 .. Ndim
) of Entity_Id
;
1091 Rnn
: array (1 .. Ndim
) of Entity_Id
;
1092 -- Entities used as subscripts on left and right sides
1094 L_Index_Type
: array (1 .. Ndim
) of Entity_Id
;
1095 R_Index_Type
: array (1 .. Ndim
) of Entity_Id
;
1096 -- Left and right index types
1103 function Build_Step
(J
: Nat
) return Node_Id
;
1104 -- The increment step for the index of the right-hand side is written
1105 -- as an attribute reference (Succ or Pred). This function returns
1106 -- the corresponding node, which is placed at the end of the loop body.
1112 function Build_Step
(J
: Nat
) return Node_Id
is
1124 Make_Assignment_Statement
(Loc
,
1125 Name
=> New_Occurrence_Of
(Rnn
(J
), Loc
),
1127 Make_Attribute_Reference
(Loc
,
1129 New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1130 Attribute_Name
=> S_Or_P
,
1131 Expressions
=> New_List
(
1132 New_Occurrence_Of
(Rnn
(J
), Loc
))));
1134 -- Note that on the last iteration of the loop, the index is increased
1135 -- (or decreased) past the corresponding bound. This is consistent with
1136 -- the C semantics of the back-end, where such an off-by-one value on a
1137 -- dead index variable is OK. However, in CodePeer mode this leads to
1138 -- spurious warnings, and thus we place a guard around the attribute
1139 -- reference. For obvious reasons we only do this for CodePeer.
1141 if CodePeer_Mode
then
1143 Make_If_Statement
(Loc
,
1146 Left_Opnd
=> New_Occurrence_Of
(Lnn
(J
), Loc
),
1148 Make_Attribute_Reference
(Loc
,
1149 Prefix
=> New_Occurrence_Of
(L_Index_Type
(J
), Loc
),
1150 Attribute_Name
=> Lim
)),
1151 Then_Statements
=> New_List
(Step
));
1157 -- Start of processing for Expand_Assign_Array_Loop
1161 F_Or_L
:= Name_Last
;
1162 S_Or_P
:= Name_Pred
;
1164 F_Or_L
:= Name_First
;
1165 S_Or_P
:= Name_Succ
;
1168 -- Setup index types and subscript entities
1175 L_Index
:= First_Index
(L_Type
);
1176 R_Index
:= First_Index
(R_Type
);
1178 for J
in 1 .. Ndim
loop
1179 Lnn
(J
) := Make_Temporary
(Loc
, 'L');
1180 Rnn
(J
) := Make_Temporary
(Loc
, 'R');
1182 L_Index_Type
(J
) := Etype
(L_Index
);
1183 R_Index_Type
(J
) := Etype
(R_Index
);
1185 Next_Index
(L_Index
);
1186 Next_Index
(R_Index
);
1190 -- Now construct the assignment statement
1193 ExprL
: constant List_Id
:= New_List
;
1194 ExprR
: constant List_Id
:= New_List
;
1197 for J
in 1 .. Ndim
loop
1198 Append_To
(ExprL
, New_Occurrence_Of
(Lnn
(J
), Loc
));
1199 Append_To
(ExprR
, New_Occurrence_Of
(Rnn
(J
), Loc
));
1203 Make_Assignment_Statement
(Loc
,
1205 Make_Indexed_Component
(Loc
,
1206 Prefix
=> Duplicate_Subexpr
(Larray
, Name_Req
=> True),
1207 Expressions
=> ExprL
),
1209 Make_Indexed_Component
(Loc
,
1210 Prefix
=> Duplicate_Subexpr
(Rarray
, Name_Req
=> True),
1211 Expressions
=> ExprR
));
1213 -- We set assignment OK, since there are some cases, e.g. in object
1214 -- declarations, where we are actually assigning into a constant.
1215 -- If there really is an illegality, it was caught long before now,
1216 -- and was flagged when the original assignment was analyzed.
1218 Set_Assignment_OK
(Name
(Assign
));
1220 -- Propagate the No_Ctrl_Actions flag to individual assignments
1222 Set_No_Ctrl_Actions
(Assign
, No_Ctrl_Actions
(N
));
1225 -- Now construct the loop from the inside out, with the last subscript
1226 -- varying most rapidly. Note that Assign is first the raw assignment
1227 -- statement, and then subsequently the loop that wraps it up.
1229 for J
in reverse 1 .. Ndim
loop
1231 Make_Block_Statement
(Loc
,
1232 Declarations
=> New_List
(
1233 Make_Object_Declaration
(Loc
,
1234 Defining_Identifier
=> Rnn
(J
),
1235 Object_Definition
=>
1236 New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1238 Make_Attribute_Reference
(Loc
,
1239 Prefix
=> New_Occurrence_Of
(R_Index_Type
(J
), Loc
),
1240 Attribute_Name
=> F_Or_L
))),
1242 Handled_Statement_Sequence
=>
1243 Make_Handled_Sequence_Of_Statements
(Loc
,
1244 Statements
=> New_List
(
1245 Make_Implicit_Loop_Statement
(N
,
1247 Make_Iteration_Scheme
(Loc
,
1248 Loop_Parameter_Specification
=>
1249 Make_Loop_Parameter_Specification
(Loc
,
1250 Defining_Identifier
=> Lnn
(J
),
1251 Reverse_Present
=> Rev
,
1252 Discrete_Subtype_Definition
=>
1253 New_Occurrence_Of
(L_Index_Type
(J
), Loc
))),
1255 Statements
=> New_List
(Assign
, Build_Step
(J
))))));
1259 end Expand_Assign_Array_Loop
;
1261 --------------------------
1262 -- Expand_Assign_Record --
1263 --------------------------
1265 procedure Expand_Assign_Record
(N
: Node_Id
) is
1266 Lhs
: constant Node_Id
:= Name
(N
);
1267 Rhs
: Node_Id
:= Expression
(N
);
1268 L_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Lhs
));
1271 -- If change of representation, then extract the real right hand side
1272 -- from the type conversion, and proceed with component-wise assignment,
1273 -- since the two types are not the same as far as the back end is
1276 if Change_Of_Representation
(N
) then
1277 Rhs
:= Expression
(Rhs
);
1279 -- If this may be a case of a large bit aligned component, then proceed
1280 -- with component-wise assignment, to avoid possible clobbering of other
1281 -- components sharing bits in the first or last byte of the component to
1284 elsif Possible_Bit_Aligned_Component
(Lhs
)
1286 Possible_Bit_Aligned_Component
(Rhs
)
1290 -- If we have a tagged type that has a complete record representation
1291 -- clause, we must do we must do component-wise assignments, since child
1292 -- types may have used gaps for their components, and we might be
1293 -- dealing with a view conversion.
1295 elsif Is_Fully_Repped_Tagged_Type
(L_Typ
) then
1298 -- If neither condition met, then nothing special to do, the back end
1299 -- can handle assignment of the entire component as a single entity.
1305 -- At this stage we know that we must do a component wise assignment
1308 Loc
: constant Source_Ptr
:= Sloc
(N
);
1309 R_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Rhs
));
1310 Decl
: constant Node_Id
:= Declaration_Node
(R_Typ
);
1314 function Find_Component
1316 Comp
: Entity_Id
) return Entity_Id
;
1317 -- Find the component with the given name in the underlying record
1318 -- declaration for Typ. We need to use the actual entity because the
1319 -- type may be private and resolution by identifier alone would fail.
1321 function Make_Component_List_Assign
1323 U_U
: Boolean := False) return List_Id
;
1324 -- Returns a sequence of statements to assign the components that
1325 -- are referenced in the given component list. The flag U_U is
1326 -- used to force the usage of the inferred value of the variant
1327 -- part expression as the switch for the generated case statement.
1329 function Make_Field_Assign
1331 U_U
: Boolean := False) return Node_Id
;
1332 -- Given C, the entity for a discriminant or component, build an
1333 -- assignment for the corresponding field values. The flag U_U
1334 -- signals the presence of an Unchecked_Union and forces the usage
1335 -- of the inferred discriminant value of C as the right hand side
1336 -- of the assignment.
1338 function Make_Field_Assigns
(CI
: List_Id
) return List_Id
;
1339 -- Given CI, a component items list, construct series of statements
1340 -- for fieldwise assignment of the corresponding components.
1342 --------------------
1343 -- Find_Component --
1344 --------------------
1346 function Find_Component
1348 Comp
: Entity_Id
) return Entity_Id
1350 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
1354 C
:= First_Entity
(Utyp
);
1355 while Present
(C
) loop
1356 if Chars
(C
) = Chars
(Comp
) then
1363 raise Program_Error
;
1366 --------------------------------
1367 -- Make_Component_List_Assign --
1368 --------------------------------
1370 function Make_Component_List_Assign
1372 U_U
: Boolean := False) return List_Id
1374 CI
: constant List_Id
:= Component_Items
(CL
);
1375 VP
: constant Node_Id
:= Variant_Part
(CL
);
1385 Result
:= Make_Field_Assigns
(CI
);
1387 if Present
(VP
) then
1388 V
:= First_Non_Pragma
(Variants
(VP
));
1390 while Present
(V
) loop
1392 DC
:= First
(Discrete_Choices
(V
));
1393 while Present
(DC
) loop
1394 Append_To
(DCH
, New_Copy_Tree
(DC
));
1399 Make_Case_Statement_Alternative
(Loc
,
1400 Discrete_Choices
=> DCH
,
1402 Make_Component_List_Assign
(Component_List
(V
))));
1403 Next_Non_Pragma
(V
);
1406 -- If we have an Unchecked_Union, use the value of the inferred
1407 -- discriminant of the variant part expression as the switch
1408 -- for the case statement. The case statement may later be
1413 New_Copy
(Get_Discriminant_Value
(
1416 Discriminant_Constraint
(Etype
(Rhs
))));
1419 Make_Selected_Component
(Loc
,
1420 Prefix
=> Duplicate_Subexpr
(Rhs
),
1422 Make_Identifier
(Loc
, Chars
(Name
(VP
))));
1426 Make_Case_Statement
(Loc
,
1428 Alternatives
=> Alts
));
1432 end Make_Component_List_Assign
;
1434 -----------------------
1435 -- Make_Field_Assign --
1436 -----------------------
1438 function Make_Field_Assign
1440 U_U
: Boolean := False) return Node_Id
1446 -- In the case of an Unchecked_Union, use the discriminant
1447 -- constraint value as on the right hand side of the assignment.
1451 New_Copy
(Get_Discriminant_Value
(C
,
1453 Discriminant_Constraint
(Etype
(Rhs
))));
1456 Make_Selected_Component
(Loc
,
1457 Prefix
=> Duplicate_Subexpr
(Rhs
),
1458 Selector_Name
=> New_Occurrence_Of
(C
, Loc
));
1462 Make_Assignment_Statement
(Loc
,
1464 Make_Selected_Component
(Loc
,
1465 Prefix
=> Duplicate_Subexpr
(Lhs
),
1467 New_Occurrence_Of
(Find_Component
(L_Typ
, C
), Loc
)),
1468 Expression
=> Expr
);
1470 -- Set Assignment_OK, so discriminants can be assigned
1472 Set_Assignment_OK
(Name
(A
), True);
1474 if Componentwise_Assignment
(N
)
1475 and then Nkind
(Name
(A
)) = N_Selected_Component
1476 and then Chars
(Selector_Name
(Name
(A
))) = Name_uParent
1478 Set_Componentwise_Assignment
(A
);
1482 end Make_Field_Assign
;
1484 ------------------------
1485 -- Make_Field_Assigns --
1486 ------------------------
1488 function Make_Field_Assigns
(CI
: List_Id
) return List_Id
is
1496 while Present
(Item
) loop
1498 -- Look for components, but exclude _tag field assignment if
1499 -- the special Componentwise_Assignment flag is set.
1501 if Nkind
(Item
) = N_Component_Declaration
1502 and then not (Is_Tag
(Defining_Identifier
(Item
))
1503 and then Componentwise_Assignment
(N
))
1506 (Result
, Make_Field_Assign
(Defining_Identifier
(Item
)));
1513 end Make_Field_Assigns
;
1515 -- Start of processing for Expand_Assign_Record
1518 -- Note that we use the base types for this processing. This results
1519 -- in some extra work in the constrained case, but the change of
1520 -- representation case is so unusual that it is not worth the effort.
1522 -- First copy the discriminants. This is done unconditionally. It
1523 -- is required in the unconstrained left side case, and also in the
1524 -- case where this assignment was constructed during the expansion
1525 -- of a type conversion (since initialization of discriminants is
1526 -- suppressed in this case). It is unnecessary but harmless in
1529 if Has_Discriminants
(L_Typ
) then
1530 F
:= First_Discriminant
(R_Typ
);
1531 while Present
(F
) loop
1533 -- If we are expanding the initialization of a derived record
1534 -- that constrains or renames discriminants of the parent, we
1535 -- must use the corresponding discriminant in the parent.
1542 and then Present
(Corresponding_Discriminant
(F
))
1544 CF
:= Corresponding_Discriminant
(F
);
1549 if Is_Unchecked_Union
(Base_Type
(R_Typ
)) then
1551 -- Within an initialization procedure this is the
1552 -- assignment to an unchecked union component, in which
1553 -- case there is no discriminant to initialize.
1555 if Inside_Init_Proc
then
1559 -- The assignment is part of a conversion from a
1560 -- derived unchecked union type with an inferable
1561 -- discriminant, to a parent type.
1563 Insert_Action
(N
, Make_Field_Assign
(CF
, True));
1567 Insert_Action
(N
, Make_Field_Assign
(CF
));
1570 Next_Discriminant
(F
);
1575 -- We know the underlying type is a record, but its current view
1576 -- may be private. We must retrieve the usable record declaration.
1578 if Nkind_In
(Decl
, N_Private_Type_Declaration
,
1579 N_Private_Extension_Declaration
)
1580 and then Present
(Full_View
(R_Typ
))
1582 RDef
:= Type_Definition
(Declaration_Node
(Full_View
(R_Typ
)));
1584 RDef
:= Type_Definition
(Decl
);
1587 if Nkind
(RDef
) = N_Derived_Type_Definition
then
1588 RDef
:= Record_Extension_Part
(RDef
);
1591 if Nkind
(RDef
) = N_Record_Definition
1592 and then Present
(Component_List
(RDef
))
1594 if Is_Unchecked_Union
(R_Typ
) then
1596 Make_Component_List_Assign
(Component_List
(RDef
), True));
1599 (N
, Make_Component_List_Assign
(Component_List
(RDef
)));
1602 Rewrite
(N
, Make_Null_Statement
(Loc
));
1605 end Expand_Assign_Record
;
1607 -----------------------------------
1608 -- Expand_N_Assignment_Statement --
1609 -----------------------------------
1611 -- This procedure implements various cases where an assignment statement
1612 -- cannot just be passed on to the back end in untransformed state.
1614 procedure Expand_N_Assignment_Statement
(N
: Node_Id
) is
1615 Loc
: constant Source_Ptr
:= Sloc
(N
);
1616 Crep
: constant Boolean := Change_Of_Representation
(N
);
1617 Lhs
: constant Node_Id
:= Name
(N
);
1618 Rhs
: constant Node_Id
:= Expression
(N
);
1619 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Lhs
));
1623 -- Special case to check right away, if the Componentwise_Assignment
1624 -- flag is set, this is a reanalysis from the expansion of the primitive
1625 -- assignment procedure for a tagged type, and all we need to do is to
1626 -- expand to assignment of components, because otherwise, we would get
1627 -- infinite recursion (since this looks like a tagged assignment which
1628 -- would normally try to *call* the primitive assignment procedure).
1630 if Componentwise_Assignment
(N
) then
1631 Expand_Assign_Record
(N
);
1635 -- Defend against invalid subscripts on left side if we are in standard
1636 -- validity checking mode. No need to do this if we are checking all
1639 -- Note that we do this right away, because there are some early return
1640 -- paths in this procedure, and this is required on all paths.
1642 if Validity_Checks_On
1643 and then Validity_Check_Default
1644 and then not Validity_Check_Subscripts
1646 Check_Valid_Lvalue_Subscripts
(Lhs
);
1649 -- Ada 2005 (AI-327): Handle assignment to priority of protected object
1651 -- Rewrite an assignment to X'Priority into a run-time call
1653 -- For example: X'Priority := New_Prio_Expr;
1654 -- ...is expanded into Set_Ceiling (X._Object, New_Prio_Expr);
1656 -- Note that although X'Priority is notionally an object, it is quite
1657 -- deliberately not defined as an aliased object in the RM. This means
1658 -- that it works fine to rewrite it as a call, without having to worry
1659 -- about complications that would other arise from X'Priority'Access,
1660 -- which is illegal, because of the lack of aliasing.
1662 if Ada_Version
>= Ada_2005
then
1665 Conctyp
: Entity_Id
;
1668 RT_Subprg_Name
: Node_Id
;
1671 -- Handle chains of renamings
1674 while Nkind
(Ent
) in N_Has_Entity
1675 and then Present
(Entity
(Ent
))
1676 and then Present
(Renamed_Object
(Entity
(Ent
)))
1678 Ent
:= Renamed_Object
(Entity
(Ent
));
1681 -- The attribute Priority applied to protected objects has been
1682 -- previously expanded into a call to the Get_Ceiling run-time
1685 if Nkind
(Ent
) = N_Function_Call
1686 and then (Entity
(Name
(Ent
)) = RTE
(RE_Get_Ceiling
)
1688 Entity
(Name
(Ent
)) = RTE
(RO_PE_Get_Ceiling
))
1690 -- Look for the enclosing concurrent type
1692 Conctyp
:= Current_Scope
;
1693 while not Is_Concurrent_Type
(Conctyp
) loop
1694 Conctyp
:= Scope
(Conctyp
);
1697 pragma Assert
(Is_Protected_Type
(Conctyp
));
1699 -- Generate the first actual of the call
1701 Subprg
:= Current_Scope
;
1702 while not Present
(Protected_Body_Subprogram
(Subprg
)) loop
1703 Subprg
:= Scope
(Subprg
);
1706 -- Select the appropriate run-time call
1708 if Number_Entries
(Conctyp
) = 0 then
1710 New_Occurrence_Of
(RTE
(RE_Set_Ceiling
), Loc
);
1713 New_Occurrence_Of
(RTE
(RO_PE_Set_Ceiling
), Loc
);
1717 Make_Procedure_Call_Statement
(Loc
,
1718 Name
=> RT_Subprg_Name
,
1719 Parameter_Associations
=> New_List
(
1720 New_Copy_Tree
(First
(Parameter_Associations
(Ent
))),
1721 Relocate_Node
(Expression
(N
))));
1730 -- Deal with assignment checks unless suppressed
1732 if not Suppress_Assignment_Checks
(N
) then
1734 -- First deal with generation of range check if required
1736 if Do_Range_Check
(Rhs
) then
1737 Generate_Range_Check
(Rhs
, Typ
, CE_Range_Check_Failed
);
1740 -- Then generate predicate check if required
1742 Apply_Predicate_Check
(Rhs
, Typ
);
1745 -- Check for a special case where a high level transformation is
1746 -- required. If we have either of:
1751 -- where P is a reference to a bit packed array, then we have to unwind
1752 -- the assignment. The exact meaning of being a reference to a bit
1753 -- packed array is as follows:
1755 -- An indexed component whose prefix is a bit packed array is a
1756 -- reference to a bit packed array.
1758 -- An indexed component or selected component whose prefix is a
1759 -- reference to a bit packed array is itself a reference ot a
1760 -- bit packed array.
1762 -- The required transformation is
1764 -- Tnn : prefix_type := P;
1765 -- Tnn.field := rhs;
1770 -- Tnn : prefix_type := P;
1771 -- Tnn (subscr) := rhs;
1774 -- Since P is going to be evaluated more than once, any subscripts
1775 -- in P must have their evaluation forced.
1777 if Nkind_In
(Lhs
, N_Indexed_Component
, N_Selected_Component
)
1778 and then Is_Ref_To_Bit_Packed_Array
(Prefix
(Lhs
))
1781 BPAR_Expr
: constant Node_Id
:= Relocate_Node
(Prefix
(Lhs
));
1782 BPAR_Typ
: constant Entity_Id
:= Etype
(BPAR_Expr
);
1783 Tnn
: constant Entity_Id
:=
1784 Make_Temporary
(Loc
, 'T', BPAR_Expr
);
1787 -- Insert the post assignment first, because we want to copy the
1788 -- BPAR_Expr tree before it gets analyzed in the context of the
1789 -- pre assignment. Note that we do not analyze the post assignment
1790 -- yet (we cannot till we have completed the analysis of the pre
1791 -- assignment). As usual, the analysis of this post assignment
1792 -- will happen on its own when we "run into" it after finishing
1793 -- the current assignment.
1796 Make_Assignment_Statement
(Loc
,
1797 Name
=> New_Copy_Tree
(BPAR_Expr
),
1798 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
1800 -- At this stage BPAR_Expr is a reference to a bit packed array
1801 -- where the reference was not expanded in the original tree,
1802 -- since it was on the left side of an assignment. But in the
1803 -- pre-assignment statement (the object definition), BPAR_Expr
1804 -- will end up on the right hand side, and must be reexpanded. To
1805 -- achieve this, we reset the analyzed flag of all selected and
1806 -- indexed components down to the actual indexed component for
1807 -- the packed array.
1811 Set_Analyzed
(Exp
, False);
1814 (Exp
, N_Selected_Component
, N_Indexed_Component
)
1816 Exp
:= Prefix
(Exp
);
1822 -- Now we can insert and analyze the pre-assignment
1824 -- If the right-hand side requires a transient scope, it has
1825 -- already been placed on the stack. However, the declaration is
1826 -- inserted in the tree outside of this scope, and must reflect
1827 -- the proper scope for its variable. This awkward bit is forced
1828 -- by the stricter scope discipline imposed by GCC 2.97.
1831 Uses_Transient_Scope
: constant Boolean :=
1833 and then N
= Node_To_Be_Wrapped
;
1836 if Uses_Transient_Scope
then
1837 Push_Scope
(Scope
(Current_Scope
));
1840 Insert_Before_And_Analyze
(N
,
1841 Make_Object_Declaration
(Loc
,
1842 Defining_Identifier
=> Tnn
,
1843 Object_Definition
=> New_Occurrence_Of
(BPAR_Typ
, Loc
),
1844 Expression
=> BPAR_Expr
));
1846 if Uses_Transient_Scope
then
1851 -- Now fix up the original assignment and continue processing
1853 Rewrite
(Prefix
(Lhs
),
1854 New_Occurrence_Of
(Tnn
, Loc
));
1856 -- We do not need to reanalyze that assignment, and we do not need
1857 -- to worry about references to the temporary, but we do need to
1858 -- make sure that the temporary is not marked as a true constant
1859 -- since we now have a generated assignment to it.
1861 Set_Is_True_Constant
(Tnn
, False);
1865 -- When we have the appropriate type of aggregate in the expression (it
1866 -- has been determined during analysis of the aggregate by setting the
1867 -- delay flag), let's perform in place assignment and thus avoid
1868 -- creating a temporary.
1870 if Is_Delayed_Aggregate
(Rhs
) then
1871 Convert_Aggr_In_Assignment
(N
);
1872 Rewrite
(N
, Make_Null_Statement
(Loc
));
1877 -- Apply discriminant check if required. If Lhs is an access type to a
1878 -- designated type with discriminants, we must always check. If the
1879 -- type has unknown discriminants, more elaborate processing below.
1881 if Has_Discriminants
(Etype
(Lhs
))
1882 and then not Has_Unknown_Discriminants
(Etype
(Lhs
))
1884 -- Skip discriminant check if change of representation. Will be
1885 -- done when the change of representation is expanded out.
1888 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
), Lhs
);
1891 -- If the type is private without discriminants, and the full type
1892 -- has discriminants (necessarily with defaults) a check may still be
1893 -- necessary if the Lhs is aliased. The private discriminants must be
1894 -- visible to build the discriminant constraints.
1896 -- Only an explicit dereference that comes from source indicates
1897 -- aliasing. Access to formals of protected operations and entries
1898 -- create dereferences but are not semantic aliasings.
1900 elsif Is_Private_Type
(Etype
(Lhs
))
1901 and then Has_Discriminants
(Typ
)
1902 and then Nkind
(Lhs
) = N_Explicit_Dereference
1903 and then Comes_From_Source
(Lhs
)
1906 Lt
: constant Entity_Id
:= Etype
(Lhs
);
1907 Ubt
: Entity_Id
:= Base_Type
(Typ
);
1910 -- In the case of an expander-generated record subtype whose base
1911 -- type still appears private, Typ will have been set to that
1912 -- private type rather than the underlying record type (because
1913 -- Underlying type will have returned the record subtype), so it's
1914 -- necessary to apply Underlying_Type again to the base type to
1915 -- get the record type we need for the discriminant check. Such
1916 -- subtypes can be created for assignments in certain cases, such
1917 -- as within an instantiation passed this kind of private type.
1918 -- It would be good to avoid this special test, but making changes
1919 -- to prevent this odd form of record subtype seems difficult. ???
1921 if Is_Private_Type
(Ubt
) then
1922 Ubt
:= Underlying_Type
(Ubt
);
1925 Set_Etype
(Lhs
, Ubt
);
1926 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Ubt
), Rhs
));
1927 Apply_Discriminant_Check
(Rhs
, Ubt
, Lhs
);
1928 Set_Etype
(Lhs
, Lt
);
1931 -- If the Lhs has a private type with unknown discriminants, it may
1932 -- have a full view with discriminants, but those are nameable only
1933 -- in the underlying type, so convert the Rhs to it before potential
1934 -- checking. Convert Lhs as well, otherwise the actual subtype might
1935 -- not be constructible.
1937 elsif Has_Unknown_Discriminants
(Base_Type
(Etype
(Lhs
)))
1938 and then Has_Discriminants
(Typ
)
1940 Rewrite
(Rhs
, OK_Convert_To
(Base_Type
(Typ
), Rhs
));
1941 Rewrite
(Lhs
, OK_Convert_To
(Base_Type
(Typ
), Lhs
));
1942 Apply_Discriminant_Check
(Rhs
, Typ
, Lhs
);
1944 -- In the access type case, we need the same discriminant check, and
1945 -- also range checks if we have an access to constrained array.
1947 elsif Is_Access_Type
(Etype
(Lhs
))
1948 and then Is_Constrained
(Designated_Type
(Etype
(Lhs
)))
1950 if Has_Discriminants
(Designated_Type
(Etype
(Lhs
))) then
1952 -- Skip discriminant check if change of representation. Will be
1953 -- done when the change of representation is expanded out.
1956 Apply_Discriminant_Check
(Rhs
, Etype
(Lhs
));
1959 elsif Is_Array_Type
(Designated_Type
(Etype
(Lhs
))) then
1960 Apply_Range_Check
(Rhs
, Etype
(Lhs
));
1962 if Is_Constrained
(Etype
(Lhs
)) then
1963 Apply_Length_Check
(Rhs
, Etype
(Lhs
));
1966 if Nkind
(Rhs
) = N_Allocator
then
1968 Target_Typ
: constant Entity_Id
:= Etype
(Expression
(Rhs
));
1969 C_Es
: Check_Result
;
1976 Etype
(Designated_Type
(Etype
(Lhs
))));
1988 -- Apply range check for access type case
1990 elsif Is_Access_Type
(Etype
(Lhs
))
1991 and then Nkind
(Rhs
) = N_Allocator
1992 and then Nkind
(Expression
(Rhs
)) = N_Qualified_Expression
1994 Analyze_And_Resolve
(Expression
(Rhs
));
1996 (Expression
(Rhs
), Designated_Type
(Etype
(Lhs
)));
1999 -- Ada 2005 (AI-231): Generate the run-time check
2001 if Is_Access_Type
(Typ
)
2002 and then Can_Never_Be_Null
(Etype
(Lhs
))
2003 and then not Can_Never_Be_Null
(Etype
(Rhs
))
2005 -- If an actual is an out parameter of a null-excluding access
2006 -- type, there is access check on entry, so we set the flag
2007 -- Suppress_Assignment_Checks on the generated statement to
2008 -- assign the actual to the parameter block, and we do not want
2009 -- to generate an additional check at this point.
2011 and then not Suppress_Assignment_Checks
(N
)
2013 Apply_Constraint_Check
(Rhs
, Etype
(Lhs
));
2016 -- Ada 2012 (AI05-148): Update current accessibility level if Rhs is a
2017 -- stand-alone obj of an anonymous access type.
2019 if Is_Access_Type
(Typ
)
2020 and then Is_Entity_Name
(Lhs
)
2021 and then Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
2024 function Lhs_Entity
return Entity_Id
;
2025 -- Look through renames to find the underlying entity.
2026 -- For assignment to a rename, we don't care about the
2027 -- Enclosing_Dynamic_Scope of the rename declaration.
2033 function Lhs_Entity
return Entity_Id
is
2034 Result
: Entity_Id
:= Entity
(Lhs
);
2037 while Present
(Renamed_Object
(Result
)) loop
2039 -- Renamed_Object must return an Entity_Name here
2040 -- because of preceding "Present (E_E_A (...))" test.
2042 Result
:= Entity
(Renamed_Object
(Result
));
2048 -- Local Declarations
2050 Access_Check
: constant Node_Id
:=
2051 Make_Raise_Program_Error
(Loc
,
2055 Dynamic_Accessibility_Level
(Rhs
),
2057 Make_Integer_Literal
(Loc
,
2060 (Enclosing_Dynamic_Scope
2062 Reason
=> PE_Accessibility_Check_Failed
);
2064 Access_Level_Update
: constant Node_Id
:=
2065 Make_Assignment_Statement
(Loc
,
2068 (Effective_Extra_Accessibility
2069 (Entity
(Lhs
)), Loc
),
2071 Dynamic_Accessibility_Level
(Rhs
));
2074 if not Accessibility_Checks_Suppressed
(Entity
(Lhs
)) then
2075 Insert_Action
(N
, Access_Check
);
2078 Insert_Action
(N
, Access_Level_Update
);
2082 -- Case of assignment to a bit packed array element. If there is a
2083 -- change of representation this must be expanded into components,
2084 -- otherwise this is a bit-field assignment.
2086 if Nkind
(Lhs
) = N_Indexed_Component
2087 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
)))
2089 -- Normal case, no change of representation
2092 Expand_Bit_Packed_Element_Set
(N
);
2095 -- Change of representation case
2098 -- Generate the following, to force component-by-component
2099 -- assignments in an efficient way. Otherwise each component
2100 -- will require a temporary and two bit-field manipulations.
2107 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
2113 Make_Object_Declaration
(Loc
,
2114 Defining_Identifier
=> Tnn
,
2115 Object_Definition
=>
2116 New_Occurrence_Of
(Etype
(Lhs
), Loc
)),
2117 Make_Assignment_Statement
(Loc
,
2118 Name
=> New_Occurrence_Of
(Tnn
, Loc
),
2119 Expression
=> Relocate_Node
(Rhs
)),
2120 Make_Assignment_Statement
(Loc
,
2121 Name
=> Relocate_Node
(Lhs
),
2122 Expression
=> New_Occurrence_Of
(Tnn
, Loc
)));
2124 Insert_Actions
(N
, Stats
);
2125 Rewrite
(N
, Make_Null_Statement
(Loc
));
2130 -- Build-in-place function call case. Note that we're not yet doing
2131 -- build-in-place for user-written assignment statements (the assignment
2132 -- here came from an aggregate.)
2134 elsif Ada_Version
>= Ada_2005
2135 and then Is_Build_In_Place_Function_Call
(Rhs
)
2137 Make_Build_In_Place_Call_In_Assignment
(N
, Rhs
);
2139 elsif Is_Tagged_Type
(Typ
) and then Is_Value_Type
(Etype
(Lhs
)) then
2141 -- Nothing to do for valuetypes
2142 -- ??? Set_Scope_Is_Transient (False);
2146 elsif Is_Tagged_Type
(Typ
)
2147 or else (Needs_Finalization
(Typ
) and then not Is_Array_Type
(Typ
))
2149 Tagged_Case
: declare
2150 L
: List_Id
:= No_List
;
2151 Expand_Ctrl_Actions
: constant Boolean := not No_Ctrl_Actions
(N
);
2154 -- In the controlled case, we ensure that function calls are
2155 -- evaluated before finalizing the target. In all cases, it makes
2156 -- the expansion easier if the side-effects are removed first.
2158 Remove_Side_Effects
(Lhs
);
2159 Remove_Side_Effects
(Rhs
);
2161 -- Avoid recursion in the mechanism
2165 -- If dispatching assignment, we need to dispatch to _assign
2167 if Is_Class_Wide_Type
(Typ
)
2169 -- If the type is tagged, we may as well use the predefined
2170 -- primitive assignment. This avoids inlining a lot of code
2171 -- and in the class-wide case, the assignment is replaced
2172 -- by a dispatching call to _assign. It is suppressed in the
2173 -- case of assignments created by the expander that correspond
2174 -- to initializations, where we do want to copy the tag
2175 -- (Expand_Ctrl_Actions flag is set False in this case). It is
2176 -- also suppressed if restriction No_Dispatching_Calls is in
2177 -- force because in that case predefined primitives are not
2180 or else (Is_Tagged_Type
(Typ
)
2181 and then not Is_Value_Type
(Etype
(Lhs
))
2182 and then Chars
(Current_Scope
) /= Name_uAssign
2183 and then Expand_Ctrl_Actions
2185 not Restriction_Active
(No_Dispatching_Calls
))
2187 if Is_Limited_Type
(Typ
) then
2189 -- This can happen in an instance when the formal is an
2190 -- extension of a limited interface, and the actual is
2191 -- limited. This is an error according to AI05-0087, but
2192 -- is not caught at the point of instantiation in earlier
2195 -- This is wrong, error messages cannot be issued during
2196 -- expansion, since they would be missed in -gnatc mode ???
2198 Error_Msg_N
("assignment not available on limited type", N
);
2202 -- Fetch the primitive op _assign and proper type to call it.
2203 -- Because of possible conflicts between private and full view,
2204 -- fetch the proper type directly from the operation profile.
2207 Op
: constant Entity_Id
:=
2208 Find_Prim_Op
(Typ
, Name_uAssign
);
2209 F_Typ
: Entity_Id
:= Etype
(First_Formal
(Op
));
2212 -- If the assignment is dispatching, make sure to use the
2215 if Is_Class_Wide_Type
(Typ
) then
2216 F_Typ
:= Class_Wide_Type
(F_Typ
);
2221 -- In case of assignment to a class-wide tagged type, before
2222 -- the assignment we generate run-time check to ensure that
2223 -- the tags of source and target match.
2225 if not Tag_Checks_Suppressed
(Typ
)
2226 and then Is_Class_Wide_Type
(Typ
)
2227 and then Is_Tagged_Type
(Typ
)
2228 and then Is_Tagged_Type
(Underlying_Type
(Etype
(Rhs
)))
2231 Make_Raise_Constraint_Error
(Loc
,
2235 Make_Selected_Component
(Loc
,
2236 Prefix
=> Duplicate_Subexpr
(Lhs
),
2238 Make_Identifier
(Loc
, Name_uTag
)),
2240 Make_Selected_Component
(Loc
,
2241 Prefix
=> Duplicate_Subexpr
(Rhs
),
2243 Make_Identifier
(Loc
, Name_uTag
))),
2244 Reason
=> CE_Tag_Check_Failed
));
2248 Left_N
: Node_Id
:= Duplicate_Subexpr
(Lhs
);
2249 Right_N
: Node_Id
:= Duplicate_Subexpr
(Rhs
);
2252 -- In order to dispatch the call to _assign the type of
2253 -- the actuals must match. Add conversion (if required).
2255 if Etype
(Lhs
) /= F_Typ
then
2256 Left_N
:= Unchecked_Convert_To
(F_Typ
, Left_N
);
2259 if Etype
(Rhs
) /= F_Typ
then
2260 Right_N
:= Unchecked_Convert_To
(F_Typ
, Right_N
);
2264 Make_Procedure_Call_Statement
(Loc
,
2265 Name
=> New_Occurrence_Of
(Op
, Loc
),
2266 Parameter_Associations
=> New_List
(
2268 Node2
=> Right_N
)));
2273 L
:= Make_Tag_Ctrl_Assignment
(N
);
2275 -- We can't afford to have destructive Finalization Actions in
2276 -- the Self assignment case, so if the target and the source
2277 -- are not obviously different, code is generated to avoid the
2278 -- self assignment case:
2280 -- if lhs'address /= rhs'address then
2281 -- <code for controlled and/or tagged assignment>
2284 -- Skip this if Restriction (No_Finalization) is active
2286 if not Statically_Different
(Lhs
, Rhs
)
2287 and then Expand_Ctrl_Actions
2288 and then not Restriction_Active
(No_Finalization
)
2291 Make_Implicit_If_Statement
(N
,
2295 Make_Attribute_Reference
(Loc
,
2296 Prefix
=> Duplicate_Subexpr
(Lhs
),
2297 Attribute_Name
=> Name_Address
),
2300 Make_Attribute_Reference
(Loc
,
2301 Prefix
=> Duplicate_Subexpr
(Rhs
),
2302 Attribute_Name
=> Name_Address
)),
2304 Then_Statements
=> L
));
2307 -- We need to set up an exception handler for implementing
2308 -- 7.6.1(18). The remaining adjustments are tackled by the
2309 -- implementation of adjust for record_controllers (see
2312 -- This is skipped if we have no finalization
2314 if Expand_Ctrl_Actions
2315 and then not Restriction_Active
(No_Finalization
)
2318 Make_Block_Statement
(Loc
,
2319 Handled_Statement_Sequence
=>
2320 Make_Handled_Sequence_Of_Statements
(Loc
,
2322 Exception_Handlers
=> New_List
(
2323 Make_Handler_For_Ctrl_Operation
(Loc
)))));
2328 Make_Block_Statement
(Loc
,
2329 Handled_Statement_Sequence
=>
2330 Make_Handled_Sequence_Of_Statements
(Loc
, Statements
=> L
)));
2332 -- If no restrictions on aborts, protect the whole assignment
2333 -- for controlled objects as per 9.8(11).
2335 if Needs_Finalization
(Typ
)
2336 and then Expand_Ctrl_Actions
2337 and then Abort_Allowed
2340 Blk
: constant Entity_Id
:=
2342 (E_Block
, Current_Scope
, Sloc
(N
), 'B');
2345 Set_Scope
(Blk
, Current_Scope
);
2346 Set_Etype
(Blk
, Standard_Void_Type
);
2347 Set_Identifier
(N
, New_Occurrence_Of
(Blk
, Sloc
(N
)));
2349 Prepend_To
(L
, Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
2350 Set_At_End_Proc
(Handled_Statement_Sequence
(N
),
2351 New_Occurrence_Of
(RTE
(RE_Abort_Undefer_Direct
), Loc
));
2352 Expand_At_End_Handler
2353 (Handled_Statement_Sequence
(N
), Blk
);
2357 -- N has been rewritten to a block statement for which it is
2358 -- known by construction that no checks are necessary: analyze
2359 -- it with all checks suppressed.
2361 Analyze
(N
, Suppress
=> All_Checks
);
2367 elsif Is_Array_Type
(Typ
) then
2369 Actual_Rhs
: Node_Id
:= Rhs
;
2372 while Nkind_In
(Actual_Rhs
, N_Type_Conversion
,
2373 N_Qualified_Expression
)
2375 Actual_Rhs
:= Expression
(Actual_Rhs
);
2378 Expand_Assign_Array
(N
, Actual_Rhs
);
2384 elsif Is_Record_Type
(Typ
) then
2385 Expand_Assign_Record
(N
);
2388 -- Scalar types. This is where we perform the processing related to the
2389 -- requirements of (RM 13.9.1(9-11)) concerning the handling of invalid
2392 elsif Is_Scalar_Type
(Typ
) then
2394 -- Case where right side is known valid
2396 if Expr_Known_Valid
(Rhs
) then
2398 -- Here the right side is valid, so it is fine. The case to deal
2399 -- with is when the left side is a local variable reference whose
2400 -- value is not currently known to be valid. If this is the case,
2401 -- and the assignment appears in an unconditional context, then
2402 -- we can mark the left side as now being valid if one of these
2403 -- conditions holds:
2405 -- The expression of the right side has Do_Range_Check set so
2406 -- that we know a range check will be performed. Note that it
2407 -- can be the case that a range check is omitted because we
2408 -- make the assumption that we can assume validity for operands
2409 -- appearing in the right side in determining whether a range
2410 -- check is required
2412 -- The subtype of the right side matches the subtype of the
2413 -- left side. In this case, even though we have not checked
2414 -- the range of the right side, we know it is in range of its
2415 -- subtype if the expression is valid.
2417 if Is_Local_Variable_Reference
(Lhs
)
2418 and then not Is_Known_Valid
(Entity
(Lhs
))
2419 and then In_Unconditional_Context
(N
)
2421 if Do_Range_Check
(Rhs
)
2422 or else Etype
(Lhs
) = Etype
(Rhs
)
2424 Set_Is_Known_Valid
(Entity
(Lhs
), True);
2428 -- Case where right side may be invalid in the sense of the RM
2429 -- reference above. The RM does not require that we check for the
2430 -- validity on an assignment, but it does require that the assignment
2431 -- of an invalid value not cause erroneous behavior.
2433 -- The general approach in GNAT is to use the Is_Known_Valid flag
2434 -- to avoid the need for validity checking on assignments. However
2435 -- in some cases, we have to do validity checking in order to make
2436 -- sure that the setting of this flag is correct.
2439 -- Validate right side if we are validating copies
2441 if Validity_Checks_On
2442 and then Validity_Check_Copies
2444 -- Skip this if left hand side is an array or record component
2445 -- and elementary component validity checks are suppressed.
2447 if Nkind_In
(Lhs
, N_Selected_Component
, N_Indexed_Component
)
2448 and then not Validity_Check_Components
2455 -- We can propagate this to the left side where appropriate
2457 if Is_Local_Variable_Reference
(Lhs
)
2458 and then not Is_Known_Valid
(Entity
(Lhs
))
2459 and then In_Unconditional_Context
(N
)
2461 Set_Is_Known_Valid
(Entity
(Lhs
), True);
2464 -- Otherwise check to see what should be done
2466 -- If left side is a local variable, then we just set its flag to
2467 -- indicate that its value may no longer be valid, since we are
2468 -- copying a potentially invalid value.
2470 elsif Is_Local_Variable_Reference
(Lhs
) then
2471 Set_Is_Known_Valid
(Entity
(Lhs
), False);
2473 -- Check for case of a nonlocal variable on the left side which
2474 -- is currently known to be valid. In this case, we simply ensure
2475 -- that the right side is valid. We only play the game of copying
2476 -- validity status for local variables, since we are doing this
2477 -- statically, not by tracing the full flow graph.
2479 elsif Is_Entity_Name
(Lhs
)
2480 and then Is_Known_Valid
(Entity
(Lhs
))
2482 -- Note: If Validity_Checking mode is set to none, we ignore
2483 -- the Ensure_Valid call so don't worry about that case here.
2487 -- In all other cases, we can safely copy an invalid value without
2488 -- worrying about the status of the left side. Since it is not a
2489 -- variable reference it will not be considered
2490 -- as being known to be valid in any case.
2499 when RE_Not_Available
=>
2501 end Expand_N_Assignment_Statement
;
2503 ------------------------------
2504 -- Expand_N_Block_Statement --
2505 ------------------------------
2507 -- Encode entity names defined in block statement
2509 procedure Expand_N_Block_Statement
(N
: Node_Id
) is
2511 Qualify_Entity_Names
(N
);
2512 end Expand_N_Block_Statement
;
2514 -----------------------------
2515 -- Expand_N_Case_Statement --
2516 -----------------------------
2518 procedure Expand_N_Case_Statement
(N
: Node_Id
) is
2519 Loc
: constant Source_Ptr
:= Sloc
(N
);
2520 Expr
: constant Node_Id
:= Expression
(N
);
2528 -- Check for the situation where we know at compile time which branch
2531 if Compile_Time_Known_Value
(Expr
) then
2532 Alt
:= Find_Static_Alternative
(N
);
2534 -- Do not consider controlled objects found in a case statement which
2535 -- actually models a case expression because their early finalization
2536 -- will affect the result of the expression.
2538 if not From_Conditional_Expression
(N
) then
2539 Process_Statements_For_Controlled_Objects
(Alt
);
2542 -- Move statements from this alternative after the case statement.
2543 -- They are already analyzed, so will be skipped by the analyzer.
2545 Insert_List_After
(N
, Statements
(Alt
));
2547 -- That leaves the case statement as a shell. So now we can kill all
2548 -- other alternatives in the case statement.
2550 Kill_Dead_Code
(Expression
(N
));
2556 -- Loop through case alternatives, skipping pragmas, and skipping
2557 -- the one alternative that we select (and therefore retain).
2559 Dead_Alt
:= First
(Alternatives
(N
));
2560 while Present
(Dead_Alt
) loop
2562 and then Nkind
(Dead_Alt
) = N_Case_Statement_Alternative
2564 Kill_Dead_Code
(Statements
(Dead_Alt
), Warn_On_Deleted_Code
);
2571 Rewrite
(N
, Make_Null_Statement
(Loc
));
2575 -- Here if the choice is not determined at compile time
2578 Last_Alt
: constant Node_Id
:= Last
(Alternatives
(N
));
2580 Others_Present
: Boolean;
2581 Others_Node
: Node_Id
;
2583 Then_Stms
: List_Id
;
2584 Else_Stms
: List_Id
;
2587 if Nkind
(First
(Discrete_Choices
(Last_Alt
))) = N_Others_Choice
then
2588 Others_Present
:= True;
2589 Others_Node
:= Last_Alt
;
2591 Others_Present
:= False;
2594 -- First step is to worry about possible invalid argument. The RM
2595 -- requires (RM 5.4(13)) that if the result is invalid (e.g. it is
2596 -- outside the base range), then Constraint_Error must be raised.
2598 -- Case of validity check required (validity checks are on, the
2599 -- expression is not known to be valid, and the case statement
2600 -- comes from source -- no need to validity check internally
2601 -- generated case statements).
2603 if Validity_Check_Default
then
2604 Ensure_Valid
(Expr
);
2607 -- If there is only a single alternative, just replace it with the
2608 -- sequence of statements since obviously that is what is going to
2609 -- be executed in all cases.
2611 Len
:= List_Length
(Alternatives
(N
));
2615 -- We still need to evaluate the expression if it has any side
2618 Remove_Side_Effects
(Expression
(N
));
2619 Alt
:= First
(Alternatives
(N
));
2621 -- Do not consider controlled objects found in a case statement
2622 -- which actually models a case expression because their early
2623 -- finalization will affect the result of the expression.
2625 if not From_Conditional_Expression
(N
) then
2626 Process_Statements_For_Controlled_Objects
(Alt
);
2629 Insert_List_After
(N
, Statements
(Alt
));
2631 -- That leaves the case statement as a shell. The alternative that
2632 -- will be executed is reset to a null list. So now we can kill
2633 -- the entire case statement.
2635 Kill_Dead_Code
(Expression
(N
));
2636 Rewrite
(N
, Make_Null_Statement
(Loc
));
2639 -- An optimization. If there are only two alternatives, and only
2640 -- a single choice, then rewrite the whole case statement as an
2641 -- if statement, since this can result in subsequent optimizations.
2642 -- This helps not only with case statements in the source of a
2643 -- simple form, but also with generated code (discriminant check
2644 -- functions in particular).
2646 -- Note: it is OK to do this before expanding out choices for any
2647 -- static predicates, since the if statement processing will handle
2648 -- the static predicate case fine.
2651 Chlist
:= Discrete_Choices
(First
(Alternatives
(N
)));
2653 if List_Length
(Chlist
) = 1 then
2654 Choice
:= First
(Chlist
);
2656 Then_Stms
:= Statements
(First
(Alternatives
(N
)));
2657 Else_Stms
:= Statements
(Last
(Alternatives
(N
)));
2659 -- For TRUE, generate "expression", not expression = true
2661 if Nkind
(Choice
) = N_Identifier
2662 and then Entity
(Choice
) = Standard_True
2664 Cond
:= Expression
(N
);
2666 -- For FALSE, generate "expression" and switch then/else
2668 elsif Nkind
(Choice
) = N_Identifier
2669 and then Entity
(Choice
) = Standard_False
2671 Cond
:= Expression
(N
);
2672 Else_Stms
:= Statements
(First
(Alternatives
(N
)));
2673 Then_Stms
:= Statements
(Last
(Alternatives
(N
)));
2675 -- For a range, generate "expression in range"
2677 elsif Nkind
(Choice
) = N_Range
2678 or else (Nkind
(Choice
) = N_Attribute_Reference
2679 and then Attribute_Name
(Choice
) = Name_Range
)
2680 or else (Is_Entity_Name
(Choice
)
2681 and then Is_Type
(Entity
(Choice
)))
2682 or else Nkind
(Choice
) = N_Subtype_Indication
2686 Left_Opnd
=> Expression
(N
),
2687 Right_Opnd
=> Relocate_Node
(Choice
));
2689 -- For any other subexpression "expression = value"
2694 Left_Opnd
=> Expression
(N
),
2695 Right_Opnd
=> Relocate_Node
(Choice
));
2698 -- Now rewrite the case as an IF
2701 Make_If_Statement
(Loc
,
2703 Then_Statements
=> Then_Stms
,
2704 Else_Statements
=> Else_Stms
));
2710 -- If the last alternative is not an Others choice, replace it with
2711 -- an N_Others_Choice. Note that we do not bother to call Analyze on
2712 -- the modified case statement, since it's only effect would be to
2713 -- compute the contents of the Others_Discrete_Choices which is not
2714 -- needed by the back end anyway.
2716 -- The reason we do this is that the back end always needs some
2717 -- default for a switch, so if we have not supplied one in the
2718 -- processing above for validity checking, then we need to supply
2721 if not Others_Present
then
2722 Others_Node
:= Make_Others_Choice
(Sloc
(Last_Alt
));
2723 Set_Others_Discrete_Choices
2724 (Others_Node
, Discrete_Choices
(Last_Alt
));
2725 Set_Discrete_Choices
(Last_Alt
, New_List
(Others_Node
));
2728 -- Deal with possible declarations of controlled objects, and also
2729 -- with rewriting choice sequences for static predicate references.
2731 Alt
:= First_Non_Pragma
(Alternatives
(N
));
2732 while Present
(Alt
) loop
2734 -- Do not consider controlled objects found in a case statement
2735 -- which actually models a case expression because their early
2736 -- finalization will affect the result of the expression.
2738 if not From_Conditional_Expression
(N
) then
2739 Process_Statements_For_Controlled_Objects
(Alt
);
2742 if Has_SP_Choice
(Alt
) then
2743 Expand_Static_Predicates_In_Choices
(Alt
);
2746 Next_Non_Pragma
(Alt
);
2749 end Expand_N_Case_Statement
;
2751 -----------------------------
2752 -- Expand_N_Exit_Statement --
2753 -----------------------------
2755 -- The only processing required is to deal with a possible C/Fortran
2756 -- boolean value used as the condition for the exit statement.
2758 procedure Expand_N_Exit_Statement
(N
: Node_Id
) is
2760 Adjust_Condition
(Condition
(N
));
2761 end Expand_N_Exit_Statement
;
2763 ----------------------------------
2764 -- Expand_Formal_Container_Loop --
2765 ----------------------------------
2767 procedure Expand_Formal_Container_Loop
(N
: Node_Id
) is
2768 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
2769 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
2770 Cursor
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
2771 Container
: constant Node_Id
:= Entity
(Name
(I_Spec
));
2772 Stats
: constant List_Id
:= Statements
(N
);
2779 -- The expansion resembles the one for Ada containers, but the
2780 -- primitives mention the domain of iteration explicitly, and
2781 -- function First applied to the container yields a cursor directly.
2783 -- Cursor : Cursor_type := First (Container);
2784 -- while Has_Element (Cursor, Container) loop
2785 -- <original loop statements>
2786 -- Cursor := Next (Container, Cursor);
2789 Build_Formal_Container_Iteration
2790 (N
, Container
, Cursor
, Init
, Advance
, New_Loop
);
2792 Set_Ekind
(Cursor
, E_Variable
);
2793 Insert_Action
(N
, Init
);
2795 Append_To
(Stats
, Advance
);
2797 Rewrite
(N
, New_Loop
);
2799 end Expand_Formal_Container_Loop
;
2801 ------------------------------------------
2802 -- Expand_Formal_Container_Element_Loop --
2803 ------------------------------------------
2805 procedure Expand_Formal_Container_Element_Loop
(N
: Node_Id
) is
2806 Loc
: constant Source_Ptr
:= Sloc
(N
);
2807 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
2808 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
2809 Element
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
2810 Container
: constant Node_Id
:= Entity
(Name
(I_Spec
));
2811 Container_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
2812 Stats
: constant List_Id
:= Statements
(N
);
2814 Cursor
: constant Entity_Id
:=
2815 Make_Defining_Identifier
(Loc
,
2816 Chars
=> New_External_Name
(Chars
(Element
), 'C'));
2817 Elmt_Decl
: Node_Id
;
2820 Element_Op
: constant Entity_Id
:=
2821 Get_Iterable_Type_Primitive
(Container_Typ
, Name_Element
);
2828 -- For an element iterator, the Element aspect must be present,
2829 -- (this is checked during analysis) and the expansion takes the form:
2831 -- Cursor : Cursor_type := First (Container);
2832 -- Elmt : Element_Type;
2833 -- while Has_Element (Cursor, Container) loop
2834 -- Elmt := Element (Container, Cursor);
2835 -- <original loop statements>
2836 -- Cursor := Next (Container, Cursor);
2839 Build_Formal_Container_Iteration
2840 (N
, Container
, Cursor
, Init
, Advance
, New_Loop
);
2842 Set_Ekind
(Cursor
, E_Variable
);
2843 Insert_Action
(N
, Init
);
2845 -- Declaration for Element.
2848 Make_Object_Declaration
(Loc
,
2849 Defining_Identifier
=> Element
,
2850 Object_Definition
=> New_Occurrence_Of
(Etype
(Element_Op
), Loc
));
2852 -- The element is only modified in expanded code, so it appears as
2853 -- unassigned to the warning machinery. We must suppress this spurious
2854 -- warning explicitly.
2856 Set_Warnings_Off
(Element
);
2859 Make_Assignment_Statement
(Loc
,
2860 Name
=> New_Occurrence_Of
(Element
, Loc
),
2862 Make_Function_Call
(Loc
,
2863 Name
=> New_Occurrence_Of
(Element_Op
, Loc
),
2864 Parameter_Associations
=> New_List
(
2865 New_Occurrence_Of
(Container
, Loc
),
2866 New_Occurrence_Of
(Cursor
, Loc
))));
2868 Prepend
(Elmt_Ref
, Stats
);
2869 Append_To
(Stats
, Advance
);
2871 -- The loop is rewritten as a block, to hold the element declaration
2874 Make_Block_Statement
(Loc
,
2875 Declarations
=> New_List
(Elmt_Decl
),
2876 Handled_Statement_Sequence
=>
2877 Make_Handled_Sequence_Of_Statements
(Loc
,
2878 Statements
=> New_List
(New_Loop
)));
2880 Rewrite
(N
, New_Loop
);
2882 end Expand_Formal_Container_Element_Loop
;
2884 -----------------------------
2885 -- Expand_N_Goto_Statement --
2886 -----------------------------
2888 -- Add poll before goto if polling active
2890 procedure Expand_N_Goto_Statement
(N
: Node_Id
) is
2892 Generate_Poll_Call
(N
);
2893 end Expand_N_Goto_Statement
;
2895 ---------------------------
2896 -- Expand_N_If_Statement --
2897 ---------------------------
2899 -- First we deal with the case of C and Fortran convention boolean values,
2900 -- with zero/non-zero semantics.
2902 -- Second, we deal with the obvious rewriting for the cases where the
2903 -- condition of the IF is known at compile time to be True or False.
2905 -- Third, we remove elsif parts which have non-empty Condition_Actions and
2906 -- rewrite as independent if statements. For example:
2917 -- <<condition actions of y>>
2923 -- This rewriting is needed if at least one elsif part has a non-empty
2924 -- Condition_Actions list. We also do the same processing if there is a
2925 -- constant condition in an elsif part (in conjunction with the first
2926 -- processing step mentioned above, for the recursive call made to deal
2927 -- with the created inner if, this deals with properly optimizing the
2928 -- cases of constant elsif conditions).
2930 procedure Expand_N_If_Statement
(N
: Node_Id
) is
2931 Loc
: constant Source_Ptr
:= Sloc
(N
);
2936 Warn_If_Deleted
: constant Boolean :=
2937 Warn_On_Deleted_Code
and then Comes_From_Source
(N
);
2938 -- Indicates whether we want warnings when we delete branches of the
2939 -- if statement based on constant condition analysis. We never want
2940 -- these warnings for expander generated code.
2943 -- Do not consider controlled objects found in an if statement which
2944 -- actually models an if expression because their early finalization
2945 -- will affect the result of the expression.
2947 if not From_Conditional_Expression
(N
) then
2948 Process_Statements_For_Controlled_Objects
(N
);
2951 Adjust_Condition
(Condition
(N
));
2953 -- The following loop deals with constant conditions for the IF. We
2954 -- need a loop because as we eliminate False conditions, we grab the
2955 -- first elsif condition and use it as the primary condition.
2957 while Compile_Time_Known_Value
(Condition
(N
)) loop
2959 -- If condition is True, we can simply rewrite the if statement now
2960 -- by replacing it by the series of then statements.
2962 if Is_True
(Expr_Value
(Condition
(N
))) then
2964 -- All the else parts can be killed
2966 Kill_Dead_Code
(Elsif_Parts
(N
), Warn_If_Deleted
);
2967 Kill_Dead_Code
(Else_Statements
(N
), Warn_If_Deleted
);
2969 Hed
:= Remove_Head
(Then_Statements
(N
));
2970 Insert_List_After
(N
, Then_Statements
(N
));
2974 -- If condition is False, then we can delete the condition and
2975 -- the Then statements
2978 -- We do not delete the condition if constant condition warnings
2979 -- are enabled, since otherwise we end up deleting the desired
2980 -- warning. Of course the backend will get rid of this True/False
2981 -- test anyway, so nothing is lost here.
2983 if not Constant_Condition_Warnings
then
2984 Kill_Dead_Code
(Condition
(N
));
2987 Kill_Dead_Code
(Then_Statements
(N
), Warn_If_Deleted
);
2989 -- If there are no elsif statements, then we simply replace the
2990 -- entire if statement by the sequence of else statements.
2992 if No
(Elsif_Parts
(N
)) then
2993 if No
(Else_Statements
(N
))
2994 or else Is_Empty_List
(Else_Statements
(N
))
2997 Make_Null_Statement
(Sloc
(N
)));
2999 Hed
:= Remove_Head
(Else_Statements
(N
));
3000 Insert_List_After
(N
, Else_Statements
(N
));
3006 -- If there are elsif statements, the first of them becomes the
3007 -- if/then section of the rebuilt if statement This is the case
3008 -- where we loop to reprocess this copied condition.
3011 Hed
:= Remove_Head
(Elsif_Parts
(N
));
3012 Insert_Actions
(N
, Condition_Actions
(Hed
));
3013 Set_Condition
(N
, Condition
(Hed
));
3014 Set_Then_Statements
(N
, Then_Statements
(Hed
));
3016 -- Hed might have been captured as the condition determining
3017 -- the current value for an entity. Now it is detached from
3018 -- the tree, so a Current_Value pointer in the condition might
3019 -- need to be updated.
3021 Set_Current_Value_Condition
(N
);
3023 if Is_Empty_List
(Elsif_Parts
(N
)) then
3024 Set_Elsif_Parts
(N
, No_List
);
3030 -- Loop through elsif parts, dealing with constant conditions and
3031 -- possible condition actions that are present.
3033 if Present
(Elsif_Parts
(N
)) then
3034 E
:= First
(Elsif_Parts
(N
));
3035 while Present
(E
) loop
3037 -- Do not consider controlled objects found in an if statement
3038 -- which actually models an if expression because their early
3039 -- finalization will affect the result of the expression.
3041 if not From_Conditional_Expression
(N
) then
3042 Process_Statements_For_Controlled_Objects
(E
);
3045 Adjust_Condition
(Condition
(E
));
3047 -- If there are condition actions, then rewrite the if statement
3048 -- as indicated above. We also do the same rewrite for a True or
3049 -- False condition. The further processing of this constant
3050 -- condition is then done by the recursive call to expand the
3051 -- newly created if statement
3053 if Present
(Condition_Actions
(E
))
3054 or else Compile_Time_Known_Value
(Condition
(E
))
3056 -- Note this is not an implicit if statement, since it is part
3057 -- of an explicit if statement in the source (or of an implicit
3058 -- if statement that has already been tested).
3061 Make_If_Statement
(Sloc
(E
),
3062 Condition
=> Condition
(E
),
3063 Then_Statements
=> Then_Statements
(E
),
3064 Elsif_Parts
=> No_List
,
3065 Else_Statements
=> Else_Statements
(N
));
3067 -- Elsif parts for new if come from remaining elsif's of parent
3069 while Present
(Next
(E
)) loop
3070 if No
(Elsif_Parts
(New_If
)) then
3071 Set_Elsif_Parts
(New_If
, New_List
);
3074 Append
(Remove_Next
(E
), Elsif_Parts
(New_If
));
3077 Set_Else_Statements
(N
, New_List
(New_If
));
3079 if Present
(Condition_Actions
(E
)) then
3080 Insert_List_Before
(New_If
, Condition_Actions
(E
));
3085 if Is_Empty_List
(Elsif_Parts
(N
)) then
3086 Set_Elsif_Parts
(N
, No_List
);
3092 -- No special processing for that elsif part, move to next
3100 -- Some more optimizations applicable if we still have an IF statement
3102 if Nkind
(N
) /= N_If_Statement
then
3106 -- Another optimization, special cases that can be simplified
3108 -- if expression then
3114 -- can be changed to:
3116 -- return expression;
3120 -- if expression then
3126 -- can be changed to:
3128 -- return not (expression);
3130 -- Only do these optimizations if we are at least at -O1 level and
3131 -- do not do them if control flow optimizations are suppressed.
3133 if Optimization_Level
> 0
3134 and then not Opt
.Suppress_Control_Flow_Optimizations
3136 if Nkind
(N
) = N_If_Statement
3137 and then No
(Elsif_Parts
(N
))
3138 and then Present
(Else_Statements
(N
))
3139 and then List_Length
(Then_Statements
(N
)) = 1
3140 and then List_Length
(Else_Statements
(N
)) = 1
3143 Then_Stm
: constant Node_Id
:= First
(Then_Statements
(N
));
3144 Else_Stm
: constant Node_Id
:= First
(Else_Statements
(N
));
3147 if Nkind
(Then_Stm
) = N_Simple_Return_Statement
3149 Nkind
(Else_Stm
) = N_Simple_Return_Statement
3152 Then_Expr
: constant Node_Id
:= Expression
(Then_Stm
);
3153 Else_Expr
: constant Node_Id
:= Expression
(Else_Stm
);
3156 if Nkind
(Then_Expr
) = N_Identifier
3158 Nkind
(Else_Expr
) = N_Identifier
3160 if Entity
(Then_Expr
) = Standard_True
3161 and then Entity
(Else_Expr
) = Standard_False
3164 Make_Simple_Return_Statement
(Loc
,
3165 Expression
=> Relocate_Node
(Condition
(N
))));
3169 elsif Entity
(Then_Expr
) = Standard_False
3170 and then Entity
(Else_Expr
) = Standard_True
3173 Make_Simple_Return_Statement
(Loc
,
3177 Relocate_Node
(Condition
(N
)))));
3187 end Expand_N_If_Statement
;
3189 --------------------------
3190 -- Expand_Iterator_Loop --
3191 --------------------------
3193 procedure Expand_Iterator_Loop
(N
: Node_Id
) is
3194 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3195 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
3196 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3197 Loc
: constant Source_Ptr
:= Sloc
(N
);
3199 Container
: constant Node_Id
:= Name
(I_Spec
);
3200 Container_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Container
));
3202 Iterator
: Entity_Id
;
3204 Stats
: List_Id
:= Statements
(N
);
3207 -- Processing for arrays
3209 if Is_Array_Type
(Container_Typ
) then
3210 Expand_Iterator_Loop_Over_Array
(N
);
3213 elsif Has_Aspect
(Container_Typ
, Aspect_Iterable
) then
3214 if Of_Present
(I_Spec
) then
3215 Expand_Formal_Container_Element_Loop
(N
);
3217 Expand_Formal_Container_Loop
(N
);
3223 -- Processing for containers
3225 -- For an "of" iterator the name is a container expression, which
3226 -- is transformed into a call to the default iterator.
3228 -- For an iterator of the form "in" the name is a function call
3229 -- that delivers an iterator type.
3231 -- In both cases, analysis of the iterator has introduced an object
3232 -- declaration to capture the domain, so that Container is an entity.
3234 -- The for loop is expanded into a while loop which uses a container
3235 -- specific cursor to desgnate each element.
3237 -- Iter : Iterator_Type := Container.Iterate;
3238 -- Cursor : Cursor_type := First (Iter);
3239 -- while Has_Element (Iter) loop
3241 -- -- The block is added when Element_Type is controlled
3243 -- Obj : Pack.Element_Type := Element (Cursor);
3244 -- -- for the "of" loop form
3246 -- <original loop statements>
3249 -- Cursor := Iter.Next (Cursor);
3252 -- If "reverse" is present, then the initialization of the cursor
3253 -- uses Last and the step becomes Prev. Pack is the name of the
3254 -- scope where the container package is instantiated.
3257 Element_Type
: constant Entity_Id
:= Etype
(Id
);
3258 Iter_Type
: Entity_Id
;
3261 Name_Init
: Name_Id
;
3262 Name_Step
: Name_Id
;
3265 -- The type of the iterator is the return type of the Iterate
3266 -- function used. For the "of" form this is the default iterator
3267 -- for the type, otherwise it is the type of the explicit
3268 -- function used in the iterator specification. The most common
3269 -- case will be an Iterate function in the container package.
3271 -- The primitive operations of the container type may not be
3272 -- use-visible, so we introduce the name of the enclosing package
3273 -- in the declarations below. The Iterator type is declared in a
3274 -- an instance within the container package itself.
3276 -- If the container type is a derived type, the cursor type is
3277 -- found in the package of the parent type.
3279 if Is_Derived_Type
(Container_Typ
) then
3280 Pack
:= Scope
(Root_Type
(Container_Typ
));
3282 Pack
:= Scope
(Container_Typ
);
3285 Iter_Type
:= Etype
(Name
(I_Spec
));
3287 -- The "of" case uses an internally generated cursor whose type
3288 -- is found in the container package. The domain of iteration
3289 -- is expanded into a call to the default Iterator function, but
3290 -- this expansion does not take place in quantified expressions
3291 -- that are analyzed with expansion disabled, and in that case the
3292 -- type of the iterator must be obtained from the aspect.
3294 if Of_Present
(I_Spec
) then
3296 Default_Iter
: constant Entity_Id
:=
3298 (Find_Value_Of_Aspect
3300 Aspect_Default_Iterator
));
3302 Container_Arg
: Node_Id
;
3306 Cursor
:= Make_Temporary
(Loc
, 'C');
3308 -- For an container element iterator, the iterator type
3309 -- is obtained from the corresponding aspect, whose return
3310 -- type is descended from the corresponding interface type
3311 -- in some instance of Ada.Iterator_Interfaces. The actuals
3312 -- of that instantiation are Cursor and Has_Element.
3314 Iter_Type
:= Etype
(Default_Iter
);
3316 -- The iterator type, which is a class_wide type, may itself
3317 -- be derived locally, so the desired instantiation is the
3318 -- scope of the root type of the iterator type.
3320 Pack
:= Scope
(Root_Type
(Etype
(Iter_Type
)));
3322 -- Rewrite domain of iteration as a call to the default
3323 -- iterator for the container type. If the container is
3324 -- a derived type and the aspect is inherited, convert
3325 -- container to parent type. The Cursor type is also
3326 -- inherited from the scope of the parent.
3328 if Base_Type
(Etype
(Container
)) =
3329 Base_Type
(Etype
(First_Formal
(Default_Iter
)))
3331 Container_Arg
:= New_Copy_Tree
(Container
);
3335 Make_Type_Conversion
(Loc
,
3338 (Etype
(First_Formal
(Default_Iter
)), Loc
),
3339 Expression
=> New_Copy_Tree
(Container
));
3342 Rewrite
(Name
(I_Spec
),
3343 Make_Function_Call
(Loc
,
3344 Name
=> New_Occurrence_Of
(Default_Iter
, Loc
),
3345 Parameter_Associations
=>
3346 New_List
(Container_Arg
)));
3347 Analyze_And_Resolve
(Name
(I_Spec
));
3349 -- Find cursor type in proper iterator package, which is an
3350 -- instantiation of Iterator_Interfaces.
3352 Ent
:= First_Entity
(Pack
);
3353 while Present
(Ent
) loop
3354 if Chars
(Ent
) = Name_Cursor
then
3355 Set_Etype
(Cursor
, Etype
(Ent
));
3362 -- Id : Element_Type renames Container (Cursor);
3363 -- This assumes that the container type has an indexing
3364 -- operation with Cursor. The check that this operation
3365 -- exists is performed in Check_Container_Indexing.
3368 Make_Object_Renaming_Declaration
(Loc
,
3369 Defining_Identifier
=> Id
,
3371 New_Occurrence_Of
(Element_Type
, Loc
),
3373 Make_Indexed_Component
(Loc
,
3374 Prefix
=> Relocate_Node
(Container_Arg
),
3376 New_List
(New_Occurrence_Of
(Cursor
, Loc
))));
3378 -- The defining identifier in the iterator is user-visible
3379 -- and must be visible in the debugger.
3381 Set_Debug_Info_Needed
(Id
);
3383 -- If the container does not have a variable indexing aspect,
3384 -- the element is a constant in the loop.
3386 if No
(Find_Value_Of_Aspect
3387 (Container_Typ
, Aspect_Variable_Indexing
))
3389 Set_Ekind
(Id
, E_Constant
);
3392 -- If the container holds controlled objects, wrap the loop
3393 -- statements and element renaming declaration with a block.
3394 -- This ensures that the result of Element (Cusor) is
3395 -- cleaned up after each iteration of the loop.
3397 if Needs_Finalization
(Element_Type
) then
3401 -- Id : Element_Type := Element (curosr);
3403 -- <original loop statements>
3407 Make_Block_Statement
(Loc
,
3408 Declarations
=> New_List
(Decl
),
3409 Handled_Statement_Sequence
=>
3410 Make_Handled_Sequence_Of_Statements
(Loc
,
3411 Statements
=> Stats
)));
3413 -- Elements do not need finalization
3416 Prepend_To
(Stats
, Decl
);
3420 -- X in Iterate (S) : type of iterator is type of explicitly
3421 -- given Iterate function, and the loop variable is the cursor.
3422 -- It will be assigned in the loop and must be a variable.
3426 Set_Ekind
(Cursor
, E_Variable
);
3429 Iterator
:= Make_Temporary
(Loc
, 'I');
3431 -- Determine the advancement and initialization steps for the
3434 -- Analysis of the expanded loop will verify that the container
3435 -- has a reverse iterator.
3437 if Reverse_Present
(I_Spec
) then
3438 Name_Init
:= Name_Last
;
3439 Name_Step
:= Name_Previous
;
3442 Name_Init
:= Name_First
;
3443 Name_Step
:= Name_Next
;
3446 -- For both iterator forms, add a call to the step operation to
3447 -- advance the cursor. Generate:
3449 -- Cursor := Iterator.Next (Cursor);
3453 -- Cursor := Next (Cursor);
3460 Make_Function_Call
(Loc
,
3462 Make_Selected_Component
(Loc
,
3463 Prefix
=> New_Occurrence_Of
(Iterator
, Loc
),
3464 Selector_Name
=> Make_Identifier
(Loc
, Name_Step
)),
3465 Parameter_Associations
=> New_List
(
3466 New_Occurrence_Of
(Cursor
, Loc
)));
3469 Make_Assignment_Statement
(Loc
,
3470 Name
=> New_Occurrence_Of
(Cursor
, Loc
),
3471 Expression
=> Rhs
));
3475 -- while Iterator.Has_Element loop
3479 -- Has_Element is the second actual in the iterator package
3482 Make_Loop_Statement
(Loc
,
3484 Make_Iteration_Scheme
(Loc
,
3486 Make_Function_Call
(Loc
,
3489 Next_Entity
(First_Entity
(Pack
)), Loc
),
3490 Parameter_Associations
=>
3491 New_List
(New_Occurrence_Of
(Cursor
, Loc
)))),
3493 Statements
=> Stats
,
3494 End_Label
=> Empty
);
3496 -- If present, preserve identifier of loop, which can be used in
3497 -- an exit statement in the body.
3499 if Present
(Identifier
(N
)) then
3500 Set_Identifier
(New_Loop
, Relocate_Node
(Identifier
(N
)));
3503 -- Create the declarations for Iterator and cursor and insert them
3504 -- before the source loop. Given that the domain of iteration is
3505 -- already an entity, the iterator is just a renaming of that
3506 -- entity. Possible optimization ???
3509 -- I : Iterator_Type renames Container;
3510 -- C : Cursor_Type := Container.[First | Last];
3513 Make_Object_Renaming_Declaration
(Loc
,
3514 Defining_Identifier
=> Iterator
,
3515 Subtype_Mark
=> New_Occurrence_Of
(Iter_Type
, Loc
),
3516 Name
=> Relocate_Node
(Name
(I_Spec
))));
3518 -- Create declaration for cursor
3525 Make_Object_Declaration
(Loc
,
3526 Defining_Identifier
=> Cursor
,
3527 Object_Definition
=>
3528 New_Occurrence_Of
(Etype
(Cursor
), Loc
),
3530 Make_Selected_Component
(Loc
,
3531 Prefix
=> New_Occurrence_Of
(Iterator
, Loc
),
3533 Make_Identifier
(Loc
, Name_Init
)));
3535 -- The cursor is only modified in expanded code, so it appears
3536 -- as unassigned to the warning machinery. We must suppress
3537 -- this spurious warning explicitly.
3539 Set_Warnings_Off
(Cursor
);
3540 Set_Assignment_OK
(Decl
);
3542 Insert_Action
(N
, Decl
);
3545 -- If the range of iteration is given by a function call that
3546 -- returns a container, the finalization actions have been saved
3547 -- in the Condition_Actions of the iterator. Insert them now at
3548 -- the head of the loop.
3550 if Present
(Condition_Actions
(Isc
)) then
3551 Insert_List_Before
(N
, Condition_Actions
(Isc
));
3555 Rewrite
(N
, New_Loop
);
3557 end Expand_Iterator_Loop
;
3559 -------------------------------------
3560 -- Expand_Iterator_Loop_Over_Array --
3561 -------------------------------------
3563 procedure Expand_Iterator_Loop_Over_Array
(N
: Node_Id
) is
3564 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3565 I_Spec
: constant Node_Id
:= Iterator_Specification
(Isc
);
3566 Array_Node
: constant Node_Id
:= Name
(I_Spec
);
3567 Array_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Array_Node
));
3568 Array_Dim
: constant Pos
:= Number_Dimensions
(Array_Typ
);
3569 Id
: constant Entity_Id
:= Defining_Identifier
(I_Spec
);
3570 Loc
: constant Source_Ptr
:= Sloc
(N
);
3571 Stats
: constant List_Id
:= Statements
(N
);
3572 Core_Loop
: Node_Id
;
3574 Iterator
: Entity_Id
;
3576 -- Start of processing for Expand_Iterator_Loop_Over_Array
3579 -- for Element of Array loop
3581 -- This case requires an internally generated cursor to iterate over
3584 if Of_Present
(I_Spec
) then
3585 Iterator
:= Make_Temporary
(Loc
, 'C');
3588 -- Element : Component_Type renames Array (Iterator);
3591 Make_Indexed_Component
(Loc
,
3592 Prefix
=> Relocate_Node
(Array_Node
),
3593 Expressions
=> New_List
(New_Occurrence_Of
(Iterator
, Loc
)));
3596 Make_Object_Renaming_Declaration
(Loc
,
3597 Defining_Identifier
=> Id
,
3599 New_Occurrence_Of
(Component_Type
(Array_Typ
), Loc
),
3602 -- Mark the loop variable as needing debug info, so that expansion
3603 -- of the renaming will result in Materialize_Entity getting set via
3604 -- Debug_Renaming_Declaration. (This setting is needed here because
3605 -- the setting in Freeze_Entity comes after the expansion, which is
3608 Set_Debug_Info_Needed
(Id
);
3610 -- for Index in Array loop
3612 -- This case utilizes the already given iterator name
3620 -- for Iterator in [reverse] Array'Range (Array_Dim) loop
3621 -- Element : Component_Type renames Array (Iterator);
3622 -- <original loop statements>
3626 Make_Loop_Statement
(Loc
,
3628 Make_Iteration_Scheme
(Loc
,
3629 Loop_Parameter_Specification
=>
3630 Make_Loop_Parameter_Specification
(Loc
,
3631 Defining_Identifier
=> Iterator
,
3632 Discrete_Subtype_Definition
=>
3633 Make_Attribute_Reference
(Loc
,
3634 Prefix
=> Relocate_Node
(Array_Node
),
3635 Attribute_Name
=> Name_Range
,
3636 Expressions
=> New_List
(
3637 Make_Integer_Literal
(Loc
, Array_Dim
))),
3638 Reverse_Present
=> Reverse_Present
(I_Spec
))),
3639 Statements
=> Stats
,
3640 End_Label
=> Empty
);
3642 -- Processing for multidimensional array
3644 if Array_Dim
> 1 then
3645 for Dim
in 1 .. Array_Dim
- 1 loop
3646 Iterator
:= Make_Temporary
(Loc
, 'C');
3648 -- Generate the dimension loops starting from the innermost one
3650 -- for Iterator in [reverse] Array'Range (Array_Dim - Dim) loop
3655 Make_Loop_Statement
(Loc
,
3657 Make_Iteration_Scheme
(Loc
,
3658 Loop_Parameter_Specification
=>
3659 Make_Loop_Parameter_Specification
(Loc
,
3660 Defining_Identifier
=> Iterator
,
3661 Discrete_Subtype_Definition
=>
3662 Make_Attribute_Reference
(Loc
,
3663 Prefix
=> Relocate_Node
(Array_Node
),
3664 Attribute_Name
=> Name_Range
,
3665 Expressions
=> New_List
(
3666 Make_Integer_Literal
(Loc
, Array_Dim
- Dim
))),
3667 Reverse_Present
=> Reverse_Present
(I_Spec
))),
3668 Statements
=> New_List
(Core_Loop
),
3669 End_Label
=> Empty
);
3671 -- Update the previously created object renaming declaration with
3672 -- the new iterator.
3674 Prepend_To
(Expressions
(Ind_Comp
),
3675 New_Occurrence_Of
(Iterator
, Loc
));
3679 -- If original loop has a source name, preserve it so it can be
3680 -- recognized by an exit statement in the body of the rewritten loop.
3681 -- This only concerns source names: the generated name of an anonymous
3682 -- loop will be create again during the subsequent analysis below.
3684 if Present
(Identifier
(N
))
3685 and then Comes_From_Source
(Identifier
(N
))
3687 Set_Identifier
(Core_Loop
, Relocate_Node
(Identifier
(N
)));
3690 Rewrite
(N
, Core_Loop
);
3692 end Expand_Iterator_Loop_Over_Array
;
3694 -----------------------------
3695 -- Expand_N_Loop_Statement --
3696 -----------------------------
3698 -- 1. Remove null loop entirely
3699 -- 2. Deal with while condition for C/Fortran boolean
3700 -- 3. Deal with loops with a non-standard enumeration type range
3701 -- 4. Deal with while loops where Condition_Actions is set
3702 -- 5. Deal with loops over predicated subtypes
3703 -- 6. Deal with loops with iterators over arrays and containers
3704 -- 7. Insert polling call if required
3706 procedure Expand_N_Loop_Statement
(N
: Node_Id
) is
3707 Loc
: constant Source_Ptr
:= Sloc
(N
);
3708 Scheme
: constant Node_Id
:= Iteration_Scheme
(N
);
3714 if Is_Null_Loop
(N
) then
3715 Rewrite
(N
, Make_Null_Statement
(Loc
));
3719 -- Deal with condition for C/Fortran Boolean
3721 if Present
(Scheme
) then
3722 Adjust_Condition
(Condition
(Scheme
));
3725 -- Generate polling call
3727 if Is_Non_Empty_List
(Statements
(N
)) then
3728 Generate_Poll_Call
(First
(Statements
(N
)));
3731 -- Nothing more to do for plain loop with no iteration scheme
3736 -- Case of for loop (Loop_Parameter_Specification present)
3738 -- Note: we do not have to worry about validity checking of the for loop
3739 -- range bounds here, since they were frozen with constant declarations
3740 -- and it is during that process that the validity checking is done.
3742 elsif Present
(Loop_Parameter_Specification
(Scheme
)) then
3744 LPS
: constant Node_Id
:=
3745 Loop_Parameter_Specification
(Scheme
);
3746 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
3747 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
3748 Btype
: constant Entity_Id
:= Base_Type
(Ltype
);
3754 -- Deal with loop over predicates
3756 if Is_Discrete_Type
(Ltype
)
3757 and then Present
(Predicate_Function
(Ltype
))
3759 Expand_Predicated_Loop
(N
);
3761 -- Handle the case where we have a for loop with the range type
3762 -- being an enumeration type with non-standard representation.
3763 -- In this case we expand:
3765 -- for x in [reverse] a .. b loop
3771 -- for xP in [reverse] integer
3772 -- range etype'Pos (a) .. etype'Pos (b)
3775 -- x : constant etype := Pos_To_Rep (xP);
3781 elsif Is_Enumeration_Type
(Btype
)
3782 and then Present
(Enum_Pos_To_Rep
(Btype
))
3785 Make_Defining_Identifier
(Loc
,
3786 Chars
=> New_External_Name
(Chars
(Loop_Id
), 'P'));
3788 -- If the type has a contiguous representation, successive
3789 -- values can be generated as offsets from the first literal.
3791 if Has_Contiguous_Rep
(Btype
) then
3793 Unchecked_Convert_To
(Btype
,
3796 Make_Integer_Literal
(Loc
,
3797 Enumeration_Rep
(First_Literal
(Btype
))),
3798 Right_Opnd
=> New_Occurrence_Of
(New_Id
, Loc
)));
3800 -- Use the constructed array Enum_Pos_To_Rep
3803 Make_Indexed_Component
(Loc
,
3805 New_Occurrence_Of
(Enum_Pos_To_Rep
(Btype
), Loc
),
3807 New_List
(New_Occurrence_Of
(New_Id
, Loc
)));
3810 -- Build declaration for loop identifier
3814 Make_Object_Declaration
(Loc
,
3815 Defining_Identifier
=> Loop_Id
,
3816 Constant_Present
=> True,
3817 Object_Definition
=> New_Occurrence_Of
(Ltype
, Loc
),
3818 Expression
=> Expr
));
3821 Make_Loop_Statement
(Loc
,
3822 Identifier
=> Identifier
(N
),
3825 Make_Iteration_Scheme
(Loc
,
3826 Loop_Parameter_Specification
=>
3827 Make_Loop_Parameter_Specification
(Loc
,
3828 Defining_Identifier
=> New_Id
,
3829 Reverse_Present
=> Reverse_Present
(LPS
),
3831 Discrete_Subtype_Definition
=>
3832 Make_Subtype_Indication
(Loc
,
3835 New_Occurrence_Of
(Standard_Natural
, Loc
),
3838 Make_Range_Constraint
(Loc
,
3843 Make_Attribute_Reference
(Loc
,
3845 New_Occurrence_Of
(Btype
, Loc
),
3847 Attribute_Name
=> Name_Pos
,
3849 Expressions
=> New_List
(
3851 (Type_Low_Bound
(Ltype
)))),
3854 Make_Attribute_Reference
(Loc
,
3856 New_Occurrence_Of
(Btype
, Loc
),
3858 Attribute_Name
=> Name_Pos
,
3860 Expressions
=> New_List
(
3865 Statements
=> New_List
(
3866 Make_Block_Statement
(Loc
,
3867 Declarations
=> Decls
,
3868 Handled_Statement_Sequence
=>
3869 Make_Handled_Sequence_Of_Statements
(Loc
,
3870 Statements
=> Statements
(N
)))),
3872 End_Label
=> End_Label
(N
)));
3874 -- The loop parameter's entity must be removed from the loop
3875 -- scope's entity list and rendered invisible, since it will
3876 -- now be located in the new block scope. Any other entities
3877 -- already associated with the loop scope, such as the loop
3878 -- parameter's subtype, will remain there.
3880 -- In an element loop, the loop will contain a declaration for
3881 -- a cursor variable; otherwise the loop id is the first entity
3882 -- in the scope constructed for the loop.
3884 if Comes_From_Source
(Loop_Id
) then
3885 pragma Assert
(First_Entity
(Scope
(Loop_Id
)) = Loop_Id
);
3889 Set_First_Entity
(Scope
(Loop_Id
), Next_Entity
(Loop_Id
));
3890 Remove_Homonym
(Loop_Id
);
3892 if Last_Entity
(Scope
(Loop_Id
)) = Loop_Id
then
3893 Set_Last_Entity
(Scope
(Loop_Id
), Empty
);
3898 -- Nothing to do with other cases of for loops
3905 -- Second case, if we have a while loop with Condition_Actions set, then
3906 -- we change it into a plain loop:
3915 -- <<condition actions>>
3920 elsif Present
(Scheme
)
3921 and then Present
(Condition_Actions
(Scheme
))
3922 and then Present
(Condition
(Scheme
))
3929 Make_Exit_Statement
(Sloc
(Condition
(Scheme
)),
3931 Make_Op_Not
(Sloc
(Condition
(Scheme
)),
3932 Right_Opnd
=> Condition
(Scheme
)));
3934 Prepend
(ES
, Statements
(N
));
3935 Insert_List_Before
(ES
, Condition_Actions
(Scheme
));
3937 -- This is not an implicit loop, since it is generated in response
3938 -- to the loop statement being processed. If this is itself
3939 -- implicit, the restriction has already been checked. If not,
3940 -- it is an explicit loop.
3943 Make_Loop_Statement
(Sloc
(N
),
3944 Identifier
=> Identifier
(N
),
3945 Statements
=> Statements
(N
),
3946 End_Label
=> End_Label
(N
)));
3951 -- Here to deal with iterator case
3953 elsif Present
(Scheme
)
3954 and then Present
(Iterator_Specification
(Scheme
))
3956 Expand_Iterator_Loop
(N
);
3958 -- An iterator loop may generate renaming declarations for elements
3959 -- that require debug information. This is the case in particular
3960 -- with element iterators, where debug information must be generated
3961 -- for the temporary that holds the element value. These temporaries
3962 -- are created within a transient block whose local declarations are
3963 -- transferred to the loop, which now has non-trivial local objects.
3965 if Nkind
(N
) = N_Loop_Statement
3966 and then Present
(Identifier
(N
))
3968 Qualify_Entity_Names
(N
);
3972 -- When the iteration scheme mentiones attribute 'Loop_Entry, the loop
3973 -- is transformed into a conditional block where the original loop is
3974 -- the sole statement. Inspect the statements of the nested loop for
3975 -- controlled objects.
3979 if Subject_To_Loop_Entry_Attributes
(Stmt
) then
3980 Stmt
:= Find_Loop_In_Conditional_Block
(Stmt
);
3983 Process_Statements_For_Controlled_Objects
(Stmt
);
3984 end Expand_N_Loop_Statement
;
3986 ----------------------------
3987 -- Expand_Predicated_Loop --
3988 ----------------------------
3990 -- Note: the expander can handle generation of loops over predicated
3991 -- subtypes for both the dynamic and static cases. Depending on what
3992 -- we decide is allowed in Ada 2012 mode and/or extensions allowed
3993 -- mode, the semantic analyzer may disallow one or both forms.
3995 procedure Expand_Predicated_Loop
(N
: Node_Id
) is
3996 Loc
: constant Source_Ptr
:= Sloc
(N
);
3997 Isc
: constant Node_Id
:= Iteration_Scheme
(N
);
3998 LPS
: constant Node_Id
:= Loop_Parameter_Specification
(Isc
);
3999 Loop_Id
: constant Entity_Id
:= Defining_Identifier
(LPS
);
4000 Ltype
: constant Entity_Id
:= Etype
(Loop_Id
);
4001 Stat
: constant List_Id
:= Static_Discrete_Predicate
(Ltype
);
4002 Stmts
: constant List_Id
:= Statements
(N
);
4005 -- Case of iteration over non-static predicate, should not be possible
4006 -- since this is not allowed by the semantics and should have been
4007 -- caught during analysis of the loop statement.
4010 raise Program_Error
;
4012 -- If the predicate list is empty, that corresponds to a predicate of
4013 -- False, in which case the loop won't run at all, and we rewrite the
4014 -- entire loop as a null statement.
4016 elsif Is_Empty_List
(Stat
) then
4017 Rewrite
(N
, Make_Null_Statement
(Loc
));
4020 -- For expansion over a static predicate we generate the following
4023 -- J : Ltype := min-val;
4028 -- when endpoint => J := startpoint;
4029 -- when endpoint => J := startpoint;
4031 -- when max-val => exit;
4032 -- when others => J := Lval'Succ (J);
4037 -- To make this a little clearer, let's take a specific example:
4039 -- type Int is range 1 .. 10;
4040 -- subtype L is Int with
4041 -- predicate => L in 3 | 10 | 5 .. 7;
4043 -- for L in StaticP loop
4044 -- Put_Line ("static:" & J'Img);
4047 -- In this case, the loop is transformed into
4054 -- when 3 => J := 5;
4055 -- when 7 => J := 10;
4057 -- when others => J := L'Succ (J);
4063 Static_Predicate
: declare
4070 function Lo_Val
(N
: Node_Id
) return Node_Id
;
4071 -- Given static expression or static range, returns an identifier
4072 -- whose value is the low bound of the expression value or range.
4074 function Hi_Val
(N
: Node_Id
) return Node_Id
;
4075 -- Given static expression or static range, returns an identifier
4076 -- whose value is the high bound of the expression value or range.
4082 function Hi_Val
(N
: Node_Id
) return Node_Id
is
4084 if Is_OK_Static_Expression
(N
) then
4085 return New_Copy
(N
);
4087 pragma Assert
(Nkind
(N
) = N_Range
);
4088 return New_Copy
(High_Bound
(N
));
4096 function Lo_Val
(N
: Node_Id
) return Node_Id
is
4098 if Is_OK_Static_Expression
(N
) then
4099 return New_Copy
(N
);
4101 pragma Assert
(Nkind
(N
) = N_Range
);
4102 return New_Copy
(Low_Bound
(N
));
4106 -- Start of processing for Static_Predicate
4109 -- Convert loop identifier to normal variable and reanalyze it so
4110 -- that this conversion works. We have to use the same defining
4111 -- identifier, since there may be references in the loop body.
4113 Set_Analyzed
(Loop_Id
, False);
4114 Set_Ekind
(Loop_Id
, E_Variable
);
4116 -- In most loops the loop variable is assigned in various
4117 -- alternatives in the body. However, in the rare case when
4118 -- the range specifies a single element, the loop variable
4119 -- may trigger a spurious warning that is could be constant.
4120 -- This warning might as well be suppressed.
4122 Set_Warnings_Off
(Loop_Id
);
4124 -- Loop to create branches of case statement
4128 while Present
(P
) loop
4129 if No
(Next
(P
)) then
4130 S
:= Make_Exit_Statement
(Loc
);
4133 Make_Assignment_Statement
(Loc
,
4134 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
4135 Expression
=> Lo_Val
(Next
(P
)));
4136 Set_Suppress_Assignment_Checks
(S
);
4140 Make_Case_Statement_Alternative
(Loc
,
4141 Statements
=> New_List
(S
),
4142 Discrete_Choices
=> New_List
(Hi_Val
(P
))));
4147 -- Add others choice
4150 Make_Assignment_Statement
(Loc
,
4151 Name
=> New_Occurrence_Of
(Loop_Id
, Loc
),
4153 Make_Attribute_Reference
(Loc
,
4154 Prefix
=> New_Occurrence_Of
(Ltype
, Loc
),
4155 Attribute_Name
=> Name_Succ
,
4156 Expressions
=> New_List
(
4157 New_Occurrence_Of
(Loop_Id
, Loc
))));
4158 Set_Suppress_Assignment_Checks
(S
);
4161 Make_Case_Statement_Alternative
(Loc
,
4162 Discrete_Choices
=> New_List
(Make_Others_Choice
(Loc
)),
4163 Statements
=> New_List
(S
)));
4165 -- Construct case statement and append to body statements
4168 Make_Case_Statement
(Loc
,
4169 Expression
=> New_Occurrence_Of
(Loop_Id
, Loc
),
4170 Alternatives
=> Alts
);
4171 Append_To
(Stmts
, Cstm
);
4176 Make_Object_Declaration
(Loc
,
4177 Defining_Identifier
=> Loop_Id
,
4178 Object_Definition
=> New_Occurrence_Of
(Ltype
, Loc
),
4179 Expression
=> Lo_Val
(First
(Stat
)));
4180 Set_Suppress_Assignment_Checks
(D
);
4183 Make_Block_Statement
(Loc
,
4184 Declarations
=> New_List
(D
),
4185 Handled_Statement_Sequence
=>
4186 Make_Handled_Sequence_Of_Statements
(Loc
,
4187 Statements
=> New_List
(
4188 Make_Loop_Statement
(Loc
,
4189 Statements
=> Stmts
,
4190 End_Label
=> Empty
)))));
4193 end Static_Predicate
;
4195 end Expand_Predicated_Loop
;
4197 ------------------------------
4198 -- Make_Tag_Ctrl_Assignment --
4199 ------------------------------
4201 function Make_Tag_Ctrl_Assignment
(N
: Node_Id
) return List_Id
is
4202 Asn
: constant Node_Id
:= Relocate_Node
(N
);
4203 L
: constant Node_Id
:= Name
(N
);
4204 Loc
: constant Source_Ptr
:= Sloc
(N
);
4205 Res
: constant List_Id
:= New_List
;
4206 T
: constant Entity_Id
:= Underlying_Type
(Etype
(L
));
4208 Comp_Asn
: constant Boolean := Is_Fully_Repped_Tagged_Type
(T
);
4209 Ctrl_Act
: constant Boolean := Needs_Finalization
(T
)
4210 and then not No_Ctrl_Actions
(N
);
4211 Save_Tag
: constant Boolean := Is_Tagged_Type
(T
)
4212 and then not Comp_Asn
4213 and then not No_Ctrl_Actions
(N
)
4214 and then Tagged_Type_Expansion
;
4215 -- Tags are not saved and restored when VM_Target because VM tags are
4216 -- represented implicitly in objects.
4218 Next_Id
: Entity_Id
;
4219 Prev_Id
: Entity_Id
;
4223 -- Finalize the target of the assignment when controlled
4225 -- We have two exceptions here:
4227 -- 1. If we are in an init proc since it is an initialization more
4228 -- than an assignment.
4230 -- 2. If the left-hand side is a temporary that was not initialized
4231 -- (or the parent part of a temporary since it is the case in
4232 -- extension aggregates). Such a temporary does not come from
4233 -- source. We must examine the original node for the prefix, because
4234 -- it may be a component of an entry formal, in which case it has
4235 -- been rewritten and does not appear to come from source either.
4237 -- Case of init proc
4239 if not Ctrl_Act
then
4242 -- The left hand side is an uninitialized temporary object
4244 elsif Nkind
(L
) = N_Type_Conversion
4245 and then Is_Entity_Name
(Expression
(L
))
4246 and then Nkind
(Parent
(Entity
(Expression
(L
)))) =
4247 N_Object_Declaration
4248 and then No_Initialization
(Parent
(Entity
(Expression
(L
))))
4255 (Obj_Ref
=> Duplicate_Subexpr_No_Checks
(L
),
4259 -- Save the Tag in a local variable Tag_Id
4262 Tag_Id
:= Make_Temporary
(Loc
, 'A');
4265 Make_Object_Declaration
(Loc
,
4266 Defining_Identifier
=> Tag_Id
,
4267 Object_Definition
=> New_Occurrence_Of
(RTE
(RE_Tag
), Loc
),
4269 Make_Selected_Component
(Loc
,
4270 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
4272 New_Occurrence_Of
(First_Tag_Component
(T
), Loc
))));
4274 -- Otherwise Tag_Id is not used
4280 -- Save the Prev and Next fields on .NET/JVM. This is not needed on non
4281 -- VM targets since the fields are not part of the object.
4283 if VM_Target
/= No_VM
4284 and then Is_Controlled
(T
)
4286 Prev_Id
:= Make_Temporary
(Loc
, 'P');
4287 Next_Id
:= Make_Temporary
(Loc
, 'N');
4290 -- Pnn : Root_Controlled_Ptr := Root_Controlled (L).Prev;
4293 Make_Object_Declaration
(Loc
,
4294 Defining_Identifier
=> Prev_Id
,
4295 Object_Definition
=>
4296 New_Occurrence_Of
(RTE
(RE_Root_Controlled_Ptr
), Loc
),
4298 Make_Selected_Component
(Loc
,
4300 Unchecked_Convert_To
4301 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4303 Make_Identifier
(Loc
, Name_Prev
))));
4306 -- Nnn : Root_Controlled_Ptr := Root_Controlled (L).Next;
4309 Make_Object_Declaration
(Loc
,
4310 Defining_Identifier
=> Next_Id
,
4311 Object_Definition
=>
4312 New_Occurrence_Of
(RTE
(RE_Root_Controlled_Ptr
), Loc
),
4314 Make_Selected_Component
(Loc
,
4316 Unchecked_Convert_To
4317 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4319 Make_Identifier
(Loc
, Name_Next
))));
4322 -- If the tagged type has a full rep clause, expand the assignment into
4323 -- component-wise assignments. Mark the node as unanalyzed in order to
4324 -- generate the proper code and propagate this scenario by setting a
4325 -- flag to avoid infinite recursion.
4328 Set_Analyzed
(Asn
, False);
4329 Set_Componentwise_Assignment
(Asn
, True);
4332 Append_To
(Res
, Asn
);
4338 Make_Assignment_Statement
(Loc
,
4340 Make_Selected_Component
(Loc
,
4341 Prefix
=> Duplicate_Subexpr_No_Checks
(L
),
4343 New_Occurrence_Of
(First_Tag_Component
(T
), Loc
)),
4344 Expression
=> New_Occurrence_Of
(Tag_Id
, Loc
)));
4347 -- Restore the Prev and Next fields on .NET/JVM
4349 if VM_Target
/= No_VM
4350 and then Is_Controlled
(T
)
4353 -- Root_Controlled (L).Prev := Prev_Id;
4356 Make_Assignment_Statement
(Loc
,
4358 Make_Selected_Component
(Loc
,
4360 Unchecked_Convert_To
4361 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4363 Make_Identifier
(Loc
, Name_Prev
)),
4364 Expression
=> New_Occurrence_Of
(Prev_Id
, Loc
)));
4367 -- Root_Controlled (L).Next := Next_Id;
4370 Make_Assignment_Statement
(Loc
,
4372 Make_Selected_Component
(Loc
,
4374 Unchecked_Convert_To
4375 (RTE
(RE_Root_Controlled
), New_Copy_Tree
(L
)),
4376 Selector_Name
=> Make_Identifier
(Loc
, Name_Next
)),
4377 Expression
=> New_Occurrence_Of
(Next_Id
, Loc
)));
4380 -- Adjust the target after the assignment when controlled (not in the
4381 -- init proc since it is an initialization more than an assignment).
4386 (Obj_Ref
=> Duplicate_Subexpr_Move_Checks
(L
),
4394 -- Could use comment here ???
4396 when RE_Not_Available
=>
4398 end Make_Tag_Ctrl_Assignment
;